Merge from emacs-23
[bpt/emacs.git] / 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. Return in *LEN the length of
1548 the character. This is like STRING_CHAR_AND_LENGTH but never
1549 returns an invalid character. If we find one, we return a `?', but
1550 with the length of the invalid character. */
1551
1552 static INLINE int
1553 string_char_and_length (const unsigned char *str, int *len)
1554 {
1555 int c;
1556
1557 c = STRING_CHAR_AND_LENGTH (str, *len);
1558 if (!CHAR_VALID_P (c, 1))
1559 /* We may not change the length here because other places in Emacs
1560 don't use this function, i.e. they silently accept invalid
1561 characters. */
1562 c = '?';
1563
1564 return c;
1565 }
1566
1567
1568
1569 /* Given a position POS containing a valid character and byte position
1570 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1571
1572 static struct text_pos
1573 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1574 {
1575 xassert (STRINGP (string) && nchars >= 0);
1576
1577 if (STRING_MULTIBYTE (string))
1578 {
1579 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1580 int len;
1581
1582 while (nchars--)
1583 {
1584 string_char_and_length (p, &len);
1585 p += len;
1586 CHARPOS (pos) += 1;
1587 BYTEPOS (pos) += len;
1588 }
1589 }
1590 else
1591 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1592
1593 return pos;
1594 }
1595
1596
1597 /* Value is the text position, i.e. character and byte position,
1598 for character position CHARPOS in STRING. */
1599
1600 static INLINE struct text_pos
1601 string_pos (EMACS_INT charpos, Lisp_Object string)
1602 {
1603 struct text_pos pos;
1604 xassert (STRINGP (string));
1605 xassert (charpos >= 0);
1606 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1607 return pos;
1608 }
1609
1610
1611 /* Value is a text position, i.e. character and byte position, for
1612 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1613 means recognize multibyte characters. */
1614
1615 static struct text_pos
1616 c_string_pos (EMACS_INT charpos, const unsigned char *s, int multibyte_p)
1617 {
1618 struct text_pos pos;
1619
1620 xassert (s != NULL);
1621 xassert (charpos >= 0);
1622
1623 if (multibyte_p)
1624 {
1625 int len;
1626
1627 SET_TEXT_POS (pos, 0, 0);
1628 while (charpos--)
1629 {
1630 string_char_and_length (s, &len);
1631 s += len;
1632 CHARPOS (pos) += 1;
1633 BYTEPOS (pos) += len;
1634 }
1635 }
1636 else
1637 SET_TEXT_POS (pos, charpos, charpos);
1638
1639 return pos;
1640 }
1641
1642
1643 /* Value is the number of characters in C string S. MULTIBYTE_P
1644 non-zero means recognize multibyte characters. */
1645
1646 static EMACS_INT
1647 number_of_chars (const unsigned char *s, int multibyte_p)
1648 {
1649 EMACS_INT nchars;
1650
1651 if (multibyte_p)
1652 {
1653 EMACS_INT rest = strlen (s);
1654 int len;
1655 unsigned char *p = (unsigned char *) s;
1656
1657 for (nchars = 0; rest > 0; ++nchars)
1658 {
1659 string_char_and_length (p, &len);
1660 rest -= len, p += len;
1661 }
1662 }
1663 else
1664 nchars = strlen (s);
1665
1666 return nchars;
1667 }
1668
1669
1670 /* Compute byte position NEWPOS->bytepos corresponding to
1671 NEWPOS->charpos. POS is a known position in string STRING.
1672 NEWPOS->charpos must be >= POS.charpos. */
1673
1674 static void
1675 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1676 {
1677 xassert (STRINGP (string));
1678 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1679
1680 if (STRING_MULTIBYTE (string))
1681 *newpos = string_pos_nchars_ahead (pos, string,
1682 CHARPOS (*newpos) - CHARPOS (pos));
1683 else
1684 BYTEPOS (*newpos) = CHARPOS (*newpos);
1685 }
1686
1687 /* EXPORT:
1688 Return an estimation of the pixel height of mode or header lines on
1689 frame F. FACE_ID specifies what line's height to estimate. */
1690
1691 int
1692 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1693 {
1694 #ifdef HAVE_WINDOW_SYSTEM
1695 if (FRAME_WINDOW_P (f))
1696 {
1697 int height = FONT_HEIGHT (FRAME_FONT (f));
1698
1699 /* This function is called so early when Emacs starts that the face
1700 cache and mode line face are not yet initialized. */
1701 if (FRAME_FACE_CACHE (f))
1702 {
1703 struct face *face = FACE_FROM_ID (f, face_id);
1704 if (face)
1705 {
1706 if (face->font)
1707 height = FONT_HEIGHT (face->font);
1708 if (face->box_line_width > 0)
1709 height += 2 * face->box_line_width;
1710 }
1711 }
1712
1713 return height;
1714 }
1715 #endif
1716
1717 return 1;
1718 }
1719
1720 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1721 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1722 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1723 not force the value into range. */
1724
1725 void
1726 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1727 int *x, int *y, NativeRectangle *bounds, int noclip)
1728 {
1729
1730 #ifdef HAVE_WINDOW_SYSTEM
1731 if (FRAME_WINDOW_P (f))
1732 {
1733 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1734 even for negative values. */
1735 if (pix_x < 0)
1736 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1737 if (pix_y < 0)
1738 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1739
1740 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1741 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1742
1743 if (bounds)
1744 STORE_NATIVE_RECT (*bounds,
1745 FRAME_COL_TO_PIXEL_X (f, pix_x),
1746 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1747 FRAME_COLUMN_WIDTH (f) - 1,
1748 FRAME_LINE_HEIGHT (f) - 1);
1749
1750 if (!noclip)
1751 {
1752 if (pix_x < 0)
1753 pix_x = 0;
1754 else if (pix_x > FRAME_TOTAL_COLS (f))
1755 pix_x = FRAME_TOTAL_COLS (f);
1756
1757 if (pix_y < 0)
1758 pix_y = 0;
1759 else if (pix_y > FRAME_LINES (f))
1760 pix_y = FRAME_LINES (f);
1761 }
1762 }
1763 #endif
1764
1765 *x = pix_x;
1766 *y = pix_y;
1767 }
1768
1769
1770 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1771 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1772 can't tell the positions because W's display is not up to date,
1773 return 0. */
1774
1775 int
1776 glyph_to_pixel_coords (struct window *w, int hpos, int vpos,
1777 int *frame_x, int *frame_y)
1778 {
1779 #ifdef HAVE_WINDOW_SYSTEM
1780 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1781 {
1782 int success_p;
1783
1784 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1785 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1786
1787 if (display_completed)
1788 {
1789 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1790 struct glyph *glyph = row->glyphs[TEXT_AREA];
1791 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1792
1793 hpos = row->x;
1794 vpos = row->y;
1795 while (glyph < end)
1796 {
1797 hpos += glyph->pixel_width;
1798 ++glyph;
1799 }
1800
1801 /* If first glyph is partially visible, its first visible position is still 0. */
1802 if (hpos < 0)
1803 hpos = 0;
1804
1805 success_p = 1;
1806 }
1807 else
1808 {
1809 hpos = vpos = 0;
1810 success_p = 0;
1811 }
1812
1813 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1814 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1815 return success_p;
1816 }
1817 #endif
1818
1819 *frame_x = hpos;
1820 *frame_y = vpos;
1821 return 1;
1822 }
1823
1824
1825 /* Find the glyph under window-relative coordinates X/Y in window W.
1826 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1827 strings. Return in *HPOS and *VPOS the row and column number of
1828 the glyph found. Return in *AREA the glyph area containing X.
1829 Value is a pointer to the glyph found or null if X/Y is not on
1830 text, or we can't tell because W's current matrix is not up to
1831 date. */
1832
1833 static
1834 struct glyph *
1835 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1836 int *dx, int *dy, int *area)
1837 {
1838 struct glyph *glyph, *end;
1839 struct glyph_row *row = NULL;
1840 int x0, i;
1841
1842 /* Find row containing Y. Give up if some row is not enabled. */
1843 for (i = 0; i < w->current_matrix->nrows; ++i)
1844 {
1845 row = MATRIX_ROW (w->current_matrix, i);
1846 if (!row->enabled_p)
1847 return NULL;
1848 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1849 break;
1850 }
1851
1852 *vpos = i;
1853 *hpos = 0;
1854
1855 /* Give up if Y is not in the window. */
1856 if (i == w->current_matrix->nrows)
1857 return NULL;
1858
1859 /* Get the glyph area containing X. */
1860 if (w->pseudo_window_p)
1861 {
1862 *area = TEXT_AREA;
1863 x0 = 0;
1864 }
1865 else
1866 {
1867 if (x < window_box_left_offset (w, TEXT_AREA))
1868 {
1869 *area = LEFT_MARGIN_AREA;
1870 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1871 }
1872 else if (x < window_box_right_offset (w, TEXT_AREA))
1873 {
1874 *area = TEXT_AREA;
1875 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1876 }
1877 else
1878 {
1879 *area = RIGHT_MARGIN_AREA;
1880 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1881 }
1882 }
1883
1884 /* Find glyph containing X. */
1885 glyph = row->glyphs[*area];
1886 end = glyph + row->used[*area];
1887 x -= x0;
1888 while (glyph < end && x >= glyph->pixel_width)
1889 {
1890 x -= glyph->pixel_width;
1891 ++glyph;
1892 }
1893
1894 if (glyph == end)
1895 return NULL;
1896
1897 if (dx)
1898 {
1899 *dx = x;
1900 *dy = y - (row->y + row->ascent - glyph->ascent);
1901 }
1902
1903 *hpos = glyph - row->glyphs[*area];
1904 return glyph;
1905 }
1906
1907 /* EXPORT:
1908 Convert frame-relative x/y to coordinates relative to window W.
1909 Takes pseudo-windows into account. */
1910
1911 void
1912 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1913 {
1914 if (w->pseudo_window_p)
1915 {
1916 /* A pseudo-window is always full-width, and starts at the
1917 left edge of the frame, plus a frame border. */
1918 struct frame *f = XFRAME (w->frame);
1919 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1920 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1921 }
1922 else
1923 {
1924 *x -= WINDOW_LEFT_EDGE_X (w);
1925 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1926 }
1927 }
1928
1929 #ifdef HAVE_WINDOW_SYSTEM
1930
1931 /* EXPORT:
1932 Return in RECTS[] at most N clipping rectangles for glyph string S.
1933 Return the number of stored rectangles. */
1934
1935 int
1936 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1937 {
1938 XRectangle r;
1939
1940 if (n <= 0)
1941 return 0;
1942
1943 if (s->row->full_width_p)
1944 {
1945 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1946 r.x = WINDOW_LEFT_EDGE_X (s->w);
1947 r.width = WINDOW_TOTAL_WIDTH (s->w);
1948
1949 /* Unless displaying a mode or menu bar line, which are always
1950 fully visible, clip to the visible part of the row. */
1951 if (s->w->pseudo_window_p)
1952 r.height = s->row->visible_height;
1953 else
1954 r.height = s->height;
1955 }
1956 else
1957 {
1958 /* This is a text line that may be partially visible. */
1959 r.x = window_box_left (s->w, s->area);
1960 r.width = window_box_width (s->w, s->area);
1961 r.height = s->row->visible_height;
1962 }
1963
1964 if (s->clip_head)
1965 if (r.x < s->clip_head->x)
1966 {
1967 if (r.width >= s->clip_head->x - r.x)
1968 r.width -= s->clip_head->x - r.x;
1969 else
1970 r.width = 0;
1971 r.x = s->clip_head->x;
1972 }
1973 if (s->clip_tail)
1974 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1975 {
1976 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1977 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1978 else
1979 r.width = 0;
1980 }
1981
1982 /* If S draws overlapping rows, it's sufficient to use the top and
1983 bottom of the window for clipping because this glyph string
1984 intentionally draws over other lines. */
1985 if (s->for_overlaps)
1986 {
1987 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1988 r.height = window_text_bottom_y (s->w) - r.y;
1989
1990 /* Alas, the above simple strategy does not work for the
1991 environments with anti-aliased text: if the same text is
1992 drawn onto the same place multiple times, it gets thicker.
1993 If the overlap we are processing is for the erased cursor, we
1994 take the intersection with the rectagle of the cursor. */
1995 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1996 {
1997 XRectangle rc, r_save = r;
1998
1999 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2000 rc.y = s->w->phys_cursor.y;
2001 rc.width = s->w->phys_cursor_width;
2002 rc.height = s->w->phys_cursor_height;
2003
2004 x_intersect_rectangles (&r_save, &rc, &r);
2005 }
2006 }
2007 else
2008 {
2009 /* Don't use S->y for clipping because it doesn't take partially
2010 visible lines into account. For example, it can be negative for
2011 partially visible lines at the top of a window. */
2012 if (!s->row->full_width_p
2013 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2014 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2015 else
2016 r.y = max (0, s->row->y);
2017 }
2018
2019 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2020
2021 /* If drawing the cursor, don't let glyph draw outside its
2022 advertised boundaries. Cleartype does this under some circumstances. */
2023 if (s->hl == DRAW_CURSOR)
2024 {
2025 struct glyph *glyph = s->first_glyph;
2026 int height, max_y;
2027
2028 if (s->x > r.x)
2029 {
2030 r.width -= s->x - r.x;
2031 r.x = s->x;
2032 }
2033 r.width = min (r.width, glyph->pixel_width);
2034
2035 /* If r.y is below window bottom, ensure that we still see a cursor. */
2036 height = min (glyph->ascent + glyph->descent,
2037 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2038 max_y = window_text_bottom_y (s->w) - height;
2039 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2040 if (s->ybase - glyph->ascent > max_y)
2041 {
2042 r.y = max_y;
2043 r.height = height;
2044 }
2045 else
2046 {
2047 /* Don't draw cursor glyph taller than our actual glyph. */
2048 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2049 if (height < r.height)
2050 {
2051 max_y = r.y + r.height;
2052 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2053 r.height = min (max_y - r.y, height);
2054 }
2055 }
2056 }
2057
2058 if (s->row->clip)
2059 {
2060 XRectangle r_save = r;
2061
2062 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2063 r.width = 0;
2064 }
2065
2066 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2067 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2068 {
2069 #ifdef CONVERT_FROM_XRECT
2070 CONVERT_FROM_XRECT (r, *rects);
2071 #else
2072 *rects = r;
2073 #endif
2074 return 1;
2075 }
2076 else
2077 {
2078 /* If we are processing overlapping and allowed to return
2079 multiple clipping rectangles, we exclude the row of the glyph
2080 string from the clipping rectangle. This is to avoid drawing
2081 the same text on the environment with anti-aliasing. */
2082 #ifdef CONVERT_FROM_XRECT
2083 XRectangle rs[2];
2084 #else
2085 XRectangle *rs = rects;
2086 #endif
2087 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2088
2089 if (s->for_overlaps & OVERLAPS_PRED)
2090 {
2091 rs[i] = r;
2092 if (r.y + r.height > row_y)
2093 {
2094 if (r.y < row_y)
2095 rs[i].height = row_y - r.y;
2096 else
2097 rs[i].height = 0;
2098 }
2099 i++;
2100 }
2101 if (s->for_overlaps & OVERLAPS_SUCC)
2102 {
2103 rs[i] = r;
2104 if (r.y < row_y + s->row->visible_height)
2105 {
2106 if (r.y + r.height > row_y + s->row->visible_height)
2107 {
2108 rs[i].y = row_y + s->row->visible_height;
2109 rs[i].height = r.y + r.height - rs[i].y;
2110 }
2111 else
2112 rs[i].height = 0;
2113 }
2114 i++;
2115 }
2116
2117 n = i;
2118 #ifdef CONVERT_FROM_XRECT
2119 for (i = 0; i < n; i++)
2120 CONVERT_FROM_XRECT (rs[i], rects[i]);
2121 #endif
2122 return n;
2123 }
2124 }
2125
2126 /* EXPORT:
2127 Return in *NR the clipping rectangle for glyph string S. */
2128
2129 void
2130 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2131 {
2132 get_glyph_string_clip_rects (s, nr, 1);
2133 }
2134
2135
2136 /* EXPORT:
2137 Return the position and height of the phys cursor in window W.
2138 Set w->phys_cursor_width to width of phys cursor.
2139 */
2140
2141 void
2142 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2143 struct glyph *glyph, int *xp, int *yp, int *heightp)
2144 {
2145 struct frame *f = XFRAME (WINDOW_FRAME (w));
2146 int x, y, wd, h, h0, y0;
2147
2148 /* Compute the width of the rectangle to draw. If on a stretch
2149 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2150 rectangle as wide as the glyph, but use a canonical character
2151 width instead. */
2152 wd = glyph->pixel_width - 1;
2153 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2154 wd++; /* Why? */
2155 #endif
2156
2157 x = w->phys_cursor.x;
2158 if (x < 0)
2159 {
2160 wd += x;
2161 x = 0;
2162 }
2163
2164 if (glyph->type == STRETCH_GLYPH
2165 && !x_stretch_cursor_p)
2166 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2167 w->phys_cursor_width = wd;
2168
2169 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2170
2171 /* If y is below window bottom, ensure that we still see a cursor. */
2172 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2173
2174 h = max (h0, glyph->ascent + glyph->descent);
2175 h0 = min (h0, glyph->ascent + glyph->descent);
2176
2177 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2178 if (y < y0)
2179 {
2180 h = max (h - (y0 - y) + 1, h0);
2181 y = y0 - 1;
2182 }
2183 else
2184 {
2185 y0 = window_text_bottom_y (w) - h0;
2186 if (y > y0)
2187 {
2188 h += y - y0;
2189 y = y0;
2190 }
2191 }
2192
2193 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2194 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2195 *heightp = h;
2196 }
2197
2198 /*
2199 * Remember which glyph the mouse is over.
2200 */
2201
2202 void
2203 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2204 {
2205 Lisp_Object window;
2206 struct window *w;
2207 struct glyph_row *r, *gr, *end_row;
2208 enum window_part part;
2209 enum glyph_row_area area;
2210 int x, y, width, height;
2211
2212 /* Try to determine frame pixel position and size of the glyph under
2213 frame pixel coordinates X/Y on frame F. */
2214
2215 if (!f->glyphs_initialized_p
2216 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2217 NILP (window)))
2218 {
2219 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2220 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2221 goto virtual_glyph;
2222 }
2223
2224 w = XWINDOW (window);
2225 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2226 height = WINDOW_FRAME_LINE_HEIGHT (w);
2227
2228 x = window_relative_x_coord (w, part, gx);
2229 y = gy - WINDOW_TOP_EDGE_Y (w);
2230
2231 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2232 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2233
2234 if (w->pseudo_window_p)
2235 {
2236 area = TEXT_AREA;
2237 part = ON_MODE_LINE; /* Don't adjust margin. */
2238 goto text_glyph;
2239 }
2240
2241 switch (part)
2242 {
2243 case ON_LEFT_MARGIN:
2244 area = LEFT_MARGIN_AREA;
2245 goto text_glyph;
2246
2247 case ON_RIGHT_MARGIN:
2248 area = RIGHT_MARGIN_AREA;
2249 goto text_glyph;
2250
2251 case ON_HEADER_LINE:
2252 case ON_MODE_LINE:
2253 gr = (part == ON_HEADER_LINE
2254 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2255 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2256 gy = gr->y;
2257 area = TEXT_AREA;
2258 goto text_glyph_row_found;
2259
2260 case ON_TEXT:
2261 area = TEXT_AREA;
2262
2263 text_glyph:
2264 gr = 0; gy = 0;
2265 for (; r <= end_row && r->enabled_p; ++r)
2266 if (r->y + r->height > y)
2267 {
2268 gr = r; gy = r->y;
2269 break;
2270 }
2271
2272 text_glyph_row_found:
2273 if (gr && gy <= y)
2274 {
2275 struct glyph *g = gr->glyphs[area];
2276 struct glyph *end = g + gr->used[area];
2277
2278 height = gr->height;
2279 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2280 if (gx + g->pixel_width > x)
2281 break;
2282
2283 if (g < end)
2284 {
2285 if (g->type == IMAGE_GLYPH)
2286 {
2287 /* Don't remember when mouse is over image, as
2288 image may have hot-spots. */
2289 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2290 return;
2291 }
2292 width = g->pixel_width;
2293 }
2294 else
2295 {
2296 /* Use nominal char spacing at end of line. */
2297 x -= gx;
2298 gx += (x / width) * width;
2299 }
2300
2301 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2302 gx += window_box_left_offset (w, area);
2303 }
2304 else
2305 {
2306 /* Use nominal line height at end of window. */
2307 gx = (x / width) * width;
2308 y -= gy;
2309 gy += (y / height) * height;
2310 }
2311 break;
2312
2313 case ON_LEFT_FRINGE:
2314 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2315 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2316 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2317 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2318 goto row_glyph;
2319
2320 case ON_RIGHT_FRINGE:
2321 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2322 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2323 : window_box_right_offset (w, TEXT_AREA));
2324 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2325 goto row_glyph;
2326
2327 case ON_SCROLL_BAR:
2328 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2329 ? 0
2330 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2331 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2332 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2333 : 0)));
2334 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2335
2336 row_glyph:
2337 gr = 0, gy = 0;
2338 for (; r <= end_row && r->enabled_p; ++r)
2339 if (r->y + r->height > y)
2340 {
2341 gr = r; gy = r->y;
2342 break;
2343 }
2344
2345 if (gr && gy <= y)
2346 height = gr->height;
2347 else
2348 {
2349 /* Use nominal line height at end of window. */
2350 y -= gy;
2351 gy += (y / height) * height;
2352 }
2353 break;
2354
2355 default:
2356 ;
2357 virtual_glyph:
2358 /* If there is no glyph under the mouse, then we divide the screen
2359 into a grid of the smallest glyph in the frame, and use that
2360 as our "glyph". */
2361
2362 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2363 round down even for negative values. */
2364 if (gx < 0)
2365 gx -= width - 1;
2366 if (gy < 0)
2367 gy -= height - 1;
2368
2369 gx = (gx / width) * width;
2370 gy = (gy / height) * height;
2371
2372 goto store_rect;
2373 }
2374
2375 gx += WINDOW_LEFT_EDGE_X (w);
2376 gy += WINDOW_TOP_EDGE_Y (w);
2377
2378 store_rect:
2379 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2380
2381 /* Visible feedback for debugging. */
2382 #if 0
2383 #if HAVE_X_WINDOWS
2384 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2385 f->output_data.x->normal_gc,
2386 gx, gy, width, height);
2387 #endif
2388 #endif
2389 }
2390
2391
2392 #endif /* HAVE_WINDOW_SYSTEM */
2393
2394 \f
2395 /***********************************************************************
2396 Lisp form evaluation
2397 ***********************************************************************/
2398
2399 /* Error handler for safe_eval and safe_call. */
2400
2401 static Lisp_Object
2402 safe_eval_handler (Lisp_Object arg)
2403 {
2404 add_to_log ("Error during redisplay: %s", arg, Qnil);
2405 return Qnil;
2406 }
2407
2408
2409 /* Evaluate SEXPR and return the result, or nil if something went
2410 wrong. Prevent redisplay during the evaluation. */
2411
2412 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2413 Return the result, or nil if something went wrong. Prevent
2414 redisplay during the evaluation. */
2415
2416 Lisp_Object
2417 safe_call (int nargs, Lisp_Object *args)
2418 {
2419 Lisp_Object val;
2420
2421 if (inhibit_eval_during_redisplay)
2422 val = Qnil;
2423 else
2424 {
2425 int count = SPECPDL_INDEX ();
2426 struct gcpro gcpro1;
2427
2428 GCPRO1 (args[0]);
2429 gcpro1.nvars = nargs;
2430 specbind (Qinhibit_redisplay, Qt);
2431 /* Use Qt to ensure debugger does not run,
2432 so there is no possibility of wanting to redisplay. */
2433 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2434 safe_eval_handler);
2435 UNGCPRO;
2436 val = unbind_to (count, val);
2437 }
2438
2439 return val;
2440 }
2441
2442
2443 /* Call function FN with one argument ARG.
2444 Return the result, or nil if something went wrong. */
2445
2446 Lisp_Object
2447 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2448 {
2449 Lisp_Object args[2];
2450 args[0] = fn;
2451 args[1] = arg;
2452 return safe_call (2, args);
2453 }
2454
2455 static Lisp_Object Qeval;
2456
2457 Lisp_Object
2458 safe_eval (Lisp_Object sexpr)
2459 {
2460 return safe_call1 (Qeval, sexpr);
2461 }
2462
2463 /* Call function FN with one argument ARG.
2464 Return the result, or nil if something went wrong. */
2465
2466 Lisp_Object
2467 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2468 {
2469 Lisp_Object args[3];
2470 args[0] = fn;
2471 args[1] = arg1;
2472 args[2] = arg2;
2473 return safe_call (3, args);
2474 }
2475
2476
2477 \f
2478 /***********************************************************************
2479 Debugging
2480 ***********************************************************************/
2481
2482 #if 0
2483
2484 /* Define CHECK_IT to perform sanity checks on iterators.
2485 This is for debugging. It is too slow to do unconditionally. */
2486
2487 static void
2488 check_it (it)
2489 struct it *it;
2490 {
2491 if (it->method == GET_FROM_STRING)
2492 {
2493 xassert (STRINGP (it->string));
2494 xassert (IT_STRING_CHARPOS (*it) >= 0);
2495 }
2496 else
2497 {
2498 xassert (IT_STRING_CHARPOS (*it) < 0);
2499 if (it->method == GET_FROM_BUFFER)
2500 {
2501 /* Check that character and byte positions agree. */
2502 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2503 }
2504 }
2505
2506 if (it->dpvec)
2507 xassert (it->current.dpvec_index >= 0);
2508 else
2509 xassert (it->current.dpvec_index < 0);
2510 }
2511
2512 #define CHECK_IT(IT) check_it ((IT))
2513
2514 #else /* not 0 */
2515
2516 #define CHECK_IT(IT) (void) 0
2517
2518 #endif /* not 0 */
2519
2520
2521 #if GLYPH_DEBUG
2522
2523 /* Check that the window end of window W is what we expect it
2524 to be---the last row in the current matrix displaying text. */
2525
2526 static void
2527 check_window_end (w)
2528 struct window *w;
2529 {
2530 if (!MINI_WINDOW_P (w)
2531 && !NILP (w->window_end_valid))
2532 {
2533 struct glyph_row *row;
2534 xassert ((row = MATRIX_ROW (w->current_matrix,
2535 XFASTINT (w->window_end_vpos)),
2536 !row->enabled_p
2537 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2538 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2539 }
2540 }
2541
2542 #define CHECK_WINDOW_END(W) check_window_end ((W))
2543
2544 #else /* not GLYPH_DEBUG */
2545
2546 #define CHECK_WINDOW_END(W) (void) 0
2547
2548 #endif /* not GLYPH_DEBUG */
2549
2550
2551 \f
2552 /***********************************************************************
2553 Iterator initialization
2554 ***********************************************************************/
2555
2556 /* Initialize IT for displaying current_buffer in window W, starting
2557 at character position CHARPOS. CHARPOS < 0 means that no buffer
2558 position is specified which is useful when the iterator is assigned
2559 a position later. BYTEPOS is the byte position corresponding to
2560 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2561
2562 If ROW is not null, calls to produce_glyphs with IT as parameter
2563 will produce glyphs in that row.
2564
2565 BASE_FACE_ID is the id of a base face to use. It must be one of
2566 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2567 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2568 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2569
2570 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2571 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2572 will be initialized to use the corresponding mode line glyph row of
2573 the desired matrix of W. */
2574
2575 void
2576 init_iterator (struct it *it, struct window *w,
2577 EMACS_INT charpos, EMACS_INT bytepos,
2578 struct glyph_row *row, enum face_id base_face_id)
2579 {
2580 int highlight_region_p;
2581 enum face_id remapped_base_face_id = base_face_id;
2582
2583 /* Some precondition checks. */
2584 xassert (w != NULL && it != NULL);
2585 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2586 && charpos <= ZV));
2587
2588 /* If face attributes have been changed since the last redisplay,
2589 free realized faces now because they depend on face definitions
2590 that might have changed. Don't free faces while there might be
2591 desired matrices pending which reference these faces. */
2592 if (face_change_count && !inhibit_free_realized_faces)
2593 {
2594 face_change_count = 0;
2595 free_all_realized_faces (Qnil);
2596 }
2597
2598 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2599 if (! NILP (Vface_remapping_alist))
2600 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2601
2602 /* Use one of the mode line rows of W's desired matrix if
2603 appropriate. */
2604 if (row == NULL)
2605 {
2606 if (base_face_id == MODE_LINE_FACE_ID
2607 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2608 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2609 else if (base_face_id == HEADER_LINE_FACE_ID)
2610 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2611 }
2612
2613 /* Clear IT. */
2614 memset (it, 0, sizeof *it);
2615 it->current.overlay_string_index = -1;
2616 it->current.dpvec_index = -1;
2617 it->base_face_id = remapped_base_face_id;
2618 it->string = Qnil;
2619 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2620
2621 /* The window in which we iterate over current_buffer: */
2622 XSETWINDOW (it->window, w);
2623 it->w = w;
2624 it->f = XFRAME (w->frame);
2625
2626 it->cmp_it.id = -1;
2627
2628 /* Extra space between lines (on window systems only). */
2629 if (base_face_id == DEFAULT_FACE_ID
2630 && FRAME_WINDOW_P (it->f))
2631 {
2632 if (NATNUMP (current_buffer->extra_line_spacing))
2633 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2634 else if (FLOATP (current_buffer->extra_line_spacing))
2635 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2636 * FRAME_LINE_HEIGHT (it->f));
2637 else if (it->f->extra_line_spacing > 0)
2638 it->extra_line_spacing = it->f->extra_line_spacing;
2639 it->max_extra_line_spacing = 0;
2640 }
2641
2642 /* If realized faces have been removed, e.g. because of face
2643 attribute changes of named faces, recompute them. When running
2644 in batch mode, the face cache of the initial frame is null. If
2645 we happen to get called, make a dummy face cache. */
2646 if (FRAME_FACE_CACHE (it->f) == NULL)
2647 init_frame_faces (it->f);
2648 if (FRAME_FACE_CACHE (it->f)->used == 0)
2649 recompute_basic_faces (it->f);
2650
2651 /* Current value of the `slice', `space-width', and 'height' properties. */
2652 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2653 it->space_width = Qnil;
2654 it->font_height = Qnil;
2655 it->override_ascent = -1;
2656
2657 /* Are control characters displayed as `^C'? */
2658 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2659
2660 /* -1 means everything between a CR and the following line end
2661 is invisible. >0 means lines indented more than this value are
2662 invisible. */
2663 it->selective = (INTEGERP (current_buffer->selective_display)
2664 ? XFASTINT (current_buffer->selective_display)
2665 : (!NILP (current_buffer->selective_display)
2666 ? -1 : 0));
2667 it->selective_display_ellipsis_p
2668 = !NILP (current_buffer->selective_display_ellipses);
2669
2670 /* Display table to use. */
2671 it->dp = window_display_table (w);
2672
2673 /* Are multibyte characters enabled in current_buffer? */
2674 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2675
2676 /* Do we need to reorder bidirectional text? Not if this is a
2677 unibyte buffer: by definition, none of the single-byte characters
2678 are strong R2L, so no reordering is needed. And bidi.c doesn't
2679 support unibyte buffers anyway. */
2680 it->bidi_p
2681 = !NILP (current_buffer->bidi_display_reordering) && it->multibyte_p;
2682
2683 /* Non-zero if we should highlight the region. */
2684 highlight_region_p
2685 = (!NILP (Vtransient_mark_mode)
2686 && !NILP (current_buffer->mark_active)
2687 && XMARKER (current_buffer->mark)->buffer != 0);
2688
2689 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2690 start and end of a visible region in window IT->w. Set both to
2691 -1 to indicate no region. */
2692 if (highlight_region_p
2693 /* Maybe highlight only in selected window. */
2694 && (/* Either show region everywhere. */
2695 highlight_nonselected_windows
2696 /* Or show region in the selected window. */
2697 || w == XWINDOW (selected_window)
2698 /* Or show the region if we are in the mini-buffer and W is
2699 the window the mini-buffer refers to. */
2700 || (MINI_WINDOW_P (XWINDOW (selected_window))
2701 && WINDOWP (minibuf_selected_window)
2702 && w == XWINDOW (minibuf_selected_window))))
2703 {
2704 EMACS_INT charpos = marker_position (current_buffer->mark);
2705 it->region_beg_charpos = min (PT, charpos);
2706 it->region_end_charpos = max (PT, charpos);
2707 }
2708 else
2709 it->region_beg_charpos = it->region_end_charpos = -1;
2710
2711 /* Get the position at which the redisplay_end_trigger hook should
2712 be run, if it is to be run at all. */
2713 if (MARKERP (w->redisplay_end_trigger)
2714 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2715 it->redisplay_end_trigger_charpos
2716 = marker_position (w->redisplay_end_trigger);
2717 else if (INTEGERP (w->redisplay_end_trigger))
2718 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2719
2720 /* Correct bogus values of tab_width. */
2721 it->tab_width = XINT (current_buffer->tab_width);
2722 if (it->tab_width <= 0 || it->tab_width > 1000)
2723 it->tab_width = 8;
2724
2725 /* Are lines in the display truncated? */
2726 if (base_face_id != DEFAULT_FACE_ID
2727 || XINT (it->w->hscroll)
2728 || (! WINDOW_FULL_WIDTH_P (it->w)
2729 && ((!NILP (Vtruncate_partial_width_windows)
2730 && !INTEGERP (Vtruncate_partial_width_windows))
2731 || (INTEGERP (Vtruncate_partial_width_windows)
2732 && (WINDOW_TOTAL_COLS (it->w)
2733 < XINT (Vtruncate_partial_width_windows))))))
2734 it->line_wrap = TRUNCATE;
2735 else if (NILP (current_buffer->truncate_lines))
2736 it->line_wrap = NILP (current_buffer->word_wrap)
2737 ? WINDOW_WRAP : WORD_WRAP;
2738 else
2739 it->line_wrap = TRUNCATE;
2740
2741 /* Get dimensions of truncation and continuation glyphs. These are
2742 displayed as fringe bitmaps under X, so we don't need them for such
2743 frames. */
2744 if (!FRAME_WINDOW_P (it->f))
2745 {
2746 if (it->line_wrap == TRUNCATE)
2747 {
2748 /* We will need the truncation glyph. */
2749 xassert (it->glyph_row == NULL);
2750 produce_special_glyphs (it, IT_TRUNCATION);
2751 it->truncation_pixel_width = it->pixel_width;
2752 }
2753 else
2754 {
2755 /* We will need the continuation glyph. */
2756 xassert (it->glyph_row == NULL);
2757 produce_special_glyphs (it, IT_CONTINUATION);
2758 it->continuation_pixel_width = it->pixel_width;
2759 }
2760
2761 /* Reset these values to zero because the produce_special_glyphs
2762 above has changed them. */
2763 it->pixel_width = it->ascent = it->descent = 0;
2764 it->phys_ascent = it->phys_descent = 0;
2765 }
2766
2767 /* Set this after getting the dimensions of truncation and
2768 continuation glyphs, so that we don't produce glyphs when calling
2769 produce_special_glyphs, above. */
2770 it->glyph_row = row;
2771 it->area = TEXT_AREA;
2772
2773 /* Forget any previous info about this row being reversed. */
2774 if (it->glyph_row)
2775 it->glyph_row->reversed_p = 0;
2776
2777 /* Get the dimensions of the display area. The display area
2778 consists of the visible window area plus a horizontally scrolled
2779 part to the left of the window. All x-values are relative to the
2780 start of this total display area. */
2781 if (base_face_id != DEFAULT_FACE_ID)
2782 {
2783 /* Mode lines, menu bar in terminal frames. */
2784 it->first_visible_x = 0;
2785 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2786 }
2787 else
2788 {
2789 it->first_visible_x
2790 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2791 it->last_visible_x = (it->first_visible_x
2792 + window_box_width (w, TEXT_AREA));
2793
2794 /* If we truncate lines, leave room for the truncator glyph(s) at
2795 the right margin. Otherwise, leave room for the continuation
2796 glyph(s). Truncation and continuation glyphs are not inserted
2797 for window-based redisplay. */
2798 if (!FRAME_WINDOW_P (it->f))
2799 {
2800 if (it->line_wrap == TRUNCATE)
2801 it->last_visible_x -= it->truncation_pixel_width;
2802 else
2803 it->last_visible_x -= it->continuation_pixel_width;
2804 }
2805
2806 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2807 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2808 }
2809
2810 /* Leave room for a border glyph. */
2811 if (!FRAME_WINDOW_P (it->f)
2812 && !WINDOW_RIGHTMOST_P (it->w))
2813 it->last_visible_x -= 1;
2814
2815 it->last_visible_y = window_text_bottom_y (w);
2816
2817 /* For mode lines and alike, arrange for the first glyph having a
2818 left box line if the face specifies a box. */
2819 if (base_face_id != DEFAULT_FACE_ID)
2820 {
2821 struct face *face;
2822
2823 it->face_id = remapped_base_face_id;
2824
2825 /* If we have a boxed mode line, make the first character appear
2826 with a left box line. */
2827 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2828 if (face->box != FACE_NO_BOX)
2829 it->start_of_box_run_p = 1;
2830 }
2831
2832 /* If we are to reorder bidirectional text, init the bidi
2833 iterator. */
2834 if (it->bidi_p)
2835 {
2836 /* Note the paragraph direction that this buffer wants to
2837 use. */
2838 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2839 it->paragraph_embedding = L2R;
2840 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2841 it->paragraph_embedding = R2L;
2842 else
2843 it->paragraph_embedding = NEUTRAL_DIR;
2844 bidi_init_it (charpos, bytepos, &it->bidi_it);
2845 }
2846
2847 /* If a buffer position was specified, set the iterator there,
2848 getting overlays and face properties from that position. */
2849 if (charpos >= BUF_BEG (current_buffer))
2850 {
2851 it->end_charpos = ZV;
2852 it->face_id = -1;
2853 IT_CHARPOS (*it) = charpos;
2854
2855 /* Compute byte position if not specified. */
2856 if (bytepos < charpos)
2857 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2858 else
2859 IT_BYTEPOS (*it) = bytepos;
2860
2861 it->start = it->current;
2862
2863 /* Compute faces etc. */
2864 reseat (it, it->current.pos, 1);
2865 }
2866
2867 CHECK_IT (it);
2868 }
2869
2870
2871 /* Initialize IT for the display of window W with window start POS. */
2872
2873 void
2874 start_display (struct it *it, struct window *w, struct text_pos pos)
2875 {
2876 struct glyph_row *row;
2877 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2878
2879 row = w->desired_matrix->rows + first_vpos;
2880 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2881 it->first_vpos = first_vpos;
2882
2883 /* Don't reseat to previous visible line start if current start
2884 position is in a string or image. */
2885 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2886 {
2887 int start_at_line_beg_p;
2888 int first_y = it->current_y;
2889
2890 /* If window start is not at a line start, skip forward to POS to
2891 get the correct continuation lines width. */
2892 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2893 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2894 if (!start_at_line_beg_p)
2895 {
2896 int new_x;
2897
2898 reseat_at_previous_visible_line_start (it);
2899 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2900
2901 new_x = it->current_x + it->pixel_width;
2902
2903 /* If lines are continued, this line may end in the middle
2904 of a multi-glyph character (e.g. a control character
2905 displayed as \003, or in the middle of an overlay
2906 string). In this case move_it_to above will not have
2907 taken us to the start of the continuation line but to the
2908 end of the continued line. */
2909 if (it->current_x > 0
2910 && it->line_wrap != TRUNCATE /* Lines are continued. */
2911 && (/* And glyph doesn't fit on the line. */
2912 new_x > it->last_visible_x
2913 /* Or it fits exactly and we're on a window
2914 system frame. */
2915 || (new_x == it->last_visible_x
2916 && FRAME_WINDOW_P (it->f))))
2917 {
2918 if (it->current.dpvec_index >= 0
2919 || it->current.overlay_string_index >= 0)
2920 {
2921 set_iterator_to_next (it, 1);
2922 move_it_in_display_line_to (it, -1, -1, 0);
2923 }
2924
2925 it->continuation_lines_width += it->current_x;
2926 }
2927
2928 /* We're starting a new display line, not affected by the
2929 height of the continued line, so clear the appropriate
2930 fields in the iterator structure. */
2931 it->max_ascent = it->max_descent = 0;
2932 it->max_phys_ascent = it->max_phys_descent = 0;
2933
2934 it->current_y = first_y;
2935 it->vpos = 0;
2936 it->current_x = it->hpos = 0;
2937 }
2938 }
2939 }
2940
2941
2942 /* Return 1 if POS is a position in ellipses displayed for invisible
2943 text. W is the window we display, for text property lookup. */
2944
2945 static int
2946 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2947 {
2948 Lisp_Object prop, window;
2949 int ellipses_p = 0;
2950 EMACS_INT charpos = CHARPOS (pos->pos);
2951
2952 /* If POS specifies a position in a display vector, this might
2953 be for an ellipsis displayed for invisible text. We won't
2954 get the iterator set up for delivering that ellipsis unless
2955 we make sure that it gets aware of the invisible text. */
2956 if (pos->dpvec_index >= 0
2957 && pos->overlay_string_index < 0
2958 && CHARPOS (pos->string_pos) < 0
2959 && charpos > BEGV
2960 && (XSETWINDOW (window, w),
2961 prop = Fget_char_property (make_number (charpos),
2962 Qinvisible, window),
2963 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2964 {
2965 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2966 window);
2967 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2968 }
2969
2970 return ellipses_p;
2971 }
2972
2973
2974 /* Initialize IT for stepping through current_buffer in window W,
2975 starting at position POS that includes overlay string and display
2976 vector/ control character translation position information. Value
2977 is zero if there are overlay strings with newlines at POS. */
2978
2979 static int
2980 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2981 {
2982 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2983 int i, overlay_strings_with_newlines = 0;
2984
2985 /* If POS specifies a position in a display vector, this might
2986 be for an ellipsis displayed for invisible text. We won't
2987 get the iterator set up for delivering that ellipsis unless
2988 we make sure that it gets aware of the invisible text. */
2989 if (in_ellipses_for_invisible_text_p (pos, w))
2990 {
2991 --charpos;
2992 bytepos = 0;
2993 }
2994
2995 /* Keep in mind: the call to reseat in init_iterator skips invisible
2996 text, so we might end up at a position different from POS. This
2997 is only a problem when POS is a row start after a newline and an
2998 overlay starts there with an after-string, and the overlay has an
2999 invisible property. Since we don't skip invisible text in
3000 display_line and elsewhere immediately after consuming the
3001 newline before the row start, such a POS will not be in a string,
3002 but the call to init_iterator below will move us to the
3003 after-string. */
3004 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3005
3006 /* This only scans the current chunk -- it should scan all chunks.
3007 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3008 to 16 in 22.1 to make this a lesser problem. */
3009 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3010 {
3011 const char *s = SDATA (it->overlay_strings[i]);
3012 const char *e = s + SBYTES (it->overlay_strings[i]);
3013
3014 while (s < e && *s != '\n')
3015 ++s;
3016
3017 if (s < e)
3018 {
3019 overlay_strings_with_newlines = 1;
3020 break;
3021 }
3022 }
3023
3024 /* If position is within an overlay string, set up IT to the right
3025 overlay string. */
3026 if (pos->overlay_string_index >= 0)
3027 {
3028 int relative_index;
3029
3030 /* If the first overlay string happens to have a `display'
3031 property for an image, the iterator will be set up for that
3032 image, and we have to undo that setup first before we can
3033 correct the overlay string index. */
3034 if (it->method == GET_FROM_IMAGE)
3035 pop_it (it);
3036
3037 /* We already have the first chunk of overlay strings in
3038 IT->overlay_strings. Load more until the one for
3039 pos->overlay_string_index is in IT->overlay_strings. */
3040 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3041 {
3042 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3043 it->current.overlay_string_index = 0;
3044 while (n--)
3045 {
3046 load_overlay_strings (it, 0);
3047 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3048 }
3049 }
3050
3051 it->current.overlay_string_index = pos->overlay_string_index;
3052 relative_index = (it->current.overlay_string_index
3053 % OVERLAY_STRING_CHUNK_SIZE);
3054 it->string = it->overlay_strings[relative_index];
3055 xassert (STRINGP (it->string));
3056 it->current.string_pos = pos->string_pos;
3057 it->method = GET_FROM_STRING;
3058 }
3059
3060 if (CHARPOS (pos->string_pos) >= 0)
3061 {
3062 /* Recorded position is not in an overlay string, but in another
3063 string. This can only be a string from a `display' property.
3064 IT should already be filled with that string. */
3065 it->current.string_pos = pos->string_pos;
3066 xassert (STRINGP (it->string));
3067 }
3068
3069 /* Restore position in display vector translations, control
3070 character translations or ellipses. */
3071 if (pos->dpvec_index >= 0)
3072 {
3073 if (it->dpvec == NULL)
3074 get_next_display_element (it);
3075 xassert (it->dpvec && it->current.dpvec_index == 0);
3076 it->current.dpvec_index = pos->dpvec_index;
3077 }
3078
3079 CHECK_IT (it);
3080 return !overlay_strings_with_newlines;
3081 }
3082
3083
3084 /* Initialize IT for stepping through current_buffer in window W
3085 starting at ROW->start. */
3086
3087 static void
3088 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3089 {
3090 init_from_display_pos (it, w, &row->start);
3091 it->start = row->start;
3092 it->continuation_lines_width = row->continuation_lines_width;
3093 CHECK_IT (it);
3094 }
3095
3096
3097 /* Initialize IT for stepping through current_buffer in window W
3098 starting in the line following ROW, i.e. starting at ROW->end.
3099 Value is zero if there are overlay strings with newlines at ROW's
3100 end position. */
3101
3102 static int
3103 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3104 {
3105 int success = 0;
3106
3107 if (init_from_display_pos (it, w, &row->end))
3108 {
3109 if (row->continued_p)
3110 it->continuation_lines_width
3111 = row->continuation_lines_width + row->pixel_width;
3112 CHECK_IT (it);
3113 success = 1;
3114 }
3115
3116 return success;
3117 }
3118
3119
3120
3121 \f
3122 /***********************************************************************
3123 Text properties
3124 ***********************************************************************/
3125
3126 /* Called when IT reaches IT->stop_charpos. Handle text property and
3127 overlay changes. Set IT->stop_charpos to the next position where
3128 to stop. */
3129
3130 static void
3131 handle_stop (struct it *it)
3132 {
3133 enum prop_handled handled;
3134 int handle_overlay_change_p;
3135 struct props *p;
3136
3137 it->dpvec = NULL;
3138 it->current.dpvec_index = -1;
3139 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3140 it->ignore_overlay_strings_at_pos_p = 0;
3141 it->ellipsis_p = 0;
3142
3143 /* Use face of preceding text for ellipsis (if invisible) */
3144 if (it->selective_display_ellipsis_p)
3145 it->saved_face_id = it->face_id;
3146
3147 do
3148 {
3149 handled = HANDLED_NORMALLY;
3150
3151 /* Call text property handlers. */
3152 for (p = it_props; p->handler; ++p)
3153 {
3154 handled = p->handler (it);
3155
3156 if (handled == HANDLED_RECOMPUTE_PROPS)
3157 break;
3158 else if (handled == HANDLED_RETURN)
3159 {
3160 /* We still want to show before and after strings from
3161 overlays even if the actual buffer text is replaced. */
3162 if (!handle_overlay_change_p
3163 || it->sp > 1
3164 || !get_overlay_strings_1 (it, 0, 0))
3165 {
3166 if (it->ellipsis_p)
3167 setup_for_ellipsis (it, 0);
3168 /* When handling a display spec, we might load an
3169 empty string. In that case, discard it here. We
3170 used to discard it in handle_single_display_spec,
3171 but that causes get_overlay_strings_1, above, to
3172 ignore overlay strings that we must check. */
3173 if (STRINGP (it->string) && !SCHARS (it->string))
3174 pop_it (it);
3175 return;
3176 }
3177 else if (STRINGP (it->string) && !SCHARS (it->string))
3178 pop_it (it);
3179 else
3180 {
3181 it->ignore_overlay_strings_at_pos_p = 1;
3182 it->string_from_display_prop_p = 0;
3183 handle_overlay_change_p = 0;
3184 }
3185 handled = HANDLED_RECOMPUTE_PROPS;
3186 break;
3187 }
3188 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3189 handle_overlay_change_p = 0;
3190 }
3191
3192 if (handled != HANDLED_RECOMPUTE_PROPS)
3193 {
3194 /* Don't check for overlay strings below when set to deliver
3195 characters from a display vector. */
3196 if (it->method == GET_FROM_DISPLAY_VECTOR)
3197 handle_overlay_change_p = 0;
3198
3199 /* Handle overlay changes.
3200 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3201 if it finds overlays. */
3202 if (handle_overlay_change_p)
3203 handled = handle_overlay_change (it);
3204 }
3205
3206 if (it->ellipsis_p)
3207 {
3208 setup_for_ellipsis (it, 0);
3209 break;
3210 }
3211 }
3212 while (handled == HANDLED_RECOMPUTE_PROPS);
3213
3214 /* Determine where to stop next. */
3215 if (handled == HANDLED_NORMALLY)
3216 compute_stop_pos (it);
3217 }
3218
3219
3220 /* Compute IT->stop_charpos from text property and overlay change
3221 information for IT's current position. */
3222
3223 static void
3224 compute_stop_pos (struct it *it)
3225 {
3226 register INTERVAL iv, next_iv;
3227 Lisp_Object object, limit, position;
3228 EMACS_INT charpos, bytepos;
3229
3230 /* If nowhere else, stop at the end. */
3231 it->stop_charpos = it->end_charpos;
3232
3233 if (STRINGP (it->string))
3234 {
3235 /* Strings are usually short, so don't limit the search for
3236 properties. */
3237 object = it->string;
3238 limit = Qnil;
3239 charpos = IT_STRING_CHARPOS (*it);
3240 bytepos = IT_STRING_BYTEPOS (*it);
3241 }
3242 else
3243 {
3244 EMACS_INT pos;
3245
3246 /* If next overlay change is in front of the current stop pos
3247 (which is IT->end_charpos), stop there. Note: value of
3248 next_overlay_change is point-max if no overlay change
3249 follows. */
3250 charpos = IT_CHARPOS (*it);
3251 bytepos = IT_BYTEPOS (*it);
3252 pos = next_overlay_change (charpos);
3253 if (pos < it->stop_charpos)
3254 it->stop_charpos = pos;
3255
3256 /* If showing the region, we have to stop at the region
3257 start or end because the face might change there. */
3258 if (it->region_beg_charpos > 0)
3259 {
3260 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3261 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3262 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3263 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3264 }
3265
3266 /* Set up variables for computing the stop position from text
3267 property changes. */
3268 XSETBUFFER (object, current_buffer);
3269 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3270 }
3271
3272 /* Get the interval containing IT's position. Value is a null
3273 interval if there isn't such an interval. */
3274 position = make_number (charpos);
3275 iv = validate_interval_range (object, &position, &position, 0);
3276 if (!NULL_INTERVAL_P (iv))
3277 {
3278 Lisp_Object values_here[LAST_PROP_IDX];
3279 struct props *p;
3280
3281 /* Get properties here. */
3282 for (p = it_props; p->handler; ++p)
3283 values_here[p->idx] = textget (iv->plist, *p->name);
3284
3285 /* Look for an interval following iv that has different
3286 properties. */
3287 for (next_iv = next_interval (iv);
3288 (!NULL_INTERVAL_P (next_iv)
3289 && (NILP (limit)
3290 || XFASTINT (limit) > next_iv->position));
3291 next_iv = next_interval (next_iv))
3292 {
3293 for (p = it_props; p->handler; ++p)
3294 {
3295 Lisp_Object new_value;
3296
3297 new_value = textget (next_iv->plist, *p->name);
3298 if (!EQ (values_here[p->idx], new_value))
3299 break;
3300 }
3301
3302 if (p->handler)
3303 break;
3304 }
3305
3306 if (!NULL_INTERVAL_P (next_iv))
3307 {
3308 if (INTEGERP (limit)
3309 && next_iv->position >= XFASTINT (limit))
3310 /* No text property change up to limit. */
3311 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3312 else
3313 /* Text properties change in next_iv. */
3314 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3315 }
3316 }
3317
3318 if (it->cmp_it.id < 0)
3319 {
3320 EMACS_INT stoppos = it->end_charpos;
3321
3322 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3323 stoppos = -1;
3324 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3325 stoppos, it->string);
3326 }
3327
3328 xassert (STRINGP (it->string)
3329 || (it->stop_charpos >= BEGV
3330 && it->stop_charpos >= IT_CHARPOS (*it)));
3331 }
3332
3333
3334 /* Return the position of the next overlay change after POS in
3335 current_buffer. Value is point-max if no overlay change
3336 follows. This is like `next-overlay-change' but doesn't use
3337 xmalloc. */
3338
3339 static EMACS_INT
3340 next_overlay_change (EMACS_INT pos)
3341 {
3342 int noverlays;
3343 EMACS_INT endpos;
3344 Lisp_Object *overlays;
3345 int i;
3346
3347 /* Get all overlays at the given position. */
3348 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3349
3350 /* If any of these overlays ends before endpos,
3351 use its ending point instead. */
3352 for (i = 0; i < noverlays; ++i)
3353 {
3354 Lisp_Object oend;
3355 EMACS_INT oendpos;
3356
3357 oend = OVERLAY_END (overlays[i]);
3358 oendpos = OVERLAY_POSITION (oend);
3359 endpos = min (endpos, oendpos);
3360 }
3361
3362 return endpos;
3363 }
3364
3365
3366 \f
3367 /***********************************************************************
3368 Fontification
3369 ***********************************************************************/
3370
3371 /* Handle changes in the `fontified' property of the current buffer by
3372 calling hook functions from Qfontification_functions to fontify
3373 regions of text. */
3374
3375 static enum prop_handled
3376 handle_fontified_prop (struct it *it)
3377 {
3378 Lisp_Object prop, pos;
3379 enum prop_handled handled = HANDLED_NORMALLY;
3380
3381 if (!NILP (Vmemory_full))
3382 return handled;
3383
3384 /* Get the value of the `fontified' property at IT's current buffer
3385 position. (The `fontified' property doesn't have a special
3386 meaning in strings.) If the value is nil, call functions from
3387 Qfontification_functions. */
3388 if (!STRINGP (it->string)
3389 && it->s == NULL
3390 && !NILP (Vfontification_functions)
3391 && !NILP (Vrun_hooks)
3392 && (pos = make_number (IT_CHARPOS (*it)),
3393 prop = Fget_char_property (pos, Qfontified, Qnil),
3394 /* Ignore the special cased nil value always present at EOB since
3395 no amount of fontifying will be able to change it. */
3396 NILP (prop) && IT_CHARPOS (*it) < Z))
3397 {
3398 int count = SPECPDL_INDEX ();
3399 Lisp_Object val;
3400
3401 val = Vfontification_functions;
3402 specbind (Qfontification_functions, Qnil);
3403
3404 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3405 safe_call1 (val, pos);
3406 else
3407 {
3408 Lisp_Object globals, fn;
3409 struct gcpro gcpro1, gcpro2;
3410
3411 globals = Qnil;
3412 GCPRO2 (val, globals);
3413
3414 for (; CONSP (val); val = XCDR (val))
3415 {
3416 fn = XCAR (val);
3417
3418 if (EQ (fn, Qt))
3419 {
3420 /* A value of t indicates this hook has a local
3421 binding; it means to run the global binding too.
3422 In a global value, t should not occur. If it
3423 does, we must ignore it to avoid an endless
3424 loop. */
3425 for (globals = Fdefault_value (Qfontification_functions);
3426 CONSP (globals);
3427 globals = XCDR (globals))
3428 {
3429 fn = XCAR (globals);
3430 if (!EQ (fn, Qt))
3431 safe_call1 (fn, pos);
3432 }
3433 }
3434 else
3435 safe_call1 (fn, pos);
3436 }
3437
3438 UNGCPRO;
3439 }
3440
3441 unbind_to (count, Qnil);
3442
3443 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3444 something. This avoids an endless loop if they failed to
3445 fontify the text for which reason ever. */
3446 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3447 handled = HANDLED_RECOMPUTE_PROPS;
3448 }
3449
3450 return handled;
3451 }
3452
3453
3454 \f
3455 /***********************************************************************
3456 Faces
3457 ***********************************************************************/
3458
3459 /* Set up iterator IT from face properties at its current position.
3460 Called from handle_stop. */
3461
3462 static enum prop_handled
3463 handle_face_prop (struct it *it)
3464 {
3465 int new_face_id;
3466 EMACS_INT next_stop;
3467
3468 if (!STRINGP (it->string))
3469 {
3470 new_face_id
3471 = face_at_buffer_position (it->w,
3472 IT_CHARPOS (*it),
3473 it->region_beg_charpos,
3474 it->region_end_charpos,
3475 &next_stop,
3476 (IT_CHARPOS (*it)
3477 + TEXT_PROP_DISTANCE_LIMIT),
3478 0, it->base_face_id);
3479
3480 /* Is this a start of a run of characters with box face?
3481 Caveat: this can be called for a freshly initialized
3482 iterator; face_id is -1 in this case. We know that the new
3483 face will not change until limit, i.e. if the new face has a
3484 box, all characters up to limit will have one. But, as
3485 usual, we don't know whether limit is really the end. */
3486 if (new_face_id != it->face_id)
3487 {
3488 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3489
3490 /* If new face has a box but old face has not, this is
3491 the start of a run of characters with box, i.e. it has
3492 a shadow on the left side. The value of face_id of the
3493 iterator will be -1 if this is the initial call that gets
3494 the face. In this case, we have to look in front of IT's
3495 position and see whether there is a face != new_face_id. */
3496 it->start_of_box_run_p
3497 = (new_face->box != FACE_NO_BOX
3498 && (it->face_id >= 0
3499 || IT_CHARPOS (*it) == BEG
3500 || new_face_id != face_before_it_pos (it)));
3501 it->face_box_p = new_face->box != FACE_NO_BOX;
3502 }
3503 }
3504 else
3505 {
3506 int base_face_id;
3507 EMACS_INT bufpos;
3508 int i;
3509 Lisp_Object from_overlay
3510 = (it->current.overlay_string_index >= 0
3511 ? it->string_overlays[it->current.overlay_string_index]
3512 : Qnil);
3513
3514 /* See if we got to this string directly or indirectly from
3515 an overlay property. That includes the before-string or
3516 after-string of an overlay, strings in display properties
3517 provided by an overlay, their text properties, etc.
3518
3519 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3520 if (! NILP (from_overlay))
3521 for (i = it->sp - 1; i >= 0; i--)
3522 {
3523 if (it->stack[i].current.overlay_string_index >= 0)
3524 from_overlay
3525 = it->string_overlays[it->stack[i].current.overlay_string_index];
3526 else if (! NILP (it->stack[i].from_overlay))
3527 from_overlay = it->stack[i].from_overlay;
3528
3529 if (!NILP (from_overlay))
3530 break;
3531 }
3532
3533 if (! NILP (from_overlay))
3534 {
3535 bufpos = IT_CHARPOS (*it);
3536 /* For a string from an overlay, the base face depends
3537 only on text properties and ignores overlays. */
3538 base_face_id
3539 = face_for_overlay_string (it->w,
3540 IT_CHARPOS (*it),
3541 it->region_beg_charpos,
3542 it->region_end_charpos,
3543 &next_stop,
3544 (IT_CHARPOS (*it)
3545 + TEXT_PROP_DISTANCE_LIMIT),
3546 0,
3547 from_overlay);
3548 }
3549 else
3550 {
3551 bufpos = 0;
3552
3553 /* For strings from a `display' property, use the face at
3554 IT's current buffer position as the base face to merge
3555 with, so that overlay strings appear in the same face as
3556 surrounding text, unless they specify their own
3557 faces. */
3558 base_face_id = underlying_face_id (it);
3559 }
3560
3561 new_face_id = face_at_string_position (it->w,
3562 it->string,
3563 IT_STRING_CHARPOS (*it),
3564 bufpos,
3565 it->region_beg_charpos,
3566 it->region_end_charpos,
3567 &next_stop,
3568 base_face_id, 0);
3569
3570 /* Is this a start of a run of characters with box? Caveat:
3571 this can be called for a freshly allocated iterator; face_id
3572 is -1 is this case. We know that the new face will not
3573 change until the next check pos, i.e. if the new face has a
3574 box, all characters up to that position will have a
3575 box. But, as usual, we don't know whether that position
3576 is really the end. */
3577 if (new_face_id != it->face_id)
3578 {
3579 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3580 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3581
3582 /* If new face has a box but old face hasn't, this is the
3583 start of a run of characters with box, i.e. it has a
3584 shadow on the left side. */
3585 it->start_of_box_run_p
3586 = new_face->box && (old_face == NULL || !old_face->box);
3587 it->face_box_p = new_face->box != FACE_NO_BOX;
3588 }
3589 }
3590
3591 it->face_id = new_face_id;
3592 return HANDLED_NORMALLY;
3593 }
3594
3595
3596 /* Return the ID of the face ``underlying'' IT's current position,
3597 which is in a string. If the iterator is associated with a
3598 buffer, return the face at IT's current buffer position.
3599 Otherwise, use the iterator's base_face_id. */
3600
3601 static int
3602 underlying_face_id (struct it *it)
3603 {
3604 int face_id = it->base_face_id, i;
3605
3606 xassert (STRINGP (it->string));
3607
3608 for (i = it->sp - 1; i >= 0; --i)
3609 if (NILP (it->stack[i].string))
3610 face_id = it->stack[i].face_id;
3611
3612 return face_id;
3613 }
3614
3615
3616 /* Compute the face one character before or after the current position
3617 of IT. BEFORE_P non-zero means get the face in front of IT's
3618 position. Value is the id of the face. */
3619
3620 static int
3621 face_before_or_after_it_pos (struct it *it, int before_p)
3622 {
3623 int face_id, limit;
3624 EMACS_INT next_check_charpos;
3625 struct text_pos pos;
3626
3627 xassert (it->s == NULL);
3628
3629 if (STRINGP (it->string))
3630 {
3631 EMACS_INT bufpos;
3632 int base_face_id;
3633
3634 /* No face change past the end of the string (for the case
3635 we are padding with spaces). No face change before the
3636 string start. */
3637 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3638 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3639 return it->face_id;
3640
3641 /* Set pos to the position before or after IT's current position. */
3642 if (before_p)
3643 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3644 else
3645 /* For composition, we must check the character after the
3646 composition. */
3647 pos = (it->what == IT_COMPOSITION
3648 ? string_pos (IT_STRING_CHARPOS (*it)
3649 + it->cmp_it.nchars, it->string)
3650 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3651
3652 if (it->current.overlay_string_index >= 0)
3653 bufpos = IT_CHARPOS (*it);
3654 else
3655 bufpos = 0;
3656
3657 base_face_id = underlying_face_id (it);
3658
3659 /* Get the face for ASCII, or unibyte. */
3660 face_id = face_at_string_position (it->w,
3661 it->string,
3662 CHARPOS (pos),
3663 bufpos,
3664 it->region_beg_charpos,
3665 it->region_end_charpos,
3666 &next_check_charpos,
3667 base_face_id, 0);
3668
3669 /* Correct the face for charsets different from ASCII. Do it
3670 for the multibyte case only. The face returned above is
3671 suitable for unibyte text if IT->string is unibyte. */
3672 if (STRING_MULTIBYTE (it->string))
3673 {
3674 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3675 int c, len;
3676 struct face *face = FACE_FROM_ID (it->f, face_id);
3677
3678 c = string_char_and_length (p, &len);
3679 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3680 }
3681 }
3682 else
3683 {
3684 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3685 || (IT_CHARPOS (*it) <= BEGV && before_p))
3686 return it->face_id;
3687
3688 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3689 pos = it->current.pos;
3690
3691 if (before_p)
3692 DEC_TEXT_POS (pos, it->multibyte_p);
3693 else
3694 {
3695 if (it->what == IT_COMPOSITION)
3696 /* For composition, we must check the position after the
3697 composition. */
3698 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3699 else
3700 INC_TEXT_POS (pos, it->multibyte_p);
3701 }
3702
3703 /* Determine face for CHARSET_ASCII, or unibyte. */
3704 face_id = face_at_buffer_position (it->w,
3705 CHARPOS (pos),
3706 it->region_beg_charpos,
3707 it->region_end_charpos,
3708 &next_check_charpos,
3709 limit, 0, -1);
3710
3711 /* Correct the face for charsets different from ASCII. Do it
3712 for the multibyte case only. The face returned above is
3713 suitable for unibyte text if current_buffer is unibyte. */
3714 if (it->multibyte_p)
3715 {
3716 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3717 struct face *face = FACE_FROM_ID (it->f, face_id);
3718 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3719 }
3720 }
3721
3722 return face_id;
3723 }
3724
3725
3726 \f
3727 /***********************************************************************
3728 Invisible text
3729 ***********************************************************************/
3730
3731 /* Set up iterator IT from invisible properties at its current
3732 position. Called from handle_stop. */
3733
3734 static enum prop_handled
3735 handle_invisible_prop (struct it *it)
3736 {
3737 enum prop_handled handled = HANDLED_NORMALLY;
3738
3739 if (STRINGP (it->string))
3740 {
3741 Lisp_Object prop, end_charpos, limit, charpos;
3742
3743 /* Get the value of the invisible text property at the
3744 current position. Value will be nil if there is no such
3745 property. */
3746 charpos = make_number (IT_STRING_CHARPOS (*it));
3747 prop = Fget_text_property (charpos, Qinvisible, it->string);
3748
3749 if (!NILP (prop)
3750 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3751 {
3752 handled = HANDLED_RECOMPUTE_PROPS;
3753
3754 /* Get the position at which the next change of the
3755 invisible text property can be found in IT->string.
3756 Value will be nil if the property value is the same for
3757 all the rest of IT->string. */
3758 XSETINT (limit, SCHARS (it->string));
3759 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3760 it->string, limit);
3761
3762 /* Text at current position is invisible. The next
3763 change in the property is at position end_charpos.
3764 Move IT's current position to that position. */
3765 if (INTEGERP (end_charpos)
3766 && XFASTINT (end_charpos) < XFASTINT (limit))
3767 {
3768 struct text_pos old;
3769 old = it->current.string_pos;
3770 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3771 compute_string_pos (&it->current.string_pos, old, it->string);
3772 }
3773 else
3774 {
3775 /* The rest of the string is invisible. If this is an
3776 overlay string, proceed with the next overlay string
3777 or whatever comes and return a character from there. */
3778 if (it->current.overlay_string_index >= 0)
3779 {
3780 next_overlay_string (it);
3781 /* Don't check for overlay strings when we just
3782 finished processing them. */
3783 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3784 }
3785 else
3786 {
3787 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3788 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3789 }
3790 }
3791 }
3792 }
3793 else
3794 {
3795 int invis_p;
3796 EMACS_INT newpos, next_stop, start_charpos, tem;
3797 Lisp_Object pos, prop, overlay;
3798
3799 /* First of all, is there invisible text at this position? */
3800 tem = start_charpos = IT_CHARPOS (*it);
3801 pos = make_number (tem);
3802 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3803 &overlay);
3804 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3805
3806 /* If we are on invisible text, skip over it. */
3807 if (invis_p && start_charpos < it->end_charpos)
3808 {
3809 /* Record whether we have to display an ellipsis for the
3810 invisible text. */
3811 int display_ellipsis_p = invis_p == 2;
3812
3813 handled = HANDLED_RECOMPUTE_PROPS;
3814
3815 /* Loop skipping over invisible text. The loop is left at
3816 ZV or with IT on the first char being visible again. */
3817 do
3818 {
3819 /* Try to skip some invisible text. Return value is the
3820 position reached which can be equal to where we start
3821 if there is nothing invisible there. This skips both
3822 over invisible text properties and overlays with
3823 invisible property. */
3824 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3825
3826 /* If we skipped nothing at all we weren't at invisible
3827 text in the first place. If everything to the end of
3828 the buffer was skipped, end the loop. */
3829 if (newpos == tem || newpos >= ZV)
3830 invis_p = 0;
3831 else
3832 {
3833 /* We skipped some characters but not necessarily
3834 all there are. Check if we ended up on visible
3835 text. Fget_char_property returns the property of
3836 the char before the given position, i.e. if we
3837 get invis_p = 0, this means that the char at
3838 newpos is visible. */
3839 pos = make_number (newpos);
3840 prop = Fget_char_property (pos, Qinvisible, it->window);
3841 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3842 }
3843
3844 /* If we ended up on invisible text, proceed to
3845 skip starting with next_stop. */
3846 if (invis_p)
3847 tem = next_stop;
3848
3849 /* If there are adjacent invisible texts, don't lose the
3850 second one's ellipsis. */
3851 if (invis_p == 2)
3852 display_ellipsis_p = 1;
3853 }
3854 while (invis_p);
3855
3856 /* The position newpos is now either ZV or on visible text. */
3857 if (it->bidi_p && newpos < ZV)
3858 {
3859 /* With bidi iteration, the region of invisible text
3860 could start and/or end in the middle of a non-base
3861 embedding level. Therefore, we need to skip
3862 invisible text using the bidi iterator, starting at
3863 IT's current position, until we find ourselves
3864 outside the invisible text. Skipping invisible text
3865 _after_ bidi iteration avoids affecting the visual
3866 order of the displayed text when invisible properties
3867 are added or removed. */
3868 if (it->bidi_it.first_elt)
3869 {
3870 /* If we were `reseat'ed to a new paragraph,
3871 determine the paragraph base direction. We need
3872 to do it now because next_element_from_buffer may
3873 not have a chance to do it, if we are going to
3874 skip any text at the beginning, which resets the
3875 FIRST_ELT flag. */
3876 bidi_paragraph_init (it->paragraph_embedding,
3877 &it->bidi_it, 1);
3878 }
3879 do
3880 {
3881 bidi_move_to_visually_next (&it->bidi_it);
3882 }
3883 while (it->stop_charpos <= it->bidi_it.charpos
3884 && it->bidi_it.charpos < newpos);
3885 IT_CHARPOS (*it) = it->bidi_it.charpos;
3886 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3887 /* If we overstepped NEWPOS, record its position in the
3888 iterator, so that we skip invisible text if later the
3889 bidi iteration lands us in the invisible region
3890 again. */
3891 if (IT_CHARPOS (*it) >= newpos)
3892 it->prev_stop = newpos;
3893 }
3894 else
3895 {
3896 IT_CHARPOS (*it) = newpos;
3897 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3898 }
3899
3900 /* If there are before-strings at the start of invisible
3901 text, and the text is invisible because of a text
3902 property, arrange to show before-strings because 20.x did
3903 it that way. (If the text is invisible because of an
3904 overlay property instead of a text property, this is
3905 already handled in the overlay code.) */
3906 if (NILP (overlay)
3907 && get_overlay_strings (it, it->stop_charpos))
3908 {
3909 handled = HANDLED_RECOMPUTE_PROPS;
3910 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3911 }
3912 else if (display_ellipsis_p)
3913 {
3914 /* Make sure that the glyphs of the ellipsis will get
3915 correct `charpos' values. If we would not update
3916 it->position here, the glyphs would belong to the
3917 last visible character _before_ the invisible
3918 text, which confuses `set_cursor_from_row'.
3919
3920 We use the last invisible position instead of the
3921 first because this way the cursor is always drawn on
3922 the first "." of the ellipsis, whenever PT is inside
3923 the invisible text. Otherwise the cursor would be
3924 placed _after_ the ellipsis when the point is after the
3925 first invisible character. */
3926 if (!STRINGP (it->object))
3927 {
3928 it->position.charpos = newpos - 1;
3929 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3930 }
3931 it->ellipsis_p = 1;
3932 /* Let the ellipsis display before
3933 considering any properties of the following char.
3934 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3935 handled = HANDLED_RETURN;
3936 }
3937 }
3938 }
3939
3940 return handled;
3941 }
3942
3943
3944 /* Make iterator IT return `...' next.
3945 Replaces LEN characters from buffer. */
3946
3947 static void
3948 setup_for_ellipsis (struct it *it, int len)
3949 {
3950 /* Use the display table definition for `...'. Invalid glyphs
3951 will be handled by the method returning elements from dpvec. */
3952 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3953 {
3954 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3955 it->dpvec = v->contents;
3956 it->dpend = v->contents + v->size;
3957 }
3958 else
3959 {
3960 /* Default `...'. */
3961 it->dpvec = default_invis_vector;
3962 it->dpend = default_invis_vector + 3;
3963 }
3964
3965 it->dpvec_char_len = len;
3966 it->current.dpvec_index = 0;
3967 it->dpvec_face_id = -1;
3968
3969 /* Remember the current face id in case glyphs specify faces.
3970 IT's face is restored in set_iterator_to_next.
3971 saved_face_id was set to preceding char's face in handle_stop. */
3972 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3973 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3974
3975 it->method = GET_FROM_DISPLAY_VECTOR;
3976 it->ellipsis_p = 1;
3977 }
3978
3979
3980 \f
3981 /***********************************************************************
3982 'display' property
3983 ***********************************************************************/
3984
3985 /* Set up iterator IT from `display' property at its current position.
3986 Called from handle_stop.
3987 We return HANDLED_RETURN if some part of the display property
3988 overrides the display of the buffer text itself.
3989 Otherwise we return HANDLED_NORMALLY. */
3990
3991 static enum prop_handled
3992 handle_display_prop (struct it *it)
3993 {
3994 Lisp_Object prop, object, overlay;
3995 struct text_pos *position;
3996 /* Nonzero if some property replaces the display of the text itself. */
3997 int display_replaced_p = 0;
3998
3999 if (STRINGP (it->string))
4000 {
4001 object = it->string;
4002 position = &it->current.string_pos;
4003 }
4004 else
4005 {
4006 XSETWINDOW (object, it->w);
4007 position = &it->current.pos;
4008 }
4009
4010 /* Reset those iterator values set from display property values. */
4011 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4012 it->space_width = Qnil;
4013 it->font_height = Qnil;
4014 it->voffset = 0;
4015
4016 /* We don't support recursive `display' properties, i.e. string
4017 values that have a string `display' property, that have a string
4018 `display' property etc. */
4019 if (!it->string_from_display_prop_p)
4020 it->area = TEXT_AREA;
4021
4022 prop = get_char_property_and_overlay (make_number (position->charpos),
4023 Qdisplay, object, &overlay);
4024 if (NILP (prop))
4025 return HANDLED_NORMALLY;
4026 /* Now OVERLAY is the overlay that gave us this property, or nil
4027 if it was a text property. */
4028
4029 if (!STRINGP (it->string))
4030 object = it->w->buffer;
4031
4032 if (CONSP (prop)
4033 /* Simple properties. */
4034 && !EQ (XCAR (prop), Qimage)
4035 && !EQ (XCAR (prop), Qspace)
4036 && !EQ (XCAR (prop), Qwhen)
4037 && !EQ (XCAR (prop), Qslice)
4038 && !EQ (XCAR (prop), Qspace_width)
4039 && !EQ (XCAR (prop), Qheight)
4040 && !EQ (XCAR (prop), Qraise)
4041 /* Marginal area specifications. */
4042 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
4043 && !EQ (XCAR (prop), Qleft_fringe)
4044 && !EQ (XCAR (prop), Qright_fringe)
4045 && !NILP (XCAR (prop)))
4046 {
4047 for (; CONSP (prop); prop = XCDR (prop))
4048 {
4049 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
4050 position, display_replaced_p))
4051 {
4052 display_replaced_p = 1;
4053 /* If some text in a string is replaced, `position' no
4054 longer points to the position of `object'. */
4055 if (STRINGP (object))
4056 break;
4057 }
4058 }
4059 }
4060 else if (VECTORP (prop))
4061 {
4062 int i;
4063 for (i = 0; i < ASIZE (prop); ++i)
4064 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4065 position, display_replaced_p))
4066 {
4067 display_replaced_p = 1;
4068 /* If some text in a string is replaced, `position' no
4069 longer points to the position of `object'. */
4070 if (STRINGP (object))
4071 break;
4072 }
4073 }
4074 else
4075 {
4076 if (handle_single_display_spec (it, prop, object, overlay,
4077 position, 0))
4078 display_replaced_p = 1;
4079 }
4080
4081 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4082 }
4083
4084
4085 /* Value is the position of the end of the `display' property starting
4086 at START_POS in OBJECT. */
4087
4088 static struct text_pos
4089 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4090 {
4091 Lisp_Object end;
4092 struct text_pos end_pos;
4093
4094 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4095 Qdisplay, object, Qnil);
4096 CHARPOS (end_pos) = XFASTINT (end);
4097 if (STRINGP (object))
4098 compute_string_pos (&end_pos, start_pos, it->string);
4099 else
4100 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4101
4102 return end_pos;
4103 }
4104
4105
4106 /* Set up IT from a single `display' specification PROP. OBJECT
4107 is the object in which the `display' property was found. *POSITION
4108 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4109 means that we previously saw a display specification which already
4110 replaced text display with something else, for example an image;
4111 we ignore such properties after the first one has been processed.
4112
4113 OVERLAY is the overlay this `display' property came from,
4114 or nil if it was a text property.
4115
4116 If PROP is a `space' or `image' specification, and in some other
4117 cases too, set *POSITION to the position where the `display'
4118 property ends.
4119
4120 Value is non-zero if something was found which replaces the display
4121 of buffer or string text. */
4122
4123 static int
4124 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4125 Lisp_Object overlay, struct text_pos *position,
4126 int display_replaced_before_p)
4127 {
4128 Lisp_Object form;
4129 Lisp_Object location, value;
4130 struct text_pos start_pos, save_pos;
4131 int valid_p;
4132
4133 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4134 If the result is non-nil, use VALUE instead of SPEC. */
4135 form = Qt;
4136 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4137 {
4138 spec = XCDR (spec);
4139 if (!CONSP (spec))
4140 return 0;
4141 form = XCAR (spec);
4142 spec = XCDR (spec);
4143 }
4144
4145 if (!NILP (form) && !EQ (form, Qt))
4146 {
4147 int count = SPECPDL_INDEX ();
4148 struct gcpro gcpro1;
4149
4150 /* Bind `object' to the object having the `display' property, a
4151 buffer or string. Bind `position' to the position in the
4152 object where the property was found, and `buffer-position'
4153 to the current position in the buffer. */
4154 specbind (Qobject, object);
4155 specbind (Qposition, make_number (CHARPOS (*position)));
4156 specbind (Qbuffer_position,
4157 make_number (STRINGP (object)
4158 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4159 GCPRO1 (form);
4160 form = safe_eval (form);
4161 UNGCPRO;
4162 unbind_to (count, Qnil);
4163 }
4164
4165 if (NILP (form))
4166 return 0;
4167
4168 /* Handle `(height HEIGHT)' specifications. */
4169 if (CONSP (spec)
4170 && EQ (XCAR (spec), Qheight)
4171 && CONSP (XCDR (spec)))
4172 {
4173 if (!FRAME_WINDOW_P (it->f))
4174 return 0;
4175
4176 it->font_height = XCAR (XCDR (spec));
4177 if (!NILP (it->font_height))
4178 {
4179 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4180 int new_height = -1;
4181
4182 if (CONSP (it->font_height)
4183 && (EQ (XCAR (it->font_height), Qplus)
4184 || EQ (XCAR (it->font_height), Qminus))
4185 && CONSP (XCDR (it->font_height))
4186 && INTEGERP (XCAR (XCDR (it->font_height))))
4187 {
4188 /* `(+ N)' or `(- N)' where N is an integer. */
4189 int steps = XINT (XCAR (XCDR (it->font_height)));
4190 if (EQ (XCAR (it->font_height), Qplus))
4191 steps = - steps;
4192 it->face_id = smaller_face (it->f, it->face_id, steps);
4193 }
4194 else if (FUNCTIONP (it->font_height))
4195 {
4196 /* Call function with current height as argument.
4197 Value is the new height. */
4198 Lisp_Object height;
4199 height = safe_call1 (it->font_height,
4200 face->lface[LFACE_HEIGHT_INDEX]);
4201 if (NUMBERP (height))
4202 new_height = XFLOATINT (height);
4203 }
4204 else if (NUMBERP (it->font_height))
4205 {
4206 /* Value is a multiple of the canonical char height. */
4207 struct face *face;
4208
4209 face = FACE_FROM_ID (it->f,
4210 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4211 new_height = (XFLOATINT (it->font_height)
4212 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4213 }
4214 else
4215 {
4216 /* Evaluate IT->font_height with `height' bound to the
4217 current specified height to get the new height. */
4218 int count = SPECPDL_INDEX ();
4219
4220 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4221 value = safe_eval (it->font_height);
4222 unbind_to (count, Qnil);
4223
4224 if (NUMBERP (value))
4225 new_height = XFLOATINT (value);
4226 }
4227
4228 if (new_height > 0)
4229 it->face_id = face_with_height (it->f, it->face_id, new_height);
4230 }
4231
4232 return 0;
4233 }
4234
4235 /* Handle `(space-width WIDTH)'. */
4236 if (CONSP (spec)
4237 && EQ (XCAR (spec), Qspace_width)
4238 && CONSP (XCDR (spec)))
4239 {
4240 if (!FRAME_WINDOW_P (it->f))
4241 return 0;
4242
4243 value = XCAR (XCDR (spec));
4244 if (NUMBERP (value) && XFLOATINT (value) > 0)
4245 it->space_width = value;
4246
4247 return 0;
4248 }
4249
4250 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4251 if (CONSP (spec)
4252 && EQ (XCAR (spec), Qslice))
4253 {
4254 Lisp_Object tem;
4255
4256 if (!FRAME_WINDOW_P (it->f))
4257 return 0;
4258
4259 if (tem = XCDR (spec), CONSP (tem))
4260 {
4261 it->slice.x = XCAR (tem);
4262 if (tem = XCDR (tem), CONSP (tem))
4263 {
4264 it->slice.y = XCAR (tem);
4265 if (tem = XCDR (tem), CONSP (tem))
4266 {
4267 it->slice.width = XCAR (tem);
4268 if (tem = XCDR (tem), CONSP (tem))
4269 it->slice.height = XCAR (tem);
4270 }
4271 }
4272 }
4273
4274 return 0;
4275 }
4276
4277 /* Handle `(raise FACTOR)'. */
4278 if (CONSP (spec)
4279 && EQ (XCAR (spec), Qraise)
4280 && CONSP (XCDR (spec)))
4281 {
4282 if (!FRAME_WINDOW_P (it->f))
4283 return 0;
4284
4285 #ifdef HAVE_WINDOW_SYSTEM
4286 value = XCAR (XCDR (spec));
4287 if (NUMBERP (value))
4288 {
4289 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4290 it->voffset = - (XFLOATINT (value)
4291 * (FONT_HEIGHT (face->font)));
4292 }
4293 #endif /* HAVE_WINDOW_SYSTEM */
4294
4295 return 0;
4296 }
4297
4298 /* Don't handle the other kinds of display specifications
4299 inside a string that we got from a `display' property. */
4300 if (it->string_from_display_prop_p)
4301 return 0;
4302
4303 /* Characters having this form of property are not displayed, so
4304 we have to find the end of the property. */
4305 start_pos = *position;
4306 *position = display_prop_end (it, object, start_pos);
4307 value = Qnil;
4308
4309 /* Stop the scan at that end position--we assume that all
4310 text properties change there. */
4311 it->stop_charpos = position->charpos;
4312
4313 /* Handle `(left-fringe BITMAP [FACE])'
4314 and `(right-fringe BITMAP [FACE])'. */
4315 if (CONSP (spec)
4316 && (EQ (XCAR (spec), Qleft_fringe)
4317 || EQ (XCAR (spec), Qright_fringe))
4318 && CONSP (XCDR (spec)))
4319 {
4320 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4321 int fringe_bitmap;
4322
4323 if (!FRAME_WINDOW_P (it->f))
4324 /* If we return here, POSITION has been advanced
4325 across the text with this property. */
4326 return 0;
4327
4328 #ifdef HAVE_WINDOW_SYSTEM
4329 value = XCAR (XCDR (spec));
4330 if (!SYMBOLP (value)
4331 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4332 /* If we return here, POSITION has been advanced
4333 across the text with this property. */
4334 return 0;
4335
4336 if (CONSP (XCDR (XCDR (spec))))
4337 {
4338 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4339 int face_id2 = lookup_derived_face (it->f, face_name,
4340 FRINGE_FACE_ID, 0);
4341 if (face_id2 >= 0)
4342 face_id = face_id2;
4343 }
4344
4345 /* Save current settings of IT so that we can restore them
4346 when we are finished with the glyph property value. */
4347
4348 save_pos = it->position;
4349 it->position = *position;
4350 push_it (it);
4351 it->position = save_pos;
4352
4353 it->area = TEXT_AREA;
4354 it->what = IT_IMAGE;
4355 it->image_id = -1; /* no image */
4356 it->position = start_pos;
4357 it->object = NILP (object) ? it->w->buffer : object;
4358 it->method = GET_FROM_IMAGE;
4359 it->from_overlay = Qnil;
4360 it->face_id = face_id;
4361
4362 /* Say that we haven't consumed the characters with
4363 `display' property yet. The call to pop_it in
4364 set_iterator_to_next will clean this up. */
4365 *position = start_pos;
4366
4367 if (EQ (XCAR (spec), Qleft_fringe))
4368 {
4369 it->left_user_fringe_bitmap = fringe_bitmap;
4370 it->left_user_fringe_face_id = face_id;
4371 }
4372 else
4373 {
4374 it->right_user_fringe_bitmap = fringe_bitmap;
4375 it->right_user_fringe_face_id = face_id;
4376 }
4377 #endif /* HAVE_WINDOW_SYSTEM */
4378 return 1;
4379 }
4380
4381 /* Prepare to handle `((margin left-margin) ...)',
4382 `((margin right-margin) ...)' and `((margin nil) ...)'
4383 prefixes for display specifications. */
4384 location = Qunbound;
4385 if (CONSP (spec) && CONSP (XCAR (spec)))
4386 {
4387 Lisp_Object tem;
4388
4389 value = XCDR (spec);
4390 if (CONSP (value))
4391 value = XCAR (value);
4392
4393 tem = XCAR (spec);
4394 if (EQ (XCAR (tem), Qmargin)
4395 && (tem = XCDR (tem),
4396 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4397 (NILP (tem)
4398 || EQ (tem, Qleft_margin)
4399 || EQ (tem, Qright_margin))))
4400 location = tem;
4401 }
4402
4403 if (EQ (location, Qunbound))
4404 {
4405 location = Qnil;
4406 value = spec;
4407 }
4408
4409 /* After this point, VALUE is the property after any
4410 margin prefix has been stripped. It must be a string,
4411 an image specification, or `(space ...)'.
4412
4413 LOCATION specifies where to display: `left-margin',
4414 `right-margin' or nil. */
4415
4416 valid_p = (STRINGP (value)
4417 #ifdef HAVE_WINDOW_SYSTEM
4418 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4419 #endif /* not HAVE_WINDOW_SYSTEM */
4420 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4421
4422 if (valid_p && !display_replaced_before_p)
4423 {
4424 /* Save current settings of IT so that we can restore them
4425 when we are finished with the glyph property value. */
4426 save_pos = it->position;
4427 it->position = *position;
4428 push_it (it);
4429 it->position = save_pos;
4430 it->from_overlay = overlay;
4431
4432 if (NILP (location))
4433 it->area = TEXT_AREA;
4434 else if (EQ (location, Qleft_margin))
4435 it->area = LEFT_MARGIN_AREA;
4436 else
4437 it->area = RIGHT_MARGIN_AREA;
4438
4439 if (STRINGP (value))
4440 {
4441 it->string = value;
4442 it->multibyte_p = STRING_MULTIBYTE (it->string);
4443 it->current.overlay_string_index = -1;
4444 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4445 it->end_charpos = it->string_nchars = SCHARS (it->string);
4446 it->method = GET_FROM_STRING;
4447 it->stop_charpos = 0;
4448 it->string_from_display_prop_p = 1;
4449 /* Say that we haven't consumed the characters with
4450 `display' property yet. The call to pop_it in
4451 set_iterator_to_next will clean this up. */
4452 if (BUFFERP (object))
4453 *position = start_pos;
4454 }
4455 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4456 {
4457 it->method = GET_FROM_STRETCH;
4458 it->object = value;
4459 *position = it->position = start_pos;
4460 }
4461 #ifdef HAVE_WINDOW_SYSTEM
4462 else
4463 {
4464 it->what = IT_IMAGE;
4465 it->image_id = lookup_image (it->f, value);
4466 it->position = start_pos;
4467 it->object = NILP (object) ? it->w->buffer : object;
4468 it->method = GET_FROM_IMAGE;
4469
4470 /* Say that we haven't consumed the characters with
4471 `display' property yet. The call to pop_it in
4472 set_iterator_to_next will clean this up. */
4473 *position = start_pos;
4474 }
4475 #endif /* HAVE_WINDOW_SYSTEM */
4476
4477 return 1;
4478 }
4479
4480 /* Invalid property or property not supported. Restore
4481 POSITION to what it was before. */
4482 *position = start_pos;
4483 return 0;
4484 }
4485
4486
4487 /* Check if SPEC is a display sub-property value whose text should be
4488 treated as intangible. */
4489
4490 static int
4491 single_display_spec_intangible_p (Lisp_Object prop)
4492 {
4493 /* Skip over `when FORM'. */
4494 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4495 {
4496 prop = XCDR (prop);
4497 if (!CONSP (prop))
4498 return 0;
4499 prop = XCDR (prop);
4500 }
4501
4502 if (STRINGP (prop))
4503 return 1;
4504
4505 if (!CONSP (prop))
4506 return 0;
4507
4508 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4509 we don't need to treat text as intangible. */
4510 if (EQ (XCAR (prop), Qmargin))
4511 {
4512 prop = XCDR (prop);
4513 if (!CONSP (prop))
4514 return 0;
4515
4516 prop = XCDR (prop);
4517 if (!CONSP (prop)
4518 || EQ (XCAR (prop), Qleft_margin)
4519 || EQ (XCAR (prop), Qright_margin))
4520 return 0;
4521 }
4522
4523 return (CONSP (prop)
4524 && (EQ (XCAR (prop), Qimage)
4525 || EQ (XCAR (prop), Qspace)));
4526 }
4527
4528
4529 /* Check if PROP is a display property value whose text should be
4530 treated as intangible. */
4531
4532 int
4533 display_prop_intangible_p (Lisp_Object prop)
4534 {
4535 if (CONSP (prop)
4536 && CONSP (XCAR (prop))
4537 && !EQ (Qmargin, XCAR (XCAR (prop))))
4538 {
4539 /* A list of sub-properties. */
4540 while (CONSP (prop))
4541 {
4542 if (single_display_spec_intangible_p (XCAR (prop)))
4543 return 1;
4544 prop = XCDR (prop);
4545 }
4546 }
4547 else if (VECTORP (prop))
4548 {
4549 /* A vector of sub-properties. */
4550 int i;
4551 for (i = 0; i < ASIZE (prop); ++i)
4552 if (single_display_spec_intangible_p (AREF (prop, i)))
4553 return 1;
4554 }
4555 else
4556 return single_display_spec_intangible_p (prop);
4557
4558 return 0;
4559 }
4560
4561
4562 /* Return 1 if PROP is a display sub-property value containing STRING. */
4563
4564 static int
4565 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4566 {
4567 if (EQ (string, prop))
4568 return 1;
4569
4570 /* Skip over `when FORM'. */
4571 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4572 {
4573 prop = XCDR (prop);
4574 if (!CONSP (prop))
4575 return 0;
4576 prop = XCDR (prop);
4577 }
4578
4579 if (CONSP (prop))
4580 /* Skip over `margin LOCATION'. */
4581 if (EQ (XCAR (prop), Qmargin))
4582 {
4583 prop = XCDR (prop);
4584 if (!CONSP (prop))
4585 return 0;
4586
4587 prop = XCDR (prop);
4588 if (!CONSP (prop))
4589 return 0;
4590 }
4591
4592 return CONSP (prop) && EQ (XCAR (prop), string);
4593 }
4594
4595
4596 /* Return 1 if STRING appears in the `display' property PROP. */
4597
4598 static int
4599 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4600 {
4601 if (CONSP (prop)
4602 && CONSP (XCAR (prop))
4603 && !EQ (Qmargin, XCAR (XCAR (prop))))
4604 {
4605 /* A list of sub-properties. */
4606 while (CONSP (prop))
4607 {
4608 if (single_display_spec_string_p (XCAR (prop), string))
4609 return 1;
4610 prop = XCDR (prop);
4611 }
4612 }
4613 else if (VECTORP (prop))
4614 {
4615 /* A vector of sub-properties. */
4616 int i;
4617 for (i = 0; i < ASIZE (prop); ++i)
4618 if (single_display_spec_string_p (AREF (prop, i), string))
4619 return 1;
4620 }
4621 else
4622 return single_display_spec_string_p (prop, string);
4623
4624 return 0;
4625 }
4626
4627 /* Look for STRING in overlays and text properties in W's buffer,
4628 between character positions FROM and TO (excluding TO).
4629 BACK_P non-zero means look back (in this case, TO is supposed to be
4630 less than FROM).
4631 Value is the first character position where STRING was found, or
4632 zero if it wasn't found before hitting TO.
4633
4634 W's buffer must be current.
4635
4636 This function may only use code that doesn't eval because it is
4637 called asynchronously from note_mouse_highlight. */
4638
4639 static EMACS_INT
4640 string_buffer_position_lim (struct window *w, Lisp_Object string,
4641 EMACS_INT from, EMACS_INT to, int back_p)
4642 {
4643 Lisp_Object limit, prop, pos;
4644 int found = 0;
4645
4646 pos = make_number (from);
4647
4648 if (!back_p) /* looking forward */
4649 {
4650 limit = make_number (min (to, ZV));
4651 while (!found && !EQ (pos, limit))
4652 {
4653 prop = Fget_char_property (pos, Qdisplay, Qnil);
4654 if (!NILP (prop) && display_prop_string_p (prop, string))
4655 found = 1;
4656 else
4657 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4658 limit);
4659 }
4660 }
4661 else /* looking back */
4662 {
4663 limit = make_number (max (to, BEGV));
4664 while (!found && !EQ (pos, limit))
4665 {
4666 prop = Fget_char_property (pos, Qdisplay, Qnil);
4667 if (!NILP (prop) && display_prop_string_p (prop, string))
4668 found = 1;
4669 else
4670 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4671 limit);
4672 }
4673 }
4674
4675 return found ? XINT (pos) : 0;
4676 }
4677
4678 /* Determine which buffer position in W's buffer STRING comes from.
4679 AROUND_CHARPOS is an approximate position where it could come from.
4680 Value is the buffer position or 0 if it couldn't be determined.
4681
4682 W's buffer must be current.
4683
4684 This function is necessary because we don't record buffer positions
4685 in glyphs generated from strings (to keep struct glyph small).
4686 This function may only use code that doesn't eval because it is
4687 called asynchronously from note_mouse_highlight. */
4688
4689 EMACS_INT
4690 string_buffer_position (struct window *w, Lisp_Object string, EMACS_INT around_charpos)
4691 {
4692 const int MAX_DISTANCE = 1000;
4693 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4694 around_charpos + MAX_DISTANCE,
4695 0);
4696
4697 if (!found)
4698 found = string_buffer_position_lim (w, string, around_charpos,
4699 around_charpos - MAX_DISTANCE, 1);
4700 return found;
4701 }
4702
4703
4704 \f
4705 /***********************************************************************
4706 `composition' property
4707 ***********************************************************************/
4708
4709 /* Set up iterator IT from `composition' property at its current
4710 position. Called from handle_stop. */
4711
4712 static enum prop_handled
4713 handle_composition_prop (struct it *it)
4714 {
4715 Lisp_Object prop, string;
4716 EMACS_INT pos, pos_byte, start, end;
4717
4718 if (STRINGP (it->string))
4719 {
4720 unsigned char *s;
4721
4722 pos = IT_STRING_CHARPOS (*it);
4723 pos_byte = IT_STRING_BYTEPOS (*it);
4724 string = it->string;
4725 s = SDATA (string) + pos_byte;
4726 it->c = STRING_CHAR (s);
4727 }
4728 else
4729 {
4730 pos = IT_CHARPOS (*it);
4731 pos_byte = IT_BYTEPOS (*it);
4732 string = Qnil;
4733 it->c = FETCH_CHAR (pos_byte);
4734 }
4735
4736 /* If there's a valid composition and point is not inside of the
4737 composition (in the case that the composition is from the current
4738 buffer), draw a glyph composed from the composition components. */
4739 if (find_composition (pos, -1, &start, &end, &prop, string)
4740 && COMPOSITION_VALID_P (start, end, prop)
4741 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4742 {
4743 if (start != pos)
4744 {
4745 if (STRINGP (it->string))
4746 pos_byte = string_char_to_byte (it->string, start);
4747 else
4748 pos_byte = CHAR_TO_BYTE (start);
4749 }
4750 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4751 prop, string);
4752
4753 if (it->cmp_it.id >= 0)
4754 {
4755 it->cmp_it.ch = -1;
4756 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4757 it->cmp_it.nglyphs = -1;
4758 }
4759 }
4760
4761 return HANDLED_NORMALLY;
4762 }
4763
4764
4765 \f
4766 /***********************************************************************
4767 Overlay strings
4768 ***********************************************************************/
4769
4770 /* The following structure is used to record overlay strings for
4771 later sorting in load_overlay_strings. */
4772
4773 struct overlay_entry
4774 {
4775 Lisp_Object overlay;
4776 Lisp_Object string;
4777 int priority;
4778 int after_string_p;
4779 };
4780
4781
4782 /* Set up iterator IT from overlay strings at its current position.
4783 Called from handle_stop. */
4784
4785 static enum prop_handled
4786 handle_overlay_change (struct it *it)
4787 {
4788 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4789 return HANDLED_RECOMPUTE_PROPS;
4790 else
4791 return HANDLED_NORMALLY;
4792 }
4793
4794
4795 /* Set up the next overlay string for delivery by IT, if there is an
4796 overlay string to deliver. Called by set_iterator_to_next when the
4797 end of the current overlay string is reached. If there are more
4798 overlay strings to display, IT->string and
4799 IT->current.overlay_string_index are set appropriately here.
4800 Otherwise IT->string is set to nil. */
4801
4802 static void
4803 next_overlay_string (struct it *it)
4804 {
4805 ++it->current.overlay_string_index;
4806 if (it->current.overlay_string_index == it->n_overlay_strings)
4807 {
4808 /* No more overlay strings. Restore IT's settings to what
4809 they were before overlay strings were processed, and
4810 continue to deliver from current_buffer. */
4811
4812 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4813 pop_it (it);
4814 xassert (it->sp > 0
4815 || (NILP (it->string)
4816 && it->method == GET_FROM_BUFFER
4817 && it->stop_charpos >= BEGV
4818 && it->stop_charpos <= it->end_charpos));
4819 it->current.overlay_string_index = -1;
4820 it->n_overlay_strings = 0;
4821 it->overlay_strings_charpos = -1;
4822
4823 /* If we're at the end of the buffer, record that we have
4824 processed the overlay strings there already, so that
4825 next_element_from_buffer doesn't try it again. */
4826 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4827 it->overlay_strings_at_end_processed_p = 1;
4828 }
4829 else
4830 {
4831 /* There are more overlay strings to process. If
4832 IT->current.overlay_string_index has advanced to a position
4833 where we must load IT->overlay_strings with more strings, do
4834 it. We must load at the IT->overlay_strings_charpos where
4835 IT->n_overlay_strings was originally computed; when invisible
4836 text is present, this might not be IT_CHARPOS (Bug#7016). */
4837 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4838
4839 if (it->current.overlay_string_index && i == 0)
4840 load_overlay_strings (it, it->overlay_strings_charpos);
4841
4842 /* Initialize IT to deliver display elements from the overlay
4843 string. */
4844 it->string = it->overlay_strings[i];
4845 it->multibyte_p = STRING_MULTIBYTE (it->string);
4846 SET_TEXT_POS (it->current.string_pos, 0, 0);
4847 it->method = GET_FROM_STRING;
4848 it->stop_charpos = 0;
4849 if (it->cmp_it.stop_pos >= 0)
4850 it->cmp_it.stop_pos = 0;
4851 }
4852
4853 CHECK_IT (it);
4854 }
4855
4856
4857 /* Compare two overlay_entry structures E1 and E2. Used as a
4858 comparison function for qsort in load_overlay_strings. Overlay
4859 strings for the same position are sorted so that
4860
4861 1. All after-strings come in front of before-strings, except
4862 when they come from the same overlay.
4863
4864 2. Within after-strings, strings are sorted so that overlay strings
4865 from overlays with higher priorities come first.
4866
4867 2. Within before-strings, strings are sorted so that overlay
4868 strings from overlays with higher priorities come last.
4869
4870 Value is analogous to strcmp. */
4871
4872
4873 static int
4874 compare_overlay_entries (const void *e1, const void *e2)
4875 {
4876 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4877 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4878 int result;
4879
4880 if (entry1->after_string_p != entry2->after_string_p)
4881 {
4882 /* Let after-strings appear in front of before-strings if
4883 they come from different overlays. */
4884 if (EQ (entry1->overlay, entry2->overlay))
4885 result = entry1->after_string_p ? 1 : -1;
4886 else
4887 result = entry1->after_string_p ? -1 : 1;
4888 }
4889 else if (entry1->after_string_p)
4890 /* After-strings sorted in order of decreasing priority. */
4891 result = entry2->priority - entry1->priority;
4892 else
4893 /* Before-strings sorted in order of increasing priority. */
4894 result = entry1->priority - entry2->priority;
4895
4896 return result;
4897 }
4898
4899
4900 /* Load the vector IT->overlay_strings with overlay strings from IT's
4901 current buffer position, or from CHARPOS if that is > 0. Set
4902 IT->n_overlays to the total number of overlay strings found.
4903
4904 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4905 a time. On entry into load_overlay_strings,
4906 IT->current.overlay_string_index gives the number of overlay
4907 strings that have already been loaded by previous calls to this
4908 function.
4909
4910 IT->add_overlay_start contains an additional overlay start
4911 position to consider for taking overlay strings from, if non-zero.
4912 This position comes into play when the overlay has an `invisible'
4913 property, and both before and after-strings. When we've skipped to
4914 the end of the overlay, because of its `invisible' property, we
4915 nevertheless want its before-string to appear.
4916 IT->add_overlay_start will contain the overlay start position
4917 in this case.
4918
4919 Overlay strings are sorted so that after-string strings come in
4920 front of before-string strings. Within before and after-strings,
4921 strings are sorted by overlay priority. See also function
4922 compare_overlay_entries. */
4923
4924 static void
4925 load_overlay_strings (struct it *it, EMACS_INT charpos)
4926 {
4927 Lisp_Object overlay, window, str, invisible;
4928 struct Lisp_Overlay *ov;
4929 EMACS_INT start, end;
4930 int size = 20;
4931 int n = 0, i, j, invis_p;
4932 struct overlay_entry *entries
4933 = (struct overlay_entry *) alloca (size * sizeof *entries);
4934
4935 if (charpos <= 0)
4936 charpos = IT_CHARPOS (*it);
4937
4938 /* Append the overlay string STRING of overlay OVERLAY to vector
4939 `entries' which has size `size' and currently contains `n'
4940 elements. AFTER_P non-zero means STRING is an after-string of
4941 OVERLAY. */
4942 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4943 do \
4944 { \
4945 Lisp_Object priority; \
4946 \
4947 if (n == size) \
4948 { \
4949 int new_size = 2 * size; \
4950 struct overlay_entry *old = entries; \
4951 entries = \
4952 (struct overlay_entry *) alloca (new_size \
4953 * sizeof *entries); \
4954 memcpy (entries, old, size * sizeof *entries); \
4955 size = new_size; \
4956 } \
4957 \
4958 entries[n].string = (STRING); \
4959 entries[n].overlay = (OVERLAY); \
4960 priority = Foverlay_get ((OVERLAY), Qpriority); \
4961 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4962 entries[n].after_string_p = (AFTER_P); \
4963 ++n; \
4964 } \
4965 while (0)
4966
4967 /* Process overlay before the overlay center. */
4968 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4969 {
4970 XSETMISC (overlay, ov);
4971 xassert (OVERLAYP (overlay));
4972 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4973 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4974
4975 if (end < charpos)
4976 break;
4977
4978 /* Skip this overlay if it doesn't start or end at IT's current
4979 position. */
4980 if (end != charpos && start != charpos)
4981 continue;
4982
4983 /* Skip this overlay if it doesn't apply to IT->w. */
4984 window = Foverlay_get (overlay, Qwindow);
4985 if (WINDOWP (window) && XWINDOW (window) != it->w)
4986 continue;
4987
4988 /* If the text ``under'' the overlay is invisible, both before-
4989 and after-strings from this overlay are visible; start and
4990 end position are indistinguishable. */
4991 invisible = Foverlay_get (overlay, Qinvisible);
4992 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4993
4994 /* If overlay has a non-empty before-string, record it. */
4995 if ((start == charpos || (end == charpos && invis_p))
4996 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4997 && SCHARS (str))
4998 RECORD_OVERLAY_STRING (overlay, str, 0);
4999
5000 /* If overlay has a non-empty after-string, record it. */
5001 if ((end == charpos || (start == charpos && invis_p))
5002 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5003 && SCHARS (str))
5004 RECORD_OVERLAY_STRING (overlay, str, 1);
5005 }
5006
5007 /* Process overlays after the overlay center. */
5008 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5009 {
5010 XSETMISC (overlay, ov);
5011 xassert (OVERLAYP (overlay));
5012 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5013 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5014
5015 if (start > charpos)
5016 break;
5017
5018 /* Skip this overlay if it doesn't start or end at IT's current
5019 position. */
5020 if (end != charpos && start != charpos)
5021 continue;
5022
5023 /* Skip this overlay if it doesn't apply to IT->w. */
5024 window = Foverlay_get (overlay, Qwindow);
5025 if (WINDOWP (window) && XWINDOW (window) != it->w)
5026 continue;
5027
5028 /* If the text ``under'' the overlay is invisible, it has a zero
5029 dimension, and both before- and after-strings apply. */
5030 invisible = Foverlay_get (overlay, Qinvisible);
5031 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5032
5033 /* If overlay has a non-empty before-string, record it. */
5034 if ((start == charpos || (end == charpos && invis_p))
5035 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5036 && SCHARS (str))
5037 RECORD_OVERLAY_STRING (overlay, str, 0);
5038
5039 /* If overlay has a non-empty after-string, record it. */
5040 if ((end == charpos || (start == charpos && invis_p))
5041 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5042 && SCHARS (str))
5043 RECORD_OVERLAY_STRING (overlay, str, 1);
5044 }
5045
5046 #undef RECORD_OVERLAY_STRING
5047
5048 /* Sort entries. */
5049 if (n > 1)
5050 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5051
5052 /* Record number of overlay strings, and where we computed it. */
5053 it->n_overlay_strings = n;
5054 it->overlay_strings_charpos = charpos;
5055
5056 /* IT->current.overlay_string_index is the number of overlay strings
5057 that have already been consumed by IT. Copy some of the
5058 remaining overlay strings to IT->overlay_strings. */
5059 i = 0;
5060 j = it->current.overlay_string_index;
5061 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5062 {
5063 it->overlay_strings[i] = entries[j].string;
5064 it->string_overlays[i++] = entries[j++].overlay;
5065 }
5066
5067 CHECK_IT (it);
5068 }
5069
5070
5071 /* Get the first chunk of overlay strings at IT's current buffer
5072 position, or at CHARPOS if that is > 0. Value is non-zero if at
5073 least one overlay string was found. */
5074
5075 static int
5076 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5077 {
5078 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5079 process. This fills IT->overlay_strings with strings, and sets
5080 IT->n_overlay_strings to the total number of strings to process.
5081 IT->pos.overlay_string_index has to be set temporarily to zero
5082 because load_overlay_strings needs this; it must be set to -1
5083 when no overlay strings are found because a zero value would
5084 indicate a position in the first overlay string. */
5085 it->current.overlay_string_index = 0;
5086 load_overlay_strings (it, charpos);
5087
5088 /* If we found overlay strings, set up IT to deliver display
5089 elements from the first one. Otherwise set up IT to deliver
5090 from current_buffer. */
5091 if (it->n_overlay_strings)
5092 {
5093 /* Make sure we know settings in current_buffer, so that we can
5094 restore meaningful values when we're done with the overlay
5095 strings. */
5096 if (compute_stop_p)
5097 compute_stop_pos (it);
5098 xassert (it->face_id >= 0);
5099
5100 /* Save IT's settings. They are restored after all overlay
5101 strings have been processed. */
5102 xassert (!compute_stop_p || it->sp == 0);
5103
5104 /* When called from handle_stop, there might be an empty display
5105 string loaded. In that case, don't bother saving it. */
5106 if (!STRINGP (it->string) || SCHARS (it->string))
5107 push_it (it);
5108
5109 /* Set up IT to deliver display elements from the first overlay
5110 string. */
5111 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5112 it->string = it->overlay_strings[0];
5113 it->from_overlay = Qnil;
5114 it->stop_charpos = 0;
5115 xassert (STRINGP (it->string));
5116 it->end_charpos = SCHARS (it->string);
5117 it->multibyte_p = STRING_MULTIBYTE (it->string);
5118 it->method = GET_FROM_STRING;
5119 return 1;
5120 }
5121
5122 it->current.overlay_string_index = -1;
5123 return 0;
5124 }
5125
5126 static int
5127 get_overlay_strings (struct it *it, EMACS_INT charpos)
5128 {
5129 it->string = Qnil;
5130 it->method = GET_FROM_BUFFER;
5131
5132 (void) get_overlay_strings_1 (it, charpos, 1);
5133
5134 CHECK_IT (it);
5135
5136 /* Value is non-zero if we found at least one overlay string. */
5137 return STRINGP (it->string);
5138 }
5139
5140
5141 \f
5142 /***********************************************************************
5143 Saving and restoring state
5144 ***********************************************************************/
5145
5146 /* Save current settings of IT on IT->stack. Called, for example,
5147 before setting up IT for an overlay string, to be able to restore
5148 IT's settings to what they were after the overlay string has been
5149 processed. */
5150
5151 static void
5152 push_it (struct it *it)
5153 {
5154 struct iterator_stack_entry *p;
5155
5156 xassert (it->sp < IT_STACK_SIZE);
5157 p = it->stack + it->sp;
5158
5159 p->stop_charpos = it->stop_charpos;
5160 p->prev_stop = it->prev_stop;
5161 p->base_level_stop = it->base_level_stop;
5162 p->cmp_it = it->cmp_it;
5163 xassert (it->face_id >= 0);
5164 p->face_id = it->face_id;
5165 p->string = it->string;
5166 p->method = it->method;
5167 p->from_overlay = it->from_overlay;
5168 switch (p->method)
5169 {
5170 case GET_FROM_IMAGE:
5171 p->u.image.object = it->object;
5172 p->u.image.image_id = it->image_id;
5173 p->u.image.slice = it->slice;
5174 break;
5175 case GET_FROM_STRETCH:
5176 p->u.stretch.object = it->object;
5177 break;
5178 }
5179 p->position = it->position;
5180 p->current = it->current;
5181 p->end_charpos = it->end_charpos;
5182 p->string_nchars = it->string_nchars;
5183 p->area = it->area;
5184 p->multibyte_p = it->multibyte_p;
5185 p->avoid_cursor_p = it->avoid_cursor_p;
5186 p->space_width = it->space_width;
5187 p->font_height = it->font_height;
5188 p->voffset = it->voffset;
5189 p->string_from_display_prop_p = it->string_from_display_prop_p;
5190 p->display_ellipsis_p = 0;
5191 p->line_wrap = it->line_wrap;
5192 ++it->sp;
5193 }
5194
5195 static void
5196 iterate_out_of_display_property (struct it *it)
5197 {
5198 /* Maybe initialize paragraph direction. If we are at the beginning
5199 of a new paragraph, next_element_from_buffer may not have a
5200 chance to do that. */
5201 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
5202 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5203 /* prev_stop can be zero, so check against BEGV as well. */
5204 while (it->bidi_it.charpos >= BEGV
5205 && it->prev_stop <= it->bidi_it.charpos
5206 && it->bidi_it.charpos < CHARPOS (it->position))
5207 bidi_move_to_visually_next (&it->bidi_it);
5208 /* Record the stop_pos we just crossed, for when we cross it
5209 back, maybe. */
5210 if (it->bidi_it.charpos > CHARPOS (it->position))
5211 it->prev_stop = CHARPOS (it->position);
5212 /* If we ended up not where pop_it put us, resync IT's
5213 positional members with the bidi iterator. */
5214 if (it->bidi_it.charpos != CHARPOS (it->position))
5215 {
5216 SET_TEXT_POS (it->position,
5217 it->bidi_it.charpos, it->bidi_it.bytepos);
5218 it->current.pos = it->position;
5219 }
5220 }
5221
5222 /* Restore IT's settings from IT->stack. Called, for example, when no
5223 more overlay strings must be processed, and we return to delivering
5224 display elements from a buffer, or when the end of a string from a
5225 `display' property is reached and we return to delivering display
5226 elements from an overlay string, or from a buffer. */
5227
5228 static void
5229 pop_it (struct it *it)
5230 {
5231 struct iterator_stack_entry *p;
5232
5233 xassert (it->sp > 0);
5234 --it->sp;
5235 p = it->stack + it->sp;
5236 it->stop_charpos = p->stop_charpos;
5237 it->prev_stop = p->prev_stop;
5238 it->base_level_stop = p->base_level_stop;
5239 it->cmp_it = p->cmp_it;
5240 it->face_id = p->face_id;
5241 it->current = p->current;
5242 it->position = p->position;
5243 it->string = p->string;
5244 it->from_overlay = p->from_overlay;
5245 if (NILP (it->string))
5246 SET_TEXT_POS (it->current.string_pos, -1, -1);
5247 it->method = p->method;
5248 switch (it->method)
5249 {
5250 case GET_FROM_IMAGE:
5251 it->image_id = p->u.image.image_id;
5252 it->object = p->u.image.object;
5253 it->slice = p->u.image.slice;
5254 break;
5255 case GET_FROM_STRETCH:
5256 it->object = p->u.comp.object;
5257 break;
5258 case GET_FROM_BUFFER:
5259 it->object = it->w->buffer;
5260 if (it->bidi_p)
5261 {
5262 /* Bidi-iterate until we get out of the portion of text, if
5263 any, covered by a `display' text property or an overlay
5264 with `display' property. (We cannot just jump there,
5265 because the internal coherency of the bidi iterator state
5266 can not be preserved across such jumps.) We also must
5267 determine the paragraph base direction if the overlay we
5268 just processed is at the beginning of a new
5269 paragraph. */
5270 iterate_out_of_display_property (it);
5271 }
5272 break;
5273 case GET_FROM_STRING:
5274 it->object = it->string;
5275 break;
5276 case GET_FROM_DISPLAY_VECTOR:
5277 if (it->s)
5278 it->method = GET_FROM_C_STRING;
5279 else if (STRINGP (it->string))
5280 it->method = GET_FROM_STRING;
5281 else
5282 {
5283 it->method = GET_FROM_BUFFER;
5284 it->object = it->w->buffer;
5285 }
5286 }
5287 it->end_charpos = p->end_charpos;
5288 it->string_nchars = p->string_nchars;
5289 it->area = p->area;
5290 it->multibyte_p = p->multibyte_p;
5291 it->avoid_cursor_p = p->avoid_cursor_p;
5292 it->space_width = p->space_width;
5293 it->font_height = p->font_height;
5294 it->voffset = p->voffset;
5295 it->string_from_display_prop_p = p->string_from_display_prop_p;
5296 it->line_wrap = p->line_wrap;
5297 }
5298
5299
5300 \f
5301 /***********************************************************************
5302 Moving over lines
5303 ***********************************************************************/
5304
5305 /* Set IT's current position to the previous line start. */
5306
5307 static void
5308 back_to_previous_line_start (struct it *it)
5309 {
5310 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5311 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5312 }
5313
5314
5315 /* Move IT to the next line start.
5316
5317 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5318 we skipped over part of the text (as opposed to moving the iterator
5319 continuously over the text). Otherwise, don't change the value
5320 of *SKIPPED_P.
5321
5322 Newlines may come from buffer text, overlay strings, or strings
5323 displayed via the `display' property. That's the reason we can't
5324 simply use find_next_newline_no_quit.
5325
5326 Note that this function may not skip over invisible text that is so
5327 because of text properties and immediately follows a newline. If
5328 it would, function reseat_at_next_visible_line_start, when called
5329 from set_iterator_to_next, would effectively make invisible
5330 characters following a newline part of the wrong glyph row, which
5331 leads to wrong cursor motion. */
5332
5333 static int
5334 forward_to_next_line_start (struct it *it, int *skipped_p)
5335 {
5336 int old_selective, newline_found_p, n;
5337 const int MAX_NEWLINE_DISTANCE = 500;
5338
5339 /* If already on a newline, just consume it to avoid unintended
5340 skipping over invisible text below. */
5341 if (it->what == IT_CHARACTER
5342 && it->c == '\n'
5343 && CHARPOS (it->position) == IT_CHARPOS (*it))
5344 {
5345 set_iterator_to_next (it, 0);
5346 it->c = 0;
5347 return 1;
5348 }
5349
5350 /* Don't handle selective display in the following. It's (a)
5351 unnecessary because it's done by the caller, and (b) leads to an
5352 infinite recursion because next_element_from_ellipsis indirectly
5353 calls this function. */
5354 old_selective = it->selective;
5355 it->selective = 0;
5356
5357 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5358 from buffer text. */
5359 for (n = newline_found_p = 0;
5360 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5361 n += STRINGP (it->string) ? 0 : 1)
5362 {
5363 if (!get_next_display_element (it))
5364 return 0;
5365 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5366 set_iterator_to_next (it, 0);
5367 }
5368
5369 /* If we didn't find a newline near enough, see if we can use a
5370 short-cut. */
5371 if (!newline_found_p)
5372 {
5373 EMACS_INT start = IT_CHARPOS (*it);
5374 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5375 Lisp_Object pos;
5376
5377 xassert (!STRINGP (it->string));
5378
5379 /* If there isn't any `display' property in sight, and no
5380 overlays, we can just use the position of the newline in
5381 buffer text. */
5382 if (it->stop_charpos >= limit
5383 || ((pos = Fnext_single_property_change (make_number (start),
5384 Qdisplay,
5385 Qnil, make_number (limit)),
5386 NILP (pos))
5387 && next_overlay_change (start) == ZV))
5388 {
5389 IT_CHARPOS (*it) = limit;
5390 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5391 *skipped_p = newline_found_p = 1;
5392 }
5393 else
5394 {
5395 while (get_next_display_element (it)
5396 && !newline_found_p)
5397 {
5398 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5399 set_iterator_to_next (it, 0);
5400 }
5401 }
5402 }
5403
5404 it->selective = old_selective;
5405 return newline_found_p;
5406 }
5407
5408
5409 /* Set IT's current position to the previous visible line start. Skip
5410 invisible text that is so either due to text properties or due to
5411 selective display. Caution: this does not change IT->current_x and
5412 IT->hpos. */
5413
5414 static void
5415 back_to_previous_visible_line_start (struct it *it)
5416 {
5417 while (IT_CHARPOS (*it) > BEGV)
5418 {
5419 back_to_previous_line_start (it);
5420
5421 if (IT_CHARPOS (*it) <= BEGV)
5422 break;
5423
5424 /* If selective > 0, then lines indented more than its value are
5425 invisible. */
5426 if (it->selective > 0
5427 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5428 (double) it->selective)) /* iftc */
5429 continue;
5430
5431 /* Check the newline before point for invisibility. */
5432 {
5433 Lisp_Object prop;
5434 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5435 Qinvisible, it->window);
5436 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5437 continue;
5438 }
5439
5440 if (IT_CHARPOS (*it) <= BEGV)
5441 break;
5442
5443 {
5444 struct it it2;
5445 EMACS_INT pos;
5446 EMACS_INT beg, end;
5447 Lisp_Object val, overlay;
5448
5449 /* If newline is part of a composition, continue from start of composition */
5450 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5451 && beg < IT_CHARPOS (*it))
5452 goto replaced;
5453
5454 /* If newline is replaced by a display property, find start of overlay
5455 or interval and continue search from that point. */
5456 it2 = *it;
5457 pos = --IT_CHARPOS (it2);
5458 --IT_BYTEPOS (it2);
5459 it2.sp = 0;
5460 it2.string_from_display_prop_p = 0;
5461 if (handle_display_prop (&it2) == HANDLED_RETURN
5462 && !NILP (val = get_char_property_and_overlay
5463 (make_number (pos), Qdisplay, Qnil, &overlay))
5464 && (OVERLAYP (overlay)
5465 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5466 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5467 goto replaced;
5468
5469 /* Newline is not replaced by anything -- so we are done. */
5470 break;
5471
5472 replaced:
5473 if (beg < BEGV)
5474 beg = BEGV;
5475 IT_CHARPOS (*it) = beg;
5476 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5477 }
5478 }
5479
5480 it->continuation_lines_width = 0;
5481
5482 xassert (IT_CHARPOS (*it) >= BEGV);
5483 xassert (IT_CHARPOS (*it) == BEGV
5484 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5485 CHECK_IT (it);
5486 }
5487
5488
5489 /* Reseat iterator IT at the previous visible line start. Skip
5490 invisible text that is so either due to text properties or due to
5491 selective display. At the end, update IT's overlay information,
5492 face information etc. */
5493
5494 void
5495 reseat_at_previous_visible_line_start (struct it *it)
5496 {
5497 back_to_previous_visible_line_start (it);
5498 reseat (it, it->current.pos, 1);
5499 CHECK_IT (it);
5500 }
5501
5502
5503 /* Reseat iterator IT on the next visible line start in the current
5504 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5505 preceding the line start. Skip over invisible text that is so
5506 because of selective display. Compute faces, overlays etc at the
5507 new position. Note that this function does not skip over text that
5508 is invisible because of text properties. */
5509
5510 static void
5511 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5512 {
5513 int newline_found_p, skipped_p = 0;
5514
5515 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5516
5517 /* Skip over lines that are invisible because they are indented
5518 more than the value of IT->selective. */
5519 if (it->selective > 0)
5520 while (IT_CHARPOS (*it) < ZV
5521 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5522 (double) it->selective)) /* iftc */
5523 {
5524 xassert (IT_BYTEPOS (*it) == BEGV
5525 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5526 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5527 }
5528
5529 /* Position on the newline if that's what's requested. */
5530 if (on_newline_p && newline_found_p)
5531 {
5532 if (STRINGP (it->string))
5533 {
5534 if (IT_STRING_CHARPOS (*it) > 0)
5535 {
5536 --IT_STRING_CHARPOS (*it);
5537 --IT_STRING_BYTEPOS (*it);
5538 }
5539 }
5540 else if (IT_CHARPOS (*it) > BEGV)
5541 {
5542 --IT_CHARPOS (*it);
5543 --IT_BYTEPOS (*it);
5544 reseat (it, it->current.pos, 0);
5545 }
5546 }
5547 else if (skipped_p)
5548 reseat (it, it->current.pos, 0);
5549
5550 CHECK_IT (it);
5551 }
5552
5553
5554 \f
5555 /***********************************************************************
5556 Changing an iterator's position
5557 ***********************************************************************/
5558
5559 /* Change IT's current position to POS in current_buffer. If FORCE_P
5560 is non-zero, always check for text properties at the new position.
5561 Otherwise, text properties are only looked up if POS >=
5562 IT->check_charpos of a property. */
5563
5564 static void
5565 reseat (struct it *it, struct text_pos pos, int force_p)
5566 {
5567 EMACS_INT original_pos = IT_CHARPOS (*it);
5568
5569 reseat_1 (it, pos, 0);
5570
5571 /* Determine where to check text properties. Avoid doing it
5572 where possible because text property lookup is very expensive. */
5573 if (force_p
5574 || CHARPOS (pos) > it->stop_charpos
5575 || CHARPOS (pos) < original_pos)
5576 {
5577 if (it->bidi_p)
5578 {
5579 /* For bidi iteration, we need to prime prev_stop and
5580 base_level_stop with our best estimations. */
5581 if (CHARPOS (pos) < it->prev_stop)
5582 {
5583 handle_stop_backwards (it, BEGV);
5584 if (CHARPOS (pos) < it->base_level_stop)
5585 it->base_level_stop = 0;
5586 }
5587 else if (CHARPOS (pos) > it->stop_charpos
5588 && it->stop_charpos >= BEGV)
5589 handle_stop_backwards (it, it->stop_charpos);
5590 else /* force_p */
5591 handle_stop (it);
5592 }
5593 else
5594 {
5595 handle_stop (it);
5596 it->prev_stop = it->base_level_stop = 0;
5597 }
5598
5599 }
5600
5601 CHECK_IT (it);
5602 }
5603
5604
5605 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5606 IT->stop_pos to POS, also. */
5607
5608 static void
5609 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5610 {
5611 /* Don't call this function when scanning a C string. */
5612 xassert (it->s == NULL);
5613
5614 /* POS must be a reasonable value. */
5615 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5616
5617 it->current.pos = it->position = pos;
5618 it->end_charpos = ZV;
5619 it->dpvec = NULL;
5620 it->current.dpvec_index = -1;
5621 it->current.overlay_string_index = -1;
5622 IT_STRING_CHARPOS (*it) = -1;
5623 IT_STRING_BYTEPOS (*it) = -1;
5624 it->string = Qnil;
5625 it->string_from_display_prop_p = 0;
5626 it->method = GET_FROM_BUFFER;
5627 it->object = it->w->buffer;
5628 it->area = TEXT_AREA;
5629 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5630 it->sp = 0;
5631 it->string_from_display_prop_p = 0;
5632 it->face_before_selective_p = 0;
5633 if (it->bidi_p)
5634 {
5635 it->bidi_it.first_elt = 1;
5636 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5637 }
5638
5639 if (set_stop_p)
5640 {
5641 it->stop_charpos = CHARPOS (pos);
5642 it->base_level_stop = CHARPOS (pos);
5643 }
5644 }
5645
5646
5647 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5648 If S is non-null, it is a C string to iterate over. Otherwise,
5649 STRING gives a Lisp string to iterate over.
5650
5651 If PRECISION > 0, don't return more then PRECISION number of
5652 characters from the string.
5653
5654 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5655 characters have been returned. FIELD_WIDTH < 0 means an infinite
5656 field width.
5657
5658 MULTIBYTE = 0 means disable processing of multibyte characters,
5659 MULTIBYTE > 0 means enable it,
5660 MULTIBYTE < 0 means use IT->multibyte_p.
5661
5662 IT must be initialized via a prior call to init_iterator before
5663 calling this function. */
5664
5665 static void
5666 reseat_to_string (struct it *it, const unsigned char *s, Lisp_Object string,
5667 EMACS_INT charpos, EMACS_INT precision, int field_width,
5668 int multibyte)
5669 {
5670 /* No region in strings. */
5671 it->region_beg_charpos = it->region_end_charpos = -1;
5672
5673 /* No text property checks performed by default, but see below. */
5674 it->stop_charpos = -1;
5675
5676 /* Set iterator position and end position. */
5677 memset (&it->current, 0, sizeof it->current);
5678 it->current.overlay_string_index = -1;
5679 it->current.dpvec_index = -1;
5680 xassert (charpos >= 0);
5681
5682 /* If STRING is specified, use its multibyteness, otherwise use the
5683 setting of MULTIBYTE, if specified. */
5684 if (multibyte >= 0)
5685 it->multibyte_p = multibyte > 0;
5686
5687 if (s == NULL)
5688 {
5689 xassert (STRINGP (string));
5690 it->string = string;
5691 it->s = NULL;
5692 it->end_charpos = it->string_nchars = SCHARS (string);
5693 it->method = GET_FROM_STRING;
5694 it->current.string_pos = string_pos (charpos, string);
5695 }
5696 else
5697 {
5698 it->s = s;
5699 it->string = Qnil;
5700
5701 /* Note that we use IT->current.pos, not it->current.string_pos,
5702 for displaying C strings. */
5703 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5704 if (it->multibyte_p)
5705 {
5706 it->current.pos = c_string_pos (charpos, s, 1);
5707 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5708 }
5709 else
5710 {
5711 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5712 it->end_charpos = it->string_nchars = strlen (s);
5713 }
5714
5715 it->method = GET_FROM_C_STRING;
5716 }
5717
5718 /* PRECISION > 0 means don't return more than PRECISION characters
5719 from the string. */
5720 if (precision > 0 && it->end_charpos - charpos > precision)
5721 it->end_charpos = it->string_nchars = charpos + precision;
5722
5723 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5724 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5725 FIELD_WIDTH < 0 means infinite field width. This is useful for
5726 padding with `-' at the end of a mode line. */
5727 if (field_width < 0)
5728 field_width = INFINITY;
5729 if (field_width > it->end_charpos - charpos)
5730 it->end_charpos = charpos + field_width;
5731
5732 /* Use the standard display table for displaying strings. */
5733 if (DISP_TABLE_P (Vstandard_display_table))
5734 it->dp = XCHAR_TABLE (Vstandard_display_table);
5735
5736 it->stop_charpos = charpos;
5737 if (s == NULL && it->multibyte_p)
5738 {
5739 EMACS_INT endpos = SCHARS (it->string);
5740 if (endpos > it->end_charpos)
5741 endpos = it->end_charpos;
5742 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5743 it->string);
5744 }
5745 CHECK_IT (it);
5746 }
5747
5748
5749 \f
5750 /***********************************************************************
5751 Iteration
5752 ***********************************************************************/
5753
5754 /* Map enum it_method value to corresponding next_element_from_* function. */
5755
5756 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5757 {
5758 next_element_from_buffer,
5759 next_element_from_display_vector,
5760 next_element_from_string,
5761 next_element_from_c_string,
5762 next_element_from_image,
5763 next_element_from_stretch
5764 };
5765
5766 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5767
5768
5769 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5770 (possibly with the following characters). */
5771
5772 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5773 ((IT)->cmp_it.id >= 0 \
5774 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5775 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5776 END_CHARPOS, (IT)->w, \
5777 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5778 (IT)->string)))
5779
5780
5781 /* Lookup the char-table Vglyphless_char_display for character C (-1
5782 if we want information for no-font case), and return the display
5783 method symbol. By side-effect, update it->what and
5784 it->glyphless_method. This function is called from
5785 get_next_display_element for each character element, and from
5786 x_produce_glyphs when no suitable font was found. */
5787
5788 Lisp_Object
5789 lookup_glyphless_char_display (int c, struct it *it)
5790 {
5791 Lisp_Object glyphless_method = Qnil;
5792
5793 if (CHAR_TABLE_P (Vglyphless_char_display)
5794 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5795 glyphless_method = (c >= 0
5796 ? CHAR_TABLE_REF (Vglyphless_char_display, c)
5797 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
5798 retry:
5799 if (NILP (glyphless_method))
5800 {
5801 if (c >= 0)
5802 /* The default is to display the character by a proper font. */
5803 return Qnil;
5804 /* The default for the no-font case is to display an empty box. */
5805 glyphless_method = Qempty_box;
5806 }
5807 if (EQ (glyphless_method, Qzero_width))
5808 {
5809 if (c >= 0)
5810 return glyphless_method;
5811 /* This method can't be used for the no-font case. */
5812 glyphless_method = Qempty_box;
5813 }
5814 if (EQ (glyphless_method, Qthin_space))
5815 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
5816 else if (EQ (glyphless_method, Qempty_box))
5817 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
5818 else if (EQ (glyphless_method, Qhex_code))
5819 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
5820 else if (STRINGP (glyphless_method))
5821 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
5822 else
5823 {
5824 /* Invalid value. We use the default method. */
5825 glyphless_method = Qnil;
5826 goto retry;
5827 }
5828 it->what = IT_GLYPHLESS;
5829 return glyphless_method;
5830 }
5831
5832 /* Load IT's display element fields with information about the next
5833 display element from the current position of IT. Value is zero if
5834 end of buffer (or C string) is reached. */
5835
5836 static struct frame *last_escape_glyph_frame = NULL;
5837 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5838 static int last_escape_glyph_merged_face_id = 0;
5839
5840 struct frame *last_glyphless_glyph_frame = NULL;
5841 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
5842 int last_glyphless_glyph_merged_face_id = 0;
5843
5844 int
5845 get_next_display_element (struct it *it)
5846 {
5847 /* Non-zero means that we found a display element. Zero means that
5848 we hit the end of what we iterate over. Performance note: the
5849 function pointer `method' used here turns out to be faster than
5850 using a sequence of if-statements. */
5851 int success_p;
5852
5853 get_next:
5854 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5855
5856 if (it->what == IT_CHARACTER)
5857 {
5858 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5859 and only if (a) the resolved directionality of that character
5860 is R..." */
5861 /* FIXME: Do we need an exception for characters from display
5862 tables? */
5863 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5864 it->c = bidi_mirror_char (it->c);
5865 /* Map via display table or translate control characters.
5866 IT->c, IT->len etc. have been set to the next character by
5867 the function call above. If we have a display table, and it
5868 contains an entry for IT->c, translate it. Don't do this if
5869 IT->c itself comes from a display table, otherwise we could
5870 end up in an infinite recursion. (An alternative could be to
5871 count the recursion depth of this function and signal an
5872 error when a certain maximum depth is reached.) Is it worth
5873 it? */
5874 if (success_p && it->dpvec == NULL)
5875 {
5876 Lisp_Object dv;
5877 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5878 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5879 nbsp_or_shy = char_is_other;
5880 int c = it->c; /* This is the character to display. */
5881
5882 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5883 {
5884 xassert (SINGLE_BYTE_CHAR_P (c));
5885 if (unibyte_display_via_language_environment)
5886 {
5887 c = DECODE_CHAR (unibyte, c);
5888 if (c < 0)
5889 c = BYTE8_TO_CHAR (it->c);
5890 }
5891 else
5892 c = BYTE8_TO_CHAR (it->c);
5893 }
5894
5895 if (it->dp
5896 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5897 VECTORP (dv)))
5898 {
5899 struct Lisp_Vector *v = XVECTOR (dv);
5900
5901 /* Return the first character from the display table
5902 entry, if not empty. If empty, don't display the
5903 current character. */
5904 if (v->size)
5905 {
5906 it->dpvec_char_len = it->len;
5907 it->dpvec = v->contents;
5908 it->dpend = v->contents + v->size;
5909 it->current.dpvec_index = 0;
5910 it->dpvec_face_id = -1;
5911 it->saved_face_id = it->face_id;
5912 it->method = GET_FROM_DISPLAY_VECTOR;
5913 it->ellipsis_p = 0;
5914 }
5915 else
5916 {
5917 set_iterator_to_next (it, 0);
5918 }
5919 goto get_next;
5920 }
5921
5922 if (! NILP (lookup_glyphless_char_display (c, it)))
5923 {
5924 if (it->what == IT_GLYPHLESS)
5925 goto done;
5926 /* Don't display this character. */
5927 set_iterator_to_next (it, 0);
5928 goto get_next;
5929 }
5930
5931 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5932 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5933 : c == 0xAD ? char_is_soft_hyphen
5934 : char_is_other);
5935
5936 /* Translate control characters into `\003' or `^C' form.
5937 Control characters coming from a display table entry are
5938 currently not translated because we use IT->dpvec to hold
5939 the translation. This could easily be changed but I
5940 don't believe that it is worth doing.
5941
5942 NBSP and SOFT-HYPEN are property translated too.
5943
5944 Non-printable characters and raw-byte characters are also
5945 translated to octal form. */
5946 if (((c < ' ' || c == 127) /* ASCII control chars */
5947 ? (it->area != TEXT_AREA
5948 /* In mode line, treat \n, \t like other crl chars. */
5949 || (c != '\t'
5950 && it->glyph_row
5951 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5952 || (c != '\n' && c != '\t'))
5953 : (nbsp_or_shy
5954 || CHAR_BYTE8_P (c)
5955 || ! CHAR_PRINTABLE_P (c))))
5956 {
5957 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5958 or a non-printable character which must be displayed
5959 either as '\003' or as `^C' where the '\\' and '^'
5960 can be defined in the display table. Fill
5961 IT->ctl_chars with glyphs for what we have to
5962 display. Then, set IT->dpvec to these glyphs. */
5963 Lisp_Object gc;
5964 int ctl_len;
5965 int face_id, lface_id = 0 ;
5966 int escape_glyph;
5967
5968 /* Handle control characters with ^. */
5969
5970 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5971 {
5972 int g;
5973
5974 g = '^'; /* default glyph for Control */
5975 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5976 if (it->dp
5977 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5978 && GLYPH_CODE_CHAR_VALID_P (gc))
5979 {
5980 g = GLYPH_CODE_CHAR (gc);
5981 lface_id = GLYPH_CODE_FACE (gc);
5982 }
5983 if (lface_id)
5984 {
5985 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5986 }
5987 else if (it->f == last_escape_glyph_frame
5988 && it->face_id == last_escape_glyph_face_id)
5989 {
5990 face_id = last_escape_glyph_merged_face_id;
5991 }
5992 else
5993 {
5994 /* Merge the escape-glyph face into the current face. */
5995 face_id = merge_faces (it->f, Qescape_glyph, 0,
5996 it->face_id);
5997 last_escape_glyph_frame = it->f;
5998 last_escape_glyph_face_id = it->face_id;
5999 last_escape_glyph_merged_face_id = face_id;
6000 }
6001
6002 XSETINT (it->ctl_chars[0], g);
6003 XSETINT (it->ctl_chars[1], c ^ 0100);
6004 ctl_len = 2;
6005 goto display_control;
6006 }
6007
6008 /* Handle non-break space in the mode where it only gets
6009 highlighting. */
6010
6011 if (EQ (Vnobreak_char_display, Qt)
6012 && nbsp_or_shy == char_is_nbsp)
6013 {
6014 /* Merge the no-break-space face into the current face. */
6015 face_id = merge_faces (it->f, Qnobreak_space, 0,
6016 it->face_id);
6017
6018 c = ' ';
6019 XSETINT (it->ctl_chars[0], ' ');
6020 ctl_len = 1;
6021 goto display_control;
6022 }
6023
6024 /* Handle sequences that start with the "escape glyph". */
6025
6026 /* the default escape glyph is \. */
6027 escape_glyph = '\\';
6028
6029 if (it->dp
6030 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6031 && GLYPH_CODE_CHAR_VALID_P (gc))
6032 {
6033 escape_glyph = GLYPH_CODE_CHAR (gc);
6034 lface_id = GLYPH_CODE_FACE (gc);
6035 }
6036 if (lface_id)
6037 {
6038 /* The display table specified a face.
6039 Merge it into face_id and also into escape_glyph. */
6040 face_id = merge_faces (it->f, Qt, lface_id,
6041 it->face_id);
6042 }
6043 else if (it->f == last_escape_glyph_frame
6044 && it->face_id == last_escape_glyph_face_id)
6045 {
6046 face_id = last_escape_glyph_merged_face_id;
6047 }
6048 else
6049 {
6050 /* Merge the escape-glyph face into the current face. */
6051 face_id = merge_faces (it->f, Qescape_glyph, 0,
6052 it->face_id);
6053 last_escape_glyph_frame = it->f;
6054 last_escape_glyph_face_id = it->face_id;
6055 last_escape_glyph_merged_face_id = face_id;
6056 }
6057
6058 /* Handle soft hyphens in the mode where they only get
6059 highlighting. */
6060
6061 if (EQ (Vnobreak_char_display, Qt)
6062 && nbsp_or_shy == char_is_soft_hyphen)
6063 {
6064 XSETINT (it->ctl_chars[0], '-');
6065 ctl_len = 1;
6066 goto display_control;
6067 }
6068
6069 /* Handle non-break space and soft hyphen
6070 with the escape glyph. */
6071
6072 if (nbsp_or_shy)
6073 {
6074 XSETINT (it->ctl_chars[0], escape_glyph);
6075 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6076 XSETINT (it->ctl_chars[1], c);
6077 ctl_len = 2;
6078 goto display_control;
6079 }
6080
6081 {
6082 char str[10];
6083 int len, i;
6084
6085 if (CHAR_BYTE8_P (c))
6086 /* Display \200 instead of \17777600. */
6087 c = CHAR_TO_BYTE8 (c);
6088 len = sprintf (str, "%03o", c);
6089
6090 XSETINT (it->ctl_chars[0], escape_glyph);
6091 for (i = 0; i < len; i++)
6092 XSETINT (it->ctl_chars[i + 1], str[i]);
6093 ctl_len = len + 1;
6094 }
6095
6096 display_control:
6097 /* Set up IT->dpvec and return first character from it. */
6098 it->dpvec_char_len = it->len;
6099 it->dpvec = it->ctl_chars;
6100 it->dpend = it->dpvec + ctl_len;
6101 it->current.dpvec_index = 0;
6102 it->dpvec_face_id = face_id;
6103 it->saved_face_id = it->face_id;
6104 it->method = GET_FROM_DISPLAY_VECTOR;
6105 it->ellipsis_p = 0;
6106 goto get_next;
6107 }
6108 it->char_to_display = c;
6109 }
6110 else if (success_p)
6111 {
6112 it->char_to_display = it->c;
6113 }
6114 }
6115
6116 #ifdef HAVE_WINDOW_SYSTEM
6117 /* Adjust face id for a multibyte character. There are no multibyte
6118 character in unibyte text. */
6119 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6120 && it->multibyte_p
6121 && success_p
6122 && FRAME_WINDOW_P (it->f))
6123 {
6124 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6125
6126 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6127 {
6128 /* Automatic composition with glyph-string. */
6129 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6130
6131 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6132 }
6133 else
6134 {
6135 EMACS_INT pos = (it->s ? -1
6136 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6137 : IT_CHARPOS (*it));
6138
6139 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6140 it->string);
6141 }
6142 }
6143 #endif
6144
6145 done:
6146 /* Is this character the last one of a run of characters with
6147 box? If yes, set IT->end_of_box_run_p to 1. */
6148 if (it->face_box_p
6149 && it->s == NULL)
6150 {
6151 if (it->method == GET_FROM_STRING && it->sp)
6152 {
6153 int face_id = underlying_face_id (it);
6154 struct face *face = FACE_FROM_ID (it->f, face_id);
6155
6156 if (face)
6157 {
6158 if (face->box == FACE_NO_BOX)
6159 {
6160 /* If the box comes from face properties in a
6161 display string, check faces in that string. */
6162 int string_face_id = face_after_it_pos (it);
6163 it->end_of_box_run_p
6164 = (FACE_FROM_ID (it->f, string_face_id)->box
6165 == FACE_NO_BOX);
6166 }
6167 /* Otherwise, the box comes from the underlying face.
6168 If this is the last string character displayed, check
6169 the next buffer location. */
6170 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6171 && (it->current.overlay_string_index
6172 == it->n_overlay_strings - 1))
6173 {
6174 EMACS_INT ignore;
6175 int next_face_id;
6176 struct text_pos pos = it->current.pos;
6177 INC_TEXT_POS (pos, it->multibyte_p);
6178
6179 next_face_id = face_at_buffer_position
6180 (it->w, CHARPOS (pos), it->region_beg_charpos,
6181 it->region_end_charpos, &ignore,
6182 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6183 -1);
6184 it->end_of_box_run_p
6185 = (FACE_FROM_ID (it->f, next_face_id)->box
6186 == FACE_NO_BOX);
6187 }
6188 }
6189 }
6190 else
6191 {
6192 int face_id = face_after_it_pos (it);
6193 it->end_of_box_run_p
6194 = (face_id != it->face_id
6195 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6196 }
6197 }
6198
6199 /* Value is 0 if end of buffer or string reached. */
6200 return success_p;
6201 }
6202
6203
6204 /* Move IT to the next display element.
6205
6206 RESEAT_P non-zero means if called on a newline in buffer text,
6207 skip to the next visible line start.
6208
6209 Functions get_next_display_element and set_iterator_to_next are
6210 separate because I find this arrangement easier to handle than a
6211 get_next_display_element function that also increments IT's
6212 position. The way it is we can first look at an iterator's current
6213 display element, decide whether it fits on a line, and if it does,
6214 increment the iterator position. The other way around we probably
6215 would either need a flag indicating whether the iterator has to be
6216 incremented the next time, or we would have to implement a
6217 decrement position function which would not be easy to write. */
6218
6219 void
6220 set_iterator_to_next (struct it *it, int reseat_p)
6221 {
6222 /* Reset flags indicating start and end of a sequence of characters
6223 with box. Reset them at the start of this function because
6224 moving the iterator to a new position might set them. */
6225 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6226
6227 switch (it->method)
6228 {
6229 case GET_FROM_BUFFER:
6230 /* The current display element of IT is a character from
6231 current_buffer. Advance in the buffer, and maybe skip over
6232 invisible lines that are so because of selective display. */
6233 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6234 reseat_at_next_visible_line_start (it, 0);
6235 else if (it->cmp_it.id >= 0)
6236 {
6237 /* We are currently getting glyphs from a composition. */
6238 int i;
6239
6240 if (! it->bidi_p)
6241 {
6242 IT_CHARPOS (*it) += it->cmp_it.nchars;
6243 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6244 if (it->cmp_it.to < it->cmp_it.nglyphs)
6245 {
6246 it->cmp_it.from = it->cmp_it.to;
6247 }
6248 else
6249 {
6250 it->cmp_it.id = -1;
6251 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6252 IT_BYTEPOS (*it),
6253 it->end_charpos, Qnil);
6254 }
6255 }
6256 else if (! it->cmp_it.reversed_p)
6257 {
6258 /* Composition created while scanning forward. */
6259 /* Update IT's char/byte positions to point to the first
6260 character of the next grapheme cluster, or to the
6261 character visually after the current composition. */
6262 for (i = 0; i < it->cmp_it.nchars; i++)
6263 bidi_move_to_visually_next (&it->bidi_it);
6264 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6265 IT_CHARPOS (*it) = it->bidi_it.charpos;
6266
6267 if (it->cmp_it.to < it->cmp_it.nglyphs)
6268 {
6269 /* Proceed to the next grapheme cluster. */
6270 it->cmp_it.from = it->cmp_it.to;
6271 }
6272 else
6273 {
6274 /* No more grapheme clusters in this composition.
6275 Find the next stop position. */
6276 EMACS_INT stop = it->end_charpos;
6277 if (it->bidi_it.scan_dir < 0)
6278 /* Now we are scanning backward and don't know
6279 where to stop. */
6280 stop = -1;
6281 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6282 IT_BYTEPOS (*it), stop, Qnil);
6283 }
6284 }
6285 else
6286 {
6287 /* Composition created while scanning backward. */
6288 /* Update IT's char/byte positions to point to the last
6289 character of the previous grapheme cluster, or the
6290 character visually after the current composition. */
6291 for (i = 0; i < it->cmp_it.nchars; i++)
6292 bidi_move_to_visually_next (&it->bidi_it);
6293 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6294 IT_CHARPOS (*it) = it->bidi_it.charpos;
6295 if (it->cmp_it.from > 0)
6296 {
6297 /* Proceed to the previous grapheme cluster. */
6298 it->cmp_it.to = it->cmp_it.from;
6299 }
6300 else
6301 {
6302 /* No more grapheme clusters in this composition.
6303 Find the next stop position. */
6304 EMACS_INT stop = it->end_charpos;
6305 if (it->bidi_it.scan_dir < 0)
6306 /* Now we are scanning backward and don't know
6307 where to stop. */
6308 stop = -1;
6309 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6310 IT_BYTEPOS (*it), stop, Qnil);
6311 }
6312 }
6313 }
6314 else
6315 {
6316 xassert (it->len != 0);
6317
6318 if (!it->bidi_p)
6319 {
6320 IT_BYTEPOS (*it) += it->len;
6321 IT_CHARPOS (*it) += 1;
6322 }
6323 else
6324 {
6325 int prev_scan_dir = it->bidi_it.scan_dir;
6326 /* If this is a new paragraph, determine its base
6327 direction (a.k.a. its base embedding level). */
6328 if (it->bidi_it.new_paragraph)
6329 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6330 bidi_move_to_visually_next (&it->bidi_it);
6331 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6332 IT_CHARPOS (*it) = it->bidi_it.charpos;
6333 if (prev_scan_dir != it->bidi_it.scan_dir)
6334 {
6335 /* As the scan direction was changed, we must
6336 re-compute the stop position for composition. */
6337 EMACS_INT stop = it->end_charpos;
6338 if (it->bidi_it.scan_dir < 0)
6339 stop = -1;
6340 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6341 IT_BYTEPOS (*it), stop, Qnil);
6342 }
6343 }
6344 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6345 }
6346 break;
6347
6348 case GET_FROM_C_STRING:
6349 /* Current display element of IT is from a C string. */
6350 IT_BYTEPOS (*it) += it->len;
6351 IT_CHARPOS (*it) += 1;
6352 break;
6353
6354 case GET_FROM_DISPLAY_VECTOR:
6355 /* Current display element of IT is from a display table entry.
6356 Advance in the display table definition. Reset it to null if
6357 end reached, and continue with characters from buffers/
6358 strings. */
6359 ++it->current.dpvec_index;
6360
6361 /* Restore face of the iterator to what they were before the
6362 display vector entry (these entries may contain faces). */
6363 it->face_id = it->saved_face_id;
6364
6365 if (it->dpvec + it->current.dpvec_index == it->dpend)
6366 {
6367 int recheck_faces = it->ellipsis_p;
6368
6369 if (it->s)
6370 it->method = GET_FROM_C_STRING;
6371 else if (STRINGP (it->string))
6372 it->method = GET_FROM_STRING;
6373 else
6374 {
6375 it->method = GET_FROM_BUFFER;
6376 it->object = it->w->buffer;
6377 }
6378
6379 it->dpvec = NULL;
6380 it->current.dpvec_index = -1;
6381
6382 /* Skip over characters which were displayed via IT->dpvec. */
6383 if (it->dpvec_char_len < 0)
6384 reseat_at_next_visible_line_start (it, 1);
6385 else if (it->dpvec_char_len > 0)
6386 {
6387 if (it->method == GET_FROM_STRING
6388 && it->n_overlay_strings > 0)
6389 it->ignore_overlay_strings_at_pos_p = 1;
6390 it->len = it->dpvec_char_len;
6391 set_iterator_to_next (it, reseat_p);
6392 }
6393
6394 /* Maybe recheck faces after display vector */
6395 if (recheck_faces)
6396 it->stop_charpos = IT_CHARPOS (*it);
6397 }
6398 break;
6399
6400 case GET_FROM_STRING:
6401 /* Current display element is a character from a Lisp string. */
6402 xassert (it->s == NULL && STRINGP (it->string));
6403 if (it->cmp_it.id >= 0)
6404 {
6405 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6406 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6407 if (it->cmp_it.to < it->cmp_it.nglyphs)
6408 it->cmp_it.from = it->cmp_it.to;
6409 else
6410 {
6411 it->cmp_it.id = -1;
6412 composition_compute_stop_pos (&it->cmp_it,
6413 IT_STRING_CHARPOS (*it),
6414 IT_STRING_BYTEPOS (*it),
6415 it->end_charpos, it->string);
6416 }
6417 }
6418 else
6419 {
6420 IT_STRING_BYTEPOS (*it) += it->len;
6421 IT_STRING_CHARPOS (*it) += 1;
6422 }
6423
6424 consider_string_end:
6425
6426 if (it->current.overlay_string_index >= 0)
6427 {
6428 /* IT->string is an overlay string. Advance to the
6429 next, if there is one. */
6430 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6431 {
6432 it->ellipsis_p = 0;
6433 next_overlay_string (it);
6434 if (it->ellipsis_p)
6435 setup_for_ellipsis (it, 0);
6436 }
6437 }
6438 else
6439 {
6440 /* IT->string is not an overlay string. If we reached
6441 its end, and there is something on IT->stack, proceed
6442 with what is on the stack. This can be either another
6443 string, this time an overlay string, or a buffer. */
6444 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6445 && it->sp > 0)
6446 {
6447 pop_it (it);
6448 if (it->method == GET_FROM_STRING)
6449 goto consider_string_end;
6450 }
6451 }
6452 break;
6453
6454 case GET_FROM_IMAGE:
6455 case GET_FROM_STRETCH:
6456 /* The position etc with which we have to proceed are on
6457 the stack. The position may be at the end of a string,
6458 if the `display' property takes up the whole string. */
6459 xassert (it->sp > 0);
6460 pop_it (it);
6461 if (it->method == GET_FROM_STRING)
6462 goto consider_string_end;
6463 break;
6464
6465 default:
6466 /* There are no other methods defined, so this should be a bug. */
6467 abort ();
6468 }
6469
6470 xassert (it->method != GET_FROM_STRING
6471 || (STRINGP (it->string)
6472 && IT_STRING_CHARPOS (*it) >= 0));
6473 }
6474
6475 /* Load IT's display element fields with information about the next
6476 display element which comes from a display table entry or from the
6477 result of translating a control character to one of the forms `^C'
6478 or `\003'.
6479
6480 IT->dpvec holds the glyphs to return as characters.
6481 IT->saved_face_id holds the face id before the display vector--it
6482 is restored into IT->face_id in set_iterator_to_next. */
6483
6484 static int
6485 next_element_from_display_vector (struct it *it)
6486 {
6487 Lisp_Object gc;
6488
6489 /* Precondition. */
6490 xassert (it->dpvec && it->current.dpvec_index >= 0);
6491
6492 it->face_id = it->saved_face_id;
6493
6494 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6495 That seemed totally bogus - so I changed it... */
6496 gc = it->dpvec[it->current.dpvec_index];
6497
6498 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6499 {
6500 it->c = GLYPH_CODE_CHAR (gc);
6501 it->len = CHAR_BYTES (it->c);
6502
6503 /* The entry may contain a face id to use. Such a face id is
6504 the id of a Lisp face, not a realized face. A face id of
6505 zero means no face is specified. */
6506 if (it->dpvec_face_id >= 0)
6507 it->face_id = it->dpvec_face_id;
6508 else
6509 {
6510 int lface_id = GLYPH_CODE_FACE (gc);
6511 if (lface_id > 0)
6512 it->face_id = merge_faces (it->f, Qt, lface_id,
6513 it->saved_face_id);
6514 }
6515 }
6516 else
6517 /* Display table entry is invalid. Return a space. */
6518 it->c = ' ', it->len = 1;
6519
6520 /* Don't change position and object of the iterator here. They are
6521 still the values of the character that had this display table
6522 entry or was translated, and that's what we want. */
6523 it->what = IT_CHARACTER;
6524 return 1;
6525 }
6526
6527
6528 /* Load IT with the next display element from Lisp string IT->string.
6529 IT->current.string_pos is the current position within the string.
6530 If IT->current.overlay_string_index >= 0, the Lisp string is an
6531 overlay string. */
6532
6533 static int
6534 next_element_from_string (struct it *it)
6535 {
6536 struct text_pos position;
6537
6538 xassert (STRINGP (it->string));
6539 xassert (IT_STRING_CHARPOS (*it) >= 0);
6540 position = it->current.string_pos;
6541
6542 /* Time to check for invisible text? */
6543 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6544 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6545 {
6546 handle_stop (it);
6547
6548 /* Since a handler may have changed IT->method, we must
6549 recurse here. */
6550 return GET_NEXT_DISPLAY_ELEMENT (it);
6551 }
6552
6553 if (it->current.overlay_string_index >= 0)
6554 {
6555 /* Get the next character from an overlay string. In overlay
6556 strings, There is no field width or padding with spaces to
6557 do. */
6558 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6559 {
6560 it->what = IT_EOB;
6561 return 0;
6562 }
6563 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6564 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6565 && next_element_from_composition (it))
6566 {
6567 return 1;
6568 }
6569 else if (STRING_MULTIBYTE (it->string))
6570 {
6571 const unsigned char *s = (SDATA (it->string)
6572 + IT_STRING_BYTEPOS (*it));
6573 it->c = string_char_and_length (s, &it->len);
6574 }
6575 else
6576 {
6577 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6578 it->len = 1;
6579 }
6580 }
6581 else
6582 {
6583 /* Get the next character from a Lisp string that is not an
6584 overlay string. Such strings come from the mode line, for
6585 example. We may have to pad with spaces, or truncate the
6586 string. See also next_element_from_c_string. */
6587 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6588 {
6589 it->what = IT_EOB;
6590 return 0;
6591 }
6592 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6593 {
6594 /* Pad with spaces. */
6595 it->c = ' ', it->len = 1;
6596 CHARPOS (position) = BYTEPOS (position) = -1;
6597 }
6598 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6599 IT_STRING_BYTEPOS (*it), it->string_nchars)
6600 && next_element_from_composition (it))
6601 {
6602 return 1;
6603 }
6604 else if (STRING_MULTIBYTE (it->string))
6605 {
6606 const unsigned char *s = (SDATA (it->string)
6607 + IT_STRING_BYTEPOS (*it));
6608 it->c = string_char_and_length (s, &it->len);
6609 }
6610 else
6611 {
6612 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6613 it->len = 1;
6614 }
6615 }
6616
6617 /* Record what we have and where it came from. */
6618 it->what = IT_CHARACTER;
6619 it->object = it->string;
6620 it->position = position;
6621 return 1;
6622 }
6623
6624
6625 /* Load IT with next display element from C string IT->s.
6626 IT->string_nchars is the maximum number of characters to return
6627 from the string. IT->end_charpos may be greater than
6628 IT->string_nchars when this function is called, in which case we
6629 may have to return padding spaces. Value is zero if end of string
6630 reached, including padding spaces. */
6631
6632 static int
6633 next_element_from_c_string (struct it *it)
6634 {
6635 int success_p = 1;
6636
6637 xassert (it->s);
6638 it->what = IT_CHARACTER;
6639 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6640 it->object = Qnil;
6641
6642 /* IT's position can be greater IT->string_nchars in case a field
6643 width or precision has been specified when the iterator was
6644 initialized. */
6645 if (IT_CHARPOS (*it) >= it->end_charpos)
6646 {
6647 /* End of the game. */
6648 it->what = IT_EOB;
6649 success_p = 0;
6650 }
6651 else if (IT_CHARPOS (*it) >= it->string_nchars)
6652 {
6653 /* Pad with spaces. */
6654 it->c = ' ', it->len = 1;
6655 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6656 }
6657 else if (it->multibyte_p)
6658 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6659 else
6660 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6661
6662 return success_p;
6663 }
6664
6665
6666 /* Set up IT to return characters from an ellipsis, if appropriate.
6667 The definition of the ellipsis glyphs may come from a display table
6668 entry. This function fills IT with the first glyph from the
6669 ellipsis if an ellipsis is to be displayed. */
6670
6671 static int
6672 next_element_from_ellipsis (struct it *it)
6673 {
6674 if (it->selective_display_ellipsis_p)
6675 setup_for_ellipsis (it, it->len);
6676 else
6677 {
6678 /* The face at the current position may be different from the
6679 face we find after the invisible text. Remember what it
6680 was in IT->saved_face_id, and signal that it's there by
6681 setting face_before_selective_p. */
6682 it->saved_face_id = it->face_id;
6683 it->method = GET_FROM_BUFFER;
6684 it->object = it->w->buffer;
6685 reseat_at_next_visible_line_start (it, 1);
6686 it->face_before_selective_p = 1;
6687 }
6688
6689 return GET_NEXT_DISPLAY_ELEMENT (it);
6690 }
6691
6692
6693 /* Deliver an image display element. The iterator IT is already
6694 filled with image information (done in handle_display_prop). Value
6695 is always 1. */
6696
6697
6698 static int
6699 next_element_from_image (struct it *it)
6700 {
6701 it->what = IT_IMAGE;
6702 it->ignore_overlay_strings_at_pos_p = 0;
6703 return 1;
6704 }
6705
6706
6707 /* Fill iterator IT with next display element from a stretch glyph
6708 property. IT->object is the value of the text property. Value is
6709 always 1. */
6710
6711 static int
6712 next_element_from_stretch (struct it *it)
6713 {
6714 it->what = IT_STRETCH;
6715 return 1;
6716 }
6717
6718 /* Scan forward from CHARPOS in the current buffer, until we find a
6719 stop position > current IT's position. Then handle the stop
6720 position before that. This is called when we bump into a stop
6721 position while reordering bidirectional text. CHARPOS should be
6722 the last previously processed stop_pos (or BEGV, if none were
6723 processed yet) whose position is less that IT's current
6724 position. */
6725
6726 static void
6727 handle_stop_backwards (struct it *it, EMACS_INT charpos)
6728 {
6729 EMACS_INT where_we_are = IT_CHARPOS (*it);
6730 struct display_pos save_current = it->current;
6731 struct text_pos save_position = it->position;
6732 struct text_pos pos1;
6733 EMACS_INT next_stop;
6734
6735 /* Scan in strict logical order. */
6736 it->bidi_p = 0;
6737 do
6738 {
6739 it->prev_stop = charpos;
6740 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6741 reseat_1 (it, pos1, 0);
6742 compute_stop_pos (it);
6743 /* We must advance forward, right? */
6744 if (it->stop_charpos <= it->prev_stop)
6745 abort ();
6746 charpos = it->stop_charpos;
6747 }
6748 while (charpos <= where_we_are);
6749
6750 next_stop = it->stop_charpos;
6751 it->stop_charpos = it->prev_stop;
6752 it->bidi_p = 1;
6753 it->current = save_current;
6754 it->position = save_position;
6755 handle_stop (it);
6756 it->stop_charpos = next_stop;
6757 }
6758
6759 /* Load IT with the next display element from current_buffer. Value
6760 is zero if end of buffer reached. IT->stop_charpos is the next
6761 position at which to stop and check for text properties or buffer
6762 end. */
6763
6764 static int
6765 next_element_from_buffer (struct it *it)
6766 {
6767 int success_p = 1;
6768
6769 xassert (IT_CHARPOS (*it) >= BEGV);
6770
6771 /* With bidi reordering, the character to display might not be the
6772 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6773 we were reseat()ed to a new buffer position, which is potentially
6774 a different paragraph. */
6775 if (it->bidi_p && it->bidi_it.first_elt)
6776 {
6777 it->bidi_it.charpos = IT_CHARPOS (*it);
6778 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6779 if (it->bidi_it.bytepos == ZV_BYTE)
6780 {
6781 /* Nothing to do, but reset the FIRST_ELT flag, like
6782 bidi_paragraph_init does, because we are not going to
6783 call it. */
6784 it->bidi_it.first_elt = 0;
6785 }
6786 else if (it->bidi_it.bytepos == BEGV_BYTE
6787 /* FIXME: Should support all Unicode line separators. */
6788 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6789 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6790 {
6791 /* If we are at the beginning of a line, we can produce the
6792 next element right away. */
6793 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6794 bidi_move_to_visually_next (&it->bidi_it);
6795 }
6796 else
6797 {
6798 EMACS_INT orig_bytepos = IT_BYTEPOS (*it);
6799
6800 /* We need to prime the bidi iterator starting at the line's
6801 beginning, before we will be able to produce the next
6802 element. */
6803 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6804 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6805 it->bidi_it.charpos = IT_CHARPOS (*it);
6806 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6807 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6808 do
6809 {
6810 /* Now return to buffer position where we were asked to
6811 get the next display element, and produce that. */
6812 bidi_move_to_visually_next (&it->bidi_it);
6813 }
6814 while (it->bidi_it.bytepos != orig_bytepos
6815 && it->bidi_it.bytepos < ZV_BYTE);
6816 }
6817
6818 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6819 /* Adjust IT's position information to where we ended up. */
6820 IT_CHARPOS (*it) = it->bidi_it.charpos;
6821 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6822 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6823 {
6824 EMACS_INT stop = it->end_charpos;
6825 if (it->bidi_it.scan_dir < 0)
6826 stop = -1;
6827 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6828 IT_BYTEPOS (*it), stop, Qnil);
6829 }
6830 }
6831
6832 if (IT_CHARPOS (*it) >= it->stop_charpos)
6833 {
6834 if (IT_CHARPOS (*it) >= it->end_charpos)
6835 {
6836 int overlay_strings_follow_p;
6837
6838 /* End of the game, except when overlay strings follow that
6839 haven't been returned yet. */
6840 if (it->overlay_strings_at_end_processed_p)
6841 overlay_strings_follow_p = 0;
6842 else
6843 {
6844 it->overlay_strings_at_end_processed_p = 1;
6845 overlay_strings_follow_p = get_overlay_strings (it, 0);
6846 }
6847
6848 if (overlay_strings_follow_p)
6849 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6850 else
6851 {
6852 it->what = IT_EOB;
6853 it->position = it->current.pos;
6854 success_p = 0;
6855 }
6856 }
6857 else if (!(!it->bidi_p
6858 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6859 || IT_CHARPOS (*it) == it->stop_charpos))
6860 {
6861 /* With bidi non-linear iteration, we could find ourselves
6862 far beyond the last computed stop_charpos, with several
6863 other stop positions in between that we missed. Scan
6864 them all now, in buffer's logical order, until we find
6865 and handle the last stop_charpos that precedes our
6866 current position. */
6867 handle_stop_backwards (it, it->stop_charpos);
6868 return GET_NEXT_DISPLAY_ELEMENT (it);
6869 }
6870 else
6871 {
6872 if (it->bidi_p)
6873 {
6874 /* Take note of the stop position we just moved across,
6875 for when we will move back across it. */
6876 it->prev_stop = it->stop_charpos;
6877 /* If we are at base paragraph embedding level, take
6878 note of the last stop position seen at this
6879 level. */
6880 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6881 it->base_level_stop = it->stop_charpos;
6882 }
6883 handle_stop (it);
6884 return GET_NEXT_DISPLAY_ELEMENT (it);
6885 }
6886 }
6887 else if (it->bidi_p
6888 /* We can sometimes back up for reasons that have nothing
6889 to do with bidi reordering. E.g., compositions. The
6890 code below is only needed when we are above the base
6891 embedding level, so test for that explicitly. */
6892 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6893 && IT_CHARPOS (*it) < it->prev_stop)
6894 {
6895 if (it->base_level_stop <= 0)
6896 it->base_level_stop = BEGV;
6897 if (IT_CHARPOS (*it) < it->base_level_stop)
6898 abort ();
6899 handle_stop_backwards (it, it->base_level_stop);
6900 return GET_NEXT_DISPLAY_ELEMENT (it);
6901 }
6902 else
6903 {
6904 /* No face changes, overlays etc. in sight, so just return a
6905 character from current_buffer. */
6906 unsigned char *p;
6907 EMACS_INT stop;
6908
6909 /* Maybe run the redisplay end trigger hook. Performance note:
6910 This doesn't seem to cost measurable time. */
6911 if (it->redisplay_end_trigger_charpos
6912 && it->glyph_row
6913 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6914 run_redisplay_end_trigger_hook (it);
6915
6916 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
6917 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6918 stop)
6919 && next_element_from_composition (it))
6920 {
6921 return 1;
6922 }
6923
6924 /* Get the next character, maybe multibyte. */
6925 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6926 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6927 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6928 else
6929 it->c = *p, it->len = 1;
6930
6931 /* Record what we have and where it came from. */
6932 it->what = IT_CHARACTER;
6933 it->object = it->w->buffer;
6934 it->position = it->current.pos;
6935
6936 /* Normally we return the character found above, except when we
6937 really want to return an ellipsis for selective display. */
6938 if (it->selective)
6939 {
6940 if (it->c == '\n')
6941 {
6942 /* A value of selective > 0 means hide lines indented more
6943 than that number of columns. */
6944 if (it->selective > 0
6945 && IT_CHARPOS (*it) + 1 < ZV
6946 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6947 IT_BYTEPOS (*it) + 1,
6948 (double) it->selective)) /* iftc */
6949 {
6950 success_p = next_element_from_ellipsis (it);
6951 it->dpvec_char_len = -1;
6952 }
6953 }
6954 else if (it->c == '\r' && it->selective == -1)
6955 {
6956 /* A value of selective == -1 means that everything from the
6957 CR to the end of the line is invisible, with maybe an
6958 ellipsis displayed for it. */
6959 success_p = next_element_from_ellipsis (it);
6960 it->dpvec_char_len = -1;
6961 }
6962 }
6963 }
6964
6965 /* Value is zero if end of buffer reached. */
6966 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6967 return success_p;
6968 }
6969
6970
6971 /* Run the redisplay end trigger hook for IT. */
6972
6973 static void
6974 run_redisplay_end_trigger_hook (struct it *it)
6975 {
6976 Lisp_Object args[3];
6977
6978 /* IT->glyph_row should be non-null, i.e. we should be actually
6979 displaying something, or otherwise we should not run the hook. */
6980 xassert (it->glyph_row);
6981
6982 /* Set up hook arguments. */
6983 args[0] = Qredisplay_end_trigger_functions;
6984 args[1] = it->window;
6985 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6986 it->redisplay_end_trigger_charpos = 0;
6987
6988 /* Since we are *trying* to run these functions, don't try to run
6989 them again, even if they get an error. */
6990 it->w->redisplay_end_trigger = Qnil;
6991 Frun_hook_with_args (3, args);
6992
6993 /* Notice if it changed the face of the character we are on. */
6994 handle_face_prop (it);
6995 }
6996
6997
6998 /* Deliver a composition display element. Unlike the other
6999 next_element_from_XXX, this function is not registered in the array
7000 get_next_element[]. It is called from next_element_from_buffer and
7001 next_element_from_string when necessary. */
7002
7003 static int
7004 next_element_from_composition (struct it *it)
7005 {
7006 it->what = IT_COMPOSITION;
7007 it->len = it->cmp_it.nbytes;
7008 if (STRINGP (it->string))
7009 {
7010 if (it->c < 0)
7011 {
7012 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7013 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7014 return 0;
7015 }
7016 it->position = it->current.string_pos;
7017 it->object = it->string;
7018 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7019 IT_STRING_BYTEPOS (*it), it->string);
7020 }
7021 else
7022 {
7023 if (it->c < 0)
7024 {
7025 IT_CHARPOS (*it) += it->cmp_it.nchars;
7026 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7027 if (it->bidi_p)
7028 {
7029 if (it->bidi_it.new_paragraph)
7030 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7031 /* Resync the bidi iterator with IT's new position.
7032 FIXME: this doesn't support bidirectional text. */
7033 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7034 bidi_move_to_visually_next (&it->bidi_it);
7035 }
7036 return 0;
7037 }
7038 it->position = it->current.pos;
7039 it->object = it->w->buffer;
7040 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7041 IT_BYTEPOS (*it), Qnil);
7042 }
7043 return 1;
7044 }
7045
7046
7047 \f
7048 /***********************************************************************
7049 Moving an iterator without producing glyphs
7050 ***********************************************************************/
7051
7052 /* Check if iterator is at a position corresponding to a valid buffer
7053 position after some move_it_ call. */
7054
7055 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7056 ((it)->method == GET_FROM_STRING \
7057 ? IT_STRING_CHARPOS (*it) == 0 \
7058 : 1)
7059
7060
7061 /* Move iterator IT to a specified buffer or X position within one
7062 line on the display without producing glyphs.
7063
7064 OP should be a bit mask including some or all of these bits:
7065 MOVE_TO_X: Stop upon reaching x-position TO_X.
7066 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7067 Regardless of OP's value, stop upon reaching the end of the display line.
7068
7069 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7070 This means, in particular, that TO_X includes window's horizontal
7071 scroll amount.
7072
7073 The return value has several possible values that
7074 say what condition caused the scan to stop:
7075
7076 MOVE_POS_MATCH_OR_ZV
7077 - when TO_POS or ZV was reached.
7078
7079 MOVE_X_REACHED
7080 -when TO_X was reached before TO_POS or ZV were reached.
7081
7082 MOVE_LINE_CONTINUED
7083 - when we reached the end of the display area and the line must
7084 be continued.
7085
7086 MOVE_LINE_TRUNCATED
7087 - when we reached the end of the display area and the line is
7088 truncated.
7089
7090 MOVE_NEWLINE_OR_CR
7091 - when we stopped at a line end, i.e. a newline or a CR and selective
7092 display is on. */
7093
7094 static enum move_it_result
7095 move_it_in_display_line_to (struct it *it,
7096 EMACS_INT to_charpos, int to_x,
7097 enum move_operation_enum op)
7098 {
7099 enum move_it_result result = MOVE_UNDEFINED;
7100 struct glyph_row *saved_glyph_row;
7101 struct it wrap_it, atpos_it, atx_it;
7102 int may_wrap = 0;
7103 enum it_method prev_method = it->method;
7104 EMACS_INT prev_pos = IT_CHARPOS (*it);
7105
7106 /* Don't produce glyphs in produce_glyphs. */
7107 saved_glyph_row = it->glyph_row;
7108 it->glyph_row = NULL;
7109
7110 /* Use wrap_it to save a copy of IT wherever a word wrap could
7111 occur. Use atpos_it to save a copy of IT at the desired buffer
7112 position, if found, so that we can scan ahead and check if the
7113 word later overshoots the window edge. Use atx_it similarly, for
7114 pixel positions. */
7115 wrap_it.sp = -1;
7116 atpos_it.sp = -1;
7117 atx_it.sp = -1;
7118
7119 #define BUFFER_POS_REACHED_P() \
7120 ((op & MOVE_TO_POS) != 0 \
7121 && BUFFERP (it->object) \
7122 && (IT_CHARPOS (*it) == to_charpos \
7123 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7124 && (it->method == GET_FROM_BUFFER \
7125 || (it->method == GET_FROM_DISPLAY_VECTOR \
7126 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7127
7128 /* If there's a line-/wrap-prefix, handle it. */
7129 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7130 && it->current_y < it->last_visible_y)
7131 handle_line_prefix (it);
7132
7133 while (1)
7134 {
7135 int x, i, ascent = 0, descent = 0;
7136
7137 /* Utility macro to reset an iterator with x, ascent, and descent. */
7138 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7139 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7140 (IT)->max_descent = descent)
7141
7142 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7143 glyph). */
7144 if ((op & MOVE_TO_POS) != 0
7145 && BUFFERP (it->object)
7146 && it->method == GET_FROM_BUFFER
7147 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7148 || (it->bidi_p
7149 && (prev_method == GET_FROM_IMAGE
7150 || prev_method == GET_FROM_STRETCH)
7151 /* Passed TO_CHARPOS from left to right. */
7152 && ((prev_pos < to_charpos
7153 && IT_CHARPOS (*it) > to_charpos)
7154 /* Passed TO_CHARPOS from right to left. */
7155 || (prev_pos > to_charpos
7156 && IT_CHARPOS (*it) < to_charpos)))))
7157 {
7158 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7159 {
7160 result = MOVE_POS_MATCH_OR_ZV;
7161 break;
7162 }
7163 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7164 /* If wrap_it is valid, the current position might be in a
7165 word that is wrapped. So, save the iterator in
7166 atpos_it and continue to see if wrapping happens. */
7167 atpos_it = *it;
7168 }
7169
7170 prev_method = it->method;
7171 if (it->method == GET_FROM_BUFFER)
7172 prev_pos = IT_CHARPOS (*it);
7173 /* Stop when ZV reached.
7174 We used to stop here when TO_CHARPOS reached as well, but that is
7175 too soon if this glyph does not fit on this line. So we handle it
7176 explicitly below. */
7177 if (!get_next_display_element (it))
7178 {
7179 result = MOVE_POS_MATCH_OR_ZV;
7180 break;
7181 }
7182
7183 if (it->line_wrap == TRUNCATE)
7184 {
7185 if (BUFFER_POS_REACHED_P ())
7186 {
7187 result = MOVE_POS_MATCH_OR_ZV;
7188 break;
7189 }
7190 }
7191 else
7192 {
7193 if (it->line_wrap == WORD_WRAP)
7194 {
7195 if (IT_DISPLAYING_WHITESPACE (it))
7196 may_wrap = 1;
7197 else if (may_wrap)
7198 {
7199 /* We have reached a glyph that follows one or more
7200 whitespace characters. If the position is
7201 already found, we are done. */
7202 if (atpos_it.sp >= 0)
7203 {
7204 *it = atpos_it;
7205 result = MOVE_POS_MATCH_OR_ZV;
7206 goto done;
7207 }
7208 if (atx_it.sp >= 0)
7209 {
7210 *it = atx_it;
7211 result = MOVE_X_REACHED;
7212 goto done;
7213 }
7214 /* Otherwise, we can wrap here. */
7215 wrap_it = *it;
7216 may_wrap = 0;
7217 }
7218 }
7219 }
7220
7221 /* Remember the line height for the current line, in case
7222 the next element doesn't fit on the line. */
7223 ascent = it->max_ascent;
7224 descent = it->max_descent;
7225
7226 /* The call to produce_glyphs will get the metrics of the
7227 display element IT is loaded with. Record the x-position
7228 before this display element, in case it doesn't fit on the
7229 line. */
7230 x = it->current_x;
7231
7232 PRODUCE_GLYPHS (it);
7233
7234 if (it->area != TEXT_AREA)
7235 {
7236 set_iterator_to_next (it, 1);
7237 continue;
7238 }
7239
7240 /* The number of glyphs we get back in IT->nglyphs will normally
7241 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7242 character on a terminal frame, or (iii) a line end. For the
7243 second case, IT->nglyphs - 1 padding glyphs will be present.
7244 (On X frames, there is only one glyph produced for a
7245 composite character.)
7246
7247 The behavior implemented below means, for continuation lines,
7248 that as many spaces of a TAB as fit on the current line are
7249 displayed there. For terminal frames, as many glyphs of a
7250 multi-glyph character are displayed in the current line, too.
7251 This is what the old redisplay code did, and we keep it that
7252 way. Under X, the whole shape of a complex character must
7253 fit on the line or it will be completely displayed in the
7254 next line.
7255
7256 Note that both for tabs and padding glyphs, all glyphs have
7257 the same width. */
7258 if (it->nglyphs)
7259 {
7260 /* More than one glyph or glyph doesn't fit on line. All
7261 glyphs have the same width. */
7262 int single_glyph_width = it->pixel_width / it->nglyphs;
7263 int new_x;
7264 int x_before_this_char = x;
7265 int hpos_before_this_char = it->hpos;
7266
7267 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7268 {
7269 new_x = x + single_glyph_width;
7270
7271 /* We want to leave anything reaching TO_X to the caller. */
7272 if ((op & MOVE_TO_X) && new_x > to_x)
7273 {
7274 if (BUFFER_POS_REACHED_P ())
7275 {
7276 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7277 goto buffer_pos_reached;
7278 if (atpos_it.sp < 0)
7279 {
7280 atpos_it = *it;
7281 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7282 }
7283 }
7284 else
7285 {
7286 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7287 {
7288 it->current_x = x;
7289 result = MOVE_X_REACHED;
7290 break;
7291 }
7292 if (atx_it.sp < 0)
7293 {
7294 atx_it = *it;
7295 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7296 }
7297 }
7298 }
7299
7300 if (/* Lines are continued. */
7301 it->line_wrap != TRUNCATE
7302 && (/* And glyph doesn't fit on the line. */
7303 new_x > it->last_visible_x
7304 /* Or it fits exactly and we're on a window
7305 system frame. */
7306 || (new_x == it->last_visible_x
7307 && FRAME_WINDOW_P (it->f))))
7308 {
7309 if (/* IT->hpos == 0 means the very first glyph
7310 doesn't fit on the line, e.g. a wide image. */
7311 it->hpos == 0
7312 || (new_x == it->last_visible_x
7313 && FRAME_WINDOW_P (it->f)))
7314 {
7315 ++it->hpos;
7316 it->current_x = new_x;
7317
7318 /* The character's last glyph just barely fits
7319 in this row. */
7320 if (i == it->nglyphs - 1)
7321 {
7322 /* If this is the destination position,
7323 return a position *before* it in this row,
7324 now that we know it fits in this row. */
7325 if (BUFFER_POS_REACHED_P ())
7326 {
7327 if (it->line_wrap != WORD_WRAP
7328 || wrap_it.sp < 0)
7329 {
7330 it->hpos = hpos_before_this_char;
7331 it->current_x = x_before_this_char;
7332 result = MOVE_POS_MATCH_OR_ZV;
7333 break;
7334 }
7335 if (it->line_wrap == WORD_WRAP
7336 && atpos_it.sp < 0)
7337 {
7338 atpos_it = *it;
7339 atpos_it.current_x = x_before_this_char;
7340 atpos_it.hpos = hpos_before_this_char;
7341 }
7342 }
7343
7344 set_iterator_to_next (it, 1);
7345 /* On graphical terminals, newlines may
7346 "overflow" into the fringe if
7347 overflow-newline-into-fringe is non-nil.
7348 On text-only terminals, newlines may
7349 overflow into the last glyph on the
7350 display line.*/
7351 if (!FRAME_WINDOW_P (it->f)
7352 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7353 {
7354 if (!get_next_display_element (it))
7355 {
7356 result = MOVE_POS_MATCH_OR_ZV;
7357 break;
7358 }
7359 if (BUFFER_POS_REACHED_P ())
7360 {
7361 if (ITERATOR_AT_END_OF_LINE_P (it))
7362 result = MOVE_POS_MATCH_OR_ZV;
7363 else
7364 result = MOVE_LINE_CONTINUED;
7365 break;
7366 }
7367 if (ITERATOR_AT_END_OF_LINE_P (it))
7368 {
7369 result = MOVE_NEWLINE_OR_CR;
7370 break;
7371 }
7372 }
7373 }
7374 }
7375 else
7376 IT_RESET_X_ASCENT_DESCENT (it);
7377
7378 if (wrap_it.sp >= 0)
7379 {
7380 *it = wrap_it;
7381 atpos_it.sp = -1;
7382 atx_it.sp = -1;
7383 }
7384
7385 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7386 IT_CHARPOS (*it)));
7387 result = MOVE_LINE_CONTINUED;
7388 break;
7389 }
7390
7391 if (BUFFER_POS_REACHED_P ())
7392 {
7393 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7394 goto buffer_pos_reached;
7395 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7396 {
7397 atpos_it = *it;
7398 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7399 }
7400 }
7401
7402 if (new_x > it->first_visible_x)
7403 {
7404 /* Glyph is visible. Increment number of glyphs that
7405 would be displayed. */
7406 ++it->hpos;
7407 }
7408 }
7409
7410 if (result != MOVE_UNDEFINED)
7411 break;
7412 }
7413 else if (BUFFER_POS_REACHED_P ())
7414 {
7415 buffer_pos_reached:
7416 IT_RESET_X_ASCENT_DESCENT (it);
7417 result = MOVE_POS_MATCH_OR_ZV;
7418 break;
7419 }
7420 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7421 {
7422 /* Stop when TO_X specified and reached. This check is
7423 necessary here because of lines consisting of a line end,
7424 only. The line end will not produce any glyphs and we
7425 would never get MOVE_X_REACHED. */
7426 xassert (it->nglyphs == 0);
7427 result = MOVE_X_REACHED;
7428 break;
7429 }
7430
7431 /* Is this a line end? If yes, we're done. */
7432 if (ITERATOR_AT_END_OF_LINE_P (it))
7433 {
7434 result = MOVE_NEWLINE_OR_CR;
7435 break;
7436 }
7437
7438 if (it->method == GET_FROM_BUFFER)
7439 prev_pos = IT_CHARPOS (*it);
7440 /* The current display element has been consumed. Advance
7441 to the next. */
7442 set_iterator_to_next (it, 1);
7443
7444 /* Stop if lines are truncated and IT's current x-position is
7445 past the right edge of the window now. */
7446 if (it->line_wrap == TRUNCATE
7447 && it->current_x >= it->last_visible_x)
7448 {
7449 if (!FRAME_WINDOW_P (it->f)
7450 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7451 {
7452 if (!get_next_display_element (it)
7453 || BUFFER_POS_REACHED_P ())
7454 {
7455 result = MOVE_POS_MATCH_OR_ZV;
7456 break;
7457 }
7458 if (ITERATOR_AT_END_OF_LINE_P (it))
7459 {
7460 result = MOVE_NEWLINE_OR_CR;
7461 break;
7462 }
7463 }
7464 result = MOVE_LINE_TRUNCATED;
7465 break;
7466 }
7467 #undef IT_RESET_X_ASCENT_DESCENT
7468 }
7469
7470 #undef BUFFER_POS_REACHED_P
7471
7472 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7473 restore the saved iterator. */
7474 if (atpos_it.sp >= 0)
7475 *it = atpos_it;
7476 else if (atx_it.sp >= 0)
7477 *it = atx_it;
7478
7479 done:
7480
7481 /* Restore the iterator settings altered at the beginning of this
7482 function. */
7483 it->glyph_row = saved_glyph_row;
7484 return result;
7485 }
7486
7487 /* For external use. */
7488 void
7489 move_it_in_display_line (struct it *it,
7490 EMACS_INT to_charpos, int to_x,
7491 enum move_operation_enum op)
7492 {
7493 if (it->line_wrap == WORD_WRAP
7494 && (op & MOVE_TO_X))
7495 {
7496 struct it save_it = *it;
7497 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7498 /* When word-wrap is on, TO_X may lie past the end
7499 of a wrapped line. Then it->current is the
7500 character on the next line, so backtrack to the
7501 space before the wrap point. */
7502 if (skip == MOVE_LINE_CONTINUED)
7503 {
7504 int prev_x = max (it->current_x - 1, 0);
7505 *it = save_it;
7506 move_it_in_display_line_to
7507 (it, -1, prev_x, MOVE_TO_X);
7508 }
7509 }
7510 else
7511 move_it_in_display_line_to (it, to_charpos, to_x, op);
7512 }
7513
7514
7515 /* Move IT forward until it satisfies one or more of the criteria in
7516 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7517
7518 OP is a bit-mask that specifies where to stop, and in particular,
7519 which of those four position arguments makes a difference. See the
7520 description of enum move_operation_enum.
7521
7522 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7523 screen line, this function will set IT to the next position >
7524 TO_CHARPOS. */
7525
7526 void
7527 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7528 {
7529 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7530 int line_height, line_start_x = 0, reached = 0;
7531
7532 for (;;)
7533 {
7534 if (op & MOVE_TO_VPOS)
7535 {
7536 /* If no TO_CHARPOS and no TO_X specified, stop at the
7537 start of the line TO_VPOS. */
7538 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7539 {
7540 if (it->vpos == to_vpos)
7541 {
7542 reached = 1;
7543 break;
7544 }
7545 else
7546 skip = move_it_in_display_line_to (it, -1, -1, 0);
7547 }
7548 else
7549 {
7550 /* TO_VPOS >= 0 means stop at TO_X in the line at
7551 TO_VPOS, or at TO_POS, whichever comes first. */
7552 if (it->vpos == to_vpos)
7553 {
7554 reached = 2;
7555 break;
7556 }
7557
7558 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7559
7560 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7561 {
7562 reached = 3;
7563 break;
7564 }
7565 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7566 {
7567 /* We have reached TO_X but not in the line we want. */
7568 skip = move_it_in_display_line_to (it, to_charpos,
7569 -1, MOVE_TO_POS);
7570 if (skip == MOVE_POS_MATCH_OR_ZV)
7571 {
7572 reached = 4;
7573 break;
7574 }
7575 }
7576 }
7577 }
7578 else if (op & MOVE_TO_Y)
7579 {
7580 struct it it_backup;
7581
7582 if (it->line_wrap == WORD_WRAP)
7583 it_backup = *it;
7584
7585 /* TO_Y specified means stop at TO_X in the line containing
7586 TO_Y---or at TO_CHARPOS if this is reached first. The
7587 problem is that we can't really tell whether the line
7588 contains TO_Y before we have completely scanned it, and
7589 this may skip past TO_X. What we do is to first scan to
7590 TO_X.
7591
7592 If TO_X is not specified, use a TO_X of zero. The reason
7593 is to make the outcome of this function more predictable.
7594 If we didn't use TO_X == 0, we would stop at the end of
7595 the line which is probably not what a caller would expect
7596 to happen. */
7597 skip = move_it_in_display_line_to
7598 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7599 (MOVE_TO_X | (op & MOVE_TO_POS)));
7600
7601 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7602 if (skip == MOVE_POS_MATCH_OR_ZV)
7603 reached = 5;
7604 else if (skip == MOVE_X_REACHED)
7605 {
7606 /* If TO_X was reached, we want to know whether TO_Y is
7607 in the line. We know this is the case if the already
7608 scanned glyphs make the line tall enough. Otherwise,
7609 we must check by scanning the rest of the line. */
7610 line_height = it->max_ascent + it->max_descent;
7611 if (to_y >= it->current_y
7612 && to_y < it->current_y + line_height)
7613 {
7614 reached = 6;
7615 break;
7616 }
7617 it_backup = *it;
7618 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7619 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7620 op & MOVE_TO_POS);
7621 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7622 line_height = it->max_ascent + it->max_descent;
7623 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7624
7625 if (to_y >= it->current_y
7626 && to_y < it->current_y + line_height)
7627 {
7628 /* If TO_Y is in this line and TO_X was reached
7629 above, we scanned too far. We have to restore
7630 IT's settings to the ones before skipping. */
7631 *it = it_backup;
7632 reached = 6;
7633 }
7634 else
7635 {
7636 skip = skip2;
7637 if (skip == MOVE_POS_MATCH_OR_ZV)
7638 reached = 7;
7639 }
7640 }
7641 else
7642 {
7643 /* Check whether TO_Y is in this line. */
7644 line_height = it->max_ascent + it->max_descent;
7645 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7646
7647 if (to_y >= it->current_y
7648 && to_y < it->current_y + line_height)
7649 {
7650 /* When word-wrap is on, TO_X may lie past the end
7651 of a wrapped line. Then it->current is the
7652 character on the next line, so backtrack to the
7653 space before the wrap point. */
7654 if (skip == MOVE_LINE_CONTINUED
7655 && it->line_wrap == WORD_WRAP)
7656 {
7657 int prev_x = max (it->current_x - 1, 0);
7658 *it = it_backup;
7659 skip = move_it_in_display_line_to
7660 (it, -1, prev_x, MOVE_TO_X);
7661 }
7662 reached = 6;
7663 }
7664 }
7665
7666 if (reached)
7667 break;
7668 }
7669 else if (BUFFERP (it->object)
7670 && (it->method == GET_FROM_BUFFER
7671 || it->method == GET_FROM_STRETCH)
7672 && IT_CHARPOS (*it) >= to_charpos)
7673 skip = MOVE_POS_MATCH_OR_ZV;
7674 else
7675 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7676
7677 switch (skip)
7678 {
7679 case MOVE_POS_MATCH_OR_ZV:
7680 reached = 8;
7681 goto out;
7682
7683 case MOVE_NEWLINE_OR_CR:
7684 set_iterator_to_next (it, 1);
7685 it->continuation_lines_width = 0;
7686 break;
7687
7688 case MOVE_LINE_TRUNCATED:
7689 it->continuation_lines_width = 0;
7690 reseat_at_next_visible_line_start (it, 0);
7691 if ((op & MOVE_TO_POS) != 0
7692 && IT_CHARPOS (*it) > to_charpos)
7693 {
7694 reached = 9;
7695 goto out;
7696 }
7697 break;
7698
7699 case MOVE_LINE_CONTINUED:
7700 /* For continued lines ending in a tab, some of the glyphs
7701 associated with the tab are displayed on the current
7702 line. Since it->current_x does not include these glyphs,
7703 we use it->last_visible_x instead. */
7704 if (it->c == '\t')
7705 {
7706 it->continuation_lines_width += it->last_visible_x;
7707 /* When moving by vpos, ensure that the iterator really
7708 advances to the next line (bug#847, bug#969). Fixme:
7709 do we need to do this in other circumstances? */
7710 if (it->current_x != it->last_visible_x
7711 && (op & MOVE_TO_VPOS)
7712 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7713 {
7714 line_start_x = it->current_x + it->pixel_width
7715 - it->last_visible_x;
7716 set_iterator_to_next (it, 0);
7717 }
7718 }
7719 else
7720 it->continuation_lines_width += it->current_x;
7721 break;
7722
7723 default:
7724 abort ();
7725 }
7726
7727 /* Reset/increment for the next run. */
7728 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7729 it->current_x = line_start_x;
7730 line_start_x = 0;
7731 it->hpos = 0;
7732 it->current_y += it->max_ascent + it->max_descent;
7733 ++it->vpos;
7734 last_height = it->max_ascent + it->max_descent;
7735 last_max_ascent = it->max_ascent;
7736 it->max_ascent = it->max_descent = 0;
7737 }
7738
7739 out:
7740
7741 /* On text terminals, we may stop at the end of a line in the middle
7742 of a multi-character glyph. If the glyph itself is continued,
7743 i.e. it is actually displayed on the next line, don't treat this
7744 stopping point as valid; move to the next line instead (unless
7745 that brings us offscreen). */
7746 if (!FRAME_WINDOW_P (it->f)
7747 && op & MOVE_TO_POS
7748 && IT_CHARPOS (*it) == to_charpos
7749 && it->what == IT_CHARACTER
7750 && it->nglyphs > 1
7751 && it->line_wrap == WINDOW_WRAP
7752 && it->current_x == it->last_visible_x - 1
7753 && it->c != '\n'
7754 && it->c != '\t'
7755 && it->vpos < XFASTINT (it->w->window_end_vpos))
7756 {
7757 it->continuation_lines_width += it->current_x;
7758 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7759 it->current_y += it->max_ascent + it->max_descent;
7760 ++it->vpos;
7761 last_height = it->max_ascent + it->max_descent;
7762 last_max_ascent = it->max_ascent;
7763 }
7764
7765 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7766 }
7767
7768
7769 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7770
7771 If DY > 0, move IT backward at least that many pixels. DY = 0
7772 means move IT backward to the preceding line start or BEGV. This
7773 function may move over more than DY pixels if IT->current_y - DY
7774 ends up in the middle of a line; in this case IT->current_y will be
7775 set to the top of the line moved to. */
7776
7777 void
7778 move_it_vertically_backward (struct it *it, int dy)
7779 {
7780 int nlines, h;
7781 struct it it2, it3;
7782 EMACS_INT start_pos;
7783
7784 move_further_back:
7785 xassert (dy >= 0);
7786
7787 start_pos = IT_CHARPOS (*it);
7788
7789 /* Estimate how many newlines we must move back. */
7790 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7791
7792 /* Set the iterator's position that many lines back. */
7793 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7794 back_to_previous_visible_line_start (it);
7795
7796 /* Reseat the iterator here. When moving backward, we don't want
7797 reseat to skip forward over invisible text, set up the iterator
7798 to deliver from overlay strings at the new position etc. So,
7799 use reseat_1 here. */
7800 reseat_1 (it, it->current.pos, 1);
7801
7802 /* We are now surely at a line start. */
7803 it->current_x = it->hpos = 0;
7804 it->continuation_lines_width = 0;
7805
7806 /* Move forward and see what y-distance we moved. First move to the
7807 start of the next line so that we get its height. We need this
7808 height to be able to tell whether we reached the specified
7809 y-distance. */
7810 it2 = *it;
7811 it2.max_ascent = it2.max_descent = 0;
7812 do
7813 {
7814 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7815 MOVE_TO_POS | MOVE_TO_VPOS);
7816 }
7817 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7818 xassert (IT_CHARPOS (*it) >= BEGV);
7819 it3 = it2;
7820
7821 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7822 xassert (IT_CHARPOS (*it) >= BEGV);
7823 /* H is the actual vertical distance from the position in *IT
7824 and the starting position. */
7825 h = it2.current_y - it->current_y;
7826 /* NLINES is the distance in number of lines. */
7827 nlines = it2.vpos - it->vpos;
7828
7829 /* Correct IT's y and vpos position
7830 so that they are relative to the starting point. */
7831 it->vpos -= nlines;
7832 it->current_y -= h;
7833
7834 if (dy == 0)
7835 {
7836 /* DY == 0 means move to the start of the screen line. The
7837 value of nlines is > 0 if continuation lines were involved. */
7838 if (nlines > 0)
7839 move_it_by_lines (it, nlines, 1);
7840 }
7841 else
7842 {
7843 /* The y-position we try to reach, relative to *IT.
7844 Note that H has been subtracted in front of the if-statement. */
7845 int target_y = it->current_y + h - dy;
7846 int y0 = it3.current_y;
7847 int y1 = line_bottom_y (&it3);
7848 int line_height = y1 - y0;
7849
7850 /* If we did not reach target_y, try to move further backward if
7851 we can. If we moved too far backward, try to move forward. */
7852 if (target_y < it->current_y
7853 /* This is heuristic. In a window that's 3 lines high, with
7854 a line height of 13 pixels each, recentering with point
7855 on the bottom line will try to move -39/2 = 19 pixels
7856 backward. Try to avoid moving into the first line. */
7857 && (it->current_y - target_y
7858 > min (window_box_height (it->w), line_height * 2 / 3))
7859 && IT_CHARPOS (*it) > BEGV)
7860 {
7861 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7862 target_y - it->current_y));
7863 dy = it->current_y - target_y;
7864 goto move_further_back;
7865 }
7866 else if (target_y >= it->current_y + line_height
7867 && IT_CHARPOS (*it) < ZV)
7868 {
7869 /* Should move forward by at least one line, maybe more.
7870
7871 Note: Calling move_it_by_lines can be expensive on
7872 terminal frames, where compute_motion is used (via
7873 vmotion) to do the job, when there are very long lines
7874 and truncate-lines is nil. That's the reason for
7875 treating terminal frames specially here. */
7876
7877 if (!FRAME_WINDOW_P (it->f))
7878 move_it_vertically (it, target_y - (it->current_y + line_height));
7879 else
7880 {
7881 do
7882 {
7883 move_it_by_lines (it, 1, 1);
7884 }
7885 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7886 }
7887 }
7888 }
7889 }
7890
7891
7892 /* Move IT by a specified amount of pixel lines DY. DY negative means
7893 move backwards. DY = 0 means move to start of screen line. At the
7894 end, IT will be on the start of a screen line. */
7895
7896 void
7897 move_it_vertically (struct it *it, int dy)
7898 {
7899 if (dy <= 0)
7900 move_it_vertically_backward (it, -dy);
7901 else
7902 {
7903 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7904 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7905 MOVE_TO_POS | MOVE_TO_Y);
7906 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7907
7908 /* If buffer ends in ZV without a newline, move to the start of
7909 the line to satisfy the post-condition. */
7910 if (IT_CHARPOS (*it) == ZV
7911 && ZV > BEGV
7912 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7913 move_it_by_lines (it, 0, 0);
7914 }
7915 }
7916
7917
7918 /* Move iterator IT past the end of the text line it is in. */
7919
7920 void
7921 move_it_past_eol (struct it *it)
7922 {
7923 enum move_it_result rc;
7924
7925 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7926 if (rc == MOVE_NEWLINE_OR_CR)
7927 set_iterator_to_next (it, 0);
7928 }
7929
7930
7931 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7932 negative means move up. DVPOS == 0 means move to the start of the
7933 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7934 NEED_Y_P is zero, IT->current_y will be left unchanged.
7935
7936 Further optimization ideas: If we would know that IT->f doesn't use
7937 a face with proportional font, we could be faster for
7938 truncate-lines nil. */
7939
7940 void
7941 move_it_by_lines (struct it *it, int dvpos, int need_y_p)
7942 {
7943
7944 /* The commented-out optimization uses vmotion on terminals. This
7945 gives bad results, because elements like it->what, on which
7946 callers such as pos_visible_p rely, aren't updated. */
7947 /* struct position pos;
7948 if (!FRAME_WINDOW_P (it->f))
7949 {
7950 struct text_pos textpos;
7951
7952 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7953 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7954 reseat (it, textpos, 1);
7955 it->vpos += pos.vpos;
7956 it->current_y += pos.vpos;
7957 }
7958 else */
7959
7960 if (dvpos == 0)
7961 {
7962 /* DVPOS == 0 means move to the start of the screen line. */
7963 move_it_vertically_backward (it, 0);
7964 xassert (it->current_x == 0 && it->hpos == 0);
7965 /* Let next call to line_bottom_y calculate real line height */
7966 last_height = 0;
7967 }
7968 else if (dvpos > 0)
7969 {
7970 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7971 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7972 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7973 }
7974 else
7975 {
7976 struct it it2;
7977 EMACS_INT start_charpos, i;
7978
7979 /* Start at the beginning of the screen line containing IT's
7980 position. This may actually move vertically backwards,
7981 in case of overlays, so adjust dvpos accordingly. */
7982 dvpos += it->vpos;
7983 move_it_vertically_backward (it, 0);
7984 dvpos -= it->vpos;
7985
7986 /* Go back -DVPOS visible lines and reseat the iterator there. */
7987 start_charpos = IT_CHARPOS (*it);
7988 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7989 back_to_previous_visible_line_start (it);
7990 reseat (it, it->current.pos, 1);
7991
7992 /* Move further back if we end up in a string or an image. */
7993 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7994 {
7995 /* First try to move to start of display line. */
7996 dvpos += it->vpos;
7997 move_it_vertically_backward (it, 0);
7998 dvpos -= it->vpos;
7999 if (IT_POS_VALID_AFTER_MOVE_P (it))
8000 break;
8001 /* If start of line is still in string or image,
8002 move further back. */
8003 back_to_previous_visible_line_start (it);
8004 reseat (it, it->current.pos, 1);
8005 dvpos--;
8006 }
8007
8008 it->current_x = it->hpos = 0;
8009
8010 /* Above call may have moved too far if continuation lines
8011 are involved. Scan forward and see if it did. */
8012 it2 = *it;
8013 it2.vpos = it2.current_y = 0;
8014 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8015 it->vpos -= it2.vpos;
8016 it->current_y -= it2.current_y;
8017 it->current_x = it->hpos = 0;
8018
8019 /* If we moved too far back, move IT some lines forward. */
8020 if (it2.vpos > -dvpos)
8021 {
8022 int delta = it2.vpos + dvpos;
8023 it2 = *it;
8024 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8025 /* Move back again if we got too far ahead. */
8026 if (IT_CHARPOS (*it) >= start_charpos)
8027 *it = it2;
8028 }
8029 }
8030 }
8031
8032 /* Return 1 if IT points into the middle of a display vector. */
8033
8034 int
8035 in_display_vector_p (struct it *it)
8036 {
8037 return (it->method == GET_FROM_DISPLAY_VECTOR
8038 && it->current.dpvec_index > 0
8039 && it->dpvec + it->current.dpvec_index != it->dpend);
8040 }
8041
8042 \f
8043 /***********************************************************************
8044 Messages
8045 ***********************************************************************/
8046
8047
8048 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8049 to *Messages*. */
8050
8051 void
8052 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8053 {
8054 Lisp_Object args[3];
8055 Lisp_Object msg, fmt;
8056 char *buffer;
8057 EMACS_INT len;
8058 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8059 USE_SAFE_ALLOCA;
8060
8061 /* Do nothing if called asynchronously. Inserting text into
8062 a buffer may call after-change-functions and alike and
8063 that would means running Lisp asynchronously. */
8064 if (handling_signal)
8065 return;
8066
8067 fmt = msg = Qnil;
8068 GCPRO4 (fmt, msg, arg1, arg2);
8069
8070 args[0] = fmt = build_string (format);
8071 args[1] = arg1;
8072 args[2] = arg2;
8073 msg = Fformat (3, args);
8074
8075 len = SBYTES (msg) + 1;
8076 SAFE_ALLOCA (buffer, char *, len);
8077 memcpy (buffer, SDATA (msg), len);
8078
8079 message_dolog (buffer, len - 1, 1, 0);
8080 SAFE_FREE ();
8081
8082 UNGCPRO;
8083 }
8084
8085
8086 /* Output a newline in the *Messages* buffer if "needs" one. */
8087
8088 void
8089 message_log_maybe_newline (void)
8090 {
8091 if (message_log_need_newline)
8092 message_dolog ("", 0, 1, 0);
8093 }
8094
8095
8096 /* Add a string M of length NBYTES to the message log, optionally
8097 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8098 nonzero, means interpret the contents of M as multibyte. This
8099 function calls low-level routines in order to bypass text property
8100 hooks, etc. which might not be safe to run.
8101
8102 This may GC (insert may run before/after change hooks),
8103 so the buffer M must NOT point to a Lisp string. */
8104
8105 void
8106 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8107 {
8108 if (!NILP (Vmemory_full))
8109 return;
8110
8111 if (!NILP (Vmessage_log_max))
8112 {
8113 struct buffer *oldbuf;
8114 Lisp_Object oldpoint, oldbegv, oldzv;
8115 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8116 EMACS_INT point_at_end = 0;
8117 EMACS_INT zv_at_end = 0;
8118 Lisp_Object old_deactivate_mark, tem;
8119 struct gcpro gcpro1;
8120
8121 old_deactivate_mark = Vdeactivate_mark;
8122 oldbuf = current_buffer;
8123 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8124 current_buffer->undo_list = Qt;
8125
8126 oldpoint = message_dolog_marker1;
8127 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8128 oldbegv = message_dolog_marker2;
8129 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8130 oldzv = message_dolog_marker3;
8131 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8132 GCPRO1 (old_deactivate_mark);
8133
8134 if (PT == Z)
8135 point_at_end = 1;
8136 if (ZV == Z)
8137 zv_at_end = 1;
8138
8139 BEGV = BEG;
8140 BEGV_BYTE = BEG_BYTE;
8141 ZV = Z;
8142 ZV_BYTE = Z_BYTE;
8143 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8144
8145 /* Insert the string--maybe converting multibyte to single byte
8146 or vice versa, so that all the text fits the buffer. */
8147 if (multibyte
8148 && NILP (current_buffer->enable_multibyte_characters))
8149 {
8150 EMACS_INT i;
8151 int c, char_bytes;
8152 unsigned char work[1];
8153
8154 /* Convert a multibyte string to single-byte
8155 for the *Message* buffer. */
8156 for (i = 0; i < nbytes; i += char_bytes)
8157 {
8158 c = string_char_and_length (m + i, &char_bytes);
8159 work[0] = (ASCII_CHAR_P (c)
8160 ? c
8161 : multibyte_char_to_unibyte (c, Qnil));
8162 insert_1_both (work, 1, 1, 1, 0, 0);
8163 }
8164 }
8165 else if (! multibyte
8166 && ! NILP (current_buffer->enable_multibyte_characters))
8167 {
8168 EMACS_INT i;
8169 int c, char_bytes;
8170 unsigned char *msg = (unsigned char *) m;
8171 unsigned char str[MAX_MULTIBYTE_LENGTH];
8172 /* Convert a single-byte string to multibyte
8173 for the *Message* buffer. */
8174 for (i = 0; i < nbytes; i++)
8175 {
8176 c = msg[i];
8177 MAKE_CHAR_MULTIBYTE (c);
8178 char_bytes = CHAR_STRING (c, str);
8179 insert_1_both (str, 1, char_bytes, 1, 0, 0);
8180 }
8181 }
8182 else if (nbytes)
8183 insert_1 (m, nbytes, 1, 0, 0);
8184
8185 if (nlflag)
8186 {
8187 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8188 int dup;
8189 insert_1 ("\n", 1, 1, 0, 0);
8190
8191 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8192 this_bol = PT;
8193 this_bol_byte = PT_BYTE;
8194
8195 /* See if this line duplicates the previous one.
8196 If so, combine duplicates. */
8197 if (this_bol > BEG)
8198 {
8199 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8200 prev_bol = PT;
8201 prev_bol_byte = PT_BYTE;
8202
8203 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8204 this_bol, this_bol_byte);
8205 if (dup)
8206 {
8207 del_range_both (prev_bol, prev_bol_byte,
8208 this_bol, this_bol_byte, 0);
8209 if (dup > 1)
8210 {
8211 char dupstr[40];
8212 int duplen;
8213
8214 /* If you change this format, don't forget to also
8215 change message_log_check_duplicate. */
8216 sprintf (dupstr, " [%d times]", dup);
8217 duplen = strlen (dupstr);
8218 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8219 insert_1 (dupstr, duplen, 1, 0, 1);
8220 }
8221 }
8222 }
8223
8224 /* If we have more than the desired maximum number of lines
8225 in the *Messages* buffer now, delete the oldest ones.
8226 This is safe because we don't have undo in this buffer. */
8227
8228 if (NATNUMP (Vmessage_log_max))
8229 {
8230 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8231 -XFASTINT (Vmessage_log_max) - 1, 0);
8232 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8233 }
8234 }
8235 BEGV = XMARKER (oldbegv)->charpos;
8236 BEGV_BYTE = marker_byte_position (oldbegv);
8237
8238 if (zv_at_end)
8239 {
8240 ZV = Z;
8241 ZV_BYTE = Z_BYTE;
8242 }
8243 else
8244 {
8245 ZV = XMARKER (oldzv)->charpos;
8246 ZV_BYTE = marker_byte_position (oldzv);
8247 }
8248
8249 if (point_at_end)
8250 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8251 else
8252 /* We can't do Fgoto_char (oldpoint) because it will run some
8253 Lisp code. */
8254 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8255 XMARKER (oldpoint)->bytepos);
8256
8257 UNGCPRO;
8258 unchain_marker (XMARKER (oldpoint));
8259 unchain_marker (XMARKER (oldbegv));
8260 unchain_marker (XMARKER (oldzv));
8261
8262 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8263 set_buffer_internal (oldbuf);
8264 if (NILP (tem))
8265 windows_or_buffers_changed = old_windows_or_buffers_changed;
8266 message_log_need_newline = !nlflag;
8267 Vdeactivate_mark = old_deactivate_mark;
8268 }
8269 }
8270
8271
8272 /* We are at the end of the buffer after just having inserted a newline.
8273 (Note: We depend on the fact we won't be crossing the gap.)
8274 Check to see if the most recent message looks a lot like the previous one.
8275 Return 0 if different, 1 if the new one should just replace it, or a
8276 value N > 1 if we should also append " [N times]". */
8277
8278 static int
8279 message_log_check_duplicate (EMACS_INT prev_bol, EMACS_INT prev_bol_byte,
8280 EMACS_INT this_bol, EMACS_INT this_bol_byte)
8281 {
8282 EMACS_INT i;
8283 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8284 int seen_dots = 0;
8285 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8286 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8287
8288 for (i = 0; i < len; i++)
8289 {
8290 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8291 seen_dots = 1;
8292 if (p1[i] != p2[i])
8293 return seen_dots;
8294 }
8295 p1 += len;
8296 if (*p1 == '\n')
8297 return 2;
8298 if (*p1++ == ' ' && *p1++ == '[')
8299 {
8300 int n = 0;
8301 while (*p1 >= '0' && *p1 <= '9')
8302 n = n * 10 + *p1++ - '0';
8303 if (strncmp (p1, " times]\n", 8) == 0)
8304 return n+1;
8305 }
8306 return 0;
8307 }
8308 \f
8309
8310 /* Display an echo area message M with a specified length of NBYTES
8311 bytes. The string may include null characters. If M is 0, clear
8312 out any existing message, and let the mini-buffer text show
8313 through.
8314
8315 This may GC, so the buffer M must NOT point to a Lisp string. */
8316
8317 void
8318 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8319 {
8320 /* First flush out any partial line written with print. */
8321 message_log_maybe_newline ();
8322 if (m)
8323 message_dolog (m, nbytes, 1, multibyte);
8324 message2_nolog (m, nbytes, multibyte);
8325 }
8326
8327
8328 /* The non-logging counterpart of message2. */
8329
8330 void
8331 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8332 {
8333 struct frame *sf = SELECTED_FRAME ();
8334 message_enable_multibyte = multibyte;
8335
8336 if (FRAME_INITIAL_P (sf))
8337 {
8338 if (noninteractive_need_newline)
8339 putc ('\n', stderr);
8340 noninteractive_need_newline = 0;
8341 if (m)
8342 fwrite (m, nbytes, 1, stderr);
8343 if (cursor_in_echo_area == 0)
8344 fprintf (stderr, "\n");
8345 fflush (stderr);
8346 }
8347 /* A null message buffer means that the frame hasn't really been
8348 initialized yet. Error messages get reported properly by
8349 cmd_error, so this must be just an informative message; toss it. */
8350 else if (INTERACTIVE
8351 && sf->glyphs_initialized_p
8352 && FRAME_MESSAGE_BUF (sf))
8353 {
8354 Lisp_Object mini_window;
8355 struct frame *f;
8356
8357 /* Get the frame containing the mini-buffer
8358 that the selected frame is using. */
8359 mini_window = FRAME_MINIBUF_WINDOW (sf);
8360 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8361
8362 FRAME_SAMPLE_VISIBILITY (f);
8363 if (FRAME_VISIBLE_P (sf)
8364 && ! FRAME_VISIBLE_P (f))
8365 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8366
8367 if (m)
8368 {
8369 set_message (m, Qnil, nbytes, multibyte);
8370 if (minibuffer_auto_raise)
8371 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8372 }
8373 else
8374 clear_message (1, 1);
8375
8376 do_pending_window_change (0);
8377 echo_area_display (1);
8378 do_pending_window_change (0);
8379 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8380 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8381 }
8382 }
8383
8384
8385 /* Display an echo area message M with a specified length of NBYTES
8386 bytes. The string may include null characters. If M is not a
8387 string, clear out any existing message, and let the mini-buffer
8388 text show through.
8389
8390 This function cancels echoing. */
8391
8392 void
8393 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8394 {
8395 struct gcpro gcpro1;
8396
8397 GCPRO1 (m);
8398 clear_message (1,1);
8399 cancel_echoing ();
8400
8401 /* First flush out any partial line written with print. */
8402 message_log_maybe_newline ();
8403 if (STRINGP (m))
8404 {
8405 char *buffer;
8406 USE_SAFE_ALLOCA;
8407
8408 SAFE_ALLOCA (buffer, char *, nbytes);
8409 memcpy (buffer, SDATA (m), nbytes);
8410 message_dolog (buffer, nbytes, 1, multibyte);
8411 SAFE_FREE ();
8412 }
8413 message3_nolog (m, nbytes, multibyte);
8414
8415 UNGCPRO;
8416 }
8417
8418
8419 /* The non-logging version of message3.
8420 This does not cancel echoing, because it is used for echoing.
8421 Perhaps we need to make a separate function for echoing
8422 and make this cancel echoing. */
8423
8424 void
8425 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8426 {
8427 struct frame *sf = SELECTED_FRAME ();
8428 message_enable_multibyte = multibyte;
8429
8430 if (FRAME_INITIAL_P (sf))
8431 {
8432 if (noninteractive_need_newline)
8433 putc ('\n', stderr);
8434 noninteractive_need_newline = 0;
8435 if (STRINGP (m))
8436 fwrite (SDATA (m), nbytes, 1, stderr);
8437 if (cursor_in_echo_area == 0)
8438 fprintf (stderr, "\n");
8439 fflush (stderr);
8440 }
8441 /* A null message buffer means that the frame hasn't really been
8442 initialized yet. Error messages get reported properly by
8443 cmd_error, so this must be just an informative message; toss it. */
8444 else if (INTERACTIVE
8445 && sf->glyphs_initialized_p
8446 && FRAME_MESSAGE_BUF (sf))
8447 {
8448 Lisp_Object mini_window;
8449 Lisp_Object frame;
8450 struct frame *f;
8451
8452 /* Get the frame containing the mini-buffer
8453 that the selected frame is using. */
8454 mini_window = FRAME_MINIBUF_WINDOW (sf);
8455 frame = XWINDOW (mini_window)->frame;
8456 f = XFRAME (frame);
8457
8458 FRAME_SAMPLE_VISIBILITY (f);
8459 if (FRAME_VISIBLE_P (sf)
8460 && !FRAME_VISIBLE_P (f))
8461 Fmake_frame_visible (frame);
8462
8463 if (STRINGP (m) && SCHARS (m) > 0)
8464 {
8465 set_message (NULL, m, nbytes, multibyte);
8466 if (minibuffer_auto_raise)
8467 Fraise_frame (frame);
8468 /* Assume we are not echoing.
8469 (If we are, echo_now will override this.) */
8470 echo_message_buffer = Qnil;
8471 }
8472 else
8473 clear_message (1, 1);
8474
8475 do_pending_window_change (0);
8476 echo_area_display (1);
8477 do_pending_window_change (0);
8478 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8479 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8480 }
8481 }
8482
8483
8484 /* Display a null-terminated echo area message M. If M is 0, clear
8485 out any existing message, and let the mini-buffer text show through.
8486
8487 The buffer M must continue to exist until after the echo area gets
8488 cleared or some other message gets displayed there. Do not pass
8489 text that is stored in a Lisp string. Do not pass text in a buffer
8490 that was alloca'd. */
8491
8492 void
8493 message1 (const char *m)
8494 {
8495 message2 (m, (m ? strlen (m) : 0), 0);
8496 }
8497
8498
8499 /* The non-logging counterpart of message1. */
8500
8501 void
8502 message1_nolog (const char *m)
8503 {
8504 message2_nolog (m, (m ? strlen (m) : 0), 0);
8505 }
8506
8507 /* Display a message M which contains a single %s
8508 which gets replaced with STRING. */
8509
8510 void
8511 message_with_string (const char *m, Lisp_Object string, int log)
8512 {
8513 CHECK_STRING (string);
8514
8515 if (noninteractive)
8516 {
8517 if (m)
8518 {
8519 if (noninteractive_need_newline)
8520 putc ('\n', stderr);
8521 noninteractive_need_newline = 0;
8522 fprintf (stderr, m, SDATA (string));
8523 if (!cursor_in_echo_area)
8524 fprintf (stderr, "\n");
8525 fflush (stderr);
8526 }
8527 }
8528 else if (INTERACTIVE)
8529 {
8530 /* The frame whose minibuffer we're going to display the message on.
8531 It may be larger than the selected frame, so we need
8532 to use its buffer, not the selected frame's buffer. */
8533 Lisp_Object mini_window;
8534 struct frame *f, *sf = SELECTED_FRAME ();
8535
8536 /* Get the frame containing the minibuffer
8537 that the selected frame is using. */
8538 mini_window = FRAME_MINIBUF_WINDOW (sf);
8539 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8540
8541 /* A null message buffer means that the frame hasn't really been
8542 initialized yet. Error messages get reported properly by
8543 cmd_error, so this must be just an informative message; toss it. */
8544 if (FRAME_MESSAGE_BUF (f))
8545 {
8546 Lisp_Object args[2], message;
8547 struct gcpro gcpro1, gcpro2;
8548
8549 args[0] = build_string (m);
8550 args[1] = message = string;
8551 GCPRO2 (args[0], message);
8552 gcpro1.nvars = 2;
8553
8554 message = Fformat (2, args);
8555
8556 if (log)
8557 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8558 else
8559 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8560
8561 UNGCPRO;
8562
8563 /* Print should start at the beginning of the message
8564 buffer next time. */
8565 message_buf_print = 0;
8566 }
8567 }
8568 }
8569
8570
8571 /* Dump an informative message to the minibuf. If M is 0, clear out
8572 any existing message, and let the mini-buffer text show through. */
8573
8574 static void
8575 vmessage (const char *m, va_list ap)
8576 {
8577 if (noninteractive)
8578 {
8579 if (m)
8580 {
8581 if (noninteractive_need_newline)
8582 putc ('\n', stderr);
8583 noninteractive_need_newline = 0;
8584 vfprintf (stderr, m, ap);
8585 if (cursor_in_echo_area == 0)
8586 fprintf (stderr, "\n");
8587 fflush (stderr);
8588 }
8589 }
8590 else if (INTERACTIVE)
8591 {
8592 /* The frame whose mini-buffer we're going to display the message
8593 on. It may be larger than the selected frame, so we need to
8594 use its buffer, not the selected frame's buffer. */
8595 Lisp_Object mini_window;
8596 struct frame *f, *sf = SELECTED_FRAME ();
8597
8598 /* Get the frame containing the mini-buffer
8599 that the selected frame is using. */
8600 mini_window = FRAME_MINIBUF_WINDOW (sf);
8601 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8602
8603 /* A null message buffer means that the frame hasn't really been
8604 initialized yet. Error messages get reported properly by
8605 cmd_error, so this must be just an informative message; toss
8606 it. */
8607 if (FRAME_MESSAGE_BUF (f))
8608 {
8609 if (m)
8610 {
8611 EMACS_INT len;
8612
8613 len = doprnt (FRAME_MESSAGE_BUF (f),
8614 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8615
8616 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8617 }
8618 else
8619 message1 (0);
8620
8621 /* Print should start at the beginning of the message
8622 buffer next time. */
8623 message_buf_print = 0;
8624 }
8625 }
8626 }
8627
8628 void
8629 message (const char *m, ...)
8630 {
8631 va_list ap;
8632 va_start (ap, m);
8633 vmessage (m, ap);
8634 va_end (ap);
8635 }
8636
8637
8638 /* The non-logging version of message. */
8639
8640 void
8641 message_nolog (const char *m, ...)
8642 {
8643 Lisp_Object old_log_max;
8644 va_list ap;
8645 va_start (ap, m);
8646 old_log_max = Vmessage_log_max;
8647 Vmessage_log_max = Qnil;
8648 vmessage (m, ap);
8649 Vmessage_log_max = old_log_max;
8650 va_end (ap);
8651 }
8652
8653
8654 /* Display the current message in the current mini-buffer. This is
8655 only called from error handlers in process.c, and is not time
8656 critical. */
8657
8658 void
8659 update_echo_area (void)
8660 {
8661 if (!NILP (echo_area_buffer[0]))
8662 {
8663 Lisp_Object string;
8664 string = Fcurrent_message ();
8665 message3 (string, SBYTES (string),
8666 !NILP (current_buffer->enable_multibyte_characters));
8667 }
8668 }
8669
8670
8671 /* Make sure echo area buffers in `echo_buffers' are live.
8672 If they aren't, make new ones. */
8673
8674 static void
8675 ensure_echo_area_buffers (void)
8676 {
8677 int i;
8678
8679 for (i = 0; i < 2; ++i)
8680 if (!BUFFERP (echo_buffer[i])
8681 || NILP (XBUFFER (echo_buffer[i])->name))
8682 {
8683 char name[30];
8684 Lisp_Object old_buffer;
8685 int j;
8686
8687 old_buffer = echo_buffer[i];
8688 sprintf (name, " *Echo Area %d*", i);
8689 echo_buffer[i] = Fget_buffer_create (build_string (name));
8690 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8691 /* to force word wrap in echo area -
8692 it was decided to postpone this*/
8693 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8694
8695 for (j = 0; j < 2; ++j)
8696 if (EQ (old_buffer, echo_area_buffer[j]))
8697 echo_area_buffer[j] = echo_buffer[i];
8698 }
8699 }
8700
8701
8702 /* Call FN with args A1..A4 with either the current or last displayed
8703 echo_area_buffer as current buffer.
8704
8705 WHICH zero means use the current message buffer
8706 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8707 from echo_buffer[] and clear it.
8708
8709 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8710 suitable buffer from echo_buffer[] and clear it.
8711
8712 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8713 that the current message becomes the last displayed one, make
8714 choose a suitable buffer for echo_area_buffer[0], and clear it.
8715
8716 Value is what FN returns. */
8717
8718 static int
8719 with_echo_area_buffer (struct window *w, int which,
8720 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
8721 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8722 {
8723 Lisp_Object buffer;
8724 int this_one, the_other, clear_buffer_p, rc;
8725 int count = SPECPDL_INDEX ();
8726
8727 /* If buffers aren't live, make new ones. */
8728 ensure_echo_area_buffers ();
8729
8730 clear_buffer_p = 0;
8731
8732 if (which == 0)
8733 this_one = 0, the_other = 1;
8734 else if (which > 0)
8735 this_one = 1, the_other = 0;
8736 else
8737 {
8738 this_one = 0, the_other = 1;
8739 clear_buffer_p = 1;
8740
8741 /* We need a fresh one in case the current echo buffer equals
8742 the one containing the last displayed echo area message. */
8743 if (!NILP (echo_area_buffer[this_one])
8744 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8745 echo_area_buffer[this_one] = Qnil;
8746 }
8747
8748 /* Choose a suitable buffer from echo_buffer[] is we don't
8749 have one. */
8750 if (NILP (echo_area_buffer[this_one]))
8751 {
8752 echo_area_buffer[this_one]
8753 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8754 ? echo_buffer[the_other]
8755 : echo_buffer[this_one]);
8756 clear_buffer_p = 1;
8757 }
8758
8759 buffer = echo_area_buffer[this_one];
8760
8761 /* Don't get confused by reusing the buffer used for echoing
8762 for a different purpose. */
8763 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8764 cancel_echoing ();
8765
8766 record_unwind_protect (unwind_with_echo_area_buffer,
8767 with_echo_area_buffer_unwind_data (w));
8768
8769 /* Make the echo area buffer current. Note that for display
8770 purposes, it is not necessary that the displayed window's buffer
8771 == current_buffer, except for text property lookup. So, let's
8772 only set that buffer temporarily here without doing a full
8773 Fset_window_buffer. We must also change w->pointm, though,
8774 because otherwise an assertions in unshow_buffer fails, and Emacs
8775 aborts. */
8776 set_buffer_internal_1 (XBUFFER (buffer));
8777 if (w)
8778 {
8779 w->buffer = buffer;
8780 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8781 }
8782
8783 current_buffer->undo_list = Qt;
8784 current_buffer->read_only = Qnil;
8785 specbind (Qinhibit_read_only, Qt);
8786 specbind (Qinhibit_modification_hooks, Qt);
8787
8788 if (clear_buffer_p && Z > BEG)
8789 del_range (BEG, Z);
8790
8791 xassert (BEGV >= BEG);
8792 xassert (ZV <= Z && ZV >= BEGV);
8793
8794 rc = fn (a1, a2, a3, a4);
8795
8796 xassert (BEGV >= BEG);
8797 xassert (ZV <= Z && ZV >= BEGV);
8798
8799 unbind_to (count, Qnil);
8800 return rc;
8801 }
8802
8803
8804 /* Save state that should be preserved around the call to the function
8805 FN called in with_echo_area_buffer. */
8806
8807 static Lisp_Object
8808 with_echo_area_buffer_unwind_data (struct window *w)
8809 {
8810 int i = 0;
8811 Lisp_Object vector, tmp;
8812
8813 /* Reduce consing by keeping one vector in
8814 Vwith_echo_area_save_vector. */
8815 vector = Vwith_echo_area_save_vector;
8816 Vwith_echo_area_save_vector = Qnil;
8817
8818 if (NILP (vector))
8819 vector = Fmake_vector (make_number (7), Qnil);
8820
8821 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8822 ASET (vector, i, Vdeactivate_mark); ++i;
8823 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8824
8825 if (w)
8826 {
8827 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8828 ASET (vector, i, w->buffer); ++i;
8829 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8830 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8831 }
8832 else
8833 {
8834 int end = i + 4;
8835 for (; i < end; ++i)
8836 ASET (vector, i, Qnil);
8837 }
8838
8839 xassert (i == ASIZE (vector));
8840 return vector;
8841 }
8842
8843
8844 /* Restore global state from VECTOR which was created by
8845 with_echo_area_buffer_unwind_data. */
8846
8847 static Lisp_Object
8848 unwind_with_echo_area_buffer (Lisp_Object vector)
8849 {
8850 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8851 Vdeactivate_mark = AREF (vector, 1);
8852 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8853
8854 if (WINDOWP (AREF (vector, 3)))
8855 {
8856 struct window *w;
8857 Lisp_Object buffer, charpos, bytepos;
8858
8859 w = XWINDOW (AREF (vector, 3));
8860 buffer = AREF (vector, 4);
8861 charpos = AREF (vector, 5);
8862 bytepos = AREF (vector, 6);
8863
8864 w->buffer = buffer;
8865 set_marker_both (w->pointm, buffer,
8866 XFASTINT (charpos), XFASTINT (bytepos));
8867 }
8868
8869 Vwith_echo_area_save_vector = vector;
8870 return Qnil;
8871 }
8872
8873
8874 /* Set up the echo area for use by print functions. MULTIBYTE_P
8875 non-zero means we will print multibyte. */
8876
8877 void
8878 setup_echo_area_for_printing (int multibyte_p)
8879 {
8880 /* If we can't find an echo area any more, exit. */
8881 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8882 Fkill_emacs (Qnil);
8883
8884 ensure_echo_area_buffers ();
8885
8886 if (!message_buf_print)
8887 {
8888 /* A message has been output since the last time we printed.
8889 Choose a fresh echo area buffer. */
8890 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8891 echo_area_buffer[0] = echo_buffer[1];
8892 else
8893 echo_area_buffer[0] = echo_buffer[0];
8894
8895 /* Switch to that buffer and clear it. */
8896 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8897 current_buffer->truncate_lines = Qnil;
8898
8899 if (Z > BEG)
8900 {
8901 int count = SPECPDL_INDEX ();
8902 specbind (Qinhibit_read_only, Qt);
8903 /* Note that undo recording is always disabled. */
8904 del_range (BEG, Z);
8905 unbind_to (count, Qnil);
8906 }
8907 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8908
8909 /* Set up the buffer for the multibyteness we need. */
8910 if (multibyte_p
8911 != !NILP (current_buffer->enable_multibyte_characters))
8912 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8913
8914 /* Raise the frame containing the echo area. */
8915 if (minibuffer_auto_raise)
8916 {
8917 struct frame *sf = SELECTED_FRAME ();
8918 Lisp_Object mini_window;
8919 mini_window = FRAME_MINIBUF_WINDOW (sf);
8920 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8921 }
8922
8923 message_log_maybe_newline ();
8924 message_buf_print = 1;
8925 }
8926 else
8927 {
8928 if (NILP (echo_area_buffer[0]))
8929 {
8930 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8931 echo_area_buffer[0] = echo_buffer[1];
8932 else
8933 echo_area_buffer[0] = echo_buffer[0];
8934 }
8935
8936 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8937 {
8938 /* Someone switched buffers between print requests. */
8939 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8940 current_buffer->truncate_lines = Qnil;
8941 }
8942 }
8943 }
8944
8945
8946 /* Display an echo area message in window W. Value is non-zero if W's
8947 height is changed. If display_last_displayed_message_p is
8948 non-zero, display the message that was last displayed, otherwise
8949 display the current message. */
8950
8951 static int
8952 display_echo_area (struct window *w)
8953 {
8954 int i, no_message_p, window_height_changed_p, count;
8955
8956 /* Temporarily disable garbage collections while displaying the echo
8957 area. This is done because a GC can print a message itself.
8958 That message would modify the echo area buffer's contents while a
8959 redisplay of the buffer is going on, and seriously confuse
8960 redisplay. */
8961 count = inhibit_garbage_collection ();
8962
8963 /* If there is no message, we must call display_echo_area_1
8964 nevertheless because it resizes the window. But we will have to
8965 reset the echo_area_buffer in question to nil at the end because
8966 with_echo_area_buffer will sets it to an empty buffer. */
8967 i = display_last_displayed_message_p ? 1 : 0;
8968 no_message_p = NILP (echo_area_buffer[i]);
8969
8970 window_height_changed_p
8971 = with_echo_area_buffer (w, display_last_displayed_message_p,
8972 display_echo_area_1,
8973 (EMACS_INT) w, Qnil, 0, 0);
8974
8975 if (no_message_p)
8976 echo_area_buffer[i] = Qnil;
8977
8978 unbind_to (count, Qnil);
8979 return window_height_changed_p;
8980 }
8981
8982
8983 /* Helper for display_echo_area. Display the current buffer which
8984 contains the current echo area message in window W, a mini-window,
8985 a pointer to which is passed in A1. A2..A4 are currently not used.
8986 Change the height of W so that all of the message is displayed.
8987 Value is non-zero if height of W was changed. */
8988
8989 static int
8990 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8991 {
8992 struct window *w = (struct window *) a1;
8993 Lisp_Object window;
8994 struct text_pos start;
8995 int window_height_changed_p = 0;
8996
8997 /* Do this before displaying, so that we have a large enough glyph
8998 matrix for the display. If we can't get enough space for the
8999 whole text, display the last N lines. That works by setting w->start. */
9000 window_height_changed_p = resize_mini_window (w, 0);
9001
9002 /* Use the starting position chosen by resize_mini_window. */
9003 SET_TEXT_POS_FROM_MARKER (start, w->start);
9004
9005 /* Display. */
9006 clear_glyph_matrix (w->desired_matrix);
9007 XSETWINDOW (window, w);
9008 try_window (window, start, 0);
9009
9010 return window_height_changed_p;
9011 }
9012
9013
9014 /* Resize the echo area window to exactly the size needed for the
9015 currently displayed message, if there is one. If a mini-buffer
9016 is active, don't shrink it. */
9017
9018 void
9019 resize_echo_area_exactly (void)
9020 {
9021 if (BUFFERP (echo_area_buffer[0])
9022 && WINDOWP (echo_area_window))
9023 {
9024 struct window *w = XWINDOW (echo_area_window);
9025 int resized_p;
9026 Lisp_Object resize_exactly;
9027
9028 if (minibuf_level == 0)
9029 resize_exactly = Qt;
9030 else
9031 resize_exactly = Qnil;
9032
9033 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9034 (EMACS_INT) w, resize_exactly, 0, 0);
9035 if (resized_p)
9036 {
9037 ++windows_or_buffers_changed;
9038 ++update_mode_lines;
9039 redisplay_internal (0);
9040 }
9041 }
9042 }
9043
9044
9045 /* Callback function for with_echo_area_buffer, when used from
9046 resize_echo_area_exactly. A1 contains a pointer to the window to
9047 resize, EXACTLY non-nil means resize the mini-window exactly to the
9048 size of the text displayed. A3 and A4 are not used. Value is what
9049 resize_mini_window returns. */
9050
9051 static int
9052 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9053 {
9054 return resize_mini_window ((struct window *) a1, !NILP (exactly));
9055 }
9056
9057
9058 /* Resize mini-window W to fit the size of its contents. EXACT_P
9059 means size the window exactly to the size needed. Otherwise, it's
9060 only enlarged until W's buffer is empty.
9061
9062 Set W->start to the right place to begin display. If the whole
9063 contents fit, start at the beginning. Otherwise, start so as
9064 to make the end of the contents appear. This is particularly
9065 important for y-or-n-p, but seems desirable generally.
9066
9067 Value is non-zero if the window height has been changed. */
9068
9069 int
9070 resize_mini_window (struct window *w, int exact_p)
9071 {
9072 struct frame *f = XFRAME (w->frame);
9073 int window_height_changed_p = 0;
9074
9075 xassert (MINI_WINDOW_P (w));
9076
9077 /* By default, start display at the beginning. */
9078 set_marker_both (w->start, w->buffer,
9079 BUF_BEGV (XBUFFER (w->buffer)),
9080 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9081
9082 /* Don't resize windows while redisplaying a window; it would
9083 confuse redisplay functions when the size of the window they are
9084 displaying changes from under them. Such a resizing can happen,
9085 for instance, when which-func prints a long message while
9086 we are running fontification-functions. We're running these
9087 functions with safe_call which binds inhibit-redisplay to t. */
9088 if (!NILP (Vinhibit_redisplay))
9089 return 0;
9090
9091 /* Nil means don't try to resize. */
9092 if (NILP (Vresize_mini_windows)
9093 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9094 return 0;
9095
9096 if (!FRAME_MINIBUF_ONLY_P (f))
9097 {
9098 struct it it;
9099 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9100 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9101 int height, max_height;
9102 int unit = FRAME_LINE_HEIGHT (f);
9103 struct text_pos start;
9104 struct buffer *old_current_buffer = NULL;
9105
9106 if (current_buffer != XBUFFER (w->buffer))
9107 {
9108 old_current_buffer = current_buffer;
9109 set_buffer_internal (XBUFFER (w->buffer));
9110 }
9111
9112 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9113
9114 /* Compute the max. number of lines specified by the user. */
9115 if (FLOATP (Vmax_mini_window_height))
9116 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9117 else if (INTEGERP (Vmax_mini_window_height))
9118 max_height = XINT (Vmax_mini_window_height);
9119 else
9120 max_height = total_height / 4;
9121
9122 /* Correct that max. height if it's bogus. */
9123 max_height = max (1, max_height);
9124 max_height = min (total_height, max_height);
9125
9126 /* Find out the height of the text in the window. */
9127 if (it.line_wrap == TRUNCATE)
9128 height = 1;
9129 else
9130 {
9131 last_height = 0;
9132 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9133 if (it.max_ascent == 0 && it.max_descent == 0)
9134 height = it.current_y + last_height;
9135 else
9136 height = it.current_y + it.max_ascent + it.max_descent;
9137 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9138 height = (height + unit - 1) / unit;
9139 }
9140
9141 /* Compute a suitable window start. */
9142 if (height > max_height)
9143 {
9144 height = max_height;
9145 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9146 move_it_vertically_backward (&it, (height - 1) * unit);
9147 start = it.current.pos;
9148 }
9149 else
9150 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9151 SET_MARKER_FROM_TEXT_POS (w->start, start);
9152
9153 if (EQ (Vresize_mini_windows, Qgrow_only))
9154 {
9155 /* Let it grow only, until we display an empty message, in which
9156 case the window shrinks again. */
9157 if (height > WINDOW_TOTAL_LINES (w))
9158 {
9159 int old_height = WINDOW_TOTAL_LINES (w);
9160 freeze_window_starts (f, 1);
9161 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9162 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9163 }
9164 else if (height < WINDOW_TOTAL_LINES (w)
9165 && (exact_p || BEGV == ZV))
9166 {
9167 int old_height = WINDOW_TOTAL_LINES (w);
9168 freeze_window_starts (f, 0);
9169 shrink_mini_window (w);
9170 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9171 }
9172 }
9173 else
9174 {
9175 /* Always resize to exact size needed. */
9176 if (height > WINDOW_TOTAL_LINES (w))
9177 {
9178 int old_height = WINDOW_TOTAL_LINES (w);
9179 freeze_window_starts (f, 1);
9180 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9181 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9182 }
9183 else if (height < WINDOW_TOTAL_LINES (w))
9184 {
9185 int old_height = WINDOW_TOTAL_LINES (w);
9186 freeze_window_starts (f, 0);
9187 shrink_mini_window (w);
9188
9189 if (height)
9190 {
9191 freeze_window_starts (f, 1);
9192 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9193 }
9194
9195 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9196 }
9197 }
9198
9199 if (old_current_buffer)
9200 set_buffer_internal (old_current_buffer);
9201 }
9202
9203 return window_height_changed_p;
9204 }
9205
9206
9207 /* Value is the current message, a string, or nil if there is no
9208 current message. */
9209
9210 Lisp_Object
9211 current_message (void)
9212 {
9213 Lisp_Object msg;
9214
9215 if (!BUFFERP (echo_area_buffer[0]))
9216 msg = Qnil;
9217 else
9218 {
9219 with_echo_area_buffer (0, 0, current_message_1,
9220 (EMACS_INT) &msg, Qnil, 0, 0);
9221 if (NILP (msg))
9222 echo_area_buffer[0] = Qnil;
9223 }
9224
9225 return msg;
9226 }
9227
9228
9229 static int
9230 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9231 {
9232 Lisp_Object *msg = (Lisp_Object *) a1;
9233
9234 if (Z > BEG)
9235 *msg = make_buffer_string (BEG, Z, 1);
9236 else
9237 *msg = Qnil;
9238 return 0;
9239 }
9240
9241
9242 /* Push the current message on Vmessage_stack for later restauration
9243 by restore_message. Value is non-zero if the current message isn't
9244 empty. This is a relatively infrequent operation, so it's not
9245 worth optimizing. */
9246
9247 int
9248 push_message (void)
9249 {
9250 Lisp_Object msg;
9251 msg = current_message ();
9252 Vmessage_stack = Fcons (msg, Vmessage_stack);
9253 return STRINGP (msg);
9254 }
9255
9256
9257 /* Restore message display from the top of Vmessage_stack. */
9258
9259 void
9260 restore_message (void)
9261 {
9262 Lisp_Object msg;
9263
9264 xassert (CONSP (Vmessage_stack));
9265 msg = XCAR (Vmessage_stack);
9266 if (STRINGP (msg))
9267 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9268 else
9269 message3_nolog (msg, 0, 0);
9270 }
9271
9272
9273 /* Handler for record_unwind_protect calling pop_message. */
9274
9275 Lisp_Object
9276 pop_message_unwind (Lisp_Object dummy)
9277 {
9278 pop_message ();
9279 return Qnil;
9280 }
9281
9282 /* Pop the top-most entry off Vmessage_stack. */
9283
9284 void
9285 pop_message (void)
9286 {
9287 xassert (CONSP (Vmessage_stack));
9288 Vmessage_stack = XCDR (Vmessage_stack);
9289 }
9290
9291
9292 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9293 exits. If the stack is not empty, we have a missing pop_message
9294 somewhere. */
9295
9296 void
9297 check_message_stack (void)
9298 {
9299 if (!NILP (Vmessage_stack))
9300 abort ();
9301 }
9302
9303
9304 /* Truncate to NCHARS what will be displayed in the echo area the next
9305 time we display it---but don't redisplay it now. */
9306
9307 void
9308 truncate_echo_area (EMACS_INT nchars)
9309 {
9310 if (nchars == 0)
9311 echo_area_buffer[0] = Qnil;
9312 /* A null message buffer means that the frame hasn't really been
9313 initialized yet. Error messages get reported properly by
9314 cmd_error, so this must be just an informative message; toss it. */
9315 else if (!noninteractive
9316 && INTERACTIVE
9317 && !NILP (echo_area_buffer[0]))
9318 {
9319 struct frame *sf = SELECTED_FRAME ();
9320 if (FRAME_MESSAGE_BUF (sf))
9321 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9322 }
9323 }
9324
9325
9326 /* Helper function for truncate_echo_area. Truncate the current
9327 message to at most NCHARS characters. */
9328
9329 static int
9330 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9331 {
9332 if (BEG + nchars < Z)
9333 del_range (BEG + nchars, Z);
9334 if (Z == BEG)
9335 echo_area_buffer[0] = Qnil;
9336 return 0;
9337 }
9338
9339
9340 /* Set the current message to a substring of S or STRING.
9341
9342 If STRING is a Lisp string, set the message to the first NBYTES
9343 bytes from STRING. NBYTES zero means use the whole string. If
9344 STRING is multibyte, the message will be displayed multibyte.
9345
9346 If S is not null, set the message to the first LEN bytes of S. LEN
9347 zero means use the whole string. MULTIBYTE_P non-zero means S is
9348 multibyte. Display the message multibyte in that case.
9349
9350 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9351 to t before calling set_message_1 (which calls insert).
9352 */
9353
9354 void
9355 set_message (const char *s, Lisp_Object string,
9356 EMACS_INT nbytes, int multibyte_p)
9357 {
9358 message_enable_multibyte
9359 = ((s && multibyte_p)
9360 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9361
9362 with_echo_area_buffer (0, -1, set_message_1,
9363 (EMACS_INT) s, string, nbytes, multibyte_p);
9364 message_buf_print = 0;
9365 help_echo_showing_p = 0;
9366 }
9367
9368
9369 /* Helper function for set_message. Arguments have the same meaning
9370 as there, with A1 corresponding to S and A2 corresponding to STRING
9371 This function is called with the echo area buffer being
9372 current. */
9373
9374 static int
9375 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9376 {
9377 const char *s = (const char *) a1;
9378 Lisp_Object string = a2;
9379
9380 /* Change multibyteness of the echo buffer appropriately. */
9381 if (message_enable_multibyte
9382 != !NILP (current_buffer->enable_multibyte_characters))
9383 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9384
9385 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9386 if (!NILP (current_buffer->bidi_display_reordering))
9387 current_buffer->bidi_paragraph_direction = Qleft_to_right;
9388
9389 /* Insert new message at BEG. */
9390 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9391
9392 if (STRINGP (string))
9393 {
9394 EMACS_INT nchars;
9395
9396 if (nbytes == 0)
9397 nbytes = SBYTES (string);
9398 nchars = string_byte_to_char (string, nbytes);
9399
9400 /* This function takes care of single/multibyte conversion. We
9401 just have to ensure that the echo area buffer has the right
9402 setting of enable_multibyte_characters. */
9403 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9404 }
9405 else if (s)
9406 {
9407 if (nbytes == 0)
9408 nbytes = strlen (s);
9409
9410 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9411 {
9412 /* Convert from multi-byte to single-byte. */
9413 EMACS_INT i;
9414 int c, n;
9415 unsigned char work[1];
9416
9417 /* Convert a multibyte string to single-byte. */
9418 for (i = 0; i < nbytes; i += n)
9419 {
9420 c = string_char_and_length (s + i, &n);
9421 work[0] = (ASCII_CHAR_P (c)
9422 ? c
9423 : multibyte_char_to_unibyte (c, Qnil));
9424 insert_1_both (work, 1, 1, 1, 0, 0);
9425 }
9426 }
9427 else if (!multibyte_p
9428 && !NILP (current_buffer->enable_multibyte_characters))
9429 {
9430 /* Convert from single-byte to multi-byte. */
9431 EMACS_INT i;
9432 int c, n;
9433 const unsigned char *msg = (const unsigned char *) s;
9434 unsigned char str[MAX_MULTIBYTE_LENGTH];
9435
9436 /* Convert a single-byte string to multibyte. */
9437 for (i = 0; i < nbytes; i++)
9438 {
9439 c = msg[i];
9440 MAKE_CHAR_MULTIBYTE (c);
9441 n = CHAR_STRING (c, str);
9442 insert_1_both (str, 1, n, 1, 0, 0);
9443 }
9444 }
9445 else
9446 insert_1 (s, nbytes, 1, 0, 0);
9447 }
9448
9449 return 0;
9450 }
9451
9452
9453 /* Clear messages. CURRENT_P non-zero means clear the current
9454 message. LAST_DISPLAYED_P non-zero means clear the message
9455 last displayed. */
9456
9457 void
9458 clear_message (int current_p, int last_displayed_p)
9459 {
9460 if (current_p)
9461 {
9462 echo_area_buffer[0] = Qnil;
9463 message_cleared_p = 1;
9464 }
9465
9466 if (last_displayed_p)
9467 echo_area_buffer[1] = Qnil;
9468
9469 message_buf_print = 0;
9470 }
9471
9472 /* Clear garbaged frames.
9473
9474 This function is used where the old redisplay called
9475 redraw_garbaged_frames which in turn called redraw_frame which in
9476 turn called clear_frame. The call to clear_frame was a source of
9477 flickering. I believe a clear_frame is not necessary. It should
9478 suffice in the new redisplay to invalidate all current matrices,
9479 and ensure a complete redisplay of all windows. */
9480
9481 static void
9482 clear_garbaged_frames (void)
9483 {
9484 if (frame_garbaged)
9485 {
9486 Lisp_Object tail, frame;
9487 int changed_count = 0;
9488
9489 FOR_EACH_FRAME (tail, frame)
9490 {
9491 struct frame *f = XFRAME (frame);
9492
9493 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9494 {
9495 if (f->resized_p)
9496 {
9497 Fredraw_frame (frame);
9498 f->force_flush_display_p = 1;
9499 }
9500 clear_current_matrices (f);
9501 changed_count++;
9502 f->garbaged = 0;
9503 f->resized_p = 0;
9504 }
9505 }
9506
9507 frame_garbaged = 0;
9508 if (changed_count)
9509 ++windows_or_buffers_changed;
9510 }
9511 }
9512
9513
9514 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9515 is non-zero update selected_frame. Value is non-zero if the
9516 mini-windows height has been changed. */
9517
9518 static int
9519 echo_area_display (int update_frame_p)
9520 {
9521 Lisp_Object mini_window;
9522 struct window *w;
9523 struct frame *f;
9524 int window_height_changed_p = 0;
9525 struct frame *sf = SELECTED_FRAME ();
9526
9527 mini_window = FRAME_MINIBUF_WINDOW (sf);
9528 w = XWINDOW (mini_window);
9529 f = XFRAME (WINDOW_FRAME (w));
9530
9531 /* Don't display if frame is invisible or not yet initialized. */
9532 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9533 return 0;
9534
9535 #ifdef HAVE_WINDOW_SYSTEM
9536 /* When Emacs starts, selected_frame may be the initial terminal
9537 frame. If we let this through, a message would be displayed on
9538 the terminal. */
9539 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9540 return 0;
9541 #endif /* HAVE_WINDOW_SYSTEM */
9542
9543 /* Redraw garbaged frames. */
9544 if (frame_garbaged)
9545 clear_garbaged_frames ();
9546
9547 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9548 {
9549 echo_area_window = mini_window;
9550 window_height_changed_p = display_echo_area (w);
9551 w->must_be_updated_p = 1;
9552
9553 /* Update the display, unless called from redisplay_internal.
9554 Also don't update the screen during redisplay itself. The
9555 update will happen at the end of redisplay, and an update
9556 here could cause confusion. */
9557 if (update_frame_p && !redisplaying_p)
9558 {
9559 int n = 0;
9560
9561 /* If the display update has been interrupted by pending
9562 input, update mode lines in the frame. Due to the
9563 pending input, it might have been that redisplay hasn't
9564 been called, so that mode lines above the echo area are
9565 garbaged. This looks odd, so we prevent it here. */
9566 if (!display_completed)
9567 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9568
9569 if (window_height_changed_p
9570 /* Don't do this if Emacs is shutting down. Redisplay
9571 needs to run hooks. */
9572 && !NILP (Vrun_hooks))
9573 {
9574 /* Must update other windows. Likewise as in other
9575 cases, don't let this update be interrupted by
9576 pending input. */
9577 int count = SPECPDL_INDEX ();
9578 specbind (Qredisplay_dont_pause, Qt);
9579 windows_or_buffers_changed = 1;
9580 redisplay_internal (0);
9581 unbind_to (count, Qnil);
9582 }
9583 else if (FRAME_WINDOW_P (f) && n == 0)
9584 {
9585 /* Window configuration is the same as before.
9586 Can do with a display update of the echo area,
9587 unless we displayed some mode lines. */
9588 update_single_window (w, 1);
9589 FRAME_RIF (f)->flush_display (f);
9590 }
9591 else
9592 update_frame (f, 1, 1);
9593
9594 /* If cursor is in the echo area, make sure that the next
9595 redisplay displays the minibuffer, so that the cursor will
9596 be replaced with what the minibuffer wants. */
9597 if (cursor_in_echo_area)
9598 ++windows_or_buffers_changed;
9599 }
9600 }
9601 else if (!EQ (mini_window, selected_window))
9602 windows_or_buffers_changed++;
9603
9604 /* Last displayed message is now the current message. */
9605 echo_area_buffer[1] = echo_area_buffer[0];
9606 /* Inform read_char that we're not echoing. */
9607 echo_message_buffer = Qnil;
9608
9609 /* Prevent redisplay optimization in redisplay_internal by resetting
9610 this_line_start_pos. This is done because the mini-buffer now
9611 displays the message instead of its buffer text. */
9612 if (EQ (mini_window, selected_window))
9613 CHARPOS (this_line_start_pos) = 0;
9614
9615 return window_height_changed_p;
9616 }
9617
9618
9619 \f
9620 /***********************************************************************
9621 Mode Lines and Frame Titles
9622 ***********************************************************************/
9623
9624 /* A buffer for constructing non-propertized mode-line strings and
9625 frame titles in it; allocated from the heap in init_xdisp and
9626 resized as needed in store_mode_line_noprop_char. */
9627
9628 static char *mode_line_noprop_buf;
9629
9630 /* The buffer's end, and a current output position in it. */
9631
9632 static char *mode_line_noprop_buf_end;
9633 static char *mode_line_noprop_ptr;
9634
9635 #define MODE_LINE_NOPROP_LEN(start) \
9636 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9637
9638 static enum {
9639 MODE_LINE_DISPLAY = 0,
9640 MODE_LINE_TITLE,
9641 MODE_LINE_NOPROP,
9642 MODE_LINE_STRING
9643 } mode_line_target;
9644
9645 /* Alist that caches the results of :propertize.
9646 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9647 static Lisp_Object mode_line_proptrans_alist;
9648
9649 /* List of strings making up the mode-line. */
9650 static Lisp_Object mode_line_string_list;
9651
9652 /* Base face property when building propertized mode line string. */
9653 static Lisp_Object mode_line_string_face;
9654 static Lisp_Object mode_line_string_face_prop;
9655
9656
9657 /* Unwind data for mode line strings */
9658
9659 static Lisp_Object Vmode_line_unwind_vector;
9660
9661 static Lisp_Object
9662 format_mode_line_unwind_data (struct buffer *obuf,
9663 Lisp_Object owin,
9664 int save_proptrans)
9665 {
9666 Lisp_Object vector, tmp;
9667
9668 /* Reduce consing by keeping one vector in
9669 Vwith_echo_area_save_vector. */
9670 vector = Vmode_line_unwind_vector;
9671 Vmode_line_unwind_vector = Qnil;
9672
9673 if (NILP (vector))
9674 vector = Fmake_vector (make_number (8), Qnil);
9675
9676 ASET (vector, 0, make_number (mode_line_target));
9677 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9678 ASET (vector, 2, mode_line_string_list);
9679 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9680 ASET (vector, 4, mode_line_string_face);
9681 ASET (vector, 5, mode_line_string_face_prop);
9682
9683 if (obuf)
9684 XSETBUFFER (tmp, obuf);
9685 else
9686 tmp = Qnil;
9687 ASET (vector, 6, tmp);
9688 ASET (vector, 7, owin);
9689
9690 return vector;
9691 }
9692
9693 static Lisp_Object
9694 unwind_format_mode_line (Lisp_Object vector)
9695 {
9696 mode_line_target = XINT (AREF (vector, 0));
9697 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9698 mode_line_string_list = AREF (vector, 2);
9699 if (! EQ (AREF (vector, 3), Qt))
9700 mode_line_proptrans_alist = AREF (vector, 3);
9701 mode_line_string_face = AREF (vector, 4);
9702 mode_line_string_face_prop = AREF (vector, 5);
9703
9704 if (!NILP (AREF (vector, 7)))
9705 /* Select window before buffer, since it may change the buffer. */
9706 Fselect_window (AREF (vector, 7), Qt);
9707
9708 if (!NILP (AREF (vector, 6)))
9709 {
9710 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9711 ASET (vector, 6, Qnil);
9712 }
9713
9714 Vmode_line_unwind_vector = vector;
9715 return Qnil;
9716 }
9717
9718
9719 /* Store a single character C for the frame title in mode_line_noprop_buf.
9720 Re-allocate mode_line_noprop_buf if necessary. */
9721
9722 static void
9723 store_mode_line_noprop_char (char c)
9724 {
9725 /* If output position has reached the end of the allocated buffer,
9726 double the buffer's size. */
9727 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9728 {
9729 int len = MODE_LINE_NOPROP_LEN (0);
9730 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9731 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9732 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9733 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9734 }
9735
9736 *mode_line_noprop_ptr++ = c;
9737 }
9738
9739
9740 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9741 mode_line_noprop_ptr. STR is the string to store. Do not copy
9742 characters that yield more columns than PRECISION; PRECISION <= 0
9743 means copy the whole string. Pad with spaces until FIELD_WIDTH
9744 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9745 pad. Called from display_mode_element when it is used to build a
9746 frame title. */
9747
9748 static int
9749 store_mode_line_noprop (const unsigned char *str, int field_width, int precision)
9750 {
9751 int n = 0;
9752 EMACS_INT dummy, nbytes;
9753
9754 /* Copy at most PRECISION chars from STR. */
9755 nbytes = strlen (str);
9756 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9757 while (nbytes--)
9758 store_mode_line_noprop_char (*str++);
9759
9760 /* Fill up with spaces until FIELD_WIDTH reached. */
9761 while (field_width > 0
9762 && n < field_width)
9763 {
9764 store_mode_line_noprop_char (' ');
9765 ++n;
9766 }
9767
9768 return n;
9769 }
9770
9771 /***********************************************************************
9772 Frame Titles
9773 ***********************************************************************/
9774
9775 #ifdef HAVE_WINDOW_SYSTEM
9776
9777 /* Set the title of FRAME, if it has changed. The title format is
9778 Vicon_title_format if FRAME is iconified, otherwise it is
9779 frame_title_format. */
9780
9781 static void
9782 x_consider_frame_title (Lisp_Object frame)
9783 {
9784 struct frame *f = XFRAME (frame);
9785
9786 if (FRAME_WINDOW_P (f)
9787 || FRAME_MINIBUF_ONLY_P (f)
9788 || f->explicit_name)
9789 {
9790 /* Do we have more than one visible frame on this X display? */
9791 Lisp_Object tail;
9792 Lisp_Object fmt;
9793 int title_start;
9794 char *title;
9795 int len;
9796 struct it it;
9797 int count = SPECPDL_INDEX ();
9798
9799 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9800 {
9801 Lisp_Object other_frame = XCAR (tail);
9802 struct frame *tf = XFRAME (other_frame);
9803
9804 if (tf != f
9805 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9806 && !FRAME_MINIBUF_ONLY_P (tf)
9807 && !EQ (other_frame, tip_frame)
9808 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9809 break;
9810 }
9811
9812 /* Set global variable indicating that multiple frames exist. */
9813 multiple_frames = CONSP (tail);
9814
9815 /* Switch to the buffer of selected window of the frame. Set up
9816 mode_line_target so that display_mode_element will output into
9817 mode_line_noprop_buf; then display the title. */
9818 record_unwind_protect (unwind_format_mode_line,
9819 format_mode_line_unwind_data
9820 (current_buffer, selected_window, 0));
9821
9822 Fselect_window (f->selected_window, Qt);
9823 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9824 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9825
9826 mode_line_target = MODE_LINE_TITLE;
9827 title_start = MODE_LINE_NOPROP_LEN (0);
9828 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9829 NULL, DEFAULT_FACE_ID);
9830 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9831 len = MODE_LINE_NOPROP_LEN (title_start);
9832 title = mode_line_noprop_buf + title_start;
9833 unbind_to (count, Qnil);
9834
9835 /* Set the title only if it's changed. This avoids consing in
9836 the common case where it hasn't. (If it turns out that we've
9837 already wasted too much time by walking through the list with
9838 display_mode_element, then we might need to optimize at a
9839 higher level than this.) */
9840 if (! STRINGP (f->name)
9841 || SBYTES (f->name) != len
9842 || memcmp (title, SDATA (f->name), len) != 0)
9843 x_implicitly_set_name (f, make_string (title, len), Qnil);
9844 }
9845 }
9846
9847 #endif /* not HAVE_WINDOW_SYSTEM */
9848
9849
9850
9851 \f
9852 /***********************************************************************
9853 Menu Bars
9854 ***********************************************************************/
9855
9856
9857 /* Prepare for redisplay by updating menu-bar item lists when
9858 appropriate. This can call eval. */
9859
9860 void
9861 prepare_menu_bars (void)
9862 {
9863 int all_windows;
9864 struct gcpro gcpro1, gcpro2;
9865 struct frame *f;
9866 Lisp_Object tooltip_frame;
9867
9868 #ifdef HAVE_WINDOW_SYSTEM
9869 tooltip_frame = tip_frame;
9870 #else
9871 tooltip_frame = Qnil;
9872 #endif
9873
9874 /* Update all frame titles based on their buffer names, etc. We do
9875 this before the menu bars so that the buffer-menu will show the
9876 up-to-date frame titles. */
9877 #ifdef HAVE_WINDOW_SYSTEM
9878 if (windows_or_buffers_changed || update_mode_lines)
9879 {
9880 Lisp_Object tail, frame;
9881
9882 FOR_EACH_FRAME (tail, frame)
9883 {
9884 f = XFRAME (frame);
9885 if (!EQ (frame, tooltip_frame)
9886 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9887 x_consider_frame_title (frame);
9888 }
9889 }
9890 #endif /* HAVE_WINDOW_SYSTEM */
9891
9892 /* Update the menu bar item lists, if appropriate. This has to be
9893 done before any actual redisplay or generation of display lines. */
9894 all_windows = (update_mode_lines
9895 || buffer_shared > 1
9896 || windows_or_buffers_changed);
9897 if (all_windows)
9898 {
9899 Lisp_Object tail, frame;
9900 int count = SPECPDL_INDEX ();
9901 /* 1 means that update_menu_bar has run its hooks
9902 so any further calls to update_menu_bar shouldn't do so again. */
9903 int menu_bar_hooks_run = 0;
9904
9905 record_unwind_save_match_data ();
9906
9907 FOR_EACH_FRAME (tail, frame)
9908 {
9909 f = XFRAME (frame);
9910
9911 /* Ignore tooltip frame. */
9912 if (EQ (frame, tooltip_frame))
9913 continue;
9914
9915 /* If a window on this frame changed size, report that to
9916 the user and clear the size-change flag. */
9917 if (FRAME_WINDOW_SIZES_CHANGED (f))
9918 {
9919 Lisp_Object functions;
9920
9921 /* Clear flag first in case we get an error below. */
9922 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9923 functions = Vwindow_size_change_functions;
9924 GCPRO2 (tail, functions);
9925
9926 while (CONSP (functions))
9927 {
9928 if (!EQ (XCAR (functions), Qt))
9929 call1 (XCAR (functions), frame);
9930 functions = XCDR (functions);
9931 }
9932 UNGCPRO;
9933 }
9934
9935 GCPRO1 (tail);
9936 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9937 #ifdef HAVE_WINDOW_SYSTEM
9938 update_tool_bar (f, 0);
9939 #endif
9940 #ifdef HAVE_NS
9941 if (windows_or_buffers_changed
9942 && FRAME_NS_P (f))
9943 ns_set_doc_edited (f, Fbuffer_modified_p
9944 (XWINDOW (f->selected_window)->buffer));
9945 #endif
9946 UNGCPRO;
9947 }
9948
9949 unbind_to (count, Qnil);
9950 }
9951 else
9952 {
9953 struct frame *sf = SELECTED_FRAME ();
9954 update_menu_bar (sf, 1, 0);
9955 #ifdef HAVE_WINDOW_SYSTEM
9956 update_tool_bar (sf, 1);
9957 #endif
9958 }
9959 }
9960
9961
9962 /* Update the menu bar item list for frame F. This has to be done
9963 before we start to fill in any display lines, because it can call
9964 eval.
9965
9966 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9967
9968 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9969 already ran the menu bar hooks for this redisplay, so there
9970 is no need to run them again. The return value is the
9971 updated value of this flag, to pass to the next call. */
9972
9973 static int
9974 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
9975 {
9976 Lisp_Object window;
9977 register struct window *w;
9978
9979 /* If called recursively during a menu update, do nothing. This can
9980 happen when, for instance, an activate-menubar-hook causes a
9981 redisplay. */
9982 if (inhibit_menubar_update)
9983 return hooks_run;
9984
9985 window = FRAME_SELECTED_WINDOW (f);
9986 w = XWINDOW (window);
9987
9988 if (FRAME_WINDOW_P (f)
9989 ?
9990 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9991 || defined (HAVE_NS) || defined (USE_GTK)
9992 FRAME_EXTERNAL_MENU_BAR (f)
9993 #else
9994 FRAME_MENU_BAR_LINES (f) > 0
9995 #endif
9996 : FRAME_MENU_BAR_LINES (f) > 0)
9997 {
9998 /* If the user has switched buffers or windows, we need to
9999 recompute to reflect the new bindings. But we'll
10000 recompute when update_mode_lines is set too; that means
10001 that people can use force-mode-line-update to request
10002 that the menu bar be recomputed. The adverse effect on
10003 the rest of the redisplay algorithm is about the same as
10004 windows_or_buffers_changed anyway. */
10005 if (windows_or_buffers_changed
10006 /* This used to test w->update_mode_line, but we believe
10007 there is no need to recompute the menu in that case. */
10008 || update_mode_lines
10009 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10010 < BUF_MODIFF (XBUFFER (w->buffer)))
10011 != !NILP (w->last_had_star))
10012 || ((!NILP (Vtransient_mark_mode)
10013 && !NILP (XBUFFER (w->buffer)->mark_active))
10014 != !NILP (w->region_showing)))
10015 {
10016 struct buffer *prev = current_buffer;
10017 int count = SPECPDL_INDEX ();
10018
10019 specbind (Qinhibit_menubar_update, Qt);
10020
10021 set_buffer_internal_1 (XBUFFER (w->buffer));
10022 if (save_match_data)
10023 record_unwind_save_match_data ();
10024 if (NILP (Voverriding_local_map_menu_flag))
10025 {
10026 specbind (Qoverriding_terminal_local_map, Qnil);
10027 specbind (Qoverriding_local_map, Qnil);
10028 }
10029
10030 if (!hooks_run)
10031 {
10032 /* Run the Lucid hook. */
10033 safe_run_hooks (Qactivate_menubar_hook);
10034
10035 /* If it has changed current-menubar from previous value,
10036 really recompute the menu-bar from the value. */
10037 if (! NILP (Vlucid_menu_bar_dirty_flag))
10038 call0 (Qrecompute_lucid_menubar);
10039
10040 safe_run_hooks (Qmenu_bar_update_hook);
10041
10042 hooks_run = 1;
10043 }
10044
10045 XSETFRAME (Vmenu_updating_frame, f);
10046 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10047
10048 /* Redisplay the menu bar in case we changed it. */
10049 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10050 || defined (HAVE_NS) || defined (USE_GTK)
10051 if (FRAME_WINDOW_P (f))
10052 {
10053 #if defined (HAVE_NS)
10054 /* All frames on Mac OS share the same menubar. So only
10055 the selected frame should be allowed to set it. */
10056 if (f == SELECTED_FRAME ())
10057 #endif
10058 set_frame_menubar (f, 0, 0);
10059 }
10060 else
10061 /* On a terminal screen, the menu bar is an ordinary screen
10062 line, and this makes it get updated. */
10063 w->update_mode_line = Qt;
10064 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10065 /* In the non-toolkit version, the menu bar is an ordinary screen
10066 line, and this makes it get updated. */
10067 w->update_mode_line = Qt;
10068 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10069
10070 unbind_to (count, Qnil);
10071 set_buffer_internal_1 (prev);
10072 }
10073 }
10074
10075 return hooks_run;
10076 }
10077
10078
10079 \f
10080 /***********************************************************************
10081 Output Cursor
10082 ***********************************************************************/
10083
10084 #ifdef HAVE_WINDOW_SYSTEM
10085
10086 /* EXPORT:
10087 Nominal cursor position -- where to draw output.
10088 HPOS and VPOS are window relative glyph matrix coordinates.
10089 X and Y are window relative pixel coordinates. */
10090
10091 struct cursor_pos output_cursor;
10092
10093
10094 /* EXPORT:
10095 Set the global variable output_cursor to CURSOR. All cursor
10096 positions are relative to updated_window. */
10097
10098 void
10099 set_output_cursor (struct cursor_pos *cursor)
10100 {
10101 output_cursor.hpos = cursor->hpos;
10102 output_cursor.vpos = cursor->vpos;
10103 output_cursor.x = cursor->x;
10104 output_cursor.y = cursor->y;
10105 }
10106
10107
10108 /* EXPORT for RIF:
10109 Set a nominal cursor position.
10110
10111 HPOS and VPOS are column/row positions in a window glyph matrix. X
10112 and Y are window text area relative pixel positions.
10113
10114 If this is done during an update, updated_window will contain the
10115 window that is being updated and the position is the future output
10116 cursor position for that window. If updated_window is null, use
10117 selected_window and display the cursor at the given position. */
10118
10119 void
10120 x_cursor_to (int vpos, int hpos, int y, int x)
10121 {
10122 struct window *w;
10123
10124 /* If updated_window is not set, work on selected_window. */
10125 if (updated_window)
10126 w = updated_window;
10127 else
10128 w = XWINDOW (selected_window);
10129
10130 /* Set the output cursor. */
10131 output_cursor.hpos = hpos;
10132 output_cursor.vpos = vpos;
10133 output_cursor.x = x;
10134 output_cursor.y = y;
10135
10136 /* If not called as part of an update, really display the cursor.
10137 This will also set the cursor position of W. */
10138 if (updated_window == NULL)
10139 {
10140 BLOCK_INPUT;
10141 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10142 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10143 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10144 UNBLOCK_INPUT;
10145 }
10146 }
10147
10148 #endif /* HAVE_WINDOW_SYSTEM */
10149
10150 \f
10151 /***********************************************************************
10152 Tool-bars
10153 ***********************************************************************/
10154
10155 #ifdef HAVE_WINDOW_SYSTEM
10156
10157 /* Where the mouse was last time we reported a mouse event. */
10158
10159 FRAME_PTR last_mouse_frame;
10160
10161 /* Tool-bar item index of the item on which a mouse button was pressed
10162 or -1. */
10163
10164 int last_tool_bar_item;
10165
10166
10167 static Lisp_Object
10168 update_tool_bar_unwind (Lisp_Object frame)
10169 {
10170 selected_frame = frame;
10171 return Qnil;
10172 }
10173
10174 /* Update the tool-bar item list for frame F. This has to be done
10175 before we start to fill in any display lines. Called from
10176 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10177 and restore it here. */
10178
10179 static void
10180 update_tool_bar (struct frame *f, int save_match_data)
10181 {
10182 #if defined (USE_GTK) || defined (HAVE_NS)
10183 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10184 #else
10185 int do_update = WINDOWP (f->tool_bar_window)
10186 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10187 #endif
10188
10189 if (do_update)
10190 {
10191 Lisp_Object window;
10192 struct window *w;
10193
10194 window = FRAME_SELECTED_WINDOW (f);
10195 w = XWINDOW (window);
10196
10197 /* If the user has switched buffers or windows, we need to
10198 recompute to reflect the new bindings. But we'll
10199 recompute when update_mode_lines is set too; that means
10200 that people can use force-mode-line-update to request
10201 that the menu bar be recomputed. The adverse effect on
10202 the rest of the redisplay algorithm is about the same as
10203 windows_or_buffers_changed anyway. */
10204 if (windows_or_buffers_changed
10205 || !NILP (w->update_mode_line)
10206 || update_mode_lines
10207 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10208 < BUF_MODIFF (XBUFFER (w->buffer)))
10209 != !NILP (w->last_had_star))
10210 || ((!NILP (Vtransient_mark_mode)
10211 && !NILP (XBUFFER (w->buffer)->mark_active))
10212 != !NILP (w->region_showing)))
10213 {
10214 struct buffer *prev = current_buffer;
10215 int count = SPECPDL_INDEX ();
10216 Lisp_Object frame, new_tool_bar;
10217 int new_n_tool_bar;
10218 struct gcpro gcpro1;
10219
10220 /* Set current_buffer to the buffer of the selected
10221 window of the frame, so that we get the right local
10222 keymaps. */
10223 set_buffer_internal_1 (XBUFFER (w->buffer));
10224
10225 /* Save match data, if we must. */
10226 if (save_match_data)
10227 record_unwind_save_match_data ();
10228
10229 /* Make sure that we don't accidentally use bogus keymaps. */
10230 if (NILP (Voverriding_local_map_menu_flag))
10231 {
10232 specbind (Qoverriding_terminal_local_map, Qnil);
10233 specbind (Qoverriding_local_map, Qnil);
10234 }
10235
10236 GCPRO1 (new_tool_bar);
10237
10238 /* We must temporarily set the selected frame to this frame
10239 before calling tool_bar_items, because the calculation of
10240 the tool-bar keymap uses the selected frame (see
10241 `tool-bar-make-keymap' in tool-bar.el). */
10242 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10243 XSETFRAME (frame, f);
10244 selected_frame = frame;
10245
10246 /* Build desired tool-bar items from keymaps. */
10247 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10248 &new_n_tool_bar);
10249
10250 /* Redisplay the tool-bar if we changed it. */
10251 if (new_n_tool_bar != f->n_tool_bar_items
10252 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10253 {
10254 /* Redisplay that happens asynchronously due to an expose event
10255 may access f->tool_bar_items. Make sure we update both
10256 variables within BLOCK_INPUT so no such event interrupts. */
10257 BLOCK_INPUT;
10258 f->tool_bar_items = new_tool_bar;
10259 f->n_tool_bar_items = new_n_tool_bar;
10260 w->update_mode_line = Qt;
10261 UNBLOCK_INPUT;
10262 }
10263
10264 UNGCPRO;
10265
10266 unbind_to (count, Qnil);
10267 set_buffer_internal_1 (prev);
10268 }
10269 }
10270 }
10271
10272
10273 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10274 F's desired tool-bar contents. F->tool_bar_items must have
10275 been set up previously by calling prepare_menu_bars. */
10276
10277 static void
10278 build_desired_tool_bar_string (struct frame *f)
10279 {
10280 int i, size, size_needed;
10281 struct gcpro gcpro1, gcpro2, gcpro3;
10282 Lisp_Object image, plist, props;
10283
10284 image = plist = props = Qnil;
10285 GCPRO3 (image, plist, props);
10286
10287 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10288 Otherwise, make a new string. */
10289
10290 /* The size of the string we might be able to reuse. */
10291 size = (STRINGP (f->desired_tool_bar_string)
10292 ? SCHARS (f->desired_tool_bar_string)
10293 : 0);
10294
10295 /* We need one space in the string for each image. */
10296 size_needed = f->n_tool_bar_items;
10297
10298 /* Reuse f->desired_tool_bar_string, if possible. */
10299 if (size < size_needed || NILP (f->desired_tool_bar_string))
10300 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10301 make_number (' '));
10302 else
10303 {
10304 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10305 Fremove_text_properties (make_number (0), make_number (size),
10306 props, f->desired_tool_bar_string);
10307 }
10308
10309 /* Put a `display' property on the string for the images to display,
10310 put a `menu_item' property on tool-bar items with a value that
10311 is the index of the item in F's tool-bar item vector. */
10312 for (i = 0; i < f->n_tool_bar_items; ++i)
10313 {
10314 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10315
10316 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10317 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10318 int hmargin, vmargin, relief, idx, end;
10319
10320 /* If image is a vector, choose the image according to the
10321 button state. */
10322 image = PROP (TOOL_BAR_ITEM_IMAGES);
10323 if (VECTORP (image))
10324 {
10325 if (enabled_p)
10326 idx = (selected_p
10327 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10328 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10329 else
10330 idx = (selected_p
10331 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10332 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10333
10334 xassert (ASIZE (image) >= idx);
10335 image = AREF (image, idx);
10336 }
10337 else
10338 idx = -1;
10339
10340 /* Ignore invalid image specifications. */
10341 if (!valid_image_p (image))
10342 continue;
10343
10344 /* Display the tool-bar button pressed, or depressed. */
10345 plist = Fcopy_sequence (XCDR (image));
10346
10347 /* Compute margin and relief to draw. */
10348 relief = (tool_bar_button_relief >= 0
10349 ? tool_bar_button_relief
10350 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10351 hmargin = vmargin = relief;
10352
10353 if (INTEGERP (Vtool_bar_button_margin)
10354 && XINT (Vtool_bar_button_margin) > 0)
10355 {
10356 hmargin += XFASTINT (Vtool_bar_button_margin);
10357 vmargin += XFASTINT (Vtool_bar_button_margin);
10358 }
10359 else if (CONSP (Vtool_bar_button_margin))
10360 {
10361 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10362 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10363 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10364
10365 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10366 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10367 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10368 }
10369
10370 if (auto_raise_tool_bar_buttons_p)
10371 {
10372 /* Add a `:relief' property to the image spec if the item is
10373 selected. */
10374 if (selected_p)
10375 {
10376 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10377 hmargin -= relief;
10378 vmargin -= relief;
10379 }
10380 }
10381 else
10382 {
10383 /* If image is selected, display it pressed, i.e. with a
10384 negative relief. If it's not selected, display it with a
10385 raised relief. */
10386 plist = Fplist_put (plist, QCrelief,
10387 (selected_p
10388 ? make_number (-relief)
10389 : make_number (relief)));
10390 hmargin -= relief;
10391 vmargin -= relief;
10392 }
10393
10394 /* Put a margin around the image. */
10395 if (hmargin || vmargin)
10396 {
10397 if (hmargin == vmargin)
10398 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10399 else
10400 plist = Fplist_put (plist, QCmargin,
10401 Fcons (make_number (hmargin),
10402 make_number (vmargin)));
10403 }
10404
10405 /* If button is not enabled, and we don't have special images
10406 for the disabled state, make the image appear disabled by
10407 applying an appropriate algorithm to it. */
10408 if (!enabled_p && idx < 0)
10409 plist = Fplist_put (plist, QCconversion, Qdisabled);
10410
10411 /* Put a `display' text property on the string for the image to
10412 display. Put a `menu-item' property on the string that gives
10413 the start of this item's properties in the tool-bar items
10414 vector. */
10415 image = Fcons (Qimage, plist);
10416 props = list4 (Qdisplay, image,
10417 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10418
10419 /* Let the last image hide all remaining spaces in the tool bar
10420 string. The string can be longer than needed when we reuse a
10421 previous string. */
10422 if (i + 1 == f->n_tool_bar_items)
10423 end = SCHARS (f->desired_tool_bar_string);
10424 else
10425 end = i + 1;
10426 Fadd_text_properties (make_number (i), make_number (end),
10427 props, f->desired_tool_bar_string);
10428 #undef PROP
10429 }
10430
10431 UNGCPRO;
10432 }
10433
10434
10435 /* Display one line of the tool-bar of frame IT->f.
10436
10437 HEIGHT specifies the desired height of the tool-bar line.
10438 If the actual height of the glyph row is less than HEIGHT, the
10439 row's height is increased to HEIGHT, and the icons are centered
10440 vertically in the new height.
10441
10442 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10443 count a final empty row in case the tool-bar width exactly matches
10444 the window width.
10445 */
10446
10447 static void
10448 display_tool_bar_line (struct it *it, int height)
10449 {
10450 struct glyph_row *row = it->glyph_row;
10451 int max_x = it->last_visible_x;
10452 struct glyph *last;
10453
10454 prepare_desired_row (row);
10455 row->y = it->current_y;
10456
10457 /* Note that this isn't made use of if the face hasn't a box,
10458 so there's no need to check the face here. */
10459 it->start_of_box_run_p = 1;
10460
10461 while (it->current_x < max_x)
10462 {
10463 int x, n_glyphs_before, i, nglyphs;
10464 struct it it_before;
10465
10466 /* Get the next display element. */
10467 if (!get_next_display_element (it))
10468 {
10469 /* Don't count empty row if we are counting needed tool-bar lines. */
10470 if (height < 0 && !it->hpos)
10471 return;
10472 break;
10473 }
10474
10475 /* Produce glyphs. */
10476 n_glyphs_before = row->used[TEXT_AREA];
10477 it_before = *it;
10478
10479 PRODUCE_GLYPHS (it);
10480
10481 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10482 i = 0;
10483 x = it_before.current_x;
10484 while (i < nglyphs)
10485 {
10486 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10487
10488 if (x + glyph->pixel_width > max_x)
10489 {
10490 /* Glyph doesn't fit on line. Backtrack. */
10491 row->used[TEXT_AREA] = n_glyphs_before;
10492 *it = it_before;
10493 /* If this is the only glyph on this line, it will never fit on the
10494 toolbar, so skip it. But ensure there is at least one glyph,
10495 so we don't accidentally disable the tool-bar. */
10496 if (n_glyphs_before == 0
10497 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10498 break;
10499 goto out;
10500 }
10501
10502 ++it->hpos;
10503 x += glyph->pixel_width;
10504 ++i;
10505 }
10506
10507 /* Stop at line ends. */
10508 if (ITERATOR_AT_END_OF_LINE_P (it))
10509 break;
10510
10511 set_iterator_to_next (it, 1);
10512 }
10513
10514 out:;
10515
10516 row->displays_text_p = row->used[TEXT_AREA] != 0;
10517
10518 /* Use default face for the border below the tool bar.
10519
10520 FIXME: When auto-resize-tool-bars is grow-only, there is
10521 no additional border below the possibly empty tool-bar lines.
10522 So to make the extra empty lines look "normal", we have to
10523 use the tool-bar face for the border too. */
10524 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10525 it->face_id = DEFAULT_FACE_ID;
10526
10527 extend_face_to_end_of_line (it);
10528 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10529 last->right_box_line_p = 1;
10530 if (last == row->glyphs[TEXT_AREA])
10531 last->left_box_line_p = 1;
10532
10533 /* Make line the desired height and center it vertically. */
10534 if ((height -= it->max_ascent + it->max_descent) > 0)
10535 {
10536 /* Don't add more than one line height. */
10537 height %= FRAME_LINE_HEIGHT (it->f);
10538 it->max_ascent += height / 2;
10539 it->max_descent += (height + 1) / 2;
10540 }
10541
10542 compute_line_metrics (it);
10543
10544 /* If line is empty, make it occupy the rest of the tool-bar. */
10545 if (!row->displays_text_p)
10546 {
10547 row->height = row->phys_height = it->last_visible_y - row->y;
10548 row->visible_height = row->height;
10549 row->ascent = row->phys_ascent = 0;
10550 row->extra_line_spacing = 0;
10551 }
10552
10553 row->full_width_p = 1;
10554 row->continued_p = 0;
10555 row->truncated_on_left_p = 0;
10556 row->truncated_on_right_p = 0;
10557
10558 it->current_x = it->hpos = 0;
10559 it->current_y += row->height;
10560 ++it->vpos;
10561 ++it->glyph_row;
10562 }
10563
10564
10565 /* Max tool-bar height. */
10566
10567 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10568 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10569
10570 /* Value is the number of screen lines needed to make all tool-bar
10571 items of frame F visible. The number of actual rows needed is
10572 returned in *N_ROWS if non-NULL. */
10573
10574 static int
10575 tool_bar_lines_needed (struct frame *f, int *n_rows)
10576 {
10577 struct window *w = XWINDOW (f->tool_bar_window);
10578 struct it it;
10579 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10580 the desired matrix, so use (unused) mode-line row as temporary row to
10581 avoid destroying the first tool-bar row. */
10582 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10583
10584 /* Initialize an iterator for iteration over
10585 F->desired_tool_bar_string in the tool-bar window of frame F. */
10586 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10587 it.first_visible_x = 0;
10588 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10589 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10590
10591 while (!ITERATOR_AT_END_P (&it))
10592 {
10593 clear_glyph_row (temp_row);
10594 it.glyph_row = temp_row;
10595 display_tool_bar_line (&it, -1);
10596 }
10597 clear_glyph_row (temp_row);
10598
10599 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10600 if (n_rows)
10601 *n_rows = it.vpos > 0 ? it.vpos : -1;
10602
10603 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10604 }
10605
10606
10607 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10608 0, 1, 0,
10609 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10610 (Lisp_Object frame)
10611 {
10612 struct frame *f;
10613 struct window *w;
10614 int nlines = 0;
10615
10616 if (NILP (frame))
10617 frame = selected_frame;
10618 else
10619 CHECK_FRAME (frame);
10620 f = XFRAME (frame);
10621
10622 if (WINDOWP (f->tool_bar_window)
10623 || (w = XWINDOW (f->tool_bar_window),
10624 WINDOW_TOTAL_LINES (w) > 0))
10625 {
10626 update_tool_bar (f, 1);
10627 if (f->n_tool_bar_items)
10628 {
10629 build_desired_tool_bar_string (f);
10630 nlines = tool_bar_lines_needed (f, NULL);
10631 }
10632 }
10633
10634 return make_number (nlines);
10635 }
10636
10637
10638 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10639 height should be changed. */
10640
10641 static int
10642 redisplay_tool_bar (struct frame *f)
10643 {
10644 struct window *w;
10645 struct it it;
10646 struct glyph_row *row;
10647
10648 #if defined (USE_GTK) || defined (HAVE_NS)
10649 if (FRAME_EXTERNAL_TOOL_BAR (f))
10650 update_frame_tool_bar (f);
10651 return 0;
10652 #endif
10653
10654 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10655 do anything. This means you must start with tool-bar-lines
10656 non-zero to get the auto-sizing effect. Or in other words, you
10657 can turn off tool-bars by specifying tool-bar-lines zero. */
10658 if (!WINDOWP (f->tool_bar_window)
10659 || (w = XWINDOW (f->tool_bar_window),
10660 WINDOW_TOTAL_LINES (w) == 0))
10661 return 0;
10662
10663 /* Set up an iterator for the tool-bar window. */
10664 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10665 it.first_visible_x = 0;
10666 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10667 row = it.glyph_row;
10668
10669 /* Build a string that represents the contents of the tool-bar. */
10670 build_desired_tool_bar_string (f);
10671 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10672
10673 if (f->n_tool_bar_rows == 0)
10674 {
10675 int nlines;
10676
10677 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10678 nlines != WINDOW_TOTAL_LINES (w)))
10679 {
10680 Lisp_Object frame;
10681 int old_height = WINDOW_TOTAL_LINES (w);
10682
10683 XSETFRAME (frame, f);
10684 Fmodify_frame_parameters (frame,
10685 Fcons (Fcons (Qtool_bar_lines,
10686 make_number (nlines)),
10687 Qnil));
10688 if (WINDOW_TOTAL_LINES (w) != old_height)
10689 {
10690 clear_glyph_matrix (w->desired_matrix);
10691 fonts_changed_p = 1;
10692 return 1;
10693 }
10694 }
10695 }
10696
10697 /* Display as many lines as needed to display all tool-bar items. */
10698
10699 if (f->n_tool_bar_rows > 0)
10700 {
10701 int border, rows, height, extra;
10702
10703 if (INTEGERP (Vtool_bar_border))
10704 border = XINT (Vtool_bar_border);
10705 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10706 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10707 else if (EQ (Vtool_bar_border, Qborder_width))
10708 border = f->border_width;
10709 else
10710 border = 0;
10711 if (border < 0)
10712 border = 0;
10713
10714 rows = f->n_tool_bar_rows;
10715 height = max (1, (it.last_visible_y - border) / rows);
10716 extra = it.last_visible_y - border - height * rows;
10717
10718 while (it.current_y < it.last_visible_y)
10719 {
10720 int h = 0;
10721 if (extra > 0 && rows-- > 0)
10722 {
10723 h = (extra + rows - 1) / rows;
10724 extra -= h;
10725 }
10726 display_tool_bar_line (&it, height + h);
10727 }
10728 }
10729 else
10730 {
10731 while (it.current_y < it.last_visible_y)
10732 display_tool_bar_line (&it, 0);
10733 }
10734
10735 /* It doesn't make much sense to try scrolling in the tool-bar
10736 window, so don't do it. */
10737 w->desired_matrix->no_scrolling_p = 1;
10738 w->must_be_updated_p = 1;
10739
10740 if (!NILP (Vauto_resize_tool_bars))
10741 {
10742 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10743 int change_height_p = 0;
10744
10745 /* If we couldn't display everything, change the tool-bar's
10746 height if there is room for more. */
10747 if (IT_STRING_CHARPOS (it) < it.end_charpos
10748 && it.current_y < max_tool_bar_height)
10749 change_height_p = 1;
10750
10751 row = it.glyph_row - 1;
10752
10753 /* If there are blank lines at the end, except for a partially
10754 visible blank line at the end that is smaller than
10755 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10756 if (!row->displays_text_p
10757 && row->height >= FRAME_LINE_HEIGHT (f))
10758 change_height_p = 1;
10759
10760 /* If row displays tool-bar items, but is partially visible,
10761 change the tool-bar's height. */
10762 if (row->displays_text_p
10763 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10764 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10765 change_height_p = 1;
10766
10767 /* Resize windows as needed by changing the `tool-bar-lines'
10768 frame parameter. */
10769 if (change_height_p)
10770 {
10771 Lisp_Object frame;
10772 int old_height = WINDOW_TOTAL_LINES (w);
10773 int nrows;
10774 int nlines = tool_bar_lines_needed (f, &nrows);
10775
10776 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10777 && !f->minimize_tool_bar_window_p)
10778 ? (nlines > old_height)
10779 : (nlines != old_height));
10780 f->minimize_tool_bar_window_p = 0;
10781
10782 if (change_height_p)
10783 {
10784 XSETFRAME (frame, f);
10785 Fmodify_frame_parameters (frame,
10786 Fcons (Fcons (Qtool_bar_lines,
10787 make_number (nlines)),
10788 Qnil));
10789 if (WINDOW_TOTAL_LINES (w) != old_height)
10790 {
10791 clear_glyph_matrix (w->desired_matrix);
10792 f->n_tool_bar_rows = nrows;
10793 fonts_changed_p = 1;
10794 return 1;
10795 }
10796 }
10797 }
10798 }
10799
10800 f->minimize_tool_bar_window_p = 0;
10801 return 0;
10802 }
10803
10804
10805 /* Get information about the tool-bar item which is displayed in GLYPH
10806 on frame F. Return in *PROP_IDX the index where tool-bar item
10807 properties start in F->tool_bar_items. Value is zero if
10808 GLYPH doesn't display a tool-bar item. */
10809
10810 static int
10811 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10812 {
10813 Lisp_Object prop;
10814 int success_p;
10815 int charpos;
10816
10817 /* This function can be called asynchronously, which means we must
10818 exclude any possibility that Fget_text_property signals an
10819 error. */
10820 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10821 charpos = max (0, charpos);
10822
10823 /* Get the text property `menu-item' at pos. The value of that
10824 property is the start index of this item's properties in
10825 F->tool_bar_items. */
10826 prop = Fget_text_property (make_number (charpos),
10827 Qmenu_item, f->current_tool_bar_string);
10828 if (INTEGERP (prop))
10829 {
10830 *prop_idx = XINT (prop);
10831 success_p = 1;
10832 }
10833 else
10834 success_p = 0;
10835
10836 return success_p;
10837 }
10838
10839 \f
10840 /* Get information about the tool-bar item at position X/Y on frame F.
10841 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10842 the current matrix of the tool-bar window of F, or NULL if not
10843 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10844 item in F->tool_bar_items. Value is
10845
10846 -1 if X/Y is not on a tool-bar item
10847 0 if X/Y is on the same item that was highlighted before.
10848 1 otherwise. */
10849
10850 static int
10851 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
10852 int *hpos, int *vpos, int *prop_idx)
10853 {
10854 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10855 struct window *w = XWINDOW (f->tool_bar_window);
10856 int area;
10857
10858 /* Find the glyph under X/Y. */
10859 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10860 if (*glyph == NULL)
10861 return -1;
10862
10863 /* Get the start of this tool-bar item's properties in
10864 f->tool_bar_items. */
10865 if (!tool_bar_item_info (f, *glyph, prop_idx))
10866 return -1;
10867
10868 /* Is mouse on the highlighted item? */
10869 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
10870 && *vpos >= hlinfo->mouse_face_beg_row
10871 && *vpos <= hlinfo->mouse_face_end_row
10872 && (*vpos > hlinfo->mouse_face_beg_row
10873 || *hpos >= hlinfo->mouse_face_beg_col)
10874 && (*vpos < hlinfo->mouse_face_end_row
10875 || *hpos < hlinfo->mouse_face_end_col
10876 || hlinfo->mouse_face_past_end))
10877 return 0;
10878
10879 return 1;
10880 }
10881
10882
10883 /* EXPORT:
10884 Handle mouse button event on the tool-bar of frame F, at
10885 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10886 0 for button release. MODIFIERS is event modifiers for button
10887 release. */
10888
10889 void
10890 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
10891 unsigned int modifiers)
10892 {
10893 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10894 struct window *w = XWINDOW (f->tool_bar_window);
10895 int hpos, vpos, prop_idx;
10896 struct glyph *glyph;
10897 Lisp_Object enabled_p;
10898
10899 /* If not on the highlighted tool-bar item, return. */
10900 frame_to_window_pixel_xy (w, &x, &y);
10901 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10902 return;
10903
10904 /* If item is disabled, do nothing. */
10905 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10906 if (NILP (enabled_p))
10907 return;
10908
10909 if (down_p)
10910 {
10911 /* Show item in pressed state. */
10912 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
10913 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10914 last_tool_bar_item = prop_idx;
10915 }
10916 else
10917 {
10918 Lisp_Object key, frame;
10919 struct input_event event;
10920 EVENT_INIT (event);
10921
10922 /* Show item in released state. */
10923 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
10924 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10925
10926 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10927
10928 XSETFRAME (frame, f);
10929 event.kind = TOOL_BAR_EVENT;
10930 event.frame_or_window = frame;
10931 event.arg = frame;
10932 kbd_buffer_store_event (&event);
10933
10934 event.kind = TOOL_BAR_EVENT;
10935 event.frame_or_window = frame;
10936 event.arg = key;
10937 event.modifiers = modifiers;
10938 kbd_buffer_store_event (&event);
10939 last_tool_bar_item = -1;
10940 }
10941 }
10942
10943
10944 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10945 tool-bar window-relative coordinates X/Y. Called from
10946 note_mouse_highlight. */
10947
10948 static void
10949 note_tool_bar_highlight (struct frame *f, int x, int y)
10950 {
10951 Lisp_Object window = f->tool_bar_window;
10952 struct window *w = XWINDOW (window);
10953 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10954 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10955 int hpos, vpos;
10956 struct glyph *glyph;
10957 struct glyph_row *row;
10958 int i;
10959 Lisp_Object enabled_p;
10960 int prop_idx;
10961 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10962 int mouse_down_p, rc;
10963
10964 /* Function note_mouse_highlight is called with negative X/Y
10965 values when mouse moves outside of the frame. */
10966 if (x <= 0 || y <= 0)
10967 {
10968 clear_mouse_face (hlinfo);
10969 return;
10970 }
10971
10972 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10973 if (rc < 0)
10974 {
10975 /* Not on tool-bar item. */
10976 clear_mouse_face (hlinfo);
10977 return;
10978 }
10979 else if (rc == 0)
10980 /* On same tool-bar item as before. */
10981 goto set_help_echo;
10982
10983 clear_mouse_face (hlinfo);
10984
10985 /* Mouse is down, but on different tool-bar item? */
10986 mouse_down_p = (dpyinfo->grabbed
10987 && f == last_mouse_frame
10988 && FRAME_LIVE_P (f));
10989 if (mouse_down_p
10990 && last_tool_bar_item != prop_idx)
10991 return;
10992
10993 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10994 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10995
10996 /* If tool-bar item is not enabled, don't highlight it. */
10997 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10998 if (!NILP (enabled_p))
10999 {
11000 /* Compute the x-position of the glyph. In front and past the
11001 image is a space. We include this in the highlighted area. */
11002 row = MATRIX_ROW (w->current_matrix, vpos);
11003 for (i = x = 0; i < hpos; ++i)
11004 x += row->glyphs[TEXT_AREA][i].pixel_width;
11005
11006 /* Record this as the current active region. */
11007 hlinfo->mouse_face_beg_col = hpos;
11008 hlinfo->mouse_face_beg_row = vpos;
11009 hlinfo->mouse_face_beg_x = x;
11010 hlinfo->mouse_face_beg_y = row->y;
11011 hlinfo->mouse_face_past_end = 0;
11012
11013 hlinfo->mouse_face_end_col = hpos + 1;
11014 hlinfo->mouse_face_end_row = vpos;
11015 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11016 hlinfo->mouse_face_end_y = row->y;
11017 hlinfo->mouse_face_window = window;
11018 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11019
11020 /* Display it as active. */
11021 show_mouse_face (hlinfo, draw);
11022 hlinfo->mouse_face_image_state = draw;
11023 }
11024
11025 set_help_echo:
11026
11027 /* Set help_echo_string to a help string to display for this tool-bar item.
11028 XTread_socket does the rest. */
11029 help_echo_object = help_echo_window = Qnil;
11030 help_echo_pos = -1;
11031 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11032 if (NILP (help_echo_string))
11033 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11034 }
11035
11036 #endif /* HAVE_WINDOW_SYSTEM */
11037
11038
11039 \f
11040 /************************************************************************
11041 Horizontal scrolling
11042 ************************************************************************/
11043
11044 static int hscroll_window_tree (Lisp_Object);
11045 static int hscroll_windows (Lisp_Object);
11046
11047 /* For all leaf windows in the window tree rooted at WINDOW, set their
11048 hscroll value so that PT is (i) visible in the window, and (ii) so
11049 that it is not within a certain margin at the window's left and
11050 right border. Value is non-zero if any window's hscroll has been
11051 changed. */
11052
11053 static int
11054 hscroll_window_tree (Lisp_Object window)
11055 {
11056 int hscrolled_p = 0;
11057 int hscroll_relative_p = FLOATP (Vhscroll_step);
11058 int hscroll_step_abs = 0;
11059 double hscroll_step_rel = 0;
11060
11061 if (hscroll_relative_p)
11062 {
11063 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11064 if (hscroll_step_rel < 0)
11065 {
11066 hscroll_relative_p = 0;
11067 hscroll_step_abs = 0;
11068 }
11069 }
11070 else if (INTEGERP (Vhscroll_step))
11071 {
11072 hscroll_step_abs = XINT (Vhscroll_step);
11073 if (hscroll_step_abs < 0)
11074 hscroll_step_abs = 0;
11075 }
11076 else
11077 hscroll_step_abs = 0;
11078
11079 while (WINDOWP (window))
11080 {
11081 struct window *w = XWINDOW (window);
11082
11083 if (WINDOWP (w->hchild))
11084 hscrolled_p |= hscroll_window_tree (w->hchild);
11085 else if (WINDOWP (w->vchild))
11086 hscrolled_p |= hscroll_window_tree (w->vchild);
11087 else if (w->cursor.vpos >= 0)
11088 {
11089 int h_margin;
11090 int text_area_width;
11091 struct glyph_row *current_cursor_row
11092 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11093 struct glyph_row *desired_cursor_row
11094 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11095 struct glyph_row *cursor_row
11096 = (desired_cursor_row->enabled_p
11097 ? desired_cursor_row
11098 : current_cursor_row);
11099
11100 text_area_width = window_box_width (w, TEXT_AREA);
11101
11102 /* Scroll when cursor is inside this scroll margin. */
11103 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11104
11105 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11106 && ((XFASTINT (w->hscroll)
11107 && w->cursor.x <= h_margin)
11108 || (cursor_row->enabled_p
11109 && cursor_row->truncated_on_right_p
11110 && (w->cursor.x >= text_area_width - h_margin))))
11111 {
11112 struct it it;
11113 int hscroll;
11114 struct buffer *saved_current_buffer;
11115 EMACS_INT pt;
11116 int wanted_x;
11117
11118 /* Find point in a display of infinite width. */
11119 saved_current_buffer = current_buffer;
11120 current_buffer = XBUFFER (w->buffer);
11121
11122 if (w == XWINDOW (selected_window))
11123 pt = BUF_PT (current_buffer);
11124 else
11125 {
11126 pt = marker_position (w->pointm);
11127 pt = max (BEGV, pt);
11128 pt = min (ZV, pt);
11129 }
11130
11131 /* Move iterator to pt starting at cursor_row->start in
11132 a line with infinite width. */
11133 init_to_row_start (&it, w, cursor_row);
11134 it.last_visible_x = INFINITY;
11135 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11136 current_buffer = saved_current_buffer;
11137
11138 /* Position cursor in window. */
11139 if (!hscroll_relative_p && hscroll_step_abs == 0)
11140 hscroll = max (0, (it.current_x
11141 - (ITERATOR_AT_END_OF_LINE_P (&it)
11142 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11143 : (text_area_width / 2))))
11144 / FRAME_COLUMN_WIDTH (it.f);
11145 else if (w->cursor.x >= text_area_width - h_margin)
11146 {
11147 if (hscroll_relative_p)
11148 wanted_x = text_area_width * (1 - hscroll_step_rel)
11149 - h_margin;
11150 else
11151 wanted_x = text_area_width
11152 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11153 - h_margin;
11154 hscroll
11155 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11156 }
11157 else
11158 {
11159 if (hscroll_relative_p)
11160 wanted_x = text_area_width * hscroll_step_rel
11161 + h_margin;
11162 else
11163 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11164 + h_margin;
11165 hscroll
11166 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11167 }
11168 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11169
11170 /* Don't call Fset_window_hscroll if value hasn't
11171 changed because it will prevent redisplay
11172 optimizations. */
11173 if (XFASTINT (w->hscroll) != hscroll)
11174 {
11175 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11176 w->hscroll = make_number (hscroll);
11177 hscrolled_p = 1;
11178 }
11179 }
11180 }
11181
11182 window = w->next;
11183 }
11184
11185 /* Value is non-zero if hscroll of any leaf window has been changed. */
11186 return hscrolled_p;
11187 }
11188
11189
11190 /* Set hscroll so that cursor is visible and not inside horizontal
11191 scroll margins for all windows in the tree rooted at WINDOW. See
11192 also hscroll_window_tree above. Value is non-zero if any window's
11193 hscroll has been changed. If it has, desired matrices on the frame
11194 of WINDOW are cleared. */
11195
11196 static int
11197 hscroll_windows (Lisp_Object window)
11198 {
11199 int hscrolled_p = hscroll_window_tree (window);
11200 if (hscrolled_p)
11201 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11202 return hscrolled_p;
11203 }
11204
11205
11206 \f
11207 /************************************************************************
11208 Redisplay
11209 ************************************************************************/
11210
11211 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11212 to a non-zero value. This is sometimes handy to have in a debugger
11213 session. */
11214
11215 #if GLYPH_DEBUG
11216
11217 /* First and last unchanged row for try_window_id. */
11218
11219 int debug_first_unchanged_at_end_vpos;
11220 int debug_last_unchanged_at_beg_vpos;
11221
11222 /* Delta vpos and y. */
11223
11224 int debug_dvpos, debug_dy;
11225
11226 /* Delta in characters and bytes for try_window_id. */
11227
11228 EMACS_INT debug_delta, debug_delta_bytes;
11229
11230 /* Values of window_end_pos and window_end_vpos at the end of
11231 try_window_id. */
11232
11233 EMACS_INT debug_end_pos, debug_end_vpos;
11234
11235 /* Append a string to W->desired_matrix->method. FMT is a printf
11236 format string. A1...A9 are a supplement for a variable-length
11237 argument list. If trace_redisplay_p is non-zero also printf the
11238 resulting string to stderr. */
11239
11240 static void
11241 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11242 struct window *w;
11243 char *fmt;
11244 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11245 {
11246 char buffer[512];
11247 char *method = w->desired_matrix->method;
11248 int len = strlen (method);
11249 int size = sizeof w->desired_matrix->method;
11250 int remaining = size - len - 1;
11251
11252 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11253 if (len && remaining)
11254 {
11255 method[len] = '|';
11256 --remaining, ++len;
11257 }
11258
11259 strncpy (method + len, buffer, remaining);
11260
11261 if (trace_redisplay_p)
11262 fprintf (stderr, "%p (%s): %s\n",
11263 w,
11264 ((BUFFERP (w->buffer)
11265 && STRINGP (XBUFFER (w->buffer)->name))
11266 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11267 : "no buffer"),
11268 buffer);
11269 }
11270
11271 #endif /* GLYPH_DEBUG */
11272
11273
11274 /* Value is non-zero if all changes in window W, which displays
11275 current_buffer, are in the text between START and END. START is a
11276 buffer position, END is given as a distance from Z. Used in
11277 redisplay_internal for display optimization. */
11278
11279 static INLINE int
11280 text_outside_line_unchanged_p (struct window *w,
11281 EMACS_INT start, EMACS_INT end)
11282 {
11283 int unchanged_p = 1;
11284
11285 /* If text or overlays have changed, see where. */
11286 if (XFASTINT (w->last_modified) < MODIFF
11287 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11288 {
11289 /* Gap in the line? */
11290 if (GPT < start || Z - GPT < end)
11291 unchanged_p = 0;
11292
11293 /* Changes start in front of the line, or end after it? */
11294 if (unchanged_p
11295 && (BEG_UNCHANGED < start - 1
11296 || END_UNCHANGED < end))
11297 unchanged_p = 0;
11298
11299 /* If selective display, can't optimize if changes start at the
11300 beginning of the line. */
11301 if (unchanged_p
11302 && INTEGERP (current_buffer->selective_display)
11303 && XINT (current_buffer->selective_display) > 0
11304 && (BEG_UNCHANGED < start || GPT <= start))
11305 unchanged_p = 0;
11306
11307 /* If there are overlays at the start or end of the line, these
11308 may have overlay strings with newlines in them. A change at
11309 START, for instance, may actually concern the display of such
11310 overlay strings as well, and they are displayed on different
11311 lines. So, quickly rule out this case. (For the future, it
11312 might be desirable to implement something more telling than
11313 just BEG/END_UNCHANGED.) */
11314 if (unchanged_p)
11315 {
11316 if (BEG + BEG_UNCHANGED == start
11317 && overlay_touches_p (start))
11318 unchanged_p = 0;
11319 if (END_UNCHANGED == end
11320 && overlay_touches_p (Z - end))
11321 unchanged_p = 0;
11322 }
11323
11324 /* Under bidi reordering, adding or deleting a character in the
11325 beginning of a paragraph, before the first strong directional
11326 character, can change the base direction of the paragraph (unless
11327 the buffer specifies a fixed paragraph direction), which will
11328 require to redisplay the whole paragraph. It might be worthwhile
11329 to find the paragraph limits and widen the range of redisplayed
11330 lines to that, but for now just give up this optimization. */
11331 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11332 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11333 unchanged_p = 0;
11334 }
11335
11336 return unchanged_p;
11337 }
11338
11339
11340 /* Do a frame update, taking possible shortcuts into account. This is
11341 the main external entry point for redisplay.
11342
11343 If the last redisplay displayed an echo area message and that message
11344 is no longer requested, we clear the echo area or bring back the
11345 mini-buffer if that is in use. */
11346
11347 void
11348 redisplay (void)
11349 {
11350 redisplay_internal (0);
11351 }
11352
11353
11354 static Lisp_Object
11355 overlay_arrow_string_or_property (Lisp_Object var)
11356 {
11357 Lisp_Object val;
11358
11359 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11360 return val;
11361
11362 return Voverlay_arrow_string;
11363 }
11364
11365 /* Return 1 if there are any overlay-arrows in current_buffer. */
11366 static int
11367 overlay_arrow_in_current_buffer_p (void)
11368 {
11369 Lisp_Object vlist;
11370
11371 for (vlist = Voverlay_arrow_variable_list;
11372 CONSP (vlist);
11373 vlist = XCDR (vlist))
11374 {
11375 Lisp_Object var = XCAR (vlist);
11376 Lisp_Object val;
11377
11378 if (!SYMBOLP (var))
11379 continue;
11380 val = find_symbol_value (var);
11381 if (MARKERP (val)
11382 && current_buffer == XMARKER (val)->buffer)
11383 return 1;
11384 }
11385 return 0;
11386 }
11387
11388
11389 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11390 has changed. */
11391
11392 static int
11393 overlay_arrows_changed_p (void)
11394 {
11395 Lisp_Object vlist;
11396
11397 for (vlist = Voverlay_arrow_variable_list;
11398 CONSP (vlist);
11399 vlist = XCDR (vlist))
11400 {
11401 Lisp_Object var = XCAR (vlist);
11402 Lisp_Object val, pstr;
11403
11404 if (!SYMBOLP (var))
11405 continue;
11406 val = find_symbol_value (var);
11407 if (!MARKERP (val))
11408 continue;
11409 if (! EQ (COERCE_MARKER (val),
11410 Fget (var, Qlast_arrow_position))
11411 || ! (pstr = overlay_arrow_string_or_property (var),
11412 EQ (pstr, Fget (var, Qlast_arrow_string))))
11413 return 1;
11414 }
11415 return 0;
11416 }
11417
11418 /* Mark overlay arrows to be updated on next redisplay. */
11419
11420 static void
11421 update_overlay_arrows (int up_to_date)
11422 {
11423 Lisp_Object vlist;
11424
11425 for (vlist = Voverlay_arrow_variable_list;
11426 CONSP (vlist);
11427 vlist = XCDR (vlist))
11428 {
11429 Lisp_Object var = XCAR (vlist);
11430
11431 if (!SYMBOLP (var))
11432 continue;
11433
11434 if (up_to_date > 0)
11435 {
11436 Lisp_Object val = find_symbol_value (var);
11437 Fput (var, Qlast_arrow_position,
11438 COERCE_MARKER (val));
11439 Fput (var, Qlast_arrow_string,
11440 overlay_arrow_string_or_property (var));
11441 }
11442 else if (up_to_date < 0
11443 || !NILP (Fget (var, Qlast_arrow_position)))
11444 {
11445 Fput (var, Qlast_arrow_position, Qt);
11446 Fput (var, Qlast_arrow_string, Qt);
11447 }
11448 }
11449 }
11450
11451
11452 /* Return overlay arrow string to display at row.
11453 Return integer (bitmap number) for arrow bitmap in left fringe.
11454 Return nil if no overlay arrow. */
11455
11456 static Lisp_Object
11457 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11458 {
11459 Lisp_Object vlist;
11460
11461 for (vlist = Voverlay_arrow_variable_list;
11462 CONSP (vlist);
11463 vlist = XCDR (vlist))
11464 {
11465 Lisp_Object var = XCAR (vlist);
11466 Lisp_Object val;
11467
11468 if (!SYMBOLP (var))
11469 continue;
11470
11471 val = find_symbol_value (var);
11472
11473 if (MARKERP (val)
11474 && current_buffer == XMARKER (val)->buffer
11475 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11476 {
11477 if (FRAME_WINDOW_P (it->f)
11478 /* FIXME: if ROW->reversed_p is set, this should test
11479 the right fringe, not the left one. */
11480 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11481 {
11482 #ifdef HAVE_WINDOW_SYSTEM
11483 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11484 {
11485 int fringe_bitmap;
11486 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11487 return make_number (fringe_bitmap);
11488 }
11489 #endif
11490 return make_number (-1); /* Use default arrow bitmap */
11491 }
11492 return overlay_arrow_string_or_property (var);
11493 }
11494 }
11495
11496 return Qnil;
11497 }
11498
11499 /* Return 1 if point moved out of or into a composition. Otherwise
11500 return 0. PREV_BUF and PREV_PT are the last point buffer and
11501 position. BUF and PT are the current point buffer and position. */
11502
11503 int
11504 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11505 struct buffer *buf, EMACS_INT pt)
11506 {
11507 EMACS_INT start, end;
11508 Lisp_Object prop;
11509 Lisp_Object buffer;
11510
11511 XSETBUFFER (buffer, buf);
11512 /* Check a composition at the last point if point moved within the
11513 same buffer. */
11514 if (prev_buf == buf)
11515 {
11516 if (prev_pt == pt)
11517 /* Point didn't move. */
11518 return 0;
11519
11520 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11521 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11522 && COMPOSITION_VALID_P (start, end, prop)
11523 && start < prev_pt && end > prev_pt)
11524 /* The last point was within the composition. Return 1 iff
11525 point moved out of the composition. */
11526 return (pt <= start || pt >= end);
11527 }
11528
11529 /* Check a composition at the current point. */
11530 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11531 && find_composition (pt, -1, &start, &end, &prop, buffer)
11532 && COMPOSITION_VALID_P (start, end, prop)
11533 && start < pt && end > pt);
11534 }
11535
11536
11537 /* Reconsider the setting of B->clip_changed which is displayed
11538 in window W. */
11539
11540 static INLINE void
11541 reconsider_clip_changes (struct window *w, struct buffer *b)
11542 {
11543 if (b->clip_changed
11544 && !NILP (w->window_end_valid)
11545 && w->current_matrix->buffer == b
11546 && w->current_matrix->zv == BUF_ZV (b)
11547 && w->current_matrix->begv == BUF_BEGV (b))
11548 b->clip_changed = 0;
11549
11550 /* If display wasn't paused, and W is not a tool bar window, see if
11551 point has been moved into or out of a composition. In that case,
11552 we set b->clip_changed to 1 to force updating the screen. If
11553 b->clip_changed has already been set to 1, we can skip this
11554 check. */
11555 if (!b->clip_changed
11556 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11557 {
11558 EMACS_INT pt;
11559
11560 if (w == XWINDOW (selected_window))
11561 pt = BUF_PT (current_buffer);
11562 else
11563 pt = marker_position (w->pointm);
11564
11565 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11566 || pt != XINT (w->last_point))
11567 && check_point_in_composition (w->current_matrix->buffer,
11568 XINT (w->last_point),
11569 XBUFFER (w->buffer), pt))
11570 b->clip_changed = 1;
11571 }
11572 }
11573 \f
11574
11575 /* Select FRAME to forward the values of frame-local variables into C
11576 variables so that the redisplay routines can access those values
11577 directly. */
11578
11579 static void
11580 select_frame_for_redisplay (Lisp_Object frame)
11581 {
11582 Lisp_Object tail, tem;
11583 Lisp_Object old = selected_frame;
11584 struct Lisp_Symbol *sym;
11585
11586 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11587
11588 selected_frame = frame;
11589
11590 do {
11591 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11592 if (CONSP (XCAR (tail))
11593 && (tem = XCAR (XCAR (tail)),
11594 SYMBOLP (tem))
11595 && (sym = indirect_variable (XSYMBOL (tem)),
11596 sym->redirect == SYMBOL_LOCALIZED)
11597 && sym->val.blv->frame_local)
11598 /* Use find_symbol_value rather than Fsymbol_value
11599 to avoid an error if it is void. */
11600 find_symbol_value (tem);
11601 } while (!EQ (frame, old) && (frame = old, 1));
11602 }
11603
11604
11605 #define STOP_POLLING \
11606 do { if (! polling_stopped_here) stop_polling (); \
11607 polling_stopped_here = 1; } while (0)
11608
11609 #define RESUME_POLLING \
11610 do { if (polling_stopped_here) start_polling (); \
11611 polling_stopped_here = 0; } while (0)
11612
11613
11614 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11615 response to any user action; therefore, we should preserve the echo
11616 area. (Actually, our caller does that job.) Perhaps in the future
11617 avoid recentering windows if it is not necessary; currently that
11618 causes some problems. */
11619
11620 static void
11621 redisplay_internal (int preserve_echo_area)
11622 {
11623 struct window *w = XWINDOW (selected_window);
11624 struct frame *f;
11625 int pause;
11626 int must_finish = 0;
11627 struct text_pos tlbufpos, tlendpos;
11628 int number_of_visible_frames;
11629 int count, count1;
11630 struct frame *sf;
11631 int polling_stopped_here = 0;
11632 Lisp_Object old_frame = selected_frame;
11633
11634 /* Non-zero means redisplay has to consider all windows on all
11635 frames. Zero means, only selected_window is considered. */
11636 int consider_all_windows_p;
11637
11638 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11639
11640 /* No redisplay if running in batch mode or frame is not yet fully
11641 initialized, or redisplay is explicitly turned off by setting
11642 Vinhibit_redisplay. */
11643 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11644 || !NILP (Vinhibit_redisplay))
11645 return;
11646
11647 /* Don't examine these until after testing Vinhibit_redisplay.
11648 When Emacs is shutting down, perhaps because its connection to
11649 X has dropped, we should not look at them at all. */
11650 f = XFRAME (w->frame);
11651 sf = SELECTED_FRAME ();
11652
11653 if (!f->glyphs_initialized_p)
11654 return;
11655
11656 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11657 if (popup_activated ())
11658 return;
11659 #endif
11660
11661 /* I don't think this happens but let's be paranoid. */
11662 if (redisplaying_p)
11663 return;
11664
11665 /* Record a function that resets redisplaying_p to its old value
11666 when we leave this function. */
11667 count = SPECPDL_INDEX ();
11668 record_unwind_protect (unwind_redisplay,
11669 Fcons (make_number (redisplaying_p), selected_frame));
11670 ++redisplaying_p;
11671 specbind (Qinhibit_free_realized_faces, Qnil);
11672
11673 {
11674 Lisp_Object tail, frame;
11675
11676 FOR_EACH_FRAME (tail, frame)
11677 {
11678 struct frame *f = XFRAME (frame);
11679 f->already_hscrolled_p = 0;
11680 }
11681 }
11682
11683 retry:
11684 if (!EQ (old_frame, selected_frame)
11685 && FRAME_LIVE_P (XFRAME (old_frame)))
11686 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11687 selected_frame and selected_window to be temporarily out-of-sync so
11688 when we come back here via `goto retry', we need to resync because we
11689 may need to run Elisp code (via prepare_menu_bars). */
11690 select_frame_for_redisplay (old_frame);
11691
11692 pause = 0;
11693 reconsider_clip_changes (w, current_buffer);
11694 last_escape_glyph_frame = NULL;
11695 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11696 last_glyphless_glyph_frame = NULL;
11697 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
11698
11699 /* If new fonts have been loaded that make a glyph matrix adjustment
11700 necessary, do it. */
11701 if (fonts_changed_p)
11702 {
11703 adjust_glyphs (NULL);
11704 ++windows_or_buffers_changed;
11705 fonts_changed_p = 0;
11706 }
11707
11708 /* If face_change_count is non-zero, init_iterator will free all
11709 realized faces, which includes the faces referenced from current
11710 matrices. So, we can't reuse current matrices in this case. */
11711 if (face_change_count)
11712 ++windows_or_buffers_changed;
11713
11714 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11715 && FRAME_TTY (sf)->previous_frame != sf)
11716 {
11717 /* Since frames on a single ASCII terminal share the same
11718 display area, displaying a different frame means redisplay
11719 the whole thing. */
11720 windows_or_buffers_changed++;
11721 SET_FRAME_GARBAGED (sf);
11722 #ifndef DOS_NT
11723 set_tty_color_mode (FRAME_TTY (sf), sf);
11724 #endif
11725 FRAME_TTY (sf)->previous_frame = sf;
11726 }
11727
11728 /* Set the visible flags for all frames. Do this before checking
11729 for resized or garbaged frames; they want to know if their frames
11730 are visible. See the comment in frame.h for
11731 FRAME_SAMPLE_VISIBILITY. */
11732 {
11733 Lisp_Object tail, frame;
11734
11735 number_of_visible_frames = 0;
11736
11737 FOR_EACH_FRAME (tail, frame)
11738 {
11739 struct frame *f = XFRAME (frame);
11740
11741 FRAME_SAMPLE_VISIBILITY (f);
11742 if (FRAME_VISIBLE_P (f))
11743 ++number_of_visible_frames;
11744 clear_desired_matrices (f);
11745 }
11746 }
11747
11748 /* Notice any pending interrupt request to change frame size. */
11749 do_pending_window_change (1);
11750
11751 /* Clear frames marked as garbaged. */
11752 if (frame_garbaged)
11753 clear_garbaged_frames ();
11754
11755 /* Build menubar and tool-bar items. */
11756 if (NILP (Vmemory_full))
11757 prepare_menu_bars ();
11758
11759 if (windows_or_buffers_changed)
11760 update_mode_lines++;
11761
11762 /* Detect case that we need to write or remove a star in the mode line. */
11763 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11764 {
11765 w->update_mode_line = Qt;
11766 if (buffer_shared > 1)
11767 update_mode_lines++;
11768 }
11769
11770 /* Avoid invocation of point motion hooks by `current_column' below. */
11771 count1 = SPECPDL_INDEX ();
11772 specbind (Qinhibit_point_motion_hooks, Qt);
11773
11774 /* If %c is in the mode line, update it if needed. */
11775 if (!NILP (w->column_number_displayed)
11776 /* This alternative quickly identifies a common case
11777 where no change is needed. */
11778 && !(PT == XFASTINT (w->last_point)
11779 && XFASTINT (w->last_modified) >= MODIFF
11780 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11781 && (XFASTINT (w->column_number_displayed)
11782 != (int) current_column ())) /* iftc */
11783 w->update_mode_line = Qt;
11784
11785 unbind_to (count1, Qnil);
11786
11787 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11788
11789 /* The variable buffer_shared is set in redisplay_window and
11790 indicates that we redisplay a buffer in different windows. See
11791 there. */
11792 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11793 || cursor_type_changed);
11794
11795 /* If specs for an arrow have changed, do thorough redisplay
11796 to ensure we remove any arrow that should no longer exist. */
11797 if (overlay_arrows_changed_p ())
11798 consider_all_windows_p = windows_or_buffers_changed = 1;
11799
11800 /* Normally the message* functions will have already displayed and
11801 updated the echo area, but the frame may have been trashed, or
11802 the update may have been preempted, so display the echo area
11803 again here. Checking message_cleared_p captures the case that
11804 the echo area should be cleared. */
11805 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11806 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11807 || (message_cleared_p
11808 && minibuf_level == 0
11809 /* If the mini-window is currently selected, this means the
11810 echo-area doesn't show through. */
11811 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11812 {
11813 int window_height_changed_p = echo_area_display (0);
11814 must_finish = 1;
11815
11816 /* If we don't display the current message, don't clear the
11817 message_cleared_p flag, because, if we did, we wouldn't clear
11818 the echo area in the next redisplay which doesn't preserve
11819 the echo area. */
11820 if (!display_last_displayed_message_p)
11821 message_cleared_p = 0;
11822
11823 if (fonts_changed_p)
11824 goto retry;
11825 else if (window_height_changed_p)
11826 {
11827 consider_all_windows_p = 1;
11828 ++update_mode_lines;
11829 ++windows_or_buffers_changed;
11830
11831 /* If window configuration was changed, frames may have been
11832 marked garbaged. Clear them or we will experience
11833 surprises wrt scrolling. */
11834 if (frame_garbaged)
11835 clear_garbaged_frames ();
11836 }
11837 }
11838 else if (EQ (selected_window, minibuf_window)
11839 && (current_buffer->clip_changed
11840 || XFASTINT (w->last_modified) < MODIFF
11841 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11842 && resize_mini_window (w, 0))
11843 {
11844 /* Resized active mini-window to fit the size of what it is
11845 showing if its contents might have changed. */
11846 must_finish = 1;
11847 /* FIXME: this causes all frames to be updated, which seems unnecessary
11848 since only the current frame needs to be considered. This function needs
11849 to be rewritten with two variables, consider_all_windows and
11850 consider_all_frames. */
11851 consider_all_windows_p = 1;
11852 ++windows_or_buffers_changed;
11853 ++update_mode_lines;
11854
11855 /* If window configuration was changed, frames may have been
11856 marked garbaged. Clear them or we will experience
11857 surprises wrt scrolling. */
11858 if (frame_garbaged)
11859 clear_garbaged_frames ();
11860 }
11861
11862
11863 /* If showing the region, and mark has changed, we must redisplay
11864 the whole window. The assignment to this_line_start_pos prevents
11865 the optimization directly below this if-statement. */
11866 if (((!NILP (Vtransient_mark_mode)
11867 && !NILP (XBUFFER (w->buffer)->mark_active))
11868 != !NILP (w->region_showing))
11869 || (!NILP (w->region_showing)
11870 && !EQ (w->region_showing,
11871 Fmarker_position (XBUFFER (w->buffer)->mark))))
11872 CHARPOS (this_line_start_pos) = 0;
11873
11874 /* Optimize the case that only the line containing the cursor in the
11875 selected window has changed. Variables starting with this_ are
11876 set in display_line and record information about the line
11877 containing the cursor. */
11878 tlbufpos = this_line_start_pos;
11879 tlendpos = this_line_end_pos;
11880 if (!consider_all_windows_p
11881 && CHARPOS (tlbufpos) > 0
11882 && NILP (w->update_mode_line)
11883 && !current_buffer->clip_changed
11884 && !current_buffer->prevent_redisplay_optimizations_p
11885 && FRAME_VISIBLE_P (XFRAME (w->frame))
11886 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11887 /* Make sure recorded data applies to current buffer, etc. */
11888 && this_line_buffer == current_buffer
11889 && current_buffer == XBUFFER (w->buffer)
11890 && NILP (w->force_start)
11891 && NILP (w->optional_new_start)
11892 /* Point must be on the line that we have info recorded about. */
11893 && PT >= CHARPOS (tlbufpos)
11894 && PT <= Z - CHARPOS (tlendpos)
11895 /* All text outside that line, including its final newline,
11896 must be unchanged. */
11897 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11898 CHARPOS (tlendpos)))
11899 {
11900 if (CHARPOS (tlbufpos) > BEGV
11901 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11902 && (CHARPOS (tlbufpos) == ZV
11903 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11904 /* Former continuation line has disappeared by becoming empty. */
11905 goto cancel;
11906 else if (XFASTINT (w->last_modified) < MODIFF
11907 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11908 || MINI_WINDOW_P (w))
11909 {
11910 /* We have to handle the case of continuation around a
11911 wide-column character (see the comment in indent.c around
11912 line 1340).
11913
11914 For instance, in the following case:
11915
11916 -------- Insert --------
11917 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11918 J_I_ ==> J_I_ `^^' are cursors.
11919 ^^ ^^
11920 -------- --------
11921
11922 As we have to redraw the line above, we cannot use this
11923 optimization. */
11924
11925 struct it it;
11926 int line_height_before = this_line_pixel_height;
11927
11928 /* Note that start_display will handle the case that the
11929 line starting at tlbufpos is a continuation line. */
11930 start_display (&it, w, tlbufpos);
11931
11932 /* Implementation note: It this still necessary? */
11933 if (it.current_x != this_line_start_x)
11934 goto cancel;
11935
11936 TRACE ((stderr, "trying display optimization 1\n"));
11937 w->cursor.vpos = -1;
11938 overlay_arrow_seen = 0;
11939 it.vpos = this_line_vpos;
11940 it.current_y = this_line_y;
11941 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11942 display_line (&it);
11943
11944 /* If line contains point, is not continued,
11945 and ends at same distance from eob as before, we win. */
11946 if (w->cursor.vpos >= 0
11947 /* Line is not continued, otherwise this_line_start_pos
11948 would have been set to 0 in display_line. */
11949 && CHARPOS (this_line_start_pos)
11950 /* Line ends as before. */
11951 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11952 /* Line has same height as before. Otherwise other lines
11953 would have to be shifted up or down. */
11954 && this_line_pixel_height == line_height_before)
11955 {
11956 /* If this is not the window's last line, we must adjust
11957 the charstarts of the lines below. */
11958 if (it.current_y < it.last_visible_y)
11959 {
11960 struct glyph_row *row
11961 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11962 EMACS_INT delta, delta_bytes;
11963
11964 /* We used to distinguish between two cases here,
11965 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11966 when the line ends in a newline or the end of the
11967 buffer's accessible portion. But both cases did
11968 the same, so they were collapsed. */
11969 delta = (Z
11970 - CHARPOS (tlendpos)
11971 - MATRIX_ROW_START_CHARPOS (row));
11972 delta_bytes = (Z_BYTE
11973 - BYTEPOS (tlendpos)
11974 - MATRIX_ROW_START_BYTEPOS (row));
11975
11976 increment_matrix_positions (w->current_matrix,
11977 this_line_vpos + 1,
11978 w->current_matrix->nrows,
11979 delta, delta_bytes);
11980 }
11981
11982 /* If this row displays text now but previously didn't,
11983 or vice versa, w->window_end_vpos may have to be
11984 adjusted. */
11985 if ((it.glyph_row - 1)->displays_text_p)
11986 {
11987 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11988 XSETINT (w->window_end_vpos, this_line_vpos);
11989 }
11990 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11991 && this_line_vpos > 0)
11992 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11993 w->window_end_valid = Qnil;
11994
11995 /* Update hint: No need to try to scroll in update_window. */
11996 w->desired_matrix->no_scrolling_p = 1;
11997
11998 #if GLYPH_DEBUG
11999 *w->desired_matrix->method = 0;
12000 debug_method_add (w, "optimization 1");
12001 #endif
12002 #ifdef HAVE_WINDOW_SYSTEM
12003 update_window_fringes (w, 0);
12004 #endif
12005 goto update;
12006 }
12007 else
12008 goto cancel;
12009 }
12010 else if (/* Cursor position hasn't changed. */
12011 PT == XFASTINT (w->last_point)
12012 /* Make sure the cursor was last displayed
12013 in this window. Otherwise we have to reposition it. */
12014 && 0 <= w->cursor.vpos
12015 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12016 {
12017 if (!must_finish)
12018 {
12019 do_pending_window_change (1);
12020
12021 /* We used to always goto end_of_redisplay here, but this
12022 isn't enough if we have a blinking cursor. */
12023 if (w->cursor_off_p == w->last_cursor_off_p)
12024 goto end_of_redisplay;
12025 }
12026 goto update;
12027 }
12028 /* If highlighting the region, or if the cursor is in the echo area,
12029 then we can't just move the cursor. */
12030 else if (! (!NILP (Vtransient_mark_mode)
12031 && !NILP (current_buffer->mark_active))
12032 && (EQ (selected_window, current_buffer->last_selected_window)
12033 || highlight_nonselected_windows)
12034 && NILP (w->region_showing)
12035 && NILP (Vshow_trailing_whitespace)
12036 && !cursor_in_echo_area)
12037 {
12038 struct it it;
12039 struct glyph_row *row;
12040
12041 /* Skip from tlbufpos to PT and see where it is. Note that
12042 PT may be in invisible text. If so, we will end at the
12043 next visible position. */
12044 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12045 NULL, DEFAULT_FACE_ID);
12046 it.current_x = this_line_start_x;
12047 it.current_y = this_line_y;
12048 it.vpos = this_line_vpos;
12049
12050 /* The call to move_it_to stops in front of PT, but
12051 moves over before-strings. */
12052 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12053
12054 if (it.vpos == this_line_vpos
12055 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12056 row->enabled_p))
12057 {
12058 xassert (this_line_vpos == it.vpos);
12059 xassert (this_line_y == it.current_y);
12060 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12061 #if GLYPH_DEBUG
12062 *w->desired_matrix->method = 0;
12063 debug_method_add (w, "optimization 3");
12064 #endif
12065 goto update;
12066 }
12067 else
12068 goto cancel;
12069 }
12070
12071 cancel:
12072 /* Text changed drastically or point moved off of line. */
12073 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12074 }
12075
12076 CHARPOS (this_line_start_pos) = 0;
12077 consider_all_windows_p |= buffer_shared > 1;
12078 ++clear_face_cache_count;
12079 #ifdef HAVE_WINDOW_SYSTEM
12080 ++clear_image_cache_count;
12081 #endif
12082
12083 /* Build desired matrices, and update the display. If
12084 consider_all_windows_p is non-zero, do it for all windows on all
12085 frames. Otherwise do it for selected_window, only. */
12086
12087 if (consider_all_windows_p)
12088 {
12089 Lisp_Object tail, frame;
12090
12091 FOR_EACH_FRAME (tail, frame)
12092 XFRAME (frame)->updated_p = 0;
12093
12094 /* Recompute # windows showing selected buffer. This will be
12095 incremented each time such a window is displayed. */
12096 buffer_shared = 0;
12097
12098 FOR_EACH_FRAME (tail, frame)
12099 {
12100 struct frame *f = XFRAME (frame);
12101
12102 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12103 {
12104 if (! EQ (frame, selected_frame))
12105 /* Select the frame, for the sake of frame-local
12106 variables. */
12107 select_frame_for_redisplay (frame);
12108
12109 /* Mark all the scroll bars to be removed; we'll redeem
12110 the ones we want when we redisplay their windows. */
12111 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12112 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12113
12114 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12115 redisplay_windows (FRAME_ROOT_WINDOW (f));
12116
12117 /* The X error handler may have deleted that frame. */
12118 if (!FRAME_LIVE_P (f))
12119 continue;
12120
12121 /* Any scroll bars which redisplay_windows should have
12122 nuked should now go away. */
12123 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12124 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12125
12126 /* If fonts changed, display again. */
12127 /* ??? rms: I suspect it is a mistake to jump all the way
12128 back to retry here. It should just retry this frame. */
12129 if (fonts_changed_p)
12130 goto retry;
12131
12132 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12133 {
12134 /* See if we have to hscroll. */
12135 if (!f->already_hscrolled_p)
12136 {
12137 f->already_hscrolled_p = 1;
12138 if (hscroll_windows (f->root_window))
12139 goto retry;
12140 }
12141
12142 /* Prevent various kinds of signals during display
12143 update. stdio is not robust about handling
12144 signals, which can cause an apparent I/O
12145 error. */
12146 if (interrupt_input)
12147 unrequest_sigio ();
12148 STOP_POLLING;
12149
12150 /* Update the display. */
12151 set_window_update_flags (XWINDOW (f->root_window), 1);
12152 pause |= update_frame (f, 0, 0);
12153 f->updated_p = 1;
12154 }
12155 }
12156 }
12157
12158 if (!EQ (old_frame, selected_frame)
12159 && FRAME_LIVE_P (XFRAME (old_frame)))
12160 /* We played a bit fast-and-loose above and allowed selected_frame
12161 and selected_window to be temporarily out-of-sync but let's make
12162 sure this stays contained. */
12163 select_frame_for_redisplay (old_frame);
12164 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12165
12166 if (!pause)
12167 {
12168 /* Do the mark_window_display_accurate after all windows have
12169 been redisplayed because this call resets flags in buffers
12170 which are needed for proper redisplay. */
12171 FOR_EACH_FRAME (tail, frame)
12172 {
12173 struct frame *f = XFRAME (frame);
12174 if (f->updated_p)
12175 {
12176 mark_window_display_accurate (f->root_window, 1);
12177 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12178 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12179 }
12180 }
12181 }
12182 }
12183 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12184 {
12185 Lisp_Object mini_window;
12186 struct frame *mini_frame;
12187
12188 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12189 /* Use list_of_error, not Qerror, so that
12190 we catch only errors and don't run the debugger. */
12191 internal_condition_case_1 (redisplay_window_1, selected_window,
12192 list_of_error,
12193 redisplay_window_error);
12194
12195 /* Compare desired and current matrices, perform output. */
12196
12197 update:
12198 /* If fonts changed, display again. */
12199 if (fonts_changed_p)
12200 goto retry;
12201
12202 /* Prevent various kinds of signals during display update.
12203 stdio is not robust about handling signals,
12204 which can cause an apparent I/O error. */
12205 if (interrupt_input)
12206 unrequest_sigio ();
12207 STOP_POLLING;
12208
12209 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12210 {
12211 if (hscroll_windows (selected_window))
12212 goto retry;
12213
12214 XWINDOW (selected_window)->must_be_updated_p = 1;
12215 pause = update_frame (sf, 0, 0);
12216 }
12217
12218 /* We may have called echo_area_display at the top of this
12219 function. If the echo area is on another frame, that may
12220 have put text on a frame other than the selected one, so the
12221 above call to update_frame would not have caught it. Catch
12222 it here. */
12223 mini_window = FRAME_MINIBUF_WINDOW (sf);
12224 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12225
12226 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12227 {
12228 XWINDOW (mini_window)->must_be_updated_p = 1;
12229 pause |= update_frame (mini_frame, 0, 0);
12230 if (!pause && hscroll_windows (mini_window))
12231 goto retry;
12232 }
12233 }
12234
12235 /* If display was paused because of pending input, make sure we do a
12236 thorough update the next time. */
12237 if (pause)
12238 {
12239 /* Prevent the optimization at the beginning of
12240 redisplay_internal that tries a single-line update of the
12241 line containing the cursor in the selected window. */
12242 CHARPOS (this_line_start_pos) = 0;
12243
12244 /* Let the overlay arrow be updated the next time. */
12245 update_overlay_arrows (0);
12246
12247 /* If we pause after scrolling, some rows in the current
12248 matrices of some windows are not valid. */
12249 if (!WINDOW_FULL_WIDTH_P (w)
12250 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12251 update_mode_lines = 1;
12252 }
12253 else
12254 {
12255 if (!consider_all_windows_p)
12256 {
12257 /* This has already been done above if
12258 consider_all_windows_p is set. */
12259 mark_window_display_accurate_1 (w, 1);
12260
12261 /* Say overlay arrows are up to date. */
12262 update_overlay_arrows (1);
12263
12264 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12265 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12266 }
12267
12268 update_mode_lines = 0;
12269 windows_or_buffers_changed = 0;
12270 cursor_type_changed = 0;
12271 }
12272
12273 /* Start SIGIO interrupts coming again. Having them off during the
12274 code above makes it less likely one will discard output, but not
12275 impossible, since there might be stuff in the system buffer here.
12276 But it is much hairier to try to do anything about that. */
12277 if (interrupt_input)
12278 request_sigio ();
12279 RESUME_POLLING;
12280
12281 /* If a frame has become visible which was not before, redisplay
12282 again, so that we display it. Expose events for such a frame
12283 (which it gets when becoming visible) don't call the parts of
12284 redisplay constructing glyphs, so simply exposing a frame won't
12285 display anything in this case. So, we have to display these
12286 frames here explicitly. */
12287 if (!pause)
12288 {
12289 Lisp_Object tail, frame;
12290 int new_count = 0;
12291
12292 FOR_EACH_FRAME (tail, frame)
12293 {
12294 int this_is_visible = 0;
12295
12296 if (XFRAME (frame)->visible)
12297 this_is_visible = 1;
12298 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12299 if (XFRAME (frame)->visible)
12300 this_is_visible = 1;
12301
12302 if (this_is_visible)
12303 new_count++;
12304 }
12305
12306 if (new_count != number_of_visible_frames)
12307 windows_or_buffers_changed++;
12308 }
12309
12310 /* Change frame size now if a change is pending. */
12311 do_pending_window_change (1);
12312
12313 /* If we just did a pending size change, or have additional
12314 visible frames, redisplay again. */
12315 if (windows_or_buffers_changed && !pause)
12316 goto retry;
12317
12318 /* Clear the face and image caches.
12319
12320 We used to do this only if consider_all_windows_p. But the cache
12321 needs to be cleared if a timer creates images in the current
12322 buffer (e.g. the test case in Bug#6230). */
12323
12324 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12325 {
12326 clear_face_cache (0);
12327 clear_face_cache_count = 0;
12328 }
12329
12330 #ifdef HAVE_WINDOW_SYSTEM
12331 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12332 {
12333 clear_image_caches (Qnil);
12334 clear_image_cache_count = 0;
12335 }
12336 #endif /* HAVE_WINDOW_SYSTEM */
12337
12338 end_of_redisplay:
12339 unbind_to (count, Qnil);
12340 RESUME_POLLING;
12341 }
12342
12343
12344 /* Redisplay, but leave alone any recent echo area message unless
12345 another message has been requested in its place.
12346
12347 This is useful in situations where you need to redisplay but no
12348 user action has occurred, making it inappropriate for the message
12349 area to be cleared. See tracking_off and
12350 wait_reading_process_output for examples of these situations.
12351
12352 FROM_WHERE is an integer saying from where this function was
12353 called. This is useful for debugging. */
12354
12355 void
12356 redisplay_preserve_echo_area (int from_where)
12357 {
12358 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12359
12360 if (!NILP (echo_area_buffer[1]))
12361 {
12362 /* We have a previously displayed message, but no current
12363 message. Redisplay the previous message. */
12364 display_last_displayed_message_p = 1;
12365 redisplay_internal (1);
12366 display_last_displayed_message_p = 0;
12367 }
12368 else
12369 redisplay_internal (1);
12370
12371 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12372 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12373 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12374 }
12375
12376
12377 /* Function registered with record_unwind_protect in
12378 redisplay_internal. Reset redisplaying_p to the value it had
12379 before redisplay_internal was called, and clear
12380 prevent_freeing_realized_faces_p. It also selects the previously
12381 selected frame, unless it has been deleted (by an X connection
12382 failure during redisplay, for example). */
12383
12384 static Lisp_Object
12385 unwind_redisplay (Lisp_Object val)
12386 {
12387 Lisp_Object old_redisplaying_p, old_frame;
12388
12389 old_redisplaying_p = XCAR (val);
12390 redisplaying_p = XFASTINT (old_redisplaying_p);
12391 old_frame = XCDR (val);
12392 if (! EQ (old_frame, selected_frame)
12393 && FRAME_LIVE_P (XFRAME (old_frame)))
12394 select_frame_for_redisplay (old_frame);
12395 return Qnil;
12396 }
12397
12398
12399 /* Mark the display of window W as accurate or inaccurate. If
12400 ACCURATE_P is non-zero mark display of W as accurate. If
12401 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12402 redisplay_internal is called. */
12403
12404 static void
12405 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12406 {
12407 if (BUFFERP (w->buffer))
12408 {
12409 struct buffer *b = XBUFFER (w->buffer);
12410
12411 w->last_modified
12412 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12413 w->last_overlay_modified
12414 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12415 w->last_had_star
12416 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12417
12418 if (accurate_p)
12419 {
12420 b->clip_changed = 0;
12421 b->prevent_redisplay_optimizations_p = 0;
12422
12423 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12424 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12425 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12426 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12427
12428 w->current_matrix->buffer = b;
12429 w->current_matrix->begv = BUF_BEGV (b);
12430 w->current_matrix->zv = BUF_ZV (b);
12431
12432 w->last_cursor = w->cursor;
12433 w->last_cursor_off_p = w->cursor_off_p;
12434
12435 if (w == XWINDOW (selected_window))
12436 w->last_point = make_number (BUF_PT (b));
12437 else
12438 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12439 }
12440 }
12441
12442 if (accurate_p)
12443 {
12444 w->window_end_valid = w->buffer;
12445 w->update_mode_line = Qnil;
12446 }
12447 }
12448
12449
12450 /* Mark the display of windows in the window tree rooted at WINDOW as
12451 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12452 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12453 be redisplayed the next time redisplay_internal is called. */
12454
12455 void
12456 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12457 {
12458 struct window *w;
12459
12460 for (; !NILP (window); window = w->next)
12461 {
12462 w = XWINDOW (window);
12463 mark_window_display_accurate_1 (w, accurate_p);
12464
12465 if (!NILP (w->vchild))
12466 mark_window_display_accurate (w->vchild, accurate_p);
12467 if (!NILP (w->hchild))
12468 mark_window_display_accurate (w->hchild, accurate_p);
12469 }
12470
12471 if (accurate_p)
12472 {
12473 update_overlay_arrows (1);
12474 }
12475 else
12476 {
12477 /* Force a thorough redisplay the next time by setting
12478 last_arrow_position and last_arrow_string to t, which is
12479 unequal to any useful value of Voverlay_arrow_... */
12480 update_overlay_arrows (-1);
12481 }
12482 }
12483
12484
12485 /* Return value in display table DP (Lisp_Char_Table *) for character
12486 C. Since a display table doesn't have any parent, we don't have to
12487 follow parent. Do not call this function directly but use the
12488 macro DISP_CHAR_VECTOR. */
12489
12490 Lisp_Object
12491 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12492 {
12493 Lisp_Object val;
12494
12495 if (ASCII_CHAR_P (c))
12496 {
12497 val = dp->ascii;
12498 if (SUB_CHAR_TABLE_P (val))
12499 val = XSUB_CHAR_TABLE (val)->contents[c];
12500 }
12501 else
12502 {
12503 Lisp_Object table;
12504
12505 XSETCHAR_TABLE (table, dp);
12506 val = char_table_ref (table, c);
12507 }
12508 if (NILP (val))
12509 val = dp->defalt;
12510 return val;
12511 }
12512
12513
12514 \f
12515 /***********************************************************************
12516 Window Redisplay
12517 ***********************************************************************/
12518
12519 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12520
12521 static void
12522 redisplay_windows (Lisp_Object window)
12523 {
12524 while (!NILP (window))
12525 {
12526 struct window *w = XWINDOW (window);
12527
12528 if (!NILP (w->hchild))
12529 redisplay_windows (w->hchild);
12530 else if (!NILP (w->vchild))
12531 redisplay_windows (w->vchild);
12532 else if (!NILP (w->buffer))
12533 {
12534 displayed_buffer = XBUFFER (w->buffer);
12535 /* Use list_of_error, not Qerror, so that
12536 we catch only errors and don't run the debugger. */
12537 internal_condition_case_1 (redisplay_window_0, window,
12538 list_of_error,
12539 redisplay_window_error);
12540 }
12541
12542 window = w->next;
12543 }
12544 }
12545
12546 static Lisp_Object
12547 redisplay_window_error (Lisp_Object ignore)
12548 {
12549 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12550 return Qnil;
12551 }
12552
12553 static Lisp_Object
12554 redisplay_window_0 (Lisp_Object window)
12555 {
12556 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12557 redisplay_window (window, 0);
12558 return Qnil;
12559 }
12560
12561 static Lisp_Object
12562 redisplay_window_1 (Lisp_Object window)
12563 {
12564 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12565 redisplay_window (window, 1);
12566 return Qnil;
12567 }
12568 \f
12569
12570 /* Increment GLYPH until it reaches END or CONDITION fails while
12571 adding (GLYPH)->pixel_width to X. */
12572
12573 #define SKIP_GLYPHS(glyph, end, x, condition) \
12574 do \
12575 { \
12576 (x) += (glyph)->pixel_width; \
12577 ++(glyph); \
12578 } \
12579 while ((glyph) < (end) && (condition))
12580
12581
12582 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12583 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12584 which positions recorded in ROW differ from current buffer
12585 positions.
12586
12587 Return 0 if cursor is not on this row, 1 otherwise. */
12588
12589 int
12590 set_cursor_from_row (struct window *w, struct glyph_row *row,
12591 struct glyph_matrix *matrix,
12592 EMACS_INT delta, EMACS_INT delta_bytes,
12593 int dy, int dvpos)
12594 {
12595 struct glyph *glyph = row->glyphs[TEXT_AREA];
12596 struct glyph *end = glyph + row->used[TEXT_AREA];
12597 struct glyph *cursor = NULL;
12598 /* The last known character position in row. */
12599 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12600 int x = row->x;
12601 EMACS_INT pt_old = PT - delta;
12602 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12603 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12604 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12605 /* A glyph beyond the edge of TEXT_AREA which we should never
12606 touch. */
12607 struct glyph *glyphs_end = end;
12608 /* Non-zero means we've found a match for cursor position, but that
12609 glyph has the avoid_cursor_p flag set. */
12610 int match_with_avoid_cursor = 0;
12611 /* Non-zero means we've seen at least one glyph that came from a
12612 display string. */
12613 int string_seen = 0;
12614 /* Largest and smalles buffer positions seen so far during scan of
12615 glyph row. */
12616 EMACS_INT bpos_max = pos_before;
12617 EMACS_INT bpos_min = pos_after;
12618 /* Last buffer position covered by an overlay string with an integer
12619 `cursor' property. */
12620 EMACS_INT bpos_covered = 0;
12621
12622 /* Skip over glyphs not having an object at the start and the end of
12623 the row. These are special glyphs like truncation marks on
12624 terminal frames. */
12625 if (row->displays_text_p)
12626 {
12627 if (!row->reversed_p)
12628 {
12629 while (glyph < end
12630 && INTEGERP (glyph->object)
12631 && glyph->charpos < 0)
12632 {
12633 x += glyph->pixel_width;
12634 ++glyph;
12635 }
12636 while (end > glyph
12637 && INTEGERP ((end - 1)->object)
12638 /* CHARPOS is zero for blanks and stretch glyphs
12639 inserted by extend_face_to_end_of_line. */
12640 && (end - 1)->charpos <= 0)
12641 --end;
12642 glyph_before = glyph - 1;
12643 glyph_after = end;
12644 }
12645 else
12646 {
12647 struct glyph *g;
12648
12649 /* If the glyph row is reversed, we need to process it from back
12650 to front, so swap the edge pointers. */
12651 glyphs_end = end = glyph - 1;
12652 glyph += row->used[TEXT_AREA] - 1;
12653
12654 while (glyph > end + 1
12655 && INTEGERP (glyph->object)
12656 && glyph->charpos < 0)
12657 {
12658 --glyph;
12659 x -= glyph->pixel_width;
12660 }
12661 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12662 --glyph;
12663 /* By default, in reversed rows we put the cursor on the
12664 rightmost (first in the reading order) glyph. */
12665 for (g = end + 1; g < glyph; g++)
12666 x += g->pixel_width;
12667 while (end < glyph
12668 && INTEGERP ((end + 1)->object)
12669 && (end + 1)->charpos <= 0)
12670 ++end;
12671 glyph_before = glyph + 1;
12672 glyph_after = end;
12673 }
12674 }
12675 else if (row->reversed_p)
12676 {
12677 /* In R2L rows that don't display text, put the cursor on the
12678 rightmost glyph. Case in point: an empty last line that is
12679 part of an R2L paragraph. */
12680 cursor = end - 1;
12681 /* Avoid placing the cursor on the last glyph of the row, where
12682 on terminal frames we hold the vertical border between
12683 adjacent windows. */
12684 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12685 && !WINDOW_RIGHTMOST_P (w)
12686 && cursor == row->glyphs[LAST_AREA] - 1)
12687 cursor--;
12688 x = -1; /* will be computed below, at label compute_x */
12689 }
12690
12691 /* Step 1: Try to find the glyph whose character position
12692 corresponds to point. If that's not possible, find 2 glyphs
12693 whose character positions are the closest to point, one before
12694 point, the other after it. */
12695 if (!row->reversed_p)
12696 while (/* not marched to end of glyph row */
12697 glyph < end
12698 /* glyph was not inserted by redisplay for internal purposes */
12699 && !INTEGERP (glyph->object))
12700 {
12701 if (BUFFERP (glyph->object))
12702 {
12703 EMACS_INT dpos = glyph->charpos - pt_old;
12704
12705 if (glyph->charpos > bpos_max)
12706 bpos_max = glyph->charpos;
12707 if (glyph->charpos < bpos_min)
12708 bpos_min = glyph->charpos;
12709 if (!glyph->avoid_cursor_p)
12710 {
12711 /* If we hit point, we've found the glyph on which to
12712 display the cursor. */
12713 if (dpos == 0)
12714 {
12715 match_with_avoid_cursor = 0;
12716 break;
12717 }
12718 /* See if we've found a better approximation to
12719 POS_BEFORE or to POS_AFTER. Note that we want the
12720 first (leftmost) glyph of all those that are the
12721 closest from below, and the last (rightmost) of all
12722 those from above. */
12723 if (0 > dpos && dpos > pos_before - pt_old)
12724 {
12725 pos_before = glyph->charpos;
12726 glyph_before = glyph;
12727 }
12728 else if (0 < dpos && dpos <= pos_after - pt_old)
12729 {
12730 pos_after = glyph->charpos;
12731 glyph_after = glyph;
12732 }
12733 }
12734 else if (dpos == 0)
12735 match_with_avoid_cursor = 1;
12736 }
12737 else if (STRINGP (glyph->object))
12738 {
12739 Lisp_Object chprop;
12740 EMACS_INT glyph_pos = glyph->charpos;
12741
12742 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12743 glyph->object);
12744 if (INTEGERP (chprop))
12745 {
12746 bpos_covered = bpos_max + XINT (chprop);
12747 /* If the `cursor' property covers buffer positions up
12748 to and including point, we should display cursor on
12749 this glyph. Note that overlays and text properties
12750 with string values stop bidi reordering, so every
12751 buffer position to the left of the string is always
12752 smaller than any position to the right of the
12753 string. Therefore, if a `cursor' property on one
12754 of the string's characters has an integer value, we
12755 will break out of the loop below _before_ we get to
12756 the position match above. IOW, integer values of
12757 the `cursor' property override the "exact match for
12758 point" strategy of positioning the cursor. */
12759 /* Implementation note: bpos_max == pt_old when, e.g.,
12760 we are in an empty line, where bpos_max is set to
12761 MATRIX_ROW_START_CHARPOS, see above. */
12762 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12763 {
12764 cursor = glyph;
12765 break;
12766 }
12767 }
12768
12769 string_seen = 1;
12770 }
12771 x += glyph->pixel_width;
12772 ++glyph;
12773 }
12774 else if (glyph > end) /* row is reversed */
12775 while (!INTEGERP (glyph->object))
12776 {
12777 if (BUFFERP (glyph->object))
12778 {
12779 EMACS_INT dpos = glyph->charpos - pt_old;
12780
12781 if (glyph->charpos > bpos_max)
12782 bpos_max = glyph->charpos;
12783 if (glyph->charpos < bpos_min)
12784 bpos_min = glyph->charpos;
12785 if (!glyph->avoid_cursor_p)
12786 {
12787 if (dpos == 0)
12788 {
12789 match_with_avoid_cursor = 0;
12790 break;
12791 }
12792 if (0 > dpos && dpos > pos_before - pt_old)
12793 {
12794 pos_before = glyph->charpos;
12795 glyph_before = glyph;
12796 }
12797 else if (0 < dpos && dpos <= pos_after - pt_old)
12798 {
12799 pos_after = glyph->charpos;
12800 glyph_after = glyph;
12801 }
12802 }
12803 else if (dpos == 0)
12804 match_with_avoid_cursor = 1;
12805 }
12806 else if (STRINGP (glyph->object))
12807 {
12808 Lisp_Object chprop;
12809 EMACS_INT glyph_pos = glyph->charpos;
12810
12811 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12812 glyph->object);
12813 if (INTEGERP (chprop))
12814 {
12815 bpos_covered = bpos_max + XINT (chprop);
12816 /* If the `cursor' property covers buffer positions up
12817 to and including point, we should display cursor on
12818 this glyph. */
12819 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12820 {
12821 cursor = glyph;
12822 break;
12823 }
12824 }
12825 string_seen = 1;
12826 }
12827 --glyph;
12828 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12829 {
12830 x--; /* can't use any pixel_width */
12831 break;
12832 }
12833 x -= glyph->pixel_width;
12834 }
12835
12836 /* Step 2: If we didn't find an exact match for point, we need to
12837 look for a proper place to put the cursor among glyphs between
12838 GLYPH_BEFORE and GLYPH_AFTER. */
12839 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12840 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12841 && bpos_covered < pt_old)
12842 {
12843 /* An empty line has a single glyph whose OBJECT is zero and
12844 whose CHARPOS is the position of a newline on that line.
12845 Note that on a TTY, there are more glyphs after that, which
12846 were produced by extend_face_to_end_of_line, but their
12847 CHARPOS is zero or negative. */
12848 int empty_line_p =
12849 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12850 && INTEGERP (glyph->object) && glyph->charpos > 0;
12851
12852 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12853 {
12854 EMACS_INT ellipsis_pos;
12855
12856 /* Scan back over the ellipsis glyphs. */
12857 if (!row->reversed_p)
12858 {
12859 ellipsis_pos = (glyph - 1)->charpos;
12860 while (glyph > row->glyphs[TEXT_AREA]
12861 && (glyph - 1)->charpos == ellipsis_pos)
12862 glyph--, x -= glyph->pixel_width;
12863 /* That loop always goes one position too far, including
12864 the glyph before the ellipsis. So scan forward over
12865 that one. */
12866 x += glyph->pixel_width;
12867 glyph++;
12868 }
12869 else /* row is reversed */
12870 {
12871 ellipsis_pos = (glyph + 1)->charpos;
12872 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12873 && (glyph + 1)->charpos == ellipsis_pos)
12874 glyph++, x += glyph->pixel_width;
12875 x -= glyph->pixel_width;
12876 glyph--;
12877 }
12878 }
12879 else if (match_with_avoid_cursor
12880 /* A truncated row may not include PT among its
12881 character positions. Setting the cursor inside the
12882 scroll margin will trigger recalculation of hscroll
12883 in hscroll_window_tree. */
12884 || (row->truncated_on_left_p && pt_old < bpos_min)
12885 || (row->truncated_on_right_p && pt_old > bpos_max)
12886 /* Zero-width characters produce no glyphs. */
12887 || (!string_seen
12888 && !empty_line_p
12889 && (row->reversed_p
12890 ? glyph_after > glyphs_end
12891 : glyph_after < glyphs_end)))
12892 {
12893 cursor = glyph_after;
12894 x = -1;
12895 }
12896 else if (string_seen)
12897 {
12898 int incr = row->reversed_p ? -1 : +1;
12899
12900 /* Need to find the glyph that came out of a string which is
12901 present at point. That glyph is somewhere between
12902 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12903 positioned between POS_BEFORE and POS_AFTER in the
12904 buffer. */
12905 struct glyph *stop = glyph_after;
12906 EMACS_INT pos = pos_before;
12907
12908 x = -1;
12909 for (glyph = glyph_before + incr;
12910 row->reversed_p ? glyph > stop : glyph < stop; )
12911 {
12912
12913 /* Any glyphs that come from the buffer are here because
12914 of bidi reordering. Skip them, and only pay
12915 attention to glyphs that came from some string. */
12916 if (STRINGP (glyph->object))
12917 {
12918 Lisp_Object str;
12919 EMACS_INT tem;
12920
12921 str = glyph->object;
12922 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12923 if (tem == 0 /* from overlay */
12924 || pos <= tem)
12925 {
12926 /* If the string from which this glyph came is
12927 found in the buffer at point, then we've
12928 found the glyph we've been looking for. If
12929 it comes from an overlay (tem == 0), and it
12930 has the `cursor' property on one of its
12931 glyphs, record that glyph as a candidate for
12932 displaying the cursor. (As in the
12933 unidirectional version, we will display the
12934 cursor on the last candidate we find.) */
12935 if (tem == 0 || tem == pt_old)
12936 {
12937 /* The glyphs from this string could have
12938 been reordered. Find the one with the
12939 smallest string position. Or there could
12940 be a character in the string with the
12941 `cursor' property, which means display
12942 cursor on that character's glyph. */
12943 EMACS_INT strpos = glyph->charpos;
12944
12945 if (tem)
12946 cursor = glyph;
12947 for ( ;
12948 (row->reversed_p ? glyph > stop : glyph < stop)
12949 && EQ (glyph->object, str);
12950 glyph += incr)
12951 {
12952 Lisp_Object cprop;
12953 EMACS_INT gpos = glyph->charpos;
12954
12955 cprop = Fget_char_property (make_number (gpos),
12956 Qcursor,
12957 glyph->object);
12958 if (!NILP (cprop))
12959 {
12960 cursor = glyph;
12961 break;
12962 }
12963 if (tem && glyph->charpos < strpos)
12964 {
12965 strpos = glyph->charpos;
12966 cursor = glyph;
12967 }
12968 }
12969
12970 if (tem == pt_old)
12971 goto compute_x;
12972 }
12973 if (tem)
12974 pos = tem + 1; /* don't find previous instances */
12975 }
12976 /* This string is not what we want; skip all of the
12977 glyphs that came from it. */
12978 while ((row->reversed_p ? glyph > stop : glyph < stop)
12979 && EQ (glyph->object, str))
12980 glyph += incr;
12981 }
12982 else
12983 glyph += incr;
12984 }
12985
12986 /* If we reached the end of the line, and END was from a string,
12987 the cursor is not on this line. */
12988 if (cursor == NULL
12989 && (row->reversed_p ? glyph <= end : glyph >= end)
12990 && STRINGP (end->object)
12991 && row->continued_p)
12992 return 0;
12993 }
12994 }
12995
12996 compute_x:
12997 if (cursor != NULL)
12998 glyph = cursor;
12999 if (x < 0)
13000 {
13001 struct glyph *g;
13002
13003 /* Need to compute x that corresponds to GLYPH. */
13004 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13005 {
13006 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13007 abort ();
13008 x += g->pixel_width;
13009 }
13010 }
13011
13012 /* ROW could be part of a continued line, which, under bidi
13013 reordering, might have other rows whose start and end charpos
13014 occlude point. Only set w->cursor if we found a better
13015 approximation to the cursor position than we have from previously
13016 examined candidate rows belonging to the same continued line. */
13017 if (/* we already have a candidate row */
13018 w->cursor.vpos >= 0
13019 /* that candidate is not the row we are processing */
13020 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13021 /* the row we are processing is part of a continued line */
13022 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
13023 /* Make sure cursor.vpos specifies a row whose start and end
13024 charpos occlude point. This is because some callers of this
13025 function leave cursor.vpos at the row where the cursor was
13026 displayed during the last redisplay cycle. */
13027 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13028 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13029 {
13030 struct glyph *g1 =
13031 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13032
13033 /* Don't consider glyphs that are outside TEXT_AREA. */
13034 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13035 return 0;
13036 /* Keep the candidate whose buffer position is the closest to
13037 point. */
13038 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13039 w->cursor.hpos >= 0
13040 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13041 && BUFFERP (g1->object)
13042 && (g1->charpos == pt_old /* an exact match always wins */
13043 || (BUFFERP (glyph->object)
13044 && eabs (g1->charpos - pt_old)
13045 < eabs (glyph->charpos - pt_old))))
13046 return 0;
13047 /* If this candidate gives an exact match, use that. */
13048 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13049 /* Otherwise, keep the candidate that comes from a row
13050 spanning less buffer positions. This may win when one or
13051 both candidate positions are on glyphs that came from
13052 display strings, for which we cannot compare buffer
13053 positions. */
13054 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13055 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13056 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13057 return 0;
13058 }
13059 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13060 w->cursor.x = x;
13061 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13062 w->cursor.y = row->y + dy;
13063
13064 if (w == XWINDOW (selected_window))
13065 {
13066 if (!row->continued_p
13067 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13068 && row->x == 0)
13069 {
13070 this_line_buffer = XBUFFER (w->buffer);
13071
13072 CHARPOS (this_line_start_pos)
13073 = MATRIX_ROW_START_CHARPOS (row) + delta;
13074 BYTEPOS (this_line_start_pos)
13075 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13076
13077 CHARPOS (this_line_end_pos)
13078 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13079 BYTEPOS (this_line_end_pos)
13080 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13081
13082 this_line_y = w->cursor.y;
13083 this_line_pixel_height = row->height;
13084 this_line_vpos = w->cursor.vpos;
13085 this_line_start_x = row->x;
13086 }
13087 else
13088 CHARPOS (this_line_start_pos) = 0;
13089 }
13090
13091 return 1;
13092 }
13093
13094
13095 /* Run window scroll functions, if any, for WINDOW with new window
13096 start STARTP. Sets the window start of WINDOW to that position.
13097
13098 We assume that the window's buffer is really current. */
13099
13100 static INLINE struct text_pos
13101 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13102 {
13103 struct window *w = XWINDOW (window);
13104 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13105
13106 if (current_buffer != XBUFFER (w->buffer))
13107 abort ();
13108
13109 if (!NILP (Vwindow_scroll_functions))
13110 {
13111 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13112 make_number (CHARPOS (startp)));
13113 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13114 /* In case the hook functions switch buffers. */
13115 if (current_buffer != XBUFFER (w->buffer))
13116 set_buffer_internal_1 (XBUFFER (w->buffer));
13117 }
13118
13119 return startp;
13120 }
13121
13122
13123 /* Make sure the line containing the cursor is fully visible.
13124 A value of 1 means there is nothing to be done.
13125 (Either the line is fully visible, or it cannot be made so,
13126 or we cannot tell.)
13127
13128 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13129 is higher than window.
13130
13131 A value of 0 means the caller should do scrolling
13132 as if point had gone off the screen. */
13133
13134 static int
13135 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13136 {
13137 struct glyph_matrix *matrix;
13138 struct glyph_row *row;
13139 int window_height;
13140
13141 if (!make_cursor_line_fully_visible_p)
13142 return 1;
13143
13144 /* It's not always possible to find the cursor, e.g, when a window
13145 is full of overlay strings. Don't do anything in that case. */
13146 if (w->cursor.vpos < 0)
13147 return 1;
13148
13149 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13150 row = MATRIX_ROW (matrix, w->cursor.vpos);
13151
13152 /* If the cursor row is not partially visible, there's nothing to do. */
13153 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13154 return 1;
13155
13156 /* If the row the cursor is in is taller than the window's height,
13157 it's not clear what to do, so do nothing. */
13158 window_height = window_box_height (w);
13159 if (row->height >= window_height)
13160 {
13161 if (!force_p || MINI_WINDOW_P (w)
13162 || w->vscroll || w->cursor.vpos == 0)
13163 return 1;
13164 }
13165 return 0;
13166 }
13167
13168
13169 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13170 non-zero means only WINDOW is redisplayed in redisplay_internal.
13171 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13172 in redisplay_window to bring a partially visible line into view in
13173 the case that only the cursor has moved.
13174
13175 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13176 last screen line's vertical height extends past the end of the screen.
13177
13178 Value is
13179
13180 1 if scrolling succeeded
13181
13182 0 if scrolling didn't find point.
13183
13184 -1 if new fonts have been loaded so that we must interrupt
13185 redisplay, adjust glyph matrices, and try again. */
13186
13187 enum
13188 {
13189 SCROLLING_SUCCESS,
13190 SCROLLING_FAILED,
13191 SCROLLING_NEED_LARGER_MATRICES
13192 };
13193
13194 static int
13195 try_scrolling (Lisp_Object window, int just_this_one_p,
13196 EMACS_INT scroll_conservatively, EMACS_INT scroll_step,
13197 int temp_scroll_step, int last_line_misfit)
13198 {
13199 struct window *w = XWINDOW (window);
13200 struct frame *f = XFRAME (w->frame);
13201 struct text_pos pos, startp;
13202 struct it it;
13203 int this_scroll_margin, scroll_max, rc, height;
13204 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13205 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13206 Lisp_Object aggressive;
13207 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13208
13209 #if GLYPH_DEBUG
13210 debug_method_add (w, "try_scrolling");
13211 #endif
13212
13213 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13214
13215 /* Compute scroll margin height in pixels. We scroll when point is
13216 within this distance from the top or bottom of the window. */
13217 if (scroll_margin > 0)
13218 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13219 * FRAME_LINE_HEIGHT (f);
13220 else
13221 this_scroll_margin = 0;
13222
13223 /* Force scroll_conservatively to have a reasonable value, to avoid
13224 overflow while computing how much to scroll. Note that the user
13225 can supply scroll-conservatively equal to `most-positive-fixnum',
13226 which can be larger than INT_MAX. */
13227 if (scroll_conservatively > scroll_limit)
13228 {
13229 scroll_conservatively = scroll_limit;
13230 scroll_max = INT_MAX;
13231 }
13232 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13233 /* Compute how much we should try to scroll maximally to bring
13234 point into view. */
13235 scroll_max = (max (scroll_step,
13236 max (scroll_conservatively, temp_scroll_step))
13237 * FRAME_LINE_HEIGHT (f));
13238 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13239 || NUMBERP (current_buffer->scroll_up_aggressively))
13240 /* We're trying to scroll because of aggressive scrolling but no
13241 scroll_step is set. Choose an arbitrary one. */
13242 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13243 else
13244 scroll_max = 0;
13245
13246 too_near_end:
13247
13248 /* Decide whether to scroll down. */
13249 if (PT > CHARPOS (startp))
13250 {
13251 int scroll_margin_y;
13252
13253 /* Compute the pixel ypos of the scroll margin, then move it to
13254 either that ypos or PT, whichever comes first. */
13255 start_display (&it, w, startp);
13256 scroll_margin_y = it.last_visible_y - this_scroll_margin
13257 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13258 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13259 (MOVE_TO_POS | MOVE_TO_Y));
13260
13261 if (PT > CHARPOS (it.current.pos))
13262 {
13263 int y0 = line_bottom_y (&it);
13264 /* Compute how many pixels below window bottom to stop searching
13265 for PT. This avoids costly search for PT that is far away if
13266 the user limited scrolling by a small number of lines, but
13267 always finds PT if scroll_conservatively is set to a large
13268 number, such as most-positive-fixnum. */
13269 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13270 int y_to_move =
13271 slack >= INT_MAX - it.last_visible_y
13272 ? INT_MAX
13273 : it.last_visible_y + slack;
13274
13275 /* Compute the distance from the scroll margin to PT or to
13276 the scroll limit, whichever comes first. This should
13277 include the height of the cursor line, to make that line
13278 fully visible. */
13279 move_it_to (&it, PT, -1, y_to_move,
13280 -1, MOVE_TO_POS | MOVE_TO_Y);
13281 dy = line_bottom_y (&it) - y0;
13282
13283 if (dy > scroll_max)
13284 return SCROLLING_FAILED;
13285
13286 scroll_down_p = 1;
13287 }
13288 }
13289
13290 if (scroll_down_p)
13291 {
13292 /* Point is in or below the bottom scroll margin, so move the
13293 window start down. If scrolling conservatively, move it just
13294 enough down to make point visible. If scroll_step is set,
13295 move it down by scroll_step. */
13296 if (scroll_conservatively)
13297 amount_to_scroll
13298 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13299 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13300 else if (scroll_step || temp_scroll_step)
13301 amount_to_scroll = scroll_max;
13302 else
13303 {
13304 aggressive = current_buffer->scroll_up_aggressively;
13305 height = WINDOW_BOX_TEXT_HEIGHT (w);
13306 if (NUMBERP (aggressive))
13307 {
13308 double float_amount = XFLOATINT (aggressive) * height;
13309 amount_to_scroll = float_amount;
13310 if (amount_to_scroll == 0 && float_amount > 0)
13311 amount_to_scroll = 1;
13312 }
13313 }
13314
13315 if (amount_to_scroll <= 0)
13316 return SCROLLING_FAILED;
13317
13318 start_display (&it, w, startp);
13319 if (scroll_max < INT_MAX)
13320 move_it_vertically (&it, amount_to_scroll);
13321 else
13322 {
13323 /* Extra precision for users who set scroll-conservatively
13324 to most-positive-fixnum: make sure the amount we scroll
13325 the window start is never less than amount_to_scroll,
13326 which was computed as distance from window bottom to
13327 point. This matters when lines at window top and lines
13328 below window bottom have different height. */
13329 struct it it1 = it;
13330 /* We use a temporary it1 because line_bottom_y can modify
13331 its argument, if it moves one line down; see there. */
13332 int start_y = line_bottom_y (&it1);
13333
13334 do {
13335 move_it_by_lines (&it, 1, 1);
13336 it1 = it;
13337 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13338 }
13339
13340 /* If STARTP is unchanged, move it down another screen line. */
13341 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13342 move_it_by_lines (&it, 1, 1);
13343 startp = it.current.pos;
13344 }
13345 else
13346 {
13347 struct text_pos scroll_margin_pos = startp;
13348
13349 /* See if point is inside the scroll margin at the top of the
13350 window. */
13351 if (this_scroll_margin)
13352 {
13353 start_display (&it, w, startp);
13354 move_it_vertically (&it, this_scroll_margin);
13355 scroll_margin_pos = it.current.pos;
13356 }
13357
13358 if (PT < CHARPOS (scroll_margin_pos))
13359 {
13360 /* Point is in the scroll margin at the top of the window or
13361 above what is displayed in the window. */
13362 int y0;
13363
13364 /* Compute the vertical distance from PT to the scroll
13365 margin position. Give up if distance is greater than
13366 scroll_max. */
13367 SET_TEXT_POS (pos, PT, PT_BYTE);
13368 start_display (&it, w, pos);
13369 y0 = it.current_y;
13370 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13371 it.last_visible_y, -1,
13372 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13373 dy = it.current_y - y0;
13374 if (dy > scroll_max)
13375 return SCROLLING_FAILED;
13376
13377 /* Compute new window start. */
13378 start_display (&it, w, startp);
13379
13380 if (scroll_conservatively)
13381 amount_to_scroll
13382 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13383 else if (scroll_step || temp_scroll_step)
13384 amount_to_scroll = scroll_max;
13385 else
13386 {
13387 aggressive = current_buffer->scroll_down_aggressively;
13388 height = WINDOW_BOX_TEXT_HEIGHT (w);
13389 if (NUMBERP (aggressive))
13390 {
13391 double float_amount = XFLOATINT (aggressive) * height;
13392 amount_to_scroll = float_amount;
13393 if (amount_to_scroll == 0 && float_amount > 0)
13394 amount_to_scroll = 1;
13395 }
13396 }
13397
13398 if (amount_to_scroll <= 0)
13399 return SCROLLING_FAILED;
13400
13401 move_it_vertically_backward (&it, amount_to_scroll);
13402 startp = it.current.pos;
13403 }
13404 }
13405
13406 /* Run window scroll functions. */
13407 startp = run_window_scroll_functions (window, startp);
13408
13409 /* Display the window. Give up if new fonts are loaded, or if point
13410 doesn't appear. */
13411 if (!try_window (window, startp, 0))
13412 rc = SCROLLING_NEED_LARGER_MATRICES;
13413 else if (w->cursor.vpos < 0)
13414 {
13415 clear_glyph_matrix (w->desired_matrix);
13416 rc = SCROLLING_FAILED;
13417 }
13418 else
13419 {
13420 /* Maybe forget recorded base line for line number display. */
13421 if (!just_this_one_p
13422 || current_buffer->clip_changed
13423 || BEG_UNCHANGED < CHARPOS (startp))
13424 w->base_line_number = Qnil;
13425
13426 /* If cursor ends up on a partially visible line,
13427 treat that as being off the bottom of the screen. */
13428 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
13429 /* It's possible that the cursor is on the first line of the
13430 buffer, which is partially obscured due to a vscroll
13431 (Bug#7537). In that case, avoid looping forever . */
13432 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
13433 {
13434 clear_glyph_matrix (w->desired_matrix);
13435 ++extra_scroll_margin_lines;
13436 goto too_near_end;
13437 }
13438 rc = SCROLLING_SUCCESS;
13439 }
13440
13441 return rc;
13442 }
13443
13444
13445 /* Compute a suitable window start for window W if display of W starts
13446 on a continuation line. Value is non-zero if a new window start
13447 was computed.
13448
13449 The new window start will be computed, based on W's width, starting
13450 from the start of the continued line. It is the start of the
13451 screen line with the minimum distance from the old start W->start. */
13452
13453 static int
13454 compute_window_start_on_continuation_line (struct window *w)
13455 {
13456 struct text_pos pos, start_pos;
13457 int window_start_changed_p = 0;
13458
13459 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13460
13461 /* If window start is on a continuation line... Window start may be
13462 < BEGV in case there's invisible text at the start of the
13463 buffer (M-x rmail, for example). */
13464 if (CHARPOS (start_pos) > BEGV
13465 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13466 {
13467 struct it it;
13468 struct glyph_row *row;
13469
13470 /* Handle the case that the window start is out of range. */
13471 if (CHARPOS (start_pos) < BEGV)
13472 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13473 else if (CHARPOS (start_pos) > ZV)
13474 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13475
13476 /* Find the start of the continued line. This should be fast
13477 because scan_buffer is fast (newline cache). */
13478 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13479 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13480 row, DEFAULT_FACE_ID);
13481 reseat_at_previous_visible_line_start (&it);
13482
13483 /* If the line start is "too far" away from the window start,
13484 say it takes too much time to compute a new window start. */
13485 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13486 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13487 {
13488 int min_distance, distance;
13489
13490 /* Move forward by display lines to find the new window
13491 start. If window width was enlarged, the new start can
13492 be expected to be > the old start. If window width was
13493 decreased, the new window start will be < the old start.
13494 So, we're looking for the display line start with the
13495 minimum distance from the old window start. */
13496 pos = it.current.pos;
13497 min_distance = INFINITY;
13498 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13499 distance < min_distance)
13500 {
13501 min_distance = distance;
13502 pos = it.current.pos;
13503 move_it_by_lines (&it, 1, 0);
13504 }
13505
13506 /* Set the window start there. */
13507 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13508 window_start_changed_p = 1;
13509 }
13510 }
13511
13512 return window_start_changed_p;
13513 }
13514
13515
13516 /* Try cursor movement in case text has not changed in window WINDOW,
13517 with window start STARTP. Value is
13518
13519 CURSOR_MOVEMENT_SUCCESS if successful
13520
13521 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13522
13523 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13524 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13525 we want to scroll as if scroll-step were set to 1. See the code.
13526
13527 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13528 which case we have to abort this redisplay, and adjust matrices
13529 first. */
13530
13531 enum
13532 {
13533 CURSOR_MOVEMENT_SUCCESS,
13534 CURSOR_MOVEMENT_CANNOT_BE_USED,
13535 CURSOR_MOVEMENT_MUST_SCROLL,
13536 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13537 };
13538
13539 static int
13540 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13541 {
13542 struct window *w = XWINDOW (window);
13543 struct frame *f = XFRAME (w->frame);
13544 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13545
13546 #if GLYPH_DEBUG
13547 if (inhibit_try_cursor_movement)
13548 return rc;
13549 #endif
13550
13551 /* Handle case where text has not changed, only point, and it has
13552 not moved off the frame. */
13553 if (/* Point may be in this window. */
13554 PT >= CHARPOS (startp)
13555 /* Selective display hasn't changed. */
13556 && !current_buffer->clip_changed
13557 /* Function force-mode-line-update is used to force a thorough
13558 redisplay. It sets either windows_or_buffers_changed or
13559 update_mode_lines. So don't take a shortcut here for these
13560 cases. */
13561 && !update_mode_lines
13562 && !windows_or_buffers_changed
13563 && !cursor_type_changed
13564 /* Can't use this case if highlighting a region. When a
13565 region exists, cursor movement has to do more than just
13566 set the cursor. */
13567 && !(!NILP (Vtransient_mark_mode)
13568 && !NILP (current_buffer->mark_active))
13569 && NILP (w->region_showing)
13570 && NILP (Vshow_trailing_whitespace)
13571 /* Right after splitting windows, last_point may be nil. */
13572 && INTEGERP (w->last_point)
13573 /* This code is not used for mini-buffer for the sake of the case
13574 of redisplaying to replace an echo area message; since in
13575 that case the mini-buffer contents per se are usually
13576 unchanged. This code is of no real use in the mini-buffer
13577 since the handling of this_line_start_pos, etc., in redisplay
13578 handles the same cases. */
13579 && !EQ (window, minibuf_window)
13580 /* When splitting windows or for new windows, it happens that
13581 redisplay is called with a nil window_end_vpos or one being
13582 larger than the window. This should really be fixed in
13583 window.c. I don't have this on my list, now, so we do
13584 approximately the same as the old redisplay code. --gerd. */
13585 && INTEGERP (w->window_end_vpos)
13586 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13587 && (FRAME_WINDOW_P (f)
13588 || !overlay_arrow_in_current_buffer_p ()))
13589 {
13590 int this_scroll_margin, top_scroll_margin;
13591 struct glyph_row *row = NULL;
13592
13593 #if GLYPH_DEBUG
13594 debug_method_add (w, "cursor movement");
13595 #endif
13596
13597 /* Scroll if point within this distance from the top or bottom
13598 of the window. This is a pixel value. */
13599 if (scroll_margin > 0)
13600 {
13601 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13602 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13603 }
13604 else
13605 this_scroll_margin = 0;
13606
13607 top_scroll_margin = this_scroll_margin;
13608 if (WINDOW_WANTS_HEADER_LINE_P (w))
13609 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13610
13611 /* Start with the row the cursor was displayed during the last
13612 not paused redisplay. Give up if that row is not valid. */
13613 if (w->last_cursor.vpos < 0
13614 || w->last_cursor.vpos >= w->current_matrix->nrows)
13615 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13616 else
13617 {
13618 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13619 if (row->mode_line_p)
13620 ++row;
13621 if (!row->enabled_p)
13622 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13623 }
13624
13625 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13626 {
13627 int scroll_p = 0, must_scroll = 0;
13628 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13629
13630 if (PT > XFASTINT (w->last_point))
13631 {
13632 /* Point has moved forward. */
13633 while (MATRIX_ROW_END_CHARPOS (row) < PT
13634 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13635 {
13636 xassert (row->enabled_p);
13637 ++row;
13638 }
13639
13640 /* If the end position of a row equals the start
13641 position of the next row, and PT is at that position,
13642 we would rather display cursor in the next line. */
13643 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13644 && MATRIX_ROW_END_CHARPOS (row) == PT
13645 && row < w->current_matrix->rows
13646 + w->current_matrix->nrows - 1
13647 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13648 && !cursor_row_p (w, row))
13649 ++row;
13650
13651 /* If within the scroll margin, scroll. Note that
13652 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13653 the next line would be drawn, and that
13654 this_scroll_margin can be zero. */
13655 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13656 || PT > MATRIX_ROW_END_CHARPOS (row)
13657 /* Line is completely visible last line in window
13658 and PT is to be set in the next line. */
13659 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13660 && PT == MATRIX_ROW_END_CHARPOS (row)
13661 && !row->ends_at_zv_p
13662 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13663 scroll_p = 1;
13664 }
13665 else if (PT < XFASTINT (w->last_point))
13666 {
13667 /* Cursor has to be moved backward. Note that PT >=
13668 CHARPOS (startp) because of the outer if-statement. */
13669 while (!row->mode_line_p
13670 && (MATRIX_ROW_START_CHARPOS (row) > PT
13671 || (MATRIX_ROW_START_CHARPOS (row) == PT
13672 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13673 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13674 row > w->current_matrix->rows
13675 && (row-1)->ends_in_newline_from_string_p))))
13676 && (row->y > top_scroll_margin
13677 || CHARPOS (startp) == BEGV))
13678 {
13679 xassert (row->enabled_p);
13680 --row;
13681 }
13682
13683 /* Consider the following case: Window starts at BEGV,
13684 there is invisible, intangible text at BEGV, so that
13685 display starts at some point START > BEGV. It can
13686 happen that we are called with PT somewhere between
13687 BEGV and START. Try to handle that case. */
13688 if (row < w->current_matrix->rows
13689 || row->mode_line_p)
13690 {
13691 row = w->current_matrix->rows;
13692 if (row->mode_line_p)
13693 ++row;
13694 }
13695
13696 /* Due to newlines in overlay strings, we may have to
13697 skip forward over overlay strings. */
13698 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13699 && MATRIX_ROW_END_CHARPOS (row) == PT
13700 && !cursor_row_p (w, row))
13701 ++row;
13702
13703 /* If within the scroll margin, scroll. */
13704 if (row->y < top_scroll_margin
13705 && CHARPOS (startp) != BEGV)
13706 scroll_p = 1;
13707 }
13708 else
13709 {
13710 /* Cursor did not move. So don't scroll even if cursor line
13711 is partially visible, as it was so before. */
13712 rc = CURSOR_MOVEMENT_SUCCESS;
13713 }
13714
13715 if (PT < MATRIX_ROW_START_CHARPOS (row)
13716 || PT > MATRIX_ROW_END_CHARPOS (row))
13717 {
13718 /* if PT is not in the glyph row, give up. */
13719 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13720 must_scroll = 1;
13721 }
13722 else if (rc != CURSOR_MOVEMENT_SUCCESS
13723 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13724 {
13725 /* If rows are bidi-reordered and point moved, back up
13726 until we find a row that does not belong to a
13727 continuation line. This is because we must consider
13728 all rows of a continued line as candidates for the
13729 new cursor positioning, since row start and end
13730 positions change non-linearly with vertical position
13731 in such rows. */
13732 /* FIXME: Revisit this when glyph ``spilling'' in
13733 continuation lines' rows is implemented for
13734 bidi-reordered rows. */
13735 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13736 {
13737 xassert (row->enabled_p);
13738 --row;
13739 /* If we hit the beginning of the displayed portion
13740 without finding the first row of a continued
13741 line, give up. */
13742 if (row <= w->current_matrix->rows)
13743 {
13744 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13745 break;
13746 }
13747
13748 }
13749 }
13750 if (must_scroll)
13751 ;
13752 else if (rc != CURSOR_MOVEMENT_SUCCESS
13753 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13754 && make_cursor_line_fully_visible_p)
13755 {
13756 if (PT == MATRIX_ROW_END_CHARPOS (row)
13757 && !row->ends_at_zv_p
13758 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13759 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13760 else if (row->height > window_box_height (w))
13761 {
13762 /* If we end up in a partially visible line, let's
13763 make it fully visible, except when it's taller
13764 than the window, in which case we can't do much
13765 about it. */
13766 *scroll_step = 1;
13767 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13768 }
13769 else
13770 {
13771 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13772 if (!cursor_row_fully_visible_p (w, 0, 1))
13773 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13774 else
13775 rc = CURSOR_MOVEMENT_SUCCESS;
13776 }
13777 }
13778 else if (scroll_p)
13779 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13780 else if (rc != CURSOR_MOVEMENT_SUCCESS
13781 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13782 {
13783 /* With bidi-reordered rows, there could be more than
13784 one candidate row whose start and end positions
13785 occlude point. We need to let set_cursor_from_row
13786 find the best candidate. */
13787 /* FIXME: Revisit this when glyph ``spilling'' in
13788 continuation lines' rows is implemented for
13789 bidi-reordered rows. */
13790 int rv = 0;
13791
13792 do
13793 {
13794 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13795 && PT <= MATRIX_ROW_END_CHARPOS (row)
13796 && cursor_row_p (w, row))
13797 rv |= set_cursor_from_row (w, row, w->current_matrix,
13798 0, 0, 0, 0);
13799 /* As soon as we've found the first suitable row
13800 whose ends_at_zv_p flag is set, we are done. */
13801 if (rv
13802 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13803 {
13804 rc = CURSOR_MOVEMENT_SUCCESS;
13805 break;
13806 }
13807 ++row;
13808 }
13809 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
13810 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13811 || (MATRIX_ROW_START_CHARPOS (row) == PT
13812 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
13813 /* If we didn't find any candidate rows, or exited the
13814 loop before all the candidates were examined, signal
13815 to the caller that this method failed. */
13816 if (rc != CURSOR_MOVEMENT_SUCCESS
13817 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
13818 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13819 else if (rv)
13820 rc = CURSOR_MOVEMENT_SUCCESS;
13821 }
13822 else
13823 {
13824 do
13825 {
13826 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13827 {
13828 rc = CURSOR_MOVEMENT_SUCCESS;
13829 break;
13830 }
13831 ++row;
13832 }
13833 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13834 && MATRIX_ROW_START_CHARPOS (row) == PT
13835 && cursor_row_p (w, row));
13836 }
13837 }
13838 }
13839
13840 return rc;
13841 }
13842
13843 void
13844 set_vertical_scroll_bar (struct window *w)
13845 {
13846 EMACS_INT start, end, whole;
13847
13848 /* Calculate the start and end positions for the current window.
13849 At some point, it would be nice to choose between scrollbars
13850 which reflect the whole buffer size, with special markers
13851 indicating narrowing, and scrollbars which reflect only the
13852 visible region.
13853
13854 Note that mini-buffers sometimes aren't displaying any text. */
13855 if (!MINI_WINDOW_P (w)
13856 || (w == XWINDOW (minibuf_window)
13857 && NILP (echo_area_buffer[0])))
13858 {
13859 struct buffer *buf = XBUFFER (w->buffer);
13860 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13861 start = marker_position (w->start) - BUF_BEGV (buf);
13862 /* I don't think this is guaranteed to be right. For the
13863 moment, we'll pretend it is. */
13864 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13865
13866 if (end < start)
13867 end = start;
13868 if (whole < (end - start))
13869 whole = end - start;
13870 }
13871 else
13872 start = end = whole = 0;
13873
13874 /* Indicate what this scroll bar ought to be displaying now. */
13875 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13876 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13877 (w, end - start, whole, start);
13878 }
13879
13880
13881 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13882 selected_window is redisplayed.
13883
13884 We can return without actually redisplaying the window if
13885 fonts_changed_p is nonzero. In that case, redisplay_internal will
13886 retry. */
13887
13888 static void
13889 redisplay_window (Lisp_Object window, int just_this_one_p)
13890 {
13891 struct window *w = XWINDOW (window);
13892 struct frame *f = XFRAME (w->frame);
13893 struct buffer *buffer = XBUFFER (w->buffer);
13894 struct buffer *old = current_buffer;
13895 struct text_pos lpoint, opoint, startp;
13896 int update_mode_line;
13897 int tem;
13898 struct it it;
13899 /* Record it now because it's overwritten. */
13900 int current_matrix_up_to_date_p = 0;
13901 int used_current_matrix_p = 0;
13902 /* This is less strict than current_matrix_up_to_date_p.
13903 It indictes that the buffer contents and narrowing are unchanged. */
13904 int buffer_unchanged_p = 0;
13905 int temp_scroll_step = 0;
13906 int count = SPECPDL_INDEX ();
13907 int rc;
13908 int centering_position = -1;
13909 int last_line_misfit = 0;
13910 EMACS_INT beg_unchanged, end_unchanged;
13911
13912 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13913 opoint = lpoint;
13914
13915 /* W must be a leaf window here. */
13916 xassert (!NILP (w->buffer));
13917 #if GLYPH_DEBUG
13918 *w->desired_matrix->method = 0;
13919 #endif
13920
13921 restart:
13922 reconsider_clip_changes (w, buffer);
13923
13924 /* Has the mode line to be updated? */
13925 update_mode_line = (!NILP (w->update_mode_line)
13926 || update_mode_lines
13927 || buffer->clip_changed
13928 || buffer->prevent_redisplay_optimizations_p);
13929
13930 if (MINI_WINDOW_P (w))
13931 {
13932 if (w == XWINDOW (echo_area_window)
13933 && !NILP (echo_area_buffer[0]))
13934 {
13935 if (update_mode_line)
13936 /* We may have to update a tty frame's menu bar or a
13937 tool-bar. Example `M-x C-h C-h C-g'. */
13938 goto finish_menu_bars;
13939 else
13940 /* We've already displayed the echo area glyphs in this window. */
13941 goto finish_scroll_bars;
13942 }
13943 else if ((w != XWINDOW (minibuf_window)
13944 || minibuf_level == 0)
13945 /* When buffer is nonempty, redisplay window normally. */
13946 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13947 /* Quail displays non-mini buffers in minibuffer window.
13948 In that case, redisplay the window normally. */
13949 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13950 {
13951 /* W is a mini-buffer window, but it's not active, so clear
13952 it. */
13953 int yb = window_text_bottom_y (w);
13954 struct glyph_row *row;
13955 int y;
13956
13957 for (y = 0, row = w->desired_matrix->rows;
13958 y < yb;
13959 y += row->height, ++row)
13960 blank_row (w, row, y);
13961 goto finish_scroll_bars;
13962 }
13963
13964 clear_glyph_matrix (w->desired_matrix);
13965 }
13966
13967 /* Otherwise set up data on this window; select its buffer and point
13968 value. */
13969 /* Really select the buffer, for the sake of buffer-local
13970 variables. */
13971 set_buffer_internal_1 (XBUFFER (w->buffer));
13972
13973 current_matrix_up_to_date_p
13974 = (!NILP (w->window_end_valid)
13975 && !current_buffer->clip_changed
13976 && !current_buffer->prevent_redisplay_optimizations_p
13977 && XFASTINT (w->last_modified) >= MODIFF
13978 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13979
13980 /* Run the window-bottom-change-functions
13981 if it is possible that the text on the screen has changed
13982 (either due to modification of the text, or any other reason). */
13983 if (!current_matrix_up_to_date_p
13984 && !NILP (Vwindow_text_change_functions))
13985 {
13986 safe_run_hooks (Qwindow_text_change_functions);
13987 goto restart;
13988 }
13989
13990 beg_unchanged = BEG_UNCHANGED;
13991 end_unchanged = END_UNCHANGED;
13992
13993 SET_TEXT_POS (opoint, PT, PT_BYTE);
13994
13995 specbind (Qinhibit_point_motion_hooks, Qt);
13996
13997 buffer_unchanged_p
13998 = (!NILP (w->window_end_valid)
13999 && !current_buffer->clip_changed
14000 && XFASTINT (w->last_modified) >= MODIFF
14001 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14002
14003 /* When windows_or_buffers_changed is non-zero, we can't rely on
14004 the window end being valid, so set it to nil there. */
14005 if (windows_or_buffers_changed)
14006 {
14007 /* If window starts on a continuation line, maybe adjust the
14008 window start in case the window's width changed. */
14009 if (XMARKER (w->start)->buffer == current_buffer)
14010 compute_window_start_on_continuation_line (w);
14011
14012 w->window_end_valid = Qnil;
14013 }
14014
14015 /* Some sanity checks. */
14016 CHECK_WINDOW_END (w);
14017 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14018 abort ();
14019 if (BYTEPOS (opoint) < CHARPOS (opoint))
14020 abort ();
14021
14022 /* If %c is in mode line, update it if needed. */
14023 if (!NILP (w->column_number_displayed)
14024 /* This alternative quickly identifies a common case
14025 where no change is needed. */
14026 && !(PT == XFASTINT (w->last_point)
14027 && XFASTINT (w->last_modified) >= MODIFF
14028 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14029 && (XFASTINT (w->column_number_displayed)
14030 != (int) current_column ())) /* iftc */
14031 update_mode_line = 1;
14032
14033 /* Count number of windows showing the selected buffer. An indirect
14034 buffer counts as its base buffer. */
14035 if (!just_this_one_p)
14036 {
14037 struct buffer *current_base, *window_base;
14038 current_base = current_buffer;
14039 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14040 if (current_base->base_buffer)
14041 current_base = current_base->base_buffer;
14042 if (window_base->base_buffer)
14043 window_base = window_base->base_buffer;
14044 if (current_base == window_base)
14045 buffer_shared++;
14046 }
14047
14048 /* Point refers normally to the selected window. For any other
14049 window, set up appropriate value. */
14050 if (!EQ (window, selected_window))
14051 {
14052 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14053 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14054 if (new_pt < BEGV)
14055 {
14056 new_pt = BEGV;
14057 new_pt_byte = BEGV_BYTE;
14058 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14059 }
14060 else if (new_pt > (ZV - 1))
14061 {
14062 new_pt = ZV;
14063 new_pt_byte = ZV_BYTE;
14064 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14065 }
14066
14067 /* We don't use SET_PT so that the point-motion hooks don't run. */
14068 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14069 }
14070
14071 /* If any of the character widths specified in the display table
14072 have changed, invalidate the width run cache. It's true that
14073 this may be a bit late to catch such changes, but the rest of
14074 redisplay goes (non-fatally) haywire when the display table is
14075 changed, so why should we worry about doing any better? */
14076 if (current_buffer->width_run_cache)
14077 {
14078 struct Lisp_Char_Table *disptab = buffer_display_table ();
14079
14080 if (! disptab_matches_widthtab (disptab,
14081 XVECTOR (current_buffer->width_table)))
14082 {
14083 invalidate_region_cache (current_buffer,
14084 current_buffer->width_run_cache,
14085 BEG, Z);
14086 recompute_width_table (current_buffer, disptab);
14087 }
14088 }
14089
14090 /* If window-start is screwed up, choose a new one. */
14091 if (XMARKER (w->start)->buffer != current_buffer)
14092 goto recenter;
14093
14094 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14095
14096 /* If someone specified a new starting point but did not insist,
14097 check whether it can be used. */
14098 if (!NILP (w->optional_new_start)
14099 && CHARPOS (startp) >= BEGV
14100 && CHARPOS (startp) <= ZV)
14101 {
14102 w->optional_new_start = Qnil;
14103 start_display (&it, w, startp);
14104 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14105 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14106 if (IT_CHARPOS (it) == PT)
14107 w->force_start = Qt;
14108 /* IT may overshoot PT if text at PT is invisible. */
14109 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14110 w->force_start = Qt;
14111 }
14112
14113 force_start:
14114
14115 /* Handle case where place to start displaying has been specified,
14116 unless the specified location is outside the accessible range. */
14117 if (!NILP (w->force_start)
14118 || w->frozen_window_start_p)
14119 {
14120 /* We set this later on if we have to adjust point. */
14121 int new_vpos = -1;
14122
14123 w->force_start = Qnil;
14124 w->vscroll = 0;
14125 w->window_end_valid = Qnil;
14126
14127 /* Forget any recorded base line for line number display. */
14128 if (!buffer_unchanged_p)
14129 w->base_line_number = Qnil;
14130
14131 /* Redisplay the mode line. Select the buffer properly for that.
14132 Also, run the hook window-scroll-functions
14133 because we have scrolled. */
14134 /* Note, we do this after clearing force_start because
14135 if there's an error, it is better to forget about force_start
14136 than to get into an infinite loop calling the hook functions
14137 and having them get more errors. */
14138 if (!update_mode_line
14139 || ! NILP (Vwindow_scroll_functions))
14140 {
14141 update_mode_line = 1;
14142 w->update_mode_line = Qt;
14143 startp = run_window_scroll_functions (window, startp);
14144 }
14145
14146 w->last_modified = make_number (0);
14147 w->last_overlay_modified = make_number (0);
14148 if (CHARPOS (startp) < BEGV)
14149 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14150 else if (CHARPOS (startp) > ZV)
14151 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14152
14153 /* Redisplay, then check if cursor has been set during the
14154 redisplay. Give up if new fonts were loaded. */
14155 /* We used to issue a CHECK_MARGINS argument to try_window here,
14156 but this causes scrolling to fail when point begins inside
14157 the scroll margin (bug#148) -- cyd */
14158 if (!try_window (window, startp, 0))
14159 {
14160 w->force_start = Qt;
14161 clear_glyph_matrix (w->desired_matrix);
14162 goto need_larger_matrices;
14163 }
14164
14165 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14166 {
14167 /* If point does not appear, try to move point so it does
14168 appear. The desired matrix has been built above, so we
14169 can use it here. */
14170 new_vpos = window_box_height (w) / 2;
14171 }
14172
14173 if (!cursor_row_fully_visible_p (w, 0, 0))
14174 {
14175 /* Point does appear, but on a line partly visible at end of window.
14176 Move it back to a fully-visible line. */
14177 new_vpos = window_box_height (w);
14178 }
14179
14180 /* If we need to move point for either of the above reasons,
14181 now actually do it. */
14182 if (new_vpos >= 0)
14183 {
14184 struct glyph_row *row;
14185
14186 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14187 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14188 ++row;
14189
14190 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14191 MATRIX_ROW_START_BYTEPOS (row));
14192
14193 if (w != XWINDOW (selected_window))
14194 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14195 else if (current_buffer == old)
14196 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14197
14198 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14199
14200 /* If we are highlighting the region, then we just changed
14201 the region, so redisplay to show it. */
14202 if (!NILP (Vtransient_mark_mode)
14203 && !NILP (current_buffer->mark_active))
14204 {
14205 clear_glyph_matrix (w->desired_matrix);
14206 if (!try_window (window, startp, 0))
14207 goto need_larger_matrices;
14208 }
14209 }
14210
14211 #if GLYPH_DEBUG
14212 debug_method_add (w, "forced window start");
14213 #endif
14214 goto done;
14215 }
14216
14217 /* Handle case where text has not changed, only point, and it has
14218 not moved off the frame, and we are not retrying after hscroll.
14219 (current_matrix_up_to_date_p is nonzero when retrying.) */
14220 if (current_matrix_up_to_date_p
14221 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14222 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14223 {
14224 switch (rc)
14225 {
14226 case CURSOR_MOVEMENT_SUCCESS:
14227 used_current_matrix_p = 1;
14228 goto done;
14229
14230 case CURSOR_MOVEMENT_MUST_SCROLL:
14231 goto try_to_scroll;
14232
14233 default:
14234 abort ();
14235 }
14236 }
14237 /* If current starting point was originally the beginning of a line
14238 but no longer is, find a new starting point. */
14239 else if (!NILP (w->start_at_line_beg)
14240 && !(CHARPOS (startp) <= BEGV
14241 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14242 {
14243 #if GLYPH_DEBUG
14244 debug_method_add (w, "recenter 1");
14245 #endif
14246 goto recenter;
14247 }
14248
14249 /* Try scrolling with try_window_id. Value is > 0 if update has
14250 been done, it is -1 if we know that the same window start will
14251 not work. It is 0 if unsuccessful for some other reason. */
14252 else if ((tem = try_window_id (w)) != 0)
14253 {
14254 #if GLYPH_DEBUG
14255 debug_method_add (w, "try_window_id %d", tem);
14256 #endif
14257
14258 if (fonts_changed_p)
14259 goto need_larger_matrices;
14260 if (tem > 0)
14261 goto done;
14262
14263 /* Otherwise try_window_id has returned -1 which means that we
14264 don't want the alternative below this comment to execute. */
14265 }
14266 else if (CHARPOS (startp) >= BEGV
14267 && CHARPOS (startp) <= ZV
14268 && PT >= CHARPOS (startp)
14269 && (CHARPOS (startp) < ZV
14270 /* Avoid starting at end of buffer. */
14271 || CHARPOS (startp) == BEGV
14272 || (XFASTINT (w->last_modified) >= MODIFF
14273 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14274 {
14275
14276 /* If first window line is a continuation line, and window start
14277 is inside the modified region, but the first change is before
14278 current window start, we must select a new window start.
14279
14280 However, if this is the result of a down-mouse event (e.g. by
14281 extending the mouse-drag-overlay), we don't want to select a
14282 new window start, since that would change the position under
14283 the mouse, resulting in an unwanted mouse-movement rather
14284 than a simple mouse-click. */
14285 if (NILP (w->start_at_line_beg)
14286 && NILP (do_mouse_tracking)
14287 && CHARPOS (startp) > BEGV
14288 && CHARPOS (startp) > BEG + beg_unchanged
14289 && CHARPOS (startp) <= Z - end_unchanged
14290 /* Even if w->start_at_line_beg is nil, a new window may
14291 start at a line_beg, since that's how set_buffer_window
14292 sets it. So, we need to check the return value of
14293 compute_window_start_on_continuation_line. (See also
14294 bug#197). */
14295 && XMARKER (w->start)->buffer == current_buffer
14296 && compute_window_start_on_continuation_line (w))
14297 {
14298 w->force_start = Qt;
14299 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14300 goto force_start;
14301 }
14302
14303 #if GLYPH_DEBUG
14304 debug_method_add (w, "same window start");
14305 #endif
14306
14307 /* Try to redisplay starting at same place as before.
14308 If point has not moved off frame, accept the results. */
14309 if (!current_matrix_up_to_date_p
14310 /* Don't use try_window_reusing_current_matrix in this case
14311 because a window scroll function can have changed the
14312 buffer. */
14313 || !NILP (Vwindow_scroll_functions)
14314 || MINI_WINDOW_P (w)
14315 || !(used_current_matrix_p
14316 = try_window_reusing_current_matrix (w)))
14317 {
14318 IF_DEBUG (debug_method_add (w, "1"));
14319 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14320 /* -1 means we need to scroll.
14321 0 means we need new matrices, but fonts_changed_p
14322 is set in that case, so we will detect it below. */
14323 goto try_to_scroll;
14324 }
14325
14326 if (fonts_changed_p)
14327 goto need_larger_matrices;
14328
14329 if (w->cursor.vpos >= 0)
14330 {
14331 if (!just_this_one_p
14332 || current_buffer->clip_changed
14333 || BEG_UNCHANGED < CHARPOS (startp))
14334 /* Forget any recorded base line for line number display. */
14335 w->base_line_number = Qnil;
14336
14337 if (!cursor_row_fully_visible_p (w, 1, 0))
14338 {
14339 clear_glyph_matrix (w->desired_matrix);
14340 last_line_misfit = 1;
14341 }
14342 /* Drop through and scroll. */
14343 else
14344 goto done;
14345 }
14346 else
14347 clear_glyph_matrix (w->desired_matrix);
14348 }
14349
14350 try_to_scroll:
14351
14352 w->last_modified = make_number (0);
14353 w->last_overlay_modified = make_number (0);
14354
14355 /* Redisplay the mode line. Select the buffer properly for that. */
14356 if (!update_mode_line)
14357 {
14358 update_mode_line = 1;
14359 w->update_mode_line = Qt;
14360 }
14361
14362 /* Try to scroll by specified few lines. */
14363 if ((scroll_conservatively
14364 || scroll_step
14365 || temp_scroll_step
14366 || NUMBERP (current_buffer->scroll_up_aggressively)
14367 || NUMBERP (current_buffer->scroll_down_aggressively))
14368 && !current_buffer->clip_changed
14369 && CHARPOS (startp) >= BEGV
14370 && CHARPOS (startp) <= ZV)
14371 {
14372 /* The function returns -1 if new fonts were loaded, 1 if
14373 successful, 0 if not successful. */
14374 int rc = try_scrolling (window, just_this_one_p,
14375 scroll_conservatively,
14376 scroll_step,
14377 temp_scroll_step, last_line_misfit);
14378 switch (rc)
14379 {
14380 case SCROLLING_SUCCESS:
14381 goto done;
14382
14383 case SCROLLING_NEED_LARGER_MATRICES:
14384 goto need_larger_matrices;
14385
14386 case SCROLLING_FAILED:
14387 break;
14388
14389 default:
14390 abort ();
14391 }
14392 }
14393
14394 /* Finally, just choose place to start which centers point */
14395
14396 recenter:
14397 if (centering_position < 0)
14398 centering_position = window_box_height (w) / 2;
14399
14400 #if GLYPH_DEBUG
14401 debug_method_add (w, "recenter");
14402 #endif
14403
14404 /* w->vscroll = 0; */
14405
14406 /* Forget any previously recorded base line for line number display. */
14407 if (!buffer_unchanged_p)
14408 w->base_line_number = Qnil;
14409
14410 /* Move backward half the height of the window. */
14411 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14412 it.current_y = it.last_visible_y;
14413 move_it_vertically_backward (&it, centering_position);
14414 xassert (IT_CHARPOS (it) >= BEGV);
14415
14416 /* The function move_it_vertically_backward may move over more
14417 than the specified y-distance. If it->w is small, e.g. a
14418 mini-buffer window, we may end up in front of the window's
14419 display area. Start displaying at the start of the line
14420 containing PT in this case. */
14421 if (it.current_y <= 0)
14422 {
14423 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14424 move_it_vertically_backward (&it, 0);
14425 it.current_y = 0;
14426 }
14427
14428 it.current_x = it.hpos = 0;
14429
14430 /* Set startp here explicitly in case that helps avoid an infinite loop
14431 in case the window-scroll-functions functions get errors. */
14432 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14433
14434 /* Run scroll hooks. */
14435 startp = run_window_scroll_functions (window, it.current.pos);
14436
14437 /* Redisplay the window. */
14438 if (!current_matrix_up_to_date_p
14439 || windows_or_buffers_changed
14440 || cursor_type_changed
14441 /* Don't use try_window_reusing_current_matrix in this case
14442 because it can have changed the buffer. */
14443 || !NILP (Vwindow_scroll_functions)
14444 || !just_this_one_p
14445 || MINI_WINDOW_P (w)
14446 || !(used_current_matrix_p
14447 = try_window_reusing_current_matrix (w)))
14448 try_window (window, startp, 0);
14449
14450 /* If new fonts have been loaded (due to fontsets), give up. We
14451 have to start a new redisplay since we need to re-adjust glyph
14452 matrices. */
14453 if (fonts_changed_p)
14454 goto need_larger_matrices;
14455
14456 /* If cursor did not appear assume that the middle of the window is
14457 in the first line of the window. Do it again with the next line.
14458 (Imagine a window of height 100, displaying two lines of height
14459 60. Moving back 50 from it->last_visible_y will end in the first
14460 line.) */
14461 if (w->cursor.vpos < 0)
14462 {
14463 if (!NILP (w->window_end_valid)
14464 && PT >= Z - XFASTINT (w->window_end_pos))
14465 {
14466 clear_glyph_matrix (w->desired_matrix);
14467 move_it_by_lines (&it, 1, 0);
14468 try_window (window, it.current.pos, 0);
14469 }
14470 else if (PT < IT_CHARPOS (it))
14471 {
14472 clear_glyph_matrix (w->desired_matrix);
14473 move_it_by_lines (&it, -1, 0);
14474 try_window (window, it.current.pos, 0);
14475 }
14476 else
14477 {
14478 /* Not much we can do about it. */
14479 }
14480 }
14481
14482 /* Consider the following case: Window starts at BEGV, there is
14483 invisible, intangible text at BEGV, so that display starts at
14484 some point START > BEGV. It can happen that we are called with
14485 PT somewhere between BEGV and START. Try to handle that case. */
14486 if (w->cursor.vpos < 0)
14487 {
14488 struct glyph_row *row = w->current_matrix->rows;
14489 if (row->mode_line_p)
14490 ++row;
14491 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14492 }
14493
14494 if (!cursor_row_fully_visible_p (w, 0, 0))
14495 {
14496 /* If vscroll is enabled, disable it and try again. */
14497 if (w->vscroll)
14498 {
14499 w->vscroll = 0;
14500 clear_glyph_matrix (w->desired_matrix);
14501 goto recenter;
14502 }
14503
14504 /* If centering point failed to make the whole line visible,
14505 put point at the top instead. That has to make the whole line
14506 visible, if it can be done. */
14507 if (centering_position == 0)
14508 goto done;
14509
14510 clear_glyph_matrix (w->desired_matrix);
14511 centering_position = 0;
14512 goto recenter;
14513 }
14514
14515 done:
14516
14517 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14518 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14519 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14520 ? Qt : Qnil);
14521
14522 /* Display the mode line, if we must. */
14523 if ((update_mode_line
14524 /* If window not full width, must redo its mode line
14525 if (a) the window to its side is being redone and
14526 (b) we do a frame-based redisplay. This is a consequence
14527 of how inverted lines are drawn in frame-based redisplay. */
14528 || (!just_this_one_p
14529 && !FRAME_WINDOW_P (f)
14530 && !WINDOW_FULL_WIDTH_P (w))
14531 /* Line number to display. */
14532 || INTEGERP (w->base_line_pos)
14533 /* Column number is displayed and different from the one displayed. */
14534 || (!NILP (w->column_number_displayed)
14535 && (XFASTINT (w->column_number_displayed)
14536 != (int) current_column ()))) /* iftc */
14537 /* This means that the window has a mode line. */
14538 && (WINDOW_WANTS_MODELINE_P (w)
14539 || WINDOW_WANTS_HEADER_LINE_P (w)))
14540 {
14541 display_mode_lines (w);
14542
14543 /* If mode line height has changed, arrange for a thorough
14544 immediate redisplay using the correct mode line height. */
14545 if (WINDOW_WANTS_MODELINE_P (w)
14546 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14547 {
14548 fonts_changed_p = 1;
14549 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14550 = DESIRED_MODE_LINE_HEIGHT (w);
14551 }
14552
14553 /* If header line height has changed, arrange for a thorough
14554 immediate redisplay using the correct header line height. */
14555 if (WINDOW_WANTS_HEADER_LINE_P (w)
14556 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14557 {
14558 fonts_changed_p = 1;
14559 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14560 = DESIRED_HEADER_LINE_HEIGHT (w);
14561 }
14562
14563 if (fonts_changed_p)
14564 goto need_larger_matrices;
14565 }
14566
14567 if (!line_number_displayed
14568 && !BUFFERP (w->base_line_pos))
14569 {
14570 w->base_line_pos = Qnil;
14571 w->base_line_number = Qnil;
14572 }
14573
14574 finish_menu_bars:
14575
14576 /* When we reach a frame's selected window, redo the frame's menu bar. */
14577 if (update_mode_line
14578 && EQ (FRAME_SELECTED_WINDOW (f), window))
14579 {
14580 int redisplay_menu_p = 0;
14581 int redisplay_tool_bar_p = 0;
14582
14583 if (FRAME_WINDOW_P (f))
14584 {
14585 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14586 || defined (HAVE_NS) || defined (USE_GTK)
14587 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14588 #else
14589 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14590 #endif
14591 }
14592 else
14593 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14594
14595 if (redisplay_menu_p)
14596 display_menu_bar (w);
14597
14598 #ifdef HAVE_WINDOW_SYSTEM
14599 if (FRAME_WINDOW_P (f))
14600 {
14601 #if defined (USE_GTK) || defined (HAVE_NS)
14602 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14603 #else
14604 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14605 && (FRAME_TOOL_BAR_LINES (f) > 0
14606 || !NILP (Vauto_resize_tool_bars));
14607 #endif
14608
14609 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14610 {
14611 ignore_mouse_drag_p = 1;
14612 }
14613 }
14614 #endif
14615 }
14616
14617 #ifdef HAVE_WINDOW_SYSTEM
14618 if (FRAME_WINDOW_P (f)
14619 && update_window_fringes (w, (just_this_one_p
14620 || (!used_current_matrix_p && !overlay_arrow_seen)
14621 || w->pseudo_window_p)))
14622 {
14623 update_begin (f);
14624 BLOCK_INPUT;
14625 if (draw_window_fringes (w, 1))
14626 x_draw_vertical_border (w);
14627 UNBLOCK_INPUT;
14628 update_end (f);
14629 }
14630 #endif /* HAVE_WINDOW_SYSTEM */
14631
14632 /* We go to this label, with fonts_changed_p nonzero,
14633 if it is necessary to try again using larger glyph matrices.
14634 We have to redeem the scroll bar even in this case,
14635 because the loop in redisplay_internal expects that. */
14636 need_larger_matrices:
14637 ;
14638 finish_scroll_bars:
14639
14640 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14641 {
14642 /* Set the thumb's position and size. */
14643 set_vertical_scroll_bar (w);
14644
14645 /* Note that we actually used the scroll bar attached to this
14646 window, so it shouldn't be deleted at the end of redisplay. */
14647 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14648 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14649 }
14650
14651 /* Restore current_buffer and value of point in it. The window
14652 update may have changed the buffer, so first make sure `opoint'
14653 is still valid (Bug#6177). */
14654 if (CHARPOS (opoint) < BEGV)
14655 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14656 else if (CHARPOS (opoint) > ZV)
14657 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14658 else
14659 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14660
14661 set_buffer_internal_1 (old);
14662 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14663 shorter. This can be caused by log truncation in *Messages*. */
14664 if (CHARPOS (lpoint) <= ZV)
14665 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14666
14667 unbind_to (count, Qnil);
14668 }
14669
14670
14671 /* Build the complete desired matrix of WINDOW with a window start
14672 buffer position POS.
14673
14674 Value is 1 if successful. It is zero if fonts were loaded during
14675 redisplay which makes re-adjusting glyph matrices necessary, and -1
14676 if point would appear in the scroll margins.
14677 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14678 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14679 set in FLAGS.) */
14680
14681 int
14682 try_window (Lisp_Object window, struct text_pos pos, int flags)
14683 {
14684 struct window *w = XWINDOW (window);
14685 struct it it;
14686 struct glyph_row *last_text_row = NULL;
14687 struct frame *f = XFRAME (w->frame);
14688
14689 /* Make POS the new window start. */
14690 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14691
14692 /* Mark cursor position as unknown. No overlay arrow seen. */
14693 w->cursor.vpos = -1;
14694 overlay_arrow_seen = 0;
14695
14696 /* Initialize iterator and info to start at POS. */
14697 start_display (&it, w, pos);
14698
14699 /* Display all lines of W. */
14700 while (it.current_y < it.last_visible_y)
14701 {
14702 if (display_line (&it))
14703 last_text_row = it.glyph_row - 1;
14704 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14705 return 0;
14706 }
14707
14708 /* Don't let the cursor end in the scroll margins. */
14709 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14710 && !MINI_WINDOW_P (w))
14711 {
14712 int this_scroll_margin;
14713
14714 if (scroll_margin > 0)
14715 {
14716 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14717 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14718 }
14719 else
14720 this_scroll_margin = 0;
14721
14722 if ((w->cursor.y >= 0 /* not vscrolled */
14723 && w->cursor.y < this_scroll_margin
14724 && CHARPOS (pos) > BEGV
14725 && IT_CHARPOS (it) < ZV)
14726 /* rms: considering make_cursor_line_fully_visible_p here
14727 seems to give wrong results. We don't want to recenter
14728 when the last line is partly visible, we want to allow
14729 that case to be handled in the usual way. */
14730 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14731 {
14732 w->cursor.vpos = -1;
14733 clear_glyph_matrix (w->desired_matrix);
14734 return -1;
14735 }
14736 }
14737
14738 /* If bottom moved off end of frame, change mode line percentage. */
14739 if (XFASTINT (w->window_end_pos) <= 0
14740 && Z != IT_CHARPOS (it))
14741 w->update_mode_line = Qt;
14742
14743 /* Set window_end_pos to the offset of the last character displayed
14744 on the window from the end of current_buffer. Set
14745 window_end_vpos to its row number. */
14746 if (last_text_row)
14747 {
14748 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14749 w->window_end_bytepos
14750 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14751 w->window_end_pos
14752 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14753 w->window_end_vpos
14754 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14755 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14756 ->displays_text_p);
14757 }
14758 else
14759 {
14760 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14761 w->window_end_pos = make_number (Z - ZV);
14762 w->window_end_vpos = make_number (0);
14763 }
14764
14765 /* But that is not valid info until redisplay finishes. */
14766 w->window_end_valid = Qnil;
14767 return 1;
14768 }
14769
14770
14771 \f
14772 /************************************************************************
14773 Window redisplay reusing current matrix when buffer has not changed
14774 ************************************************************************/
14775
14776 /* Try redisplay of window W showing an unchanged buffer with a
14777 different window start than the last time it was displayed by
14778 reusing its current matrix. Value is non-zero if successful.
14779 W->start is the new window start. */
14780
14781 static int
14782 try_window_reusing_current_matrix (struct window *w)
14783 {
14784 struct frame *f = XFRAME (w->frame);
14785 struct glyph_row *row, *bottom_row;
14786 struct it it;
14787 struct run run;
14788 struct text_pos start, new_start;
14789 int nrows_scrolled, i;
14790 struct glyph_row *last_text_row;
14791 struct glyph_row *last_reused_text_row;
14792 struct glyph_row *start_row;
14793 int start_vpos, min_y, max_y;
14794
14795 #if GLYPH_DEBUG
14796 if (inhibit_try_window_reusing)
14797 return 0;
14798 #endif
14799
14800 if (/* This function doesn't handle terminal frames. */
14801 !FRAME_WINDOW_P (f)
14802 /* Don't try to reuse the display if windows have been split
14803 or such. */
14804 || windows_or_buffers_changed
14805 || cursor_type_changed)
14806 return 0;
14807
14808 /* Can't do this if region may have changed. */
14809 if ((!NILP (Vtransient_mark_mode)
14810 && !NILP (current_buffer->mark_active))
14811 || !NILP (w->region_showing)
14812 || !NILP (Vshow_trailing_whitespace))
14813 return 0;
14814
14815 /* If top-line visibility has changed, give up. */
14816 if (WINDOW_WANTS_HEADER_LINE_P (w)
14817 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14818 return 0;
14819
14820 /* Give up if old or new display is scrolled vertically. We could
14821 make this function handle this, but right now it doesn't. */
14822 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14823 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14824 return 0;
14825
14826 /* The variable new_start now holds the new window start. The old
14827 start `start' can be determined from the current matrix. */
14828 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14829 start = start_row->minpos;
14830 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14831
14832 /* Clear the desired matrix for the display below. */
14833 clear_glyph_matrix (w->desired_matrix);
14834
14835 if (CHARPOS (new_start) <= CHARPOS (start))
14836 {
14837 int first_row_y;
14838
14839 /* Don't use this method if the display starts with an ellipsis
14840 displayed for invisible text. It's not easy to handle that case
14841 below, and it's certainly not worth the effort since this is
14842 not a frequent case. */
14843 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14844 return 0;
14845
14846 IF_DEBUG (debug_method_add (w, "twu1"));
14847
14848 /* Display up to a row that can be reused. The variable
14849 last_text_row is set to the last row displayed that displays
14850 text. Note that it.vpos == 0 if or if not there is a
14851 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14852 start_display (&it, w, new_start);
14853 first_row_y = it.current_y;
14854 w->cursor.vpos = -1;
14855 last_text_row = last_reused_text_row = NULL;
14856
14857 while (it.current_y < it.last_visible_y
14858 && !fonts_changed_p)
14859 {
14860 /* If we have reached into the characters in the START row,
14861 that means the line boundaries have changed. So we
14862 can't start copying with the row START. Maybe it will
14863 work to start copying with the following row. */
14864 while (IT_CHARPOS (it) > CHARPOS (start))
14865 {
14866 /* Advance to the next row as the "start". */
14867 start_row++;
14868 start = start_row->minpos;
14869 /* If there are no more rows to try, or just one, give up. */
14870 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14871 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14872 || CHARPOS (start) == ZV)
14873 {
14874 clear_glyph_matrix (w->desired_matrix);
14875 return 0;
14876 }
14877
14878 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14879 }
14880 /* If we have reached alignment,
14881 we can copy the rest of the rows. */
14882 if (IT_CHARPOS (it) == CHARPOS (start))
14883 break;
14884
14885 if (display_line (&it))
14886 last_text_row = it.glyph_row - 1;
14887 }
14888
14889 /* A value of current_y < last_visible_y means that we stopped
14890 at the previous window start, which in turn means that we
14891 have at least one reusable row. */
14892 if (it.current_y < it.last_visible_y)
14893 {
14894 /* IT.vpos always starts from 0; it counts text lines. */
14895 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14896
14897 /* Find PT if not already found in the lines displayed. */
14898 if (w->cursor.vpos < 0)
14899 {
14900 int dy = it.current_y - start_row->y;
14901
14902 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14903 row = row_containing_pos (w, PT, row, NULL, dy);
14904 if (row)
14905 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14906 dy, nrows_scrolled);
14907 else
14908 {
14909 clear_glyph_matrix (w->desired_matrix);
14910 return 0;
14911 }
14912 }
14913
14914 /* Scroll the display. Do it before the current matrix is
14915 changed. The problem here is that update has not yet
14916 run, i.e. part of the current matrix is not up to date.
14917 scroll_run_hook will clear the cursor, and use the
14918 current matrix to get the height of the row the cursor is
14919 in. */
14920 run.current_y = start_row->y;
14921 run.desired_y = it.current_y;
14922 run.height = it.last_visible_y - it.current_y;
14923
14924 if (run.height > 0 && run.current_y != run.desired_y)
14925 {
14926 update_begin (f);
14927 FRAME_RIF (f)->update_window_begin_hook (w);
14928 FRAME_RIF (f)->clear_window_mouse_face (w);
14929 FRAME_RIF (f)->scroll_run_hook (w, &run);
14930 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14931 update_end (f);
14932 }
14933
14934 /* Shift current matrix down by nrows_scrolled lines. */
14935 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14936 rotate_matrix (w->current_matrix,
14937 start_vpos,
14938 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14939 nrows_scrolled);
14940
14941 /* Disable lines that must be updated. */
14942 for (i = 0; i < nrows_scrolled; ++i)
14943 (start_row + i)->enabled_p = 0;
14944
14945 /* Re-compute Y positions. */
14946 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14947 max_y = it.last_visible_y;
14948 for (row = start_row + nrows_scrolled;
14949 row < bottom_row;
14950 ++row)
14951 {
14952 row->y = it.current_y;
14953 row->visible_height = row->height;
14954
14955 if (row->y < min_y)
14956 row->visible_height -= min_y - row->y;
14957 if (row->y + row->height > max_y)
14958 row->visible_height -= row->y + row->height - max_y;
14959 row->redraw_fringe_bitmaps_p = 1;
14960
14961 it.current_y += row->height;
14962
14963 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14964 last_reused_text_row = row;
14965 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14966 break;
14967 }
14968
14969 /* Disable lines in the current matrix which are now
14970 below the window. */
14971 for (++row; row < bottom_row; ++row)
14972 row->enabled_p = row->mode_line_p = 0;
14973 }
14974
14975 /* Update window_end_pos etc.; last_reused_text_row is the last
14976 reused row from the current matrix containing text, if any.
14977 The value of last_text_row is the last displayed line
14978 containing text. */
14979 if (last_reused_text_row)
14980 {
14981 w->window_end_bytepos
14982 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14983 w->window_end_pos
14984 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14985 w->window_end_vpos
14986 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14987 w->current_matrix));
14988 }
14989 else if (last_text_row)
14990 {
14991 w->window_end_bytepos
14992 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14993 w->window_end_pos
14994 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14995 w->window_end_vpos
14996 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14997 }
14998 else
14999 {
15000 /* This window must be completely empty. */
15001 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15002 w->window_end_pos = make_number (Z - ZV);
15003 w->window_end_vpos = make_number (0);
15004 }
15005 w->window_end_valid = Qnil;
15006
15007 /* Update hint: don't try scrolling again in update_window. */
15008 w->desired_matrix->no_scrolling_p = 1;
15009
15010 #if GLYPH_DEBUG
15011 debug_method_add (w, "try_window_reusing_current_matrix 1");
15012 #endif
15013 return 1;
15014 }
15015 else if (CHARPOS (new_start) > CHARPOS (start))
15016 {
15017 struct glyph_row *pt_row, *row;
15018 struct glyph_row *first_reusable_row;
15019 struct glyph_row *first_row_to_display;
15020 int dy;
15021 int yb = window_text_bottom_y (w);
15022
15023 /* Find the row starting at new_start, if there is one. Don't
15024 reuse a partially visible line at the end. */
15025 first_reusable_row = start_row;
15026 while (first_reusable_row->enabled_p
15027 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15028 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15029 < CHARPOS (new_start)))
15030 ++first_reusable_row;
15031
15032 /* Give up if there is no row to reuse. */
15033 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15034 || !first_reusable_row->enabled_p
15035 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15036 != CHARPOS (new_start)))
15037 return 0;
15038
15039 /* We can reuse fully visible rows beginning with
15040 first_reusable_row to the end of the window. Set
15041 first_row_to_display to the first row that cannot be reused.
15042 Set pt_row to the row containing point, if there is any. */
15043 pt_row = NULL;
15044 for (first_row_to_display = first_reusable_row;
15045 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15046 ++first_row_to_display)
15047 {
15048 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15049 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15050 pt_row = first_row_to_display;
15051 }
15052
15053 /* Start displaying at the start of first_row_to_display. */
15054 xassert (first_row_to_display->y < yb);
15055 init_to_row_start (&it, w, first_row_to_display);
15056
15057 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15058 - start_vpos);
15059 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15060 - nrows_scrolled);
15061 it.current_y = (first_row_to_display->y - first_reusable_row->y
15062 + WINDOW_HEADER_LINE_HEIGHT (w));
15063
15064 /* Display lines beginning with first_row_to_display in the
15065 desired matrix. Set last_text_row to the last row displayed
15066 that displays text. */
15067 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15068 if (pt_row == NULL)
15069 w->cursor.vpos = -1;
15070 last_text_row = NULL;
15071 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15072 if (display_line (&it))
15073 last_text_row = it.glyph_row - 1;
15074
15075 /* If point is in a reused row, adjust y and vpos of the cursor
15076 position. */
15077 if (pt_row)
15078 {
15079 w->cursor.vpos -= nrows_scrolled;
15080 w->cursor.y -= first_reusable_row->y - start_row->y;
15081 }
15082
15083 /* Give up if point isn't in a row displayed or reused. (This
15084 also handles the case where w->cursor.vpos < nrows_scrolled
15085 after the calls to display_line, which can happen with scroll
15086 margins. See bug#1295.) */
15087 if (w->cursor.vpos < 0)
15088 {
15089 clear_glyph_matrix (w->desired_matrix);
15090 return 0;
15091 }
15092
15093 /* Scroll the display. */
15094 run.current_y = first_reusable_row->y;
15095 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15096 run.height = it.last_visible_y - run.current_y;
15097 dy = run.current_y - run.desired_y;
15098
15099 if (run.height)
15100 {
15101 update_begin (f);
15102 FRAME_RIF (f)->update_window_begin_hook (w);
15103 FRAME_RIF (f)->clear_window_mouse_face (w);
15104 FRAME_RIF (f)->scroll_run_hook (w, &run);
15105 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15106 update_end (f);
15107 }
15108
15109 /* Adjust Y positions of reused rows. */
15110 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15111 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15112 max_y = it.last_visible_y;
15113 for (row = first_reusable_row; row < first_row_to_display; ++row)
15114 {
15115 row->y -= dy;
15116 row->visible_height = row->height;
15117 if (row->y < min_y)
15118 row->visible_height -= min_y - row->y;
15119 if (row->y + row->height > max_y)
15120 row->visible_height -= row->y + row->height - max_y;
15121 row->redraw_fringe_bitmaps_p = 1;
15122 }
15123
15124 /* Scroll the current matrix. */
15125 xassert (nrows_scrolled > 0);
15126 rotate_matrix (w->current_matrix,
15127 start_vpos,
15128 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15129 -nrows_scrolled);
15130
15131 /* Disable rows not reused. */
15132 for (row -= nrows_scrolled; row < bottom_row; ++row)
15133 row->enabled_p = 0;
15134
15135 /* Point may have moved to a different line, so we cannot assume that
15136 the previous cursor position is valid; locate the correct row. */
15137 if (pt_row)
15138 {
15139 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15140 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15141 row++)
15142 {
15143 w->cursor.vpos++;
15144 w->cursor.y = row->y;
15145 }
15146 if (row < bottom_row)
15147 {
15148 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15149 struct glyph *end = glyph + row->used[TEXT_AREA];
15150
15151 /* Can't use this optimization with bidi-reordered glyph
15152 rows, unless cursor is already at point. */
15153 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15154 {
15155 if (!(w->cursor.hpos >= 0
15156 && w->cursor.hpos < row->used[TEXT_AREA]
15157 && BUFFERP (glyph->object)
15158 && glyph->charpos == PT))
15159 return 0;
15160 }
15161 else
15162 for (; glyph < end
15163 && (!BUFFERP (glyph->object)
15164 || glyph->charpos < PT);
15165 glyph++)
15166 {
15167 w->cursor.hpos++;
15168 w->cursor.x += glyph->pixel_width;
15169 }
15170 }
15171 }
15172
15173 /* Adjust window end. A null value of last_text_row means that
15174 the window end is in reused rows which in turn means that
15175 only its vpos can have changed. */
15176 if (last_text_row)
15177 {
15178 w->window_end_bytepos
15179 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15180 w->window_end_pos
15181 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15182 w->window_end_vpos
15183 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15184 }
15185 else
15186 {
15187 w->window_end_vpos
15188 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15189 }
15190
15191 w->window_end_valid = Qnil;
15192 w->desired_matrix->no_scrolling_p = 1;
15193
15194 #if GLYPH_DEBUG
15195 debug_method_add (w, "try_window_reusing_current_matrix 2");
15196 #endif
15197 return 1;
15198 }
15199
15200 return 0;
15201 }
15202
15203
15204 \f
15205 /************************************************************************
15206 Window redisplay reusing current matrix when buffer has changed
15207 ************************************************************************/
15208
15209 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15210 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15211 EMACS_INT *, EMACS_INT *);
15212 static struct glyph_row *
15213 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15214 struct glyph_row *);
15215
15216
15217 /* Return the last row in MATRIX displaying text. If row START is
15218 non-null, start searching with that row. IT gives the dimensions
15219 of the display. Value is null if matrix is empty; otherwise it is
15220 a pointer to the row found. */
15221
15222 static struct glyph_row *
15223 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15224 struct glyph_row *start)
15225 {
15226 struct glyph_row *row, *row_found;
15227
15228 /* Set row_found to the last row in IT->w's current matrix
15229 displaying text. The loop looks funny but think of partially
15230 visible lines. */
15231 row_found = NULL;
15232 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15233 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15234 {
15235 xassert (row->enabled_p);
15236 row_found = row;
15237 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15238 break;
15239 ++row;
15240 }
15241
15242 return row_found;
15243 }
15244
15245
15246 /* Return the last row in the current matrix of W that is not affected
15247 by changes at the start of current_buffer that occurred since W's
15248 current matrix was built. Value is null if no such row exists.
15249
15250 BEG_UNCHANGED us the number of characters unchanged at the start of
15251 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15252 first changed character in current_buffer. Characters at positions <
15253 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15254 when the current matrix was built. */
15255
15256 static struct glyph_row *
15257 find_last_unchanged_at_beg_row (struct window *w)
15258 {
15259 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15260 struct glyph_row *row;
15261 struct glyph_row *row_found = NULL;
15262 int yb = window_text_bottom_y (w);
15263
15264 /* Find the last row displaying unchanged text. */
15265 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15266 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15267 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15268 ++row)
15269 {
15270 if (/* If row ends before first_changed_pos, it is unchanged,
15271 except in some case. */
15272 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15273 /* When row ends in ZV and we write at ZV it is not
15274 unchanged. */
15275 && !row->ends_at_zv_p
15276 /* When first_changed_pos is the end of a continued line,
15277 row is not unchanged because it may be no longer
15278 continued. */
15279 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15280 && (row->continued_p
15281 || row->exact_window_width_line_p)))
15282 row_found = row;
15283
15284 /* Stop if last visible row. */
15285 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15286 break;
15287 }
15288
15289 return row_found;
15290 }
15291
15292
15293 /* Find the first glyph row in the current matrix of W that is not
15294 affected by changes at the end of current_buffer since the
15295 time W's current matrix was built.
15296
15297 Return in *DELTA the number of chars by which buffer positions in
15298 unchanged text at the end of current_buffer must be adjusted.
15299
15300 Return in *DELTA_BYTES the corresponding number of bytes.
15301
15302 Value is null if no such row exists, i.e. all rows are affected by
15303 changes. */
15304
15305 static struct glyph_row *
15306 find_first_unchanged_at_end_row (struct window *w,
15307 EMACS_INT *delta, EMACS_INT *delta_bytes)
15308 {
15309 struct glyph_row *row;
15310 struct glyph_row *row_found = NULL;
15311
15312 *delta = *delta_bytes = 0;
15313
15314 /* Display must not have been paused, otherwise the current matrix
15315 is not up to date. */
15316 eassert (!NILP (w->window_end_valid));
15317
15318 /* A value of window_end_pos >= END_UNCHANGED means that the window
15319 end is in the range of changed text. If so, there is no
15320 unchanged row at the end of W's current matrix. */
15321 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15322 return NULL;
15323
15324 /* Set row to the last row in W's current matrix displaying text. */
15325 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15326
15327 /* If matrix is entirely empty, no unchanged row exists. */
15328 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15329 {
15330 /* The value of row is the last glyph row in the matrix having a
15331 meaningful buffer position in it. The end position of row
15332 corresponds to window_end_pos. This allows us to translate
15333 buffer positions in the current matrix to current buffer
15334 positions for characters not in changed text. */
15335 EMACS_INT Z_old =
15336 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15337 EMACS_INT Z_BYTE_old =
15338 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15339 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15340 struct glyph_row *first_text_row
15341 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15342
15343 *delta = Z - Z_old;
15344 *delta_bytes = Z_BYTE - Z_BYTE_old;
15345
15346 /* Set last_unchanged_pos to the buffer position of the last
15347 character in the buffer that has not been changed. Z is the
15348 index + 1 of the last character in current_buffer, i.e. by
15349 subtracting END_UNCHANGED we get the index of the last
15350 unchanged character, and we have to add BEG to get its buffer
15351 position. */
15352 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15353 last_unchanged_pos_old = last_unchanged_pos - *delta;
15354
15355 /* Search backward from ROW for a row displaying a line that
15356 starts at a minimum position >= last_unchanged_pos_old. */
15357 for (; row > first_text_row; --row)
15358 {
15359 /* This used to abort, but it can happen.
15360 It is ok to just stop the search instead here. KFS. */
15361 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15362 break;
15363
15364 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15365 row_found = row;
15366 }
15367 }
15368
15369 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15370
15371 return row_found;
15372 }
15373
15374
15375 /* Make sure that glyph rows in the current matrix of window W
15376 reference the same glyph memory as corresponding rows in the
15377 frame's frame matrix. This function is called after scrolling W's
15378 current matrix on a terminal frame in try_window_id and
15379 try_window_reusing_current_matrix. */
15380
15381 static void
15382 sync_frame_with_window_matrix_rows (struct window *w)
15383 {
15384 struct frame *f = XFRAME (w->frame);
15385 struct glyph_row *window_row, *window_row_end, *frame_row;
15386
15387 /* Preconditions: W must be a leaf window and full-width. Its frame
15388 must have a frame matrix. */
15389 xassert (NILP (w->hchild) && NILP (w->vchild));
15390 xassert (WINDOW_FULL_WIDTH_P (w));
15391 xassert (!FRAME_WINDOW_P (f));
15392
15393 /* If W is a full-width window, glyph pointers in W's current matrix
15394 have, by definition, to be the same as glyph pointers in the
15395 corresponding frame matrix. Note that frame matrices have no
15396 marginal areas (see build_frame_matrix). */
15397 window_row = w->current_matrix->rows;
15398 window_row_end = window_row + w->current_matrix->nrows;
15399 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15400 while (window_row < window_row_end)
15401 {
15402 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15403 struct glyph *end = window_row->glyphs[LAST_AREA];
15404
15405 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15406 frame_row->glyphs[TEXT_AREA] = start;
15407 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15408 frame_row->glyphs[LAST_AREA] = end;
15409
15410 /* Disable frame rows whose corresponding window rows have
15411 been disabled in try_window_id. */
15412 if (!window_row->enabled_p)
15413 frame_row->enabled_p = 0;
15414
15415 ++window_row, ++frame_row;
15416 }
15417 }
15418
15419
15420 /* Find the glyph row in window W containing CHARPOS. Consider all
15421 rows between START and END (not inclusive). END null means search
15422 all rows to the end of the display area of W. Value is the row
15423 containing CHARPOS or null. */
15424
15425 struct glyph_row *
15426 row_containing_pos (struct window *w, EMACS_INT charpos,
15427 struct glyph_row *start, struct glyph_row *end, int dy)
15428 {
15429 struct glyph_row *row = start;
15430 struct glyph_row *best_row = NULL;
15431 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15432 int last_y;
15433
15434 /* If we happen to start on a header-line, skip that. */
15435 if (row->mode_line_p)
15436 ++row;
15437
15438 if ((end && row >= end) || !row->enabled_p)
15439 return NULL;
15440
15441 last_y = window_text_bottom_y (w) - dy;
15442
15443 while (1)
15444 {
15445 /* Give up if we have gone too far. */
15446 if (end && row >= end)
15447 return NULL;
15448 /* This formerly returned if they were equal.
15449 I think that both quantities are of a "last plus one" type;
15450 if so, when they are equal, the row is within the screen. -- rms. */
15451 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15452 return NULL;
15453
15454 /* If it is in this row, return this row. */
15455 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15456 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15457 /* The end position of a row equals the start
15458 position of the next row. If CHARPOS is there, we
15459 would rather display it in the next line, except
15460 when this line ends in ZV. */
15461 && !row->ends_at_zv_p
15462 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15463 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15464 {
15465 struct glyph *g;
15466
15467 if (NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15468 || (!best_row && !row->continued_p))
15469 return row;
15470 /* In bidi-reordered rows, there could be several rows
15471 occluding point, all of them belonging to the same
15472 continued line. We need to find the row which fits
15473 CHARPOS the best. */
15474 for (g = row->glyphs[TEXT_AREA];
15475 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15476 g++)
15477 {
15478 if (!STRINGP (g->object))
15479 {
15480 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15481 {
15482 mindif = eabs (g->charpos - charpos);
15483 best_row = row;
15484 /* Exact match always wins. */
15485 if (mindif == 0)
15486 return best_row;
15487 }
15488 }
15489 }
15490 }
15491 else if (best_row && !row->continued_p)
15492 return best_row;
15493 ++row;
15494 }
15495 }
15496
15497
15498 /* Try to redisplay window W by reusing its existing display. W's
15499 current matrix must be up to date when this function is called,
15500 i.e. window_end_valid must not be nil.
15501
15502 Value is
15503
15504 1 if display has been updated
15505 0 if otherwise unsuccessful
15506 -1 if redisplay with same window start is known not to succeed
15507
15508 The following steps are performed:
15509
15510 1. Find the last row in the current matrix of W that is not
15511 affected by changes at the start of current_buffer. If no such row
15512 is found, give up.
15513
15514 2. Find the first row in W's current matrix that is not affected by
15515 changes at the end of current_buffer. Maybe there is no such row.
15516
15517 3. Display lines beginning with the row + 1 found in step 1 to the
15518 row found in step 2 or, if step 2 didn't find a row, to the end of
15519 the window.
15520
15521 4. If cursor is not known to appear on the window, give up.
15522
15523 5. If display stopped at the row found in step 2, scroll the
15524 display and current matrix as needed.
15525
15526 6. Maybe display some lines at the end of W, if we must. This can
15527 happen under various circumstances, like a partially visible line
15528 becoming fully visible, or because newly displayed lines are displayed
15529 in smaller font sizes.
15530
15531 7. Update W's window end information. */
15532
15533 static int
15534 try_window_id (struct window *w)
15535 {
15536 struct frame *f = XFRAME (w->frame);
15537 struct glyph_matrix *current_matrix = w->current_matrix;
15538 struct glyph_matrix *desired_matrix = w->desired_matrix;
15539 struct glyph_row *last_unchanged_at_beg_row;
15540 struct glyph_row *first_unchanged_at_end_row;
15541 struct glyph_row *row;
15542 struct glyph_row *bottom_row;
15543 int bottom_vpos;
15544 struct it it;
15545 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
15546 int dvpos, dy;
15547 struct text_pos start_pos;
15548 struct run run;
15549 int first_unchanged_at_end_vpos = 0;
15550 struct glyph_row *last_text_row, *last_text_row_at_end;
15551 struct text_pos start;
15552 EMACS_INT first_changed_charpos, last_changed_charpos;
15553
15554 #if GLYPH_DEBUG
15555 if (inhibit_try_window_id)
15556 return 0;
15557 #endif
15558
15559 /* This is handy for debugging. */
15560 #if 0
15561 #define GIVE_UP(X) \
15562 do { \
15563 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15564 return 0; \
15565 } while (0)
15566 #else
15567 #define GIVE_UP(X) return 0
15568 #endif
15569
15570 SET_TEXT_POS_FROM_MARKER (start, w->start);
15571
15572 /* Don't use this for mini-windows because these can show
15573 messages and mini-buffers, and we don't handle that here. */
15574 if (MINI_WINDOW_P (w))
15575 GIVE_UP (1);
15576
15577 /* This flag is used to prevent redisplay optimizations. */
15578 if (windows_or_buffers_changed || cursor_type_changed)
15579 GIVE_UP (2);
15580
15581 /* Verify that narrowing has not changed.
15582 Also verify that we were not told to prevent redisplay optimizations.
15583 It would be nice to further
15584 reduce the number of cases where this prevents try_window_id. */
15585 if (current_buffer->clip_changed
15586 || current_buffer->prevent_redisplay_optimizations_p)
15587 GIVE_UP (3);
15588
15589 /* Window must either use window-based redisplay or be full width. */
15590 if (!FRAME_WINDOW_P (f)
15591 && (!FRAME_LINE_INS_DEL_OK (f)
15592 || !WINDOW_FULL_WIDTH_P (w)))
15593 GIVE_UP (4);
15594
15595 /* Give up if point is known NOT to appear in W. */
15596 if (PT < CHARPOS (start))
15597 GIVE_UP (5);
15598
15599 /* Another way to prevent redisplay optimizations. */
15600 if (XFASTINT (w->last_modified) == 0)
15601 GIVE_UP (6);
15602
15603 /* Verify that window is not hscrolled. */
15604 if (XFASTINT (w->hscroll) != 0)
15605 GIVE_UP (7);
15606
15607 /* Verify that display wasn't paused. */
15608 if (NILP (w->window_end_valid))
15609 GIVE_UP (8);
15610
15611 /* Can't use this if highlighting a region because a cursor movement
15612 will do more than just set the cursor. */
15613 if (!NILP (Vtransient_mark_mode)
15614 && !NILP (current_buffer->mark_active))
15615 GIVE_UP (9);
15616
15617 /* Likewise if highlighting trailing whitespace. */
15618 if (!NILP (Vshow_trailing_whitespace))
15619 GIVE_UP (11);
15620
15621 /* Likewise if showing a region. */
15622 if (!NILP (w->region_showing))
15623 GIVE_UP (10);
15624
15625 /* Can't use this if overlay arrow position and/or string have
15626 changed. */
15627 if (overlay_arrows_changed_p ())
15628 GIVE_UP (12);
15629
15630 /* When word-wrap is on, adding a space to the first word of a
15631 wrapped line can change the wrap position, altering the line
15632 above it. It might be worthwhile to handle this more
15633 intelligently, but for now just redisplay from scratch. */
15634 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15635 GIVE_UP (21);
15636
15637 /* Under bidi reordering, adding or deleting a character in the
15638 beginning of a paragraph, before the first strong directional
15639 character, can change the base direction of the paragraph (unless
15640 the buffer specifies a fixed paragraph direction), which will
15641 require to redisplay the whole paragraph. It might be worthwhile
15642 to find the paragraph limits and widen the range of redisplayed
15643 lines to that, but for now just give up this optimization and
15644 redisplay from scratch. */
15645 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15646 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15647 GIVE_UP (22);
15648
15649 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15650 only if buffer has really changed. The reason is that the gap is
15651 initially at Z for freshly visited files. The code below would
15652 set end_unchanged to 0 in that case. */
15653 if (MODIFF > SAVE_MODIFF
15654 /* This seems to happen sometimes after saving a buffer. */
15655 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15656 {
15657 if (GPT - BEG < BEG_UNCHANGED)
15658 BEG_UNCHANGED = GPT - BEG;
15659 if (Z - GPT < END_UNCHANGED)
15660 END_UNCHANGED = Z - GPT;
15661 }
15662
15663 /* The position of the first and last character that has been changed. */
15664 first_changed_charpos = BEG + BEG_UNCHANGED;
15665 last_changed_charpos = Z - END_UNCHANGED;
15666
15667 /* If window starts after a line end, and the last change is in
15668 front of that newline, then changes don't affect the display.
15669 This case happens with stealth-fontification. Note that although
15670 the display is unchanged, glyph positions in the matrix have to
15671 be adjusted, of course. */
15672 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15673 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15674 && ((last_changed_charpos < CHARPOS (start)
15675 && CHARPOS (start) == BEGV)
15676 || (last_changed_charpos < CHARPOS (start) - 1
15677 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15678 {
15679 EMACS_INT Z_old, delta, Z_BYTE_old, delta_bytes;
15680 struct glyph_row *r0;
15681
15682 /* Compute how many chars/bytes have been added to or removed
15683 from the buffer. */
15684 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15685 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15686 delta = Z - Z_old;
15687 delta_bytes = Z_BYTE - Z_BYTE_old;
15688
15689 /* Give up if PT is not in the window. Note that it already has
15690 been checked at the start of try_window_id that PT is not in
15691 front of the window start. */
15692 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15693 GIVE_UP (13);
15694
15695 /* If window start is unchanged, we can reuse the whole matrix
15696 as is, after adjusting glyph positions. No need to compute
15697 the window end again, since its offset from Z hasn't changed. */
15698 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15699 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15700 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15701 /* PT must not be in a partially visible line. */
15702 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15703 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15704 {
15705 /* Adjust positions in the glyph matrix. */
15706 if (delta || delta_bytes)
15707 {
15708 struct glyph_row *r1
15709 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15710 increment_matrix_positions (w->current_matrix,
15711 MATRIX_ROW_VPOS (r0, current_matrix),
15712 MATRIX_ROW_VPOS (r1, current_matrix),
15713 delta, delta_bytes);
15714 }
15715
15716 /* Set the cursor. */
15717 row = row_containing_pos (w, PT, r0, NULL, 0);
15718 if (row)
15719 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15720 else
15721 abort ();
15722 return 1;
15723 }
15724 }
15725
15726 /* Handle the case that changes are all below what is displayed in
15727 the window, and that PT is in the window. This shortcut cannot
15728 be taken if ZV is visible in the window, and text has been added
15729 there that is visible in the window. */
15730 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15731 /* ZV is not visible in the window, or there are no
15732 changes at ZV, actually. */
15733 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15734 || first_changed_charpos == last_changed_charpos))
15735 {
15736 struct glyph_row *r0;
15737
15738 /* Give up if PT is not in the window. Note that it already has
15739 been checked at the start of try_window_id that PT is not in
15740 front of the window start. */
15741 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15742 GIVE_UP (14);
15743
15744 /* If window start is unchanged, we can reuse the whole matrix
15745 as is, without changing glyph positions since no text has
15746 been added/removed in front of the window end. */
15747 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15748 if (TEXT_POS_EQUAL_P (start, r0->minpos)
15749 /* PT must not be in a partially visible line. */
15750 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15751 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15752 {
15753 /* We have to compute the window end anew since text
15754 could have been added/removed after it. */
15755 w->window_end_pos
15756 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15757 w->window_end_bytepos
15758 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15759
15760 /* Set the cursor. */
15761 row = row_containing_pos (w, PT, r0, NULL, 0);
15762 if (row)
15763 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15764 else
15765 abort ();
15766 return 2;
15767 }
15768 }
15769
15770 /* Give up if window start is in the changed area.
15771
15772 The condition used to read
15773
15774 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15775
15776 but why that was tested escapes me at the moment. */
15777 if (CHARPOS (start) >= first_changed_charpos
15778 && CHARPOS (start) <= last_changed_charpos)
15779 GIVE_UP (15);
15780
15781 /* Check that window start agrees with the start of the first glyph
15782 row in its current matrix. Check this after we know the window
15783 start is not in changed text, otherwise positions would not be
15784 comparable. */
15785 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15786 if (!TEXT_POS_EQUAL_P (start, row->minpos))
15787 GIVE_UP (16);
15788
15789 /* Give up if the window ends in strings. Overlay strings
15790 at the end are difficult to handle, so don't try. */
15791 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15792 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15793 GIVE_UP (20);
15794
15795 /* Compute the position at which we have to start displaying new
15796 lines. Some of the lines at the top of the window might be
15797 reusable because they are not displaying changed text. Find the
15798 last row in W's current matrix not affected by changes at the
15799 start of current_buffer. Value is null if changes start in the
15800 first line of window. */
15801 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15802 if (last_unchanged_at_beg_row)
15803 {
15804 /* Avoid starting to display in the moddle of a character, a TAB
15805 for instance. This is easier than to set up the iterator
15806 exactly, and it's not a frequent case, so the additional
15807 effort wouldn't really pay off. */
15808 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15809 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15810 && last_unchanged_at_beg_row > w->current_matrix->rows)
15811 --last_unchanged_at_beg_row;
15812
15813 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15814 GIVE_UP (17);
15815
15816 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15817 GIVE_UP (18);
15818 start_pos = it.current.pos;
15819
15820 /* Start displaying new lines in the desired matrix at the same
15821 vpos we would use in the current matrix, i.e. below
15822 last_unchanged_at_beg_row. */
15823 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15824 current_matrix);
15825 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15826 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15827
15828 xassert (it.hpos == 0 && it.current_x == 0);
15829 }
15830 else
15831 {
15832 /* There are no reusable lines at the start of the window.
15833 Start displaying in the first text line. */
15834 start_display (&it, w, start);
15835 it.vpos = it.first_vpos;
15836 start_pos = it.current.pos;
15837 }
15838
15839 /* Find the first row that is not affected by changes at the end of
15840 the buffer. Value will be null if there is no unchanged row, in
15841 which case we must redisplay to the end of the window. delta
15842 will be set to the value by which buffer positions beginning with
15843 first_unchanged_at_end_row have to be adjusted due to text
15844 changes. */
15845 first_unchanged_at_end_row
15846 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15847 IF_DEBUG (debug_delta = delta);
15848 IF_DEBUG (debug_delta_bytes = delta_bytes);
15849
15850 /* Set stop_pos to the buffer position up to which we will have to
15851 display new lines. If first_unchanged_at_end_row != NULL, this
15852 is the buffer position of the start of the line displayed in that
15853 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15854 that we don't stop at a buffer position. */
15855 stop_pos = 0;
15856 if (first_unchanged_at_end_row)
15857 {
15858 xassert (last_unchanged_at_beg_row == NULL
15859 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15860
15861 /* If this is a continuation line, move forward to the next one
15862 that isn't. Changes in lines above affect this line.
15863 Caution: this may move first_unchanged_at_end_row to a row
15864 not displaying text. */
15865 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15866 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15867 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15868 < it.last_visible_y))
15869 ++first_unchanged_at_end_row;
15870
15871 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15872 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15873 >= it.last_visible_y))
15874 first_unchanged_at_end_row = NULL;
15875 else
15876 {
15877 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15878 + delta);
15879 first_unchanged_at_end_vpos
15880 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15881 xassert (stop_pos >= Z - END_UNCHANGED);
15882 }
15883 }
15884 else if (last_unchanged_at_beg_row == NULL)
15885 GIVE_UP (19);
15886
15887
15888 #if GLYPH_DEBUG
15889
15890 /* Either there is no unchanged row at the end, or the one we have
15891 now displays text. This is a necessary condition for the window
15892 end pos calculation at the end of this function. */
15893 xassert (first_unchanged_at_end_row == NULL
15894 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15895
15896 debug_last_unchanged_at_beg_vpos
15897 = (last_unchanged_at_beg_row
15898 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15899 : -1);
15900 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15901
15902 #endif /* GLYPH_DEBUG != 0 */
15903
15904
15905 /* Display new lines. Set last_text_row to the last new line
15906 displayed which has text on it, i.e. might end up as being the
15907 line where the window_end_vpos is. */
15908 w->cursor.vpos = -1;
15909 last_text_row = NULL;
15910 overlay_arrow_seen = 0;
15911 while (it.current_y < it.last_visible_y
15912 && !fonts_changed_p
15913 && (first_unchanged_at_end_row == NULL
15914 || IT_CHARPOS (it) < stop_pos))
15915 {
15916 if (display_line (&it))
15917 last_text_row = it.glyph_row - 1;
15918 }
15919
15920 if (fonts_changed_p)
15921 return -1;
15922
15923
15924 /* Compute differences in buffer positions, y-positions etc. for
15925 lines reused at the bottom of the window. Compute what we can
15926 scroll. */
15927 if (first_unchanged_at_end_row
15928 /* No lines reused because we displayed everything up to the
15929 bottom of the window. */
15930 && it.current_y < it.last_visible_y)
15931 {
15932 dvpos = (it.vpos
15933 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15934 current_matrix));
15935 dy = it.current_y - first_unchanged_at_end_row->y;
15936 run.current_y = first_unchanged_at_end_row->y;
15937 run.desired_y = run.current_y + dy;
15938 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15939 }
15940 else
15941 {
15942 delta = delta_bytes = dvpos = dy
15943 = run.current_y = run.desired_y = run.height = 0;
15944 first_unchanged_at_end_row = NULL;
15945 }
15946 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15947
15948
15949 /* Find the cursor if not already found. We have to decide whether
15950 PT will appear on this window (it sometimes doesn't, but this is
15951 not a very frequent case.) This decision has to be made before
15952 the current matrix is altered. A value of cursor.vpos < 0 means
15953 that PT is either in one of the lines beginning at
15954 first_unchanged_at_end_row or below the window. Don't care for
15955 lines that might be displayed later at the window end; as
15956 mentioned, this is not a frequent case. */
15957 if (w->cursor.vpos < 0)
15958 {
15959 /* Cursor in unchanged rows at the top? */
15960 if (PT < CHARPOS (start_pos)
15961 && last_unchanged_at_beg_row)
15962 {
15963 row = row_containing_pos (w, PT,
15964 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15965 last_unchanged_at_beg_row + 1, 0);
15966 if (row)
15967 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15968 }
15969
15970 /* Start from first_unchanged_at_end_row looking for PT. */
15971 else if (first_unchanged_at_end_row)
15972 {
15973 row = row_containing_pos (w, PT - delta,
15974 first_unchanged_at_end_row, NULL, 0);
15975 if (row)
15976 set_cursor_from_row (w, row, w->current_matrix, delta,
15977 delta_bytes, dy, dvpos);
15978 }
15979
15980 /* Give up if cursor was not found. */
15981 if (w->cursor.vpos < 0)
15982 {
15983 clear_glyph_matrix (w->desired_matrix);
15984 return -1;
15985 }
15986 }
15987
15988 /* Don't let the cursor end in the scroll margins. */
15989 {
15990 int this_scroll_margin, cursor_height;
15991
15992 this_scroll_margin = max (0, scroll_margin);
15993 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15994 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15995 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15996
15997 if ((w->cursor.y < this_scroll_margin
15998 && CHARPOS (start) > BEGV)
15999 /* Old redisplay didn't take scroll margin into account at the bottom,
16000 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16001 || (w->cursor.y + (make_cursor_line_fully_visible_p
16002 ? cursor_height + this_scroll_margin
16003 : 1)) > it.last_visible_y)
16004 {
16005 w->cursor.vpos = -1;
16006 clear_glyph_matrix (w->desired_matrix);
16007 return -1;
16008 }
16009 }
16010
16011 /* Scroll the display. Do it before changing the current matrix so
16012 that xterm.c doesn't get confused about where the cursor glyph is
16013 found. */
16014 if (dy && run.height)
16015 {
16016 update_begin (f);
16017
16018 if (FRAME_WINDOW_P (f))
16019 {
16020 FRAME_RIF (f)->update_window_begin_hook (w);
16021 FRAME_RIF (f)->clear_window_mouse_face (w);
16022 FRAME_RIF (f)->scroll_run_hook (w, &run);
16023 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16024 }
16025 else
16026 {
16027 /* Terminal frame. In this case, dvpos gives the number of
16028 lines to scroll by; dvpos < 0 means scroll up. */
16029 int first_unchanged_at_end_vpos
16030 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16031 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
16032 int end = (WINDOW_TOP_EDGE_LINE (w)
16033 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16034 + window_internal_height (w));
16035
16036 #if defined (HAVE_GPM) || defined (MSDOS)
16037 x_clear_window_mouse_face (w);
16038 #endif
16039 /* Perform the operation on the screen. */
16040 if (dvpos > 0)
16041 {
16042 /* Scroll last_unchanged_at_beg_row to the end of the
16043 window down dvpos lines. */
16044 set_terminal_window (f, end);
16045
16046 /* On dumb terminals delete dvpos lines at the end
16047 before inserting dvpos empty lines. */
16048 if (!FRAME_SCROLL_REGION_OK (f))
16049 ins_del_lines (f, end - dvpos, -dvpos);
16050
16051 /* Insert dvpos empty lines in front of
16052 last_unchanged_at_beg_row. */
16053 ins_del_lines (f, from, dvpos);
16054 }
16055 else if (dvpos < 0)
16056 {
16057 /* Scroll up last_unchanged_at_beg_vpos to the end of
16058 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16059 set_terminal_window (f, end);
16060
16061 /* Delete dvpos lines in front of
16062 last_unchanged_at_beg_vpos. ins_del_lines will set
16063 the cursor to the given vpos and emit |dvpos| delete
16064 line sequences. */
16065 ins_del_lines (f, from + dvpos, dvpos);
16066
16067 /* On a dumb terminal insert dvpos empty lines at the
16068 end. */
16069 if (!FRAME_SCROLL_REGION_OK (f))
16070 ins_del_lines (f, end + dvpos, -dvpos);
16071 }
16072
16073 set_terminal_window (f, 0);
16074 }
16075
16076 update_end (f);
16077 }
16078
16079 /* Shift reused rows of the current matrix to the right position.
16080 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16081 text. */
16082 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16083 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16084 if (dvpos < 0)
16085 {
16086 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16087 bottom_vpos, dvpos);
16088 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16089 bottom_vpos, 0);
16090 }
16091 else if (dvpos > 0)
16092 {
16093 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16094 bottom_vpos, dvpos);
16095 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16096 first_unchanged_at_end_vpos + dvpos, 0);
16097 }
16098
16099 /* For frame-based redisplay, make sure that current frame and window
16100 matrix are in sync with respect to glyph memory. */
16101 if (!FRAME_WINDOW_P (f))
16102 sync_frame_with_window_matrix_rows (w);
16103
16104 /* Adjust buffer positions in reused rows. */
16105 if (delta || delta_bytes)
16106 increment_matrix_positions (current_matrix,
16107 first_unchanged_at_end_vpos + dvpos,
16108 bottom_vpos, delta, delta_bytes);
16109
16110 /* Adjust Y positions. */
16111 if (dy)
16112 shift_glyph_matrix (w, current_matrix,
16113 first_unchanged_at_end_vpos + dvpos,
16114 bottom_vpos, dy);
16115
16116 if (first_unchanged_at_end_row)
16117 {
16118 first_unchanged_at_end_row += dvpos;
16119 if (first_unchanged_at_end_row->y >= it.last_visible_y
16120 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16121 first_unchanged_at_end_row = NULL;
16122 }
16123
16124 /* If scrolling up, there may be some lines to display at the end of
16125 the window. */
16126 last_text_row_at_end = NULL;
16127 if (dy < 0)
16128 {
16129 /* Scrolling up can leave for example a partially visible line
16130 at the end of the window to be redisplayed. */
16131 /* Set last_row to the glyph row in the current matrix where the
16132 window end line is found. It has been moved up or down in
16133 the matrix by dvpos. */
16134 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16135 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16136
16137 /* If last_row is the window end line, it should display text. */
16138 xassert (last_row->displays_text_p);
16139
16140 /* If window end line was partially visible before, begin
16141 displaying at that line. Otherwise begin displaying with the
16142 line following it. */
16143 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16144 {
16145 init_to_row_start (&it, w, last_row);
16146 it.vpos = last_vpos;
16147 it.current_y = last_row->y;
16148 }
16149 else
16150 {
16151 init_to_row_end (&it, w, last_row);
16152 it.vpos = 1 + last_vpos;
16153 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16154 ++last_row;
16155 }
16156
16157 /* We may start in a continuation line. If so, we have to
16158 get the right continuation_lines_width and current_x. */
16159 it.continuation_lines_width = last_row->continuation_lines_width;
16160 it.hpos = it.current_x = 0;
16161
16162 /* Display the rest of the lines at the window end. */
16163 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16164 while (it.current_y < it.last_visible_y
16165 && !fonts_changed_p)
16166 {
16167 /* Is it always sure that the display agrees with lines in
16168 the current matrix? I don't think so, so we mark rows
16169 displayed invalid in the current matrix by setting their
16170 enabled_p flag to zero. */
16171 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16172 if (display_line (&it))
16173 last_text_row_at_end = it.glyph_row - 1;
16174 }
16175 }
16176
16177 /* Update window_end_pos and window_end_vpos. */
16178 if (first_unchanged_at_end_row
16179 && !last_text_row_at_end)
16180 {
16181 /* Window end line if one of the preserved rows from the current
16182 matrix. Set row to the last row displaying text in current
16183 matrix starting at first_unchanged_at_end_row, after
16184 scrolling. */
16185 xassert (first_unchanged_at_end_row->displays_text_p);
16186 row = find_last_row_displaying_text (w->current_matrix, &it,
16187 first_unchanged_at_end_row);
16188 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16189
16190 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16191 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16192 w->window_end_vpos
16193 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16194 xassert (w->window_end_bytepos >= 0);
16195 IF_DEBUG (debug_method_add (w, "A"));
16196 }
16197 else if (last_text_row_at_end)
16198 {
16199 w->window_end_pos
16200 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16201 w->window_end_bytepos
16202 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16203 w->window_end_vpos
16204 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16205 xassert (w->window_end_bytepos >= 0);
16206 IF_DEBUG (debug_method_add (w, "B"));
16207 }
16208 else if (last_text_row)
16209 {
16210 /* We have displayed either to the end of the window or at the
16211 end of the window, i.e. the last row with text is to be found
16212 in the desired matrix. */
16213 w->window_end_pos
16214 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16215 w->window_end_bytepos
16216 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16217 w->window_end_vpos
16218 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16219 xassert (w->window_end_bytepos >= 0);
16220 }
16221 else if (first_unchanged_at_end_row == NULL
16222 && last_text_row == NULL
16223 && last_text_row_at_end == NULL)
16224 {
16225 /* Displayed to end of window, but no line containing text was
16226 displayed. Lines were deleted at the end of the window. */
16227 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16228 int vpos = XFASTINT (w->window_end_vpos);
16229 struct glyph_row *current_row = current_matrix->rows + vpos;
16230 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16231
16232 for (row = NULL;
16233 row == NULL && vpos >= first_vpos;
16234 --vpos, --current_row, --desired_row)
16235 {
16236 if (desired_row->enabled_p)
16237 {
16238 if (desired_row->displays_text_p)
16239 row = desired_row;
16240 }
16241 else if (current_row->displays_text_p)
16242 row = current_row;
16243 }
16244
16245 xassert (row != NULL);
16246 w->window_end_vpos = make_number (vpos + 1);
16247 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16248 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16249 xassert (w->window_end_bytepos >= 0);
16250 IF_DEBUG (debug_method_add (w, "C"));
16251 }
16252 else
16253 abort ();
16254
16255 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16256 debug_end_vpos = XFASTINT (w->window_end_vpos));
16257
16258 /* Record that display has not been completed. */
16259 w->window_end_valid = Qnil;
16260 w->desired_matrix->no_scrolling_p = 1;
16261 return 3;
16262
16263 #undef GIVE_UP
16264 }
16265
16266
16267 \f
16268 /***********************************************************************
16269 More debugging support
16270 ***********************************************************************/
16271
16272 #if GLYPH_DEBUG
16273
16274 void dump_glyph_row (struct glyph_row *, int, int);
16275 void dump_glyph_matrix (struct glyph_matrix *, int);
16276 void dump_glyph (struct glyph_row *, struct glyph *, int);
16277
16278
16279 /* Dump the contents of glyph matrix MATRIX on stderr.
16280
16281 GLYPHS 0 means don't show glyph contents.
16282 GLYPHS 1 means show glyphs in short form
16283 GLYPHS > 1 means show glyphs in long form. */
16284
16285 void
16286 dump_glyph_matrix (matrix, glyphs)
16287 struct glyph_matrix *matrix;
16288 int glyphs;
16289 {
16290 int i;
16291 for (i = 0; i < matrix->nrows; ++i)
16292 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16293 }
16294
16295
16296 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16297 the glyph row and area where the glyph comes from. */
16298
16299 void
16300 dump_glyph (row, glyph, area)
16301 struct glyph_row *row;
16302 struct glyph *glyph;
16303 int area;
16304 {
16305 if (glyph->type == CHAR_GLYPH)
16306 {
16307 fprintf (stderr,
16308 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16309 glyph - row->glyphs[TEXT_AREA],
16310 'C',
16311 glyph->charpos,
16312 (BUFFERP (glyph->object)
16313 ? 'B'
16314 : (STRINGP (glyph->object)
16315 ? 'S'
16316 : '-')),
16317 glyph->pixel_width,
16318 glyph->u.ch,
16319 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16320 ? glyph->u.ch
16321 : '.'),
16322 glyph->face_id,
16323 glyph->left_box_line_p,
16324 glyph->right_box_line_p);
16325 }
16326 else if (glyph->type == STRETCH_GLYPH)
16327 {
16328 fprintf (stderr,
16329 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16330 glyph - row->glyphs[TEXT_AREA],
16331 'S',
16332 glyph->charpos,
16333 (BUFFERP (glyph->object)
16334 ? 'B'
16335 : (STRINGP (glyph->object)
16336 ? 'S'
16337 : '-')),
16338 glyph->pixel_width,
16339 0,
16340 '.',
16341 glyph->face_id,
16342 glyph->left_box_line_p,
16343 glyph->right_box_line_p);
16344 }
16345 else if (glyph->type == IMAGE_GLYPH)
16346 {
16347 fprintf (stderr,
16348 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16349 glyph - row->glyphs[TEXT_AREA],
16350 'I',
16351 glyph->charpos,
16352 (BUFFERP (glyph->object)
16353 ? 'B'
16354 : (STRINGP (glyph->object)
16355 ? 'S'
16356 : '-')),
16357 glyph->pixel_width,
16358 glyph->u.img_id,
16359 '.',
16360 glyph->face_id,
16361 glyph->left_box_line_p,
16362 glyph->right_box_line_p);
16363 }
16364 else if (glyph->type == COMPOSITE_GLYPH)
16365 {
16366 fprintf (stderr,
16367 " %5d %4c %6d %c %3d 0x%05x",
16368 glyph - row->glyphs[TEXT_AREA],
16369 '+',
16370 glyph->charpos,
16371 (BUFFERP (glyph->object)
16372 ? 'B'
16373 : (STRINGP (glyph->object)
16374 ? 'S'
16375 : '-')),
16376 glyph->pixel_width,
16377 glyph->u.cmp.id);
16378 if (glyph->u.cmp.automatic)
16379 fprintf (stderr,
16380 "[%d-%d]",
16381 glyph->slice.cmp.from, glyph->slice.cmp.to);
16382 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16383 glyph->face_id,
16384 glyph->left_box_line_p,
16385 glyph->right_box_line_p);
16386 }
16387 }
16388
16389
16390 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16391 GLYPHS 0 means don't show glyph contents.
16392 GLYPHS 1 means show glyphs in short form
16393 GLYPHS > 1 means show glyphs in long form. */
16394
16395 void
16396 dump_glyph_row (row, vpos, glyphs)
16397 struct glyph_row *row;
16398 int vpos, glyphs;
16399 {
16400 if (glyphs != 1)
16401 {
16402 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16403 fprintf (stderr, "======================================================================\n");
16404
16405 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16406 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16407 vpos,
16408 MATRIX_ROW_START_CHARPOS (row),
16409 MATRIX_ROW_END_CHARPOS (row),
16410 row->used[TEXT_AREA],
16411 row->contains_overlapping_glyphs_p,
16412 row->enabled_p,
16413 row->truncated_on_left_p,
16414 row->truncated_on_right_p,
16415 row->continued_p,
16416 MATRIX_ROW_CONTINUATION_LINE_P (row),
16417 row->displays_text_p,
16418 row->ends_at_zv_p,
16419 row->fill_line_p,
16420 row->ends_in_middle_of_char_p,
16421 row->starts_in_middle_of_char_p,
16422 row->mouse_face_p,
16423 row->x,
16424 row->y,
16425 row->pixel_width,
16426 row->height,
16427 row->visible_height,
16428 row->ascent,
16429 row->phys_ascent);
16430 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16431 row->end.overlay_string_index,
16432 row->continuation_lines_width);
16433 fprintf (stderr, "%9d %5d\n",
16434 CHARPOS (row->start.string_pos),
16435 CHARPOS (row->end.string_pos));
16436 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16437 row->end.dpvec_index);
16438 }
16439
16440 if (glyphs > 1)
16441 {
16442 int area;
16443
16444 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16445 {
16446 struct glyph *glyph = row->glyphs[area];
16447 struct glyph *glyph_end = glyph + row->used[area];
16448
16449 /* Glyph for a line end in text. */
16450 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16451 ++glyph_end;
16452
16453 if (glyph < glyph_end)
16454 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16455
16456 for (; glyph < glyph_end; ++glyph)
16457 dump_glyph (row, glyph, area);
16458 }
16459 }
16460 else if (glyphs == 1)
16461 {
16462 int area;
16463
16464 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16465 {
16466 char *s = (char *) alloca (row->used[area] + 1);
16467 int i;
16468
16469 for (i = 0; i < row->used[area]; ++i)
16470 {
16471 struct glyph *glyph = row->glyphs[area] + i;
16472 if (glyph->type == CHAR_GLYPH
16473 && glyph->u.ch < 0x80
16474 && glyph->u.ch >= ' ')
16475 s[i] = glyph->u.ch;
16476 else
16477 s[i] = '.';
16478 }
16479
16480 s[i] = '\0';
16481 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16482 }
16483 }
16484 }
16485
16486
16487 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16488 Sdump_glyph_matrix, 0, 1, "p",
16489 doc: /* Dump the current matrix of the selected window to stderr.
16490 Shows contents of glyph row structures. With non-nil
16491 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16492 glyphs in short form, otherwise show glyphs in long form. */)
16493 (Lisp_Object glyphs)
16494 {
16495 struct window *w = XWINDOW (selected_window);
16496 struct buffer *buffer = XBUFFER (w->buffer);
16497
16498 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16499 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16500 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16501 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16502 fprintf (stderr, "=============================================\n");
16503 dump_glyph_matrix (w->current_matrix,
16504 NILP (glyphs) ? 0 : XINT (glyphs));
16505 return Qnil;
16506 }
16507
16508
16509 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16510 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16511 (void)
16512 {
16513 struct frame *f = XFRAME (selected_frame);
16514 dump_glyph_matrix (f->current_matrix, 1);
16515 return Qnil;
16516 }
16517
16518
16519 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16520 doc: /* Dump glyph row ROW to stderr.
16521 GLYPH 0 means don't dump glyphs.
16522 GLYPH 1 means dump glyphs in short form.
16523 GLYPH > 1 or omitted means dump glyphs in long form. */)
16524 (Lisp_Object row, Lisp_Object glyphs)
16525 {
16526 struct glyph_matrix *matrix;
16527 int vpos;
16528
16529 CHECK_NUMBER (row);
16530 matrix = XWINDOW (selected_window)->current_matrix;
16531 vpos = XINT (row);
16532 if (vpos >= 0 && vpos < matrix->nrows)
16533 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16534 vpos,
16535 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16536 return Qnil;
16537 }
16538
16539
16540 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16541 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16542 GLYPH 0 means don't dump glyphs.
16543 GLYPH 1 means dump glyphs in short form.
16544 GLYPH > 1 or omitted means dump glyphs in long form. */)
16545 (Lisp_Object row, Lisp_Object glyphs)
16546 {
16547 struct frame *sf = SELECTED_FRAME ();
16548 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16549 int vpos;
16550
16551 CHECK_NUMBER (row);
16552 vpos = XINT (row);
16553 if (vpos >= 0 && vpos < m->nrows)
16554 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16555 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16556 return Qnil;
16557 }
16558
16559
16560 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16561 doc: /* Toggle tracing of redisplay.
16562 With ARG, turn tracing on if and only if ARG is positive. */)
16563 (Lisp_Object arg)
16564 {
16565 if (NILP (arg))
16566 trace_redisplay_p = !trace_redisplay_p;
16567 else
16568 {
16569 arg = Fprefix_numeric_value (arg);
16570 trace_redisplay_p = XINT (arg) > 0;
16571 }
16572
16573 return Qnil;
16574 }
16575
16576
16577 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16578 doc: /* Like `format', but print result to stderr.
16579 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16580 (int nargs, Lisp_Object *args)
16581 {
16582 Lisp_Object s = Fformat (nargs, args);
16583 fprintf (stderr, "%s", SDATA (s));
16584 return Qnil;
16585 }
16586
16587 #endif /* GLYPH_DEBUG */
16588
16589
16590 \f
16591 /***********************************************************************
16592 Building Desired Matrix Rows
16593 ***********************************************************************/
16594
16595 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16596 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16597
16598 static struct glyph_row *
16599 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16600 {
16601 struct frame *f = XFRAME (WINDOW_FRAME (w));
16602 struct buffer *buffer = XBUFFER (w->buffer);
16603 struct buffer *old = current_buffer;
16604 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16605 int arrow_len = SCHARS (overlay_arrow_string);
16606 const unsigned char *arrow_end = arrow_string + arrow_len;
16607 const unsigned char *p;
16608 struct it it;
16609 int multibyte_p;
16610 int n_glyphs_before;
16611
16612 set_buffer_temp (buffer);
16613 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16614 it.glyph_row->used[TEXT_AREA] = 0;
16615 SET_TEXT_POS (it.position, 0, 0);
16616
16617 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16618 p = arrow_string;
16619 while (p < arrow_end)
16620 {
16621 Lisp_Object face, ilisp;
16622
16623 /* Get the next character. */
16624 if (multibyte_p)
16625 it.c = it.char_to_display = string_char_and_length (p, &it.len);
16626 else
16627 {
16628 it.c = it.char_to_display = *p, it.len = 1;
16629 if (! ASCII_CHAR_P (it.c))
16630 it.char_to_display = BYTE8_TO_CHAR (it.c);
16631 }
16632 p += it.len;
16633
16634 /* Get its face. */
16635 ilisp = make_number (p - arrow_string);
16636 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16637 it.face_id = compute_char_face (f, it.char_to_display, face);
16638
16639 /* Compute its width, get its glyphs. */
16640 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16641 SET_TEXT_POS (it.position, -1, -1);
16642 PRODUCE_GLYPHS (&it);
16643
16644 /* If this character doesn't fit any more in the line, we have
16645 to remove some glyphs. */
16646 if (it.current_x > it.last_visible_x)
16647 {
16648 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16649 break;
16650 }
16651 }
16652
16653 set_buffer_temp (old);
16654 return it.glyph_row;
16655 }
16656
16657
16658 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16659 glyphs are only inserted for terminal frames since we can't really
16660 win with truncation glyphs when partially visible glyphs are
16661 involved. Which glyphs to insert is determined by
16662 produce_special_glyphs. */
16663
16664 static void
16665 insert_left_trunc_glyphs (struct it *it)
16666 {
16667 struct it truncate_it;
16668 struct glyph *from, *end, *to, *toend;
16669
16670 xassert (!FRAME_WINDOW_P (it->f));
16671
16672 /* Get the truncation glyphs. */
16673 truncate_it = *it;
16674 truncate_it.current_x = 0;
16675 truncate_it.face_id = DEFAULT_FACE_ID;
16676 truncate_it.glyph_row = &scratch_glyph_row;
16677 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16678 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16679 truncate_it.object = make_number (0);
16680 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16681
16682 /* Overwrite glyphs from IT with truncation glyphs. */
16683 if (!it->glyph_row->reversed_p)
16684 {
16685 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16686 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16687 to = it->glyph_row->glyphs[TEXT_AREA];
16688 toend = to + it->glyph_row->used[TEXT_AREA];
16689
16690 while (from < end)
16691 *to++ = *from++;
16692
16693 /* There may be padding glyphs left over. Overwrite them too. */
16694 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16695 {
16696 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16697 while (from < end)
16698 *to++ = *from++;
16699 }
16700
16701 if (to > toend)
16702 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16703 }
16704 else
16705 {
16706 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
16707 that back to front. */
16708 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
16709 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16710 toend = it->glyph_row->glyphs[TEXT_AREA];
16711 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
16712
16713 while (from >= end && to >= toend)
16714 *to-- = *from--;
16715 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
16716 {
16717 from =
16718 truncate_it.glyph_row->glyphs[TEXT_AREA]
16719 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16720 while (from >= end && to >= toend)
16721 *to-- = *from--;
16722 }
16723 if (from >= end)
16724 {
16725 /* Need to free some room before prepending additional
16726 glyphs. */
16727 int move_by = from - end + 1;
16728 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
16729 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
16730
16731 for ( ; g >= g0; g--)
16732 g[move_by] = *g;
16733 while (from >= end)
16734 *to-- = *from--;
16735 it->glyph_row->used[TEXT_AREA] += move_by;
16736 }
16737 }
16738 }
16739
16740
16741 /* Compute the pixel height and width of IT->glyph_row.
16742
16743 Most of the time, ascent and height of a display line will be equal
16744 to the max_ascent and max_height values of the display iterator
16745 structure. This is not the case if
16746
16747 1. We hit ZV without displaying anything. In this case, max_ascent
16748 and max_height will be zero.
16749
16750 2. We have some glyphs that don't contribute to the line height.
16751 (The glyph row flag contributes_to_line_height_p is for future
16752 pixmap extensions).
16753
16754 The first case is easily covered by using default values because in
16755 these cases, the line height does not really matter, except that it
16756 must not be zero. */
16757
16758 static void
16759 compute_line_metrics (struct it *it)
16760 {
16761 struct glyph_row *row = it->glyph_row;
16762 int area, i;
16763
16764 if (FRAME_WINDOW_P (it->f))
16765 {
16766 int i, min_y, max_y;
16767
16768 /* The line may consist of one space only, that was added to
16769 place the cursor on it. If so, the row's height hasn't been
16770 computed yet. */
16771 if (row->height == 0)
16772 {
16773 if (it->max_ascent + it->max_descent == 0)
16774 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16775 row->ascent = it->max_ascent;
16776 row->height = it->max_ascent + it->max_descent;
16777 row->phys_ascent = it->max_phys_ascent;
16778 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16779 row->extra_line_spacing = it->max_extra_line_spacing;
16780 }
16781
16782 /* Compute the width of this line. */
16783 row->pixel_width = row->x;
16784 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16785 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16786
16787 xassert (row->pixel_width >= 0);
16788 xassert (row->ascent >= 0 && row->height > 0);
16789
16790 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16791 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16792
16793 /* If first line's physical ascent is larger than its logical
16794 ascent, use the physical ascent, and make the row taller.
16795 This makes accented characters fully visible. */
16796 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16797 && row->phys_ascent > row->ascent)
16798 {
16799 row->height += row->phys_ascent - row->ascent;
16800 row->ascent = row->phys_ascent;
16801 }
16802
16803 /* Compute how much of the line is visible. */
16804 row->visible_height = row->height;
16805
16806 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16807 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16808
16809 if (row->y < min_y)
16810 row->visible_height -= min_y - row->y;
16811 if (row->y + row->height > max_y)
16812 row->visible_height -= row->y + row->height - max_y;
16813 }
16814 else
16815 {
16816 row->pixel_width = row->used[TEXT_AREA];
16817 if (row->continued_p)
16818 row->pixel_width -= it->continuation_pixel_width;
16819 else if (row->truncated_on_right_p)
16820 row->pixel_width -= it->truncation_pixel_width;
16821 row->ascent = row->phys_ascent = 0;
16822 row->height = row->phys_height = row->visible_height = 1;
16823 row->extra_line_spacing = 0;
16824 }
16825
16826 /* Compute a hash code for this row. */
16827 row->hash = 0;
16828 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16829 for (i = 0; i < row->used[area]; ++i)
16830 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16831 + row->glyphs[area][i].u.val
16832 + row->glyphs[area][i].face_id
16833 + row->glyphs[area][i].padding_p
16834 + (row->glyphs[area][i].type << 2));
16835
16836 it->max_ascent = it->max_descent = 0;
16837 it->max_phys_ascent = it->max_phys_descent = 0;
16838 }
16839
16840
16841 /* Append one space to the glyph row of iterator IT if doing a
16842 window-based redisplay. The space has the same face as
16843 IT->face_id. Value is non-zero if a space was added.
16844
16845 This function is called to make sure that there is always one glyph
16846 at the end of a glyph row that the cursor can be set on under
16847 window-systems. (If there weren't such a glyph we would not know
16848 how wide and tall a box cursor should be displayed).
16849
16850 At the same time this space let's a nicely handle clearing to the
16851 end of the line if the row ends in italic text. */
16852
16853 static int
16854 append_space_for_newline (struct it *it, int default_face_p)
16855 {
16856 if (FRAME_WINDOW_P (it->f))
16857 {
16858 int n = it->glyph_row->used[TEXT_AREA];
16859
16860 if (it->glyph_row->glyphs[TEXT_AREA] + n
16861 < it->glyph_row->glyphs[1 + TEXT_AREA])
16862 {
16863 /* Save some values that must not be changed.
16864 Must save IT->c and IT->len because otherwise
16865 ITERATOR_AT_END_P wouldn't work anymore after
16866 append_space_for_newline has been called. */
16867 enum display_element_type saved_what = it->what;
16868 int saved_c = it->c, saved_len = it->len;
16869 int saved_char_to_display = it->char_to_display;
16870 int saved_x = it->current_x;
16871 int saved_face_id = it->face_id;
16872 struct text_pos saved_pos;
16873 Lisp_Object saved_object;
16874 struct face *face;
16875
16876 saved_object = it->object;
16877 saved_pos = it->position;
16878
16879 it->what = IT_CHARACTER;
16880 memset (&it->position, 0, sizeof it->position);
16881 it->object = make_number (0);
16882 it->c = it->char_to_display = ' ';
16883 it->len = 1;
16884
16885 if (default_face_p)
16886 it->face_id = DEFAULT_FACE_ID;
16887 else if (it->face_before_selective_p)
16888 it->face_id = it->saved_face_id;
16889 face = FACE_FROM_ID (it->f, it->face_id);
16890 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16891
16892 PRODUCE_GLYPHS (it);
16893
16894 it->override_ascent = -1;
16895 it->constrain_row_ascent_descent_p = 0;
16896 it->current_x = saved_x;
16897 it->object = saved_object;
16898 it->position = saved_pos;
16899 it->what = saved_what;
16900 it->face_id = saved_face_id;
16901 it->len = saved_len;
16902 it->c = saved_c;
16903 it->char_to_display = saved_char_to_display;
16904 return 1;
16905 }
16906 }
16907
16908 return 0;
16909 }
16910
16911
16912 /* Extend the face of the last glyph in the text area of IT->glyph_row
16913 to the end of the display line. Called from display_line. If the
16914 glyph row is empty, add a space glyph to it so that we know the
16915 face to draw. Set the glyph row flag fill_line_p. If the glyph
16916 row is R2L, prepend a stretch glyph to cover the empty space to the
16917 left of the leftmost glyph. */
16918
16919 static void
16920 extend_face_to_end_of_line (struct it *it)
16921 {
16922 struct face *face;
16923 struct frame *f = it->f;
16924
16925 /* If line is already filled, do nothing. Non window-system frames
16926 get a grace of one more ``pixel'' because their characters are
16927 1-``pixel'' wide, so they hit the equality too early. This grace
16928 is needed only for R2L rows that are not continued, to produce
16929 one extra blank where we could display the cursor. */
16930 if (it->current_x >= it->last_visible_x
16931 + (!FRAME_WINDOW_P (f)
16932 && it->glyph_row->reversed_p
16933 && !it->glyph_row->continued_p))
16934 return;
16935
16936 /* Face extension extends the background and box of IT->face_id
16937 to the end of the line. If the background equals the background
16938 of the frame, we don't have to do anything. */
16939 if (it->face_before_selective_p)
16940 face = FACE_FROM_ID (f, it->saved_face_id);
16941 else
16942 face = FACE_FROM_ID (f, it->face_id);
16943
16944 if (FRAME_WINDOW_P (f)
16945 && it->glyph_row->displays_text_p
16946 && face->box == FACE_NO_BOX
16947 && face->background == FRAME_BACKGROUND_PIXEL (f)
16948 && !face->stipple
16949 && !it->glyph_row->reversed_p)
16950 return;
16951
16952 /* Set the glyph row flag indicating that the face of the last glyph
16953 in the text area has to be drawn to the end of the text area. */
16954 it->glyph_row->fill_line_p = 1;
16955
16956 /* If current character of IT is not ASCII, make sure we have the
16957 ASCII face. This will be automatically undone the next time
16958 get_next_display_element returns a multibyte character. Note
16959 that the character will always be single byte in unibyte
16960 text. */
16961 if (!ASCII_CHAR_P (it->c))
16962 {
16963 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16964 }
16965
16966 if (FRAME_WINDOW_P (f))
16967 {
16968 /* If the row is empty, add a space with the current face of IT,
16969 so that we know which face to draw. */
16970 if (it->glyph_row->used[TEXT_AREA] == 0)
16971 {
16972 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16973 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16974 it->glyph_row->used[TEXT_AREA] = 1;
16975 }
16976 #ifdef HAVE_WINDOW_SYSTEM
16977 if (it->glyph_row->reversed_p)
16978 {
16979 /* Prepend a stretch glyph to the row, such that the
16980 rightmost glyph will be drawn flushed all the way to the
16981 right margin of the window. The stretch glyph that will
16982 occupy the empty space, if any, to the left of the
16983 glyphs. */
16984 struct font *font = face->font ? face->font : FRAME_FONT (f);
16985 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
16986 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
16987 struct glyph *g;
16988 int row_width, stretch_ascent, stretch_width;
16989 struct text_pos saved_pos;
16990 int saved_face_id, saved_avoid_cursor;
16991
16992 for (row_width = 0, g = row_start; g < row_end; g++)
16993 row_width += g->pixel_width;
16994 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
16995 if (stretch_width > 0)
16996 {
16997 stretch_ascent =
16998 (((it->ascent + it->descent)
16999 * FONT_BASE (font)) / FONT_HEIGHT (font));
17000 saved_pos = it->position;
17001 memset (&it->position, 0, sizeof it->position);
17002 saved_avoid_cursor = it->avoid_cursor_p;
17003 it->avoid_cursor_p = 1;
17004 saved_face_id = it->face_id;
17005 /* The last row's stretch glyph should get the default
17006 face, to avoid painting the rest of the window with
17007 the region face, if the region ends at ZV. */
17008 if (it->glyph_row->ends_at_zv_p)
17009 it->face_id = DEFAULT_FACE_ID;
17010 else
17011 it->face_id = face->id;
17012 append_stretch_glyph (it, make_number (0), stretch_width,
17013 it->ascent + it->descent, stretch_ascent);
17014 it->position = saved_pos;
17015 it->avoid_cursor_p = saved_avoid_cursor;
17016 it->face_id = saved_face_id;
17017 }
17018 }
17019 #endif /* HAVE_WINDOW_SYSTEM */
17020 }
17021 else
17022 {
17023 /* Save some values that must not be changed. */
17024 int saved_x = it->current_x;
17025 struct text_pos saved_pos;
17026 Lisp_Object saved_object;
17027 enum display_element_type saved_what = it->what;
17028 int saved_face_id = it->face_id;
17029
17030 saved_object = it->object;
17031 saved_pos = it->position;
17032
17033 it->what = IT_CHARACTER;
17034 memset (&it->position, 0, sizeof it->position);
17035 it->object = make_number (0);
17036 it->c = it->char_to_display = ' ';
17037 it->len = 1;
17038 /* The last row's blank glyphs should get the default face, to
17039 avoid painting the rest of the window with the region face,
17040 if the region ends at ZV. */
17041 if (it->glyph_row->ends_at_zv_p)
17042 it->face_id = DEFAULT_FACE_ID;
17043 else
17044 it->face_id = face->id;
17045
17046 PRODUCE_GLYPHS (it);
17047
17048 while (it->current_x <= it->last_visible_x)
17049 PRODUCE_GLYPHS (it);
17050
17051 /* Don't count these blanks really. It would let us insert a left
17052 truncation glyph below and make us set the cursor on them, maybe. */
17053 it->current_x = saved_x;
17054 it->object = saved_object;
17055 it->position = saved_pos;
17056 it->what = saved_what;
17057 it->face_id = saved_face_id;
17058 }
17059 }
17060
17061
17062 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17063 trailing whitespace. */
17064
17065 static int
17066 trailing_whitespace_p (EMACS_INT charpos)
17067 {
17068 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17069 int c = 0;
17070
17071 while (bytepos < ZV_BYTE
17072 && (c = FETCH_CHAR (bytepos),
17073 c == ' ' || c == '\t'))
17074 ++bytepos;
17075
17076 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17077 {
17078 if (bytepos != PT_BYTE)
17079 return 1;
17080 }
17081 return 0;
17082 }
17083
17084
17085 /* Highlight trailing whitespace, if any, in ROW. */
17086
17087 void
17088 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17089 {
17090 int used = row->used[TEXT_AREA];
17091
17092 if (used)
17093 {
17094 struct glyph *start = row->glyphs[TEXT_AREA];
17095 struct glyph *glyph = start + used - 1;
17096
17097 if (row->reversed_p)
17098 {
17099 /* Right-to-left rows need to be processed in the opposite
17100 direction, so swap the edge pointers. */
17101 glyph = start;
17102 start = row->glyphs[TEXT_AREA] + used - 1;
17103 }
17104
17105 /* Skip over glyphs inserted to display the cursor at the
17106 end of a line, for extending the face of the last glyph
17107 to the end of the line on terminals, and for truncation
17108 and continuation glyphs. */
17109 if (!row->reversed_p)
17110 {
17111 while (glyph >= start
17112 && glyph->type == CHAR_GLYPH
17113 && INTEGERP (glyph->object))
17114 --glyph;
17115 }
17116 else
17117 {
17118 while (glyph <= start
17119 && glyph->type == CHAR_GLYPH
17120 && INTEGERP (glyph->object))
17121 ++glyph;
17122 }
17123
17124 /* If last glyph is a space or stretch, and it's trailing
17125 whitespace, set the face of all trailing whitespace glyphs in
17126 IT->glyph_row to `trailing-whitespace'. */
17127 if ((row->reversed_p ? glyph <= start : glyph >= start)
17128 && BUFFERP (glyph->object)
17129 && (glyph->type == STRETCH_GLYPH
17130 || (glyph->type == CHAR_GLYPH
17131 && glyph->u.ch == ' '))
17132 && trailing_whitespace_p (glyph->charpos))
17133 {
17134 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17135 if (face_id < 0)
17136 return;
17137
17138 if (!row->reversed_p)
17139 {
17140 while (glyph >= start
17141 && BUFFERP (glyph->object)
17142 && (glyph->type == STRETCH_GLYPH
17143 || (glyph->type == CHAR_GLYPH
17144 && glyph->u.ch == ' ')))
17145 (glyph--)->face_id = face_id;
17146 }
17147 else
17148 {
17149 while (glyph <= start
17150 && BUFFERP (glyph->object)
17151 && (glyph->type == STRETCH_GLYPH
17152 || (glyph->type == CHAR_GLYPH
17153 && glyph->u.ch == ' ')))
17154 (glyph++)->face_id = face_id;
17155 }
17156 }
17157 }
17158 }
17159
17160
17161 /* Value is non-zero if glyph row ROW in window W should be
17162 used to hold the cursor. */
17163
17164 static int
17165 cursor_row_p (struct window *w, struct glyph_row *row)
17166 {
17167 int cursor_row_p = 1;
17168
17169 if (PT == CHARPOS (row->end.pos))
17170 {
17171 /* Suppose the row ends on a string.
17172 Unless the row is continued, that means it ends on a newline
17173 in the string. If it's anything other than a display string
17174 (e.g. a before-string from an overlay), we don't want the
17175 cursor there. (This heuristic seems to give the optimal
17176 behavior for the various types of multi-line strings.) */
17177 if (CHARPOS (row->end.string_pos) >= 0)
17178 {
17179 if (row->continued_p)
17180 cursor_row_p = 1;
17181 else
17182 {
17183 /* Check for `display' property. */
17184 struct glyph *beg = row->glyphs[TEXT_AREA];
17185 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17186 struct glyph *glyph;
17187
17188 cursor_row_p = 0;
17189 for (glyph = end; glyph >= beg; --glyph)
17190 if (STRINGP (glyph->object))
17191 {
17192 Lisp_Object prop
17193 = Fget_char_property (make_number (PT),
17194 Qdisplay, Qnil);
17195 cursor_row_p =
17196 (!NILP (prop)
17197 && display_prop_string_p (prop, glyph->object));
17198 break;
17199 }
17200 }
17201 }
17202 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17203 {
17204 /* If the row ends in middle of a real character,
17205 and the line is continued, we want the cursor here.
17206 That's because CHARPOS (ROW->end.pos) would equal
17207 PT if PT is before the character. */
17208 if (!row->ends_in_ellipsis_p)
17209 cursor_row_p = row->continued_p;
17210 else
17211 /* If the row ends in an ellipsis, then
17212 CHARPOS (ROW->end.pos) will equal point after the
17213 invisible text. We want that position to be displayed
17214 after the ellipsis. */
17215 cursor_row_p = 0;
17216 }
17217 /* If the row ends at ZV, display the cursor at the end of that
17218 row instead of at the start of the row below. */
17219 else if (row->ends_at_zv_p)
17220 cursor_row_p = 1;
17221 else
17222 cursor_row_p = 0;
17223 }
17224
17225 return cursor_row_p;
17226 }
17227
17228 \f
17229
17230 /* Push the display property PROP so that it will be rendered at the
17231 current position in IT. Return 1 if PROP was successfully pushed,
17232 0 otherwise. */
17233
17234 static int
17235 push_display_prop (struct it *it, Lisp_Object prop)
17236 {
17237 push_it (it);
17238
17239 if (STRINGP (prop))
17240 {
17241 if (SCHARS (prop) == 0)
17242 {
17243 pop_it (it);
17244 return 0;
17245 }
17246
17247 it->string = prop;
17248 it->multibyte_p = STRING_MULTIBYTE (it->string);
17249 it->current.overlay_string_index = -1;
17250 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17251 it->end_charpos = it->string_nchars = SCHARS (it->string);
17252 it->method = GET_FROM_STRING;
17253 it->stop_charpos = 0;
17254 }
17255 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17256 {
17257 it->method = GET_FROM_STRETCH;
17258 it->object = prop;
17259 }
17260 #ifdef HAVE_WINDOW_SYSTEM
17261 else if (IMAGEP (prop))
17262 {
17263 it->what = IT_IMAGE;
17264 it->image_id = lookup_image (it->f, prop);
17265 it->method = GET_FROM_IMAGE;
17266 }
17267 #endif /* HAVE_WINDOW_SYSTEM */
17268 else
17269 {
17270 pop_it (it); /* bogus display property, give up */
17271 return 0;
17272 }
17273
17274 return 1;
17275 }
17276
17277 /* Return the character-property PROP at the current position in IT. */
17278
17279 static Lisp_Object
17280 get_it_property (struct it *it, Lisp_Object prop)
17281 {
17282 Lisp_Object position;
17283
17284 if (STRINGP (it->object))
17285 position = make_number (IT_STRING_CHARPOS (*it));
17286 else if (BUFFERP (it->object))
17287 position = make_number (IT_CHARPOS (*it));
17288 else
17289 return Qnil;
17290
17291 return Fget_char_property (position, prop, it->object);
17292 }
17293
17294 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17295
17296 static void
17297 handle_line_prefix (struct it *it)
17298 {
17299 Lisp_Object prefix;
17300 if (it->continuation_lines_width > 0)
17301 {
17302 prefix = get_it_property (it, Qwrap_prefix);
17303 if (NILP (prefix))
17304 prefix = Vwrap_prefix;
17305 }
17306 else
17307 {
17308 prefix = get_it_property (it, Qline_prefix);
17309 if (NILP (prefix))
17310 prefix = Vline_prefix;
17311 }
17312 if (! NILP (prefix) && push_display_prop (it, prefix))
17313 {
17314 /* If the prefix is wider than the window, and we try to wrap
17315 it, it would acquire its own wrap prefix, and so on till the
17316 iterator stack overflows. So, don't wrap the prefix. */
17317 it->line_wrap = TRUNCATE;
17318 it->avoid_cursor_p = 1;
17319 }
17320 }
17321
17322 \f
17323
17324 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17325 only for R2L lines from display_line, when it decides that too many
17326 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be
17327 continued. */
17328 static void
17329 unproduce_glyphs (struct it *it, int n)
17330 {
17331 struct glyph *glyph, *end;
17332
17333 xassert (it->glyph_row);
17334 xassert (it->glyph_row->reversed_p);
17335 xassert (it->area == TEXT_AREA);
17336 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17337
17338 if (n > it->glyph_row->used[TEXT_AREA])
17339 n = it->glyph_row->used[TEXT_AREA];
17340 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17341 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17342 for ( ; glyph < end; glyph++)
17343 glyph[-n] = *glyph;
17344 }
17345
17346 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17347 and ROW->maxpos. */
17348 static void
17349 find_row_edges (struct it *it, struct glyph_row *row,
17350 EMACS_INT min_pos, EMACS_INT min_bpos,
17351 EMACS_INT max_pos, EMACS_INT max_bpos)
17352 {
17353 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17354 lines' rows is implemented for bidi-reordered rows. */
17355
17356 /* ROW->minpos is the value of min_pos, the minimal buffer position
17357 we have in ROW. */
17358 if (min_pos <= ZV)
17359 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17360 else
17361 {
17362 /* We didn't find _any_ valid buffer positions in any of the
17363 glyphs, so we must trust the iterator's computed
17364 positions. */
17365 row->minpos = row->start.pos;
17366 max_pos = CHARPOS (it->current.pos);
17367 max_bpos = BYTEPOS (it->current.pos);
17368 }
17369
17370 if (!max_pos)
17371 abort ();
17372
17373 /* Here are the various use-cases for ending the row, and the
17374 corresponding values for ROW->maxpos:
17375
17376 Line ends in a newline from buffer eol_pos + 1
17377 Line is continued from buffer max_pos + 1
17378 Line is truncated on right it->current.pos
17379 Line ends in a newline from string max_pos
17380 Line is continued from string max_pos
17381 Line is continued from display vector max_pos
17382 Line is entirely from a string min_pos == max_pos
17383 Line is entirely from a display vector min_pos == max_pos
17384 Line that ends at ZV ZV
17385
17386 If you discover other use-cases, please add them here as
17387 appropriate. */
17388 if (row->ends_at_zv_p)
17389 row->maxpos = it->current.pos;
17390 else if (row->used[TEXT_AREA])
17391 {
17392 if (row->ends_in_newline_from_string_p)
17393 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17394 else if (CHARPOS (it->eol_pos) > 0)
17395 SET_TEXT_POS (row->maxpos,
17396 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17397 else if (row->continued_p)
17398 {
17399 /* If max_pos is different from IT's current position, it
17400 means IT->method does not belong to the display element
17401 at max_pos. However, it also means that the display
17402 element at max_pos was displayed in its entirety on this
17403 line, which is equivalent to saying that the next line
17404 starts at the next buffer position. */
17405 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17406 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17407 else
17408 {
17409 INC_BOTH (max_pos, max_bpos);
17410 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17411 }
17412 }
17413 else if (row->truncated_on_right_p)
17414 /* display_line already called reseat_at_next_visible_line_start,
17415 which puts the iterator at the beginning of the next line, in
17416 the logical order. */
17417 row->maxpos = it->current.pos;
17418 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17419 /* A line that is entirely from a string/image/stretch... */
17420 row->maxpos = row->minpos;
17421 else
17422 abort ();
17423 }
17424 else
17425 row->maxpos = it->current.pos;
17426 }
17427
17428 /* Construct the glyph row IT->glyph_row in the desired matrix of
17429 IT->w from text at the current position of IT. See dispextern.h
17430 for an overview of struct it. Value is non-zero if
17431 IT->glyph_row displays text, as opposed to a line displaying ZV
17432 only. */
17433
17434 static int
17435 display_line (struct it *it)
17436 {
17437 struct glyph_row *row = it->glyph_row;
17438 Lisp_Object overlay_arrow_string;
17439 struct it wrap_it;
17440 int may_wrap = 0, wrap_x;
17441 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17442 int wrap_row_phys_ascent, wrap_row_phys_height;
17443 int wrap_row_extra_line_spacing;
17444 EMACS_INT wrap_row_min_pos, wrap_row_min_bpos;
17445 EMACS_INT wrap_row_max_pos, wrap_row_max_bpos;
17446 int cvpos;
17447 EMACS_INT min_pos = ZV + 1, min_bpos, max_pos = 0, max_bpos;
17448
17449 /* We always start displaying at hpos zero even if hscrolled. */
17450 xassert (it->hpos == 0 && it->current_x == 0);
17451
17452 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17453 >= it->w->desired_matrix->nrows)
17454 {
17455 it->w->nrows_scale_factor++;
17456 fonts_changed_p = 1;
17457 return 0;
17458 }
17459
17460 /* Is IT->w showing the region? */
17461 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17462
17463 /* Clear the result glyph row and enable it. */
17464 prepare_desired_row (row);
17465
17466 row->y = it->current_y;
17467 row->start = it->start;
17468 row->continuation_lines_width = it->continuation_lines_width;
17469 row->displays_text_p = 1;
17470 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17471 it->starts_in_middle_of_char_p = 0;
17472
17473 /* Arrange the overlays nicely for our purposes. Usually, we call
17474 display_line on only one line at a time, in which case this
17475 can't really hurt too much, or we call it on lines which appear
17476 one after another in the buffer, in which case all calls to
17477 recenter_overlay_lists but the first will be pretty cheap. */
17478 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17479
17480 /* Move over display elements that are not visible because we are
17481 hscrolled. This may stop at an x-position < IT->first_visible_x
17482 if the first glyph is partially visible or if we hit a line end. */
17483 if (it->current_x < it->first_visible_x)
17484 {
17485 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17486 MOVE_TO_POS | MOVE_TO_X);
17487 }
17488 else
17489 {
17490 /* We only do this when not calling `move_it_in_display_line_to'
17491 above, because move_it_in_display_line_to calls
17492 handle_line_prefix itself. */
17493 handle_line_prefix (it);
17494 }
17495
17496 /* Get the initial row height. This is either the height of the
17497 text hscrolled, if there is any, or zero. */
17498 row->ascent = it->max_ascent;
17499 row->height = it->max_ascent + it->max_descent;
17500 row->phys_ascent = it->max_phys_ascent;
17501 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17502 row->extra_line_spacing = it->max_extra_line_spacing;
17503
17504 /* Utility macro to record max and min buffer positions seen until now. */
17505 #define RECORD_MAX_MIN_POS(IT) \
17506 do \
17507 { \
17508 if (IT_CHARPOS (*(IT)) < min_pos) \
17509 { \
17510 min_pos = IT_CHARPOS (*(IT)); \
17511 min_bpos = IT_BYTEPOS (*(IT)); \
17512 } \
17513 if (IT_CHARPOS (*(IT)) > max_pos) \
17514 { \
17515 max_pos = IT_CHARPOS (*(IT)); \
17516 max_bpos = IT_BYTEPOS (*(IT)); \
17517 } \
17518 } \
17519 while (0)
17520
17521 /* Loop generating characters. The loop is left with IT on the next
17522 character to display. */
17523 while (1)
17524 {
17525 int n_glyphs_before, hpos_before, x_before;
17526 int x, i, nglyphs;
17527 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17528
17529 /* Retrieve the next thing to display. Value is zero if end of
17530 buffer reached. */
17531 if (!get_next_display_element (it))
17532 {
17533 /* Maybe add a space at the end of this line that is used to
17534 display the cursor there under X. Set the charpos of the
17535 first glyph of blank lines not corresponding to any text
17536 to -1. */
17537 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17538 row->exact_window_width_line_p = 1;
17539 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17540 || row->used[TEXT_AREA] == 0)
17541 {
17542 row->glyphs[TEXT_AREA]->charpos = -1;
17543 row->displays_text_p = 0;
17544
17545 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17546 && (!MINI_WINDOW_P (it->w)
17547 || (minibuf_level && EQ (it->window, minibuf_window))))
17548 row->indicate_empty_line_p = 1;
17549 }
17550
17551 it->continuation_lines_width = 0;
17552 row->ends_at_zv_p = 1;
17553 /* A row that displays right-to-left text must always have
17554 its last face extended all the way to the end of line,
17555 even if this row ends in ZV, because we still write to
17556 the screen left to right. */
17557 if (row->reversed_p)
17558 extend_face_to_end_of_line (it);
17559 break;
17560 }
17561
17562 /* Now, get the metrics of what we want to display. This also
17563 generates glyphs in `row' (which is IT->glyph_row). */
17564 n_glyphs_before = row->used[TEXT_AREA];
17565 x = it->current_x;
17566
17567 /* Remember the line height so far in case the next element doesn't
17568 fit on the line. */
17569 if (it->line_wrap != TRUNCATE)
17570 {
17571 ascent = it->max_ascent;
17572 descent = it->max_descent;
17573 phys_ascent = it->max_phys_ascent;
17574 phys_descent = it->max_phys_descent;
17575
17576 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17577 {
17578 if (IT_DISPLAYING_WHITESPACE (it))
17579 may_wrap = 1;
17580 else if (may_wrap)
17581 {
17582 wrap_it = *it;
17583 wrap_x = x;
17584 wrap_row_used = row->used[TEXT_AREA];
17585 wrap_row_ascent = row->ascent;
17586 wrap_row_height = row->height;
17587 wrap_row_phys_ascent = row->phys_ascent;
17588 wrap_row_phys_height = row->phys_height;
17589 wrap_row_extra_line_spacing = row->extra_line_spacing;
17590 wrap_row_min_pos = min_pos;
17591 wrap_row_min_bpos = min_bpos;
17592 wrap_row_max_pos = max_pos;
17593 wrap_row_max_bpos = max_bpos;
17594 may_wrap = 0;
17595 }
17596 }
17597 }
17598
17599 PRODUCE_GLYPHS (it);
17600
17601 /* If this display element was in marginal areas, continue with
17602 the next one. */
17603 if (it->area != TEXT_AREA)
17604 {
17605 row->ascent = max (row->ascent, it->max_ascent);
17606 row->height = max (row->height, it->max_ascent + it->max_descent);
17607 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17608 row->phys_height = max (row->phys_height,
17609 it->max_phys_ascent + it->max_phys_descent);
17610 row->extra_line_spacing = max (row->extra_line_spacing,
17611 it->max_extra_line_spacing);
17612 set_iterator_to_next (it, 1);
17613 continue;
17614 }
17615
17616 /* Does the display element fit on the line? If we truncate
17617 lines, we should draw past the right edge of the window. If
17618 we don't truncate, we want to stop so that we can display the
17619 continuation glyph before the right margin. If lines are
17620 continued, there are two possible strategies for characters
17621 resulting in more than 1 glyph (e.g. tabs): Display as many
17622 glyphs as possible in this line and leave the rest for the
17623 continuation line, or display the whole element in the next
17624 line. Original redisplay did the former, so we do it also. */
17625 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17626 hpos_before = it->hpos;
17627 x_before = x;
17628
17629 if (/* Not a newline. */
17630 nglyphs > 0
17631 /* Glyphs produced fit entirely in the line. */
17632 && it->current_x < it->last_visible_x)
17633 {
17634 it->hpos += nglyphs;
17635 row->ascent = max (row->ascent, it->max_ascent);
17636 row->height = max (row->height, it->max_ascent + it->max_descent);
17637 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17638 row->phys_height = max (row->phys_height,
17639 it->max_phys_ascent + it->max_phys_descent);
17640 row->extra_line_spacing = max (row->extra_line_spacing,
17641 it->max_extra_line_spacing);
17642 if (it->current_x - it->pixel_width < it->first_visible_x)
17643 row->x = x - it->first_visible_x;
17644 /* Record the maximum and minimum buffer positions seen so
17645 far in glyphs that will be displayed by this row. */
17646 if (it->bidi_p)
17647 RECORD_MAX_MIN_POS (it);
17648 }
17649 else
17650 {
17651 int new_x;
17652 struct glyph *glyph;
17653
17654 for (i = 0; i < nglyphs; ++i, x = new_x)
17655 {
17656 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17657 new_x = x + glyph->pixel_width;
17658
17659 if (/* Lines are continued. */
17660 it->line_wrap != TRUNCATE
17661 && (/* Glyph doesn't fit on the line. */
17662 new_x > it->last_visible_x
17663 /* Or it fits exactly on a window system frame. */
17664 || (new_x == it->last_visible_x
17665 && FRAME_WINDOW_P (it->f))))
17666 {
17667 /* End of a continued line. */
17668
17669 if (it->hpos == 0
17670 || (new_x == it->last_visible_x
17671 && FRAME_WINDOW_P (it->f)))
17672 {
17673 /* Current glyph is the only one on the line or
17674 fits exactly on the line. We must continue
17675 the line because we can't draw the cursor
17676 after the glyph. */
17677 row->continued_p = 1;
17678 it->current_x = new_x;
17679 it->continuation_lines_width += new_x;
17680 ++it->hpos;
17681 /* Record the maximum and minimum buffer
17682 positions seen so far in glyphs that will be
17683 displayed by this row. */
17684 if (it->bidi_p)
17685 RECORD_MAX_MIN_POS (it);
17686 if (i == nglyphs - 1)
17687 {
17688 /* If line-wrap is on, check if a previous
17689 wrap point was found. */
17690 if (wrap_row_used > 0
17691 /* Even if there is a previous wrap
17692 point, continue the line here as
17693 usual, if (i) the previous character
17694 was a space or tab AND (ii) the
17695 current character is not. */
17696 && (!may_wrap
17697 || IT_DISPLAYING_WHITESPACE (it)))
17698 goto back_to_wrap;
17699
17700 set_iterator_to_next (it, 1);
17701 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17702 {
17703 if (!get_next_display_element (it))
17704 {
17705 row->exact_window_width_line_p = 1;
17706 it->continuation_lines_width = 0;
17707 row->continued_p = 0;
17708 row->ends_at_zv_p = 1;
17709 }
17710 else if (ITERATOR_AT_END_OF_LINE_P (it))
17711 {
17712 row->continued_p = 0;
17713 row->exact_window_width_line_p = 1;
17714 }
17715 }
17716 }
17717 }
17718 else if (CHAR_GLYPH_PADDING_P (*glyph)
17719 && !FRAME_WINDOW_P (it->f))
17720 {
17721 /* A padding glyph that doesn't fit on this line.
17722 This means the whole character doesn't fit
17723 on the line. */
17724 if (row->reversed_p)
17725 unproduce_glyphs (it, row->used[TEXT_AREA]
17726 - n_glyphs_before);
17727 row->used[TEXT_AREA] = n_glyphs_before;
17728
17729 /* Fill the rest of the row with continuation
17730 glyphs like in 20.x. */
17731 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17732 < row->glyphs[1 + TEXT_AREA])
17733 produce_special_glyphs (it, IT_CONTINUATION);
17734
17735 row->continued_p = 1;
17736 it->current_x = x_before;
17737 it->continuation_lines_width += x_before;
17738
17739 /* Restore the height to what it was before the
17740 element not fitting on the line. */
17741 it->max_ascent = ascent;
17742 it->max_descent = descent;
17743 it->max_phys_ascent = phys_ascent;
17744 it->max_phys_descent = phys_descent;
17745 }
17746 else if (wrap_row_used > 0)
17747 {
17748 back_to_wrap:
17749 if (row->reversed_p)
17750 unproduce_glyphs (it,
17751 row->used[TEXT_AREA] - wrap_row_used);
17752 *it = wrap_it;
17753 it->continuation_lines_width += wrap_x;
17754 row->used[TEXT_AREA] = wrap_row_used;
17755 row->ascent = wrap_row_ascent;
17756 row->height = wrap_row_height;
17757 row->phys_ascent = wrap_row_phys_ascent;
17758 row->phys_height = wrap_row_phys_height;
17759 row->extra_line_spacing = wrap_row_extra_line_spacing;
17760 min_pos = wrap_row_min_pos;
17761 min_bpos = wrap_row_min_bpos;
17762 max_pos = wrap_row_max_pos;
17763 max_bpos = wrap_row_max_bpos;
17764 row->continued_p = 1;
17765 row->ends_at_zv_p = 0;
17766 row->exact_window_width_line_p = 0;
17767 it->continuation_lines_width += x;
17768
17769 /* Make sure that a non-default face is extended
17770 up to the right margin of the window. */
17771 extend_face_to_end_of_line (it);
17772 }
17773 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17774 {
17775 /* A TAB that extends past the right edge of the
17776 window. This produces a single glyph on
17777 window system frames. We leave the glyph in
17778 this row and let it fill the row, but don't
17779 consume the TAB. */
17780 it->continuation_lines_width += it->last_visible_x;
17781 row->ends_in_middle_of_char_p = 1;
17782 row->continued_p = 1;
17783 glyph->pixel_width = it->last_visible_x - x;
17784 it->starts_in_middle_of_char_p = 1;
17785 }
17786 else
17787 {
17788 /* Something other than a TAB that draws past
17789 the right edge of the window. Restore
17790 positions to values before the element. */
17791 if (row->reversed_p)
17792 unproduce_glyphs (it, row->used[TEXT_AREA]
17793 - (n_glyphs_before + i));
17794 row->used[TEXT_AREA] = n_glyphs_before + i;
17795
17796 /* Display continuation glyphs. */
17797 if (!FRAME_WINDOW_P (it->f))
17798 produce_special_glyphs (it, IT_CONTINUATION);
17799 row->continued_p = 1;
17800
17801 it->current_x = x_before;
17802 it->continuation_lines_width += x;
17803 extend_face_to_end_of_line (it);
17804
17805 if (nglyphs > 1 && i > 0)
17806 {
17807 row->ends_in_middle_of_char_p = 1;
17808 it->starts_in_middle_of_char_p = 1;
17809 }
17810
17811 /* Restore the height to what it was before the
17812 element not fitting on the line. */
17813 it->max_ascent = ascent;
17814 it->max_descent = descent;
17815 it->max_phys_ascent = phys_ascent;
17816 it->max_phys_descent = phys_descent;
17817 }
17818
17819 break;
17820 }
17821 else if (new_x > it->first_visible_x)
17822 {
17823 /* Increment number of glyphs actually displayed. */
17824 ++it->hpos;
17825
17826 /* Record the maximum and minimum buffer positions
17827 seen so far in glyphs that will be displayed by
17828 this row. */
17829 if (it->bidi_p)
17830 RECORD_MAX_MIN_POS (it);
17831
17832 if (x < it->first_visible_x)
17833 /* Glyph is partially visible, i.e. row starts at
17834 negative X position. */
17835 row->x = x - it->first_visible_x;
17836 }
17837 else
17838 {
17839 /* Glyph is completely off the left margin of the
17840 window. This should not happen because of the
17841 move_it_in_display_line at the start of this
17842 function, unless the text display area of the
17843 window is empty. */
17844 xassert (it->first_visible_x <= it->last_visible_x);
17845 }
17846 }
17847
17848 row->ascent = max (row->ascent, it->max_ascent);
17849 row->height = max (row->height, it->max_ascent + it->max_descent);
17850 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17851 row->phys_height = max (row->phys_height,
17852 it->max_phys_ascent + it->max_phys_descent);
17853 row->extra_line_spacing = max (row->extra_line_spacing,
17854 it->max_extra_line_spacing);
17855
17856 /* End of this display line if row is continued. */
17857 if (row->continued_p || row->ends_at_zv_p)
17858 break;
17859 }
17860
17861 at_end_of_line:
17862 /* Is this a line end? If yes, we're also done, after making
17863 sure that a non-default face is extended up to the right
17864 margin of the window. */
17865 if (ITERATOR_AT_END_OF_LINE_P (it))
17866 {
17867 int used_before = row->used[TEXT_AREA];
17868
17869 row->ends_in_newline_from_string_p = STRINGP (it->object);
17870
17871 /* Add a space at the end of the line that is used to
17872 display the cursor there. */
17873 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17874 append_space_for_newline (it, 0);
17875
17876 /* Extend the face to the end of the line. */
17877 extend_face_to_end_of_line (it);
17878
17879 /* Make sure we have the position. */
17880 if (used_before == 0)
17881 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17882
17883 /* Record the position of the newline, for use in
17884 find_row_edges. */
17885 it->eol_pos = it->current.pos;
17886
17887 /* Consume the line end. This skips over invisible lines. */
17888 set_iterator_to_next (it, 1);
17889 it->continuation_lines_width = 0;
17890 break;
17891 }
17892
17893 /* Proceed with next display element. Note that this skips
17894 over lines invisible because of selective display. */
17895 set_iterator_to_next (it, 1);
17896
17897 /* If we truncate lines, we are done when the last displayed
17898 glyphs reach past the right margin of the window. */
17899 if (it->line_wrap == TRUNCATE
17900 && (FRAME_WINDOW_P (it->f)
17901 ? (it->current_x >= it->last_visible_x)
17902 : (it->current_x > it->last_visible_x)))
17903 {
17904 /* Maybe add truncation glyphs. */
17905 if (!FRAME_WINDOW_P (it->f))
17906 {
17907 int i, n;
17908
17909 if (!row->reversed_p)
17910 {
17911 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17912 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17913 break;
17914 }
17915 else
17916 {
17917 for (i = 0; i < row->used[TEXT_AREA]; i++)
17918 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17919 break;
17920 /* Remove any padding glyphs at the front of ROW, to
17921 make room for the truncation glyphs we will be
17922 adding below. The loop below always inserts at
17923 least one truncation glyph, so also remove the
17924 last glyph added to ROW. */
17925 unproduce_glyphs (it, i + 1);
17926 /* Adjust i for the loop below. */
17927 i = row->used[TEXT_AREA] - (i + 1);
17928 }
17929
17930 for (n = row->used[TEXT_AREA]; i < n; ++i)
17931 {
17932 row->used[TEXT_AREA] = i;
17933 produce_special_glyphs (it, IT_TRUNCATION);
17934 }
17935 }
17936 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17937 {
17938 /* Don't truncate if we can overflow newline into fringe. */
17939 if (!get_next_display_element (it))
17940 {
17941 it->continuation_lines_width = 0;
17942 row->ends_at_zv_p = 1;
17943 row->exact_window_width_line_p = 1;
17944 break;
17945 }
17946 if (ITERATOR_AT_END_OF_LINE_P (it))
17947 {
17948 row->exact_window_width_line_p = 1;
17949 goto at_end_of_line;
17950 }
17951 }
17952
17953 row->truncated_on_right_p = 1;
17954 it->continuation_lines_width = 0;
17955 reseat_at_next_visible_line_start (it, 0);
17956 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17957 it->hpos = hpos_before;
17958 it->current_x = x_before;
17959 break;
17960 }
17961 }
17962
17963 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17964 at the left window margin. */
17965 if (it->first_visible_x
17966 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
17967 {
17968 if (!FRAME_WINDOW_P (it->f))
17969 insert_left_trunc_glyphs (it);
17970 row->truncated_on_left_p = 1;
17971 }
17972
17973 /* Remember the position at which this line ends.
17974
17975 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
17976 cannot be before the call to find_row_edges below, since that is
17977 where these positions are determined. */
17978 row->end = it->current;
17979 if (!it->bidi_p)
17980 {
17981 row->minpos = row->start.pos;
17982 row->maxpos = row->end.pos;
17983 }
17984 else
17985 {
17986 /* ROW->minpos and ROW->maxpos must be the smallest and
17987 `1 + the largest' buffer positions in ROW. But if ROW was
17988 bidi-reordered, these two positions can be anywhere in the
17989 row, so we must determine them now. */
17990 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
17991 }
17992
17993 /* If the start of this line is the overlay arrow-position, then
17994 mark this glyph row as the one containing the overlay arrow.
17995 This is clearly a mess with variable size fonts. It would be
17996 better to let it be displayed like cursors under X. */
17997 if ((row->displays_text_p || !overlay_arrow_seen)
17998 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17999 !NILP (overlay_arrow_string)))
18000 {
18001 /* Overlay arrow in window redisplay is a fringe bitmap. */
18002 if (STRINGP (overlay_arrow_string))
18003 {
18004 struct glyph_row *arrow_row
18005 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18006 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18007 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18008 struct glyph *p = row->glyphs[TEXT_AREA];
18009 struct glyph *p2, *end;
18010
18011 /* Copy the arrow glyphs. */
18012 while (glyph < arrow_end)
18013 *p++ = *glyph++;
18014
18015 /* Throw away padding glyphs. */
18016 p2 = p;
18017 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18018 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18019 ++p2;
18020 if (p2 > p)
18021 {
18022 while (p2 < end)
18023 *p++ = *p2++;
18024 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18025 }
18026 }
18027 else
18028 {
18029 xassert (INTEGERP (overlay_arrow_string));
18030 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18031 }
18032 overlay_arrow_seen = 1;
18033 }
18034
18035 /* Compute pixel dimensions of this line. */
18036 compute_line_metrics (it);
18037
18038 /* Record whether this row ends inside an ellipsis. */
18039 row->ends_in_ellipsis_p
18040 = (it->method == GET_FROM_DISPLAY_VECTOR
18041 && it->ellipsis_p);
18042
18043 /* Save fringe bitmaps in this row. */
18044 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18045 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18046 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18047 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18048
18049 it->left_user_fringe_bitmap = 0;
18050 it->left_user_fringe_face_id = 0;
18051 it->right_user_fringe_bitmap = 0;
18052 it->right_user_fringe_face_id = 0;
18053
18054 /* Maybe set the cursor. */
18055 cvpos = it->w->cursor.vpos;
18056 if ((cvpos < 0
18057 /* In bidi-reordered rows, keep checking for proper cursor
18058 position even if one has been found already, because buffer
18059 positions in such rows change non-linearly with ROW->VPOS,
18060 when a line is continued. One exception: when we are at ZV,
18061 display cursor on the first suitable glyph row, since all
18062 the empty rows after that also have their position set to ZV. */
18063 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18064 lines' rows is implemented for bidi-reordered rows. */
18065 || (it->bidi_p
18066 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18067 && PT >= MATRIX_ROW_START_CHARPOS (row)
18068 && PT <= MATRIX_ROW_END_CHARPOS (row)
18069 && cursor_row_p (it->w, row))
18070 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18071
18072 /* Highlight trailing whitespace. */
18073 if (!NILP (Vshow_trailing_whitespace))
18074 highlight_trailing_whitespace (it->f, it->glyph_row);
18075
18076 /* Prepare for the next line. This line starts horizontally at (X
18077 HPOS) = (0 0). Vertical positions are incremented. As a
18078 convenience for the caller, IT->glyph_row is set to the next
18079 row to be used. */
18080 it->current_x = it->hpos = 0;
18081 it->current_y += row->height;
18082 SET_TEXT_POS (it->eol_pos, 0, 0);
18083 ++it->vpos;
18084 ++it->glyph_row;
18085 /* The next row should by default use the same value of the
18086 reversed_p flag as this one. set_iterator_to_next decides when
18087 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18088 the flag accordingly. */
18089 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18090 it->glyph_row->reversed_p = row->reversed_p;
18091 it->start = row->end;
18092 return row->displays_text_p;
18093
18094 #undef RECORD_MAX_MIN_POS
18095 }
18096
18097 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18098 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18099 doc: /* Return paragraph direction at point in BUFFER.
18100 Value is either `left-to-right' or `right-to-left'.
18101 If BUFFER is omitted or nil, it defaults to the current buffer.
18102
18103 Paragraph direction determines how the text in the paragraph is displayed.
18104 In left-to-right paragraphs, text begins at the left margin of the window
18105 and the reading direction is generally left to right. In right-to-left
18106 paragraphs, text begins at the right margin and is read from right to left.
18107
18108 See also `bidi-paragraph-direction'. */)
18109 (Lisp_Object buffer)
18110 {
18111 struct buffer *buf;
18112 struct buffer *old;
18113
18114 if (NILP (buffer))
18115 buf = current_buffer;
18116 else
18117 {
18118 CHECK_BUFFER (buffer);
18119 buf = XBUFFER (buffer);
18120 old = current_buffer;
18121 }
18122
18123 if (NILP (buf->bidi_display_reordering))
18124 return Qleft_to_right;
18125 else if (!NILP (buf->bidi_paragraph_direction))
18126 return buf->bidi_paragraph_direction;
18127 else
18128 {
18129 /* Determine the direction from buffer text. We could try to
18130 use current_matrix if it is up to date, but this seems fast
18131 enough as it is. */
18132 struct bidi_it itb;
18133 EMACS_INT pos = BUF_PT (buf);
18134 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18135 int c;
18136
18137 if (buf != current_buffer)
18138 set_buffer_temp (buf);
18139 /* bidi_paragraph_init finds the base direction of the paragraph
18140 by searching forward from paragraph start. We need the base
18141 direction of the current or _previous_ paragraph, so we need
18142 to make sure we are within that paragraph. To that end, find
18143 the previous non-empty line. */
18144 if (pos >= ZV && pos > BEGV)
18145 {
18146 pos--;
18147 bytepos = CHAR_TO_BYTE (pos);
18148 }
18149 while ((c = FETCH_BYTE (bytepos)) == '\n'
18150 || c == ' ' || c == '\t' || c == '\f')
18151 {
18152 if (bytepos <= BEGV_BYTE)
18153 break;
18154 bytepos--;
18155 pos--;
18156 }
18157 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18158 bytepos--;
18159 itb.charpos = pos;
18160 itb.bytepos = bytepos;
18161 itb.first_elt = 1;
18162 itb.separator_limit = -1;
18163 itb.paragraph_dir = NEUTRAL_DIR;
18164
18165 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18166 if (buf != current_buffer)
18167 set_buffer_temp (old);
18168 switch (itb.paragraph_dir)
18169 {
18170 case L2R:
18171 return Qleft_to_right;
18172 break;
18173 case R2L:
18174 return Qright_to_left;
18175 break;
18176 default:
18177 abort ();
18178 }
18179 }
18180 }
18181
18182
18183 \f
18184 /***********************************************************************
18185 Menu Bar
18186 ***********************************************************************/
18187
18188 /* Redisplay the menu bar in the frame for window W.
18189
18190 The menu bar of X frames that don't have X toolkit support is
18191 displayed in a special window W->frame->menu_bar_window.
18192
18193 The menu bar of terminal frames is treated specially as far as
18194 glyph matrices are concerned. Menu bar lines are not part of
18195 windows, so the update is done directly on the frame matrix rows
18196 for the menu bar. */
18197
18198 static void
18199 display_menu_bar (struct window *w)
18200 {
18201 struct frame *f = XFRAME (WINDOW_FRAME (w));
18202 struct it it;
18203 Lisp_Object items;
18204 int i;
18205
18206 /* Don't do all this for graphical frames. */
18207 #ifdef HAVE_NTGUI
18208 if (FRAME_W32_P (f))
18209 return;
18210 #endif
18211 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18212 if (FRAME_X_P (f))
18213 return;
18214 #endif
18215
18216 #ifdef HAVE_NS
18217 if (FRAME_NS_P (f))
18218 return;
18219 #endif /* HAVE_NS */
18220
18221 #ifdef USE_X_TOOLKIT
18222 xassert (!FRAME_WINDOW_P (f));
18223 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18224 it.first_visible_x = 0;
18225 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18226 #else /* not USE_X_TOOLKIT */
18227 if (FRAME_WINDOW_P (f))
18228 {
18229 /* Menu bar lines are displayed in the desired matrix of the
18230 dummy window menu_bar_window. */
18231 struct window *menu_w;
18232 xassert (WINDOWP (f->menu_bar_window));
18233 menu_w = XWINDOW (f->menu_bar_window);
18234 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18235 MENU_FACE_ID);
18236 it.first_visible_x = 0;
18237 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18238 }
18239 else
18240 {
18241 /* This is a TTY frame, i.e. character hpos/vpos are used as
18242 pixel x/y. */
18243 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18244 MENU_FACE_ID);
18245 it.first_visible_x = 0;
18246 it.last_visible_x = FRAME_COLS (f);
18247 }
18248 #endif /* not USE_X_TOOLKIT */
18249
18250 if (! mode_line_inverse_video)
18251 /* Force the menu-bar to be displayed in the default face. */
18252 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18253
18254 /* Clear all rows of the menu bar. */
18255 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18256 {
18257 struct glyph_row *row = it.glyph_row + i;
18258 clear_glyph_row (row);
18259 row->enabled_p = 1;
18260 row->full_width_p = 1;
18261 }
18262
18263 /* Display all items of the menu bar. */
18264 items = FRAME_MENU_BAR_ITEMS (it.f);
18265 for (i = 0; i < XVECTOR (items)->size; i += 4)
18266 {
18267 Lisp_Object string;
18268
18269 /* Stop at nil string. */
18270 string = AREF (items, i + 1);
18271 if (NILP (string))
18272 break;
18273
18274 /* Remember where item was displayed. */
18275 ASET (items, i + 3, make_number (it.hpos));
18276
18277 /* Display the item, pad with one space. */
18278 if (it.current_x < it.last_visible_x)
18279 display_string (NULL, string, Qnil, 0, 0, &it,
18280 SCHARS (string) + 1, 0, 0, -1);
18281 }
18282
18283 /* Fill out the line with spaces. */
18284 if (it.current_x < it.last_visible_x)
18285 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18286
18287 /* Compute the total height of the lines. */
18288 compute_line_metrics (&it);
18289 }
18290
18291
18292 \f
18293 /***********************************************************************
18294 Mode Line
18295 ***********************************************************************/
18296
18297 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18298 FORCE is non-zero, redisplay mode lines unconditionally.
18299 Otherwise, redisplay only mode lines that are garbaged. Value is
18300 the number of windows whose mode lines were redisplayed. */
18301
18302 static int
18303 redisplay_mode_lines (Lisp_Object window, int force)
18304 {
18305 int nwindows = 0;
18306
18307 while (!NILP (window))
18308 {
18309 struct window *w = XWINDOW (window);
18310
18311 if (WINDOWP (w->hchild))
18312 nwindows += redisplay_mode_lines (w->hchild, force);
18313 else if (WINDOWP (w->vchild))
18314 nwindows += redisplay_mode_lines (w->vchild, force);
18315 else if (force
18316 || FRAME_GARBAGED_P (XFRAME (w->frame))
18317 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18318 {
18319 struct text_pos lpoint;
18320 struct buffer *old = current_buffer;
18321
18322 /* Set the window's buffer for the mode line display. */
18323 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18324 set_buffer_internal_1 (XBUFFER (w->buffer));
18325
18326 /* Point refers normally to the selected window. For any
18327 other window, set up appropriate value. */
18328 if (!EQ (window, selected_window))
18329 {
18330 struct text_pos pt;
18331
18332 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18333 if (CHARPOS (pt) < BEGV)
18334 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18335 else if (CHARPOS (pt) > (ZV - 1))
18336 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18337 else
18338 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18339 }
18340
18341 /* Display mode lines. */
18342 clear_glyph_matrix (w->desired_matrix);
18343 if (display_mode_lines (w))
18344 {
18345 ++nwindows;
18346 w->must_be_updated_p = 1;
18347 }
18348
18349 /* Restore old settings. */
18350 set_buffer_internal_1 (old);
18351 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18352 }
18353
18354 window = w->next;
18355 }
18356
18357 return nwindows;
18358 }
18359
18360
18361 /* Display the mode and/or header line of window W. Value is the
18362 sum number of mode lines and header lines displayed. */
18363
18364 static int
18365 display_mode_lines (struct window *w)
18366 {
18367 Lisp_Object old_selected_window, old_selected_frame;
18368 int n = 0;
18369
18370 old_selected_frame = selected_frame;
18371 selected_frame = w->frame;
18372 old_selected_window = selected_window;
18373 XSETWINDOW (selected_window, w);
18374
18375 /* These will be set while the mode line specs are processed. */
18376 line_number_displayed = 0;
18377 w->column_number_displayed = Qnil;
18378
18379 if (WINDOW_WANTS_MODELINE_P (w))
18380 {
18381 struct window *sel_w = XWINDOW (old_selected_window);
18382
18383 /* Select mode line face based on the real selected window. */
18384 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18385 current_buffer->mode_line_format);
18386 ++n;
18387 }
18388
18389 if (WINDOW_WANTS_HEADER_LINE_P (w))
18390 {
18391 display_mode_line (w, HEADER_LINE_FACE_ID,
18392 current_buffer->header_line_format);
18393 ++n;
18394 }
18395
18396 selected_frame = old_selected_frame;
18397 selected_window = old_selected_window;
18398 return n;
18399 }
18400
18401
18402 /* Display mode or header line of window W. FACE_ID specifies which
18403 line to display; it is either MODE_LINE_FACE_ID or
18404 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18405 display. Value is the pixel height of the mode/header line
18406 displayed. */
18407
18408 static int
18409 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18410 {
18411 struct it it;
18412 struct face *face;
18413 int count = SPECPDL_INDEX ();
18414
18415 init_iterator (&it, w, -1, -1, NULL, face_id);
18416 /* Don't extend on a previously drawn mode-line.
18417 This may happen if called from pos_visible_p. */
18418 it.glyph_row->enabled_p = 0;
18419 prepare_desired_row (it.glyph_row);
18420
18421 it.glyph_row->mode_line_p = 1;
18422
18423 if (! mode_line_inverse_video)
18424 /* Force the mode-line to be displayed in the default face. */
18425 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18426
18427 record_unwind_protect (unwind_format_mode_line,
18428 format_mode_line_unwind_data (NULL, Qnil, 0));
18429
18430 mode_line_target = MODE_LINE_DISPLAY;
18431
18432 /* Temporarily make frame's keyboard the current kboard so that
18433 kboard-local variables in the mode_line_format will get the right
18434 values. */
18435 push_kboard (FRAME_KBOARD (it.f));
18436 record_unwind_save_match_data ();
18437 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18438 pop_kboard ();
18439
18440 unbind_to (count, Qnil);
18441
18442 /* Fill up with spaces. */
18443 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18444
18445 compute_line_metrics (&it);
18446 it.glyph_row->full_width_p = 1;
18447 it.glyph_row->continued_p = 0;
18448 it.glyph_row->truncated_on_left_p = 0;
18449 it.glyph_row->truncated_on_right_p = 0;
18450
18451 /* Make a 3D mode-line have a shadow at its right end. */
18452 face = FACE_FROM_ID (it.f, face_id);
18453 extend_face_to_end_of_line (&it);
18454 if (face->box != FACE_NO_BOX)
18455 {
18456 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18457 + it.glyph_row->used[TEXT_AREA] - 1);
18458 last->right_box_line_p = 1;
18459 }
18460
18461 return it.glyph_row->height;
18462 }
18463
18464 /* Move element ELT in LIST to the front of LIST.
18465 Return the updated list. */
18466
18467 static Lisp_Object
18468 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18469 {
18470 register Lisp_Object tail, prev;
18471 register Lisp_Object tem;
18472
18473 tail = list;
18474 prev = Qnil;
18475 while (CONSP (tail))
18476 {
18477 tem = XCAR (tail);
18478
18479 if (EQ (elt, tem))
18480 {
18481 /* Splice out the link TAIL. */
18482 if (NILP (prev))
18483 list = XCDR (tail);
18484 else
18485 Fsetcdr (prev, XCDR (tail));
18486
18487 /* Now make it the first. */
18488 Fsetcdr (tail, list);
18489 return tail;
18490 }
18491 else
18492 prev = tail;
18493 tail = XCDR (tail);
18494 QUIT;
18495 }
18496
18497 /* Not found--return unchanged LIST. */
18498 return list;
18499 }
18500
18501 /* Contribute ELT to the mode line for window IT->w. How it
18502 translates into text depends on its data type.
18503
18504 IT describes the display environment in which we display, as usual.
18505
18506 DEPTH is the depth in recursion. It is used to prevent
18507 infinite recursion here.
18508
18509 FIELD_WIDTH is the number of characters the display of ELT should
18510 occupy in the mode line, and PRECISION is the maximum number of
18511 characters to display from ELT's representation. See
18512 display_string for details.
18513
18514 Returns the hpos of the end of the text generated by ELT.
18515
18516 PROPS is a property list to add to any string we encounter.
18517
18518 If RISKY is nonzero, remove (disregard) any properties in any string
18519 we encounter, and ignore :eval and :propertize.
18520
18521 The global variable `mode_line_target' determines whether the
18522 output is passed to `store_mode_line_noprop',
18523 `store_mode_line_string', or `display_string'. */
18524
18525 static int
18526 display_mode_element (struct it *it, int depth, int field_width, int precision,
18527 Lisp_Object elt, Lisp_Object props, int risky)
18528 {
18529 int n = 0, field, prec;
18530 int literal = 0;
18531
18532 tail_recurse:
18533 if (depth > 100)
18534 elt = build_string ("*too-deep*");
18535
18536 depth++;
18537
18538 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18539 {
18540 case Lisp_String:
18541 {
18542 /* A string: output it and check for %-constructs within it. */
18543 unsigned char c;
18544 EMACS_INT offset = 0;
18545
18546 if (SCHARS (elt) > 0
18547 && (!NILP (props) || risky))
18548 {
18549 Lisp_Object oprops, aelt;
18550 oprops = Ftext_properties_at (make_number (0), elt);
18551
18552 /* If the starting string's properties are not what
18553 we want, translate the string. Also, if the string
18554 is risky, do that anyway. */
18555
18556 if (NILP (Fequal (props, oprops)) || risky)
18557 {
18558 /* If the starting string has properties,
18559 merge the specified ones onto the existing ones. */
18560 if (! NILP (oprops) && !risky)
18561 {
18562 Lisp_Object tem;
18563
18564 oprops = Fcopy_sequence (oprops);
18565 tem = props;
18566 while (CONSP (tem))
18567 {
18568 oprops = Fplist_put (oprops, XCAR (tem),
18569 XCAR (XCDR (tem)));
18570 tem = XCDR (XCDR (tem));
18571 }
18572 props = oprops;
18573 }
18574
18575 aelt = Fassoc (elt, mode_line_proptrans_alist);
18576 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18577 {
18578 /* AELT is what we want. Move it to the front
18579 without consing. */
18580 elt = XCAR (aelt);
18581 mode_line_proptrans_alist
18582 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18583 }
18584 else
18585 {
18586 Lisp_Object tem;
18587
18588 /* If AELT has the wrong props, it is useless.
18589 so get rid of it. */
18590 if (! NILP (aelt))
18591 mode_line_proptrans_alist
18592 = Fdelq (aelt, mode_line_proptrans_alist);
18593
18594 elt = Fcopy_sequence (elt);
18595 Fset_text_properties (make_number (0), Flength (elt),
18596 props, elt);
18597 /* Add this item to mode_line_proptrans_alist. */
18598 mode_line_proptrans_alist
18599 = Fcons (Fcons (elt, props),
18600 mode_line_proptrans_alist);
18601 /* Truncate mode_line_proptrans_alist
18602 to at most 50 elements. */
18603 tem = Fnthcdr (make_number (50),
18604 mode_line_proptrans_alist);
18605 if (! NILP (tem))
18606 XSETCDR (tem, Qnil);
18607 }
18608 }
18609 }
18610
18611 offset = 0;
18612
18613 if (literal)
18614 {
18615 prec = precision - n;
18616 switch (mode_line_target)
18617 {
18618 case MODE_LINE_NOPROP:
18619 case MODE_LINE_TITLE:
18620 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18621 break;
18622 case MODE_LINE_STRING:
18623 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18624 break;
18625 case MODE_LINE_DISPLAY:
18626 n += display_string (NULL, elt, Qnil, 0, 0, it,
18627 0, prec, 0, STRING_MULTIBYTE (elt));
18628 break;
18629 }
18630
18631 break;
18632 }
18633
18634 /* Handle the non-literal case. */
18635
18636 while ((precision <= 0 || n < precision)
18637 && SREF (elt, offset) != 0
18638 && (mode_line_target != MODE_LINE_DISPLAY
18639 || it->current_x < it->last_visible_x))
18640 {
18641 EMACS_INT last_offset = offset;
18642
18643 /* Advance to end of string or next format specifier. */
18644 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18645 ;
18646
18647 if (offset - 1 != last_offset)
18648 {
18649 EMACS_INT nchars, nbytes;
18650
18651 /* Output to end of string or up to '%'. Field width
18652 is length of string. Don't output more than
18653 PRECISION allows us. */
18654 offset--;
18655
18656 prec = c_string_width (SDATA (elt) + last_offset,
18657 offset - last_offset, precision - n,
18658 &nchars, &nbytes);
18659
18660 switch (mode_line_target)
18661 {
18662 case MODE_LINE_NOPROP:
18663 case MODE_LINE_TITLE:
18664 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18665 break;
18666 case MODE_LINE_STRING:
18667 {
18668 EMACS_INT bytepos = last_offset;
18669 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18670 EMACS_INT endpos = (precision <= 0
18671 ? string_byte_to_char (elt, offset)
18672 : charpos + nchars);
18673
18674 n += store_mode_line_string (NULL,
18675 Fsubstring (elt, make_number (charpos),
18676 make_number (endpos)),
18677 0, 0, 0, Qnil);
18678 }
18679 break;
18680 case MODE_LINE_DISPLAY:
18681 {
18682 EMACS_INT bytepos = last_offset;
18683 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18684
18685 if (precision <= 0)
18686 nchars = string_byte_to_char (elt, offset) - charpos;
18687 n += display_string (NULL, elt, Qnil, 0, charpos,
18688 it, 0, nchars, 0,
18689 STRING_MULTIBYTE (elt));
18690 }
18691 break;
18692 }
18693 }
18694 else /* c == '%' */
18695 {
18696 EMACS_INT percent_position = offset;
18697
18698 /* Get the specified minimum width. Zero means
18699 don't pad. */
18700 field = 0;
18701 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18702 field = field * 10 + c - '0';
18703
18704 /* Don't pad beyond the total padding allowed. */
18705 if (field_width - n > 0 && field > field_width - n)
18706 field = field_width - n;
18707
18708 /* Note that either PRECISION <= 0 or N < PRECISION. */
18709 prec = precision - n;
18710
18711 if (c == 'M')
18712 n += display_mode_element (it, depth, field, prec,
18713 Vglobal_mode_string, props,
18714 risky);
18715 else if (c != 0)
18716 {
18717 int multibyte;
18718 EMACS_INT bytepos, charpos;
18719 const unsigned char *spec;
18720 Lisp_Object string;
18721
18722 bytepos = percent_position;
18723 charpos = (STRING_MULTIBYTE (elt)
18724 ? string_byte_to_char (elt, bytepos)
18725 : bytepos);
18726 spec = decode_mode_spec (it->w, c, field, prec, &string);
18727 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18728
18729 switch (mode_line_target)
18730 {
18731 case MODE_LINE_NOPROP:
18732 case MODE_LINE_TITLE:
18733 n += store_mode_line_noprop (spec, field, prec);
18734 break;
18735 case MODE_LINE_STRING:
18736 {
18737 int len = strlen (spec);
18738 Lisp_Object tem = make_string (spec, len);
18739 props = Ftext_properties_at (make_number (charpos), elt);
18740 /* Should only keep face property in props */
18741 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18742 }
18743 break;
18744 case MODE_LINE_DISPLAY:
18745 {
18746 int nglyphs_before, nwritten;
18747
18748 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18749 nwritten = display_string (spec, string, elt,
18750 charpos, 0, it,
18751 field, prec, 0,
18752 multibyte);
18753
18754 /* Assign to the glyphs written above the
18755 string where the `%x' came from, position
18756 of the `%'. */
18757 if (nwritten > 0)
18758 {
18759 struct glyph *glyph
18760 = (it->glyph_row->glyphs[TEXT_AREA]
18761 + nglyphs_before);
18762 int i;
18763
18764 for (i = 0; i < nwritten; ++i)
18765 {
18766 glyph[i].object = elt;
18767 glyph[i].charpos = charpos;
18768 }
18769
18770 n += nwritten;
18771 }
18772 }
18773 break;
18774 }
18775 }
18776 else /* c == 0 */
18777 break;
18778 }
18779 }
18780 }
18781 break;
18782
18783 case Lisp_Symbol:
18784 /* A symbol: process the value of the symbol recursively
18785 as if it appeared here directly. Avoid error if symbol void.
18786 Special case: if value of symbol is a string, output the string
18787 literally. */
18788 {
18789 register Lisp_Object tem;
18790
18791 /* If the variable is not marked as risky to set
18792 then its contents are risky to use. */
18793 if (NILP (Fget (elt, Qrisky_local_variable)))
18794 risky = 1;
18795
18796 tem = Fboundp (elt);
18797 if (!NILP (tem))
18798 {
18799 tem = Fsymbol_value (elt);
18800 /* If value is a string, output that string literally:
18801 don't check for % within it. */
18802 if (STRINGP (tem))
18803 literal = 1;
18804
18805 if (!EQ (tem, elt))
18806 {
18807 /* Give up right away for nil or t. */
18808 elt = tem;
18809 goto tail_recurse;
18810 }
18811 }
18812 }
18813 break;
18814
18815 case Lisp_Cons:
18816 {
18817 register Lisp_Object car, tem;
18818
18819 /* A cons cell: five distinct cases.
18820 If first element is :eval or :propertize, do something special.
18821 If first element is a string or a cons, process all the elements
18822 and effectively concatenate them.
18823 If first element is a negative number, truncate displaying cdr to
18824 at most that many characters. If positive, pad (with spaces)
18825 to at least that many characters.
18826 If first element is a symbol, process the cadr or caddr recursively
18827 according to whether the symbol's value is non-nil or nil. */
18828 car = XCAR (elt);
18829 if (EQ (car, QCeval))
18830 {
18831 /* An element of the form (:eval FORM) means evaluate FORM
18832 and use the result as mode line elements. */
18833
18834 if (risky)
18835 break;
18836
18837 if (CONSP (XCDR (elt)))
18838 {
18839 Lisp_Object spec;
18840 spec = safe_eval (XCAR (XCDR (elt)));
18841 n += display_mode_element (it, depth, field_width - n,
18842 precision - n, spec, props,
18843 risky);
18844 }
18845 }
18846 else if (EQ (car, QCpropertize))
18847 {
18848 /* An element of the form (:propertize ELT PROPS...)
18849 means display ELT but applying properties PROPS. */
18850
18851 if (risky)
18852 break;
18853
18854 if (CONSP (XCDR (elt)))
18855 n += display_mode_element (it, depth, field_width - n,
18856 precision - n, XCAR (XCDR (elt)),
18857 XCDR (XCDR (elt)), risky);
18858 }
18859 else if (SYMBOLP (car))
18860 {
18861 tem = Fboundp (car);
18862 elt = XCDR (elt);
18863 if (!CONSP (elt))
18864 goto invalid;
18865 /* elt is now the cdr, and we know it is a cons cell.
18866 Use its car if CAR has a non-nil value. */
18867 if (!NILP (tem))
18868 {
18869 tem = Fsymbol_value (car);
18870 if (!NILP (tem))
18871 {
18872 elt = XCAR (elt);
18873 goto tail_recurse;
18874 }
18875 }
18876 /* Symbol's value is nil (or symbol is unbound)
18877 Get the cddr of the original list
18878 and if possible find the caddr and use that. */
18879 elt = XCDR (elt);
18880 if (NILP (elt))
18881 break;
18882 else if (!CONSP (elt))
18883 goto invalid;
18884 elt = XCAR (elt);
18885 goto tail_recurse;
18886 }
18887 else if (INTEGERP (car))
18888 {
18889 register int lim = XINT (car);
18890 elt = XCDR (elt);
18891 if (lim < 0)
18892 {
18893 /* Negative int means reduce maximum width. */
18894 if (precision <= 0)
18895 precision = -lim;
18896 else
18897 precision = min (precision, -lim);
18898 }
18899 else if (lim > 0)
18900 {
18901 /* Padding specified. Don't let it be more than
18902 current maximum. */
18903 if (precision > 0)
18904 lim = min (precision, lim);
18905
18906 /* If that's more padding than already wanted, queue it.
18907 But don't reduce padding already specified even if
18908 that is beyond the current truncation point. */
18909 field_width = max (lim, field_width);
18910 }
18911 goto tail_recurse;
18912 }
18913 else if (STRINGP (car) || CONSP (car))
18914 {
18915 Lisp_Object halftail = elt;
18916 int len = 0;
18917
18918 while (CONSP (elt)
18919 && (precision <= 0 || n < precision))
18920 {
18921 n += display_mode_element (it, depth,
18922 /* Do padding only after the last
18923 element in the list. */
18924 (! CONSP (XCDR (elt))
18925 ? field_width - n
18926 : 0),
18927 precision - n, XCAR (elt),
18928 props, risky);
18929 elt = XCDR (elt);
18930 len++;
18931 if ((len & 1) == 0)
18932 halftail = XCDR (halftail);
18933 /* Check for cycle. */
18934 if (EQ (halftail, elt))
18935 break;
18936 }
18937 }
18938 }
18939 break;
18940
18941 default:
18942 invalid:
18943 elt = build_string ("*invalid*");
18944 goto tail_recurse;
18945 }
18946
18947 /* Pad to FIELD_WIDTH. */
18948 if (field_width > 0 && n < field_width)
18949 {
18950 switch (mode_line_target)
18951 {
18952 case MODE_LINE_NOPROP:
18953 case MODE_LINE_TITLE:
18954 n += store_mode_line_noprop ("", field_width - n, 0);
18955 break;
18956 case MODE_LINE_STRING:
18957 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18958 break;
18959 case MODE_LINE_DISPLAY:
18960 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18961 0, 0, 0);
18962 break;
18963 }
18964 }
18965
18966 return n;
18967 }
18968
18969 /* Store a mode-line string element in mode_line_string_list.
18970
18971 If STRING is non-null, display that C string. Otherwise, the Lisp
18972 string LISP_STRING is displayed.
18973
18974 FIELD_WIDTH is the minimum number of output glyphs to produce.
18975 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18976 with spaces. FIELD_WIDTH <= 0 means don't pad.
18977
18978 PRECISION is the maximum number of characters to output from
18979 STRING. PRECISION <= 0 means don't truncate the string.
18980
18981 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18982 properties to the string.
18983
18984 PROPS are the properties to add to the string.
18985 The mode_line_string_face face property is always added to the string.
18986 */
18987
18988 static int
18989 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
18990 int field_width, int precision, Lisp_Object props)
18991 {
18992 EMACS_INT len;
18993 int n = 0;
18994
18995 if (string != NULL)
18996 {
18997 len = strlen (string);
18998 if (precision > 0 && len > precision)
18999 len = precision;
19000 lisp_string = make_string (string, len);
19001 if (NILP (props))
19002 props = mode_line_string_face_prop;
19003 else if (!NILP (mode_line_string_face))
19004 {
19005 Lisp_Object face = Fplist_get (props, Qface);
19006 props = Fcopy_sequence (props);
19007 if (NILP (face))
19008 face = mode_line_string_face;
19009 else
19010 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19011 props = Fplist_put (props, Qface, face);
19012 }
19013 Fadd_text_properties (make_number (0), make_number (len),
19014 props, lisp_string);
19015 }
19016 else
19017 {
19018 len = XFASTINT (Flength (lisp_string));
19019 if (precision > 0 && len > precision)
19020 {
19021 len = precision;
19022 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19023 precision = -1;
19024 }
19025 if (!NILP (mode_line_string_face))
19026 {
19027 Lisp_Object face;
19028 if (NILP (props))
19029 props = Ftext_properties_at (make_number (0), lisp_string);
19030 face = Fplist_get (props, Qface);
19031 if (NILP (face))
19032 face = mode_line_string_face;
19033 else
19034 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19035 props = Fcons (Qface, Fcons (face, Qnil));
19036 if (copy_string)
19037 lisp_string = Fcopy_sequence (lisp_string);
19038 }
19039 if (!NILP (props))
19040 Fadd_text_properties (make_number (0), make_number (len),
19041 props, lisp_string);
19042 }
19043
19044 if (len > 0)
19045 {
19046 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19047 n += len;
19048 }
19049
19050 if (field_width > len)
19051 {
19052 field_width -= len;
19053 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19054 if (!NILP (props))
19055 Fadd_text_properties (make_number (0), make_number (field_width),
19056 props, lisp_string);
19057 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19058 n += field_width;
19059 }
19060
19061 return n;
19062 }
19063
19064
19065 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19066 1, 4, 0,
19067 doc: /* Format a string out of a mode line format specification.
19068 First arg FORMAT specifies the mode line format (see `mode-line-format'
19069 for details) to use.
19070
19071 Optional second arg FACE specifies the face property to put
19072 on all characters for which no face is specified.
19073 The value t means whatever face the window's mode line currently uses
19074 \(either `mode-line' or `mode-line-inactive', depending).
19075 A value of nil means the default is no face property.
19076 If FACE is an integer, the value string has no text properties.
19077
19078 Optional third and fourth args WINDOW and BUFFER specify the window
19079 and buffer to use as the context for the formatting (defaults
19080 are the selected window and the window's buffer). */)
19081 (Lisp_Object format, Lisp_Object face, Lisp_Object window, Lisp_Object buffer)
19082 {
19083 struct it it;
19084 int len;
19085 struct window *w;
19086 struct buffer *old_buffer = NULL;
19087 int face_id = -1;
19088 int no_props = INTEGERP (face);
19089 int count = SPECPDL_INDEX ();
19090 Lisp_Object str;
19091 int string_start = 0;
19092
19093 if (NILP (window))
19094 window = selected_window;
19095 CHECK_WINDOW (window);
19096 w = XWINDOW (window);
19097
19098 if (NILP (buffer))
19099 buffer = w->buffer;
19100 CHECK_BUFFER (buffer);
19101
19102 /* Make formatting the modeline a non-op when noninteractive, otherwise
19103 there will be problems later caused by a partially initialized frame. */
19104 if (NILP (format) || noninteractive)
19105 return empty_unibyte_string;
19106
19107 if (no_props)
19108 face = Qnil;
19109
19110 if (!NILP (face))
19111 {
19112 if (EQ (face, Qt))
19113 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
19114 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
19115 }
19116
19117 if (face_id < 0)
19118 face_id = DEFAULT_FACE_ID;
19119
19120 if (XBUFFER (buffer) != current_buffer)
19121 old_buffer = current_buffer;
19122
19123 /* Save things including mode_line_proptrans_alist,
19124 and set that to nil so that we don't alter the outer value. */
19125 record_unwind_protect (unwind_format_mode_line,
19126 format_mode_line_unwind_data
19127 (old_buffer, selected_window, 1));
19128 mode_line_proptrans_alist = Qnil;
19129
19130 Fselect_window (window, Qt);
19131 if (old_buffer)
19132 set_buffer_internal_1 (XBUFFER (buffer));
19133
19134 init_iterator (&it, w, -1, -1, NULL, face_id);
19135
19136 if (no_props)
19137 {
19138 mode_line_target = MODE_LINE_NOPROP;
19139 mode_line_string_face_prop = Qnil;
19140 mode_line_string_list = Qnil;
19141 string_start = MODE_LINE_NOPROP_LEN (0);
19142 }
19143 else
19144 {
19145 mode_line_target = MODE_LINE_STRING;
19146 mode_line_string_list = Qnil;
19147 mode_line_string_face = face;
19148 mode_line_string_face_prop
19149 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19150 }
19151
19152 push_kboard (FRAME_KBOARD (it.f));
19153 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19154 pop_kboard ();
19155
19156 if (no_props)
19157 {
19158 len = MODE_LINE_NOPROP_LEN (string_start);
19159 str = make_string (mode_line_noprop_buf + string_start, len);
19160 }
19161 else
19162 {
19163 mode_line_string_list = Fnreverse (mode_line_string_list);
19164 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19165 empty_unibyte_string);
19166 }
19167
19168 unbind_to (count, Qnil);
19169 return str;
19170 }
19171
19172 /* Write a null-terminated, right justified decimal representation of
19173 the positive integer D to BUF using a minimal field width WIDTH. */
19174
19175 static void
19176 pint2str (register char *buf, register int width, register int d)
19177 {
19178 register char *p = buf;
19179
19180 if (d <= 0)
19181 *p++ = '0';
19182 else
19183 {
19184 while (d > 0)
19185 {
19186 *p++ = d % 10 + '0';
19187 d /= 10;
19188 }
19189 }
19190
19191 for (width -= (int) (p - buf); width > 0; --width)
19192 *p++ = ' ';
19193 *p-- = '\0';
19194 while (p > buf)
19195 {
19196 d = *buf;
19197 *buf++ = *p;
19198 *p-- = d;
19199 }
19200 }
19201
19202 /* Write a null-terminated, right justified decimal and "human
19203 readable" representation of the nonnegative integer D to BUF using
19204 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19205
19206 static const char power_letter[] =
19207 {
19208 0, /* not used */
19209 'k', /* kilo */
19210 'M', /* mega */
19211 'G', /* giga */
19212 'T', /* tera */
19213 'P', /* peta */
19214 'E', /* exa */
19215 'Z', /* zetta */
19216 'Y' /* yotta */
19217 };
19218
19219 static void
19220 pint2hrstr (char *buf, int width, int d)
19221 {
19222 /* We aim to represent the nonnegative integer D as
19223 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19224 int quotient = d;
19225 int remainder = 0;
19226 /* -1 means: do not use TENTHS. */
19227 int tenths = -1;
19228 int exponent = 0;
19229
19230 /* Length of QUOTIENT.TENTHS as a string. */
19231 int length;
19232
19233 char * psuffix;
19234 char * p;
19235
19236 if (1000 <= quotient)
19237 {
19238 /* Scale to the appropriate EXPONENT. */
19239 do
19240 {
19241 remainder = quotient % 1000;
19242 quotient /= 1000;
19243 exponent++;
19244 }
19245 while (1000 <= quotient);
19246
19247 /* Round to nearest and decide whether to use TENTHS or not. */
19248 if (quotient <= 9)
19249 {
19250 tenths = remainder / 100;
19251 if (50 <= remainder % 100)
19252 {
19253 if (tenths < 9)
19254 tenths++;
19255 else
19256 {
19257 quotient++;
19258 if (quotient == 10)
19259 tenths = -1;
19260 else
19261 tenths = 0;
19262 }
19263 }
19264 }
19265 else
19266 if (500 <= remainder)
19267 {
19268 if (quotient < 999)
19269 quotient++;
19270 else
19271 {
19272 quotient = 1;
19273 exponent++;
19274 tenths = 0;
19275 }
19276 }
19277 }
19278
19279 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19280 if (tenths == -1 && quotient <= 99)
19281 if (quotient <= 9)
19282 length = 1;
19283 else
19284 length = 2;
19285 else
19286 length = 3;
19287 p = psuffix = buf + max (width, length);
19288
19289 /* Print EXPONENT. */
19290 if (exponent)
19291 *psuffix++ = power_letter[exponent];
19292 *psuffix = '\0';
19293
19294 /* Print TENTHS. */
19295 if (tenths >= 0)
19296 {
19297 *--p = '0' + tenths;
19298 *--p = '.';
19299 }
19300
19301 /* Print QUOTIENT. */
19302 do
19303 {
19304 int digit = quotient % 10;
19305 *--p = '0' + digit;
19306 }
19307 while ((quotient /= 10) != 0);
19308
19309 /* Print leading spaces. */
19310 while (buf < p)
19311 *--p = ' ';
19312 }
19313
19314 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19315 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19316 type of CODING_SYSTEM. Return updated pointer into BUF. */
19317
19318 static unsigned char invalid_eol_type[] = "(*invalid*)";
19319
19320 static char *
19321 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19322 {
19323 Lisp_Object val;
19324 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
19325 const unsigned char *eol_str;
19326 int eol_str_len;
19327 /* The EOL conversion we are using. */
19328 Lisp_Object eoltype;
19329
19330 val = CODING_SYSTEM_SPEC (coding_system);
19331 eoltype = Qnil;
19332
19333 if (!VECTORP (val)) /* Not yet decided. */
19334 {
19335 if (multibyte)
19336 *buf++ = '-';
19337 if (eol_flag)
19338 eoltype = eol_mnemonic_undecided;
19339 /* Don't mention EOL conversion if it isn't decided. */
19340 }
19341 else
19342 {
19343 Lisp_Object attrs;
19344 Lisp_Object eolvalue;
19345
19346 attrs = AREF (val, 0);
19347 eolvalue = AREF (val, 2);
19348
19349 if (multibyte)
19350 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19351
19352 if (eol_flag)
19353 {
19354 /* The EOL conversion that is normal on this system. */
19355
19356 if (NILP (eolvalue)) /* Not yet decided. */
19357 eoltype = eol_mnemonic_undecided;
19358 else if (VECTORP (eolvalue)) /* Not yet decided. */
19359 eoltype = eol_mnemonic_undecided;
19360 else /* eolvalue is Qunix, Qdos, or Qmac. */
19361 eoltype = (EQ (eolvalue, Qunix)
19362 ? eol_mnemonic_unix
19363 : (EQ (eolvalue, Qdos) == 1
19364 ? eol_mnemonic_dos : eol_mnemonic_mac));
19365 }
19366 }
19367
19368 if (eol_flag)
19369 {
19370 /* Mention the EOL conversion if it is not the usual one. */
19371 if (STRINGP (eoltype))
19372 {
19373 eol_str = SDATA (eoltype);
19374 eol_str_len = SBYTES (eoltype);
19375 }
19376 else if (CHARACTERP (eoltype))
19377 {
19378 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19379 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19380 eol_str = tmp;
19381 }
19382 else
19383 {
19384 eol_str = invalid_eol_type;
19385 eol_str_len = sizeof (invalid_eol_type) - 1;
19386 }
19387 memcpy (buf, eol_str, eol_str_len);
19388 buf += eol_str_len;
19389 }
19390
19391 return buf;
19392 }
19393
19394 /* Return a string for the output of a mode line %-spec for window W,
19395 generated by character C. PRECISION >= 0 means don't return a
19396 string longer than that value. FIELD_WIDTH > 0 means pad the
19397 string returned with spaces to that value. Return a Lisp string in
19398 *STRING if the resulting string is taken from that Lisp string.
19399
19400 Note we operate on the current buffer for most purposes,
19401 the exception being w->base_line_pos. */
19402
19403 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19404
19405 static const char *
19406 decode_mode_spec (struct window *w, register int c, int field_width,
19407 int precision, Lisp_Object *string)
19408 {
19409 Lisp_Object obj;
19410 struct frame *f = XFRAME (WINDOW_FRAME (w));
19411 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19412 struct buffer *b = current_buffer;
19413
19414 obj = Qnil;
19415 *string = Qnil;
19416
19417 switch (c)
19418 {
19419 case '*':
19420 if (!NILP (b->read_only))
19421 return "%";
19422 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19423 return "*";
19424 return "-";
19425
19426 case '+':
19427 /* This differs from %* only for a modified read-only buffer. */
19428 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19429 return "*";
19430 if (!NILP (b->read_only))
19431 return "%";
19432 return "-";
19433
19434 case '&':
19435 /* This differs from %* in ignoring read-only-ness. */
19436 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19437 return "*";
19438 return "-";
19439
19440 case '%':
19441 return "%";
19442
19443 case '[':
19444 {
19445 int i;
19446 char *p;
19447
19448 if (command_loop_level > 5)
19449 return "[[[... ";
19450 p = decode_mode_spec_buf;
19451 for (i = 0; i < command_loop_level; i++)
19452 *p++ = '[';
19453 *p = 0;
19454 return decode_mode_spec_buf;
19455 }
19456
19457 case ']':
19458 {
19459 int i;
19460 char *p;
19461
19462 if (command_loop_level > 5)
19463 return " ...]]]";
19464 p = decode_mode_spec_buf;
19465 for (i = 0; i < command_loop_level; i++)
19466 *p++ = ']';
19467 *p = 0;
19468 return decode_mode_spec_buf;
19469 }
19470
19471 case '-':
19472 {
19473 register int i;
19474
19475 /* Let lots_of_dashes be a string of infinite length. */
19476 if (mode_line_target == MODE_LINE_NOPROP ||
19477 mode_line_target == MODE_LINE_STRING)
19478 return "--";
19479 if (field_width <= 0
19480 || field_width > sizeof (lots_of_dashes))
19481 {
19482 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19483 decode_mode_spec_buf[i] = '-';
19484 decode_mode_spec_buf[i] = '\0';
19485 return decode_mode_spec_buf;
19486 }
19487 else
19488 return lots_of_dashes;
19489 }
19490
19491 case 'b':
19492 obj = b->name;
19493 break;
19494
19495 case 'c':
19496 /* %c and %l are ignored in `frame-title-format'.
19497 (In redisplay_internal, the frame title is drawn _before_ the
19498 windows are updated, so the stuff which depends on actual
19499 window contents (such as %l) may fail to render properly, or
19500 even crash emacs.) */
19501 if (mode_line_target == MODE_LINE_TITLE)
19502 return "";
19503 else
19504 {
19505 int col = (int) current_column (); /* iftc */
19506 w->column_number_displayed = make_number (col);
19507 pint2str (decode_mode_spec_buf, field_width, col);
19508 return decode_mode_spec_buf;
19509 }
19510
19511 case 'e':
19512 #ifndef SYSTEM_MALLOC
19513 {
19514 if (NILP (Vmemory_full))
19515 return "";
19516 else
19517 return "!MEM FULL! ";
19518 }
19519 #else
19520 return "";
19521 #endif
19522
19523 case 'F':
19524 /* %F displays the frame name. */
19525 if (!NILP (f->title))
19526 return (char *) SDATA (f->title);
19527 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19528 return (char *) SDATA (f->name);
19529 return "Emacs";
19530
19531 case 'f':
19532 obj = b->filename;
19533 break;
19534
19535 case 'i':
19536 {
19537 EMACS_INT size = ZV - BEGV;
19538 pint2str (decode_mode_spec_buf, field_width, size);
19539 return decode_mode_spec_buf;
19540 }
19541
19542 case 'I':
19543 {
19544 EMACS_INT size = ZV - BEGV;
19545 pint2hrstr (decode_mode_spec_buf, field_width, size);
19546 return decode_mode_spec_buf;
19547 }
19548
19549 case 'l':
19550 {
19551 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
19552 int topline, nlines, height;
19553 EMACS_INT junk;
19554
19555 /* %c and %l are ignored in `frame-title-format'. */
19556 if (mode_line_target == MODE_LINE_TITLE)
19557 return "";
19558
19559 startpos = XMARKER (w->start)->charpos;
19560 startpos_byte = marker_byte_position (w->start);
19561 height = WINDOW_TOTAL_LINES (w);
19562
19563 /* If we decided that this buffer isn't suitable for line numbers,
19564 don't forget that too fast. */
19565 if (EQ (w->base_line_pos, w->buffer))
19566 goto no_value;
19567 /* But do forget it, if the window shows a different buffer now. */
19568 else if (BUFFERP (w->base_line_pos))
19569 w->base_line_pos = Qnil;
19570
19571 /* If the buffer is very big, don't waste time. */
19572 if (INTEGERP (Vline_number_display_limit)
19573 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19574 {
19575 w->base_line_pos = Qnil;
19576 w->base_line_number = Qnil;
19577 goto no_value;
19578 }
19579
19580 if (INTEGERP (w->base_line_number)
19581 && INTEGERP (w->base_line_pos)
19582 && XFASTINT (w->base_line_pos) <= startpos)
19583 {
19584 line = XFASTINT (w->base_line_number);
19585 linepos = XFASTINT (w->base_line_pos);
19586 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19587 }
19588 else
19589 {
19590 line = 1;
19591 linepos = BUF_BEGV (b);
19592 linepos_byte = BUF_BEGV_BYTE (b);
19593 }
19594
19595 /* Count lines from base line to window start position. */
19596 nlines = display_count_lines (linepos, linepos_byte,
19597 startpos_byte,
19598 startpos, &junk);
19599
19600 topline = nlines + line;
19601
19602 /* Determine a new base line, if the old one is too close
19603 or too far away, or if we did not have one.
19604 "Too close" means it's plausible a scroll-down would
19605 go back past it. */
19606 if (startpos == BUF_BEGV (b))
19607 {
19608 w->base_line_number = make_number (topline);
19609 w->base_line_pos = make_number (BUF_BEGV (b));
19610 }
19611 else if (nlines < height + 25 || nlines > height * 3 + 50
19612 || linepos == BUF_BEGV (b))
19613 {
19614 EMACS_INT limit = BUF_BEGV (b);
19615 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
19616 EMACS_INT position;
19617 int distance = (height * 2 + 30) * line_number_display_limit_width;
19618
19619 if (startpos - distance > limit)
19620 {
19621 limit = startpos - distance;
19622 limit_byte = CHAR_TO_BYTE (limit);
19623 }
19624
19625 nlines = display_count_lines (startpos, startpos_byte,
19626 limit_byte,
19627 - (height * 2 + 30),
19628 &position);
19629 /* If we couldn't find the lines we wanted within
19630 line_number_display_limit_width chars per line,
19631 give up on line numbers for this window. */
19632 if (position == limit_byte && limit == startpos - distance)
19633 {
19634 w->base_line_pos = w->buffer;
19635 w->base_line_number = Qnil;
19636 goto no_value;
19637 }
19638
19639 w->base_line_number = make_number (topline - nlines);
19640 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19641 }
19642
19643 /* Now count lines from the start pos to point. */
19644 nlines = display_count_lines (startpos, startpos_byte,
19645 PT_BYTE, PT, &junk);
19646
19647 /* Record that we did display the line number. */
19648 line_number_displayed = 1;
19649
19650 /* Make the string to show. */
19651 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19652 return decode_mode_spec_buf;
19653 no_value:
19654 {
19655 char* p = decode_mode_spec_buf;
19656 int pad = field_width - 2;
19657 while (pad-- > 0)
19658 *p++ = ' ';
19659 *p++ = '?';
19660 *p++ = '?';
19661 *p = '\0';
19662 return decode_mode_spec_buf;
19663 }
19664 }
19665 break;
19666
19667 case 'm':
19668 obj = b->mode_name;
19669 break;
19670
19671 case 'n':
19672 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19673 return " Narrow";
19674 break;
19675
19676 case 'p':
19677 {
19678 EMACS_INT pos = marker_position (w->start);
19679 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19680
19681 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19682 {
19683 if (pos <= BUF_BEGV (b))
19684 return "All";
19685 else
19686 return "Bottom";
19687 }
19688 else if (pos <= BUF_BEGV (b))
19689 return "Top";
19690 else
19691 {
19692 if (total > 1000000)
19693 /* Do it differently for a large value, to avoid overflow. */
19694 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19695 else
19696 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19697 /* We can't normally display a 3-digit number,
19698 so get us a 2-digit number that is close. */
19699 if (total == 100)
19700 total = 99;
19701 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19702 return decode_mode_spec_buf;
19703 }
19704 }
19705
19706 /* Display percentage of size above the bottom of the screen. */
19707 case 'P':
19708 {
19709 EMACS_INT toppos = marker_position (w->start);
19710 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19711 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19712
19713 if (botpos >= BUF_ZV (b))
19714 {
19715 if (toppos <= BUF_BEGV (b))
19716 return "All";
19717 else
19718 return "Bottom";
19719 }
19720 else
19721 {
19722 if (total > 1000000)
19723 /* Do it differently for a large value, to avoid overflow. */
19724 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19725 else
19726 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19727 /* We can't normally display a 3-digit number,
19728 so get us a 2-digit number that is close. */
19729 if (total == 100)
19730 total = 99;
19731 if (toppos <= BUF_BEGV (b))
19732 sprintf (decode_mode_spec_buf, "Top%2ld%%", (long)total);
19733 else
19734 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19735 return decode_mode_spec_buf;
19736 }
19737 }
19738
19739 case 's':
19740 /* status of process */
19741 obj = Fget_buffer_process (Fcurrent_buffer ());
19742 if (NILP (obj))
19743 return "no process";
19744 #ifndef MSDOS
19745 obj = Fsymbol_name (Fprocess_status (obj));
19746 #endif
19747 break;
19748
19749 case '@':
19750 {
19751 int count = inhibit_garbage_collection ();
19752 Lisp_Object val = call1 (intern ("file-remote-p"),
19753 current_buffer->directory);
19754 unbind_to (count, Qnil);
19755
19756 if (NILP (val))
19757 return "-";
19758 else
19759 return "@";
19760 }
19761
19762 case 't': /* indicate TEXT or BINARY */
19763 #ifdef MODE_LINE_BINARY_TEXT
19764 return MODE_LINE_BINARY_TEXT (b);
19765 #else
19766 return "T";
19767 #endif
19768
19769 case 'z':
19770 /* coding-system (not including end-of-line format) */
19771 case 'Z':
19772 /* coding-system (including end-of-line type) */
19773 {
19774 int eol_flag = (c == 'Z');
19775 char *p = decode_mode_spec_buf;
19776
19777 if (! FRAME_WINDOW_P (f))
19778 {
19779 /* No need to mention EOL here--the terminal never needs
19780 to do EOL conversion. */
19781 p = decode_mode_spec_coding (CODING_ID_NAME
19782 (FRAME_KEYBOARD_CODING (f)->id),
19783 p, 0);
19784 p = decode_mode_spec_coding (CODING_ID_NAME
19785 (FRAME_TERMINAL_CODING (f)->id),
19786 p, 0);
19787 }
19788 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19789 p, eol_flag);
19790
19791 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19792 #ifdef subprocesses
19793 obj = Fget_buffer_process (Fcurrent_buffer ());
19794 if (PROCESSP (obj))
19795 {
19796 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19797 p, eol_flag);
19798 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19799 p, eol_flag);
19800 }
19801 #endif /* subprocesses */
19802 #endif /* 0 */
19803 *p = 0;
19804 return decode_mode_spec_buf;
19805 }
19806 }
19807
19808 if (STRINGP (obj))
19809 {
19810 *string = obj;
19811 return (char *) SDATA (obj);
19812 }
19813 else
19814 return "";
19815 }
19816
19817
19818 /* Count up to COUNT lines starting from START / START_BYTE.
19819 But don't go beyond LIMIT_BYTE.
19820 Return the number of lines thus found (always nonnegative).
19821
19822 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19823
19824 static int
19825 display_count_lines (EMACS_INT start, EMACS_INT start_byte,
19826 EMACS_INT limit_byte, int count,
19827 EMACS_INT *byte_pos_ptr)
19828 {
19829 register unsigned char *cursor;
19830 unsigned char *base;
19831
19832 register int ceiling;
19833 register unsigned char *ceiling_addr;
19834 int orig_count = count;
19835
19836 /* If we are not in selective display mode,
19837 check only for newlines. */
19838 int selective_display = (!NILP (current_buffer->selective_display)
19839 && !INTEGERP (current_buffer->selective_display));
19840
19841 if (count > 0)
19842 {
19843 while (start_byte < limit_byte)
19844 {
19845 ceiling = BUFFER_CEILING_OF (start_byte);
19846 ceiling = min (limit_byte - 1, ceiling);
19847 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19848 base = (cursor = BYTE_POS_ADDR (start_byte));
19849 while (1)
19850 {
19851 if (selective_display)
19852 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19853 ;
19854 else
19855 while (*cursor != '\n' && ++cursor != ceiling_addr)
19856 ;
19857
19858 if (cursor != ceiling_addr)
19859 {
19860 if (--count == 0)
19861 {
19862 start_byte += cursor - base + 1;
19863 *byte_pos_ptr = start_byte;
19864 return orig_count;
19865 }
19866 else
19867 if (++cursor == ceiling_addr)
19868 break;
19869 }
19870 else
19871 break;
19872 }
19873 start_byte += cursor - base;
19874 }
19875 }
19876 else
19877 {
19878 while (start_byte > limit_byte)
19879 {
19880 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19881 ceiling = max (limit_byte, ceiling);
19882 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19883 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19884 while (1)
19885 {
19886 if (selective_display)
19887 while (--cursor != ceiling_addr
19888 && *cursor != '\n' && *cursor != 015)
19889 ;
19890 else
19891 while (--cursor != ceiling_addr && *cursor != '\n')
19892 ;
19893
19894 if (cursor != ceiling_addr)
19895 {
19896 if (++count == 0)
19897 {
19898 start_byte += cursor - base + 1;
19899 *byte_pos_ptr = start_byte;
19900 /* When scanning backwards, we should
19901 not count the newline posterior to which we stop. */
19902 return - orig_count - 1;
19903 }
19904 }
19905 else
19906 break;
19907 }
19908 /* Here we add 1 to compensate for the last decrement
19909 of CURSOR, which took it past the valid range. */
19910 start_byte += cursor - base + 1;
19911 }
19912 }
19913
19914 *byte_pos_ptr = limit_byte;
19915
19916 if (count < 0)
19917 return - orig_count + count;
19918 return orig_count - count;
19919
19920 }
19921
19922
19923 \f
19924 /***********************************************************************
19925 Displaying strings
19926 ***********************************************************************/
19927
19928 /* Display a NUL-terminated string, starting with index START.
19929
19930 If STRING is non-null, display that C string. Otherwise, the Lisp
19931 string LISP_STRING is displayed. There's a case that STRING is
19932 non-null and LISP_STRING is not nil. It means STRING is a string
19933 data of LISP_STRING. In that case, we display LISP_STRING while
19934 ignoring its text properties.
19935
19936 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19937 FACE_STRING. Display STRING or LISP_STRING with the face at
19938 FACE_STRING_POS in FACE_STRING:
19939
19940 Display the string in the environment given by IT, but use the
19941 standard display table, temporarily.
19942
19943 FIELD_WIDTH is the minimum number of output glyphs to produce.
19944 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19945 with spaces. If STRING has more characters, more than FIELD_WIDTH
19946 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19947
19948 PRECISION is the maximum number of characters to output from
19949 STRING. PRECISION < 0 means don't truncate the string.
19950
19951 This is roughly equivalent to printf format specifiers:
19952
19953 FIELD_WIDTH PRECISION PRINTF
19954 ----------------------------------------
19955 -1 -1 %s
19956 -1 10 %.10s
19957 10 -1 %10s
19958 20 10 %20.10s
19959
19960 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19961 display them, and < 0 means obey the current buffer's value of
19962 enable_multibyte_characters.
19963
19964 Value is the number of columns displayed. */
19965
19966 static int
19967 display_string (const unsigned char *string, Lisp_Object lisp_string, Lisp_Object face_string,
19968 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
19969 int field_width, int precision, int max_x, int multibyte)
19970 {
19971 int hpos_at_start = it->hpos;
19972 int saved_face_id = it->face_id;
19973 struct glyph_row *row = it->glyph_row;
19974
19975 /* Initialize the iterator IT for iteration over STRING beginning
19976 with index START. */
19977 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19978 precision, field_width, multibyte);
19979 if (string && STRINGP (lisp_string))
19980 /* LISP_STRING is the one returned by decode_mode_spec. We should
19981 ignore its text properties. */
19982 it->stop_charpos = -1;
19983
19984 /* If displaying STRING, set up the face of the iterator
19985 from LISP_STRING, if that's given. */
19986 if (STRINGP (face_string))
19987 {
19988 EMACS_INT endptr;
19989 struct face *face;
19990
19991 it->face_id
19992 = face_at_string_position (it->w, face_string, face_string_pos,
19993 0, it->region_beg_charpos,
19994 it->region_end_charpos,
19995 &endptr, it->base_face_id, 0);
19996 face = FACE_FROM_ID (it->f, it->face_id);
19997 it->face_box_p = face->box != FACE_NO_BOX;
19998 }
19999
20000 /* Set max_x to the maximum allowed X position. Don't let it go
20001 beyond the right edge of the window. */
20002 if (max_x <= 0)
20003 max_x = it->last_visible_x;
20004 else
20005 max_x = min (max_x, it->last_visible_x);
20006
20007 /* Skip over display elements that are not visible. because IT->w is
20008 hscrolled. */
20009 if (it->current_x < it->first_visible_x)
20010 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20011 MOVE_TO_POS | MOVE_TO_X);
20012
20013 row->ascent = it->max_ascent;
20014 row->height = it->max_ascent + it->max_descent;
20015 row->phys_ascent = it->max_phys_ascent;
20016 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20017 row->extra_line_spacing = it->max_extra_line_spacing;
20018
20019 /* This condition is for the case that we are called with current_x
20020 past last_visible_x. */
20021 while (it->current_x < max_x)
20022 {
20023 int x_before, x, n_glyphs_before, i, nglyphs;
20024
20025 /* Get the next display element. */
20026 if (!get_next_display_element (it))
20027 break;
20028
20029 /* Produce glyphs. */
20030 x_before = it->current_x;
20031 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
20032 PRODUCE_GLYPHS (it);
20033
20034 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
20035 i = 0;
20036 x = x_before;
20037 while (i < nglyphs)
20038 {
20039 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20040
20041 if (it->line_wrap != TRUNCATE
20042 && x + glyph->pixel_width > max_x)
20043 {
20044 /* End of continued line or max_x reached. */
20045 if (CHAR_GLYPH_PADDING_P (*glyph))
20046 {
20047 /* A wide character is unbreakable. */
20048 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
20049 it->current_x = x_before;
20050 }
20051 else
20052 {
20053 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
20054 it->current_x = x;
20055 }
20056 break;
20057 }
20058 else if (x + glyph->pixel_width >= it->first_visible_x)
20059 {
20060 /* Glyph is at least partially visible. */
20061 ++it->hpos;
20062 if (x < it->first_visible_x)
20063 it->glyph_row->x = x - it->first_visible_x;
20064 }
20065 else
20066 {
20067 /* Glyph is off the left margin of the display area.
20068 Should not happen. */
20069 abort ();
20070 }
20071
20072 row->ascent = max (row->ascent, it->max_ascent);
20073 row->height = max (row->height, it->max_ascent + it->max_descent);
20074 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20075 row->phys_height = max (row->phys_height,
20076 it->max_phys_ascent + it->max_phys_descent);
20077 row->extra_line_spacing = max (row->extra_line_spacing,
20078 it->max_extra_line_spacing);
20079 x += glyph->pixel_width;
20080 ++i;
20081 }
20082
20083 /* Stop if max_x reached. */
20084 if (i < nglyphs)
20085 break;
20086
20087 /* Stop at line ends. */
20088 if (ITERATOR_AT_END_OF_LINE_P (it))
20089 {
20090 it->continuation_lines_width = 0;
20091 break;
20092 }
20093
20094 set_iterator_to_next (it, 1);
20095
20096 /* Stop if truncating at the right edge. */
20097 if (it->line_wrap == TRUNCATE
20098 && it->current_x >= it->last_visible_x)
20099 {
20100 /* Add truncation mark, but don't do it if the line is
20101 truncated at a padding space. */
20102 if (IT_CHARPOS (*it) < it->string_nchars)
20103 {
20104 if (!FRAME_WINDOW_P (it->f))
20105 {
20106 int i, n;
20107
20108 if (it->current_x > it->last_visible_x)
20109 {
20110 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20111 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20112 break;
20113 for (n = row->used[TEXT_AREA]; i < n; ++i)
20114 {
20115 row->used[TEXT_AREA] = i;
20116 produce_special_glyphs (it, IT_TRUNCATION);
20117 }
20118 }
20119 produce_special_glyphs (it, IT_TRUNCATION);
20120 }
20121 it->glyph_row->truncated_on_right_p = 1;
20122 }
20123 break;
20124 }
20125 }
20126
20127 /* Maybe insert a truncation at the left. */
20128 if (it->first_visible_x
20129 && IT_CHARPOS (*it) > 0)
20130 {
20131 if (!FRAME_WINDOW_P (it->f))
20132 insert_left_trunc_glyphs (it);
20133 it->glyph_row->truncated_on_left_p = 1;
20134 }
20135
20136 it->face_id = saved_face_id;
20137
20138 /* Value is number of columns displayed. */
20139 return it->hpos - hpos_at_start;
20140 }
20141
20142
20143 \f
20144 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20145 appears as an element of LIST or as the car of an element of LIST.
20146 If PROPVAL is a list, compare each element against LIST in that
20147 way, and return 1/2 if any element of PROPVAL is found in LIST.
20148 Otherwise return 0. This function cannot quit.
20149 The return value is 2 if the text is invisible but with an ellipsis
20150 and 1 if it's invisible and without an ellipsis. */
20151
20152 int
20153 invisible_p (register Lisp_Object propval, Lisp_Object list)
20154 {
20155 register Lisp_Object tail, proptail;
20156
20157 for (tail = list; CONSP (tail); tail = XCDR (tail))
20158 {
20159 register Lisp_Object tem;
20160 tem = XCAR (tail);
20161 if (EQ (propval, tem))
20162 return 1;
20163 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20164 return NILP (XCDR (tem)) ? 1 : 2;
20165 }
20166
20167 if (CONSP (propval))
20168 {
20169 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20170 {
20171 Lisp_Object propelt;
20172 propelt = XCAR (proptail);
20173 for (tail = list; CONSP (tail); tail = XCDR (tail))
20174 {
20175 register Lisp_Object tem;
20176 tem = XCAR (tail);
20177 if (EQ (propelt, tem))
20178 return 1;
20179 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20180 return NILP (XCDR (tem)) ? 1 : 2;
20181 }
20182 }
20183 }
20184
20185 return 0;
20186 }
20187
20188 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20189 doc: /* Non-nil if the property makes the text invisible.
20190 POS-OR-PROP can be a marker or number, in which case it is taken to be
20191 a position in the current buffer and the value of the `invisible' property
20192 is checked; or it can be some other value, which is then presumed to be the
20193 value of the `invisible' property of the text of interest.
20194 The non-nil value returned can be t for truly invisible text or something
20195 else if the text is replaced by an ellipsis. */)
20196 (Lisp_Object pos_or_prop)
20197 {
20198 Lisp_Object prop
20199 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20200 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20201 : pos_or_prop);
20202 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20203 return (invis == 0 ? Qnil
20204 : invis == 1 ? Qt
20205 : make_number (invis));
20206 }
20207
20208 /* Calculate a width or height in pixels from a specification using
20209 the following elements:
20210
20211 SPEC ::=
20212 NUM - a (fractional) multiple of the default font width/height
20213 (NUM) - specifies exactly NUM pixels
20214 UNIT - a fixed number of pixels, see below.
20215 ELEMENT - size of a display element in pixels, see below.
20216 (NUM . SPEC) - equals NUM * SPEC
20217 (+ SPEC SPEC ...) - add pixel values
20218 (- SPEC SPEC ...) - subtract pixel values
20219 (- SPEC) - negate pixel value
20220
20221 NUM ::=
20222 INT or FLOAT - a number constant
20223 SYMBOL - use symbol's (buffer local) variable binding.
20224
20225 UNIT ::=
20226 in - pixels per inch *)
20227 mm - pixels per 1/1000 meter *)
20228 cm - pixels per 1/100 meter *)
20229 width - width of current font in pixels.
20230 height - height of current font in pixels.
20231
20232 *) using the ratio(s) defined in display-pixels-per-inch.
20233
20234 ELEMENT ::=
20235
20236 left-fringe - left fringe width in pixels
20237 right-fringe - right fringe width in pixels
20238
20239 left-margin - left margin width in pixels
20240 right-margin - right margin width in pixels
20241
20242 scroll-bar - scroll-bar area width in pixels
20243
20244 Examples:
20245
20246 Pixels corresponding to 5 inches:
20247 (5 . in)
20248
20249 Total width of non-text areas on left side of window (if scroll-bar is on left):
20250 '(space :width (+ left-fringe left-margin scroll-bar))
20251
20252 Align to first text column (in header line):
20253 '(space :align-to 0)
20254
20255 Align to middle of text area minus half the width of variable `my-image'
20256 containing a loaded image:
20257 '(space :align-to (0.5 . (- text my-image)))
20258
20259 Width of left margin minus width of 1 character in the default font:
20260 '(space :width (- left-margin 1))
20261
20262 Width of left margin minus width of 2 characters in the current font:
20263 '(space :width (- left-margin (2 . width)))
20264
20265 Center 1 character over left-margin (in header line):
20266 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20267
20268 Different ways to express width of left fringe plus left margin minus one pixel:
20269 '(space :width (- (+ left-fringe left-margin) (1)))
20270 '(space :width (+ left-fringe left-margin (- (1))))
20271 '(space :width (+ left-fringe left-margin (-1)))
20272
20273 */
20274
20275 #define NUMVAL(X) \
20276 ((INTEGERP (X) || FLOATP (X)) \
20277 ? XFLOATINT (X) \
20278 : - 1)
20279
20280 int
20281 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20282 struct font *font, int width_p, int *align_to)
20283 {
20284 double pixels;
20285
20286 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20287 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20288
20289 if (NILP (prop))
20290 return OK_PIXELS (0);
20291
20292 xassert (FRAME_LIVE_P (it->f));
20293
20294 if (SYMBOLP (prop))
20295 {
20296 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20297 {
20298 char *unit = SDATA (SYMBOL_NAME (prop));
20299
20300 if (unit[0] == 'i' && unit[1] == 'n')
20301 pixels = 1.0;
20302 else if (unit[0] == 'm' && unit[1] == 'm')
20303 pixels = 25.4;
20304 else if (unit[0] == 'c' && unit[1] == 'm')
20305 pixels = 2.54;
20306 else
20307 pixels = 0;
20308 if (pixels > 0)
20309 {
20310 double ppi;
20311 #ifdef HAVE_WINDOW_SYSTEM
20312 if (FRAME_WINDOW_P (it->f)
20313 && (ppi = (width_p
20314 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20315 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20316 ppi > 0))
20317 return OK_PIXELS (ppi / pixels);
20318 #endif
20319
20320 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20321 || (CONSP (Vdisplay_pixels_per_inch)
20322 && (ppi = (width_p
20323 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20324 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20325 ppi > 0)))
20326 return OK_PIXELS (ppi / pixels);
20327
20328 return 0;
20329 }
20330 }
20331
20332 #ifdef HAVE_WINDOW_SYSTEM
20333 if (EQ (prop, Qheight))
20334 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20335 if (EQ (prop, Qwidth))
20336 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20337 #else
20338 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20339 return OK_PIXELS (1);
20340 #endif
20341
20342 if (EQ (prop, Qtext))
20343 return OK_PIXELS (width_p
20344 ? window_box_width (it->w, TEXT_AREA)
20345 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20346
20347 if (align_to && *align_to < 0)
20348 {
20349 *res = 0;
20350 if (EQ (prop, Qleft))
20351 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20352 if (EQ (prop, Qright))
20353 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20354 if (EQ (prop, Qcenter))
20355 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20356 + window_box_width (it->w, TEXT_AREA) / 2);
20357 if (EQ (prop, Qleft_fringe))
20358 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20359 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20360 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20361 if (EQ (prop, Qright_fringe))
20362 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20363 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20364 : window_box_right_offset (it->w, TEXT_AREA));
20365 if (EQ (prop, Qleft_margin))
20366 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20367 if (EQ (prop, Qright_margin))
20368 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20369 if (EQ (prop, Qscroll_bar))
20370 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20371 ? 0
20372 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20373 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20374 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20375 : 0)));
20376 }
20377 else
20378 {
20379 if (EQ (prop, Qleft_fringe))
20380 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20381 if (EQ (prop, Qright_fringe))
20382 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20383 if (EQ (prop, Qleft_margin))
20384 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20385 if (EQ (prop, Qright_margin))
20386 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20387 if (EQ (prop, Qscroll_bar))
20388 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20389 }
20390
20391 prop = Fbuffer_local_value (prop, it->w->buffer);
20392 }
20393
20394 if (INTEGERP (prop) || FLOATP (prop))
20395 {
20396 int base_unit = (width_p
20397 ? FRAME_COLUMN_WIDTH (it->f)
20398 : FRAME_LINE_HEIGHT (it->f));
20399 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20400 }
20401
20402 if (CONSP (prop))
20403 {
20404 Lisp_Object car = XCAR (prop);
20405 Lisp_Object cdr = XCDR (prop);
20406
20407 if (SYMBOLP (car))
20408 {
20409 #ifdef HAVE_WINDOW_SYSTEM
20410 if (FRAME_WINDOW_P (it->f)
20411 && valid_image_p (prop))
20412 {
20413 int id = lookup_image (it->f, prop);
20414 struct image *img = IMAGE_FROM_ID (it->f, id);
20415
20416 return OK_PIXELS (width_p ? img->width : img->height);
20417 }
20418 #endif
20419 if (EQ (car, Qplus) || EQ (car, Qminus))
20420 {
20421 int first = 1;
20422 double px;
20423
20424 pixels = 0;
20425 while (CONSP (cdr))
20426 {
20427 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20428 font, width_p, align_to))
20429 return 0;
20430 if (first)
20431 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20432 else
20433 pixels += px;
20434 cdr = XCDR (cdr);
20435 }
20436 if (EQ (car, Qminus))
20437 pixels = -pixels;
20438 return OK_PIXELS (pixels);
20439 }
20440
20441 car = Fbuffer_local_value (car, it->w->buffer);
20442 }
20443
20444 if (INTEGERP (car) || FLOATP (car))
20445 {
20446 double fact;
20447 pixels = XFLOATINT (car);
20448 if (NILP (cdr))
20449 return OK_PIXELS (pixels);
20450 if (calc_pixel_width_or_height (&fact, it, cdr,
20451 font, width_p, align_to))
20452 return OK_PIXELS (pixels * fact);
20453 return 0;
20454 }
20455
20456 return 0;
20457 }
20458
20459 return 0;
20460 }
20461
20462 \f
20463 /***********************************************************************
20464 Glyph Display
20465 ***********************************************************************/
20466
20467 #ifdef HAVE_WINDOW_SYSTEM
20468
20469 #if GLYPH_DEBUG
20470
20471 void
20472 dump_glyph_string (s)
20473 struct glyph_string *s;
20474 {
20475 fprintf (stderr, "glyph string\n");
20476 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20477 s->x, s->y, s->width, s->height);
20478 fprintf (stderr, " ybase = %d\n", s->ybase);
20479 fprintf (stderr, " hl = %d\n", s->hl);
20480 fprintf (stderr, " left overhang = %d, right = %d\n",
20481 s->left_overhang, s->right_overhang);
20482 fprintf (stderr, " nchars = %d\n", s->nchars);
20483 fprintf (stderr, " extends to end of line = %d\n",
20484 s->extends_to_end_of_line_p);
20485 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20486 fprintf (stderr, " bg width = %d\n", s->background_width);
20487 }
20488
20489 #endif /* GLYPH_DEBUG */
20490
20491 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20492 of XChar2b structures for S; it can't be allocated in
20493 init_glyph_string because it must be allocated via `alloca'. W
20494 is the window on which S is drawn. ROW and AREA are the glyph row
20495 and area within the row from which S is constructed. START is the
20496 index of the first glyph structure covered by S. HL is a
20497 face-override for drawing S. */
20498
20499 #ifdef HAVE_NTGUI
20500 #define OPTIONAL_HDC(hdc) HDC hdc,
20501 #define DECLARE_HDC(hdc) HDC hdc;
20502 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20503 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20504 #endif
20505
20506 #ifndef OPTIONAL_HDC
20507 #define OPTIONAL_HDC(hdc)
20508 #define DECLARE_HDC(hdc)
20509 #define ALLOCATE_HDC(hdc, f)
20510 #define RELEASE_HDC(hdc, f)
20511 #endif
20512
20513 static void
20514 init_glyph_string (struct glyph_string *s,
20515 OPTIONAL_HDC (hdc)
20516 XChar2b *char2b, struct window *w, struct glyph_row *row,
20517 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20518 {
20519 memset (s, 0, sizeof *s);
20520 s->w = w;
20521 s->f = XFRAME (w->frame);
20522 #ifdef HAVE_NTGUI
20523 s->hdc = hdc;
20524 #endif
20525 s->display = FRAME_X_DISPLAY (s->f);
20526 s->window = FRAME_X_WINDOW (s->f);
20527 s->char2b = char2b;
20528 s->hl = hl;
20529 s->row = row;
20530 s->area = area;
20531 s->first_glyph = row->glyphs[area] + start;
20532 s->height = row->height;
20533 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20534 s->ybase = s->y + row->ascent;
20535 }
20536
20537
20538 /* Append the list of glyph strings with head H and tail T to the list
20539 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20540
20541 static INLINE void
20542 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20543 struct glyph_string *h, struct glyph_string *t)
20544 {
20545 if (h)
20546 {
20547 if (*head)
20548 (*tail)->next = h;
20549 else
20550 *head = h;
20551 h->prev = *tail;
20552 *tail = t;
20553 }
20554 }
20555
20556
20557 /* Prepend the list of glyph strings with head H and tail T to the
20558 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20559 result. */
20560
20561 static INLINE void
20562 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20563 struct glyph_string *h, struct glyph_string *t)
20564 {
20565 if (h)
20566 {
20567 if (*head)
20568 (*head)->prev = t;
20569 else
20570 *tail = t;
20571 t->next = *head;
20572 *head = h;
20573 }
20574 }
20575
20576
20577 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20578 Set *HEAD and *TAIL to the resulting list. */
20579
20580 static INLINE void
20581 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20582 struct glyph_string *s)
20583 {
20584 s->next = s->prev = NULL;
20585 append_glyph_string_lists (head, tail, s, s);
20586 }
20587
20588
20589 /* Get face and two-byte form of character C in face FACE_ID on frame
20590 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20591 means we want to display multibyte text. DISPLAY_P non-zero means
20592 make sure that X resources for the face returned are allocated.
20593 Value is a pointer to a realized face that is ready for display if
20594 DISPLAY_P is non-zero. */
20595
20596 static INLINE struct face *
20597 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20598 XChar2b *char2b, int multibyte_p, int display_p)
20599 {
20600 struct face *face = FACE_FROM_ID (f, face_id);
20601
20602 if (face->font)
20603 {
20604 unsigned code = face->font->driver->encode_char (face->font, c);
20605
20606 if (code != FONT_INVALID_CODE)
20607 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20608 else
20609 STORE_XCHAR2B (char2b, 0, 0);
20610 }
20611
20612 /* Make sure X resources of the face are allocated. */
20613 #ifdef HAVE_X_WINDOWS
20614 if (display_p)
20615 #endif
20616 {
20617 xassert (face != NULL);
20618 PREPARE_FACE_FOR_DISPLAY (f, face);
20619 }
20620
20621 return face;
20622 }
20623
20624
20625 /* Get face and two-byte form of character glyph GLYPH on frame F.
20626 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20627 a pointer to a realized face that is ready for display. */
20628
20629 static INLINE struct face *
20630 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20631 XChar2b *char2b, int *two_byte_p)
20632 {
20633 struct face *face;
20634
20635 xassert (glyph->type == CHAR_GLYPH);
20636 face = FACE_FROM_ID (f, glyph->face_id);
20637
20638 if (two_byte_p)
20639 *two_byte_p = 0;
20640
20641 if (face->font)
20642 {
20643 unsigned code;
20644
20645 if (CHAR_BYTE8_P (glyph->u.ch))
20646 code = CHAR_TO_BYTE8 (glyph->u.ch);
20647 else
20648 code = face->font->driver->encode_char (face->font, glyph->u.ch);
20649
20650 if (code != FONT_INVALID_CODE)
20651 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20652 else
20653 STORE_XCHAR2B (char2b, 0, 0);
20654 }
20655
20656 /* Make sure X resources of the face are allocated. */
20657 xassert (face != NULL);
20658 PREPARE_FACE_FOR_DISPLAY (f, face);
20659 return face;
20660 }
20661
20662
20663 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
20664 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
20665
20666 static INLINE int
20667 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
20668 {
20669 unsigned code;
20670
20671 if (CHAR_BYTE8_P (c))
20672 code = CHAR_TO_BYTE8 (c);
20673 else
20674 code = font->driver->encode_char (font, c);
20675
20676 if (code == FONT_INVALID_CODE)
20677 return 0;
20678 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20679 return 1;
20680 }
20681
20682
20683 /* Fill glyph string S with composition components specified by S->cmp.
20684
20685 BASE_FACE is the base face of the composition.
20686 S->cmp_from is the index of the first component for S.
20687
20688 OVERLAPS non-zero means S should draw the foreground only, and use
20689 its physical height for clipping. See also draw_glyphs.
20690
20691 Value is the index of a component not in S. */
20692
20693 static int
20694 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20695 int overlaps)
20696 {
20697 int i;
20698 /* For all glyphs of this composition, starting at the offset
20699 S->cmp_from, until we reach the end of the definition or encounter a
20700 glyph that requires the different face, add it to S. */
20701 struct face *face;
20702
20703 xassert (s);
20704
20705 s->for_overlaps = overlaps;
20706 s->face = NULL;
20707 s->font = NULL;
20708 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20709 {
20710 int c = COMPOSITION_GLYPH (s->cmp, i);
20711
20712 if (c != '\t')
20713 {
20714 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20715 -1, Qnil);
20716
20717 face = get_char_face_and_encoding (s->f, c, face_id,
20718 s->char2b + i, 1, 1);
20719 if (face)
20720 {
20721 if (! s->face)
20722 {
20723 s->face = face;
20724 s->font = s->face->font;
20725 }
20726 else if (s->face != face)
20727 break;
20728 }
20729 }
20730 ++s->nchars;
20731 }
20732 s->cmp_to = i;
20733
20734 /* All glyph strings for the same composition has the same width,
20735 i.e. the width set for the first component of the composition. */
20736 s->width = s->first_glyph->pixel_width;
20737
20738 /* If the specified font could not be loaded, use the frame's
20739 default font, but record the fact that we couldn't load it in
20740 the glyph string so that we can draw rectangles for the
20741 characters of the glyph string. */
20742 if (s->font == NULL)
20743 {
20744 s->font_not_found_p = 1;
20745 s->font = FRAME_FONT (s->f);
20746 }
20747
20748 /* Adjust base line for subscript/superscript text. */
20749 s->ybase += s->first_glyph->voffset;
20750
20751 /* This glyph string must always be drawn with 16-bit functions. */
20752 s->two_byte_p = 1;
20753
20754 return s->cmp_to;
20755 }
20756
20757 static int
20758 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
20759 int start, int end, int overlaps)
20760 {
20761 struct glyph *glyph, *last;
20762 Lisp_Object lgstring;
20763 int i;
20764
20765 s->for_overlaps = overlaps;
20766 glyph = s->row->glyphs[s->area] + start;
20767 last = s->row->glyphs[s->area] + end;
20768 s->cmp_id = glyph->u.cmp.id;
20769 s->cmp_from = glyph->slice.cmp.from;
20770 s->cmp_to = glyph->slice.cmp.to + 1;
20771 s->face = FACE_FROM_ID (s->f, face_id);
20772 lgstring = composition_gstring_from_id (s->cmp_id);
20773 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20774 glyph++;
20775 while (glyph < last
20776 && glyph->u.cmp.automatic
20777 && glyph->u.cmp.id == s->cmp_id
20778 && s->cmp_to == glyph->slice.cmp.from)
20779 s->cmp_to = (glyph++)->slice.cmp.to + 1;
20780
20781 for (i = s->cmp_from; i < s->cmp_to; i++)
20782 {
20783 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20784 unsigned code = LGLYPH_CODE (lglyph);
20785
20786 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20787 }
20788 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20789 return glyph - s->row->glyphs[s->area];
20790 }
20791
20792
20793 /* Fill glyph string S from a sequence glyphs for glyphless characters.
20794 See the comment of fill_glyph_string for arguments.
20795 Value is the index of the first glyph not in S. */
20796
20797
20798 static int
20799 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
20800 int start, int end, int overlaps)
20801 {
20802 struct glyph *glyph, *last;
20803 int voffset;
20804
20805 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
20806 s->for_overlaps = overlaps;
20807 glyph = s->row->glyphs[s->area] + start;
20808 last = s->row->glyphs[s->area] + end;
20809 voffset = glyph->voffset;
20810 s->face = FACE_FROM_ID (s->f, face_id);
20811 s->font = s->face->font;
20812 s->nchars = 1;
20813 s->width = glyph->pixel_width;
20814 glyph++;
20815 while (glyph < last
20816 && glyph->type == GLYPHLESS_GLYPH
20817 && glyph->voffset == voffset
20818 && glyph->face_id == face_id)
20819 {
20820 s->nchars++;
20821 s->width += glyph->pixel_width;
20822 glyph++;
20823 }
20824 s->ybase += voffset;
20825 return glyph - s->row->glyphs[s->area];
20826 }
20827
20828
20829 /* Fill glyph string S from a sequence of character glyphs.
20830
20831 FACE_ID is the face id of the string. START is the index of the
20832 first glyph to consider, END is the index of the last + 1.
20833 OVERLAPS non-zero means S should draw the foreground only, and use
20834 its physical height for clipping. See also draw_glyphs.
20835
20836 Value is the index of the first glyph not in S. */
20837
20838 static int
20839 fill_glyph_string (struct glyph_string *s, int face_id,
20840 int start, int end, int overlaps)
20841 {
20842 struct glyph *glyph, *last;
20843 int voffset;
20844 int glyph_not_available_p;
20845
20846 xassert (s->f == XFRAME (s->w->frame));
20847 xassert (s->nchars == 0);
20848 xassert (start >= 0 && end > start);
20849
20850 s->for_overlaps = overlaps;
20851 glyph = s->row->glyphs[s->area] + start;
20852 last = s->row->glyphs[s->area] + end;
20853 voffset = glyph->voffset;
20854 s->padding_p = glyph->padding_p;
20855 glyph_not_available_p = glyph->glyph_not_available_p;
20856
20857 while (glyph < last
20858 && glyph->type == CHAR_GLYPH
20859 && glyph->voffset == voffset
20860 /* Same face id implies same font, nowadays. */
20861 && glyph->face_id == face_id
20862 && glyph->glyph_not_available_p == glyph_not_available_p)
20863 {
20864 int two_byte_p;
20865
20866 s->face = get_glyph_face_and_encoding (s->f, glyph,
20867 s->char2b + s->nchars,
20868 &two_byte_p);
20869 s->two_byte_p = two_byte_p;
20870 ++s->nchars;
20871 xassert (s->nchars <= end - start);
20872 s->width += glyph->pixel_width;
20873 if (glyph++->padding_p != s->padding_p)
20874 break;
20875 }
20876
20877 s->font = s->face->font;
20878
20879 /* If the specified font could not be loaded, use the frame's font,
20880 but record the fact that we couldn't load it in
20881 S->font_not_found_p so that we can draw rectangles for the
20882 characters of the glyph string. */
20883 if (s->font == NULL || glyph_not_available_p)
20884 {
20885 s->font_not_found_p = 1;
20886 s->font = FRAME_FONT (s->f);
20887 }
20888
20889 /* Adjust base line for subscript/superscript text. */
20890 s->ybase += voffset;
20891
20892 xassert (s->face && s->face->gc);
20893 return glyph - s->row->glyphs[s->area];
20894 }
20895
20896
20897 /* Fill glyph string S from image glyph S->first_glyph. */
20898
20899 static void
20900 fill_image_glyph_string (struct glyph_string *s)
20901 {
20902 xassert (s->first_glyph->type == IMAGE_GLYPH);
20903 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20904 xassert (s->img);
20905 s->slice = s->first_glyph->slice.img;
20906 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20907 s->font = s->face->font;
20908 s->width = s->first_glyph->pixel_width;
20909
20910 /* Adjust base line for subscript/superscript text. */
20911 s->ybase += s->first_glyph->voffset;
20912 }
20913
20914
20915 /* Fill glyph string S from a sequence of stretch glyphs.
20916
20917 ROW is the glyph row in which the glyphs are found, AREA is the
20918 area within the row. START is the index of the first glyph to
20919 consider, END is the index of the last + 1.
20920
20921 Value is the index of the first glyph not in S. */
20922
20923 static int
20924 fill_stretch_glyph_string (struct glyph_string *s, struct glyph_row *row,
20925 enum glyph_row_area area, int start, int end)
20926 {
20927 struct glyph *glyph, *last;
20928 int voffset, face_id;
20929
20930 xassert (s->first_glyph->type == STRETCH_GLYPH);
20931
20932 glyph = s->row->glyphs[s->area] + start;
20933 last = s->row->glyphs[s->area] + end;
20934 face_id = glyph->face_id;
20935 s->face = FACE_FROM_ID (s->f, face_id);
20936 s->font = s->face->font;
20937 s->width = glyph->pixel_width;
20938 s->nchars = 1;
20939 voffset = glyph->voffset;
20940
20941 for (++glyph;
20942 (glyph < last
20943 && glyph->type == STRETCH_GLYPH
20944 && glyph->voffset == voffset
20945 && glyph->face_id == face_id);
20946 ++glyph)
20947 s->width += glyph->pixel_width;
20948
20949 /* Adjust base line for subscript/superscript text. */
20950 s->ybase += voffset;
20951
20952 /* The case that face->gc == 0 is handled when drawing the glyph
20953 string by calling PREPARE_FACE_FOR_DISPLAY. */
20954 xassert (s->face);
20955 return glyph - s->row->glyphs[s->area];
20956 }
20957
20958 static struct font_metrics *
20959 get_per_char_metric (struct frame *f, struct font *font, XChar2b *char2b)
20960 {
20961 static struct font_metrics metrics;
20962 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20963
20964 if (! font || code == FONT_INVALID_CODE)
20965 return NULL;
20966 font->driver->text_extents (font, &code, 1, &metrics);
20967 return &metrics;
20968 }
20969
20970 /* EXPORT for RIF:
20971 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20972 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20973 assumed to be zero. */
20974
20975 void
20976 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
20977 {
20978 *left = *right = 0;
20979
20980 if (glyph->type == CHAR_GLYPH)
20981 {
20982 struct face *face;
20983 XChar2b char2b;
20984 struct font_metrics *pcm;
20985
20986 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20987 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20988 {
20989 if (pcm->rbearing > pcm->width)
20990 *right = pcm->rbearing - pcm->width;
20991 if (pcm->lbearing < 0)
20992 *left = -pcm->lbearing;
20993 }
20994 }
20995 else if (glyph->type == COMPOSITE_GLYPH)
20996 {
20997 if (! glyph->u.cmp.automatic)
20998 {
20999 struct composition *cmp = composition_table[glyph->u.cmp.id];
21000
21001 if (cmp->rbearing > cmp->pixel_width)
21002 *right = cmp->rbearing - cmp->pixel_width;
21003 if (cmp->lbearing < 0)
21004 *left = - cmp->lbearing;
21005 }
21006 else
21007 {
21008 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21009 struct font_metrics metrics;
21010
21011 composition_gstring_width (gstring, glyph->slice.cmp.from,
21012 glyph->slice.cmp.to + 1, &metrics);
21013 if (metrics.rbearing > metrics.width)
21014 *right = metrics.rbearing - metrics.width;
21015 if (metrics.lbearing < 0)
21016 *left = - metrics.lbearing;
21017 }
21018 }
21019 }
21020
21021
21022 /* Return the index of the first glyph preceding glyph string S that
21023 is overwritten by S because of S's left overhang. Value is -1
21024 if no glyphs are overwritten. */
21025
21026 static int
21027 left_overwritten (struct glyph_string *s)
21028 {
21029 int k;
21030
21031 if (s->left_overhang)
21032 {
21033 int x = 0, i;
21034 struct glyph *glyphs = s->row->glyphs[s->area];
21035 int first = s->first_glyph - glyphs;
21036
21037 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21038 x -= glyphs[i].pixel_width;
21039
21040 k = i + 1;
21041 }
21042 else
21043 k = -1;
21044
21045 return k;
21046 }
21047
21048
21049 /* Return the index of the first glyph preceding glyph string S that
21050 is overwriting S because of its right overhang. Value is -1 if no
21051 glyph in front of S overwrites S. */
21052
21053 static int
21054 left_overwriting (struct glyph_string *s)
21055 {
21056 int i, k, x;
21057 struct glyph *glyphs = s->row->glyphs[s->area];
21058 int first = s->first_glyph - glyphs;
21059
21060 k = -1;
21061 x = 0;
21062 for (i = first - 1; i >= 0; --i)
21063 {
21064 int left, right;
21065 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21066 if (x + right > 0)
21067 k = i;
21068 x -= glyphs[i].pixel_width;
21069 }
21070
21071 return k;
21072 }
21073
21074
21075 /* Return the index of the last glyph following glyph string S that is
21076 overwritten by S because of S's right overhang. Value is -1 if
21077 no such glyph is found. */
21078
21079 static int
21080 right_overwritten (struct glyph_string *s)
21081 {
21082 int k = -1;
21083
21084 if (s->right_overhang)
21085 {
21086 int x = 0, i;
21087 struct glyph *glyphs = s->row->glyphs[s->area];
21088 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21089 int end = s->row->used[s->area];
21090
21091 for (i = first; i < end && s->right_overhang > x; ++i)
21092 x += glyphs[i].pixel_width;
21093
21094 k = i;
21095 }
21096
21097 return k;
21098 }
21099
21100
21101 /* Return the index of the last glyph following glyph string S that
21102 overwrites S because of its left overhang. Value is negative
21103 if no such glyph is found. */
21104
21105 static int
21106 right_overwriting (struct glyph_string *s)
21107 {
21108 int i, k, x;
21109 int end = s->row->used[s->area];
21110 struct glyph *glyphs = s->row->glyphs[s->area];
21111 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21112
21113 k = -1;
21114 x = 0;
21115 for (i = first; i < end; ++i)
21116 {
21117 int left, right;
21118 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21119 if (x - left < 0)
21120 k = i;
21121 x += glyphs[i].pixel_width;
21122 }
21123
21124 return k;
21125 }
21126
21127
21128 /* Set background width of glyph string S. START is the index of the
21129 first glyph following S. LAST_X is the right-most x-position + 1
21130 in the drawing area. */
21131
21132 static INLINE void
21133 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21134 {
21135 /* If the face of this glyph string has to be drawn to the end of
21136 the drawing area, set S->extends_to_end_of_line_p. */
21137
21138 if (start == s->row->used[s->area]
21139 && s->area == TEXT_AREA
21140 && ((s->row->fill_line_p
21141 && (s->hl == DRAW_NORMAL_TEXT
21142 || s->hl == DRAW_IMAGE_RAISED
21143 || s->hl == DRAW_IMAGE_SUNKEN))
21144 || s->hl == DRAW_MOUSE_FACE))
21145 s->extends_to_end_of_line_p = 1;
21146
21147 /* If S extends its face to the end of the line, set its
21148 background_width to the distance to the right edge of the drawing
21149 area. */
21150 if (s->extends_to_end_of_line_p)
21151 s->background_width = last_x - s->x + 1;
21152 else
21153 s->background_width = s->width;
21154 }
21155
21156
21157 /* Compute overhangs and x-positions for glyph string S and its
21158 predecessors, or successors. X is the starting x-position for S.
21159 BACKWARD_P non-zero means process predecessors. */
21160
21161 static void
21162 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21163 {
21164 if (backward_p)
21165 {
21166 while (s)
21167 {
21168 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21169 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21170 x -= s->width;
21171 s->x = x;
21172 s = s->prev;
21173 }
21174 }
21175 else
21176 {
21177 while (s)
21178 {
21179 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21180 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21181 s->x = x;
21182 x += s->width;
21183 s = s->next;
21184 }
21185 }
21186 }
21187
21188
21189
21190 /* The following macros are only called from draw_glyphs below.
21191 They reference the following parameters of that function directly:
21192 `w', `row', `area', and `overlap_p'
21193 as well as the following local variables:
21194 `s', `f', and `hdc' (in W32) */
21195
21196 #ifdef HAVE_NTGUI
21197 /* On W32, silently add local `hdc' variable to argument list of
21198 init_glyph_string. */
21199 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21200 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21201 #else
21202 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21203 init_glyph_string (s, char2b, w, row, area, start, hl)
21204 #endif
21205
21206 /* Add a glyph string for a stretch glyph to the list of strings
21207 between HEAD and TAIL. START is the index of the stretch glyph in
21208 row area AREA of glyph row ROW. END is the index of the last glyph
21209 in that glyph row area. X is the current output position assigned
21210 to the new glyph string constructed. HL overrides that face of the
21211 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21212 is the right-most x-position of the drawing area. */
21213
21214 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21215 and below -- keep them on one line. */
21216 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21217 do \
21218 { \
21219 s = (struct glyph_string *) alloca (sizeof *s); \
21220 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21221 START = fill_stretch_glyph_string (s, row, area, START, END); \
21222 append_glyph_string (&HEAD, &TAIL, s); \
21223 s->x = (X); \
21224 } \
21225 while (0)
21226
21227
21228 /* Add a glyph string for an image glyph to the list of strings
21229 between HEAD and TAIL. START is the index of the image glyph in
21230 row area AREA of glyph row ROW. END is the index of the last glyph
21231 in that glyph row area. X is the current output position assigned
21232 to the new glyph string constructed. HL overrides that face of the
21233 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21234 is the right-most x-position of the drawing area. */
21235
21236 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21237 do \
21238 { \
21239 s = (struct glyph_string *) alloca (sizeof *s); \
21240 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21241 fill_image_glyph_string (s); \
21242 append_glyph_string (&HEAD, &TAIL, s); \
21243 ++START; \
21244 s->x = (X); \
21245 } \
21246 while (0)
21247
21248
21249 /* Add a glyph string for a sequence of character glyphs to the list
21250 of strings between HEAD and TAIL. START is the index of the first
21251 glyph in row area AREA of glyph row ROW that is part of the new
21252 glyph string. END is the index of the last glyph in that glyph row
21253 area. X is the current output position assigned to the new glyph
21254 string constructed. HL overrides that face of the glyph; e.g. it
21255 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21256 right-most x-position of the drawing area. */
21257
21258 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21259 do \
21260 { \
21261 int face_id; \
21262 XChar2b *char2b; \
21263 \
21264 face_id = (row)->glyphs[area][START].face_id; \
21265 \
21266 s = (struct glyph_string *) alloca (sizeof *s); \
21267 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21268 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21269 append_glyph_string (&HEAD, &TAIL, s); \
21270 s->x = (X); \
21271 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21272 } \
21273 while (0)
21274
21275
21276 /* Add a glyph string for a composite sequence to the list of strings
21277 between HEAD and TAIL. START is the index of the first glyph in
21278 row area AREA of glyph row ROW that is part of the new glyph
21279 string. END is the index of the last glyph in that glyph row area.
21280 X is the current output position assigned to the new glyph string
21281 constructed. HL overrides that face of the glyph; e.g. it is
21282 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21283 x-position of the drawing area. */
21284
21285 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21286 do { \
21287 int face_id = (row)->glyphs[area][START].face_id; \
21288 struct face *base_face = FACE_FROM_ID (f, face_id); \
21289 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21290 struct composition *cmp = composition_table[cmp_id]; \
21291 XChar2b *char2b; \
21292 struct glyph_string *first_s; \
21293 int n; \
21294 \
21295 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21296 \
21297 /* Make glyph_strings for each glyph sequence that is drawable by \
21298 the same face, and append them to HEAD/TAIL. */ \
21299 for (n = 0; n < cmp->glyph_len;) \
21300 { \
21301 s = (struct glyph_string *) alloca (sizeof *s); \
21302 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21303 append_glyph_string (&(HEAD), &(TAIL), s); \
21304 s->cmp = cmp; \
21305 s->cmp_from = n; \
21306 s->x = (X); \
21307 if (n == 0) \
21308 first_s = s; \
21309 n = fill_composite_glyph_string (s, base_face, overlaps); \
21310 } \
21311 \
21312 ++START; \
21313 s = first_s; \
21314 } while (0)
21315
21316
21317 /* Add a glyph string for a glyph-string sequence to the list of strings
21318 between HEAD and TAIL. */
21319
21320 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21321 do { \
21322 int face_id; \
21323 XChar2b *char2b; \
21324 Lisp_Object gstring; \
21325 \
21326 face_id = (row)->glyphs[area][START].face_id; \
21327 gstring = (composition_gstring_from_id \
21328 ((row)->glyphs[area][START].u.cmp.id)); \
21329 s = (struct glyph_string *) alloca (sizeof *s); \
21330 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21331 * LGSTRING_GLYPH_LEN (gstring)); \
21332 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21333 append_glyph_string (&(HEAD), &(TAIL), s); \
21334 s->x = (X); \
21335 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21336 } while (0)
21337
21338
21339 /* Add a glyph string for a sequence of glyphless character's glyphs
21340 to the list of strings between HEAD and TAIL. The meanings of
21341 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21342
21343 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21344 do \
21345 { \
21346 int face_id; \
21347 XChar2b *char2b; \
21348 \
21349 face_id = (row)->glyphs[area][START].face_id; \
21350 \
21351 s = (struct glyph_string *) alloca (sizeof *s); \
21352 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21353 append_glyph_string (&HEAD, &TAIL, s); \
21354 s->x = (X); \
21355 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21356 overlaps); \
21357 } \
21358 while (0)
21359
21360
21361 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21362 of AREA of glyph row ROW on window W between indices START and END.
21363 HL overrides the face for drawing glyph strings, e.g. it is
21364 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21365 x-positions of the drawing area.
21366
21367 This is an ugly monster macro construct because we must use alloca
21368 to allocate glyph strings (because draw_glyphs can be called
21369 asynchronously). */
21370
21371 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21372 do \
21373 { \
21374 HEAD = TAIL = NULL; \
21375 while (START < END) \
21376 { \
21377 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21378 switch (first_glyph->type) \
21379 { \
21380 case CHAR_GLYPH: \
21381 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21382 HL, X, LAST_X); \
21383 break; \
21384 \
21385 case COMPOSITE_GLYPH: \
21386 if (first_glyph->u.cmp.automatic) \
21387 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21388 HL, X, LAST_X); \
21389 else \
21390 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21391 HL, X, LAST_X); \
21392 break; \
21393 \
21394 case STRETCH_GLYPH: \
21395 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21396 HL, X, LAST_X); \
21397 break; \
21398 \
21399 case IMAGE_GLYPH: \
21400 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21401 HL, X, LAST_X); \
21402 break; \
21403 \
21404 case GLYPHLESS_GLYPH: \
21405 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
21406 HL, X, LAST_X); \
21407 break; \
21408 \
21409 default: \
21410 abort (); \
21411 } \
21412 \
21413 if (s) \
21414 { \
21415 set_glyph_string_background_width (s, START, LAST_X); \
21416 (X) += s->width; \
21417 } \
21418 } \
21419 } while (0)
21420
21421
21422 /* Draw glyphs between START and END in AREA of ROW on window W,
21423 starting at x-position X. X is relative to AREA in W. HL is a
21424 face-override with the following meaning:
21425
21426 DRAW_NORMAL_TEXT draw normally
21427 DRAW_CURSOR draw in cursor face
21428 DRAW_MOUSE_FACE draw in mouse face.
21429 DRAW_INVERSE_VIDEO draw in mode line face
21430 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21431 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21432
21433 If OVERLAPS is non-zero, draw only the foreground of characters and
21434 clip to the physical height of ROW. Non-zero value also defines
21435 the overlapping part to be drawn:
21436
21437 OVERLAPS_PRED overlap with preceding rows
21438 OVERLAPS_SUCC overlap with succeeding rows
21439 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21440 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21441
21442 Value is the x-position reached, relative to AREA of W. */
21443
21444 static int
21445 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21446 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21447 enum draw_glyphs_face hl, int overlaps)
21448 {
21449 struct glyph_string *head, *tail;
21450 struct glyph_string *s;
21451 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21452 int i, j, x_reached, last_x, area_left = 0;
21453 struct frame *f = XFRAME (WINDOW_FRAME (w));
21454 DECLARE_HDC (hdc);
21455
21456 ALLOCATE_HDC (hdc, f);
21457
21458 /* Let's rather be paranoid than getting a SEGV. */
21459 end = min (end, row->used[area]);
21460 start = max (0, start);
21461 start = min (end, start);
21462
21463 /* Translate X to frame coordinates. Set last_x to the right
21464 end of the drawing area. */
21465 if (row->full_width_p)
21466 {
21467 /* X is relative to the left edge of W, without scroll bars
21468 or fringes. */
21469 area_left = WINDOW_LEFT_EDGE_X (w);
21470 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21471 }
21472 else
21473 {
21474 area_left = window_box_left (w, area);
21475 last_x = area_left + window_box_width (w, area);
21476 }
21477 x += area_left;
21478
21479 /* Build a doubly-linked list of glyph_string structures between
21480 head and tail from what we have to draw. Note that the macro
21481 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21482 the reason we use a separate variable `i'. */
21483 i = start;
21484 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21485 if (tail)
21486 x_reached = tail->x + tail->background_width;
21487 else
21488 x_reached = x;
21489
21490 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21491 the row, redraw some glyphs in front or following the glyph
21492 strings built above. */
21493 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21494 {
21495 struct glyph_string *h, *t;
21496 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
21497 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
21498 int dummy_x = 0;
21499
21500 /* If mouse highlighting is on, we may need to draw adjacent
21501 glyphs using mouse-face highlighting. */
21502 if (area == TEXT_AREA && row->mouse_face_p)
21503 {
21504 struct glyph_row *mouse_beg_row, *mouse_end_row;
21505
21506 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
21507 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
21508
21509 if (row >= mouse_beg_row && row <= mouse_end_row)
21510 {
21511 check_mouse_face = 1;
21512 mouse_beg_col = (row == mouse_beg_row)
21513 ? hlinfo->mouse_face_beg_col : 0;
21514 mouse_end_col = (row == mouse_end_row)
21515 ? hlinfo->mouse_face_end_col
21516 : row->used[TEXT_AREA];
21517 }
21518 }
21519
21520 /* Compute overhangs for all glyph strings. */
21521 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21522 for (s = head; s; s = s->next)
21523 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21524
21525 /* Prepend glyph strings for glyphs in front of the first glyph
21526 string that are overwritten because of the first glyph
21527 string's left overhang. The background of all strings
21528 prepended must be drawn because the first glyph string
21529 draws over it. */
21530 i = left_overwritten (head);
21531 if (i >= 0)
21532 {
21533 enum draw_glyphs_face overlap_hl;
21534
21535 /* If this row contains mouse highlighting, attempt to draw
21536 the overlapped glyphs with the correct highlight. This
21537 code fails if the overlap encompasses more than one glyph
21538 and mouse-highlight spans only some of these glyphs.
21539 However, making it work perfectly involves a lot more
21540 code, and I don't know if the pathological case occurs in
21541 practice, so we'll stick to this for now. --- cyd */
21542 if (check_mouse_face
21543 && mouse_beg_col < start && mouse_end_col > i)
21544 overlap_hl = DRAW_MOUSE_FACE;
21545 else
21546 overlap_hl = DRAW_NORMAL_TEXT;
21547
21548 j = i;
21549 BUILD_GLYPH_STRINGS (j, start, h, t,
21550 overlap_hl, dummy_x, last_x);
21551 start = i;
21552 compute_overhangs_and_x (t, head->x, 1);
21553 prepend_glyph_string_lists (&head, &tail, h, t);
21554 clip_head = head;
21555 }
21556
21557 /* Prepend glyph strings for glyphs in front of the first glyph
21558 string that overwrite that glyph string because of their
21559 right overhang. For these strings, only the foreground must
21560 be drawn, because it draws over the glyph string at `head'.
21561 The background must not be drawn because this would overwrite
21562 right overhangs of preceding glyphs for which no glyph
21563 strings exist. */
21564 i = left_overwriting (head);
21565 if (i >= 0)
21566 {
21567 enum draw_glyphs_face overlap_hl;
21568
21569 if (check_mouse_face
21570 && mouse_beg_col < start && mouse_end_col > i)
21571 overlap_hl = DRAW_MOUSE_FACE;
21572 else
21573 overlap_hl = DRAW_NORMAL_TEXT;
21574
21575 clip_head = head;
21576 BUILD_GLYPH_STRINGS (i, start, h, t,
21577 overlap_hl, dummy_x, last_x);
21578 for (s = h; s; s = s->next)
21579 s->background_filled_p = 1;
21580 compute_overhangs_and_x (t, head->x, 1);
21581 prepend_glyph_string_lists (&head, &tail, h, t);
21582 }
21583
21584 /* Append glyphs strings for glyphs following the last glyph
21585 string tail that are overwritten by tail. The background of
21586 these strings has to be drawn because tail's foreground draws
21587 over it. */
21588 i = right_overwritten (tail);
21589 if (i >= 0)
21590 {
21591 enum draw_glyphs_face overlap_hl;
21592
21593 if (check_mouse_face
21594 && mouse_beg_col < i && mouse_end_col > end)
21595 overlap_hl = DRAW_MOUSE_FACE;
21596 else
21597 overlap_hl = DRAW_NORMAL_TEXT;
21598
21599 BUILD_GLYPH_STRINGS (end, i, h, t,
21600 overlap_hl, x, last_x);
21601 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21602 we don't have `end = i;' here. */
21603 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21604 append_glyph_string_lists (&head, &tail, h, t);
21605 clip_tail = tail;
21606 }
21607
21608 /* Append glyph strings for glyphs following the last glyph
21609 string tail that overwrite tail. The foreground of such
21610 glyphs has to be drawn because it writes into the background
21611 of tail. The background must not be drawn because it could
21612 paint over the foreground of following glyphs. */
21613 i = right_overwriting (tail);
21614 if (i >= 0)
21615 {
21616 enum draw_glyphs_face overlap_hl;
21617 if (check_mouse_face
21618 && mouse_beg_col < i && mouse_end_col > end)
21619 overlap_hl = DRAW_MOUSE_FACE;
21620 else
21621 overlap_hl = DRAW_NORMAL_TEXT;
21622
21623 clip_tail = tail;
21624 i++; /* We must include the Ith glyph. */
21625 BUILD_GLYPH_STRINGS (end, i, h, t,
21626 overlap_hl, x, last_x);
21627 for (s = h; s; s = s->next)
21628 s->background_filled_p = 1;
21629 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21630 append_glyph_string_lists (&head, &tail, h, t);
21631 }
21632 if (clip_head || clip_tail)
21633 for (s = head; s; s = s->next)
21634 {
21635 s->clip_head = clip_head;
21636 s->clip_tail = clip_tail;
21637 }
21638 }
21639
21640 /* Draw all strings. */
21641 for (s = head; s; s = s->next)
21642 FRAME_RIF (f)->draw_glyph_string (s);
21643
21644 #ifndef HAVE_NS
21645 /* When focus a sole frame and move horizontally, this sets on_p to 0
21646 causing a failure to erase prev cursor position. */
21647 if (area == TEXT_AREA
21648 && !row->full_width_p
21649 /* When drawing overlapping rows, only the glyph strings'
21650 foreground is drawn, which doesn't erase a cursor
21651 completely. */
21652 && !overlaps)
21653 {
21654 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21655 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21656 : (tail ? tail->x + tail->background_width : x));
21657 x0 -= area_left;
21658 x1 -= area_left;
21659
21660 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21661 row->y, MATRIX_ROW_BOTTOM_Y (row));
21662 }
21663 #endif
21664
21665 /* Value is the x-position up to which drawn, relative to AREA of W.
21666 This doesn't include parts drawn because of overhangs. */
21667 if (row->full_width_p)
21668 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21669 else
21670 x_reached -= area_left;
21671
21672 RELEASE_HDC (hdc, f);
21673
21674 return x_reached;
21675 }
21676
21677 /* Expand row matrix if too narrow. Don't expand if area
21678 is not present. */
21679
21680 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21681 { \
21682 if (!fonts_changed_p \
21683 && (it->glyph_row->glyphs[area] \
21684 < it->glyph_row->glyphs[area + 1])) \
21685 { \
21686 it->w->ncols_scale_factor++; \
21687 fonts_changed_p = 1; \
21688 } \
21689 }
21690
21691 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21692 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21693
21694 static INLINE void
21695 append_glyph (struct it *it)
21696 {
21697 struct glyph *glyph;
21698 enum glyph_row_area area = it->area;
21699
21700 xassert (it->glyph_row);
21701 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21702
21703 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21704 if (glyph < it->glyph_row->glyphs[area + 1])
21705 {
21706 /* If the glyph row is reversed, we need to prepend the glyph
21707 rather than append it. */
21708 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21709 {
21710 struct glyph *g;
21711
21712 /* Make room for the additional glyph. */
21713 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21714 g[1] = *g;
21715 glyph = it->glyph_row->glyphs[area];
21716 }
21717 glyph->charpos = CHARPOS (it->position);
21718 glyph->object = it->object;
21719 if (it->pixel_width > 0)
21720 {
21721 glyph->pixel_width = it->pixel_width;
21722 glyph->padding_p = 0;
21723 }
21724 else
21725 {
21726 /* Assure at least 1-pixel width. Otherwise, cursor can't
21727 be displayed correctly. */
21728 glyph->pixel_width = 1;
21729 glyph->padding_p = 1;
21730 }
21731 glyph->ascent = it->ascent;
21732 glyph->descent = it->descent;
21733 glyph->voffset = it->voffset;
21734 glyph->type = CHAR_GLYPH;
21735 glyph->avoid_cursor_p = it->avoid_cursor_p;
21736 glyph->multibyte_p = it->multibyte_p;
21737 glyph->left_box_line_p = it->start_of_box_run_p;
21738 glyph->right_box_line_p = it->end_of_box_run_p;
21739 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21740 || it->phys_descent > it->descent);
21741 glyph->glyph_not_available_p = it->glyph_not_available_p;
21742 glyph->face_id = it->face_id;
21743 glyph->u.ch = it->char_to_display;
21744 glyph->slice.img = null_glyph_slice;
21745 glyph->font_type = FONT_TYPE_UNKNOWN;
21746 if (it->bidi_p)
21747 {
21748 glyph->resolved_level = it->bidi_it.resolved_level;
21749 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21750 abort ();
21751 glyph->bidi_type = it->bidi_it.type;
21752 }
21753 else
21754 {
21755 glyph->resolved_level = 0;
21756 glyph->bidi_type = UNKNOWN_BT;
21757 }
21758 ++it->glyph_row->used[area];
21759 }
21760 else
21761 IT_EXPAND_MATRIX_WIDTH (it, area);
21762 }
21763
21764 /* Store one glyph for the composition IT->cmp_it.id in
21765 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21766 non-null. */
21767
21768 static INLINE void
21769 append_composite_glyph (struct it *it)
21770 {
21771 struct glyph *glyph;
21772 enum glyph_row_area area = it->area;
21773
21774 xassert (it->glyph_row);
21775
21776 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21777 if (glyph < it->glyph_row->glyphs[area + 1])
21778 {
21779 /* If the glyph row is reversed, we need to prepend the glyph
21780 rather than append it. */
21781 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
21782 {
21783 struct glyph *g;
21784
21785 /* Make room for the new glyph. */
21786 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
21787 g[1] = *g;
21788 glyph = it->glyph_row->glyphs[it->area];
21789 }
21790 glyph->charpos = it->cmp_it.charpos;
21791 glyph->object = it->object;
21792 glyph->pixel_width = it->pixel_width;
21793 glyph->ascent = it->ascent;
21794 glyph->descent = it->descent;
21795 glyph->voffset = it->voffset;
21796 glyph->type = COMPOSITE_GLYPH;
21797 if (it->cmp_it.ch < 0)
21798 {
21799 glyph->u.cmp.automatic = 0;
21800 glyph->u.cmp.id = it->cmp_it.id;
21801 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
21802 }
21803 else
21804 {
21805 glyph->u.cmp.automatic = 1;
21806 glyph->u.cmp.id = it->cmp_it.id;
21807 glyph->slice.cmp.from = it->cmp_it.from;
21808 glyph->slice.cmp.to = it->cmp_it.to - 1;
21809 }
21810 glyph->avoid_cursor_p = it->avoid_cursor_p;
21811 glyph->multibyte_p = it->multibyte_p;
21812 glyph->left_box_line_p = it->start_of_box_run_p;
21813 glyph->right_box_line_p = it->end_of_box_run_p;
21814 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21815 || it->phys_descent > it->descent);
21816 glyph->padding_p = 0;
21817 glyph->glyph_not_available_p = 0;
21818 glyph->face_id = it->face_id;
21819 glyph->font_type = FONT_TYPE_UNKNOWN;
21820 if (it->bidi_p)
21821 {
21822 glyph->resolved_level = it->bidi_it.resolved_level;
21823 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21824 abort ();
21825 glyph->bidi_type = it->bidi_it.type;
21826 }
21827 ++it->glyph_row->used[area];
21828 }
21829 else
21830 IT_EXPAND_MATRIX_WIDTH (it, area);
21831 }
21832
21833
21834 /* Change IT->ascent and IT->height according to the setting of
21835 IT->voffset. */
21836
21837 static INLINE void
21838 take_vertical_position_into_account (struct it *it)
21839 {
21840 if (it->voffset)
21841 {
21842 if (it->voffset < 0)
21843 /* Increase the ascent so that we can display the text higher
21844 in the line. */
21845 it->ascent -= it->voffset;
21846 else
21847 /* Increase the descent so that we can display the text lower
21848 in the line. */
21849 it->descent += it->voffset;
21850 }
21851 }
21852
21853
21854 /* Produce glyphs/get display metrics for the image IT is loaded with.
21855 See the description of struct display_iterator in dispextern.h for
21856 an overview of struct display_iterator. */
21857
21858 static void
21859 produce_image_glyph (struct it *it)
21860 {
21861 struct image *img;
21862 struct face *face;
21863 int glyph_ascent, crop;
21864 struct glyph_slice slice;
21865
21866 xassert (it->what == IT_IMAGE);
21867
21868 face = FACE_FROM_ID (it->f, it->face_id);
21869 xassert (face);
21870 /* Make sure X resources of the face is loaded. */
21871 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21872
21873 if (it->image_id < 0)
21874 {
21875 /* Fringe bitmap. */
21876 it->ascent = it->phys_ascent = 0;
21877 it->descent = it->phys_descent = 0;
21878 it->pixel_width = 0;
21879 it->nglyphs = 0;
21880 return;
21881 }
21882
21883 img = IMAGE_FROM_ID (it->f, it->image_id);
21884 xassert (img);
21885 /* Make sure X resources of the image is loaded. */
21886 prepare_image_for_display (it->f, img);
21887
21888 slice.x = slice.y = 0;
21889 slice.width = img->width;
21890 slice.height = img->height;
21891
21892 if (INTEGERP (it->slice.x))
21893 slice.x = XINT (it->slice.x);
21894 else if (FLOATP (it->slice.x))
21895 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21896
21897 if (INTEGERP (it->slice.y))
21898 slice.y = XINT (it->slice.y);
21899 else if (FLOATP (it->slice.y))
21900 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21901
21902 if (INTEGERP (it->slice.width))
21903 slice.width = XINT (it->slice.width);
21904 else if (FLOATP (it->slice.width))
21905 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21906
21907 if (INTEGERP (it->slice.height))
21908 slice.height = XINT (it->slice.height);
21909 else if (FLOATP (it->slice.height))
21910 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21911
21912 if (slice.x >= img->width)
21913 slice.x = img->width;
21914 if (slice.y >= img->height)
21915 slice.y = img->height;
21916 if (slice.x + slice.width >= img->width)
21917 slice.width = img->width - slice.x;
21918 if (slice.y + slice.height > img->height)
21919 slice.height = img->height - slice.y;
21920
21921 if (slice.width == 0 || slice.height == 0)
21922 return;
21923
21924 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21925
21926 it->descent = slice.height - glyph_ascent;
21927 if (slice.y == 0)
21928 it->descent += img->vmargin;
21929 if (slice.y + slice.height == img->height)
21930 it->descent += img->vmargin;
21931 it->phys_descent = it->descent;
21932
21933 it->pixel_width = slice.width;
21934 if (slice.x == 0)
21935 it->pixel_width += img->hmargin;
21936 if (slice.x + slice.width == img->width)
21937 it->pixel_width += img->hmargin;
21938
21939 /* It's quite possible for images to have an ascent greater than
21940 their height, so don't get confused in that case. */
21941 if (it->descent < 0)
21942 it->descent = 0;
21943
21944 it->nglyphs = 1;
21945
21946 if (face->box != FACE_NO_BOX)
21947 {
21948 if (face->box_line_width > 0)
21949 {
21950 if (slice.y == 0)
21951 it->ascent += face->box_line_width;
21952 if (slice.y + slice.height == img->height)
21953 it->descent += face->box_line_width;
21954 }
21955
21956 if (it->start_of_box_run_p && slice.x == 0)
21957 it->pixel_width += eabs (face->box_line_width);
21958 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21959 it->pixel_width += eabs (face->box_line_width);
21960 }
21961
21962 take_vertical_position_into_account (it);
21963
21964 /* Automatically crop wide image glyphs at right edge so we can
21965 draw the cursor on same display row. */
21966 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21967 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21968 {
21969 it->pixel_width -= crop;
21970 slice.width -= crop;
21971 }
21972
21973 if (it->glyph_row)
21974 {
21975 struct glyph *glyph;
21976 enum glyph_row_area area = it->area;
21977
21978 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21979 if (glyph < it->glyph_row->glyphs[area + 1])
21980 {
21981 glyph->charpos = CHARPOS (it->position);
21982 glyph->object = it->object;
21983 glyph->pixel_width = it->pixel_width;
21984 glyph->ascent = glyph_ascent;
21985 glyph->descent = it->descent;
21986 glyph->voffset = it->voffset;
21987 glyph->type = IMAGE_GLYPH;
21988 glyph->avoid_cursor_p = it->avoid_cursor_p;
21989 glyph->multibyte_p = it->multibyte_p;
21990 glyph->left_box_line_p = it->start_of_box_run_p;
21991 glyph->right_box_line_p = it->end_of_box_run_p;
21992 glyph->overlaps_vertically_p = 0;
21993 glyph->padding_p = 0;
21994 glyph->glyph_not_available_p = 0;
21995 glyph->face_id = it->face_id;
21996 glyph->u.img_id = img->id;
21997 glyph->slice.img = slice;
21998 glyph->font_type = FONT_TYPE_UNKNOWN;
21999 if (it->bidi_p)
22000 {
22001 glyph->resolved_level = it->bidi_it.resolved_level;
22002 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22003 abort ();
22004 glyph->bidi_type = it->bidi_it.type;
22005 }
22006 ++it->glyph_row->used[area];
22007 }
22008 else
22009 IT_EXPAND_MATRIX_WIDTH (it, area);
22010 }
22011 }
22012
22013
22014 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22015 of the glyph, WIDTH and HEIGHT are the width and height of the
22016 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22017
22018 static void
22019 append_stretch_glyph (struct it *it, Lisp_Object object,
22020 int width, int height, int ascent)
22021 {
22022 struct glyph *glyph;
22023 enum glyph_row_area area = it->area;
22024
22025 xassert (ascent >= 0 && ascent <= height);
22026
22027 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22028 if (glyph < it->glyph_row->glyphs[area + 1])
22029 {
22030 /* If the glyph row is reversed, we need to prepend the glyph
22031 rather than append it. */
22032 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22033 {
22034 struct glyph *g;
22035
22036 /* Make room for the additional glyph. */
22037 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22038 g[1] = *g;
22039 glyph = it->glyph_row->glyphs[area];
22040 }
22041 glyph->charpos = CHARPOS (it->position);
22042 glyph->object = object;
22043 glyph->pixel_width = width;
22044 glyph->ascent = ascent;
22045 glyph->descent = height - ascent;
22046 glyph->voffset = it->voffset;
22047 glyph->type = STRETCH_GLYPH;
22048 glyph->avoid_cursor_p = it->avoid_cursor_p;
22049 glyph->multibyte_p = it->multibyte_p;
22050 glyph->left_box_line_p = it->start_of_box_run_p;
22051 glyph->right_box_line_p = it->end_of_box_run_p;
22052 glyph->overlaps_vertically_p = 0;
22053 glyph->padding_p = 0;
22054 glyph->glyph_not_available_p = 0;
22055 glyph->face_id = it->face_id;
22056 glyph->u.stretch.ascent = ascent;
22057 glyph->u.stretch.height = height;
22058 glyph->slice.img = null_glyph_slice;
22059 glyph->font_type = FONT_TYPE_UNKNOWN;
22060 if (it->bidi_p)
22061 {
22062 glyph->resolved_level = it->bidi_it.resolved_level;
22063 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22064 abort ();
22065 glyph->bidi_type = it->bidi_it.type;
22066 }
22067 else
22068 {
22069 glyph->resolved_level = 0;
22070 glyph->bidi_type = UNKNOWN_BT;
22071 }
22072 ++it->glyph_row->used[area];
22073 }
22074 else
22075 IT_EXPAND_MATRIX_WIDTH (it, area);
22076 }
22077
22078
22079 /* Produce a stretch glyph for iterator IT. IT->object is the value
22080 of the glyph property displayed. The value must be a list
22081 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22082 being recognized:
22083
22084 1. `:width WIDTH' specifies that the space should be WIDTH *
22085 canonical char width wide. WIDTH may be an integer or floating
22086 point number.
22087
22088 2. `:relative-width FACTOR' specifies that the width of the stretch
22089 should be computed from the width of the first character having the
22090 `glyph' property, and should be FACTOR times that width.
22091
22092 3. `:align-to HPOS' specifies that the space should be wide enough
22093 to reach HPOS, a value in canonical character units.
22094
22095 Exactly one of the above pairs must be present.
22096
22097 4. `:height HEIGHT' specifies that the height of the stretch produced
22098 should be HEIGHT, measured in canonical character units.
22099
22100 5. `:relative-height FACTOR' specifies that the height of the
22101 stretch should be FACTOR times the height of the characters having
22102 the glyph property.
22103
22104 Either none or exactly one of 4 or 5 must be present.
22105
22106 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22107 of the stretch should be used for the ascent of the stretch.
22108 ASCENT must be in the range 0 <= ASCENT <= 100. */
22109
22110 static void
22111 produce_stretch_glyph (struct it *it)
22112 {
22113 /* (space :width WIDTH :height HEIGHT ...) */
22114 Lisp_Object prop, plist;
22115 int width = 0, height = 0, align_to = -1;
22116 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22117 int ascent = 0;
22118 double tem;
22119 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22120 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22121
22122 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22123
22124 /* List should start with `space'. */
22125 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22126 plist = XCDR (it->object);
22127
22128 /* Compute the width of the stretch. */
22129 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22130 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22131 {
22132 /* Absolute width `:width WIDTH' specified and valid. */
22133 zero_width_ok_p = 1;
22134 width = (int)tem;
22135 }
22136 else if (prop = Fplist_get (plist, QCrelative_width),
22137 NUMVAL (prop) > 0)
22138 {
22139 /* Relative width `:relative-width FACTOR' specified and valid.
22140 Compute the width of the characters having the `glyph'
22141 property. */
22142 struct it it2;
22143 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22144
22145 it2 = *it;
22146 if (it->multibyte_p)
22147 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22148 else
22149 {
22150 it2.c = it2.char_to_display = *p, it2.len = 1;
22151 if (! ASCII_CHAR_P (it2.c))
22152 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22153 }
22154
22155 it2.glyph_row = NULL;
22156 it2.what = IT_CHARACTER;
22157 x_produce_glyphs (&it2);
22158 width = NUMVAL (prop) * it2.pixel_width;
22159 }
22160 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22161 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22162 {
22163 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22164 align_to = (align_to < 0
22165 ? 0
22166 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22167 else if (align_to < 0)
22168 align_to = window_box_left_offset (it->w, TEXT_AREA);
22169 width = max (0, (int)tem + align_to - it->current_x);
22170 zero_width_ok_p = 1;
22171 }
22172 else
22173 /* Nothing specified -> width defaults to canonical char width. */
22174 width = FRAME_COLUMN_WIDTH (it->f);
22175
22176 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22177 width = 1;
22178
22179 /* Compute height. */
22180 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22181 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22182 {
22183 height = (int)tem;
22184 zero_height_ok_p = 1;
22185 }
22186 else if (prop = Fplist_get (plist, QCrelative_height),
22187 NUMVAL (prop) > 0)
22188 height = FONT_HEIGHT (font) * NUMVAL (prop);
22189 else
22190 height = FONT_HEIGHT (font);
22191
22192 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22193 height = 1;
22194
22195 /* Compute percentage of height used for ascent. If
22196 `:ascent ASCENT' is present and valid, use that. Otherwise,
22197 derive the ascent from the font in use. */
22198 if (prop = Fplist_get (plist, QCascent),
22199 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22200 ascent = height * NUMVAL (prop) / 100.0;
22201 else if (!NILP (prop)
22202 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22203 ascent = min (max (0, (int)tem), height);
22204 else
22205 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22206
22207 if (width > 0 && it->line_wrap != TRUNCATE
22208 && it->current_x + width > it->last_visible_x)
22209 width = it->last_visible_x - it->current_x - 1;
22210
22211 if (width > 0 && height > 0 && it->glyph_row)
22212 {
22213 Lisp_Object object = it->stack[it->sp - 1].string;
22214 if (!STRINGP (object))
22215 object = it->w->buffer;
22216 append_stretch_glyph (it, object, width, height, ascent);
22217 }
22218
22219 it->pixel_width = width;
22220 it->ascent = it->phys_ascent = ascent;
22221 it->descent = it->phys_descent = height - it->ascent;
22222 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22223
22224 take_vertical_position_into_account (it);
22225 }
22226
22227 /* Calculate line-height and line-spacing properties.
22228 An integer value specifies explicit pixel value.
22229 A float value specifies relative value to current face height.
22230 A cons (float . face-name) specifies relative value to
22231 height of specified face font.
22232
22233 Returns height in pixels, or nil. */
22234
22235
22236 static Lisp_Object
22237 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22238 int boff, int override)
22239 {
22240 Lisp_Object face_name = Qnil;
22241 int ascent, descent, height;
22242
22243 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22244 return val;
22245
22246 if (CONSP (val))
22247 {
22248 face_name = XCAR (val);
22249 val = XCDR (val);
22250 if (!NUMBERP (val))
22251 val = make_number (1);
22252 if (NILP (face_name))
22253 {
22254 height = it->ascent + it->descent;
22255 goto scale;
22256 }
22257 }
22258
22259 if (NILP (face_name))
22260 {
22261 font = FRAME_FONT (it->f);
22262 boff = FRAME_BASELINE_OFFSET (it->f);
22263 }
22264 else if (EQ (face_name, Qt))
22265 {
22266 override = 0;
22267 }
22268 else
22269 {
22270 int face_id;
22271 struct face *face;
22272
22273 face_id = lookup_named_face (it->f, face_name, 0);
22274 if (face_id < 0)
22275 return make_number (-1);
22276
22277 face = FACE_FROM_ID (it->f, face_id);
22278 font = face->font;
22279 if (font == NULL)
22280 return make_number (-1);
22281 boff = font->baseline_offset;
22282 if (font->vertical_centering)
22283 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22284 }
22285
22286 ascent = FONT_BASE (font) + boff;
22287 descent = FONT_DESCENT (font) - boff;
22288
22289 if (override)
22290 {
22291 it->override_ascent = ascent;
22292 it->override_descent = descent;
22293 it->override_boff = boff;
22294 }
22295
22296 height = ascent + descent;
22297
22298 scale:
22299 if (FLOATP (val))
22300 height = (int)(XFLOAT_DATA (val) * height);
22301 else if (INTEGERP (val))
22302 height *= XINT (val);
22303
22304 return make_number (height);
22305 }
22306
22307
22308 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22309 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22310 and only if this is for a character for which no font was found.
22311
22312 If the display method (it->glyphless_method) is
22313 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
22314 length of the acronym or the hexadecimal string, UPPER_XOFF and
22315 UPPER_YOFF are pixel offsets for the upper part of the string,
22316 LOWER_XOFF and LOWER_YOFF are for the lower part.
22317
22318 For the other display methods, LEN through LOWER_YOFF are zero. */
22319
22320 static void
22321 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22322 short upper_xoff, short upper_yoff,
22323 short lower_xoff, short lower_yoff)
22324 {
22325 struct glyph *glyph;
22326 enum glyph_row_area area = it->area;
22327
22328 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22329 if (glyph < it->glyph_row->glyphs[area + 1])
22330 {
22331 /* If the glyph row is reversed, we need to prepend the glyph
22332 rather than append it. */
22333 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22334 {
22335 struct glyph *g;
22336
22337 /* Make room for the additional glyph. */
22338 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22339 g[1] = *g;
22340 glyph = it->glyph_row->glyphs[area];
22341 }
22342 glyph->charpos = CHARPOS (it->position);
22343 glyph->object = it->object;
22344 glyph->pixel_width = it->pixel_width;
22345 glyph->ascent = it->ascent;
22346 glyph->descent = it->descent;
22347 glyph->voffset = it->voffset;
22348 glyph->type = GLYPHLESS_GLYPH;
22349 glyph->u.glyphless.method = it->glyphless_method;
22350 glyph->u.glyphless.for_no_font = for_no_font;
22351 glyph->u.glyphless.len = len;
22352 glyph->u.glyphless.ch = it->c;
22353 glyph->slice.glyphless.upper_xoff = upper_xoff;
22354 glyph->slice.glyphless.upper_yoff = upper_yoff;
22355 glyph->slice.glyphless.lower_xoff = lower_xoff;
22356 glyph->slice.glyphless.lower_yoff = lower_yoff;
22357 glyph->avoid_cursor_p = it->avoid_cursor_p;
22358 glyph->multibyte_p = it->multibyte_p;
22359 glyph->left_box_line_p = it->start_of_box_run_p;
22360 glyph->right_box_line_p = it->end_of_box_run_p;
22361 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22362 || it->phys_descent > it->descent);
22363 glyph->padding_p = 0;
22364 glyph->glyph_not_available_p = 0;
22365 glyph->face_id = face_id;
22366 glyph->font_type = FONT_TYPE_UNKNOWN;
22367 if (it->bidi_p)
22368 {
22369 glyph->resolved_level = it->bidi_it.resolved_level;
22370 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22371 abort ();
22372 glyph->bidi_type = it->bidi_it.type;
22373 }
22374 ++it->glyph_row->used[area];
22375 }
22376 else
22377 IT_EXPAND_MATRIX_WIDTH (it, area);
22378 }
22379
22380
22381 /* Produce a glyph for a glyphless character for iterator IT.
22382 IT->glyphless_method specifies which method to use for displaying
22383 the character. See the description of enum
22384 glyphless_display_method in dispextern.h for the detail.
22385
22386 FOR_NO_FONT is nonzero if and only if this is for a character for
22387 which no font was found. ACRONYM, if non-nil, is an acronym string
22388 for the character. */
22389
22390 static void
22391 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22392 {
22393 int face_id;
22394 struct face *face;
22395 struct font *font;
22396 int base_width, base_height, width, height;
22397 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22398 int len;
22399
22400 /* Get the metrics of the base font. We always refer to the current
22401 ASCII face. */
22402 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22403 font = face->font ? face->font : FRAME_FONT (it->f);
22404 it->ascent = FONT_BASE (font) + font->baseline_offset;
22405 it->descent = FONT_DESCENT (font) - font->baseline_offset;
22406 base_height = it->ascent + it->descent;
22407 base_width = font->average_width;
22408
22409 /* Get a face ID for the glyph by utilizing a cache (the same way as
22410 doen for `escape-glyph' in get_next_display_element). */
22411 if (it->f == last_glyphless_glyph_frame
22412 && it->face_id == last_glyphless_glyph_face_id)
22413 {
22414 face_id = last_glyphless_glyph_merged_face_id;
22415 }
22416 else
22417 {
22418 /* Merge the `glyphless-char' face into the current face. */
22419 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
22420 last_glyphless_glyph_frame = it->f;
22421 last_glyphless_glyph_face_id = it->face_id;
22422 last_glyphless_glyph_merged_face_id = face_id;
22423 }
22424
22425 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
22426 {
22427 it->pixel_width = THIN_SPACE_WIDTH;
22428 len = 0;
22429 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22430 }
22431 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
22432 {
22433 width = CHAR_WIDTH (it->c);
22434 if (width == 0)
22435 width = 1;
22436 else if (width > 4)
22437 width = 4;
22438 it->pixel_width = base_width * width;
22439 len = 0;
22440 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22441 }
22442 else
22443 {
22444 char buf[7], *str;
22445 unsigned int code[6];
22446 int upper_len;
22447 int ascent, descent;
22448 struct font_metrics metrics_upper, metrics_lower;
22449
22450 face = FACE_FROM_ID (it->f, face_id);
22451 font = face->font ? face->font : FRAME_FONT (it->f);
22452 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22453
22454 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
22455 {
22456 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
22457 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
22458 str = STRINGP (acronym) ? (char *) SDATA (acronym) : "";
22459 }
22460 else
22461 {
22462 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
22463 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
22464 str = buf;
22465 }
22466 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
22467 code[len] = font->driver->encode_char (font, str[len]);
22468 upper_len = (len + 1) / 2;
22469 font->driver->text_extents (font, code, upper_len,
22470 &metrics_upper);
22471 font->driver->text_extents (font, code + upper_len, len - upper_len,
22472 &metrics_lower);
22473
22474
22475
22476 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
22477 width = max (metrics_upper.width, metrics_lower.width) + 4;
22478 upper_xoff = upper_yoff = 2; /* the typical case */
22479 if (base_width >= width)
22480 {
22481 /* Align the upper to the left, the lower to the right. */
22482 it->pixel_width = base_width;
22483 lower_xoff = base_width - 2 - metrics_lower.width;
22484 }
22485 else
22486 {
22487 /* Center the shorter one. */
22488 it->pixel_width = width;
22489 if (metrics_upper.width >= metrics_lower.width)
22490 lower_xoff = (width - metrics_lower.width) / 2;
22491 else
22492 upper_xoff = (width - metrics_upper.width) / 2;
22493 }
22494
22495 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
22496 top, bottom, and between upper and lower strings. */
22497 height = (metrics_upper.ascent + metrics_upper.descent
22498 + metrics_lower.ascent + metrics_lower.descent) + 5;
22499 /* Center vertically.
22500 H:base_height, D:base_descent
22501 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
22502
22503 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
22504 descent = D - H/2 + h/2;
22505 lower_yoff = descent - 2 - ld;
22506 upper_yoff = lower_yoff - la - 1 - ud; */
22507 ascent = - (it->descent - (base_height + height + 1) / 2);
22508 descent = it->descent - (base_height - height) / 2;
22509 lower_yoff = descent - 2 - metrics_lower.descent;
22510 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
22511 - metrics_upper.descent);
22512 /* Don't make the height shorter than the base height. */
22513 if (height > base_height)
22514 {
22515 it->ascent = ascent;
22516 it->descent = descent;
22517 }
22518 }
22519
22520 it->phys_ascent = it->ascent;
22521 it->phys_descent = it->descent;
22522 if (it->glyph_row)
22523 append_glyphless_glyph (it, face_id, for_no_font, len,
22524 upper_xoff, upper_yoff,
22525 lower_xoff, lower_yoff);
22526 it->nglyphs = 1;
22527 take_vertical_position_into_account (it);
22528 }
22529
22530
22531 /* RIF:
22532 Produce glyphs/get display metrics for the display element IT is
22533 loaded with. See the description of struct it in dispextern.h
22534 for an overview of struct it. */
22535
22536 void
22537 x_produce_glyphs (struct it *it)
22538 {
22539 int extra_line_spacing = it->extra_line_spacing;
22540
22541 it->glyph_not_available_p = 0;
22542
22543 if (it->what == IT_CHARACTER)
22544 {
22545 XChar2b char2b;
22546 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22547 struct font *font = face->font;
22548 struct font_metrics *pcm = NULL;
22549 int boff; /* baseline offset */
22550
22551 if (font == NULL)
22552 {
22553 /* When no suitable font is found, display this character by
22554 the method specified in the first extra slot of
22555 Vglyphless_char_display. */
22556 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
22557
22558 xassert (it->what == IT_GLYPHLESS);
22559 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
22560 goto done;
22561 }
22562
22563 boff = font->baseline_offset;
22564 if (font->vertical_centering)
22565 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22566
22567 if (it->char_to_display != '\n' && it->char_to_display != '\t')
22568 {
22569 int stretched_p;
22570
22571 it->nglyphs = 1;
22572
22573 if (it->override_ascent >= 0)
22574 {
22575 it->ascent = it->override_ascent;
22576 it->descent = it->override_descent;
22577 boff = it->override_boff;
22578 }
22579 else
22580 {
22581 it->ascent = FONT_BASE (font) + boff;
22582 it->descent = FONT_DESCENT (font) - boff;
22583 }
22584
22585 if (get_char_glyph_code (it->char_to_display, font, &char2b))
22586 {
22587 pcm = get_per_char_metric (it->f, font, &char2b);
22588 if (pcm->width == 0
22589 && pcm->rbearing == 0 && pcm->lbearing == 0)
22590 pcm = NULL;
22591 }
22592
22593 if (pcm)
22594 {
22595 it->phys_ascent = pcm->ascent + boff;
22596 it->phys_descent = pcm->descent - boff;
22597 it->pixel_width = pcm->width;
22598 }
22599 else
22600 {
22601 it->glyph_not_available_p = 1;
22602 it->phys_ascent = it->ascent;
22603 it->phys_descent = it->descent;
22604 it->pixel_width = font->space_width;
22605 }
22606
22607 if (it->constrain_row_ascent_descent_p)
22608 {
22609 if (it->descent > it->max_descent)
22610 {
22611 it->ascent += it->descent - it->max_descent;
22612 it->descent = it->max_descent;
22613 }
22614 if (it->ascent > it->max_ascent)
22615 {
22616 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22617 it->ascent = it->max_ascent;
22618 }
22619 it->phys_ascent = min (it->phys_ascent, it->ascent);
22620 it->phys_descent = min (it->phys_descent, it->descent);
22621 extra_line_spacing = 0;
22622 }
22623
22624 /* If this is a space inside a region of text with
22625 `space-width' property, change its width. */
22626 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22627 if (stretched_p)
22628 it->pixel_width *= XFLOATINT (it->space_width);
22629
22630 /* If face has a box, add the box thickness to the character
22631 height. If character has a box line to the left and/or
22632 right, add the box line width to the character's width. */
22633 if (face->box != FACE_NO_BOX)
22634 {
22635 int thick = face->box_line_width;
22636
22637 if (thick > 0)
22638 {
22639 it->ascent += thick;
22640 it->descent += thick;
22641 }
22642 else
22643 thick = -thick;
22644
22645 if (it->start_of_box_run_p)
22646 it->pixel_width += thick;
22647 if (it->end_of_box_run_p)
22648 it->pixel_width += thick;
22649 }
22650
22651 /* If face has an overline, add the height of the overline
22652 (1 pixel) and a 1 pixel margin to the character height. */
22653 if (face->overline_p)
22654 it->ascent += overline_margin;
22655
22656 if (it->constrain_row_ascent_descent_p)
22657 {
22658 if (it->ascent > it->max_ascent)
22659 it->ascent = it->max_ascent;
22660 if (it->descent > it->max_descent)
22661 it->descent = it->max_descent;
22662 }
22663
22664 take_vertical_position_into_account (it);
22665
22666 /* If we have to actually produce glyphs, do it. */
22667 if (it->glyph_row)
22668 {
22669 if (stretched_p)
22670 {
22671 /* Translate a space with a `space-width' property
22672 into a stretch glyph. */
22673 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22674 / FONT_HEIGHT (font));
22675 append_stretch_glyph (it, it->object, it->pixel_width,
22676 it->ascent + it->descent, ascent);
22677 }
22678 else
22679 append_glyph (it);
22680
22681 /* If characters with lbearing or rbearing are displayed
22682 in this line, record that fact in a flag of the
22683 glyph row. This is used to optimize X output code. */
22684 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22685 it->glyph_row->contains_overlapping_glyphs_p = 1;
22686 }
22687 if (! stretched_p && it->pixel_width == 0)
22688 /* We assure that all visible glyphs have at least 1-pixel
22689 width. */
22690 it->pixel_width = 1;
22691 }
22692 else if (it->char_to_display == '\n')
22693 {
22694 /* A newline has no width, but we need the height of the
22695 line. But if previous part of the line sets a height,
22696 don't increase that height */
22697
22698 Lisp_Object height;
22699 Lisp_Object total_height = Qnil;
22700
22701 it->override_ascent = -1;
22702 it->pixel_width = 0;
22703 it->nglyphs = 0;
22704
22705 height = get_it_property (it, Qline_height);
22706 /* Split (line-height total-height) list */
22707 if (CONSP (height)
22708 && CONSP (XCDR (height))
22709 && NILP (XCDR (XCDR (height))))
22710 {
22711 total_height = XCAR (XCDR (height));
22712 height = XCAR (height);
22713 }
22714 height = calc_line_height_property (it, height, font, boff, 1);
22715
22716 if (it->override_ascent >= 0)
22717 {
22718 it->ascent = it->override_ascent;
22719 it->descent = it->override_descent;
22720 boff = it->override_boff;
22721 }
22722 else
22723 {
22724 it->ascent = FONT_BASE (font) + boff;
22725 it->descent = FONT_DESCENT (font) - boff;
22726 }
22727
22728 if (EQ (height, Qt))
22729 {
22730 if (it->descent > it->max_descent)
22731 {
22732 it->ascent += it->descent - it->max_descent;
22733 it->descent = it->max_descent;
22734 }
22735 if (it->ascent > it->max_ascent)
22736 {
22737 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22738 it->ascent = it->max_ascent;
22739 }
22740 it->phys_ascent = min (it->phys_ascent, it->ascent);
22741 it->phys_descent = min (it->phys_descent, it->descent);
22742 it->constrain_row_ascent_descent_p = 1;
22743 extra_line_spacing = 0;
22744 }
22745 else
22746 {
22747 Lisp_Object spacing;
22748
22749 it->phys_ascent = it->ascent;
22750 it->phys_descent = it->descent;
22751
22752 if ((it->max_ascent > 0 || it->max_descent > 0)
22753 && face->box != FACE_NO_BOX
22754 && face->box_line_width > 0)
22755 {
22756 it->ascent += face->box_line_width;
22757 it->descent += face->box_line_width;
22758 }
22759 if (!NILP (height)
22760 && XINT (height) > it->ascent + it->descent)
22761 it->ascent = XINT (height) - it->descent;
22762
22763 if (!NILP (total_height))
22764 spacing = calc_line_height_property (it, total_height, font, boff, 0);
22765 else
22766 {
22767 spacing = get_it_property (it, Qline_spacing);
22768 spacing = calc_line_height_property (it, spacing, font, boff, 0);
22769 }
22770 if (INTEGERP (spacing))
22771 {
22772 extra_line_spacing = XINT (spacing);
22773 if (!NILP (total_height))
22774 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22775 }
22776 }
22777 }
22778 else /* i.e. (it->char_to_display == '\t') */
22779 {
22780 if (font->space_width > 0)
22781 {
22782 int tab_width = it->tab_width * font->space_width;
22783 int x = it->current_x + it->continuation_lines_width;
22784 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22785
22786 /* If the distance from the current position to the next tab
22787 stop is less than a space character width, use the
22788 tab stop after that. */
22789 if (next_tab_x - x < font->space_width)
22790 next_tab_x += tab_width;
22791
22792 it->pixel_width = next_tab_x - x;
22793 it->nglyphs = 1;
22794 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22795 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22796
22797 if (it->glyph_row)
22798 {
22799 append_stretch_glyph (it, it->object, it->pixel_width,
22800 it->ascent + it->descent, it->ascent);
22801 }
22802 }
22803 else
22804 {
22805 it->pixel_width = 0;
22806 it->nglyphs = 1;
22807 }
22808 }
22809 }
22810 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22811 {
22812 /* A static composition.
22813
22814 Note: A composition is represented as one glyph in the
22815 glyph matrix. There are no padding glyphs.
22816
22817 Important note: pixel_width, ascent, and descent are the
22818 values of what is drawn by draw_glyphs (i.e. the values of
22819 the overall glyphs composed). */
22820 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22821 int boff; /* baseline offset */
22822 struct composition *cmp = composition_table[it->cmp_it.id];
22823 int glyph_len = cmp->glyph_len;
22824 struct font *font = face->font;
22825
22826 it->nglyphs = 1;
22827
22828 /* If we have not yet calculated pixel size data of glyphs of
22829 the composition for the current face font, calculate them
22830 now. Theoretically, we have to check all fonts for the
22831 glyphs, but that requires much time and memory space. So,
22832 here we check only the font of the first glyph. This may
22833 lead to incorrect display, but it's very rare, and C-l
22834 (recenter-top-bottom) can correct the display anyway. */
22835 if (! cmp->font || cmp->font != font)
22836 {
22837 /* Ascent and descent of the font of the first character
22838 of this composition (adjusted by baseline offset).
22839 Ascent and descent of overall glyphs should not be less
22840 than these, respectively. */
22841 int font_ascent, font_descent, font_height;
22842 /* Bounding box of the overall glyphs. */
22843 int leftmost, rightmost, lowest, highest;
22844 int lbearing, rbearing;
22845 int i, width, ascent, descent;
22846 int left_padded = 0, right_padded = 0;
22847 int c;
22848 XChar2b char2b;
22849 struct font_metrics *pcm;
22850 int font_not_found_p;
22851 EMACS_INT pos;
22852
22853 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22854 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22855 break;
22856 if (glyph_len < cmp->glyph_len)
22857 right_padded = 1;
22858 for (i = 0; i < glyph_len; i++)
22859 {
22860 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22861 break;
22862 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22863 }
22864 if (i > 0)
22865 left_padded = 1;
22866
22867 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22868 : IT_CHARPOS (*it));
22869 /* If no suitable font is found, use the default font. */
22870 font_not_found_p = font == NULL;
22871 if (font_not_found_p)
22872 {
22873 face = face->ascii_face;
22874 font = face->font;
22875 }
22876 boff = font->baseline_offset;
22877 if (font->vertical_centering)
22878 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22879 font_ascent = FONT_BASE (font) + boff;
22880 font_descent = FONT_DESCENT (font) - boff;
22881 font_height = FONT_HEIGHT (font);
22882
22883 cmp->font = (void *) font;
22884
22885 pcm = NULL;
22886 if (! font_not_found_p)
22887 {
22888 get_char_face_and_encoding (it->f, c, it->face_id,
22889 &char2b, it->multibyte_p, 0);
22890 pcm = get_per_char_metric (it->f, font, &char2b);
22891 }
22892
22893 /* Initialize the bounding box. */
22894 if (pcm)
22895 {
22896 width = pcm->width;
22897 ascent = pcm->ascent;
22898 descent = pcm->descent;
22899 lbearing = pcm->lbearing;
22900 rbearing = pcm->rbearing;
22901 }
22902 else
22903 {
22904 width = font->space_width;
22905 ascent = FONT_BASE (font);
22906 descent = FONT_DESCENT (font);
22907 lbearing = 0;
22908 rbearing = width;
22909 }
22910
22911 rightmost = width;
22912 leftmost = 0;
22913 lowest = - descent + boff;
22914 highest = ascent + boff;
22915
22916 if (! font_not_found_p
22917 && font->default_ascent
22918 && CHAR_TABLE_P (Vuse_default_ascent)
22919 && !NILP (Faref (Vuse_default_ascent,
22920 make_number (it->char_to_display))))
22921 highest = font->default_ascent + boff;
22922
22923 /* Draw the first glyph at the normal position. It may be
22924 shifted to right later if some other glyphs are drawn
22925 at the left. */
22926 cmp->offsets[i * 2] = 0;
22927 cmp->offsets[i * 2 + 1] = boff;
22928 cmp->lbearing = lbearing;
22929 cmp->rbearing = rbearing;
22930
22931 /* Set cmp->offsets for the remaining glyphs. */
22932 for (i++; i < glyph_len; i++)
22933 {
22934 int left, right, btm, top;
22935 int ch = COMPOSITION_GLYPH (cmp, i);
22936 int face_id;
22937 struct face *this_face;
22938 int this_boff;
22939
22940 if (ch == '\t')
22941 ch = ' ';
22942 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22943 this_face = FACE_FROM_ID (it->f, face_id);
22944 font = this_face->font;
22945
22946 if (font == NULL)
22947 pcm = NULL;
22948 else
22949 {
22950 this_boff = font->baseline_offset;
22951 if (font->vertical_centering)
22952 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22953 get_char_face_and_encoding (it->f, ch, face_id,
22954 &char2b, it->multibyte_p, 0);
22955 pcm = get_per_char_metric (it->f, font, &char2b);
22956 }
22957 if (! pcm)
22958 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22959 else
22960 {
22961 width = pcm->width;
22962 ascent = pcm->ascent;
22963 descent = pcm->descent;
22964 lbearing = pcm->lbearing;
22965 rbearing = pcm->rbearing;
22966 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22967 {
22968 /* Relative composition with or without
22969 alternate chars. */
22970 left = (leftmost + rightmost - width) / 2;
22971 btm = - descent + boff;
22972 if (font->relative_compose
22973 && (! CHAR_TABLE_P (Vignore_relative_composition)
22974 || NILP (Faref (Vignore_relative_composition,
22975 make_number (ch)))))
22976 {
22977
22978 if (- descent >= font->relative_compose)
22979 /* One extra pixel between two glyphs. */
22980 btm = highest + 1;
22981 else if (ascent <= 0)
22982 /* One extra pixel between two glyphs. */
22983 btm = lowest - 1 - ascent - descent;
22984 }
22985 }
22986 else
22987 {
22988 /* A composition rule is specified by an integer
22989 value that encodes global and new reference
22990 points (GREF and NREF). GREF and NREF are
22991 specified by numbers as below:
22992
22993 0---1---2 -- ascent
22994 | |
22995 | |
22996 | |
22997 9--10--11 -- center
22998 | |
22999 ---3---4---5--- baseline
23000 | |
23001 6---7---8 -- descent
23002 */
23003 int rule = COMPOSITION_RULE (cmp, i);
23004 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23005
23006 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23007 grefx = gref % 3, nrefx = nref % 3;
23008 grefy = gref / 3, nrefy = nref / 3;
23009 if (xoff)
23010 xoff = font_height * (xoff - 128) / 256;
23011 if (yoff)
23012 yoff = font_height * (yoff - 128) / 256;
23013
23014 left = (leftmost
23015 + grefx * (rightmost - leftmost) / 2
23016 - nrefx * width / 2
23017 + xoff);
23018
23019 btm = ((grefy == 0 ? highest
23020 : grefy == 1 ? 0
23021 : grefy == 2 ? lowest
23022 : (highest + lowest) / 2)
23023 - (nrefy == 0 ? ascent + descent
23024 : nrefy == 1 ? descent - boff
23025 : nrefy == 2 ? 0
23026 : (ascent + descent) / 2)
23027 + yoff);
23028 }
23029
23030 cmp->offsets[i * 2] = left;
23031 cmp->offsets[i * 2 + 1] = btm + descent;
23032
23033 /* Update the bounding box of the overall glyphs. */
23034 if (width > 0)
23035 {
23036 right = left + width;
23037 if (left < leftmost)
23038 leftmost = left;
23039 if (right > rightmost)
23040 rightmost = right;
23041 }
23042 top = btm + descent + ascent;
23043 if (top > highest)
23044 highest = top;
23045 if (btm < lowest)
23046 lowest = btm;
23047
23048 if (cmp->lbearing > left + lbearing)
23049 cmp->lbearing = left + lbearing;
23050 if (cmp->rbearing < left + rbearing)
23051 cmp->rbearing = left + rbearing;
23052 }
23053 }
23054
23055 /* If there are glyphs whose x-offsets are negative,
23056 shift all glyphs to the right and make all x-offsets
23057 non-negative. */
23058 if (leftmost < 0)
23059 {
23060 for (i = 0; i < cmp->glyph_len; i++)
23061 cmp->offsets[i * 2] -= leftmost;
23062 rightmost -= leftmost;
23063 cmp->lbearing -= leftmost;
23064 cmp->rbearing -= leftmost;
23065 }
23066
23067 if (left_padded && cmp->lbearing < 0)
23068 {
23069 for (i = 0; i < cmp->glyph_len; i++)
23070 cmp->offsets[i * 2] -= cmp->lbearing;
23071 rightmost -= cmp->lbearing;
23072 cmp->rbearing -= cmp->lbearing;
23073 cmp->lbearing = 0;
23074 }
23075 if (right_padded && rightmost < cmp->rbearing)
23076 {
23077 rightmost = cmp->rbearing;
23078 }
23079
23080 cmp->pixel_width = rightmost;
23081 cmp->ascent = highest;
23082 cmp->descent = - lowest;
23083 if (cmp->ascent < font_ascent)
23084 cmp->ascent = font_ascent;
23085 if (cmp->descent < font_descent)
23086 cmp->descent = font_descent;
23087 }
23088
23089 if (it->glyph_row
23090 && (cmp->lbearing < 0
23091 || cmp->rbearing > cmp->pixel_width))
23092 it->glyph_row->contains_overlapping_glyphs_p = 1;
23093
23094 it->pixel_width = cmp->pixel_width;
23095 it->ascent = it->phys_ascent = cmp->ascent;
23096 it->descent = it->phys_descent = cmp->descent;
23097 if (face->box != FACE_NO_BOX)
23098 {
23099 int thick = face->box_line_width;
23100
23101 if (thick > 0)
23102 {
23103 it->ascent += thick;
23104 it->descent += thick;
23105 }
23106 else
23107 thick = - thick;
23108
23109 if (it->start_of_box_run_p)
23110 it->pixel_width += thick;
23111 if (it->end_of_box_run_p)
23112 it->pixel_width += thick;
23113 }
23114
23115 /* If face has an overline, add the height of the overline
23116 (1 pixel) and a 1 pixel margin to the character height. */
23117 if (face->overline_p)
23118 it->ascent += overline_margin;
23119
23120 take_vertical_position_into_account (it);
23121 if (it->ascent < 0)
23122 it->ascent = 0;
23123 if (it->descent < 0)
23124 it->descent = 0;
23125
23126 if (it->glyph_row)
23127 append_composite_glyph (it);
23128 }
23129 else if (it->what == IT_COMPOSITION)
23130 {
23131 /* A dynamic (automatic) composition. */
23132 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23133 Lisp_Object gstring;
23134 struct font_metrics metrics;
23135
23136 gstring = composition_gstring_from_id (it->cmp_it.id);
23137 it->pixel_width
23138 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23139 &metrics);
23140 if (it->glyph_row
23141 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23142 it->glyph_row->contains_overlapping_glyphs_p = 1;
23143 it->ascent = it->phys_ascent = metrics.ascent;
23144 it->descent = it->phys_descent = metrics.descent;
23145 if (face->box != FACE_NO_BOX)
23146 {
23147 int thick = face->box_line_width;
23148
23149 if (thick > 0)
23150 {
23151 it->ascent += thick;
23152 it->descent += thick;
23153 }
23154 else
23155 thick = - thick;
23156
23157 if (it->start_of_box_run_p)
23158 it->pixel_width += thick;
23159 if (it->end_of_box_run_p)
23160 it->pixel_width += thick;
23161 }
23162 /* If face has an overline, add the height of the overline
23163 (1 pixel) and a 1 pixel margin to the character height. */
23164 if (face->overline_p)
23165 it->ascent += overline_margin;
23166 take_vertical_position_into_account (it);
23167 if (it->ascent < 0)
23168 it->ascent = 0;
23169 if (it->descent < 0)
23170 it->descent = 0;
23171
23172 if (it->glyph_row)
23173 append_composite_glyph (it);
23174 }
23175 else if (it->what == IT_GLYPHLESS)
23176 produce_glyphless_glyph (it, 0, Qnil);
23177 else if (it->what == IT_IMAGE)
23178 produce_image_glyph (it);
23179 else if (it->what == IT_STRETCH)
23180 produce_stretch_glyph (it);
23181
23182 done:
23183 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23184 because this isn't true for images with `:ascent 100'. */
23185 xassert (it->ascent >= 0 && it->descent >= 0);
23186 if (it->area == TEXT_AREA)
23187 it->current_x += it->pixel_width;
23188
23189 if (extra_line_spacing > 0)
23190 {
23191 it->descent += extra_line_spacing;
23192 if (extra_line_spacing > it->max_extra_line_spacing)
23193 it->max_extra_line_spacing = extra_line_spacing;
23194 }
23195
23196 it->max_ascent = max (it->max_ascent, it->ascent);
23197 it->max_descent = max (it->max_descent, it->descent);
23198 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23199 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23200 }
23201
23202 /* EXPORT for RIF:
23203 Output LEN glyphs starting at START at the nominal cursor position.
23204 Advance the nominal cursor over the text. The global variable
23205 updated_window contains the window being updated, updated_row is
23206 the glyph row being updated, and updated_area is the area of that
23207 row being updated. */
23208
23209 void
23210 x_write_glyphs (struct glyph *start, int len)
23211 {
23212 int x, hpos;
23213
23214 xassert (updated_window && updated_row);
23215 BLOCK_INPUT;
23216
23217 /* Write glyphs. */
23218
23219 hpos = start - updated_row->glyphs[updated_area];
23220 x = draw_glyphs (updated_window, output_cursor.x,
23221 updated_row, updated_area,
23222 hpos, hpos + len,
23223 DRAW_NORMAL_TEXT, 0);
23224
23225 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23226 if (updated_area == TEXT_AREA
23227 && updated_window->phys_cursor_on_p
23228 && updated_window->phys_cursor.vpos == output_cursor.vpos
23229 && updated_window->phys_cursor.hpos >= hpos
23230 && updated_window->phys_cursor.hpos < hpos + len)
23231 updated_window->phys_cursor_on_p = 0;
23232
23233 UNBLOCK_INPUT;
23234
23235 /* Advance the output cursor. */
23236 output_cursor.hpos += len;
23237 output_cursor.x = x;
23238 }
23239
23240
23241 /* EXPORT for RIF:
23242 Insert LEN glyphs from START at the nominal cursor position. */
23243
23244 void
23245 x_insert_glyphs (struct glyph *start, int len)
23246 {
23247 struct frame *f;
23248 struct window *w;
23249 int line_height, shift_by_width, shifted_region_width;
23250 struct glyph_row *row;
23251 struct glyph *glyph;
23252 int frame_x, frame_y;
23253 EMACS_INT hpos;
23254
23255 xassert (updated_window && updated_row);
23256 BLOCK_INPUT;
23257 w = updated_window;
23258 f = XFRAME (WINDOW_FRAME (w));
23259
23260 /* Get the height of the line we are in. */
23261 row = updated_row;
23262 line_height = row->height;
23263
23264 /* Get the width of the glyphs to insert. */
23265 shift_by_width = 0;
23266 for (glyph = start; glyph < start + len; ++glyph)
23267 shift_by_width += glyph->pixel_width;
23268
23269 /* Get the width of the region to shift right. */
23270 shifted_region_width = (window_box_width (w, updated_area)
23271 - output_cursor.x
23272 - shift_by_width);
23273
23274 /* Shift right. */
23275 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23276 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23277
23278 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23279 line_height, shift_by_width);
23280
23281 /* Write the glyphs. */
23282 hpos = start - row->glyphs[updated_area];
23283 draw_glyphs (w, output_cursor.x, row, updated_area,
23284 hpos, hpos + len,
23285 DRAW_NORMAL_TEXT, 0);
23286
23287 /* Advance the output cursor. */
23288 output_cursor.hpos += len;
23289 output_cursor.x += shift_by_width;
23290 UNBLOCK_INPUT;
23291 }
23292
23293
23294 /* EXPORT for RIF:
23295 Erase the current text line from the nominal cursor position
23296 (inclusive) to pixel column TO_X (exclusive). The idea is that
23297 everything from TO_X onward is already erased.
23298
23299 TO_X is a pixel position relative to updated_area of
23300 updated_window. TO_X == -1 means clear to the end of this area. */
23301
23302 void
23303 x_clear_end_of_line (int to_x)
23304 {
23305 struct frame *f;
23306 struct window *w = updated_window;
23307 int max_x, min_y, max_y;
23308 int from_x, from_y, to_y;
23309
23310 xassert (updated_window && updated_row);
23311 f = XFRAME (w->frame);
23312
23313 if (updated_row->full_width_p)
23314 max_x = WINDOW_TOTAL_WIDTH (w);
23315 else
23316 max_x = window_box_width (w, updated_area);
23317 max_y = window_text_bottom_y (w);
23318
23319 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23320 of window. For TO_X > 0, truncate to end of drawing area. */
23321 if (to_x == 0)
23322 return;
23323 else if (to_x < 0)
23324 to_x = max_x;
23325 else
23326 to_x = min (to_x, max_x);
23327
23328 to_y = min (max_y, output_cursor.y + updated_row->height);
23329
23330 /* Notice if the cursor will be cleared by this operation. */
23331 if (!updated_row->full_width_p)
23332 notice_overwritten_cursor (w, updated_area,
23333 output_cursor.x, -1,
23334 updated_row->y,
23335 MATRIX_ROW_BOTTOM_Y (updated_row));
23336
23337 from_x = output_cursor.x;
23338
23339 /* Translate to frame coordinates. */
23340 if (updated_row->full_width_p)
23341 {
23342 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23343 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23344 }
23345 else
23346 {
23347 int area_left = window_box_left (w, updated_area);
23348 from_x += area_left;
23349 to_x += area_left;
23350 }
23351
23352 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23353 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23354 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23355
23356 /* Prevent inadvertently clearing to end of the X window. */
23357 if (to_x > from_x && to_y > from_y)
23358 {
23359 BLOCK_INPUT;
23360 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23361 to_x - from_x, to_y - from_y);
23362 UNBLOCK_INPUT;
23363 }
23364 }
23365
23366 #endif /* HAVE_WINDOW_SYSTEM */
23367
23368
23369 \f
23370 /***********************************************************************
23371 Cursor types
23372 ***********************************************************************/
23373
23374 /* Value is the internal representation of the specified cursor type
23375 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23376 of the bar cursor. */
23377
23378 static enum text_cursor_kinds
23379 get_specified_cursor_type (Lisp_Object arg, int *width)
23380 {
23381 enum text_cursor_kinds type;
23382
23383 if (NILP (arg))
23384 return NO_CURSOR;
23385
23386 if (EQ (arg, Qbox))
23387 return FILLED_BOX_CURSOR;
23388
23389 if (EQ (arg, Qhollow))
23390 return HOLLOW_BOX_CURSOR;
23391
23392 if (EQ (arg, Qbar))
23393 {
23394 *width = 2;
23395 return BAR_CURSOR;
23396 }
23397
23398 if (CONSP (arg)
23399 && EQ (XCAR (arg), Qbar)
23400 && INTEGERP (XCDR (arg))
23401 && XINT (XCDR (arg)) >= 0)
23402 {
23403 *width = XINT (XCDR (arg));
23404 return BAR_CURSOR;
23405 }
23406
23407 if (EQ (arg, Qhbar))
23408 {
23409 *width = 2;
23410 return HBAR_CURSOR;
23411 }
23412
23413 if (CONSP (arg)
23414 && EQ (XCAR (arg), Qhbar)
23415 && INTEGERP (XCDR (arg))
23416 && XINT (XCDR (arg)) >= 0)
23417 {
23418 *width = XINT (XCDR (arg));
23419 return HBAR_CURSOR;
23420 }
23421
23422 /* Treat anything unknown as "hollow box cursor".
23423 It was bad to signal an error; people have trouble fixing
23424 .Xdefaults with Emacs, when it has something bad in it. */
23425 type = HOLLOW_BOX_CURSOR;
23426
23427 return type;
23428 }
23429
23430 /* Set the default cursor types for specified frame. */
23431 void
23432 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23433 {
23434 int width;
23435 Lisp_Object tem;
23436
23437 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23438 FRAME_CURSOR_WIDTH (f) = width;
23439
23440 /* By default, set up the blink-off state depending on the on-state. */
23441
23442 tem = Fassoc (arg, Vblink_cursor_alist);
23443 if (!NILP (tem))
23444 {
23445 FRAME_BLINK_OFF_CURSOR (f)
23446 = get_specified_cursor_type (XCDR (tem), &width);
23447 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23448 }
23449 else
23450 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23451 }
23452
23453
23454 #ifdef HAVE_WINDOW_SYSTEM
23455
23456 /* Return the cursor we want to be displayed in window W. Return
23457 width of bar/hbar cursor through WIDTH arg. Return with
23458 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23459 (i.e. if the `system caret' should track this cursor).
23460
23461 In a mini-buffer window, we want the cursor only to appear if we
23462 are reading input from this window. For the selected window, we
23463 want the cursor type given by the frame parameter or buffer local
23464 setting of cursor-type. If explicitly marked off, draw no cursor.
23465 In all other cases, we want a hollow box cursor. */
23466
23467 static enum text_cursor_kinds
23468 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23469 int *active_cursor)
23470 {
23471 struct frame *f = XFRAME (w->frame);
23472 struct buffer *b = XBUFFER (w->buffer);
23473 int cursor_type = DEFAULT_CURSOR;
23474 Lisp_Object alt_cursor;
23475 int non_selected = 0;
23476
23477 *active_cursor = 1;
23478
23479 /* Echo area */
23480 if (cursor_in_echo_area
23481 && FRAME_HAS_MINIBUF_P (f)
23482 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23483 {
23484 if (w == XWINDOW (echo_area_window))
23485 {
23486 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
23487 {
23488 *width = FRAME_CURSOR_WIDTH (f);
23489 return FRAME_DESIRED_CURSOR (f);
23490 }
23491 else
23492 return get_specified_cursor_type (b->cursor_type, width);
23493 }
23494
23495 *active_cursor = 0;
23496 non_selected = 1;
23497 }
23498
23499 /* Detect a nonselected window or nonselected frame. */
23500 else if (w != XWINDOW (f->selected_window)
23501 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
23502 {
23503 *active_cursor = 0;
23504
23505 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23506 return NO_CURSOR;
23507
23508 non_selected = 1;
23509 }
23510
23511 /* Never display a cursor in a window in which cursor-type is nil. */
23512 if (NILP (b->cursor_type))
23513 return NO_CURSOR;
23514
23515 /* Get the normal cursor type for this window. */
23516 if (EQ (b->cursor_type, Qt))
23517 {
23518 cursor_type = FRAME_DESIRED_CURSOR (f);
23519 *width = FRAME_CURSOR_WIDTH (f);
23520 }
23521 else
23522 cursor_type = get_specified_cursor_type (b->cursor_type, width);
23523
23524 /* Use cursor-in-non-selected-windows instead
23525 for non-selected window or frame. */
23526 if (non_selected)
23527 {
23528 alt_cursor = b->cursor_in_non_selected_windows;
23529 if (!EQ (Qt, alt_cursor))
23530 return get_specified_cursor_type (alt_cursor, width);
23531 /* t means modify the normal cursor type. */
23532 if (cursor_type == FILLED_BOX_CURSOR)
23533 cursor_type = HOLLOW_BOX_CURSOR;
23534 else if (cursor_type == BAR_CURSOR && *width > 1)
23535 --*width;
23536 return cursor_type;
23537 }
23538
23539 /* Use normal cursor if not blinked off. */
23540 if (!w->cursor_off_p)
23541 {
23542 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23543 {
23544 if (cursor_type == FILLED_BOX_CURSOR)
23545 {
23546 /* Using a block cursor on large images can be very annoying.
23547 So use a hollow cursor for "large" images.
23548 If image is not transparent (no mask), also use hollow cursor. */
23549 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23550 if (img != NULL && IMAGEP (img->spec))
23551 {
23552 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23553 where N = size of default frame font size.
23554 This should cover most of the "tiny" icons people may use. */
23555 if (!img->mask
23556 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23557 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23558 cursor_type = HOLLOW_BOX_CURSOR;
23559 }
23560 }
23561 else if (cursor_type != NO_CURSOR)
23562 {
23563 /* Display current only supports BOX and HOLLOW cursors for images.
23564 So for now, unconditionally use a HOLLOW cursor when cursor is
23565 not a solid box cursor. */
23566 cursor_type = HOLLOW_BOX_CURSOR;
23567 }
23568 }
23569 return cursor_type;
23570 }
23571
23572 /* Cursor is blinked off, so determine how to "toggle" it. */
23573
23574 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23575 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
23576 return get_specified_cursor_type (XCDR (alt_cursor), width);
23577
23578 /* Then see if frame has specified a specific blink off cursor type. */
23579 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23580 {
23581 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23582 return FRAME_BLINK_OFF_CURSOR (f);
23583 }
23584
23585 #if 0
23586 /* Some people liked having a permanently visible blinking cursor,
23587 while others had very strong opinions against it. So it was
23588 decided to remove it. KFS 2003-09-03 */
23589
23590 /* Finally perform built-in cursor blinking:
23591 filled box <-> hollow box
23592 wide [h]bar <-> narrow [h]bar
23593 narrow [h]bar <-> no cursor
23594 other type <-> no cursor */
23595
23596 if (cursor_type == FILLED_BOX_CURSOR)
23597 return HOLLOW_BOX_CURSOR;
23598
23599 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23600 {
23601 *width = 1;
23602 return cursor_type;
23603 }
23604 #endif
23605
23606 return NO_CURSOR;
23607 }
23608
23609
23610 /* Notice when the text cursor of window W has been completely
23611 overwritten by a drawing operation that outputs glyphs in AREA
23612 starting at X0 and ending at X1 in the line starting at Y0 and
23613 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23614 the rest of the line after X0 has been written. Y coordinates
23615 are window-relative. */
23616
23617 static void
23618 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23619 int x0, int x1, int y0, int y1)
23620 {
23621 int cx0, cx1, cy0, cy1;
23622 struct glyph_row *row;
23623
23624 if (!w->phys_cursor_on_p)
23625 return;
23626 if (area != TEXT_AREA)
23627 return;
23628
23629 if (w->phys_cursor.vpos < 0
23630 || w->phys_cursor.vpos >= w->current_matrix->nrows
23631 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23632 !(row->enabled_p && row->displays_text_p)))
23633 return;
23634
23635 if (row->cursor_in_fringe_p)
23636 {
23637 row->cursor_in_fringe_p = 0;
23638 draw_fringe_bitmap (w, row, row->reversed_p);
23639 w->phys_cursor_on_p = 0;
23640 return;
23641 }
23642
23643 cx0 = w->phys_cursor.x;
23644 cx1 = cx0 + w->phys_cursor_width;
23645 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23646 return;
23647
23648 /* The cursor image will be completely removed from the
23649 screen if the output area intersects the cursor area in
23650 y-direction. When we draw in [y0 y1[, and some part of
23651 the cursor is at y < y0, that part must have been drawn
23652 before. When scrolling, the cursor is erased before
23653 actually scrolling, so we don't come here. When not
23654 scrolling, the rows above the old cursor row must have
23655 changed, and in this case these rows must have written
23656 over the cursor image.
23657
23658 Likewise if part of the cursor is below y1, with the
23659 exception of the cursor being in the first blank row at
23660 the buffer and window end because update_text_area
23661 doesn't draw that row. (Except when it does, but
23662 that's handled in update_text_area.) */
23663
23664 cy0 = w->phys_cursor.y;
23665 cy1 = cy0 + w->phys_cursor_height;
23666 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23667 return;
23668
23669 w->phys_cursor_on_p = 0;
23670 }
23671
23672 #endif /* HAVE_WINDOW_SYSTEM */
23673
23674 \f
23675 /************************************************************************
23676 Mouse Face
23677 ************************************************************************/
23678
23679 #ifdef HAVE_WINDOW_SYSTEM
23680
23681 /* EXPORT for RIF:
23682 Fix the display of area AREA of overlapping row ROW in window W
23683 with respect to the overlapping part OVERLAPS. */
23684
23685 void
23686 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
23687 enum glyph_row_area area, int overlaps)
23688 {
23689 int i, x;
23690
23691 BLOCK_INPUT;
23692
23693 x = 0;
23694 for (i = 0; i < row->used[area];)
23695 {
23696 if (row->glyphs[area][i].overlaps_vertically_p)
23697 {
23698 int start = i, start_x = x;
23699
23700 do
23701 {
23702 x += row->glyphs[area][i].pixel_width;
23703 ++i;
23704 }
23705 while (i < row->used[area]
23706 && row->glyphs[area][i].overlaps_vertically_p);
23707
23708 draw_glyphs (w, start_x, row, area,
23709 start, i,
23710 DRAW_NORMAL_TEXT, overlaps);
23711 }
23712 else
23713 {
23714 x += row->glyphs[area][i].pixel_width;
23715 ++i;
23716 }
23717 }
23718
23719 UNBLOCK_INPUT;
23720 }
23721
23722
23723 /* EXPORT:
23724 Draw the cursor glyph of window W in glyph row ROW. See the
23725 comment of draw_glyphs for the meaning of HL. */
23726
23727 void
23728 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23729 enum draw_glyphs_face hl)
23730 {
23731 /* If cursor hpos is out of bounds, don't draw garbage. This can
23732 happen in mini-buffer windows when switching between echo area
23733 glyphs and mini-buffer. */
23734 if ((row->reversed_p
23735 ? (w->phys_cursor.hpos >= 0)
23736 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23737 {
23738 int on_p = w->phys_cursor_on_p;
23739 int x1;
23740 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23741 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23742 hl, 0);
23743 w->phys_cursor_on_p = on_p;
23744
23745 if (hl == DRAW_CURSOR)
23746 w->phys_cursor_width = x1 - w->phys_cursor.x;
23747 /* When we erase the cursor, and ROW is overlapped by other
23748 rows, make sure that these overlapping parts of other rows
23749 are redrawn. */
23750 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23751 {
23752 w->phys_cursor_width = x1 - w->phys_cursor.x;
23753
23754 if (row > w->current_matrix->rows
23755 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23756 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23757 OVERLAPS_ERASED_CURSOR);
23758
23759 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23760 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23761 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23762 OVERLAPS_ERASED_CURSOR);
23763 }
23764 }
23765 }
23766
23767
23768 /* EXPORT:
23769 Erase the image of a cursor of window W from the screen. */
23770
23771 void
23772 erase_phys_cursor (struct window *w)
23773 {
23774 struct frame *f = XFRAME (w->frame);
23775 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23776 int hpos = w->phys_cursor.hpos;
23777 int vpos = w->phys_cursor.vpos;
23778 int mouse_face_here_p = 0;
23779 struct glyph_matrix *active_glyphs = w->current_matrix;
23780 struct glyph_row *cursor_row;
23781 struct glyph *cursor_glyph;
23782 enum draw_glyphs_face hl;
23783
23784 /* No cursor displayed or row invalidated => nothing to do on the
23785 screen. */
23786 if (w->phys_cursor_type == NO_CURSOR)
23787 goto mark_cursor_off;
23788
23789 /* VPOS >= active_glyphs->nrows means that window has been resized.
23790 Don't bother to erase the cursor. */
23791 if (vpos >= active_glyphs->nrows)
23792 goto mark_cursor_off;
23793
23794 /* If row containing cursor is marked invalid, there is nothing we
23795 can do. */
23796 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23797 if (!cursor_row->enabled_p)
23798 goto mark_cursor_off;
23799
23800 /* If line spacing is > 0, old cursor may only be partially visible in
23801 window after split-window. So adjust visible height. */
23802 cursor_row->visible_height = min (cursor_row->visible_height,
23803 window_text_bottom_y (w) - cursor_row->y);
23804
23805 /* If row is completely invisible, don't attempt to delete a cursor which
23806 isn't there. This can happen if cursor is at top of a window, and
23807 we switch to a buffer with a header line in that window. */
23808 if (cursor_row->visible_height <= 0)
23809 goto mark_cursor_off;
23810
23811 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23812 if (cursor_row->cursor_in_fringe_p)
23813 {
23814 cursor_row->cursor_in_fringe_p = 0;
23815 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23816 goto mark_cursor_off;
23817 }
23818
23819 /* This can happen when the new row is shorter than the old one.
23820 In this case, either draw_glyphs or clear_end_of_line
23821 should have cleared the cursor. Note that we wouldn't be
23822 able to erase the cursor in this case because we don't have a
23823 cursor glyph at hand. */
23824 if ((cursor_row->reversed_p
23825 ? (w->phys_cursor.hpos < 0)
23826 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23827 goto mark_cursor_off;
23828
23829 /* If the cursor is in the mouse face area, redisplay that when
23830 we clear the cursor. */
23831 if (! NILP (hlinfo->mouse_face_window)
23832 && coords_in_mouse_face_p (w, hpos, vpos)
23833 /* Don't redraw the cursor's spot in mouse face if it is at the
23834 end of a line (on a newline). The cursor appears there, but
23835 mouse highlighting does not. */
23836 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
23837 mouse_face_here_p = 1;
23838
23839 /* Maybe clear the display under the cursor. */
23840 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23841 {
23842 int x, y, left_x;
23843 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23844 int width;
23845
23846 cursor_glyph = get_phys_cursor_glyph (w);
23847 if (cursor_glyph == NULL)
23848 goto mark_cursor_off;
23849
23850 width = cursor_glyph->pixel_width;
23851 left_x = window_box_left_offset (w, TEXT_AREA);
23852 x = w->phys_cursor.x;
23853 if (x < left_x)
23854 width -= left_x - x;
23855 width = min (width, window_box_width (w, TEXT_AREA) - x);
23856 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23857 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23858
23859 if (width > 0)
23860 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23861 }
23862
23863 /* Erase the cursor by redrawing the character underneath it. */
23864 if (mouse_face_here_p)
23865 hl = DRAW_MOUSE_FACE;
23866 else
23867 hl = DRAW_NORMAL_TEXT;
23868 draw_phys_cursor_glyph (w, cursor_row, hl);
23869
23870 mark_cursor_off:
23871 w->phys_cursor_on_p = 0;
23872 w->phys_cursor_type = NO_CURSOR;
23873 }
23874
23875
23876 /* EXPORT:
23877 Display or clear cursor of window W. If ON is zero, clear the
23878 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23879 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23880
23881 void
23882 display_and_set_cursor (struct window *w, int on,
23883 int hpos, int vpos, int x, int y)
23884 {
23885 struct frame *f = XFRAME (w->frame);
23886 int new_cursor_type;
23887 int new_cursor_width;
23888 int active_cursor;
23889 struct glyph_row *glyph_row;
23890 struct glyph *glyph;
23891
23892 /* This is pointless on invisible frames, and dangerous on garbaged
23893 windows and frames; in the latter case, the frame or window may
23894 be in the midst of changing its size, and x and y may be off the
23895 window. */
23896 if (! FRAME_VISIBLE_P (f)
23897 || FRAME_GARBAGED_P (f)
23898 || vpos >= w->current_matrix->nrows
23899 || hpos >= w->current_matrix->matrix_w)
23900 return;
23901
23902 /* If cursor is off and we want it off, return quickly. */
23903 if (!on && !w->phys_cursor_on_p)
23904 return;
23905
23906 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23907 /* If cursor row is not enabled, we don't really know where to
23908 display the cursor. */
23909 if (!glyph_row->enabled_p)
23910 {
23911 w->phys_cursor_on_p = 0;
23912 return;
23913 }
23914
23915 glyph = NULL;
23916 if (!glyph_row->exact_window_width_line_p
23917 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
23918 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23919
23920 xassert (interrupt_input_blocked);
23921
23922 /* Set new_cursor_type to the cursor we want to be displayed. */
23923 new_cursor_type = get_window_cursor_type (w, glyph,
23924 &new_cursor_width, &active_cursor);
23925
23926 /* If cursor is currently being shown and we don't want it to be or
23927 it is in the wrong place, or the cursor type is not what we want,
23928 erase it. */
23929 if (w->phys_cursor_on_p
23930 && (!on
23931 || w->phys_cursor.x != x
23932 || w->phys_cursor.y != y
23933 || new_cursor_type != w->phys_cursor_type
23934 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23935 && new_cursor_width != w->phys_cursor_width)))
23936 erase_phys_cursor (w);
23937
23938 /* Don't check phys_cursor_on_p here because that flag is only set
23939 to zero in some cases where we know that the cursor has been
23940 completely erased, to avoid the extra work of erasing the cursor
23941 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23942 still not be visible, or it has only been partly erased. */
23943 if (on)
23944 {
23945 w->phys_cursor_ascent = glyph_row->ascent;
23946 w->phys_cursor_height = glyph_row->height;
23947
23948 /* Set phys_cursor_.* before x_draw_.* is called because some
23949 of them may need the information. */
23950 w->phys_cursor.x = x;
23951 w->phys_cursor.y = glyph_row->y;
23952 w->phys_cursor.hpos = hpos;
23953 w->phys_cursor.vpos = vpos;
23954 }
23955
23956 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23957 new_cursor_type, new_cursor_width,
23958 on, active_cursor);
23959 }
23960
23961
23962 /* Switch the display of W's cursor on or off, according to the value
23963 of ON. */
23964
23965 void
23966 update_window_cursor (struct window *w, int on)
23967 {
23968 /* Don't update cursor in windows whose frame is in the process
23969 of being deleted. */
23970 if (w->current_matrix)
23971 {
23972 BLOCK_INPUT;
23973 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23974 w->phys_cursor.x, w->phys_cursor.y);
23975 UNBLOCK_INPUT;
23976 }
23977 }
23978
23979
23980 /* Call update_window_cursor with parameter ON_P on all leaf windows
23981 in the window tree rooted at W. */
23982
23983 static void
23984 update_cursor_in_window_tree (struct window *w, int on_p)
23985 {
23986 while (w)
23987 {
23988 if (!NILP (w->hchild))
23989 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23990 else if (!NILP (w->vchild))
23991 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23992 else
23993 update_window_cursor (w, on_p);
23994
23995 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23996 }
23997 }
23998
23999
24000 /* EXPORT:
24001 Display the cursor on window W, or clear it, according to ON_P.
24002 Don't change the cursor's position. */
24003
24004 void
24005 x_update_cursor (struct frame *f, int on_p)
24006 {
24007 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24008 }
24009
24010
24011 /* EXPORT:
24012 Clear the cursor of window W to background color, and mark the
24013 cursor as not shown. This is used when the text where the cursor
24014 is about to be rewritten. */
24015
24016 void
24017 x_clear_cursor (struct window *w)
24018 {
24019 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24020 update_window_cursor (w, 0);
24021 }
24022
24023 #endif /* HAVE_WINDOW_SYSTEM */
24024
24025 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24026 and MSDOS. */
24027 void
24028 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24029 int start_hpos, int end_hpos,
24030 enum draw_glyphs_face draw)
24031 {
24032 #ifdef HAVE_WINDOW_SYSTEM
24033 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24034 {
24035 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24036 return;
24037 }
24038 #endif
24039 #if defined (HAVE_GPM) || defined (MSDOS)
24040 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24041 #endif
24042 }
24043
24044 /* EXPORT:
24045 Display the active region described by mouse_face_* according to DRAW. */
24046
24047 void
24048 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24049 {
24050 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24051 struct frame *f = XFRAME (WINDOW_FRAME (w));
24052
24053 if (/* If window is in the process of being destroyed, don't bother
24054 to do anything. */
24055 w->current_matrix != NULL
24056 /* Don't update mouse highlight if hidden */
24057 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24058 /* Recognize when we are called to operate on rows that don't exist
24059 anymore. This can happen when a window is split. */
24060 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24061 {
24062 int phys_cursor_on_p = w->phys_cursor_on_p;
24063 struct glyph_row *row, *first, *last;
24064
24065 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24066 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24067
24068 for (row = first; row <= last && row->enabled_p; ++row)
24069 {
24070 int start_hpos, end_hpos, start_x;
24071
24072 /* For all but the first row, the highlight starts at column 0. */
24073 if (row == first)
24074 {
24075 /* R2L rows have BEG and END in reversed order, but the
24076 screen drawing geometry is always left to right. So
24077 we need to mirror the beginning and end of the
24078 highlighted area in R2L rows. */
24079 if (!row->reversed_p)
24080 {
24081 start_hpos = hlinfo->mouse_face_beg_col;
24082 start_x = hlinfo->mouse_face_beg_x;
24083 }
24084 else if (row == last)
24085 {
24086 start_hpos = hlinfo->mouse_face_end_col;
24087 start_x = hlinfo->mouse_face_end_x;
24088 }
24089 else
24090 {
24091 start_hpos = 0;
24092 start_x = 0;
24093 }
24094 }
24095 else if (row->reversed_p && row == last)
24096 {
24097 start_hpos = hlinfo->mouse_face_end_col;
24098 start_x = hlinfo->mouse_face_end_x;
24099 }
24100 else
24101 {
24102 start_hpos = 0;
24103 start_x = 0;
24104 }
24105
24106 if (row == last)
24107 {
24108 if (!row->reversed_p)
24109 end_hpos = hlinfo->mouse_face_end_col;
24110 else if (row == first)
24111 end_hpos = hlinfo->mouse_face_beg_col;
24112 else
24113 {
24114 end_hpos = row->used[TEXT_AREA];
24115 if (draw == DRAW_NORMAL_TEXT)
24116 row->fill_line_p = 1; /* Clear to end of line */
24117 }
24118 }
24119 else if (row->reversed_p && row == first)
24120 end_hpos = hlinfo->mouse_face_beg_col;
24121 else
24122 {
24123 end_hpos = row->used[TEXT_AREA];
24124 if (draw == DRAW_NORMAL_TEXT)
24125 row->fill_line_p = 1; /* Clear to end of line */
24126 }
24127
24128 if (end_hpos > start_hpos)
24129 {
24130 draw_row_with_mouse_face (w, start_x, row,
24131 start_hpos, end_hpos, draw);
24132
24133 row->mouse_face_p
24134 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24135 }
24136 }
24137
24138 #ifdef HAVE_WINDOW_SYSTEM
24139 /* When we've written over the cursor, arrange for it to
24140 be displayed again. */
24141 if (FRAME_WINDOW_P (f)
24142 && phys_cursor_on_p && !w->phys_cursor_on_p)
24143 {
24144 BLOCK_INPUT;
24145 display_and_set_cursor (w, 1,
24146 w->phys_cursor.hpos, w->phys_cursor.vpos,
24147 w->phys_cursor.x, w->phys_cursor.y);
24148 UNBLOCK_INPUT;
24149 }
24150 #endif /* HAVE_WINDOW_SYSTEM */
24151 }
24152
24153 #ifdef HAVE_WINDOW_SYSTEM
24154 /* Change the mouse cursor. */
24155 if (FRAME_WINDOW_P (f))
24156 {
24157 if (draw == DRAW_NORMAL_TEXT
24158 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24159 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24160 else if (draw == DRAW_MOUSE_FACE)
24161 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24162 else
24163 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24164 }
24165 #endif /* HAVE_WINDOW_SYSTEM */
24166 }
24167
24168 /* EXPORT:
24169 Clear out the mouse-highlighted active region.
24170 Redraw it un-highlighted first. Value is non-zero if mouse
24171 face was actually drawn unhighlighted. */
24172
24173 int
24174 clear_mouse_face (Mouse_HLInfo *hlinfo)
24175 {
24176 int cleared = 0;
24177
24178 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24179 {
24180 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24181 cleared = 1;
24182 }
24183
24184 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24185 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24186 hlinfo->mouse_face_window = Qnil;
24187 hlinfo->mouse_face_overlay = Qnil;
24188 return cleared;
24189 }
24190
24191 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24192 within the mouse face on that window. */
24193 static int
24194 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24195 {
24196 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24197
24198 /* Quickly resolve the easy cases. */
24199 if (!(WINDOWP (hlinfo->mouse_face_window)
24200 && XWINDOW (hlinfo->mouse_face_window) == w))
24201 return 0;
24202 if (vpos < hlinfo->mouse_face_beg_row
24203 || vpos > hlinfo->mouse_face_end_row)
24204 return 0;
24205 if (vpos > hlinfo->mouse_face_beg_row
24206 && vpos < hlinfo->mouse_face_end_row)
24207 return 1;
24208
24209 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24210 {
24211 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24212 {
24213 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24214 return 1;
24215 }
24216 else if ((vpos == hlinfo->mouse_face_beg_row
24217 && hpos >= hlinfo->mouse_face_beg_col)
24218 || (vpos == hlinfo->mouse_face_end_row
24219 && hpos < hlinfo->mouse_face_end_col))
24220 return 1;
24221 }
24222 else
24223 {
24224 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24225 {
24226 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24227 return 1;
24228 }
24229 else if ((vpos == hlinfo->mouse_face_beg_row
24230 && hpos <= hlinfo->mouse_face_beg_col)
24231 || (vpos == hlinfo->mouse_face_end_row
24232 && hpos > hlinfo->mouse_face_end_col))
24233 return 1;
24234 }
24235 return 0;
24236 }
24237
24238
24239 /* EXPORT:
24240 Non-zero if physical cursor of window W is within mouse face. */
24241
24242 int
24243 cursor_in_mouse_face_p (struct window *w)
24244 {
24245 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24246 }
24247
24248
24249 \f
24250 /* Find the glyph rows START_ROW and END_ROW of window W that display
24251 characters between buffer positions START_CHARPOS and END_CHARPOS
24252 (excluding END_CHARPOS). This is similar to row_containing_pos,
24253 but is more accurate when bidi reordering makes buffer positions
24254 change non-linearly with glyph rows. */
24255 static void
24256 rows_from_pos_range (struct window *w,
24257 EMACS_INT start_charpos, EMACS_INT end_charpos,
24258 struct glyph_row **start, struct glyph_row **end)
24259 {
24260 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24261 int last_y = window_text_bottom_y (w);
24262 struct glyph_row *row;
24263
24264 *start = NULL;
24265 *end = NULL;
24266
24267 while (!first->enabled_p
24268 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24269 first++;
24270
24271 /* Find the START row. */
24272 for (row = first;
24273 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24274 row++)
24275 {
24276 /* A row can potentially be the START row if the range of the
24277 characters it displays intersects the range
24278 [START_CHARPOS..END_CHARPOS). */
24279 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24280 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24281 /* See the commentary in row_containing_pos, for the
24282 explanation of the complicated way to check whether
24283 some position is beyond the end of the characters
24284 displayed by a row. */
24285 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24286 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24287 && !row->ends_at_zv_p
24288 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24289 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24290 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24291 && !row->ends_at_zv_p
24292 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24293 {
24294 /* Found a candidate row. Now make sure at least one of the
24295 glyphs it displays has a charpos from the range
24296 [START_CHARPOS..END_CHARPOS).
24297
24298 This is not obvious because bidi reordering could make
24299 buffer positions of a row be 1,2,3,102,101,100, and if we
24300 want to highlight characters in [50..60), we don't want
24301 this row, even though [50..60) does intersect [1..103),
24302 the range of character positions given by the row's start
24303 and end positions. */
24304 struct glyph *g = row->glyphs[TEXT_AREA];
24305 struct glyph *e = g + row->used[TEXT_AREA];
24306
24307 while (g < e)
24308 {
24309 if (BUFFERP (g->object)
24310 && start_charpos <= g->charpos && g->charpos < end_charpos)
24311 *start = row;
24312 g++;
24313 }
24314 if (*start)
24315 break;
24316 }
24317 }
24318
24319 /* Find the END row. */
24320 if (!*start
24321 /* If the last row is partially visible, start looking for END
24322 from that row, instead of starting from FIRST. */
24323 && !(row->enabled_p
24324 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24325 row = first;
24326 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24327 {
24328 struct glyph_row *next = row + 1;
24329
24330 if (!next->enabled_p
24331 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24332 /* The first row >= START whose range of displayed characters
24333 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24334 is the row END + 1. */
24335 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24336 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24337 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24338 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24339 && !next->ends_at_zv_p
24340 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24341 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24342 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24343 && !next->ends_at_zv_p
24344 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24345 {
24346 *end = row;
24347 break;
24348 }
24349 else
24350 {
24351 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24352 but none of the characters it displays are in the range, it is
24353 also END + 1. */
24354 struct glyph *g = next->glyphs[TEXT_AREA];
24355 struct glyph *e = g + next->used[TEXT_AREA];
24356
24357 while (g < e)
24358 {
24359 if (BUFFERP (g->object)
24360 && start_charpos <= g->charpos && g->charpos < end_charpos)
24361 break;
24362 g++;
24363 }
24364 if (g == e)
24365 {
24366 *end = row;
24367 break;
24368 }
24369 }
24370 }
24371 }
24372
24373 /* This function sets the mouse_face_* elements of HLINFO, assuming
24374 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24375 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24376 for the overlay or run of text properties specifying the mouse
24377 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24378 before-string and after-string that must also be highlighted.
24379 DISPLAY_STRING, if non-nil, is a display string that may cover some
24380 or all of the highlighted text. */
24381
24382 static void
24383 mouse_face_from_buffer_pos (Lisp_Object window,
24384 Mouse_HLInfo *hlinfo,
24385 EMACS_INT mouse_charpos,
24386 EMACS_INT start_charpos,
24387 EMACS_INT end_charpos,
24388 Lisp_Object before_string,
24389 Lisp_Object after_string,
24390 Lisp_Object display_string)
24391 {
24392 struct window *w = XWINDOW (window);
24393 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24394 struct glyph_row *r1, *r2;
24395 struct glyph *glyph, *end;
24396 EMACS_INT ignore, pos;
24397 int x;
24398
24399 xassert (NILP (display_string) || STRINGP (display_string));
24400 xassert (NILP (before_string) || STRINGP (before_string));
24401 xassert (NILP (after_string) || STRINGP (after_string));
24402
24403 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
24404 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
24405 if (r1 == NULL)
24406 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24407 /* If the before-string or display-string contains newlines,
24408 rows_from_pos_range skips to its last row. Move back. */
24409 if (!NILP (before_string) || !NILP (display_string))
24410 {
24411 struct glyph_row *prev;
24412 while ((prev = r1 - 1, prev >= first)
24413 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
24414 && prev->used[TEXT_AREA] > 0)
24415 {
24416 struct glyph *beg = prev->glyphs[TEXT_AREA];
24417 glyph = beg + prev->used[TEXT_AREA];
24418 while (--glyph >= beg && INTEGERP (glyph->object));
24419 if (glyph < beg
24420 || !(EQ (glyph->object, before_string)
24421 || EQ (glyph->object, display_string)))
24422 break;
24423 r1 = prev;
24424 }
24425 }
24426 if (r2 == NULL)
24427 {
24428 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24429 hlinfo->mouse_face_past_end = 1;
24430 }
24431 else if (!NILP (after_string))
24432 {
24433 /* If the after-string has newlines, advance to its last row. */
24434 struct glyph_row *next;
24435 struct glyph_row *last
24436 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24437
24438 for (next = r2 + 1;
24439 next <= last
24440 && next->used[TEXT_AREA] > 0
24441 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
24442 ++next)
24443 r2 = next;
24444 }
24445 /* The rest of the display engine assumes that mouse_face_beg_row is
24446 either above below mouse_face_end_row or identical to it. But
24447 with bidi-reordered continued lines, the row for START_CHARPOS
24448 could be below the row for END_CHARPOS. If so, swap the rows and
24449 store them in correct order. */
24450 if (r1->y > r2->y)
24451 {
24452 struct glyph_row *tem = r2;
24453
24454 r2 = r1;
24455 r1 = tem;
24456 }
24457
24458 hlinfo->mouse_face_beg_y = r1->y;
24459 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
24460 hlinfo->mouse_face_end_y = r2->y;
24461 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
24462
24463 /* For a bidi-reordered row, the positions of BEFORE_STRING,
24464 AFTER_STRING, DISPLAY_STRING, START_CHARPOS, and END_CHARPOS
24465 could be anywhere in the row and in any order. The strategy
24466 below is to find the leftmost and the rightmost glyph that
24467 belongs to either of these 3 strings, or whose position is
24468 between START_CHARPOS and END_CHARPOS, and highlight all the
24469 glyphs between those two. This may cover more than just the text
24470 between START_CHARPOS and END_CHARPOS if the range of characters
24471 strides the bidi level boundary, e.g. if the beginning is in R2L
24472 text while the end is in L2R text or vice versa. */
24473 if (!r1->reversed_p)
24474 {
24475 /* This row is in a left to right paragraph. Scan it left to
24476 right. */
24477 glyph = r1->glyphs[TEXT_AREA];
24478 end = glyph + r1->used[TEXT_AREA];
24479 x = r1->x;
24480
24481 /* Skip truncation glyphs at the start of the glyph row. */
24482 if (r1->displays_text_p)
24483 for (; glyph < end
24484 && INTEGERP (glyph->object)
24485 && glyph->charpos < 0;
24486 ++glyph)
24487 x += glyph->pixel_width;
24488
24489 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24490 or DISPLAY_STRING, and the first glyph from buffer whose
24491 position is between START_CHARPOS and END_CHARPOS. */
24492 for (; glyph < end
24493 && !INTEGERP (glyph->object)
24494 && !EQ (glyph->object, display_string)
24495 && !(BUFFERP (glyph->object)
24496 && (glyph->charpos >= start_charpos
24497 && glyph->charpos < end_charpos));
24498 ++glyph)
24499 {
24500 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24501 are present at buffer positions between START_CHARPOS and
24502 END_CHARPOS, or if they come from an overlay. */
24503 if (EQ (glyph->object, before_string))
24504 {
24505 pos = string_buffer_position (w, before_string,
24506 start_charpos);
24507 /* If pos == 0, it means before_string came from an
24508 overlay, not from a buffer position. */
24509 if (!pos || (pos >= start_charpos && pos < end_charpos))
24510 break;
24511 }
24512 else if (EQ (glyph->object, after_string))
24513 {
24514 pos = string_buffer_position (w, after_string, end_charpos);
24515 if (!pos || (pos >= start_charpos && pos < end_charpos))
24516 break;
24517 }
24518 x += glyph->pixel_width;
24519 }
24520 hlinfo->mouse_face_beg_x = x;
24521 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24522 }
24523 else
24524 {
24525 /* This row is in a right to left paragraph. Scan it right to
24526 left. */
24527 struct glyph *g;
24528
24529 end = r1->glyphs[TEXT_AREA] - 1;
24530 glyph = end + r1->used[TEXT_AREA];
24531
24532 /* Skip truncation glyphs at the start of the glyph row. */
24533 if (r1->displays_text_p)
24534 for (; glyph > end
24535 && INTEGERP (glyph->object)
24536 && glyph->charpos < 0;
24537 --glyph)
24538 ;
24539
24540 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24541 or DISPLAY_STRING, and the first glyph from buffer whose
24542 position is between START_CHARPOS and END_CHARPOS. */
24543 for (; glyph > end
24544 && !INTEGERP (glyph->object)
24545 && !EQ (glyph->object, display_string)
24546 && !(BUFFERP (glyph->object)
24547 && (glyph->charpos >= start_charpos
24548 && glyph->charpos < end_charpos));
24549 --glyph)
24550 {
24551 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24552 are present at buffer positions between START_CHARPOS and
24553 END_CHARPOS, or if they come from an overlay. */
24554 if (EQ (glyph->object, before_string))
24555 {
24556 pos = string_buffer_position (w, before_string, start_charpos);
24557 /* If pos == 0, it means before_string came from an
24558 overlay, not from a buffer position. */
24559 if (!pos || (pos >= start_charpos && pos < end_charpos))
24560 break;
24561 }
24562 else if (EQ (glyph->object, after_string))
24563 {
24564 pos = string_buffer_position (w, after_string, end_charpos);
24565 if (!pos || (pos >= start_charpos && pos < end_charpos))
24566 break;
24567 }
24568 }
24569
24570 glyph++; /* first glyph to the right of the highlighted area */
24571 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
24572 x += g->pixel_width;
24573 hlinfo->mouse_face_beg_x = x;
24574 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24575 }
24576
24577 /* If the highlight ends in a different row, compute GLYPH and END
24578 for the end row. Otherwise, reuse the values computed above for
24579 the row where the highlight begins. */
24580 if (r2 != r1)
24581 {
24582 if (!r2->reversed_p)
24583 {
24584 glyph = r2->glyphs[TEXT_AREA];
24585 end = glyph + r2->used[TEXT_AREA];
24586 x = r2->x;
24587 }
24588 else
24589 {
24590 end = r2->glyphs[TEXT_AREA] - 1;
24591 glyph = end + r2->used[TEXT_AREA];
24592 }
24593 }
24594
24595 if (!r2->reversed_p)
24596 {
24597 /* Skip truncation and continuation glyphs near the end of the
24598 row, and also blanks and stretch glyphs inserted by
24599 extend_face_to_end_of_line. */
24600 while (end > glyph
24601 && INTEGERP ((end - 1)->object)
24602 && (end - 1)->charpos <= 0)
24603 --end;
24604 /* Scan the rest of the glyph row from the end, looking for the
24605 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24606 DISPLAY_STRING, or whose position is between START_CHARPOS
24607 and END_CHARPOS */
24608 for (--end;
24609 end > glyph
24610 && !INTEGERP (end->object)
24611 && !EQ (end->object, display_string)
24612 && !(BUFFERP (end->object)
24613 && (end->charpos >= start_charpos
24614 && end->charpos < end_charpos));
24615 --end)
24616 {
24617 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24618 are present at buffer positions between START_CHARPOS and
24619 END_CHARPOS, or if they come from an overlay. */
24620 if (EQ (end->object, before_string))
24621 {
24622 pos = string_buffer_position (w, before_string, start_charpos);
24623 if (!pos || (pos >= start_charpos && pos < end_charpos))
24624 break;
24625 }
24626 else if (EQ (end->object, after_string))
24627 {
24628 pos = string_buffer_position (w, after_string, end_charpos);
24629 if (!pos || (pos >= start_charpos && pos < end_charpos))
24630 break;
24631 }
24632 }
24633 /* Find the X coordinate of the last glyph to be highlighted. */
24634 for (; glyph <= end; ++glyph)
24635 x += glyph->pixel_width;
24636
24637 hlinfo->mouse_face_end_x = x;
24638 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
24639 }
24640 else
24641 {
24642 /* Skip truncation and continuation glyphs near the end of the
24643 row, and also blanks and stretch glyphs inserted by
24644 extend_face_to_end_of_line. */
24645 x = r2->x;
24646 end++;
24647 while (end < glyph
24648 && INTEGERP (end->object)
24649 && end->charpos <= 0)
24650 {
24651 x += end->pixel_width;
24652 ++end;
24653 }
24654 /* Scan the rest of the glyph row from the end, looking for the
24655 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24656 DISPLAY_STRING, or whose position is between START_CHARPOS
24657 and END_CHARPOS */
24658 for ( ;
24659 end < glyph
24660 && !INTEGERP (end->object)
24661 && !EQ (end->object, display_string)
24662 && !(BUFFERP (end->object)
24663 && (end->charpos >= start_charpos
24664 && end->charpos < end_charpos));
24665 ++end)
24666 {
24667 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24668 are present at buffer positions between START_CHARPOS and
24669 END_CHARPOS, or if they come from an overlay. */
24670 if (EQ (end->object, before_string))
24671 {
24672 pos = string_buffer_position (w, before_string, start_charpos);
24673 if (!pos || (pos >= start_charpos && pos < end_charpos))
24674 break;
24675 }
24676 else if (EQ (end->object, after_string))
24677 {
24678 pos = string_buffer_position (w, after_string, end_charpos);
24679 if (!pos || (pos >= start_charpos && pos < end_charpos))
24680 break;
24681 }
24682 x += end->pixel_width;
24683 }
24684 hlinfo->mouse_face_end_x = x;
24685 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
24686 }
24687
24688 hlinfo->mouse_face_window = window;
24689 hlinfo->mouse_face_face_id
24690 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
24691 mouse_charpos + 1,
24692 !hlinfo->mouse_face_hidden, -1);
24693 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
24694 }
24695
24696 /* The following function is not used anymore (replaced with
24697 mouse_face_from_string_pos), but I leave it here for the time
24698 being, in case someone would. */
24699
24700 #if 0 /* not used */
24701
24702 /* Find the position of the glyph for position POS in OBJECT in
24703 window W's current matrix, and return in *X, *Y the pixel
24704 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
24705
24706 RIGHT_P non-zero means return the position of the right edge of the
24707 glyph, RIGHT_P zero means return the left edge position.
24708
24709 If no glyph for POS exists in the matrix, return the position of
24710 the glyph with the next smaller position that is in the matrix, if
24711 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
24712 exists in the matrix, return the position of the glyph with the
24713 next larger position in OBJECT.
24714
24715 Value is non-zero if a glyph was found. */
24716
24717 static int
24718 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
24719 int *hpos, int *vpos, int *x, int *y, int right_p)
24720 {
24721 int yb = window_text_bottom_y (w);
24722 struct glyph_row *r;
24723 struct glyph *best_glyph = NULL;
24724 struct glyph_row *best_row = NULL;
24725 int best_x = 0;
24726
24727 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24728 r->enabled_p && r->y < yb;
24729 ++r)
24730 {
24731 struct glyph *g = r->glyphs[TEXT_AREA];
24732 struct glyph *e = g + r->used[TEXT_AREA];
24733 int gx;
24734
24735 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24736 if (EQ (g->object, object))
24737 {
24738 if (g->charpos == pos)
24739 {
24740 best_glyph = g;
24741 best_x = gx;
24742 best_row = r;
24743 goto found;
24744 }
24745 else if (best_glyph == NULL
24746 || ((eabs (g->charpos - pos)
24747 < eabs (best_glyph->charpos - pos))
24748 && (right_p
24749 ? g->charpos < pos
24750 : g->charpos > pos)))
24751 {
24752 best_glyph = g;
24753 best_x = gx;
24754 best_row = r;
24755 }
24756 }
24757 }
24758
24759 found:
24760
24761 if (best_glyph)
24762 {
24763 *x = best_x;
24764 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
24765
24766 if (right_p)
24767 {
24768 *x += best_glyph->pixel_width;
24769 ++*hpos;
24770 }
24771
24772 *y = best_row->y;
24773 *vpos = best_row - w->current_matrix->rows;
24774 }
24775
24776 return best_glyph != NULL;
24777 }
24778 #endif /* not used */
24779
24780 /* Find the positions of the first and the last glyphs in window W's
24781 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
24782 (assumed to be a string), and return in HLINFO's mouse_face_*
24783 members the pixel and column/row coordinates of those glyphs. */
24784
24785 static void
24786 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
24787 Lisp_Object object,
24788 EMACS_INT startpos, EMACS_INT endpos)
24789 {
24790 int yb = window_text_bottom_y (w);
24791 struct glyph_row *r;
24792 struct glyph *g, *e;
24793 int gx;
24794 int found = 0;
24795
24796 /* Find the glyph row with at least one position in the range
24797 [STARTPOS..ENDPOS], and the first glyph in that row whose
24798 position belongs to that range. */
24799 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24800 r->enabled_p && r->y < yb;
24801 ++r)
24802 {
24803 if (!r->reversed_p)
24804 {
24805 g = r->glyphs[TEXT_AREA];
24806 e = g + r->used[TEXT_AREA];
24807 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24808 if (EQ (g->object, object)
24809 && startpos <= g->charpos && g->charpos <= endpos)
24810 {
24811 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24812 hlinfo->mouse_face_beg_y = r->y;
24813 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24814 hlinfo->mouse_face_beg_x = gx;
24815 found = 1;
24816 break;
24817 }
24818 }
24819 else
24820 {
24821 struct glyph *g1;
24822
24823 e = r->glyphs[TEXT_AREA];
24824 g = e + r->used[TEXT_AREA];
24825 for ( ; g > e; --g)
24826 if (EQ ((g-1)->object, object)
24827 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
24828 {
24829 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24830 hlinfo->mouse_face_beg_y = r->y;
24831 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24832 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
24833 gx += g1->pixel_width;
24834 hlinfo->mouse_face_beg_x = gx;
24835 found = 1;
24836 break;
24837 }
24838 }
24839 if (found)
24840 break;
24841 }
24842
24843 if (!found)
24844 return;
24845
24846 /* Starting with the next row, look for the first row which does NOT
24847 include any glyphs whose positions are in the range. */
24848 for (++r; r->enabled_p && r->y < yb; ++r)
24849 {
24850 g = r->glyphs[TEXT_AREA];
24851 e = g + r->used[TEXT_AREA];
24852 found = 0;
24853 for ( ; g < e; ++g)
24854 if (EQ (g->object, object)
24855 && startpos <= g->charpos && g->charpos <= endpos)
24856 {
24857 found = 1;
24858 break;
24859 }
24860 if (!found)
24861 break;
24862 }
24863
24864 /* The highlighted region ends on the previous row. */
24865 r--;
24866
24867 /* Set the end row and its vertical pixel coordinate. */
24868 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
24869 hlinfo->mouse_face_end_y = r->y;
24870
24871 /* Compute and set the end column and the end column's horizontal
24872 pixel coordinate. */
24873 if (!r->reversed_p)
24874 {
24875 g = r->glyphs[TEXT_AREA];
24876 e = g + r->used[TEXT_AREA];
24877 for ( ; e > g; --e)
24878 if (EQ ((e-1)->object, object)
24879 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
24880 break;
24881 hlinfo->mouse_face_end_col = e - g;
24882
24883 for (gx = r->x; g < e; ++g)
24884 gx += g->pixel_width;
24885 hlinfo->mouse_face_end_x = gx;
24886 }
24887 else
24888 {
24889 e = r->glyphs[TEXT_AREA];
24890 g = e + r->used[TEXT_AREA];
24891 for (gx = r->x ; e < g; ++e)
24892 {
24893 if (EQ (e->object, object)
24894 && startpos <= e->charpos && e->charpos <= endpos)
24895 break;
24896 gx += e->pixel_width;
24897 }
24898 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
24899 hlinfo->mouse_face_end_x = gx;
24900 }
24901 }
24902
24903 #ifdef HAVE_WINDOW_SYSTEM
24904
24905 /* See if position X, Y is within a hot-spot of an image. */
24906
24907 static int
24908 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
24909 {
24910 if (!CONSP (hot_spot))
24911 return 0;
24912
24913 if (EQ (XCAR (hot_spot), Qrect))
24914 {
24915 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24916 Lisp_Object rect = XCDR (hot_spot);
24917 Lisp_Object tem;
24918 if (!CONSP (rect))
24919 return 0;
24920 if (!CONSP (XCAR (rect)))
24921 return 0;
24922 if (!CONSP (XCDR (rect)))
24923 return 0;
24924 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24925 return 0;
24926 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24927 return 0;
24928 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24929 return 0;
24930 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24931 return 0;
24932 return 1;
24933 }
24934 else if (EQ (XCAR (hot_spot), Qcircle))
24935 {
24936 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24937 Lisp_Object circ = XCDR (hot_spot);
24938 Lisp_Object lr, lx0, ly0;
24939 if (CONSP (circ)
24940 && CONSP (XCAR (circ))
24941 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24942 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24943 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24944 {
24945 double r = XFLOATINT (lr);
24946 double dx = XINT (lx0) - x;
24947 double dy = XINT (ly0) - y;
24948 return (dx * dx + dy * dy <= r * r);
24949 }
24950 }
24951 else if (EQ (XCAR (hot_spot), Qpoly))
24952 {
24953 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24954 if (VECTORP (XCDR (hot_spot)))
24955 {
24956 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24957 Lisp_Object *poly = v->contents;
24958 int n = v->size;
24959 int i;
24960 int inside = 0;
24961 Lisp_Object lx, ly;
24962 int x0, y0;
24963
24964 /* Need an even number of coordinates, and at least 3 edges. */
24965 if (n < 6 || n & 1)
24966 return 0;
24967
24968 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24969 If count is odd, we are inside polygon. Pixels on edges
24970 may or may not be included depending on actual geometry of the
24971 polygon. */
24972 if ((lx = poly[n-2], !INTEGERP (lx))
24973 || (ly = poly[n-1], !INTEGERP (lx)))
24974 return 0;
24975 x0 = XINT (lx), y0 = XINT (ly);
24976 for (i = 0; i < n; i += 2)
24977 {
24978 int x1 = x0, y1 = y0;
24979 if ((lx = poly[i], !INTEGERP (lx))
24980 || (ly = poly[i+1], !INTEGERP (ly)))
24981 return 0;
24982 x0 = XINT (lx), y0 = XINT (ly);
24983
24984 /* Does this segment cross the X line? */
24985 if (x0 >= x)
24986 {
24987 if (x1 >= x)
24988 continue;
24989 }
24990 else if (x1 < x)
24991 continue;
24992 if (y > y0 && y > y1)
24993 continue;
24994 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24995 inside = !inside;
24996 }
24997 return inside;
24998 }
24999 }
25000 return 0;
25001 }
25002
25003 Lisp_Object
25004 find_hot_spot (Lisp_Object map, int x, int y)
25005 {
25006 while (CONSP (map))
25007 {
25008 if (CONSP (XCAR (map))
25009 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25010 return XCAR (map);
25011 map = XCDR (map);
25012 }
25013
25014 return Qnil;
25015 }
25016
25017 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25018 3, 3, 0,
25019 doc: /* Lookup in image map MAP coordinates X and Y.
25020 An image map is an alist where each element has the format (AREA ID PLIST).
25021 An AREA is specified as either a rectangle, a circle, or a polygon:
25022 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25023 pixel coordinates of the upper left and bottom right corners.
25024 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25025 and the radius of the circle; r may be a float or integer.
25026 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25027 vector describes one corner in the polygon.
25028 Returns the alist element for the first matching AREA in MAP. */)
25029 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25030 {
25031 if (NILP (map))
25032 return Qnil;
25033
25034 CHECK_NUMBER (x);
25035 CHECK_NUMBER (y);
25036
25037 return find_hot_spot (map, XINT (x), XINT (y));
25038 }
25039
25040
25041 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25042 static void
25043 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25044 {
25045 /* Do not change cursor shape while dragging mouse. */
25046 if (!NILP (do_mouse_tracking))
25047 return;
25048
25049 if (!NILP (pointer))
25050 {
25051 if (EQ (pointer, Qarrow))
25052 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25053 else if (EQ (pointer, Qhand))
25054 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25055 else if (EQ (pointer, Qtext))
25056 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25057 else if (EQ (pointer, intern ("hdrag")))
25058 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25059 #ifdef HAVE_X_WINDOWS
25060 else if (EQ (pointer, intern ("vdrag")))
25061 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25062 #endif
25063 else if (EQ (pointer, intern ("hourglass")))
25064 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25065 else if (EQ (pointer, Qmodeline))
25066 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25067 else
25068 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25069 }
25070
25071 if (cursor != No_Cursor)
25072 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25073 }
25074
25075 #endif /* HAVE_WINDOW_SYSTEM */
25076
25077 /* Take proper action when mouse has moved to the mode or header line
25078 or marginal area AREA of window W, x-position X and y-position Y.
25079 X is relative to the start of the text display area of W, so the
25080 width of bitmap areas and scroll bars must be subtracted to get a
25081 position relative to the start of the mode line. */
25082
25083 static void
25084 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25085 enum window_part area)
25086 {
25087 struct window *w = XWINDOW (window);
25088 struct frame *f = XFRAME (w->frame);
25089 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25090 #ifdef HAVE_WINDOW_SYSTEM
25091 Display_Info *dpyinfo;
25092 #endif
25093 Cursor cursor = No_Cursor;
25094 Lisp_Object pointer = Qnil;
25095 int dx, dy, width, height;
25096 EMACS_INT charpos;
25097 Lisp_Object string, object = Qnil;
25098 Lisp_Object pos, help;
25099
25100 Lisp_Object mouse_face;
25101 int original_x_pixel = x;
25102 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25103 struct glyph_row *row;
25104
25105 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25106 {
25107 int x0;
25108 struct glyph *end;
25109
25110 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25111 returns them in row/column units! */
25112 string = mode_line_string (w, area, &x, &y, &charpos,
25113 &object, &dx, &dy, &width, &height);
25114
25115 row = (area == ON_MODE_LINE
25116 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25117 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25118
25119 /* Find the glyph under the mouse pointer. */
25120 if (row->mode_line_p && row->enabled_p)
25121 {
25122 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25123 end = glyph + row->used[TEXT_AREA];
25124
25125 for (x0 = original_x_pixel;
25126 glyph < end && x0 >= glyph->pixel_width;
25127 ++glyph)
25128 x0 -= glyph->pixel_width;
25129
25130 if (glyph >= end)
25131 glyph = NULL;
25132 }
25133 }
25134 else
25135 {
25136 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25137 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25138 returns them in row/column units! */
25139 string = marginal_area_string (w, area, &x, &y, &charpos,
25140 &object, &dx, &dy, &width, &height);
25141 }
25142
25143 help = Qnil;
25144
25145 #ifdef HAVE_WINDOW_SYSTEM
25146 if (IMAGEP (object))
25147 {
25148 Lisp_Object image_map, hotspot;
25149 if ((image_map = Fplist_get (XCDR (object), QCmap),
25150 !NILP (image_map))
25151 && (hotspot = find_hot_spot (image_map, dx, dy),
25152 CONSP (hotspot))
25153 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25154 {
25155 Lisp_Object area_id, plist;
25156
25157 area_id = XCAR (hotspot);
25158 /* Could check AREA_ID to see if we enter/leave this hot-spot.
25159 If so, we could look for mouse-enter, mouse-leave
25160 properties in PLIST (and do something...). */
25161 hotspot = XCDR (hotspot);
25162 if (CONSP (hotspot)
25163 && (plist = XCAR (hotspot), CONSP (plist)))
25164 {
25165 pointer = Fplist_get (plist, Qpointer);
25166 if (NILP (pointer))
25167 pointer = Qhand;
25168 help = Fplist_get (plist, Qhelp_echo);
25169 if (!NILP (help))
25170 {
25171 help_echo_string = help;
25172 /* Is this correct? ++kfs */
25173 XSETWINDOW (help_echo_window, w);
25174 help_echo_object = w->buffer;
25175 help_echo_pos = charpos;
25176 }
25177 }
25178 }
25179 if (NILP (pointer))
25180 pointer = Fplist_get (XCDR (object), QCpointer);
25181 }
25182 #endif /* HAVE_WINDOW_SYSTEM */
25183
25184 if (STRINGP (string))
25185 {
25186 pos = make_number (charpos);
25187 /* If we're on a string with `help-echo' text property, arrange
25188 for the help to be displayed. This is done by setting the
25189 global variable help_echo_string to the help string. */
25190 if (NILP (help))
25191 {
25192 help = Fget_text_property (pos, Qhelp_echo, string);
25193 if (!NILP (help))
25194 {
25195 help_echo_string = help;
25196 XSETWINDOW (help_echo_window, w);
25197 help_echo_object = string;
25198 help_echo_pos = charpos;
25199 }
25200 }
25201
25202 #ifdef HAVE_WINDOW_SYSTEM
25203 if (FRAME_WINDOW_P (f))
25204 {
25205 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25206 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25207 if (NILP (pointer))
25208 pointer = Fget_text_property (pos, Qpointer, string);
25209
25210 /* Change the mouse pointer according to what is under X/Y. */
25211 if (NILP (pointer)
25212 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25213 {
25214 Lisp_Object map;
25215 map = Fget_text_property (pos, Qlocal_map, string);
25216 if (!KEYMAPP (map))
25217 map = Fget_text_property (pos, Qkeymap, string);
25218 if (!KEYMAPP (map))
25219 cursor = dpyinfo->vertical_scroll_bar_cursor;
25220 }
25221 }
25222 #endif
25223
25224 /* Change the mouse face according to what is under X/Y. */
25225 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25226 if (!NILP (mouse_face)
25227 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25228 && glyph)
25229 {
25230 Lisp_Object b, e;
25231
25232 struct glyph * tmp_glyph;
25233
25234 int gpos;
25235 int gseq_length;
25236 int total_pixel_width;
25237 EMACS_INT begpos, endpos, ignore;
25238
25239 int vpos, hpos;
25240
25241 b = Fprevious_single_property_change (make_number (charpos + 1),
25242 Qmouse_face, string, Qnil);
25243 if (NILP (b))
25244 begpos = 0;
25245 else
25246 begpos = XINT (b);
25247
25248 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25249 if (NILP (e))
25250 endpos = SCHARS (string);
25251 else
25252 endpos = XINT (e);
25253
25254 /* Calculate the glyph position GPOS of GLYPH in the
25255 displayed string, relative to the beginning of the
25256 highlighted part of the string.
25257
25258 Note: GPOS is different from CHARPOS. CHARPOS is the
25259 position of GLYPH in the internal string object. A mode
25260 line string format has structures which are converted to
25261 a flattened string by the Emacs Lisp interpreter. The
25262 internal string is an element of those structures. The
25263 displayed string is the flattened string. */
25264 tmp_glyph = row_start_glyph;
25265 while (tmp_glyph < glyph
25266 && (!(EQ (tmp_glyph->object, glyph->object)
25267 && begpos <= tmp_glyph->charpos
25268 && tmp_glyph->charpos < endpos)))
25269 tmp_glyph++;
25270 gpos = glyph - tmp_glyph;
25271
25272 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25273 the highlighted part of the displayed string to which
25274 GLYPH belongs. Note: GSEQ_LENGTH is different from
25275 SCHARS (STRING), because the latter returns the length of
25276 the internal string. */
25277 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25278 tmp_glyph > glyph
25279 && (!(EQ (tmp_glyph->object, glyph->object)
25280 && begpos <= tmp_glyph->charpos
25281 && tmp_glyph->charpos < endpos));
25282 tmp_glyph--)
25283 ;
25284 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25285
25286 /* Calculate the total pixel width of all the glyphs between
25287 the beginning of the highlighted area and GLYPH. */
25288 total_pixel_width = 0;
25289 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25290 total_pixel_width += tmp_glyph->pixel_width;
25291
25292 /* Pre calculation of re-rendering position. Note: X is in
25293 column units here, after the call to mode_line_string or
25294 marginal_area_string. */
25295 hpos = x - gpos;
25296 vpos = (area == ON_MODE_LINE
25297 ? (w->current_matrix)->nrows - 1
25298 : 0);
25299
25300 /* If GLYPH's position is included in the region that is
25301 already drawn in mouse face, we have nothing to do. */
25302 if ( EQ (window, hlinfo->mouse_face_window)
25303 && (!row->reversed_p
25304 ? (hlinfo->mouse_face_beg_col <= hpos
25305 && hpos < hlinfo->mouse_face_end_col)
25306 /* In R2L rows we swap BEG and END, see below. */
25307 : (hlinfo->mouse_face_end_col <= hpos
25308 && hpos < hlinfo->mouse_face_beg_col))
25309 && hlinfo->mouse_face_beg_row == vpos )
25310 return;
25311
25312 if (clear_mouse_face (hlinfo))
25313 cursor = No_Cursor;
25314
25315 if (!row->reversed_p)
25316 {
25317 hlinfo->mouse_face_beg_col = hpos;
25318 hlinfo->mouse_face_beg_x = original_x_pixel
25319 - (total_pixel_width + dx);
25320 hlinfo->mouse_face_end_col = hpos + gseq_length;
25321 hlinfo->mouse_face_end_x = 0;
25322 }
25323 else
25324 {
25325 /* In R2L rows, show_mouse_face expects BEG and END
25326 coordinates to be swapped. */
25327 hlinfo->mouse_face_end_col = hpos;
25328 hlinfo->mouse_face_end_x = original_x_pixel
25329 - (total_pixel_width + dx);
25330 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25331 hlinfo->mouse_face_beg_x = 0;
25332 }
25333
25334 hlinfo->mouse_face_beg_row = vpos;
25335 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25336 hlinfo->mouse_face_beg_y = 0;
25337 hlinfo->mouse_face_end_y = 0;
25338 hlinfo->mouse_face_past_end = 0;
25339 hlinfo->mouse_face_window = window;
25340
25341 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25342 charpos,
25343 0, 0, 0,
25344 &ignore,
25345 glyph->face_id,
25346 1);
25347 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25348
25349 if (NILP (pointer))
25350 pointer = Qhand;
25351 }
25352 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25353 clear_mouse_face (hlinfo);
25354 }
25355 #ifdef HAVE_WINDOW_SYSTEM
25356 if (FRAME_WINDOW_P (f))
25357 define_frame_cursor1 (f, cursor, pointer);
25358 #endif
25359 }
25360
25361
25362 /* EXPORT:
25363 Take proper action when the mouse has moved to position X, Y on
25364 frame F as regards highlighting characters that have mouse-face
25365 properties. Also de-highlighting chars where the mouse was before.
25366 X and Y can be negative or out of range. */
25367
25368 void
25369 note_mouse_highlight (struct frame *f, int x, int y)
25370 {
25371 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25372 enum window_part part;
25373 Lisp_Object window;
25374 struct window *w;
25375 Cursor cursor = No_Cursor;
25376 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25377 struct buffer *b;
25378
25379 /* When a menu is active, don't highlight because this looks odd. */
25380 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25381 if (popup_activated ())
25382 return;
25383 #endif
25384
25385 if (NILP (Vmouse_highlight)
25386 || !f->glyphs_initialized_p
25387 || f->pointer_invisible)
25388 return;
25389
25390 hlinfo->mouse_face_mouse_x = x;
25391 hlinfo->mouse_face_mouse_y = y;
25392 hlinfo->mouse_face_mouse_frame = f;
25393
25394 if (hlinfo->mouse_face_defer)
25395 return;
25396
25397 if (gc_in_progress)
25398 {
25399 hlinfo->mouse_face_deferred_gc = 1;
25400 return;
25401 }
25402
25403 /* Which window is that in? */
25404 window = window_from_coordinates (f, x, y, &part, 1);
25405
25406 /* If we were displaying active text in another window, clear that.
25407 Also clear if we move out of text area in same window. */
25408 if (! EQ (window, hlinfo->mouse_face_window)
25409 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
25410 && !NILP (hlinfo->mouse_face_window)))
25411 clear_mouse_face (hlinfo);
25412
25413 /* Not on a window -> return. */
25414 if (!WINDOWP (window))
25415 return;
25416
25417 /* Reset help_echo_string. It will get recomputed below. */
25418 help_echo_string = Qnil;
25419
25420 /* Convert to window-relative pixel coordinates. */
25421 w = XWINDOW (window);
25422 frame_to_window_pixel_xy (w, &x, &y);
25423
25424 #ifdef HAVE_WINDOW_SYSTEM
25425 /* Handle tool-bar window differently since it doesn't display a
25426 buffer. */
25427 if (EQ (window, f->tool_bar_window))
25428 {
25429 note_tool_bar_highlight (f, x, y);
25430 return;
25431 }
25432 #endif
25433
25434 /* Mouse is on the mode, header line or margin? */
25435 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
25436 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
25437 {
25438 note_mode_line_or_margin_highlight (window, x, y, part);
25439 return;
25440 }
25441
25442 #ifdef HAVE_WINDOW_SYSTEM
25443 if (part == ON_VERTICAL_BORDER)
25444 {
25445 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25446 help_echo_string = build_string ("drag-mouse-1: resize");
25447 }
25448 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
25449 || part == ON_SCROLL_BAR)
25450 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25451 else
25452 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25453 #endif
25454
25455 /* Are we in a window whose display is up to date?
25456 And verify the buffer's text has not changed. */
25457 b = XBUFFER (w->buffer);
25458 if (part == ON_TEXT
25459 && EQ (w->window_end_valid, w->buffer)
25460 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
25461 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
25462 {
25463 int hpos, vpos, i, dx, dy, area;
25464 EMACS_INT pos;
25465 struct glyph *glyph;
25466 Lisp_Object object;
25467 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
25468 Lisp_Object *overlay_vec = NULL;
25469 int noverlays;
25470 struct buffer *obuf;
25471 EMACS_INT obegv, ozv;
25472 int same_region;
25473
25474 /* Find the glyph under X/Y. */
25475 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
25476
25477 #ifdef HAVE_WINDOW_SYSTEM
25478 /* Look for :pointer property on image. */
25479 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25480 {
25481 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25482 if (img != NULL && IMAGEP (img->spec))
25483 {
25484 Lisp_Object image_map, hotspot;
25485 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
25486 !NILP (image_map))
25487 && (hotspot = find_hot_spot (image_map,
25488 glyph->slice.img.x + dx,
25489 glyph->slice.img.y + dy),
25490 CONSP (hotspot))
25491 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25492 {
25493 Lisp_Object area_id, plist;
25494
25495 area_id = XCAR (hotspot);
25496 /* Could check AREA_ID to see if we enter/leave this hot-spot.
25497 If so, we could look for mouse-enter, mouse-leave
25498 properties in PLIST (and do something...). */
25499 hotspot = XCDR (hotspot);
25500 if (CONSP (hotspot)
25501 && (plist = XCAR (hotspot), CONSP (plist)))
25502 {
25503 pointer = Fplist_get (plist, Qpointer);
25504 if (NILP (pointer))
25505 pointer = Qhand;
25506 help_echo_string = Fplist_get (plist, Qhelp_echo);
25507 if (!NILP (help_echo_string))
25508 {
25509 help_echo_window = window;
25510 help_echo_object = glyph->object;
25511 help_echo_pos = glyph->charpos;
25512 }
25513 }
25514 }
25515 if (NILP (pointer))
25516 pointer = Fplist_get (XCDR (img->spec), QCpointer);
25517 }
25518 }
25519 #endif /* HAVE_WINDOW_SYSTEM */
25520
25521 /* Clear mouse face if X/Y not over text. */
25522 if (glyph == NULL
25523 || area != TEXT_AREA
25524 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
25525 /* Glyph's OBJECT is an integer for glyphs inserted by the
25526 display engine for its internal purposes, like truncation
25527 and continuation glyphs and blanks beyond the end of
25528 line's text on text terminals. If we are over such a
25529 glyph, we are not over any text. */
25530 || INTEGERP (glyph->object)
25531 /* R2L rows have a stretch glyph at their front, which
25532 stands for no text, whereas L2R rows have no glyphs at
25533 all beyond the end of text. Treat such stretch glyphs
25534 like we do with NULL glyphs in L2R rows. */
25535 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
25536 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
25537 && glyph->type == STRETCH_GLYPH
25538 && glyph->avoid_cursor_p))
25539 {
25540 if (clear_mouse_face (hlinfo))
25541 cursor = No_Cursor;
25542 #ifdef HAVE_WINDOW_SYSTEM
25543 if (FRAME_WINDOW_P (f) && NILP (pointer))
25544 {
25545 if (area != TEXT_AREA)
25546 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25547 else
25548 pointer = Vvoid_text_area_pointer;
25549 }
25550 #endif
25551 goto set_cursor;
25552 }
25553
25554 pos = glyph->charpos;
25555 object = glyph->object;
25556 if (!STRINGP (object) && !BUFFERP (object))
25557 goto set_cursor;
25558
25559 /* If we get an out-of-range value, return now; avoid an error. */
25560 if (BUFFERP (object) && pos > BUF_Z (b))
25561 goto set_cursor;
25562
25563 /* Make the window's buffer temporarily current for
25564 overlays_at and compute_char_face. */
25565 obuf = current_buffer;
25566 current_buffer = b;
25567 obegv = BEGV;
25568 ozv = ZV;
25569 BEGV = BEG;
25570 ZV = Z;
25571
25572 /* Is this char mouse-active or does it have help-echo? */
25573 position = make_number (pos);
25574
25575 if (BUFFERP (object))
25576 {
25577 /* Put all the overlays we want in a vector in overlay_vec. */
25578 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
25579 /* Sort overlays into increasing priority order. */
25580 noverlays = sort_overlays (overlay_vec, noverlays, w);
25581 }
25582 else
25583 noverlays = 0;
25584
25585 same_region = coords_in_mouse_face_p (w, hpos, vpos);
25586
25587 if (same_region)
25588 cursor = No_Cursor;
25589
25590 /* Check mouse-face highlighting. */
25591 if (! same_region
25592 /* If there exists an overlay with mouse-face overlapping
25593 the one we are currently highlighting, we have to
25594 check if we enter the overlapping overlay, and then
25595 highlight only that. */
25596 || (OVERLAYP (hlinfo->mouse_face_overlay)
25597 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
25598 {
25599 /* Find the highest priority overlay with a mouse-face. */
25600 overlay = Qnil;
25601 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
25602 {
25603 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
25604 if (!NILP (mouse_face))
25605 overlay = overlay_vec[i];
25606 }
25607
25608 /* If we're highlighting the same overlay as before, there's
25609 no need to do that again. */
25610 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
25611 goto check_help_echo;
25612 hlinfo->mouse_face_overlay = overlay;
25613
25614 /* Clear the display of the old active region, if any. */
25615 if (clear_mouse_face (hlinfo))
25616 cursor = No_Cursor;
25617
25618 /* If no overlay applies, get a text property. */
25619 if (NILP (overlay))
25620 mouse_face = Fget_text_property (position, Qmouse_face, object);
25621
25622 /* Next, compute the bounds of the mouse highlighting and
25623 display it. */
25624 if (!NILP (mouse_face) && STRINGP (object))
25625 {
25626 /* The mouse-highlighting comes from a display string
25627 with a mouse-face. */
25628 Lisp_Object b, e;
25629 EMACS_INT ignore;
25630
25631 b = Fprevious_single_property_change
25632 (make_number (pos + 1), Qmouse_face, object, Qnil);
25633 e = Fnext_single_property_change
25634 (position, Qmouse_face, object, Qnil);
25635 if (NILP (b))
25636 b = make_number (0);
25637 if (NILP (e))
25638 e = make_number (SCHARS (object) - 1);
25639 mouse_face_from_string_pos (w, hlinfo, object,
25640 XINT (b), XINT (e));
25641 hlinfo->mouse_face_past_end = 0;
25642 hlinfo->mouse_face_window = window;
25643 hlinfo->mouse_face_face_id
25644 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
25645 glyph->face_id, 1);
25646 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25647 cursor = No_Cursor;
25648 }
25649 else
25650 {
25651 /* The mouse-highlighting, if any, comes from an overlay
25652 or text property in the buffer. */
25653 Lisp_Object buffer, display_string;
25654
25655 if (STRINGP (object))
25656 {
25657 /* If we are on a display string with no mouse-face,
25658 check if the text under it has one. */
25659 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
25660 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25661 pos = string_buffer_position (w, object, start);
25662 if (pos > 0)
25663 {
25664 mouse_face = get_char_property_and_overlay
25665 (make_number (pos), Qmouse_face, w->buffer, &overlay);
25666 buffer = w->buffer;
25667 display_string = object;
25668 }
25669 }
25670 else
25671 {
25672 buffer = object;
25673 display_string = Qnil;
25674 }
25675
25676 if (!NILP (mouse_face))
25677 {
25678 Lisp_Object before, after;
25679 Lisp_Object before_string, after_string;
25680 /* To correctly find the limits of mouse highlight
25681 in a bidi-reordered buffer, we must not use the
25682 optimization of limiting the search in
25683 previous-single-property-change and
25684 next-single-property-change, because
25685 rows_from_pos_range needs the real start and end
25686 positions to DTRT in this case. That's because
25687 the first row visible in a window does not
25688 necessarily display the character whose position
25689 is the smallest. */
25690 Lisp_Object lim1 =
25691 NILP (XBUFFER (buffer)->bidi_display_reordering)
25692 ? Fmarker_position (w->start)
25693 : Qnil;
25694 Lisp_Object lim2 =
25695 NILP (XBUFFER (buffer)->bidi_display_reordering)
25696 ? make_number (BUF_Z (XBUFFER (buffer))
25697 - XFASTINT (w->window_end_pos))
25698 : Qnil;
25699
25700 if (NILP (overlay))
25701 {
25702 /* Handle the text property case. */
25703 before = Fprevious_single_property_change
25704 (make_number (pos + 1), Qmouse_face, buffer, lim1);
25705 after = Fnext_single_property_change
25706 (make_number (pos), Qmouse_face, buffer, lim2);
25707 before_string = after_string = Qnil;
25708 }
25709 else
25710 {
25711 /* Handle the overlay case. */
25712 before = Foverlay_start (overlay);
25713 after = Foverlay_end (overlay);
25714 before_string = Foverlay_get (overlay, Qbefore_string);
25715 after_string = Foverlay_get (overlay, Qafter_string);
25716
25717 if (!STRINGP (before_string)) before_string = Qnil;
25718 if (!STRINGP (after_string)) after_string = Qnil;
25719 }
25720
25721 mouse_face_from_buffer_pos (window, hlinfo, pos,
25722 XFASTINT (before),
25723 XFASTINT (after),
25724 before_string, after_string,
25725 display_string);
25726 cursor = No_Cursor;
25727 }
25728 }
25729 }
25730
25731 check_help_echo:
25732
25733 /* Look for a `help-echo' property. */
25734 if (NILP (help_echo_string)) {
25735 Lisp_Object help, overlay;
25736
25737 /* Check overlays first. */
25738 help = overlay = Qnil;
25739 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
25740 {
25741 overlay = overlay_vec[i];
25742 help = Foverlay_get (overlay, Qhelp_echo);
25743 }
25744
25745 if (!NILP (help))
25746 {
25747 help_echo_string = help;
25748 help_echo_window = window;
25749 help_echo_object = overlay;
25750 help_echo_pos = pos;
25751 }
25752 else
25753 {
25754 Lisp_Object object = glyph->object;
25755 EMACS_INT charpos = glyph->charpos;
25756
25757 /* Try text properties. */
25758 if (STRINGP (object)
25759 && charpos >= 0
25760 && charpos < SCHARS (object))
25761 {
25762 help = Fget_text_property (make_number (charpos),
25763 Qhelp_echo, object);
25764 if (NILP (help))
25765 {
25766 /* If the string itself doesn't specify a help-echo,
25767 see if the buffer text ``under'' it does. */
25768 struct glyph_row *r
25769 = MATRIX_ROW (w->current_matrix, vpos);
25770 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25771 EMACS_INT pos = string_buffer_position (w, object, start);
25772 if (pos > 0)
25773 {
25774 help = Fget_char_property (make_number (pos),
25775 Qhelp_echo, w->buffer);
25776 if (!NILP (help))
25777 {
25778 charpos = pos;
25779 object = w->buffer;
25780 }
25781 }
25782 }
25783 }
25784 else if (BUFFERP (object)
25785 && charpos >= BEGV
25786 && charpos < ZV)
25787 help = Fget_text_property (make_number (charpos), Qhelp_echo,
25788 object);
25789
25790 if (!NILP (help))
25791 {
25792 help_echo_string = help;
25793 help_echo_window = window;
25794 help_echo_object = object;
25795 help_echo_pos = charpos;
25796 }
25797 }
25798 }
25799
25800 #ifdef HAVE_WINDOW_SYSTEM
25801 /* Look for a `pointer' property. */
25802 if (FRAME_WINDOW_P (f) && NILP (pointer))
25803 {
25804 /* Check overlays first. */
25805 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
25806 pointer = Foverlay_get (overlay_vec[i], Qpointer);
25807
25808 if (NILP (pointer))
25809 {
25810 Lisp_Object object = glyph->object;
25811 EMACS_INT charpos = glyph->charpos;
25812
25813 /* Try text properties. */
25814 if (STRINGP (object)
25815 && charpos >= 0
25816 && charpos < SCHARS (object))
25817 {
25818 pointer = Fget_text_property (make_number (charpos),
25819 Qpointer, object);
25820 if (NILP (pointer))
25821 {
25822 /* If the string itself doesn't specify a pointer,
25823 see if the buffer text ``under'' it does. */
25824 struct glyph_row *r
25825 = MATRIX_ROW (w->current_matrix, vpos);
25826 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25827 EMACS_INT pos = string_buffer_position (w, object,
25828 start);
25829 if (pos > 0)
25830 pointer = Fget_char_property (make_number (pos),
25831 Qpointer, w->buffer);
25832 }
25833 }
25834 else if (BUFFERP (object)
25835 && charpos >= BEGV
25836 && charpos < ZV)
25837 pointer = Fget_text_property (make_number (charpos),
25838 Qpointer, object);
25839 }
25840 }
25841 #endif /* HAVE_WINDOW_SYSTEM */
25842
25843 BEGV = obegv;
25844 ZV = ozv;
25845 current_buffer = obuf;
25846 }
25847
25848 set_cursor:
25849
25850 #ifdef HAVE_WINDOW_SYSTEM
25851 if (FRAME_WINDOW_P (f))
25852 define_frame_cursor1 (f, cursor, pointer);
25853 #else
25854 /* This is here to prevent a compiler error, about "label at end of
25855 compound statement". */
25856 return;
25857 #endif
25858 }
25859
25860
25861 /* EXPORT for RIF:
25862 Clear any mouse-face on window W. This function is part of the
25863 redisplay interface, and is called from try_window_id and similar
25864 functions to ensure the mouse-highlight is off. */
25865
25866 void
25867 x_clear_window_mouse_face (struct window *w)
25868 {
25869 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25870 Lisp_Object window;
25871
25872 BLOCK_INPUT;
25873 XSETWINDOW (window, w);
25874 if (EQ (window, hlinfo->mouse_face_window))
25875 clear_mouse_face (hlinfo);
25876 UNBLOCK_INPUT;
25877 }
25878
25879
25880 /* EXPORT:
25881 Just discard the mouse face information for frame F, if any.
25882 This is used when the size of F is changed. */
25883
25884 void
25885 cancel_mouse_face (struct frame *f)
25886 {
25887 Lisp_Object window;
25888 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25889
25890 window = hlinfo->mouse_face_window;
25891 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
25892 {
25893 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25894 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25895 hlinfo->mouse_face_window = Qnil;
25896 }
25897 }
25898
25899
25900 \f
25901 /***********************************************************************
25902 Exposure Events
25903 ***********************************************************************/
25904
25905 #ifdef HAVE_WINDOW_SYSTEM
25906
25907 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
25908 which intersects rectangle R. R is in window-relative coordinates. */
25909
25910 static void
25911 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
25912 enum glyph_row_area area)
25913 {
25914 struct glyph *first = row->glyphs[area];
25915 struct glyph *end = row->glyphs[area] + row->used[area];
25916 struct glyph *last;
25917 int first_x, start_x, x;
25918
25919 if (area == TEXT_AREA && row->fill_line_p)
25920 /* If row extends face to end of line write the whole line. */
25921 draw_glyphs (w, 0, row, area,
25922 0, row->used[area],
25923 DRAW_NORMAL_TEXT, 0);
25924 else
25925 {
25926 /* Set START_X to the window-relative start position for drawing glyphs of
25927 AREA. The first glyph of the text area can be partially visible.
25928 The first glyphs of other areas cannot. */
25929 start_x = window_box_left_offset (w, area);
25930 x = start_x;
25931 if (area == TEXT_AREA)
25932 x += row->x;
25933
25934 /* Find the first glyph that must be redrawn. */
25935 while (first < end
25936 && x + first->pixel_width < r->x)
25937 {
25938 x += first->pixel_width;
25939 ++first;
25940 }
25941
25942 /* Find the last one. */
25943 last = first;
25944 first_x = x;
25945 while (last < end
25946 && x < r->x + r->width)
25947 {
25948 x += last->pixel_width;
25949 ++last;
25950 }
25951
25952 /* Repaint. */
25953 if (last > first)
25954 draw_glyphs (w, first_x - start_x, row, area,
25955 first - row->glyphs[area], last - row->glyphs[area],
25956 DRAW_NORMAL_TEXT, 0);
25957 }
25958 }
25959
25960
25961 /* Redraw the parts of the glyph row ROW on window W intersecting
25962 rectangle R. R is in window-relative coordinates. Value is
25963 non-zero if mouse-face was overwritten. */
25964
25965 static int
25966 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
25967 {
25968 xassert (row->enabled_p);
25969
25970 if (row->mode_line_p || w->pseudo_window_p)
25971 draw_glyphs (w, 0, row, TEXT_AREA,
25972 0, row->used[TEXT_AREA],
25973 DRAW_NORMAL_TEXT, 0);
25974 else
25975 {
25976 if (row->used[LEFT_MARGIN_AREA])
25977 expose_area (w, row, r, LEFT_MARGIN_AREA);
25978 if (row->used[TEXT_AREA])
25979 expose_area (w, row, r, TEXT_AREA);
25980 if (row->used[RIGHT_MARGIN_AREA])
25981 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25982 draw_row_fringe_bitmaps (w, row);
25983 }
25984
25985 return row->mouse_face_p;
25986 }
25987
25988
25989 /* Redraw those parts of glyphs rows during expose event handling that
25990 overlap other rows. Redrawing of an exposed line writes over parts
25991 of lines overlapping that exposed line; this function fixes that.
25992
25993 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25994 row in W's current matrix that is exposed and overlaps other rows.
25995 LAST_OVERLAPPING_ROW is the last such row. */
25996
25997 static void
25998 expose_overlaps (struct window *w,
25999 struct glyph_row *first_overlapping_row,
26000 struct glyph_row *last_overlapping_row,
26001 XRectangle *r)
26002 {
26003 struct glyph_row *row;
26004
26005 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26006 if (row->overlapping_p)
26007 {
26008 xassert (row->enabled_p && !row->mode_line_p);
26009
26010 row->clip = r;
26011 if (row->used[LEFT_MARGIN_AREA])
26012 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26013
26014 if (row->used[TEXT_AREA])
26015 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26016
26017 if (row->used[RIGHT_MARGIN_AREA])
26018 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26019 row->clip = NULL;
26020 }
26021 }
26022
26023
26024 /* Return non-zero if W's cursor intersects rectangle R. */
26025
26026 static int
26027 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26028 {
26029 XRectangle cr, result;
26030 struct glyph *cursor_glyph;
26031 struct glyph_row *row;
26032
26033 if (w->phys_cursor.vpos >= 0
26034 && w->phys_cursor.vpos < w->current_matrix->nrows
26035 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26036 row->enabled_p)
26037 && row->cursor_in_fringe_p)
26038 {
26039 /* Cursor is in the fringe. */
26040 cr.x = window_box_right_offset (w,
26041 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26042 ? RIGHT_MARGIN_AREA
26043 : TEXT_AREA));
26044 cr.y = row->y;
26045 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26046 cr.height = row->height;
26047 return x_intersect_rectangles (&cr, r, &result);
26048 }
26049
26050 cursor_glyph = get_phys_cursor_glyph (w);
26051 if (cursor_glyph)
26052 {
26053 /* r is relative to W's box, but w->phys_cursor.x is relative
26054 to left edge of W's TEXT area. Adjust it. */
26055 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26056 cr.y = w->phys_cursor.y;
26057 cr.width = cursor_glyph->pixel_width;
26058 cr.height = w->phys_cursor_height;
26059 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26060 I assume the effect is the same -- and this is portable. */
26061 return x_intersect_rectangles (&cr, r, &result);
26062 }
26063 /* If we don't understand the format, pretend we're not in the hot-spot. */
26064 return 0;
26065 }
26066
26067
26068 /* EXPORT:
26069 Draw a vertical window border to the right of window W if W doesn't
26070 have vertical scroll bars. */
26071
26072 void
26073 x_draw_vertical_border (struct window *w)
26074 {
26075 struct frame *f = XFRAME (WINDOW_FRAME (w));
26076
26077 /* We could do better, if we knew what type of scroll-bar the adjacent
26078 windows (on either side) have... But we don't :-(
26079 However, I think this works ok. ++KFS 2003-04-25 */
26080
26081 /* Redraw borders between horizontally adjacent windows. Don't
26082 do it for frames with vertical scroll bars because either the
26083 right scroll bar of a window, or the left scroll bar of its
26084 neighbor will suffice as a border. */
26085 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26086 return;
26087
26088 if (!WINDOW_RIGHTMOST_P (w)
26089 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26090 {
26091 int x0, x1, y0, y1;
26092
26093 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26094 y1 -= 1;
26095
26096 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26097 x1 -= 1;
26098
26099 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26100 }
26101 else if (!WINDOW_LEFTMOST_P (w)
26102 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26103 {
26104 int x0, x1, y0, y1;
26105
26106 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26107 y1 -= 1;
26108
26109 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26110 x0 -= 1;
26111
26112 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26113 }
26114 }
26115
26116
26117 /* Redraw the part of window W intersection rectangle FR. Pixel
26118 coordinates in FR are frame-relative. Call this function with
26119 input blocked. Value is non-zero if the exposure overwrites
26120 mouse-face. */
26121
26122 static int
26123 expose_window (struct window *w, XRectangle *fr)
26124 {
26125 struct frame *f = XFRAME (w->frame);
26126 XRectangle wr, r;
26127 int mouse_face_overwritten_p = 0;
26128
26129 /* If window is not yet fully initialized, do nothing. This can
26130 happen when toolkit scroll bars are used and a window is split.
26131 Reconfiguring the scroll bar will generate an expose for a newly
26132 created window. */
26133 if (w->current_matrix == NULL)
26134 return 0;
26135
26136 /* When we're currently updating the window, display and current
26137 matrix usually don't agree. Arrange for a thorough display
26138 later. */
26139 if (w == updated_window)
26140 {
26141 SET_FRAME_GARBAGED (f);
26142 return 0;
26143 }
26144
26145 /* Frame-relative pixel rectangle of W. */
26146 wr.x = WINDOW_LEFT_EDGE_X (w);
26147 wr.y = WINDOW_TOP_EDGE_Y (w);
26148 wr.width = WINDOW_TOTAL_WIDTH (w);
26149 wr.height = WINDOW_TOTAL_HEIGHT (w);
26150
26151 if (x_intersect_rectangles (fr, &wr, &r))
26152 {
26153 int yb = window_text_bottom_y (w);
26154 struct glyph_row *row;
26155 int cursor_cleared_p;
26156 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26157
26158 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26159 r.x, r.y, r.width, r.height));
26160
26161 /* Convert to window coordinates. */
26162 r.x -= WINDOW_LEFT_EDGE_X (w);
26163 r.y -= WINDOW_TOP_EDGE_Y (w);
26164
26165 /* Turn off the cursor. */
26166 if (!w->pseudo_window_p
26167 && phys_cursor_in_rect_p (w, &r))
26168 {
26169 x_clear_cursor (w);
26170 cursor_cleared_p = 1;
26171 }
26172 else
26173 cursor_cleared_p = 0;
26174
26175 /* Update lines intersecting rectangle R. */
26176 first_overlapping_row = last_overlapping_row = NULL;
26177 for (row = w->current_matrix->rows;
26178 row->enabled_p;
26179 ++row)
26180 {
26181 int y0 = row->y;
26182 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26183
26184 if ((y0 >= r.y && y0 < r.y + r.height)
26185 || (y1 > r.y && y1 < r.y + r.height)
26186 || (r.y >= y0 && r.y < y1)
26187 || (r.y + r.height > y0 && r.y + r.height < y1))
26188 {
26189 /* A header line may be overlapping, but there is no need
26190 to fix overlapping areas for them. KFS 2005-02-12 */
26191 if (row->overlapping_p && !row->mode_line_p)
26192 {
26193 if (first_overlapping_row == NULL)
26194 first_overlapping_row = row;
26195 last_overlapping_row = row;
26196 }
26197
26198 row->clip = fr;
26199 if (expose_line (w, row, &r))
26200 mouse_face_overwritten_p = 1;
26201 row->clip = NULL;
26202 }
26203 else if (row->overlapping_p)
26204 {
26205 /* We must redraw a row overlapping the exposed area. */
26206 if (y0 < r.y
26207 ? y0 + row->phys_height > r.y
26208 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26209 {
26210 if (first_overlapping_row == NULL)
26211 first_overlapping_row = row;
26212 last_overlapping_row = row;
26213 }
26214 }
26215
26216 if (y1 >= yb)
26217 break;
26218 }
26219
26220 /* Display the mode line if there is one. */
26221 if (WINDOW_WANTS_MODELINE_P (w)
26222 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26223 row->enabled_p)
26224 && row->y < r.y + r.height)
26225 {
26226 if (expose_line (w, row, &r))
26227 mouse_face_overwritten_p = 1;
26228 }
26229
26230 if (!w->pseudo_window_p)
26231 {
26232 /* Fix the display of overlapping rows. */
26233 if (first_overlapping_row)
26234 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26235 fr);
26236
26237 /* Draw border between windows. */
26238 x_draw_vertical_border (w);
26239
26240 /* Turn the cursor on again. */
26241 if (cursor_cleared_p)
26242 update_window_cursor (w, 1);
26243 }
26244 }
26245
26246 return mouse_face_overwritten_p;
26247 }
26248
26249
26250
26251 /* Redraw (parts) of all windows in the window tree rooted at W that
26252 intersect R. R contains frame pixel coordinates. Value is
26253 non-zero if the exposure overwrites mouse-face. */
26254
26255 static int
26256 expose_window_tree (struct window *w, XRectangle *r)
26257 {
26258 struct frame *f = XFRAME (w->frame);
26259 int mouse_face_overwritten_p = 0;
26260
26261 while (w && !FRAME_GARBAGED_P (f))
26262 {
26263 if (!NILP (w->hchild))
26264 mouse_face_overwritten_p
26265 |= expose_window_tree (XWINDOW (w->hchild), r);
26266 else if (!NILP (w->vchild))
26267 mouse_face_overwritten_p
26268 |= expose_window_tree (XWINDOW (w->vchild), r);
26269 else
26270 mouse_face_overwritten_p |= expose_window (w, r);
26271
26272 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26273 }
26274
26275 return mouse_face_overwritten_p;
26276 }
26277
26278
26279 /* EXPORT:
26280 Redisplay an exposed area of frame F. X and Y are the upper-left
26281 corner of the exposed rectangle. W and H are width and height of
26282 the exposed area. All are pixel values. W or H zero means redraw
26283 the entire frame. */
26284
26285 void
26286 expose_frame (struct frame *f, int x, int y, int w, int h)
26287 {
26288 XRectangle r;
26289 int mouse_face_overwritten_p = 0;
26290
26291 TRACE ((stderr, "expose_frame "));
26292
26293 /* No need to redraw if frame will be redrawn soon. */
26294 if (FRAME_GARBAGED_P (f))
26295 {
26296 TRACE ((stderr, " garbaged\n"));
26297 return;
26298 }
26299
26300 /* If basic faces haven't been realized yet, there is no point in
26301 trying to redraw anything. This can happen when we get an expose
26302 event while Emacs is starting, e.g. by moving another window. */
26303 if (FRAME_FACE_CACHE (f) == NULL
26304 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26305 {
26306 TRACE ((stderr, " no faces\n"));
26307 return;
26308 }
26309
26310 if (w == 0 || h == 0)
26311 {
26312 r.x = r.y = 0;
26313 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26314 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26315 }
26316 else
26317 {
26318 r.x = x;
26319 r.y = y;
26320 r.width = w;
26321 r.height = h;
26322 }
26323
26324 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26325 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26326
26327 if (WINDOWP (f->tool_bar_window))
26328 mouse_face_overwritten_p
26329 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26330
26331 #ifdef HAVE_X_WINDOWS
26332 #ifndef MSDOS
26333 #ifndef USE_X_TOOLKIT
26334 if (WINDOWP (f->menu_bar_window))
26335 mouse_face_overwritten_p
26336 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26337 #endif /* not USE_X_TOOLKIT */
26338 #endif
26339 #endif
26340
26341 /* Some window managers support a focus-follows-mouse style with
26342 delayed raising of frames. Imagine a partially obscured frame,
26343 and moving the mouse into partially obscured mouse-face on that
26344 frame. The visible part of the mouse-face will be highlighted,
26345 then the WM raises the obscured frame. With at least one WM, KDE
26346 2.1, Emacs is not getting any event for the raising of the frame
26347 (even tried with SubstructureRedirectMask), only Expose events.
26348 These expose events will draw text normally, i.e. not
26349 highlighted. Which means we must redo the highlight here.
26350 Subsume it under ``we love X''. --gerd 2001-08-15 */
26351 /* Included in Windows version because Windows most likely does not
26352 do the right thing if any third party tool offers
26353 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26354 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26355 {
26356 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26357 if (f == hlinfo->mouse_face_mouse_frame)
26358 {
26359 int x = hlinfo->mouse_face_mouse_x;
26360 int y = hlinfo->mouse_face_mouse_y;
26361 clear_mouse_face (hlinfo);
26362 note_mouse_highlight (f, x, y);
26363 }
26364 }
26365 }
26366
26367
26368 /* EXPORT:
26369 Determine the intersection of two rectangles R1 and R2. Return
26370 the intersection in *RESULT. Value is non-zero if RESULT is not
26371 empty. */
26372
26373 int
26374 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26375 {
26376 XRectangle *left, *right;
26377 XRectangle *upper, *lower;
26378 int intersection_p = 0;
26379
26380 /* Rearrange so that R1 is the left-most rectangle. */
26381 if (r1->x < r2->x)
26382 left = r1, right = r2;
26383 else
26384 left = r2, right = r1;
26385
26386 /* X0 of the intersection is right.x0, if this is inside R1,
26387 otherwise there is no intersection. */
26388 if (right->x <= left->x + left->width)
26389 {
26390 result->x = right->x;
26391
26392 /* The right end of the intersection is the minimum of the
26393 the right ends of left and right. */
26394 result->width = (min (left->x + left->width, right->x + right->width)
26395 - result->x);
26396
26397 /* Same game for Y. */
26398 if (r1->y < r2->y)
26399 upper = r1, lower = r2;
26400 else
26401 upper = r2, lower = r1;
26402
26403 /* The upper end of the intersection is lower.y0, if this is inside
26404 of upper. Otherwise, there is no intersection. */
26405 if (lower->y <= upper->y + upper->height)
26406 {
26407 result->y = lower->y;
26408
26409 /* The lower end of the intersection is the minimum of the lower
26410 ends of upper and lower. */
26411 result->height = (min (lower->y + lower->height,
26412 upper->y + upper->height)
26413 - result->y);
26414 intersection_p = 1;
26415 }
26416 }
26417
26418 return intersection_p;
26419 }
26420
26421 #endif /* HAVE_WINDOW_SYSTEM */
26422
26423 \f
26424 /***********************************************************************
26425 Initialization
26426 ***********************************************************************/
26427
26428 void
26429 syms_of_xdisp (void)
26430 {
26431 Vwith_echo_area_save_vector = Qnil;
26432 staticpro (&Vwith_echo_area_save_vector);
26433
26434 Vmessage_stack = Qnil;
26435 staticpro (&Vmessage_stack);
26436
26437 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
26438 staticpro (&Qinhibit_redisplay);
26439
26440 message_dolog_marker1 = Fmake_marker ();
26441 staticpro (&message_dolog_marker1);
26442 message_dolog_marker2 = Fmake_marker ();
26443 staticpro (&message_dolog_marker2);
26444 message_dolog_marker3 = Fmake_marker ();
26445 staticpro (&message_dolog_marker3);
26446
26447 #if GLYPH_DEBUG
26448 defsubr (&Sdump_frame_glyph_matrix);
26449 defsubr (&Sdump_glyph_matrix);
26450 defsubr (&Sdump_glyph_row);
26451 defsubr (&Sdump_tool_bar_row);
26452 defsubr (&Strace_redisplay);
26453 defsubr (&Strace_to_stderr);
26454 #endif
26455 #ifdef HAVE_WINDOW_SYSTEM
26456 defsubr (&Stool_bar_lines_needed);
26457 defsubr (&Slookup_image_map);
26458 #endif
26459 defsubr (&Sformat_mode_line);
26460 defsubr (&Sinvisible_p);
26461 defsubr (&Scurrent_bidi_paragraph_direction);
26462
26463 staticpro (&Qmenu_bar_update_hook);
26464 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
26465
26466 staticpro (&Qoverriding_terminal_local_map);
26467 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
26468
26469 staticpro (&Qoverriding_local_map);
26470 Qoverriding_local_map = intern_c_string ("overriding-local-map");
26471
26472 staticpro (&Qwindow_scroll_functions);
26473 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
26474
26475 staticpro (&Qwindow_text_change_functions);
26476 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
26477
26478 staticpro (&Qredisplay_end_trigger_functions);
26479 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
26480
26481 staticpro (&Qinhibit_point_motion_hooks);
26482 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
26483
26484 Qeval = intern_c_string ("eval");
26485 staticpro (&Qeval);
26486
26487 QCdata = intern_c_string (":data");
26488 staticpro (&QCdata);
26489 Qdisplay = intern_c_string ("display");
26490 staticpro (&Qdisplay);
26491 Qspace_width = intern_c_string ("space-width");
26492 staticpro (&Qspace_width);
26493 Qraise = intern_c_string ("raise");
26494 staticpro (&Qraise);
26495 Qslice = intern_c_string ("slice");
26496 staticpro (&Qslice);
26497 Qspace = intern_c_string ("space");
26498 staticpro (&Qspace);
26499 Qmargin = intern_c_string ("margin");
26500 staticpro (&Qmargin);
26501 Qpointer = intern_c_string ("pointer");
26502 staticpro (&Qpointer);
26503 Qleft_margin = intern_c_string ("left-margin");
26504 staticpro (&Qleft_margin);
26505 Qright_margin = intern_c_string ("right-margin");
26506 staticpro (&Qright_margin);
26507 Qcenter = intern_c_string ("center");
26508 staticpro (&Qcenter);
26509 Qline_height = intern_c_string ("line-height");
26510 staticpro (&Qline_height);
26511 QCalign_to = intern_c_string (":align-to");
26512 staticpro (&QCalign_to);
26513 QCrelative_width = intern_c_string (":relative-width");
26514 staticpro (&QCrelative_width);
26515 QCrelative_height = intern_c_string (":relative-height");
26516 staticpro (&QCrelative_height);
26517 QCeval = intern_c_string (":eval");
26518 staticpro (&QCeval);
26519 QCpropertize = intern_c_string (":propertize");
26520 staticpro (&QCpropertize);
26521 QCfile = intern_c_string (":file");
26522 staticpro (&QCfile);
26523 Qfontified = intern_c_string ("fontified");
26524 staticpro (&Qfontified);
26525 Qfontification_functions = intern_c_string ("fontification-functions");
26526 staticpro (&Qfontification_functions);
26527 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
26528 staticpro (&Qtrailing_whitespace);
26529 Qescape_glyph = intern_c_string ("escape-glyph");
26530 staticpro (&Qescape_glyph);
26531 Qnobreak_space = intern_c_string ("nobreak-space");
26532 staticpro (&Qnobreak_space);
26533 Qimage = intern_c_string ("image");
26534 staticpro (&Qimage);
26535 Qtext = intern_c_string ("text");
26536 staticpro (&Qtext);
26537 Qboth = intern_c_string ("both");
26538 staticpro (&Qboth);
26539 Qboth_horiz = intern_c_string ("both-horiz");
26540 staticpro (&Qboth_horiz);
26541 Qtext_image_horiz = intern_c_string ("text-image-horiz");
26542 staticpro (&Qtext_image_horiz);
26543 QCmap = intern_c_string (":map");
26544 staticpro (&QCmap);
26545 QCpointer = intern_c_string (":pointer");
26546 staticpro (&QCpointer);
26547 Qrect = intern_c_string ("rect");
26548 staticpro (&Qrect);
26549 Qcircle = intern_c_string ("circle");
26550 staticpro (&Qcircle);
26551 Qpoly = intern_c_string ("poly");
26552 staticpro (&Qpoly);
26553 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
26554 staticpro (&Qmessage_truncate_lines);
26555 Qgrow_only = intern_c_string ("grow-only");
26556 staticpro (&Qgrow_only);
26557 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
26558 staticpro (&Qinhibit_menubar_update);
26559 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
26560 staticpro (&Qinhibit_eval_during_redisplay);
26561 Qposition = intern_c_string ("position");
26562 staticpro (&Qposition);
26563 Qbuffer_position = intern_c_string ("buffer-position");
26564 staticpro (&Qbuffer_position);
26565 Qobject = intern_c_string ("object");
26566 staticpro (&Qobject);
26567 Qbar = intern_c_string ("bar");
26568 staticpro (&Qbar);
26569 Qhbar = intern_c_string ("hbar");
26570 staticpro (&Qhbar);
26571 Qbox = intern_c_string ("box");
26572 staticpro (&Qbox);
26573 Qhollow = intern_c_string ("hollow");
26574 staticpro (&Qhollow);
26575 Qhand = intern_c_string ("hand");
26576 staticpro (&Qhand);
26577 Qarrow = intern_c_string ("arrow");
26578 staticpro (&Qarrow);
26579 Qtext = intern_c_string ("text");
26580 staticpro (&Qtext);
26581 Qrisky_local_variable = intern_c_string ("risky-local-variable");
26582 staticpro (&Qrisky_local_variable);
26583 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
26584 staticpro (&Qinhibit_free_realized_faces);
26585
26586 list_of_error = Fcons (Fcons (intern_c_string ("error"),
26587 Fcons (intern_c_string ("void-variable"), Qnil)),
26588 Qnil);
26589 staticpro (&list_of_error);
26590
26591 Qlast_arrow_position = intern_c_string ("last-arrow-position");
26592 staticpro (&Qlast_arrow_position);
26593 Qlast_arrow_string = intern_c_string ("last-arrow-string");
26594 staticpro (&Qlast_arrow_string);
26595
26596 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
26597 staticpro (&Qoverlay_arrow_string);
26598 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
26599 staticpro (&Qoverlay_arrow_bitmap);
26600
26601 echo_buffer[0] = echo_buffer[1] = Qnil;
26602 staticpro (&echo_buffer[0]);
26603 staticpro (&echo_buffer[1]);
26604
26605 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
26606 staticpro (&echo_area_buffer[0]);
26607 staticpro (&echo_area_buffer[1]);
26608
26609 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
26610 staticpro (&Vmessages_buffer_name);
26611
26612 mode_line_proptrans_alist = Qnil;
26613 staticpro (&mode_line_proptrans_alist);
26614 mode_line_string_list = Qnil;
26615 staticpro (&mode_line_string_list);
26616 mode_line_string_face = Qnil;
26617 staticpro (&mode_line_string_face);
26618 mode_line_string_face_prop = Qnil;
26619 staticpro (&mode_line_string_face_prop);
26620 Vmode_line_unwind_vector = Qnil;
26621 staticpro (&Vmode_line_unwind_vector);
26622
26623 help_echo_string = Qnil;
26624 staticpro (&help_echo_string);
26625 help_echo_object = Qnil;
26626 staticpro (&help_echo_object);
26627 help_echo_window = Qnil;
26628 staticpro (&help_echo_window);
26629 previous_help_echo_string = Qnil;
26630 staticpro (&previous_help_echo_string);
26631 help_echo_pos = -1;
26632
26633 Qright_to_left = intern_c_string ("right-to-left");
26634 staticpro (&Qright_to_left);
26635 Qleft_to_right = intern_c_string ("left-to-right");
26636 staticpro (&Qleft_to_right);
26637
26638 #ifdef HAVE_WINDOW_SYSTEM
26639 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
26640 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
26641 For example, if a block cursor is over a tab, it will be drawn as
26642 wide as that tab on the display. */);
26643 x_stretch_cursor_p = 0;
26644 #endif
26645
26646 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
26647 doc: /* *Non-nil means highlight trailing whitespace.
26648 The face used for trailing whitespace is `trailing-whitespace'. */);
26649 Vshow_trailing_whitespace = Qnil;
26650
26651 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
26652 doc: /* *Control highlighting of nobreak space and soft hyphen.
26653 A value of t means highlight the character itself (for nobreak space,
26654 use face `nobreak-space').
26655 A value of nil means no highlighting.
26656 Other values mean display the escape glyph followed by an ordinary
26657 space or ordinary hyphen. */);
26658 Vnobreak_char_display = Qt;
26659
26660 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
26661 doc: /* *The pointer shape to show in void text areas.
26662 A value of nil means to show the text pointer. Other options are `arrow',
26663 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
26664 Vvoid_text_area_pointer = Qarrow;
26665
26666 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
26667 doc: /* Non-nil means don't actually do any redisplay.
26668 This is used for internal purposes. */);
26669 Vinhibit_redisplay = Qnil;
26670
26671 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
26672 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
26673 Vglobal_mode_string = Qnil;
26674
26675 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
26676 doc: /* Marker for where to display an arrow on top of the buffer text.
26677 This must be the beginning of a line in order to work.
26678 See also `overlay-arrow-string'. */);
26679 Voverlay_arrow_position = Qnil;
26680
26681 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
26682 doc: /* String to display as an arrow in non-window frames.
26683 See also `overlay-arrow-position'. */);
26684 Voverlay_arrow_string = make_pure_c_string ("=>");
26685
26686 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
26687 doc: /* List of variables (symbols) which hold markers for overlay arrows.
26688 The symbols on this list are examined during redisplay to determine
26689 where to display overlay arrows. */);
26690 Voverlay_arrow_variable_list
26691 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
26692
26693 DEFVAR_INT ("scroll-step", &scroll_step,
26694 doc: /* *The number of lines to try scrolling a window by when point moves out.
26695 If that fails to bring point back on frame, point is centered instead.
26696 If this is zero, point is always centered after it moves off frame.
26697 If you want scrolling to always be a line at a time, you should set
26698 `scroll-conservatively' to a large value rather than set this to 1. */);
26699
26700 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
26701 doc: /* *Scroll up to this many lines, to bring point back on screen.
26702 If point moves off-screen, redisplay will scroll by up to
26703 `scroll-conservatively' lines in order to bring point just barely
26704 onto the screen again. If that cannot be done, then redisplay
26705 recenters point as usual.
26706
26707 A value of zero means always recenter point if it moves off screen. */);
26708 scroll_conservatively = 0;
26709
26710 DEFVAR_INT ("scroll-margin", &scroll_margin,
26711 doc: /* *Number of lines of margin at the top and bottom of a window.
26712 Recenter the window whenever point gets within this many lines
26713 of the top or bottom of the window. */);
26714 scroll_margin = 0;
26715
26716 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
26717 doc: /* Pixels per inch value for non-window system displays.
26718 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
26719 Vdisplay_pixels_per_inch = make_float (72.0);
26720
26721 #if GLYPH_DEBUG
26722 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
26723 #endif
26724
26725 DEFVAR_LISP ("truncate-partial-width-windows",
26726 &Vtruncate_partial_width_windows,
26727 doc: /* Non-nil means truncate lines in windows narrower than the frame.
26728 For an integer value, truncate lines in each window narrower than the
26729 full frame width, provided the window width is less than that integer;
26730 otherwise, respect the value of `truncate-lines'.
26731
26732 For any other non-nil value, truncate lines in all windows that do
26733 not span the full frame width.
26734
26735 A value of nil means to respect the value of `truncate-lines'.
26736
26737 If `word-wrap' is enabled, you might want to reduce this. */);
26738 Vtruncate_partial_width_windows = make_number (50);
26739
26740 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
26741 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
26742 Any other value means to use the appropriate face, `mode-line',
26743 `header-line', or `menu' respectively. */);
26744 mode_line_inverse_video = 1;
26745
26746 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
26747 doc: /* *Maximum buffer size for which line number should be displayed.
26748 If the buffer is bigger than this, the line number does not appear
26749 in the mode line. A value of nil means no limit. */);
26750 Vline_number_display_limit = Qnil;
26751
26752 DEFVAR_INT ("line-number-display-limit-width",
26753 &line_number_display_limit_width,
26754 doc: /* *Maximum line width (in characters) for line number display.
26755 If the average length of the lines near point is bigger than this, then the
26756 line number may be omitted from the mode line. */);
26757 line_number_display_limit_width = 200;
26758
26759 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
26760 doc: /* *Non-nil means highlight region even in nonselected windows. */);
26761 highlight_nonselected_windows = 0;
26762
26763 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
26764 doc: /* Non-nil if more than one frame is visible on this display.
26765 Minibuffer-only frames don't count, but iconified frames do.
26766 This variable is not guaranteed to be accurate except while processing
26767 `frame-title-format' and `icon-title-format'. */);
26768
26769 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
26770 doc: /* Template for displaying the title bar of visible frames.
26771 \(Assuming the window manager supports this feature.)
26772
26773 This variable has the same structure as `mode-line-format', except that
26774 the %c and %l constructs are ignored. It is used only on frames for
26775 which no explicit name has been set \(see `modify-frame-parameters'). */);
26776
26777 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
26778 doc: /* Template for displaying the title bar of an iconified frame.
26779 \(Assuming the window manager supports this feature.)
26780 This variable has the same structure as `mode-line-format' (which see),
26781 and is used only on frames for which no explicit name has been set
26782 \(see `modify-frame-parameters'). */);
26783 Vicon_title_format
26784 = Vframe_title_format
26785 = pure_cons (intern_c_string ("multiple-frames"),
26786 pure_cons (make_pure_c_string ("%b"),
26787 pure_cons (pure_cons (empty_unibyte_string,
26788 pure_cons (intern_c_string ("invocation-name"),
26789 pure_cons (make_pure_c_string ("@"),
26790 pure_cons (intern_c_string ("system-name"),
26791 Qnil)))),
26792 Qnil)));
26793
26794 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
26795 doc: /* Maximum number of lines to keep in the message log buffer.
26796 If nil, disable message logging. If t, log messages but don't truncate
26797 the buffer when it becomes large. */);
26798 Vmessage_log_max = make_number (100);
26799
26800 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
26801 doc: /* Functions called before redisplay, if window sizes have changed.
26802 The value should be a list of functions that take one argument.
26803 Just before redisplay, for each frame, if any of its windows have changed
26804 size since the last redisplay, or have been split or deleted,
26805 all the functions in the list are called, with the frame as argument. */);
26806 Vwindow_size_change_functions = Qnil;
26807
26808 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
26809 doc: /* List of functions to call before redisplaying a window with scrolling.
26810 Each function is called with two arguments, the window and its new
26811 display-start position. Note that these functions are also called by
26812 `set-window-buffer'. Also note that the value of `window-end' is not
26813 valid when these functions are called. */);
26814 Vwindow_scroll_functions = Qnil;
26815
26816 DEFVAR_LISP ("window-text-change-functions",
26817 &Vwindow_text_change_functions,
26818 doc: /* Functions to call in redisplay when text in the window might change. */);
26819 Vwindow_text_change_functions = Qnil;
26820
26821 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
26822 doc: /* Functions called when redisplay of a window reaches the end trigger.
26823 Each function is called with two arguments, the window and the end trigger value.
26824 See `set-window-redisplay-end-trigger'. */);
26825 Vredisplay_end_trigger_functions = Qnil;
26826
26827 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
26828 doc: /* *Non-nil means autoselect window with mouse pointer.
26829 If nil, do not autoselect windows.
26830 A positive number means delay autoselection by that many seconds: a
26831 window is autoselected only after the mouse has remained in that
26832 window for the duration of the delay.
26833 A negative number has a similar effect, but causes windows to be
26834 autoselected only after the mouse has stopped moving. \(Because of
26835 the way Emacs compares mouse events, you will occasionally wait twice
26836 that time before the window gets selected.\)
26837 Any other value means to autoselect window instantaneously when the
26838 mouse pointer enters it.
26839
26840 Autoselection selects the minibuffer only if it is active, and never
26841 unselects the minibuffer if it is active.
26842
26843 When customizing this variable make sure that the actual value of
26844 `focus-follows-mouse' matches the behavior of your window manager. */);
26845 Vmouse_autoselect_window = Qnil;
26846
26847 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
26848 doc: /* *Non-nil means automatically resize tool-bars.
26849 This dynamically changes the tool-bar's height to the minimum height
26850 that is needed to make all tool-bar items visible.
26851 If value is `grow-only', the tool-bar's height is only increased
26852 automatically; to decrease the tool-bar height, use \\[recenter]. */);
26853 Vauto_resize_tool_bars = Qt;
26854
26855 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
26856 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
26857 auto_raise_tool_bar_buttons_p = 1;
26858
26859 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
26860 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
26861 make_cursor_line_fully_visible_p = 1;
26862
26863 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
26864 doc: /* *Border below tool-bar in pixels.
26865 If an integer, use it as the height of the border.
26866 If it is one of `internal-border-width' or `border-width', use the
26867 value of the corresponding frame parameter.
26868 Otherwise, no border is added below the tool-bar. */);
26869 Vtool_bar_border = Qinternal_border_width;
26870
26871 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
26872 doc: /* *Margin around tool-bar buttons in pixels.
26873 If an integer, use that for both horizontal and vertical margins.
26874 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
26875 HORZ specifying the horizontal margin, and VERT specifying the
26876 vertical margin. */);
26877 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
26878
26879 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
26880 doc: /* *Relief thickness of tool-bar buttons. */);
26881 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
26882
26883 DEFVAR_LISP ("tool-bar-style", &Vtool_bar_style,
26884 doc: /* *Tool bar style to use.
26885 It can be one of
26886 image - show images only
26887 text - show text only
26888 both - show both, text below image
26889 both-horiz - show text to the right of the image
26890 text-image-horiz - show text to the left of the image
26891 any other - use system default or image if no system default. */);
26892 Vtool_bar_style = Qnil;
26893
26894 DEFVAR_INT ("tool-bar-max-label-size", &tool_bar_max_label_size,
26895 doc: /* *Maximum number of characters a label can have to be shown.
26896 The tool bar style must also show labels for this to have any effect, see
26897 `tool-bar-style'. */);
26898 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
26899
26900 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
26901 doc: /* List of functions to call to fontify regions of text.
26902 Each function is called with one argument POS. Functions must
26903 fontify a region starting at POS in the current buffer, and give
26904 fontified regions the property `fontified'. */);
26905 Vfontification_functions = Qnil;
26906 Fmake_variable_buffer_local (Qfontification_functions);
26907
26908 DEFVAR_BOOL ("unibyte-display-via-language-environment",
26909 &unibyte_display_via_language_environment,
26910 doc: /* *Non-nil means display unibyte text according to language environment.
26911 Specifically, this means that raw bytes in the range 160-255 decimal
26912 are displayed by converting them to the equivalent multibyte characters
26913 according to the current language environment. As a result, they are
26914 displayed according to the current fontset.
26915
26916 Note that this variable affects only how these bytes are displayed,
26917 but does not change the fact they are interpreted as raw bytes. */);
26918 unibyte_display_via_language_environment = 0;
26919
26920 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
26921 doc: /* *Maximum height for resizing mini-windows.
26922 If a float, it specifies a fraction of the mini-window frame's height.
26923 If an integer, it specifies a number of lines. */);
26924 Vmax_mini_window_height = make_float (0.25);
26925
26926 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
26927 doc: /* *How to resize mini-windows.
26928 A value of nil means don't automatically resize mini-windows.
26929 A value of t means resize them to fit the text displayed in them.
26930 A value of `grow-only', the default, means let mini-windows grow
26931 only, until their display becomes empty, at which point the windows
26932 go back to their normal size. */);
26933 Vresize_mini_windows = Qgrow_only;
26934
26935 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
26936 doc: /* Alist specifying how to blink the cursor off.
26937 Each element has the form (ON-STATE . OFF-STATE). Whenever the
26938 `cursor-type' frame-parameter or variable equals ON-STATE,
26939 comparing using `equal', Emacs uses OFF-STATE to specify
26940 how to blink it off. ON-STATE and OFF-STATE are values for
26941 the `cursor-type' frame parameter.
26942
26943 If a frame's ON-STATE has no entry in this list,
26944 the frame's other specifications determine how to blink the cursor off. */);
26945 Vblink_cursor_alist = Qnil;
26946
26947 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
26948 doc: /* Allow or disallow automatic horizontal scrolling of windows.
26949 If non-nil, windows are automatically scrolled horizontally to make
26950 point visible. */);
26951 automatic_hscrolling_p = 1;
26952 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
26953 staticpro (&Qauto_hscroll_mode);
26954
26955 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
26956 doc: /* *How many columns away from the window edge point is allowed to get
26957 before automatic hscrolling will horizontally scroll the window. */);
26958 hscroll_margin = 5;
26959
26960 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
26961 doc: /* *How many columns to scroll the window when point gets too close to the edge.
26962 When point is less than `hscroll-margin' columns from the window
26963 edge, automatic hscrolling will scroll the window by the amount of columns
26964 determined by this variable. If its value is a positive integer, scroll that
26965 many columns. If it's a positive floating-point number, it specifies the
26966 fraction of the window's width to scroll. If it's nil or zero, point will be
26967 centered horizontally after the scroll. Any other value, including negative
26968 numbers, are treated as if the value were zero.
26969
26970 Automatic hscrolling always moves point outside the scroll margin, so if
26971 point was more than scroll step columns inside the margin, the window will
26972 scroll more than the value given by the scroll step.
26973
26974 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26975 and `scroll-right' overrides this variable's effect. */);
26976 Vhscroll_step = make_number (0);
26977
26978 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
26979 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26980 Bind this around calls to `message' to let it take effect. */);
26981 message_truncate_lines = 0;
26982
26983 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
26984 doc: /* Normal hook run to update the menu bar definitions.
26985 Redisplay runs this hook before it redisplays the menu bar.
26986 This is used to update submenus such as Buffers,
26987 whose contents depend on various data. */);
26988 Vmenu_bar_update_hook = Qnil;
26989
26990 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
26991 doc: /* Frame for which we are updating a menu.
26992 The enable predicate for a menu binding should check this variable. */);
26993 Vmenu_updating_frame = Qnil;
26994
26995 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
26996 doc: /* Non-nil means don't update menu bars. Internal use only. */);
26997 inhibit_menubar_update = 0;
26998
26999 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
27000 doc: /* Prefix prepended to all continuation lines at display time.
27001 The value may be a string, an image, or a stretch-glyph; it is
27002 interpreted in the same way as the value of a `display' text property.
27003
27004 This variable is overridden by any `wrap-prefix' text or overlay
27005 property.
27006
27007 To add a prefix to non-continuation lines, use `line-prefix'. */);
27008 Vwrap_prefix = Qnil;
27009 staticpro (&Qwrap_prefix);
27010 Qwrap_prefix = intern_c_string ("wrap-prefix");
27011 Fmake_variable_buffer_local (Qwrap_prefix);
27012
27013 DEFVAR_LISP ("line-prefix", &Vline_prefix,
27014 doc: /* Prefix prepended to all non-continuation lines at display time.
27015 The value may be a string, an image, or a stretch-glyph; it is
27016 interpreted in the same way as the value of a `display' text property.
27017
27018 This variable is overridden by any `line-prefix' text or overlay
27019 property.
27020
27021 To add a prefix to continuation lines, use `wrap-prefix'. */);
27022 Vline_prefix = Qnil;
27023 staticpro (&Qline_prefix);
27024 Qline_prefix = intern_c_string ("line-prefix");
27025 Fmake_variable_buffer_local (Qline_prefix);
27026
27027 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
27028 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27029 inhibit_eval_during_redisplay = 0;
27030
27031 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
27032 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27033 inhibit_free_realized_faces = 0;
27034
27035 #if GLYPH_DEBUG
27036 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
27037 doc: /* Inhibit try_window_id display optimization. */);
27038 inhibit_try_window_id = 0;
27039
27040 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
27041 doc: /* Inhibit try_window_reusing display optimization. */);
27042 inhibit_try_window_reusing = 0;
27043
27044 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
27045 doc: /* Inhibit try_cursor_movement display optimization. */);
27046 inhibit_try_cursor_movement = 0;
27047 #endif /* GLYPH_DEBUG */
27048
27049 DEFVAR_INT ("overline-margin", &overline_margin,
27050 doc: /* *Space between overline and text, in pixels.
27051 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27052 margin to the caracter height. */);
27053 overline_margin = 2;
27054
27055 DEFVAR_INT ("underline-minimum-offset",
27056 &underline_minimum_offset,
27057 doc: /* Minimum distance between baseline and underline.
27058 This can improve legibility of underlined text at small font sizes,
27059 particularly when using variable `x-use-underline-position-properties'
27060 with fonts that specify an UNDERLINE_POSITION relatively close to the
27061 baseline. The default value is 1. */);
27062 underline_minimum_offset = 1;
27063
27064 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
27065 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27066 This feature only works when on a window system that can change
27067 cursor shapes. */);
27068 display_hourglass_p = 1;
27069
27070 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
27071 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27072 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27073
27074 hourglass_atimer = NULL;
27075 hourglass_shown_p = 0;
27076
27077 DEFSYM (Qglyphless_char, "glyphless-char");
27078 DEFSYM (Qhex_code, "hex-code");
27079 DEFSYM (Qempty_box, "empty-box");
27080 DEFSYM (Qthin_space, "thin-space");
27081 DEFSYM (Qzero_width, "zero-width");
27082
27083 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27084 /* Intern this now in case it isn't already done.
27085 Setting this variable twice is harmless.
27086 But don't staticpro it here--that is done in alloc.c. */
27087 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27088 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27089
27090 DEFVAR_LISP ("glyphless-char-display", &Vglyphless_char_display,
27091 doc: /* Char-table to control displaying of glyphless characters.
27092 Each element, if non-nil, is an ASCII acronym string (displayed in a box)
27093 or one of these symbols:
27094 hex-code: display the hexadecimal code of a character in a box
27095 empty-box: display as an empty box
27096 thin-space: display as 1-pixel width space
27097 zero-width: don't display
27098
27099 It has one extra slot to control the display of a character for which
27100 no font is found. The value of the slot is `hex-code' or `empty-box'.
27101 The default is `empty-box'. */);
27102 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27103 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27104 Qempty_box);
27105 }
27106
27107
27108 /* Initialize this module when Emacs starts. */
27109
27110 void
27111 init_xdisp (void)
27112 {
27113 Lisp_Object root_window;
27114 struct window *mini_w;
27115
27116 current_header_line_height = current_mode_line_height = -1;
27117
27118 CHARPOS (this_line_start_pos) = 0;
27119
27120 mini_w = XWINDOW (minibuf_window);
27121 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
27122
27123 if (!noninteractive)
27124 {
27125 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
27126 int i;
27127
27128 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
27129 set_window_height (root_window,
27130 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27131 0);
27132 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27133 set_window_height (minibuf_window, 1, 0);
27134
27135 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27136 mini_w->total_cols = make_number (FRAME_COLS (f));
27137
27138 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27139 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27140 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27141
27142 /* The default ellipsis glyphs `...'. */
27143 for (i = 0; i < 3; ++i)
27144 default_invis_vector[i] = make_number ('.');
27145 }
27146
27147 {
27148 /* Allocate the buffer for frame titles.
27149 Also used for `format-mode-line'. */
27150 int size = 100;
27151 mode_line_noprop_buf = (char *) xmalloc (size);
27152 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27153 mode_line_noprop_ptr = mode_line_noprop_buf;
27154 mode_line_target = MODE_LINE_DISPLAY;
27155 }
27156
27157 help_echo_showing_p = 0;
27158 }
27159
27160 /* Since w32 does not support atimers, it defines its own implementation of
27161 the following three functions in w32fns.c. */
27162 #ifndef WINDOWSNT
27163
27164 /* Platform-independent portion of hourglass implementation. */
27165
27166 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27167 int
27168 hourglass_started (void)
27169 {
27170 return hourglass_shown_p || hourglass_atimer != NULL;
27171 }
27172
27173 /* Cancel a currently active hourglass timer, and start a new one. */
27174 void
27175 start_hourglass (void)
27176 {
27177 #if defined (HAVE_WINDOW_SYSTEM)
27178 EMACS_TIME delay;
27179 int secs, usecs = 0;
27180
27181 cancel_hourglass ();
27182
27183 if (INTEGERP (Vhourglass_delay)
27184 && XINT (Vhourglass_delay) > 0)
27185 secs = XFASTINT (Vhourglass_delay);
27186 else if (FLOATP (Vhourglass_delay)
27187 && XFLOAT_DATA (Vhourglass_delay) > 0)
27188 {
27189 Lisp_Object tem;
27190 tem = Ftruncate (Vhourglass_delay, Qnil);
27191 secs = XFASTINT (tem);
27192 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27193 }
27194 else
27195 secs = DEFAULT_HOURGLASS_DELAY;
27196
27197 EMACS_SET_SECS_USECS (delay, secs, usecs);
27198 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27199 show_hourglass, NULL);
27200 #endif
27201 }
27202
27203
27204 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27205 shown. */
27206 void
27207 cancel_hourglass (void)
27208 {
27209 #if defined (HAVE_WINDOW_SYSTEM)
27210 if (hourglass_atimer)
27211 {
27212 cancel_atimer (hourglass_atimer);
27213 hourglass_atimer = NULL;
27214 }
27215
27216 if (hourglass_shown_p)
27217 hide_hourglass ();
27218 #endif
27219 }
27220 #endif /* ! WINDOWSNT */
27221