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 Qhexa_code, Qempty_box, Qthin_space, Qzero_width;
975
976 /* Default pixel width of `thin-space' display method. */
977 #define THIN_SPACE_WIDTH 1
978
979 /* Default number of seconds to wait before displaying an hourglass
980 cursor. */
981 #define DEFAULT_HOURGLASS_DELAY 1
982
983 \f
984 /* Function prototypes. */
985
986 static void setup_for_ellipsis (struct it *, int);
987 static void mark_window_display_accurate_1 (struct window *, int);
988 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
989 static int display_prop_string_p (Lisp_Object, Lisp_Object);
990 static int cursor_row_p (struct window *, struct glyph_row *);
991 static int redisplay_mode_lines (Lisp_Object, int);
992 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
993
994 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
995
996 static void handle_line_prefix (struct it *);
997
998 static void pint2str (char *, int, int);
999 static void pint2hrstr (char *, int, int);
1000 static struct text_pos run_window_scroll_functions (Lisp_Object,
1001 struct text_pos);
1002 static void reconsider_clip_changes (struct window *, struct buffer *);
1003 static int text_outside_line_unchanged_p (struct window *,
1004 EMACS_INT, EMACS_INT);
1005 static void store_mode_line_noprop_char (char);
1006 static int store_mode_line_noprop (const unsigned char *, int, int);
1007 static void handle_stop (struct it *);
1008 static void handle_stop_backwards (struct it *, EMACS_INT);
1009 static int single_display_spec_intangible_p (Lisp_Object);
1010 static void ensure_echo_area_buffers (void);
1011 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
1012 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
1013 static int with_echo_area_buffer (struct window *, int,
1014 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
1015 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1016 static void clear_garbaged_frames (void);
1017 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1018 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1019 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1020 static int display_echo_area (struct window *);
1021 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1022 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1023 static Lisp_Object unwind_redisplay (Lisp_Object);
1024 static int string_char_and_length (const unsigned char *, int *);
1025 static struct text_pos display_prop_end (struct it *, Lisp_Object,
1026 struct text_pos);
1027 static int compute_window_start_on_continuation_line (struct window *);
1028 static Lisp_Object safe_eval_handler (Lisp_Object);
1029 static void insert_left_trunc_glyphs (struct it *);
1030 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
1031 Lisp_Object);
1032 static void extend_face_to_end_of_line (struct it *);
1033 static int append_space_for_newline (struct it *, int);
1034 static int cursor_row_fully_visible_p (struct window *, int, int);
1035 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
1036 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
1037 static int trailing_whitespace_p (EMACS_INT);
1038 static int message_log_check_duplicate (EMACS_INT, EMACS_INT,
1039 EMACS_INT, EMACS_INT);
1040 static void push_it (struct it *);
1041 static void pop_it (struct it *);
1042 static void sync_frame_with_window_matrix_rows (struct window *);
1043 static void select_frame_for_redisplay (Lisp_Object);
1044 static void redisplay_internal (int);
1045 static int echo_area_display (int);
1046 static void redisplay_windows (Lisp_Object);
1047 static void redisplay_window (Lisp_Object, int);
1048 static Lisp_Object redisplay_window_error (Lisp_Object);
1049 static Lisp_Object redisplay_window_0 (Lisp_Object);
1050 static Lisp_Object redisplay_window_1 (Lisp_Object);
1051 static int update_menu_bar (struct frame *, int, int);
1052 static int try_window_reusing_current_matrix (struct window *);
1053 static int try_window_id (struct window *);
1054 static int display_line (struct it *);
1055 static int display_mode_lines (struct window *);
1056 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
1057 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
1058 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
1059 static const char *decode_mode_spec (struct window *, int, int, int,
1060 Lisp_Object *);
1061 static void display_menu_bar (struct window *);
1062 static int display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT, int,
1063 EMACS_INT *);
1064 static int display_string (const unsigned char *, Lisp_Object, Lisp_Object,
1065 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
1066 static void compute_line_metrics (struct it *);
1067 static void run_redisplay_end_trigger_hook (struct it *);
1068 static int get_overlay_strings (struct it *, EMACS_INT);
1069 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
1070 static void next_overlay_string (struct it *);
1071 static void reseat (struct it *, struct text_pos, int);
1072 static void reseat_1 (struct it *, struct text_pos, int);
1073 static void back_to_previous_visible_line_start (struct it *);
1074 void reseat_at_previous_visible_line_start (struct it *);
1075 static void reseat_at_next_visible_line_start (struct it *, int);
1076 static int next_element_from_ellipsis (struct it *);
1077 static int next_element_from_display_vector (struct it *);
1078 static int next_element_from_string (struct it *);
1079 static int next_element_from_c_string (struct it *);
1080 static int next_element_from_buffer (struct it *);
1081 static int next_element_from_composition (struct it *);
1082 static int next_element_from_image (struct it *);
1083 static int next_element_from_stretch (struct it *);
1084 static void load_overlay_strings (struct it *, EMACS_INT);
1085 static int init_from_display_pos (struct it *, struct window *,
1086 struct display_pos *);
1087 static void reseat_to_string (struct it *, const unsigned char *,
1088 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
1089 static enum move_it_result
1090 move_it_in_display_line_to (struct it *, EMACS_INT, int,
1091 enum move_operation_enum);
1092 void move_it_vertically_backward (struct it *, int);
1093 static void init_to_row_start (struct it *, struct window *,
1094 struct glyph_row *);
1095 static int init_to_row_end (struct it *, struct window *,
1096 struct glyph_row *);
1097 static void back_to_previous_line_start (struct it *);
1098 static int forward_to_next_line_start (struct it *, int *);
1099 static struct text_pos string_pos_nchars_ahead (struct text_pos,
1100 Lisp_Object, EMACS_INT);
1101 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
1102 static struct text_pos c_string_pos (EMACS_INT, const unsigned char *, int);
1103 static EMACS_INT number_of_chars (const unsigned char *, int);
1104 static void compute_stop_pos (struct it *);
1105 static void compute_string_pos (struct text_pos *, struct text_pos,
1106 Lisp_Object);
1107 static int face_before_or_after_it_pos (struct it *, int);
1108 static EMACS_INT next_overlay_change (EMACS_INT);
1109 static int handle_single_display_spec (struct it *, Lisp_Object,
1110 Lisp_Object, Lisp_Object,
1111 struct text_pos *, int);
1112 static int underlying_face_id (struct it *);
1113 static int in_ellipses_for_invisible_text_p (struct display_pos *,
1114 struct window *);
1115
1116 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1117 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1118
1119 #ifdef HAVE_WINDOW_SYSTEM
1120
1121 static void x_consider_frame_title (Lisp_Object);
1122 static int tool_bar_lines_needed (struct frame *, int *);
1123 static void update_tool_bar (struct frame *, int);
1124 static void build_desired_tool_bar_string (struct frame *f);
1125 static int redisplay_tool_bar (struct frame *);
1126 static void display_tool_bar_line (struct it *, int);
1127 static void notice_overwritten_cursor (struct window *,
1128 enum glyph_row_area,
1129 int, int, int, int);
1130 static void append_stretch_glyph (struct it *, Lisp_Object,
1131 int, int, int);
1132
1133
1134 #endif /* HAVE_WINDOW_SYSTEM */
1135
1136 static int coords_in_mouse_face_p (struct window *, int, int);
1137
1138
1139 \f
1140 /***********************************************************************
1141 Window display dimensions
1142 ***********************************************************************/
1143
1144 /* Return the bottom boundary y-position for text lines in window W.
1145 This is the first y position at which a line cannot start.
1146 It is relative to the top of the window.
1147
1148 This is the height of W minus the height of a mode line, if any. */
1149
1150 INLINE int
1151 window_text_bottom_y (struct window *w)
1152 {
1153 int height = WINDOW_TOTAL_HEIGHT (w);
1154
1155 if (WINDOW_WANTS_MODELINE_P (w))
1156 height -= CURRENT_MODE_LINE_HEIGHT (w);
1157 return height;
1158 }
1159
1160 /* Return the pixel width of display area AREA of window W. AREA < 0
1161 means return the total width of W, not including fringes to
1162 the left and right of the window. */
1163
1164 INLINE int
1165 window_box_width (struct window *w, int area)
1166 {
1167 int cols = XFASTINT (w->total_cols);
1168 int pixels = 0;
1169
1170 if (!w->pseudo_window_p)
1171 {
1172 cols -= WINDOW_SCROLL_BAR_COLS (w);
1173
1174 if (area == TEXT_AREA)
1175 {
1176 if (INTEGERP (w->left_margin_cols))
1177 cols -= XFASTINT (w->left_margin_cols);
1178 if (INTEGERP (w->right_margin_cols))
1179 cols -= XFASTINT (w->right_margin_cols);
1180 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1181 }
1182 else if (area == LEFT_MARGIN_AREA)
1183 {
1184 cols = (INTEGERP (w->left_margin_cols)
1185 ? XFASTINT (w->left_margin_cols) : 0);
1186 pixels = 0;
1187 }
1188 else if (area == RIGHT_MARGIN_AREA)
1189 {
1190 cols = (INTEGERP (w->right_margin_cols)
1191 ? XFASTINT (w->right_margin_cols) : 0);
1192 pixels = 0;
1193 }
1194 }
1195
1196 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1197 }
1198
1199
1200 /* Return the pixel height of the display area of window W, not
1201 including mode lines of W, if any. */
1202
1203 INLINE int
1204 window_box_height (struct window *w)
1205 {
1206 struct frame *f = XFRAME (w->frame);
1207 int height = WINDOW_TOTAL_HEIGHT (w);
1208
1209 xassert (height >= 0);
1210
1211 /* Note: the code below that determines the mode-line/header-line
1212 height is essentially the same as that contained in the macro
1213 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1214 the appropriate glyph row has its `mode_line_p' flag set,
1215 and if it doesn't, uses estimate_mode_line_height instead. */
1216
1217 if (WINDOW_WANTS_MODELINE_P (w))
1218 {
1219 struct glyph_row *ml_row
1220 = (w->current_matrix && w->current_matrix->rows
1221 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1222 : 0);
1223 if (ml_row && ml_row->mode_line_p)
1224 height -= ml_row->height;
1225 else
1226 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1227 }
1228
1229 if (WINDOW_WANTS_HEADER_LINE_P (w))
1230 {
1231 struct glyph_row *hl_row
1232 = (w->current_matrix && w->current_matrix->rows
1233 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1234 : 0);
1235 if (hl_row && hl_row->mode_line_p)
1236 height -= hl_row->height;
1237 else
1238 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1239 }
1240
1241 /* With a very small font and a mode-line that's taller than
1242 default, we might end up with a negative height. */
1243 return max (0, height);
1244 }
1245
1246 /* Return the window-relative coordinate of the left edge of display
1247 area AREA of window W. AREA < 0 means return the left edge of the
1248 whole window, to the right of the left fringe of W. */
1249
1250 INLINE int
1251 window_box_left_offset (struct window *w, int area)
1252 {
1253 int x;
1254
1255 if (w->pseudo_window_p)
1256 return 0;
1257
1258 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1259
1260 if (area == TEXT_AREA)
1261 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1262 + window_box_width (w, LEFT_MARGIN_AREA));
1263 else if (area == RIGHT_MARGIN_AREA)
1264 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1265 + window_box_width (w, LEFT_MARGIN_AREA)
1266 + window_box_width (w, TEXT_AREA)
1267 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1268 ? 0
1269 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1270 else if (area == LEFT_MARGIN_AREA
1271 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1272 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1273
1274 return x;
1275 }
1276
1277
1278 /* Return the window-relative coordinate of the right edge of display
1279 area AREA of window W. AREA < 0 means return the right edge of the
1280 whole window, to the left of the right fringe of W. */
1281
1282 INLINE int
1283 window_box_right_offset (struct window *w, int area)
1284 {
1285 return window_box_left_offset (w, area) + window_box_width (w, area);
1286 }
1287
1288 /* Return the frame-relative coordinate of the left edge of display
1289 area AREA of window W. AREA < 0 means return the left edge of the
1290 whole window, to the right of the left fringe of W. */
1291
1292 INLINE int
1293 window_box_left (struct window *w, int area)
1294 {
1295 struct frame *f = XFRAME (w->frame);
1296 int x;
1297
1298 if (w->pseudo_window_p)
1299 return FRAME_INTERNAL_BORDER_WIDTH (f);
1300
1301 x = (WINDOW_LEFT_EDGE_X (w)
1302 + window_box_left_offset (w, area));
1303
1304 return x;
1305 }
1306
1307
1308 /* Return the frame-relative coordinate of the right edge of display
1309 area AREA of window W. AREA < 0 means return the right edge of the
1310 whole window, to the left of the right fringe of W. */
1311
1312 INLINE int
1313 window_box_right (struct window *w, int area)
1314 {
1315 return window_box_left (w, area) + window_box_width (w, area);
1316 }
1317
1318 /* Get the bounding box of the display area AREA of window W, without
1319 mode lines, in frame-relative coordinates. AREA < 0 means the
1320 whole window, not including the left and right fringes of
1321 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1322 coordinates of the upper-left corner of the box. Return in
1323 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1324
1325 INLINE void
1326 window_box (struct window *w, int area, int *box_x, int *box_y,
1327 int *box_width, int *box_height)
1328 {
1329 if (box_width)
1330 *box_width = window_box_width (w, area);
1331 if (box_height)
1332 *box_height = window_box_height (w);
1333 if (box_x)
1334 *box_x = window_box_left (w, area);
1335 if (box_y)
1336 {
1337 *box_y = WINDOW_TOP_EDGE_Y (w);
1338 if (WINDOW_WANTS_HEADER_LINE_P (w))
1339 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1340 }
1341 }
1342
1343
1344 /* Get the bounding box of the display area AREA of window W, without
1345 mode lines. AREA < 0 means the whole window, not including the
1346 left and right fringe of the window. Return in *TOP_LEFT_X
1347 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1348 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1349 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1350 box. */
1351
1352 INLINE void
1353 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1354 int *bottom_right_x, int *bottom_right_y)
1355 {
1356 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1357 bottom_right_y);
1358 *bottom_right_x += *top_left_x;
1359 *bottom_right_y += *top_left_y;
1360 }
1361
1362
1363 \f
1364 /***********************************************************************
1365 Utilities
1366 ***********************************************************************/
1367
1368 /* Return the bottom y-position of the line the iterator IT is in.
1369 This can modify IT's settings. */
1370
1371 int
1372 line_bottom_y (struct it *it)
1373 {
1374 int line_height = it->max_ascent + it->max_descent;
1375 int line_top_y = it->current_y;
1376
1377 if (line_height == 0)
1378 {
1379 if (last_height)
1380 line_height = last_height;
1381 else if (IT_CHARPOS (*it) < ZV)
1382 {
1383 move_it_by_lines (it, 1, 1);
1384 line_height = (it->max_ascent || it->max_descent
1385 ? it->max_ascent + it->max_descent
1386 : last_height);
1387 }
1388 else
1389 {
1390 struct glyph_row *row = it->glyph_row;
1391
1392 /* Use the default character height. */
1393 it->glyph_row = NULL;
1394 it->what = IT_CHARACTER;
1395 it->c = ' ';
1396 it->len = 1;
1397 PRODUCE_GLYPHS (it);
1398 line_height = it->ascent + it->descent;
1399 it->glyph_row = row;
1400 }
1401 }
1402
1403 return line_top_y + line_height;
1404 }
1405
1406
1407 /* Return 1 if position CHARPOS is visible in window W.
1408 CHARPOS < 0 means return info about WINDOW_END position.
1409 If visible, set *X and *Y to pixel coordinates of top left corner.
1410 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1411 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1412
1413 int
1414 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1415 int *rtop, int *rbot, int *rowh, int *vpos)
1416 {
1417 struct it it;
1418 struct text_pos top;
1419 int visible_p = 0;
1420 struct buffer *old_buffer = NULL;
1421
1422 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1423 return visible_p;
1424
1425 if (XBUFFER (w->buffer) != current_buffer)
1426 {
1427 old_buffer = current_buffer;
1428 set_buffer_internal_1 (XBUFFER (w->buffer));
1429 }
1430
1431 SET_TEXT_POS_FROM_MARKER (top, w->start);
1432
1433 /* Compute exact mode line heights. */
1434 if (WINDOW_WANTS_MODELINE_P (w))
1435 current_mode_line_height
1436 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1437 current_buffer->mode_line_format);
1438
1439 if (WINDOW_WANTS_HEADER_LINE_P (w))
1440 current_header_line_height
1441 = display_mode_line (w, HEADER_LINE_FACE_ID,
1442 current_buffer->header_line_format);
1443
1444 start_display (&it, w, top);
1445 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1446 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1447
1448 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1449 {
1450 /* We have reached CHARPOS, or passed it. How the call to
1451 move_it_to can overshoot: (i) If CHARPOS is on invisible
1452 text, move_it_to stops at the end of the invisible text,
1453 after CHARPOS. (ii) If CHARPOS is in a display vector,
1454 move_it_to stops on its last glyph. */
1455 int top_x = it.current_x;
1456 int top_y = it.current_y;
1457 enum it_method it_method = it.method;
1458 /* Calling line_bottom_y may change it.method, it.position, etc. */
1459 int bottom_y = (last_height = 0, line_bottom_y (&it));
1460 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1461
1462 if (top_y < window_top_y)
1463 visible_p = bottom_y > window_top_y;
1464 else if (top_y < it.last_visible_y)
1465 visible_p = 1;
1466 if (visible_p)
1467 {
1468 if (it_method == GET_FROM_DISPLAY_VECTOR)
1469 {
1470 /* We stopped on the last glyph of a display vector.
1471 Try and recompute. Hack alert! */
1472 if (charpos < 2 || top.charpos >= charpos)
1473 top_x = it.glyph_row->x;
1474 else
1475 {
1476 struct it it2;
1477 start_display (&it2, w, top);
1478 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1479 get_next_display_element (&it2);
1480 PRODUCE_GLYPHS (&it2);
1481 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1482 || it2.current_x > it2.last_visible_x)
1483 top_x = it.glyph_row->x;
1484 else
1485 {
1486 top_x = it2.current_x;
1487 top_y = it2.current_y;
1488 }
1489 }
1490 }
1491
1492 *x = top_x;
1493 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1494 *rtop = max (0, window_top_y - top_y);
1495 *rbot = max (0, bottom_y - it.last_visible_y);
1496 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1497 - max (top_y, window_top_y)));
1498 *vpos = it.vpos;
1499 }
1500 }
1501 else
1502 {
1503 struct it it2;
1504
1505 it2 = it;
1506 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1507 move_it_by_lines (&it, 1, 0);
1508 if (charpos < IT_CHARPOS (it)
1509 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1510 {
1511 visible_p = 1;
1512 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1513 *x = it2.current_x;
1514 *y = it2.current_y + it2.max_ascent - it2.ascent;
1515 *rtop = max (0, -it2.current_y);
1516 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1517 - it.last_visible_y));
1518 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1519 it.last_visible_y)
1520 - max (it2.current_y,
1521 WINDOW_HEADER_LINE_HEIGHT (w))));
1522 *vpos = it2.vpos;
1523 }
1524 }
1525
1526 if (old_buffer)
1527 set_buffer_internal_1 (old_buffer);
1528
1529 current_header_line_height = current_mode_line_height = -1;
1530
1531 if (visible_p && XFASTINT (w->hscroll) > 0)
1532 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1533
1534 #if 0
1535 /* Debugging code. */
1536 if (visible_p)
1537 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1538 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1539 else
1540 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1541 #endif
1542
1543 return visible_p;
1544 }
1545
1546
1547 /* Return the next character from STR which is MAXLEN bytes long.
1548 Return in *LEN the length of the character. This is like
1549 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1550 we find one, we return a `?', but with the length of the invalid
1551 character. */
1552
1553 static INLINE int
1554 string_char_and_length (const unsigned char *str, int *len)
1555 {
1556 int c;
1557
1558 c = STRING_CHAR_AND_LENGTH (str, *len);
1559 if (!CHAR_VALID_P (c, 1))
1560 /* We may not change the length here because other places in Emacs
1561 don't use this function, i.e. they silently accept invalid
1562 characters. */
1563 c = '?';
1564
1565 return c;
1566 }
1567
1568
1569
1570 /* Given a position POS containing a valid character and byte position
1571 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1572
1573 static struct text_pos
1574 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1575 {
1576 xassert (STRINGP (string) && nchars >= 0);
1577
1578 if (STRING_MULTIBYTE (string))
1579 {
1580 EMACS_INT rest = SBYTES (string) - BYTEPOS (pos);
1581 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1582 int len;
1583
1584 while (nchars--)
1585 {
1586 string_char_and_length (p, &len);
1587 p += len, rest -= len;
1588 xassert (rest >= 0);
1589 CHARPOS (pos) += 1;
1590 BYTEPOS (pos) += len;
1591 }
1592 }
1593 else
1594 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1595
1596 return pos;
1597 }
1598
1599
1600 /* Value is the text position, i.e. character and byte position,
1601 for character position CHARPOS in STRING. */
1602
1603 static INLINE struct text_pos
1604 string_pos (EMACS_INT charpos, Lisp_Object string)
1605 {
1606 struct text_pos pos;
1607 xassert (STRINGP (string));
1608 xassert (charpos >= 0);
1609 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1610 return pos;
1611 }
1612
1613
1614 /* Value is a text position, i.e. character and byte position, for
1615 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1616 means recognize multibyte characters. */
1617
1618 static struct text_pos
1619 c_string_pos (EMACS_INT charpos, const unsigned char *s, int multibyte_p)
1620 {
1621 struct text_pos pos;
1622
1623 xassert (s != NULL);
1624 xassert (charpos >= 0);
1625
1626 if (multibyte_p)
1627 {
1628 EMACS_INT rest = strlen (s);
1629 int len;
1630
1631 SET_TEXT_POS (pos, 0, 0);
1632 while (charpos--)
1633 {
1634 string_char_and_length (s, &len);
1635 s += len, rest -= len;
1636 xassert (rest >= 0);
1637 CHARPOS (pos) += 1;
1638 BYTEPOS (pos) += len;
1639 }
1640 }
1641 else
1642 SET_TEXT_POS (pos, charpos, charpos);
1643
1644 return pos;
1645 }
1646
1647
1648 /* Value is the number of characters in C string S. MULTIBYTE_P
1649 non-zero means recognize multibyte characters. */
1650
1651 static EMACS_INT
1652 number_of_chars (const unsigned char *s, int multibyte_p)
1653 {
1654 EMACS_INT nchars;
1655
1656 if (multibyte_p)
1657 {
1658 EMACS_INT rest = strlen (s);
1659 int len;
1660 unsigned char *p = (unsigned char *) s;
1661
1662 for (nchars = 0; rest > 0; ++nchars)
1663 {
1664 string_char_and_length (p, &len);
1665 rest -= len, p += len;
1666 }
1667 }
1668 else
1669 nchars = strlen (s);
1670
1671 return nchars;
1672 }
1673
1674
1675 /* Compute byte position NEWPOS->bytepos corresponding to
1676 NEWPOS->charpos. POS is a known position in string STRING.
1677 NEWPOS->charpos must be >= POS.charpos. */
1678
1679 static void
1680 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1681 {
1682 xassert (STRINGP (string));
1683 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1684
1685 if (STRING_MULTIBYTE (string))
1686 *newpos = string_pos_nchars_ahead (pos, string,
1687 CHARPOS (*newpos) - CHARPOS (pos));
1688 else
1689 BYTEPOS (*newpos) = CHARPOS (*newpos);
1690 }
1691
1692 /* EXPORT:
1693 Return an estimation of the pixel height of mode or header lines on
1694 frame F. FACE_ID specifies what line's height to estimate. */
1695
1696 int
1697 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1698 {
1699 #ifdef HAVE_WINDOW_SYSTEM
1700 if (FRAME_WINDOW_P (f))
1701 {
1702 int height = FONT_HEIGHT (FRAME_FONT (f));
1703
1704 /* This function is called so early when Emacs starts that the face
1705 cache and mode line face are not yet initialized. */
1706 if (FRAME_FACE_CACHE (f))
1707 {
1708 struct face *face = FACE_FROM_ID (f, face_id);
1709 if (face)
1710 {
1711 if (face->font)
1712 height = FONT_HEIGHT (face->font);
1713 if (face->box_line_width > 0)
1714 height += 2 * face->box_line_width;
1715 }
1716 }
1717
1718 return height;
1719 }
1720 #endif
1721
1722 return 1;
1723 }
1724
1725 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1726 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1727 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1728 not force the value into range. */
1729
1730 void
1731 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1732 int *x, int *y, NativeRectangle *bounds, int noclip)
1733 {
1734
1735 #ifdef HAVE_WINDOW_SYSTEM
1736 if (FRAME_WINDOW_P (f))
1737 {
1738 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1739 even for negative values. */
1740 if (pix_x < 0)
1741 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1742 if (pix_y < 0)
1743 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1744
1745 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1746 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1747
1748 if (bounds)
1749 STORE_NATIVE_RECT (*bounds,
1750 FRAME_COL_TO_PIXEL_X (f, pix_x),
1751 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1752 FRAME_COLUMN_WIDTH (f) - 1,
1753 FRAME_LINE_HEIGHT (f) - 1);
1754
1755 if (!noclip)
1756 {
1757 if (pix_x < 0)
1758 pix_x = 0;
1759 else if (pix_x > FRAME_TOTAL_COLS (f))
1760 pix_x = FRAME_TOTAL_COLS (f);
1761
1762 if (pix_y < 0)
1763 pix_y = 0;
1764 else if (pix_y > FRAME_LINES (f))
1765 pix_y = FRAME_LINES (f);
1766 }
1767 }
1768 #endif
1769
1770 *x = pix_x;
1771 *y = pix_y;
1772 }
1773
1774
1775 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1776 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1777 can't tell the positions because W's display is not up to date,
1778 return 0. */
1779
1780 int
1781 glyph_to_pixel_coords (struct window *w, int hpos, int vpos,
1782 int *frame_x, int *frame_y)
1783 {
1784 #ifdef HAVE_WINDOW_SYSTEM
1785 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1786 {
1787 int success_p;
1788
1789 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1790 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1791
1792 if (display_completed)
1793 {
1794 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1795 struct glyph *glyph = row->glyphs[TEXT_AREA];
1796 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1797
1798 hpos = row->x;
1799 vpos = row->y;
1800 while (glyph < end)
1801 {
1802 hpos += glyph->pixel_width;
1803 ++glyph;
1804 }
1805
1806 /* If first glyph is partially visible, its first visible position is still 0. */
1807 if (hpos < 0)
1808 hpos = 0;
1809
1810 success_p = 1;
1811 }
1812 else
1813 {
1814 hpos = vpos = 0;
1815 success_p = 0;
1816 }
1817
1818 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1819 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1820 return success_p;
1821 }
1822 #endif
1823
1824 *frame_x = hpos;
1825 *frame_y = vpos;
1826 return 1;
1827 }
1828
1829
1830 /* Find the glyph under window-relative coordinates X/Y in window W.
1831 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1832 strings. Return in *HPOS and *VPOS the row and column number of
1833 the glyph found. Return in *AREA the glyph area containing X.
1834 Value is a pointer to the glyph found or null if X/Y is not on
1835 text, or we can't tell because W's current matrix is not up to
1836 date. */
1837
1838 static
1839 struct glyph *
1840 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1841 int *dx, int *dy, int *area)
1842 {
1843 struct glyph *glyph, *end;
1844 struct glyph_row *row = NULL;
1845 int x0, i;
1846
1847 /* Find row containing Y. Give up if some row is not enabled. */
1848 for (i = 0; i < w->current_matrix->nrows; ++i)
1849 {
1850 row = MATRIX_ROW (w->current_matrix, i);
1851 if (!row->enabled_p)
1852 return NULL;
1853 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1854 break;
1855 }
1856
1857 *vpos = i;
1858 *hpos = 0;
1859
1860 /* Give up if Y is not in the window. */
1861 if (i == w->current_matrix->nrows)
1862 return NULL;
1863
1864 /* Get the glyph area containing X. */
1865 if (w->pseudo_window_p)
1866 {
1867 *area = TEXT_AREA;
1868 x0 = 0;
1869 }
1870 else
1871 {
1872 if (x < window_box_left_offset (w, TEXT_AREA))
1873 {
1874 *area = LEFT_MARGIN_AREA;
1875 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1876 }
1877 else if (x < window_box_right_offset (w, TEXT_AREA))
1878 {
1879 *area = TEXT_AREA;
1880 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1881 }
1882 else
1883 {
1884 *area = RIGHT_MARGIN_AREA;
1885 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1886 }
1887 }
1888
1889 /* Find glyph containing X. */
1890 glyph = row->glyphs[*area];
1891 end = glyph + row->used[*area];
1892 x -= x0;
1893 while (glyph < end && x >= glyph->pixel_width)
1894 {
1895 x -= glyph->pixel_width;
1896 ++glyph;
1897 }
1898
1899 if (glyph == end)
1900 return NULL;
1901
1902 if (dx)
1903 {
1904 *dx = x;
1905 *dy = y - (row->y + row->ascent - glyph->ascent);
1906 }
1907
1908 *hpos = glyph - row->glyphs[*area];
1909 return glyph;
1910 }
1911
1912 /* EXPORT:
1913 Convert frame-relative x/y to coordinates relative to window W.
1914 Takes pseudo-windows into account. */
1915
1916 void
1917 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1918 {
1919 if (w->pseudo_window_p)
1920 {
1921 /* A pseudo-window is always full-width, and starts at the
1922 left edge of the frame, plus a frame border. */
1923 struct frame *f = XFRAME (w->frame);
1924 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1925 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1926 }
1927 else
1928 {
1929 *x -= WINDOW_LEFT_EDGE_X (w);
1930 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1931 }
1932 }
1933
1934 #ifdef HAVE_WINDOW_SYSTEM
1935
1936 /* EXPORT:
1937 Return in RECTS[] at most N clipping rectangles for glyph string S.
1938 Return the number of stored rectangles. */
1939
1940 int
1941 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1942 {
1943 XRectangle r;
1944
1945 if (n <= 0)
1946 return 0;
1947
1948 if (s->row->full_width_p)
1949 {
1950 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1951 r.x = WINDOW_LEFT_EDGE_X (s->w);
1952 r.width = WINDOW_TOTAL_WIDTH (s->w);
1953
1954 /* Unless displaying a mode or menu bar line, which are always
1955 fully visible, clip to the visible part of the row. */
1956 if (s->w->pseudo_window_p)
1957 r.height = s->row->visible_height;
1958 else
1959 r.height = s->height;
1960 }
1961 else
1962 {
1963 /* This is a text line that may be partially visible. */
1964 r.x = window_box_left (s->w, s->area);
1965 r.width = window_box_width (s->w, s->area);
1966 r.height = s->row->visible_height;
1967 }
1968
1969 if (s->clip_head)
1970 if (r.x < s->clip_head->x)
1971 {
1972 if (r.width >= s->clip_head->x - r.x)
1973 r.width -= s->clip_head->x - r.x;
1974 else
1975 r.width = 0;
1976 r.x = s->clip_head->x;
1977 }
1978 if (s->clip_tail)
1979 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1980 {
1981 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1982 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1983 else
1984 r.width = 0;
1985 }
1986
1987 /* If S draws overlapping rows, it's sufficient to use the top and
1988 bottom of the window for clipping because this glyph string
1989 intentionally draws over other lines. */
1990 if (s->for_overlaps)
1991 {
1992 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1993 r.height = window_text_bottom_y (s->w) - r.y;
1994
1995 /* Alas, the above simple strategy does not work for the
1996 environments with anti-aliased text: if the same text is
1997 drawn onto the same place multiple times, it gets thicker.
1998 If the overlap we are processing is for the erased cursor, we
1999 take the intersection with the rectagle of the cursor. */
2000 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2001 {
2002 XRectangle rc, r_save = r;
2003
2004 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2005 rc.y = s->w->phys_cursor.y;
2006 rc.width = s->w->phys_cursor_width;
2007 rc.height = s->w->phys_cursor_height;
2008
2009 x_intersect_rectangles (&r_save, &rc, &r);
2010 }
2011 }
2012 else
2013 {
2014 /* Don't use S->y for clipping because it doesn't take partially
2015 visible lines into account. For example, it can be negative for
2016 partially visible lines at the top of a window. */
2017 if (!s->row->full_width_p
2018 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2019 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2020 else
2021 r.y = max (0, s->row->y);
2022 }
2023
2024 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2025
2026 /* If drawing the cursor, don't let glyph draw outside its
2027 advertised boundaries. Cleartype does this under some circumstances. */
2028 if (s->hl == DRAW_CURSOR)
2029 {
2030 struct glyph *glyph = s->first_glyph;
2031 int height, max_y;
2032
2033 if (s->x > r.x)
2034 {
2035 r.width -= s->x - r.x;
2036 r.x = s->x;
2037 }
2038 r.width = min (r.width, glyph->pixel_width);
2039
2040 /* If r.y is below window bottom, ensure that we still see a cursor. */
2041 height = min (glyph->ascent + glyph->descent,
2042 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2043 max_y = window_text_bottom_y (s->w) - height;
2044 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2045 if (s->ybase - glyph->ascent > max_y)
2046 {
2047 r.y = max_y;
2048 r.height = height;
2049 }
2050 else
2051 {
2052 /* Don't draw cursor glyph taller than our actual glyph. */
2053 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2054 if (height < r.height)
2055 {
2056 max_y = r.y + r.height;
2057 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2058 r.height = min (max_y - r.y, height);
2059 }
2060 }
2061 }
2062
2063 if (s->row->clip)
2064 {
2065 XRectangle r_save = r;
2066
2067 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2068 r.width = 0;
2069 }
2070
2071 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2072 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2073 {
2074 #ifdef CONVERT_FROM_XRECT
2075 CONVERT_FROM_XRECT (r, *rects);
2076 #else
2077 *rects = r;
2078 #endif
2079 return 1;
2080 }
2081 else
2082 {
2083 /* If we are processing overlapping and allowed to return
2084 multiple clipping rectangles, we exclude the row of the glyph
2085 string from the clipping rectangle. This is to avoid drawing
2086 the same text on the environment with anti-aliasing. */
2087 #ifdef CONVERT_FROM_XRECT
2088 XRectangle rs[2];
2089 #else
2090 XRectangle *rs = rects;
2091 #endif
2092 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2093
2094 if (s->for_overlaps & OVERLAPS_PRED)
2095 {
2096 rs[i] = r;
2097 if (r.y + r.height > row_y)
2098 {
2099 if (r.y < row_y)
2100 rs[i].height = row_y - r.y;
2101 else
2102 rs[i].height = 0;
2103 }
2104 i++;
2105 }
2106 if (s->for_overlaps & OVERLAPS_SUCC)
2107 {
2108 rs[i] = r;
2109 if (r.y < row_y + s->row->visible_height)
2110 {
2111 if (r.y + r.height > row_y + s->row->visible_height)
2112 {
2113 rs[i].y = row_y + s->row->visible_height;
2114 rs[i].height = r.y + r.height - rs[i].y;
2115 }
2116 else
2117 rs[i].height = 0;
2118 }
2119 i++;
2120 }
2121
2122 n = i;
2123 #ifdef CONVERT_FROM_XRECT
2124 for (i = 0; i < n; i++)
2125 CONVERT_FROM_XRECT (rs[i], rects[i]);
2126 #endif
2127 return n;
2128 }
2129 }
2130
2131 /* EXPORT:
2132 Return in *NR the clipping rectangle for glyph string S. */
2133
2134 void
2135 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2136 {
2137 get_glyph_string_clip_rects (s, nr, 1);
2138 }
2139
2140
2141 /* EXPORT:
2142 Return the position and height of the phys cursor in window W.
2143 Set w->phys_cursor_width to width of phys cursor.
2144 */
2145
2146 void
2147 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2148 struct glyph *glyph, int *xp, int *yp, int *heightp)
2149 {
2150 struct frame *f = XFRAME (WINDOW_FRAME (w));
2151 int x, y, wd, h, h0, y0;
2152
2153 /* Compute the width of the rectangle to draw. If on a stretch
2154 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2155 rectangle as wide as the glyph, but use a canonical character
2156 width instead. */
2157 wd = glyph->pixel_width - 1;
2158 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2159 wd++; /* Why? */
2160 #endif
2161
2162 x = w->phys_cursor.x;
2163 if (x < 0)
2164 {
2165 wd += x;
2166 x = 0;
2167 }
2168
2169 if (glyph->type == STRETCH_GLYPH
2170 && !x_stretch_cursor_p)
2171 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2172 w->phys_cursor_width = wd;
2173
2174 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2175
2176 /* If y is below window bottom, ensure that we still see a cursor. */
2177 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2178
2179 h = max (h0, glyph->ascent + glyph->descent);
2180 h0 = min (h0, glyph->ascent + glyph->descent);
2181
2182 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2183 if (y < y0)
2184 {
2185 h = max (h - (y0 - y) + 1, h0);
2186 y = y0 - 1;
2187 }
2188 else
2189 {
2190 y0 = window_text_bottom_y (w) - h0;
2191 if (y > y0)
2192 {
2193 h += y - y0;
2194 y = y0;
2195 }
2196 }
2197
2198 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2199 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2200 *heightp = h;
2201 }
2202
2203 /*
2204 * Remember which glyph the mouse is over.
2205 */
2206
2207 void
2208 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2209 {
2210 Lisp_Object window;
2211 struct window *w;
2212 struct glyph_row *r, *gr, *end_row;
2213 enum window_part part;
2214 enum glyph_row_area area;
2215 int x, y, width, height;
2216
2217 /* Try to determine frame pixel position and size of the glyph under
2218 frame pixel coordinates X/Y on frame F. */
2219
2220 if (!f->glyphs_initialized_p
2221 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2222 NILP (window)))
2223 {
2224 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2225 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2226 goto virtual_glyph;
2227 }
2228
2229 w = XWINDOW (window);
2230 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2231 height = WINDOW_FRAME_LINE_HEIGHT (w);
2232
2233 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2234 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2235
2236 if (w->pseudo_window_p)
2237 {
2238 area = TEXT_AREA;
2239 part = ON_MODE_LINE; /* Don't adjust margin. */
2240 goto text_glyph;
2241 }
2242
2243 switch (part)
2244 {
2245 case ON_LEFT_MARGIN:
2246 area = LEFT_MARGIN_AREA;
2247 goto text_glyph;
2248
2249 case ON_RIGHT_MARGIN:
2250 area = RIGHT_MARGIN_AREA;
2251 goto text_glyph;
2252
2253 case ON_HEADER_LINE:
2254 case ON_MODE_LINE:
2255 gr = (part == ON_HEADER_LINE
2256 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2257 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2258 gy = gr->y;
2259 area = TEXT_AREA;
2260 goto text_glyph_row_found;
2261
2262 case ON_TEXT:
2263 area = TEXT_AREA;
2264
2265 text_glyph:
2266 gr = 0; gy = 0;
2267 for (; r <= end_row && r->enabled_p; ++r)
2268 if (r->y + r->height > y)
2269 {
2270 gr = r; gy = r->y;
2271 break;
2272 }
2273
2274 text_glyph_row_found:
2275 if (gr && gy <= y)
2276 {
2277 struct glyph *g = gr->glyphs[area];
2278 struct glyph *end = g + gr->used[area];
2279
2280 height = gr->height;
2281 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2282 if (gx + g->pixel_width > x)
2283 break;
2284
2285 if (g < end)
2286 {
2287 if (g->type == IMAGE_GLYPH)
2288 {
2289 /* Don't remember when mouse is over image, as
2290 image may have hot-spots. */
2291 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2292 return;
2293 }
2294 width = g->pixel_width;
2295 }
2296 else
2297 {
2298 /* Use nominal char spacing at end of line. */
2299 x -= gx;
2300 gx += (x / width) * width;
2301 }
2302
2303 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2304 gx += window_box_left_offset (w, area);
2305 }
2306 else
2307 {
2308 /* Use nominal line height at end of window. */
2309 gx = (x / width) * width;
2310 y -= gy;
2311 gy += (y / height) * height;
2312 }
2313 break;
2314
2315 case ON_LEFT_FRINGE:
2316 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2317 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2318 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2319 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2320 goto row_glyph;
2321
2322 case ON_RIGHT_FRINGE:
2323 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2324 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2325 : window_box_right_offset (w, TEXT_AREA));
2326 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2327 goto row_glyph;
2328
2329 case ON_SCROLL_BAR:
2330 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2331 ? 0
2332 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2333 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2334 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2335 : 0)));
2336 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2337
2338 row_glyph:
2339 gr = 0, gy = 0;
2340 for (; r <= end_row && r->enabled_p; ++r)
2341 if (r->y + r->height > y)
2342 {
2343 gr = r; gy = r->y;
2344 break;
2345 }
2346
2347 if (gr && gy <= y)
2348 height = gr->height;
2349 else
2350 {
2351 /* Use nominal line height at end of window. */
2352 y -= gy;
2353 gy += (y / height) * height;
2354 }
2355 break;
2356
2357 default:
2358 ;
2359 virtual_glyph:
2360 /* If there is no glyph under the mouse, then we divide the screen
2361 into a grid of the smallest glyph in the frame, and use that
2362 as our "glyph". */
2363
2364 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2365 round down even for negative values. */
2366 if (gx < 0)
2367 gx -= width - 1;
2368 if (gy < 0)
2369 gy -= height - 1;
2370
2371 gx = (gx / width) * width;
2372 gy = (gy / height) * height;
2373
2374 goto store_rect;
2375 }
2376
2377 gx += WINDOW_LEFT_EDGE_X (w);
2378 gy += WINDOW_TOP_EDGE_Y (w);
2379
2380 store_rect:
2381 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2382
2383 /* Visible feedback for debugging. */
2384 #if 0
2385 #if HAVE_X_WINDOWS
2386 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2387 f->output_data.x->normal_gc,
2388 gx, gy, width, height);
2389 #endif
2390 #endif
2391 }
2392
2393
2394 #endif /* HAVE_WINDOW_SYSTEM */
2395
2396 \f
2397 /***********************************************************************
2398 Lisp form evaluation
2399 ***********************************************************************/
2400
2401 /* Error handler for safe_eval and safe_call. */
2402
2403 static Lisp_Object
2404 safe_eval_handler (Lisp_Object arg)
2405 {
2406 add_to_log ("Error during redisplay: %s", arg, Qnil);
2407 return Qnil;
2408 }
2409
2410
2411 /* Evaluate SEXPR and return the result, or nil if something went
2412 wrong. Prevent redisplay during the evaluation. */
2413
2414 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2415 Return the result, or nil if something went wrong. Prevent
2416 redisplay during the evaluation. */
2417
2418 Lisp_Object
2419 safe_call (int nargs, Lisp_Object *args)
2420 {
2421 Lisp_Object val;
2422
2423 if (inhibit_eval_during_redisplay)
2424 val = Qnil;
2425 else
2426 {
2427 int count = SPECPDL_INDEX ();
2428 struct gcpro gcpro1;
2429
2430 GCPRO1 (args[0]);
2431 gcpro1.nvars = nargs;
2432 specbind (Qinhibit_redisplay, Qt);
2433 /* Use Qt to ensure debugger does not run,
2434 so there is no possibility of wanting to redisplay. */
2435 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2436 safe_eval_handler);
2437 UNGCPRO;
2438 val = unbind_to (count, val);
2439 }
2440
2441 return val;
2442 }
2443
2444
2445 /* Call function FN with one argument ARG.
2446 Return the result, or nil if something went wrong. */
2447
2448 Lisp_Object
2449 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2450 {
2451 Lisp_Object args[2];
2452 args[0] = fn;
2453 args[1] = arg;
2454 return safe_call (2, args);
2455 }
2456
2457 static Lisp_Object Qeval;
2458
2459 Lisp_Object
2460 safe_eval (Lisp_Object sexpr)
2461 {
2462 return safe_call1 (Qeval, sexpr);
2463 }
2464
2465 /* Call function FN with one argument ARG.
2466 Return the result, or nil if something went wrong. */
2467
2468 Lisp_Object
2469 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2470 {
2471 Lisp_Object args[3];
2472 args[0] = fn;
2473 args[1] = arg1;
2474 args[2] = arg2;
2475 return safe_call (3, args);
2476 }
2477
2478
2479 \f
2480 /***********************************************************************
2481 Debugging
2482 ***********************************************************************/
2483
2484 #if 0
2485
2486 /* Define CHECK_IT to perform sanity checks on iterators.
2487 This is for debugging. It is too slow to do unconditionally. */
2488
2489 static void
2490 check_it (it)
2491 struct it *it;
2492 {
2493 if (it->method == GET_FROM_STRING)
2494 {
2495 xassert (STRINGP (it->string));
2496 xassert (IT_STRING_CHARPOS (*it) >= 0);
2497 }
2498 else
2499 {
2500 xassert (IT_STRING_CHARPOS (*it) < 0);
2501 if (it->method == GET_FROM_BUFFER)
2502 {
2503 /* Check that character and byte positions agree. */
2504 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2505 }
2506 }
2507
2508 if (it->dpvec)
2509 xassert (it->current.dpvec_index >= 0);
2510 else
2511 xassert (it->current.dpvec_index < 0);
2512 }
2513
2514 #define CHECK_IT(IT) check_it ((IT))
2515
2516 #else /* not 0 */
2517
2518 #define CHECK_IT(IT) (void) 0
2519
2520 #endif /* not 0 */
2521
2522
2523 #if GLYPH_DEBUG
2524
2525 /* Check that the window end of window W is what we expect it
2526 to be---the last row in the current matrix displaying text. */
2527
2528 static void
2529 check_window_end (w)
2530 struct window *w;
2531 {
2532 if (!MINI_WINDOW_P (w)
2533 && !NILP (w->window_end_valid))
2534 {
2535 struct glyph_row *row;
2536 xassert ((row = MATRIX_ROW (w->current_matrix,
2537 XFASTINT (w->window_end_vpos)),
2538 !row->enabled_p
2539 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2540 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2541 }
2542 }
2543
2544 #define CHECK_WINDOW_END(W) check_window_end ((W))
2545
2546 #else /* not GLYPH_DEBUG */
2547
2548 #define CHECK_WINDOW_END(W) (void) 0
2549
2550 #endif /* not GLYPH_DEBUG */
2551
2552
2553 \f
2554 /***********************************************************************
2555 Iterator initialization
2556 ***********************************************************************/
2557
2558 /* Initialize IT for displaying current_buffer in window W, starting
2559 at character position CHARPOS. CHARPOS < 0 means that no buffer
2560 position is specified which is useful when the iterator is assigned
2561 a position later. BYTEPOS is the byte position corresponding to
2562 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2563
2564 If ROW is not null, calls to produce_glyphs with IT as parameter
2565 will produce glyphs in that row.
2566
2567 BASE_FACE_ID is the id of a base face to use. It must be one of
2568 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2569 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2570 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2571
2572 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2573 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2574 will be initialized to use the corresponding mode line glyph row of
2575 the desired matrix of W. */
2576
2577 void
2578 init_iterator (struct it *it, struct window *w,
2579 EMACS_INT charpos, EMACS_INT bytepos,
2580 struct glyph_row *row, enum face_id base_face_id)
2581 {
2582 int highlight_region_p;
2583 enum face_id remapped_base_face_id = base_face_id;
2584
2585 /* Some precondition checks. */
2586 xassert (w != NULL && it != NULL);
2587 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2588 && charpos <= ZV));
2589
2590 /* If face attributes have been changed since the last redisplay,
2591 free realized faces now because they depend on face definitions
2592 that might have changed. Don't free faces while there might be
2593 desired matrices pending which reference these faces. */
2594 if (face_change_count && !inhibit_free_realized_faces)
2595 {
2596 face_change_count = 0;
2597 free_all_realized_faces (Qnil);
2598 }
2599
2600 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2601 if (! NILP (Vface_remapping_alist))
2602 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2603
2604 /* Use one of the mode line rows of W's desired matrix if
2605 appropriate. */
2606 if (row == NULL)
2607 {
2608 if (base_face_id == MODE_LINE_FACE_ID
2609 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2610 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2611 else if (base_face_id == HEADER_LINE_FACE_ID)
2612 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2613 }
2614
2615 /* Clear IT. */
2616 memset (it, 0, sizeof *it);
2617 it->current.overlay_string_index = -1;
2618 it->current.dpvec_index = -1;
2619 it->base_face_id = remapped_base_face_id;
2620 it->string = Qnil;
2621 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2622
2623 /* The window in which we iterate over current_buffer: */
2624 XSETWINDOW (it->window, w);
2625 it->w = w;
2626 it->f = XFRAME (w->frame);
2627
2628 it->cmp_it.id = -1;
2629
2630 /* Extra space between lines (on window systems only). */
2631 if (base_face_id == DEFAULT_FACE_ID
2632 && FRAME_WINDOW_P (it->f))
2633 {
2634 if (NATNUMP (current_buffer->extra_line_spacing))
2635 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2636 else if (FLOATP (current_buffer->extra_line_spacing))
2637 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2638 * FRAME_LINE_HEIGHT (it->f));
2639 else if (it->f->extra_line_spacing > 0)
2640 it->extra_line_spacing = it->f->extra_line_spacing;
2641 it->max_extra_line_spacing = 0;
2642 }
2643
2644 /* If realized faces have been removed, e.g. because of face
2645 attribute changes of named faces, recompute them. When running
2646 in batch mode, the face cache of the initial frame is null. If
2647 we happen to get called, make a dummy face cache. */
2648 if (FRAME_FACE_CACHE (it->f) == NULL)
2649 init_frame_faces (it->f);
2650 if (FRAME_FACE_CACHE (it->f)->used == 0)
2651 recompute_basic_faces (it->f);
2652
2653 /* Current value of the `slice', `space-width', and 'height' properties. */
2654 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2655 it->space_width = Qnil;
2656 it->font_height = Qnil;
2657 it->override_ascent = -1;
2658
2659 /* Are control characters displayed as `^C'? */
2660 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2661
2662 /* -1 means everything between a CR and the following line end
2663 is invisible. >0 means lines indented more than this value are
2664 invisible. */
2665 it->selective = (INTEGERP (current_buffer->selective_display)
2666 ? XFASTINT (current_buffer->selective_display)
2667 : (!NILP (current_buffer->selective_display)
2668 ? -1 : 0));
2669 it->selective_display_ellipsis_p
2670 = !NILP (current_buffer->selective_display_ellipses);
2671
2672 /* Display table to use. */
2673 it->dp = window_display_table (w);
2674
2675 /* Are multibyte characters enabled in current_buffer? */
2676 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2677
2678 /* Do we need to reorder bidirectional text? Not if this is a
2679 unibyte buffer: by definition, none of the single-byte characters
2680 are strong R2L, so no reordering is needed. And bidi.c doesn't
2681 support unibyte buffers anyway. */
2682 it->bidi_p
2683 = !NILP (current_buffer->bidi_display_reordering) && it->multibyte_p;
2684
2685 /* Non-zero if we should highlight the region. */
2686 highlight_region_p
2687 = (!NILP (Vtransient_mark_mode)
2688 && !NILP (current_buffer->mark_active)
2689 && XMARKER (current_buffer->mark)->buffer != 0);
2690
2691 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2692 start and end of a visible region in window IT->w. Set both to
2693 -1 to indicate no region. */
2694 if (highlight_region_p
2695 /* Maybe highlight only in selected window. */
2696 && (/* Either show region everywhere. */
2697 highlight_nonselected_windows
2698 /* Or show region in the selected window. */
2699 || w == XWINDOW (selected_window)
2700 /* Or show the region if we are in the mini-buffer and W is
2701 the window the mini-buffer refers to. */
2702 || (MINI_WINDOW_P (XWINDOW (selected_window))
2703 && WINDOWP (minibuf_selected_window)
2704 && w == XWINDOW (minibuf_selected_window))))
2705 {
2706 EMACS_INT charpos = marker_position (current_buffer->mark);
2707 it->region_beg_charpos = min (PT, charpos);
2708 it->region_end_charpos = max (PT, charpos);
2709 }
2710 else
2711 it->region_beg_charpos = it->region_end_charpos = -1;
2712
2713 /* Get the position at which the redisplay_end_trigger hook should
2714 be run, if it is to be run at all. */
2715 if (MARKERP (w->redisplay_end_trigger)
2716 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2717 it->redisplay_end_trigger_charpos
2718 = marker_position (w->redisplay_end_trigger);
2719 else if (INTEGERP (w->redisplay_end_trigger))
2720 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2721
2722 /* Correct bogus values of tab_width. */
2723 it->tab_width = XINT (current_buffer->tab_width);
2724 if (it->tab_width <= 0 || it->tab_width > 1000)
2725 it->tab_width = 8;
2726
2727 /* Are lines in the display truncated? */
2728 if (base_face_id != DEFAULT_FACE_ID
2729 || XINT (it->w->hscroll)
2730 || (! WINDOW_FULL_WIDTH_P (it->w)
2731 && ((!NILP (Vtruncate_partial_width_windows)
2732 && !INTEGERP (Vtruncate_partial_width_windows))
2733 || (INTEGERP (Vtruncate_partial_width_windows)
2734 && (WINDOW_TOTAL_COLS (it->w)
2735 < XINT (Vtruncate_partial_width_windows))))))
2736 it->line_wrap = TRUNCATE;
2737 else if (NILP (current_buffer->truncate_lines))
2738 it->line_wrap = NILP (current_buffer->word_wrap)
2739 ? WINDOW_WRAP : WORD_WRAP;
2740 else
2741 it->line_wrap = TRUNCATE;
2742
2743 /* Get dimensions of truncation and continuation glyphs. These are
2744 displayed as fringe bitmaps under X, so we don't need them for such
2745 frames. */
2746 if (!FRAME_WINDOW_P (it->f))
2747 {
2748 if (it->line_wrap == TRUNCATE)
2749 {
2750 /* We will need the truncation glyph. */
2751 xassert (it->glyph_row == NULL);
2752 produce_special_glyphs (it, IT_TRUNCATION);
2753 it->truncation_pixel_width = it->pixel_width;
2754 }
2755 else
2756 {
2757 /* We will need the continuation glyph. */
2758 xassert (it->glyph_row == NULL);
2759 produce_special_glyphs (it, IT_CONTINUATION);
2760 it->continuation_pixel_width = it->pixel_width;
2761 }
2762
2763 /* Reset these values to zero because the produce_special_glyphs
2764 above has changed them. */
2765 it->pixel_width = it->ascent = it->descent = 0;
2766 it->phys_ascent = it->phys_descent = 0;
2767 }
2768
2769 /* Set this after getting the dimensions of truncation and
2770 continuation glyphs, so that we don't produce glyphs when calling
2771 produce_special_glyphs, above. */
2772 it->glyph_row = row;
2773 it->area = TEXT_AREA;
2774
2775 /* Forget any previous info about this row being reversed. */
2776 if (it->glyph_row)
2777 it->glyph_row->reversed_p = 0;
2778
2779 /* Get the dimensions of the display area. The display area
2780 consists of the visible window area plus a horizontally scrolled
2781 part to the left of the window. All x-values are relative to the
2782 start of this total display area. */
2783 if (base_face_id != DEFAULT_FACE_ID)
2784 {
2785 /* Mode lines, menu bar in terminal frames. */
2786 it->first_visible_x = 0;
2787 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2788 }
2789 else
2790 {
2791 it->first_visible_x
2792 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2793 it->last_visible_x = (it->first_visible_x
2794 + window_box_width (w, TEXT_AREA));
2795
2796 /* If we truncate lines, leave room for the truncator glyph(s) at
2797 the right margin. Otherwise, leave room for the continuation
2798 glyph(s). Truncation and continuation glyphs are not inserted
2799 for window-based redisplay. */
2800 if (!FRAME_WINDOW_P (it->f))
2801 {
2802 if (it->line_wrap == TRUNCATE)
2803 it->last_visible_x -= it->truncation_pixel_width;
2804 else
2805 it->last_visible_x -= it->continuation_pixel_width;
2806 }
2807
2808 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2809 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2810 }
2811
2812 /* Leave room for a border glyph. */
2813 if (!FRAME_WINDOW_P (it->f)
2814 && !WINDOW_RIGHTMOST_P (it->w))
2815 it->last_visible_x -= 1;
2816
2817 it->last_visible_y = window_text_bottom_y (w);
2818
2819 /* For mode lines and alike, arrange for the first glyph having a
2820 left box line if the face specifies a box. */
2821 if (base_face_id != DEFAULT_FACE_ID)
2822 {
2823 struct face *face;
2824
2825 it->face_id = remapped_base_face_id;
2826
2827 /* If we have a boxed mode line, make the first character appear
2828 with a left box line. */
2829 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2830 if (face->box != FACE_NO_BOX)
2831 it->start_of_box_run_p = 1;
2832 }
2833
2834 /* If we are to reorder bidirectional text, init the bidi
2835 iterator. */
2836 if (it->bidi_p)
2837 {
2838 /* Note the paragraph direction that this buffer wants to
2839 use. */
2840 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2841 it->paragraph_embedding = L2R;
2842 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2843 it->paragraph_embedding = R2L;
2844 else
2845 it->paragraph_embedding = NEUTRAL_DIR;
2846 bidi_init_it (charpos, bytepos, &it->bidi_it);
2847 }
2848
2849 /* If a buffer position was specified, set the iterator there,
2850 getting overlays and face properties from that position. */
2851 if (charpos >= BUF_BEG (current_buffer))
2852 {
2853 it->end_charpos = ZV;
2854 it->face_id = -1;
2855 IT_CHARPOS (*it) = charpos;
2856
2857 /* Compute byte position if not specified. */
2858 if (bytepos < charpos)
2859 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2860 else
2861 IT_BYTEPOS (*it) = bytepos;
2862
2863 it->start = it->current;
2864
2865 /* Compute faces etc. */
2866 reseat (it, it->current.pos, 1);
2867 }
2868
2869 CHECK_IT (it);
2870 }
2871
2872
2873 /* Initialize IT for the display of window W with window start POS. */
2874
2875 void
2876 start_display (struct it *it, struct window *w, struct text_pos pos)
2877 {
2878 struct glyph_row *row;
2879 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2880
2881 row = w->desired_matrix->rows + first_vpos;
2882 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2883 it->first_vpos = first_vpos;
2884
2885 /* Don't reseat to previous visible line start if current start
2886 position is in a string or image. */
2887 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2888 {
2889 int start_at_line_beg_p;
2890 int first_y = it->current_y;
2891
2892 /* If window start is not at a line start, skip forward to POS to
2893 get the correct continuation lines width. */
2894 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2895 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2896 if (!start_at_line_beg_p)
2897 {
2898 int new_x;
2899
2900 reseat_at_previous_visible_line_start (it);
2901 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2902
2903 new_x = it->current_x + it->pixel_width;
2904
2905 /* If lines are continued, this line may end in the middle
2906 of a multi-glyph character (e.g. a control character
2907 displayed as \003, or in the middle of an overlay
2908 string). In this case move_it_to above will not have
2909 taken us to the start of the continuation line but to the
2910 end of the continued line. */
2911 if (it->current_x > 0
2912 && it->line_wrap != TRUNCATE /* Lines are continued. */
2913 && (/* And glyph doesn't fit on the line. */
2914 new_x > it->last_visible_x
2915 /* Or it fits exactly and we're on a window
2916 system frame. */
2917 || (new_x == it->last_visible_x
2918 && FRAME_WINDOW_P (it->f))))
2919 {
2920 if (it->current.dpvec_index >= 0
2921 || it->current.overlay_string_index >= 0)
2922 {
2923 set_iterator_to_next (it, 1);
2924 move_it_in_display_line_to (it, -1, -1, 0);
2925 }
2926
2927 it->continuation_lines_width += it->current_x;
2928 }
2929
2930 /* We're starting a new display line, not affected by the
2931 height of the continued line, so clear the appropriate
2932 fields in the iterator structure. */
2933 it->max_ascent = it->max_descent = 0;
2934 it->max_phys_ascent = it->max_phys_descent = 0;
2935
2936 it->current_y = first_y;
2937 it->vpos = 0;
2938 it->current_x = it->hpos = 0;
2939 }
2940 }
2941 }
2942
2943
2944 /* Return 1 if POS is a position in ellipses displayed for invisible
2945 text. W is the window we display, for text property lookup. */
2946
2947 static int
2948 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2949 {
2950 Lisp_Object prop, window;
2951 int ellipses_p = 0;
2952 EMACS_INT charpos = CHARPOS (pos->pos);
2953
2954 /* If POS specifies a position in a display vector, this might
2955 be for an ellipsis displayed for invisible text. We won't
2956 get the iterator set up for delivering that ellipsis unless
2957 we make sure that it gets aware of the invisible text. */
2958 if (pos->dpvec_index >= 0
2959 && pos->overlay_string_index < 0
2960 && CHARPOS (pos->string_pos) < 0
2961 && charpos > BEGV
2962 && (XSETWINDOW (window, w),
2963 prop = Fget_char_property (make_number (charpos),
2964 Qinvisible, window),
2965 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2966 {
2967 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2968 window);
2969 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2970 }
2971
2972 return ellipses_p;
2973 }
2974
2975
2976 /* Initialize IT for stepping through current_buffer in window W,
2977 starting at position POS that includes overlay string and display
2978 vector/ control character translation position information. Value
2979 is zero if there are overlay strings with newlines at POS. */
2980
2981 static int
2982 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2983 {
2984 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2985 int i, overlay_strings_with_newlines = 0;
2986
2987 /* If POS specifies a position in a display vector, this might
2988 be for an ellipsis displayed for invisible text. We won't
2989 get the iterator set up for delivering that ellipsis unless
2990 we make sure that it gets aware of the invisible text. */
2991 if (in_ellipses_for_invisible_text_p (pos, w))
2992 {
2993 --charpos;
2994 bytepos = 0;
2995 }
2996
2997 /* Keep in mind: the call to reseat in init_iterator skips invisible
2998 text, so we might end up at a position different from POS. This
2999 is only a problem when POS is a row start after a newline and an
3000 overlay starts there with an after-string, and the overlay has an
3001 invisible property. Since we don't skip invisible text in
3002 display_line and elsewhere immediately after consuming the
3003 newline before the row start, such a POS will not be in a string,
3004 but the call to init_iterator below will move us to the
3005 after-string. */
3006 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3007
3008 /* This only scans the current chunk -- it should scan all chunks.
3009 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3010 to 16 in 22.1 to make this a lesser problem. */
3011 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3012 {
3013 const char *s = SDATA (it->overlay_strings[i]);
3014 const char *e = s + SBYTES (it->overlay_strings[i]);
3015
3016 while (s < e && *s != '\n')
3017 ++s;
3018
3019 if (s < e)
3020 {
3021 overlay_strings_with_newlines = 1;
3022 break;
3023 }
3024 }
3025
3026 /* If position is within an overlay string, set up IT to the right
3027 overlay string. */
3028 if (pos->overlay_string_index >= 0)
3029 {
3030 int relative_index;
3031
3032 /* If the first overlay string happens to have a `display'
3033 property for an image, the iterator will be set up for that
3034 image, and we have to undo that setup first before we can
3035 correct the overlay string index. */
3036 if (it->method == GET_FROM_IMAGE)
3037 pop_it (it);
3038
3039 /* We already have the first chunk of overlay strings in
3040 IT->overlay_strings. Load more until the one for
3041 pos->overlay_string_index is in IT->overlay_strings. */
3042 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3043 {
3044 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3045 it->current.overlay_string_index = 0;
3046 while (n--)
3047 {
3048 load_overlay_strings (it, 0);
3049 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3050 }
3051 }
3052
3053 it->current.overlay_string_index = pos->overlay_string_index;
3054 relative_index = (it->current.overlay_string_index
3055 % OVERLAY_STRING_CHUNK_SIZE);
3056 it->string = it->overlay_strings[relative_index];
3057 xassert (STRINGP (it->string));
3058 it->current.string_pos = pos->string_pos;
3059 it->method = GET_FROM_STRING;
3060 }
3061
3062 if (CHARPOS (pos->string_pos) >= 0)
3063 {
3064 /* Recorded position is not in an overlay string, but in another
3065 string. This can only be a string from a `display' property.
3066 IT should already be filled with that string. */
3067 it->current.string_pos = pos->string_pos;
3068 xassert (STRINGP (it->string));
3069 }
3070
3071 /* Restore position in display vector translations, control
3072 character translations or ellipses. */
3073 if (pos->dpvec_index >= 0)
3074 {
3075 if (it->dpvec == NULL)
3076 get_next_display_element (it);
3077 xassert (it->dpvec && it->current.dpvec_index == 0);
3078 it->current.dpvec_index = pos->dpvec_index;
3079 }
3080
3081 CHECK_IT (it);
3082 return !overlay_strings_with_newlines;
3083 }
3084
3085
3086 /* Initialize IT for stepping through current_buffer in window W
3087 starting at ROW->start. */
3088
3089 static void
3090 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3091 {
3092 init_from_display_pos (it, w, &row->start);
3093 it->start = row->start;
3094 it->continuation_lines_width = row->continuation_lines_width;
3095 CHECK_IT (it);
3096 }
3097
3098
3099 /* Initialize IT for stepping through current_buffer in window W
3100 starting in the line following ROW, i.e. starting at ROW->end.
3101 Value is zero if there are overlay strings with newlines at ROW's
3102 end position. */
3103
3104 static int
3105 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3106 {
3107 int success = 0;
3108
3109 if (init_from_display_pos (it, w, &row->end))
3110 {
3111 if (row->continued_p)
3112 it->continuation_lines_width
3113 = row->continuation_lines_width + row->pixel_width;
3114 CHECK_IT (it);
3115 success = 1;
3116 }
3117
3118 return success;
3119 }
3120
3121
3122
3123 \f
3124 /***********************************************************************
3125 Text properties
3126 ***********************************************************************/
3127
3128 /* Called when IT reaches IT->stop_charpos. Handle text property and
3129 overlay changes. Set IT->stop_charpos to the next position where
3130 to stop. */
3131
3132 static void
3133 handle_stop (struct it *it)
3134 {
3135 enum prop_handled handled;
3136 int handle_overlay_change_p;
3137 struct props *p;
3138
3139 it->dpvec = NULL;
3140 it->current.dpvec_index = -1;
3141 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3142 it->ignore_overlay_strings_at_pos_p = 0;
3143 it->ellipsis_p = 0;
3144
3145 /* Use face of preceding text for ellipsis (if invisible) */
3146 if (it->selective_display_ellipsis_p)
3147 it->saved_face_id = it->face_id;
3148
3149 do
3150 {
3151 handled = HANDLED_NORMALLY;
3152
3153 /* Call text property handlers. */
3154 for (p = it_props; p->handler; ++p)
3155 {
3156 handled = p->handler (it);
3157
3158 if (handled == HANDLED_RECOMPUTE_PROPS)
3159 break;
3160 else if (handled == HANDLED_RETURN)
3161 {
3162 /* We still want to show before and after strings from
3163 overlays even if the actual buffer text is replaced. */
3164 if (!handle_overlay_change_p
3165 || it->sp > 1
3166 || !get_overlay_strings_1 (it, 0, 0))
3167 {
3168 if (it->ellipsis_p)
3169 setup_for_ellipsis (it, 0);
3170 /* When handling a display spec, we might load an
3171 empty string. In that case, discard it here. We
3172 used to discard it in handle_single_display_spec,
3173 but that causes get_overlay_strings_1, above, to
3174 ignore overlay strings that we must check. */
3175 if (STRINGP (it->string) && !SCHARS (it->string))
3176 pop_it (it);
3177 return;
3178 }
3179 else if (STRINGP (it->string) && !SCHARS (it->string))
3180 pop_it (it);
3181 else
3182 {
3183 it->ignore_overlay_strings_at_pos_p = 1;
3184 it->string_from_display_prop_p = 0;
3185 handle_overlay_change_p = 0;
3186 }
3187 handled = HANDLED_RECOMPUTE_PROPS;
3188 break;
3189 }
3190 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3191 handle_overlay_change_p = 0;
3192 }
3193
3194 if (handled != HANDLED_RECOMPUTE_PROPS)
3195 {
3196 /* Don't check for overlay strings below when set to deliver
3197 characters from a display vector. */
3198 if (it->method == GET_FROM_DISPLAY_VECTOR)
3199 handle_overlay_change_p = 0;
3200
3201 /* Handle overlay changes.
3202 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3203 if it finds overlays. */
3204 if (handle_overlay_change_p)
3205 handled = handle_overlay_change (it);
3206 }
3207
3208 if (it->ellipsis_p)
3209 {
3210 setup_for_ellipsis (it, 0);
3211 break;
3212 }
3213 }
3214 while (handled == HANDLED_RECOMPUTE_PROPS);
3215
3216 /* Determine where to stop next. */
3217 if (handled == HANDLED_NORMALLY)
3218 compute_stop_pos (it);
3219 }
3220
3221
3222 /* Compute IT->stop_charpos from text property and overlay change
3223 information for IT's current position. */
3224
3225 static void
3226 compute_stop_pos (struct it *it)
3227 {
3228 register INTERVAL iv, next_iv;
3229 Lisp_Object object, limit, position;
3230 EMACS_INT charpos, bytepos;
3231
3232 /* If nowhere else, stop at the end. */
3233 it->stop_charpos = it->end_charpos;
3234
3235 if (STRINGP (it->string))
3236 {
3237 /* Strings are usually short, so don't limit the search for
3238 properties. */
3239 object = it->string;
3240 limit = Qnil;
3241 charpos = IT_STRING_CHARPOS (*it);
3242 bytepos = IT_STRING_BYTEPOS (*it);
3243 }
3244 else
3245 {
3246 EMACS_INT pos;
3247
3248 /* If next overlay change is in front of the current stop pos
3249 (which is IT->end_charpos), stop there. Note: value of
3250 next_overlay_change is point-max if no overlay change
3251 follows. */
3252 charpos = IT_CHARPOS (*it);
3253 bytepos = IT_BYTEPOS (*it);
3254 pos = next_overlay_change (charpos);
3255 if (pos < it->stop_charpos)
3256 it->stop_charpos = pos;
3257
3258 /* If showing the region, we have to stop at the region
3259 start or end because the face might change there. */
3260 if (it->region_beg_charpos > 0)
3261 {
3262 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3263 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3264 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3265 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3266 }
3267
3268 /* Set up variables for computing the stop position from text
3269 property changes. */
3270 XSETBUFFER (object, current_buffer);
3271 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3272 }
3273
3274 /* Get the interval containing IT's position. Value is a null
3275 interval if there isn't such an interval. */
3276 position = make_number (charpos);
3277 iv = validate_interval_range (object, &position, &position, 0);
3278 if (!NULL_INTERVAL_P (iv))
3279 {
3280 Lisp_Object values_here[LAST_PROP_IDX];
3281 struct props *p;
3282
3283 /* Get properties here. */
3284 for (p = it_props; p->handler; ++p)
3285 values_here[p->idx] = textget (iv->plist, *p->name);
3286
3287 /* Look for an interval following iv that has different
3288 properties. */
3289 for (next_iv = next_interval (iv);
3290 (!NULL_INTERVAL_P (next_iv)
3291 && (NILP (limit)
3292 || XFASTINT (limit) > next_iv->position));
3293 next_iv = next_interval (next_iv))
3294 {
3295 for (p = it_props; p->handler; ++p)
3296 {
3297 Lisp_Object new_value;
3298
3299 new_value = textget (next_iv->plist, *p->name);
3300 if (!EQ (values_here[p->idx], new_value))
3301 break;
3302 }
3303
3304 if (p->handler)
3305 break;
3306 }
3307
3308 if (!NULL_INTERVAL_P (next_iv))
3309 {
3310 if (INTEGERP (limit)
3311 && next_iv->position >= XFASTINT (limit))
3312 /* No text property change up to limit. */
3313 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3314 else
3315 /* Text properties change in next_iv. */
3316 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3317 }
3318 }
3319
3320 if (it->cmp_it.id < 0)
3321 {
3322 EMACS_INT stoppos = it->end_charpos;
3323
3324 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3325 stoppos = -1;
3326 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3327 stoppos, it->string);
3328 }
3329
3330 xassert (STRINGP (it->string)
3331 || (it->stop_charpos >= BEGV
3332 && it->stop_charpos >= IT_CHARPOS (*it)));
3333 }
3334
3335
3336 /* Return the position of the next overlay change after POS in
3337 current_buffer. Value is point-max if no overlay change
3338 follows. This is like `next-overlay-change' but doesn't use
3339 xmalloc. */
3340
3341 static EMACS_INT
3342 next_overlay_change (EMACS_INT pos)
3343 {
3344 int noverlays;
3345 EMACS_INT endpos;
3346 Lisp_Object *overlays;
3347 int i;
3348
3349 /* Get all overlays at the given position. */
3350 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3351
3352 /* If any of these overlays ends before endpos,
3353 use its ending point instead. */
3354 for (i = 0; i < noverlays; ++i)
3355 {
3356 Lisp_Object oend;
3357 EMACS_INT oendpos;
3358
3359 oend = OVERLAY_END (overlays[i]);
3360 oendpos = OVERLAY_POSITION (oend);
3361 endpos = min (endpos, oendpos);
3362 }
3363
3364 return endpos;
3365 }
3366
3367
3368 \f
3369 /***********************************************************************
3370 Fontification
3371 ***********************************************************************/
3372
3373 /* Handle changes in the `fontified' property of the current buffer by
3374 calling hook functions from Qfontification_functions to fontify
3375 regions of text. */
3376
3377 static enum prop_handled
3378 handle_fontified_prop (struct it *it)
3379 {
3380 Lisp_Object prop, pos;
3381 enum prop_handled handled = HANDLED_NORMALLY;
3382
3383 if (!NILP (Vmemory_full))
3384 return handled;
3385
3386 /* Get the value of the `fontified' property at IT's current buffer
3387 position. (The `fontified' property doesn't have a special
3388 meaning in strings.) If the value is nil, call functions from
3389 Qfontification_functions. */
3390 if (!STRINGP (it->string)
3391 && it->s == NULL
3392 && !NILP (Vfontification_functions)
3393 && !NILP (Vrun_hooks)
3394 && (pos = make_number (IT_CHARPOS (*it)),
3395 prop = Fget_char_property (pos, Qfontified, Qnil),
3396 /* Ignore the special cased nil value always present at EOB since
3397 no amount of fontifying will be able to change it. */
3398 NILP (prop) && IT_CHARPOS (*it) < Z))
3399 {
3400 int count = SPECPDL_INDEX ();
3401 Lisp_Object val;
3402
3403 val = Vfontification_functions;
3404 specbind (Qfontification_functions, Qnil);
3405
3406 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3407 safe_call1 (val, pos);
3408 else
3409 {
3410 Lisp_Object globals, fn;
3411 struct gcpro gcpro1, gcpro2;
3412
3413 globals = Qnil;
3414 GCPRO2 (val, globals);
3415
3416 for (; CONSP (val); val = XCDR (val))
3417 {
3418 fn = XCAR (val);
3419
3420 if (EQ (fn, Qt))
3421 {
3422 /* A value of t indicates this hook has a local
3423 binding; it means to run the global binding too.
3424 In a global value, t should not occur. If it
3425 does, we must ignore it to avoid an endless
3426 loop. */
3427 for (globals = Fdefault_value (Qfontification_functions);
3428 CONSP (globals);
3429 globals = XCDR (globals))
3430 {
3431 fn = XCAR (globals);
3432 if (!EQ (fn, Qt))
3433 safe_call1 (fn, pos);
3434 }
3435 }
3436 else
3437 safe_call1 (fn, pos);
3438 }
3439
3440 UNGCPRO;
3441 }
3442
3443 unbind_to (count, Qnil);
3444
3445 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3446 something. This avoids an endless loop if they failed to
3447 fontify the text for which reason ever. */
3448 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3449 handled = HANDLED_RECOMPUTE_PROPS;
3450 }
3451
3452 return handled;
3453 }
3454
3455
3456 \f
3457 /***********************************************************************
3458 Faces
3459 ***********************************************************************/
3460
3461 /* Set up iterator IT from face properties at its current position.
3462 Called from handle_stop. */
3463
3464 static enum prop_handled
3465 handle_face_prop (struct it *it)
3466 {
3467 int new_face_id;
3468 EMACS_INT next_stop;
3469
3470 if (!STRINGP (it->string))
3471 {
3472 new_face_id
3473 = face_at_buffer_position (it->w,
3474 IT_CHARPOS (*it),
3475 it->region_beg_charpos,
3476 it->region_end_charpos,
3477 &next_stop,
3478 (IT_CHARPOS (*it)
3479 + TEXT_PROP_DISTANCE_LIMIT),
3480 0, it->base_face_id);
3481
3482 /* Is this a start of a run of characters with box face?
3483 Caveat: this can be called for a freshly initialized
3484 iterator; face_id is -1 in this case. We know that the new
3485 face will not change until limit, i.e. if the new face has a
3486 box, all characters up to limit will have one. But, as
3487 usual, we don't know whether limit is really the end. */
3488 if (new_face_id != it->face_id)
3489 {
3490 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3491
3492 /* If new face has a box but old face has not, this is
3493 the start of a run of characters with box, i.e. it has
3494 a shadow on the left side. The value of face_id of the
3495 iterator will be -1 if this is the initial call that gets
3496 the face. In this case, we have to look in front of IT's
3497 position and see whether there is a face != new_face_id. */
3498 it->start_of_box_run_p
3499 = (new_face->box != FACE_NO_BOX
3500 && (it->face_id >= 0
3501 || IT_CHARPOS (*it) == BEG
3502 || new_face_id != face_before_it_pos (it)));
3503 it->face_box_p = new_face->box != FACE_NO_BOX;
3504 }
3505 }
3506 else
3507 {
3508 int base_face_id;
3509 EMACS_INT bufpos;
3510 int i;
3511 Lisp_Object from_overlay
3512 = (it->current.overlay_string_index >= 0
3513 ? it->string_overlays[it->current.overlay_string_index]
3514 : Qnil);
3515
3516 /* See if we got to this string directly or indirectly from
3517 an overlay property. That includes the before-string or
3518 after-string of an overlay, strings in display properties
3519 provided by an overlay, their text properties, etc.
3520
3521 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3522 if (! NILP (from_overlay))
3523 for (i = it->sp - 1; i >= 0; i--)
3524 {
3525 if (it->stack[i].current.overlay_string_index >= 0)
3526 from_overlay
3527 = it->string_overlays[it->stack[i].current.overlay_string_index];
3528 else if (! NILP (it->stack[i].from_overlay))
3529 from_overlay = it->stack[i].from_overlay;
3530
3531 if (!NILP (from_overlay))
3532 break;
3533 }
3534
3535 if (! NILP (from_overlay))
3536 {
3537 bufpos = IT_CHARPOS (*it);
3538 /* For a string from an overlay, the base face depends
3539 only on text properties and ignores overlays. */
3540 base_face_id
3541 = face_for_overlay_string (it->w,
3542 IT_CHARPOS (*it),
3543 it->region_beg_charpos,
3544 it->region_end_charpos,
3545 &next_stop,
3546 (IT_CHARPOS (*it)
3547 + TEXT_PROP_DISTANCE_LIMIT),
3548 0,
3549 from_overlay);
3550 }
3551 else
3552 {
3553 bufpos = 0;
3554
3555 /* For strings from a `display' property, use the face at
3556 IT's current buffer position as the base face to merge
3557 with, so that overlay strings appear in the same face as
3558 surrounding text, unless they specify their own
3559 faces. */
3560 base_face_id = underlying_face_id (it);
3561 }
3562
3563 new_face_id = face_at_string_position (it->w,
3564 it->string,
3565 IT_STRING_CHARPOS (*it),
3566 bufpos,
3567 it->region_beg_charpos,
3568 it->region_end_charpos,
3569 &next_stop,
3570 base_face_id, 0);
3571
3572 /* Is this a start of a run of characters with box? Caveat:
3573 this can be called for a freshly allocated iterator; face_id
3574 is -1 is this case. We know that the new face will not
3575 change until the next check pos, i.e. if the new face has a
3576 box, all characters up to that position will have a
3577 box. But, as usual, we don't know whether that position
3578 is really the end. */
3579 if (new_face_id != it->face_id)
3580 {
3581 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3582 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3583
3584 /* If new face has a box but old face hasn't, this is the
3585 start of a run of characters with box, i.e. it has a
3586 shadow on the left side. */
3587 it->start_of_box_run_p
3588 = new_face->box && (old_face == NULL || !old_face->box);
3589 it->face_box_p = new_face->box != FACE_NO_BOX;
3590 }
3591 }
3592
3593 it->face_id = new_face_id;
3594 return HANDLED_NORMALLY;
3595 }
3596
3597
3598 /* Return the ID of the face ``underlying'' IT's current position,
3599 which is in a string. If the iterator is associated with a
3600 buffer, return the face at IT's current buffer position.
3601 Otherwise, use the iterator's base_face_id. */
3602
3603 static int
3604 underlying_face_id (struct it *it)
3605 {
3606 int face_id = it->base_face_id, i;
3607
3608 xassert (STRINGP (it->string));
3609
3610 for (i = it->sp - 1; i >= 0; --i)
3611 if (NILP (it->stack[i].string))
3612 face_id = it->stack[i].face_id;
3613
3614 return face_id;
3615 }
3616
3617
3618 /* Compute the face one character before or after the current position
3619 of IT. BEFORE_P non-zero means get the face in front of IT's
3620 position. Value is the id of the face. */
3621
3622 static int
3623 face_before_or_after_it_pos (struct it *it, int before_p)
3624 {
3625 int face_id, limit;
3626 EMACS_INT next_check_charpos;
3627 struct text_pos pos;
3628
3629 xassert (it->s == NULL);
3630
3631 if (STRINGP (it->string))
3632 {
3633 EMACS_INT bufpos;
3634 int base_face_id;
3635
3636 /* No face change past the end of the string (for the case
3637 we are padding with spaces). No face change before the
3638 string start. */
3639 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3640 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3641 return it->face_id;
3642
3643 /* Set pos to the position before or after IT's current position. */
3644 if (before_p)
3645 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3646 else
3647 /* For composition, we must check the character after the
3648 composition. */
3649 pos = (it->what == IT_COMPOSITION
3650 ? string_pos (IT_STRING_CHARPOS (*it)
3651 + it->cmp_it.nchars, it->string)
3652 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3653
3654 if (it->current.overlay_string_index >= 0)
3655 bufpos = IT_CHARPOS (*it);
3656 else
3657 bufpos = 0;
3658
3659 base_face_id = underlying_face_id (it);
3660
3661 /* Get the face for ASCII, or unibyte. */
3662 face_id = face_at_string_position (it->w,
3663 it->string,
3664 CHARPOS (pos),
3665 bufpos,
3666 it->region_beg_charpos,
3667 it->region_end_charpos,
3668 &next_check_charpos,
3669 base_face_id, 0);
3670
3671 /* Correct the face for charsets different from ASCII. Do it
3672 for the multibyte case only. The face returned above is
3673 suitable for unibyte text if IT->string is unibyte. */
3674 if (STRING_MULTIBYTE (it->string))
3675 {
3676 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3677 int c, len;
3678 struct face *face = FACE_FROM_ID (it->f, face_id);
3679
3680 c = string_char_and_length (p, &len);
3681 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3682 }
3683 }
3684 else
3685 {
3686 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3687 || (IT_CHARPOS (*it) <= BEGV && before_p))
3688 return it->face_id;
3689
3690 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3691 pos = it->current.pos;
3692
3693 if (before_p)
3694 DEC_TEXT_POS (pos, it->multibyte_p);
3695 else
3696 {
3697 if (it->what == IT_COMPOSITION)
3698 /* For composition, we must check the position after the
3699 composition. */
3700 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3701 else
3702 INC_TEXT_POS (pos, it->multibyte_p);
3703 }
3704
3705 /* Determine face for CHARSET_ASCII, or unibyte. */
3706 face_id = face_at_buffer_position (it->w,
3707 CHARPOS (pos),
3708 it->region_beg_charpos,
3709 it->region_end_charpos,
3710 &next_check_charpos,
3711 limit, 0, -1);
3712
3713 /* Correct the face for charsets different from ASCII. Do it
3714 for the multibyte case only. The face returned above is
3715 suitable for unibyte text if current_buffer is unibyte. */
3716 if (it->multibyte_p)
3717 {
3718 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3719 struct face *face = FACE_FROM_ID (it->f, face_id);
3720 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3721 }
3722 }
3723
3724 return face_id;
3725 }
3726
3727
3728 \f
3729 /***********************************************************************
3730 Invisible text
3731 ***********************************************************************/
3732
3733 /* Set up iterator IT from invisible properties at its current
3734 position. Called from handle_stop. */
3735
3736 static enum prop_handled
3737 handle_invisible_prop (struct it *it)
3738 {
3739 enum prop_handled handled = HANDLED_NORMALLY;
3740
3741 if (STRINGP (it->string))
3742 {
3743 Lisp_Object prop, end_charpos, limit, charpos;
3744
3745 /* Get the value of the invisible text property at the
3746 current position. Value will be nil if there is no such
3747 property. */
3748 charpos = make_number (IT_STRING_CHARPOS (*it));
3749 prop = Fget_text_property (charpos, Qinvisible, it->string);
3750
3751 if (!NILP (prop)
3752 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3753 {
3754 handled = HANDLED_RECOMPUTE_PROPS;
3755
3756 /* Get the position at which the next change of the
3757 invisible text property can be found in IT->string.
3758 Value will be nil if the property value is the same for
3759 all the rest of IT->string. */
3760 XSETINT (limit, SCHARS (it->string));
3761 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3762 it->string, limit);
3763
3764 /* Text at current position is invisible. The next
3765 change in the property is at position end_charpos.
3766 Move IT's current position to that position. */
3767 if (INTEGERP (end_charpos)
3768 && XFASTINT (end_charpos) < XFASTINT (limit))
3769 {
3770 struct text_pos old;
3771 old = it->current.string_pos;
3772 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3773 compute_string_pos (&it->current.string_pos, old, it->string);
3774 }
3775 else
3776 {
3777 /* The rest of the string is invisible. If this is an
3778 overlay string, proceed with the next overlay string
3779 or whatever comes and return a character from there. */
3780 if (it->current.overlay_string_index >= 0)
3781 {
3782 next_overlay_string (it);
3783 /* Don't check for overlay strings when we just
3784 finished processing them. */
3785 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3786 }
3787 else
3788 {
3789 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3790 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3791 }
3792 }
3793 }
3794 }
3795 else
3796 {
3797 int invis_p;
3798 EMACS_INT newpos, next_stop, start_charpos, tem;
3799 Lisp_Object pos, prop, overlay;
3800
3801 /* First of all, is there invisible text at this position? */
3802 tem = start_charpos = IT_CHARPOS (*it);
3803 pos = make_number (tem);
3804 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3805 &overlay);
3806 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3807
3808 /* If we are on invisible text, skip over it. */
3809 if (invis_p && start_charpos < it->end_charpos)
3810 {
3811 /* Record whether we have to display an ellipsis for the
3812 invisible text. */
3813 int display_ellipsis_p = invis_p == 2;
3814
3815 handled = HANDLED_RECOMPUTE_PROPS;
3816
3817 /* Loop skipping over invisible text. The loop is left at
3818 ZV or with IT on the first char being visible again. */
3819 do
3820 {
3821 /* Try to skip some invisible text. Return value is the
3822 position reached which can be equal to where we start
3823 if there is nothing invisible there. This skips both
3824 over invisible text properties and overlays with
3825 invisible property. */
3826 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3827
3828 /* If we skipped nothing at all we weren't at invisible
3829 text in the first place. If everything to the end of
3830 the buffer was skipped, end the loop. */
3831 if (newpos == tem || newpos >= ZV)
3832 invis_p = 0;
3833 else
3834 {
3835 /* We skipped some characters but not necessarily
3836 all there are. Check if we ended up on visible
3837 text. Fget_char_property returns the property of
3838 the char before the given position, i.e. if we
3839 get invis_p = 0, this means that the char at
3840 newpos is visible. */
3841 pos = make_number (newpos);
3842 prop = Fget_char_property (pos, Qinvisible, it->window);
3843 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3844 }
3845
3846 /* If we ended up on invisible text, proceed to
3847 skip starting with next_stop. */
3848 if (invis_p)
3849 tem = next_stop;
3850
3851 /* If there are adjacent invisible texts, don't lose the
3852 second one's ellipsis. */
3853 if (invis_p == 2)
3854 display_ellipsis_p = 1;
3855 }
3856 while (invis_p);
3857
3858 /* The position newpos is now either ZV or on visible text. */
3859 if (it->bidi_p && newpos < ZV)
3860 {
3861 /* With bidi iteration, the region of invisible text
3862 could start and/or end in the middle of a non-base
3863 embedding level. Therefore, we need to skip
3864 invisible text using the bidi iterator, starting at
3865 IT's current position, until we find ourselves
3866 outside the invisible text. Skipping invisible text
3867 _after_ bidi iteration avoids affecting the visual
3868 order of the displayed text when invisible properties
3869 are added or removed. */
3870 if (it->bidi_it.first_elt)
3871 {
3872 /* If we were `reseat'ed to a new paragraph,
3873 determine the paragraph base direction. We need
3874 to do it now because next_element_from_buffer may
3875 not have a chance to do it, if we are going to
3876 skip any text at the beginning, which resets the
3877 FIRST_ELT flag. */
3878 bidi_paragraph_init (it->paragraph_embedding,
3879 &it->bidi_it, 1);
3880 }
3881 do
3882 {
3883 bidi_move_to_visually_next (&it->bidi_it);
3884 }
3885 while (it->stop_charpos <= it->bidi_it.charpos
3886 && it->bidi_it.charpos < newpos);
3887 IT_CHARPOS (*it) = it->bidi_it.charpos;
3888 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3889 /* If we overstepped NEWPOS, record its position in the
3890 iterator, so that we skip invisible text if later the
3891 bidi iteration lands us in the invisible region
3892 again. */
3893 if (IT_CHARPOS (*it) >= newpos)
3894 it->prev_stop = newpos;
3895 }
3896 else
3897 {
3898 IT_CHARPOS (*it) = newpos;
3899 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3900 }
3901
3902 /* If there are before-strings at the start of invisible
3903 text, and the text is invisible because of a text
3904 property, arrange to show before-strings because 20.x did
3905 it that way. (If the text is invisible because of an
3906 overlay property instead of a text property, this is
3907 already handled in the overlay code.) */
3908 if (NILP (overlay)
3909 && get_overlay_strings (it, it->stop_charpos))
3910 {
3911 handled = HANDLED_RECOMPUTE_PROPS;
3912 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3913 }
3914 else if (display_ellipsis_p)
3915 {
3916 /* Make sure that the glyphs of the ellipsis will get
3917 correct `charpos' values. If we would not update
3918 it->position here, the glyphs would belong to the
3919 last visible character _before_ the invisible
3920 text, which confuses `set_cursor_from_row'.
3921
3922 We use the last invisible position instead of the
3923 first because this way the cursor is always drawn on
3924 the first "." of the ellipsis, whenever PT is inside
3925 the invisible text. Otherwise the cursor would be
3926 placed _after_ the ellipsis when the point is after the
3927 first invisible character. */
3928 if (!STRINGP (it->object))
3929 {
3930 it->position.charpos = newpos - 1;
3931 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3932 }
3933 it->ellipsis_p = 1;
3934 /* Let the ellipsis display before
3935 considering any properties of the following char.
3936 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3937 handled = HANDLED_RETURN;
3938 }
3939 }
3940 }
3941
3942 return handled;
3943 }
3944
3945
3946 /* Make iterator IT return `...' next.
3947 Replaces LEN characters from buffer. */
3948
3949 static void
3950 setup_for_ellipsis (struct it *it, int len)
3951 {
3952 /* Use the display table definition for `...'. Invalid glyphs
3953 will be handled by the method returning elements from dpvec. */
3954 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3955 {
3956 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3957 it->dpvec = v->contents;
3958 it->dpend = v->contents + v->size;
3959 }
3960 else
3961 {
3962 /* Default `...'. */
3963 it->dpvec = default_invis_vector;
3964 it->dpend = default_invis_vector + 3;
3965 }
3966
3967 it->dpvec_char_len = len;
3968 it->current.dpvec_index = 0;
3969 it->dpvec_face_id = -1;
3970
3971 /* Remember the current face id in case glyphs specify faces.
3972 IT's face is restored in set_iterator_to_next.
3973 saved_face_id was set to preceding char's face in handle_stop. */
3974 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3975 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3976
3977 it->method = GET_FROM_DISPLAY_VECTOR;
3978 it->ellipsis_p = 1;
3979 }
3980
3981
3982 \f
3983 /***********************************************************************
3984 'display' property
3985 ***********************************************************************/
3986
3987 /* Set up iterator IT from `display' property at its current position.
3988 Called from handle_stop.
3989 We return HANDLED_RETURN if some part of the display property
3990 overrides the display of the buffer text itself.
3991 Otherwise we return HANDLED_NORMALLY. */
3992
3993 static enum prop_handled
3994 handle_display_prop (struct it *it)
3995 {
3996 Lisp_Object prop, object, overlay;
3997 struct text_pos *position;
3998 /* Nonzero if some property replaces the display of the text itself. */
3999 int display_replaced_p = 0;
4000
4001 if (STRINGP (it->string))
4002 {
4003 object = it->string;
4004 position = &it->current.string_pos;
4005 }
4006 else
4007 {
4008 XSETWINDOW (object, it->w);
4009 position = &it->current.pos;
4010 }
4011
4012 /* Reset those iterator values set from display property values. */
4013 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4014 it->space_width = Qnil;
4015 it->font_height = Qnil;
4016 it->voffset = 0;
4017
4018 /* We don't support recursive `display' properties, i.e. string
4019 values that have a string `display' property, that have a string
4020 `display' property etc. */
4021 if (!it->string_from_display_prop_p)
4022 it->area = TEXT_AREA;
4023
4024 prop = get_char_property_and_overlay (make_number (position->charpos),
4025 Qdisplay, object, &overlay);
4026 if (NILP (prop))
4027 return HANDLED_NORMALLY;
4028 /* Now OVERLAY is the overlay that gave us this property, or nil
4029 if it was a text property. */
4030
4031 if (!STRINGP (it->string))
4032 object = it->w->buffer;
4033
4034 if (CONSP (prop)
4035 /* Simple properties. */
4036 && !EQ (XCAR (prop), Qimage)
4037 && !EQ (XCAR (prop), Qspace)
4038 && !EQ (XCAR (prop), Qwhen)
4039 && !EQ (XCAR (prop), Qslice)
4040 && !EQ (XCAR (prop), Qspace_width)
4041 && !EQ (XCAR (prop), Qheight)
4042 && !EQ (XCAR (prop), Qraise)
4043 /* Marginal area specifications. */
4044 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
4045 && !EQ (XCAR (prop), Qleft_fringe)
4046 && !EQ (XCAR (prop), Qright_fringe)
4047 && !NILP (XCAR (prop)))
4048 {
4049 for (; CONSP (prop); prop = XCDR (prop))
4050 {
4051 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
4052 position, display_replaced_p))
4053 {
4054 display_replaced_p = 1;
4055 /* If some text in a string is replaced, `position' no
4056 longer points to the position of `object'. */
4057 if (STRINGP (object))
4058 break;
4059 }
4060 }
4061 }
4062 else if (VECTORP (prop))
4063 {
4064 int i;
4065 for (i = 0; i < ASIZE (prop); ++i)
4066 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4067 position, display_replaced_p))
4068 {
4069 display_replaced_p = 1;
4070 /* If some text in a string is replaced, `position' no
4071 longer points to the position of `object'. */
4072 if (STRINGP (object))
4073 break;
4074 }
4075 }
4076 else
4077 {
4078 if (handle_single_display_spec (it, prop, object, overlay,
4079 position, 0))
4080 display_replaced_p = 1;
4081 }
4082
4083 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4084 }
4085
4086
4087 /* Value is the position of the end of the `display' property starting
4088 at START_POS in OBJECT. */
4089
4090 static struct text_pos
4091 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4092 {
4093 Lisp_Object end;
4094 struct text_pos end_pos;
4095
4096 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4097 Qdisplay, object, Qnil);
4098 CHARPOS (end_pos) = XFASTINT (end);
4099 if (STRINGP (object))
4100 compute_string_pos (&end_pos, start_pos, it->string);
4101 else
4102 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4103
4104 return end_pos;
4105 }
4106
4107
4108 /* Set up IT from a single `display' specification PROP. OBJECT
4109 is the object in which the `display' property was found. *POSITION
4110 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4111 means that we previously saw a display specification which already
4112 replaced text display with something else, for example an image;
4113 we ignore such properties after the first one has been processed.
4114
4115 OVERLAY is the overlay this `display' property came from,
4116 or nil if it was a text property.
4117
4118 If PROP is a `space' or `image' specification, and in some other
4119 cases too, set *POSITION to the position where the `display'
4120 property ends.
4121
4122 Value is non-zero if something was found which replaces the display
4123 of buffer or string text. */
4124
4125 static int
4126 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4127 Lisp_Object overlay, struct text_pos *position,
4128 int display_replaced_before_p)
4129 {
4130 Lisp_Object form;
4131 Lisp_Object location, value;
4132 struct text_pos start_pos, save_pos;
4133 int valid_p;
4134
4135 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4136 If the result is non-nil, use VALUE instead of SPEC. */
4137 form = Qt;
4138 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4139 {
4140 spec = XCDR (spec);
4141 if (!CONSP (spec))
4142 return 0;
4143 form = XCAR (spec);
4144 spec = XCDR (spec);
4145 }
4146
4147 if (!NILP (form) && !EQ (form, Qt))
4148 {
4149 int count = SPECPDL_INDEX ();
4150 struct gcpro gcpro1;
4151
4152 /* Bind `object' to the object having the `display' property, a
4153 buffer or string. Bind `position' to the position in the
4154 object where the property was found, and `buffer-position'
4155 to the current position in the buffer. */
4156 specbind (Qobject, object);
4157 specbind (Qposition, make_number (CHARPOS (*position)));
4158 specbind (Qbuffer_position,
4159 make_number (STRINGP (object)
4160 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4161 GCPRO1 (form);
4162 form = safe_eval (form);
4163 UNGCPRO;
4164 unbind_to (count, Qnil);
4165 }
4166
4167 if (NILP (form))
4168 return 0;
4169
4170 /* Handle `(height HEIGHT)' specifications. */
4171 if (CONSP (spec)
4172 && EQ (XCAR (spec), Qheight)
4173 && CONSP (XCDR (spec)))
4174 {
4175 if (!FRAME_WINDOW_P (it->f))
4176 return 0;
4177
4178 it->font_height = XCAR (XCDR (spec));
4179 if (!NILP (it->font_height))
4180 {
4181 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4182 int new_height = -1;
4183
4184 if (CONSP (it->font_height)
4185 && (EQ (XCAR (it->font_height), Qplus)
4186 || EQ (XCAR (it->font_height), Qminus))
4187 && CONSP (XCDR (it->font_height))
4188 && INTEGERP (XCAR (XCDR (it->font_height))))
4189 {
4190 /* `(+ N)' or `(- N)' where N is an integer. */
4191 int steps = XINT (XCAR (XCDR (it->font_height)));
4192 if (EQ (XCAR (it->font_height), Qplus))
4193 steps = - steps;
4194 it->face_id = smaller_face (it->f, it->face_id, steps);
4195 }
4196 else if (FUNCTIONP (it->font_height))
4197 {
4198 /* Call function with current height as argument.
4199 Value is the new height. */
4200 Lisp_Object height;
4201 height = safe_call1 (it->font_height,
4202 face->lface[LFACE_HEIGHT_INDEX]);
4203 if (NUMBERP (height))
4204 new_height = XFLOATINT (height);
4205 }
4206 else if (NUMBERP (it->font_height))
4207 {
4208 /* Value is a multiple of the canonical char height. */
4209 struct face *face;
4210
4211 face = FACE_FROM_ID (it->f,
4212 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4213 new_height = (XFLOATINT (it->font_height)
4214 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4215 }
4216 else
4217 {
4218 /* Evaluate IT->font_height with `height' bound to the
4219 current specified height to get the new height. */
4220 int count = SPECPDL_INDEX ();
4221
4222 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4223 value = safe_eval (it->font_height);
4224 unbind_to (count, Qnil);
4225
4226 if (NUMBERP (value))
4227 new_height = XFLOATINT (value);
4228 }
4229
4230 if (new_height > 0)
4231 it->face_id = face_with_height (it->f, it->face_id, new_height);
4232 }
4233
4234 return 0;
4235 }
4236
4237 /* Handle `(space-width WIDTH)'. */
4238 if (CONSP (spec)
4239 && EQ (XCAR (spec), Qspace_width)
4240 && CONSP (XCDR (spec)))
4241 {
4242 if (!FRAME_WINDOW_P (it->f))
4243 return 0;
4244
4245 value = XCAR (XCDR (spec));
4246 if (NUMBERP (value) && XFLOATINT (value) > 0)
4247 it->space_width = value;
4248
4249 return 0;
4250 }
4251
4252 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4253 if (CONSP (spec)
4254 && EQ (XCAR (spec), Qslice))
4255 {
4256 Lisp_Object tem;
4257
4258 if (!FRAME_WINDOW_P (it->f))
4259 return 0;
4260
4261 if (tem = XCDR (spec), CONSP (tem))
4262 {
4263 it->slice.x = XCAR (tem);
4264 if (tem = XCDR (tem), CONSP (tem))
4265 {
4266 it->slice.y = XCAR (tem);
4267 if (tem = XCDR (tem), CONSP (tem))
4268 {
4269 it->slice.width = XCAR (tem);
4270 if (tem = XCDR (tem), CONSP (tem))
4271 it->slice.height = XCAR (tem);
4272 }
4273 }
4274 }
4275
4276 return 0;
4277 }
4278
4279 /* Handle `(raise FACTOR)'. */
4280 if (CONSP (spec)
4281 && EQ (XCAR (spec), Qraise)
4282 && CONSP (XCDR (spec)))
4283 {
4284 if (!FRAME_WINDOW_P (it->f))
4285 return 0;
4286
4287 #ifdef HAVE_WINDOW_SYSTEM
4288 value = XCAR (XCDR (spec));
4289 if (NUMBERP (value))
4290 {
4291 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4292 it->voffset = - (XFLOATINT (value)
4293 * (FONT_HEIGHT (face->font)));
4294 }
4295 #endif /* HAVE_WINDOW_SYSTEM */
4296
4297 return 0;
4298 }
4299
4300 /* Don't handle the other kinds of display specifications
4301 inside a string that we got from a `display' property. */
4302 if (it->string_from_display_prop_p)
4303 return 0;
4304
4305 /* Characters having this form of property are not displayed, so
4306 we have to find the end of the property. */
4307 start_pos = *position;
4308 *position = display_prop_end (it, object, start_pos);
4309 value = Qnil;
4310
4311 /* Stop the scan at that end position--we assume that all
4312 text properties change there. */
4313 it->stop_charpos = position->charpos;
4314
4315 /* Handle `(left-fringe BITMAP [FACE])'
4316 and `(right-fringe BITMAP [FACE])'. */
4317 if (CONSP (spec)
4318 && (EQ (XCAR (spec), Qleft_fringe)
4319 || EQ (XCAR (spec), Qright_fringe))
4320 && CONSP (XCDR (spec)))
4321 {
4322 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4323 int fringe_bitmap;
4324
4325 if (!FRAME_WINDOW_P (it->f))
4326 /* If we return here, POSITION has been advanced
4327 across the text with this property. */
4328 return 0;
4329
4330 #ifdef HAVE_WINDOW_SYSTEM
4331 value = XCAR (XCDR (spec));
4332 if (!SYMBOLP (value)
4333 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4334 /* If we return here, POSITION has been advanced
4335 across the text with this property. */
4336 return 0;
4337
4338 if (CONSP (XCDR (XCDR (spec))))
4339 {
4340 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4341 int face_id2 = lookup_derived_face (it->f, face_name,
4342 FRINGE_FACE_ID, 0);
4343 if (face_id2 >= 0)
4344 face_id = face_id2;
4345 }
4346
4347 /* Save current settings of IT so that we can restore them
4348 when we are finished with the glyph property value. */
4349
4350 save_pos = it->position;
4351 it->position = *position;
4352 push_it (it);
4353 it->position = save_pos;
4354
4355 it->area = TEXT_AREA;
4356 it->what = IT_IMAGE;
4357 it->image_id = -1; /* no image */
4358 it->position = start_pos;
4359 it->object = NILP (object) ? it->w->buffer : object;
4360 it->method = GET_FROM_IMAGE;
4361 it->from_overlay = Qnil;
4362 it->face_id = face_id;
4363
4364 /* Say that we haven't consumed the characters with
4365 `display' property yet. The call to pop_it in
4366 set_iterator_to_next will clean this up. */
4367 *position = start_pos;
4368
4369 if (EQ (XCAR (spec), Qleft_fringe))
4370 {
4371 it->left_user_fringe_bitmap = fringe_bitmap;
4372 it->left_user_fringe_face_id = face_id;
4373 }
4374 else
4375 {
4376 it->right_user_fringe_bitmap = fringe_bitmap;
4377 it->right_user_fringe_face_id = face_id;
4378 }
4379 #endif /* HAVE_WINDOW_SYSTEM */
4380 return 1;
4381 }
4382
4383 /* Prepare to handle `((margin left-margin) ...)',
4384 `((margin right-margin) ...)' and `((margin nil) ...)'
4385 prefixes for display specifications. */
4386 location = Qunbound;
4387 if (CONSP (spec) && CONSP (XCAR (spec)))
4388 {
4389 Lisp_Object tem;
4390
4391 value = XCDR (spec);
4392 if (CONSP (value))
4393 value = XCAR (value);
4394
4395 tem = XCAR (spec);
4396 if (EQ (XCAR (tem), Qmargin)
4397 && (tem = XCDR (tem),
4398 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4399 (NILP (tem)
4400 || EQ (tem, Qleft_margin)
4401 || EQ (tem, Qright_margin))))
4402 location = tem;
4403 }
4404
4405 if (EQ (location, Qunbound))
4406 {
4407 location = Qnil;
4408 value = spec;
4409 }
4410
4411 /* After this point, VALUE is the property after any
4412 margin prefix has been stripped. It must be a string,
4413 an image specification, or `(space ...)'.
4414
4415 LOCATION specifies where to display: `left-margin',
4416 `right-margin' or nil. */
4417
4418 valid_p = (STRINGP (value)
4419 #ifdef HAVE_WINDOW_SYSTEM
4420 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4421 #endif /* not HAVE_WINDOW_SYSTEM */
4422 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4423
4424 if (valid_p && !display_replaced_before_p)
4425 {
4426 /* Save current settings of IT so that we can restore them
4427 when we are finished with the glyph property value. */
4428 save_pos = it->position;
4429 it->position = *position;
4430 push_it (it);
4431 it->position = save_pos;
4432 it->from_overlay = overlay;
4433
4434 if (NILP (location))
4435 it->area = TEXT_AREA;
4436 else if (EQ (location, Qleft_margin))
4437 it->area = LEFT_MARGIN_AREA;
4438 else
4439 it->area = RIGHT_MARGIN_AREA;
4440
4441 if (STRINGP (value))
4442 {
4443 it->string = value;
4444 it->multibyte_p = STRING_MULTIBYTE (it->string);
4445 it->current.overlay_string_index = -1;
4446 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4447 it->end_charpos = it->string_nchars = SCHARS (it->string);
4448 it->method = GET_FROM_STRING;
4449 it->stop_charpos = 0;
4450 it->string_from_display_prop_p = 1;
4451 /* Say that we haven't consumed the characters with
4452 `display' property yet. The call to pop_it in
4453 set_iterator_to_next will clean this up. */
4454 if (BUFFERP (object))
4455 *position = start_pos;
4456 }
4457 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4458 {
4459 it->method = GET_FROM_STRETCH;
4460 it->object = value;
4461 *position = it->position = start_pos;
4462 }
4463 #ifdef HAVE_WINDOW_SYSTEM
4464 else
4465 {
4466 it->what = IT_IMAGE;
4467 it->image_id = lookup_image (it->f, value);
4468 it->position = start_pos;
4469 it->object = NILP (object) ? it->w->buffer : object;
4470 it->method = GET_FROM_IMAGE;
4471
4472 /* Say that we haven't consumed the characters with
4473 `display' property yet. The call to pop_it in
4474 set_iterator_to_next will clean this up. */
4475 *position = start_pos;
4476 }
4477 #endif /* HAVE_WINDOW_SYSTEM */
4478
4479 return 1;
4480 }
4481
4482 /* Invalid property or property not supported. Restore
4483 POSITION to what it was before. */
4484 *position = start_pos;
4485 return 0;
4486 }
4487
4488
4489 /* Check if SPEC is a display sub-property value whose text should be
4490 treated as intangible. */
4491
4492 static int
4493 single_display_spec_intangible_p (Lisp_Object prop)
4494 {
4495 /* Skip over `when FORM'. */
4496 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4497 {
4498 prop = XCDR (prop);
4499 if (!CONSP (prop))
4500 return 0;
4501 prop = XCDR (prop);
4502 }
4503
4504 if (STRINGP (prop))
4505 return 1;
4506
4507 if (!CONSP (prop))
4508 return 0;
4509
4510 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4511 we don't need to treat text as intangible. */
4512 if (EQ (XCAR (prop), Qmargin))
4513 {
4514 prop = XCDR (prop);
4515 if (!CONSP (prop))
4516 return 0;
4517
4518 prop = XCDR (prop);
4519 if (!CONSP (prop)
4520 || EQ (XCAR (prop), Qleft_margin)
4521 || EQ (XCAR (prop), Qright_margin))
4522 return 0;
4523 }
4524
4525 return (CONSP (prop)
4526 && (EQ (XCAR (prop), Qimage)
4527 || EQ (XCAR (prop), Qspace)));
4528 }
4529
4530
4531 /* Check if PROP is a display property value whose text should be
4532 treated as intangible. */
4533
4534 int
4535 display_prop_intangible_p (Lisp_Object prop)
4536 {
4537 if (CONSP (prop)
4538 && CONSP (XCAR (prop))
4539 && !EQ (Qmargin, XCAR (XCAR (prop))))
4540 {
4541 /* A list of sub-properties. */
4542 while (CONSP (prop))
4543 {
4544 if (single_display_spec_intangible_p (XCAR (prop)))
4545 return 1;
4546 prop = XCDR (prop);
4547 }
4548 }
4549 else if (VECTORP (prop))
4550 {
4551 /* A vector of sub-properties. */
4552 int i;
4553 for (i = 0; i < ASIZE (prop); ++i)
4554 if (single_display_spec_intangible_p (AREF (prop, i)))
4555 return 1;
4556 }
4557 else
4558 return single_display_spec_intangible_p (prop);
4559
4560 return 0;
4561 }
4562
4563
4564 /* Return 1 if PROP is a display sub-property value containing STRING. */
4565
4566 static int
4567 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4568 {
4569 if (EQ (string, prop))
4570 return 1;
4571
4572 /* Skip over `when FORM'. */
4573 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4574 {
4575 prop = XCDR (prop);
4576 if (!CONSP (prop))
4577 return 0;
4578 prop = XCDR (prop);
4579 }
4580
4581 if (CONSP (prop))
4582 /* Skip over `margin LOCATION'. */
4583 if (EQ (XCAR (prop), Qmargin))
4584 {
4585 prop = XCDR (prop);
4586 if (!CONSP (prop))
4587 return 0;
4588
4589 prop = XCDR (prop);
4590 if (!CONSP (prop))
4591 return 0;
4592 }
4593
4594 return CONSP (prop) && EQ (XCAR (prop), string);
4595 }
4596
4597
4598 /* Return 1 if STRING appears in the `display' property PROP. */
4599
4600 static int
4601 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4602 {
4603 if (CONSP (prop)
4604 && CONSP (XCAR (prop))
4605 && !EQ (Qmargin, XCAR (XCAR (prop))))
4606 {
4607 /* A list of sub-properties. */
4608 while (CONSP (prop))
4609 {
4610 if (single_display_spec_string_p (XCAR (prop), string))
4611 return 1;
4612 prop = XCDR (prop);
4613 }
4614 }
4615 else if (VECTORP (prop))
4616 {
4617 /* A vector of sub-properties. */
4618 int i;
4619 for (i = 0; i < ASIZE (prop); ++i)
4620 if (single_display_spec_string_p (AREF (prop, i), string))
4621 return 1;
4622 }
4623 else
4624 return single_display_spec_string_p (prop, string);
4625
4626 return 0;
4627 }
4628
4629 /* Look for STRING in overlays and text properties in W's buffer,
4630 between character positions FROM and TO (excluding TO).
4631 BACK_P non-zero means look back (in this case, TO is supposed to be
4632 less than FROM).
4633 Value is the first character position where STRING was found, or
4634 zero if it wasn't found before hitting TO.
4635
4636 W's buffer must be current.
4637
4638 This function may only use code that doesn't eval because it is
4639 called asynchronously from note_mouse_highlight. */
4640
4641 static EMACS_INT
4642 string_buffer_position_lim (struct window *w, Lisp_Object string,
4643 EMACS_INT from, EMACS_INT to, int back_p)
4644 {
4645 Lisp_Object limit, prop, pos;
4646 int found = 0;
4647
4648 pos = make_number (from);
4649
4650 if (!back_p) /* looking forward */
4651 {
4652 limit = make_number (min (to, ZV));
4653 while (!found && !EQ (pos, limit))
4654 {
4655 prop = Fget_char_property (pos, Qdisplay, Qnil);
4656 if (!NILP (prop) && display_prop_string_p (prop, string))
4657 found = 1;
4658 else
4659 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4660 limit);
4661 }
4662 }
4663 else /* looking back */
4664 {
4665 limit = make_number (max (to, BEGV));
4666 while (!found && !EQ (pos, limit))
4667 {
4668 prop = Fget_char_property (pos, Qdisplay, Qnil);
4669 if (!NILP (prop) && display_prop_string_p (prop, string))
4670 found = 1;
4671 else
4672 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4673 limit);
4674 }
4675 }
4676
4677 return found ? XINT (pos) : 0;
4678 }
4679
4680 /* Determine which buffer position in W's buffer STRING comes from.
4681 AROUND_CHARPOS is an approximate position where it could come from.
4682 Value is the buffer position or 0 if it couldn't be determined.
4683
4684 W's buffer must be current.
4685
4686 This function is necessary because we don't record buffer positions
4687 in glyphs generated from strings (to keep struct glyph small).
4688 This function may only use code that doesn't eval because it is
4689 called asynchronously from note_mouse_highlight. */
4690
4691 EMACS_INT
4692 string_buffer_position (struct window *w, Lisp_Object string, EMACS_INT around_charpos)
4693 {
4694 const int MAX_DISTANCE = 1000;
4695 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4696 around_charpos + MAX_DISTANCE,
4697 0);
4698
4699 if (!found)
4700 found = string_buffer_position_lim (w, string, around_charpos,
4701 around_charpos - MAX_DISTANCE, 1);
4702 return found;
4703 }
4704
4705
4706 \f
4707 /***********************************************************************
4708 `composition' property
4709 ***********************************************************************/
4710
4711 /* Set up iterator IT from `composition' property at its current
4712 position. Called from handle_stop. */
4713
4714 static enum prop_handled
4715 handle_composition_prop (struct it *it)
4716 {
4717 Lisp_Object prop, string;
4718 EMACS_INT pos, pos_byte, start, end;
4719
4720 if (STRINGP (it->string))
4721 {
4722 unsigned char *s;
4723
4724 pos = IT_STRING_CHARPOS (*it);
4725 pos_byte = IT_STRING_BYTEPOS (*it);
4726 string = it->string;
4727 s = SDATA (string) + pos_byte;
4728 it->c = STRING_CHAR (s);
4729 }
4730 else
4731 {
4732 pos = IT_CHARPOS (*it);
4733 pos_byte = IT_BYTEPOS (*it);
4734 string = Qnil;
4735 it->c = FETCH_CHAR (pos_byte);
4736 }
4737
4738 /* If there's a valid composition and point is not inside of the
4739 composition (in the case that the composition is from the current
4740 buffer), draw a glyph composed from the composition components. */
4741 if (find_composition (pos, -1, &start, &end, &prop, string)
4742 && COMPOSITION_VALID_P (start, end, prop)
4743 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4744 {
4745 if (start != pos)
4746 {
4747 if (STRINGP (it->string))
4748 pos_byte = string_char_to_byte (it->string, start);
4749 else
4750 pos_byte = CHAR_TO_BYTE (start);
4751 }
4752 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4753 prop, string);
4754
4755 if (it->cmp_it.id >= 0)
4756 {
4757 it->cmp_it.ch = -1;
4758 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4759 it->cmp_it.nglyphs = -1;
4760 }
4761 }
4762
4763 return HANDLED_NORMALLY;
4764 }
4765
4766
4767 \f
4768 /***********************************************************************
4769 Overlay strings
4770 ***********************************************************************/
4771
4772 /* The following structure is used to record overlay strings for
4773 later sorting in load_overlay_strings. */
4774
4775 struct overlay_entry
4776 {
4777 Lisp_Object overlay;
4778 Lisp_Object string;
4779 int priority;
4780 int after_string_p;
4781 };
4782
4783
4784 /* Set up iterator IT from overlay strings at its current position.
4785 Called from handle_stop. */
4786
4787 static enum prop_handled
4788 handle_overlay_change (struct it *it)
4789 {
4790 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4791 return HANDLED_RECOMPUTE_PROPS;
4792 else
4793 return HANDLED_NORMALLY;
4794 }
4795
4796
4797 /* Set up the next overlay string for delivery by IT, if there is an
4798 overlay string to deliver. Called by set_iterator_to_next when the
4799 end of the current overlay string is reached. If there are more
4800 overlay strings to display, IT->string and
4801 IT->current.overlay_string_index are set appropriately here.
4802 Otherwise IT->string is set to nil. */
4803
4804 static void
4805 next_overlay_string (struct it *it)
4806 {
4807 ++it->current.overlay_string_index;
4808 if (it->current.overlay_string_index == it->n_overlay_strings)
4809 {
4810 /* No more overlay strings. Restore IT's settings to what
4811 they were before overlay strings were processed, and
4812 continue to deliver from current_buffer. */
4813
4814 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4815 pop_it (it);
4816 xassert (it->sp > 0
4817 || (NILP (it->string)
4818 && it->method == GET_FROM_BUFFER
4819 && it->stop_charpos >= BEGV
4820 && it->stop_charpos <= it->end_charpos));
4821 it->current.overlay_string_index = -1;
4822 it->n_overlay_strings = 0;
4823
4824 /* If we're at the end of the buffer, record that we have
4825 processed the overlay strings there already, so that
4826 next_element_from_buffer doesn't try it again. */
4827 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4828 it->overlay_strings_at_end_processed_p = 1;
4829 }
4830 else
4831 {
4832 /* There are more overlay strings to process. If
4833 IT->current.overlay_string_index has advanced to a position
4834 where we must load IT->overlay_strings with more strings, do
4835 it. */
4836 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4837
4838 if (it->current.overlay_string_index && i == 0)
4839 load_overlay_strings (it, 0);
4840
4841 /* Initialize IT to deliver display elements from the overlay
4842 string. */
4843 it->string = it->overlay_strings[i];
4844 it->multibyte_p = STRING_MULTIBYTE (it->string);
4845 SET_TEXT_POS (it->current.string_pos, 0, 0);
4846 it->method = GET_FROM_STRING;
4847 it->stop_charpos = 0;
4848 if (it->cmp_it.stop_pos >= 0)
4849 it->cmp_it.stop_pos = 0;
4850 }
4851
4852 CHECK_IT (it);
4853 }
4854
4855
4856 /* Compare two overlay_entry structures E1 and E2. Used as a
4857 comparison function for qsort in load_overlay_strings. Overlay
4858 strings for the same position are sorted so that
4859
4860 1. All after-strings come in front of before-strings, except
4861 when they come from the same overlay.
4862
4863 2. Within after-strings, strings are sorted so that overlay strings
4864 from overlays with higher priorities come first.
4865
4866 2. Within before-strings, strings are sorted so that overlay
4867 strings from overlays with higher priorities come last.
4868
4869 Value is analogous to strcmp. */
4870
4871
4872 static int
4873 compare_overlay_entries (const void *e1, const void *e2)
4874 {
4875 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4876 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4877 int result;
4878
4879 if (entry1->after_string_p != entry2->after_string_p)
4880 {
4881 /* Let after-strings appear in front of before-strings if
4882 they come from different overlays. */
4883 if (EQ (entry1->overlay, entry2->overlay))
4884 result = entry1->after_string_p ? 1 : -1;
4885 else
4886 result = entry1->after_string_p ? -1 : 1;
4887 }
4888 else if (entry1->after_string_p)
4889 /* After-strings sorted in order of decreasing priority. */
4890 result = entry2->priority - entry1->priority;
4891 else
4892 /* Before-strings sorted in order of increasing priority. */
4893 result = entry1->priority - entry2->priority;
4894
4895 return result;
4896 }
4897
4898
4899 /* Load the vector IT->overlay_strings with overlay strings from IT's
4900 current buffer position, or from CHARPOS if that is > 0. Set
4901 IT->n_overlays to the total number of overlay strings found.
4902
4903 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4904 a time. On entry into load_overlay_strings,
4905 IT->current.overlay_string_index gives the number of overlay
4906 strings that have already been loaded by previous calls to this
4907 function.
4908
4909 IT->add_overlay_start contains an additional overlay start
4910 position to consider for taking overlay strings from, if non-zero.
4911 This position comes into play when the overlay has an `invisible'
4912 property, and both before and after-strings. When we've skipped to
4913 the end of the overlay, because of its `invisible' property, we
4914 nevertheless want its before-string to appear.
4915 IT->add_overlay_start will contain the overlay start position
4916 in this case.
4917
4918 Overlay strings are sorted so that after-string strings come in
4919 front of before-string strings. Within before and after-strings,
4920 strings are sorted by overlay priority. See also function
4921 compare_overlay_entries. */
4922
4923 static void
4924 load_overlay_strings (struct it *it, EMACS_INT charpos)
4925 {
4926 Lisp_Object overlay, window, str, invisible;
4927 struct Lisp_Overlay *ov;
4928 EMACS_INT start, end;
4929 int size = 20;
4930 int n = 0, i, j, invis_p;
4931 struct overlay_entry *entries
4932 = (struct overlay_entry *) alloca (size * sizeof *entries);
4933
4934 if (charpos <= 0)
4935 charpos = IT_CHARPOS (*it);
4936
4937 /* Append the overlay string STRING of overlay OVERLAY to vector
4938 `entries' which has size `size' and currently contains `n'
4939 elements. AFTER_P non-zero means STRING is an after-string of
4940 OVERLAY. */
4941 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4942 do \
4943 { \
4944 Lisp_Object priority; \
4945 \
4946 if (n == size) \
4947 { \
4948 int new_size = 2 * size; \
4949 struct overlay_entry *old = entries; \
4950 entries = \
4951 (struct overlay_entry *) alloca (new_size \
4952 * sizeof *entries); \
4953 memcpy (entries, old, size * sizeof *entries); \
4954 size = new_size; \
4955 } \
4956 \
4957 entries[n].string = (STRING); \
4958 entries[n].overlay = (OVERLAY); \
4959 priority = Foverlay_get ((OVERLAY), Qpriority); \
4960 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4961 entries[n].after_string_p = (AFTER_P); \
4962 ++n; \
4963 } \
4964 while (0)
4965
4966 /* Process overlay before the overlay center. */
4967 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4968 {
4969 XSETMISC (overlay, ov);
4970 xassert (OVERLAYP (overlay));
4971 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4972 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4973
4974 if (end < charpos)
4975 break;
4976
4977 /* Skip this overlay if it doesn't start or end at IT's current
4978 position. */
4979 if (end != charpos && start != charpos)
4980 continue;
4981
4982 /* Skip this overlay if it doesn't apply to IT->w. */
4983 window = Foverlay_get (overlay, Qwindow);
4984 if (WINDOWP (window) && XWINDOW (window) != it->w)
4985 continue;
4986
4987 /* If the text ``under'' the overlay is invisible, both before-
4988 and after-strings from this overlay are visible; start and
4989 end position are indistinguishable. */
4990 invisible = Foverlay_get (overlay, Qinvisible);
4991 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4992
4993 /* If overlay has a non-empty before-string, record it. */
4994 if ((start == charpos || (end == charpos && invis_p))
4995 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4996 && SCHARS (str))
4997 RECORD_OVERLAY_STRING (overlay, str, 0);
4998
4999 /* If overlay has a non-empty after-string, record it. */
5000 if ((end == charpos || (start == charpos && invis_p))
5001 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5002 && SCHARS (str))
5003 RECORD_OVERLAY_STRING (overlay, str, 1);
5004 }
5005
5006 /* Process overlays after the overlay center. */
5007 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5008 {
5009 XSETMISC (overlay, ov);
5010 xassert (OVERLAYP (overlay));
5011 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5012 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5013
5014 if (start > charpos)
5015 break;
5016
5017 /* Skip this overlay if it doesn't start or end at IT's current
5018 position. */
5019 if (end != charpos && start != charpos)
5020 continue;
5021
5022 /* Skip this overlay if it doesn't apply to IT->w. */
5023 window = Foverlay_get (overlay, Qwindow);
5024 if (WINDOWP (window) && XWINDOW (window) != it->w)
5025 continue;
5026
5027 /* If the text ``under'' the overlay is invisible, it has a zero
5028 dimension, and both before- and after-strings apply. */
5029 invisible = Foverlay_get (overlay, Qinvisible);
5030 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5031
5032 /* If overlay has a non-empty before-string, record it. */
5033 if ((start == charpos || (end == charpos && invis_p))
5034 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5035 && SCHARS (str))
5036 RECORD_OVERLAY_STRING (overlay, str, 0);
5037
5038 /* If overlay has a non-empty after-string, record it. */
5039 if ((end == charpos || (start == charpos && invis_p))
5040 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5041 && SCHARS (str))
5042 RECORD_OVERLAY_STRING (overlay, str, 1);
5043 }
5044
5045 #undef RECORD_OVERLAY_STRING
5046
5047 /* Sort entries. */
5048 if (n > 1)
5049 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5050
5051 /* Record the total number of strings to process. */
5052 it->n_overlay_strings = n;
5053
5054 /* IT->current.overlay_string_index is the number of overlay strings
5055 that have already been consumed by IT. Copy some of the
5056 remaining overlay strings to IT->overlay_strings. */
5057 i = 0;
5058 j = it->current.overlay_string_index;
5059 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5060 {
5061 it->overlay_strings[i] = entries[j].string;
5062 it->string_overlays[i++] = entries[j++].overlay;
5063 }
5064
5065 CHECK_IT (it);
5066 }
5067
5068
5069 /* Get the first chunk of overlay strings at IT's current buffer
5070 position, or at CHARPOS if that is > 0. Value is non-zero if at
5071 least one overlay string was found. */
5072
5073 static int
5074 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5075 {
5076 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5077 process. This fills IT->overlay_strings with strings, and sets
5078 IT->n_overlay_strings to the total number of strings to process.
5079 IT->pos.overlay_string_index has to be set temporarily to zero
5080 because load_overlay_strings needs this; it must be set to -1
5081 when no overlay strings are found because a zero value would
5082 indicate a position in the first overlay string. */
5083 it->current.overlay_string_index = 0;
5084 load_overlay_strings (it, charpos);
5085
5086 /* If we found overlay strings, set up IT to deliver display
5087 elements from the first one. Otherwise set up IT to deliver
5088 from current_buffer. */
5089 if (it->n_overlay_strings)
5090 {
5091 /* Make sure we know settings in current_buffer, so that we can
5092 restore meaningful values when we're done with the overlay
5093 strings. */
5094 if (compute_stop_p)
5095 compute_stop_pos (it);
5096 xassert (it->face_id >= 0);
5097
5098 /* Save IT's settings. They are restored after all overlay
5099 strings have been processed. */
5100 xassert (!compute_stop_p || it->sp == 0);
5101
5102 /* When called from handle_stop, there might be an empty display
5103 string loaded. In that case, don't bother saving it. */
5104 if (!STRINGP (it->string) || SCHARS (it->string))
5105 push_it (it);
5106
5107 /* Set up IT to deliver display elements from the first overlay
5108 string. */
5109 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5110 it->string = it->overlay_strings[0];
5111 it->from_overlay = Qnil;
5112 it->stop_charpos = 0;
5113 xassert (STRINGP (it->string));
5114 it->end_charpos = SCHARS (it->string);
5115 it->multibyte_p = STRING_MULTIBYTE (it->string);
5116 it->method = GET_FROM_STRING;
5117 return 1;
5118 }
5119
5120 it->current.overlay_string_index = -1;
5121 return 0;
5122 }
5123
5124 static int
5125 get_overlay_strings (struct it *it, EMACS_INT charpos)
5126 {
5127 it->string = Qnil;
5128 it->method = GET_FROM_BUFFER;
5129
5130 (void) get_overlay_strings_1 (it, charpos, 1);
5131
5132 CHECK_IT (it);
5133
5134 /* Value is non-zero if we found at least one overlay string. */
5135 return STRINGP (it->string);
5136 }
5137
5138
5139 \f
5140 /***********************************************************************
5141 Saving and restoring state
5142 ***********************************************************************/
5143
5144 /* Save current settings of IT on IT->stack. Called, for example,
5145 before setting up IT for an overlay string, to be able to restore
5146 IT's settings to what they were after the overlay string has been
5147 processed. */
5148
5149 static void
5150 push_it (struct it *it)
5151 {
5152 struct iterator_stack_entry *p;
5153
5154 xassert (it->sp < IT_STACK_SIZE);
5155 p = it->stack + it->sp;
5156
5157 p->stop_charpos = it->stop_charpos;
5158 p->prev_stop = it->prev_stop;
5159 p->base_level_stop = it->base_level_stop;
5160 p->cmp_it = it->cmp_it;
5161 xassert (it->face_id >= 0);
5162 p->face_id = it->face_id;
5163 p->string = it->string;
5164 p->method = it->method;
5165 p->from_overlay = it->from_overlay;
5166 switch (p->method)
5167 {
5168 case GET_FROM_IMAGE:
5169 p->u.image.object = it->object;
5170 p->u.image.image_id = it->image_id;
5171 p->u.image.slice = it->slice;
5172 break;
5173 case GET_FROM_STRETCH:
5174 p->u.stretch.object = it->object;
5175 break;
5176 }
5177 p->position = it->position;
5178 p->current = it->current;
5179 p->end_charpos = it->end_charpos;
5180 p->string_nchars = it->string_nchars;
5181 p->area = it->area;
5182 p->multibyte_p = it->multibyte_p;
5183 p->avoid_cursor_p = it->avoid_cursor_p;
5184 p->space_width = it->space_width;
5185 p->font_height = it->font_height;
5186 p->voffset = it->voffset;
5187 p->string_from_display_prop_p = it->string_from_display_prop_p;
5188 p->display_ellipsis_p = 0;
5189 p->line_wrap = it->line_wrap;
5190 ++it->sp;
5191 }
5192
5193 static void
5194 iterate_out_of_display_property (struct it *it)
5195 {
5196 /* Maybe initialize paragraph direction. If we are at the beginning
5197 of a new paragraph, next_element_from_buffer may not have a
5198 chance to do that. */
5199 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
5200 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5201 /* prev_stop can be zero, so check against BEGV as well. */
5202 while (it->bidi_it.charpos >= BEGV
5203 && it->prev_stop <= it->bidi_it.charpos
5204 && it->bidi_it.charpos < CHARPOS (it->position))
5205 bidi_move_to_visually_next (&it->bidi_it);
5206 /* Record the stop_pos we just crossed, for when we cross it
5207 back, maybe. */
5208 if (it->bidi_it.charpos > CHARPOS (it->position))
5209 it->prev_stop = CHARPOS (it->position);
5210 /* If we ended up not where pop_it put us, resync IT's
5211 positional members with the bidi iterator. */
5212 if (it->bidi_it.charpos != CHARPOS (it->position))
5213 {
5214 SET_TEXT_POS (it->position,
5215 it->bidi_it.charpos, it->bidi_it.bytepos);
5216 it->current.pos = it->position;
5217 }
5218 }
5219
5220 /* Restore IT's settings from IT->stack. Called, for example, when no
5221 more overlay strings must be processed, and we return to delivering
5222 display elements from a buffer, or when the end of a string from a
5223 `display' property is reached and we return to delivering display
5224 elements from an overlay string, or from a buffer. */
5225
5226 static void
5227 pop_it (struct it *it)
5228 {
5229 struct iterator_stack_entry *p;
5230
5231 xassert (it->sp > 0);
5232 --it->sp;
5233 p = it->stack + it->sp;
5234 it->stop_charpos = p->stop_charpos;
5235 it->prev_stop = p->prev_stop;
5236 it->base_level_stop = p->base_level_stop;
5237 it->cmp_it = p->cmp_it;
5238 it->face_id = p->face_id;
5239 it->current = p->current;
5240 it->position = p->position;
5241 it->string = p->string;
5242 it->from_overlay = p->from_overlay;
5243 if (NILP (it->string))
5244 SET_TEXT_POS (it->current.string_pos, -1, -1);
5245 it->method = p->method;
5246 switch (it->method)
5247 {
5248 case GET_FROM_IMAGE:
5249 it->image_id = p->u.image.image_id;
5250 it->object = p->u.image.object;
5251 it->slice = p->u.image.slice;
5252 break;
5253 case GET_FROM_STRETCH:
5254 it->object = p->u.comp.object;
5255 break;
5256 case GET_FROM_BUFFER:
5257 it->object = it->w->buffer;
5258 if (it->bidi_p)
5259 {
5260 /* Bidi-iterate until we get out of the portion of text, if
5261 any, covered by a `display' text property or an overlay
5262 with `display' property. (We cannot just jump there,
5263 because the internal coherency of the bidi iterator state
5264 can not be preserved across such jumps.) We also must
5265 determine the paragraph base direction if the overlay we
5266 just processed is at the beginning of a new
5267 paragraph. */
5268 iterate_out_of_display_property (it);
5269 }
5270 break;
5271 case GET_FROM_STRING:
5272 it->object = it->string;
5273 break;
5274 case GET_FROM_DISPLAY_VECTOR:
5275 if (it->s)
5276 it->method = GET_FROM_C_STRING;
5277 else if (STRINGP (it->string))
5278 it->method = GET_FROM_STRING;
5279 else
5280 {
5281 it->method = GET_FROM_BUFFER;
5282 it->object = it->w->buffer;
5283 }
5284 }
5285 it->end_charpos = p->end_charpos;
5286 it->string_nchars = p->string_nchars;
5287 it->area = p->area;
5288 it->multibyte_p = p->multibyte_p;
5289 it->avoid_cursor_p = p->avoid_cursor_p;
5290 it->space_width = p->space_width;
5291 it->font_height = p->font_height;
5292 it->voffset = p->voffset;
5293 it->string_from_display_prop_p = p->string_from_display_prop_p;
5294 it->line_wrap = p->line_wrap;
5295 }
5296
5297
5298 \f
5299 /***********************************************************************
5300 Moving over lines
5301 ***********************************************************************/
5302
5303 /* Set IT's current position to the previous line start. */
5304
5305 static void
5306 back_to_previous_line_start (struct it *it)
5307 {
5308 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5309 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5310 }
5311
5312
5313 /* Move IT to the next line start.
5314
5315 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5316 we skipped over part of the text (as opposed to moving the iterator
5317 continuously over the text). Otherwise, don't change the value
5318 of *SKIPPED_P.
5319
5320 Newlines may come from buffer text, overlay strings, or strings
5321 displayed via the `display' property. That's the reason we can't
5322 simply use find_next_newline_no_quit.
5323
5324 Note that this function may not skip over invisible text that is so
5325 because of text properties and immediately follows a newline. If
5326 it would, function reseat_at_next_visible_line_start, when called
5327 from set_iterator_to_next, would effectively make invisible
5328 characters following a newline part of the wrong glyph row, which
5329 leads to wrong cursor motion. */
5330
5331 static int
5332 forward_to_next_line_start (struct it *it, int *skipped_p)
5333 {
5334 int old_selective, newline_found_p, n;
5335 const int MAX_NEWLINE_DISTANCE = 500;
5336
5337 /* If already on a newline, just consume it to avoid unintended
5338 skipping over invisible text below. */
5339 if (it->what == IT_CHARACTER
5340 && it->c == '\n'
5341 && CHARPOS (it->position) == IT_CHARPOS (*it))
5342 {
5343 set_iterator_to_next (it, 0);
5344 it->c = 0;
5345 return 1;
5346 }
5347
5348 /* Don't handle selective display in the following. It's (a)
5349 unnecessary because it's done by the caller, and (b) leads to an
5350 infinite recursion because next_element_from_ellipsis indirectly
5351 calls this function. */
5352 old_selective = it->selective;
5353 it->selective = 0;
5354
5355 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5356 from buffer text. */
5357 for (n = newline_found_p = 0;
5358 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5359 n += STRINGP (it->string) ? 0 : 1)
5360 {
5361 if (!get_next_display_element (it))
5362 return 0;
5363 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5364 set_iterator_to_next (it, 0);
5365 }
5366
5367 /* If we didn't find a newline near enough, see if we can use a
5368 short-cut. */
5369 if (!newline_found_p)
5370 {
5371 EMACS_INT start = IT_CHARPOS (*it);
5372 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5373 Lisp_Object pos;
5374
5375 xassert (!STRINGP (it->string));
5376
5377 /* If there isn't any `display' property in sight, and no
5378 overlays, we can just use the position of the newline in
5379 buffer text. */
5380 if (it->stop_charpos >= limit
5381 || ((pos = Fnext_single_property_change (make_number (start),
5382 Qdisplay,
5383 Qnil, make_number (limit)),
5384 NILP (pos))
5385 && next_overlay_change (start) == ZV))
5386 {
5387 IT_CHARPOS (*it) = limit;
5388 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5389 *skipped_p = newline_found_p = 1;
5390 }
5391 else
5392 {
5393 while (get_next_display_element (it)
5394 && !newline_found_p)
5395 {
5396 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5397 set_iterator_to_next (it, 0);
5398 }
5399 }
5400 }
5401
5402 it->selective = old_selective;
5403 return newline_found_p;
5404 }
5405
5406
5407 /* Set IT's current position to the previous visible line start. Skip
5408 invisible text that is so either due to text properties or due to
5409 selective display. Caution: this does not change IT->current_x and
5410 IT->hpos. */
5411
5412 static void
5413 back_to_previous_visible_line_start (struct it *it)
5414 {
5415 while (IT_CHARPOS (*it) > BEGV)
5416 {
5417 back_to_previous_line_start (it);
5418
5419 if (IT_CHARPOS (*it) <= BEGV)
5420 break;
5421
5422 /* If selective > 0, then lines indented more than its value are
5423 invisible. */
5424 if (it->selective > 0
5425 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5426 (double) it->selective)) /* iftc */
5427 continue;
5428
5429 /* Check the newline before point for invisibility. */
5430 {
5431 Lisp_Object prop;
5432 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5433 Qinvisible, it->window);
5434 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5435 continue;
5436 }
5437
5438 if (IT_CHARPOS (*it) <= BEGV)
5439 break;
5440
5441 {
5442 struct it it2;
5443 EMACS_INT pos;
5444 EMACS_INT beg, end;
5445 Lisp_Object val, overlay;
5446
5447 /* If newline is part of a composition, continue from start of composition */
5448 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5449 && beg < IT_CHARPOS (*it))
5450 goto replaced;
5451
5452 /* If newline is replaced by a display property, find start of overlay
5453 or interval and continue search from that point. */
5454 it2 = *it;
5455 pos = --IT_CHARPOS (it2);
5456 --IT_BYTEPOS (it2);
5457 it2.sp = 0;
5458 it2.string_from_display_prop_p = 0;
5459 if (handle_display_prop (&it2) == HANDLED_RETURN
5460 && !NILP (val = get_char_property_and_overlay
5461 (make_number (pos), Qdisplay, Qnil, &overlay))
5462 && (OVERLAYP (overlay)
5463 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5464 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5465 goto replaced;
5466
5467 /* Newline is not replaced by anything -- so we are done. */
5468 break;
5469
5470 replaced:
5471 if (beg < BEGV)
5472 beg = BEGV;
5473 IT_CHARPOS (*it) = beg;
5474 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5475 }
5476 }
5477
5478 it->continuation_lines_width = 0;
5479
5480 xassert (IT_CHARPOS (*it) >= BEGV);
5481 xassert (IT_CHARPOS (*it) == BEGV
5482 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5483 CHECK_IT (it);
5484 }
5485
5486
5487 /* Reseat iterator IT at the previous visible line start. Skip
5488 invisible text that is so either due to text properties or due to
5489 selective display. At the end, update IT's overlay information,
5490 face information etc. */
5491
5492 void
5493 reseat_at_previous_visible_line_start (struct it *it)
5494 {
5495 back_to_previous_visible_line_start (it);
5496 reseat (it, it->current.pos, 1);
5497 CHECK_IT (it);
5498 }
5499
5500
5501 /* Reseat iterator IT on the next visible line start in the current
5502 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5503 preceding the line start. Skip over invisible text that is so
5504 because of selective display. Compute faces, overlays etc at the
5505 new position. Note that this function does not skip over text that
5506 is invisible because of text properties. */
5507
5508 static void
5509 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5510 {
5511 int newline_found_p, skipped_p = 0;
5512
5513 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5514
5515 /* Skip over lines that are invisible because they are indented
5516 more than the value of IT->selective. */
5517 if (it->selective > 0)
5518 while (IT_CHARPOS (*it) < ZV
5519 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5520 (double) it->selective)) /* iftc */
5521 {
5522 xassert (IT_BYTEPOS (*it) == BEGV
5523 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5524 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5525 }
5526
5527 /* Position on the newline if that's what's requested. */
5528 if (on_newline_p && newline_found_p)
5529 {
5530 if (STRINGP (it->string))
5531 {
5532 if (IT_STRING_CHARPOS (*it) > 0)
5533 {
5534 --IT_STRING_CHARPOS (*it);
5535 --IT_STRING_BYTEPOS (*it);
5536 }
5537 }
5538 else if (IT_CHARPOS (*it) > BEGV)
5539 {
5540 --IT_CHARPOS (*it);
5541 --IT_BYTEPOS (*it);
5542 reseat (it, it->current.pos, 0);
5543 }
5544 }
5545 else if (skipped_p)
5546 reseat (it, it->current.pos, 0);
5547
5548 CHECK_IT (it);
5549 }
5550
5551
5552 \f
5553 /***********************************************************************
5554 Changing an iterator's position
5555 ***********************************************************************/
5556
5557 /* Change IT's current position to POS in current_buffer. If FORCE_P
5558 is non-zero, always check for text properties at the new position.
5559 Otherwise, text properties are only looked up if POS >=
5560 IT->check_charpos of a property. */
5561
5562 static void
5563 reseat (struct it *it, struct text_pos pos, int force_p)
5564 {
5565 EMACS_INT original_pos = IT_CHARPOS (*it);
5566
5567 reseat_1 (it, pos, 0);
5568
5569 /* Determine where to check text properties. Avoid doing it
5570 where possible because text property lookup is very expensive. */
5571 if (force_p
5572 || CHARPOS (pos) > it->stop_charpos
5573 || CHARPOS (pos) < original_pos)
5574 {
5575 if (it->bidi_p)
5576 {
5577 /* For bidi iteration, we need to prime prev_stop and
5578 base_level_stop with our best estimations. */
5579 if (CHARPOS (pos) < it->prev_stop)
5580 {
5581 handle_stop_backwards (it, BEGV);
5582 if (CHARPOS (pos) < it->base_level_stop)
5583 it->base_level_stop = 0;
5584 }
5585 else if (CHARPOS (pos) > it->stop_charpos
5586 && it->stop_charpos >= BEGV)
5587 handle_stop_backwards (it, it->stop_charpos);
5588 else /* force_p */
5589 handle_stop (it);
5590 }
5591 else
5592 {
5593 handle_stop (it);
5594 it->prev_stop = it->base_level_stop = 0;
5595 }
5596
5597 }
5598
5599 CHECK_IT (it);
5600 }
5601
5602
5603 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5604 IT->stop_pos to POS, also. */
5605
5606 static void
5607 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5608 {
5609 /* Don't call this function when scanning a C string. */
5610 xassert (it->s == NULL);
5611
5612 /* POS must be a reasonable value. */
5613 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5614
5615 it->current.pos = it->position = pos;
5616 it->end_charpos = ZV;
5617 it->dpvec = NULL;
5618 it->current.dpvec_index = -1;
5619 it->current.overlay_string_index = -1;
5620 IT_STRING_CHARPOS (*it) = -1;
5621 IT_STRING_BYTEPOS (*it) = -1;
5622 it->string = Qnil;
5623 it->string_from_display_prop_p = 0;
5624 it->method = GET_FROM_BUFFER;
5625 it->object = it->w->buffer;
5626 it->area = TEXT_AREA;
5627 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5628 it->sp = 0;
5629 it->string_from_display_prop_p = 0;
5630 it->face_before_selective_p = 0;
5631 if (it->bidi_p)
5632 {
5633 it->bidi_it.first_elt = 1;
5634 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5635 }
5636
5637 if (set_stop_p)
5638 {
5639 it->stop_charpos = CHARPOS (pos);
5640 it->base_level_stop = CHARPOS (pos);
5641 }
5642 }
5643
5644
5645 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5646 If S is non-null, it is a C string to iterate over. Otherwise,
5647 STRING gives a Lisp string to iterate over.
5648
5649 If PRECISION > 0, don't return more then PRECISION number of
5650 characters from the string.
5651
5652 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5653 characters have been returned. FIELD_WIDTH < 0 means an infinite
5654 field width.
5655
5656 MULTIBYTE = 0 means disable processing of multibyte characters,
5657 MULTIBYTE > 0 means enable it,
5658 MULTIBYTE < 0 means use IT->multibyte_p.
5659
5660 IT must be initialized via a prior call to init_iterator before
5661 calling this function. */
5662
5663 static void
5664 reseat_to_string (struct it *it, const unsigned char *s, Lisp_Object string,
5665 EMACS_INT charpos, EMACS_INT precision, int field_width,
5666 int multibyte)
5667 {
5668 /* No region in strings. */
5669 it->region_beg_charpos = it->region_end_charpos = -1;
5670
5671 /* No text property checks performed by default, but see below. */
5672 it->stop_charpos = -1;
5673
5674 /* Set iterator position and end position. */
5675 memset (&it->current, 0, sizeof it->current);
5676 it->current.overlay_string_index = -1;
5677 it->current.dpvec_index = -1;
5678 xassert (charpos >= 0);
5679
5680 /* If STRING is specified, use its multibyteness, otherwise use the
5681 setting of MULTIBYTE, if specified. */
5682 if (multibyte >= 0)
5683 it->multibyte_p = multibyte > 0;
5684
5685 if (s == NULL)
5686 {
5687 xassert (STRINGP (string));
5688 it->string = string;
5689 it->s = NULL;
5690 it->end_charpos = it->string_nchars = SCHARS (string);
5691 it->method = GET_FROM_STRING;
5692 it->current.string_pos = string_pos (charpos, string);
5693 }
5694 else
5695 {
5696 it->s = s;
5697 it->string = Qnil;
5698
5699 /* Note that we use IT->current.pos, not it->current.string_pos,
5700 for displaying C strings. */
5701 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5702 if (it->multibyte_p)
5703 {
5704 it->current.pos = c_string_pos (charpos, s, 1);
5705 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5706 }
5707 else
5708 {
5709 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5710 it->end_charpos = it->string_nchars = strlen (s);
5711 }
5712
5713 it->method = GET_FROM_C_STRING;
5714 }
5715
5716 /* PRECISION > 0 means don't return more than PRECISION characters
5717 from the string. */
5718 if (precision > 0 && it->end_charpos - charpos > precision)
5719 it->end_charpos = it->string_nchars = charpos + precision;
5720
5721 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5722 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5723 FIELD_WIDTH < 0 means infinite field width. This is useful for
5724 padding with `-' at the end of a mode line. */
5725 if (field_width < 0)
5726 field_width = INFINITY;
5727 if (field_width > it->end_charpos - charpos)
5728 it->end_charpos = charpos + field_width;
5729
5730 /* Use the standard display table for displaying strings. */
5731 if (DISP_TABLE_P (Vstandard_display_table))
5732 it->dp = XCHAR_TABLE (Vstandard_display_table);
5733
5734 it->stop_charpos = charpos;
5735 if (s == NULL && it->multibyte_p)
5736 {
5737 EMACS_INT endpos = SCHARS (it->string);
5738 if (endpos > it->end_charpos)
5739 endpos = it->end_charpos;
5740 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5741 it->string);
5742 }
5743 CHECK_IT (it);
5744 }
5745
5746
5747 \f
5748 /***********************************************************************
5749 Iteration
5750 ***********************************************************************/
5751
5752 /* Map enum it_method value to corresponding next_element_from_* function. */
5753
5754 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5755 {
5756 next_element_from_buffer,
5757 next_element_from_display_vector,
5758 next_element_from_string,
5759 next_element_from_c_string,
5760 next_element_from_image,
5761 next_element_from_stretch
5762 };
5763
5764 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5765
5766
5767 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5768 (possibly with the following characters). */
5769
5770 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5771 ((IT)->cmp_it.id >= 0 \
5772 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5773 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5774 END_CHARPOS, (IT)->w, \
5775 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5776 (IT)->string)))
5777
5778
5779 /* Lookup the char-table Vglyphless_char_display for character C (-1
5780 if we want information for no-font case), and return the display
5781 method symbol. By side-effect, update it->what and
5782 it->glyphless_method. This function is called from
5783 get_next_display_element for each character element, and from
5784 x_produce_glyphs when no suitable font was found. */
5785
5786 Lisp_Object
5787 lookup_glyphless_char_display (int c, struct it *it)
5788 {
5789 Lisp_Object glyphless_method = Qnil;
5790
5791 if (CHAR_TABLE_P (Vglyphless_char_display)
5792 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5793 glyphless_method = (c >= 0
5794 ? CHAR_TABLE_REF (Vglyphless_char_display, c)
5795 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
5796 retry:
5797 if (NILP (glyphless_method))
5798 {
5799 if (c >= 0)
5800 /* The default is to display the character by a proper font. */
5801 return Qnil;
5802 /* The default for the no-font case is to display an empty box. */
5803 glyphless_method = Qempty_box;
5804 }
5805 if (EQ (glyphless_method, Qzero_width))
5806 {
5807 if (c >= 0)
5808 return glyphless_method;
5809 /* This method can't be used for the no-font case. */
5810 glyphless_method = Qempty_box;
5811 }
5812 if (EQ (glyphless_method, Qthin_space))
5813 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
5814 else if (EQ (glyphless_method, Qempty_box))
5815 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
5816 else if (EQ (glyphless_method, Qhexa_code))
5817 it->glyphless_method = GLYPHLESS_DISPLAY_HEXA_CODE;
5818 else if (STRINGP (glyphless_method))
5819 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
5820 else
5821 {
5822 /* Invalid value. We use the default method. */
5823 glyphless_method = Qnil;
5824 goto retry;
5825 }
5826 it->what = IT_GLYPHLESS;
5827 return glyphless_method;
5828 }
5829
5830 /* Load IT's display element fields with information about the next
5831 display element from the current position of IT. Value is zero if
5832 end of buffer (or C string) is reached. */
5833
5834 static struct frame *last_escape_glyph_frame = NULL;
5835 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5836 static int last_escape_glyph_merged_face_id = 0;
5837
5838 struct frame *last_glyphless_glyph_frame = NULL;
5839 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
5840 int last_glyphless_glyph_merged_face_id = 0;
5841
5842 int
5843 get_next_display_element (struct it *it)
5844 {
5845 /* Non-zero means that we found a display element. Zero means that
5846 we hit the end of what we iterate over. Performance note: the
5847 function pointer `method' used here turns out to be faster than
5848 using a sequence of if-statements. */
5849 int success_p;
5850
5851 get_next:
5852 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5853
5854 if (it->what == IT_CHARACTER)
5855 {
5856 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5857 and only if (a) the resolved directionality of that character
5858 is R..." */
5859 /* FIXME: Do we need an exception for characters from display
5860 tables? */
5861 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5862 it->c = bidi_mirror_char (it->c);
5863 /* Map via display table or translate control characters.
5864 IT->c, IT->len etc. have been set to the next character by
5865 the function call above. If we have a display table, and it
5866 contains an entry for IT->c, translate it. Don't do this if
5867 IT->c itself comes from a display table, otherwise we could
5868 end up in an infinite recursion. (An alternative could be to
5869 count the recursion depth of this function and signal an
5870 error when a certain maximum depth is reached.) Is it worth
5871 it? */
5872 if (success_p && it->dpvec == NULL)
5873 {
5874 Lisp_Object dv;
5875 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5876 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5877 nbsp_or_shy = char_is_other;
5878 int c = it->c; /* This is the character to display. */
5879
5880 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5881 {
5882 xassert (SINGLE_BYTE_CHAR_P (c));
5883 if (unibyte_display_via_language_environment)
5884 {
5885 c = DECODE_CHAR (unibyte, c);
5886 if (c < 0)
5887 c = BYTE8_TO_CHAR (it->c);
5888 }
5889 else
5890 c = BYTE8_TO_CHAR (it->c);
5891 }
5892
5893 if (it->dp
5894 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5895 VECTORP (dv)))
5896 {
5897 struct Lisp_Vector *v = XVECTOR (dv);
5898
5899 /* Return the first character from the display table
5900 entry, if not empty. If empty, don't display the
5901 current character. */
5902 if (v->size)
5903 {
5904 it->dpvec_char_len = it->len;
5905 it->dpvec = v->contents;
5906 it->dpend = v->contents + v->size;
5907 it->current.dpvec_index = 0;
5908 it->dpvec_face_id = -1;
5909 it->saved_face_id = it->face_id;
5910 it->method = GET_FROM_DISPLAY_VECTOR;
5911 it->ellipsis_p = 0;
5912 }
5913 else
5914 {
5915 set_iterator_to_next (it, 0);
5916 }
5917 goto get_next;
5918 }
5919
5920 if (! NILP (lookup_glyphless_char_display (c, it)))
5921 {
5922 if (it->what == IT_GLYPHLESS)
5923 goto done;
5924 /* Don't display this character. */
5925 set_iterator_to_next (it, 0);
5926 goto get_next;
5927 }
5928
5929 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5930 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5931 : c == 0xAD ? char_is_soft_hyphen
5932 : char_is_other);
5933
5934 /* Translate control characters into `\003' or `^C' form.
5935 Control characters coming from a display table entry are
5936 currently not translated because we use IT->dpvec to hold
5937 the translation. This could easily be changed but I
5938 don't believe that it is worth doing.
5939
5940 NBSP and SOFT-HYPEN are property translated too.
5941
5942 Non-printable characters and raw-byte characters are also
5943 translated to octal form. */
5944 if (((c < ' ' || c == 127) /* ASCII control chars */
5945 ? (it->area != TEXT_AREA
5946 /* In mode line, treat \n, \t like other crl chars. */
5947 || (c != '\t'
5948 && it->glyph_row
5949 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5950 || (c != '\n' && c != '\t'))
5951 : (nbsp_or_shy
5952 || CHAR_BYTE8_P (c)
5953 || ! CHAR_PRINTABLE_P (c))))
5954 {
5955 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5956 or a non-printable character which must be displayed
5957 either as '\003' or as `^C' where the '\\' and '^'
5958 can be defined in the display table. Fill
5959 IT->ctl_chars with glyphs for what we have to
5960 display. Then, set IT->dpvec to these glyphs. */
5961 Lisp_Object gc;
5962 int ctl_len;
5963 int face_id, lface_id = 0 ;
5964 int escape_glyph;
5965
5966 /* Handle control characters with ^. */
5967
5968 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5969 {
5970 int g;
5971
5972 g = '^'; /* default glyph for Control */
5973 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5974 if (it->dp
5975 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5976 && GLYPH_CODE_CHAR_VALID_P (gc))
5977 {
5978 g = GLYPH_CODE_CHAR (gc);
5979 lface_id = GLYPH_CODE_FACE (gc);
5980 }
5981 if (lface_id)
5982 {
5983 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5984 }
5985 else if (it->f == last_escape_glyph_frame
5986 && it->face_id == last_escape_glyph_face_id)
5987 {
5988 face_id = last_escape_glyph_merged_face_id;
5989 }
5990 else
5991 {
5992 /* Merge the escape-glyph face into the current face. */
5993 face_id = merge_faces (it->f, Qescape_glyph, 0,
5994 it->face_id);
5995 last_escape_glyph_frame = it->f;
5996 last_escape_glyph_face_id = it->face_id;
5997 last_escape_glyph_merged_face_id = face_id;
5998 }
5999
6000 XSETINT (it->ctl_chars[0], g);
6001 XSETINT (it->ctl_chars[1], c ^ 0100);
6002 ctl_len = 2;
6003 goto display_control;
6004 }
6005
6006 /* Handle non-break space in the mode where it only gets
6007 highlighting. */
6008
6009 if (EQ (Vnobreak_char_display, Qt)
6010 && nbsp_or_shy == char_is_nbsp)
6011 {
6012 /* Merge the no-break-space face into the current face. */
6013 face_id = merge_faces (it->f, Qnobreak_space, 0,
6014 it->face_id);
6015
6016 c = ' ';
6017 XSETINT (it->ctl_chars[0], ' ');
6018 ctl_len = 1;
6019 goto display_control;
6020 }
6021
6022 /* Handle sequences that start with the "escape glyph". */
6023
6024 /* the default escape glyph is \. */
6025 escape_glyph = '\\';
6026
6027 if (it->dp
6028 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6029 && GLYPH_CODE_CHAR_VALID_P (gc))
6030 {
6031 escape_glyph = GLYPH_CODE_CHAR (gc);
6032 lface_id = GLYPH_CODE_FACE (gc);
6033 }
6034 if (lface_id)
6035 {
6036 /* The display table specified a face.
6037 Merge it into face_id and also into escape_glyph. */
6038 face_id = merge_faces (it->f, Qt, lface_id,
6039 it->face_id);
6040 }
6041 else if (it->f == last_escape_glyph_frame
6042 && it->face_id == last_escape_glyph_face_id)
6043 {
6044 face_id = last_escape_glyph_merged_face_id;
6045 }
6046 else
6047 {
6048 /* Merge the escape-glyph face into the current face. */
6049 face_id = merge_faces (it->f, Qescape_glyph, 0,
6050 it->face_id);
6051 last_escape_glyph_frame = it->f;
6052 last_escape_glyph_face_id = it->face_id;
6053 last_escape_glyph_merged_face_id = face_id;
6054 }
6055
6056 /* Handle soft hyphens in the mode where they only get
6057 highlighting. */
6058
6059 if (EQ (Vnobreak_char_display, Qt)
6060 && nbsp_or_shy == char_is_soft_hyphen)
6061 {
6062 XSETINT (it->ctl_chars[0], '-');
6063 ctl_len = 1;
6064 goto display_control;
6065 }
6066
6067 /* Handle non-break space and soft hyphen
6068 with the escape glyph. */
6069
6070 if (nbsp_or_shy)
6071 {
6072 XSETINT (it->ctl_chars[0], escape_glyph);
6073 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6074 XSETINT (it->ctl_chars[1], c);
6075 ctl_len = 2;
6076 goto display_control;
6077 }
6078
6079 {
6080 char str[10];
6081 int len, i;
6082
6083 if (CHAR_BYTE8_P (c))
6084 /* Display \200 instead of \17777600. */
6085 c = CHAR_TO_BYTE8 (c);
6086 len = sprintf (str, "%03o", c);
6087
6088 XSETINT (it->ctl_chars[0], escape_glyph);
6089 for (i = 0; i < len; i++)
6090 XSETINT (it->ctl_chars[i + 1], str[i]);
6091 ctl_len = len + 1;
6092 }
6093
6094 display_control:
6095 /* Set up IT->dpvec and return first character from it. */
6096 it->dpvec_char_len = it->len;
6097 it->dpvec = it->ctl_chars;
6098 it->dpend = it->dpvec + ctl_len;
6099 it->current.dpvec_index = 0;
6100 it->dpvec_face_id = face_id;
6101 it->saved_face_id = it->face_id;
6102 it->method = GET_FROM_DISPLAY_VECTOR;
6103 it->ellipsis_p = 0;
6104 goto get_next;
6105 }
6106 it->char_to_display = c;
6107 }
6108 else if (success_p)
6109 {
6110 it->char_to_display = it->c;
6111 }
6112 }
6113
6114 #ifdef HAVE_WINDOW_SYSTEM
6115 /* Adjust face id for a multibyte character. There are no multibyte
6116 character in unibyte text. */
6117 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6118 && it->multibyte_p
6119 && success_p
6120 && FRAME_WINDOW_P (it->f))
6121 {
6122 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6123
6124 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6125 {
6126 /* Automatic composition with glyph-string. */
6127 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6128
6129 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6130 }
6131 else
6132 {
6133 EMACS_INT pos = (it->s ? -1
6134 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6135 : IT_CHARPOS (*it));
6136
6137 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6138 it->string);
6139 }
6140 }
6141 #endif
6142
6143 done:
6144 /* Is this character the last one of a run of characters with
6145 box? If yes, set IT->end_of_box_run_p to 1. */
6146 if (it->face_box_p
6147 && it->s == NULL)
6148 {
6149 if (it->method == GET_FROM_STRING && it->sp)
6150 {
6151 int face_id = underlying_face_id (it);
6152 struct face *face = FACE_FROM_ID (it->f, face_id);
6153
6154 if (face)
6155 {
6156 if (face->box == FACE_NO_BOX)
6157 {
6158 /* If the box comes from face properties in a
6159 display string, check faces in that string. */
6160 int string_face_id = face_after_it_pos (it);
6161 it->end_of_box_run_p
6162 = (FACE_FROM_ID (it->f, string_face_id)->box
6163 == FACE_NO_BOX);
6164 }
6165 /* Otherwise, the box comes from the underlying face.
6166 If this is the last string character displayed, check
6167 the next buffer location. */
6168 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6169 && (it->current.overlay_string_index
6170 == it->n_overlay_strings - 1))
6171 {
6172 EMACS_INT ignore;
6173 int next_face_id;
6174 struct text_pos pos = it->current.pos;
6175 INC_TEXT_POS (pos, it->multibyte_p);
6176
6177 next_face_id = face_at_buffer_position
6178 (it->w, CHARPOS (pos), it->region_beg_charpos,
6179 it->region_end_charpos, &ignore,
6180 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6181 -1);
6182 it->end_of_box_run_p
6183 = (FACE_FROM_ID (it->f, next_face_id)->box
6184 == FACE_NO_BOX);
6185 }
6186 }
6187 }
6188 else
6189 {
6190 int face_id = face_after_it_pos (it);
6191 it->end_of_box_run_p
6192 = (face_id != it->face_id
6193 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6194 }
6195 }
6196
6197 /* Value is 0 if end of buffer or string reached. */
6198 return success_p;
6199 }
6200
6201
6202 /* Move IT to the next display element.
6203
6204 RESEAT_P non-zero means if called on a newline in buffer text,
6205 skip to the next visible line start.
6206
6207 Functions get_next_display_element and set_iterator_to_next are
6208 separate because I find this arrangement easier to handle than a
6209 get_next_display_element function that also increments IT's
6210 position. The way it is we can first look at an iterator's current
6211 display element, decide whether it fits on a line, and if it does,
6212 increment the iterator position. The other way around we probably
6213 would either need a flag indicating whether the iterator has to be
6214 incremented the next time, or we would have to implement a
6215 decrement position function which would not be easy to write. */
6216
6217 void
6218 set_iterator_to_next (struct it *it, int reseat_p)
6219 {
6220 /* Reset flags indicating start and end of a sequence of characters
6221 with box. Reset them at the start of this function because
6222 moving the iterator to a new position might set them. */
6223 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6224
6225 switch (it->method)
6226 {
6227 case GET_FROM_BUFFER:
6228 /* The current display element of IT is a character from
6229 current_buffer. Advance in the buffer, and maybe skip over
6230 invisible lines that are so because of selective display. */
6231 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6232 reseat_at_next_visible_line_start (it, 0);
6233 else if (it->cmp_it.id >= 0)
6234 {
6235 /* We are currently getting glyphs from a composition. */
6236 int i;
6237
6238 if (! it->bidi_p)
6239 {
6240 IT_CHARPOS (*it) += it->cmp_it.nchars;
6241 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6242 if (it->cmp_it.to < it->cmp_it.nglyphs)
6243 {
6244 it->cmp_it.from = it->cmp_it.to;
6245 }
6246 else
6247 {
6248 it->cmp_it.id = -1;
6249 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6250 IT_BYTEPOS (*it),
6251 it->end_charpos, Qnil);
6252 }
6253 }
6254 else if (! it->cmp_it.reversed_p)
6255 {
6256 /* Composition created while scanning forward. */
6257 /* Update IT's char/byte positions to point to the first
6258 character of the next grapheme cluster, or to the
6259 character visually after the current composition. */
6260 for (i = 0; i < it->cmp_it.nchars; i++)
6261 bidi_move_to_visually_next (&it->bidi_it);
6262 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6263 IT_CHARPOS (*it) = it->bidi_it.charpos;
6264
6265 if (it->cmp_it.to < it->cmp_it.nglyphs)
6266 {
6267 /* Proceed to the next grapheme cluster. */
6268 it->cmp_it.from = it->cmp_it.to;
6269 }
6270 else
6271 {
6272 /* No more grapheme clusters in this composition.
6273 Find the next stop position. */
6274 EMACS_INT stop = it->end_charpos;
6275 if (it->bidi_it.scan_dir < 0)
6276 /* Now we are scanning backward and don't know
6277 where to stop. */
6278 stop = -1;
6279 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6280 IT_BYTEPOS (*it), stop, Qnil);
6281 }
6282 }
6283 else
6284 {
6285 /* Composition created while scanning backward. */
6286 /* Update IT's char/byte positions to point to the last
6287 character of the previous grapheme cluster, or the
6288 character visually after the current composition. */
6289 for (i = 0; i < it->cmp_it.nchars; i++)
6290 bidi_move_to_visually_next (&it->bidi_it);
6291 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6292 IT_CHARPOS (*it) = it->bidi_it.charpos;
6293 if (it->cmp_it.from > 0)
6294 {
6295 /* Proceed to the previous grapheme cluster. */
6296 it->cmp_it.to = it->cmp_it.from;
6297 }
6298 else
6299 {
6300 /* No more grapheme clusters in this composition.
6301 Find the next stop position. */
6302 EMACS_INT stop = it->end_charpos;
6303 if (it->bidi_it.scan_dir < 0)
6304 /* Now we are scanning backward and don't know
6305 where to stop. */
6306 stop = -1;
6307 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6308 IT_BYTEPOS (*it), stop, Qnil);
6309 }
6310 }
6311 }
6312 else
6313 {
6314 xassert (it->len != 0);
6315
6316 if (!it->bidi_p)
6317 {
6318 IT_BYTEPOS (*it) += it->len;
6319 IT_CHARPOS (*it) += 1;
6320 }
6321 else
6322 {
6323 int prev_scan_dir = it->bidi_it.scan_dir;
6324 /* If this is a new paragraph, determine its base
6325 direction (a.k.a. its base embedding level). */
6326 if (it->bidi_it.new_paragraph)
6327 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6328 bidi_move_to_visually_next (&it->bidi_it);
6329 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6330 IT_CHARPOS (*it) = it->bidi_it.charpos;
6331 if (prev_scan_dir != it->bidi_it.scan_dir)
6332 {
6333 /* As the scan direction was changed, we must
6334 re-compute the stop position for composition. */
6335 EMACS_INT stop = it->end_charpos;
6336 if (it->bidi_it.scan_dir < 0)
6337 stop = -1;
6338 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6339 IT_BYTEPOS (*it), stop, Qnil);
6340 }
6341 }
6342 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6343 }
6344 break;
6345
6346 case GET_FROM_C_STRING:
6347 /* Current display element of IT is from a C string. */
6348 IT_BYTEPOS (*it) += it->len;
6349 IT_CHARPOS (*it) += 1;
6350 break;
6351
6352 case GET_FROM_DISPLAY_VECTOR:
6353 /* Current display element of IT is from a display table entry.
6354 Advance in the display table definition. Reset it to null if
6355 end reached, and continue with characters from buffers/
6356 strings. */
6357 ++it->current.dpvec_index;
6358
6359 /* Restore face of the iterator to what they were before the
6360 display vector entry (these entries may contain faces). */
6361 it->face_id = it->saved_face_id;
6362
6363 if (it->dpvec + it->current.dpvec_index == it->dpend)
6364 {
6365 int recheck_faces = it->ellipsis_p;
6366
6367 if (it->s)
6368 it->method = GET_FROM_C_STRING;
6369 else if (STRINGP (it->string))
6370 it->method = GET_FROM_STRING;
6371 else
6372 {
6373 it->method = GET_FROM_BUFFER;
6374 it->object = it->w->buffer;
6375 }
6376
6377 it->dpvec = NULL;
6378 it->current.dpvec_index = -1;
6379
6380 /* Skip over characters which were displayed via IT->dpvec. */
6381 if (it->dpvec_char_len < 0)
6382 reseat_at_next_visible_line_start (it, 1);
6383 else if (it->dpvec_char_len > 0)
6384 {
6385 if (it->method == GET_FROM_STRING
6386 && it->n_overlay_strings > 0)
6387 it->ignore_overlay_strings_at_pos_p = 1;
6388 it->len = it->dpvec_char_len;
6389 set_iterator_to_next (it, reseat_p);
6390 }
6391
6392 /* Maybe recheck faces after display vector */
6393 if (recheck_faces)
6394 it->stop_charpos = IT_CHARPOS (*it);
6395 }
6396 break;
6397
6398 case GET_FROM_STRING:
6399 /* Current display element is a character from a Lisp string. */
6400 xassert (it->s == NULL && STRINGP (it->string));
6401 if (it->cmp_it.id >= 0)
6402 {
6403 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6404 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6405 if (it->cmp_it.to < it->cmp_it.nglyphs)
6406 it->cmp_it.from = it->cmp_it.to;
6407 else
6408 {
6409 it->cmp_it.id = -1;
6410 composition_compute_stop_pos (&it->cmp_it,
6411 IT_STRING_CHARPOS (*it),
6412 IT_STRING_BYTEPOS (*it),
6413 it->end_charpos, it->string);
6414 }
6415 }
6416 else
6417 {
6418 IT_STRING_BYTEPOS (*it) += it->len;
6419 IT_STRING_CHARPOS (*it) += 1;
6420 }
6421
6422 consider_string_end:
6423
6424 if (it->current.overlay_string_index >= 0)
6425 {
6426 /* IT->string is an overlay string. Advance to the
6427 next, if there is one. */
6428 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6429 {
6430 it->ellipsis_p = 0;
6431 next_overlay_string (it);
6432 if (it->ellipsis_p)
6433 setup_for_ellipsis (it, 0);
6434 }
6435 }
6436 else
6437 {
6438 /* IT->string is not an overlay string. If we reached
6439 its end, and there is something on IT->stack, proceed
6440 with what is on the stack. This can be either another
6441 string, this time an overlay string, or a buffer. */
6442 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6443 && it->sp > 0)
6444 {
6445 pop_it (it);
6446 if (it->method == GET_FROM_STRING)
6447 goto consider_string_end;
6448 }
6449 }
6450 break;
6451
6452 case GET_FROM_IMAGE:
6453 case GET_FROM_STRETCH:
6454 /* The position etc with which we have to proceed are on
6455 the stack. The position may be at the end of a string,
6456 if the `display' property takes up the whole string. */
6457 xassert (it->sp > 0);
6458 pop_it (it);
6459 if (it->method == GET_FROM_STRING)
6460 goto consider_string_end;
6461 break;
6462
6463 default:
6464 /* There are no other methods defined, so this should be a bug. */
6465 abort ();
6466 }
6467
6468 xassert (it->method != GET_FROM_STRING
6469 || (STRINGP (it->string)
6470 && IT_STRING_CHARPOS (*it) >= 0));
6471 }
6472
6473 /* Load IT's display element fields with information about the next
6474 display element which comes from a display table entry or from the
6475 result of translating a control character to one of the forms `^C'
6476 or `\003'.
6477
6478 IT->dpvec holds the glyphs to return as characters.
6479 IT->saved_face_id holds the face id before the display vector--it
6480 is restored into IT->face_id in set_iterator_to_next. */
6481
6482 static int
6483 next_element_from_display_vector (struct it *it)
6484 {
6485 Lisp_Object gc;
6486
6487 /* Precondition. */
6488 xassert (it->dpvec && it->current.dpvec_index >= 0);
6489
6490 it->face_id = it->saved_face_id;
6491
6492 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6493 That seemed totally bogus - so I changed it... */
6494 gc = it->dpvec[it->current.dpvec_index];
6495
6496 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6497 {
6498 it->c = GLYPH_CODE_CHAR (gc);
6499 it->len = CHAR_BYTES (it->c);
6500
6501 /* The entry may contain a face id to use. Such a face id is
6502 the id of a Lisp face, not a realized face. A face id of
6503 zero means no face is specified. */
6504 if (it->dpvec_face_id >= 0)
6505 it->face_id = it->dpvec_face_id;
6506 else
6507 {
6508 int lface_id = GLYPH_CODE_FACE (gc);
6509 if (lface_id > 0)
6510 it->face_id = merge_faces (it->f, Qt, lface_id,
6511 it->saved_face_id);
6512 }
6513 }
6514 else
6515 /* Display table entry is invalid. Return a space. */
6516 it->c = ' ', it->len = 1;
6517
6518 /* Don't change position and object of the iterator here. They are
6519 still the values of the character that had this display table
6520 entry or was translated, and that's what we want. */
6521 it->what = IT_CHARACTER;
6522 return 1;
6523 }
6524
6525
6526 /* Load IT with the next display element from Lisp string IT->string.
6527 IT->current.string_pos is the current position within the string.
6528 If IT->current.overlay_string_index >= 0, the Lisp string is an
6529 overlay string. */
6530
6531 static int
6532 next_element_from_string (struct it *it)
6533 {
6534 struct text_pos position;
6535
6536 xassert (STRINGP (it->string));
6537 xassert (IT_STRING_CHARPOS (*it) >= 0);
6538 position = it->current.string_pos;
6539
6540 /* Time to check for invisible text? */
6541 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6542 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6543 {
6544 handle_stop (it);
6545
6546 /* Since a handler may have changed IT->method, we must
6547 recurse here. */
6548 return GET_NEXT_DISPLAY_ELEMENT (it);
6549 }
6550
6551 if (it->current.overlay_string_index >= 0)
6552 {
6553 /* Get the next character from an overlay string. In overlay
6554 strings, There is no field width or padding with spaces to
6555 do. */
6556 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6557 {
6558 it->what = IT_EOB;
6559 return 0;
6560 }
6561 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6562 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6563 && next_element_from_composition (it))
6564 {
6565 return 1;
6566 }
6567 else if (STRING_MULTIBYTE (it->string))
6568 {
6569 const unsigned char *s = (SDATA (it->string)
6570 + IT_STRING_BYTEPOS (*it));
6571 it->c = string_char_and_length (s, &it->len);
6572 }
6573 else
6574 {
6575 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6576 it->len = 1;
6577 }
6578 }
6579 else
6580 {
6581 /* Get the next character from a Lisp string that is not an
6582 overlay string. Such strings come from the mode line, for
6583 example. We may have to pad with spaces, or truncate the
6584 string. See also next_element_from_c_string. */
6585 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6586 {
6587 it->what = IT_EOB;
6588 return 0;
6589 }
6590 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6591 {
6592 /* Pad with spaces. */
6593 it->c = ' ', it->len = 1;
6594 CHARPOS (position) = BYTEPOS (position) = -1;
6595 }
6596 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6597 IT_STRING_BYTEPOS (*it), it->string_nchars)
6598 && next_element_from_composition (it))
6599 {
6600 return 1;
6601 }
6602 else if (STRING_MULTIBYTE (it->string))
6603 {
6604 const unsigned char *s = (SDATA (it->string)
6605 + IT_STRING_BYTEPOS (*it));
6606 it->c = string_char_and_length (s, &it->len);
6607 }
6608 else
6609 {
6610 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6611 it->len = 1;
6612 }
6613 }
6614
6615 /* Record what we have and where it came from. */
6616 it->what = IT_CHARACTER;
6617 it->object = it->string;
6618 it->position = position;
6619 return 1;
6620 }
6621
6622
6623 /* Load IT with next display element from C string IT->s.
6624 IT->string_nchars is the maximum number of characters to return
6625 from the string. IT->end_charpos may be greater than
6626 IT->string_nchars when this function is called, in which case we
6627 may have to return padding spaces. Value is zero if end of string
6628 reached, including padding spaces. */
6629
6630 static int
6631 next_element_from_c_string (struct it *it)
6632 {
6633 int success_p = 1;
6634
6635 xassert (it->s);
6636 it->what = IT_CHARACTER;
6637 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6638 it->object = Qnil;
6639
6640 /* IT's position can be greater IT->string_nchars in case a field
6641 width or precision has been specified when the iterator was
6642 initialized. */
6643 if (IT_CHARPOS (*it) >= it->end_charpos)
6644 {
6645 /* End of the game. */
6646 it->what = IT_EOB;
6647 success_p = 0;
6648 }
6649 else if (IT_CHARPOS (*it) >= it->string_nchars)
6650 {
6651 /* Pad with spaces. */
6652 it->c = ' ', it->len = 1;
6653 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6654 }
6655 else if (it->multibyte_p)
6656 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6657 else
6658 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6659
6660 return success_p;
6661 }
6662
6663
6664 /* Set up IT to return characters from an ellipsis, if appropriate.
6665 The definition of the ellipsis glyphs may come from a display table
6666 entry. This function fills IT with the first glyph from the
6667 ellipsis if an ellipsis is to be displayed. */
6668
6669 static int
6670 next_element_from_ellipsis (struct it *it)
6671 {
6672 if (it->selective_display_ellipsis_p)
6673 setup_for_ellipsis (it, it->len);
6674 else
6675 {
6676 /* The face at the current position may be different from the
6677 face we find after the invisible text. Remember what it
6678 was in IT->saved_face_id, and signal that it's there by
6679 setting face_before_selective_p. */
6680 it->saved_face_id = it->face_id;
6681 it->method = GET_FROM_BUFFER;
6682 it->object = it->w->buffer;
6683 reseat_at_next_visible_line_start (it, 1);
6684 it->face_before_selective_p = 1;
6685 }
6686
6687 return GET_NEXT_DISPLAY_ELEMENT (it);
6688 }
6689
6690
6691 /* Deliver an image display element. The iterator IT is already
6692 filled with image information (done in handle_display_prop). Value
6693 is always 1. */
6694
6695
6696 static int
6697 next_element_from_image (struct it *it)
6698 {
6699 it->what = IT_IMAGE;
6700 it->ignore_overlay_strings_at_pos_p = 0;
6701 return 1;
6702 }
6703
6704
6705 /* Fill iterator IT with next display element from a stretch glyph
6706 property. IT->object is the value of the text property. Value is
6707 always 1. */
6708
6709 static int
6710 next_element_from_stretch (struct it *it)
6711 {
6712 it->what = IT_STRETCH;
6713 return 1;
6714 }
6715
6716 /* Scan forward from CHARPOS in the current buffer, until we find a
6717 stop position > current IT's position. Then handle the stop
6718 position before that. This is called when we bump into a stop
6719 position while reordering bidirectional text. CHARPOS should be
6720 the last previously processed stop_pos (or BEGV, if none were
6721 processed yet) whose position is less that IT's current
6722 position. */
6723
6724 static void
6725 handle_stop_backwards (struct it *it, EMACS_INT charpos)
6726 {
6727 EMACS_INT where_we_are = IT_CHARPOS (*it);
6728 struct display_pos save_current = it->current;
6729 struct text_pos save_position = it->position;
6730 struct text_pos pos1;
6731 EMACS_INT next_stop;
6732
6733 /* Scan in strict logical order. */
6734 it->bidi_p = 0;
6735 do
6736 {
6737 it->prev_stop = charpos;
6738 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6739 reseat_1 (it, pos1, 0);
6740 compute_stop_pos (it);
6741 /* We must advance forward, right? */
6742 if (it->stop_charpos <= it->prev_stop)
6743 abort ();
6744 charpos = it->stop_charpos;
6745 }
6746 while (charpos <= where_we_are);
6747
6748 next_stop = it->stop_charpos;
6749 it->stop_charpos = it->prev_stop;
6750 it->bidi_p = 1;
6751 it->current = save_current;
6752 it->position = save_position;
6753 handle_stop (it);
6754 it->stop_charpos = next_stop;
6755 }
6756
6757 /* Load IT with the next display element from current_buffer. Value
6758 is zero if end of buffer reached. IT->stop_charpos is the next
6759 position at which to stop and check for text properties or buffer
6760 end. */
6761
6762 static int
6763 next_element_from_buffer (struct it *it)
6764 {
6765 int success_p = 1;
6766
6767 xassert (IT_CHARPOS (*it) >= BEGV);
6768
6769 /* With bidi reordering, the character to display might not be the
6770 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6771 we were reseat()ed to a new buffer position, which is potentially
6772 a different paragraph. */
6773 if (it->bidi_p && it->bidi_it.first_elt)
6774 {
6775 it->bidi_it.charpos = IT_CHARPOS (*it);
6776 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6777 if (it->bidi_it.bytepos == ZV_BYTE)
6778 {
6779 /* Nothing to do, but reset the FIRST_ELT flag, like
6780 bidi_paragraph_init does, because we are not going to
6781 call it. */
6782 it->bidi_it.first_elt = 0;
6783 }
6784 else if (it->bidi_it.bytepos == BEGV_BYTE
6785 /* FIXME: Should support all Unicode line separators. */
6786 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6787 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6788 {
6789 /* If we are at the beginning of a line, we can produce the
6790 next element right away. */
6791 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6792 bidi_move_to_visually_next (&it->bidi_it);
6793 }
6794 else
6795 {
6796 EMACS_INT orig_bytepos = IT_BYTEPOS (*it);
6797
6798 /* We need to prime the bidi iterator starting at the line's
6799 beginning, before we will be able to produce the next
6800 element. */
6801 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6802 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6803 it->bidi_it.charpos = IT_CHARPOS (*it);
6804 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6805 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6806 do
6807 {
6808 /* Now return to buffer position where we were asked to
6809 get the next display element, and produce that. */
6810 bidi_move_to_visually_next (&it->bidi_it);
6811 }
6812 while (it->bidi_it.bytepos != orig_bytepos
6813 && it->bidi_it.bytepos < ZV_BYTE);
6814 }
6815
6816 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6817 /* Adjust IT's position information to where we ended up. */
6818 IT_CHARPOS (*it) = it->bidi_it.charpos;
6819 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6820 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6821 {
6822 EMACS_INT stop = it->end_charpos;
6823 if (it->bidi_it.scan_dir < 0)
6824 stop = -1;
6825 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6826 IT_BYTEPOS (*it), stop, Qnil);
6827 }
6828 }
6829
6830 if (IT_CHARPOS (*it) >= it->stop_charpos)
6831 {
6832 if (IT_CHARPOS (*it) >= it->end_charpos)
6833 {
6834 int overlay_strings_follow_p;
6835
6836 /* End of the game, except when overlay strings follow that
6837 haven't been returned yet. */
6838 if (it->overlay_strings_at_end_processed_p)
6839 overlay_strings_follow_p = 0;
6840 else
6841 {
6842 it->overlay_strings_at_end_processed_p = 1;
6843 overlay_strings_follow_p = get_overlay_strings (it, 0);
6844 }
6845
6846 if (overlay_strings_follow_p)
6847 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6848 else
6849 {
6850 it->what = IT_EOB;
6851 it->position = it->current.pos;
6852 success_p = 0;
6853 }
6854 }
6855 else if (!(!it->bidi_p
6856 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6857 || IT_CHARPOS (*it) == it->stop_charpos))
6858 {
6859 /* With bidi non-linear iteration, we could find ourselves
6860 far beyond the last computed stop_charpos, with several
6861 other stop positions in between that we missed. Scan
6862 them all now, in buffer's logical order, until we find
6863 and handle the last stop_charpos that precedes our
6864 current position. */
6865 handle_stop_backwards (it, it->stop_charpos);
6866 return GET_NEXT_DISPLAY_ELEMENT (it);
6867 }
6868 else
6869 {
6870 if (it->bidi_p)
6871 {
6872 /* Take note of the stop position we just moved across,
6873 for when we will move back across it. */
6874 it->prev_stop = it->stop_charpos;
6875 /* If we are at base paragraph embedding level, take
6876 note of the last stop position seen at this
6877 level. */
6878 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6879 it->base_level_stop = it->stop_charpos;
6880 }
6881 handle_stop (it);
6882 return GET_NEXT_DISPLAY_ELEMENT (it);
6883 }
6884 }
6885 else if (it->bidi_p
6886 /* We can sometimes back up for reasons that have nothing
6887 to do with bidi reordering. E.g., compositions. The
6888 code below is only needed when we are above the base
6889 embedding level, so test for that explicitly. */
6890 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6891 && IT_CHARPOS (*it) < it->prev_stop)
6892 {
6893 if (it->base_level_stop <= 0)
6894 it->base_level_stop = BEGV;
6895 if (IT_CHARPOS (*it) < it->base_level_stop)
6896 abort ();
6897 handle_stop_backwards (it, it->base_level_stop);
6898 return GET_NEXT_DISPLAY_ELEMENT (it);
6899 }
6900 else
6901 {
6902 /* No face changes, overlays etc. in sight, so just return a
6903 character from current_buffer. */
6904 unsigned char *p;
6905 EMACS_INT stop;
6906
6907 /* Maybe run the redisplay end trigger hook. Performance note:
6908 This doesn't seem to cost measurable time. */
6909 if (it->redisplay_end_trigger_charpos
6910 && it->glyph_row
6911 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6912 run_redisplay_end_trigger_hook (it);
6913
6914 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
6915 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6916 stop)
6917 && next_element_from_composition (it))
6918 {
6919 return 1;
6920 }
6921
6922 /* Get the next character, maybe multibyte. */
6923 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6924 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6925 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6926 else
6927 it->c = *p, it->len = 1;
6928
6929 /* Record what we have and where it came from. */
6930 it->what = IT_CHARACTER;
6931 it->object = it->w->buffer;
6932 it->position = it->current.pos;
6933
6934 /* Normally we return the character found above, except when we
6935 really want to return an ellipsis for selective display. */
6936 if (it->selective)
6937 {
6938 if (it->c == '\n')
6939 {
6940 /* A value of selective > 0 means hide lines indented more
6941 than that number of columns. */
6942 if (it->selective > 0
6943 && IT_CHARPOS (*it) + 1 < ZV
6944 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6945 IT_BYTEPOS (*it) + 1,
6946 (double) it->selective)) /* iftc */
6947 {
6948 success_p = next_element_from_ellipsis (it);
6949 it->dpvec_char_len = -1;
6950 }
6951 }
6952 else if (it->c == '\r' && it->selective == -1)
6953 {
6954 /* A value of selective == -1 means that everything from the
6955 CR to the end of the line is invisible, with maybe an
6956 ellipsis displayed for it. */
6957 success_p = next_element_from_ellipsis (it);
6958 it->dpvec_char_len = -1;
6959 }
6960 }
6961 }
6962
6963 /* Value is zero if end of buffer reached. */
6964 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6965 return success_p;
6966 }
6967
6968
6969 /* Run the redisplay end trigger hook for IT. */
6970
6971 static void
6972 run_redisplay_end_trigger_hook (struct it *it)
6973 {
6974 Lisp_Object args[3];
6975
6976 /* IT->glyph_row should be non-null, i.e. we should be actually
6977 displaying something, or otherwise we should not run the hook. */
6978 xassert (it->glyph_row);
6979
6980 /* Set up hook arguments. */
6981 args[0] = Qredisplay_end_trigger_functions;
6982 args[1] = it->window;
6983 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6984 it->redisplay_end_trigger_charpos = 0;
6985
6986 /* Since we are *trying* to run these functions, don't try to run
6987 them again, even if they get an error. */
6988 it->w->redisplay_end_trigger = Qnil;
6989 Frun_hook_with_args (3, args);
6990
6991 /* Notice if it changed the face of the character we are on. */
6992 handle_face_prop (it);
6993 }
6994
6995
6996 /* Deliver a composition display element. Unlike the other
6997 next_element_from_XXX, this function is not registered in the array
6998 get_next_element[]. It is called from next_element_from_buffer and
6999 next_element_from_string when necessary. */
7000
7001 static int
7002 next_element_from_composition (struct it *it)
7003 {
7004 it->what = IT_COMPOSITION;
7005 it->len = it->cmp_it.nbytes;
7006 if (STRINGP (it->string))
7007 {
7008 if (it->c < 0)
7009 {
7010 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7011 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7012 return 0;
7013 }
7014 it->position = it->current.string_pos;
7015 it->object = it->string;
7016 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7017 IT_STRING_BYTEPOS (*it), it->string);
7018 }
7019 else
7020 {
7021 if (it->c < 0)
7022 {
7023 IT_CHARPOS (*it) += it->cmp_it.nchars;
7024 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7025 if (it->bidi_p)
7026 {
7027 if (it->bidi_it.new_paragraph)
7028 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7029 /* Resync the bidi iterator with IT's new position.
7030 FIXME: this doesn't support bidirectional text. */
7031 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7032 bidi_move_to_visually_next (&it->bidi_it);
7033 }
7034 return 0;
7035 }
7036 it->position = it->current.pos;
7037 it->object = it->w->buffer;
7038 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7039 IT_BYTEPOS (*it), Qnil);
7040 }
7041 return 1;
7042 }
7043
7044
7045 \f
7046 /***********************************************************************
7047 Moving an iterator without producing glyphs
7048 ***********************************************************************/
7049
7050 /* Check if iterator is at a position corresponding to a valid buffer
7051 position after some move_it_ call. */
7052
7053 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7054 ((it)->method == GET_FROM_STRING \
7055 ? IT_STRING_CHARPOS (*it) == 0 \
7056 : 1)
7057
7058
7059 /* Move iterator IT to a specified buffer or X position within one
7060 line on the display without producing glyphs.
7061
7062 OP should be a bit mask including some or all of these bits:
7063 MOVE_TO_X: Stop upon reaching x-position TO_X.
7064 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7065 Regardless of OP's value, stop upon reaching the end of the display line.
7066
7067 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7068 This means, in particular, that TO_X includes window's horizontal
7069 scroll amount.
7070
7071 The return value has several possible values that
7072 say what condition caused the scan to stop:
7073
7074 MOVE_POS_MATCH_OR_ZV
7075 - when TO_POS or ZV was reached.
7076
7077 MOVE_X_REACHED
7078 -when TO_X was reached before TO_POS or ZV were reached.
7079
7080 MOVE_LINE_CONTINUED
7081 - when we reached the end of the display area and the line must
7082 be continued.
7083
7084 MOVE_LINE_TRUNCATED
7085 - when we reached the end of the display area and the line is
7086 truncated.
7087
7088 MOVE_NEWLINE_OR_CR
7089 - when we stopped at a line end, i.e. a newline or a CR and selective
7090 display is on. */
7091
7092 static enum move_it_result
7093 move_it_in_display_line_to (struct it *it,
7094 EMACS_INT to_charpos, int to_x,
7095 enum move_operation_enum op)
7096 {
7097 enum move_it_result result = MOVE_UNDEFINED;
7098 struct glyph_row *saved_glyph_row;
7099 struct it wrap_it, atpos_it, atx_it;
7100 int may_wrap = 0;
7101 enum it_method prev_method = it->method;
7102 EMACS_INT prev_pos = IT_CHARPOS (*it);
7103
7104 /* Don't produce glyphs in produce_glyphs. */
7105 saved_glyph_row = it->glyph_row;
7106 it->glyph_row = NULL;
7107
7108 /* Use wrap_it to save a copy of IT wherever a word wrap could
7109 occur. Use atpos_it to save a copy of IT at the desired buffer
7110 position, if found, so that we can scan ahead and check if the
7111 word later overshoots the window edge. Use atx_it similarly, for
7112 pixel positions. */
7113 wrap_it.sp = -1;
7114 atpos_it.sp = -1;
7115 atx_it.sp = -1;
7116
7117 #define BUFFER_POS_REACHED_P() \
7118 ((op & MOVE_TO_POS) != 0 \
7119 && BUFFERP (it->object) \
7120 && (IT_CHARPOS (*it) == to_charpos \
7121 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7122 && (it->method == GET_FROM_BUFFER \
7123 || (it->method == GET_FROM_DISPLAY_VECTOR \
7124 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7125
7126 /* If there's a line-/wrap-prefix, handle it. */
7127 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7128 && it->current_y < it->last_visible_y)
7129 handle_line_prefix (it);
7130
7131 while (1)
7132 {
7133 int x, i, ascent = 0, descent = 0;
7134
7135 /* Utility macro to reset an iterator with x, ascent, and descent. */
7136 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7137 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7138 (IT)->max_descent = descent)
7139
7140 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7141 glyph). */
7142 if ((op & MOVE_TO_POS) != 0
7143 && BUFFERP (it->object)
7144 && it->method == GET_FROM_BUFFER
7145 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7146 || (it->bidi_p
7147 && (prev_method == GET_FROM_IMAGE
7148 || prev_method == GET_FROM_STRETCH)
7149 /* Passed TO_CHARPOS from left to right. */
7150 && ((prev_pos < to_charpos
7151 && IT_CHARPOS (*it) > to_charpos)
7152 /* Passed TO_CHARPOS from right to left. */
7153 || (prev_pos > to_charpos
7154 && IT_CHARPOS (*it) < to_charpos)))))
7155 {
7156 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7157 {
7158 result = MOVE_POS_MATCH_OR_ZV;
7159 break;
7160 }
7161 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7162 /* If wrap_it is valid, the current position might be in a
7163 word that is wrapped. So, save the iterator in
7164 atpos_it and continue to see if wrapping happens. */
7165 atpos_it = *it;
7166 }
7167
7168 prev_method = it->method;
7169 if (it->method == GET_FROM_BUFFER)
7170 prev_pos = IT_CHARPOS (*it);
7171 /* Stop when ZV reached.
7172 We used to stop here when TO_CHARPOS reached as well, but that is
7173 too soon if this glyph does not fit on this line. So we handle it
7174 explicitly below. */
7175 if (!get_next_display_element (it))
7176 {
7177 result = MOVE_POS_MATCH_OR_ZV;
7178 break;
7179 }
7180
7181 if (it->line_wrap == TRUNCATE)
7182 {
7183 if (BUFFER_POS_REACHED_P ())
7184 {
7185 result = MOVE_POS_MATCH_OR_ZV;
7186 break;
7187 }
7188 }
7189 else
7190 {
7191 if (it->line_wrap == WORD_WRAP)
7192 {
7193 if (IT_DISPLAYING_WHITESPACE (it))
7194 may_wrap = 1;
7195 else if (may_wrap)
7196 {
7197 /* We have reached a glyph that follows one or more
7198 whitespace characters. If the position is
7199 already found, we are done. */
7200 if (atpos_it.sp >= 0)
7201 {
7202 *it = atpos_it;
7203 result = MOVE_POS_MATCH_OR_ZV;
7204 goto done;
7205 }
7206 if (atx_it.sp >= 0)
7207 {
7208 *it = atx_it;
7209 result = MOVE_X_REACHED;
7210 goto done;
7211 }
7212 /* Otherwise, we can wrap here. */
7213 wrap_it = *it;
7214 may_wrap = 0;
7215 }
7216 }
7217 }
7218
7219 /* Remember the line height for the current line, in case
7220 the next element doesn't fit on the line. */
7221 ascent = it->max_ascent;
7222 descent = it->max_descent;
7223
7224 /* The call to produce_glyphs will get the metrics of the
7225 display element IT is loaded with. Record the x-position
7226 before this display element, in case it doesn't fit on the
7227 line. */
7228 x = it->current_x;
7229
7230 PRODUCE_GLYPHS (it);
7231
7232 if (it->area != TEXT_AREA)
7233 {
7234 set_iterator_to_next (it, 1);
7235 continue;
7236 }
7237
7238 /* The number of glyphs we get back in IT->nglyphs will normally
7239 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7240 character on a terminal frame, or (iii) a line end. For the
7241 second case, IT->nglyphs - 1 padding glyphs will be present.
7242 (On X frames, there is only one glyph produced for a
7243 composite character.)
7244
7245 The behavior implemented below means, for continuation lines,
7246 that as many spaces of a TAB as fit on the current line are
7247 displayed there. For terminal frames, as many glyphs of a
7248 multi-glyph character are displayed in the current line, too.
7249 This is what the old redisplay code did, and we keep it that
7250 way. Under X, the whole shape of a complex character must
7251 fit on the line or it will be completely displayed in the
7252 next line.
7253
7254 Note that both for tabs and padding glyphs, all glyphs have
7255 the same width. */
7256 if (it->nglyphs)
7257 {
7258 /* More than one glyph or glyph doesn't fit on line. All
7259 glyphs have the same width. */
7260 int single_glyph_width = it->pixel_width / it->nglyphs;
7261 int new_x;
7262 int x_before_this_char = x;
7263 int hpos_before_this_char = it->hpos;
7264
7265 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7266 {
7267 new_x = x + single_glyph_width;
7268
7269 /* We want to leave anything reaching TO_X to the caller. */
7270 if ((op & MOVE_TO_X) && new_x > to_x)
7271 {
7272 if (BUFFER_POS_REACHED_P ())
7273 {
7274 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7275 goto buffer_pos_reached;
7276 if (atpos_it.sp < 0)
7277 {
7278 atpos_it = *it;
7279 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7280 }
7281 }
7282 else
7283 {
7284 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7285 {
7286 it->current_x = x;
7287 result = MOVE_X_REACHED;
7288 break;
7289 }
7290 if (atx_it.sp < 0)
7291 {
7292 atx_it = *it;
7293 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7294 }
7295 }
7296 }
7297
7298 if (/* Lines are continued. */
7299 it->line_wrap != TRUNCATE
7300 && (/* And glyph doesn't fit on the line. */
7301 new_x > it->last_visible_x
7302 /* Or it fits exactly and we're on a window
7303 system frame. */
7304 || (new_x == it->last_visible_x
7305 && FRAME_WINDOW_P (it->f))))
7306 {
7307 if (/* IT->hpos == 0 means the very first glyph
7308 doesn't fit on the line, e.g. a wide image. */
7309 it->hpos == 0
7310 || (new_x == it->last_visible_x
7311 && FRAME_WINDOW_P (it->f)))
7312 {
7313 ++it->hpos;
7314 it->current_x = new_x;
7315
7316 /* The character's last glyph just barely fits
7317 in this row. */
7318 if (i == it->nglyphs - 1)
7319 {
7320 /* If this is the destination position,
7321 return a position *before* it in this row,
7322 now that we know it fits in this row. */
7323 if (BUFFER_POS_REACHED_P ())
7324 {
7325 if (it->line_wrap != WORD_WRAP
7326 || wrap_it.sp < 0)
7327 {
7328 it->hpos = hpos_before_this_char;
7329 it->current_x = x_before_this_char;
7330 result = MOVE_POS_MATCH_OR_ZV;
7331 break;
7332 }
7333 if (it->line_wrap == WORD_WRAP
7334 && atpos_it.sp < 0)
7335 {
7336 atpos_it = *it;
7337 atpos_it.current_x = x_before_this_char;
7338 atpos_it.hpos = hpos_before_this_char;
7339 }
7340 }
7341
7342 set_iterator_to_next (it, 1);
7343 /* On graphical terminals, newlines may
7344 "overflow" into the fringe if
7345 overflow-newline-into-fringe is non-nil.
7346 On text-only terminals, newlines may
7347 overflow into the last glyph on the
7348 display line.*/
7349 if (!FRAME_WINDOW_P (it->f)
7350 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7351 {
7352 if (!get_next_display_element (it))
7353 {
7354 result = MOVE_POS_MATCH_OR_ZV;
7355 break;
7356 }
7357 if (BUFFER_POS_REACHED_P ())
7358 {
7359 if (ITERATOR_AT_END_OF_LINE_P (it))
7360 result = MOVE_POS_MATCH_OR_ZV;
7361 else
7362 result = MOVE_LINE_CONTINUED;
7363 break;
7364 }
7365 if (ITERATOR_AT_END_OF_LINE_P (it))
7366 {
7367 result = MOVE_NEWLINE_OR_CR;
7368 break;
7369 }
7370 }
7371 }
7372 }
7373 else
7374 IT_RESET_X_ASCENT_DESCENT (it);
7375
7376 if (wrap_it.sp >= 0)
7377 {
7378 *it = wrap_it;
7379 atpos_it.sp = -1;
7380 atx_it.sp = -1;
7381 }
7382
7383 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7384 IT_CHARPOS (*it)));
7385 result = MOVE_LINE_CONTINUED;
7386 break;
7387 }
7388
7389 if (BUFFER_POS_REACHED_P ())
7390 {
7391 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7392 goto buffer_pos_reached;
7393 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7394 {
7395 atpos_it = *it;
7396 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7397 }
7398 }
7399
7400 if (new_x > it->first_visible_x)
7401 {
7402 /* Glyph is visible. Increment number of glyphs that
7403 would be displayed. */
7404 ++it->hpos;
7405 }
7406 }
7407
7408 if (result != MOVE_UNDEFINED)
7409 break;
7410 }
7411 else if (BUFFER_POS_REACHED_P ())
7412 {
7413 buffer_pos_reached:
7414 IT_RESET_X_ASCENT_DESCENT (it);
7415 result = MOVE_POS_MATCH_OR_ZV;
7416 break;
7417 }
7418 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7419 {
7420 /* Stop when TO_X specified and reached. This check is
7421 necessary here because of lines consisting of a line end,
7422 only. The line end will not produce any glyphs and we
7423 would never get MOVE_X_REACHED. */
7424 xassert (it->nglyphs == 0);
7425 result = MOVE_X_REACHED;
7426 break;
7427 }
7428
7429 /* Is this a line end? If yes, we're done. */
7430 if (ITERATOR_AT_END_OF_LINE_P (it))
7431 {
7432 result = MOVE_NEWLINE_OR_CR;
7433 break;
7434 }
7435
7436 if (it->method == GET_FROM_BUFFER)
7437 prev_pos = IT_CHARPOS (*it);
7438 /* The current display element has been consumed. Advance
7439 to the next. */
7440 set_iterator_to_next (it, 1);
7441
7442 /* Stop if lines are truncated and IT's current x-position is
7443 past the right edge of the window now. */
7444 if (it->line_wrap == TRUNCATE
7445 && it->current_x >= it->last_visible_x)
7446 {
7447 if (!FRAME_WINDOW_P (it->f)
7448 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7449 {
7450 if (!get_next_display_element (it)
7451 || BUFFER_POS_REACHED_P ())
7452 {
7453 result = MOVE_POS_MATCH_OR_ZV;
7454 break;
7455 }
7456 if (ITERATOR_AT_END_OF_LINE_P (it))
7457 {
7458 result = MOVE_NEWLINE_OR_CR;
7459 break;
7460 }
7461 }
7462 result = MOVE_LINE_TRUNCATED;
7463 break;
7464 }
7465 #undef IT_RESET_X_ASCENT_DESCENT
7466 }
7467
7468 #undef BUFFER_POS_REACHED_P
7469
7470 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7471 restore the saved iterator. */
7472 if (atpos_it.sp >= 0)
7473 *it = atpos_it;
7474 else if (atx_it.sp >= 0)
7475 *it = atx_it;
7476
7477 done:
7478
7479 /* Restore the iterator settings altered at the beginning of this
7480 function. */
7481 it->glyph_row = saved_glyph_row;
7482 return result;
7483 }
7484
7485 /* For external use. */
7486 void
7487 move_it_in_display_line (struct it *it,
7488 EMACS_INT to_charpos, int to_x,
7489 enum move_operation_enum op)
7490 {
7491 if (it->line_wrap == WORD_WRAP
7492 && (op & MOVE_TO_X))
7493 {
7494 struct it save_it = *it;
7495 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7496 /* When word-wrap is on, TO_X may lie past the end
7497 of a wrapped line. Then it->current is the
7498 character on the next line, so backtrack to the
7499 space before the wrap point. */
7500 if (skip == MOVE_LINE_CONTINUED)
7501 {
7502 int prev_x = max (it->current_x - 1, 0);
7503 *it = save_it;
7504 move_it_in_display_line_to
7505 (it, -1, prev_x, MOVE_TO_X);
7506 }
7507 }
7508 else
7509 move_it_in_display_line_to (it, to_charpos, to_x, op);
7510 }
7511
7512
7513 /* Move IT forward until it satisfies one or more of the criteria in
7514 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7515
7516 OP is a bit-mask that specifies where to stop, and in particular,
7517 which of those four position arguments makes a difference. See the
7518 description of enum move_operation_enum.
7519
7520 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7521 screen line, this function will set IT to the next position >
7522 TO_CHARPOS. */
7523
7524 void
7525 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7526 {
7527 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7528 int line_height, line_start_x = 0, reached = 0;
7529
7530 for (;;)
7531 {
7532 if (op & MOVE_TO_VPOS)
7533 {
7534 /* If no TO_CHARPOS and no TO_X specified, stop at the
7535 start of the line TO_VPOS. */
7536 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7537 {
7538 if (it->vpos == to_vpos)
7539 {
7540 reached = 1;
7541 break;
7542 }
7543 else
7544 skip = move_it_in_display_line_to (it, -1, -1, 0);
7545 }
7546 else
7547 {
7548 /* TO_VPOS >= 0 means stop at TO_X in the line at
7549 TO_VPOS, or at TO_POS, whichever comes first. */
7550 if (it->vpos == to_vpos)
7551 {
7552 reached = 2;
7553 break;
7554 }
7555
7556 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7557
7558 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7559 {
7560 reached = 3;
7561 break;
7562 }
7563 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7564 {
7565 /* We have reached TO_X but not in the line we want. */
7566 skip = move_it_in_display_line_to (it, to_charpos,
7567 -1, MOVE_TO_POS);
7568 if (skip == MOVE_POS_MATCH_OR_ZV)
7569 {
7570 reached = 4;
7571 break;
7572 }
7573 }
7574 }
7575 }
7576 else if (op & MOVE_TO_Y)
7577 {
7578 struct it it_backup;
7579
7580 if (it->line_wrap == WORD_WRAP)
7581 it_backup = *it;
7582
7583 /* TO_Y specified means stop at TO_X in the line containing
7584 TO_Y---or at TO_CHARPOS if this is reached first. The
7585 problem is that we can't really tell whether the line
7586 contains TO_Y before we have completely scanned it, and
7587 this may skip past TO_X. What we do is to first scan to
7588 TO_X.
7589
7590 If TO_X is not specified, use a TO_X of zero. The reason
7591 is to make the outcome of this function more predictable.
7592 If we didn't use TO_X == 0, we would stop at the end of
7593 the line which is probably not what a caller would expect
7594 to happen. */
7595 skip = move_it_in_display_line_to
7596 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7597 (MOVE_TO_X | (op & MOVE_TO_POS)));
7598
7599 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7600 if (skip == MOVE_POS_MATCH_OR_ZV)
7601 reached = 5;
7602 else if (skip == MOVE_X_REACHED)
7603 {
7604 /* If TO_X was reached, we want to know whether TO_Y is
7605 in the line. We know this is the case if the already
7606 scanned glyphs make the line tall enough. Otherwise,
7607 we must check by scanning the rest of the line. */
7608 line_height = it->max_ascent + it->max_descent;
7609 if (to_y >= it->current_y
7610 && to_y < it->current_y + line_height)
7611 {
7612 reached = 6;
7613 break;
7614 }
7615 it_backup = *it;
7616 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7617 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7618 op & MOVE_TO_POS);
7619 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7620 line_height = it->max_ascent + it->max_descent;
7621 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7622
7623 if (to_y >= it->current_y
7624 && to_y < it->current_y + line_height)
7625 {
7626 /* If TO_Y is in this line and TO_X was reached
7627 above, we scanned too far. We have to restore
7628 IT's settings to the ones before skipping. */
7629 *it = it_backup;
7630 reached = 6;
7631 }
7632 else
7633 {
7634 skip = skip2;
7635 if (skip == MOVE_POS_MATCH_OR_ZV)
7636 reached = 7;
7637 }
7638 }
7639 else
7640 {
7641 /* Check whether TO_Y is in this line. */
7642 line_height = it->max_ascent + it->max_descent;
7643 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7644
7645 if (to_y >= it->current_y
7646 && to_y < it->current_y + line_height)
7647 {
7648 /* When word-wrap is on, TO_X may lie past the end
7649 of a wrapped line. Then it->current is the
7650 character on the next line, so backtrack to the
7651 space before the wrap point. */
7652 if (skip == MOVE_LINE_CONTINUED
7653 && it->line_wrap == WORD_WRAP)
7654 {
7655 int prev_x = max (it->current_x - 1, 0);
7656 *it = it_backup;
7657 skip = move_it_in_display_line_to
7658 (it, -1, prev_x, MOVE_TO_X);
7659 }
7660 reached = 6;
7661 }
7662 }
7663
7664 if (reached)
7665 break;
7666 }
7667 else if (BUFFERP (it->object)
7668 && (it->method == GET_FROM_BUFFER
7669 || it->method == GET_FROM_STRETCH)
7670 && IT_CHARPOS (*it) >= to_charpos)
7671 skip = MOVE_POS_MATCH_OR_ZV;
7672 else
7673 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7674
7675 switch (skip)
7676 {
7677 case MOVE_POS_MATCH_OR_ZV:
7678 reached = 8;
7679 goto out;
7680
7681 case MOVE_NEWLINE_OR_CR:
7682 set_iterator_to_next (it, 1);
7683 it->continuation_lines_width = 0;
7684 break;
7685
7686 case MOVE_LINE_TRUNCATED:
7687 it->continuation_lines_width = 0;
7688 reseat_at_next_visible_line_start (it, 0);
7689 if ((op & MOVE_TO_POS) != 0
7690 && IT_CHARPOS (*it) > to_charpos)
7691 {
7692 reached = 9;
7693 goto out;
7694 }
7695 break;
7696
7697 case MOVE_LINE_CONTINUED:
7698 /* For continued lines ending in a tab, some of the glyphs
7699 associated with the tab are displayed on the current
7700 line. Since it->current_x does not include these glyphs,
7701 we use it->last_visible_x instead. */
7702 if (it->c == '\t')
7703 {
7704 it->continuation_lines_width += it->last_visible_x;
7705 /* When moving by vpos, ensure that the iterator really
7706 advances to the next line (bug#847, bug#969). Fixme:
7707 do we need to do this in other circumstances? */
7708 if (it->current_x != it->last_visible_x
7709 && (op & MOVE_TO_VPOS)
7710 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7711 {
7712 line_start_x = it->current_x + it->pixel_width
7713 - it->last_visible_x;
7714 set_iterator_to_next (it, 0);
7715 }
7716 }
7717 else
7718 it->continuation_lines_width += it->current_x;
7719 break;
7720
7721 default:
7722 abort ();
7723 }
7724
7725 /* Reset/increment for the next run. */
7726 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7727 it->current_x = line_start_x;
7728 line_start_x = 0;
7729 it->hpos = 0;
7730 it->current_y += it->max_ascent + it->max_descent;
7731 ++it->vpos;
7732 last_height = it->max_ascent + it->max_descent;
7733 last_max_ascent = it->max_ascent;
7734 it->max_ascent = it->max_descent = 0;
7735 }
7736
7737 out:
7738
7739 /* On text terminals, we may stop at the end of a line in the middle
7740 of a multi-character glyph. If the glyph itself is continued,
7741 i.e. it is actually displayed on the next line, don't treat this
7742 stopping point as valid; move to the next line instead (unless
7743 that brings us offscreen). */
7744 if (!FRAME_WINDOW_P (it->f)
7745 && op & MOVE_TO_POS
7746 && IT_CHARPOS (*it) == to_charpos
7747 && it->what == IT_CHARACTER
7748 && it->nglyphs > 1
7749 && it->line_wrap == WINDOW_WRAP
7750 && it->current_x == it->last_visible_x - 1
7751 && it->c != '\n'
7752 && it->c != '\t'
7753 && it->vpos < XFASTINT (it->w->window_end_vpos))
7754 {
7755 it->continuation_lines_width += it->current_x;
7756 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7757 it->current_y += it->max_ascent + it->max_descent;
7758 ++it->vpos;
7759 last_height = it->max_ascent + it->max_descent;
7760 last_max_ascent = it->max_ascent;
7761 }
7762
7763 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7764 }
7765
7766
7767 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7768
7769 If DY > 0, move IT backward at least that many pixels. DY = 0
7770 means move IT backward to the preceding line start or BEGV. This
7771 function may move over more than DY pixels if IT->current_y - DY
7772 ends up in the middle of a line; in this case IT->current_y will be
7773 set to the top of the line moved to. */
7774
7775 void
7776 move_it_vertically_backward (struct it *it, int dy)
7777 {
7778 int nlines, h;
7779 struct it it2, it3;
7780 EMACS_INT start_pos;
7781
7782 move_further_back:
7783 xassert (dy >= 0);
7784
7785 start_pos = IT_CHARPOS (*it);
7786
7787 /* Estimate how many newlines we must move back. */
7788 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7789
7790 /* Set the iterator's position that many lines back. */
7791 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7792 back_to_previous_visible_line_start (it);
7793
7794 /* Reseat the iterator here. When moving backward, we don't want
7795 reseat to skip forward over invisible text, set up the iterator
7796 to deliver from overlay strings at the new position etc. So,
7797 use reseat_1 here. */
7798 reseat_1 (it, it->current.pos, 1);
7799
7800 /* We are now surely at a line start. */
7801 it->current_x = it->hpos = 0;
7802 it->continuation_lines_width = 0;
7803
7804 /* Move forward and see what y-distance we moved. First move to the
7805 start of the next line so that we get its height. We need this
7806 height to be able to tell whether we reached the specified
7807 y-distance. */
7808 it2 = *it;
7809 it2.max_ascent = it2.max_descent = 0;
7810 do
7811 {
7812 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7813 MOVE_TO_POS | MOVE_TO_VPOS);
7814 }
7815 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7816 xassert (IT_CHARPOS (*it) >= BEGV);
7817 it3 = it2;
7818
7819 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7820 xassert (IT_CHARPOS (*it) >= BEGV);
7821 /* H is the actual vertical distance from the position in *IT
7822 and the starting position. */
7823 h = it2.current_y - it->current_y;
7824 /* NLINES is the distance in number of lines. */
7825 nlines = it2.vpos - it->vpos;
7826
7827 /* Correct IT's y and vpos position
7828 so that they are relative to the starting point. */
7829 it->vpos -= nlines;
7830 it->current_y -= h;
7831
7832 if (dy == 0)
7833 {
7834 /* DY == 0 means move to the start of the screen line. The
7835 value of nlines is > 0 if continuation lines were involved. */
7836 if (nlines > 0)
7837 move_it_by_lines (it, nlines, 1);
7838 }
7839 else
7840 {
7841 /* The y-position we try to reach, relative to *IT.
7842 Note that H has been subtracted in front of the if-statement. */
7843 int target_y = it->current_y + h - dy;
7844 int y0 = it3.current_y;
7845 int y1 = line_bottom_y (&it3);
7846 int line_height = y1 - y0;
7847
7848 /* If we did not reach target_y, try to move further backward if
7849 we can. If we moved too far backward, try to move forward. */
7850 if (target_y < it->current_y
7851 /* This is heuristic. In a window that's 3 lines high, with
7852 a line height of 13 pixels each, recentering with point
7853 on the bottom line will try to move -39/2 = 19 pixels
7854 backward. Try to avoid moving into the first line. */
7855 && (it->current_y - target_y
7856 > min (window_box_height (it->w), line_height * 2 / 3))
7857 && IT_CHARPOS (*it) > BEGV)
7858 {
7859 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7860 target_y - it->current_y));
7861 dy = it->current_y - target_y;
7862 goto move_further_back;
7863 }
7864 else if (target_y >= it->current_y + line_height
7865 && IT_CHARPOS (*it) < ZV)
7866 {
7867 /* Should move forward by at least one line, maybe more.
7868
7869 Note: Calling move_it_by_lines can be expensive on
7870 terminal frames, where compute_motion is used (via
7871 vmotion) to do the job, when there are very long lines
7872 and truncate-lines is nil. That's the reason for
7873 treating terminal frames specially here. */
7874
7875 if (!FRAME_WINDOW_P (it->f))
7876 move_it_vertically (it, target_y - (it->current_y + line_height));
7877 else
7878 {
7879 do
7880 {
7881 move_it_by_lines (it, 1, 1);
7882 }
7883 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7884 }
7885 }
7886 }
7887 }
7888
7889
7890 /* Move IT by a specified amount of pixel lines DY. DY negative means
7891 move backwards. DY = 0 means move to start of screen line. At the
7892 end, IT will be on the start of a screen line. */
7893
7894 void
7895 move_it_vertically (struct it *it, int dy)
7896 {
7897 if (dy <= 0)
7898 move_it_vertically_backward (it, -dy);
7899 else
7900 {
7901 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7902 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7903 MOVE_TO_POS | MOVE_TO_Y);
7904 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7905
7906 /* If buffer ends in ZV without a newline, move to the start of
7907 the line to satisfy the post-condition. */
7908 if (IT_CHARPOS (*it) == ZV
7909 && ZV > BEGV
7910 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7911 move_it_by_lines (it, 0, 0);
7912 }
7913 }
7914
7915
7916 /* Move iterator IT past the end of the text line it is in. */
7917
7918 void
7919 move_it_past_eol (struct it *it)
7920 {
7921 enum move_it_result rc;
7922
7923 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7924 if (rc == MOVE_NEWLINE_OR_CR)
7925 set_iterator_to_next (it, 0);
7926 }
7927
7928
7929 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7930 negative means move up. DVPOS == 0 means move to the start of the
7931 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7932 NEED_Y_P is zero, IT->current_y will be left unchanged.
7933
7934 Further optimization ideas: If we would know that IT->f doesn't use
7935 a face with proportional font, we could be faster for
7936 truncate-lines nil. */
7937
7938 void
7939 move_it_by_lines (struct it *it, int dvpos, int need_y_p)
7940 {
7941
7942 /* The commented-out optimization uses vmotion on terminals. This
7943 gives bad results, because elements like it->what, on which
7944 callers such as pos_visible_p rely, aren't updated. */
7945 /* struct position pos;
7946 if (!FRAME_WINDOW_P (it->f))
7947 {
7948 struct text_pos textpos;
7949
7950 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7951 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7952 reseat (it, textpos, 1);
7953 it->vpos += pos.vpos;
7954 it->current_y += pos.vpos;
7955 }
7956 else */
7957
7958 if (dvpos == 0)
7959 {
7960 /* DVPOS == 0 means move to the start of the screen line. */
7961 move_it_vertically_backward (it, 0);
7962 xassert (it->current_x == 0 && it->hpos == 0);
7963 /* Let next call to line_bottom_y calculate real line height */
7964 last_height = 0;
7965 }
7966 else if (dvpos > 0)
7967 {
7968 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7969 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7970 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7971 }
7972 else
7973 {
7974 struct it it2;
7975 EMACS_INT start_charpos, i;
7976
7977 /* Start at the beginning of the screen line containing IT's
7978 position. This may actually move vertically backwards,
7979 in case of overlays, so adjust dvpos accordingly. */
7980 dvpos += it->vpos;
7981 move_it_vertically_backward (it, 0);
7982 dvpos -= it->vpos;
7983
7984 /* Go back -DVPOS visible lines and reseat the iterator there. */
7985 start_charpos = IT_CHARPOS (*it);
7986 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7987 back_to_previous_visible_line_start (it);
7988 reseat (it, it->current.pos, 1);
7989
7990 /* Move further back if we end up in a string or an image. */
7991 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7992 {
7993 /* First try to move to start of display line. */
7994 dvpos += it->vpos;
7995 move_it_vertically_backward (it, 0);
7996 dvpos -= it->vpos;
7997 if (IT_POS_VALID_AFTER_MOVE_P (it))
7998 break;
7999 /* If start of line is still in string or image,
8000 move further back. */
8001 back_to_previous_visible_line_start (it);
8002 reseat (it, it->current.pos, 1);
8003 dvpos--;
8004 }
8005
8006 it->current_x = it->hpos = 0;
8007
8008 /* Above call may have moved too far if continuation lines
8009 are involved. Scan forward and see if it did. */
8010 it2 = *it;
8011 it2.vpos = it2.current_y = 0;
8012 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8013 it->vpos -= it2.vpos;
8014 it->current_y -= it2.current_y;
8015 it->current_x = it->hpos = 0;
8016
8017 /* If we moved too far back, move IT some lines forward. */
8018 if (it2.vpos > -dvpos)
8019 {
8020 int delta = it2.vpos + dvpos;
8021 it2 = *it;
8022 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8023 /* Move back again if we got too far ahead. */
8024 if (IT_CHARPOS (*it) >= start_charpos)
8025 *it = it2;
8026 }
8027 }
8028 }
8029
8030 /* Return 1 if IT points into the middle of a display vector. */
8031
8032 int
8033 in_display_vector_p (struct it *it)
8034 {
8035 return (it->method == GET_FROM_DISPLAY_VECTOR
8036 && it->current.dpvec_index > 0
8037 && it->dpvec + it->current.dpvec_index != it->dpend);
8038 }
8039
8040 \f
8041 /***********************************************************************
8042 Messages
8043 ***********************************************************************/
8044
8045
8046 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8047 to *Messages*. */
8048
8049 void
8050 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8051 {
8052 Lisp_Object args[3];
8053 Lisp_Object msg, fmt;
8054 char *buffer;
8055 EMACS_INT len;
8056 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8057 USE_SAFE_ALLOCA;
8058
8059 /* Do nothing if called asynchronously. Inserting text into
8060 a buffer may call after-change-functions and alike and
8061 that would means running Lisp asynchronously. */
8062 if (handling_signal)
8063 return;
8064
8065 fmt = msg = Qnil;
8066 GCPRO4 (fmt, msg, arg1, arg2);
8067
8068 args[0] = fmt = build_string (format);
8069 args[1] = arg1;
8070 args[2] = arg2;
8071 msg = Fformat (3, args);
8072
8073 len = SBYTES (msg) + 1;
8074 SAFE_ALLOCA (buffer, char *, len);
8075 memcpy (buffer, SDATA (msg), len);
8076
8077 message_dolog (buffer, len - 1, 1, 0);
8078 SAFE_FREE ();
8079
8080 UNGCPRO;
8081 }
8082
8083
8084 /* Output a newline in the *Messages* buffer if "needs" one. */
8085
8086 void
8087 message_log_maybe_newline (void)
8088 {
8089 if (message_log_need_newline)
8090 message_dolog ("", 0, 1, 0);
8091 }
8092
8093
8094 /* Add a string M of length NBYTES to the message log, optionally
8095 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8096 nonzero, means interpret the contents of M as multibyte. This
8097 function calls low-level routines in order to bypass text property
8098 hooks, etc. which might not be safe to run.
8099
8100 This may GC (insert may run before/after change hooks),
8101 so the buffer M must NOT point to a Lisp string. */
8102
8103 void
8104 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8105 {
8106 if (!NILP (Vmemory_full))
8107 return;
8108
8109 if (!NILP (Vmessage_log_max))
8110 {
8111 struct buffer *oldbuf;
8112 Lisp_Object oldpoint, oldbegv, oldzv;
8113 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8114 EMACS_INT point_at_end = 0;
8115 EMACS_INT zv_at_end = 0;
8116 Lisp_Object old_deactivate_mark, tem;
8117 struct gcpro gcpro1;
8118
8119 old_deactivate_mark = Vdeactivate_mark;
8120 oldbuf = current_buffer;
8121 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8122 current_buffer->undo_list = Qt;
8123
8124 oldpoint = message_dolog_marker1;
8125 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8126 oldbegv = message_dolog_marker2;
8127 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8128 oldzv = message_dolog_marker3;
8129 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8130 GCPRO1 (old_deactivate_mark);
8131
8132 if (PT == Z)
8133 point_at_end = 1;
8134 if (ZV == Z)
8135 zv_at_end = 1;
8136
8137 BEGV = BEG;
8138 BEGV_BYTE = BEG_BYTE;
8139 ZV = Z;
8140 ZV_BYTE = Z_BYTE;
8141 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8142
8143 /* Insert the string--maybe converting multibyte to single byte
8144 or vice versa, so that all the text fits the buffer. */
8145 if (multibyte
8146 && NILP (current_buffer->enable_multibyte_characters))
8147 {
8148 EMACS_INT i;
8149 int c, char_bytes;
8150 unsigned char work[1];
8151
8152 /* Convert a multibyte string to single-byte
8153 for the *Message* buffer. */
8154 for (i = 0; i < nbytes; i += char_bytes)
8155 {
8156 c = string_char_and_length (m + i, &char_bytes);
8157 work[0] = (ASCII_CHAR_P (c)
8158 ? c
8159 : multibyte_char_to_unibyte (c, Qnil));
8160 insert_1_both (work, 1, 1, 1, 0, 0);
8161 }
8162 }
8163 else if (! multibyte
8164 && ! NILP (current_buffer->enable_multibyte_characters))
8165 {
8166 EMACS_INT i;
8167 int c, char_bytes;
8168 unsigned char *msg = (unsigned char *) m;
8169 unsigned char str[MAX_MULTIBYTE_LENGTH];
8170 /* Convert a single-byte string to multibyte
8171 for the *Message* buffer. */
8172 for (i = 0; i < nbytes; i++)
8173 {
8174 c = msg[i];
8175 MAKE_CHAR_MULTIBYTE (c);
8176 char_bytes = CHAR_STRING (c, str);
8177 insert_1_both (str, 1, char_bytes, 1, 0, 0);
8178 }
8179 }
8180 else if (nbytes)
8181 insert_1 (m, nbytes, 1, 0, 0);
8182
8183 if (nlflag)
8184 {
8185 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8186 int dup;
8187 insert_1 ("\n", 1, 1, 0, 0);
8188
8189 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8190 this_bol = PT;
8191 this_bol_byte = PT_BYTE;
8192
8193 /* See if this line duplicates the previous one.
8194 If so, combine duplicates. */
8195 if (this_bol > BEG)
8196 {
8197 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8198 prev_bol = PT;
8199 prev_bol_byte = PT_BYTE;
8200
8201 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8202 this_bol, this_bol_byte);
8203 if (dup)
8204 {
8205 del_range_both (prev_bol, prev_bol_byte,
8206 this_bol, this_bol_byte, 0);
8207 if (dup > 1)
8208 {
8209 char dupstr[40];
8210 int duplen;
8211
8212 /* If you change this format, don't forget to also
8213 change message_log_check_duplicate. */
8214 sprintf (dupstr, " [%d times]", dup);
8215 duplen = strlen (dupstr);
8216 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8217 insert_1 (dupstr, duplen, 1, 0, 1);
8218 }
8219 }
8220 }
8221
8222 /* If we have more than the desired maximum number of lines
8223 in the *Messages* buffer now, delete the oldest ones.
8224 This is safe because we don't have undo in this buffer. */
8225
8226 if (NATNUMP (Vmessage_log_max))
8227 {
8228 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8229 -XFASTINT (Vmessage_log_max) - 1, 0);
8230 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8231 }
8232 }
8233 BEGV = XMARKER (oldbegv)->charpos;
8234 BEGV_BYTE = marker_byte_position (oldbegv);
8235
8236 if (zv_at_end)
8237 {
8238 ZV = Z;
8239 ZV_BYTE = Z_BYTE;
8240 }
8241 else
8242 {
8243 ZV = XMARKER (oldzv)->charpos;
8244 ZV_BYTE = marker_byte_position (oldzv);
8245 }
8246
8247 if (point_at_end)
8248 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8249 else
8250 /* We can't do Fgoto_char (oldpoint) because it will run some
8251 Lisp code. */
8252 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8253 XMARKER (oldpoint)->bytepos);
8254
8255 UNGCPRO;
8256 unchain_marker (XMARKER (oldpoint));
8257 unchain_marker (XMARKER (oldbegv));
8258 unchain_marker (XMARKER (oldzv));
8259
8260 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8261 set_buffer_internal (oldbuf);
8262 if (NILP (tem))
8263 windows_or_buffers_changed = old_windows_or_buffers_changed;
8264 message_log_need_newline = !nlflag;
8265 Vdeactivate_mark = old_deactivate_mark;
8266 }
8267 }
8268
8269
8270 /* We are at the end of the buffer after just having inserted a newline.
8271 (Note: We depend on the fact we won't be crossing the gap.)
8272 Check to see if the most recent message looks a lot like the previous one.
8273 Return 0 if different, 1 if the new one should just replace it, or a
8274 value N > 1 if we should also append " [N times]". */
8275
8276 static int
8277 message_log_check_duplicate (EMACS_INT prev_bol, EMACS_INT prev_bol_byte,
8278 EMACS_INT this_bol, EMACS_INT this_bol_byte)
8279 {
8280 EMACS_INT i;
8281 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8282 int seen_dots = 0;
8283 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8284 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8285
8286 for (i = 0; i < len; i++)
8287 {
8288 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8289 seen_dots = 1;
8290 if (p1[i] != p2[i])
8291 return seen_dots;
8292 }
8293 p1 += len;
8294 if (*p1 == '\n')
8295 return 2;
8296 if (*p1++ == ' ' && *p1++ == '[')
8297 {
8298 int n = 0;
8299 while (*p1 >= '0' && *p1 <= '9')
8300 n = n * 10 + *p1++ - '0';
8301 if (strncmp (p1, " times]\n", 8) == 0)
8302 return n+1;
8303 }
8304 return 0;
8305 }
8306 \f
8307
8308 /* Display an echo area message M with a specified length of NBYTES
8309 bytes. The string may include null characters. If M is 0, clear
8310 out any existing message, and let the mini-buffer text show
8311 through.
8312
8313 This may GC, so the buffer M must NOT point to a Lisp string. */
8314
8315 void
8316 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8317 {
8318 /* First flush out any partial line written with print. */
8319 message_log_maybe_newline ();
8320 if (m)
8321 message_dolog (m, nbytes, 1, multibyte);
8322 message2_nolog (m, nbytes, multibyte);
8323 }
8324
8325
8326 /* The non-logging counterpart of message2. */
8327
8328 void
8329 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8330 {
8331 struct frame *sf = SELECTED_FRAME ();
8332 message_enable_multibyte = multibyte;
8333
8334 if (FRAME_INITIAL_P (sf))
8335 {
8336 if (noninteractive_need_newline)
8337 putc ('\n', stderr);
8338 noninteractive_need_newline = 0;
8339 if (m)
8340 fwrite (m, nbytes, 1, stderr);
8341 if (cursor_in_echo_area == 0)
8342 fprintf (stderr, "\n");
8343 fflush (stderr);
8344 }
8345 /* A null message buffer means that the frame hasn't really been
8346 initialized yet. Error messages get reported properly by
8347 cmd_error, so this must be just an informative message; toss it. */
8348 else if (INTERACTIVE
8349 && sf->glyphs_initialized_p
8350 && FRAME_MESSAGE_BUF (sf))
8351 {
8352 Lisp_Object mini_window;
8353 struct frame *f;
8354
8355 /* Get the frame containing the mini-buffer
8356 that the selected frame is using. */
8357 mini_window = FRAME_MINIBUF_WINDOW (sf);
8358 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8359
8360 FRAME_SAMPLE_VISIBILITY (f);
8361 if (FRAME_VISIBLE_P (sf)
8362 && ! FRAME_VISIBLE_P (f))
8363 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8364
8365 if (m)
8366 {
8367 set_message (m, Qnil, nbytes, multibyte);
8368 if (minibuffer_auto_raise)
8369 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8370 }
8371 else
8372 clear_message (1, 1);
8373
8374 do_pending_window_change (0);
8375 echo_area_display (1);
8376 do_pending_window_change (0);
8377 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8378 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8379 }
8380 }
8381
8382
8383 /* Display an echo area message M with a specified length of NBYTES
8384 bytes. The string may include null characters. If M is not a
8385 string, clear out any existing message, and let the mini-buffer
8386 text show through.
8387
8388 This function cancels echoing. */
8389
8390 void
8391 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8392 {
8393 struct gcpro gcpro1;
8394
8395 GCPRO1 (m);
8396 clear_message (1,1);
8397 cancel_echoing ();
8398
8399 /* First flush out any partial line written with print. */
8400 message_log_maybe_newline ();
8401 if (STRINGP (m))
8402 {
8403 char *buffer;
8404 USE_SAFE_ALLOCA;
8405
8406 SAFE_ALLOCA (buffer, char *, nbytes);
8407 memcpy (buffer, SDATA (m), nbytes);
8408 message_dolog (buffer, nbytes, 1, multibyte);
8409 SAFE_FREE ();
8410 }
8411 message3_nolog (m, nbytes, multibyte);
8412
8413 UNGCPRO;
8414 }
8415
8416
8417 /* The non-logging version of message3.
8418 This does not cancel echoing, because it is used for echoing.
8419 Perhaps we need to make a separate function for echoing
8420 and make this cancel echoing. */
8421
8422 void
8423 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8424 {
8425 struct frame *sf = SELECTED_FRAME ();
8426 message_enable_multibyte = multibyte;
8427
8428 if (FRAME_INITIAL_P (sf))
8429 {
8430 if (noninteractive_need_newline)
8431 putc ('\n', stderr);
8432 noninteractive_need_newline = 0;
8433 if (STRINGP (m))
8434 fwrite (SDATA (m), nbytes, 1, stderr);
8435 if (cursor_in_echo_area == 0)
8436 fprintf (stderr, "\n");
8437 fflush (stderr);
8438 }
8439 /* A null message buffer means that the frame hasn't really been
8440 initialized yet. Error messages get reported properly by
8441 cmd_error, so this must be just an informative message; toss it. */
8442 else if (INTERACTIVE
8443 && sf->glyphs_initialized_p
8444 && FRAME_MESSAGE_BUF (sf))
8445 {
8446 Lisp_Object mini_window;
8447 Lisp_Object frame;
8448 struct frame *f;
8449
8450 /* Get the frame containing the mini-buffer
8451 that the selected frame is using. */
8452 mini_window = FRAME_MINIBUF_WINDOW (sf);
8453 frame = XWINDOW (mini_window)->frame;
8454 f = XFRAME (frame);
8455
8456 FRAME_SAMPLE_VISIBILITY (f);
8457 if (FRAME_VISIBLE_P (sf)
8458 && !FRAME_VISIBLE_P (f))
8459 Fmake_frame_visible (frame);
8460
8461 if (STRINGP (m) && SCHARS (m) > 0)
8462 {
8463 set_message (NULL, m, nbytes, multibyte);
8464 if (minibuffer_auto_raise)
8465 Fraise_frame (frame);
8466 /* Assume we are not echoing.
8467 (If we are, echo_now will override this.) */
8468 echo_message_buffer = Qnil;
8469 }
8470 else
8471 clear_message (1, 1);
8472
8473 do_pending_window_change (0);
8474 echo_area_display (1);
8475 do_pending_window_change (0);
8476 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8477 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8478 }
8479 }
8480
8481
8482 /* Display a null-terminated echo area message M. If M is 0, clear
8483 out any existing message, and let the mini-buffer text show through.
8484
8485 The buffer M must continue to exist until after the echo area gets
8486 cleared or some other message gets displayed there. Do not pass
8487 text that is stored in a Lisp string. Do not pass text in a buffer
8488 that was alloca'd. */
8489
8490 void
8491 message1 (const char *m)
8492 {
8493 message2 (m, (m ? strlen (m) : 0), 0);
8494 }
8495
8496
8497 /* The non-logging counterpart of message1. */
8498
8499 void
8500 message1_nolog (const char *m)
8501 {
8502 message2_nolog (m, (m ? strlen (m) : 0), 0);
8503 }
8504
8505 /* Display a message M which contains a single %s
8506 which gets replaced with STRING. */
8507
8508 void
8509 message_with_string (const char *m, Lisp_Object string, int log)
8510 {
8511 CHECK_STRING (string);
8512
8513 if (noninteractive)
8514 {
8515 if (m)
8516 {
8517 if (noninteractive_need_newline)
8518 putc ('\n', stderr);
8519 noninteractive_need_newline = 0;
8520 fprintf (stderr, m, SDATA (string));
8521 if (!cursor_in_echo_area)
8522 fprintf (stderr, "\n");
8523 fflush (stderr);
8524 }
8525 }
8526 else if (INTERACTIVE)
8527 {
8528 /* The frame whose minibuffer we're going to display the message on.
8529 It may be larger than the selected frame, so we need
8530 to use its buffer, not the selected frame's buffer. */
8531 Lisp_Object mini_window;
8532 struct frame *f, *sf = SELECTED_FRAME ();
8533
8534 /* Get the frame containing the minibuffer
8535 that the selected frame is using. */
8536 mini_window = FRAME_MINIBUF_WINDOW (sf);
8537 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8538
8539 /* A null message buffer means that the frame hasn't really been
8540 initialized yet. Error messages get reported properly by
8541 cmd_error, so this must be just an informative message; toss it. */
8542 if (FRAME_MESSAGE_BUF (f))
8543 {
8544 Lisp_Object args[2], message;
8545 struct gcpro gcpro1, gcpro2;
8546
8547 args[0] = build_string (m);
8548 args[1] = message = string;
8549 GCPRO2 (args[0], message);
8550 gcpro1.nvars = 2;
8551
8552 message = Fformat (2, args);
8553
8554 if (log)
8555 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8556 else
8557 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8558
8559 UNGCPRO;
8560
8561 /* Print should start at the beginning of the message
8562 buffer next time. */
8563 message_buf_print = 0;
8564 }
8565 }
8566 }
8567
8568
8569 /* Dump an informative message to the minibuf. If M is 0, clear out
8570 any existing message, and let the mini-buffer text show through. */
8571
8572 static void
8573 vmessage (const char *m, va_list ap)
8574 {
8575 if (noninteractive)
8576 {
8577 if (m)
8578 {
8579 if (noninteractive_need_newline)
8580 putc ('\n', stderr);
8581 noninteractive_need_newline = 0;
8582 vfprintf (stderr, m, ap);
8583 if (cursor_in_echo_area == 0)
8584 fprintf (stderr, "\n");
8585 fflush (stderr);
8586 }
8587 }
8588 else if (INTERACTIVE)
8589 {
8590 /* The frame whose mini-buffer we're going to display the message
8591 on. It may be larger than the selected frame, so we need to
8592 use its buffer, not the selected frame's buffer. */
8593 Lisp_Object mini_window;
8594 struct frame *f, *sf = SELECTED_FRAME ();
8595
8596 /* Get the frame containing the mini-buffer
8597 that the selected frame is using. */
8598 mini_window = FRAME_MINIBUF_WINDOW (sf);
8599 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8600
8601 /* A null message buffer means that the frame hasn't really been
8602 initialized yet. Error messages get reported properly by
8603 cmd_error, so this must be just an informative message; toss
8604 it. */
8605 if (FRAME_MESSAGE_BUF (f))
8606 {
8607 if (m)
8608 {
8609 EMACS_INT len;
8610
8611 len = doprnt (FRAME_MESSAGE_BUF (f),
8612 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8613
8614 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8615 }
8616 else
8617 message1 (0);
8618
8619 /* Print should start at the beginning of the message
8620 buffer next time. */
8621 message_buf_print = 0;
8622 }
8623 }
8624 }
8625
8626 void
8627 message (const char *m, ...)
8628 {
8629 va_list ap;
8630 va_start (ap, m);
8631 vmessage (m, ap);
8632 va_end (ap);
8633 }
8634
8635
8636 /* The non-logging version of message. */
8637
8638 void
8639 message_nolog (const char *m, ...)
8640 {
8641 Lisp_Object old_log_max;
8642 va_list ap;
8643 va_start (ap, m);
8644 old_log_max = Vmessage_log_max;
8645 Vmessage_log_max = Qnil;
8646 vmessage (m, ap);
8647 Vmessage_log_max = old_log_max;
8648 va_end (ap);
8649 }
8650
8651
8652 /* Display the current message in the current mini-buffer. This is
8653 only called from error handlers in process.c, and is not time
8654 critical. */
8655
8656 void
8657 update_echo_area (void)
8658 {
8659 if (!NILP (echo_area_buffer[0]))
8660 {
8661 Lisp_Object string;
8662 string = Fcurrent_message ();
8663 message3 (string, SBYTES (string),
8664 !NILP (current_buffer->enable_multibyte_characters));
8665 }
8666 }
8667
8668
8669 /* Make sure echo area buffers in `echo_buffers' are live.
8670 If they aren't, make new ones. */
8671
8672 static void
8673 ensure_echo_area_buffers (void)
8674 {
8675 int i;
8676
8677 for (i = 0; i < 2; ++i)
8678 if (!BUFFERP (echo_buffer[i])
8679 || NILP (XBUFFER (echo_buffer[i])->name))
8680 {
8681 char name[30];
8682 Lisp_Object old_buffer;
8683 int j;
8684
8685 old_buffer = echo_buffer[i];
8686 sprintf (name, " *Echo Area %d*", i);
8687 echo_buffer[i] = Fget_buffer_create (build_string (name));
8688 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8689 /* to force word wrap in echo area -
8690 it was decided to postpone this*/
8691 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8692
8693 for (j = 0; j < 2; ++j)
8694 if (EQ (old_buffer, echo_area_buffer[j]))
8695 echo_area_buffer[j] = echo_buffer[i];
8696 }
8697 }
8698
8699
8700 /* Call FN with args A1..A4 with either the current or last displayed
8701 echo_area_buffer as current buffer.
8702
8703 WHICH zero means use the current message buffer
8704 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8705 from echo_buffer[] and clear it.
8706
8707 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8708 suitable buffer from echo_buffer[] and clear it.
8709
8710 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8711 that the current message becomes the last displayed one, make
8712 choose a suitable buffer for echo_area_buffer[0], and clear it.
8713
8714 Value is what FN returns. */
8715
8716 static int
8717 with_echo_area_buffer (struct window *w, int which,
8718 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
8719 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8720 {
8721 Lisp_Object buffer;
8722 int this_one, the_other, clear_buffer_p, rc;
8723 int count = SPECPDL_INDEX ();
8724
8725 /* If buffers aren't live, make new ones. */
8726 ensure_echo_area_buffers ();
8727
8728 clear_buffer_p = 0;
8729
8730 if (which == 0)
8731 this_one = 0, the_other = 1;
8732 else if (which > 0)
8733 this_one = 1, the_other = 0;
8734 else
8735 {
8736 this_one = 0, the_other = 1;
8737 clear_buffer_p = 1;
8738
8739 /* We need a fresh one in case the current echo buffer equals
8740 the one containing the last displayed echo area message. */
8741 if (!NILP (echo_area_buffer[this_one])
8742 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8743 echo_area_buffer[this_one] = Qnil;
8744 }
8745
8746 /* Choose a suitable buffer from echo_buffer[] is we don't
8747 have one. */
8748 if (NILP (echo_area_buffer[this_one]))
8749 {
8750 echo_area_buffer[this_one]
8751 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8752 ? echo_buffer[the_other]
8753 : echo_buffer[this_one]);
8754 clear_buffer_p = 1;
8755 }
8756
8757 buffer = echo_area_buffer[this_one];
8758
8759 /* Don't get confused by reusing the buffer used for echoing
8760 for a different purpose. */
8761 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8762 cancel_echoing ();
8763
8764 record_unwind_protect (unwind_with_echo_area_buffer,
8765 with_echo_area_buffer_unwind_data (w));
8766
8767 /* Make the echo area buffer current. Note that for display
8768 purposes, it is not necessary that the displayed window's buffer
8769 == current_buffer, except for text property lookup. So, let's
8770 only set that buffer temporarily here without doing a full
8771 Fset_window_buffer. We must also change w->pointm, though,
8772 because otherwise an assertions in unshow_buffer fails, and Emacs
8773 aborts. */
8774 set_buffer_internal_1 (XBUFFER (buffer));
8775 if (w)
8776 {
8777 w->buffer = buffer;
8778 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8779 }
8780
8781 current_buffer->undo_list = Qt;
8782 current_buffer->read_only = Qnil;
8783 specbind (Qinhibit_read_only, Qt);
8784 specbind (Qinhibit_modification_hooks, Qt);
8785
8786 if (clear_buffer_p && Z > BEG)
8787 del_range (BEG, Z);
8788
8789 xassert (BEGV >= BEG);
8790 xassert (ZV <= Z && ZV >= BEGV);
8791
8792 rc = fn (a1, a2, a3, a4);
8793
8794 xassert (BEGV >= BEG);
8795 xassert (ZV <= Z && ZV >= BEGV);
8796
8797 unbind_to (count, Qnil);
8798 return rc;
8799 }
8800
8801
8802 /* Save state that should be preserved around the call to the function
8803 FN called in with_echo_area_buffer. */
8804
8805 static Lisp_Object
8806 with_echo_area_buffer_unwind_data (struct window *w)
8807 {
8808 int i = 0;
8809 Lisp_Object vector, tmp;
8810
8811 /* Reduce consing by keeping one vector in
8812 Vwith_echo_area_save_vector. */
8813 vector = Vwith_echo_area_save_vector;
8814 Vwith_echo_area_save_vector = Qnil;
8815
8816 if (NILP (vector))
8817 vector = Fmake_vector (make_number (7), Qnil);
8818
8819 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8820 ASET (vector, i, Vdeactivate_mark); ++i;
8821 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8822
8823 if (w)
8824 {
8825 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8826 ASET (vector, i, w->buffer); ++i;
8827 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8828 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8829 }
8830 else
8831 {
8832 int end = i + 4;
8833 for (; i < end; ++i)
8834 ASET (vector, i, Qnil);
8835 }
8836
8837 xassert (i == ASIZE (vector));
8838 return vector;
8839 }
8840
8841
8842 /* Restore global state from VECTOR which was created by
8843 with_echo_area_buffer_unwind_data. */
8844
8845 static Lisp_Object
8846 unwind_with_echo_area_buffer (Lisp_Object vector)
8847 {
8848 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8849 Vdeactivate_mark = AREF (vector, 1);
8850 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8851
8852 if (WINDOWP (AREF (vector, 3)))
8853 {
8854 struct window *w;
8855 Lisp_Object buffer, charpos, bytepos;
8856
8857 w = XWINDOW (AREF (vector, 3));
8858 buffer = AREF (vector, 4);
8859 charpos = AREF (vector, 5);
8860 bytepos = AREF (vector, 6);
8861
8862 w->buffer = buffer;
8863 set_marker_both (w->pointm, buffer,
8864 XFASTINT (charpos), XFASTINT (bytepos));
8865 }
8866
8867 Vwith_echo_area_save_vector = vector;
8868 return Qnil;
8869 }
8870
8871
8872 /* Set up the echo area for use by print functions. MULTIBYTE_P
8873 non-zero means we will print multibyte. */
8874
8875 void
8876 setup_echo_area_for_printing (int multibyte_p)
8877 {
8878 /* If we can't find an echo area any more, exit. */
8879 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8880 Fkill_emacs (Qnil);
8881
8882 ensure_echo_area_buffers ();
8883
8884 if (!message_buf_print)
8885 {
8886 /* A message has been output since the last time we printed.
8887 Choose a fresh echo area buffer. */
8888 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8889 echo_area_buffer[0] = echo_buffer[1];
8890 else
8891 echo_area_buffer[0] = echo_buffer[0];
8892
8893 /* Switch to that buffer and clear it. */
8894 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8895 current_buffer->truncate_lines = Qnil;
8896
8897 if (Z > BEG)
8898 {
8899 int count = SPECPDL_INDEX ();
8900 specbind (Qinhibit_read_only, Qt);
8901 /* Note that undo recording is always disabled. */
8902 del_range (BEG, Z);
8903 unbind_to (count, Qnil);
8904 }
8905 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8906
8907 /* Set up the buffer for the multibyteness we need. */
8908 if (multibyte_p
8909 != !NILP (current_buffer->enable_multibyte_characters))
8910 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8911
8912 /* Raise the frame containing the echo area. */
8913 if (minibuffer_auto_raise)
8914 {
8915 struct frame *sf = SELECTED_FRAME ();
8916 Lisp_Object mini_window;
8917 mini_window = FRAME_MINIBUF_WINDOW (sf);
8918 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8919 }
8920
8921 message_log_maybe_newline ();
8922 message_buf_print = 1;
8923 }
8924 else
8925 {
8926 if (NILP (echo_area_buffer[0]))
8927 {
8928 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8929 echo_area_buffer[0] = echo_buffer[1];
8930 else
8931 echo_area_buffer[0] = echo_buffer[0];
8932 }
8933
8934 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8935 {
8936 /* Someone switched buffers between print requests. */
8937 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8938 current_buffer->truncate_lines = Qnil;
8939 }
8940 }
8941 }
8942
8943
8944 /* Display an echo area message in window W. Value is non-zero if W's
8945 height is changed. If display_last_displayed_message_p is
8946 non-zero, display the message that was last displayed, otherwise
8947 display the current message. */
8948
8949 static int
8950 display_echo_area (struct window *w)
8951 {
8952 int i, no_message_p, window_height_changed_p, count;
8953
8954 /* Temporarily disable garbage collections while displaying the echo
8955 area. This is done because a GC can print a message itself.
8956 That message would modify the echo area buffer's contents while a
8957 redisplay of the buffer is going on, and seriously confuse
8958 redisplay. */
8959 count = inhibit_garbage_collection ();
8960
8961 /* If there is no message, we must call display_echo_area_1
8962 nevertheless because it resizes the window. But we will have to
8963 reset the echo_area_buffer in question to nil at the end because
8964 with_echo_area_buffer will sets it to an empty buffer. */
8965 i = display_last_displayed_message_p ? 1 : 0;
8966 no_message_p = NILP (echo_area_buffer[i]);
8967
8968 window_height_changed_p
8969 = with_echo_area_buffer (w, display_last_displayed_message_p,
8970 display_echo_area_1,
8971 (EMACS_INT) w, Qnil, 0, 0);
8972
8973 if (no_message_p)
8974 echo_area_buffer[i] = Qnil;
8975
8976 unbind_to (count, Qnil);
8977 return window_height_changed_p;
8978 }
8979
8980
8981 /* Helper for display_echo_area. Display the current buffer which
8982 contains the current echo area message in window W, a mini-window,
8983 a pointer to which is passed in A1. A2..A4 are currently not used.
8984 Change the height of W so that all of the message is displayed.
8985 Value is non-zero if height of W was changed. */
8986
8987 static int
8988 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8989 {
8990 struct window *w = (struct window *) a1;
8991 Lisp_Object window;
8992 struct text_pos start;
8993 int window_height_changed_p = 0;
8994
8995 /* Do this before displaying, so that we have a large enough glyph
8996 matrix for the display. If we can't get enough space for the
8997 whole text, display the last N lines. That works by setting w->start. */
8998 window_height_changed_p = resize_mini_window (w, 0);
8999
9000 /* Use the starting position chosen by resize_mini_window. */
9001 SET_TEXT_POS_FROM_MARKER (start, w->start);
9002
9003 /* Display. */
9004 clear_glyph_matrix (w->desired_matrix);
9005 XSETWINDOW (window, w);
9006 try_window (window, start, 0);
9007
9008 return window_height_changed_p;
9009 }
9010
9011
9012 /* Resize the echo area window to exactly the size needed for the
9013 currently displayed message, if there is one. If a mini-buffer
9014 is active, don't shrink it. */
9015
9016 void
9017 resize_echo_area_exactly (void)
9018 {
9019 if (BUFFERP (echo_area_buffer[0])
9020 && WINDOWP (echo_area_window))
9021 {
9022 struct window *w = XWINDOW (echo_area_window);
9023 int resized_p;
9024 Lisp_Object resize_exactly;
9025
9026 if (minibuf_level == 0)
9027 resize_exactly = Qt;
9028 else
9029 resize_exactly = Qnil;
9030
9031 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9032 (EMACS_INT) w, resize_exactly, 0, 0);
9033 if (resized_p)
9034 {
9035 ++windows_or_buffers_changed;
9036 ++update_mode_lines;
9037 redisplay_internal (0);
9038 }
9039 }
9040 }
9041
9042
9043 /* Callback function for with_echo_area_buffer, when used from
9044 resize_echo_area_exactly. A1 contains a pointer to the window to
9045 resize, EXACTLY non-nil means resize the mini-window exactly to the
9046 size of the text displayed. A3 and A4 are not used. Value is what
9047 resize_mini_window returns. */
9048
9049 static int
9050 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9051 {
9052 return resize_mini_window ((struct window *) a1, !NILP (exactly));
9053 }
9054
9055
9056 /* Resize mini-window W to fit the size of its contents. EXACT_P
9057 means size the window exactly to the size needed. Otherwise, it's
9058 only enlarged until W's buffer is empty.
9059
9060 Set W->start to the right place to begin display. If the whole
9061 contents fit, start at the beginning. Otherwise, start so as
9062 to make the end of the contents appear. This is particularly
9063 important for y-or-n-p, but seems desirable generally.
9064
9065 Value is non-zero if the window height has been changed. */
9066
9067 int
9068 resize_mini_window (struct window *w, int exact_p)
9069 {
9070 struct frame *f = XFRAME (w->frame);
9071 int window_height_changed_p = 0;
9072
9073 xassert (MINI_WINDOW_P (w));
9074
9075 /* By default, start display at the beginning. */
9076 set_marker_both (w->start, w->buffer,
9077 BUF_BEGV (XBUFFER (w->buffer)),
9078 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9079
9080 /* Don't resize windows while redisplaying a window; it would
9081 confuse redisplay functions when the size of the window they are
9082 displaying changes from under them. Such a resizing can happen,
9083 for instance, when which-func prints a long message while
9084 we are running fontification-functions. We're running these
9085 functions with safe_call which binds inhibit-redisplay to t. */
9086 if (!NILP (Vinhibit_redisplay))
9087 return 0;
9088
9089 /* Nil means don't try to resize. */
9090 if (NILP (Vresize_mini_windows)
9091 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9092 return 0;
9093
9094 if (!FRAME_MINIBUF_ONLY_P (f))
9095 {
9096 struct it it;
9097 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9098 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9099 int height, max_height;
9100 int unit = FRAME_LINE_HEIGHT (f);
9101 struct text_pos start;
9102 struct buffer *old_current_buffer = NULL;
9103
9104 if (current_buffer != XBUFFER (w->buffer))
9105 {
9106 old_current_buffer = current_buffer;
9107 set_buffer_internal (XBUFFER (w->buffer));
9108 }
9109
9110 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9111
9112 /* Compute the max. number of lines specified by the user. */
9113 if (FLOATP (Vmax_mini_window_height))
9114 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9115 else if (INTEGERP (Vmax_mini_window_height))
9116 max_height = XINT (Vmax_mini_window_height);
9117 else
9118 max_height = total_height / 4;
9119
9120 /* Correct that max. height if it's bogus. */
9121 max_height = max (1, max_height);
9122 max_height = min (total_height, max_height);
9123
9124 /* Find out the height of the text in the window. */
9125 if (it.line_wrap == TRUNCATE)
9126 height = 1;
9127 else
9128 {
9129 last_height = 0;
9130 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9131 if (it.max_ascent == 0 && it.max_descent == 0)
9132 height = it.current_y + last_height;
9133 else
9134 height = it.current_y + it.max_ascent + it.max_descent;
9135 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9136 height = (height + unit - 1) / unit;
9137 }
9138
9139 /* Compute a suitable window start. */
9140 if (height > max_height)
9141 {
9142 height = max_height;
9143 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9144 move_it_vertically_backward (&it, (height - 1) * unit);
9145 start = it.current.pos;
9146 }
9147 else
9148 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9149 SET_MARKER_FROM_TEXT_POS (w->start, start);
9150
9151 if (EQ (Vresize_mini_windows, Qgrow_only))
9152 {
9153 /* Let it grow only, until we display an empty message, in which
9154 case the window shrinks again. */
9155 if (height > WINDOW_TOTAL_LINES (w))
9156 {
9157 int old_height = WINDOW_TOTAL_LINES (w);
9158 freeze_window_starts (f, 1);
9159 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9160 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9161 }
9162 else if (height < WINDOW_TOTAL_LINES (w)
9163 && (exact_p || BEGV == ZV))
9164 {
9165 int old_height = WINDOW_TOTAL_LINES (w);
9166 freeze_window_starts (f, 0);
9167 shrink_mini_window (w);
9168 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9169 }
9170 }
9171 else
9172 {
9173 /* Always resize to exact size needed. */
9174 if (height > WINDOW_TOTAL_LINES (w))
9175 {
9176 int old_height = WINDOW_TOTAL_LINES (w);
9177 freeze_window_starts (f, 1);
9178 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9179 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9180 }
9181 else if (height < WINDOW_TOTAL_LINES (w))
9182 {
9183 int old_height = WINDOW_TOTAL_LINES (w);
9184 freeze_window_starts (f, 0);
9185 shrink_mini_window (w);
9186
9187 if (height)
9188 {
9189 freeze_window_starts (f, 1);
9190 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9191 }
9192
9193 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9194 }
9195 }
9196
9197 if (old_current_buffer)
9198 set_buffer_internal (old_current_buffer);
9199 }
9200
9201 return window_height_changed_p;
9202 }
9203
9204
9205 /* Value is the current message, a string, or nil if there is no
9206 current message. */
9207
9208 Lisp_Object
9209 current_message (void)
9210 {
9211 Lisp_Object msg;
9212
9213 if (!BUFFERP (echo_area_buffer[0]))
9214 msg = Qnil;
9215 else
9216 {
9217 with_echo_area_buffer (0, 0, current_message_1,
9218 (EMACS_INT) &msg, Qnil, 0, 0);
9219 if (NILP (msg))
9220 echo_area_buffer[0] = Qnil;
9221 }
9222
9223 return msg;
9224 }
9225
9226
9227 static int
9228 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9229 {
9230 Lisp_Object *msg = (Lisp_Object *) a1;
9231
9232 if (Z > BEG)
9233 *msg = make_buffer_string (BEG, Z, 1);
9234 else
9235 *msg = Qnil;
9236 return 0;
9237 }
9238
9239
9240 /* Push the current message on Vmessage_stack for later restauration
9241 by restore_message. Value is non-zero if the current message isn't
9242 empty. This is a relatively infrequent operation, so it's not
9243 worth optimizing. */
9244
9245 int
9246 push_message (void)
9247 {
9248 Lisp_Object msg;
9249 msg = current_message ();
9250 Vmessage_stack = Fcons (msg, Vmessage_stack);
9251 return STRINGP (msg);
9252 }
9253
9254
9255 /* Restore message display from the top of Vmessage_stack. */
9256
9257 void
9258 restore_message (void)
9259 {
9260 Lisp_Object msg;
9261
9262 xassert (CONSP (Vmessage_stack));
9263 msg = XCAR (Vmessage_stack);
9264 if (STRINGP (msg))
9265 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9266 else
9267 message3_nolog (msg, 0, 0);
9268 }
9269
9270
9271 /* Handler for record_unwind_protect calling pop_message. */
9272
9273 Lisp_Object
9274 pop_message_unwind (Lisp_Object dummy)
9275 {
9276 pop_message ();
9277 return Qnil;
9278 }
9279
9280 /* Pop the top-most entry off Vmessage_stack. */
9281
9282 void
9283 pop_message (void)
9284 {
9285 xassert (CONSP (Vmessage_stack));
9286 Vmessage_stack = XCDR (Vmessage_stack);
9287 }
9288
9289
9290 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9291 exits. If the stack is not empty, we have a missing pop_message
9292 somewhere. */
9293
9294 void
9295 check_message_stack (void)
9296 {
9297 if (!NILP (Vmessage_stack))
9298 abort ();
9299 }
9300
9301
9302 /* Truncate to NCHARS what will be displayed in the echo area the next
9303 time we display it---but don't redisplay it now. */
9304
9305 void
9306 truncate_echo_area (EMACS_INT nchars)
9307 {
9308 if (nchars == 0)
9309 echo_area_buffer[0] = Qnil;
9310 /* A null message buffer means that the frame hasn't really been
9311 initialized yet. Error messages get reported properly by
9312 cmd_error, so this must be just an informative message; toss it. */
9313 else if (!noninteractive
9314 && INTERACTIVE
9315 && !NILP (echo_area_buffer[0]))
9316 {
9317 struct frame *sf = SELECTED_FRAME ();
9318 if (FRAME_MESSAGE_BUF (sf))
9319 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9320 }
9321 }
9322
9323
9324 /* Helper function for truncate_echo_area. Truncate the current
9325 message to at most NCHARS characters. */
9326
9327 static int
9328 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9329 {
9330 if (BEG + nchars < Z)
9331 del_range (BEG + nchars, Z);
9332 if (Z == BEG)
9333 echo_area_buffer[0] = Qnil;
9334 return 0;
9335 }
9336
9337
9338 /* Set the current message to a substring of S or STRING.
9339
9340 If STRING is a Lisp string, set the message to the first NBYTES
9341 bytes from STRING. NBYTES zero means use the whole string. If
9342 STRING is multibyte, the message will be displayed multibyte.
9343
9344 If S is not null, set the message to the first LEN bytes of S. LEN
9345 zero means use the whole string. MULTIBYTE_P non-zero means S is
9346 multibyte. Display the message multibyte in that case.
9347
9348 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9349 to t before calling set_message_1 (which calls insert).
9350 */
9351
9352 void
9353 set_message (const char *s, Lisp_Object string,
9354 EMACS_INT nbytes, int multibyte_p)
9355 {
9356 message_enable_multibyte
9357 = ((s && multibyte_p)
9358 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9359
9360 with_echo_area_buffer (0, -1, set_message_1,
9361 (EMACS_INT) s, string, nbytes, multibyte_p);
9362 message_buf_print = 0;
9363 help_echo_showing_p = 0;
9364 }
9365
9366
9367 /* Helper function for set_message. Arguments have the same meaning
9368 as there, with A1 corresponding to S and A2 corresponding to STRING
9369 This function is called with the echo area buffer being
9370 current. */
9371
9372 static int
9373 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9374 {
9375 const char *s = (const char *) a1;
9376 Lisp_Object string = a2;
9377
9378 /* Change multibyteness of the echo buffer appropriately. */
9379 if (message_enable_multibyte
9380 != !NILP (current_buffer->enable_multibyte_characters))
9381 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9382
9383 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9384
9385 /* Insert new message at BEG. */
9386 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9387
9388 if (STRINGP (string))
9389 {
9390 EMACS_INT nchars;
9391
9392 if (nbytes == 0)
9393 nbytes = SBYTES (string);
9394 nchars = string_byte_to_char (string, nbytes);
9395
9396 /* This function takes care of single/multibyte conversion. We
9397 just have to ensure that the echo area buffer has the right
9398 setting of enable_multibyte_characters. */
9399 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9400 }
9401 else if (s)
9402 {
9403 if (nbytes == 0)
9404 nbytes = strlen (s);
9405
9406 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9407 {
9408 /* Convert from multi-byte to single-byte. */
9409 EMACS_INT i;
9410 int c, n;
9411 unsigned char work[1];
9412
9413 /* Convert a multibyte string to single-byte. */
9414 for (i = 0; i < nbytes; i += n)
9415 {
9416 c = string_char_and_length (s + i, &n);
9417 work[0] = (ASCII_CHAR_P (c)
9418 ? c
9419 : multibyte_char_to_unibyte (c, Qnil));
9420 insert_1_both (work, 1, 1, 1, 0, 0);
9421 }
9422 }
9423 else if (!multibyte_p
9424 && !NILP (current_buffer->enable_multibyte_characters))
9425 {
9426 /* Convert from single-byte to multi-byte. */
9427 EMACS_INT i;
9428 int c, n;
9429 const unsigned char *msg = (const unsigned char *) s;
9430 unsigned char str[MAX_MULTIBYTE_LENGTH];
9431
9432 /* Convert a single-byte string to multibyte. */
9433 for (i = 0; i < nbytes; i++)
9434 {
9435 c = msg[i];
9436 MAKE_CHAR_MULTIBYTE (c);
9437 n = CHAR_STRING (c, str);
9438 insert_1_both (str, 1, n, 1, 0, 0);
9439 }
9440 }
9441 else
9442 insert_1 (s, nbytes, 1, 0, 0);
9443 }
9444
9445 return 0;
9446 }
9447
9448
9449 /* Clear messages. CURRENT_P non-zero means clear the current
9450 message. LAST_DISPLAYED_P non-zero means clear the message
9451 last displayed. */
9452
9453 void
9454 clear_message (int current_p, int last_displayed_p)
9455 {
9456 if (current_p)
9457 {
9458 echo_area_buffer[0] = Qnil;
9459 message_cleared_p = 1;
9460 }
9461
9462 if (last_displayed_p)
9463 echo_area_buffer[1] = Qnil;
9464
9465 message_buf_print = 0;
9466 }
9467
9468 /* Clear garbaged frames.
9469
9470 This function is used where the old redisplay called
9471 redraw_garbaged_frames which in turn called redraw_frame which in
9472 turn called clear_frame. The call to clear_frame was a source of
9473 flickering. I believe a clear_frame is not necessary. It should
9474 suffice in the new redisplay to invalidate all current matrices,
9475 and ensure a complete redisplay of all windows. */
9476
9477 static void
9478 clear_garbaged_frames (void)
9479 {
9480 if (frame_garbaged)
9481 {
9482 Lisp_Object tail, frame;
9483 int changed_count = 0;
9484
9485 FOR_EACH_FRAME (tail, frame)
9486 {
9487 struct frame *f = XFRAME (frame);
9488
9489 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9490 {
9491 if (f->resized_p)
9492 {
9493 Fredraw_frame (frame);
9494 f->force_flush_display_p = 1;
9495 }
9496 clear_current_matrices (f);
9497 changed_count++;
9498 f->garbaged = 0;
9499 f->resized_p = 0;
9500 }
9501 }
9502
9503 frame_garbaged = 0;
9504 if (changed_count)
9505 ++windows_or_buffers_changed;
9506 }
9507 }
9508
9509
9510 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9511 is non-zero update selected_frame. Value is non-zero if the
9512 mini-windows height has been changed. */
9513
9514 static int
9515 echo_area_display (int update_frame_p)
9516 {
9517 Lisp_Object mini_window;
9518 struct window *w;
9519 struct frame *f;
9520 int window_height_changed_p = 0;
9521 struct frame *sf = SELECTED_FRAME ();
9522
9523 mini_window = FRAME_MINIBUF_WINDOW (sf);
9524 w = XWINDOW (mini_window);
9525 f = XFRAME (WINDOW_FRAME (w));
9526
9527 /* Don't display if frame is invisible or not yet initialized. */
9528 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9529 return 0;
9530
9531 #ifdef HAVE_WINDOW_SYSTEM
9532 /* When Emacs starts, selected_frame may be the initial terminal
9533 frame. If we let this through, a message would be displayed on
9534 the terminal. */
9535 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9536 return 0;
9537 #endif /* HAVE_WINDOW_SYSTEM */
9538
9539 /* Redraw garbaged frames. */
9540 if (frame_garbaged)
9541 clear_garbaged_frames ();
9542
9543 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9544 {
9545 echo_area_window = mini_window;
9546 window_height_changed_p = display_echo_area (w);
9547 w->must_be_updated_p = 1;
9548
9549 /* Update the display, unless called from redisplay_internal.
9550 Also don't update the screen during redisplay itself. The
9551 update will happen at the end of redisplay, and an update
9552 here could cause confusion. */
9553 if (update_frame_p && !redisplaying_p)
9554 {
9555 int n = 0;
9556
9557 /* If the display update has been interrupted by pending
9558 input, update mode lines in the frame. Due to the
9559 pending input, it might have been that redisplay hasn't
9560 been called, so that mode lines above the echo area are
9561 garbaged. This looks odd, so we prevent it here. */
9562 if (!display_completed)
9563 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9564
9565 if (window_height_changed_p
9566 /* Don't do this if Emacs is shutting down. Redisplay
9567 needs to run hooks. */
9568 && !NILP (Vrun_hooks))
9569 {
9570 /* Must update other windows. Likewise as in other
9571 cases, don't let this update be interrupted by
9572 pending input. */
9573 int count = SPECPDL_INDEX ();
9574 specbind (Qredisplay_dont_pause, Qt);
9575 windows_or_buffers_changed = 1;
9576 redisplay_internal (0);
9577 unbind_to (count, Qnil);
9578 }
9579 else if (FRAME_WINDOW_P (f) && n == 0)
9580 {
9581 /* Window configuration is the same as before.
9582 Can do with a display update of the echo area,
9583 unless we displayed some mode lines. */
9584 update_single_window (w, 1);
9585 FRAME_RIF (f)->flush_display (f);
9586 }
9587 else
9588 update_frame (f, 1, 1);
9589
9590 /* If cursor is in the echo area, make sure that the next
9591 redisplay displays the minibuffer, so that the cursor will
9592 be replaced with what the minibuffer wants. */
9593 if (cursor_in_echo_area)
9594 ++windows_or_buffers_changed;
9595 }
9596 }
9597 else if (!EQ (mini_window, selected_window))
9598 windows_or_buffers_changed++;
9599
9600 /* Last displayed message is now the current message. */
9601 echo_area_buffer[1] = echo_area_buffer[0];
9602 /* Inform read_char that we're not echoing. */
9603 echo_message_buffer = Qnil;
9604
9605 /* Prevent redisplay optimization in redisplay_internal by resetting
9606 this_line_start_pos. This is done because the mini-buffer now
9607 displays the message instead of its buffer text. */
9608 if (EQ (mini_window, selected_window))
9609 CHARPOS (this_line_start_pos) = 0;
9610
9611 return window_height_changed_p;
9612 }
9613
9614
9615 \f
9616 /***********************************************************************
9617 Mode Lines and Frame Titles
9618 ***********************************************************************/
9619
9620 /* A buffer for constructing non-propertized mode-line strings and
9621 frame titles in it; allocated from the heap in init_xdisp and
9622 resized as needed in store_mode_line_noprop_char. */
9623
9624 static char *mode_line_noprop_buf;
9625
9626 /* The buffer's end, and a current output position in it. */
9627
9628 static char *mode_line_noprop_buf_end;
9629 static char *mode_line_noprop_ptr;
9630
9631 #define MODE_LINE_NOPROP_LEN(start) \
9632 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9633
9634 static enum {
9635 MODE_LINE_DISPLAY = 0,
9636 MODE_LINE_TITLE,
9637 MODE_LINE_NOPROP,
9638 MODE_LINE_STRING
9639 } mode_line_target;
9640
9641 /* Alist that caches the results of :propertize.
9642 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9643 static Lisp_Object mode_line_proptrans_alist;
9644
9645 /* List of strings making up the mode-line. */
9646 static Lisp_Object mode_line_string_list;
9647
9648 /* Base face property when building propertized mode line string. */
9649 static Lisp_Object mode_line_string_face;
9650 static Lisp_Object mode_line_string_face_prop;
9651
9652
9653 /* Unwind data for mode line strings */
9654
9655 static Lisp_Object Vmode_line_unwind_vector;
9656
9657 static Lisp_Object
9658 format_mode_line_unwind_data (struct buffer *obuf,
9659 Lisp_Object owin,
9660 int save_proptrans)
9661 {
9662 Lisp_Object vector, tmp;
9663
9664 /* Reduce consing by keeping one vector in
9665 Vwith_echo_area_save_vector. */
9666 vector = Vmode_line_unwind_vector;
9667 Vmode_line_unwind_vector = Qnil;
9668
9669 if (NILP (vector))
9670 vector = Fmake_vector (make_number (8), Qnil);
9671
9672 ASET (vector, 0, make_number (mode_line_target));
9673 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9674 ASET (vector, 2, mode_line_string_list);
9675 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9676 ASET (vector, 4, mode_line_string_face);
9677 ASET (vector, 5, mode_line_string_face_prop);
9678
9679 if (obuf)
9680 XSETBUFFER (tmp, obuf);
9681 else
9682 tmp = Qnil;
9683 ASET (vector, 6, tmp);
9684 ASET (vector, 7, owin);
9685
9686 return vector;
9687 }
9688
9689 static Lisp_Object
9690 unwind_format_mode_line (Lisp_Object vector)
9691 {
9692 mode_line_target = XINT (AREF (vector, 0));
9693 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9694 mode_line_string_list = AREF (vector, 2);
9695 if (! EQ (AREF (vector, 3), Qt))
9696 mode_line_proptrans_alist = AREF (vector, 3);
9697 mode_line_string_face = AREF (vector, 4);
9698 mode_line_string_face_prop = AREF (vector, 5);
9699
9700 if (!NILP (AREF (vector, 7)))
9701 /* Select window before buffer, since it may change the buffer. */
9702 Fselect_window (AREF (vector, 7), Qt);
9703
9704 if (!NILP (AREF (vector, 6)))
9705 {
9706 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9707 ASET (vector, 6, Qnil);
9708 }
9709
9710 Vmode_line_unwind_vector = vector;
9711 return Qnil;
9712 }
9713
9714
9715 /* Store a single character C for the frame title in mode_line_noprop_buf.
9716 Re-allocate mode_line_noprop_buf if necessary. */
9717
9718 static void
9719 store_mode_line_noprop_char (char c)
9720 {
9721 /* If output position has reached the end of the allocated buffer,
9722 double the buffer's size. */
9723 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9724 {
9725 int len = MODE_LINE_NOPROP_LEN (0);
9726 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9727 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9728 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9729 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9730 }
9731
9732 *mode_line_noprop_ptr++ = c;
9733 }
9734
9735
9736 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9737 mode_line_noprop_ptr. STR is the string to store. Do not copy
9738 characters that yield more columns than PRECISION; PRECISION <= 0
9739 means copy the whole string. Pad with spaces until FIELD_WIDTH
9740 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9741 pad. Called from display_mode_element when it is used to build a
9742 frame title. */
9743
9744 static int
9745 store_mode_line_noprop (const unsigned char *str, int field_width, int precision)
9746 {
9747 int n = 0;
9748 EMACS_INT dummy, nbytes;
9749
9750 /* Copy at most PRECISION chars from STR. */
9751 nbytes = strlen (str);
9752 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9753 while (nbytes--)
9754 store_mode_line_noprop_char (*str++);
9755
9756 /* Fill up with spaces until FIELD_WIDTH reached. */
9757 while (field_width > 0
9758 && n < field_width)
9759 {
9760 store_mode_line_noprop_char (' ');
9761 ++n;
9762 }
9763
9764 return n;
9765 }
9766
9767 /***********************************************************************
9768 Frame Titles
9769 ***********************************************************************/
9770
9771 #ifdef HAVE_WINDOW_SYSTEM
9772
9773 /* Set the title of FRAME, if it has changed. The title format is
9774 Vicon_title_format if FRAME is iconified, otherwise it is
9775 frame_title_format. */
9776
9777 static void
9778 x_consider_frame_title (Lisp_Object frame)
9779 {
9780 struct frame *f = XFRAME (frame);
9781
9782 if (FRAME_WINDOW_P (f)
9783 || FRAME_MINIBUF_ONLY_P (f)
9784 || f->explicit_name)
9785 {
9786 /* Do we have more than one visible frame on this X display? */
9787 Lisp_Object tail;
9788 Lisp_Object fmt;
9789 int title_start;
9790 char *title;
9791 int len;
9792 struct it it;
9793 int count = SPECPDL_INDEX ();
9794
9795 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9796 {
9797 Lisp_Object other_frame = XCAR (tail);
9798 struct frame *tf = XFRAME (other_frame);
9799
9800 if (tf != f
9801 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9802 && !FRAME_MINIBUF_ONLY_P (tf)
9803 && !EQ (other_frame, tip_frame)
9804 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9805 break;
9806 }
9807
9808 /* Set global variable indicating that multiple frames exist. */
9809 multiple_frames = CONSP (tail);
9810
9811 /* Switch to the buffer of selected window of the frame. Set up
9812 mode_line_target so that display_mode_element will output into
9813 mode_line_noprop_buf; then display the title. */
9814 record_unwind_protect (unwind_format_mode_line,
9815 format_mode_line_unwind_data
9816 (current_buffer, selected_window, 0));
9817
9818 Fselect_window (f->selected_window, Qt);
9819 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9820 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9821
9822 mode_line_target = MODE_LINE_TITLE;
9823 title_start = MODE_LINE_NOPROP_LEN (0);
9824 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9825 NULL, DEFAULT_FACE_ID);
9826 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9827 len = MODE_LINE_NOPROP_LEN (title_start);
9828 title = mode_line_noprop_buf + title_start;
9829 unbind_to (count, Qnil);
9830
9831 /* Set the title only if it's changed. This avoids consing in
9832 the common case where it hasn't. (If it turns out that we've
9833 already wasted too much time by walking through the list with
9834 display_mode_element, then we might need to optimize at a
9835 higher level than this.) */
9836 if (! STRINGP (f->name)
9837 || SBYTES (f->name) != len
9838 || memcmp (title, SDATA (f->name), len) != 0)
9839 x_implicitly_set_name (f, make_string (title, len), Qnil);
9840 }
9841 }
9842
9843 #endif /* not HAVE_WINDOW_SYSTEM */
9844
9845
9846
9847 \f
9848 /***********************************************************************
9849 Menu Bars
9850 ***********************************************************************/
9851
9852
9853 /* Prepare for redisplay by updating menu-bar item lists when
9854 appropriate. This can call eval. */
9855
9856 void
9857 prepare_menu_bars (void)
9858 {
9859 int all_windows;
9860 struct gcpro gcpro1, gcpro2;
9861 struct frame *f;
9862 Lisp_Object tooltip_frame;
9863
9864 #ifdef HAVE_WINDOW_SYSTEM
9865 tooltip_frame = tip_frame;
9866 #else
9867 tooltip_frame = Qnil;
9868 #endif
9869
9870 /* Update all frame titles based on their buffer names, etc. We do
9871 this before the menu bars so that the buffer-menu will show the
9872 up-to-date frame titles. */
9873 #ifdef HAVE_WINDOW_SYSTEM
9874 if (windows_or_buffers_changed || update_mode_lines)
9875 {
9876 Lisp_Object tail, frame;
9877
9878 FOR_EACH_FRAME (tail, frame)
9879 {
9880 f = XFRAME (frame);
9881 if (!EQ (frame, tooltip_frame)
9882 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9883 x_consider_frame_title (frame);
9884 }
9885 }
9886 #endif /* HAVE_WINDOW_SYSTEM */
9887
9888 /* Update the menu bar item lists, if appropriate. This has to be
9889 done before any actual redisplay or generation of display lines. */
9890 all_windows = (update_mode_lines
9891 || buffer_shared > 1
9892 || windows_or_buffers_changed);
9893 if (all_windows)
9894 {
9895 Lisp_Object tail, frame;
9896 int count = SPECPDL_INDEX ();
9897 /* 1 means that update_menu_bar has run its hooks
9898 so any further calls to update_menu_bar shouldn't do so again. */
9899 int menu_bar_hooks_run = 0;
9900
9901 record_unwind_save_match_data ();
9902
9903 FOR_EACH_FRAME (tail, frame)
9904 {
9905 f = XFRAME (frame);
9906
9907 /* Ignore tooltip frame. */
9908 if (EQ (frame, tooltip_frame))
9909 continue;
9910
9911 /* If a window on this frame changed size, report that to
9912 the user and clear the size-change flag. */
9913 if (FRAME_WINDOW_SIZES_CHANGED (f))
9914 {
9915 Lisp_Object functions;
9916
9917 /* Clear flag first in case we get an error below. */
9918 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9919 functions = Vwindow_size_change_functions;
9920 GCPRO2 (tail, functions);
9921
9922 while (CONSP (functions))
9923 {
9924 if (!EQ (XCAR (functions), Qt))
9925 call1 (XCAR (functions), frame);
9926 functions = XCDR (functions);
9927 }
9928 UNGCPRO;
9929 }
9930
9931 GCPRO1 (tail);
9932 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9933 #ifdef HAVE_WINDOW_SYSTEM
9934 update_tool_bar (f, 0);
9935 #endif
9936 #ifdef HAVE_NS
9937 if (windows_or_buffers_changed
9938 && FRAME_NS_P (f))
9939 ns_set_doc_edited (f, Fbuffer_modified_p
9940 (XWINDOW (f->selected_window)->buffer));
9941 #endif
9942 UNGCPRO;
9943 }
9944
9945 unbind_to (count, Qnil);
9946 }
9947 else
9948 {
9949 struct frame *sf = SELECTED_FRAME ();
9950 update_menu_bar (sf, 1, 0);
9951 #ifdef HAVE_WINDOW_SYSTEM
9952 update_tool_bar (sf, 1);
9953 #endif
9954 }
9955 }
9956
9957
9958 /* Update the menu bar item list for frame F. This has to be done
9959 before we start to fill in any display lines, because it can call
9960 eval.
9961
9962 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9963
9964 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9965 already ran the menu bar hooks for this redisplay, so there
9966 is no need to run them again. The return value is the
9967 updated value of this flag, to pass to the next call. */
9968
9969 static int
9970 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
9971 {
9972 Lisp_Object window;
9973 register struct window *w;
9974
9975 /* If called recursively during a menu update, do nothing. This can
9976 happen when, for instance, an activate-menubar-hook causes a
9977 redisplay. */
9978 if (inhibit_menubar_update)
9979 return hooks_run;
9980
9981 window = FRAME_SELECTED_WINDOW (f);
9982 w = XWINDOW (window);
9983
9984 if (FRAME_WINDOW_P (f)
9985 ?
9986 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9987 || defined (HAVE_NS) || defined (USE_GTK)
9988 FRAME_EXTERNAL_MENU_BAR (f)
9989 #else
9990 FRAME_MENU_BAR_LINES (f) > 0
9991 #endif
9992 : FRAME_MENU_BAR_LINES (f) > 0)
9993 {
9994 /* If the user has switched buffers or windows, we need to
9995 recompute to reflect the new bindings. But we'll
9996 recompute when update_mode_lines is set too; that means
9997 that people can use force-mode-line-update to request
9998 that the menu bar be recomputed. The adverse effect on
9999 the rest of the redisplay algorithm is about the same as
10000 windows_or_buffers_changed anyway. */
10001 if (windows_or_buffers_changed
10002 /* This used to test w->update_mode_line, but we believe
10003 there is no need to recompute the menu in that case. */
10004 || update_mode_lines
10005 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10006 < BUF_MODIFF (XBUFFER (w->buffer)))
10007 != !NILP (w->last_had_star))
10008 || ((!NILP (Vtransient_mark_mode)
10009 && !NILP (XBUFFER (w->buffer)->mark_active))
10010 != !NILP (w->region_showing)))
10011 {
10012 struct buffer *prev = current_buffer;
10013 int count = SPECPDL_INDEX ();
10014
10015 specbind (Qinhibit_menubar_update, Qt);
10016
10017 set_buffer_internal_1 (XBUFFER (w->buffer));
10018 if (save_match_data)
10019 record_unwind_save_match_data ();
10020 if (NILP (Voverriding_local_map_menu_flag))
10021 {
10022 specbind (Qoverriding_terminal_local_map, Qnil);
10023 specbind (Qoverriding_local_map, Qnil);
10024 }
10025
10026 if (!hooks_run)
10027 {
10028 /* Run the Lucid hook. */
10029 safe_run_hooks (Qactivate_menubar_hook);
10030
10031 /* If it has changed current-menubar from previous value,
10032 really recompute the menu-bar from the value. */
10033 if (! NILP (Vlucid_menu_bar_dirty_flag))
10034 call0 (Qrecompute_lucid_menubar);
10035
10036 safe_run_hooks (Qmenu_bar_update_hook);
10037
10038 hooks_run = 1;
10039 }
10040
10041 XSETFRAME (Vmenu_updating_frame, f);
10042 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10043
10044 /* Redisplay the menu bar in case we changed it. */
10045 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10046 || defined (HAVE_NS) || defined (USE_GTK)
10047 if (FRAME_WINDOW_P (f))
10048 {
10049 #if defined (HAVE_NS)
10050 /* All frames on Mac OS share the same menubar. So only
10051 the selected frame should be allowed to set it. */
10052 if (f == SELECTED_FRAME ())
10053 #endif
10054 set_frame_menubar (f, 0, 0);
10055 }
10056 else
10057 /* On a terminal screen, the menu bar is an ordinary screen
10058 line, and this makes it get updated. */
10059 w->update_mode_line = Qt;
10060 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10061 /* In the non-toolkit version, the menu bar is an ordinary screen
10062 line, and this makes it get updated. */
10063 w->update_mode_line = Qt;
10064 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10065
10066 unbind_to (count, Qnil);
10067 set_buffer_internal_1 (prev);
10068 }
10069 }
10070
10071 return hooks_run;
10072 }
10073
10074
10075 \f
10076 /***********************************************************************
10077 Output Cursor
10078 ***********************************************************************/
10079
10080 #ifdef HAVE_WINDOW_SYSTEM
10081
10082 /* EXPORT:
10083 Nominal cursor position -- where to draw output.
10084 HPOS and VPOS are window relative glyph matrix coordinates.
10085 X and Y are window relative pixel coordinates. */
10086
10087 struct cursor_pos output_cursor;
10088
10089
10090 /* EXPORT:
10091 Set the global variable output_cursor to CURSOR. All cursor
10092 positions are relative to updated_window. */
10093
10094 void
10095 set_output_cursor (struct cursor_pos *cursor)
10096 {
10097 output_cursor.hpos = cursor->hpos;
10098 output_cursor.vpos = cursor->vpos;
10099 output_cursor.x = cursor->x;
10100 output_cursor.y = cursor->y;
10101 }
10102
10103
10104 /* EXPORT for RIF:
10105 Set a nominal cursor position.
10106
10107 HPOS and VPOS are column/row positions in a window glyph matrix. X
10108 and Y are window text area relative pixel positions.
10109
10110 If this is done during an update, updated_window will contain the
10111 window that is being updated and the position is the future output
10112 cursor position for that window. If updated_window is null, use
10113 selected_window and display the cursor at the given position. */
10114
10115 void
10116 x_cursor_to (int vpos, int hpos, int y, int x)
10117 {
10118 struct window *w;
10119
10120 /* If updated_window is not set, work on selected_window. */
10121 if (updated_window)
10122 w = updated_window;
10123 else
10124 w = XWINDOW (selected_window);
10125
10126 /* Set the output cursor. */
10127 output_cursor.hpos = hpos;
10128 output_cursor.vpos = vpos;
10129 output_cursor.x = x;
10130 output_cursor.y = y;
10131
10132 /* If not called as part of an update, really display the cursor.
10133 This will also set the cursor position of W. */
10134 if (updated_window == NULL)
10135 {
10136 BLOCK_INPUT;
10137 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10138 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10139 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10140 UNBLOCK_INPUT;
10141 }
10142 }
10143
10144 #endif /* HAVE_WINDOW_SYSTEM */
10145
10146 \f
10147 /***********************************************************************
10148 Tool-bars
10149 ***********************************************************************/
10150
10151 #ifdef HAVE_WINDOW_SYSTEM
10152
10153 /* Where the mouse was last time we reported a mouse event. */
10154
10155 FRAME_PTR last_mouse_frame;
10156
10157 /* Tool-bar item index of the item on which a mouse button was pressed
10158 or -1. */
10159
10160 int last_tool_bar_item;
10161
10162
10163 static Lisp_Object
10164 update_tool_bar_unwind (Lisp_Object frame)
10165 {
10166 selected_frame = frame;
10167 return Qnil;
10168 }
10169
10170 /* Update the tool-bar item list for frame F. This has to be done
10171 before we start to fill in any display lines. Called from
10172 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10173 and restore it here. */
10174
10175 static void
10176 update_tool_bar (struct frame *f, int save_match_data)
10177 {
10178 #if defined (USE_GTK) || defined (HAVE_NS)
10179 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10180 #else
10181 int do_update = WINDOWP (f->tool_bar_window)
10182 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10183 #endif
10184
10185 if (do_update)
10186 {
10187 Lisp_Object window;
10188 struct window *w;
10189
10190 window = FRAME_SELECTED_WINDOW (f);
10191 w = XWINDOW (window);
10192
10193 /* If the user has switched buffers or windows, we need to
10194 recompute to reflect the new bindings. But we'll
10195 recompute when update_mode_lines is set too; that means
10196 that people can use force-mode-line-update to request
10197 that the menu bar be recomputed. The adverse effect on
10198 the rest of the redisplay algorithm is about the same as
10199 windows_or_buffers_changed anyway. */
10200 if (windows_or_buffers_changed
10201 || !NILP (w->update_mode_line)
10202 || update_mode_lines
10203 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10204 < BUF_MODIFF (XBUFFER (w->buffer)))
10205 != !NILP (w->last_had_star))
10206 || ((!NILP (Vtransient_mark_mode)
10207 && !NILP (XBUFFER (w->buffer)->mark_active))
10208 != !NILP (w->region_showing)))
10209 {
10210 struct buffer *prev = current_buffer;
10211 int count = SPECPDL_INDEX ();
10212 Lisp_Object frame, new_tool_bar;
10213 int new_n_tool_bar;
10214 struct gcpro gcpro1;
10215
10216 /* Set current_buffer to the buffer of the selected
10217 window of the frame, so that we get the right local
10218 keymaps. */
10219 set_buffer_internal_1 (XBUFFER (w->buffer));
10220
10221 /* Save match data, if we must. */
10222 if (save_match_data)
10223 record_unwind_save_match_data ();
10224
10225 /* Make sure that we don't accidentally use bogus keymaps. */
10226 if (NILP (Voverriding_local_map_menu_flag))
10227 {
10228 specbind (Qoverriding_terminal_local_map, Qnil);
10229 specbind (Qoverriding_local_map, Qnil);
10230 }
10231
10232 GCPRO1 (new_tool_bar);
10233
10234 /* We must temporarily set the selected frame to this frame
10235 before calling tool_bar_items, because the calculation of
10236 the tool-bar keymap uses the selected frame (see
10237 `tool-bar-make-keymap' in tool-bar.el). */
10238 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10239 XSETFRAME (frame, f);
10240 selected_frame = frame;
10241
10242 /* Build desired tool-bar items from keymaps. */
10243 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10244 &new_n_tool_bar);
10245
10246 /* Redisplay the tool-bar if we changed it. */
10247 if (new_n_tool_bar != f->n_tool_bar_items
10248 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10249 {
10250 /* Redisplay that happens asynchronously due to an expose event
10251 may access f->tool_bar_items. Make sure we update both
10252 variables within BLOCK_INPUT so no such event interrupts. */
10253 BLOCK_INPUT;
10254 f->tool_bar_items = new_tool_bar;
10255 f->n_tool_bar_items = new_n_tool_bar;
10256 w->update_mode_line = Qt;
10257 UNBLOCK_INPUT;
10258 }
10259
10260 UNGCPRO;
10261
10262 unbind_to (count, Qnil);
10263 set_buffer_internal_1 (prev);
10264 }
10265 }
10266 }
10267
10268
10269 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10270 F's desired tool-bar contents. F->tool_bar_items must have
10271 been set up previously by calling prepare_menu_bars. */
10272
10273 static void
10274 build_desired_tool_bar_string (struct frame *f)
10275 {
10276 int i, size, size_needed;
10277 struct gcpro gcpro1, gcpro2, gcpro3;
10278 Lisp_Object image, plist, props;
10279
10280 image = plist = props = Qnil;
10281 GCPRO3 (image, plist, props);
10282
10283 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10284 Otherwise, make a new string. */
10285
10286 /* The size of the string we might be able to reuse. */
10287 size = (STRINGP (f->desired_tool_bar_string)
10288 ? SCHARS (f->desired_tool_bar_string)
10289 : 0);
10290
10291 /* We need one space in the string for each image. */
10292 size_needed = f->n_tool_bar_items;
10293
10294 /* Reuse f->desired_tool_bar_string, if possible. */
10295 if (size < size_needed || NILP (f->desired_tool_bar_string))
10296 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10297 make_number (' '));
10298 else
10299 {
10300 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10301 Fremove_text_properties (make_number (0), make_number (size),
10302 props, f->desired_tool_bar_string);
10303 }
10304
10305 /* Put a `display' property on the string for the images to display,
10306 put a `menu_item' property on tool-bar items with a value that
10307 is the index of the item in F's tool-bar item vector. */
10308 for (i = 0; i < f->n_tool_bar_items; ++i)
10309 {
10310 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10311
10312 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10313 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10314 int hmargin, vmargin, relief, idx, end;
10315
10316 /* If image is a vector, choose the image according to the
10317 button state. */
10318 image = PROP (TOOL_BAR_ITEM_IMAGES);
10319 if (VECTORP (image))
10320 {
10321 if (enabled_p)
10322 idx = (selected_p
10323 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10324 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10325 else
10326 idx = (selected_p
10327 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10328 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10329
10330 xassert (ASIZE (image) >= idx);
10331 image = AREF (image, idx);
10332 }
10333 else
10334 idx = -1;
10335
10336 /* Ignore invalid image specifications. */
10337 if (!valid_image_p (image))
10338 continue;
10339
10340 /* Display the tool-bar button pressed, or depressed. */
10341 plist = Fcopy_sequence (XCDR (image));
10342
10343 /* Compute margin and relief to draw. */
10344 relief = (tool_bar_button_relief >= 0
10345 ? tool_bar_button_relief
10346 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10347 hmargin = vmargin = relief;
10348
10349 if (INTEGERP (Vtool_bar_button_margin)
10350 && XINT (Vtool_bar_button_margin) > 0)
10351 {
10352 hmargin += XFASTINT (Vtool_bar_button_margin);
10353 vmargin += XFASTINT (Vtool_bar_button_margin);
10354 }
10355 else if (CONSP (Vtool_bar_button_margin))
10356 {
10357 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10358 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10359 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10360
10361 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10362 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10363 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10364 }
10365
10366 if (auto_raise_tool_bar_buttons_p)
10367 {
10368 /* Add a `:relief' property to the image spec if the item is
10369 selected. */
10370 if (selected_p)
10371 {
10372 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10373 hmargin -= relief;
10374 vmargin -= relief;
10375 }
10376 }
10377 else
10378 {
10379 /* If image is selected, display it pressed, i.e. with a
10380 negative relief. If it's not selected, display it with a
10381 raised relief. */
10382 plist = Fplist_put (plist, QCrelief,
10383 (selected_p
10384 ? make_number (-relief)
10385 : make_number (relief)));
10386 hmargin -= relief;
10387 vmargin -= relief;
10388 }
10389
10390 /* Put a margin around the image. */
10391 if (hmargin || vmargin)
10392 {
10393 if (hmargin == vmargin)
10394 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10395 else
10396 plist = Fplist_put (plist, QCmargin,
10397 Fcons (make_number (hmargin),
10398 make_number (vmargin)));
10399 }
10400
10401 /* If button is not enabled, and we don't have special images
10402 for the disabled state, make the image appear disabled by
10403 applying an appropriate algorithm to it. */
10404 if (!enabled_p && idx < 0)
10405 plist = Fplist_put (plist, QCconversion, Qdisabled);
10406
10407 /* Put a `display' text property on the string for the image to
10408 display. Put a `menu-item' property on the string that gives
10409 the start of this item's properties in the tool-bar items
10410 vector. */
10411 image = Fcons (Qimage, plist);
10412 props = list4 (Qdisplay, image,
10413 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10414
10415 /* Let the last image hide all remaining spaces in the tool bar
10416 string. The string can be longer than needed when we reuse a
10417 previous string. */
10418 if (i + 1 == f->n_tool_bar_items)
10419 end = SCHARS (f->desired_tool_bar_string);
10420 else
10421 end = i + 1;
10422 Fadd_text_properties (make_number (i), make_number (end),
10423 props, f->desired_tool_bar_string);
10424 #undef PROP
10425 }
10426
10427 UNGCPRO;
10428 }
10429
10430
10431 /* Display one line of the tool-bar of frame IT->f.
10432
10433 HEIGHT specifies the desired height of the tool-bar line.
10434 If the actual height of the glyph row is less than HEIGHT, the
10435 row's height is increased to HEIGHT, and the icons are centered
10436 vertically in the new height.
10437
10438 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10439 count a final empty row in case the tool-bar width exactly matches
10440 the window width.
10441 */
10442
10443 static void
10444 display_tool_bar_line (struct it *it, int height)
10445 {
10446 struct glyph_row *row = it->glyph_row;
10447 int max_x = it->last_visible_x;
10448 struct glyph *last;
10449
10450 prepare_desired_row (row);
10451 row->y = it->current_y;
10452
10453 /* Note that this isn't made use of if the face hasn't a box,
10454 so there's no need to check the face here. */
10455 it->start_of_box_run_p = 1;
10456
10457 while (it->current_x < max_x)
10458 {
10459 int x, n_glyphs_before, i, nglyphs;
10460 struct it it_before;
10461
10462 /* Get the next display element. */
10463 if (!get_next_display_element (it))
10464 {
10465 /* Don't count empty row if we are counting needed tool-bar lines. */
10466 if (height < 0 && !it->hpos)
10467 return;
10468 break;
10469 }
10470
10471 /* Produce glyphs. */
10472 n_glyphs_before = row->used[TEXT_AREA];
10473 it_before = *it;
10474
10475 PRODUCE_GLYPHS (it);
10476
10477 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10478 i = 0;
10479 x = it_before.current_x;
10480 while (i < nglyphs)
10481 {
10482 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10483
10484 if (x + glyph->pixel_width > max_x)
10485 {
10486 /* Glyph doesn't fit on line. Backtrack. */
10487 row->used[TEXT_AREA] = n_glyphs_before;
10488 *it = it_before;
10489 /* If this is the only glyph on this line, it will never fit on the
10490 toolbar, so skip it. But ensure there is at least one glyph,
10491 so we don't accidentally disable the tool-bar. */
10492 if (n_glyphs_before == 0
10493 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10494 break;
10495 goto out;
10496 }
10497
10498 ++it->hpos;
10499 x += glyph->pixel_width;
10500 ++i;
10501 }
10502
10503 /* Stop at line ends. */
10504 if (ITERATOR_AT_END_OF_LINE_P (it))
10505 break;
10506
10507 set_iterator_to_next (it, 1);
10508 }
10509
10510 out:;
10511
10512 row->displays_text_p = row->used[TEXT_AREA] != 0;
10513
10514 /* Use default face for the border below the tool bar.
10515
10516 FIXME: When auto-resize-tool-bars is grow-only, there is
10517 no additional border below the possibly empty tool-bar lines.
10518 So to make the extra empty lines look "normal", we have to
10519 use the tool-bar face for the border too. */
10520 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10521 it->face_id = DEFAULT_FACE_ID;
10522
10523 extend_face_to_end_of_line (it);
10524 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10525 last->right_box_line_p = 1;
10526 if (last == row->glyphs[TEXT_AREA])
10527 last->left_box_line_p = 1;
10528
10529 /* Make line the desired height and center it vertically. */
10530 if ((height -= it->max_ascent + it->max_descent) > 0)
10531 {
10532 /* Don't add more than one line height. */
10533 height %= FRAME_LINE_HEIGHT (it->f);
10534 it->max_ascent += height / 2;
10535 it->max_descent += (height + 1) / 2;
10536 }
10537
10538 compute_line_metrics (it);
10539
10540 /* If line is empty, make it occupy the rest of the tool-bar. */
10541 if (!row->displays_text_p)
10542 {
10543 row->height = row->phys_height = it->last_visible_y - row->y;
10544 row->visible_height = row->height;
10545 row->ascent = row->phys_ascent = 0;
10546 row->extra_line_spacing = 0;
10547 }
10548
10549 row->full_width_p = 1;
10550 row->continued_p = 0;
10551 row->truncated_on_left_p = 0;
10552 row->truncated_on_right_p = 0;
10553
10554 it->current_x = it->hpos = 0;
10555 it->current_y += row->height;
10556 ++it->vpos;
10557 ++it->glyph_row;
10558 }
10559
10560
10561 /* Max tool-bar height. */
10562
10563 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10564 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10565
10566 /* Value is the number of screen lines needed to make all tool-bar
10567 items of frame F visible. The number of actual rows needed is
10568 returned in *N_ROWS if non-NULL. */
10569
10570 static int
10571 tool_bar_lines_needed (struct frame *f, int *n_rows)
10572 {
10573 struct window *w = XWINDOW (f->tool_bar_window);
10574 struct it it;
10575 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10576 the desired matrix, so use (unused) mode-line row as temporary row to
10577 avoid destroying the first tool-bar row. */
10578 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10579
10580 /* Initialize an iterator for iteration over
10581 F->desired_tool_bar_string in the tool-bar window of frame F. */
10582 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10583 it.first_visible_x = 0;
10584 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10585 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10586
10587 while (!ITERATOR_AT_END_P (&it))
10588 {
10589 clear_glyph_row (temp_row);
10590 it.glyph_row = temp_row;
10591 display_tool_bar_line (&it, -1);
10592 }
10593 clear_glyph_row (temp_row);
10594
10595 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10596 if (n_rows)
10597 *n_rows = it.vpos > 0 ? it.vpos : -1;
10598
10599 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10600 }
10601
10602
10603 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10604 0, 1, 0,
10605 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10606 (Lisp_Object frame)
10607 {
10608 struct frame *f;
10609 struct window *w;
10610 int nlines = 0;
10611
10612 if (NILP (frame))
10613 frame = selected_frame;
10614 else
10615 CHECK_FRAME (frame);
10616 f = XFRAME (frame);
10617
10618 if (WINDOWP (f->tool_bar_window)
10619 || (w = XWINDOW (f->tool_bar_window),
10620 WINDOW_TOTAL_LINES (w) > 0))
10621 {
10622 update_tool_bar (f, 1);
10623 if (f->n_tool_bar_items)
10624 {
10625 build_desired_tool_bar_string (f);
10626 nlines = tool_bar_lines_needed (f, NULL);
10627 }
10628 }
10629
10630 return make_number (nlines);
10631 }
10632
10633
10634 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10635 height should be changed. */
10636
10637 static int
10638 redisplay_tool_bar (struct frame *f)
10639 {
10640 struct window *w;
10641 struct it it;
10642 struct glyph_row *row;
10643
10644 #if defined (USE_GTK) || defined (HAVE_NS)
10645 if (FRAME_EXTERNAL_TOOL_BAR (f))
10646 update_frame_tool_bar (f);
10647 return 0;
10648 #endif
10649
10650 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10651 do anything. This means you must start with tool-bar-lines
10652 non-zero to get the auto-sizing effect. Or in other words, you
10653 can turn off tool-bars by specifying tool-bar-lines zero. */
10654 if (!WINDOWP (f->tool_bar_window)
10655 || (w = XWINDOW (f->tool_bar_window),
10656 WINDOW_TOTAL_LINES (w) == 0))
10657 return 0;
10658
10659 /* Set up an iterator for the tool-bar window. */
10660 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10661 it.first_visible_x = 0;
10662 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10663 row = it.glyph_row;
10664
10665 /* Build a string that represents the contents of the tool-bar. */
10666 build_desired_tool_bar_string (f);
10667 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10668
10669 if (f->n_tool_bar_rows == 0)
10670 {
10671 int nlines;
10672
10673 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10674 nlines != WINDOW_TOTAL_LINES (w)))
10675 {
10676 Lisp_Object frame;
10677 int old_height = WINDOW_TOTAL_LINES (w);
10678
10679 XSETFRAME (frame, f);
10680 Fmodify_frame_parameters (frame,
10681 Fcons (Fcons (Qtool_bar_lines,
10682 make_number (nlines)),
10683 Qnil));
10684 if (WINDOW_TOTAL_LINES (w) != old_height)
10685 {
10686 clear_glyph_matrix (w->desired_matrix);
10687 fonts_changed_p = 1;
10688 return 1;
10689 }
10690 }
10691 }
10692
10693 /* Display as many lines as needed to display all tool-bar items. */
10694
10695 if (f->n_tool_bar_rows > 0)
10696 {
10697 int border, rows, height, extra;
10698
10699 if (INTEGERP (Vtool_bar_border))
10700 border = XINT (Vtool_bar_border);
10701 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10702 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10703 else if (EQ (Vtool_bar_border, Qborder_width))
10704 border = f->border_width;
10705 else
10706 border = 0;
10707 if (border < 0)
10708 border = 0;
10709
10710 rows = f->n_tool_bar_rows;
10711 height = max (1, (it.last_visible_y - border) / rows);
10712 extra = it.last_visible_y - border - height * rows;
10713
10714 while (it.current_y < it.last_visible_y)
10715 {
10716 int h = 0;
10717 if (extra > 0 && rows-- > 0)
10718 {
10719 h = (extra + rows - 1) / rows;
10720 extra -= h;
10721 }
10722 display_tool_bar_line (&it, height + h);
10723 }
10724 }
10725 else
10726 {
10727 while (it.current_y < it.last_visible_y)
10728 display_tool_bar_line (&it, 0);
10729 }
10730
10731 /* It doesn't make much sense to try scrolling in the tool-bar
10732 window, so don't do it. */
10733 w->desired_matrix->no_scrolling_p = 1;
10734 w->must_be_updated_p = 1;
10735
10736 if (!NILP (Vauto_resize_tool_bars))
10737 {
10738 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10739 int change_height_p = 0;
10740
10741 /* If we couldn't display everything, change the tool-bar's
10742 height if there is room for more. */
10743 if (IT_STRING_CHARPOS (it) < it.end_charpos
10744 && it.current_y < max_tool_bar_height)
10745 change_height_p = 1;
10746
10747 row = it.glyph_row - 1;
10748
10749 /* If there are blank lines at the end, except for a partially
10750 visible blank line at the end that is smaller than
10751 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10752 if (!row->displays_text_p
10753 && row->height >= FRAME_LINE_HEIGHT (f))
10754 change_height_p = 1;
10755
10756 /* If row displays tool-bar items, but is partially visible,
10757 change the tool-bar's height. */
10758 if (row->displays_text_p
10759 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10760 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10761 change_height_p = 1;
10762
10763 /* Resize windows as needed by changing the `tool-bar-lines'
10764 frame parameter. */
10765 if (change_height_p)
10766 {
10767 Lisp_Object frame;
10768 int old_height = WINDOW_TOTAL_LINES (w);
10769 int nrows;
10770 int nlines = tool_bar_lines_needed (f, &nrows);
10771
10772 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10773 && !f->minimize_tool_bar_window_p)
10774 ? (nlines > old_height)
10775 : (nlines != old_height));
10776 f->minimize_tool_bar_window_p = 0;
10777
10778 if (change_height_p)
10779 {
10780 XSETFRAME (frame, f);
10781 Fmodify_frame_parameters (frame,
10782 Fcons (Fcons (Qtool_bar_lines,
10783 make_number (nlines)),
10784 Qnil));
10785 if (WINDOW_TOTAL_LINES (w) != old_height)
10786 {
10787 clear_glyph_matrix (w->desired_matrix);
10788 f->n_tool_bar_rows = nrows;
10789 fonts_changed_p = 1;
10790 return 1;
10791 }
10792 }
10793 }
10794 }
10795
10796 f->minimize_tool_bar_window_p = 0;
10797 return 0;
10798 }
10799
10800
10801 /* Get information about the tool-bar item which is displayed in GLYPH
10802 on frame F. Return in *PROP_IDX the index where tool-bar item
10803 properties start in F->tool_bar_items. Value is zero if
10804 GLYPH doesn't display a tool-bar item. */
10805
10806 static int
10807 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10808 {
10809 Lisp_Object prop;
10810 int success_p;
10811 int charpos;
10812
10813 /* This function can be called asynchronously, which means we must
10814 exclude any possibility that Fget_text_property signals an
10815 error. */
10816 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10817 charpos = max (0, charpos);
10818
10819 /* Get the text property `menu-item' at pos. The value of that
10820 property is the start index of this item's properties in
10821 F->tool_bar_items. */
10822 prop = Fget_text_property (make_number (charpos),
10823 Qmenu_item, f->current_tool_bar_string);
10824 if (INTEGERP (prop))
10825 {
10826 *prop_idx = XINT (prop);
10827 success_p = 1;
10828 }
10829 else
10830 success_p = 0;
10831
10832 return success_p;
10833 }
10834
10835 \f
10836 /* Get information about the tool-bar item at position X/Y on frame F.
10837 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10838 the current matrix of the tool-bar window of F, or NULL if not
10839 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10840 item in F->tool_bar_items. Value is
10841
10842 -1 if X/Y is not on a tool-bar item
10843 0 if X/Y is on the same item that was highlighted before.
10844 1 otherwise. */
10845
10846 static int
10847 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
10848 int *hpos, int *vpos, int *prop_idx)
10849 {
10850 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10851 struct window *w = XWINDOW (f->tool_bar_window);
10852 int area;
10853
10854 /* Find the glyph under X/Y. */
10855 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10856 if (*glyph == NULL)
10857 return -1;
10858
10859 /* Get the start of this tool-bar item's properties in
10860 f->tool_bar_items. */
10861 if (!tool_bar_item_info (f, *glyph, prop_idx))
10862 return -1;
10863
10864 /* Is mouse on the highlighted item? */
10865 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
10866 && *vpos >= hlinfo->mouse_face_beg_row
10867 && *vpos <= hlinfo->mouse_face_end_row
10868 && (*vpos > hlinfo->mouse_face_beg_row
10869 || *hpos >= hlinfo->mouse_face_beg_col)
10870 && (*vpos < hlinfo->mouse_face_end_row
10871 || *hpos < hlinfo->mouse_face_end_col
10872 || hlinfo->mouse_face_past_end))
10873 return 0;
10874
10875 return 1;
10876 }
10877
10878
10879 /* EXPORT:
10880 Handle mouse button event on the tool-bar of frame F, at
10881 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10882 0 for button release. MODIFIERS is event modifiers for button
10883 release. */
10884
10885 void
10886 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
10887 unsigned int modifiers)
10888 {
10889 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10890 struct window *w = XWINDOW (f->tool_bar_window);
10891 int hpos, vpos, prop_idx;
10892 struct glyph *glyph;
10893 Lisp_Object enabled_p;
10894
10895 /* If not on the highlighted tool-bar item, return. */
10896 frame_to_window_pixel_xy (w, &x, &y);
10897 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10898 return;
10899
10900 /* If item is disabled, do nothing. */
10901 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10902 if (NILP (enabled_p))
10903 return;
10904
10905 if (down_p)
10906 {
10907 /* Show item in pressed state. */
10908 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
10909 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10910 last_tool_bar_item = prop_idx;
10911 }
10912 else
10913 {
10914 Lisp_Object key, frame;
10915 struct input_event event;
10916 EVENT_INIT (event);
10917
10918 /* Show item in released state. */
10919 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
10920 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10921
10922 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10923
10924 XSETFRAME (frame, f);
10925 event.kind = TOOL_BAR_EVENT;
10926 event.frame_or_window = frame;
10927 event.arg = frame;
10928 kbd_buffer_store_event (&event);
10929
10930 event.kind = TOOL_BAR_EVENT;
10931 event.frame_or_window = frame;
10932 event.arg = key;
10933 event.modifiers = modifiers;
10934 kbd_buffer_store_event (&event);
10935 last_tool_bar_item = -1;
10936 }
10937 }
10938
10939
10940 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10941 tool-bar window-relative coordinates X/Y. Called from
10942 note_mouse_highlight. */
10943
10944 static void
10945 note_tool_bar_highlight (struct frame *f, int x, int y)
10946 {
10947 Lisp_Object window = f->tool_bar_window;
10948 struct window *w = XWINDOW (window);
10949 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10950 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10951 int hpos, vpos;
10952 struct glyph *glyph;
10953 struct glyph_row *row;
10954 int i;
10955 Lisp_Object enabled_p;
10956 int prop_idx;
10957 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10958 int mouse_down_p, rc;
10959
10960 /* Function note_mouse_highlight is called with negative X/Y
10961 values when mouse moves outside of the frame. */
10962 if (x <= 0 || y <= 0)
10963 {
10964 clear_mouse_face (hlinfo);
10965 return;
10966 }
10967
10968 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10969 if (rc < 0)
10970 {
10971 /* Not on tool-bar item. */
10972 clear_mouse_face (hlinfo);
10973 return;
10974 }
10975 else if (rc == 0)
10976 /* On same tool-bar item as before. */
10977 goto set_help_echo;
10978
10979 clear_mouse_face (hlinfo);
10980
10981 /* Mouse is down, but on different tool-bar item? */
10982 mouse_down_p = (dpyinfo->grabbed
10983 && f == last_mouse_frame
10984 && FRAME_LIVE_P (f));
10985 if (mouse_down_p
10986 && last_tool_bar_item != prop_idx)
10987 return;
10988
10989 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10990 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10991
10992 /* If tool-bar item is not enabled, don't highlight it. */
10993 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10994 if (!NILP (enabled_p))
10995 {
10996 /* Compute the x-position of the glyph. In front and past the
10997 image is a space. We include this in the highlighted area. */
10998 row = MATRIX_ROW (w->current_matrix, vpos);
10999 for (i = x = 0; i < hpos; ++i)
11000 x += row->glyphs[TEXT_AREA][i].pixel_width;
11001
11002 /* Record this as the current active region. */
11003 hlinfo->mouse_face_beg_col = hpos;
11004 hlinfo->mouse_face_beg_row = vpos;
11005 hlinfo->mouse_face_beg_x = x;
11006 hlinfo->mouse_face_beg_y = row->y;
11007 hlinfo->mouse_face_past_end = 0;
11008
11009 hlinfo->mouse_face_end_col = hpos + 1;
11010 hlinfo->mouse_face_end_row = vpos;
11011 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11012 hlinfo->mouse_face_end_y = row->y;
11013 hlinfo->mouse_face_window = window;
11014 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11015
11016 /* Display it as active. */
11017 show_mouse_face (hlinfo, draw);
11018 hlinfo->mouse_face_image_state = draw;
11019 }
11020
11021 set_help_echo:
11022
11023 /* Set help_echo_string to a help string to display for this tool-bar item.
11024 XTread_socket does the rest. */
11025 help_echo_object = help_echo_window = Qnil;
11026 help_echo_pos = -1;
11027 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11028 if (NILP (help_echo_string))
11029 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11030 }
11031
11032 #endif /* HAVE_WINDOW_SYSTEM */
11033
11034
11035 \f
11036 /************************************************************************
11037 Horizontal scrolling
11038 ************************************************************************/
11039
11040 static int hscroll_window_tree (Lisp_Object);
11041 static int hscroll_windows (Lisp_Object);
11042
11043 /* For all leaf windows in the window tree rooted at WINDOW, set their
11044 hscroll value so that PT is (i) visible in the window, and (ii) so
11045 that it is not within a certain margin at the window's left and
11046 right border. Value is non-zero if any window's hscroll has been
11047 changed. */
11048
11049 static int
11050 hscroll_window_tree (Lisp_Object window)
11051 {
11052 int hscrolled_p = 0;
11053 int hscroll_relative_p = FLOATP (Vhscroll_step);
11054 int hscroll_step_abs = 0;
11055 double hscroll_step_rel = 0;
11056
11057 if (hscroll_relative_p)
11058 {
11059 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11060 if (hscroll_step_rel < 0)
11061 {
11062 hscroll_relative_p = 0;
11063 hscroll_step_abs = 0;
11064 }
11065 }
11066 else if (INTEGERP (Vhscroll_step))
11067 {
11068 hscroll_step_abs = XINT (Vhscroll_step);
11069 if (hscroll_step_abs < 0)
11070 hscroll_step_abs = 0;
11071 }
11072 else
11073 hscroll_step_abs = 0;
11074
11075 while (WINDOWP (window))
11076 {
11077 struct window *w = XWINDOW (window);
11078
11079 if (WINDOWP (w->hchild))
11080 hscrolled_p |= hscroll_window_tree (w->hchild);
11081 else if (WINDOWP (w->vchild))
11082 hscrolled_p |= hscroll_window_tree (w->vchild);
11083 else if (w->cursor.vpos >= 0)
11084 {
11085 int h_margin;
11086 int text_area_width;
11087 struct glyph_row *current_cursor_row
11088 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11089 struct glyph_row *desired_cursor_row
11090 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11091 struct glyph_row *cursor_row
11092 = (desired_cursor_row->enabled_p
11093 ? desired_cursor_row
11094 : current_cursor_row);
11095
11096 text_area_width = window_box_width (w, TEXT_AREA);
11097
11098 /* Scroll when cursor is inside this scroll margin. */
11099 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11100
11101 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11102 && ((XFASTINT (w->hscroll)
11103 && w->cursor.x <= h_margin)
11104 || (cursor_row->enabled_p
11105 && cursor_row->truncated_on_right_p
11106 && (w->cursor.x >= text_area_width - h_margin))))
11107 {
11108 struct it it;
11109 int hscroll;
11110 struct buffer *saved_current_buffer;
11111 EMACS_INT pt;
11112 int wanted_x;
11113
11114 /* Find point in a display of infinite width. */
11115 saved_current_buffer = current_buffer;
11116 current_buffer = XBUFFER (w->buffer);
11117
11118 if (w == XWINDOW (selected_window))
11119 pt = BUF_PT (current_buffer);
11120 else
11121 {
11122 pt = marker_position (w->pointm);
11123 pt = max (BEGV, pt);
11124 pt = min (ZV, pt);
11125 }
11126
11127 /* Move iterator to pt starting at cursor_row->start in
11128 a line with infinite width. */
11129 init_to_row_start (&it, w, cursor_row);
11130 it.last_visible_x = INFINITY;
11131 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11132 current_buffer = saved_current_buffer;
11133
11134 /* Position cursor in window. */
11135 if (!hscroll_relative_p && hscroll_step_abs == 0)
11136 hscroll = max (0, (it.current_x
11137 - (ITERATOR_AT_END_OF_LINE_P (&it)
11138 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11139 : (text_area_width / 2))))
11140 / FRAME_COLUMN_WIDTH (it.f);
11141 else if (w->cursor.x >= text_area_width - h_margin)
11142 {
11143 if (hscroll_relative_p)
11144 wanted_x = text_area_width * (1 - hscroll_step_rel)
11145 - h_margin;
11146 else
11147 wanted_x = text_area_width
11148 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11149 - h_margin;
11150 hscroll
11151 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11152 }
11153 else
11154 {
11155 if (hscroll_relative_p)
11156 wanted_x = text_area_width * hscroll_step_rel
11157 + h_margin;
11158 else
11159 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11160 + h_margin;
11161 hscroll
11162 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11163 }
11164 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11165
11166 /* Don't call Fset_window_hscroll if value hasn't
11167 changed because it will prevent redisplay
11168 optimizations. */
11169 if (XFASTINT (w->hscroll) != hscroll)
11170 {
11171 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11172 w->hscroll = make_number (hscroll);
11173 hscrolled_p = 1;
11174 }
11175 }
11176 }
11177
11178 window = w->next;
11179 }
11180
11181 /* Value is non-zero if hscroll of any leaf window has been changed. */
11182 return hscrolled_p;
11183 }
11184
11185
11186 /* Set hscroll so that cursor is visible and not inside horizontal
11187 scroll margins for all windows in the tree rooted at WINDOW. See
11188 also hscroll_window_tree above. Value is non-zero if any window's
11189 hscroll has been changed. If it has, desired matrices on the frame
11190 of WINDOW are cleared. */
11191
11192 static int
11193 hscroll_windows (Lisp_Object window)
11194 {
11195 int hscrolled_p = hscroll_window_tree (window);
11196 if (hscrolled_p)
11197 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11198 return hscrolled_p;
11199 }
11200
11201
11202 \f
11203 /************************************************************************
11204 Redisplay
11205 ************************************************************************/
11206
11207 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11208 to a non-zero value. This is sometimes handy to have in a debugger
11209 session. */
11210
11211 #if GLYPH_DEBUG
11212
11213 /* First and last unchanged row for try_window_id. */
11214
11215 int debug_first_unchanged_at_end_vpos;
11216 int debug_last_unchanged_at_beg_vpos;
11217
11218 /* Delta vpos and y. */
11219
11220 int debug_dvpos, debug_dy;
11221
11222 /* Delta in characters and bytes for try_window_id. */
11223
11224 EMACS_INT debug_delta, debug_delta_bytes;
11225
11226 /* Values of window_end_pos and window_end_vpos at the end of
11227 try_window_id. */
11228
11229 EMACS_INT debug_end_pos, debug_end_vpos;
11230
11231 /* Append a string to W->desired_matrix->method. FMT is a printf
11232 format string. A1...A9 are a supplement for a variable-length
11233 argument list. If trace_redisplay_p is non-zero also printf the
11234 resulting string to stderr. */
11235
11236 static void
11237 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11238 struct window *w;
11239 char *fmt;
11240 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11241 {
11242 char buffer[512];
11243 char *method = w->desired_matrix->method;
11244 int len = strlen (method);
11245 int size = sizeof w->desired_matrix->method;
11246 int remaining = size - len - 1;
11247
11248 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11249 if (len && remaining)
11250 {
11251 method[len] = '|';
11252 --remaining, ++len;
11253 }
11254
11255 strncpy (method + len, buffer, remaining);
11256
11257 if (trace_redisplay_p)
11258 fprintf (stderr, "%p (%s): %s\n",
11259 w,
11260 ((BUFFERP (w->buffer)
11261 && STRINGP (XBUFFER (w->buffer)->name))
11262 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11263 : "no buffer"),
11264 buffer);
11265 }
11266
11267 #endif /* GLYPH_DEBUG */
11268
11269
11270 /* Value is non-zero if all changes in window W, which displays
11271 current_buffer, are in the text between START and END. START is a
11272 buffer position, END is given as a distance from Z. Used in
11273 redisplay_internal for display optimization. */
11274
11275 static INLINE int
11276 text_outside_line_unchanged_p (struct window *w,
11277 EMACS_INT start, EMACS_INT end)
11278 {
11279 int unchanged_p = 1;
11280
11281 /* If text or overlays have changed, see where. */
11282 if (XFASTINT (w->last_modified) < MODIFF
11283 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11284 {
11285 /* Gap in the line? */
11286 if (GPT < start || Z - GPT < end)
11287 unchanged_p = 0;
11288
11289 /* Changes start in front of the line, or end after it? */
11290 if (unchanged_p
11291 && (BEG_UNCHANGED < start - 1
11292 || END_UNCHANGED < end))
11293 unchanged_p = 0;
11294
11295 /* If selective display, can't optimize if changes start at the
11296 beginning of the line. */
11297 if (unchanged_p
11298 && INTEGERP (current_buffer->selective_display)
11299 && XINT (current_buffer->selective_display) > 0
11300 && (BEG_UNCHANGED < start || GPT <= start))
11301 unchanged_p = 0;
11302
11303 /* If there are overlays at the start or end of the line, these
11304 may have overlay strings with newlines in them. A change at
11305 START, for instance, may actually concern the display of such
11306 overlay strings as well, and they are displayed on different
11307 lines. So, quickly rule out this case. (For the future, it
11308 might be desirable to implement something more telling than
11309 just BEG/END_UNCHANGED.) */
11310 if (unchanged_p)
11311 {
11312 if (BEG + BEG_UNCHANGED == start
11313 && overlay_touches_p (start))
11314 unchanged_p = 0;
11315 if (END_UNCHANGED == end
11316 && overlay_touches_p (Z - end))
11317 unchanged_p = 0;
11318 }
11319
11320 /* Under bidi reordering, adding or deleting a character in the
11321 beginning of a paragraph, before the first strong directional
11322 character, can change the base direction of the paragraph (unless
11323 the buffer specifies a fixed paragraph direction), which will
11324 require to redisplay the whole paragraph. It might be worthwhile
11325 to find the paragraph limits and widen the range of redisplayed
11326 lines to that, but for now just give up this optimization. */
11327 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11328 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11329 unchanged_p = 0;
11330 }
11331
11332 return unchanged_p;
11333 }
11334
11335
11336 /* Do a frame update, taking possible shortcuts into account. This is
11337 the main external entry point for redisplay.
11338
11339 If the last redisplay displayed an echo area message and that message
11340 is no longer requested, we clear the echo area or bring back the
11341 mini-buffer if that is in use. */
11342
11343 void
11344 redisplay (void)
11345 {
11346 redisplay_internal (0);
11347 }
11348
11349
11350 static Lisp_Object
11351 overlay_arrow_string_or_property (Lisp_Object var)
11352 {
11353 Lisp_Object val;
11354
11355 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11356 return val;
11357
11358 return Voverlay_arrow_string;
11359 }
11360
11361 /* Return 1 if there are any overlay-arrows in current_buffer. */
11362 static int
11363 overlay_arrow_in_current_buffer_p (void)
11364 {
11365 Lisp_Object vlist;
11366
11367 for (vlist = Voverlay_arrow_variable_list;
11368 CONSP (vlist);
11369 vlist = XCDR (vlist))
11370 {
11371 Lisp_Object var = XCAR (vlist);
11372 Lisp_Object val;
11373
11374 if (!SYMBOLP (var))
11375 continue;
11376 val = find_symbol_value (var);
11377 if (MARKERP (val)
11378 && current_buffer == XMARKER (val)->buffer)
11379 return 1;
11380 }
11381 return 0;
11382 }
11383
11384
11385 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11386 has changed. */
11387
11388 static int
11389 overlay_arrows_changed_p (void)
11390 {
11391 Lisp_Object vlist;
11392
11393 for (vlist = Voverlay_arrow_variable_list;
11394 CONSP (vlist);
11395 vlist = XCDR (vlist))
11396 {
11397 Lisp_Object var = XCAR (vlist);
11398 Lisp_Object val, pstr;
11399
11400 if (!SYMBOLP (var))
11401 continue;
11402 val = find_symbol_value (var);
11403 if (!MARKERP (val))
11404 continue;
11405 if (! EQ (COERCE_MARKER (val),
11406 Fget (var, Qlast_arrow_position))
11407 || ! (pstr = overlay_arrow_string_or_property (var),
11408 EQ (pstr, Fget (var, Qlast_arrow_string))))
11409 return 1;
11410 }
11411 return 0;
11412 }
11413
11414 /* Mark overlay arrows to be updated on next redisplay. */
11415
11416 static void
11417 update_overlay_arrows (int up_to_date)
11418 {
11419 Lisp_Object vlist;
11420
11421 for (vlist = Voverlay_arrow_variable_list;
11422 CONSP (vlist);
11423 vlist = XCDR (vlist))
11424 {
11425 Lisp_Object var = XCAR (vlist);
11426
11427 if (!SYMBOLP (var))
11428 continue;
11429
11430 if (up_to_date > 0)
11431 {
11432 Lisp_Object val = find_symbol_value (var);
11433 Fput (var, Qlast_arrow_position,
11434 COERCE_MARKER (val));
11435 Fput (var, Qlast_arrow_string,
11436 overlay_arrow_string_or_property (var));
11437 }
11438 else if (up_to_date < 0
11439 || !NILP (Fget (var, Qlast_arrow_position)))
11440 {
11441 Fput (var, Qlast_arrow_position, Qt);
11442 Fput (var, Qlast_arrow_string, Qt);
11443 }
11444 }
11445 }
11446
11447
11448 /* Return overlay arrow string to display at row.
11449 Return integer (bitmap number) for arrow bitmap in left fringe.
11450 Return nil if no overlay arrow. */
11451
11452 static Lisp_Object
11453 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11454 {
11455 Lisp_Object vlist;
11456
11457 for (vlist = Voverlay_arrow_variable_list;
11458 CONSP (vlist);
11459 vlist = XCDR (vlist))
11460 {
11461 Lisp_Object var = XCAR (vlist);
11462 Lisp_Object val;
11463
11464 if (!SYMBOLP (var))
11465 continue;
11466
11467 val = find_symbol_value (var);
11468
11469 if (MARKERP (val)
11470 && current_buffer == XMARKER (val)->buffer
11471 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11472 {
11473 if (FRAME_WINDOW_P (it->f)
11474 /* FIXME: if ROW->reversed_p is set, this should test
11475 the right fringe, not the left one. */
11476 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11477 {
11478 #ifdef HAVE_WINDOW_SYSTEM
11479 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11480 {
11481 int fringe_bitmap;
11482 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11483 return make_number (fringe_bitmap);
11484 }
11485 #endif
11486 return make_number (-1); /* Use default arrow bitmap */
11487 }
11488 return overlay_arrow_string_or_property (var);
11489 }
11490 }
11491
11492 return Qnil;
11493 }
11494
11495 /* Return 1 if point moved out of or into a composition. Otherwise
11496 return 0. PREV_BUF and PREV_PT are the last point buffer and
11497 position. BUF and PT are the current point buffer and position. */
11498
11499 int
11500 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11501 struct buffer *buf, EMACS_INT pt)
11502 {
11503 EMACS_INT start, end;
11504 Lisp_Object prop;
11505 Lisp_Object buffer;
11506
11507 XSETBUFFER (buffer, buf);
11508 /* Check a composition at the last point if point moved within the
11509 same buffer. */
11510 if (prev_buf == buf)
11511 {
11512 if (prev_pt == pt)
11513 /* Point didn't move. */
11514 return 0;
11515
11516 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11517 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11518 && COMPOSITION_VALID_P (start, end, prop)
11519 && start < prev_pt && end > prev_pt)
11520 /* The last point was within the composition. Return 1 iff
11521 point moved out of the composition. */
11522 return (pt <= start || pt >= end);
11523 }
11524
11525 /* Check a composition at the current point. */
11526 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11527 && find_composition (pt, -1, &start, &end, &prop, buffer)
11528 && COMPOSITION_VALID_P (start, end, prop)
11529 && start < pt && end > pt);
11530 }
11531
11532
11533 /* Reconsider the setting of B->clip_changed which is displayed
11534 in window W. */
11535
11536 static INLINE void
11537 reconsider_clip_changes (struct window *w, struct buffer *b)
11538 {
11539 if (b->clip_changed
11540 && !NILP (w->window_end_valid)
11541 && w->current_matrix->buffer == b
11542 && w->current_matrix->zv == BUF_ZV (b)
11543 && w->current_matrix->begv == BUF_BEGV (b))
11544 b->clip_changed = 0;
11545
11546 /* If display wasn't paused, and W is not a tool bar window, see if
11547 point has been moved into or out of a composition. In that case,
11548 we set b->clip_changed to 1 to force updating the screen. If
11549 b->clip_changed has already been set to 1, we can skip this
11550 check. */
11551 if (!b->clip_changed
11552 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11553 {
11554 EMACS_INT pt;
11555
11556 if (w == XWINDOW (selected_window))
11557 pt = BUF_PT (current_buffer);
11558 else
11559 pt = marker_position (w->pointm);
11560
11561 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11562 || pt != XINT (w->last_point))
11563 && check_point_in_composition (w->current_matrix->buffer,
11564 XINT (w->last_point),
11565 XBUFFER (w->buffer), pt))
11566 b->clip_changed = 1;
11567 }
11568 }
11569 \f
11570
11571 /* Select FRAME to forward the values of frame-local variables into C
11572 variables so that the redisplay routines can access those values
11573 directly. */
11574
11575 static void
11576 select_frame_for_redisplay (Lisp_Object frame)
11577 {
11578 Lisp_Object tail, tem;
11579 Lisp_Object old = selected_frame;
11580 struct Lisp_Symbol *sym;
11581
11582 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11583
11584 selected_frame = frame;
11585
11586 do {
11587 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11588 if (CONSP (XCAR (tail))
11589 && (tem = XCAR (XCAR (tail)),
11590 SYMBOLP (tem))
11591 && (sym = indirect_variable (XSYMBOL (tem)),
11592 sym->redirect == SYMBOL_LOCALIZED)
11593 && sym->val.blv->frame_local)
11594 /* Use find_symbol_value rather than Fsymbol_value
11595 to avoid an error if it is void. */
11596 find_symbol_value (tem);
11597 } while (!EQ (frame, old) && (frame = old, 1));
11598 }
11599
11600
11601 #define STOP_POLLING \
11602 do { if (! polling_stopped_here) stop_polling (); \
11603 polling_stopped_here = 1; } while (0)
11604
11605 #define RESUME_POLLING \
11606 do { if (polling_stopped_here) start_polling (); \
11607 polling_stopped_here = 0; } while (0)
11608
11609
11610 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11611 response to any user action; therefore, we should preserve the echo
11612 area. (Actually, our caller does that job.) Perhaps in the future
11613 avoid recentering windows if it is not necessary; currently that
11614 causes some problems. */
11615
11616 static void
11617 redisplay_internal (int preserve_echo_area)
11618 {
11619 struct window *w = XWINDOW (selected_window);
11620 struct frame *f;
11621 int pause;
11622 int must_finish = 0;
11623 struct text_pos tlbufpos, tlendpos;
11624 int number_of_visible_frames;
11625 int count, count1;
11626 struct frame *sf;
11627 int polling_stopped_here = 0;
11628 Lisp_Object old_frame = selected_frame;
11629
11630 /* Non-zero means redisplay has to consider all windows on all
11631 frames. Zero means, only selected_window is considered. */
11632 int consider_all_windows_p;
11633
11634 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11635
11636 /* No redisplay if running in batch mode or frame is not yet fully
11637 initialized, or redisplay is explicitly turned off by setting
11638 Vinhibit_redisplay. */
11639 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11640 || !NILP (Vinhibit_redisplay))
11641 return;
11642
11643 /* Don't examine these until after testing Vinhibit_redisplay.
11644 When Emacs is shutting down, perhaps because its connection to
11645 X has dropped, we should not look at them at all. */
11646 f = XFRAME (w->frame);
11647 sf = SELECTED_FRAME ();
11648
11649 if (!f->glyphs_initialized_p)
11650 return;
11651
11652 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11653 if (popup_activated ())
11654 return;
11655 #endif
11656
11657 /* I don't think this happens but let's be paranoid. */
11658 if (redisplaying_p)
11659 return;
11660
11661 /* Record a function that resets redisplaying_p to its old value
11662 when we leave this function. */
11663 count = SPECPDL_INDEX ();
11664 record_unwind_protect (unwind_redisplay,
11665 Fcons (make_number (redisplaying_p), selected_frame));
11666 ++redisplaying_p;
11667 specbind (Qinhibit_free_realized_faces, Qnil);
11668
11669 {
11670 Lisp_Object tail, frame;
11671
11672 FOR_EACH_FRAME (tail, frame)
11673 {
11674 struct frame *f = XFRAME (frame);
11675 f->already_hscrolled_p = 0;
11676 }
11677 }
11678
11679 retry:
11680 if (!EQ (old_frame, selected_frame)
11681 && FRAME_LIVE_P (XFRAME (old_frame)))
11682 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11683 selected_frame and selected_window to be temporarily out-of-sync so
11684 when we come back here via `goto retry', we need to resync because we
11685 may need to run Elisp code (via prepare_menu_bars). */
11686 select_frame_for_redisplay (old_frame);
11687
11688 pause = 0;
11689 reconsider_clip_changes (w, current_buffer);
11690 last_escape_glyph_frame = NULL;
11691 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11692 last_glyphless_glyph_frame = NULL;
11693 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
11694
11695 /* If new fonts have been loaded that make a glyph matrix adjustment
11696 necessary, do it. */
11697 if (fonts_changed_p)
11698 {
11699 adjust_glyphs (NULL);
11700 ++windows_or_buffers_changed;
11701 fonts_changed_p = 0;
11702 }
11703
11704 /* If face_change_count is non-zero, init_iterator will free all
11705 realized faces, which includes the faces referenced from current
11706 matrices. So, we can't reuse current matrices in this case. */
11707 if (face_change_count)
11708 ++windows_or_buffers_changed;
11709
11710 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11711 && FRAME_TTY (sf)->previous_frame != sf)
11712 {
11713 /* Since frames on a single ASCII terminal share the same
11714 display area, displaying a different frame means redisplay
11715 the whole thing. */
11716 windows_or_buffers_changed++;
11717 SET_FRAME_GARBAGED (sf);
11718 #ifndef DOS_NT
11719 set_tty_color_mode (FRAME_TTY (sf), sf);
11720 #endif
11721 FRAME_TTY (sf)->previous_frame = sf;
11722 }
11723
11724 /* Set the visible flags for all frames. Do this before checking
11725 for resized or garbaged frames; they want to know if their frames
11726 are visible. See the comment in frame.h for
11727 FRAME_SAMPLE_VISIBILITY. */
11728 {
11729 Lisp_Object tail, frame;
11730
11731 number_of_visible_frames = 0;
11732
11733 FOR_EACH_FRAME (tail, frame)
11734 {
11735 struct frame *f = XFRAME (frame);
11736
11737 FRAME_SAMPLE_VISIBILITY (f);
11738 if (FRAME_VISIBLE_P (f))
11739 ++number_of_visible_frames;
11740 clear_desired_matrices (f);
11741 }
11742 }
11743
11744 /* Notice any pending interrupt request to change frame size. */
11745 do_pending_window_change (1);
11746
11747 /* Clear frames marked as garbaged. */
11748 if (frame_garbaged)
11749 clear_garbaged_frames ();
11750
11751 /* Build menubar and tool-bar items. */
11752 if (NILP (Vmemory_full))
11753 prepare_menu_bars ();
11754
11755 if (windows_or_buffers_changed)
11756 update_mode_lines++;
11757
11758 /* Detect case that we need to write or remove a star in the mode line. */
11759 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11760 {
11761 w->update_mode_line = Qt;
11762 if (buffer_shared > 1)
11763 update_mode_lines++;
11764 }
11765
11766 /* Avoid invocation of point motion hooks by `current_column' below. */
11767 count1 = SPECPDL_INDEX ();
11768 specbind (Qinhibit_point_motion_hooks, Qt);
11769
11770 /* If %c is in the mode line, update it if needed. */
11771 if (!NILP (w->column_number_displayed)
11772 /* This alternative quickly identifies a common case
11773 where no change is needed. */
11774 && !(PT == XFASTINT (w->last_point)
11775 && XFASTINT (w->last_modified) >= MODIFF
11776 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11777 && (XFASTINT (w->column_number_displayed)
11778 != (int) current_column ())) /* iftc */
11779 w->update_mode_line = Qt;
11780
11781 unbind_to (count1, Qnil);
11782
11783 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11784
11785 /* The variable buffer_shared is set in redisplay_window and
11786 indicates that we redisplay a buffer in different windows. See
11787 there. */
11788 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11789 || cursor_type_changed);
11790
11791 /* If specs for an arrow have changed, do thorough redisplay
11792 to ensure we remove any arrow that should no longer exist. */
11793 if (overlay_arrows_changed_p ())
11794 consider_all_windows_p = windows_or_buffers_changed = 1;
11795
11796 /* Normally the message* functions will have already displayed and
11797 updated the echo area, but the frame may have been trashed, or
11798 the update may have been preempted, so display the echo area
11799 again here. Checking message_cleared_p captures the case that
11800 the echo area should be cleared. */
11801 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11802 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11803 || (message_cleared_p
11804 && minibuf_level == 0
11805 /* If the mini-window is currently selected, this means the
11806 echo-area doesn't show through. */
11807 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11808 {
11809 int window_height_changed_p = echo_area_display (0);
11810 must_finish = 1;
11811
11812 /* If we don't display the current message, don't clear the
11813 message_cleared_p flag, because, if we did, we wouldn't clear
11814 the echo area in the next redisplay which doesn't preserve
11815 the echo area. */
11816 if (!display_last_displayed_message_p)
11817 message_cleared_p = 0;
11818
11819 if (fonts_changed_p)
11820 goto retry;
11821 else if (window_height_changed_p)
11822 {
11823 consider_all_windows_p = 1;
11824 ++update_mode_lines;
11825 ++windows_or_buffers_changed;
11826
11827 /* If window configuration was changed, frames may have been
11828 marked garbaged. Clear them or we will experience
11829 surprises wrt scrolling. */
11830 if (frame_garbaged)
11831 clear_garbaged_frames ();
11832 }
11833 }
11834 else if (EQ (selected_window, minibuf_window)
11835 && (current_buffer->clip_changed
11836 || XFASTINT (w->last_modified) < MODIFF
11837 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11838 && resize_mini_window (w, 0))
11839 {
11840 /* Resized active mini-window to fit the size of what it is
11841 showing if its contents might have changed. */
11842 must_finish = 1;
11843 /* FIXME: this causes all frames to be updated, which seems unnecessary
11844 since only the current frame needs to be considered. This function needs
11845 to be rewritten with two variables, consider_all_windows and
11846 consider_all_frames. */
11847 consider_all_windows_p = 1;
11848 ++windows_or_buffers_changed;
11849 ++update_mode_lines;
11850
11851 /* If window configuration was changed, frames may have been
11852 marked garbaged. Clear them or we will experience
11853 surprises wrt scrolling. */
11854 if (frame_garbaged)
11855 clear_garbaged_frames ();
11856 }
11857
11858
11859 /* If showing the region, and mark has changed, we must redisplay
11860 the whole window. The assignment to this_line_start_pos prevents
11861 the optimization directly below this if-statement. */
11862 if (((!NILP (Vtransient_mark_mode)
11863 && !NILP (XBUFFER (w->buffer)->mark_active))
11864 != !NILP (w->region_showing))
11865 || (!NILP (w->region_showing)
11866 && !EQ (w->region_showing,
11867 Fmarker_position (XBUFFER (w->buffer)->mark))))
11868 CHARPOS (this_line_start_pos) = 0;
11869
11870 /* Optimize the case that only the line containing the cursor in the
11871 selected window has changed. Variables starting with this_ are
11872 set in display_line and record information about the line
11873 containing the cursor. */
11874 tlbufpos = this_line_start_pos;
11875 tlendpos = this_line_end_pos;
11876 if (!consider_all_windows_p
11877 && CHARPOS (tlbufpos) > 0
11878 && NILP (w->update_mode_line)
11879 && !current_buffer->clip_changed
11880 && !current_buffer->prevent_redisplay_optimizations_p
11881 && FRAME_VISIBLE_P (XFRAME (w->frame))
11882 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11883 /* Make sure recorded data applies to current buffer, etc. */
11884 && this_line_buffer == current_buffer
11885 && current_buffer == XBUFFER (w->buffer)
11886 && NILP (w->force_start)
11887 && NILP (w->optional_new_start)
11888 /* Point must be on the line that we have info recorded about. */
11889 && PT >= CHARPOS (tlbufpos)
11890 && PT <= Z - CHARPOS (tlendpos)
11891 /* All text outside that line, including its final newline,
11892 must be unchanged. */
11893 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11894 CHARPOS (tlendpos)))
11895 {
11896 if (CHARPOS (tlbufpos) > BEGV
11897 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11898 && (CHARPOS (tlbufpos) == ZV
11899 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11900 /* Former continuation line has disappeared by becoming empty. */
11901 goto cancel;
11902 else if (XFASTINT (w->last_modified) < MODIFF
11903 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11904 || MINI_WINDOW_P (w))
11905 {
11906 /* We have to handle the case of continuation around a
11907 wide-column character (see the comment in indent.c around
11908 line 1340).
11909
11910 For instance, in the following case:
11911
11912 -------- Insert --------
11913 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11914 J_I_ ==> J_I_ `^^' are cursors.
11915 ^^ ^^
11916 -------- --------
11917
11918 As we have to redraw the line above, we cannot use this
11919 optimization. */
11920
11921 struct it it;
11922 int line_height_before = this_line_pixel_height;
11923
11924 /* Note that start_display will handle the case that the
11925 line starting at tlbufpos is a continuation line. */
11926 start_display (&it, w, tlbufpos);
11927
11928 /* Implementation note: It this still necessary? */
11929 if (it.current_x != this_line_start_x)
11930 goto cancel;
11931
11932 TRACE ((stderr, "trying display optimization 1\n"));
11933 w->cursor.vpos = -1;
11934 overlay_arrow_seen = 0;
11935 it.vpos = this_line_vpos;
11936 it.current_y = this_line_y;
11937 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11938 display_line (&it);
11939
11940 /* If line contains point, is not continued,
11941 and ends at same distance from eob as before, we win. */
11942 if (w->cursor.vpos >= 0
11943 /* Line is not continued, otherwise this_line_start_pos
11944 would have been set to 0 in display_line. */
11945 && CHARPOS (this_line_start_pos)
11946 /* Line ends as before. */
11947 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11948 /* Line has same height as before. Otherwise other lines
11949 would have to be shifted up or down. */
11950 && this_line_pixel_height == line_height_before)
11951 {
11952 /* If this is not the window's last line, we must adjust
11953 the charstarts of the lines below. */
11954 if (it.current_y < it.last_visible_y)
11955 {
11956 struct glyph_row *row
11957 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11958 EMACS_INT delta, delta_bytes;
11959
11960 /* We used to distinguish between two cases here,
11961 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11962 when the line ends in a newline or the end of the
11963 buffer's accessible portion. But both cases did
11964 the same, so they were collapsed. */
11965 delta = (Z
11966 - CHARPOS (tlendpos)
11967 - MATRIX_ROW_START_CHARPOS (row));
11968 delta_bytes = (Z_BYTE
11969 - BYTEPOS (tlendpos)
11970 - MATRIX_ROW_START_BYTEPOS (row));
11971
11972 increment_matrix_positions (w->current_matrix,
11973 this_line_vpos + 1,
11974 w->current_matrix->nrows,
11975 delta, delta_bytes);
11976 }
11977
11978 /* If this row displays text now but previously didn't,
11979 or vice versa, w->window_end_vpos may have to be
11980 adjusted. */
11981 if ((it.glyph_row - 1)->displays_text_p)
11982 {
11983 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11984 XSETINT (w->window_end_vpos, this_line_vpos);
11985 }
11986 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11987 && this_line_vpos > 0)
11988 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11989 w->window_end_valid = Qnil;
11990
11991 /* Update hint: No need to try to scroll in update_window. */
11992 w->desired_matrix->no_scrolling_p = 1;
11993
11994 #if GLYPH_DEBUG
11995 *w->desired_matrix->method = 0;
11996 debug_method_add (w, "optimization 1");
11997 #endif
11998 #ifdef HAVE_WINDOW_SYSTEM
11999 update_window_fringes (w, 0);
12000 #endif
12001 goto update;
12002 }
12003 else
12004 goto cancel;
12005 }
12006 else if (/* Cursor position hasn't changed. */
12007 PT == XFASTINT (w->last_point)
12008 /* Make sure the cursor was last displayed
12009 in this window. Otherwise we have to reposition it. */
12010 && 0 <= w->cursor.vpos
12011 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12012 {
12013 if (!must_finish)
12014 {
12015 do_pending_window_change (1);
12016
12017 /* We used to always goto end_of_redisplay here, but this
12018 isn't enough if we have a blinking cursor. */
12019 if (w->cursor_off_p == w->last_cursor_off_p)
12020 goto end_of_redisplay;
12021 }
12022 goto update;
12023 }
12024 /* If highlighting the region, or if the cursor is in the echo area,
12025 then we can't just move the cursor. */
12026 else if (! (!NILP (Vtransient_mark_mode)
12027 && !NILP (current_buffer->mark_active))
12028 && (EQ (selected_window, current_buffer->last_selected_window)
12029 || highlight_nonselected_windows)
12030 && NILP (w->region_showing)
12031 && NILP (Vshow_trailing_whitespace)
12032 && !cursor_in_echo_area)
12033 {
12034 struct it it;
12035 struct glyph_row *row;
12036
12037 /* Skip from tlbufpos to PT and see where it is. Note that
12038 PT may be in invisible text. If so, we will end at the
12039 next visible position. */
12040 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12041 NULL, DEFAULT_FACE_ID);
12042 it.current_x = this_line_start_x;
12043 it.current_y = this_line_y;
12044 it.vpos = this_line_vpos;
12045
12046 /* The call to move_it_to stops in front of PT, but
12047 moves over before-strings. */
12048 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12049
12050 if (it.vpos == this_line_vpos
12051 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12052 row->enabled_p))
12053 {
12054 xassert (this_line_vpos == it.vpos);
12055 xassert (this_line_y == it.current_y);
12056 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12057 #if GLYPH_DEBUG
12058 *w->desired_matrix->method = 0;
12059 debug_method_add (w, "optimization 3");
12060 #endif
12061 goto update;
12062 }
12063 else
12064 goto cancel;
12065 }
12066
12067 cancel:
12068 /* Text changed drastically or point moved off of line. */
12069 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12070 }
12071
12072 CHARPOS (this_line_start_pos) = 0;
12073 consider_all_windows_p |= buffer_shared > 1;
12074 ++clear_face_cache_count;
12075 #ifdef HAVE_WINDOW_SYSTEM
12076 ++clear_image_cache_count;
12077 #endif
12078
12079 /* Build desired matrices, and update the display. If
12080 consider_all_windows_p is non-zero, do it for all windows on all
12081 frames. Otherwise do it for selected_window, only. */
12082
12083 if (consider_all_windows_p)
12084 {
12085 Lisp_Object tail, frame;
12086
12087 FOR_EACH_FRAME (tail, frame)
12088 XFRAME (frame)->updated_p = 0;
12089
12090 /* Recompute # windows showing selected buffer. This will be
12091 incremented each time such a window is displayed. */
12092 buffer_shared = 0;
12093
12094 FOR_EACH_FRAME (tail, frame)
12095 {
12096 struct frame *f = XFRAME (frame);
12097
12098 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12099 {
12100 if (! EQ (frame, selected_frame))
12101 /* Select the frame, for the sake of frame-local
12102 variables. */
12103 select_frame_for_redisplay (frame);
12104
12105 /* Mark all the scroll bars to be removed; we'll redeem
12106 the ones we want when we redisplay their windows. */
12107 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12108 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12109
12110 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12111 redisplay_windows (FRAME_ROOT_WINDOW (f));
12112
12113 /* The X error handler may have deleted that frame. */
12114 if (!FRAME_LIVE_P (f))
12115 continue;
12116
12117 /* Any scroll bars which redisplay_windows should have
12118 nuked should now go away. */
12119 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12120 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12121
12122 /* If fonts changed, display again. */
12123 /* ??? rms: I suspect it is a mistake to jump all the way
12124 back to retry here. It should just retry this frame. */
12125 if (fonts_changed_p)
12126 goto retry;
12127
12128 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12129 {
12130 /* See if we have to hscroll. */
12131 if (!f->already_hscrolled_p)
12132 {
12133 f->already_hscrolled_p = 1;
12134 if (hscroll_windows (f->root_window))
12135 goto retry;
12136 }
12137
12138 /* Prevent various kinds of signals during display
12139 update. stdio is not robust about handling
12140 signals, which can cause an apparent I/O
12141 error. */
12142 if (interrupt_input)
12143 unrequest_sigio ();
12144 STOP_POLLING;
12145
12146 /* Update the display. */
12147 set_window_update_flags (XWINDOW (f->root_window), 1);
12148 pause |= update_frame (f, 0, 0);
12149 f->updated_p = 1;
12150 }
12151 }
12152 }
12153
12154 if (!EQ (old_frame, selected_frame)
12155 && FRAME_LIVE_P (XFRAME (old_frame)))
12156 /* We played a bit fast-and-loose above and allowed selected_frame
12157 and selected_window to be temporarily out-of-sync but let's make
12158 sure this stays contained. */
12159 select_frame_for_redisplay (old_frame);
12160 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12161
12162 if (!pause)
12163 {
12164 /* Do the mark_window_display_accurate after all windows have
12165 been redisplayed because this call resets flags in buffers
12166 which are needed for proper redisplay. */
12167 FOR_EACH_FRAME (tail, frame)
12168 {
12169 struct frame *f = XFRAME (frame);
12170 if (f->updated_p)
12171 {
12172 mark_window_display_accurate (f->root_window, 1);
12173 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12174 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12175 }
12176 }
12177 }
12178 }
12179 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12180 {
12181 Lisp_Object mini_window;
12182 struct frame *mini_frame;
12183
12184 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12185 /* Use list_of_error, not Qerror, so that
12186 we catch only errors and don't run the debugger. */
12187 internal_condition_case_1 (redisplay_window_1, selected_window,
12188 list_of_error,
12189 redisplay_window_error);
12190
12191 /* Compare desired and current matrices, perform output. */
12192
12193 update:
12194 /* If fonts changed, display again. */
12195 if (fonts_changed_p)
12196 goto retry;
12197
12198 /* Prevent various kinds of signals during display update.
12199 stdio is not robust about handling signals,
12200 which can cause an apparent I/O error. */
12201 if (interrupt_input)
12202 unrequest_sigio ();
12203 STOP_POLLING;
12204
12205 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12206 {
12207 if (hscroll_windows (selected_window))
12208 goto retry;
12209
12210 XWINDOW (selected_window)->must_be_updated_p = 1;
12211 pause = update_frame (sf, 0, 0);
12212 }
12213
12214 /* We may have called echo_area_display at the top of this
12215 function. If the echo area is on another frame, that may
12216 have put text on a frame other than the selected one, so the
12217 above call to update_frame would not have caught it. Catch
12218 it here. */
12219 mini_window = FRAME_MINIBUF_WINDOW (sf);
12220 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12221
12222 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12223 {
12224 XWINDOW (mini_window)->must_be_updated_p = 1;
12225 pause |= update_frame (mini_frame, 0, 0);
12226 if (!pause && hscroll_windows (mini_window))
12227 goto retry;
12228 }
12229 }
12230
12231 /* If display was paused because of pending input, make sure we do a
12232 thorough update the next time. */
12233 if (pause)
12234 {
12235 /* Prevent the optimization at the beginning of
12236 redisplay_internal that tries a single-line update of the
12237 line containing the cursor in the selected window. */
12238 CHARPOS (this_line_start_pos) = 0;
12239
12240 /* Let the overlay arrow be updated the next time. */
12241 update_overlay_arrows (0);
12242
12243 /* If we pause after scrolling, some rows in the current
12244 matrices of some windows are not valid. */
12245 if (!WINDOW_FULL_WIDTH_P (w)
12246 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12247 update_mode_lines = 1;
12248 }
12249 else
12250 {
12251 if (!consider_all_windows_p)
12252 {
12253 /* This has already been done above if
12254 consider_all_windows_p is set. */
12255 mark_window_display_accurate_1 (w, 1);
12256
12257 /* Say overlay arrows are up to date. */
12258 update_overlay_arrows (1);
12259
12260 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12261 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12262 }
12263
12264 update_mode_lines = 0;
12265 windows_or_buffers_changed = 0;
12266 cursor_type_changed = 0;
12267 }
12268
12269 /* Start SIGIO interrupts coming again. Having them off during the
12270 code above makes it less likely one will discard output, but not
12271 impossible, since there might be stuff in the system buffer here.
12272 But it is much hairier to try to do anything about that. */
12273 if (interrupt_input)
12274 request_sigio ();
12275 RESUME_POLLING;
12276
12277 /* If a frame has become visible which was not before, redisplay
12278 again, so that we display it. Expose events for such a frame
12279 (which it gets when becoming visible) don't call the parts of
12280 redisplay constructing glyphs, so simply exposing a frame won't
12281 display anything in this case. So, we have to display these
12282 frames here explicitly. */
12283 if (!pause)
12284 {
12285 Lisp_Object tail, frame;
12286 int new_count = 0;
12287
12288 FOR_EACH_FRAME (tail, frame)
12289 {
12290 int this_is_visible = 0;
12291
12292 if (XFRAME (frame)->visible)
12293 this_is_visible = 1;
12294 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12295 if (XFRAME (frame)->visible)
12296 this_is_visible = 1;
12297
12298 if (this_is_visible)
12299 new_count++;
12300 }
12301
12302 if (new_count != number_of_visible_frames)
12303 windows_or_buffers_changed++;
12304 }
12305
12306 /* Change frame size now if a change is pending. */
12307 do_pending_window_change (1);
12308
12309 /* If we just did a pending size change, or have additional
12310 visible frames, redisplay again. */
12311 if (windows_or_buffers_changed && !pause)
12312 goto retry;
12313
12314 /* Clear the face and image caches.
12315
12316 We used to do this only if consider_all_windows_p. But the cache
12317 needs to be cleared if a timer creates images in the current
12318 buffer (e.g. the test case in Bug#6230). */
12319
12320 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12321 {
12322 clear_face_cache (0);
12323 clear_face_cache_count = 0;
12324 }
12325
12326 #ifdef HAVE_WINDOW_SYSTEM
12327 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12328 {
12329 clear_image_caches (Qnil);
12330 clear_image_cache_count = 0;
12331 }
12332 #endif /* HAVE_WINDOW_SYSTEM */
12333
12334 end_of_redisplay:
12335 unbind_to (count, Qnil);
12336 RESUME_POLLING;
12337 }
12338
12339
12340 /* Redisplay, but leave alone any recent echo area message unless
12341 another message has been requested in its place.
12342
12343 This is useful in situations where you need to redisplay but no
12344 user action has occurred, making it inappropriate for the message
12345 area to be cleared. See tracking_off and
12346 wait_reading_process_output for examples of these situations.
12347
12348 FROM_WHERE is an integer saying from where this function was
12349 called. This is useful for debugging. */
12350
12351 void
12352 redisplay_preserve_echo_area (int from_where)
12353 {
12354 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12355
12356 if (!NILP (echo_area_buffer[1]))
12357 {
12358 /* We have a previously displayed message, but no current
12359 message. Redisplay the previous message. */
12360 display_last_displayed_message_p = 1;
12361 redisplay_internal (1);
12362 display_last_displayed_message_p = 0;
12363 }
12364 else
12365 redisplay_internal (1);
12366
12367 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12368 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12369 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12370 }
12371
12372
12373 /* Function registered with record_unwind_protect in
12374 redisplay_internal. Reset redisplaying_p to the value it had
12375 before redisplay_internal was called, and clear
12376 prevent_freeing_realized_faces_p. It also selects the previously
12377 selected frame, unless it has been deleted (by an X connection
12378 failure during redisplay, for example). */
12379
12380 static Lisp_Object
12381 unwind_redisplay (Lisp_Object val)
12382 {
12383 Lisp_Object old_redisplaying_p, old_frame;
12384
12385 old_redisplaying_p = XCAR (val);
12386 redisplaying_p = XFASTINT (old_redisplaying_p);
12387 old_frame = XCDR (val);
12388 if (! EQ (old_frame, selected_frame)
12389 && FRAME_LIVE_P (XFRAME (old_frame)))
12390 select_frame_for_redisplay (old_frame);
12391 return Qnil;
12392 }
12393
12394
12395 /* Mark the display of window W as accurate or inaccurate. If
12396 ACCURATE_P is non-zero mark display of W as accurate. If
12397 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12398 redisplay_internal is called. */
12399
12400 static void
12401 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12402 {
12403 if (BUFFERP (w->buffer))
12404 {
12405 struct buffer *b = XBUFFER (w->buffer);
12406
12407 w->last_modified
12408 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12409 w->last_overlay_modified
12410 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12411 w->last_had_star
12412 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12413
12414 if (accurate_p)
12415 {
12416 b->clip_changed = 0;
12417 b->prevent_redisplay_optimizations_p = 0;
12418
12419 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12420 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12421 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12422 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12423
12424 w->current_matrix->buffer = b;
12425 w->current_matrix->begv = BUF_BEGV (b);
12426 w->current_matrix->zv = BUF_ZV (b);
12427
12428 w->last_cursor = w->cursor;
12429 w->last_cursor_off_p = w->cursor_off_p;
12430
12431 if (w == XWINDOW (selected_window))
12432 w->last_point = make_number (BUF_PT (b));
12433 else
12434 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12435 }
12436 }
12437
12438 if (accurate_p)
12439 {
12440 w->window_end_valid = w->buffer;
12441 w->update_mode_line = Qnil;
12442 }
12443 }
12444
12445
12446 /* Mark the display of windows in the window tree rooted at WINDOW as
12447 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12448 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12449 be redisplayed the next time redisplay_internal is called. */
12450
12451 void
12452 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12453 {
12454 struct window *w;
12455
12456 for (; !NILP (window); window = w->next)
12457 {
12458 w = XWINDOW (window);
12459 mark_window_display_accurate_1 (w, accurate_p);
12460
12461 if (!NILP (w->vchild))
12462 mark_window_display_accurate (w->vchild, accurate_p);
12463 if (!NILP (w->hchild))
12464 mark_window_display_accurate (w->hchild, accurate_p);
12465 }
12466
12467 if (accurate_p)
12468 {
12469 update_overlay_arrows (1);
12470 }
12471 else
12472 {
12473 /* Force a thorough redisplay the next time by setting
12474 last_arrow_position and last_arrow_string to t, which is
12475 unequal to any useful value of Voverlay_arrow_... */
12476 update_overlay_arrows (-1);
12477 }
12478 }
12479
12480
12481 /* Return value in display table DP (Lisp_Char_Table *) for character
12482 C. Since a display table doesn't have any parent, we don't have to
12483 follow parent. Do not call this function directly but use the
12484 macro DISP_CHAR_VECTOR. */
12485
12486 Lisp_Object
12487 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12488 {
12489 Lisp_Object val;
12490
12491 if (ASCII_CHAR_P (c))
12492 {
12493 val = dp->ascii;
12494 if (SUB_CHAR_TABLE_P (val))
12495 val = XSUB_CHAR_TABLE (val)->contents[c];
12496 }
12497 else
12498 {
12499 Lisp_Object table;
12500
12501 XSETCHAR_TABLE (table, dp);
12502 val = char_table_ref (table, c);
12503 }
12504 if (NILP (val))
12505 val = dp->defalt;
12506 return val;
12507 }
12508
12509
12510 \f
12511 /***********************************************************************
12512 Window Redisplay
12513 ***********************************************************************/
12514
12515 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12516
12517 static void
12518 redisplay_windows (Lisp_Object window)
12519 {
12520 while (!NILP (window))
12521 {
12522 struct window *w = XWINDOW (window);
12523
12524 if (!NILP (w->hchild))
12525 redisplay_windows (w->hchild);
12526 else if (!NILP (w->vchild))
12527 redisplay_windows (w->vchild);
12528 else if (!NILP (w->buffer))
12529 {
12530 displayed_buffer = XBUFFER (w->buffer);
12531 /* Use list_of_error, not Qerror, so that
12532 we catch only errors and don't run the debugger. */
12533 internal_condition_case_1 (redisplay_window_0, window,
12534 list_of_error,
12535 redisplay_window_error);
12536 }
12537
12538 window = w->next;
12539 }
12540 }
12541
12542 static Lisp_Object
12543 redisplay_window_error (Lisp_Object ignore)
12544 {
12545 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12546 return Qnil;
12547 }
12548
12549 static Lisp_Object
12550 redisplay_window_0 (Lisp_Object window)
12551 {
12552 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12553 redisplay_window (window, 0);
12554 return Qnil;
12555 }
12556
12557 static Lisp_Object
12558 redisplay_window_1 (Lisp_Object window)
12559 {
12560 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12561 redisplay_window (window, 1);
12562 return Qnil;
12563 }
12564 \f
12565
12566 /* Increment GLYPH until it reaches END or CONDITION fails while
12567 adding (GLYPH)->pixel_width to X. */
12568
12569 #define SKIP_GLYPHS(glyph, end, x, condition) \
12570 do \
12571 { \
12572 (x) += (glyph)->pixel_width; \
12573 ++(glyph); \
12574 } \
12575 while ((glyph) < (end) && (condition))
12576
12577
12578 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12579 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12580 which positions recorded in ROW differ from current buffer
12581 positions.
12582
12583 Return 0 if cursor is not on this row, 1 otherwise. */
12584
12585 int
12586 set_cursor_from_row (struct window *w, struct glyph_row *row,
12587 struct glyph_matrix *matrix,
12588 EMACS_INT delta, EMACS_INT delta_bytes,
12589 int dy, int dvpos)
12590 {
12591 struct glyph *glyph = row->glyphs[TEXT_AREA];
12592 struct glyph *end = glyph + row->used[TEXT_AREA];
12593 struct glyph *cursor = NULL;
12594 /* The last known character position in row. */
12595 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12596 int x = row->x;
12597 EMACS_INT pt_old = PT - delta;
12598 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12599 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12600 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12601 /* A glyph beyond the edge of TEXT_AREA which we should never
12602 touch. */
12603 struct glyph *glyphs_end = end;
12604 /* Non-zero means we've found a match for cursor position, but that
12605 glyph has the avoid_cursor_p flag set. */
12606 int match_with_avoid_cursor = 0;
12607 /* Non-zero means we've seen at least one glyph that came from a
12608 display string. */
12609 int string_seen = 0;
12610 /* Largest and smalles buffer positions seen so far during scan of
12611 glyph row. */
12612 EMACS_INT bpos_max = pos_before;
12613 EMACS_INT bpos_min = pos_after;
12614 /* Last buffer position covered by an overlay string with an integer
12615 `cursor' property. */
12616 EMACS_INT bpos_covered = 0;
12617
12618 /* Skip over glyphs not having an object at the start and the end of
12619 the row. These are special glyphs like truncation marks on
12620 terminal frames. */
12621 if (row->displays_text_p)
12622 {
12623 if (!row->reversed_p)
12624 {
12625 while (glyph < end
12626 && INTEGERP (glyph->object)
12627 && glyph->charpos < 0)
12628 {
12629 x += glyph->pixel_width;
12630 ++glyph;
12631 }
12632 while (end > glyph
12633 && INTEGERP ((end - 1)->object)
12634 /* CHARPOS is zero for blanks and stretch glyphs
12635 inserted by extend_face_to_end_of_line. */
12636 && (end - 1)->charpos <= 0)
12637 --end;
12638 glyph_before = glyph - 1;
12639 glyph_after = end;
12640 }
12641 else
12642 {
12643 struct glyph *g;
12644
12645 /* If the glyph row is reversed, we need to process it from back
12646 to front, so swap the edge pointers. */
12647 glyphs_end = end = glyph - 1;
12648 glyph += row->used[TEXT_AREA] - 1;
12649
12650 while (glyph > end + 1
12651 && INTEGERP (glyph->object)
12652 && glyph->charpos < 0)
12653 {
12654 --glyph;
12655 x -= glyph->pixel_width;
12656 }
12657 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12658 --glyph;
12659 /* By default, in reversed rows we put the cursor on the
12660 rightmost (first in the reading order) glyph. */
12661 for (g = end + 1; g < glyph; g++)
12662 x += g->pixel_width;
12663 while (end < glyph
12664 && INTEGERP ((end + 1)->object)
12665 && (end + 1)->charpos <= 0)
12666 ++end;
12667 glyph_before = glyph + 1;
12668 glyph_after = end;
12669 }
12670 }
12671 else if (row->reversed_p)
12672 {
12673 /* In R2L rows that don't display text, put the cursor on the
12674 rightmost glyph. Case in point: an empty last line that is
12675 part of an R2L paragraph. */
12676 cursor = end - 1;
12677 /* Avoid placing the cursor on the last glyph of the row, where
12678 on terminal frames we hold the vertical border between
12679 adjacent windows. */
12680 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12681 && !WINDOW_RIGHTMOST_P (w)
12682 && cursor == row->glyphs[LAST_AREA] - 1)
12683 cursor--;
12684 x = -1; /* will be computed below, at label compute_x */
12685 }
12686
12687 /* Step 1: Try to find the glyph whose character position
12688 corresponds to point. If that's not possible, find 2 glyphs
12689 whose character positions are the closest to point, one before
12690 point, the other after it. */
12691 if (!row->reversed_p)
12692 while (/* not marched to end of glyph row */
12693 glyph < end
12694 /* glyph was not inserted by redisplay for internal purposes */
12695 && !INTEGERP (glyph->object))
12696 {
12697 if (BUFFERP (glyph->object))
12698 {
12699 EMACS_INT dpos = glyph->charpos - pt_old;
12700
12701 if (glyph->charpos > bpos_max)
12702 bpos_max = glyph->charpos;
12703 if (glyph->charpos < bpos_min)
12704 bpos_min = glyph->charpos;
12705 if (!glyph->avoid_cursor_p)
12706 {
12707 /* If we hit point, we've found the glyph on which to
12708 display the cursor. */
12709 if (dpos == 0)
12710 {
12711 match_with_avoid_cursor = 0;
12712 break;
12713 }
12714 /* See if we've found a better approximation to
12715 POS_BEFORE or to POS_AFTER. Note that we want the
12716 first (leftmost) glyph of all those that are the
12717 closest from below, and the last (rightmost) of all
12718 those from above. */
12719 if (0 > dpos && dpos > pos_before - pt_old)
12720 {
12721 pos_before = glyph->charpos;
12722 glyph_before = glyph;
12723 }
12724 else if (0 < dpos && dpos <= pos_after - pt_old)
12725 {
12726 pos_after = glyph->charpos;
12727 glyph_after = glyph;
12728 }
12729 }
12730 else if (dpos == 0)
12731 match_with_avoid_cursor = 1;
12732 }
12733 else if (STRINGP (glyph->object))
12734 {
12735 Lisp_Object chprop;
12736 EMACS_INT glyph_pos = glyph->charpos;
12737
12738 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12739 glyph->object);
12740 if (INTEGERP (chprop))
12741 {
12742 bpos_covered = bpos_max + XINT (chprop);
12743 /* If the `cursor' property covers buffer positions up
12744 to and including point, we should display cursor on
12745 this glyph. Note that overlays and text properties
12746 with string values stop bidi reordering, so every
12747 buffer position to the left of the string is always
12748 smaller than any position to the right of the
12749 string. Therefore, if a `cursor' property on one
12750 of the string's characters has an integer value, we
12751 will break out of the loop below _before_ we get to
12752 the position match above. IOW, integer values of
12753 the `cursor' property override the "exact match for
12754 point" strategy of positioning the cursor. */
12755 /* Implementation note: bpos_max == pt_old when, e.g.,
12756 we are in an empty line, where bpos_max is set to
12757 MATRIX_ROW_START_CHARPOS, see above. */
12758 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12759 {
12760 cursor = glyph;
12761 break;
12762 }
12763 }
12764
12765 string_seen = 1;
12766 }
12767 x += glyph->pixel_width;
12768 ++glyph;
12769 }
12770 else if (glyph > end) /* row is reversed */
12771 while (!INTEGERP (glyph->object))
12772 {
12773 if (BUFFERP (glyph->object))
12774 {
12775 EMACS_INT dpos = glyph->charpos - pt_old;
12776
12777 if (glyph->charpos > bpos_max)
12778 bpos_max = glyph->charpos;
12779 if (glyph->charpos < bpos_min)
12780 bpos_min = glyph->charpos;
12781 if (!glyph->avoid_cursor_p)
12782 {
12783 if (dpos == 0)
12784 {
12785 match_with_avoid_cursor = 0;
12786 break;
12787 }
12788 if (0 > dpos && dpos > pos_before - pt_old)
12789 {
12790 pos_before = glyph->charpos;
12791 glyph_before = glyph;
12792 }
12793 else if (0 < dpos && dpos <= pos_after - pt_old)
12794 {
12795 pos_after = glyph->charpos;
12796 glyph_after = glyph;
12797 }
12798 }
12799 else if (dpos == 0)
12800 match_with_avoid_cursor = 1;
12801 }
12802 else if (STRINGP (glyph->object))
12803 {
12804 Lisp_Object chprop;
12805 EMACS_INT glyph_pos = glyph->charpos;
12806
12807 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12808 glyph->object);
12809 if (INTEGERP (chprop))
12810 {
12811 bpos_covered = bpos_max + XINT (chprop);
12812 /* If the `cursor' property covers buffer positions up
12813 to and including point, we should display cursor on
12814 this glyph. */
12815 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12816 {
12817 cursor = glyph;
12818 break;
12819 }
12820 }
12821 string_seen = 1;
12822 }
12823 --glyph;
12824 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12825 {
12826 x--; /* can't use any pixel_width */
12827 break;
12828 }
12829 x -= glyph->pixel_width;
12830 }
12831
12832 /* Step 2: If we didn't find an exact match for point, we need to
12833 look for a proper place to put the cursor among glyphs between
12834 GLYPH_BEFORE and GLYPH_AFTER. */
12835 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12836 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12837 && bpos_covered < pt_old)
12838 {
12839 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12840 {
12841 EMACS_INT ellipsis_pos;
12842
12843 /* Scan back over the ellipsis glyphs. */
12844 if (!row->reversed_p)
12845 {
12846 ellipsis_pos = (glyph - 1)->charpos;
12847 while (glyph > row->glyphs[TEXT_AREA]
12848 && (glyph - 1)->charpos == ellipsis_pos)
12849 glyph--, x -= glyph->pixel_width;
12850 /* That loop always goes one position too far, including
12851 the glyph before the ellipsis. So scan forward over
12852 that one. */
12853 x += glyph->pixel_width;
12854 glyph++;
12855 }
12856 else /* row is reversed */
12857 {
12858 ellipsis_pos = (glyph + 1)->charpos;
12859 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12860 && (glyph + 1)->charpos == ellipsis_pos)
12861 glyph++, x += glyph->pixel_width;
12862 x -= glyph->pixel_width;
12863 glyph--;
12864 }
12865 }
12866 else if (match_with_avoid_cursor
12867 /* A truncated row may not include PT among its
12868 character positions. Setting the cursor inside the
12869 scroll margin will trigger recalculation of hscroll
12870 in hscroll_window_tree. */
12871 || (row->truncated_on_left_p && pt_old < bpos_min)
12872 || (row->truncated_on_right_p && pt_old > bpos_max)
12873 /* Zero-width characters produce no glyphs. */
12874 || ((row->reversed_p
12875 ? glyph_after > glyphs_end
12876 : glyph_after < glyphs_end)
12877 && eabs (glyph_after - glyph_before) == 1))
12878 {
12879 cursor = glyph_after;
12880 x = -1;
12881 }
12882 else if (string_seen)
12883 {
12884 int incr = row->reversed_p ? -1 : +1;
12885
12886 /* Need to find the glyph that came out of a string which is
12887 present at point. That glyph is somewhere between
12888 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12889 positioned between POS_BEFORE and POS_AFTER in the
12890 buffer. */
12891 struct glyph *stop = glyph_after;
12892 EMACS_INT pos = pos_before;
12893
12894 x = -1;
12895 for (glyph = glyph_before + incr;
12896 row->reversed_p ? glyph > stop : glyph < stop; )
12897 {
12898
12899 /* Any glyphs that come from the buffer are here because
12900 of bidi reordering. Skip them, and only pay
12901 attention to glyphs that came from some string. */
12902 if (STRINGP (glyph->object))
12903 {
12904 Lisp_Object str;
12905 EMACS_INT tem;
12906
12907 str = glyph->object;
12908 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12909 if (tem == 0 /* from overlay */
12910 || pos <= tem)
12911 {
12912 /* If the string from which this glyph came is
12913 found in the buffer at point, then we've
12914 found the glyph we've been looking for. If
12915 it comes from an overlay (tem == 0), and it
12916 has the `cursor' property on one of its
12917 glyphs, record that glyph as a candidate for
12918 displaying the cursor. (As in the
12919 unidirectional version, we will display the
12920 cursor on the last candidate we find.) */
12921 if (tem == 0 || tem == pt_old)
12922 {
12923 /* The glyphs from this string could have
12924 been reordered. Find the one with the
12925 smallest string position. Or there could
12926 be a character in the string with the
12927 `cursor' property, which means display
12928 cursor on that character's glyph. */
12929 EMACS_INT strpos = glyph->charpos;
12930
12931 cursor = glyph;
12932 for (glyph += incr;
12933 (row->reversed_p ? glyph > stop : glyph < stop)
12934 && EQ (glyph->object, str);
12935 glyph += incr)
12936 {
12937 Lisp_Object cprop;
12938 EMACS_INT gpos = glyph->charpos;
12939
12940 cprop = Fget_char_property (make_number (gpos),
12941 Qcursor,
12942 glyph->object);
12943 if (!NILP (cprop))
12944 {
12945 cursor = glyph;
12946 break;
12947 }
12948 if (glyph->charpos < strpos)
12949 {
12950 strpos = glyph->charpos;
12951 cursor = glyph;
12952 }
12953 }
12954
12955 if (tem == pt_old)
12956 goto compute_x;
12957 }
12958 if (tem)
12959 pos = tem + 1; /* don't find previous instances */
12960 }
12961 /* This string is not what we want; skip all of the
12962 glyphs that came from it. */
12963 do
12964 glyph += incr;
12965 while ((row->reversed_p ? glyph > stop : glyph < stop)
12966 && EQ (glyph->object, str));
12967 }
12968 else
12969 glyph += incr;
12970 }
12971
12972 /* If we reached the end of the line, and END was from a string,
12973 the cursor is not on this line. */
12974 if (cursor == NULL
12975 && (row->reversed_p ? glyph <= end : glyph >= end)
12976 && STRINGP (end->object)
12977 && row->continued_p)
12978 return 0;
12979 }
12980 }
12981
12982 compute_x:
12983 if (cursor != NULL)
12984 glyph = cursor;
12985 if (x < 0)
12986 {
12987 struct glyph *g;
12988
12989 /* Need to compute x that corresponds to GLYPH. */
12990 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12991 {
12992 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12993 abort ();
12994 x += g->pixel_width;
12995 }
12996 }
12997
12998 /* ROW could be part of a continued line, which, under bidi
12999 reordering, might have other rows whose start and end charpos
13000 occlude point. Only set w->cursor if we found a better
13001 approximation to the cursor position than we have from previously
13002 examined candidate rows belonging to the same continued line. */
13003 if (/* we already have a candidate row */
13004 w->cursor.vpos >= 0
13005 /* that candidate is not the row we are processing */
13006 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13007 /* the row we are processing is part of a continued line */
13008 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
13009 /* Make sure cursor.vpos specifies a row whose start and end
13010 charpos occlude point. This is because some callers of this
13011 function leave cursor.vpos at the row where the cursor was
13012 displayed during the last redisplay cycle. */
13013 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13014 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13015 {
13016 struct glyph *g1 =
13017 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13018
13019 /* Don't consider glyphs that are outside TEXT_AREA. */
13020 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13021 return 0;
13022 /* Keep the candidate whose buffer position is the closest to
13023 point. */
13024 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13025 w->cursor.hpos >= 0
13026 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13027 && BUFFERP (g1->object)
13028 && (g1->charpos == pt_old /* an exact match always wins */
13029 || (BUFFERP (glyph->object)
13030 && eabs (g1->charpos - pt_old)
13031 < eabs (glyph->charpos - pt_old))))
13032 return 0;
13033 /* If this candidate gives an exact match, use that. */
13034 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13035 /* Otherwise, keep the candidate that comes from a row
13036 spanning less buffer positions. This may win when one or
13037 both candidate positions are on glyphs that came from
13038 display strings, for which we cannot compare buffer
13039 positions. */
13040 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13041 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13042 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13043 return 0;
13044 }
13045 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13046 w->cursor.x = x;
13047 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13048 w->cursor.y = row->y + dy;
13049
13050 if (w == XWINDOW (selected_window))
13051 {
13052 if (!row->continued_p
13053 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13054 && row->x == 0)
13055 {
13056 this_line_buffer = XBUFFER (w->buffer);
13057
13058 CHARPOS (this_line_start_pos)
13059 = MATRIX_ROW_START_CHARPOS (row) + delta;
13060 BYTEPOS (this_line_start_pos)
13061 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13062
13063 CHARPOS (this_line_end_pos)
13064 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13065 BYTEPOS (this_line_end_pos)
13066 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13067
13068 this_line_y = w->cursor.y;
13069 this_line_pixel_height = row->height;
13070 this_line_vpos = w->cursor.vpos;
13071 this_line_start_x = row->x;
13072 }
13073 else
13074 CHARPOS (this_line_start_pos) = 0;
13075 }
13076
13077 return 1;
13078 }
13079
13080
13081 /* Run window scroll functions, if any, for WINDOW with new window
13082 start STARTP. Sets the window start of WINDOW to that position.
13083
13084 We assume that the window's buffer is really current. */
13085
13086 static INLINE struct text_pos
13087 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13088 {
13089 struct window *w = XWINDOW (window);
13090 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13091
13092 if (current_buffer != XBUFFER (w->buffer))
13093 abort ();
13094
13095 if (!NILP (Vwindow_scroll_functions))
13096 {
13097 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13098 make_number (CHARPOS (startp)));
13099 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13100 /* In case the hook functions switch buffers. */
13101 if (current_buffer != XBUFFER (w->buffer))
13102 set_buffer_internal_1 (XBUFFER (w->buffer));
13103 }
13104
13105 return startp;
13106 }
13107
13108
13109 /* Make sure the line containing the cursor is fully visible.
13110 A value of 1 means there is nothing to be done.
13111 (Either the line is fully visible, or it cannot be made so,
13112 or we cannot tell.)
13113
13114 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13115 is higher than window.
13116
13117 A value of 0 means the caller should do scrolling
13118 as if point had gone off the screen. */
13119
13120 static int
13121 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13122 {
13123 struct glyph_matrix *matrix;
13124 struct glyph_row *row;
13125 int window_height;
13126
13127 if (!make_cursor_line_fully_visible_p)
13128 return 1;
13129
13130 /* It's not always possible to find the cursor, e.g, when a window
13131 is full of overlay strings. Don't do anything in that case. */
13132 if (w->cursor.vpos < 0)
13133 return 1;
13134
13135 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13136 row = MATRIX_ROW (matrix, w->cursor.vpos);
13137
13138 /* If the cursor row is not partially visible, there's nothing to do. */
13139 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13140 return 1;
13141
13142 /* If the row the cursor is in is taller than the window's height,
13143 it's not clear what to do, so do nothing. */
13144 window_height = window_box_height (w);
13145 if (row->height >= window_height)
13146 {
13147 if (!force_p || MINI_WINDOW_P (w)
13148 || w->vscroll || w->cursor.vpos == 0)
13149 return 1;
13150 }
13151 return 0;
13152 }
13153
13154
13155 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13156 non-zero means only WINDOW is redisplayed in redisplay_internal.
13157 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13158 in redisplay_window to bring a partially visible line into view in
13159 the case that only the cursor has moved.
13160
13161 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13162 last screen line's vertical height extends past the end of the screen.
13163
13164 Value is
13165
13166 1 if scrolling succeeded
13167
13168 0 if scrolling didn't find point.
13169
13170 -1 if new fonts have been loaded so that we must interrupt
13171 redisplay, adjust glyph matrices, and try again. */
13172
13173 enum
13174 {
13175 SCROLLING_SUCCESS,
13176 SCROLLING_FAILED,
13177 SCROLLING_NEED_LARGER_MATRICES
13178 };
13179
13180 static int
13181 try_scrolling (Lisp_Object window, int just_this_one_p,
13182 EMACS_INT scroll_conservatively, EMACS_INT scroll_step,
13183 int temp_scroll_step, int last_line_misfit)
13184 {
13185 struct window *w = XWINDOW (window);
13186 struct frame *f = XFRAME (w->frame);
13187 struct text_pos pos, startp;
13188 struct it it;
13189 int this_scroll_margin, scroll_max, rc, height;
13190 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13191 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13192 Lisp_Object aggressive;
13193 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13194
13195 #if GLYPH_DEBUG
13196 debug_method_add (w, "try_scrolling");
13197 #endif
13198
13199 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13200
13201 /* Compute scroll margin height in pixels. We scroll when point is
13202 within this distance from the top or bottom of the window. */
13203 if (scroll_margin > 0)
13204 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13205 * FRAME_LINE_HEIGHT (f);
13206 else
13207 this_scroll_margin = 0;
13208
13209 /* Force scroll_conservatively to have a reasonable value, to avoid
13210 overflow while computing how much to scroll. Note that the user
13211 can supply scroll-conservatively equal to `most-positive-fixnum',
13212 which can be larger than INT_MAX. */
13213 if (scroll_conservatively > scroll_limit)
13214 {
13215 scroll_conservatively = scroll_limit;
13216 scroll_max = INT_MAX;
13217 }
13218 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13219 /* Compute how much we should try to scroll maximally to bring
13220 point into view. */
13221 scroll_max = (max (scroll_step,
13222 max (scroll_conservatively, temp_scroll_step))
13223 * FRAME_LINE_HEIGHT (f));
13224 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13225 || NUMBERP (current_buffer->scroll_up_aggressively))
13226 /* We're trying to scroll because of aggressive scrolling but no
13227 scroll_step is set. Choose an arbitrary one. */
13228 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13229 else
13230 scroll_max = 0;
13231
13232 too_near_end:
13233
13234 /* Decide whether to scroll down. */
13235 if (PT > CHARPOS (startp))
13236 {
13237 int scroll_margin_y;
13238
13239 /* Compute the pixel ypos of the scroll margin, then move it to
13240 either that ypos or PT, whichever comes first. */
13241 start_display (&it, w, startp);
13242 scroll_margin_y = it.last_visible_y - this_scroll_margin
13243 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13244 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13245 (MOVE_TO_POS | MOVE_TO_Y));
13246
13247 if (PT > CHARPOS (it.current.pos))
13248 {
13249 int y0 = line_bottom_y (&it);
13250 /* Compute how many pixels below window bottom to stop searching
13251 for PT. This avoids costly search for PT that is far away if
13252 the user limited scrolling by a small number of lines, but
13253 always finds PT if scroll_conservatively is set to a large
13254 number, such as most-positive-fixnum. */
13255 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13256 int y_to_move =
13257 slack >= INT_MAX - it.last_visible_y
13258 ? INT_MAX
13259 : it.last_visible_y + slack;
13260
13261 /* Compute the distance from the scroll margin to PT or to
13262 the scroll limit, whichever comes first. This should
13263 include the height of the cursor line, to make that line
13264 fully visible. */
13265 move_it_to (&it, PT, -1, y_to_move,
13266 -1, MOVE_TO_POS | MOVE_TO_Y);
13267 dy = line_bottom_y (&it) - y0;
13268
13269 if (dy > scroll_max)
13270 return SCROLLING_FAILED;
13271
13272 scroll_down_p = 1;
13273 }
13274 }
13275
13276 if (scroll_down_p)
13277 {
13278 /* Point is in or below the bottom scroll margin, so move the
13279 window start down. If scrolling conservatively, move it just
13280 enough down to make point visible. If scroll_step is set,
13281 move it down by scroll_step. */
13282 if (scroll_conservatively)
13283 amount_to_scroll
13284 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13285 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13286 else if (scroll_step || temp_scroll_step)
13287 amount_to_scroll = scroll_max;
13288 else
13289 {
13290 aggressive = current_buffer->scroll_up_aggressively;
13291 height = WINDOW_BOX_TEXT_HEIGHT (w);
13292 if (NUMBERP (aggressive))
13293 {
13294 double float_amount = XFLOATINT (aggressive) * height;
13295 amount_to_scroll = float_amount;
13296 if (amount_to_scroll == 0 && float_amount > 0)
13297 amount_to_scroll = 1;
13298 }
13299 }
13300
13301 if (amount_to_scroll <= 0)
13302 return SCROLLING_FAILED;
13303
13304 start_display (&it, w, startp);
13305 if (scroll_max < INT_MAX)
13306 move_it_vertically (&it, amount_to_scroll);
13307 else
13308 {
13309 /* Extra precision for users who set scroll-conservatively
13310 to most-positive-fixnum: make sure the amount we scroll
13311 the window start is never less than amount_to_scroll,
13312 which was computed as distance from window bottom to
13313 point. This matters when lines at window top and lines
13314 below window bottom have different height. */
13315 struct it it1 = it;
13316 /* We use a temporary it1 because line_bottom_y can modify
13317 its argument, if it moves one line down; see there. */
13318 int start_y = line_bottom_y (&it1);
13319
13320 do {
13321 move_it_by_lines (&it, 1, 1);
13322 it1 = it;
13323 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13324 }
13325
13326 /* If STARTP is unchanged, move it down another screen line. */
13327 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13328 move_it_by_lines (&it, 1, 1);
13329 startp = it.current.pos;
13330 }
13331 else
13332 {
13333 struct text_pos scroll_margin_pos = startp;
13334
13335 /* See if point is inside the scroll margin at the top of the
13336 window. */
13337 if (this_scroll_margin)
13338 {
13339 start_display (&it, w, startp);
13340 move_it_vertically (&it, this_scroll_margin);
13341 scroll_margin_pos = it.current.pos;
13342 }
13343
13344 if (PT < CHARPOS (scroll_margin_pos))
13345 {
13346 /* Point is in the scroll margin at the top of the window or
13347 above what is displayed in the window. */
13348 int y0;
13349
13350 /* Compute the vertical distance from PT to the scroll
13351 margin position. Give up if distance is greater than
13352 scroll_max. */
13353 SET_TEXT_POS (pos, PT, PT_BYTE);
13354 start_display (&it, w, pos);
13355 y0 = it.current_y;
13356 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13357 it.last_visible_y, -1,
13358 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13359 dy = it.current_y - y0;
13360 if (dy > scroll_max)
13361 return SCROLLING_FAILED;
13362
13363 /* Compute new window start. */
13364 start_display (&it, w, startp);
13365
13366 if (scroll_conservatively)
13367 amount_to_scroll
13368 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13369 else if (scroll_step || temp_scroll_step)
13370 amount_to_scroll = scroll_max;
13371 else
13372 {
13373 aggressive = current_buffer->scroll_down_aggressively;
13374 height = WINDOW_BOX_TEXT_HEIGHT (w);
13375 if (NUMBERP (aggressive))
13376 {
13377 double float_amount = XFLOATINT (aggressive) * height;
13378 amount_to_scroll = float_amount;
13379 if (amount_to_scroll == 0 && float_amount > 0)
13380 amount_to_scroll = 1;
13381 }
13382 }
13383
13384 if (amount_to_scroll <= 0)
13385 return SCROLLING_FAILED;
13386
13387 move_it_vertically_backward (&it, amount_to_scroll);
13388 startp = it.current.pos;
13389 }
13390 }
13391
13392 /* Run window scroll functions. */
13393 startp = run_window_scroll_functions (window, startp);
13394
13395 /* Display the window. Give up if new fonts are loaded, or if point
13396 doesn't appear. */
13397 if (!try_window (window, startp, 0))
13398 rc = SCROLLING_NEED_LARGER_MATRICES;
13399 else if (w->cursor.vpos < 0)
13400 {
13401 clear_glyph_matrix (w->desired_matrix);
13402 rc = SCROLLING_FAILED;
13403 }
13404 else
13405 {
13406 /* Maybe forget recorded base line for line number display. */
13407 if (!just_this_one_p
13408 || current_buffer->clip_changed
13409 || BEG_UNCHANGED < CHARPOS (startp))
13410 w->base_line_number = Qnil;
13411
13412 /* If cursor ends up on a partially visible line,
13413 treat that as being off the bottom of the screen. */
13414 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13415 {
13416 clear_glyph_matrix (w->desired_matrix);
13417 ++extra_scroll_margin_lines;
13418 goto too_near_end;
13419 }
13420 rc = SCROLLING_SUCCESS;
13421 }
13422
13423 return rc;
13424 }
13425
13426
13427 /* Compute a suitable window start for window W if display of W starts
13428 on a continuation line. Value is non-zero if a new window start
13429 was computed.
13430
13431 The new window start will be computed, based on W's width, starting
13432 from the start of the continued line. It is the start of the
13433 screen line with the minimum distance from the old start W->start. */
13434
13435 static int
13436 compute_window_start_on_continuation_line (struct window *w)
13437 {
13438 struct text_pos pos, start_pos;
13439 int window_start_changed_p = 0;
13440
13441 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13442
13443 /* If window start is on a continuation line... Window start may be
13444 < BEGV in case there's invisible text at the start of the
13445 buffer (M-x rmail, for example). */
13446 if (CHARPOS (start_pos) > BEGV
13447 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13448 {
13449 struct it it;
13450 struct glyph_row *row;
13451
13452 /* Handle the case that the window start is out of range. */
13453 if (CHARPOS (start_pos) < BEGV)
13454 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13455 else if (CHARPOS (start_pos) > ZV)
13456 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13457
13458 /* Find the start of the continued line. This should be fast
13459 because scan_buffer is fast (newline cache). */
13460 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13461 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13462 row, DEFAULT_FACE_ID);
13463 reseat_at_previous_visible_line_start (&it);
13464
13465 /* If the line start is "too far" away from the window start,
13466 say it takes too much time to compute a new window start. */
13467 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13468 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13469 {
13470 int min_distance, distance;
13471
13472 /* Move forward by display lines to find the new window
13473 start. If window width was enlarged, the new start can
13474 be expected to be > the old start. If window width was
13475 decreased, the new window start will be < the old start.
13476 So, we're looking for the display line start with the
13477 minimum distance from the old window start. */
13478 pos = it.current.pos;
13479 min_distance = INFINITY;
13480 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13481 distance < min_distance)
13482 {
13483 min_distance = distance;
13484 pos = it.current.pos;
13485 move_it_by_lines (&it, 1, 0);
13486 }
13487
13488 /* Set the window start there. */
13489 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13490 window_start_changed_p = 1;
13491 }
13492 }
13493
13494 return window_start_changed_p;
13495 }
13496
13497
13498 /* Try cursor movement in case text has not changed in window WINDOW,
13499 with window start STARTP. Value is
13500
13501 CURSOR_MOVEMENT_SUCCESS if successful
13502
13503 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13504
13505 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13506 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13507 we want to scroll as if scroll-step were set to 1. See the code.
13508
13509 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13510 which case we have to abort this redisplay, and adjust matrices
13511 first. */
13512
13513 enum
13514 {
13515 CURSOR_MOVEMENT_SUCCESS,
13516 CURSOR_MOVEMENT_CANNOT_BE_USED,
13517 CURSOR_MOVEMENT_MUST_SCROLL,
13518 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13519 };
13520
13521 static int
13522 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13523 {
13524 struct window *w = XWINDOW (window);
13525 struct frame *f = XFRAME (w->frame);
13526 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13527
13528 #if GLYPH_DEBUG
13529 if (inhibit_try_cursor_movement)
13530 return rc;
13531 #endif
13532
13533 /* Handle case where text has not changed, only point, and it has
13534 not moved off the frame. */
13535 if (/* Point may be in this window. */
13536 PT >= CHARPOS (startp)
13537 /* Selective display hasn't changed. */
13538 && !current_buffer->clip_changed
13539 /* Function force-mode-line-update is used to force a thorough
13540 redisplay. It sets either windows_or_buffers_changed or
13541 update_mode_lines. So don't take a shortcut here for these
13542 cases. */
13543 && !update_mode_lines
13544 && !windows_or_buffers_changed
13545 && !cursor_type_changed
13546 /* Can't use this case if highlighting a region. When a
13547 region exists, cursor movement has to do more than just
13548 set the cursor. */
13549 && !(!NILP (Vtransient_mark_mode)
13550 && !NILP (current_buffer->mark_active))
13551 && NILP (w->region_showing)
13552 && NILP (Vshow_trailing_whitespace)
13553 /* Right after splitting windows, last_point may be nil. */
13554 && INTEGERP (w->last_point)
13555 /* This code is not used for mini-buffer for the sake of the case
13556 of redisplaying to replace an echo area message; since in
13557 that case the mini-buffer contents per se are usually
13558 unchanged. This code is of no real use in the mini-buffer
13559 since the handling of this_line_start_pos, etc., in redisplay
13560 handles the same cases. */
13561 && !EQ (window, minibuf_window)
13562 /* When splitting windows or for new windows, it happens that
13563 redisplay is called with a nil window_end_vpos or one being
13564 larger than the window. This should really be fixed in
13565 window.c. I don't have this on my list, now, so we do
13566 approximately the same as the old redisplay code. --gerd. */
13567 && INTEGERP (w->window_end_vpos)
13568 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13569 && (FRAME_WINDOW_P (f)
13570 || !overlay_arrow_in_current_buffer_p ()))
13571 {
13572 int this_scroll_margin, top_scroll_margin;
13573 struct glyph_row *row = NULL;
13574
13575 #if GLYPH_DEBUG
13576 debug_method_add (w, "cursor movement");
13577 #endif
13578
13579 /* Scroll if point within this distance from the top or bottom
13580 of the window. This is a pixel value. */
13581 if (scroll_margin > 0)
13582 {
13583 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13584 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13585 }
13586 else
13587 this_scroll_margin = 0;
13588
13589 top_scroll_margin = this_scroll_margin;
13590 if (WINDOW_WANTS_HEADER_LINE_P (w))
13591 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13592
13593 /* Start with the row the cursor was displayed during the last
13594 not paused redisplay. Give up if that row is not valid. */
13595 if (w->last_cursor.vpos < 0
13596 || w->last_cursor.vpos >= w->current_matrix->nrows)
13597 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13598 else
13599 {
13600 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13601 if (row->mode_line_p)
13602 ++row;
13603 if (!row->enabled_p)
13604 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13605 }
13606
13607 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13608 {
13609 int scroll_p = 0, must_scroll = 0;
13610 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13611
13612 if (PT > XFASTINT (w->last_point))
13613 {
13614 /* Point has moved forward. */
13615 while (MATRIX_ROW_END_CHARPOS (row) < PT
13616 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13617 {
13618 xassert (row->enabled_p);
13619 ++row;
13620 }
13621
13622 /* If the end position of a row equals the start
13623 position of the next row, and PT is at that position,
13624 we would rather display cursor in the next line. */
13625 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13626 && MATRIX_ROW_END_CHARPOS (row) == PT
13627 && row < w->current_matrix->rows
13628 + w->current_matrix->nrows - 1
13629 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13630 && !cursor_row_p (w, row))
13631 ++row;
13632
13633 /* If within the scroll margin, scroll. Note that
13634 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13635 the next line would be drawn, and that
13636 this_scroll_margin can be zero. */
13637 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13638 || PT > MATRIX_ROW_END_CHARPOS (row)
13639 /* Line is completely visible last line in window
13640 and PT is to be set in the next line. */
13641 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13642 && PT == MATRIX_ROW_END_CHARPOS (row)
13643 && !row->ends_at_zv_p
13644 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13645 scroll_p = 1;
13646 }
13647 else if (PT < XFASTINT (w->last_point))
13648 {
13649 /* Cursor has to be moved backward. Note that PT >=
13650 CHARPOS (startp) because of the outer if-statement. */
13651 while (!row->mode_line_p
13652 && (MATRIX_ROW_START_CHARPOS (row) > PT
13653 || (MATRIX_ROW_START_CHARPOS (row) == PT
13654 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13655 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13656 row > w->current_matrix->rows
13657 && (row-1)->ends_in_newline_from_string_p))))
13658 && (row->y > top_scroll_margin
13659 || CHARPOS (startp) == BEGV))
13660 {
13661 xassert (row->enabled_p);
13662 --row;
13663 }
13664
13665 /* Consider the following case: Window starts at BEGV,
13666 there is invisible, intangible text at BEGV, so that
13667 display starts at some point START > BEGV. It can
13668 happen that we are called with PT somewhere between
13669 BEGV and START. Try to handle that case. */
13670 if (row < w->current_matrix->rows
13671 || row->mode_line_p)
13672 {
13673 row = w->current_matrix->rows;
13674 if (row->mode_line_p)
13675 ++row;
13676 }
13677
13678 /* Due to newlines in overlay strings, we may have to
13679 skip forward over overlay strings. */
13680 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13681 && MATRIX_ROW_END_CHARPOS (row) == PT
13682 && !cursor_row_p (w, row))
13683 ++row;
13684
13685 /* If within the scroll margin, scroll. */
13686 if (row->y < top_scroll_margin
13687 && CHARPOS (startp) != BEGV)
13688 scroll_p = 1;
13689 }
13690 else
13691 {
13692 /* Cursor did not move. So don't scroll even if cursor line
13693 is partially visible, as it was so before. */
13694 rc = CURSOR_MOVEMENT_SUCCESS;
13695 }
13696
13697 if (PT < MATRIX_ROW_START_CHARPOS (row)
13698 || PT > MATRIX_ROW_END_CHARPOS (row))
13699 {
13700 /* if PT is not in the glyph row, give up. */
13701 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13702 must_scroll = 1;
13703 }
13704 else if (rc != CURSOR_MOVEMENT_SUCCESS
13705 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13706 {
13707 /* If rows are bidi-reordered and point moved, back up
13708 until we find a row that does not belong to a
13709 continuation line. This is because we must consider
13710 all rows of a continued line as candidates for the
13711 new cursor positioning, since row start and end
13712 positions change non-linearly with vertical position
13713 in such rows. */
13714 /* FIXME: Revisit this when glyph ``spilling'' in
13715 continuation lines' rows is implemented for
13716 bidi-reordered rows. */
13717 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13718 {
13719 xassert (row->enabled_p);
13720 --row;
13721 /* If we hit the beginning of the displayed portion
13722 without finding the first row of a continued
13723 line, give up. */
13724 if (row <= w->current_matrix->rows)
13725 {
13726 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13727 break;
13728 }
13729
13730 }
13731 }
13732 if (must_scroll)
13733 ;
13734 else if (rc != CURSOR_MOVEMENT_SUCCESS
13735 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13736 && make_cursor_line_fully_visible_p)
13737 {
13738 if (PT == MATRIX_ROW_END_CHARPOS (row)
13739 && !row->ends_at_zv_p
13740 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13741 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13742 else if (row->height > window_box_height (w))
13743 {
13744 /* If we end up in a partially visible line, let's
13745 make it fully visible, except when it's taller
13746 than the window, in which case we can't do much
13747 about it. */
13748 *scroll_step = 1;
13749 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13750 }
13751 else
13752 {
13753 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13754 if (!cursor_row_fully_visible_p (w, 0, 1))
13755 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13756 else
13757 rc = CURSOR_MOVEMENT_SUCCESS;
13758 }
13759 }
13760 else if (scroll_p)
13761 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13762 else if (rc != CURSOR_MOVEMENT_SUCCESS
13763 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13764 {
13765 /* With bidi-reordered rows, there could be more than
13766 one candidate row whose start and end positions
13767 occlude point. We need to let set_cursor_from_row
13768 find the best candidate. */
13769 /* FIXME: Revisit this when glyph ``spilling'' in
13770 continuation lines' rows is implemented for
13771 bidi-reordered rows. */
13772 int rv = 0;
13773
13774 do
13775 {
13776 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13777 && PT <= MATRIX_ROW_END_CHARPOS (row)
13778 && cursor_row_p (w, row))
13779 rv |= set_cursor_from_row (w, row, w->current_matrix,
13780 0, 0, 0, 0);
13781 /* As soon as we've found the first suitable row
13782 whose ends_at_zv_p flag is set, we are done. */
13783 if (rv
13784 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13785 {
13786 rc = CURSOR_MOVEMENT_SUCCESS;
13787 break;
13788 }
13789 ++row;
13790 }
13791 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
13792 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13793 || (MATRIX_ROW_START_CHARPOS (row) == PT
13794 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
13795 /* If we didn't find any candidate rows, or exited the
13796 loop before all the candidates were examined, signal
13797 to the caller that this method failed. */
13798 if (rc != CURSOR_MOVEMENT_SUCCESS
13799 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
13800 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13801 else if (rv)
13802 rc = CURSOR_MOVEMENT_SUCCESS;
13803 }
13804 else
13805 {
13806 do
13807 {
13808 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13809 {
13810 rc = CURSOR_MOVEMENT_SUCCESS;
13811 break;
13812 }
13813 ++row;
13814 }
13815 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13816 && MATRIX_ROW_START_CHARPOS (row) == PT
13817 && cursor_row_p (w, row));
13818 }
13819 }
13820 }
13821
13822 return rc;
13823 }
13824
13825 void
13826 set_vertical_scroll_bar (struct window *w)
13827 {
13828 EMACS_INT start, end, whole;
13829
13830 /* Calculate the start and end positions for the current window.
13831 At some point, it would be nice to choose between scrollbars
13832 which reflect the whole buffer size, with special markers
13833 indicating narrowing, and scrollbars which reflect only the
13834 visible region.
13835
13836 Note that mini-buffers sometimes aren't displaying any text. */
13837 if (!MINI_WINDOW_P (w)
13838 || (w == XWINDOW (minibuf_window)
13839 && NILP (echo_area_buffer[0])))
13840 {
13841 struct buffer *buf = XBUFFER (w->buffer);
13842 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13843 start = marker_position (w->start) - BUF_BEGV (buf);
13844 /* I don't think this is guaranteed to be right. For the
13845 moment, we'll pretend it is. */
13846 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13847
13848 if (end < start)
13849 end = start;
13850 if (whole < (end - start))
13851 whole = end - start;
13852 }
13853 else
13854 start = end = whole = 0;
13855
13856 /* Indicate what this scroll bar ought to be displaying now. */
13857 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13858 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13859 (w, end - start, whole, start);
13860 }
13861
13862
13863 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13864 selected_window is redisplayed.
13865
13866 We can return without actually redisplaying the window if
13867 fonts_changed_p is nonzero. In that case, redisplay_internal will
13868 retry. */
13869
13870 static void
13871 redisplay_window (Lisp_Object window, int just_this_one_p)
13872 {
13873 struct window *w = XWINDOW (window);
13874 struct frame *f = XFRAME (w->frame);
13875 struct buffer *buffer = XBUFFER (w->buffer);
13876 struct buffer *old = current_buffer;
13877 struct text_pos lpoint, opoint, startp;
13878 int update_mode_line;
13879 int tem;
13880 struct it it;
13881 /* Record it now because it's overwritten. */
13882 int current_matrix_up_to_date_p = 0;
13883 int used_current_matrix_p = 0;
13884 /* This is less strict than current_matrix_up_to_date_p.
13885 It indictes that the buffer contents and narrowing are unchanged. */
13886 int buffer_unchanged_p = 0;
13887 int temp_scroll_step = 0;
13888 int count = SPECPDL_INDEX ();
13889 int rc;
13890 int centering_position = -1;
13891 int last_line_misfit = 0;
13892 EMACS_INT beg_unchanged, end_unchanged;
13893
13894 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13895 opoint = lpoint;
13896
13897 /* W must be a leaf window here. */
13898 xassert (!NILP (w->buffer));
13899 #if GLYPH_DEBUG
13900 *w->desired_matrix->method = 0;
13901 #endif
13902
13903 restart:
13904 reconsider_clip_changes (w, buffer);
13905
13906 /* Has the mode line to be updated? */
13907 update_mode_line = (!NILP (w->update_mode_line)
13908 || update_mode_lines
13909 || buffer->clip_changed
13910 || buffer->prevent_redisplay_optimizations_p);
13911
13912 if (MINI_WINDOW_P (w))
13913 {
13914 if (w == XWINDOW (echo_area_window)
13915 && !NILP (echo_area_buffer[0]))
13916 {
13917 if (update_mode_line)
13918 /* We may have to update a tty frame's menu bar or a
13919 tool-bar. Example `M-x C-h C-h C-g'. */
13920 goto finish_menu_bars;
13921 else
13922 /* We've already displayed the echo area glyphs in this window. */
13923 goto finish_scroll_bars;
13924 }
13925 else if ((w != XWINDOW (minibuf_window)
13926 || minibuf_level == 0)
13927 /* When buffer is nonempty, redisplay window normally. */
13928 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13929 /* Quail displays non-mini buffers in minibuffer window.
13930 In that case, redisplay the window normally. */
13931 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13932 {
13933 /* W is a mini-buffer window, but it's not active, so clear
13934 it. */
13935 int yb = window_text_bottom_y (w);
13936 struct glyph_row *row;
13937 int y;
13938
13939 for (y = 0, row = w->desired_matrix->rows;
13940 y < yb;
13941 y += row->height, ++row)
13942 blank_row (w, row, y);
13943 goto finish_scroll_bars;
13944 }
13945
13946 clear_glyph_matrix (w->desired_matrix);
13947 }
13948
13949 /* Otherwise set up data on this window; select its buffer and point
13950 value. */
13951 /* Really select the buffer, for the sake of buffer-local
13952 variables. */
13953 set_buffer_internal_1 (XBUFFER (w->buffer));
13954
13955 current_matrix_up_to_date_p
13956 = (!NILP (w->window_end_valid)
13957 && !current_buffer->clip_changed
13958 && !current_buffer->prevent_redisplay_optimizations_p
13959 && XFASTINT (w->last_modified) >= MODIFF
13960 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13961
13962 /* Run the window-bottom-change-functions
13963 if it is possible that the text on the screen has changed
13964 (either due to modification of the text, or any other reason). */
13965 if (!current_matrix_up_to_date_p
13966 && !NILP (Vwindow_text_change_functions))
13967 {
13968 safe_run_hooks (Qwindow_text_change_functions);
13969 goto restart;
13970 }
13971
13972 beg_unchanged = BEG_UNCHANGED;
13973 end_unchanged = END_UNCHANGED;
13974
13975 SET_TEXT_POS (opoint, PT, PT_BYTE);
13976
13977 specbind (Qinhibit_point_motion_hooks, Qt);
13978
13979 buffer_unchanged_p
13980 = (!NILP (w->window_end_valid)
13981 && !current_buffer->clip_changed
13982 && XFASTINT (w->last_modified) >= MODIFF
13983 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13984
13985 /* When windows_or_buffers_changed is non-zero, we can't rely on
13986 the window end being valid, so set it to nil there. */
13987 if (windows_or_buffers_changed)
13988 {
13989 /* If window starts on a continuation line, maybe adjust the
13990 window start in case the window's width changed. */
13991 if (XMARKER (w->start)->buffer == current_buffer)
13992 compute_window_start_on_continuation_line (w);
13993
13994 w->window_end_valid = Qnil;
13995 }
13996
13997 /* Some sanity checks. */
13998 CHECK_WINDOW_END (w);
13999 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14000 abort ();
14001 if (BYTEPOS (opoint) < CHARPOS (opoint))
14002 abort ();
14003
14004 /* If %c is in mode line, update it if needed. */
14005 if (!NILP (w->column_number_displayed)
14006 /* This alternative quickly identifies a common case
14007 where no change is needed. */
14008 && !(PT == XFASTINT (w->last_point)
14009 && XFASTINT (w->last_modified) >= MODIFF
14010 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14011 && (XFASTINT (w->column_number_displayed)
14012 != (int) current_column ())) /* iftc */
14013 update_mode_line = 1;
14014
14015 /* Count number of windows showing the selected buffer. An indirect
14016 buffer counts as its base buffer. */
14017 if (!just_this_one_p)
14018 {
14019 struct buffer *current_base, *window_base;
14020 current_base = current_buffer;
14021 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14022 if (current_base->base_buffer)
14023 current_base = current_base->base_buffer;
14024 if (window_base->base_buffer)
14025 window_base = window_base->base_buffer;
14026 if (current_base == window_base)
14027 buffer_shared++;
14028 }
14029
14030 /* Point refers normally to the selected window. For any other
14031 window, set up appropriate value. */
14032 if (!EQ (window, selected_window))
14033 {
14034 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14035 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14036 if (new_pt < BEGV)
14037 {
14038 new_pt = BEGV;
14039 new_pt_byte = BEGV_BYTE;
14040 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14041 }
14042 else if (new_pt > (ZV - 1))
14043 {
14044 new_pt = ZV;
14045 new_pt_byte = ZV_BYTE;
14046 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14047 }
14048
14049 /* We don't use SET_PT so that the point-motion hooks don't run. */
14050 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14051 }
14052
14053 /* If any of the character widths specified in the display table
14054 have changed, invalidate the width run cache. It's true that
14055 this may be a bit late to catch such changes, but the rest of
14056 redisplay goes (non-fatally) haywire when the display table is
14057 changed, so why should we worry about doing any better? */
14058 if (current_buffer->width_run_cache)
14059 {
14060 struct Lisp_Char_Table *disptab = buffer_display_table ();
14061
14062 if (! disptab_matches_widthtab (disptab,
14063 XVECTOR (current_buffer->width_table)))
14064 {
14065 invalidate_region_cache (current_buffer,
14066 current_buffer->width_run_cache,
14067 BEG, Z);
14068 recompute_width_table (current_buffer, disptab);
14069 }
14070 }
14071
14072 /* If window-start is screwed up, choose a new one. */
14073 if (XMARKER (w->start)->buffer != current_buffer)
14074 goto recenter;
14075
14076 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14077
14078 /* If someone specified a new starting point but did not insist,
14079 check whether it can be used. */
14080 if (!NILP (w->optional_new_start)
14081 && CHARPOS (startp) >= BEGV
14082 && CHARPOS (startp) <= ZV)
14083 {
14084 w->optional_new_start = Qnil;
14085 start_display (&it, w, startp);
14086 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14087 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14088 if (IT_CHARPOS (it) == PT)
14089 w->force_start = Qt;
14090 /* IT may overshoot PT if text at PT is invisible. */
14091 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14092 w->force_start = Qt;
14093 }
14094
14095 force_start:
14096
14097 /* Handle case where place to start displaying has been specified,
14098 unless the specified location is outside the accessible range. */
14099 if (!NILP (w->force_start)
14100 || w->frozen_window_start_p)
14101 {
14102 /* We set this later on if we have to adjust point. */
14103 int new_vpos = -1;
14104
14105 w->force_start = Qnil;
14106 w->vscroll = 0;
14107 w->window_end_valid = Qnil;
14108
14109 /* Forget any recorded base line for line number display. */
14110 if (!buffer_unchanged_p)
14111 w->base_line_number = Qnil;
14112
14113 /* Redisplay the mode line. Select the buffer properly for that.
14114 Also, run the hook window-scroll-functions
14115 because we have scrolled. */
14116 /* Note, we do this after clearing force_start because
14117 if there's an error, it is better to forget about force_start
14118 than to get into an infinite loop calling the hook functions
14119 and having them get more errors. */
14120 if (!update_mode_line
14121 || ! NILP (Vwindow_scroll_functions))
14122 {
14123 update_mode_line = 1;
14124 w->update_mode_line = Qt;
14125 startp = run_window_scroll_functions (window, startp);
14126 }
14127
14128 w->last_modified = make_number (0);
14129 w->last_overlay_modified = make_number (0);
14130 if (CHARPOS (startp) < BEGV)
14131 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14132 else if (CHARPOS (startp) > ZV)
14133 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14134
14135 /* Redisplay, then check if cursor has been set during the
14136 redisplay. Give up if new fonts were loaded. */
14137 /* We used to issue a CHECK_MARGINS argument to try_window here,
14138 but this causes scrolling to fail when point begins inside
14139 the scroll margin (bug#148) -- cyd */
14140 if (!try_window (window, startp, 0))
14141 {
14142 w->force_start = Qt;
14143 clear_glyph_matrix (w->desired_matrix);
14144 goto need_larger_matrices;
14145 }
14146
14147 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14148 {
14149 /* If point does not appear, try to move point so it does
14150 appear. The desired matrix has been built above, so we
14151 can use it here. */
14152 new_vpos = window_box_height (w) / 2;
14153 }
14154
14155 if (!cursor_row_fully_visible_p (w, 0, 0))
14156 {
14157 /* Point does appear, but on a line partly visible at end of window.
14158 Move it back to a fully-visible line. */
14159 new_vpos = window_box_height (w);
14160 }
14161
14162 /* If we need to move point for either of the above reasons,
14163 now actually do it. */
14164 if (new_vpos >= 0)
14165 {
14166 struct glyph_row *row;
14167
14168 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14169 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14170 ++row;
14171
14172 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14173 MATRIX_ROW_START_BYTEPOS (row));
14174
14175 if (w != XWINDOW (selected_window))
14176 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14177 else if (current_buffer == old)
14178 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14179
14180 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14181
14182 /* If we are highlighting the region, then we just changed
14183 the region, so redisplay to show it. */
14184 if (!NILP (Vtransient_mark_mode)
14185 && !NILP (current_buffer->mark_active))
14186 {
14187 clear_glyph_matrix (w->desired_matrix);
14188 if (!try_window (window, startp, 0))
14189 goto need_larger_matrices;
14190 }
14191 }
14192
14193 #if GLYPH_DEBUG
14194 debug_method_add (w, "forced window start");
14195 #endif
14196 goto done;
14197 }
14198
14199 /* Handle case where text has not changed, only point, and it has
14200 not moved off the frame, and we are not retrying after hscroll.
14201 (current_matrix_up_to_date_p is nonzero when retrying.) */
14202 if (current_matrix_up_to_date_p
14203 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14204 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14205 {
14206 switch (rc)
14207 {
14208 case CURSOR_MOVEMENT_SUCCESS:
14209 used_current_matrix_p = 1;
14210 goto done;
14211
14212 case CURSOR_MOVEMENT_MUST_SCROLL:
14213 goto try_to_scroll;
14214
14215 default:
14216 abort ();
14217 }
14218 }
14219 /* If current starting point was originally the beginning of a line
14220 but no longer is, find a new starting point. */
14221 else if (!NILP (w->start_at_line_beg)
14222 && !(CHARPOS (startp) <= BEGV
14223 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14224 {
14225 #if GLYPH_DEBUG
14226 debug_method_add (w, "recenter 1");
14227 #endif
14228 goto recenter;
14229 }
14230
14231 /* Try scrolling with try_window_id. Value is > 0 if update has
14232 been done, it is -1 if we know that the same window start will
14233 not work. It is 0 if unsuccessful for some other reason. */
14234 else if ((tem = try_window_id (w)) != 0)
14235 {
14236 #if GLYPH_DEBUG
14237 debug_method_add (w, "try_window_id %d", tem);
14238 #endif
14239
14240 if (fonts_changed_p)
14241 goto need_larger_matrices;
14242 if (tem > 0)
14243 goto done;
14244
14245 /* Otherwise try_window_id has returned -1 which means that we
14246 don't want the alternative below this comment to execute. */
14247 }
14248 else if (CHARPOS (startp) >= BEGV
14249 && CHARPOS (startp) <= ZV
14250 && PT >= CHARPOS (startp)
14251 && (CHARPOS (startp) < ZV
14252 /* Avoid starting at end of buffer. */
14253 || CHARPOS (startp) == BEGV
14254 || (XFASTINT (w->last_modified) >= MODIFF
14255 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14256 {
14257
14258 /* If first window line is a continuation line, and window start
14259 is inside the modified region, but the first change is before
14260 current window start, we must select a new window start.
14261
14262 However, if this is the result of a down-mouse event (e.g. by
14263 extending the mouse-drag-overlay), we don't want to select a
14264 new window start, since that would change the position under
14265 the mouse, resulting in an unwanted mouse-movement rather
14266 than a simple mouse-click. */
14267 if (NILP (w->start_at_line_beg)
14268 && NILP (do_mouse_tracking)
14269 && CHARPOS (startp) > BEGV
14270 && CHARPOS (startp) > BEG + beg_unchanged
14271 && CHARPOS (startp) <= Z - end_unchanged
14272 /* Even if w->start_at_line_beg is nil, a new window may
14273 start at a line_beg, since that's how set_buffer_window
14274 sets it. So, we need to check the return value of
14275 compute_window_start_on_continuation_line. (See also
14276 bug#197). */
14277 && XMARKER (w->start)->buffer == current_buffer
14278 && compute_window_start_on_continuation_line (w))
14279 {
14280 w->force_start = Qt;
14281 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14282 goto force_start;
14283 }
14284
14285 #if GLYPH_DEBUG
14286 debug_method_add (w, "same window start");
14287 #endif
14288
14289 /* Try to redisplay starting at same place as before.
14290 If point has not moved off frame, accept the results. */
14291 if (!current_matrix_up_to_date_p
14292 /* Don't use try_window_reusing_current_matrix in this case
14293 because a window scroll function can have changed the
14294 buffer. */
14295 || !NILP (Vwindow_scroll_functions)
14296 || MINI_WINDOW_P (w)
14297 || !(used_current_matrix_p
14298 = try_window_reusing_current_matrix (w)))
14299 {
14300 IF_DEBUG (debug_method_add (w, "1"));
14301 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14302 /* -1 means we need to scroll.
14303 0 means we need new matrices, but fonts_changed_p
14304 is set in that case, so we will detect it below. */
14305 goto try_to_scroll;
14306 }
14307
14308 if (fonts_changed_p)
14309 goto need_larger_matrices;
14310
14311 if (w->cursor.vpos >= 0)
14312 {
14313 if (!just_this_one_p
14314 || current_buffer->clip_changed
14315 || BEG_UNCHANGED < CHARPOS (startp))
14316 /* Forget any recorded base line for line number display. */
14317 w->base_line_number = Qnil;
14318
14319 if (!cursor_row_fully_visible_p (w, 1, 0))
14320 {
14321 clear_glyph_matrix (w->desired_matrix);
14322 last_line_misfit = 1;
14323 }
14324 /* Drop through and scroll. */
14325 else
14326 goto done;
14327 }
14328 else
14329 clear_glyph_matrix (w->desired_matrix);
14330 }
14331
14332 try_to_scroll:
14333
14334 w->last_modified = make_number (0);
14335 w->last_overlay_modified = make_number (0);
14336
14337 /* Redisplay the mode line. Select the buffer properly for that. */
14338 if (!update_mode_line)
14339 {
14340 update_mode_line = 1;
14341 w->update_mode_line = Qt;
14342 }
14343
14344 /* Try to scroll by specified few lines. */
14345 if ((scroll_conservatively
14346 || scroll_step
14347 || temp_scroll_step
14348 || NUMBERP (current_buffer->scroll_up_aggressively)
14349 || NUMBERP (current_buffer->scroll_down_aggressively))
14350 && !current_buffer->clip_changed
14351 && CHARPOS (startp) >= BEGV
14352 && CHARPOS (startp) <= ZV)
14353 {
14354 /* The function returns -1 if new fonts were loaded, 1 if
14355 successful, 0 if not successful. */
14356 int rc = try_scrolling (window, just_this_one_p,
14357 scroll_conservatively,
14358 scroll_step,
14359 temp_scroll_step, last_line_misfit);
14360 switch (rc)
14361 {
14362 case SCROLLING_SUCCESS:
14363 goto done;
14364
14365 case SCROLLING_NEED_LARGER_MATRICES:
14366 goto need_larger_matrices;
14367
14368 case SCROLLING_FAILED:
14369 break;
14370
14371 default:
14372 abort ();
14373 }
14374 }
14375
14376 /* Finally, just choose place to start which centers point */
14377
14378 recenter:
14379 if (centering_position < 0)
14380 centering_position = window_box_height (w) / 2;
14381
14382 #if GLYPH_DEBUG
14383 debug_method_add (w, "recenter");
14384 #endif
14385
14386 /* w->vscroll = 0; */
14387
14388 /* Forget any previously recorded base line for line number display. */
14389 if (!buffer_unchanged_p)
14390 w->base_line_number = Qnil;
14391
14392 /* Move backward half the height of the window. */
14393 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14394 it.current_y = it.last_visible_y;
14395 move_it_vertically_backward (&it, centering_position);
14396 xassert (IT_CHARPOS (it) >= BEGV);
14397
14398 /* The function move_it_vertically_backward may move over more
14399 than the specified y-distance. If it->w is small, e.g. a
14400 mini-buffer window, we may end up in front of the window's
14401 display area. Start displaying at the start of the line
14402 containing PT in this case. */
14403 if (it.current_y <= 0)
14404 {
14405 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14406 move_it_vertically_backward (&it, 0);
14407 it.current_y = 0;
14408 }
14409
14410 it.current_x = it.hpos = 0;
14411
14412 /* Set startp here explicitly in case that helps avoid an infinite loop
14413 in case the window-scroll-functions functions get errors. */
14414 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14415
14416 /* Run scroll hooks. */
14417 startp = run_window_scroll_functions (window, it.current.pos);
14418
14419 /* Redisplay the window. */
14420 if (!current_matrix_up_to_date_p
14421 || windows_or_buffers_changed
14422 || cursor_type_changed
14423 /* Don't use try_window_reusing_current_matrix in this case
14424 because it can have changed the buffer. */
14425 || !NILP (Vwindow_scroll_functions)
14426 || !just_this_one_p
14427 || MINI_WINDOW_P (w)
14428 || !(used_current_matrix_p
14429 = try_window_reusing_current_matrix (w)))
14430 try_window (window, startp, 0);
14431
14432 /* If new fonts have been loaded (due to fontsets), give up. We
14433 have to start a new redisplay since we need to re-adjust glyph
14434 matrices. */
14435 if (fonts_changed_p)
14436 goto need_larger_matrices;
14437
14438 /* If cursor did not appear assume that the middle of the window is
14439 in the first line of the window. Do it again with the next line.
14440 (Imagine a window of height 100, displaying two lines of height
14441 60. Moving back 50 from it->last_visible_y will end in the first
14442 line.) */
14443 if (w->cursor.vpos < 0)
14444 {
14445 if (!NILP (w->window_end_valid)
14446 && PT >= Z - XFASTINT (w->window_end_pos))
14447 {
14448 clear_glyph_matrix (w->desired_matrix);
14449 move_it_by_lines (&it, 1, 0);
14450 try_window (window, it.current.pos, 0);
14451 }
14452 else if (PT < IT_CHARPOS (it))
14453 {
14454 clear_glyph_matrix (w->desired_matrix);
14455 move_it_by_lines (&it, -1, 0);
14456 try_window (window, it.current.pos, 0);
14457 }
14458 else
14459 {
14460 /* Not much we can do about it. */
14461 }
14462 }
14463
14464 /* Consider the following case: Window starts at BEGV, there is
14465 invisible, intangible text at BEGV, so that display starts at
14466 some point START > BEGV. It can happen that we are called with
14467 PT somewhere between BEGV and START. Try to handle that case. */
14468 if (w->cursor.vpos < 0)
14469 {
14470 struct glyph_row *row = w->current_matrix->rows;
14471 if (row->mode_line_p)
14472 ++row;
14473 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14474 }
14475
14476 if (!cursor_row_fully_visible_p (w, 0, 0))
14477 {
14478 /* If vscroll is enabled, disable it and try again. */
14479 if (w->vscroll)
14480 {
14481 w->vscroll = 0;
14482 clear_glyph_matrix (w->desired_matrix);
14483 goto recenter;
14484 }
14485
14486 /* If centering point failed to make the whole line visible,
14487 put point at the top instead. That has to make the whole line
14488 visible, if it can be done. */
14489 if (centering_position == 0)
14490 goto done;
14491
14492 clear_glyph_matrix (w->desired_matrix);
14493 centering_position = 0;
14494 goto recenter;
14495 }
14496
14497 done:
14498
14499 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14500 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14501 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14502 ? Qt : Qnil);
14503
14504 /* Display the mode line, if we must. */
14505 if ((update_mode_line
14506 /* If window not full width, must redo its mode line
14507 if (a) the window to its side is being redone and
14508 (b) we do a frame-based redisplay. This is a consequence
14509 of how inverted lines are drawn in frame-based redisplay. */
14510 || (!just_this_one_p
14511 && !FRAME_WINDOW_P (f)
14512 && !WINDOW_FULL_WIDTH_P (w))
14513 /* Line number to display. */
14514 || INTEGERP (w->base_line_pos)
14515 /* Column number is displayed and different from the one displayed. */
14516 || (!NILP (w->column_number_displayed)
14517 && (XFASTINT (w->column_number_displayed)
14518 != (int) current_column ()))) /* iftc */
14519 /* This means that the window has a mode line. */
14520 && (WINDOW_WANTS_MODELINE_P (w)
14521 || WINDOW_WANTS_HEADER_LINE_P (w)))
14522 {
14523 display_mode_lines (w);
14524
14525 /* If mode line height has changed, arrange for a thorough
14526 immediate redisplay using the correct mode line height. */
14527 if (WINDOW_WANTS_MODELINE_P (w)
14528 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14529 {
14530 fonts_changed_p = 1;
14531 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14532 = DESIRED_MODE_LINE_HEIGHT (w);
14533 }
14534
14535 /* If header line height has changed, arrange for a thorough
14536 immediate redisplay using the correct header line height. */
14537 if (WINDOW_WANTS_HEADER_LINE_P (w)
14538 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14539 {
14540 fonts_changed_p = 1;
14541 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14542 = DESIRED_HEADER_LINE_HEIGHT (w);
14543 }
14544
14545 if (fonts_changed_p)
14546 goto need_larger_matrices;
14547 }
14548
14549 if (!line_number_displayed
14550 && !BUFFERP (w->base_line_pos))
14551 {
14552 w->base_line_pos = Qnil;
14553 w->base_line_number = Qnil;
14554 }
14555
14556 finish_menu_bars:
14557
14558 /* When we reach a frame's selected window, redo the frame's menu bar. */
14559 if (update_mode_line
14560 && EQ (FRAME_SELECTED_WINDOW (f), window))
14561 {
14562 int redisplay_menu_p = 0;
14563 int redisplay_tool_bar_p = 0;
14564
14565 if (FRAME_WINDOW_P (f))
14566 {
14567 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14568 || defined (HAVE_NS) || defined (USE_GTK)
14569 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14570 #else
14571 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14572 #endif
14573 }
14574 else
14575 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14576
14577 if (redisplay_menu_p)
14578 display_menu_bar (w);
14579
14580 #ifdef HAVE_WINDOW_SYSTEM
14581 if (FRAME_WINDOW_P (f))
14582 {
14583 #if defined (USE_GTK) || defined (HAVE_NS)
14584 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14585 #else
14586 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14587 && (FRAME_TOOL_BAR_LINES (f) > 0
14588 || !NILP (Vauto_resize_tool_bars));
14589 #endif
14590
14591 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14592 {
14593 ignore_mouse_drag_p = 1;
14594 }
14595 }
14596 #endif
14597 }
14598
14599 #ifdef HAVE_WINDOW_SYSTEM
14600 if (FRAME_WINDOW_P (f)
14601 && update_window_fringes (w, (just_this_one_p
14602 || (!used_current_matrix_p && !overlay_arrow_seen)
14603 || w->pseudo_window_p)))
14604 {
14605 update_begin (f);
14606 BLOCK_INPUT;
14607 if (draw_window_fringes (w, 1))
14608 x_draw_vertical_border (w);
14609 UNBLOCK_INPUT;
14610 update_end (f);
14611 }
14612 #endif /* HAVE_WINDOW_SYSTEM */
14613
14614 /* We go to this label, with fonts_changed_p nonzero,
14615 if it is necessary to try again using larger glyph matrices.
14616 We have to redeem the scroll bar even in this case,
14617 because the loop in redisplay_internal expects that. */
14618 need_larger_matrices:
14619 ;
14620 finish_scroll_bars:
14621
14622 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14623 {
14624 /* Set the thumb's position and size. */
14625 set_vertical_scroll_bar (w);
14626
14627 /* Note that we actually used the scroll bar attached to this
14628 window, so it shouldn't be deleted at the end of redisplay. */
14629 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14630 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14631 }
14632
14633 /* Restore current_buffer and value of point in it. The window
14634 update may have changed the buffer, so first make sure `opoint'
14635 is still valid (Bug#6177). */
14636 if (CHARPOS (opoint) < BEGV)
14637 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14638 else if (CHARPOS (opoint) > ZV)
14639 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14640 else
14641 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14642
14643 set_buffer_internal_1 (old);
14644 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14645 shorter. This can be caused by log truncation in *Messages*. */
14646 if (CHARPOS (lpoint) <= ZV)
14647 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14648
14649 unbind_to (count, Qnil);
14650 }
14651
14652
14653 /* Build the complete desired matrix of WINDOW with a window start
14654 buffer position POS.
14655
14656 Value is 1 if successful. It is zero if fonts were loaded during
14657 redisplay which makes re-adjusting glyph matrices necessary, and -1
14658 if point would appear in the scroll margins.
14659 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14660 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14661 set in FLAGS.) */
14662
14663 int
14664 try_window (Lisp_Object window, struct text_pos pos, int flags)
14665 {
14666 struct window *w = XWINDOW (window);
14667 struct it it;
14668 struct glyph_row *last_text_row = NULL;
14669 struct frame *f = XFRAME (w->frame);
14670
14671 /* Make POS the new window start. */
14672 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14673
14674 /* Mark cursor position as unknown. No overlay arrow seen. */
14675 w->cursor.vpos = -1;
14676 overlay_arrow_seen = 0;
14677
14678 /* Initialize iterator and info to start at POS. */
14679 start_display (&it, w, pos);
14680
14681 /* Display all lines of W. */
14682 while (it.current_y < it.last_visible_y)
14683 {
14684 if (display_line (&it))
14685 last_text_row = it.glyph_row - 1;
14686 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14687 return 0;
14688 }
14689
14690 /* Don't let the cursor end in the scroll margins. */
14691 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14692 && !MINI_WINDOW_P (w))
14693 {
14694 int this_scroll_margin;
14695
14696 if (scroll_margin > 0)
14697 {
14698 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14699 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14700 }
14701 else
14702 this_scroll_margin = 0;
14703
14704 if ((w->cursor.y >= 0 /* not vscrolled */
14705 && w->cursor.y < this_scroll_margin
14706 && CHARPOS (pos) > BEGV
14707 && IT_CHARPOS (it) < ZV)
14708 /* rms: considering make_cursor_line_fully_visible_p here
14709 seems to give wrong results. We don't want to recenter
14710 when the last line is partly visible, we want to allow
14711 that case to be handled in the usual way. */
14712 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14713 {
14714 w->cursor.vpos = -1;
14715 clear_glyph_matrix (w->desired_matrix);
14716 return -1;
14717 }
14718 }
14719
14720 /* If bottom moved off end of frame, change mode line percentage. */
14721 if (XFASTINT (w->window_end_pos) <= 0
14722 && Z != IT_CHARPOS (it))
14723 w->update_mode_line = Qt;
14724
14725 /* Set window_end_pos to the offset of the last character displayed
14726 on the window from the end of current_buffer. Set
14727 window_end_vpos to its row number. */
14728 if (last_text_row)
14729 {
14730 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14731 w->window_end_bytepos
14732 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14733 w->window_end_pos
14734 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14735 w->window_end_vpos
14736 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14737 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14738 ->displays_text_p);
14739 }
14740 else
14741 {
14742 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14743 w->window_end_pos = make_number (Z - ZV);
14744 w->window_end_vpos = make_number (0);
14745 }
14746
14747 /* But that is not valid info until redisplay finishes. */
14748 w->window_end_valid = Qnil;
14749 return 1;
14750 }
14751
14752
14753 \f
14754 /************************************************************************
14755 Window redisplay reusing current matrix when buffer has not changed
14756 ************************************************************************/
14757
14758 /* Try redisplay of window W showing an unchanged buffer with a
14759 different window start than the last time it was displayed by
14760 reusing its current matrix. Value is non-zero if successful.
14761 W->start is the new window start. */
14762
14763 static int
14764 try_window_reusing_current_matrix (struct window *w)
14765 {
14766 struct frame *f = XFRAME (w->frame);
14767 struct glyph_row *row, *bottom_row;
14768 struct it it;
14769 struct run run;
14770 struct text_pos start, new_start;
14771 int nrows_scrolled, i;
14772 struct glyph_row *last_text_row;
14773 struct glyph_row *last_reused_text_row;
14774 struct glyph_row *start_row;
14775 int start_vpos, min_y, max_y;
14776
14777 #if GLYPH_DEBUG
14778 if (inhibit_try_window_reusing)
14779 return 0;
14780 #endif
14781
14782 if (/* This function doesn't handle terminal frames. */
14783 !FRAME_WINDOW_P (f)
14784 /* Don't try to reuse the display if windows have been split
14785 or such. */
14786 || windows_or_buffers_changed
14787 || cursor_type_changed)
14788 return 0;
14789
14790 /* Can't do this if region may have changed. */
14791 if ((!NILP (Vtransient_mark_mode)
14792 && !NILP (current_buffer->mark_active))
14793 || !NILP (w->region_showing)
14794 || !NILP (Vshow_trailing_whitespace))
14795 return 0;
14796
14797 /* If top-line visibility has changed, give up. */
14798 if (WINDOW_WANTS_HEADER_LINE_P (w)
14799 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14800 return 0;
14801
14802 /* Give up if old or new display is scrolled vertically. We could
14803 make this function handle this, but right now it doesn't. */
14804 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14805 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14806 return 0;
14807
14808 /* The variable new_start now holds the new window start. The old
14809 start `start' can be determined from the current matrix. */
14810 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14811 start = start_row->minpos;
14812 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14813
14814 /* Clear the desired matrix for the display below. */
14815 clear_glyph_matrix (w->desired_matrix);
14816
14817 if (CHARPOS (new_start) <= CHARPOS (start))
14818 {
14819 int first_row_y;
14820
14821 /* Don't use this method if the display starts with an ellipsis
14822 displayed for invisible text. It's not easy to handle that case
14823 below, and it's certainly not worth the effort since this is
14824 not a frequent case. */
14825 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14826 return 0;
14827
14828 IF_DEBUG (debug_method_add (w, "twu1"));
14829
14830 /* Display up to a row that can be reused. The variable
14831 last_text_row is set to the last row displayed that displays
14832 text. Note that it.vpos == 0 if or if not there is a
14833 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14834 start_display (&it, w, new_start);
14835 first_row_y = it.current_y;
14836 w->cursor.vpos = -1;
14837 last_text_row = last_reused_text_row = NULL;
14838
14839 while (it.current_y < it.last_visible_y
14840 && !fonts_changed_p)
14841 {
14842 /* If we have reached into the characters in the START row,
14843 that means the line boundaries have changed. So we
14844 can't start copying with the row START. Maybe it will
14845 work to start copying with the following row. */
14846 while (IT_CHARPOS (it) > CHARPOS (start))
14847 {
14848 /* Advance to the next row as the "start". */
14849 start_row++;
14850 start = start_row->minpos;
14851 /* If there are no more rows to try, or just one, give up. */
14852 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14853 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14854 || CHARPOS (start) == ZV)
14855 {
14856 clear_glyph_matrix (w->desired_matrix);
14857 return 0;
14858 }
14859
14860 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14861 }
14862 /* If we have reached alignment,
14863 we can copy the rest of the rows. */
14864 if (IT_CHARPOS (it) == CHARPOS (start))
14865 break;
14866
14867 if (display_line (&it))
14868 last_text_row = it.glyph_row - 1;
14869 }
14870
14871 /* A value of current_y < last_visible_y means that we stopped
14872 at the previous window start, which in turn means that we
14873 have at least one reusable row. */
14874 if (it.current_y < it.last_visible_y)
14875 {
14876 /* IT.vpos always starts from 0; it counts text lines. */
14877 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14878
14879 /* Find PT if not already found in the lines displayed. */
14880 if (w->cursor.vpos < 0)
14881 {
14882 int dy = it.current_y - start_row->y;
14883
14884 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14885 row = row_containing_pos (w, PT, row, NULL, dy);
14886 if (row)
14887 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14888 dy, nrows_scrolled);
14889 else
14890 {
14891 clear_glyph_matrix (w->desired_matrix);
14892 return 0;
14893 }
14894 }
14895
14896 /* Scroll the display. Do it before the current matrix is
14897 changed. The problem here is that update has not yet
14898 run, i.e. part of the current matrix is not up to date.
14899 scroll_run_hook will clear the cursor, and use the
14900 current matrix to get the height of the row the cursor is
14901 in. */
14902 run.current_y = start_row->y;
14903 run.desired_y = it.current_y;
14904 run.height = it.last_visible_y - it.current_y;
14905
14906 if (run.height > 0 && run.current_y != run.desired_y)
14907 {
14908 update_begin (f);
14909 FRAME_RIF (f)->update_window_begin_hook (w);
14910 FRAME_RIF (f)->clear_window_mouse_face (w);
14911 FRAME_RIF (f)->scroll_run_hook (w, &run);
14912 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14913 update_end (f);
14914 }
14915
14916 /* Shift current matrix down by nrows_scrolled lines. */
14917 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14918 rotate_matrix (w->current_matrix,
14919 start_vpos,
14920 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14921 nrows_scrolled);
14922
14923 /* Disable lines that must be updated. */
14924 for (i = 0; i < nrows_scrolled; ++i)
14925 (start_row + i)->enabled_p = 0;
14926
14927 /* Re-compute Y positions. */
14928 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14929 max_y = it.last_visible_y;
14930 for (row = start_row + nrows_scrolled;
14931 row < bottom_row;
14932 ++row)
14933 {
14934 row->y = it.current_y;
14935 row->visible_height = row->height;
14936
14937 if (row->y < min_y)
14938 row->visible_height -= min_y - row->y;
14939 if (row->y + row->height > max_y)
14940 row->visible_height -= row->y + row->height - max_y;
14941 row->redraw_fringe_bitmaps_p = 1;
14942
14943 it.current_y += row->height;
14944
14945 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14946 last_reused_text_row = row;
14947 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14948 break;
14949 }
14950
14951 /* Disable lines in the current matrix which are now
14952 below the window. */
14953 for (++row; row < bottom_row; ++row)
14954 row->enabled_p = row->mode_line_p = 0;
14955 }
14956
14957 /* Update window_end_pos etc.; last_reused_text_row is the last
14958 reused row from the current matrix containing text, if any.
14959 The value of last_text_row is the last displayed line
14960 containing text. */
14961 if (last_reused_text_row)
14962 {
14963 w->window_end_bytepos
14964 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14965 w->window_end_pos
14966 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14967 w->window_end_vpos
14968 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14969 w->current_matrix));
14970 }
14971 else if (last_text_row)
14972 {
14973 w->window_end_bytepos
14974 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14975 w->window_end_pos
14976 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14977 w->window_end_vpos
14978 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14979 }
14980 else
14981 {
14982 /* This window must be completely empty. */
14983 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14984 w->window_end_pos = make_number (Z - ZV);
14985 w->window_end_vpos = make_number (0);
14986 }
14987 w->window_end_valid = Qnil;
14988
14989 /* Update hint: don't try scrolling again in update_window. */
14990 w->desired_matrix->no_scrolling_p = 1;
14991
14992 #if GLYPH_DEBUG
14993 debug_method_add (w, "try_window_reusing_current_matrix 1");
14994 #endif
14995 return 1;
14996 }
14997 else if (CHARPOS (new_start) > CHARPOS (start))
14998 {
14999 struct glyph_row *pt_row, *row;
15000 struct glyph_row *first_reusable_row;
15001 struct glyph_row *first_row_to_display;
15002 int dy;
15003 int yb = window_text_bottom_y (w);
15004
15005 /* Find the row starting at new_start, if there is one. Don't
15006 reuse a partially visible line at the end. */
15007 first_reusable_row = start_row;
15008 while (first_reusable_row->enabled_p
15009 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15010 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15011 < CHARPOS (new_start)))
15012 ++first_reusable_row;
15013
15014 /* Give up if there is no row to reuse. */
15015 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15016 || !first_reusable_row->enabled_p
15017 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15018 != CHARPOS (new_start)))
15019 return 0;
15020
15021 /* We can reuse fully visible rows beginning with
15022 first_reusable_row to the end of the window. Set
15023 first_row_to_display to the first row that cannot be reused.
15024 Set pt_row to the row containing point, if there is any. */
15025 pt_row = NULL;
15026 for (first_row_to_display = first_reusable_row;
15027 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15028 ++first_row_to_display)
15029 {
15030 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15031 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15032 pt_row = first_row_to_display;
15033 }
15034
15035 /* Start displaying at the start of first_row_to_display. */
15036 xassert (first_row_to_display->y < yb);
15037 init_to_row_start (&it, w, first_row_to_display);
15038
15039 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15040 - start_vpos);
15041 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15042 - nrows_scrolled);
15043 it.current_y = (first_row_to_display->y - first_reusable_row->y
15044 + WINDOW_HEADER_LINE_HEIGHT (w));
15045
15046 /* Display lines beginning with first_row_to_display in the
15047 desired matrix. Set last_text_row to the last row displayed
15048 that displays text. */
15049 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15050 if (pt_row == NULL)
15051 w->cursor.vpos = -1;
15052 last_text_row = NULL;
15053 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15054 if (display_line (&it))
15055 last_text_row = it.glyph_row - 1;
15056
15057 /* If point is in a reused row, adjust y and vpos of the cursor
15058 position. */
15059 if (pt_row)
15060 {
15061 w->cursor.vpos -= nrows_scrolled;
15062 w->cursor.y -= first_reusable_row->y - start_row->y;
15063 }
15064
15065 /* Give up if point isn't in a row displayed or reused. (This
15066 also handles the case where w->cursor.vpos < nrows_scrolled
15067 after the calls to display_line, which can happen with scroll
15068 margins. See bug#1295.) */
15069 if (w->cursor.vpos < 0)
15070 {
15071 clear_glyph_matrix (w->desired_matrix);
15072 return 0;
15073 }
15074
15075 /* Scroll the display. */
15076 run.current_y = first_reusable_row->y;
15077 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15078 run.height = it.last_visible_y - run.current_y;
15079 dy = run.current_y - run.desired_y;
15080
15081 if (run.height)
15082 {
15083 update_begin (f);
15084 FRAME_RIF (f)->update_window_begin_hook (w);
15085 FRAME_RIF (f)->clear_window_mouse_face (w);
15086 FRAME_RIF (f)->scroll_run_hook (w, &run);
15087 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15088 update_end (f);
15089 }
15090
15091 /* Adjust Y positions of reused rows. */
15092 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15093 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15094 max_y = it.last_visible_y;
15095 for (row = first_reusable_row; row < first_row_to_display; ++row)
15096 {
15097 row->y -= dy;
15098 row->visible_height = row->height;
15099 if (row->y < min_y)
15100 row->visible_height -= min_y - row->y;
15101 if (row->y + row->height > max_y)
15102 row->visible_height -= row->y + row->height - max_y;
15103 row->redraw_fringe_bitmaps_p = 1;
15104 }
15105
15106 /* Scroll the current matrix. */
15107 xassert (nrows_scrolled > 0);
15108 rotate_matrix (w->current_matrix,
15109 start_vpos,
15110 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15111 -nrows_scrolled);
15112
15113 /* Disable rows not reused. */
15114 for (row -= nrows_scrolled; row < bottom_row; ++row)
15115 row->enabled_p = 0;
15116
15117 /* Point may have moved to a different line, so we cannot assume that
15118 the previous cursor position is valid; locate the correct row. */
15119 if (pt_row)
15120 {
15121 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15122 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15123 row++)
15124 {
15125 w->cursor.vpos++;
15126 w->cursor.y = row->y;
15127 }
15128 if (row < bottom_row)
15129 {
15130 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15131 struct glyph *end = glyph + row->used[TEXT_AREA];
15132
15133 /* Can't use this optimization with bidi-reordered glyph
15134 rows, unless cursor is already at point. */
15135 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15136 {
15137 if (!(w->cursor.hpos >= 0
15138 && w->cursor.hpos < row->used[TEXT_AREA]
15139 && BUFFERP (glyph->object)
15140 && glyph->charpos == PT))
15141 return 0;
15142 }
15143 else
15144 for (; glyph < end
15145 && (!BUFFERP (glyph->object)
15146 || glyph->charpos < PT);
15147 glyph++)
15148 {
15149 w->cursor.hpos++;
15150 w->cursor.x += glyph->pixel_width;
15151 }
15152 }
15153 }
15154
15155 /* Adjust window end. A null value of last_text_row means that
15156 the window end is in reused rows which in turn means that
15157 only its vpos can have changed. */
15158 if (last_text_row)
15159 {
15160 w->window_end_bytepos
15161 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15162 w->window_end_pos
15163 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15164 w->window_end_vpos
15165 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15166 }
15167 else
15168 {
15169 w->window_end_vpos
15170 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15171 }
15172
15173 w->window_end_valid = Qnil;
15174 w->desired_matrix->no_scrolling_p = 1;
15175
15176 #if GLYPH_DEBUG
15177 debug_method_add (w, "try_window_reusing_current_matrix 2");
15178 #endif
15179 return 1;
15180 }
15181
15182 return 0;
15183 }
15184
15185
15186 \f
15187 /************************************************************************
15188 Window redisplay reusing current matrix when buffer has changed
15189 ************************************************************************/
15190
15191 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15192 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15193 EMACS_INT *, EMACS_INT *);
15194 static struct glyph_row *
15195 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15196 struct glyph_row *);
15197
15198
15199 /* Return the last row in MATRIX displaying text. If row START is
15200 non-null, start searching with that row. IT gives the dimensions
15201 of the display. Value is null if matrix is empty; otherwise it is
15202 a pointer to the row found. */
15203
15204 static struct glyph_row *
15205 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15206 struct glyph_row *start)
15207 {
15208 struct glyph_row *row, *row_found;
15209
15210 /* Set row_found to the last row in IT->w's current matrix
15211 displaying text. The loop looks funny but think of partially
15212 visible lines. */
15213 row_found = NULL;
15214 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15215 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15216 {
15217 xassert (row->enabled_p);
15218 row_found = row;
15219 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15220 break;
15221 ++row;
15222 }
15223
15224 return row_found;
15225 }
15226
15227
15228 /* Return the last row in the current matrix of W that is not affected
15229 by changes at the start of current_buffer that occurred since W's
15230 current matrix was built. Value is null if no such row exists.
15231
15232 BEG_UNCHANGED us the number of characters unchanged at the start of
15233 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15234 first changed character in current_buffer. Characters at positions <
15235 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15236 when the current matrix was built. */
15237
15238 static struct glyph_row *
15239 find_last_unchanged_at_beg_row (struct window *w)
15240 {
15241 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15242 struct glyph_row *row;
15243 struct glyph_row *row_found = NULL;
15244 int yb = window_text_bottom_y (w);
15245
15246 /* Find the last row displaying unchanged text. */
15247 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15248 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15249 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15250 ++row)
15251 {
15252 if (/* If row ends before first_changed_pos, it is unchanged,
15253 except in some case. */
15254 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15255 /* When row ends in ZV and we write at ZV it is not
15256 unchanged. */
15257 && !row->ends_at_zv_p
15258 /* When first_changed_pos is the end of a continued line,
15259 row is not unchanged because it may be no longer
15260 continued. */
15261 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15262 && (row->continued_p
15263 || row->exact_window_width_line_p)))
15264 row_found = row;
15265
15266 /* Stop if last visible row. */
15267 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15268 break;
15269 }
15270
15271 return row_found;
15272 }
15273
15274
15275 /* Find the first glyph row in the current matrix of W that is not
15276 affected by changes at the end of current_buffer since the
15277 time W's current matrix was built.
15278
15279 Return in *DELTA the number of chars by which buffer positions in
15280 unchanged text at the end of current_buffer must be adjusted.
15281
15282 Return in *DELTA_BYTES the corresponding number of bytes.
15283
15284 Value is null if no such row exists, i.e. all rows are affected by
15285 changes. */
15286
15287 static struct glyph_row *
15288 find_first_unchanged_at_end_row (struct window *w,
15289 EMACS_INT *delta, EMACS_INT *delta_bytes)
15290 {
15291 struct glyph_row *row;
15292 struct glyph_row *row_found = NULL;
15293
15294 *delta = *delta_bytes = 0;
15295
15296 /* Display must not have been paused, otherwise the current matrix
15297 is not up to date. */
15298 eassert (!NILP (w->window_end_valid));
15299
15300 /* A value of window_end_pos >= END_UNCHANGED means that the window
15301 end is in the range of changed text. If so, there is no
15302 unchanged row at the end of W's current matrix. */
15303 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15304 return NULL;
15305
15306 /* Set row to the last row in W's current matrix displaying text. */
15307 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15308
15309 /* If matrix is entirely empty, no unchanged row exists. */
15310 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15311 {
15312 /* The value of row is the last glyph row in the matrix having a
15313 meaningful buffer position in it. The end position of row
15314 corresponds to window_end_pos. This allows us to translate
15315 buffer positions in the current matrix to current buffer
15316 positions for characters not in changed text. */
15317 EMACS_INT Z_old =
15318 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15319 EMACS_INT Z_BYTE_old =
15320 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15321 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15322 struct glyph_row *first_text_row
15323 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15324
15325 *delta = Z - Z_old;
15326 *delta_bytes = Z_BYTE - Z_BYTE_old;
15327
15328 /* Set last_unchanged_pos to the buffer position of the last
15329 character in the buffer that has not been changed. Z is the
15330 index + 1 of the last character in current_buffer, i.e. by
15331 subtracting END_UNCHANGED we get the index of the last
15332 unchanged character, and we have to add BEG to get its buffer
15333 position. */
15334 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15335 last_unchanged_pos_old = last_unchanged_pos - *delta;
15336
15337 /* Search backward from ROW for a row displaying a line that
15338 starts at a minimum position >= last_unchanged_pos_old. */
15339 for (; row > first_text_row; --row)
15340 {
15341 /* This used to abort, but it can happen.
15342 It is ok to just stop the search instead here. KFS. */
15343 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15344 break;
15345
15346 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15347 row_found = row;
15348 }
15349 }
15350
15351 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15352
15353 return row_found;
15354 }
15355
15356
15357 /* Make sure that glyph rows in the current matrix of window W
15358 reference the same glyph memory as corresponding rows in the
15359 frame's frame matrix. This function is called after scrolling W's
15360 current matrix on a terminal frame in try_window_id and
15361 try_window_reusing_current_matrix. */
15362
15363 static void
15364 sync_frame_with_window_matrix_rows (struct window *w)
15365 {
15366 struct frame *f = XFRAME (w->frame);
15367 struct glyph_row *window_row, *window_row_end, *frame_row;
15368
15369 /* Preconditions: W must be a leaf window and full-width. Its frame
15370 must have a frame matrix. */
15371 xassert (NILP (w->hchild) && NILP (w->vchild));
15372 xassert (WINDOW_FULL_WIDTH_P (w));
15373 xassert (!FRAME_WINDOW_P (f));
15374
15375 /* If W is a full-width window, glyph pointers in W's current matrix
15376 have, by definition, to be the same as glyph pointers in the
15377 corresponding frame matrix. Note that frame matrices have no
15378 marginal areas (see build_frame_matrix). */
15379 window_row = w->current_matrix->rows;
15380 window_row_end = window_row + w->current_matrix->nrows;
15381 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15382 while (window_row < window_row_end)
15383 {
15384 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15385 struct glyph *end = window_row->glyphs[LAST_AREA];
15386
15387 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15388 frame_row->glyphs[TEXT_AREA] = start;
15389 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15390 frame_row->glyphs[LAST_AREA] = end;
15391
15392 /* Disable frame rows whose corresponding window rows have
15393 been disabled in try_window_id. */
15394 if (!window_row->enabled_p)
15395 frame_row->enabled_p = 0;
15396
15397 ++window_row, ++frame_row;
15398 }
15399 }
15400
15401
15402 /* Find the glyph row in window W containing CHARPOS. Consider all
15403 rows between START and END (not inclusive). END null means search
15404 all rows to the end of the display area of W. Value is the row
15405 containing CHARPOS or null. */
15406
15407 struct glyph_row *
15408 row_containing_pos (struct window *w, EMACS_INT charpos,
15409 struct glyph_row *start, struct glyph_row *end, int dy)
15410 {
15411 struct glyph_row *row = start;
15412 struct glyph_row *best_row = NULL;
15413 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15414 int last_y;
15415
15416 /* If we happen to start on a header-line, skip that. */
15417 if (row->mode_line_p)
15418 ++row;
15419
15420 if ((end && row >= end) || !row->enabled_p)
15421 return NULL;
15422
15423 last_y = window_text_bottom_y (w) - dy;
15424
15425 while (1)
15426 {
15427 /* Give up if we have gone too far. */
15428 if (end && row >= end)
15429 return NULL;
15430 /* This formerly returned if they were equal.
15431 I think that both quantities are of a "last plus one" type;
15432 if so, when they are equal, the row is within the screen. -- rms. */
15433 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15434 return NULL;
15435
15436 /* If it is in this row, return this row. */
15437 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15438 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15439 /* The end position of a row equals the start
15440 position of the next row. If CHARPOS is there, we
15441 would rather display it in the next line, except
15442 when this line ends in ZV. */
15443 && !row->ends_at_zv_p
15444 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15445 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15446 {
15447 struct glyph *g;
15448
15449 if (NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15450 || (!best_row && !row->continued_p))
15451 return row;
15452 /* In bidi-reordered rows, there could be several rows
15453 occluding point, all of them belonging to the same
15454 continued line. We need to find the row which fits
15455 CHARPOS the best. */
15456 for (g = row->glyphs[TEXT_AREA];
15457 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15458 g++)
15459 {
15460 if (!STRINGP (g->object))
15461 {
15462 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15463 {
15464 mindif = eabs (g->charpos - charpos);
15465 best_row = row;
15466 /* Exact match always wins. */
15467 if (mindif == 0)
15468 return best_row;
15469 }
15470 }
15471 }
15472 }
15473 else if (best_row && !row->continued_p)
15474 return best_row;
15475 ++row;
15476 }
15477 }
15478
15479
15480 /* Try to redisplay window W by reusing its existing display. W's
15481 current matrix must be up to date when this function is called,
15482 i.e. window_end_valid must not be nil.
15483
15484 Value is
15485
15486 1 if display has been updated
15487 0 if otherwise unsuccessful
15488 -1 if redisplay with same window start is known not to succeed
15489
15490 The following steps are performed:
15491
15492 1. Find the last row in the current matrix of W that is not
15493 affected by changes at the start of current_buffer. If no such row
15494 is found, give up.
15495
15496 2. Find the first row in W's current matrix that is not affected by
15497 changes at the end of current_buffer. Maybe there is no such row.
15498
15499 3. Display lines beginning with the row + 1 found in step 1 to the
15500 row found in step 2 or, if step 2 didn't find a row, to the end of
15501 the window.
15502
15503 4. If cursor is not known to appear on the window, give up.
15504
15505 5. If display stopped at the row found in step 2, scroll the
15506 display and current matrix as needed.
15507
15508 6. Maybe display some lines at the end of W, if we must. This can
15509 happen under various circumstances, like a partially visible line
15510 becoming fully visible, or because newly displayed lines are displayed
15511 in smaller font sizes.
15512
15513 7. Update W's window end information. */
15514
15515 static int
15516 try_window_id (struct window *w)
15517 {
15518 struct frame *f = XFRAME (w->frame);
15519 struct glyph_matrix *current_matrix = w->current_matrix;
15520 struct glyph_matrix *desired_matrix = w->desired_matrix;
15521 struct glyph_row *last_unchanged_at_beg_row;
15522 struct glyph_row *first_unchanged_at_end_row;
15523 struct glyph_row *row;
15524 struct glyph_row *bottom_row;
15525 int bottom_vpos;
15526 struct it it;
15527 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
15528 int dvpos, dy;
15529 struct text_pos start_pos;
15530 struct run run;
15531 int first_unchanged_at_end_vpos = 0;
15532 struct glyph_row *last_text_row, *last_text_row_at_end;
15533 struct text_pos start;
15534 EMACS_INT first_changed_charpos, last_changed_charpos;
15535
15536 #if GLYPH_DEBUG
15537 if (inhibit_try_window_id)
15538 return 0;
15539 #endif
15540
15541 /* This is handy for debugging. */
15542 #if 0
15543 #define GIVE_UP(X) \
15544 do { \
15545 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15546 return 0; \
15547 } while (0)
15548 #else
15549 #define GIVE_UP(X) return 0
15550 #endif
15551
15552 SET_TEXT_POS_FROM_MARKER (start, w->start);
15553
15554 /* Don't use this for mini-windows because these can show
15555 messages and mini-buffers, and we don't handle that here. */
15556 if (MINI_WINDOW_P (w))
15557 GIVE_UP (1);
15558
15559 /* This flag is used to prevent redisplay optimizations. */
15560 if (windows_or_buffers_changed || cursor_type_changed)
15561 GIVE_UP (2);
15562
15563 /* Verify that narrowing has not changed.
15564 Also verify that we were not told to prevent redisplay optimizations.
15565 It would be nice to further
15566 reduce the number of cases where this prevents try_window_id. */
15567 if (current_buffer->clip_changed
15568 || current_buffer->prevent_redisplay_optimizations_p)
15569 GIVE_UP (3);
15570
15571 /* Window must either use window-based redisplay or be full width. */
15572 if (!FRAME_WINDOW_P (f)
15573 && (!FRAME_LINE_INS_DEL_OK (f)
15574 || !WINDOW_FULL_WIDTH_P (w)))
15575 GIVE_UP (4);
15576
15577 /* Give up if point is known NOT to appear in W. */
15578 if (PT < CHARPOS (start))
15579 GIVE_UP (5);
15580
15581 /* Another way to prevent redisplay optimizations. */
15582 if (XFASTINT (w->last_modified) == 0)
15583 GIVE_UP (6);
15584
15585 /* Verify that window is not hscrolled. */
15586 if (XFASTINT (w->hscroll) != 0)
15587 GIVE_UP (7);
15588
15589 /* Verify that display wasn't paused. */
15590 if (NILP (w->window_end_valid))
15591 GIVE_UP (8);
15592
15593 /* Can't use this if highlighting a region because a cursor movement
15594 will do more than just set the cursor. */
15595 if (!NILP (Vtransient_mark_mode)
15596 && !NILP (current_buffer->mark_active))
15597 GIVE_UP (9);
15598
15599 /* Likewise if highlighting trailing whitespace. */
15600 if (!NILP (Vshow_trailing_whitespace))
15601 GIVE_UP (11);
15602
15603 /* Likewise if showing a region. */
15604 if (!NILP (w->region_showing))
15605 GIVE_UP (10);
15606
15607 /* Can't use this if overlay arrow position and/or string have
15608 changed. */
15609 if (overlay_arrows_changed_p ())
15610 GIVE_UP (12);
15611
15612 /* When word-wrap is on, adding a space to the first word of a
15613 wrapped line can change the wrap position, altering the line
15614 above it. It might be worthwhile to handle this more
15615 intelligently, but for now just redisplay from scratch. */
15616 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15617 GIVE_UP (21);
15618
15619 /* Under bidi reordering, adding or deleting a character in the
15620 beginning of a paragraph, before the first strong directional
15621 character, can change the base direction of the paragraph (unless
15622 the buffer specifies a fixed paragraph direction), which will
15623 require to redisplay the whole paragraph. It might be worthwhile
15624 to find the paragraph limits and widen the range of redisplayed
15625 lines to that, but for now just give up this optimization and
15626 redisplay from scratch. */
15627 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15628 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15629 GIVE_UP (22);
15630
15631 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15632 only if buffer has really changed. The reason is that the gap is
15633 initially at Z for freshly visited files. The code below would
15634 set end_unchanged to 0 in that case. */
15635 if (MODIFF > SAVE_MODIFF
15636 /* This seems to happen sometimes after saving a buffer. */
15637 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15638 {
15639 if (GPT - BEG < BEG_UNCHANGED)
15640 BEG_UNCHANGED = GPT - BEG;
15641 if (Z - GPT < END_UNCHANGED)
15642 END_UNCHANGED = Z - GPT;
15643 }
15644
15645 /* The position of the first and last character that has been changed. */
15646 first_changed_charpos = BEG + BEG_UNCHANGED;
15647 last_changed_charpos = Z - END_UNCHANGED;
15648
15649 /* If window starts after a line end, and the last change is in
15650 front of that newline, then changes don't affect the display.
15651 This case happens with stealth-fontification. Note that although
15652 the display is unchanged, glyph positions in the matrix have to
15653 be adjusted, of course. */
15654 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15655 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15656 && ((last_changed_charpos < CHARPOS (start)
15657 && CHARPOS (start) == BEGV)
15658 || (last_changed_charpos < CHARPOS (start) - 1
15659 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15660 {
15661 EMACS_INT Z_old, delta, Z_BYTE_old, delta_bytes;
15662 struct glyph_row *r0;
15663
15664 /* Compute how many chars/bytes have been added to or removed
15665 from the buffer. */
15666 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15667 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15668 delta = Z - Z_old;
15669 delta_bytes = Z_BYTE - Z_BYTE_old;
15670
15671 /* Give up if PT is not in the window. Note that it already has
15672 been checked at the start of try_window_id that PT is not in
15673 front of the window start. */
15674 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15675 GIVE_UP (13);
15676
15677 /* If window start is unchanged, we can reuse the whole matrix
15678 as is, after adjusting glyph positions. No need to compute
15679 the window end again, since its offset from Z hasn't changed. */
15680 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15681 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15682 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15683 /* PT must not be in a partially visible line. */
15684 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15685 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15686 {
15687 /* Adjust positions in the glyph matrix. */
15688 if (delta || delta_bytes)
15689 {
15690 struct glyph_row *r1
15691 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15692 increment_matrix_positions (w->current_matrix,
15693 MATRIX_ROW_VPOS (r0, current_matrix),
15694 MATRIX_ROW_VPOS (r1, current_matrix),
15695 delta, delta_bytes);
15696 }
15697
15698 /* Set the cursor. */
15699 row = row_containing_pos (w, PT, r0, NULL, 0);
15700 if (row)
15701 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15702 else
15703 abort ();
15704 return 1;
15705 }
15706 }
15707
15708 /* Handle the case that changes are all below what is displayed in
15709 the window, and that PT is in the window. This shortcut cannot
15710 be taken if ZV is visible in the window, and text has been added
15711 there that is visible in the window. */
15712 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15713 /* ZV is not visible in the window, or there are no
15714 changes at ZV, actually. */
15715 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15716 || first_changed_charpos == last_changed_charpos))
15717 {
15718 struct glyph_row *r0;
15719
15720 /* Give up if PT is not in the window. Note that it already has
15721 been checked at the start of try_window_id that PT is not in
15722 front of the window start. */
15723 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15724 GIVE_UP (14);
15725
15726 /* If window start is unchanged, we can reuse the whole matrix
15727 as is, without changing glyph positions since no text has
15728 been added/removed in front of the window end. */
15729 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15730 if (TEXT_POS_EQUAL_P (start, r0->minpos)
15731 /* PT must not be in a partially visible line. */
15732 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15733 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15734 {
15735 /* We have to compute the window end anew since text
15736 could have been added/removed after it. */
15737 w->window_end_pos
15738 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15739 w->window_end_bytepos
15740 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15741
15742 /* Set the cursor. */
15743 row = row_containing_pos (w, PT, r0, NULL, 0);
15744 if (row)
15745 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15746 else
15747 abort ();
15748 return 2;
15749 }
15750 }
15751
15752 /* Give up if window start is in the changed area.
15753
15754 The condition used to read
15755
15756 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15757
15758 but why that was tested escapes me at the moment. */
15759 if (CHARPOS (start) >= first_changed_charpos
15760 && CHARPOS (start) <= last_changed_charpos)
15761 GIVE_UP (15);
15762
15763 /* Check that window start agrees with the start of the first glyph
15764 row in its current matrix. Check this after we know the window
15765 start is not in changed text, otherwise positions would not be
15766 comparable. */
15767 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15768 if (!TEXT_POS_EQUAL_P (start, row->minpos))
15769 GIVE_UP (16);
15770
15771 /* Give up if the window ends in strings. Overlay strings
15772 at the end are difficult to handle, so don't try. */
15773 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15774 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15775 GIVE_UP (20);
15776
15777 /* Compute the position at which we have to start displaying new
15778 lines. Some of the lines at the top of the window might be
15779 reusable because they are not displaying changed text. Find the
15780 last row in W's current matrix not affected by changes at the
15781 start of current_buffer. Value is null if changes start in the
15782 first line of window. */
15783 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15784 if (last_unchanged_at_beg_row)
15785 {
15786 /* Avoid starting to display in the moddle of a character, a TAB
15787 for instance. This is easier than to set up the iterator
15788 exactly, and it's not a frequent case, so the additional
15789 effort wouldn't really pay off. */
15790 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15791 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15792 && last_unchanged_at_beg_row > w->current_matrix->rows)
15793 --last_unchanged_at_beg_row;
15794
15795 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15796 GIVE_UP (17);
15797
15798 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15799 GIVE_UP (18);
15800 start_pos = it.current.pos;
15801
15802 /* Start displaying new lines in the desired matrix at the same
15803 vpos we would use in the current matrix, i.e. below
15804 last_unchanged_at_beg_row. */
15805 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15806 current_matrix);
15807 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15808 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15809
15810 xassert (it.hpos == 0 && it.current_x == 0);
15811 }
15812 else
15813 {
15814 /* There are no reusable lines at the start of the window.
15815 Start displaying in the first text line. */
15816 start_display (&it, w, start);
15817 it.vpos = it.first_vpos;
15818 start_pos = it.current.pos;
15819 }
15820
15821 /* Find the first row that is not affected by changes at the end of
15822 the buffer. Value will be null if there is no unchanged row, in
15823 which case we must redisplay to the end of the window. delta
15824 will be set to the value by which buffer positions beginning with
15825 first_unchanged_at_end_row have to be adjusted due to text
15826 changes. */
15827 first_unchanged_at_end_row
15828 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15829 IF_DEBUG (debug_delta = delta);
15830 IF_DEBUG (debug_delta_bytes = delta_bytes);
15831
15832 /* Set stop_pos to the buffer position up to which we will have to
15833 display new lines. If first_unchanged_at_end_row != NULL, this
15834 is the buffer position of the start of the line displayed in that
15835 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15836 that we don't stop at a buffer position. */
15837 stop_pos = 0;
15838 if (first_unchanged_at_end_row)
15839 {
15840 xassert (last_unchanged_at_beg_row == NULL
15841 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15842
15843 /* If this is a continuation line, move forward to the next one
15844 that isn't. Changes in lines above affect this line.
15845 Caution: this may move first_unchanged_at_end_row to a row
15846 not displaying text. */
15847 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15848 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15849 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15850 < it.last_visible_y))
15851 ++first_unchanged_at_end_row;
15852
15853 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15854 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15855 >= it.last_visible_y))
15856 first_unchanged_at_end_row = NULL;
15857 else
15858 {
15859 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15860 + delta);
15861 first_unchanged_at_end_vpos
15862 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15863 xassert (stop_pos >= Z - END_UNCHANGED);
15864 }
15865 }
15866 else if (last_unchanged_at_beg_row == NULL)
15867 GIVE_UP (19);
15868
15869
15870 #if GLYPH_DEBUG
15871
15872 /* Either there is no unchanged row at the end, or the one we have
15873 now displays text. This is a necessary condition for the window
15874 end pos calculation at the end of this function. */
15875 xassert (first_unchanged_at_end_row == NULL
15876 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15877
15878 debug_last_unchanged_at_beg_vpos
15879 = (last_unchanged_at_beg_row
15880 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15881 : -1);
15882 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15883
15884 #endif /* GLYPH_DEBUG != 0 */
15885
15886
15887 /* Display new lines. Set last_text_row to the last new line
15888 displayed which has text on it, i.e. might end up as being the
15889 line where the window_end_vpos is. */
15890 w->cursor.vpos = -1;
15891 last_text_row = NULL;
15892 overlay_arrow_seen = 0;
15893 while (it.current_y < it.last_visible_y
15894 && !fonts_changed_p
15895 && (first_unchanged_at_end_row == NULL
15896 || IT_CHARPOS (it) < stop_pos))
15897 {
15898 if (display_line (&it))
15899 last_text_row = it.glyph_row - 1;
15900 }
15901
15902 if (fonts_changed_p)
15903 return -1;
15904
15905
15906 /* Compute differences in buffer positions, y-positions etc. for
15907 lines reused at the bottom of the window. Compute what we can
15908 scroll. */
15909 if (first_unchanged_at_end_row
15910 /* No lines reused because we displayed everything up to the
15911 bottom of the window. */
15912 && it.current_y < it.last_visible_y)
15913 {
15914 dvpos = (it.vpos
15915 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15916 current_matrix));
15917 dy = it.current_y - first_unchanged_at_end_row->y;
15918 run.current_y = first_unchanged_at_end_row->y;
15919 run.desired_y = run.current_y + dy;
15920 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15921 }
15922 else
15923 {
15924 delta = delta_bytes = dvpos = dy
15925 = run.current_y = run.desired_y = run.height = 0;
15926 first_unchanged_at_end_row = NULL;
15927 }
15928 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15929
15930
15931 /* Find the cursor if not already found. We have to decide whether
15932 PT will appear on this window (it sometimes doesn't, but this is
15933 not a very frequent case.) This decision has to be made before
15934 the current matrix is altered. A value of cursor.vpos < 0 means
15935 that PT is either in one of the lines beginning at
15936 first_unchanged_at_end_row or below the window. Don't care for
15937 lines that might be displayed later at the window end; as
15938 mentioned, this is not a frequent case. */
15939 if (w->cursor.vpos < 0)
15940 {
15941 /* Cursor in unchanged rows at the top? */
15942 if (PT < CHARPOS (start_pos)
15943 && last_unchanged_at_beg_row)
15944 {
15945 row = row_containing_pos (w, PT,
15946 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15947 last_unchanged_at_beg_row + 1, 0);
15948 if (row)
15949 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15950 }
15951
15952 /* Start from first_unchanged_at_end_row looking for PT. */
15953 else if (first_unchanged_at_end_row)
15954 {
15955 row = row_containing_pos (w, PT - delta,
15956 first_unchanged_at_end_row, NULL, 0);
15957 if (row)
15958 set_cursor_from_row (w, row, w->current_matrix, delta,
15959 delta_bytes, dy, dvpos);
15960 }
15961
15962 /* Give up if cursor was not found. */
15963 if (w->cursor.vpos < 0)
15964 {
15965 clear_glyph_matrix (w->desired_matrix);
15966 return -1;
15967 }
15968 }
15969
15970 /* Don't let the cursor end in the scroll margins. */
15971 {
15972 int this_scroll_margin, cursor_height;
15973
15974 this_scroll_margin = max (0, scroll_margin);
15975 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15976 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15977 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15978
15979 if ((w->cursor.y < this_scroll_margin
15980 && CHARPOS (start) > BEGV)
15981 /* Old redisplay didn't take scroll margin into account at the bottom,
15982 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15983 || (w->cursor.y + (make_cursor_line_fully_visible_p
15984 ? cursor_height + this_scroll_margin
15985 : 1)) > it.last_visible_y)
15986 {
15987 w->cursor.vpos = -1;
15988 clear_glyph_matrix (w->desired_matrix);
15989 return -1;
15990 }
15991 }
15992
15993 /* Scroll the display. Do it before changing the current matrix so
15994 that xterm.c doesn't get confused about where the cursor glyph is
15995 found. */
15996 if (dy && run.height)
15997 {
15998 update_begin (f);
15999
16000 if (FRAME_WINDOW_P (f))
16001 {
16002 FRAME_RIF (f)->update_window_begin_hook (w);
16003 FRAME_RIF (f)->clear_window_mouse_face (w);
16004 FRAME_RIF (f)->scroll_run_hook (w, &run);
16005 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16006 }
16007 else
16008 {
16009 /* Terminal frame. In this case, dvpos gives the number of
16010 lines to scroll by; dvpos < 0 means scroll up. */
16011 int first_unchanged_at_end_vpos
16012 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16013 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
16014 int end = (WINDOW_TOP_EDGE_LINE (w)
16015 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16016 + window_internal_height (w));
16017
16018 #if defined (HAVE_GPM) || defined (MSDOS)
16019 x_clear_window_mouse_face (w);
16020 #endif
16021 /* Perform the operation on the screen. */
16022 if (dvpos > 0)
16023 {
16024 /* Scroll last_unchanged_at_beg_row to the end of the
16025 window down dvpos lines. */
16026 set_terminal_window (f, end);
16027
16028 /* On dumb terminals delete dvpos lines at the end
16029 before inserting dvpos empty lines. */
16030 if (!FRAME_SCROLL_REGION_OK (f))
16031 ins_del_lines (f, end - dvpos, -dvpos);
16032
16033 /* Insert dvpos empty lines in front of
16034 last_unchanged_at_beg_row. */
16035 ins_del_lines (f, from, dvpos);
16036 }
16037 else if (dvpos < 0)
16038 {
16039 /* Scroll up last_unchanged_at_beg_vpos to the end of
16040 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16041 set_terminal_window (f, end);
16042
16043 /* Delete dvpos lines in front of
16044 last_unchanged_at_beg_vpos. ins_del_lines will set
16045 the cursor to the given vpos and emit |dvpos| delete
16046 line sequences. */
16047 ins_del_lines (f, from + dvpos, dvpos);
16048
16049 /* On a dumb terminal insert dvpos empty lines at the
16050 end. */
16051 if (!FRAME_SCROLL_REGION_OK (f))
16052 ins_del_lines (f, end + dvpos, -dvpos);
16053 }
16054
16055 set_terminal_window (f, 0);
16056 }
16057
16058 update_end (f);
16059 }
16060
16061 /* Shift reused rows of the current matrix to the right position.
16062 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16063 text. */
16064 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16065 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16066 if (dvpos < 0)
16067 {
16068 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16069 bottom_vpos, dvpos);
16070 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16071 bottom_vpos, 0);
16072 }
16073 else if (dvpos > 0)
16074 {
16075 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16076 bottom_vpos, dvpos);
16077 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16078 first_unchanged_at_end_vpos + dvpos, 0);
16079 }
16080
16081 /* For frame-based redisplay, make sure that current frame and window
16082 matrix are in sync with respect to glyph memory. */
16083 if (!FRAME_WINDOW_P (f))
16084 sync_frame_with_window_matrix_rows (w);
16085
16086 /* Adjust buffer positions in reused rows. */
16087 if (delta || delta_bytes)
16088 increment_matrix_positions (current_matrix,
16089 first_unchanged_at_end_vpos + dvpos,
16090 bottom_vpos, delta, delta_bytes);
16091
16092 /* Adjust Y positions. */
16093 if (dy)
16094 shift_glyph_matrix (w, current_matrix,
16095 first_unchanged_at_end_vpos + dvpos,
16096 bottom_vpos, dy);
16097
16098 if (first_unchanged_at_end_row)
16099 {
16100 first_unchanged_at_end_row += dvpos;
16101 if (first_unchanged_at_end_row->y >= it.last_visible_y
16102 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16103 first_unchanged_at_end_row = NULL;
16104 }
16105
16106 /* If scrolling up, there may be some lines to display at the end of
16107 the window. */
16108 last_text_row_at_end = NULL;
16109 if (dy < 0)
16110 {
16111 /* Scrolling up can leave for example a partially visible line
16112 at the end of the window to be redisplayed. */
16113 /* Set last_row to the glyph row in the current matrix where the
16114 window end line is found. It has been moved up or down in
16115 the matrix by dvpos. */
16116 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16117 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16118
16119 /* If last_row is the window end line, it should display text. */
16120 xassert (last_row->displays_text_p);
16121
16122 /* If window end line was partially visible before, begin
16123 displaying at that line. Otherwise begin displaying with the
16124 line following it. */
16125 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16126 {
16127 init_to_row_start (&it, w, last_row);
16128 it.vpos = last_vpos;
16129 it.current_y = last_row->y;
16130 }
16131 else
16132 {
16133 init_to_row_end (&it, w, last_row);
16134 it.vpos = 1 + last_vpos;
16135 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16136 ++last_row;
16137 }
16138
16139 /* We may start in a continuation line. If so, we have to
16140 get the right continuation_lines_width and current_x. */
16141 it.continuation_lines_width = last_row->continuation_lines_width;
16142 it.hpos = it.current_x = 0;
16143
16144 /* Display the rest of the lines at the window end. */
16145 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16146 while (it.current_y < it.last_visible_y
16147 && !fonts_changed_p)
16148 {
16149 /* Is it always sure that the display agrees with lines in
16150 the current matrix? I don't think so, so we mark rows
16151 displayed invalid in the current matrix by setting their
16152 enabled_p flag to zero. */
16153 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16154 if (display_line (&it))
16155 last_text_row_at_end = it.glyph_row - 1;
16156 }
16157 }
16158
16159 /* Update window_end_pos and window_end_vpos. */
16160 if (first_unchanged_at_end_row
16161 && !last_text_row_at_end)
16162 {
16163 /* Window end line if one of the preserved rows from the current
16164 matrix. Set row to the last row displaying text in current
16165 matrix starting at first_unchanged_at_end_row, after
16166 scrolling. */
16167 xassert (first_unchanged_at_end_row->displays_text_p);
16168 row = find_last_row_displaying_text (w->current_matrix, &it,
16169 first_unchanged_at_end_row);
16170 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16171
16172 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16173 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16174 w->window_end_vpos
16175 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16176 xassert (w->window_end_bytepos >= 0);
16177 IF_DEBUG (debug_method_add (w, "A"));
16178 }
16179 else if (last_text_row_at_end)
16180 {
16181 w->window_end_pos
16182 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16183 w->window_end_bytepos
16184 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16185 w->window_end_vpos
16186 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16187 xassert (w->window_end_bytepos >= 0);
16188 IF_DEBUG (debug_method_add (w, "B"));
16189 }
16190 else if (last_text_row)
16191 {
16192 /* We have displayed either to the end of the window or at the
16193 end of the window, i.e. the last row with text is to be found
16194 in the desired matrix. */
16195 w->window_end_pos
16196 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16197 w->window_end_bytepos
16198 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16199 w->window_end_vpos
16200 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16201 xassert (w->window_end_bytepos >= 0);
16202 }
16203 else if (first_unchanged_at_end_row == NULL
16204 && last_text_row == NULL
16205 && last_text_row_at_end == NULL)
16206 {
16207 /* Displayed to end of window, but no line containing text was
16208 displayed. Lines were deleted at the end of the window. */
16209 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16210 int vpos = XFASTINT (w->window_end_vpos);
16211 struct glyph_row *current_row = current_matrix->rows + vpos;
16212 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16213
16214 for (row = NULL;
16215 row == NULL && vpos >= first_vpos;
16216 --vpos, --current_row, --desired_row)
16217 {
16218 if (desired_row->enabled_p)
16219 {
16220 if (desired_row->displays_text_p)
16221 row = desired_row;
16222 }
16223 else if (current_row->displays_text_p)
16224 row = current_row;
16225 }
16226
16227 xassert (row != NULL);
16228 w->window_end_vpos = make_number (vpos + 1);
16229 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16230 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16231 xassert (w->window_end_bytepos >= 0);
16232 IF_DEBUG (debug_method_add (w, "C"));
16233 }
16234 else
16235 abort ();
16236
16237 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16238 debug_end_vpos = XFASTINT (w->window_end_vpos));
16239
16240 /* Record that display has not been completed. */
16241 w->window_end_valid = Qnil;
16242 w->desired_matrix->no_scrolling_p = 1;
16243 return 3;
16244
16245 #undef GIVE_UP
16246 }
16247
16248
16249 \f
16250 /***********************************************************************
16251 More debugging support
16252 ***********************************************************************/
16253
16254 #if GLYPH_DEBUG
16255
16256 void dump_glyph_row (struct glyph_row *, int, int);
16257 void dump_glyph_matrix (struct glyph_matrix *, int);
16258 void dump_glyph (struct glyph_row *, struct glyph *, int);
16259
16260
16261 /* Dump the contents of glyph matrix MATRIX on stderr.
16262
16263 GLYPHS 0 means don't show glyph contents.
16264 GLYPHS 1 means show glyphs in short form
16265 GLYPHS > 1 means show glyphs in long form. */
16266
16267 void
16268 dump_glyph_matrix (matrix, glyphs)
16269 struct glyph_matrix *matrix;
16270 int glyphs;
16271 {
16272 int i;
16273 for (i = 0; i < matrix->nrows; ++i)
16274 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16275 }
16276
16277
16278 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16279 the glyph row and area where the glyph comes from. */
16280
16281 void
16282 dump_glyph (row, glyph, area)
16283 struct glyph_row *row;
16284 struct glyph *glyph;
16285 int area;
16286 {
16287 if (glyph->type == CHAR_GLYPH)
16288 {
16289 fprintf (stderr,
16290 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16291 glyph - row->glyphs[TEXT_AREA],
16292 'C',
16293 glyph->charpos,
16294 (BUFFERP (glyph->object)
16295 ? 'B'
16296 : (STRINGP (glyph->object)
16297 ? 'S'
16298 : '-')),
16299 glyph->pixel_width,
16300 glyph->u.ch,
16301 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16302 ? glyph->u.ch
16303 : '.'),
16304 glyph->face_id,
16305 glyph->left_box_line_p,
16306 glyph->right_box_line_p);
16307 }
16308 else if (glyph->type == STRETCH_GLYPH)
16309 {
16310 fprintf (stderr,
16311 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16312 glyph - row->glyphs[TEXT_AREA],
16313 'S',
16314 glyph->charpos,
16315 (BUFFERP (glyph->object)
16316 ? 'B'
16317 : (STRINGP (glyph->object)
16318 ? 'S'
16319 : '-')),
16320 glyph->pixel_width,
16321 0,
16322 '.',
16323 glyph->face_id,
16324 glyph->left_box_line_p,
16325 glyph->right_box_line_p);
16326 }
16327 else if (glyph->type == IMAGE_GLYPH)
16328 {
16329 fprintf (stderr,
16330 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16331 glyph - row->glyphs[TEXT_AREA],
16332 'I',
16333 glyph->charpos,
16334 (BUFFERP (glyph->object)
16335 ? 'B'
16336 : (STRINGP (glyph->object)
16337 ? 'S'
16338 : '-')),
16339 glyph->pixel_width,
16340 glyph->u.img_id,
16341 '.',
16342 glyph->face_id,
16343 glyph->left_box_line_p,
16344 glyph->right_box_line_p);
16345 }
16346 else if (glyph->type == COMPOSITE_GLYPH)
16347 {
16348 fprintf (stderr,
16349 " %5d %4c %6d %c %3d 0x%05x",
16350 glyph - row->glyphs[TEXT_AREA],
16351 '+',
16352 glyph->charpos,
16353 (BUFFERP (glyph->object)
16354 ? 'B'
16355 : (STRINGP (glyph->object)
16356 ? 'S'
16357 : '-')),
16358 glyph->pixel_width,
16359 glyph->u.cmp.id);
16360 if (glyph->u.cmp.automatic)
16361 fprintf (stderr,
16362 "[%d-%d]",
16363 glyph->slice.cmp.from, glyph->slice.cmp.to);
16364 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16365 glyph->face_id,
16366 glyph->left_box_line_p,
16367 glyph->right_box_line_p);
16368 }
16369 }
16370
16371
16372 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16373 GLYPHS 0 means don't show glyph contents.
16374 GLYPHS 1 means show glyphs in short form
16375 GLYPHS > 1 means show glyphs in long form. */
16376
16377 void
16378 dump_glyph_row (row, vpos, glyphs)
16379 struct glyph_row *row;
16380 int vpos, glyphs;
16381 {
16382 if (glyphs != 1)
16383 {
16384 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16385 fprintf (stderr, "======================================================================\n");
16386
16387 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16388 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16389 vpos,
16390 MATRIX_ROW_START_CHARPOS (row),
16391 MATRIX_ROW_END_CHARPOS (row),
16392 row->used[TEXT_AREA],
16393 row->contains_overlapping_glyphs_p,
16394 row->enabled_p,
16395 row->truncated_on_left_p,
16396 row->truncated_on_right_p,
16397 row->continued_p,
16398 MATRIX_ROW_CONTINUATION_LINE_P (row),
16399 row->displays_text_p,
16400 row->ends_at_zv_p,
16401 row->fill_line_p,
16402 row->ends_in_middle_of_char_p,
16403 row->starts_in_middle_of_char_p,
16404 row->mouse_face_p,
16405 row->x,
16406 row->y,
16407 row->pixel_width,
16408 row->height,
16409 row->visible_height,
16410 row->ascent,
16411 row->phys_ascent);
16412 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16413 row->end.overlay_string_index,
16414 row->continuation_lines_width);
16415 fprintf (stderr, "%9d %5d\n",
16416 CHARPOS (row->start.string_pos),
16417 CHARPOS (row->end.string_pos));
16418 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16419 row->end.dpvec_index);
16420 }
16421
16422 if (glyphs > 1)
16423 {
16424 int area;
16425
16426 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16427 {
16428 struct glyph *glyph = row->glyphs[area];
16429 struct glyph *glyph_end = glyph + row->used[area];
16430
16431 /* Glyph for a line end in text. */
16432 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16433 ++glyph_end;
16434
16435 if (glyph < glyph_end)
16436 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16437
16438 for (; glyph < glyph_end; ++glyph)
16439 dump_glyph (row, glyph, area);
16440 }
16441 }
16442 else if (glyphs == 1)
16443 {
16444 int area;
16445
16446 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16447 {
16448 char *s = (char *) alloca (row->used[area] + 1);
16449 int i;
16450
16451 for (i = 0; i < row->used[area]; ++i)
16452 {
16453 struct glyph *glyph = row->glyphs[area] + i;
16454 if (glyph->type == CHAR_GLYPH
16455 && glyph->u.ch < 0x80
16456 && glyph->u.ch >= ' ')
16457 s[i] = glyph->u.ch;
16458 else
16459 s[i] = '.';
16460 }
16461
16462 s[i] = '\0';
16463 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16464 }
16465 }
16466 }
16467
16468
16469 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16470 Sdump_glyph_matrix, 0, 1, "p",
16471 doc: /* Dump the current matrix of the selected window to stderr.
16472 Shows contents of glyph row structures. With non-nil
16473 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16474 glyphs in short form, otherwise show glyphs in long form. */)
16475 (Lisp_Object glyphs)
16476 {
16477 struct window *w = XWINDOW (selected_window);
16478 struct buffer *buffer = XBUFFER (w->buffer);
16479
16480 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16481 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16482 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16483 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16484 fprintf (stderr, "=============================================\n");
16485 dump_glyph_matrix (w->current_matrix,
16486 NILP (glyphs) ? 0 : XINT (glyphs));
16487 return Qnil;
16488 }
16489
16490
16491 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16492 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16493 (void)
16494 {
16495 struct frame *f = XFRAME (selected_frame);
16496 dump_glyph_matrix (f->current_matrix, 1);
16497 return Qnil;
16498 }
16499
16500
16501 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16502 doc: /* Dump glyph row ROW to stderr.
16503 GLYPH 0 means don't dump glyphs.
16504 GLYPH 1 means dump glyphs in short form.
16505 GLYPH > 1 or omitted means dump glyphs in long form. */)
16506 (Lisp_Object row, Lisp_Object glyphs)
16507 {
16508 struct glyph_matrix *matrix;
16509 int vpos;
16510
16511 CHECK_NUMBER (row);
16512 matrix = XWINDOW (selected_window)->current_matrix;
16513 vpos = XINT (row);
16514 if (vpos >= 0 && vpos < matrix->nrows)
16515 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16516 vpos,
16517 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16518 return Qnil;
16519 }
16520
16521
16522 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16523 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16524 GLYPH 0 means don't dump glyphs.
16525 GLYPH 1 means dump glyphs in short form.
16526 GLYPH > 1 or omitted means dump glyphs in long form. */)
16527 (Lisp_Object row, Lisp_Object glyphs)
16528 {
16529 struct frame *sf = SELECTED_FRAME ();
16530 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16531 int vpos;
16532
16533 CHECK_NUMBER (row);
16534 vpos = XINT (row);
16535 if (vpos >= 0 && vpos < m->nrows)
16536 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16537 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16538 return Qnil;
16539 }
16540
16541
16542 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16543 doc: /* Toggle tracing of redisplay.
16544 With ARG, turn tracing on if and only if ARG is positive. */)
16545 (Lisp_Object arg)
16546 {
16547 if (NILP (arg))
16548 trace_redisplay_p = !trace_redisplay_p;
16549 else
16550 {
16551 arg = Fprefix_numeric_value (arg);
16552 trace_redisplay_p = XINT (arg) > 0;
16553 }
16554
16555 return Qnil;
16556 }
16557
16558
16559 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16560 doc: /* Like `format', but print result to stderr.
16561 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16562 (int nargs, Lisp_Object *args)
16563 {
16564 Lisp_Object s = Fformat (nargs, args);
16565 fprintf (stderr, "%s", SDATA (s));
16566 return Qnil;
16567 }
16568
16569 #endif /* GLYPH_DEBUG */
16570
16571
16572 \f
16573 /***********************************************************************
16574 Building Desired Matrix Rows
16575 ***********************************************************************/
16576
16577 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16578 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16579
16580 static struct glyph_row *
16581 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16582 {
16583 struct frame *f = XFRAME (WINDOW_FRAME (w));
16584 struct buffer *buffer = XBUFFER (w->buffer);
16585 struct buffer *old = current_buffer;
16586 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16587 int arrow_len = SCHARS (overlay_arrow_string);
16588 const unsigned char *arrow_end = arrow_string + arrow_len;
16589 const unsigned char *p;
16590 struct it it;
16591 int multibyte_p;
16592 int n_glyphs_before;
16593
16594 set_buffer_temp (buffer);
16595 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16596 it.glyph_row->used[TEXT_AREA] = 0;
16597 SET_TEXT_POS (it.position, 0, 0);
16598
16599 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16600 p = arrow_string;
16601 while (p < arrow_end)
16602 {
16603 Lisp_Object face, ilisp;
16604
16605 /* Get the next character. */
16606 if (multibyte_p)
16607 it.c = it.char_to_display = string_char_and_length (p, &it.len);
16608 else
16609 {
16610 it.c = it.char_to_display = *p, it.len = 1;
16611 if (! ASCII_CHAR_P (it.c))
16612 it.char_to_display = BYTE8_TO_CHAR (it.c);
16613 }
16614 p += it.len;
16615
16616 /* Get its face. */
16617 ilisp = make_number (p - arrow_string);
16618 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16619 it.face_id = compute_char_face (f, it.char_to_display, face);
16620
16621 /* Compute its width, get its glyphs. */
16622 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16623 SET_TEXT_POS (it.position, -1, -1);
16624 PRODUCE_GLYPHS (&it);
16625
16626 /* If this character doesn't fit any more in the line, we have
16627 to remove some glyphs. */
16628 if (it.current_x > it.last_visible_x)
16629 {
16630 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16631 break;
16632 }
16633 }
16634
16635 set_buffer_temp (old);
16636 return it.glyph_row;
16637 }
16638
16639
16640 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16641 glyphs are only inserted for terminal frames since we can't really
16642 win with truncation glyphs when partially visible glyphs are
16643 involved. Which glyphs to insert is determined by
16644 produce_special_glyphs. */
16645
16646 static void
16647 insert_left_trunc_glyphs (struct it *it)
16648 {
16649 struct it truncate_it;
16650 struct glyph *from, *end, *to, *toend;
16651
16652 xassert (!FRAME_WINDOW_P (it->f));
16653
16654 /* Get the truncation glyphs. */
16655 truncate_it = *it;
16656 truncate_it.current_x = 0;
16657 truncate_it.face_id = DEFAULT_FACE_ID;
16658 truncate_it.glyph_row = &scratch_glyph_row;
16659 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16660 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16661 truncate_it.object = make_number (0);
16662 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16663
16664 /* Overwrite glyphs from IT with truncation glyphs. */
16665 if (!it->glyph_row->reversed_p)
16666 {
16667 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16668 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16669 to = it->glyph_row->glyphs[TEXT_AREA];
16670 toend = to + it->glyph_row->used[TEXT_AREA];
16671
16672 while (from < end)
16673 *to++ = *from++;
16674
16675 /* There may be padding glyphs left over. Overwrite them too. */
16676 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16677 {
16678 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16679 while (from < end)
16680 *to++ = *from++;
16681 }
16682
16683 if (to > toend)
16684 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16685 }
16686 else
16687 {
16688 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
16689 that back to front. */
16690 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
16691 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16692 toend = it->glyph_row->glyphs[TEXT_AREA];
16693 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
16694
16695 while (from >= end && to >= toend)
16696 *to-- = *from--;
16697 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
16698 {
16699 from =
16700 truncate_it.glyph_row->glyphs[TEXT_AREA]
16701 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16702 while (from >= end && to >= toend)
16703 *to-- = *from--;
16704 }
16705 if (from >= end)
16706 {
16707 /* Need to free some room before prepending additional
16708 glyphs. */
16709 int move_by = from - end + 1;
16710 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
16711 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
16712
16713 for ( ; g >= g0; g--)
16714 g[move_by] = *g;
16715 while (from >= end)
16716 *to-- = *from--;
16717 it->glyph_row->used[TEXT_AREA] += move_by;
16718 }
16719 }
16720 }
16721
16722
16723 /* Compute the pixel height and width of IT->glyph_row.
16724
16725 Most of the time, ascent and height of a display line will be equal
16726 to the max_ascent and max_height values of the display iterator
16727 structure. This is not the case if
16728
16729 1. We hit ZV without displaying anything. In this case, max_ascent
16730 and max_height will be zero.
16731
16732 2. We have some glyphs that don't contribute to the line height.
16733 (The glyph row flag contributes_to_line_height_p is for future
16734 pixmap extensions).
16735
16736 The first case is easily covered by using default values because in
16737 these cases, the line height does not really matter, except that it
16738 must not be zero. */
16739
16740 static void
16741 compute_line_metrics (struct it *it)
16742 {
16743 struct glyph_row *row = it->glyph_row;
16744 int area, i;
16745
16746 if (FRAME_WINDOW_P (it->f))
16747 {
16748 int i, min_y, max_y;
16749
16750 /* The line may consist of one space only, that was added to
16751 place the cursor on it. If so, the row's height hasn't been
16752 computed yet. */
16753 if (row->height == 0)
16754 {
16755 if (it->max_ascent + it->max_descent == 0)
16756 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16757 row->ascent = it->max_ascent;
16758 row->height = it->max_ascent + it->max_descent;
16759 row->phys_ascent = it->max_phys_ascent;
16760 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16761 row->extra_line_spacing = it->max_extra_line_spacing;
16762 }
16763
16764 /* Compute the width of this line. */
16765 row->pixel_width = row->x;
16766 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16767 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16768
16769 xassert (row->pixel_width >= 0);
16770 xassert (row->ascent >= 0 && row->height > 0);
16771
16772 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16773 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16774
16775 /* If first line's physical ascent is larger than its logical
16776 ascent, use the physical ascent, and make the row taller.
16777 This makes accented characters fully visible. */
16778 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16779 && row->phys_ascent > row->ascent)
16780 {
16781 row->height += row->phys_ascent - row->ascent;
16782 row->ascent = row->phys_ascent;
16783 }
16784
16785 /* Compute how much of the line is visible. */
16786 row->visible_height = row->height;
16787
16788 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16789 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16790
16791 if (row->y < min_y)
16792 row->visible_height -= min_y - row->y;
16793 if (row->y + row->height > max_y)
16794 row->visible_height -= row->y + row->height - max_y;
16795 }
16796 else
16797 {
16798 row->pixel_width = row->used[TEXT_AREA];
16799 if (row->continued_p)
16800 row->pixel_width -= it->continuation_pixel_width;
16801 else if (row->truncated_on_right_p)
16802 row->pixel_width -= it->truncation_pixel_width;
16803 row->ascent = row->phys_ascent = 0;
16804 row->height = row->phys_height = row->visible_height = 1;
16805 row->extra_line_spacing = 0;
16806 }
16807
16808 /* Compute a hash code for this row. */
16809 row->hash = 0;
16810 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16811 for (i = 0; i < row->used[area]; ++i)
16812 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16813 + row->glyphs[area][i].u.val
16814 + row->glyphs[area][i].face_id
16815 + row->glyphs[area][i].padding_p
16816 + (row->glyphs[area][i].type << 2));
16817
16818 it->max_ascent = it->max_descent = 0;
16819 it->max_phys_ascent = it->max_phys_descent = 0;
16820 }
16821
16822
16823 /* Append one space to the glyph row of iterator IT if doing a
16824 window-based redisplay. The space has the same face as
16825 IT->face_id. Value is non-zero if a space was added.
16826
16827 This function is called to make sure that there is always one glyph
16828 at the end of a glyph row that the cursor can be set on under
16829 window-systems. (If there weren't such a glyph we would not know
16830 how wide and tall a box cursor should be displayed).
16831
16832 At the same time this space let's a nicely handle clearing to the
16833 end of the line if the row ends in italic text. */
16834
16835 static int
16836 append_space_for_newline (struct it *it, int default_face_p)
16837 {
16838 if (FRAME_WINDOW_P (it->f))
16839 {
16840 int n = it->glyph_row->used[TEXT_AREA];
16841
16842 if (it->glyph_row->glyphs[TEXT_AREA] + n
16843 < it->glyph_row->glyphs[1 + TEXT_AREA])
16844 {
16845 /* Save some values that must not be changed.
16846 Must save IT->c and IT->len because otherwise
16847 ITERATOR_AT_END_P wouldn't work anymore after
16848 append_space_for_newline has been called. */
16849 enum display_element_type saved_what = it->what;
16850 int saved_c = it->c, saved_len = it->len;
16851 int saved_char_to_display = it->char_to_display;
16852 int saved_x = it->current_x;
16853 int saved_face_id = it->face_id;
16854 struct text_pos saved_pos;
16855 Lisp_Object saved_object;
16856 struct face *face;
16857
16858 saved_object = it->object;
16859 saved_pos = it->position;
16860
16861 it->what = IT_CHARACTER;
16862 memset (&it->position, 0, sizeof it->position);
16863 it->object = make_number (0);
16864 it->c = it->char_to_display = ' ';
16865 it->len = 1;
16866
16867 if (default_face_p)
16868 it->face_id = DEFAULT_FACE_ID;
16869 else if (it->face_before_selective_p)
16870 it->face_id = it->saved_face_id;
16871 face = FACE_FROM_ID (it->f, it->face_id);
16872 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16873
16874 PRODUCE_GLYPHS (it);
16875
16876 it->override_ascent = -1;
16877 it->constrain_row_ascent_descent_p = 0;
16878 it->current_x = saved_x;
16879 it->object = saved_object;
16880 it->position = saved_pos;
16881 it->what = saved_what;
16882 it->face_id = saved_face_id;
16883 it->len = saved_len;
16884 it->c = saved_c;
16885 it->char_to_display = saved_char_to_display;
16886 return 1;
16887 }
16888 }
16889
16890 return 0;
16891 }
16892
16893
16894 /* Extend the face of the last glyph in the text area of IT->glyph_row
16895 to the end of the display line. Called from display_line. If the
16896 glyph row is empty, add a space glyph to it so that we know the
16897 face to draw. Set the glyph row flag fill_line_p. If the glyph
16898 row is R2L, prepend a stretch glyph to cover the empty space to the
16899 left of the leftmost glyph. */
16900
16901 static void
16902 extend_face_to_end_of_line (struct it *it)
16903 {
16904 struct face *face;
16905 struct frame *f = it->f;
16906
16907 /* If line is already filled, do nothing. Non window-system frames
16908 get a grace of one more ``pixel'' because their characters are
16909 1-``pixel'' wide, so they hit the equality too early. This grace
16910 is needed only for R2L rows that are not continued, to produce
16911 one extra blank where we could display the cursor. */
16912 if (it->current_x >= it->last_visible_x
16913 + (!FRAME_WINDOW_P (f)
16914 && it->glyph_row->reversed_p
16915 && !it->glyph_row->continued_p))
16916 return;
16917
16918 /* Face extension extends the background and box of IT->face_id
16919 to the end of the line. If the background equals the background
16920 of the frame, we don't have to do anything. */
16921 if (it->face_before_selective_p)
16922 face = FACE_FROM_ID (f, it->saved_face_id);
16923 else
16924 face = FACE_FROM_ID (f, it->face_id);
16925
16926 if (FRAME_WINDOW_P (f)
16927 && it->glyph_row->displays_text_p
16928 && face->box == FACE_NO_BOX
16929 && face->background == FRAME_BACKGROUND_PIXEL (f)
16930 && !face->stipple
16931 && !it->glyph_row->reversed_p)
16932 return;
16933
16934 /* Set the glyph row flag indicating that the face of the last glyph
16935 in the text area has to be drawn to the end of the text area. */
16936 it->glyph_row->fill_line_p = 1;
16937
16938 /* If current character of IT is not ASCII, make sure we have the
16939 ASCII face. This will be automatically undone the next time
16940 get_next_display_element returns a multibyte character. Note
16941 that the character will always be single byte in unibyte
16942 text. */
16943 if (!ASCII_CHAR_P (it->c))
16944 {
16945 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16946 }
16947
16948 if (FRAME_WINDOW_P (f))
16949 {
16950 /* If the row is empty, add a space with the current face of IT,
16951 so that we know which face to draw. */
16952 if (it->glyph_row->used[TEXT_AREA] == 0)
16953 {
16954 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16955 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16956 it->glyph_row->used[TEXT_AREA] = 1;
16957 }
16958 #ifdef HAVE_WINDOW_SYSTEM
16959 if (it->glyph_row->reversed_p)
16960 {
16961 /* Prepend a stretch glyph to the row, such that the
16962 rightmost glyph will be drawn flushed all the way to the
16963 right margin of the window. The stretch glyph that will
16964 occupy the empty space, if any, to the left of the
16965 glyphs. */
16966 struct font *font = face->font ? face->font : FRAME_FONT (f);
16967 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
16968 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
16969 struct glyph *g;
16970 int row_width, stretch_ascent, stretch_width;
16971 struct text_pos saved_pos;
16972 int saved_face_id, saved_avoid_cursor;
16973
16974 for (row_width = 0, g = row_start; g < row_end; g++)
16975 row_width += g->pixel_width;
16976 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
16977 if (stretch_width > 0)
16978 {
16979 stretch_ascent =
16980 (((it->ascent + it->descent)
16981 * FONT_BASE (font)) / FONT_HEIGHT (font));
16982 saved_pos = it->position;
16983 memset (&it->position, 0, sizeof it->position);
16984 saved_avoid_cursor = it->avoid_cursor_p;
16985 it->avoid_cursor_p = 1;
16986 saved_face_id = it->face_id;
16987 /* The last row's stretch glyph should get the default
16988 face, to avoid painting the rest of the window with
16989 the region face, if the region ends at ZV. */
16990 if (it->glyph_row->ends_at_zv_p)
16991 it->face_id = DEFAULT_FACE_ID;
16992 else
16993 it->face_id = face->id;
16994 append_stretch_glyph (it, make_number (0), stretch_width,
16995 it->ascent + it->descent, stretch_ascent);
16996 it->position = saved_pos;
16997 it->avoid_cursor_p = saved_avoid_cursor;
16998 it->face_id = saved_face_id;
16999 }
17000 }
17001 #endif /* HAVE_WINDOW_SYSTEM */
17002 }
17003 else
17004 {
17005 /* Save some values that must not be changed. */
17006 int saved_x = it->current_x;
17007 struct text_pos saved_pos;
17008 Lisp_Object saved_object;
17009 enum display_element_type saved_what = it->what;
17010 int saved_face_id = it->face_id;
17011
17012 saved_object = it->object;
17013 saved_pos = it->position;
17014
17015 it->what = IT_CHARACTER;
17016 memset (&it->position, 0, sizeof it->position);
17017 it->object = make_number (0);
17018 it->c = it->char_to_display = ' ';
17019 it->len = 1;
17020 /* The last row's blank glyphs should get the default face, to
17021 avoid painting the rest of the window with the region face,
17022 if the region ends at ZV. */
17023 if (it->glyph_row->ends_at_zv_p)
17024 it->face_id = DEFAULT_FACE_ID;
17025 else
17026 it->face_id = face->id;
17027
17028 PRODUCE_GLYPHS (it);
17029
17030 while (it->current_x <= it->last_visible_x)
17031 PRODUCE_GLYPHS (it);
17032
17033 /* Don't count these blanks really. It would let us insert a left
17034 truncation glyph below and make us set the cursor on them, maybe. */
17035 it->current_x = saved_x;
17036 it->object = saved_object;
17037 it->position = saved_pos;
17038 it->what = saved_what;
17039 it->face_id = saved_face_id;
17040 }
17041 }
17042
17043
17044 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17045 trailing whitespace. */
17046
17047 static int
17048 trailing_whitespace_p (EMACS_INT charpos)
17049 {
17050 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17051 int c = 0;
17052
17053 while (bytepos < ZV_BYTE
17054 && (c = FETCH_CHAR (bytepos),
17055 c == ' ' || c == '\t'))
17056 ++bytepos;
17057
17058 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17059 {
17060 if (bytepos != PT_BYTE)
17061 return 1;
17062 }
17063 return 0;
17064 }
17065
17066
17067 /* Highlight trailing whitespace, if any, in ROW. */
17068
17069 void
17070 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17071 {
17072 int used = row->used[TEXT_AREA];
17073
17074 if (used)
17075 {
17076 struct glyph *start = row->glyphs[TEXT_AREA];
17077 struct glyph *glyph = start + used - 1;
17078
17079 if (row->reversed_p)
17080 {
17081 /* Right-to-left rows need to be processed in the opposite
17082 direction, so swap the edge pointers. */
17083 glyph = start;
17084 start = row->glyphs[TEXT_AREA] + used - 1;
17085 }
17086
17087 /* Skip over glyphs inserted to display the cursor at the
17088 end of a line, for extending the face of the last glyph
17089 to the end of the line on terminals, and for truncation
17090 and continuation glyphs. */
17091 if (!row->reversed_p)
17092 {
17093 while (glyph >= start
17094 && glyph->type == CHAR_GLYPH
17095 && INTEGERP (glyph->object))
17096 --glyph;
17097 }
17098 else
17099 {
17100 while (glyph <= start
17101 && glyph->type == CHAR_GLYPH
17102 && INTEGERP (glyph->object))
17103 ++glyph;
17104 }
17105
17106 /* If last glyph is a space or stretch, and it's trailing
17107 whitespace, set the face of all trailing whitespace glyphs in
17108 IT->glyph_row to `trailing-whitespace'. */
17109 if ((row->reversed_p ? glyph <= start : glyph >= start)
17110 && BUFFERP (glyph->object)
17111 && (glyph->type == STRETCH_GLYPH
17112 || (glyph->type == CHAR_GLYPH
17113 && glyph->u.ch == ' '))
17114 && trailing_whitespace_p (glyph->charpos))
17115 {
17116 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17117 if (face_id < 0)
17118 return;
17119
17120 if (!row->reversed_p)
17121 {
17122 while (glyph >= start
17123 && BUFFERP (glyph->object)
17124 && (glyph->type == STRETCH_GLYPH
17125 || (glyph->type == CHAR_GLYPH
17126 && glyph->u.ch == ' ')))
17127 (glyph--)->face_id = face_id;
17128 }
17129 else
17130 {
17131 while (glyph <= start
17132 && BUFFERP (glyph->object)
17133 && (glyph->type == STRETCH_GLYPH
17134 || (glyph->type == CHAR_GLYPH
17135 && glyph->u.ch == ' ')))
17136 (glyph++)->face_id = face_id;
17137 }
17138 }
17139 }
17140 }
17141
17142
17143 /* Value is non-zero if glyph row ROW in window W should be
17144 used to hold the cursor. */
17145
17146 static int
17147 cursor_row_p (struct window *w, struct glyph_row *row)
17148 {
17149 int cursor_row_p = 1;
17150
17151 if (PT == CHARPOS (row->end.pos))
17152 {
17153 /* Suppose the row ends on a string.
17154 Unless the row is continued, that means it ends on a newline
17155 in the string. If it's anything other than a display string
17156 (e.g. a before-string from an overlay), we don't want the
17157 cursor there. (This heuristic seems to give the optimal
17158 behavior for the various types of multi-line strings.) */
17159 if (CHARPOS (row->end.string_pos) >= 0)
17160 {
17161 if (row->continued_p)
17162 cursor_row_p = 1;
17163 else
17164 {
17165 /* Check for `display' property. */
17166 struct glyph *beg = row->glyphs[TEXT_AREA];
17167 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17168 struct glyph *glyph;
17169
17170 cursor_row_p = 0;
17171 for (glyph = end; glyph >= beg; --glyph)
17172 if (STRINGP (glyph->object))
17173 {
17174 Lisp_Object prop
17175 = Fget_char_property (make_number (PT),
17176 Qdisplay, Qnil);
17177 cursor_row_p =
17178 (!NILP (prop)
17179 && display_prop_string_p (prop, glyph->object));
17180 break;
17181 }
17182 }
17183 }
17184 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17185 {
17186 /* If the row ends in middle of a real character,
17187 and the line is continued, we want the cursor here.
17188 That's because CHARPOS (ROW->end.pos) would equal
17189 PT if PT is before the character. */
17190 if (!row->ends_in_ellipsis_p)
17191 cursor_row_p = row->continued_p;
17192 else
17193 /* If the row ends in an ellipsis, then
17194 CHARPOS (ROW->end.pos) will equal point after the
17195 invisible text. We want that position to be displayed
17196 after the ellipsis. */
17197 cursor_row_p = 0;
17198 }
17199 /* If the row ends at ZV, display the cursor at the end of that
17200 row instead of at the start of the row below. */
17201 else if (row->ends_at_zv_p)
17202 cursor_row_p = 1;
17203 else
17204 cursor_row_p = 0;
17205 }
17206
17207 return cursor_row_p;
17208 }
17209
17210 \f
17211
17212 /* Push the display property PROP so that it will be rendered at the
17213 current position in IT. Return 1 if PROP was successfully pushed,
17214 0 otherwise. */
17215
17216 static int
17217 push_display_prop (struct it *it, Lisp_Object prop)
17218 {
17219 push_it (it);
17220
17221 if (STRINGP (prop))
17222 {
17223 if (SCHARS (prop) == 0)
17224 {
17225 pop_it (it);
17226 return 0;
17227 }
17228
17229 it->string = prop;
17230 it->multibyte_p = STRING_MULTIBYTE (it->string);
17231 it->current.overlay_string_index = -1;
17232 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17233 it->end_charpos = it->string_nchars = SCHARS (it->string);
17234 it->method = GET_FROM_STRING;
17235 it->stop_charpos = 0;
17236 }
17237 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17238 {
17239 it->method = GET_FROM_STRETCH;
17240 it->object = prop;
17241 }
17242 #ifdef HAVE_WINDOW_SYSTEM
17243 else if (IMAGEP (prop))
17244 {
17245 it->what = IT_IMAGE;
17246 it->image_id = lookup_image (it->f, prop);
17247 it->method = GET_FROM_IMAGE;
17248 }
17249 #endif /* HAVE_WINDOW_SYSTEM */
17250 else
17251 {
17252 pop_it (it); /* bogus display property, give up */
17253 return 0;
17254 }
17255
17256 return 1;
17257 }
17258
17259 /* Return the character-property PROP at the current position in IT. */
17260
17261 static Lisp_Object
17262 get_it_property (struct it *it, Lisp_Object prop)
17263 {
17264 Lisp_Object position;
17265
17266 if (STRINGP (it->object))
17267 position = make_number (IT_STRING_CHARPOS (*it));
17268 else if (BUFFERP (it->object))
17269 position = make_number (IT_CHARPOS (*it));
17270 else
17271 return Qnil;
17272
17273 return Fget_char_property (position, prop, it->object);
17274 }
17275
17276 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17277
17278 static void
17279 handle_line_prefix (struct it *it)
17280 {
17281 Lisp_Object prefix;
17282 if (it->continuation_lines_width > 0)
17283 {
17284 prefix = get_it_property (it, Qwrap_prefix);
17285 if (NILP (prefix))
17286 prefix = Vwrap_prefix;
17287 }
17288 else
17289 {
17290 prefix = get_it_property (it, Qline_prefix);
17291 if (NILP (prefix))
17292 prefix = Vline_prefix;
17293 }
17294 if (! NILP (prefix) && push_display_prop (it, prefix))
17295 {
17296 /* If the prefix is wider than the window, and we try to wrap
17297 it, it would acquire its own wrap prefix, and so on till the
17298 iterator stack overflows. So, don't wrap the prefix. */
17299 it->line_wrap = TRUNCATE;
17300 it->avoid_cursor_p = 1;
17301 }
17302 }
17303
17304 \f
17305
17306 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17307 only for R2L lines from display_line, when it decides that too many
17308 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be
17309 continued. */
17310 static void
17311 unproduce_glyphs (struct it *it, int n)
17312 {
17313 struct glyph *glyph, *end;
17314
17315 xassert (it->glyph_row);
17316 xassert (it->glyph_row->reversed_p);
17317 xassert (it->area == TEXT_AREA);
17318 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17319
17320 if (n > it->glyph_row->used[TEXT_AREA])
17321 n = it->glyph_row->used[TEXT_AREA];
17322 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17323 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17324 for ( ; glyph < end; glyph++)
17325 glyph[-n] = *glyph;
17326 }
17327
17328 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17329 and ROW->maxpos. */
17330 static void
17331 find_row_edges (struct it *it, struct glyph_row *row,
17332 EMACS_INT min_pos, EMACS_INT min_bpos,
17333 EMACS_INT max_pos, EMACS_INT max_bpos)
17334 {
17335 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17336 lines' rows is implemented for bidi-reordered rows. */
17337
17338 /* ROW->minpos is the value of min_pos, the minimal buffer position
17339 we have in ROW. */
17340 if (min_pos <= ZV)
17341 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17342 else
17343 {
17344 /* We didn't find _any_ valid buffer positions in any of the
17345 glyphs, so we must trust the iterator's computed
17346 positions. */
17347 row->minpos = row->start.pos;
17348 max_pos = CHARPOS (it->current.pos);
17349 max_bpos = BYTEPOS (it->current.pos);
17350 }
17351
17352 if (!max_pos)
17353 abort ();
17354
17355 /* Here are the various use-cases for ending the row, and the
17356 corresponding values for ROW->maxpos:
17357
17358 Line ends in a newline from buffer eol_pos + 1
17359 Line is continued from buffer max_pos + 1
17360 Line is truncated on right it->current.pos
17361 Line ends in a newline from string max_pos
17362 Line is continued from string max_pos
17363 Line is continued from display vector max_pos
17364 Line is entirely from a string min_pos == max_pos
17365 Line is entirely from a display vector min_pos == max_pos
17366 Line that ends at ZV ZV
17367
17368 If you discover other use-cases, please add them here as
17369 appropriate. */
17370 if (row->ends_at_zv_p)
17371 row->maxpos = it->current.pos;
17372 else if (row->used[TEXT_AREA])
17373 {
17374 if (row->ends_in_newline_from_string_p)
17375 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17376 else if (CHARPOS (it->eol_pos) > 0)
17377 SET_TEXT_POS (row->maxpos,
17378 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17379 else if (row->continued_p)
17380 {
17381 /* If max_pos is different from IT's current position, it
17382 means IT->method does not belong to the display element
17383 at max_pos. However, it also means that the display
17384 element at max_pos was displayed in its entirety on this
17385 line, which is equivalent to saying that the next line
17386 starts at the next buffer position. */
17387 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17388 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17389 else
17390 {
17391 INC_BOTH (max_pos, max_bpos);
17392 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17393 }
17394 }
17395 else if (row->truncated_on_right_p)
17396 /* display_line already called reseat_at_next_visible_line_start,
17397 which puts the iterator at the beginning of the next line, in
17398 the logical order. */
17399 row->maxpos = it->current.pos;
17400 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17401 /* A line that is entirely from a string/image/stretch... */
17402 row->maxpos = row->minpos;
17403 else
17404 abort ();
17405 }
17406 else
17407 row->maxpos = it->current.pos;
17408 }
17409
17410 /* Construct the glyph row IT->glyph_row in the desired matrix of
17411 IT->w from text at the current position of IT. See dispextern.h
17412 for an overview of struct it. Value is non-zero if
17413 IT->glyph_row displays text, as opposed to a line displaying ZV
17414 only. */
17415
17416 static int
17417 display_line (struct it *it)
17418 {
17419 struct glyph_row *row = it->glyph_row;
17420 Lisp_Object overlay_arrow_string;
17421 struct it wrap_it;
17422 int may_wrap = 0, wrap_x;
17423 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17424 int wrap_row_phys_ascent, wrap_row_phys_height;
17425 int wrap_row_extra_line_spacing;
17426 EMACS_INT wrap_row_min_pos, wrap_row_min_bpos;
17427 EMACS_INT wrap_row_max_pos, wrap_row_max_bpos;
17428 int cvpos;
17429 EMACS_INT min_pos = ZV + 1, min_bpos, max_pos = 0, max_bpos;
17430
17431 /* We always start displaying at hpos zero even if hscrolled. */
17432 xassert (it->hpos == 0 && it->current_x == 0);
17433
17434 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17435 >= it->w->desired_matrix->nrows)
17436 {
17437 it->w->nrows_scale_factor++;
17438 fonts_changed_p = 1;
17439 return 0;
17440 }
17441
17442 /* Is IT->w showing the region? */
17443 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17444
17445 /* Clear the result glyph row and enable it. */
17446 prepare_desired_row (row);
17447
17448 row->y = it->current_y;
17449 row->start = it->start;
17450 row->continuation_lines_width = it->continuation_lines_width;
17451 row->displays_text_p = 1;
17452 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17453 it->starts_in_middle_of_char_p = 0;
17454
17455 /* Arrange the overlays nicely for our purposes. Usually, we call
17456 display_line on only one line at a time, in which case this
17457 can't really hurt too much, or we call it on lines which appear
17458 one after another in the buffer, in which case all calls to
17459 recenter_overlay_lists but the first will be pretty cheap. */
17460 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17461
17462 /* Move over display elements that are not visible because we are
17463 hscrolled. This may stop at an x-position < IT->first_visible_x
17464 if the first glyph is partially visible or if we hit a line end. */
17465 if (it->current_x < it->first_visible_x)
17466 {
17467 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17468 MOVE_TO_POS | MOVE_TO_X);
17469 }
17470 else
17471 {
17472 /* We only do this when not calling `move_it_in_display_line_to'
17473 above, because move_it_in_display_line_to calls
17474 handle_line_prefix itself. */
17475 handle_line_prefix (it);
17476 }
17477
17478 /* Get the initial row height. This is either the height of the
17479 text hscrolled, if there is any, or zero. */
17480 row->ascent = it->max_ascent;
17481 row->height = it->max_ascent + it->max_descent;
17482 row->phys_ascent = it->max_phys_ascent;
17483 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17484 row->extra_line_spacing = it->max_extra_line_spacing;
17485
17486 /* Utility macro to record max and min buffer positions seen until now. */
17487 #define RECORD_MAX_MIN_POS(IT) \
17488 do \
17489 { \
17490 if (IT_CHARPOS (*(IT)) < min_pos) \
17491 { \
17492 min_pos = IT_CHARPOS (*(IT)); \
17493 min_bpos = IT_BYTEPOS (*(IT)); \
17494 } \
17495 if (IT_CHARPOS (*(IT)) > max_pos) \
17496 { \
17497 max_pos = IT_CHARPOS (*(IT)); \
17498 max_bpos = IT_BYTEPOS (*(IT)); \
17499 } \
17500 } \
17501 while (0)
17502
17503 /* Loop generating characters. The loop is left with IT on the next
17504 character to display. */
17505 while (1)
17506 {
17507 int n_glyphs_before, hpos_before, x_before;
17508 int x, i, nglyphs;
17509 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17510
17511 /* Retrieve the next thing to display. Value is zero if end of
17512 buffer reached. */
17513 if (!get_next_display_element (it))
17514 {
17515 /* Maybe add a space at the end of this line that is used to
17516 display the cursor there under X. Set the charpos of the
17517 first glyph of blank lines not corresponding to any text
17518 to -1. */
17519 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17520 row->exact_window_width_line_p = 1;
17521 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17522 || row->used[TEXT_AREA] == 0)
17523 {
17524 row->glyphs[TEXT_AREA]->charpos = -1;
17525 row->displays_text_p = 0;
17526
17527 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17528 && (!MINI_WINDOW_P (it->w)
17529 || (minibuf_level && EQ (it->window, minibuf_window))))
17530 row->indicate_empty_line_p = 1;
17531 }
17532
17533 it->continuation_lines_width = 0;
17534 row->ends_at_zv_p = 1;
17535 /* A row that displays right-to-left text must always have
17536 its last face extended all the way to the end of line,
17537 even if this row ends in ZV, because we still write to
17538 the screen left to right. */
17539 if (row->reversed_p)
17540 extend_face_to_end_of_line (it);
17541 break;
17542 }
17543
17544 /* Now, get the metrics of what we want to display. This also
17545 generates glyphs in `row' (which is IT->glyph_row). */
17546 n_glyphs_before = row->used[TEXT_AREA];
17547 x = it->current_x;
17548
17549 /* Remember the line height so far in case the next element doesn't
17550 fit on the line. */
17551 if (it->line_wrap != TRUNCATE)
17552 {
17553 ascent = it->max_ascent;
17554 descent = it->max_descent;
17555 phys_ascent = it->max_phys_ascent;
17556 phys_descent = it->max_phys_descent;
17557
17558 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17559 {
17560 if (IT_DISPLAYING_WHITESPACE (it))
17561 may_wrap = 1;
17562 else if (may_wrap)
17563 {
17564 wrap_it = *it;
17565 wrap_x = x;
17566 wrap_row_used = row->used[TEXT_AREA];
17567 wrap_row_ascent = row->ascent;
17568 wrap_row_height = row->height;
17569 wrap_row_phys_ascent = row->phys_ascent;
17570 wrap_row_phys_height = row->phys_height;
17571 wrap_row_extra_line_spacing = row->extra_line_spacing;
17572 wrap_row_min_pos = min_pos;
17573 wrap_row_min_bpos = min_bpos;
17574 wrap_row_max_pos = max_pos;
17575 wrap_row_max_bpos = max_bpos;
17576 may_wrap = 0;
17577 }
17578 }
17579 }
17580
17581 PRODUCE_GLYPHS (it);
17582
17583 /* If this display element was in marginal areas, continue with
17584 the next one. */
17585 if (it->area != TEXT_AREA)
17586 {
17587 row->ascent = max (row->ascent, it->max_ascent);
17588 row->height = max (row->height, it->max_ascent + it->max_descent);
17589 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17590 row->phys_height = max (row->phys_height,
17591 it->max_phys_ascent + it->max_phys_descent);
17592 row->extra_line_spacing = max (row->extra_line_spacing,
17593 it->max_extra_line_spacing);
17594 set_iterator_to_next (it, 1);
17595 continue;
17596 }
17597
17598 /* Does the display element fit on the line? If we truncate
17599 lines, we should draw past the right edge of the window. If
17600 we don't truncate, we want to stop so that we can display the
17601 continuation glyph before the right margin. If lines are
17602 continued, there are two possible strategies for characters
17603 resulting in more than 1 glyph (e.g. tabs): Display as many
17604 glyphs as possible in this line and leave the rest for the
17605 continuation line, or display the whole element in the next
17606 line. Original redisplay did the former, so we do it also. */
17607 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17608 hpos_before = it->hpos;
17609 x_before = x;
17610
17611 if (/* Not a newline. */
17612 nglyphs > 0
17613 /* Glyphs produced fit entirely in the line. */
17614 && it->current_x < it->last_visible_x)
17615 {
17616 it->hpos += nglyphs;
17617 row->ascent = max (row->ascent, it->max_ascent);
17618 row->height = max (row->height, it->max_ascent + it->max_descent);
17619 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17620 row->phys_height = max (row->phys_height,
17621 it->max_phys_ascent + it->max_phys_descent);
17622 row->extra_line_spacing = max (row->extra_line_spacing,
17623 it->max_extra_line_spacing);
17624 if (it->current_x - it->pixel_width < it->first_visible_x)
17625 row->x = x - it->first_visible_x;
17626 /* Record the maximum and minimum buffer positions seen so
17627 far in glyphs that will be displayed by this row. */
17628 if (it->bidi_p)
17629 RECORD_MAX_MIN_POS (it);
17630 }
17631 else
17632 {
17633 int new_x;
17634 struct glyph *glyph;
17635
17636 for (i = 0; i < nglyphs; ++i, x = new_x)
17637 {
17638 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17639 new_x = x + glyph->pixel_width;
17640
17641 if (/* Lines are continued. */
17642 it->line_wrap != TRUNCATE
17643 && (/* Glyph doesn't fit on the line. */
17644 new_x > it->last_visible_x
17645 /* Or it fits exactly on a window system frame. */
17646 || (new_x == it->last_visible_x
17647 && FRAME_WINDOW_P (it->f))))
17648 {
17649 /* End of a continued line. */
17650
17651 if (it->hpos == 0
17652 || (new_x == it->last_visible_x
17653 && FRAME_WINDOW_P (it->f)))
17654 {
17655 /* Current glyph is the only one on the line or
17656 fits exactly on the line. We must continue
17657 the line because we can't draw the cursor
17658 after the glyph. */
17659 row->continued_p = 1;
17660 it->current_x = new_x;
17661 it->continuation_lines_width += new_x;
17662 ++it->hpos;
17663 /* Record the maximum and minimum buffer
17664 positions seen so far in glyphs that will be
17665 displayed by this row. */
17666 if (it->bidi_p)
17667 RECORD_MAX_MIN_POS (it);
17668 if (i == nglyphs - 1)
17669 {
17670 /* If line-wrap is on, check if a previous
17671 wrap point was found. */
17672 if (wrap_row_used > 0
17673 /* Even if there is a previous wrap
17674 point, continue the line here as
17675 usual, if (i) the previous character
17676 was a space or tab AND (ii) the
17677 current character is not. */
17678 && (!may_wrap
17679 || IT_DISPLAYING_WHITESPACE (it)))
17680 goto back_to_wrap;
17681
17682 set_iterator_to_next (it, 1);
17683 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17684 {
17685 if (!get_next_display_element (it))
17686 {
17687 row->exact_window_width_line_p = 1;
17688 it->continuation_lines_width = 0;
17689 row->continued_p = 0;
17690 row->ends_at_zv_p = 1;
17691 }
17692 else if (ITERATOR_AT_END_OF_LINE_P (it))
17693 {
17694 row->continued_p = 0;
17695 row->exact_window_width_line_p = 1;
17696 }
17697 }
17698 }
17699 }
17700 else if (CHAR_GLYPH_PADDING_P (*glyph)
17701 && !FRAME_WINDOW_P (it->f))
17702 {
17703 /* A padding glyph that doesn't fit on this line.
17704 This means the whole character doesn't fit
17705 on the line. */
17706 if (row->reversed_p)
17707 unproduce_glyphs (it, row->used[TEXT_AREA]
17708 - n_glyphs_before);
17709 row->used[TEXT_AREA] = n_glyphs_before;
17710
17711 /* Fill the rest of the row with continuation
17712 glyphs like in 20.x. */
17713 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17714 < row->glyphs[1 + TEXT_AREA])
17715 produce_special_glyphs (it, IT_CONTINUATION);
17716
17717 row->continued_p = 1;
17718 it->current_x = x_before;
17719 it->continuation_lines_width += x_before;
17720
17721 /* Restore the height to what it was before the
17722 element not fitting on the line. */
17723 it->max_ascent = ascent;
17724 it->max_descent = descent;
17725 it->max_phys_ascent = phys_ascent;
17726 it->max_phys_descent = phys_descent;
17727 }
17728 else if (wrap_row_used > 0)
17729 {
17730 back_to_wrap:
17731 if (row->reversed_p)
17732 unproduce_glyphs (it,
17733 row->used[TEXT_AREA] - wrap_row_used);
17734 *it = wrap_it;
17735 it->continuation_lines_width += wrap_x;
17736 row->used[TEXT_AREA] = wrap_row_used;
17737 row->ascent = wrap_row_ascent;
17738 row->height = wrap_row_height;
17739 row->phys_ascent = wrap_row_phys_ascent;
17740 row->phys_height = wrap_row_phys_height;
17741 row->extra_line_spacing = wrap_row_extra_line_spacing;
17742 min_pos = wrap_row_min_pos;
17743 min_bpos = wrap_row_min_bpos;
17744 max_pos = wrap_row_max_pos;
17745 max_bpos = wrap_row_max_bpos;
17746 row->continued_p = 1;
17747 row->ends_at_zv_p = 0;
17748 row->exact_window_width_line_p = 0;
17749 it->continuation_lines_width += x;
17750
17751 /* Make sure that a non-default face is extended
17752 up to the right margin of the window. */
17753 extend_face_to_end_of_line (it);
17754 }
17755 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17756 {
17757 /* A TAB that extends past the right edge of the
17758 window. This produces a single glyph on
17759 window system frames. We leave the glyph in
17760 this row and let it fill the row, but don't
17761 consume the TAB. */
17762 it->continuation_lines_width += it->last_visible_x;
17763 row->ends_in_middle_of_char_p = 1;
17764 row->continued_p = 1;
17765 glyph->pixel_width = it->last_visible_x - x;
17766 it->starts_in_middle_of_char_p = 1;
17767 }
17768 else
17769 {
17770 /* Something other than a TAB that draws past
17771 the right edge of the window. Restore
17772 positions to values before the element. */
17773 if (row->reversed_p)
17774 unproduce_glyphs (it, row->used[TEXT_AREA]
17775 - (n_glyphs_before + i));
17776 row->used[TEXT_AREA] = n_glyphs_before + i;
17777
17778 /* Display continuation glyphs. */
17779 if (!FRAME_WINDOW_P (it->f))
17780 produce_special_glyphs (it, IT_CONTINUATION);
17781 row->continued_p = 1;
17782
17783 it->current_x = x_before;
17784 it->continuation_lines_width += x;
17785 extend_face_to_end_of_line (it);
17786
17787 if (nglyphs > 1 && i > 0)
17788 {
17789 row->ends_in_middle_of_char_p = 1;
17790 it->starts_in_middle_of_char_p = 1;
17791 }
17792
17793 /* Restore the height to what it was before the
17794 element not fitting on the line. */
17795 it->max_ascent = ascent;
17796 it->max_descent = descent;
17797 it->max_phys_ascent = phys_ascent;
17798 it->max_phys_descent = phys_descent;
17799 }
17800
17801 break;
17802 }
17803 else if (new_x > it->first_visible_x)
17804 {
17805 /* Increment number of glyphs actually displayed. */
17806 ++it->hpos;
17807
17808 /* Record the maximum and minimum buffer positions
17809 seen so far in glyphs that will be displayed by
17810 this row. */
17811 if (it->bidi_p)
17812 RECORD_MAX_MIN_POS (it);
17813
17814 if (x < it->first_visible_x)
17815 /* Glyph is partially visible, i.e. row starts at
17816 negative X position. */
17817 row->x = x - it->first_visible_x;
17818 }
17819 else
17820 {
17821 /* Glyph is completely off the left margin of the
17822 window. This should not happen because of the
17823 move_it_in_display_line at the start of this
17824 function, unless the text display area of the
17825 window is empty. */
17826 xassert (it->first_visible_x <= it->last_visible_x);
17827 }
17828 }
17829
17830 row->ascent = max (row->ascent, it->max_ascent);
17831 row->height = max (row->height, it->max_ascent + it->max_descent);
17832 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17833 row->phys_height = max (row->phys_height,
17834 it->max_phys_ascent + it->max_phys_descent);
17835 row->extra_line_spacing = max (row->extra_line_spacing,
17836 it->max_extra_line_spacing);
17837
17838 /* End of this display line if row is continued. */
17839 if (row->continued_p || row->ends_at_zv_p)
17840 break;
17841 }
17842
17843 at_end_of_line:
17844 /* Is this a line end? If yes, we're also done, after making
17845 sure that a non-default face is extended up to the right
17846 margin of the window. */
17847 if (ITERATOR_AT_END_OF_LINE_P (it))
17848 {
17849 int used_before = row->used[TEXT_AREA];
17850
17851 row->ends_in_newline_from_string_p = STRINGP (it->object);
17852
17853 /* Add a space at the end of the line that is used to
17854 display the cursor there. */
17855 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17856 append_space_for_newline (it, 0);
17857
17858 /* Extend the face to the end of the line. */
17859 extend_face_to_end_of_line (it);
17860
17861 /* Make sure we have the position. */
17862 if (used_before == 0)
17863 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17864
17865 /* Record the position of the newline, for use in
17866 find_row_edges. */
17867 it->eol_pos = it->current.pos;
17868
17869 /* Consume the line end. This skips over invisible lines. */
17870 set_iterator_to_next (it, 1);
17871 it->continuation_lines_width = 0;
17872 break;
17873 }
17874
17875 /* Proceed with next display element. Note that this skips
17876 over lines invisible because of selective display. */
17877 set_iterator_to_next (it, 1);
17878
17879 /* If we truncate lines, we are done when the last displayed
17880 glyphs reach past the right margin of the window. */
17881 if (it->line_wrap == TRUNCATE
17882 && (FRAME_WINDOW_P (it->f)
17883 ? (it->current_x >= it->last_visible_x)
17884 : (it->current_x > it->last_visible_x)))
17885 {
17886 /* Maybe add truncation glyphs. */
17887 if (!FRAME_WINDOW_P (it->f))
17888 {
17889 int i, n;
17890
17891 if (!row->reversed_p)
17892 {
17893 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17894 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17895 break;
17896 }
17897 else
17898 {
17899 for (i = 0; i < row->used[TEXT_AREA]; i++)
17900 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17901 break;
17902 /* Remove any padding glyphs at the front of ROW, to
17903 make room for the truncation glyphs we will be
17904 adding below. The loop below always inserts at
17905 least one truncation glyph, so also remove the
17906 last glyph added to ROW. */
17907 unproduce_glyphs (it, i + 1);
17908 /* Adjust i for the loop below. */
17909 i = row->used[TEXT_AREA] - (i + 1);
17910 }
17911
17912 for (n = row->used[TEXT_AREA]; i < n; ++i)
17913 {
17914 row->used[TEXT_AREA] = i;
17915 produce_special_glyphs (it, IT_TRUNCATION);
17916 }
17917 }
17918 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17919 {
17920 /* Don't truncate if we can overflow newline into fringe. */
17921 if (!get_next_display_element (it))
17922 {
17923 it->continuation_lines_width = 0;
17924 row->ends_at_zv_p = 1;
17925 row->exact_window_width_line_p = 1;
17926 break;
17927 }
17928 if (ITERATOR_AT_END_OF_LINE_P (it))
17929 {
17930 row->exact_window_width_line_p = 1;
17931 goto at_end_of_line;
17932 }
17933 }
17934
17935 row->truncated_on_right_p = 1;
17936 it->continuation_lines_width = 0;
17937 reseat_at_next_visible_line_start (it, 0);
17938 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17939 it->hpos = hpos_before;
17940 it->current_x = x_before;
17941 break;
17942 }
17943 }
17944
17945 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17946 at the left window margin. */
17947 if (it->first_visible_x
17948 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
17949 {
17950 if (!FRAME_WINDOW_P (it->f))
17951 insert_left_trunc_glyphs (it);
17952 row->truncated_on_left_p = 1;
17953 }
17954
17955 /* Remember the position at which this line ends.
17956
17957 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
17958 cannot be before the call to find_row_edges below, since that is
17959 where these positions are determined. */
17960 row->end = it->current;
17961 if (!it->bidi_p)
17962 {
17963 row->minpos = row->start.pos;
17964 row->maxpos = row->end.pos;
17965 }
17966 else
17967 {
17968 /* ROW->minpos and ROW->maxpos must be the smallest and
17969 `1 + the largest' buffer positions in ROW. But if ROW was
17970 bidi-reordered, these two positions can be anywhere in the
17971 row, so we must determine them now. */
17972 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
17973 }
17974
17975 /* If the start of this line is the overlay arrow-position, then
17976 mark this glyph row as the one containing the overlay arrow.
17977 This is clearly a mess with variable size fonts. It would be
17978 better to let it be displayed like cursors under X. */
17979 if ((row->displays_text_p || !overlay_arrow_seen)
17980 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17981 !NILP (overlay_arrow_string)))
17982 {
17983 /* Overlay arrow in window redisplay is a fringe bitmap. */
17984 if (STRINGP (overlay_arrow_string))
17985 {
17986 struct glyph_row *arrow_row
17987 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17988 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17989 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17990 struct glyph *p = row->glyphs[TEXT_AREA];
17991 struct glyph *p2, *end;
17992
17993 /* Copy the arrow glyphs. */
17994 while (glyph < arrow_end)
17995 *p++ = *glyph++;
17996
17997 /* Throw away padding glyphs. */
17998 p2 = p;
17999 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18000 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18001 ++p2;
18002 if (p2 > p)
18003 {
18004 while (p2 < end)
18005 *p++ = *p2++;
18006 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18007 }
18008 }
18009 else
18010 {
18011 xassert (INTEGERP (overlay_arrow_string));
18012 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18013 }
18014 overlay_arrow_seen = 1;
18015 }
18016
18017 /* Compute pixel dimensions of this line. */
18018 compute_line_metrics (it);
18019
18020 /* Record whether this row ends inside an ellipsis. */
18021 row->ends_in_ellipsis_p
18022 = (it->method == GET_FROM_DISPLAY_VECTOR
18023 && it->ellipsis_p);
18024
18025 /* Save fringe bitmaps in this row. */
18026 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18027 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18028 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18029 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18030
18031 it->left_user_fringe_bitmap = 0;
18032 it->left_user_fringe_face_id = 0;
18033 it->right_user_fringe_bitmap = 0;
18034 it->right_user_fringe_face_id = 0;
18035
18036 /* Maybe set the cursor. */
18037 cvpos = it->w->cursor.vpos;
18038 if ((cvpos < 0
18039 /* In bidi-reordered rows, keep checking for proper cursor
18040 position even if one has been found already, because buffer
18041 positions in such rows change non-linearly with ROW->VPOS,
18042 when a line is continued. One exception: when we are at ZV,
18043 display cursor on the first suitable glyph row, since all
18044 the empty rows after that also have their position set to ZV. */
18045 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18046 lines' rows is implemented for bidi-reordered rows. */
18047 || (it->bidi_p
18048 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18049 && PT >= MATRIX_ROW_START_CHARPOS (row)
18050 && PT <= MATRIX_ROW_END_CHARPOS (row)
18051 && cursor_row_p (it->w, row))
18052 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18053
18054 /* Highlight trailing whitespace. */
18055 if (!NILP (Vshow_trailing_whitespace))
18056 highlight_trailing_whitespace (it->f, it->glyph_row);
18057
18058 /* Prepare for the next line. This line starts horizontally at (X
18059 HPOS) = (0 0). Vertical positions are incremented. As a
18060 convenience for the caller, IT->glyph_row is set to the next
18061 row to be used. */
18062 it->current_x = it->hpos = 0;
18063 it->current_y += row->height;
18064 SET_TEXT_POS (it->eol_pos, 0, 0);
18065 ++it->vpos;
18066 ++it->glyph_row;
18067 /* The next row should by default use the same value of the
18068 reversed_p flag as this one. set_iterator_to_next decides when
18069 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18070 the flag accordingly. */
18071 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18072 it->glyph_row->reversed_p = row->reversed_p;
18073 it->start = row->end;
18074 return row->displays_text_p;
18075
18076 #undef RECORD_MAX_MIN_POS
18077 }
18078
18079 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18080 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18081 doc: /* Return paragraph direction at point in BUFFER.
18082 Value is either `left-to-right' or `right-to-left'.
18083 If BUFFER is omitted or nil, it defaults to the current buffer.
18084
18085 Paragraph direction determines how the text in the paragraph is displayed.
18086 In left-to-right paragraphs, text begins at the left margin of the window
18087 and the reading direction is generally left to right. In right-to-left
18088 paragraphs, text begins at the right margin and is read from right to left.
18089
18090 See also `bidi-paragraph-direction'. */)
18091 (Lisp_Object buffer)
18092 {
18093 struct buffer *buf;
18094 struct buffer *old;
18095
18096 if (NILP (buffer))
18097 buf = current_buffer;
18098 else
18099 {
18100 CHECK_BUFFER (buffer);
18101 buf = XBUFFER (buffer);
18102 old = current_buffer;
18103 }
18104
18105 if (NILP (buf->bidi_display_reordering))
18106 return Qleft_to_right;
18107 else if (!NILP (buf->bidi_paragraph_direction))
18108 return buf->bidi_paragraph_direction;
18109 else
18110 {
18111 /* Determine the direction from buffer text. We could try to
18112 use current_matrix if it is up to date, but this seems fast
18113 enough as it is. */
18114 struct bidi_it itb;
18115 EMACS_INT pos = BUF_PT (buf);
18116 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18117 int c;
18118
18119 if (buf != current_buffer)
18120 set_buffer_temp (buf);
18121 /* bidi_paragraph_init finds the base direction of the paragraph
18122 by searching forward from paragraph start. We need the base
18123 direction of the current or _previous_ paragraph, so we need
18124 to make sure we are within that paragraph. To that end, find
18125 the previous non-empty line. */
18126 if (pos >= ZV && pos > BEGV)
18127 {
18128 pos--;
18129 bytepos = CHAR_TO_BYTE (pos);
18130 }
18131 while ((c = FETCH_BYTE (bytepos)) == '\n'
18132 || c == ' ' || c == '\t' || c == '\f')
18133 {
18134 if (bytepos <= BEGV_BYTE)
18135 break;
18136 bytepos--;
18137 pos--;
18138 }
18139 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18140 bytepos--;
18141 itb.charpos = pos;
18142 itb.bytepos = bytepos;
18143 itb.first_elt = 1;
18144 itb.separator_limit = -1;
18145 itb.paragraph_dir = NEUTRAL_DIR;
18146
18147 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18148 if (buf != current_buffer)
18149 set_buffer_temp (old);
18150 switch (itb.paragraph_dir)
18151 {
18152 case L2R:
18153 return Qleft_to_right;
18154 break;
18155 case R2L:
18156 return Qright_to_left;
18157 break;
18158 default:
18159 abort ();
18160 }
18161 }
18162 }
18163
18164
18165 \f
18166 /***********************************************************************
18167 Menu Bar
18168 ***********************************************************************/
18169
18170 /* Redisplay the menu bar in the frame for window W.
18171
18172 The menu bar of X frames that don't have X toolkit support is
18173 displayed in a special window W->frame->menu_bar_window.
18174
18175 The menu bar of terminal frames is treated specially as far as
18176 glyph matrices are concerned. Menu bar lines are not part of
18177 windows, so the update is done directly on the frame matrix rows
18178 for the menu bar. */
18179
18180 static void
18181 display_menu_bar (struct window *w)
18182 {
18183 struct frame *f = XFRAME (WINDOW_FRAME (w));
18184 struct it it;
18185 Lisp_Object items;
18186 int i;
18187
18188 /* Don't do all this for graphical frames. */
18189 #ifdef HAVE_NTGUI
18190 if (FRAME_W32_P (f))
18191 return;
18192 #endif
18193 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18194 if (FRAME_X_P (f))
18195 return;
18196 #endif
18197
18198 #ifdef HAVE_NS
18199 if (FRAME_NS_P (f))
18200 return;
18201 #endif /* HAVE_NS */
18202
18203 #ifdef USE_X_TOOLKIT
18204 xassert (!FRAME_WINDOW_P (f));
18205 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18206 it.first_visible_x = 0;
18207 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18208 #else /* not USE_X_TOOLKIT */
18209 if (FRAME_WINDOW_P (f))
18210 {
18211 /* Menu bar lines are displayed in the desired matrix of the
18212 dummy window menu_bar_window. */
18213 struct window *menu_w;
18214 xassert (WINDOWP (f->menu_bar_window));
18215 menu_w = XWINDOW (f->menu_bar_window);
18216 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18217 MENU_FACE_ID);
18218 it.first_visible_x = 0;
18219 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18220 }
18221 else
18222 {
18223 /* This is a TTY frame, i.e. character hpos/vpos are used as
18224 pixel x/y. */
18225 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18226 MENU_FACE_ID);
18227 it.first_visible_x = 0;
18228 it.last_visible_x = FRAME_COLS (f);
18229 }
18230 #endif /* not USE_X_TOOLKIT */
18231
18232 if (! mode_line_inverse_video)
18233 /* Force the menu-bar to be displayed in the default face. */
18234 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18235
18236 /* Clear all rows of the menu bar. */
18237 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18238 {
18239 struct glyph_row *row = it.glyph_row + i;
18240 clear_glyph_row (row);
18241 row->enabled_p = 1;
18242 row->full_width_p = 1;
18243 }
18244
18245 /* Display all items of the menu bar. */
18246 items = FRAME_MENU_BAR_ITEMS (it.f);
18247 for (i = 0; i < XVECTOR (items)->size; i += 4)
18248 {
18249 Lisp_Object string;
18250
18251 /* Stop at nil string. */
18252 string = AREF (items, i + 1);
18253 if (NILP (string))
18254 break;
18255
18256 /* Remember where item was displayed. */
18257 ASET (items, i + 3, make_number (it.hpos));
18258
18259 /* Display the item, pad with one space. */
18260 if (it.current_x < it.last_visible_x)
18261 display_string (NULL, string, Qnil, 0, 0, &it,
18262 SCHARS (string) + 1, 0, 0, -1);
18263 }
18264
18265 /* Fill out the line with spaces. */
18266 if (it.current_x < it.last_visible_x)
18267 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18268
18269 /* Compute the total height of the lines. */
18270 compute_line_metrics (&it);
18271 }
18272
18273
18274 \f
18275 /***********************************************************************
18276 Mode Line
18277 ***********************************************************************/
18278
18279 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18280 FORCE is non-zero, redisplay mode lines unconditionally.
18281 Otherwise, redisplay only mode lines that are garbaged. Value is
18282 the number of windows whose mode lines were redisplayed. */
18283
18284 static int
18285 redisplay_mode_lines (Lisp_Object window, int force)
18286 {
18287 int nwindows = 0;
18288
18289 while (!NILP (window))
18290 {
18291 struct window *w = XWINDOW (window);
18292
18293 if (WINDOWP (w->hchild))
18294 nwindows += redisplay_mode_lines (w->hchild, force);
18295 else if (WINDOWP (w->vchild))
18296 nwindows += redisplay_mode_lines (w->vchild, force);
18297 else if (force
18298 || FRAME_GARBAGED_P (XFRAME (w->frame))
18299 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18300 {
18301 struct text_pos lpoint;
18302 struct buffer *old = current_buffer;
18303
18304 /* Set the window's buffer for the mode line display. */
18305 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18306 set_buffer_internal_1 (XBUFFER (w->buffer));
18307
18308 /* Point refers normally to the selected window. For any
18309 other window, set up appropriate value. */
18310 if (!EQ (window, selected_window))
18311 {
18312 struct text_pos pt;
18313
18314 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18315 if (CHARPOS (pt) < BEGV)
18316 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18317 else if (CHARPOS (pt) > (ZV - 1))
18318 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18319 else
18320 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18321 }
18322
18323 /* Display mode lines. */
18324 clear_glyph_matrix (w->desired_matrix);
18325 if (display_mode_lines (w))
18326 {
18327 ++nwindows;
18328 w->must_be_updated_p = 1;
18329 }
18330
18331 /* Restore old settings. */
18332 set_buffer_internal_1 (old);
18333 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18334 }
18335
18336 window = w->next;
18337 }
18338
18339 return nwindows;
18340 }
18341
18342
18343 /* Display the mode and/or header line of window W. Value is the
18344 sum number of mode lines and header lines displayed. */
18345
18346 static int
18347 display_mode_lines (struct window *w)
18348 {
18349 Lisp_Object old_selected_window, old_selected_frame;
18350 int n = 0;
18351
18352 old_selected_frame = selected_frame;
18353 selected_frame = w->frame;
18354 old_selected_window = selected_window;
18355 XSETWINDOW (selected_window, w);
18356
18357 /* These will be set while the mode line specs are processed. */
18358 line_number_displayed = 0;
18359 w->column_number_displayed = Qnil;
18360
18361 if (WINDOW_WANTS_MODELINE_P (w))
18362 {
18363 struct window *sel_w = XWINDOW (old_selected_window);
18364
18365 /* Select mode line face based on the real selected window. */
18366 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18367 current_buffer->mode_line_format);
18368 ++n;
18369 }
18370
18371 if (WINDOW_WANTS_HEADER_LINE_P (w))
18372 {
18373 display_mode_line (w, HEADER_LINE_FACE_ID,
18374 current_buffer->header_line_format);
18375 ++n;
18376 }
18377
18378 selected_frame = old_selected_frame;
18379 selected_window = old_selected_window;
18380 return n;
18381 }
18382
18383
18384 /* Display mode or header line of window W. FACE_ID specifies which
18385 line to display; it is either MODE_LINE_FACE_ID or
18386 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18387 display. Value is the pixel height of the mode/header line
18388 displayed. */
18389
18390 static int
18391 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18392 {
18393 struct it it;
18394 struct face *face;
18395 int count = SPECPDL_INDEX ();
18396
18397 init_iterator (&it, w, -1, -1, NULL, face_id);
18398 /* Don't extend on a previously drawn mode-line.
18399 This may happen if called from pos_visible_p. */
18400 it.glyph_row->enabled_p = 0;
18401 prepare_desired_row (it.glyph_row);
18402
18403 it.glyph_row->mode_line_p = 1;
18404
18405 if (! mode_line_inverse_video)
18406 /* Force the mode-line to be displayed in the default face. */
18407 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18408
18409 record_unwind_protect (unwind_format_mode_line,
18410 format_mode_line_unwind_data (NULL, Qnil, 0));
18411
18412 mode_line_target = MODE_LINE_DISPLAY;
18413
18414 /* Temporarily make frame's keyboard the current kboard so that
18415 kboard-local variables in the mode_line_format will get the right
18416 values. */
18417 push_kboard (FRAME_KBOARD (it.f));
18418 record_unwind_save_match_data ();
18419 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18420 pop_kboard ();
18421
18422 unbind_to (count, Qnil);
18423
18424 /* Fill up with spaces. */
18425 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18426
18427 compute_line_metrics (&it);
18428 it.glyph_row->full_width_p = 1;
18429 it.glyph_row->continued_p = 0;
18430 it.glyph_row->truncated_on_left_p = 0;
18431 it.glyph_row->truncated_on_right_p = 0;
18432
18433 /* Make a 3D mode-line have a shadow at its right end. */
18434 face = FACE_FROM_ID (it.f, face_id);
18435 extend_face_to_end_of_line (&it);
18436 if (face->box != FACE_NO_BOX)
18437 {
18438 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18439 + it.glyph_row->used[TEXT_AREA] - 1);
18440 last->right_box_line_p = 1;
18441 }
18442
18443 return it.glyph_row->height;
18444 }
18445
18446 /* Move element ELT in LIST to the front of LIST.
18447 Return the updated list. */
18448
18449 static Lisp_Object
18450 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18451 {
18452 register Lisp_Object tail, prev;
18453 register Lisp_Object tem;
18454
18455 tail = list;
18456 prev = Qnil;
18457 while (CONSP (tail))
18458 {
18459 tem = XCAR (tail);
18460
18461 if (EQ (elt, tem))
18462 {
18463 /* Splice out the link TAIL. */
18464 if (NILP (prev))
18465 list = XCDR (tail);
18466 else
18467 Fsetcdr (prev, XCDR (tail));
18468
18469 /* Now make it the first. */
18470 Fsetcdr (tail, list);
18471 return tail;
18472 }
18473 else
18474 prev = tail;
18475 tail = XCDR (tail);
18476 QUIT;
18477 }
18478
18479 /* Not found--return unchanged LIST. */
18480 return list;
18481 }
18482
18483 /* Contribute ELT to the mode line for window IT->w. How it
18484 translates into text depends on its data type.
18485
18486 IT describes the display environment in which we display, as usual.
18487
18488 DEPTH is the depth in recursion. It is used to prevent
18489 infinite recursion here.
18490
18491 FIELD_WIDTH is the number of characters the display of ELT should
18492 occupy in the mode line, and PRECISION is the maximum number of
18493 characters to display from ELT's representation. See
18494 display_string for details.
18495
18496 Returns the hpos of the end of the text generated by ELT.
18497
18498 PROPS is a property list to add to any string we encounter.
18499
18500 If RISKY is nonzero, remove (disregard) any properties in any string
18501 we encounter, and ignore :eval and :propertize.
18502
18503 The global variable `mode_line_target' determines whether the
18504 output is passed to `store_mode_line_noprop',
18505 `store_mode_line_string', or `display_string'. */
18506
18507 static int
18508 display_mode_element (struct it *it, int depth, int field_width, int precision,
18509 Lisp_Object elt, Lisp_Object props, int risky)
18510 {
18511 int n = 0, field, prec;
18512 int literal = 0;
18513
18514 tail_recurse:
18515 if (depth > 100)
18516 elt = build_string ("*too-deep*");
18517
18518 depth++;
18519
18520 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18521 {
18522 case Lisp_String:
18523 {
18524 /* A string: output it and check for %-constructs within it. */
18525 unsigned char c;
18526 EMACS_INT offset = 0;
18527
18528 if (SCHARS (elt) > 0
18529 && (!NILP (props) || risky))
18530 {
18531 Lisp_Object oprops, aelt;
18532 oprops = Ftext_properties_at (make_number (0), elt);
18533
18534 /* If the starting string's properties are not what
18535 we want, translate the string. Also, if the string
18536 is risky, do that anyway. */
18537
18538 if (NILP (Fequal (props, oprops)) || risky)
18539 {
18540 /* If the starting string has properties,
18541 merge the specified ones onto the existing ones. */
18542 if (! NILP (oprops) && !risky)
18543 {
18544 Lisp_Object tem;
18545
18546 oprops = Fcopy_sequence (oprops);
18547 tem = props;
18548 while (CONSP (tem))
18549 {
18550 oprops = Fplist_put (oprops, XCAR (tem),
18551 XCAR (XCDR (tem)));
18552 tem = XCDR (XCDR (tem));
18553 }
18554 props = oprops;
18555 }
18556
18557 aelt = Fassoc (elt, mode_line_proptrans_alist);
18558 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18559 {
18560 /* AELT is what we want. Move it to the front
18561 without consing. */
18562 elt = XCAR (aelt);
18563 mode_line_proptrans_alist
18564 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18565 }
18566 else
18567 {
18568 Lisp_Object tem;
18569
18570 /* If AELT has the wrong props, it is useless.
18571 so get rid of it. */
18572 if (! NILP (aelt))
18573 mode_line_proptrans_alist
18574 = Fdelq (aelt, mode_line_proptrans_alist);
18575
18576 elt = Fcopy_sequence (elt);
18577 Fset_text_properties (make_number (0), Flength (elt),
18578 props, elt);
18579 /* Add this item to mode_line_proptrans_alist. */
18580 mode_line_proptrans_alist
18581 = Fcons (Fcons (elt, props),
18582 mode_line_proptrans_alist);
18583 /* Truncate mode_line_proptrans_alist
18584 to at most 50 elements. */
18585 tem = Fnthcdr (make_number (50),
18586 mode_line_proptrans_alist);
18587 if (! NILP (tem))
18588 XSETCDR (tem, Qnil);
18589 }
18590 }
18591 }
18592
18593 offset = 0;
18594
18595 if (literal)
18596 {
18597 prec = precision - n;
18598 switch (mode_line_target)
18599 {
18600 case MODE_LINE_NOPROP:
18601 case MODE_LINE_TITLE:
18602 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18603 break;
18604 case MODE_LINE_STRING:
18605 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18606 break;
18607 case MODE_LINE_DISPLAY:
18608 n += display_string (NULL, elt, Qnil, 0, 0, it,
18609 0, prec, 0, STRING_MULTIBYTE (elt));
18610 break;
18611 }
18612
18613 break;
18614 }
18615
18616 /* Handle the non-literal case. */
18617
18618 while ((precision <= 0 || n < precision)
18619 && SREF (elt, offset) != 0
18620 && (mode_line_target != MODE_LINE_DISPLAY
18621 || it->current_x < it->last_visible_x))
18622 {
18623 EMACS_INT last_offset = offset;
18624
18625 /* Advance to end of string or next format specifier. */
18626 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18627 ;
18628
18629 if (offset - 1 != last_offset)
18630 {
18631 EMACS_INT nchars, nbytes;
18632
18633 /* Output to end of string or up to '%'. Field width
18634 is length of string. Don't output more than
18635 PRECISION allows us. */
18636 offset--;
18637
18638 prec = c_string_width (SDATA (elt) + last_offset,
18639 offset - last_offset, precision - n,
18640 &nchars, &nbytes);
18641
18642 switch (mode_line_target)
18643 {
18644 case MODE_LINE_NOPROP:
18645 case MODE_LINE_TITLE:
18646 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18647 break;
18648 case MODE_LINE_STRING:
18649 {
18650 EMACS_INT bytepos = last_offset;
18651 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18652 EMACS_INT endpos = (precision <= 0
18653 ? string_byte_to_char (elt, offset)
18654 : charpos + nchars);
18655
18656 n += store_mode_line_string (NULL,
18657 Fsubstring (elt, make_number (charpos),
18658 make_number (endpos)),
18659 0, 0, 0, Qnil);
18660 }
18661 break;
18662 case MODE_LINE_DISPLAY:
18663 {
18664 EMACS_INT bytepos = last_offset;
18665 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18666
18667 if (precision <= 0)
18668 nchars = string_byte_to_char (elt, offset) - charpos;
18669 n += display_string (NULL, elt, Qnil, 0, charpos,
18670 it, 0, nchars, 0,
18671 STRING_MULTIBYTE (elt));
18672 }
18673 break;
18674 }
18675 }
18676 else /* c == '%' */
18677 {
18678 EMACS_INT percent_position = offset;
18679
18680 /* Get the specified minimum width. Zero means
18681 don't pad. */
18682 field = 0;
18683 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18684 field = field * 10 + c - '0';
18685
18686 /* Don't pad beyond the total padding allowed. */
18687 if (field_width - n > 0 && field > field_width - n)
18688 field = field_width - n;
18689
18690 /* Note that either PRECISION <= 0 or N < PRECISION. */
18691 prec = precision - n;
18692
18693 if (c == 'M')
18694 n += display_mode_element (it, depth, field, prec,
18695 Vglobal_mode_string, props,
18696 risky);
18697 else if (c != 0)
18698 {
18699 int multibyte;
18700 EMACS_INT bytepos, charpos;
18701 const unsigned char *spec;
18702 Lisp_Object string;
18703
18704 bytepos = percent_position;
18705 charpos = (STRING_MULTIBYTE (elt)
18706 ? string_byte_to_char (elt, bytepos)
18707 : bytepos);
18708 spec = decode_mode_spec (it->w, c, field, prec, &string);
18709 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18710
18711 switch (mode_line_target)
18712 {
18713 case MODE_LINE_NOPROP:
18714 case MODE_LINE_TITLE:
18715 n += store_mode_line_noprop (spec, field, prec);
18716 break;
18717 case MODE_LINE_STRING:
18718 {
18719 int len = strlen (spec);
18720 Lisp_Object tem = make_string (spec, len);
18721 props = Ftext_properties_at (make_number (charpos), elt);
18722 /* Should only keep face property in props */
18723 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18724 }
18725 break;
18726 case MODE_LINE_DISPLAY:
18727 {
18728 int nglyphs_before, nwritten;
18729
18730 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18731 nwritten = display_string (spec, string, elt,
18732 charpos, 0, it,
18733 field, prec, 0,
18734 multibyte);
18735
18736 /* Assign to the glyphs written above the
18737 string where the `%x' came from, position
18738 of the `%'. */
18739 if (nwritten > 0)
18740 {
18741 struct glyph *glyph
18742 = (it->glyph_row->glyphs[TEXT_AREA]
18743 + nglyphs_before);
18744 int i;
18745
18746 for (i = 0; i < nwritten; ++i)
18747 {
18748 glyph[i].object = elt;
18749 glyph[i].charpos = charpos;
18750 }
18751
18752 n += nwritten;
18753 }
18754 }
18755 break;
18756 }
18757 }
18758 else /* c == 0 */
18759 break;
18760 }
18761 }
18762 }
18763 break;
18764
18765 case Lisp_Symbol:
18766 /* A symbol: process the value of the symbol recursively
18767 as if it appeared here directly. Avoid error if symbol void.
18768 Special case: if value of symbol is a string, output the string
18769 literally. */
18770 {
18771 register Lisp_Object tem;
18772
18773 /* If the variable is not marked as risky to set
18774 then its contents are risky to use. */
18775 if (NILP (Fget (elt, Qrisky_local_variable)))
18776 risky = 1;
18777
18778 tem = Fboundp (elt);
18779 if (!NILP (tem))
18780 {
18781 tem = Fsymbol_value (elt);
18782 /* If value is a string, output that string literally:
18783 don't check for % within it. */
18784 if (STRINGP (tem))
18785 literal = 1;
18786
18787 if (!EQ (tem, elt))
18788 {
18789 /* Give up right away for nil or t. */
18790 elt = tem;
18791 goto tail_recurse;
18792 }
18793 }
18794 }
18795 break;
18796
18797 case Lisp_Cons:
18798 {
18799 register Lisp_Object car, tem;
18800
18801 /* A cons cell: five distinct cases.
18802 If first element is :eval or :propertize, do something special.
18803 If first element is a string or a cons, process all the elements
18804 and effectively concatenate them.
18805 If first element is a negative number, truncate displaying cdr to
18806 at most that many characters. If positive, pad (with spaces)
18807 to at least that many characters.
18808 If first element is a symbol, process the cadr or caddr recursively
18809 according to whether the symbol's value is non-nil or nil. */
18810 car = XCAR (elt);
18811 if (EQ (car, QCeval))
18812 {
18813 /* An element of the form (:eval FORM) means evaluate FORM
18814 and use the result as mode line elements. */
18815
18816 if (risky)
18817 break;
18818
18819 if (CONSP (XCDR (elt)))
18820 {
18821 Lisp_Object spec;
18822 spec = safe_eval (XCAR (XCDR (elt)));
18823 n += display_mode_element (it, depth, field_width - n,
18824 precision - n, spec, props,
18825 risky);
18826 }
18827 }
18828 else if (EQ (car, QCpropertize))
18829 {
18830 /* An element of the form (:propertize ELT PROPS...)
18831 means display ELT but applying properties PROPS. */
18832
18833 if (risky)
18834 break;
18835
18836 if (CONSP (XCDR (elt)))
18837 n += display_mode_element (it, depth, field_width - n,
18838 precision - n, XCAR (XCDR (elt)),
18839 XCDR (XCDR (elt)), risky);
18840 }
18841 else if (SYMBOLP (car))
18842 {
18843 tem = Fboundp (car);
18844 elt = XCDR (elt);
18845 if (!CONSP (elt))
18846 goto invalid;
18847 /* elt is now the cdr, and we know it is a cons cell.
18848 Use its car if CAR has a non-nil value. */
18849 if (!NILP (tem))
18850 {
18851 tem = Fsymbol_value (car);
18852 if (!NILP (tem))
18853 {
18854 elt = XCAR (elt);
18855 goto tail_recurse;
18856 }
18857 }
18858 /* Symbol's value is nil (or symbol is unbound)
18859 Get the cddr of the original list
18860 and if possible find the caddr and use that. */
18861 elt = XCDR (elt);
18862 if (NILP (elt))
18863 break;
18864 else if (!CONSP (elt))
18865 goto invalid;
18866 elt = XCAR (elt);
18867 goto tail_recurse;
18868 }
18869 else if (INTEGERP (car))
18870 {
18871 register int lim = XINT (car);
18872 elt = XCDR (elt);
18873 if (lim < 0)
18874 {
18875 /* Negative int means reduce maximum width. */
18876 if (precision <= 0)
18877 precision = -lim;
18878 else
18879 precision = min (precision, -lim);
18880 }
18881 else if (lim > 0)
18882 {
18883 /* Padding specified. Don't let it be more than
18884 current maximum. */
18885 if (precision > 0)
18886 lim = min (precision, lim);
18887
18888 /* If that's more padding than already wanted, queue it.
18889 But don't reduce padding already specified even if
18890 that is beyond the current truncation point. */
18891 field_width = max (lim, field_width);
18892 }
18893 goto tail_recurse;
18894 }
18895 else if (STRINGP (car) || CONSP (car))
18896 {
18897 Lisp_Object halftail = elt;
18898 int len = 0;
18899
18900 while (CONSP (elt)
18901 && (precision <= 0 || n < precision))
18902 {
18903 n += display_mode_element (it, depth,
18904 /* Do padding only after the last
18905 element in the list. */
18906 (! CONSP (XCDR (elt))
18907 ? field_width - n
18908 : 0),
18909 precision - n, XCAR (elt),
18910 props, risky);
18911 elt = XCDR (elt);
18912 len++;
18913 if ((len & 1) == 0)
18914 halftail = XCDR (halftail);
18915 /* Check for cycle. */
18916 if (EQ (halftail, elt))
18917 break;
18918 }
18919 }
18920 }
18921 break;
18922
18923 default:
18924 invalid:
18925 elt = build_string ("*invalid*");
18926 goto tail_recurse;
18927 }
18928
18929 /* Pad to FIELD_WIDTH. */
18930 if (field_width > 0 && n < field_width)
18931 {
18932 switch (mode_line_target)
18933 {
18934 case MODE_LINE_NOPROP:
18935 case MODE_LINE_TITLE:
18936 n += store_mode_line_noprop ("", field_width - n, 0);
18937 break;
18938 case MODE_LINE_STRING:
18939 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18940 break;
18941 case MODE_LINE_DISPLAY:
18942 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18943 0, 0, 0);
18944 break;
18945 }
18946 }
18947
18948 return n;
18949 }
18950
18951 /* Store a mode-line string element in mode_line_string_list.
18952
18953 If STRING is non-null, display that C string. Otherwise, the Lisp
18954 string LISP_STRING is displayed.
18955
18956 FIELD_WIDTH is the minimum number of output glyphs to produce.
18957 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18958 with spaces. FIELD_WIDTH <= 0 means don't pad.
18959
18960 PRECISION is the maximum number of characters to output from
18961 STRING. PRECISION <= 0 means don't truncate the string.
18962
18963 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18964 properties to the string.
18965
18966 PROPS are the properties to add to the string.
18967 The mode_line_string_face face property is always added to the string.
18968 */
18969
18970 static int
18971 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
18972 int field_width, int precision, Lisp_Object props)
18973 {
18974 EMACS_INT len;
18975 int n = 0;
18976
18977 if (string != NULL)
18978 {
18979 len = strlen (string);
18980 if (precision > 0 && len > precision)
18981 len = precision;
18982 lisp_string = make_string (string, len);
18983 if (NILP (props))
18984 props = mode_line_string_face_prop;
18985 else if (!NILP (mode_line_string_face))
18986 {
18987 Lisp_Object face = Fplist_get (props, Qface);
18988 props = Fcopy_sequence (props);
18989 if (NILP (face))
18990 face = mode_line_string_face;
18991 else
18992 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18993 props = Fplist_put (props, Qface, face);
18994 }
18995 Fadd_text_properties (make_number (0), make_number (len),
18996 props, lisp_string);
18997 }
18998 else
18999 {
19000 len = XFASTINT (Flength (lisp_string));
19001 if (precision > 0 && len > precision)
19002 {
19003 len = precision;
19004 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19005 precision = -1;
19006 }
19007 if (!NILP (mode_line_string_face))
19008 {
19009 Lisp_Object face;
19010 if (NILP (props))
19011 props = Ftext_properties_at (make_number (0), lisp_string);
19012 face = Fplist_get (props, Qface);
19013 if (NILP (face))
19014 face = mode_line_string_face;
19015 else
19016 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19017 props = Fcons (Qface, Fcons (face, Qnil));
19018 if (copy_string)
19019 lisp_string = Fcopy_sequence (lisp_string);
19020 }
19021 if (!NILP (props))
19022 Fadd_text_properties (make_number (0), make_number (len),
19023 props, lisp_string);
19024 }
19025
19026 if (len > 0)
19027 {
19028 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19029 n += len;
19030 }
19031
19032 if (field_width > len)
19033 {
19034 field_width -= len;
19035 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19036 if (!NILP (props))
19037 Fadd_text_properties (make_number (0), make_number (field_width),
19038 props, lisp_string);
19039 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19040 n += field_width;
19041 }
19042
19043 return n;
19044 }
19045
19046
19047 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19048 1, 4, 0,
19049 doc: /* Format a string out of a mode line format specification.
19050 First arg FORMAT specifies the mode line format (see `mode-line-format'
19051 for details) to use.
19052
19053 Optional second arg FACE specifies the face property to put
19054 on all characters for which no face is specified.
19055 The value t means whatever face the window's mode line currently uses
19056 \(either `mode-line' or `mode-line-inactive', depending).
19057 A value of nil means the default is no face property.
19058 If FACE is an integer, the value string has no text properties.
19059
19060 Optional third and fourth args WINDOW and BUFFER specify the window
19061 and buffer to use as the context for the formatting (defaults
19062 are the selected window and the window's buffer). */)
19063 (Lisp_Object format, Lisp_Object face, Lisp_Object window, Lisp_Object buffer)
19064 {
19065 struct it it;
19066 int len;
19067 struct window *w;
19068 struct buffer *old_buffer = NULL;
19069 int face_id = -1;
19070 int no_props = INTEGERP (face);
19071 int count = SPECPDL_INDEX ();
19072 Lisp_Object str;
19073 int string_start = 0;
19074
19075 if (NILP (window))
19076 window = selected_window;
19077 CHECK_WINDOW (window);
19078 w = XWINDOW (window);
19079
19080 if (NILP (buffer))
19081 buffer = w->buffer;
19082 CHECK_BUFFER (buffer);
19083
19084 /* Make formatting the modeline a non-op when noninteractive, otherwise
19085 there will be problems later caused by a partially initialized frame. */
19086 if (NILP (format) || noninteractive)
19087 return empty_unibyte_string;
19088
19089 if (no_props)
19090 face = Qnil;
19091
19092 if (!NILP (face))
19093 {
19094 if (EQ (face, Qt))
19095 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
19096 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
19097 }
19098
19099 if (face_id < 0)
19100 face_id = DEFAULT_FACE_ID;
19101
19102 if (XBUFFER (buffer) != current_buffer)
19103 old_buffer = current_buffer;
19104
19105 /* Save things including mode_line_proptrans_alist,
19106 and set that to nil so that we don't alter the outer value. */
19107 record_unwind_protect (unwind_format_mode_line,
19108 format_mode_line_unwind_data
19109 (old_buffer, selected_window, 1));
19110 mode_line_proptrans_alist = Qnil;
19111
19112 Fselect_window (window, Qt);
19113 if (old_buffer)
19114 set_buffer_internal_1 (XBUFFER (buffer));
19115
19116 init_iterator (&it, w, -1, -1, NULL, face_id);
19117
19118 if (no_props)
19119 {
19120 mode_line_target = MODE_LINE_NOPROP;
19121 mode_line_string_face_prop = Qnil;
19122 mode_line_string_list = Qnil;
19123 string_start = MODE_LINE_NOPROP_LEN (0);
19124 }
19125 else
19126 {
19127 mode_line_target = MODE_LINE_STRING;
19128 mode_line_string_list = Qnil;
19129 mode_line_string_face = face;
19130 mode_line_string_face_prop
19131 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19132 }
19133
19134 push_kboard (FRAME_KBOARD (it.f));
19135 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19136 pop_kboard ();
19137
19138 if (no_props)
19139 {
19140 len = MODE_LINE_NOPROP_LEN (string_start);
19141 str = make_string (mode_line_noprop_buf + string_start, len);
19142 }
19143 else
19144 {
19145 mode_line_string_list = Fnreverse (mode_line_string_list);
19146 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19147 empty_unibyte_string);
19148 }
19149
19150 unbind_to (count, Qnil);
19151 return str;
19152 }
19153
19154 /* Write a null-terminated, right justified decimal representation of
19155 the positive integer D to BUF using a minimal field width WIDTH. */
19156
19157 static void
19158 pint2str (register char *buf, register int width, register int d)
19159 {
19160 register char *p = buf;
19161
19162 if (d <= 0)
19163 *p++ = '0';
19164 else
19165 {
19166 while (d > 0)
19167 {
19168 *p++ = d % 10 + '0';
19169 d /= 10;
19170 }
19171 }
19172
19173 for (width -= (int) (p - buf); width > 0; --width)
19174 *p++ = ' ';
19175 *p-- = '\0';
19176 while (p > buf)
19177 {
19178 d = *buf;
19179 *buf++ = *p;
19180 *p-- = d;
19181 }
19182 }
19183
19184 /* Write a null-terminated, right justified decimal and "human
19185 readable" representation of the nonnegative integer D to BUF using
19186 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19187
19188 static const char power_letter[] =
19189 {
19190 0, /* not used */
19191 'k', /* kilo */
19192 'M', /* mega */
19193 'G', /* giga */
19194 'T', /* tera */
19195 'P', /* peta */
19196 'E', /* exa */
19197 'Z', /* zetta */
19198 'Y' /* yotta */
19199 };
19200
19201 static void
19202 pint2hrstr (char *buf, int width, int d)
19203 {
19204 /* We aim to represent the nonnegative integer D as
19205 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19206 int quotient = d;
19207 int remainder = 0;
19208 /* -1 means: do not use TENTHS. */
19209 int tenths = -1;
19210 int exponent = 0;
19211
19212 /* Length of QUOTIENT.TENTHS as a string. */
19213 int length;
19214
19215 char * psuffix;
19216 char * p;
19217
19218 if (1000 <= quotient)
19219 {
19220 /* Scale to the appropriate EXPONENT. */
19221 do
19222 {
19223 remainder = quotient % 1000;
19224 quotient /= 1000;
19225 exponent++;
19226 }
19227 while (1000 <= quotient);
19228
19229 /* Round to nearest and decide whether to use TENTHS or not. */
19230 if (quotient <= 9)
19231 {
19232 tenths = remainder / 100;
19233 if (50 <= remainder % 100)
19234 {
19235 if (tenths < 9)
19236 tenths++;
19237 else
19238 {
19239 quotient++;
19240 if (quotient == 10)
19241 tenths = -1;
19242 else
19243 tenths = 0;
19244 }
19245 }
19246 }
19247 else
19248 if (500 <= remainder)
19249 {
19250 if (quotient < 999)
19251 quotient++;
19252 else
19253 {
19254 quotient = 1;
19255 exponent++;
19256 tenths = 0;
19257 }
19258 }
19259 }
19260
19261 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19262 if (tenths == -1 && quotient <= 99)
19263 if (quotient <= 9)
19264 length = 1;
19265 else
19266 length = 2;
19267 else
19268 length = 3;
19269 p = psuffix = buf + max (width, length);
19270
19271 /* Print EXPONENT. */
19272 if (exponent)
19273 *psuffix++ = power_letter[exponent];
19274 *psuffix = '\0';
19275
19276 /* Print TENTHS. */
19277 if (tenths >= 0)
19278 {
19279 *--p = '0' + tenths;
19280 *--p = '.';
19281 }
19282
19283 /* Print QUOTIENT. */
19284 do
19285 {
19286 int digit = quotient % 10;
19287 *--p = '0' + digit;
19288 }
19289 while ((quotient /= 10) != 0);
19290
19291 /* Print leading spaces. */
19292 while (buf < p)
19293 *--p = ' ';
19294 }
19295
19296 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19297 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19298 type of CODING_SYSTEM. Return updated pointer into BUF. */
19299
19300 static unsigned char invalid_eol_type[] = "(*invalid*)";
19301
19302 static char *
19303 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19304 {
19305 Lisp_Object val;
19306 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
19307 const unsigned char *eol_str;
19308 int eol_str_len;
19309 /* The EOL conversion we are using. */
19310 Lisp_Object eoltype;
19311
19312 val = CODING_SYSTEM_SPEC (coding_system);
19313 eoltype = Qnil;
19314
19315 if (!VECTORP (val)) /* Not yet decided. */
19316 {
19317 if (multibyte)
19318 *buf++ = '-';
19319 if (eol_flag)
19320 eoltype = eol_mnemonic_undecided;
19321 /* Don't mention EOL conversion if it isn't decided. */
19322 }
19323 else
19324 {
19325 Lisp_Object attrs;
19326 Lisp_Object eolvalue;
19327
19328 attrs = AREF (val, 0);
19329 eolvalue = AREF (val, 2);
19330
19331 if (multibyte)
19332 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19333
19334 if (eol_flag)
19335 {
19336 /* The EOL conversion that is normal on this system. */
19337
19338 if (NILP (eolvalue)) /* Not yet decided. */
19339 eoltype = eol_mnemonic_undecided;
19340 else if (VECTORP (eolvalue)) /* Not yet decided. */
19341 eoltype = eol_mnemonic_undecided;
19342 else /* eolvalue is Qunix, Qdos, or Qmac. */
19343 eoltype = (EQ (eolvalue, Qunix)
19344 ? eol_mnemonic_unix
19345 : (EQ (eolvalue, Qdos) == 1
19346 ? eol_mnemonic_dos : eol_mnemonic_mac));
19347 }
19348 }
19349
19350 if (eol_flag)
19351 {
19352 /* Mention the EOL conversion if it is not the usual one. */
19353 if (STRINGP (eoltype))
19354 {
19355 eol_str = SDATA (eoltype);
19356 eol_str_len = SBYTES (eoltype);
19357 }
19358 else if (CHARACTERP (eoltype))
19359 {
19360 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19361 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19362 eol_str = tmp;
19363 }
19364 else
19365 {
19366 eol_str = invalid_eol_type;
19367 eol_str_len = sizeof (invalid_eol_type) - 1;
19368 }
19369 memcpy (buf, eol_str, eol_str_len);
19370 buf += eol_str_len;
19371 }
19372
19373 return buf;
19374 }
19375
19376 /* Return a string for the output of a mode line %-spec for window W,
19377 generated by character C. PRECISION >= 0 means don't return a
19378 string longer than that value. FIELD_WIDTH > 0 means pad the
19379 string returned with spaces to that value. Return a Lisp string in
19380 *STRING if the resulting string is taken from that Lisp string.
19381
19382 Note we operate on the current buffer for most purposes,
19383 the exception being w->base_line_pos. */
19384
19385 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19386
19387 static const char *
19388 decode_mode_spec (struct window *w, register int c, int field_width,
19389 int precision, Lisp_Object *string)
19390 {
19391 Lisp_Object obj;
19392 struct frame *f = XFRAME (WINDOW_FRAME (w));
19393 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19394 struct buffer *b = current_buffer;
19395
19396 obj = Qnil;
19397 *string = Qnil;
19398
19399 switch (c)
19400 {
19401 case '*':
19402 if (!NILP (b->read_only))
19403 return "%";
19404 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19405 return "*";
19406 return "-";
19407
19408 case '+':
19409 /* This differs from %* only for a modified read-only buffer. */
19410 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19411 return "*";
19412 if (!NILP (b->read_only))
19413 return "%";
19414 return "-";
19415
19416 case '&':
19417 /* This differs from %* in ignoring read-only-ness. */
19418 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19419 return "*";
19420 return "-";
19421
19422 case '%':
19423 return "%";
19424
19425 case '[':
19426 {
19427 int i;
19428 char *p;
19429
19430 if (command_loop_level > 5)
19431 return "[[[... ";
19432 p = decode_mode_spec_buf;
19433 for (i = 0; i < command_loop_level; i++)
19434 *p++ = '[';
19435 *p = 0;
19436 return decode_mode_spec_buf;
19437 }
19438
19439 case ']':
19440 {
19441 int i;
19442 char *p;
19443
19444 if (command_loop_level > 5)
19445 return " ...]]]";
19446 p = decode_mode_spec_buf;
19447 for (i = 0; i < command_loop_level; i++)
19448 *p++ = ']';
19449 *p = 0;
19450 return decode_mode_spec_buf;
19451 }
19452
19453 case '-':
19454 {
19455 register int i;
19456
19457 /* Let lots_of_dashes be a string of infinite length. */
19458 if (mode_line_target == MODE_LINE_NOPROP ||
19459 mode_line_target == MODE_LINE_STRING)
19460 return "--";
19461 if (field_width <= 0
19462 || field_width > sizeof (lots_of_dashes))
19463 {
19464 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19465 decode_mode_spec_buf[i] = '-';
19466 decode_mode_spec_buf[i] = '\0';
19467 return decode_mode_spec_buf;
19468 }
19469 else
19470 return lots_of_dashes;
19471 }
19472
19473 case 'b':
19474 obj = b->name;
19475 break;
19476
19477 case 'c':
19478 /* %c and %l are ignored in `frame-title-format'.
19479 (In redisplay_internal, the frame title is drawn _before_ the
19480 windows are updated, so the stuff which depends on actual
19481 window contents (such as %l) may fail to render properly, or
19482 even crash emacs.) */
19483 if (mode_line_target == MODE_LINE_TITLE)
19484 return "";
19485 else
19486 {
19487 int col = (int) current_column (); /* iftc */
19488 w->column_number_displayed = make_number (col);
19489 pint2str (decode_mode_spec_buf, field_width, col);
19490 return decode_mode_spec_buf;
19491 }
19492
19493 case 'e':
19494 #ifndef SYSTEM_MALLOC
19495 {
19496 if (NILP (Vmemory_full))
19497 return "";
19498 else
19499 return "!MEM FULL! ";
19500 }
19501 #else
19502 return "";
19503 #endif
19504
19505 case 'F':
19506 /* %F displays the frame name. */
19507 if (!NILP (f->title))
19508 return (char *) SDATA (f->title);
19509 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19510 return (char *) SDATA (f->name);
19511 return "Emacs";
19512
19513 case 'f':
19514 obj = b->filename;
19515 break;
19516
19517 case 'i':
19518 {
19519 EMACS_INT size = ZV - BEGV;
19520 pint2str (decode_mode_spec_buf, field_width, size);
19521 return decode_mode_spec_buf;
19522 }
19523
19524 case 'I':
19525 {
19526 EMACS_INT size = ZV - BEGV;
19527 pint2hrstr (decode_mode_spec_buf, field_width, size);
19528 return decode_mode_spec_buf;
19529 }
19530
19531 case 'l':
19532 {
19533 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
19534 int topline, nlines, height;
19535 EMACS_INT junk;
19536
19537 /* %c and %l are ignored in `frame-title-format'. */
19538 if (mode_line_target == MODE_LINE_TITLE)
19539 return "";
19540
19541 startpos = XMARKER (w->start)->charpos;
19542 startpos_byte = marker_byte_position (w->start);
19543 height = WINDOW_TOTAL_LINES (w);
19544
19545 /* If we decided that this buffer isn't suitable for line numbers,
19546 don't forget that too fast. */
19547 if (EQ (w->base_line_pos, w->buffer))
19548 goto no_value;
19549 /* But do forget it, if the window shows a different buffer now. */
19550 else if (BUFFERP (w->base_line_pos))
19551 w->base_line_pos = Qnil;
19552
19553 /* If the buffer is very big, don't waste time. */
19554 if (INTEGERP (Vline_number_display_limit)
19555 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19556 {
19557 w->base_line_pos = Qnil;
19558 w->base_line_number = Qnil;
19559 goto no_value;
19560 }
19561
19562 if (INTEGERP (w->base_line_number)
19563 && INTEGERP (w->base_line_pos)
19564 && XFASTINT (w->base_line_pos) <= startpos)
19565 {
19566 line = XFASTINT (w->base_line_number);
19567 linepos = XFASTINT (w->base_line_pos);
19568 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19569 }
19570 else
19571 {
19572 line = 1;
19573 linepos = BUF_BEGV (b);
19574 linepos_byte = BUF_BEGV_BYTE (b);
19575 }
19576
19577 /* Count lines from base line to window start position. */
19578 nlines = display_count_lines (linepos, linepos_byte,
19579 startpos_byte,
19580 startpos, &junk);
19581
19582 topline = nlines + line;
19583
19584 /* Determine a new base line, if the old one is too close
19585 or too far away, or if we did not have one.
19586 "Too close" means it's plausible a scroll-down would
19587 go back past it. */
19588 if (startpos == BUF_BEGV (b))
19589 {
19590 w->base_line_number = make_number (topline);
19591 w->base_line_pos = make_number (BUF_BEGV (b));
19592 }
19593 else if (nlines < height + 25 || nlines > height * 3 + 50
19594 || linepos == BUF_BEGV (b))
19595 {
19596 EMACS_INT limit = BUF_BEGV (b);
19597 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
19598 EMACS_INT position;
19599 int distance = (height * 2 + 30) * line_number_display_limit_width;
19600
19601 if (startpos - distance > limit)
19602 {
19603 limit = startpos - distance;
19604 limit_byte = CHAR_TO_BYTE (limit);
19605 }
19606
19607 nlines = display_count_lines (startpos, startpos_byte,
19608 limit_byte,
19609 - (height * 2 + 30),
19610 &position);
19611 /* If we couldn't find the lines we wanted within
19612 line_number_display_limit_width chars per line,
19613 give up on line numbers for this window. */
19614 if (position == limit_byte && limit == startpos - distance)
19615 {
19616 w->base_line_pos = w->buffer;
19617 w->base_line_number = Qnil;
19618 goto no_value;
19619 }
19620
19621 w->base_line_number = make_number (topline - nlines);
19622 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19623 }
19624
19625 /* Now count lines from the start pos to point. */
19626 nlines = display_count_lines (startpos, startpos_byte,
19627 PT_BYTE, PT, &junk);
19628
19629 /* Record that we did display the line number. */
19630 line_number_displayed = 1;
19631
19632 /* Make the string to show. */
19633 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19634 return decode_mode_spec_buf;
19635 no_value:
19636 {
19637 char* p = decode_mode_spec_buf;
19638 int pad = field_width - 2;
19639 while (pad-- > 0)
19640 *p++ = ' ';
19641 *p++ = '?';
19642 *p++ = '?';
19643 *p = '\0';
19644 return decode_mode_spec_buf;
19645 }
19646 }
19647 break;
19648
19649 case 'm':
19650 obj = b->mode_name;
19651 break;
19652
19653 case 'n':
19654 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19655 return " Narrow";
19656 break;
19657
19658 case 'p':
19659 {
19660 EMACS_INT pos = marker_position (w->start);
19661 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19662
19663 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19664 {
19665 if (pos <= BUF_BEGV (b))
19666 return "All";
19667 else
19668 return "Bottom";
19669 }
19670 else if (pos <= BUF_BEGV (b))
19671 return "Top";
19672 else
19673 {
19674 if (total > 1000000)
19675 /* Do it differently for a large value, to avoid overflow. */
19676 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19677 else
19678 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19679 /* We can't normally display a 3-digit number,
19680 so get us a 2-digit number that is close. */
19681 if (total == 100)
19682 total = 99;
19683 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19684 return decode_mode_spec_buf;
19685 }
19686 }
19687
19688 /* Display percentage of size above the bottom of the screen. */
19689 case 'P':
19690 {
19691 EMACS_INT toppos = marker_position (w->start);
19692 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19693 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19694
19695 if (botpos >= BUF_ZV (b))
19696 {
19697 if (toppos <= BUF_BEGV (b))
19698 return "All";
19699 else
19700 return "Bottom";
19701 }
19702 else
19703 {
19704 if (total > 1000000)
19705 /* Do it differently for a large value, to avoid overflow. */
19706 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19707 else
19708 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19709 /* We can't normally display a 3-digit number,
19710 so get us a 2-digit number that is close. */
19711 if (total == 100)
19712 total = 99;
19713 if (toppos <= BUF_BEGV (b))
19714 sprintf (decode_mode_spec_buf, "Top%2ld%%", (long)total);
19715 else
19716 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19717 return decode_mode_spec_buf;
19718 }
19719 }
19720
19721 case 's':
19722 /* status of process */
19723 obj = Fget_buffer_process (Fcurrent_buffer ());
19724 if (NILP (obj))
19725 return "no process";
19726 #ifndef MSDOS
19727 obj = Fsymbol_name (Fprocess_status (obj));
19728 #endif
19729 break;
19730
19731 case '@':
19732 {
19733 int count = inhibit_garbage_collection ();
19734 Lisp_Object val = call1 (intern ("file-remote-p"),
19735 current_buffer->directory);
19736 unbind_to (count, Qnil);
19737
19738 if (NILP (val))
19739 return "-";
19740 else
19741 return "@";
19742 }
19743
19744 case 't': /* indicate TEXT or BINARY */
19745 #ifdef MODE_LINE_BINARY_TEXT
19746 return MODE_LINE_BINARY_TEXT (b);
19747 #else
19748 return "T";
19749 #endif
19750
19751 case 'z':
19752 /* coding-system (not including end-of-line format) */
19753 case 'Z':
19754 /* coding-system (including end-of-line type) */
19755 {
19756 int eol_flag = (c == 'Z');
19757 char *p = decode_mode_spec_buf;
19758
19759 if (! FRAME_WINDOW_P (f))
19760 {
19761 /* No need to mention EOL here--the terminal never needs
19762 to do EOL conversion. */
19763 p = decode_mode_spec_coding (CODING_ID_NAME
19764 (FRAME_KEYBOARD_CODING (f)->id),
19765 p, 0);
19766 p = decode_mode_spec_coding (CODING_ID_NAME
19767 (FRAME_TERMINAL_CODING (f)->id),
19768 p, 0);
19769 }
19770 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19771 p, eol_flag);
19772
19773 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19774 #ifdef subprocesses
19775 obj = Fget_buffer_process (Fcurrent_buffer ());
19776 if (PROCESSP (obj))
19777 {
19778 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19779 p, eol_flag);
19780 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19781 p, eol_flag);
19782 }
19783 #endif /* subprocesses */
19784 #endif /* 0 */
19785 *p = 0;
19786 return decode_mode_spec_buf;
19787 }
19788 }
19789
19790 if (STRINGP (obj))
19791 {
19792 *string = obj;
19793 return (char *) SDATA (obj);
19794 }
19795 else
19796 return "";
19797 }
19798
19799
19800 /* Count up to COUNT lines starting from START / START_BYTE.
19801 But don't go beyond LIMIT_BYTE.
19802 Return the number of lines thus found (always nonnegative).
19803
19804 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19805
19806 static int
19807 display_count_lines (EMACS_INT start, EMACS_INT start_byte,
19808 EMACS_INT limit_byte, int count,
19809 EMACS_INT *byte_pos_ptr)
19810 {
19811 register unsigned char *cursor;
19812 unsigned char *base;
19813
19814 register int ceiling;
19815 register unsigned char *ceiling_addr;
19816 int orig_count = count;
19817
19818 /* If we are not in selective display mode,
19819 check only for newlines. */
19820 int selective_display = (!NILP (current_buffer->selective_display)
19821 && !INTEGERP (current_buffer->selective_display));
19822
19823 if (count > 0)
19824 {
19825 while (start_byte < limit_byte)
19826 {
19827 ceiling = BUFFER_CEILING_OF (start_byte);
19828 ceiling = min (limit_byte - 1, ceiling);
19829 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19830 base = (cursor = BYTE_POS_ADDR (start_byte));
19831 while (1)
19832 {
19833 if (selective_display)
19834 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19835 ;
19836 else
19837 while (*cursor != '\n' && ++cursor != ceiling_addr)
19838 ;
19839
19840 if (cursor != ceiling_addr)
19841 {
19842 if (--count == 0)
19843 {
19844 start_byte += cursor - base + 1;
19845 *byte_pos_ptr = start_byte;
19846 return orig_count;
19847 }
19848 else
19849 if (++cursor == ceiling_addr)
19850 break;
19851 }
19852 else
19853 break;
19854 }
19855 start_byte += cursor - base;
19856 }
19857 }
19858 else
19859 {
19860 while (start_byte > limit_byte)
19861 {
19862 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19863 ceiling = max (limit_byte, ceiling);
19864 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19865 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19866 while (1)
19867 {
19868 if (selective_display)
19869 while (--cursor != ceiling_addr
19870 && *cursor != '\n' && *cursor != 015)
19871 ;
19872 else
19873 while (--cursor != ceiling_addr && *cursor != '\n')
19874 ;
19875
19876 if (cursor != ceiling_addr)
19877 {
19878 if (++count == 0)
19879 {
19880 start_byte += cursor - base + 1;
19881 *byte_pos_ptr = start_byte;
19882 /* When scanning backwards, we should
19883 not count the newline posterior to which we stop. */
19884 return - orig_count - 1;
19885 }
19886 }
19887 else
19888 break;
19889 }
19890 /* Here we add 1 to compensate for the last decrement
19891 of CURSOR, which took it past the valid range. */
19892 start_byte += cursor - base + 1;
19893 }
19894 }
19895
19896 *byte_pos_ptr = limit_byte;
19897
19898 if (count < 0)
19899 return - orig_count + count;
19900 return orig_count - count;
19901
19902 }
19903
19904
19905 \f
19906 /***********************************************************************
19907 Displaying strings
19908 ***********************************************************************/
19909
19910 /* Display a NUL-terminated string, starting with index START.
19911
19912 If STRING is non-null, display that C string. Otherwise, the Lisp
19913 string LISP_STRING is displayed. There's a case that STRING is
19914 non-null and LISP_STRING is not nil. It means STRING is a string
19915 data of LISP_STRING. In that case, we display LISP_STRING while
19916 ignoring its text properties.
19917
19918 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19919 FACE_STRING. Display STRING or LISP_STRING with the face at
19920 FACE_STRING_POS in FACE_STRING:
19921
19922 Display the string in the environment given by IT, but use the
19923 standard display table, temporarily.
19924
19925 FIELD_WIDTH is the minimum number of output glyphs to produce.
19926 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19927 with spaces. If STRING has more characters, more than FIELD_WIDTH
19928 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19929
19930 PRECISION is the maximum number of characters to output from
19931 STRING. PRECISION < 0 means don't truncate the string.
19932
19933 This is roughly equivalent to printf format specifiers:
19934
19935 FIELD_WIDTH PRECISION PRINTF
19936 ----------------------------------------
19937 -1 -1 %s
19938 -1 10 %.10s
19939 10 -1 %10s
19940 20 10 %20.10s
19941
19942 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19943 display them, and < 0 means obey the current buffer's value of
19944 enable_multibyte_characters.
19945
19946 Value is the number of columns displayed. */
19947
19948 static int
19949 display_string (const unsigned char *string, Lisp_Object lisp_string, Lisp_Object face_string,
19950 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
19951 int field_width, int precision, int max_x, int multibyte)
19952 {
19953 int hpos_at_start = it->hpos;
19954 int saved_face_id = it->face_id;
19955 struct glyph_row *row = it->glyph_row;
19956
19957 /* Initialize the iterator IT for iteration over STRING beginning
19958 with index START. */
19959 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19960 precision, field_width, multibyte);
19961 if (string && STRINGP (lisp_string))
19962 /* LISP_STRING is the one returned by decode_mode_spec. We should
19963 ignore its text properties. */
19964 it->stop_charpos = -1;
19965
19966 /* If displaying STRING, set up the face of the iterator
19967 from LISP_STRING, if that's given. */
19968 if (STRINGP (face_string))
19969 {
19970 EMACS_INT endptr;
19971 struct face *face;
19972
19973 it->face_id
19974 = face_at_string_position (it->w, face_string, face_string_pos,
19975 0, it->region_beg_charpos,
19976 it->region_end_charpos,
19977 &endptr, it->base_face_id, 0);
19978 face = FACE_FROM_ID (it->f, it->face_id);
19979 it->face_box_p = face->box != FACE_NO_BOX;
19980 }
19981
19982 /* Set max_x to the maximum allowed X position. Don't let it go
19983 beyond the right edge of the window. */
19984 if (max_x <= 0)
19985 max_x = it->last_visible_x;
19986 else
19987 max_x = min (max_x, it->last_visible_x);
19988
19989 /* Skip over display elements that are not visible. because IT->w is
19990 hscrolled. */
19991 if (it->current_x < it->first_visible_x)
19992 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19993 MOVE_TO_POS | MOVE_TO_X);
19994
19995 row->ascent = it->max_ascent;
19996 row->height = it->max_ascent + it->max_descent;
19997 row->phys_ascent = it->max_phys_ascent;
19998 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19999 row->extra_line_spacing = it->max_extra_line_spacing;
20000
20001 /* This condition is for the case that we are called with current_x
20002 past last_visible_x. */
20003 while (it->current_x < max_x)
20004 {
20005 int x_before, x, n_glyphs_before, i, nglyphs;
20006
20007 /* Get the next display element. */
20008 if (!get_next_display_element (it))
20009 break;
20010
20011 /* Produce glyphs. */
20012 x_before = it->current_x;
20013 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
20014 PRODUCE_GLYPHS (it);
20015
20016 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
20017 i = 0;
20018 x = x_before;
20019 while (i < nglyphs)
20020 {
20021 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20022
20023 if (it->line_wrap != TRUNCATE
20024 && x + glyph->pixel_width > max_x)
20025 {
20026 /* End of continued line or max_x reached. */
20027 if (CHAR_GLYPH_PADDING_P (*glyph))
20028 {
20029 /* A wide character is unbreakable. */
20030 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
20031 it->current_x = x_before;
20032 }
20033 else
20034 {
20035 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
20036 it->current_x = x;
20037 }
20038 break;
20039 }
20040 else if (x + glyph->pixel_width >= it->first_visible_x)
20041 {
20042 /* Glyph is at least partially visible. */
20043 ++it->hpos;
20044 if (x < it->first_visible_x)
20045 it->glyph_row->x = x - it->first_visible_x;
20046 }
20047 else
20048 {
20049 /* Glyph is off the left margin of the display area.
20050 Should not happen. */
20051 abort ();
20052 }
20053
20054 row->ascent = max (row->ascent, it->max_ascent);
20055 row->height = max (row->height, it->max_ascent + it->max_descent);
20056 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20057 row->phys_height = max (row->phys_height,
20058 it->max_phys_ascent + it->max_phys_descent);
20059 row->extra_line_spacing = max (row->extra_line_spacing,
20060 it->max_extra_line_spacing);
20061 x += glyph->pixel_width;
20062 ++i;
20063 }
20064
20065 /* Stop if max_x reached. */
20066 if (i < nglyphs)
20067 break;
20068
20069 /* Stop at line ends. */
20070 if (ITERATOR_AT_END_OF_LINE_P (it))
20071 {
20072 it->continuation_lines_width = 0;
20073 break;
20074 }
20075
20076 set_iterator_to_next (it, 1);
20077
20078 /* Stop if truncating at the right edge. */
20079 if (it->line_wrap == TRUNCATE
20080 && it->current_x >= it->last_visible_x)
20081 {
20082 /* Add truncation mark, but don't do it if the line is
20083 truncated at a padding space. */
20084 if (IT_CHARPOS (*it) < it->string_nchars)
20085 {
20086 if (!FRAME_WINDOW_P (it->f))
20087 {
20088 int i, n;
20089
20090 if (it->current_x > it->last_visible_x)
20091 {
20092 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20093 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20094 break;
20095 for (n = row->used[TEXT_AREA]; i < n; ++i)
20096 {
20097 row->used[TEXT_AREA] = i;
20098 produce_special_glyphs (it, IT_TRUNCATION);
20099 }
20100 }
20101 produce_special_glyphs (it, IT_TRUNCATION);
20102 }
20103 it->glyph_row->truncated_on_right_p = 1;
20104 }
20105 break;
20106 }
20107 }
20108
20109 /* Maybe insert a truncation at the left. */
20110 if (it->first_visible_x
20111 && IT_CHARPOS (*it) > 0)
20112 {
20113 if (!FRAME_WINDOW_P (it->f))
20114 insert_left_trunc_glyphs (it);
20115 it->glyph_row->truncated_on_left_p = 1;
20116 }
20117
20118 it->face_id = saved_face_id;
20119
20120 /* Value is number of columns displayed. */
20121 return it->hpos - hpos_at_start;
20122 }
20123
20124
20125 \f
20126 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20127 appears as an element of LIST or as the car of an element of LIST.
20128 If PROPVAL is a list, compare each element against LIST in that
20129 way, and return 1/2 if any element of PROPVAL is found in LIST.
20130 Otherwise return 0. This function cannot quit.
20131 The return value is 2 if the text is invisible but with an ellipsis
20132 and 1 if it's invisible and without an ellipsis. */
20133
20134 int
20135 invisible_p (register Lisp_Object propval, Lisp_Object list)
20136 {
20137 register Lisp_Object tail, proptail;
20138
20139 for (tail = list; CONSP (tail); tail = XCDR (tail))
20140 {
20141 register Lisp_Object tem;
20142 tem = XCAR (tail);
20143 if (EQ (propval, tem))
20144 return 1;
20145 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20146 return NILP (XCDR (tem)) ? 1 : 2;
20147 }
20148
20149 if (CONSP (propval))
20150 {
20151 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20152 {
20153 Lisp_Object propelt;
20154 propelt = XCAR (proptail);
20155 for (tail = list; CONSP (tail); tail = XCDR (tail))
20156 {
20157 register Lisp_Object tem;
20158 tem = XCAR (tail);
20159 if (EQ (propelt, tem))
20160 return 1;
20161 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20162 return NILP (XCDR (tem)) ? 1 : 2;
20163 }
20164 }
20165 }
20166
20167 return 0;
20168 }
20169
20170 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20171 doc: /* Non-nil if the property makes the text invisible.
20172 POS-OR-PROP can be a marker or number, in which case it is taken to be
20173 a position in the current buffer and the value of the `invisible' property
20174 is checked; or it can be some other value, which is then presumed to be the
20175 value of the `invisible' property of the text of interest.
20176 The non-nil value returned can be t for truly invisible text or something
20177 else if the text is replaced by an ellipsis. */)
20178 (Lisp_Object pos_or_prop)
20179 {
20180 Lisp_Object prop
20181 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20182 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20183 : pos_or_prop);
20184 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20185 return (invis == 0 ? Qnil
20186 : invis == 1 ? Qt
20187 : make_number (invis));
20188 }
20189
20190 /* Calculate a width or height in pixels from a specification using
20191 the following elements:
20192
20193 SPEC ::=
20194 NUM - a (fractional) multiple of the default font width/height
20195 (NUM) - specifies exactly NUM pixels
20196 UNIT - a fixed number of pixels, see below.
20197 ELEMENT - size of a display element in pixels, see below.
20198 (NUM . SPEC) - equals NUM * SPEC
20199 (+ SPEC SPEC ...) - add pixel values
20200 (- SPEC SPEC ...) - subtract pixel values
20201 (- SPEC) - negate pixel value
20202
20203 NUM ::=
20204 INT or FLOAT - a number constant
20205 SYMBOL - use symbol's (buffer local) variable binding.
20206
20207 UNIT ::=
20208 in - pixels per inch *)
20209 mm - pixels per 1/1000 meter *)
20210 cm - pixels per 1/100 meter *)
20211 width - width of current font in pixels.
20212 height - height of current font in pixels.
20213
20214 *) using the ratio(s) defined in display-pixels-per-inch.
20215
20216 ELEMENT ::=
20217
20218 left-fringe - left fringe width in pixels
20219 right-fringe - right fringe width in pixels
20220
20221 left-margin - left margin width in pixels
20222 right-margin - right margin width in pixels
20223
20224 scroll-bar - scroll-bar area width in pixels
20225
20226 Examples:
20227
20228 Pixels corresponding to 5 inches:
20229 (5 . in)
20230
20231 Total width of non-text areas on left side of window (if scroll-bar is on left):
20232 '(space :width (+ left-fringe left-margin scroll-bar))
20233
20234 Align to first text column (in header line):
20235 '(space :align-to 0)
20236
20237 Align to middle of text area minus half the width of variable `my-image'
20238 containing a loaded image:
20239 '(space :align-to (0.5 . (- text my-image)))
20240
20241 Width of left margin minus width of 1 character in the default font:
20242 '(space :width (- left-margin 1))
20243
20244 Width of left margin minus width of 2 characters in the current font:
20245 '(space :width (- left-margin (2 . width)))
20246
20247 Center 1 character over left-margin (in header line):
20248 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20249
20250 Different ways to express width of left fringe plus left margin minus one pixel:
20251 '(space :width (- (+ left-fringe left-margin) (1)))
20252 '(space :width (+ left-fringe left-margin (- (1))))
20253 '(space :width (+ left-fringe left-margin (-1)))
20254
20255 */
20256
20257 #define NUMVAL(X) \
20258 ((INTEGERP (X) || FLOATP (X)) \
20259 ? XFLOATINT (X) \
20260 : - 1)
20261
20262 int
20263 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20264 struct font *font, int width_p, int *align_to)
20265 {
20266 double pixels;
20267
20268 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20269 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20270
20271 if (NILP (prop))
20272 return OK_PIXELS (0);
20273
20274 xassert (FRAME_LIVE_P (it->f));
20275
20276 if (SYMBOLP (prop))
20277 {
20278 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20279 {
20280 char *unit = SDATA (SYMBOL_NAME (prop));
20281
20282 if (unit[0] == 'i' && unit[1] == 'n')
20283 pixels = 1.0;
20284 else if (unit[0] == 'm' && unit[1] == 'm')
20285 pixels = 25.4;
20286 else if (unit[0] == 'c' && unit[1] == 'm')
20287 pixels = 2.54;
20288 else
20289 pixels = 0;
20290 if (pixels > 0)
20291 {
20292 double ppi;
20293 #ifdef HAVE_WINDOW_SYSTEM
20294 if (FRAME_WINDOW_P (it->f)
20295 && (ppi = (width_p
20296 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20297 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20298 ppi > 0))
20299 return OK_PIXELS (ppi / pixels);
20300 #endif
20301
20302 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20303 || (CONSP (Vdisplay_pixels_per_inch)
20304 && (ppi = (width_p
20305 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20306 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20307 ppi > 0)))
20308 return OK_PIXELS (ppi / pixels);
20309
20310 return 0;
20311 }
20312 }
20313
20314 #ifdef HAVE_WINDOW_SYSTEM
20315 if (EQ (prop, Qheight))
20316 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20317 if (EQ (prop, Qwidth))
20318 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20319 #else
20320 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20321 return OK_PIXELS (1);
20322 #endif
20323
20324 if (EQ (prop, Qtext))
20325 return OK_PIXELS (width_p
20326 ? window_box_width (it->w, TEXT_AREA)
20327 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20328
20329 if (align_to && *align_to < 0)
20330 {
20331 *res = 0;
20332 if (EQ (prop, Qleft))
20333 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20334 if (EQ (prop, Qright))
20335 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20336 if (EQ (prop, Qcenter))
20337 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20338 + window_box_width (it->w, TEXT_AREA) / 2);
20339 if (EQ (prop, Qleft_fringe))
20340 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20341 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20342 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20343 if (EQ (prop, Qright_fringe))
20344 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20345 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20346 : window_box_right_offset (it->w, TEXT_AREA));
20347 if (EQ (prop, Qleft_margin))
20348 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20349 if (EQ (prop, Qright_margin))
20350 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20351 if (EQ (prop, Qscroll_bar))
20352 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20353 ? 0
20354 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20355 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20356 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20357 : 0)));
20358 }
20359 else
20360 {
20361 if (EQ (prop, Qleft_fringe))
20362 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20363 if (EQ (prop, Qright_fringe))
20364 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20365 if (EQ (prop, Qleft_margin))
20366 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20367 if (EQ (prop, Qright_margin))
20368 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20369 if (EQ (prop, Qscroll_bar))
20370 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20371 }
20372
20373 prop = Fbuffer_local_value (prop, it->w->buffer);
20374 }
20375
20376 if (INTEGERP (prop) || FLOATP (prop))
20377 {
20378 int base_unit = (width_p
20379 ? FRAME_COLUMN_WIDTH (it->f)
20380 : FRAME_LINE_HEIGHT (it->f));
20381 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20382 }
20383
20384 if (CONSP (prop))
20385 {
20386 Lisp_Object car = XCAR (prop);
20387 Lisp_Object cdr = XCDR (prop);
20388
20389 if (SYMBOLP (car))
20390 {
20391 #ifdef HAVE_WINDOW_SYSTEM
20392 if (FRAME_WINDOW_P (it->f)
20393 && valid_image_p (prop))
20394 {
20395 int id = lookup_image (it->f, prop);
20396 struct image *img = IMAGE_FROM_ID (it->f, id);
20397
20398 return OK_PIXELS (width_p ? img->width : img->height);
20399 }
20400 #endif
20401 if (EQ (car, Qplus) || EQ (car, Qminus))
20402 {
20403 int first = 1;
20404 double px;
20405
20406 pixels = 0;
20407 while (CONSP (cdr))
20408 {
20409 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20410 font, width_p, align_to))
20411 return 0;
20412 if (first)
20413 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20414 else
20415 pixels += px;
20416 cdr = XCDR (cdr);
20417 }
20418 if (EQ (car, Qminus))
20419 pixels = -pixels;
20420 return OK_PIXELS (pixels);
20421 }
20422
20423 car = Fbuffer_local_value (car, it->w->buffer);
20424 }
20425
20426 if (INTEGERP (car) || FLOATP (car))
20427 {
20428 double fact;
20429 pixels = XFLOATINT (car);
20430 if (NILP (cdr))
20431 return OK_PIXELS (pixels);
20432 if (calc_pixel_width_or_height (&fact, it, cdr,
20433 font, width_p, align_to))
20434 return OK_PIXELS (pixels * fact);
20435 return 0;
20436 }
20437
20438 return 0;
20439 }
20440
20441 return 0;
20442 }
20443
20444 \f
20445 /***********************************************************************
20446 Glyph Display
20447 ***********************************************************************/
20448
20449 #ifdef HAVE_WINDOW_SYSTEM
20450
20451 #if GLYPH_DEBUG
20452
20453 void
20454 dump_glyph_string (s)
20455 struct glyph_string *s;
20456 {
20457 fprintf (stderr, "glyph string\n");
20458 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20459 s->x, s->y, s->width, s->height);
20460 fprintf (stderr, " ybase = %d\n", s->ybase);
20461 fprintf (stderr, " hl = %d\n", s->hl);
20462 fprintf (stderr, " left overhang = %d, right = %d\n",
20463 s->left_overhang, s->right_overhang);
20464 fprintf (stderr, " nchars = %d\n", s->nchars);
20465 fprintf (stderr, " extends to end of line = %d\n",
20466 s->extends_to_end_of_line_p);
20467 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20468 fprintf (stderr, " bg width = %d\n", s->background_width);
20469 }
20470
20471 #endif /* GLYPH_DEBUG */
20472
20473 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20474 of XChar2b structures for S; it can't be allocated in
20475 init_glyph_string because it must be allocated via `alloca'. W
20476 is the window on which S is drawn. ROW and AREA are the glyph row
20477 and area within the row from which S is constructed. START is the
20478 index of the first glyph structure covered by S. HL is a
20479 face-override for drawing S. */
20480
20481 #ifdef HAVE_NTGUI
20482 #define OPTIONAL_HDC(hdc) HDC hdc,
20483 #define DECLARE_HDC(hdc) HDC hdc;
20484 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20485 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20486 #endif
20487
20488 #ifndef OPTIONAL_HDC
20489 #define OPTIONAL_HDC(hdc)
20490 #define DECLARE_HDC(hdc)
20491 #define ALLOCATE_HDC(hdc, f)
20492 #define RELEASE_HDC(hdc, f)
20493 #endif
20494
20495 static void
20496 init_glyph_string (struct glyph_string *s,
20497 OPTIONAL_HDC (hdc)
20498 XChar2b *char2b, struct window *w, struct glyph_row *row,
20499 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20500 {
20501 memset (s, 0, sizeof *s);
20502 s->w = w;
20503 s->f = XFRAME (w->frame);
20504 #ifdef HAVE_NTGUI
20505 s->hdc = hdc;
20506 #endif
20507 s->display = FRAME_X_DISPLAY (s->f);
20508 s->window = FRAME_X_WINDOW (s->f);
20509 s->char2b = char2b;
20510 s->hl = hl;
20511 s->row = row;
20512 s->area = area;
20513 s->first_glyph = row->glyphs[area] + start;
20514 s->height = row->height;
20515 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20516 s->ybase = s->y + row->ascent;
20517 }
20518
20519
20520 /* Append the list of glyph strings with head H and tail T to the list
20521 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20522
20523 static INLINE void
20524 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20525 struct glyph_string *h, struct glyph_string *t)
20526 {
20527 if (h)
20528 {
20529 if (*head)
20530 (*tail)->next = h;
20531 else
20532 *head = h;
20533 h->prev = *tail;
20534 *tail = t;
20535 }
20536 }
20537
20538
20539 /* Prepend the list of glyph strings with head H and tail T to the
20540 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20541 result. */
20542
20543 static INLINE void
20544 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20545 struct glyph_string *h, struct glyph_string *t)
20546 {
20547 if (h)
20548 {
20549 if (*head)
20550 (*head)->prev = t;
20551 else
20552 *tail = t;
20553 t->next = *head;
20554 *head = h;
20555 }
20556 }
20557
20558
20559 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20560 Set *HEAD and *TAIL to the resulting list. */
20561
20562 static INLINE void
20563 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20564 struct glyph_string *s)
20565 {
20566 s->next = s->prev = NULL;
20567 append_glyph_string_lists (head, tail, s, s);
20568 }
20569
20570
20571 /* Get face and two-byte form of character C in face FACE_ID on frame
20572 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20573 means we want to display multibyte text. DISPLAY_P non-zero means
20574 make sure that X resources for the face returned are allocated.
20575 Value is a pointer to a realized face that is ready for display if
20576 DISPLAY_P is non-zero. */
20577
20578 static INLINE struct face *
20579 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20580 XChar2b *char2b, int multibyte_p, int display_p)
20581 {
20582 struct face *face = FACE_FROM_ID (f, face_id);
20583
20584 if (face->font)
20585 {
20586 unsigned code = face->font->driver->encode_char (face->font, c);
20587
20588 if (code != FONT_INVALID_CODE)
20589 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20590 else
20591 STORE_XCHAR2B (char2b, 0, 0);
20592 }
20593
20594 /* Make sure X resources of the face are allocated. */
20595 #ifdef HAVE_X_WINDOWS
20596 if (display_p)
20597 #endif
20598 {
20599 xassert (face != NULL);
20600 PREPARE_FACE_FOR_DISPLAY (f, face);
20601 }
20602
20603 return face;
20604 }
20605
20606
20607 /* Get face and two-byte form of character glyph GLYPH on frame F.
20608 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20609 a pointer to a realized face that is ready for display. */
20610
20611 static INLINE struct face *
20612 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20613 XChar2b *char2b, int *two_byte_p)
20614 {
20615 struct face *face;
20616
20617 xassert (glyph->type == CHAR_GLYPH);
20618 face = FACE_FROM_ID (f, glyph->face_id);
20619
20620 if (two_byte_p)
20621 *two_byte_p = 0;
20622
20623 if (face->font)
20624 {
20625 unsigned code;
20626
20627 if (CHAR_BYTE8_P (glyph->u.ch))
20628 code = CHAR_TO_BYTE8 (glyph->u.ch);
20629 else
20630 code = face->font->driver->encode_char (face->font, glyph->u.ch);
20631
20632 if (code != FONT_INVALID_CODE)
20633 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20634 else
20635 STORE_XCHAR2B (char2b, 0, 0);
20636 }
20637
20638 /* Make sure X resources of the face are allocated. */
20639 xassert (face != NULL);
20640 PREPARE_FACE_FOR_DISPLAY (f, face);
20641 return face;
20642 }
20643
20644
20645 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
20646 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
20647
20648 static INLINE int
20649 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
20650 {
20651 unsigned code;
20652
20653 if (CHAR_BYTE8_P (c))
20654 code = CHAR_TO_BYTE8 (c);
20655 else
20656 code = font->driver->encode_char (font, c);
20657
20658 if (code == FONT_INVALID_CODE)
20659 return 0;
20660 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20661 return 1;
20662 }
20663
20664
20665 /* Fill glyph string S with composition components specified by S->cmp.
20666
20667 BASE_FACE is the base face of the composition.
20668 S->cmp_from is the index of the first component for S.
20669
20670 OVERLAPS non-zero means S should draw the foreground only, and use
20671 its physical height for clipping. See also draw_glyphs.
20672
20673 Value is the index of a component not in S. */
20674
20675 static int
20676 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20677 int overlaps)
20678 {
20679 int i;
20680 /* For all glyphs of this composition, starting at the offset
20681 S->cmp_from, until we reach the end of the definition or encounter a
20682 glyph that requires the different face, add it to S. */
20683 struct face *face;
20684
20685 xassert (s);
20686
20687 s->for_overlaps = overlaps;
20688 s->face = NULL;
20689 s->font = NULL;
20690 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20691 {
20692 int c = COMPOSITION_GLYPH (s->cmp, i);
20693
20694 if (c != '\t')
20695 {
20696 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20697 -1, Qnil);
20698
20699 face = get_char_face_and_encoding (s->f, c, face_id,
20700 s->char2b + i, 1, 1);
20701 if (face)
20702 {
20703 if (! s->face)
20704 {
20705 s->face = face;
20706 s->font = s->face->font;
20707 }
20708 else if (s->face != face)
20709 break;
20710 }
20711 }
20712 ++s->nchars;
20713 }
20714 s->cmp_to = i;
20715
20716 /* All glyph strings for the same composition has the same width,
20717 i.e. the width set for the first component of the composition. */
20718 s->width = s->first_glyph->pixel_width;
20719
20720 /* If the specified font could not be loaded, use the frame's
20721 default font, but record the fact that we couldn't load it in
20722 the glyph string so that we can draw rectangles for the
20723 characters of the glyph string. */
20724 if (s->font == NULL)
20725 {
20726 s->font_not_found_p = 1;
20727 s->font = FRAME_FONT (s->f);
20728 }
20729
20730 /* Adjust base line for subscript/superscript text. */
20731 s->ybase += s->first_glyph->voffset;
20732
20733 /* This glyph string must always be drawn with 16-bit functions. */
20734 s->two_byte_p = 1;
20735
20736 return s->cmp_to;
20737 }
20738
20739 static int
20740 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
20741 int start, int end, int overlaps)
20742 {
20743 struct glyph *glyph, *last;
20744 Lisp_Object lgstring;
20745 int i;
20746
20747 s->for_overlaps = overlaps;
20748 glyph = s->row->glyphs[s->area] + start;
20749 last = s->row->glyphs[s->area] + end;
20750 s->cmp_id = glyph->u.cmp.id;
20751 s->cmp_from = glyph->slice.cmp.from;
20752 s->cmp_to = glyph->slice.cmp.to + 1;
20753 s->face = FACE_FROM_ID (s->f, face_id);
20754 lgstring = composition_gstring_from_id (s->cmp_id);
20755 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20756 glyph++;
20757 while (glyph < last
20758 && glyph->u.cmp.automatic
20759 && glyph->u.cmp.id == s->cmp_id
20760 && s->cmp_to == glyph->slice.cmp.from)
20761 s->cmp_to = (glyph++)->slice.cmp.to + 1;
20762
20763 for (i = s->cmp_from; i < s->cmp_to; i++)
20764 {
20765 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20766 unsigned code = LGLYPH_CODE (lglyph);
20767
20768 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20769 }
20770 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20771 return glyph - s->row->glyphs[s->area];
20772 }
20773
20774
20775 /* Fill glyph string S from a sequence glyphs for glyphless characters.
20776 See the comment of fill_glyph_string for arguments.
20777 Value is the index of the first glyph not in S. */
20778
20779
20780 static int
20781 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
20782 int start, int end, int overlaps)
20783 {
20784 struct glyph *glyph, *last;
20785 int voffset;
20786
20787 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
20788 s->for_overlaps = overlaps;
20789 glyph = s->row->glyphs[s->area] + start;
20790 last = s->row->glyphs[s->area] + end;
20791 voffset = glyph->voffset;
20792 s->face = FACE_FROM_ID (s->f, face_id);
20793 s->font = s->face->font;
20794 s->nchars = 1;
20795 s->width = glyph->pixel_width;
20796 glyph++;
20797 while (glyph < last
20798 && glyph->type == GLYPHLESS_GLYPH
20799 && glyph->voffset == voffset
20800 && glyph->face_id == face_id)
20801 {
20802 s->nchars++;
20803 s->width += glyph->pixel_width;
20804 glyph++;
20805 }
20806 s->ybase += voffset;
20807 return glyph - s->row->glyphs[s->area];
20808 }
20809
20810
20811 /* Fill glyph string S from a sequence of character glyphs.
20812
20813 FACE_ID is the face id of the string. START is the index of the
20814 first glyph to consider, END is the index of the last + 1.
20815 OVERLAPS non-zero means S should draw the foreground only, and use
20816 its physical height for clipping. See also draw_glyphs.
20817
20818 Value is the index of the first glyph not in S. */
20819
20820 static int
20821 fill_glyph_string (struct glyph_string *s, int face_id,
20822 int start, int end, int overlaps)
20823 {
20824 struct glyph *glyph, *last;
20825 int voffset;
20826 int glyph_not_available_p;
20827
20828 xassert (s->f == XFRAME (s->w->frame));
20829 xassert (s->nchars == 0);
20830 xassert (start >= 0 && end > start);
20831
20832 s->for_overlaps = overlaps;
20833 glyph = s->row->glyphs[s->area] + start;
20834 last = s->row->glyphs[s->area] + end;
20835 voffset = glyph->voffset;
20836 s->padding_p = glyph->padding_p;
20837 glyph_not_available_p = glyph->glyph_not_available_p;
20838
20839 while (glyph < last
20840 && glyph->type == CHAR_GLYPH
20841 && glyph->voffset == voffset
20842 /* Same face id implies same font, nowadays. */
20843 && glyph->face_id == face_id
20844 && glyph->glyph_not_available_p == glyph_not_available_p)
20845 {
20846 int two_byte_p;
20847
20848 s->face = get_glyph_face_and_encoding (s->f, glyph,
20849 s->char2b + s->nchars,
20850 &two_byte_p);
20851 s->two_byte_p = two_byte_p;
20852 ++s->nchars;
20853 xassert (s->nchars <= end - start);
20854 s->width += glyph->pixel_width;
20855 if (glyph++->padding_p != s->padding_p)
20856 break;
20857 }
20858
20859 s->font = s->face->font;
20860
20861 /* If the specified font could not be loaded, use the frame's font,
20862 but record the fact that we couldn't load it in
20863 S->font_not_found_p so that we can draw rectangles for the
20864 characters of the glyph string. */
20865 if (s->font == NULL || glyph_not_available_p)
20866 {
20867 s->font_not_found_p = 1;
20868 s->font = FRAME_FONT (s->f);
20869 }
20870
20871 /* Adjust base line for subscript/superscript text. */
20872 s->ybase += voffset;
20873
20874 xassert (s->face && s->face->gc);
20875 return glyph - s->row->glyphs[s->area];
20876 }
20877
20878
20879 /* Fill glyph string S from image glyph S->first_glyph. */
20880
20881 static void
20882 fill_image_glyph_string (struct glyph_string *s)
20883 {
20884 xassert (s->first_glyph->type == IMAGE_GLYPH);
20885 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20886 xassert (s->img);
20887 s->slice = s->first_glyph->slice.img;
20888 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20889 s->font = s->face->font;
20890 s->width = s->first_glyph->pixel_width;
20891
20892 /* Adjust base line for subscript/superscript text. */
20893 s->ybase += s->first_glyph->voffset;
20894 }
20895
20896
20897 /* Fill glyph string S from a sequence of stretch glyphs.
20898
20899 ROW is the glyph row in which the glyphs are found, AREA is the
20900 area within the row. START is the index of the first glyph to
20901 consider, END is the index of the last + 1.
20902
20903 Value is the index of the first glyph not in S. */
20904
20905 static int
20906 fill_stretch_glyph_string (struct glyph_string *s, struct glyph_row *row,
20907 enum glyph_row_area area, int start, int end)
20908 {
20909 struct glyph *glyph, *last;
20910 int voffset, face_id;
20911
20912 xassert (s->first_glyph->type == STRETCH_GLYPH);
20913
20914 glyph = s->row->glyphs[s->area] + start;
20915 last = s->row->glyphs[s->area] + end;
20916 face_id = glyph->face_id;
20917 s->face = FACE_FROM_ID (s->f, face_id);
20918 s->font = s->face->font;
20919 s->width = glyph->pixel_width;
20920 s->nchars = 1;
20921 voffset = glyph->voffset;
20922
20923 for (++glyph;
20924 (glyph < last
20925 && glyph->type == STRETCH_GLYPH
20926 && glyph->voffset == voffset
20927 && glyph->face_id == face_id);
20928 ++glyph)
20929 s->width += glyph->pixel_width;
20930
20931 /* Adjust base line for subscript/superscript text. */
20932 s->ybase += voffset;
20933
20934 /* The case that face->gc == 0 is handled when drawing the glyph
20935 string by calling PREPARE_FACE_FOR_DISPLAY. */
20936 xassert (s->face);
20937 return glyph - s->row->glyphs[s->area];
20938 }
20939
20940 static struct font_metrics *
20941 get_per_char_metric (struct frame *f, struct font *font, XChar2b *char2b)
20942 {
20943 static struct font_metrics metrics;
20944 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20945
20946 if (! font || code == FONT_INVALID_CODE)
20947 return NULL;
20948 font->driver->text_extents (font, &code, 1, &metrics);
20949 return &metrics;
20950 }
20951
20952 /* EXPORT for RIF:
20953 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20954 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20955 assumed to be zero. */
20956
20957 void
20958 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
20959 {
20960 *left = *right = 0;
20961
20962 if (glyph->type == CHAR_GLYPH)
20963 {
20964 struct face *face;
20965 XChar2b char2b;
20966 struct font_metrics *pcm;
20967
20968 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20969 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20970 {
20971 if (pcm->rbearing > pcm->width)
20972 *right = pcm->rbearing - pcm->width;
20973 if (pcm->lbearing < 0)
20974 *left = -pcm->lbearing;
20975 }
20976 }
20977 else if (glyph->type == COMPOSITE_GLYPH)
20978 {
20979 if (! glyph->u.cmp.automatic)
20980 {
20981 struct composition *cmp = composition_table[glyph->u.cmp.id];
20982
20983 if (cmp->rbearing > cmp->pixel_width)
20984 *right = cmp->rbearing - cmp->pixel_width;
20985 if (cmp->lbearing < 0)
20986 *left = - cmp->lbearing;
20987 }
20988 else
20989 {
20990 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20991 struct font_metrics metrics;
20992
20993 composition_gstring_width (gstring, glyph->slice.cmp.from,
20994 glyph->slice.cmp.to + 1, &metrics);
20995 if (metrics.rbearing > metrics.width)
20996 *right = metrics.rbearing - metrics.width;
20997 if (metrics.lbearing < 0)
20998 *left = - metrics.lbearing;
20999 }
21000 }
21001 }
21002
21003
21004 /* Return the index of the first glyph preceding glyph string S that
21005 is overwritten by S because of S's left overhang. Value is -1
21006 if no glyphs are overwritten. */
21007
21008 static int
21009 left_overwritten (struct glyph_string *s)
21010 {
21011 int k;
21012
21013 if (s->left_overhang)
21014 {
21015 int x = 0, i;
21016 struct glyph *glyphs = s->row->glyphs[s->area];
21017 int first = s->first_glyph - glyphs;
21018
21019 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21020 x -= glyphs[i].pixel_width;
21021
21022 k = i + 1;
21023 }
21024 else
21025 k = -1;
21026
21027 return k;
21028 }
21029
21030
21031 /* Return the index of the first glyph preceding glyph string S that
21032 is overwriting S because of its right overhang. Value is -1 if no
21033 glyph in front of S overwrites S. */
21034
21035 static int
21036 left_overwriting (struct glyph_string *s)
21037 {
21038 int i, k, x;
21039 struct glyph *glyphs = s->row->glyphs[s->area];
21040 int first = s->first_glyph - glyphs;
21041
21042 k = -1;
21043 x = 0;
21044 for (i = first - 1; i >= 0; --i)
21045 {
21046 int left, right;
21047 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21048 if (x + right > 0)
21049 k = i;
21050 x -= glyphs[i].pixel_width;
21051 }
21052
21053 return k;
21054 }
21055
21056
21057 /* Return the index of the last glyph following glyph string S that is
21058 overwritten by S because of S's right overhang. Value is -1 if
21059 no such glyph is found. */
21060
21061 static int
21062 right_overwritten (struct glyph_string *s)
21063 {
21064 int k = -1;
21065
21066 if (s->right_overhang)
21067 {
21068 int x = 0, i;
21069 struct glyph *glyphs = s->row->glyphs[s->area];
21070 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21071 int end = s->row->used[s->area];
21072
21073 for (i = first; i < end && s->right_overhang > x; ++i)
21074 x += glyphs[i].pixel_width;
21075
21076 k = i;
21077 }
21078
21079 return k;
21080 }
21081
21082
21083 /* Return the index of the last glyph following glyph string S that
21084 overwrites S because of its left overhang. Value is negative
21085 if no such glyph is found. */
21086
21087 static int
21088 right_overwriting (struct glyph_string *s)
21089 {
21090 int i, k, x;
21091 int end = s->row->used[s->area];
21092 struct glyph *glyphs = s->row->glyphs[s->area];
21093 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21094
21095 k = -1;
21096 x = 0;
21097 for (i = first; i < end; ++i)
21098 {
21099 int left, right;
21100 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21101 if (x - left < 0)
21102 k = i;
21103 x += glyphs[i].pixel_width;
21104 }
21105
21106 return k;
21107 }
21108
21109
21110 /* Set background width of glyph string S. START is the index of the
21111 first glyph following S. LAST_X is the right-most x-position + 1
21112 in the drawing area. */
21113
21114 static INLINE void
21115 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21116 {
21117 /* If the face of this glyph string has to be drawn to the end of
21118 the drawing area, set S->extends_to_end_of_line_p. */
21119
21120 if (start == s->row->used[s->area]
21121 && s->area == TEXT_AREA
21122 && ((s->row->fill_line_p
21123 && (s->hl == DRAW_NORMAL_TEXT
21124 || s->hl == DRAW_IMAGE_RAISED
21125 || s->hl == DRAW_IMAGE_SUNKEN))
21126 || s->hl == DRAW_MOUSE_FACE))
21127 s->extends_to_end_of_line_p = 1;
21128
21129 /* If S extends its face to the end of the line, set its
21130 background_width to the distance to the right edge of the drawing
21131 area. */
21132 if (s->extends_to_end_of_line_p)
21133 s->background_width = last_x - s->x + 1;
21134 else
21135 s->background_width = s->width;
21136 }
21137
21138
21139 /* Compute overhangs and x-positions for glyph string S and its
21140 predecessors, or successors. X is the starting x-position for S.
21141 BACKWARD_P non-zero means process predecessors. */
21142
21143 static void
21144 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21145 {
21146 if (backward_p)
21147 {
21148 while (s)
21149 {
21150 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21151 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21152 x -= s->width;
21153 s->x = x;
21154 s = s->prev;
21155 }
21156 }
21157 else
21158 {
21159 while (s)
21160 {
21161 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21162 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21163 s->x = x;
21164 x += s->width;
21165 s = s->next;
21166 }
21167 }
21168 }
21169
21170
21171
21172 /* The following macros are only called from draw_glyphs below.
21173 They reference the following parameters of that function directly:
21174 `w', `row', `area', and `overlap_p'
21175 as well as the following local variables:
21176 `s', `f', and `hdc' (in W32) */
21177
21178 #ifdef HAVE_NTGUI
21179 /* On W32, silently add local `hdc' variable to argument list of
21180 init_glyph_string. */
21181 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21182 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21183 #else
21184 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21185 init_glyph_string (s, char2b, w, row, area, start, hl)
21186 #endif
21187
21188 /* Add a glyph string for a stretch glyph to the list of strings
21189 between HEAD and TAIL. START is the index of the stretch glyph in
21190 row area AREA of glyph row ROW. END is the index of the last glyph
21191 in that glyph row area. X is the current output position assigned
21192 to the new glyph string constructed. HL overrides that face of the
21193 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21194 is the right-most x-position of the drawing area. */
21195
21196 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21197 and below -- keep them on one line. */
21198 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21199 do \
21200 { \
21201 s = (struct glyph_string *) alloca (sizeof *s); \
21202 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21203 START = fill_stretch_glyph_string (s, row, area, START, END); \
21204 append_glyph_string (&HEAD, &TAIL, s); \
21205 s->x = (X); \
21206 } \
21207 while (0)
21208
21209
21210 /* Add a glyph string for an image glyph to the list of strings
21211 between HEAD and TAIL. START is the index of the image glyph in
21212 row area AREA of glyph row ROW. END is the index of the last glyph
21213 in that glyph row area. X is the current output position assigned
21214 to the new glyph string constructed. HL overrides that face of the
21215 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21216 is the right-most x-position of the drawing area. */
21217
21218 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21219 do \
21220 { \
21221 s = (struct glyph_string *) alloca (sizeof *s); \
21222 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21223 fill_image_glyph_string (s); \
21224 append_glyph_string (&HEAD, &TAIL, s); \
21225 ++START; \
21226 s->x = (X); \
21227 } \
21228 while (0)
21229
21230
21231 /* Add a glyph string for a sequence of character glyphs to the list
21232 of strings between HEAD and TAIL. START is the index of the first
21233 glyph in row area AREA of glyph row ROW that is part of the new
21234 glyph string. END is the index of the last glyph in that glyph row
21235 area. X is the current output position assigned to the new glyph
21236 string constructed. HL overrides that face of the glyph; e.g. it
21237 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21238 right-most x-position of the drawing area. */
21239
21240 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21241 do \
21242 { \
21243 int face_id; \
21244 XChar2b *char2b; \
21245 \
21246 face_id = (row)->glyphs[area][START].face_id; \
21247 \
21248 s = (struct glyph_string *) alloca (sizeof *s); \
21249 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21250 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21251 append_glyph_string (&HEAD, &TAIL, s); \
21252 s->x = (X); \
21253 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21254 } \
21255 while (0)
21256
21257
21258 /* Add a glyph string for a composite sequence to the list of strings
21259 between HEAD and TAIL. START is the index of the first glyph in
21260 row area AREA of glyph row ROW that is part of the new glyph
21261 string. END is the index of the last glyph in that glyph row area.
21262 X is the current output position assigned to the new glyph string
21263 constructed. HL overrides that face of the glyph; e.g. it is
21264 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21265 x-position of the drawing area. */
21266
21267 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21268 do { \
21269 int face_id = (row)->glyphs[area][START].face_id; \
21270 struct face *base_face = FACE_FROM_ID (f, face_id); \
21271 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21272 struct composition *cmp = composition_table[cmp_id]; \
21273 XChar2b *char2b; \
21274 struct glyph_string *first_s; \
21275 int n; \
21276 \
21277 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21278 \
21279 /* Make glyph_strings for each glyph sequence that is drawable by \
21280 the same face, and append them to HEAD/TAIL. */ \
21281 for (n = 0; n < cmp->glyph_len;) \
21282 { \
21283 s = (struct glyph_string *) alloca (sizeof *s); \
21284 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21285 append_glyph_string (&(HEAD), &(TAIL), s); \
21286 s->cmp = cmp; \
21287 s->cmp_from = n; \
21288 s->x = (X); \
21289 if (n == 0) \
21290 first_s = s; \
21291 n = fill_composite_glyph_string (s, base_face, overlaps); \
21292 } \
21293 \
21294 ++START; \
21295 s = first_s; \
21296 } while (0)
21297
21298
21299 /* Add a glyph string for a glyph-string sequence to the list of strings
21300 between HEAD and TAIL. */
21301
21302 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21303 do { \
21304 int face_id; \
21305 XChar2b *char2b; \
21306 Lisp_Object gstring; \
21307 \
21308 face_id = (row)->glyphs[area][START].face_id; \
21309 gstring = (composition_gstring_from_id \
21310 ((row)->glyphs[area][START].u.cmp.id)); \
21311 s = (struct glyph_string *) alloca (sizeof *s); \
21312 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21313 * LGSTRING_GLYPH_LEN (gstring)); \
21314 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21315 append_glyph_string (&(HEAD), &(TAIL), s); \
21316 s->x = (X); \
21317 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21318 } while (0)
21319
21320
21321 /* Add a glyph string for a sequence of glyphless character's glyphs
21322 to the list of strings between HEAD and TAIL. The meanings of
21323 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21324
21325 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21326 do \
21327 { \
21328 int face_id; \
21329 XChar2b *char2b; \
21330 \
21331 face_id = (row)->glyphs[area][START].face_id; \
21332 \
21333 s = (struct glyph_string *) alloca (sizeof *s); \
21334 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21335 append_glyph_string (&HEAD, &TAIL, s); \
21336 s->x = (X); \
21337 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21338 overlaps); \
21339 } \
21340 while (0)
21341
21342
21343 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21344 of AREA of glyph row ROW on window W between indices START and END.
21345 HL overrides the face for drawing glyph strings, e.g. it is
21346 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21347 x-positions of the drawing area.
21348
21349 This is an ugly monster macro construct because we must use alloca
21350 to allocate glyph strings (because draw_glyphs can be called
21351 asynchronously). */
21352
21353 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21354 do \
21355 { \
21356 HEAD = TAIL = NULL; \
21357 while (START < END) \
21358 { \
21359 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21360 switch (first_glyph->type) \
21361 { \
21362 case CHAR_GLYPH: \
21363 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21364 HL, X, LAST_X); \
21365 break; \
21366 \
21367 case COMPOSITE_GLYPH: \
21368 if (first_glyph->u.cmp.automatic) \
21369 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21370 HL, X, LAST_X); \
21371 else \
21372 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21373 HL, X, LAST_X); \
21374 break; \
21375 \
21376 case STRETCH_GLYPH: \
21377 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21378 HL, X, LAST_X); \
21379 break; \
21380 \
21381 case IMAGE_GLYPH: \
21382 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21383 HL, X, LAST_X); \
21384 break; \
21385 \
21386 case GLYPHLESS_GLYPH: \
21387 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
21388 HL, X, LAST_X); \
21389 break; \
21390 \
21391 default: \
21392 abort (); \
21393 } \
21394 \
21395 if (s) \
21396 { \
21397 set_glyph_string_background_width (s, START, LAST_X); \
21398 (X) += s->width; \
21399 } \
21400 } \
21401 } while (0)
21402
21403
21404 /* Draw glyphs between START and END in AREA of ROW on window W,
21405 starting at x-position X. X is relative to AREA in W. HL is a
21406 face-override with the following meaning:
21407
21408 DRAW_NORMAL_TEXT draw normally
21409 DRAW_CURSOR draw in cursor face
21410 DRAW_MOUSE_FACE draw in mouse face.
21411 DRAW_INVERSE_VIDEO draw in mode line face
21412 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21413 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21414
21415 If OVERLAPS is non-zero, draw only the foreground of characters and
21416 clip to the physical height of ROW. Non-zero value also defines
21417 the overlapping part to be drawn:
21418
21419 OVERLAPS_PRED overlap with preceding rows
21420 OVERLAPS_SUCC overlap with succeeding rows
21421 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21422 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21423
21424 Value is the x-position reached, relative to AREA of W. */
21425
21426 static int
21427 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21428 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21429 enum draw_glyphs_face hl, int overlaps)
21430 {
21431 struct glyph_string *head, *tail;
21432 struct glyph_string *s;
21433 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21434 int i, j, x_reached, last_x, area_left = 0;
21435 struct frame *f = XFRAME (WINDOW_FRAME (w));
21436 DECLARE_HDC (hdc);
21437
21438 ALLOCATE_HDC (hdc, f);
21439
21440 /* Let's rather be paranoid than getting a SEGV. */
21441 end = min (end, row->used[area]);
21442 start = max (0, start);
21443 start = min (end, start);
21444
21445 /* Translate X to frame coordinates. Set last_x to the right
21446 end of the drawing area. */
21447 if (row->full_width_p)
21448 {
21449 /* X is relative to the left edge of W, without scroll bars
21450 or fringes. */
21451 area_left = WINDOW_LEFT_EDGE_X (w);
21452 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21453 }
21454 else
21455 {
21456 area_left = window_box_left (w, area);
21457 last_x = area_left + window_box_width (w, area);
21458 }
21459 x += area_left;
21460
21461 /* Build a doubly-linked list of glyph_string structures between
21462 head and tail from what we have to draw. Note that the macro
21463 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21464 the reason we use a separate variable `i'. */
21465 i = start;
21466 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21467 if (tail)
21468 x_reached = tail->x + tail->background_width;
21469 else
21470 x_reached = x;
21471
21472 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21473 the row, redraw some glyphs in front or following the glyph
21474 strings built above. */
21475 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21476 {
21477 struct glyph_string *h, *t;
21478 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
21479 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
21480 int dummy_x = 0;
21481
21482 /* If mouse highlighting is on, we may need to draw adjacent
21483 glyphs using mouse-face highlighting. */
21484 if (area == TEXT_AREA && row->mouse_face_p)
21485 {
21486 struct glyph_row *mouse_beg_row, *mouse_end_row;
21487
21488 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
21489 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
21490
21491 if (row >= mouse_beg_row && row <= mouse_end_row)
21492 {
21493 check_mouse_face = 1;
21494 mouse_beg_col = (row == mouse_beg_row)
21495 ? hlinfo->mouse_face_beg_col : 0;
21496 mouse_end_col = (row == mouse_end_row)
21497 ? hlinfo->mouse_face_end_col
21498 : row->used[TEXT_AREA];
21499 }
21500 }
21501
21502 /* Compute overhangs for all glyph strings. */
21503 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21504 for (s = head; s; s = s->next)
21505 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21506
21507 /* Prepend glyph strings for glyphs in front of the first glyph
21508 string that are overwritten because of the first glyph
21509 string's left overhang. The background of all strings
21510 prepended must be drawn because the first glyph string
21511 draws over it. */
21512 i = left_overwritten (head);
21513 if (i >= 0)
21514 {
21515 enum draw_glyphs_face overlap_hl;
21516
21517 /* If this row contains mouse highlighting, attempt to draw
21518 the overlapped glyphs with the correct highlight. This
21519 code fails if the overlap encompasses more than one glyph
21520 and mouse-highlight spans only some of these glyphs.
21521 However, making it work perfectly involves a lot more
21522 code, and I don't know if the pathological case occurs in
21523 practice, so we'll stick to this for now. --- cyd */
21524 if (check_mouse_face
21525 && mouse_beg_col < start && mouse_end_col > i)
21526 overlap_hl = DRAW_MOUSE_FACE;
21527 else
21528 overlap_hl = DRAW_NORMAL_TEXT;
21529
21530 j = i;
21531 BUILD_GLYPH_STRINGS (j, start, h, t,
21532 overlap_hl, dummy_x, last_x);
21533 start = i;
21534 compute_overhangs_and_x (t, head->x, 1);
21535 prepend_glyph_string_lists (&head, &tail, h, t);
21536 clip_head = head;
21537 }
21538
21539 /* Prepend glyph strings for glyphs in front of the first glyph
21540 string that overwrite that glyph string because of their
21541 right overhang. For these strings, only the foreground must
21542 be drawn, because it draws over the glyph string at `head'.
21543 The background must not be drawn because this would overwrite
21544 right overhangs of preceding glyphs for which no glyph
21545 strings exist. */
21546 i = left_overwriting (head);
21547 if (i >= 0)
21548 {
21549 enum draw_glyphs_face overlap_hl;
21550
21551 if (check_mouse_face
21552 && mouse_beg_col < start && mouse_end_col > i)
21553 overlap_hl = DRAW_MOUSE_FACE;
21554 else
21555 overlap_hl = DRAW_NORMAL_TEXT;
21556
21557 clip_head = head;
21558 BUILD_GLYPH_STRINGS (i, start, h, t,
21559 overlap_hl, dummy_x, last_x);
21560 for (s = h; s; s = s->next)
21561 s->background_filled_p = 1;
21562 compute_overhangs_and_x (t, head->x, 1);
21563 prepend_glyph_string_lists (&head, &tail, h, t);
21564 }
21565
21566 /* Append glyphs strings for glyphs following the last glyph
21567 string tail that are overwritten by tail. The background of
21568 these strings has to be drawn because tail's foreground draws
21569 over it. */
21570 i = right_overwritten (tail);
21571 if (i >= 0)
21572 {
21573 enum draw_glyphs_face overlap_hl;
21574
21575 if (check_mouse_face
21576 && mouse_beg_col < i && mouse_end_col > end)
21577 overlap_hl = DRAW_MOUSE_FACE;
21578 else
21579 overlap_hl = DRAW_NORMAL_TEXT;
21580
21581 BUILD_GLYPH_STRINGS (end, i, h, t,
21582 overlap_hl, x, last_x);
21583 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21584 we don't have `end = i;' here. */
21585 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21586 append_glyph_string_lists (&head, &tail, h, t);
21587 clip_tail = tail;
21588 }
21589
21590 /* Append glyph strings for glyphs following the last glyph
21591 string tail that overwrite tail. The foreground of such
21592 glyphs has to be drawn because it writes into the background
21593 of tail. The background must not be drawn because it could
21594 paint over the foreground of following glyphs. */
21595 i = right_overwriting (tail);
21596 if (i >= 0)
21597 {
21598 enum draw_glyphs_face overlap_hl;
21599 if (check_mouse_face
21600 && mouse_beg_col < i && mouse_end_col > end)
21601 overlap_hl = DRAW_MOUSE_FACE;
21602 else
21603 overlap_hl = DRAW_NORMAL_TEXT;
21604
21605 clip_tail = tail;
21606 i++; /* We must include the Ith glyph. */
21607 BUILD_GLYPH_STRINGS (end, i, h, t,
21608 overlap_hl, x, last_x);
21609 for (s = h; s; s = s->next)
21610 s->background_filled_p = 1;
21611 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21612 append_glyph_string_lists (&head, &tail, h, t);
21613 }
21614 if (clip_head || clip_tail)
21615 for (s = head; s; s = s->next)
21616 {
21617 s->clip_head = clip_head;
21618 s->clip_tail = clip_tail;
21619 }
21620 }
21621
21622 /* Draw all strings. */
21623 for (s = head; s; s = s->next)
21624 FRAME_RIF (f)->draw_glyph_string (s);
21625
21626 #ifndef HAVE_NS
21627 /* When focus a sole frame and move horizontally, this sets on_p to 0
21628 causing a failure to erase prev cursor position. */
21629 if (area == TEXT_AREA
21630 && !row->full_width_p
21631 /* When drawing overlapping rows, only the glyph strings'
21632 foreground is drawn, which doesn't erase a cursor
21633 completely. */
21634 && !overlaps)
21635 {
21636 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21637 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21638 : (tail ? tail->x + tail->background_width : x));
21639 x0 -= area_left;
21640 x1 -= area_left;
21641
21642 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21643 row->y, MATRIX_ROW_BOTTOM_Y (row));
21644 }
21645 #endif
21646
21647 /* Value is the x-position up to which drawn, relative to AREA of W.
21648 This doesn't include parts drawn because of overhangs. */
21649 if (row->full_width_p)
21650 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21651 else
21652 x_reached -= area_left;
21653
21654 RELEASE_HDC (hdc, f);
21655
21656 return x_reached;
21657 }
21658
21659 /* Expand row matrix if too narrow. Don't expand if area
21660 is not present. */
21661
21662 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21663 { \
21664 if (!fonts_changed_p \
21665 && (it->glyph_row->glyphs[area] \
21666 < it->glyph_row->glyphs[area + 1])) \
21667 { \
21668 it->w->ncols_scale_factor++; \
21669 fonts_changed_p = 1; \
21670 } \
21671 }
21672
21673 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21674 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21675
21676 static INLINE void
21677 append_glyph (struct it *it)
21678 {
21679 struct glyph *glyph;
21680 enum glyph_row_area area = it->area;
21681
21682 xassert (it->glyph_row);
21683 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21684
21685 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21686 if (glyph < it->glyph_row->glyphs[area + 1])
21687 {
21688 /* If the glyph row is reversed, we need to prepend the glyph
21689 rather than append it. */
21690 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21691 {
21692 struct glyph *g;
21693
21694 /* Make room for the additional glyph. */
21695 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21696 g[1] = *g;
21697 glyph = it->glyph_row->glyphs[area];
21698 }
21699 glyph->charpos = CHARPOS (it->position);
21700 glyph->object = it->object;
21701 if (it->pixel_width > 0)
21702 {
21703 glyph->pixel_width = it->pixel_width;
21704 glyph->padding_p = 0;
21705 }
21706 else
21707 {
21708 /* Assure at least 1-pixel width. Otherwise, cursor can't
21709 be displayed correctly. */
21710 glyph->pixel_width = 1;
21711 glyph->padding_p = 1;
21712 }
21713 glyph->ascent = it->ascent;
21714 glyph->descent = it->descent;
21715 glyph->voffset = it->voffset;
21716 glyph->type = CHAR_GLYPH;
21717 glyph->avoid_cursor_p = it->avoid_cursor_p;
21718 glyph->multibyte_p = it->multibyte_p;
21719 glyph->left_box_line_p = it->start_of_box_run_p;
21720 glyph->right_box_line_p = it->end_of_box_run_p;
21721 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21722 || it->phys_descent > it->descent);
21723 glyph->glyph_not_available_p = it->glyph_not_available_p;
21724 glyph->face_id = it->face_id;
21725 glyph->u.ch = it->char_to_display;
21726 glyph->slice.img = null_glyph_slice;
21727 glyph->font_type = FONT_TYPE_UNKNOWN;
21728 if (it->bidi_p)
21729 {
21730 glyph->resolved_level = it->bidi_it.resolved_level;
21731 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21732 abort ();
21733 glyph->bidi_type = it->bidi_it.type;
21734 }
21735 else
21736 {
21737 glyph->resolved_level = 0;
21738 glyph->bidi_type = UNKNOWN_BT;
21739 }
21740 ++it->glyph_row->used[area];
21741 }
21742 else
21743 IT_EXPAND_MATRIX_WIDTH (it, area);
21744 }
21745
21746 /* Store one glyph for the composition IT->cmp_it.id in
21747 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21748 non-null. */
21749
21750 static INLINE void
21751 append_composite_glyph (struct it *it)
21752 {
21753 struct glyph *glyph;
21754 enum glyph_row_area area = it->area;
21755
21756 xassert (it->glyph_row);
21757
21758 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21759 if (glyph < it->glyph_row->glyphs[area + 1])
21760 {
21761 /* If the glyph row is reversed, we need to prepend the glyph
21762 rather than append it. */
21763 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
21764 {
21765 struct glyph *g;
21766
21767 /* Make room for the new glyph. */
21768 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
21769 g[1] = *g;
21770 glyph = it->glyph_row->glyphs[it->area];
21771 }
21772 glyph->charpos = it->cmp_it.charpos;
21773 glyph->object = it->object;
21774 glyph->pixel_width = it->pixel_width;
21775 glyph->ascent = it->ascent;
21776 glyph->descent = it->descent;
21777 glyph->voffset = it->voffset;
21778 glyph->type = COMPOSITE_GLYPH;
21779 if (it->cmp_it.ch < 0)
21780 {
21781 glyph->u.cmp.automatic = 0;
21782 glyph->u.cmp.id = it->cmp_it.id;
21783 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
21784 }
21785 else
21786 {
21787 glyph->u.cmp.automatic = 1;
21788 glyph->u.cmp.id = it->cmp_it.id;
21789 glyph->slice.cmp.from = it->cmp_it.from;
21790 glyph->slice.cmp.to = it->cmp_it.to - 1;
21791 }
21792 glyph->avoid_cursor_p = it->avoid_cursor_p;
21793 glyph->multibyte_p = it->multibyte_p;
21794 glyph->left_box_line_p = it->start_of_box_run_p;
21795 glyph->right_box_line_p = it->end_of_box_run_p;
21796 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21797 || it->phys_descent > it->descent);
21798 glyph->padding_p = 0;
21799 glyph->glyph_not_available_p = 0;
21800 glyph->face_id = it->face_id;
21801 glyph->font_type = FONT_TYPE_UNKNOWN;
21802 if (it->bidi_p)
21803 {
21804 glyph->resolved_level = it->bidi_it.resolved_level;
21805 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21806 abort ();
21807 glyph->bidi_type = it->bidi_it.type;
21808 }
21809 ++it->glyph_row->used[area];
21810 }
21811 else
21812 IT_EXPAND_MATRIX_WIDTH (it, area);
21813 }
21814
21815
21816 /* Change IT->ascent and IT->height according to the setting of
21817 IT->voffset. */
21818
21819 static INLINE void
21820 take_vertical_position_into_account (struct it *it)
21821 {
21822 if (it->voffset)
21823 {
21824 if (it->voffset < 0)
21825 /* Increase the ascent so that we can display the text higher
21826 in the line. */
21827 it->ascent -= it->voffset;
21828 else
21829 /* Increase the descent so that we can display the text lower
21830 in the line. */
21831 it->descent += it->voffset;
21832 }
21833 }
21834
21835
21836 /* Produce glyphs/get display metrics for the image IT is loaded with.
21837 See the description of struct display_iterator in dispextern.h for
21838 an overview of struct display_iterator. */
21839
21840 static void
21841 produce_image_glyph (struct it *it)
21842 {
21843 struct image *img;
21844 struct face *face;
21845 int glyph_ascent, crop;
21846 struct glyph_slice slice;
21847
21848 xassert (it->what == IT_IMAGE);
21849
21850 face = FACE_FROM_ID (it->f, it->face_id);
21851 xassert (face);
21852 /* Make sure X resources of the face is loaded. */
21853 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21854
21855 if (it->image_id < 0)
21856 {
21857 /* Fringe bitmap. */
21858 it->ascent = it->phys_ascent = 0;
21859 it->descent = it->phys_descent = 0;
21860 it->pixel_width = 0;
21861 it->nglyphs = 0;
21862 return;
21863 }
21864
21865 img = IMAGE_FROM_ID (it->f, it->image_id);
21866 xassert (img);
21867 /* Make sure X resources of the image is loaded. */
21868 prepare_image_for_display (it->f, img);
21869
21870 slice.x = slice.y = 0;
21871 slice.width = img->width;
21872 slice.height = img->height;
21873
21874 if (INTEGERP (it->slice.x))
21875 slice.x = XINT (it->slice.x);
21876 else if (FLOATP (it->slice.x))
21877 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21878
21879 if (INTEGERP (it->slice.y))
21880 slice.y = XINT (it->slice.y);
21881 else if (FLOATP (it->slice.y))
21882 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21883
21884 if (INTEGERP (it->slice.width))
21885 slice.width = XINT (it->slice.width);
21886 else if (FLOATP (it->slice.width))
21887 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21888
21889 if (INTEGERP (it->slice.height))
21890 slice.height = XINT (it->slice.height);
21891 else if (FLOATP (it->slice.height))
21892 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21893
21894 if (slice.x >= img->width)
21895 slice.x = img->width;
21896 if (slice.y >= img->height)
21897 slice.y = img->height;
21898 if (slice.x + slice.width >= img->width)
21899 slice.width = img->width - slice.x;
21900 if (slice.y + slice.height > img->height)
21901 slice.height = img->height - slice.y;
21902
21903 if (slice.width == 0 || slice.height == 0)
21904 return;
21905
21906 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21907
21908 it->descent = slice.height - glyph_ascent;
21909 if (slice.y == 0)
21910 it->descent += img->vmargin;
21911 if (slice.y + slice.height == img->height)
21912 it->descent += img->vmargin;
21913 it->phys_descent = it->descent;
21914
21915 it->pixel_width = slice.width;
21916 if (slice.x == 0)
21917 it->pixel_width += img->hmargin;
21918 if (slice.x + slice.width == img->width)
21919 it->pixel_width += img->hmargin;
21920
21921 /* It's quite possible for images to have an ascent greater than
21922 their height, so don't get confused in that case. */
21923 if (it->descent < 0)
21924 it->descent = 0;
21925
21926 it->nglyphs = 1;
21927
21928 if (face->box != FACE_NO_BOX)
21929 {
21930 if (face->box_line_width > 0)
21931 {
21932 if (slice.y == 0)
21933 it->ascent += face->box_line_width;
21934 if (slice.y + slice.height == img->height)
21935 it->descent += face->box_line_width;
21936 }
21937
21938 if (it->start_of_box_run_p && slice.x == 0)
21939 it->pixel_width += eabs (face->box_line_width);
21940 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21941 it->pixel_width += eabs (face->box_line_width);
21942 }
21943
21944 take_vertical_position_into_account (it);
21945
21946 /* Automatically crop wide image glyphs at right edge so we can
21947 draw the cursor on same display row. */
21948 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21949 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21950 {
21951 it->pixel_width -= crop;
21952 slice.width -= crop;
21953 }
21954
21955 if (it->glyph_row)
21956 {
21957 struct glyph *glyph;
21958 enum glyph_row_area area = it->area;
21959
21960 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21961 if (glyph < it->glyph_row->glyphs[area + 1])
21962 {
21963 glyph->charpos = CHARPOS (it->position);
21964 glyph->object = it->object;
21965 glyph->pixel_width = it->pixel_width;
21966 glyph->ascent = glyph_ascent;
21967 glyph->descent = it->descent;
21968 glyph->voffset = it->voffset;
21969 glyph->type = IMAGE_GLYPH;
21970 glyph->avoid_cursor_p = it->avoid_cursor_p;
21971 glyph->multibyte_p = it->multibyte_p;
21972 glyph->left_box_line_p = it->start_of_box_run_p;
21973 glyph->right_box_line_p = it->end_of_box_run_p;
21974 glyph->overlaps_vertically_p = 0;
21975 glyph->padding_p = 0;
21976 glyph->glyph_not_available_p = 0;
21977 glyph->face_id = it->face_id;
21978 glyph->u.img_id = img->id;
21979 glyph->slice.img = slice;
21980 glyph->font_type = FONT_TYPE_UNKNOWN;
21981 if (it->bidi_p)
21982 {
21983 glyph->resolved_level = it->bidi_it.resolved_level;
21984 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21985 abort ();
21986 glyph->bidi_type = it->bidi_it.type;
21987 }
21988 ++it->glyph_row->used[area];
21989 }
21990 else
21991 IT_EXPAND_MATRIX_WIDTH (it, area);
21992 }
21993 }
21994
21995
21996 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
21997 of the glyph, WIDTH and HEIGHT are the width and height of the
21998 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
21999
22000 static void
22001 append_stretch_glyph (struct it *it, Lisp_Object object,
22002 int width, int height, int ascent)
22003 {
22004 struct glyph *glyph;
22005 enum glyph_row_area area = it->area;
22006
22007 xassert (ascent >= 0 && ascent <= height);
22008
22009 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22010 if (glyph < it->glyph_row->glyphs[area + 1])
22011 {
22012 /* If the glyph row is reversed, we need to prepend the glyph
22013 rather than append it. */
22014 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22015 {
22016 struct glyph *g;
22017
22018 /* Make room for the additional glyph. */
22019 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22020 g[1] = *g;
22021 glyph = it->glyph_row->glyphs[area];
22022 }
22023 glyph->charpos = CHARPOS (it->position);
22024 glyph->object = object;
22025 glyph->pixel_width = width;
22026 glyph->ascent = ascent;
22027 glyph->descent = height - ascent;
22028 glyph->voffset = it->voffset;
22029 glyph->type = STRETCH_GLYPH;
22030 glyph->avoid_cursor_p = it->avoid_cursor_p;
22031 glyph->multibyte_p = it->multibyte_p;
22032 glyph->left_box_line_p = it->start_of_box_run_p;
22033 glyph->right_box_line_p = it->end_of_box_run_p;
22034 glyph->overlaps_vertically_p = 0;
22035 glyph->padding_p = 0;
22036 glyph->glyph_not_available_p = 0;
22037 glyph->face_id = it->face_id;
22038 glyph->u.stretch.ascent = ascent;
22039 glyph->u.stretch.height = height;
22040 glyph->slice.img = null_glyph_slice;
22041 glyph->font_type = FONT_TYPE_UNKNOWN;
22042 if (it->bidi_p)
22043 {
22044 glyph->resolved_level = it->bidi_it.resolved_level;
22045 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22046 abort ();
22047 glyph->bidi_type = it->bidi_it.type;
22048 }
22049 else
22050 {
22051 glyph->resolved_level = 0;
22052 glyph->bidi_type = UNKNOWN_BT;
22053 }
22054 ++it->glyph_row->used[area];
22055 }
22056 else
22057 IT_EXPAND_MATRIX_WIDTH (it, area);
22058 }
22059
22060
22061 /* Produce a stretch glyph for iterator IT. IT->object is the value
22062 of the glyph property displayed. The value must be a list
22063 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22064 being recognized:
22065
22066 1. `:width WIDTH' specifies that the space should be WIDTH *
22067 canonical char width wide. WIDTH may be an integer or floating
22068 point number.
22069
22070 2. `:relative-width FACTOR' specifies that the width of the stretch
22071 should be computed from the width of the first character having the
22072 `glyph' property, and should be FACTOR times that width.
22073
22074 3. `:align-to HPOS' specifies that the space should be wide enough
22075 to reach HPOS, a value in canonical character units.
22076
22077 Exactly one of the above pairs must be present.
22078
22079 4. `:height HEIGHT' specifies that the height of the stretch produced
22080 should be HEIGHT, measured in canonical character units.
22081
22082 5. `:relative-height FACTOR' specifies that the height of the
22083 stretch should be FACTOR times the height of the characters having
22084 the glyph property.
22085
22086 Either none or exactly one of 4 or 5 must be present.
22087
22088 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22089 of the stretch should be used for the ascent of the stretch.
22090 ASCENT must be in the range 0 <= ASCENT <= 100. */
22091
22092 static void
22093 produce_stretch_glyph (struct it *it)
22094 {
22095 /* (space :width WIDTH :height HEIGHT ...) */
22096 Lisp_Object prop, plist;
22097 int width = 0, height = 0, align_to = -1;
22098 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22099 int ascent = 0;
22100 double tem;
22101 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22102 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22103
22104 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22105
22106 /* List should start with `space'. */
22107 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22108 plist = XCDR (it->object);
22109
22110 /* Compute the width of the stretch. */
22111 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22112 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22113 {
22114 /* Absolute width `:width WIDTH' specified and valid. */
22115 zero_width_ok_p = 1;
22116 width = (int)tem;
22117 }
22118 else if (prop = Fplist_get (plist, QCrelative_width),
22119 NUMVAL (prop) > 0)
22120 {
22121 /* Relative width `:relative-width FACTOR' specified and valid.
22122 Compute the width of the characters having the `glyph'
22123 property. */
22124 struct it it2;
22125 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22126
22127 it2 = *it;
22128 if (it->multibyte_p)
22129 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22130 else
22131 {
22132 it2.c = it2.char_to_display = *p, it2.len = 1;
22133 if (! ASCII_CHAR_P (it2.c))
22134 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22135 }
22136
22137 it2.glyph_row = NULL;
22138 it2.what = IT_CHARACTER;
22139 x_produce_glyphs (&it2);
22140 width = NUMVAL (prop) * it2.pixel_width;
22141 }
22142 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22143 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22144 {
22145 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22146 align_to = (align_to < 0
22147 ? 0
22148 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22149 else if (align_to < 0)
22150 align_to = window_box_left_offset (it->w, TEXT_AREA);
22151 width = max (0, (int)tem + align_to - it->current_x);
22152 zero_width_ok_p = 1;
22153 }
22154 else
22155 /* Nothing specified -> width defaults to canonical char width. */
22156 width = FRAME_COLUMN_WIDTH (it->f);
22157
22158 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22159 width = 1;
22160
22161 /* Compute height. */
22162 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22163 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22164 {
22165 height = (int)tem;
22166 zero_height_ok_p = 1;
22167 }
22168 else if (prop = Fplist_get (plist, QCrelative_height),
22169 NUMVAL (prop) > 0)
22170 height = FONT_HEIGHT (font) * NUMVAL (prop);
22171 else
22172 height = FONT_HEIGHT (font);
22173
22174 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22175 height = 1;
22176
22177 /* Compute percentage of height used for ascent. If
22178 `:ascent ASCENT' is present and valid, use that. Otherwise,
22179 derive the ascent from the font in use. */
22180 if (prop = Fplist_get (plist, QCascent),
22181 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22182 ascent = height * NUMVAL (prop) / 100.0;
22183 else if (!NILP (prop)
22184 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22185 ascent = min (max (0, (int)tem), height);
22186 else
22187 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22188
22189 if (width > 0 && it->line_wrap != TRUNCATE
22190 && it->current_x + width > it->last_visible_x)
22191 width = it->last_visible_x - it->current_x - 1;
22192
22193 if (width > 0 && height > 0 && it->glyph_row)
22194 {
22195 Lisp_Object object = it->stack[it->sp - 1].string;
22196 if (!STRINGP (object))
22197 object = it->w->buffer;
22198 append_stretch_glyph (it, object, width, height, ascent);
22199 }
22200
22201 it->pixel_width = width;
22202 it->ascent = it->phys_ascent = ascent;
22203 it->descent = it->phys_descent = height - it->ascent;
22204 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22205
22206 take_vertical_position_into_account (it);
22207 }
22208
22209 /* Calculate line-height and line-spacing properties.
22210 An integer value specifies explicit pixel value.
22211 A float value specifies relative value to current face height.
22212 A cons (float . face-name) specifies relative value to
22213 height of specified face font.
22214
22215 Returns height in pixels, or nil. */
22216
22217
22218 static Lisp_Object
22219 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22220 int boff, int override)
22221 {
22222 Lisp_Object face_name = Qnil;
22223 int ascent, descent, height;
22224
22225 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22226 return val;
22227
22228 if (CONSP (val))
22229 {
22230 face_name = XCAR (val);
22231 val = XCDR (val);
22232 if (!NUMBERP (val))
22233 val = make_number (1);
22234 if (NILP (face_name))
22235 {
22236 height = it->ascent + it->descent;
22237 goto scale;
22238 }
22239 }
22240
22241 if (NILP (face_name))
22242 {
22243 font = FRAME_FONT (it->f);
22244 boff = FRAME_BASELINE_OFFSET (it->f);
22245 }
22246 else if (EQ (face_name, Qt))
22247 {
22248 override = 0;
22249 }
22250 else
22251 {
22252 int face_id;
22253 struct face *face;
22254
22255 face_id = lookup_named_face (it->f, face_name, 0);
22256 if (face_id < 0)
22257 return make_number (-1);
22258
22259 face = FACE_FROM_ID (it->f, face_id);
22260 font = face->font;
22261 if (font == NULL)
22262 return make_number (-1);
22263 boff = font->baseline_offset;
22264 if (font->vertical_centering)
22265 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22266 }
22267
22268 ascent = FONT_BASE (font) + boff;
22269 descent = FONT_DESCENT (font) - boff;
22270
22271 if (override)
22272 {
22273 it->override_ascent = ascent;
22274 it->override_descent = descent;
22275 it->override_boff = boff;
22276 }
22277
22278 height = ascent + descent;
22279
22280 scale:
22281 if (FLOATP (val))
22282 height = (int)(XFLOAT_DATA (val) * height);
22283 else if (INTEGERP (val))
22284 height *= XINT (val);
22285
22286 return make_number (height);
22287 }
22288
22289
22290 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22291 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22292 and only if this is for a character for which no font was found.
22293
22294 If the display method (it->glyphless_method) is
22295 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEXA_CODE, LEN is a
22296 length of the acronym or the hexadecimal string, UPPER_XOFF and
22297 UPPER_YOFF are pixel offsets for the upper part of the string,
22298 LOWER_XOFF and LOWER_YOFF are for the lower part.
22299
22300 For the other display methods, LEN through LOWER_YOFF are zero. */
22301
22302 static void
22303 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22304 short upper_xoff, short upper_yoff,
22305 short lower_xoff, short lower_yoff)
22306 {
22307 struct glyph *glyph;
22308 enum glyph_row_area area = it->area;
22309
22310 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22311 if (glyph < it->glyph_row->glyphs[area + 1])
22312 {
22313 /* If the glyph row is reversed, we need to prepend the glyph
22314 rather than append it. */
22315 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22316 {
22317 struct glyph *g;
22318
22319 /* Make room for the additional glyph. */
22320 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22321 g[1] = *g;
22322 glyph = it->glyph_row->glyphs[area];
22323 }
22324 glyph->charpos = CHARPOS (it->position);
22325 glyph->object = it->object;
22326 glyph->pixel_width = it->pixel_width;
22327 glyph->ascent = it->ascent;
22328 glyph->descent = it->descent;
22329 glyph->voffset = it->voffset;
22330 glyph->type = GLYPHLESS_GLYPH;
22331 glyph->u.glyphless.method = it->glyphless_method;
22332 glyph->u.glyphless.for_no_font = for_no_font;
22333 glyph->u.glyphless.len = len;
22334 glyph->u.glyphless.ch = it->c;
22335 glyph->slice.glyphless.upper_xoff = upper_xoff;
22336 glyph->slice.glyphless.upper_yoff = upper_yoff;
22337 glyph->slice.glyphless.lower_xoff = lower_xoff;
22338 glyph->slice.glyphless.lower_yoff = lower_yoff;
22339 glyph->avoid_cursor_p = it->avoid_cursor_p;
22340 glyph->multibyte_p = it->multibyte_p;
22341 glyph->left_box_line_p = it->start_of_box_run_p;
22342 glyph->right_box_line_p = it->end_of_box_run_p;
22343 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22344 || it->phys_descent > it->descent);
22345 glyph->padding_p = 0;
22346 glyph->glyph_not_available_p = 0;
22347 glyph->face_id = face_id;
22348 glyph->font_type = FONT_TYPE_UNKNOWN;
22349 if (it->bidi_p)
22350 {
22351 glyph->resolved_level = it->bidi_it.resolved_level;
22352 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22353 abort ();
22354 glyph->bidi_type = it->bidi_it.type;
22355 }
22356 ++it->glyph_row->used[area];
22357 }
22358 else
22359 IT_EXPAND_MATRIX_WIDTH (it, area);
22360 }
22361
22362
22363 /* Produce a glyph for a glyphless character for iterator IT.
22364 IT->glyphless_method specifies which method to use for displaying
22365 the character. See the description of enum
22366 glyphless_display_method in dispextern.h for the detail.
22367
22368 FOR_NO_FONT is nonzero if and only if this is for a character for
22369 which no font was found. ACRONYM, if non-nil, is an acronym string
22370 for the character. */
22371
22372 static void
22373 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22374 {
22375 int face_id;
22376 struct face *face;
22377 struct font *font;
22378 int base_width, base_height, width, height;
22379 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22380 int len;
22381
22382 /* Get the metrics of the base font. We always refer to the current
22383 ASCII face. */
22384 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22385 font = face->font ? face->font : FRAME_FONT (it->f);
22386 it->ascent = FONT_BASE (font) + font->baseline_offset;
22387 it->descent = FONT_DESCENT (font) - font->baseline_offset;
22388 base_height = it->ascent + it->descent;
22389 base_width = font->average_width;
22390
22391 /* Get a face ID for the glyph by utilizing a cache (the same way as
22392 doen for `escape-glyph' in get_next_display_element). */
22393 if (it->f == last_glyphless_glyph_frame
22394 && it->face_id == last_glyphless_glyph_face_id)
22395 {
22396 face_id = last_glyphless_glyph_merged_face_id;
22397 }
22398 else
22399 {
22400 /* Merge the `glyphless-char' face into the current face. */
22401 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
22402 last_glyphless_glyph_frame = it->f;
22403 last_glyphless_glyph_face_id = it->face_id;
22404 last_glyphless_glyph_merged_face_id = face_id;
22405 }
22406
22407 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
22408 {
22409 it->pixel_width = THIN_SPACE_WIDTH;
22410 len = 0;
22411 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22412 }
22413 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
22414 {
22415 width = CHAR_WIDTH (it->c);
22416 if (width == 0)
22417 width = 1;
22418 else if (width > 4)
22419 width = 4;
22420 it->pixel_width = base_width * width;
22421 len = 0;
22422 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22423 }
22424 else
22425 {
22426 char buf[7], *str;
22427 unsigned int code[6];
22428 int upper_len;
22429 int ascent, descent;
22430 struct font_metrics metrics_upper, metrics_lower;
22431
22432 face = FACE_FROM_ID (it->f, face_id);
22433 font = face->font ? face->font : FRAME_FONT (it->f);
22434 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22435
22436 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
22437 {
22438 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
22439 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
22440 str = STRINGP (acronym) ? (char *) SDATA (acronym) : "";
22441 }
22442 else
22443 {
22444 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEXA_CODE);
22445 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
22446 str = buf;
22447 }
22448 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
22449 code[len] = font->driver->encode_char (font, str[len]);
22450 upper_len = (len + 1) / 2;
22451 font->driver->text_extents (font, code, upper_len,
22452 &metrics_upper);
22453 font->driver->text_extents (font, code + upper_len, len - upper_len,
22454 &metrics_lower);
22455
22456
22457
22458 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
22459 width = max (metrics_upper.width, metrics_lower.width) + 4;
22460 upper_xoff = upper_yoff = 2; /* the typical case */
22461 if (base_width >= width)
22462 {
22463 /* Align the upper to the left, the lower to the right. */
22464 it->pixel_width = base_width;
22465 lower_xoff = base_width - 2 - metrics_lower.width;
22466 }
22467 else
22468 {
22469 /* Center the shorter one. */
22470 it->pixel_width = width;
22471 if (metrics_upper.width >= metrics_lower.width)
22472 lower_xoff = (width - metrics_lower.width) / 2;
22473 else
22474 upper_xoff = (width - metrics_upper.width) / 2;
22475 }
22476
22477 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
22478 top, bottom, and between upper and lower strings. */
22479 height = (metrics_upper.ascent + metrics_upper.descent
22480 + metrics_lower.ascent + metrics_lower.descent) + 5;
22481 /* Center vertically.
22482 H:base_height, D:base_descent
22483 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
22484
22485 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
22486 descent = D - H/2 + h/2;
22487 lower_yoff = descent - 2 - ld;
22488 upper_yoff = lower_yoff - la - 1 - ud; */
22489 ascent = - (it->descent - (base_height + height + 1) / 2);
22490 descent = it->descent - (base_height - height) / 2;
22491 lower_yoff = descent - 2 - metrics_lower.descent;
22492 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
22493 - metrics_upper.descent);
22494 /* Don't make the height shorter than the base height. */
22495 if (height > base_height)
22496 {
22497 it->ascent = ascent;
22498 it->descent = descent;
22499 }
22500 }
22501
22502 it->phys_ascent = it->ascent;
22503 it->phys_descent = it->descent;
22504 if (it->glyph_row)
22505 append_glyphless_glyph (it, face_id, for_no_font, len,
22506 upper_xoff, upper_yoff,
22507 lower_xoff, lower_yoff);
22508 it->nglyphs = 1;
22509 take_vertical_position_into_account (it);
22510 }
22511
22512
22513 /* RIF:
22514 Produce glyphs/get display metrics for the display element IT is
22515 loaded with. See the description of struct it in dispextern.h
22516 for an overview of struct it. */
22517
22518 void
22519 x_produce_glyphs (struct it *it)
22520 {
22521 int extra_line_spacing = it->extra_line_spacing;
22522
22523 it->glyph_not_available_p = 0;
22524
22525 if (it->what == IT_CHARACTER)
22526 {
22527 XChar2b char2b;
22528 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22529 struct font *font = face->font;
22530 struct font_metrics *pcm = NULL;
22531 int boff; /* baseline offset */
22532
22533 if (font == NULL)
22534 {
22535 /* When no suitable font is found, display this character by
22536 the method specified in the first extra slot of
22537 Vglyphless_char_display. */
22538 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
22539
22540 xassert (it->what == IT_GLYPHLESS);
22541 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
22542 goto done;
22543 }
22544
22545 boff = font->baseline_offset;
22546 if (font->vertical_centering)
22547 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22548
22549 if (it->char_to_display != '\n' && it->char_to_display != '\t')
22550 {
22551 int stretched_p;
22552
22553 it->nglyphs = 1;
22554
22555 if (it->override_ascent >= 0)
22556 {
22557 it->ascent = it->override_ascent;
22558 it->descent = it->override_descent;
22559 boff = it->override_boff;
22560 }
22561 else
22562 {
22563 it->ascent = FONT_BASE (font) + boff;
22564 it->descent = FONT_DESCENT (font) - boff;
22565 }
22566
22567 if (get_char_glyph_code (it->char_to_display, font, &char2b))
22568 {
22569 pcm = get_per_char_metric (it->f, font, &char2b);
22570 if (pcm->width == 0
22571 && pcm->rbearing == 0 && pcm->lbearing == 0)
22572 pcm = NULL;
22573 }
22574
22575 if (pcm)
22576 {
22577 it->phys_ascent = pcm->ascent + boff;
22578 it->phys_descent = pcm->descent - boff;
22579 it->pixel_width = pcm->width;
22580 }
22581 else
22582 {
22583 it->glyph_not_available_p = 1;
22584 it->phys_ascent = it->ascent;
22585 it->phys_descent = it->descent;
22586 it->pixel_width = font->space_width;
22587 }
22588
22589 if (it->constrain_row_ascent_descent_p)
22590 {
22591 if (it->descent > it->max_descent)
22592 {
22593 it->ascent += it->descent - it->max_descent;
22594 it->descent = it->max_descent;
22595 }
22596 if (it->ascent > it->max_ascent)
22597 {
22598 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22599 it->ascent = it->max_ascent;
22600 }
22601 it->phys_ascent = min (it->phys_ascent, it->ascent);
22602 it->phys_descent = min (it->phys_descent, it->descent);
22603 extra_line_spacing = 0;
22604 }
22605
22606 /* If this is a space inside a region of text with
22607 `space-width' property, change its width. */
22608 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22609 if (stretched_p)
22610 it->pixel_width *= XFLOATINT (it->space_width);
22611
22612 /* If face has a box, add the box thickness to the character
22613 height. If character has a box line to the left and/or
22614 right, add the box line width to the character's width. */
22615 if (face->box != FACE_NO_BOX)
22616 {
22617 int thick = face->box_line_width;
22618
22619 if (thick > 0)
22620 {
22621 it->ascent += thick;
22622 it->descent += thick;
22623 }
22624 else
22625 thick = -thick;
22626
22627 if (it->start_of_box_run_p)
22628 it->pixel_width += thick;
22629 if (it->end_of_box_run_p)
22630 it->pixel_width += thick;
22631 }
22632
22633 /* If face has an overline, add the height of the overline
22634 (1 pixel) and a 1 pixel margin to the character height. */
22635 if (face->overline_p)
22636 it->ascent += overline_margin;
22637
22638 if (it->constrain_row_ascent_descent_p)
22639 {
22640 if (it->ascent > it->max_ascent)
22641 it->ascent = it->max_ascent;
22642 if (it->descent > it->max_descent)
22643 it->descent = it->max_descent;
22644 }
22645
22646 take_vertical_position_into_account (it);
22647
22648 /* If we have to actually produce glyphs, do it. */
22649 if (it->glyph_row)
22650 {
22651 if (stretched_p)
22652 {
22653 /* Translate a space with a `space-width' property
22654 into a stretch glyph. */
22655 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22656 / FONT_HEIGHT (font));
22657 append_stretch_glyph (it, it->object, it->pixel_width,
22658 it->ascent + it->descent, ascent);
22659 }
22660 else
22661 append_glyph (it);
22662
22663 /* If characters with lbearing or rbearing are displayed
22664 in this line, record that fact in a flag of the
22665 glyph row. This is used to optimize X output code. */
22666 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22667 it->glyph_row->contains_overlapping_glyphs_p = 1;
22668 }
22669 if (! stretched_p && it->pixel_width == 0)
22670 /* We assure that all visible glyphs have at least 1-pixel
22671 width. */
22672 it->pixel_width = 1;
22673 }
22674 else if (it->char_to_display == '\n')
22675 {
22676 /* A newline has no width, but we need the height of the
22677 line. But if previous part of the line sets a height,
22678 don't increase that height */
22679
22680 Lisp_Object height;
22681 Lisp_Object total_height = Qnil;
22682
22683 it->override_ascent = -1;
22684 it->pixel_width = 0;
22685 it->nglyphs = 0;
22686
22687 height = get_it_property (it, Qline_height);
22688 /* Split (line-height total-height) list */
22689 if (CONSP (height)
22690 && CONSP (XCDR (height))
22691 && NILP (XCDR (XCDR (height))))
22692 {
22693 total_height = XCAR (XCDR (height));
22694 height = XCAR (height);
22695 }
22696 height = calc_line_height_property (it, height, font, boff, 1);
22697
22698 if (it->override_ascent >= 0)
22699 {
22700 it->ascent = it->override_ascent;
22701 it->descent = it->override_descent;
22702 boff = it->override_boff;
22703 }
22704 else
22705 {
22706 it->ascent = FONT_BASE (font) + boff;
22707 it->descent = FONT_DESCENT (font) - boff;
22708 }
22709
22710 if (EQ (height, Qt))
22711 {
22712 if (it->descent > it->max_descent)
22713 {
22714 it->ascent += it->descent - it->max_descent;
22715 it->descent = it->max_descent;
22716 }
22717 if (it->ascent > it->max_ascent)
22718 {
22719 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22720 it->ascent = it->max_ascent;
22721 }
22722 it->phys_ascent = min (it->phys_ascent, it->ascent);
22723 it->phys_descent = min (it->phys_descent, it->descent);
22724 it->constrain_row_ascent_descent_p = 1;
22725 extra_line_spacing = 0;
22726 }
22727 else
22728 {
22729 Lisp_Object spacing;
22730
22731 it->phys_ascent = it->ascent;
22732 it->phys_descent = it->descent;
22733
22734 if ((it->max_ascent > 0 || it->max_descent > 0)
22735 && face->box != FACE_NO_BOX
22736 && face->box_line_width > 0)
22737 {
22738 it->ascent += face->box_line_width;
22739 it->descent += face->box_line_width;
22740 }
22741 if (!NILP (height)
22742 && XINT (height) > it->ascent + it->descent)
22743 it->ascent = XINT (height) - it->descent;
22744
22745 if (!NILP (total_height))
22746 spacing = calc_line_height_property (it, total_height, font, boff, 0);
22747 else
22748 {
22749 spacing = get_it_property (it, Qline_spacing);
22750 spacing = calc_line_height_property (it, spacing, font, boff, 0);
22751 }
22752 if (INTEGERP (spacing))
22753 {
22754 extra_line_spacing = XINT (spacing);
22755 if (!NILP (total_height))
22756 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22757 }
22758 }
22759 }
22760 else /* i.e. (it->char_to_display == '\t') */
22761 {
22762 if (font->space_width > 0)
22763 {
22764 int tab_width = it->tab_width * font->space_width;
22765 int x = it->current_x + it->continuation_lines_width;
22766 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22767
22768 /* If the distance from the current position to the next tab
22769 stop is less than a space character width, use the
22770 tab stop after that. */
22771 if (next_tab_x - x < font->space_width)
22772 next_tab_x += tab_width;
22773
22774 it->pixel_width = next_tab_x - x;
22775 it->nglyphs = 1;
22776 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22777 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22778
22779 if (it->glyph_row)
22780 {
22781 append_stretch_glyph (it, it->object, it->pixel_width,
22782 it->ascent + it->descent, it->ascent);
22783 }
22784 }
22785 else
22786 {
22787 it->pixel_width = 0;
22788 it->nglyphs = 1;
22789 }
22790 }
22791 }
22792 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22793 {
22794 /* A static composition.
22795
22796 Note: A composition is represented as one glyph in the
22797 glyph matrix. There are no padding glyphs.
22798
22799 Important note: pixel_width, ascent, and descent are the
22800 values of what is drawn by draw_glyphs (i.e. the values of
22801 the overall glyphs composed). */
22802 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22803 int boff; /* baseline offset */
22804 struct composition *cmp = composition_table[it->cmp_it.id];
22805 int glyph_len = cmp->glyph_len;
22806 struct font *font = face->font;
22807
22808 it->nglyphs = 1;
22809
22810 /* If we have not yet calculated pixel size data of glyphs of
22811 the composition for the current face font, calculate them
22812 now. Theoretically, we have to check all fonts for the
22813 glyphs, but that requires much time and memory space. So,
22814 here we check only the font of the first glyph. This may
22815 lead to incorrect display, but it's very rare, and C-l
22816 (recenter-top-bottom) can correct the display anyway. */
22817 if (! cmp->font || cmp->font != font)
22818 {
22819 /* Ascent and descent of the font of the first character
22820 of this composition (adjusted by baseline offset).
22821 Ascent and descent of overall glyphs should not be less
22822 than these, respectively. */
22823 int font_ascent, font_descent, font_height;
22824 /* Bounding box of the overall glyphs. */
22825 int leftmost, rightmost, lowest, highest;
22826 int lbearing, rbearing;
22827 int i, width, ascent, descent;
22828 int left_padded = 0, right_padded = 0;
22829 int c;
22830 XChar2b char2b;
22831 struct font_metrics *pcm;
22832 int font_not_found_p;
22833 EMACS_INT pos;
22834
22835 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22836 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22837 break;
22838 if (glyph_len < cmp->glyph_len)
22839 right_padded = 1;
22840 for (i = 0; i < glyph_len; i++)
22841 {
22842 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22843 break;
22844 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22845 }
22846 if (i > 0)
22847 left_padded = 1;
22848
22849 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22850 : IT_CHARPOS (*it));
22851 /* If no suitable font is found, use the default font. */
22852 font_not_found_p = font == NULL;
22853 if (font_not_found_p)
22854 {
22855 face = face->ascii_face;
22856 font = face->font;
22857 }
22858 boff = font->baseline_offset;
22859 if (font->vertical_centering)
22860 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22861 font_ascent = FONT_BASE (font) + boff;
22862 font_descent = FONT_DESCENT (font) - boff;
22863 font_height = FONT_HEIGHT (font);
22864
22865 cmp->font = (void *) font;
22866
22867 pcm = NULL;
22868 if (! font_not_found_p)
22869 {
22870 get_char_face_and_encoding (it->f, c, it->face_id,
22871 &char2b, it->multibyte_p, 0);
22872 pcm = get_per_char_metric (it->f, font, &char2b);
22873 }
22874
22875 /* Initialize the bounding box. */
22876 if (pcm)
22877 {
22878 width = pcm->width;
22879 ascent = pcm->ascent;
22880 descent = pcm->descent;
22881 lbearing = pcm->lbearing;
22882 rbearing = pcm->rbearing;
22883 }
22884 else
22885 {
22886 width = font->space_width;
22887 ascent = FONT_BASE (font);
22888 descent = FONT_DESCENT (font);
22889 lbearing = 0;
22890 rbearing = width;
22891 }
22892
22893 rightmost = width;
22894 leftmost = 0;
22895 lowest = - descent + boff;
22896 highest = ascent + boff;
22897
22898 if (! font_not_found_p
22899 && font->default_ascent
22900 && CHAR_TABLE_P (Vuse_default_ascent)
22901 && !NILP (Faref (Vuse_default_ascent,
22902 make_number (it->char_to_display))))
22903 highest = font->default_ascent + boff;
22904
22905 /* Draw the first glyph at the normal position. It may be
22906 shifted to right later if some other glyphs are drawn
22907 at the left. */
22908 cmp->offsets[i * 2] = 0;
22909 cmp->offsets[i * 2 + 1] = boff;
22910 cmp->lbearing = lbearing;
22911 cmp->rbearing = rbearing;
22912
22913 /* Set cmp->offsets for the remaining glyphs. */
22914 for (i++; i < glyph_len; i++)
22915 {
22916 int left, right, btm, top;
22917 int ch = COMPOSITION_GLYPH (cmp, i);
22918 int face_id;
22919 struct face *this_face;
22920 int this_boff;
22921
22922 if (ch == '\t')
22923 ch = ' ';
22924 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22925 this_face = FACE_FROM_ID (it->f, face_id);
22926 font = this_face->font;
22927
22928 if (font == NULL)
22929 pcm = NULL;
22930 else
22931 {
22932 this_boff = font->baseline_offset;
22933 if (font->vertical_centering)
22934 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22935 get_char_face_and_encoding (it->f, ch, face_id,
22936 &char2b, it->multibyte_p, 0);
22937 pcm = get_per_char_metric (it->f, font, &char2b);
22938 }
22939 if (! pcm)
22940 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22941 else
22942 {
22943 width = pcm->width;
22944 ascent = pcm->ascent;
22945 descent = pcm->descent;
22946 lbearing = pcm->lbearing;
22947 rbearing = pcm->rbearing;
22948 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22949 {
22950 /* Relative composition with or without
22951 alternate chars. */
22952 left = (leftmost + rightmost - width) / 2;
22953 btm = - descent + boff;
22954 if (font->relative_compose
22955 && (! CHAR_TABLE_P (Vignore_relative_composition)
22956 || NILP (Faref (Vignore_relative_composition,
22957 make_number (ch)))))
22958 {
22959
22960 if (- descent >= font->relative_compose)
22961 /* One extra pixel between two glyphs. */
22962 btm = highest + 1;
22963 else if (ascent <= 0)
22964 /* One extra pixel between two glyphs. */
22965 btm = lowest - 1 - ascent - descent;
22966 }
22967 }
22968 else
22969 {
22970 /* A composition rule is specified by an integer
22971 value that encodes global and new reference
22972 points (GREF and NREF). GREF and NREF are
22973 specified by numbers as below:
22974
22975 0---1---2 -- ascent
22976 | |
22977 | |
22978 | |
22979 9--10--11 -- center
22980 | |
22981 ---3---4---5--- baseline
22982 | |
22983 6---7---8 -- descent
22984 */
22985 int rule = COMPOSITION_RULE (cmp, i);
22986 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22987
22988 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22989 grefx = gref % 3, nrefx = nref % 3;
22990 grefy = gref / 3, nrefy = nref / 3;
22991 if (xoff)
22992 xoff = font_height * (xoff - 128) / 256;
22993 if (yoff)
22994 yoff = font_height * (yoff - 128) / 256;
22995
22996 left = (leftmost
22997 + grefx * (rightmost - leftmost) / 2
22998 - nrefx * width / 2
22999 + xoff);
23000
23001 btm = ((grefy == 0 ? highest
23002 : grefy == 1 ? 0
23003 : grefy == 2 ? lowest
23004 : (highest + lowest) / 2)
23005 - (nrefy == 0 ? ascent + descent
23006 : nrefy == 1 ? descent - boff
23007 : nrefy == 2 ? 0
23008 : (ascent + descent) / 2)
23009 + yoff);
23010 }
23011
23012 cmp->offsets[i * 2] = left;
23013 cmp->offsets[i * 2 + 1] = btm + descent;
23014
23015 /* Update the bounding box of the overall glyphs. */
23016 if (width > 0)
23017 {
23018 right = left + width;
23019 if (left < leftmost)
23020 leftmost = left;
23021 if (right > rightmost)
23022 rightmost = right;
23023 }
23024 top = btm + descent + ascent;
23025 if (top > highest)
23026 highest = top;
23027 if (btm < lowest)
23028 lowest = btm;
23029
23030 if (cmp->lbearing > left + lbearing)
23031 cmp->lbearing = left + lbearing;
23032 if (cmp->rbearing < left + rbearing)
23033 cmp->rbearing = left + rbearing;
23034 }
23035 }
23036
23037 /* If there are glyphs whose x-offsets are negative,
23038 shift all glyphs to the right and make all x-offsets
23039 non-negative. */
23040 if (leftmost < 0)
23041 {
23042 for (i = 0; i < cmp->glyph_len; i++)
23043 cmp->offsets[i * 2] -= leftmost;
23044 rightmost -= leftmost;
23045 cmp->lbearing -= leftmost;
23046 cmp->rbearing -= leftmost;
23047 }
23048
23049 if (left_padded && cmp->lbearing < 0)
23050 {
23051 for (i = 0; i < cmp->glyph_len; i++)
23052 cmp->offsets[i * 2] -= cmp->lbearing;
23053 rightmost -= cmp->lbearing;
23054 cmp->rbearing -= cmp->lbearing;
23055 cmp->lbearing = 0;
23056 }
23057 if (right_padded && rightmost < cmp->rbearing)
23058 {
23059 rightmost = cmp->rbearing;
23060 }
23061
23062 cmp->pixel_width = rightmost;
23063 cmp->ascent = highest;
23064 cmp->descent = - lowest;
23065 if (cmp->ascent < font_ascent)
23066 cmp->ascent = font_ascent;
23067 if (cmp->descent < font_descent)
23068 cmp->descent = font_descent;
23069 }
23070
23071 if (it->glyph_row
23072 && (cmp->lbearing < 0
23073 || cmp->rbearing > cmp->pixel_width))
23074 it->glyph_row->contains_overlapping_glyphs_p = 1;
23075
23076 it->pixel_width = cmp->pixel_width;
23077 it->ascent = it->phys_ascent = cmp->ascent;
23078 it->descent = it->phys_descent = cmp->descent;
23079 if (face->box != FACE_NO_BOX)
23080 {
23081 int thick = face->box_line_width;
23082
23083 if (thick > 0)
23084 {
23085 it->ascent += thick;
23086 it->descent += thick;
23087 }
23088 else
23089 thick = - thick;
23090
23091 if (it->start_of_box_run_p)
23092 it->pixel_width += thick;
23093 if (it->end_of_box_run_p)
23094 it->pixel_width += thick;
23095 }
23096
23097 /* If face has an overline, add the height of the overline
23098 (1 pixel) and a 1 pixel margin to the character height. */
23099 if (face->overline_p)
23100 it->ascent += overline_margin;
23101
23102 take_vertical_position_into_account (it);
23103 if (it->ascent < 0)
23104 it->ascent = 0;
23105 if (it->descent < 0)
23106 it->descent = 0;
23107
23108 if (it->glyph_row)
23109 append_composite_glyph (it);
23110 }
23111 else if (it->what == IT_COMPOSITION)
23112 {
23113 /* A dynamic (automatic) composition. */
23114 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23115 Lisp_Object gstring;
23116 struct font_metrics metrics;
23117
23118 gstring = composition_gstring_from_id (it->cmp_it.id);
23119 it->pixel_width
23120 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23121 &metrics);
23122 if (it->glyph_row
23123 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23124 it->glyph_row->contains_overlapping_glyphs_p = 1;
23125 it->ascent = it->phys_ascent = metrics.ascent;
23126 it->descent = it->phys_descent = metrics.descent;
23127 if (face->box != FACE_NO_BOX)
23128 {
23129 int thick = face->box_line_width;
23130
23131 if (thick > 0)
23132 {
23133 it->ascent += thick;
23134 it->descent += thick;
23135 }
23136 else
23137 thick = - thick;
23138
23139 if (it->start_of_box_run_p)
23140 it->pixel_width += thick;
23141 if (it->end_of_box_run_p)
23142 it->pixel_width += thick;
23143 }
23144 /* If face has an overline, add the height of the overline
23145 (1 pixel) and a 1 pixel margin to the character height. */
23146 if (face->overline_p)
23147 it->ascent += overline_margin;
23148 take_vertical_position_into_account (it);
23149 if (it->ascent < 0)
23150 it->ascent = 0;
23151 if (it->descent < 0)
23152 it->descent = 0;
23153
23154 if (it->glyph_row)
23155 append_composite_glyph (it);
23156 }
23157 else if (it->what == IT_GLYPHLESS)
23158 produce_glyphless_glyph (it, 0, Qnil);
23159 else if (it->what == IT_IMAGE)
23160 produce_image_glyph (it);
23161 else if (it->what == IT_STRETCH)
23162 produce_stretch_glyph (it);
23163
23164 done:
23165 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23166 because this isn't true for images with `:ascent 100'. */
23167 xassert (it->ascent >= 0 && it->descent >= 0);
23168 if (it->area == TEXT_AREA)
23169 it->current_x += it->pixel_width;
23170
23171 if (extra_line_spacing > 0)
23172 {
23173 it->descent += extra_line_spacing;
23174 if (extra_line_spacing > it->max_extra_line_spacing)
23175 it->max_extra_line_spacing = extra_line_spacing;
23176 }
23177
23178 it->max_ascent = max (it->max_ascent, it->ascent);
23179 it->max_descent = max (it->max_descent, it->descent);
23180 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23181 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23182 }
23183
23184 /* EXPORT for RIF:
23185 Output LEN glyphs starting at START at the nominal cursor position.
23186 Advance the nominal cursor over the text. The global variable
23187 updated_window contains the window being updated, updated_row is
23188 the glyph row being updated, and updated_area is the area of that
23189 row being updated. */
23190
23191 void
23192 x_write_glyphs (struct glyph *start, int len)
23193 {
23194 int x, hpos;
23195
23196 xassert (updated_window && updated_row);
23197 BLOCK_INPUT;
23198
23199 /* Write glyphs. */
23200
23201 hpos = start - updated_row->glyphs[updated_area];
23202 x = draw_glyphs (updated_window, output_cursor.x,
23203 updated_row, updated_area,
23204 hpos, hpos + len,
23205 DRAW_NORMAL_TEXT, 0);
23206
23207 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23208 if (updated_area == TEXT_AREA
23209 && updated_window->phys_cursor_on_p
23210 && updated_window->phys_cursor.vpos == output_cursor.vpos
23211 && updated_window->phys_cursor.hpos >= hpos
23212 && updated_window->phys_cursor.hpos < hpos + len)
23213 updated_window->phys_cursor_on_p = 0;
23214
23215 UNBLOCK_INPUT;
23216
23217 /* Advance the output cursor. */
23218 output_cursor.hpos += len;
23219 output_cursor.x = x;
23220 }
23221
23222
23223 /* EXPORT for RIF:
23224 Insert LEN glyphs from START at the nominal cursor position. */
23225
23226 void
23227 x_insert_glyphs (struct glyph *start, int len)
23228 {
23229 struct frame *f;
23230 struct window *w;
23231 int line_height, shift_by_width, shifted_region_width;
23232 struct glyph_row *row;
23233 struct glyph *glyph;
23234 int frame_x, frame_y;
23235 EMACS_INT hpos;
23236
23237 xassert (updated_window && updated_row);
23238 BLOCK_INPUT;
23239 w = updated_window;
23240 f = XFRAME (WINDOW_FRAME (w));
23241
23242 /* Get the height of the line we are in. */
23243 row = updated_row;
23244 line_height = row->height;
23245
23246 /* Get the width of the glyphs to insert. */
23247 shift_by_width = 0;
23248 for (glyph = start; glyph < start + len; ++glyph)
23249 shift_by_width += glyph->pixel_width;
23250
23251 /* Get the width of the region to shift right. */
23252 shifted_region_width = (window_box_width (w, updated_area)
23253 - output_cursor.x
23254 - shift_by_width);
23255
23256 /* Shift right. */
23257 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23258 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23259
23260 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23261 line_height, shift_by_width);
23262
23263 /* Write the glyphs. */
23264 hpos = start - row->glyphs[updated_area];
23265 draw_glyphs (w, output_cursor.x, row, updated_area,
23266 hpos, hpos + len,
23267 DRAW_NORMAL_TEXT, 0);
23268
23269 /* Advance the output cursor. */
23270 output_cursor.hpos += len;
23271 output_cursor.x += shift_by_width;
23272 UNBLOCK_INPUT;
23273 }
23274
23275
23276 /* EXPORT for RIF:
23277 Erase the current text line from the nominal cursor position
23278 (inclusive) to pixel column TO_X (exclusive). The idea is that
23279 everything from TO_X onward is already erased.
23280
23281 TO_X is a pixel position relative to updated_area of
23282 updated_window. TO_X == -1 means clear to the end of this area. */
23283
23284 void
23285 x_clear_end_of_line (int to_x)
23286 {
23287 struct frame *f;
23288 struct window *w = updated_window;
23289 int max_x, min_y, max_y;
23290 int from_x, from_y, to_y;
23291
23292 xassert (updated_window && updated_row);
23293 f = XFRAME (w->frame);
23294
23295 if (updated_row->full_width_p)
23296 max_x = WINDOW_TOTAL_WIDTH (w);
23297 else
23298 max_x = window_box_width (w, updated_area);
23299 max_y = window_text_bottom_y (w);
23300
23301 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23302 of window. For TO_X > 0, truncate to end of drawing area. */
23303 if (to_x == 0)
23304 return;
23305 else if (to_x < 0)
23306 to_x = max_x;
23307 else
23308 to_x = min (to_x, max_x);
23309
23310 to_y = min (max_y, output_cursor.y + updated_row->height);
23311
23312 /* Notice if the cursor will be cleared by this operation. */
23313 if (!updated_row->full_width_p)
23314 notice_overwritten_cursor (w, updated_area,
23315 output_cursor.x, -1,
23316 updated_row->y,
23317 MATRIX_ROW_BOTTOM_Y (updated_row));
23318
23319 from_x = output_cursor.x;
23320
23321 /* Translate to frame coordinates. */
23322 if (updated_row->full_width_p)
23323 {
23324 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23325 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23326 }
23327 else
23328 {
23329 int area_left = window_box_left (w, updated_area);
23330 from_x += area_left;
23331 to_x += area_left;
23332 }
23333
23334 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23335 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23336 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23337
23338 /* Prevent inadvertently clearing to end of the X window. */
23339 if (to_x > from_x && to_y > from_y)
23340 {
23341 BLOCK_INPUT;
23342 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23343 to_x - from_x, to_y - from_y);
23344 UNBLOCK_INPUT;
23345 }
23346 }
23347
23348 #endif /* HAVE_WINDOW_SYSTEM */
23349
23350
23351 \f
23352 /***********************************************************************
23353 Cursor types
23354 ***********************************************************************/
23355
23356 /* Value is the internal representation of the specified cursor type
23357 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23358 of the bar cursor. */
23359
23360 static enum text_cursor_kinds
23361 get_specified_cursor_type (Lisp_Object arg, int *width)
23362 {
23363 enum text_cursor_kinds type;
23364
23365 if (NILP (arg))
23366 return NO_CURSOR;
23367
23368 if (EQ (arg, Qbox))
23369 return FILLED_BOX_CURSOR;
23370
23371 if (EQ (arg, Qhollow))
23372 return HOLLOW_BOX_CURSOR;
23373
23374 if (EQ (arg, Qbar))
23375 {
23376 *width = 2;
23377 return BAR_CURSOR;
23378 }
23379
23380 if (CONSP (arg)
23381 && EQ (XCAR (arg), Qbar)
23382 && INTEGERP (XCDR (arg))
23383 && XINT (XCDR (arg)) >= 0)
23384 {
23385 *width = XINT (XCDR (arg));
23386 return BAR_CURSOR;
23387 }
23388
23389 if (EQ (arg, Qhbar))
23390 {
23391 *width = 2;
23392 return HBAR_CURSOR;
23393 }
23394
23395 if (CONSP (arg)
23396 && EQ (XCAR (arg), Qhbar)
23397 && INTEGERP (XCDR (arg))
23398 && XINT (XCDR (arg)) >= 0)
23399 {
23400 *width = XINT (XCDR (arg));
23401 return HBAR_CURSOR;
23402 }
23403
23404 /* Treat anything unknown as "hollow box cursor".
23405 It was bad to signal an error; people have trouble fixing
23406 .Xdefaults with Emacs, when it has something bad in it. */
23407 type = HOLLOW_BOX_CURSOR;
23408
23409 return type;
23410 }
23411
23412 /* Set the default cursor types for specified frame. */
23413 void
23414 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23415 {
23416 int width;
23417 Lisp_Object tem;
23418
23419 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23420 FRAME_CURSOR_WIDTH (f) = width;
23421
23422 /* By default, set up the blink-off state depending on the on-state. */
23423
23424 tem = Fassoc (arg, Vblink_cursor_alist);
23425 if (!NILP (tem))
23426 {
23427 FRAME_BLINK_OFF_CURSOR (f)
23428 = get_specified_cursor_type (XCDR (tem), &width);
23429 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23430 }
23431 else
23432 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23433 }
23434
23435
23436 #ifdef HAVE_WINDOW_SYSTEM
23437
23438 /* Return the cursor we want to be displayed in window W. Return
23439 width of bar/hbar cursor through WIDTH arg. Return with
23440 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23441 (i.e. if the `system caret' should track this cursor).
23442
23443 In a mini-buffer window, we want the cursor only to appear if we
23444 are reading input from this window. For the selected window, we
23445 want the cursor type given by the frame parameter or buffer local
23446 setting of cursor-type. If explicitly marked off, draw no cursor.
23447 In all other cases, we want a hollow box cursor. */
23448
23449 static enum text_cursor_kinds
23450 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23451 int *active_cursor)
23452 {
23453 struct frame *f = XFRAME (w->frame);
23454 struct buffer *b = XBUFFER (w->buffer);
23455 int cursor_type = DEFAULT_CURSOR;
23456 Lisp_Object alt_cursor;
23457 int non_selected = 0;
23458
23459 *active_cursor = 1;
23460
23461 /* Echo area */
23462 if (cursor_in_echo_area
23463 && FRAME_HAS_MINIBUF_P (f)
23464 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23465 {
23466 if (w == XWINDOW (echo_area_window))
23467 {
23468 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
23469 {
23470 *width = FRAME_CURSOR_WIDTH (f);
23471 return FRAME_DESIRED_CURSOR (f);
23472 }
23473 else
23474 return get_specified_cursor_type (b->cursor_type, width);
23475 }
23476
23477 *active_cursor = 0;
23478 non_selected = 1;
23479 }
23480
23481 /* Detect a nonselected window or nonselected frame. */
23482 else if (w != XWINDOW (f->selected_window)
23483 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
23484 {
23485 *active_cursor = 0;
23486
23487 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23488 return NO_CURSOR;
23489
23490 non_selected = 1;
23491 }
23492
23493 /* Never display a cursor in a window in which cursor-type is nil. */
23494 if (NILP (b->cursor_type))
23495 return NO_CURSOR;
23496
23497 /* Get the normal cursor type for this window. */
23498 if (EQ (b->cursor_type, Qt))
23499 {
23500 cursor_type = FRAME_DESIRED_CURSOR (f);
23501 *width = FRAME_CURSOR_WIDTH (f);
23502 }
23503 else
23504 cursor_type = get_specified_cursor_type (b->cursor_type, width);
23505
23506 /* Use cursor-in-non-selected-windows instead
23507 for non-selected window or frame. */
23508 if (non_selected)
23509 {
23510 alt_cursor = b->cursor_in_non_selected_windows;
23511 if (!EQ (Qt, alt_cursor))
23512 return get_specified_cursor_type (alt_cursor, width);
23513 /* t means modify the normal cursor type. */
23514 if (cursor_type == FILLED_BOX_CURSOR)
23515 cursor_type = HOLLOW_BOX_CURSOR;
23516 else if (cursor_type == BAR_CURSOR && *width > 1)
23517 --*width;
23518 return cursor_type;
23519 }
23520
23521 /* Use normal cursor if not blinked off. */
23522 if (!w->cursor_off_p)
23523 {
23524 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23525 {
23526 if (cursor_type == FILLED_BOX_CURSOR)
23527 {
23528 /* Using a block cursor on large images can be very annoying.
23529 So use a hollow cursor for "large" images.
23530 If image is not transparent (no mask), also use hollow cursor. */
23531 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23532 if (img != NULL && IMAGEP (img->spec))
23533 {
23534 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23535 where N = size of default frame font size.
23536 This should cover most of the "tiny" icons people may use. */
23537 if (!img->mask
23538 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23539 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23540 cursor_type = HOLLOW_BOX_CURSOR;
23541 }
23542 }
23543 else if (cursor_type != NO_CURSOR)
23544 {
23545 /* Display current only supports BOX and HOLLOW cursors for images.
23546 So for now, unconditionally use a HOLLOW cursor when cursor is
23547 not a solid box cursor. */
23548 cursor_type = HOLLOW_BOX_CURSOR;
23549 }
23550 }
23551 return cursor_type;
23552 }
23553
23554 /* Cursor is blinked off, so determine how to "toggle" it. */
23555
23556 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23557 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
23558 return get_specified_cursor_type (XCDR (alt_cursor), width);
23559
23560 /* Then see if frame has specified a specific blink off cursor type. */
23561 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23562 {
23563 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23564 return FRAME_BLINK_OFF_CURSOR (f);
23565 }
23566
23567 #if 0
23568 /* Some people liked having a permanently visible blinking cursor,
23569 while others had very strong opinions against it. So it was
23570 decided to remove it. KFS 2003-09-03 */
23571
23572 /* Finally perform built-in cursor blinking:
23573 filled box <-> hollow box
23574 wide [h]bar <-> narrow [h]bar
23575 narrow [h]bar <-> no cursor
23576 other type <-> no cursor */
23577
23578 if (cursor_type == FILLED_BOX_CURSOR)
23579 return HOLLOW_BOX_CURSOR;
23580
23581 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23582 {
23583 *width = 1;
23584 return cursor_type;
23585 }
23586 #endif
23587
23588 return NO_CURSOR;
23589 }
23590
23591
23592 /* Notice when the text cursor of window W has been completely
23593 overwritten by a drawing operation that outputs glyphs in AREA
23594 starting at X0 and ending at X1 in the line starting at Y0 and
23595 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23596 the rest of the line after X0 has been written. Y coordinates
23597 are window-relative. */
23598
23599 static void
23600 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23601 int x0, int x1, int y0, int y1)
23602 {
23603 int cx0, cx1, cy0, cy1;
23604 struct glyph_row *row;
23605
23606 if (!w->phys_cursor_on_p)
23607 return;
23608 if (area != TEXT_AREA)
23609 return;
23610
23611 if (w->phys_cursor.vpos < 0
23612 || w->phys_cursor.vpos >= w->current_matrix->nrows
23613 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23614 !(row->enabled_p && row->displays_text_p)))
23615 return;
23616
23617 if (row->cursor_in_fringe_p)
23618 {
23619 row->cursor_in_fringe_p = 0;
23620 draw_fringe_bitmap (w, row, row->reversed_p);
23621 w->phys_cursor_on_p = 0;
23622 return;
23623 }
23624
23625 cx0 = w->phys_cursor.x;
23626 cx1 = cx0 + w->phys_cursor_width;
23627 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23628 return;
23629
23630 /* The cursor image will be completely removed from the
23631 screen if the output area intersects the cursor area in
23632 y-direction. When we draw in [y0 y1[, and some part of
23633 the cursor is at y < y0, that part must have been drawn
23634 before. When scrolling, the cursor is erased before
23635 actually scrolling, so we don't come here. When not
23636 scrolling, the rows above the old cursor row must have
23637 changed, and in this case these rows must have written
23638 over the cursor image.
23639
23640 Likewise if part of the cursor is below y1, with the
23641 exception of the cursor being in the first blank row at
23642 the buffer and window end because update_text_area
23643 doesn't draw that row. (Except when it does, but
23644 that's handled in update_text_area.) */
23645
23646 cy0 = w->phys_cursor.y;
23647 cy1 = cy0 + w->phys_cursor_height;
23648 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23649 return;
23650
23651 w->phys_cursor_on_p = 0;
23652 }
23653
23654 #endif /* HAVE_WINDOW_SYSTEM */
23655
23656 \f
23657 /************************************************************************
23658 Mouse Face
23659 ************************************************************************/
23660
23661 #ifdef HAVE_WINDOW_SYSTEM
23662
23663 /* EXPORT for RIF:
23664 Fix the display of area AREA of overlapping row ROW in window W
23665 with respect to the overlapping part OVERLAPS. */
23666
23667 void
23668 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
23669 enum glyph_row_area area, int overlaps)
23670 {
23671 int i, x;
23672
23673 BLOCK_INPUT;
23674
23675 x = 0;
23676 for (i = 0; i < row->used[area];)
23677 {
23678 if (row->glyphs[area][i].overlaps_vertically_p)
23679 {
23680 int start = i, start_x = x;
23681
23682 do
23683 {
23684 x += row->glyphs[area][i].pixel_width;
23685 ++i;
23686 }
23687 while (i < row->used[area]
23688 && row->glyphs[area][i].overlaps_vertically_p);
23689
23690 draw_glyphs (w, start_x, row, area,
23691 start, i,
23692 DRAW_NORMAL_TEXT, overlaps);
23693 }
23694 else
23695 {
23696 x += row->glyphs[area][i].pixel_width;
23697 ++i;
23698 }
23699 }
23700
23701 UNBLOCK_INPUT;
23702 }
23703
23704
23705 /* EXPORT:
23706 Draw the cursor glyph of window W in glyph row ROW. See the
23707 comment of draw_glyphs for the meaning of HL. */
23708
23709 void
23710 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23711 enum draw_glyphs_face hl)
23712 {
23713 /* If cursor hpos is out of bounds, don't draw garbage. This can
23714 happen in mini-buffer windows when switching between echo area
23715 glyphs and mini-buffer. */
23716 if ((row->reversed_p
23717 ? (w->phys_cursor.hpos >= 0)
23718 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23719 {
23720 int on_p = w->phys_cursor_on_p;
23721 int x1;
23722 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23723 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23724 hl, 0);
23725 w->phys_cursor_on_p = on_p;
23726
23727 if (hl == DRAW_CURSOR)
23728 w->phys_cursor_width = x1 - w->phys_cursor.x;
23729 /* When we erase the cursor, and ROW is overlapped by other
23730 rows, make sure that these overlapping parts of other rows
23731 are redrawn. */
23732 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23733 {
23734 w->phys_cursor_width = x1 - w->phys_cursor.x;
23735
23736 if (row > w->current_matrix->rows
23737 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23738 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23739 OVERLAPS_ERASED_CURSOR);
23740
23741 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23742 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23743 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23744 OVERLAPS_ERASED_CURSOR);
23745 }
23746 }
23747 }
23748
23749
23750 /* EXPORT:
23751 Erase the image of a cursor of window W from the screen. */
23752
23753 void
23754 erase_phys_cursor (struct window *w)
23755 {
23756 struct frame *f = XFRAME (w->frame);
23757 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23758 int hpos = w->phys_cursor.hpos;
23759 int vpos = w->phys_cursor.vpos;
23760 int mouse_face_here_p = 0;
23761 struct glyph_matrix *active_glyphs = w->current_matrix;
23762 struct glyph_row *cursor_row;
23763 struct glyph *cursor_glyph;
23764 enum draw_glyphs_face hl;
23765
23766 /* No cursor displayed or row invalidated => nothing to do on the
23767 screen. */
23768 if (w->phys_cursor_type == NO_CURSOR)
23769 goto mark_cursor_off;
23770
23771 /* VPOS >= active_glyphs->nrows means that window has been resized.
23772 Don't bother to erase the cursor. */
23773 if (vpos >= active_glyphs->nrows)
23774 goto mark_cursor_off;
23775
23776 /* If row containing cursor is marked invalid, there is nothing we
23777 can do. */
23778 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23779 if (!cursor_row->enabled_p)
23780 goto mark_cursor_off;
23781
23782 /* If line spacing is > 0, old cursor may only be partially visible in
23783 window after split-window. So adjust visible height. */
23784 cursor_row->visible_height = min (cursor_row->visible_height,
23785 window_text_bottom_y (w) - cursor_row->y);
23786
23787 /* If row is completely invisible, don't attempt to delete a cursor which
23788 isn't there. This can happen if cursor is at top of a window, and
23789 we switch to a buffer with a header line in that window. */
23790 if (cursor_row->visible_height <= 0)
23791 goto mark_cursor_off;
23792
23793 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23794 if (cursor_row->cursor_in_fringe_p)
23795 {
23796 cursor_row->cursor_in_fringe_p = 0;
23797 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23798 goto mark_cursor_off;
23799 }
23800
23801 /* This can happen when the new row is shorter than the old one.
23802 In this case, either draw_glyphs or clear_end_of_line
23803 should have cleared the cursor. Note that we wouldn't be
23804 able to erase the cursor in this case because we don't have a
23805 cursor glyph at hand. */
23806 if ((cursor_row->reversed_p
23807 ? (w->phys_cursor.hpos < 0)
23808 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23809 goto mark_cursor_off;
23810
23811 /* If the cursor is in the mouse face area, redisplay that when
23812 we clear the cursor. */
23813 if (! NILP (hlinfo->mouse_face_window)
23814 && coords_in_mouse_face_p (w, hpos, vpos)
23815 /* Don't redraw the cursor's spot in mouse face if it is at the
23816 end of a line (on a newline). The cursor appears there, but
23817 mouse highlighting does not. */
23818 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
23819 mouse_face_here_p = 1;
23820
23821 /* Maybe clear the display under the cursor. */
23822 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23823 {
23824 int x, y, left_x;
23825 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23826 int width;
23827
23828 cursor_glyph = get_phys_cursor_glyph (w);
23829 if (cursor_glyph == NULL)
23830 goto mark_cursor_off;
23831
23832 width = cursor_glyph->pixel_width;
23833 left_x = window_box_left_offset (w, TEXT_AREA);
23834 x = w->phys_cursor.x;
23835 if (x < left_x)
23836 width -= left_x - x;
23837 width = min (width, window_box_width (w, TEXT_AREA) - x);
23838 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23839 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23840
23841 if (width > 0)
23842 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23843 }
23844
23845 /* Erase the cursor by redrawing the character underneath it. */
23846 if (mouse_face_here_p)
23847 hl = DRAW_MOUSE_FACE;
23848 else
23849 hl = DRAW_NORMAL_TEXT;
23850 draw_phys_cursor_glyph (w, cursor_row, hl);
23851
23852 mark_cursor_off:
23853 w->phys_cursor_on_p = 0;
23854 w->phys_cursor_type = NO_CURSOR;
23855 }
23856
23857
23858 /* EXPORT:
23859 Display or clear cursor of window W. If ON is zero, clear the
23860 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23861 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23862
23863 void
23864 display_and_set_cursor (struct window *w, int on,
23865 int hpos, int vpos, int x, int y)
23866 {
23867 struct frame *f = XFRAME (w->frame);
23868 int new_cursor_type;
23869 int new_cursor_width;
23870 int active_cursor;
23871 struct glyph_row *glyph_row;
23872 struct glyph *glyph;
23873
23874 /* This is pointless on invisible frames, and dangerous on garbaged
23875 windows and frames; in the latter case, the frame or window may
23876 be in the midst of changing its size, and x and y may be off the
23877 window. */
23878 if (! FRAME_VISIBLE_P (f)
23879 || FRAME_GARBAGED_P (f)
23880 || vpos >= w->current_matrix->nrows
23881 || hpos >= w->current_matrix->matrix_w)
23882 return;
23883
23884 /* If cursor is off and we want it off, return quickly. */
23885 if (!on && !w->phys_cursor_on_p)
23886 return;
23887
23888 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23889 /* If cursor row is not enabled, we don't really know where to
23890 display the cursor. */
23891 if (!glyph_row->enabled_p)
23892 {
23893 w->phys_cursor_on_p = 0;
23894 return;
23895 }
23896
23897 glyph = NULL;
23898 if (!glyph_row->exact_window_width_line_p
23899 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
23900 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23901
23902 xassert (interrupt_input_blocked);
23903
23904 /* Set new_cursor_type to the cursor we want to be displayed. */
23905 new_cursor_type = get_window_cursor_type (w, glyph,
23906 &new_cursor_width, &active_cursor);
23907
23908 /* If cursor is currently being shown and we don't want it to be or
23909 it is in the wrong place, or the cursor type is not what we want,
23910 erase it. */
23911 if (w->phys_cursor_on_p
23912 && (!on
23913 || w->phys_cursor.x != x
23914 || w->phys_cursor.y != y
23915 || new_cursor_type != w->phys_cursor_type
23916 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23917 && new_cursor_width != w->phys_cursor_width)))
23918 erase_phys_cursor (w);
23919
23920 /* Don't check phys_cursor_on_p here because that flag is only set
23921 to zero in some cases where we know that the cursor has been
23922 completely erased, to avoid the extra work of erasing the cursor
23923 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23924 still not be visible, or it has only been partly erased. */
23925 if (on)
23926 {
23927 w->phys_cursor_ascent = glyph_row->ascent;
23928 w->phys_cursor_height = glyph_row->height;
23929
23930 /* Set phys_cursor_.* before x_draw_.* is called because some
23931 of them may need the information. */
23932 w->phys_cursor.x = x;
23933 w->phys_cursor.y = glyph_row->y;
23934 w->phys_cursor.hpos = hpos;
23935 w->phys_cursor.vpos = vpos;
23936 }
23937
23938 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23939 new_cursor_type, new_cursor_width,
23940 on, active_cursor);
23941 }
23942
23943
23944 /* Switch the display of W's cursor on or off, according to the value
23945 of ON. */
23946
23947 void
23948 update_window_cursor (struct window *w, int on)
23949 {
23950 /* Don't update cursor in windows whose frame is in the process
23951 of being deleted. */
23952 if (w->current_matrix)
23953 {
23954 BLOCK_INPUT;
23955 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23956 w->phys_cursor.x, w->phys_cursor.y);
23957 UNBLOCK_INPUT;
23958 }
23959 }
23960
23961
23962 /* Call update_window_cursor with parameter ON_P on all leaf windows
23963 in the window tree rooted at W. */
23964
23965 static void
23966 update_cursor_in_window_tree (struct window *w, int on_p)
23967 {
23968 while (w)
23969 {
23970 if (!NILP (w->hchild))
23971 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23972 else if (!NILP (w->vchild))
23973 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23974 else
23975 update_window_cursor (w, on_p);
23976
23977 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23978 }
23979 }
23980
23981
23982 /* EXPORT:
23983 Display the cursor on window W, or clear it, according to ON_P.
23984 Don't change the cursor's position. */
23985
23986 void
23987 x_update_cursor (struct frame *f, int on_p)
23988 {
23989 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23990 }
23991
23992
23993 /* EXPORT:
23994 Clear the cursor of window W to background color, and mark the
23995 cursor as not shown. This is used when the text where the cursor
23996 is about to be rewritten. */
23997
23998 void
23999 x_clear_cursor (struct window *w)
24000 {
24001 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24002 update_window_cursor (w, 0);
24003 }
24004
24005 #endif /* HAVE_WINDOW_SYSTEM */
24006
24007 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24008 and MSDOS. */
24009 void
24010 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24011 int start_hpos, int end_hpos,
24012 enum draw_glyphs_face draw)
24013 {
24014 #ifdef HAVE_WINDOW_SYSTEM
24015 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24016 {
24017 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24018 return;
24019 }
24020 #endif
24021 #if defined (HAVE_GPM) || defined (MSDOS)
24022 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24023 #endif
24024 }
24025
24026 /* EXPORT:
24027 Display the active region described by mouse_face_* according to DRAW. */
24028
24029 void
24030 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24031 {
24032 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24033 struct frame *f = XFRAME (WINDOW_FRAME (w));
24034
24035 if (/* If window is in the process of being destroyed, don't bother
24036 to do anything. */
24037 w->current_matrix != NULL
24038 /* Don't update mouse highlight if hidden */
24039 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24040 /* Recognize when we are called to operate on rows that don't exist
24041 anymore. This can happen when a window is split. */
24042 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24043 {
24044 int phys_cursor_on_p = w->phys_cursor_on_p;
24045 struct glyph_row *row, *first, *last;
24046
24047 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24048 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24049
24050 for (row = first; row <= last && row->enabled_p; ++row)
24051 {
24052 int start_hpos, end_hpos, start_x;
24053
24054 /* For all but the first row, the highlight starts at column 0. */
24055 if (row == first)
24056 {
24057 /* R2L rows have BEG and END in reversed order, but the
24058 screen drawing geometry is always left to right. So
24059 we need to mirror the beginning and end of the
24060 highlighted area in R2L rows. */
24061 if (!row->reversed_p)
24062 {
24063 start_hpos = hlinfo->mouse_face_beg_col;
24064 start_x = hlinfo->mouse_face_beg_x;
24065 }
24066 else if (row == last)
24067 {
24068 start_hpos = hlinfo->mouse_face_end_col;
24069 start_x = hlinfo->mouse_face_end_x;
24070 }
24071 else
24072 {
24073 start_hpos = 0;
24074 start_x = 0;
24075 }
24076 }
24077 else if (row->reversed_p && row == last)
24078 {
24079 start_hpos = hlinfo->mouse_face_end_col;
24080 start_x = hlinfo->mouse_face_end_x;
24081 }
24082 else
24083 {
24084 start_hpos = 0;
24085 start_x = 0;
24086 }
24087
24088 if (row == last)
24089 {
24090 if (!row->reversed_p)
24091 end_hpos = hlinfo->mouse_face_end_col;
24092 else if (row == first)
24093 end_hpos = hlinfo->mouse_face_beg_col;
24094 else
24095 {
24096 end_hpos = row->used[TEXT_AREA];
24097 if (draw == DRAW_NORMAL_TEXT)
24098 row->fill_line_p = 1; /* Clear to end of line */
24099 }
24100 }
24101 else if (row->reversed_p && row == first)
24102 end_hpos = hlinfo->mouse_face_beg_col;
24103 else
24104 {
24105 end_hpos = row->used[TEXT_AREA];
24106 if (draw == DRAW_NORMAL_TEXT)
24107 row->fill_line_p = 1; /* Clear to end of line */
24108 }
24109
24110 if (end_hpos > start_hpos)
24111 {
24112 draw_row_with_mouse_face (w, start_x, row,
24113 start_hpos, end_hpos, draw);
24114
24115 row->mouse_face_p
24116 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24117 }
24118 }
24119
24120 #ifdef HAVE_WINDOW_SYSTEM
24121 /* When we've written over the cursor, arrange for it to
24122 be displayed again. */
24123 if (FRAME_WINDOW_P (f)
24124 && phys_cursor_on_p && !w->phys_cursor_on_p)
24125 {
24126 BLOCK_INPUT;
24127 display_and_set_cursor (w, 1,
24128 w->phys_cursor.hpos, w->phys_cursor.vpos,
24129 w->phys_cursor.x, w->phys_cursor.y);
24130 UNBLOCK_INPUT;
24131 }
24132 #endif /* HAVE_WINDOW_SYSTEM */
24133 }
24134
24135 #ifdef HAVE_WINDOW_SYSTEM
24136 /* Change the mouse cursor. */
24137 if (FRAME_WINDOW_P (f))
24138 {
24139 if (draw == DRAW_NORMAL_TEXT
24140 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24141 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24142 else if (draw == DRAW_MOUSE_FACE)
24143 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24144 else
24145 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24146 }
24147 #endif /* HAVE_WINDOW_SYSTEM */
24148 }
24149
24150 /* EXPORT:
24151 Clear out the mouse-highlighted active region.
24152 Redraw it un-highlighted first. Value is non-zero if mouse
24153 face was actually drawn unhighlighted. */
24154
24155 int
24156 clear_mouse_face (Mouse_HLInfo *hlinfo)
24157 {
24158 int cleared = 0;
24159
24160 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24161 {
24162 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24163 cleared = 1;
24164 }
24165
24166 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24167 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24168 hlinfo->mouse_face_window = Qnil;
24169 hlinfo->mouse_face_overlay = Qnil;
24170 return cleared;
24171 }
24172
24173 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24174 within the mouse face on that window. */
24175 static int
24176 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24177 {
24178 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24179
24180 /* Quickly resolve the easy cases. */
24181 if (!(WINDOWP (hlinfo->mouse_face_window)
24182 && XWINDOW (hlinfo->mouse_face_window) == w))
24183 return 0;
24184 if (vpos < hlinfo->mouse_face_beg_row
24185 || vpos > hlinfo->mouse_face_end_row)
24186 return 0;
24187 if (vpos > hlinfo->mouse_face_beg_row
24188 && vpos < hlinfo->mouse_face_end_row)
24189 return 1;
24190
24191 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24192 {
24193 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24194 {
24195 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24196 return 1;
24197 }
24198 else if ((vpos == hlinfo->mouse_face_beg_row
24199 && hpos >= hlinfo->mouse_face_beg_col)
24200 || (vpos == hlinfo->mouse_face_end_row
24201 && hpos < hlinfo->mouse_face_end_col))
24202 return 1;
24203 }
24204 else
24205 {
24206 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24207 {
24208 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24209 return 1;
24210 }
24211 else if ((vpos == hlinfo->mouse_face_beg_row
24212 && hpos <= hlinfo->mouse_face_beg_col)
24213 || (vpos == hlinfo->mouse_face_end_row
24214 && hpos > hlinfo->mouse_face_end_col))
24215 return 1;
24216 }
24217 return 0;
24218 }
24219
24220
24221 /* EXPORT:
24222 Non-zero if physical cursor of window W is within mouse face. */
24223
24224 int
24225 cursor_in_mouse_face_p (struct window *w)
24226 {
24227 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24228 }
24229
24230
24231 \f
24232 /* Find the glyph rows START_ROW and END_ROW of window W that display
24233 characters between buffer positions START_CHARPOS and END_CHARPOS
24234 (excluding END_CHARPOS). This is similar to row_containing_pos,
24235 but is more accurate when bidi reordering makes buffer positions
24236 change non-linearly with glyph rows. */
24237 static void
24238 rows_from_pos_range (struct window *w,
24239 EMACS_INT start_charpos, EMACS_INT end_charpos,
24240 struct glyph_row **start, struct glyph_row **end)
24241 {
24242 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24243 int last_y = window_text_bottom_y (w);
24244 struct glyph_row *row;
24245
24246 *start = NULL;
24247 *end = NULL;
24248
24249 while (!first->enabled_p
24250 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24251 first++;
24252
24253 /* Find the START row. */
24254 for (row = first;
24255 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24256 row++)
24257 {
24258 /* A row can potentially be the START row if the range of the
24259 characters it displays intersects the range
24260 [START_CHARPOS..END_CHARPOS). */
24261 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24262 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24263 /* See the commentary in row_containing_pos, for the
24264 explanation of the complicated way to check whether
24265 some position is beyond the end of the characters
24266 displayed by a row. */
24267 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24268 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24269 && !row->ends_at_zv_p
24270 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24271 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24272 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24273 && !row->ends_at_zv_p
24274 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24275 {
24276 /* Found a candidate row. Now make sure at least one of the
24277 glyphs it displays has a charpos from the range
24278 [START_CHARPOS..END_CHARPOS).
24279
24280 This is not obvious because bidi reordering could make
24281 buffer positions of a row be 1,2,3,102,101,100, and if we
24282 want to highlight characters in [50..60), we don't want
24283 this row, even though [50..60) does intersect [1..103),
24284 the range of character positions given by the row's start
24285 and end positions. */
24286 struct glyph *g = row->glyphs[TEXT_AREA];
24287 struct glyph *e = g + row->used[TEXT_AREA];
24288
24289 while (g < e)
24290 {
24291 if (BUFFERP (g->object)
24292 && start_charpos <= g->charpos && g->charpos < end_charpos)
24293 *start = row;
24294 g++;
24295 }
24296 if (*start)
24297 break;
24298 }
24299 }
24300
24301 /* Find the END row. */
24302 if (!*start
24303 /* If the last row is partially visible, start looking for END
24304 from that row, instead of starting from FIRST. */
24305 && !(row->enabled_p
24306 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24307 row = first;
24308 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24309 {
24310 struct glyph_row *next = row + 1;
24311
24312 if (!next->enabled_p
24313 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24314 /* The first row >= START whose range of displayed characters
24315 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24316 is the row END + 1. */
24317 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24318 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24319 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24320 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24321 && !next->ends_at_zv_p
24322 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24323 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24324 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24325 && !next->ends_at_zv_p
24326 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24327 {
24328 *end = row;
24329 break;
24330 }
24331 else
24332 {
24333 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24334 but none of the characters it displays are in the range, it is
24335 also END + 1. */
24336 struct glyph *g = next->glyphs[TEXT_AREA];
24337 struct glyph *e = g + next->used[TEXT_AREA];
24338
24339 while (g < e)
24340 {
24341 if (BUFFERP (g->object)
24342 && start_charpos <= g->charpos && g->charpos < end_charpos)
24343 break;
24344 g++;
24345 }
24346 if (g == e)
24347 {
24348 *end = row;
24349 break;
24350 }
24351 }
24352 }
24353 }
24354
24355 /* This function sets the mouse_face_* elements of HLINFO, assuming
24356 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24357 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24358 for the overlay or run of text properties specifying the mouse
24359 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24360 before-string and after-string that must also be highlighted.
24361 DISPLAY_STRING, if non-nil, is a display string that may cover some
24362 or all of the highlighted text. */
24363
24364 static void
24365 mouse_face_from_buffer_pos (Lisp_Object window,
24366 Mouse_HLInfo *hlinfo,
24367 EMACS_INT mouse_charpos,
24368 EMACS_INT start_charpos,
24369 EMACS_INT end_charpos,
24370 Lisp_Object before_string,
24371 Lisp_Object after_string,
24372 Lisp_Object display_string)
24373 {
24374 struct window *w = XWINDOW (window);
24375 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24376 struct glyph_row *r1, *r2;
24377 struct glyph *glyph, *end;
24378 EMACS_INT ignore, pos;
24379 int x;
24380
24381 xassert (NILP (display_string) || STRINGP (display_string));
24382 xassert (NILP (before_string) || STRINGP (before_string));
24383 xassert (NILP (after_string) || STRINGP (after_string));
24384
24385 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
24386 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
24387 if (r1 == NULL)
24388 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24389 /* If the before-string or display-string contains newlines,
24390 rows_from_pos_range skips to its last row. Move back. */
24391 if (!NILP (before_string) || !NILP (display_string))
24392 {
24393 struct glyph_row *prev;
24394 while ((prev = r1 - 1, prev >= first)
24395 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
24396 && prev->used[TEXT_AREA] > 0)
24397 {
24398 struct glyph *beg = prev->glyphs[TEXT_AREA];
24399 glyph = beg + prev->used[TEXT_AREA];
24400 while (--glyph >= beg && INTEGERP (glyph->object));
24401 if (glyph < beg
24402 || !(EQ (glyph->object, before_string)
24403 || EQ (glyph->object, display_string)))
24404 break;
24405 r1 = prev;
24406 }
24407 }
24408 if (r2 == NULL)
24409 {
24410 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24411 hlinfo->mouse_face_past_end = 1;
24412 }
24413 else if (!NILP (after_string))
24414 {
24415 /* If the after-string has newlines, advance to its last row. */
24416 struct glyph_row *next;
24417 struct glyph_row *last
24418 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24419
24420 for (next = r2 + 1;
24421 next <= last
24422 && next->used[TEXT_AREA] > 0
24423 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
24424 ++next)
24425 r2 = next;
24426 }
24427 /* The rest of the display engine assumes that mouse_face_beg_row is
24428 either above below mouse_face_end_row or identical to it. But
24429 with bidi-reordered continued lines, the row for START_CHARPOS
24430 could be below the row for END_CHARPOS. If so, swap the rows and
24431 store them in correct order. */
24432 if (r1->y > r2->y)
24433 {
24434 struct glyph_row *tem = r2;
24435
24436 r2 = r1;
24437 r1 = tem;
24438 }
24439
24440 hlinfo->mouse_face_beg_y = r1->y;
24441 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
24442 hlinfo->mouse_face_end_y = r2->y;
24443 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
24444
24445 /* For a bidi-reordered row, the positions of BEFORE_STRING,
24446 AFTER_STRING, DISPLAY_STRING, START_CHARPOS, and END_CHARPOS
24447 could be anywhere in the row and in any order. The strategy
24448 below is to find the leftmost and the rightmost glyph that
24449 belongs to either of these 3 strings, or whose position is
24450 between START_CHARPOS and END_CHARPOS, and highlight all the
24451 glyphs between those two. This may cover more than just the text
24452 between START_CHARPOS and END_CHARPOS if the range of characters
24453 strides the bidi level boundary, e.g. if the beginning is in R2L
24454 text while the end is in L2R text or vice versa. */
24455 if (!r1->reversed_p)
24456 {
24457 /* This row is in a left to right paragraph. Scan it left to
24458 right. */
24459 glyph = r1->glyphs[TEXT_AREA];
24460 end = glyph + r1->used[TEXT_AREA];
24461 x = r1->x;
24462
24463 /* Skip truncation glyphs at the start of the glyph row. */
24464 if (r1->displays_text_p)
24465 for (; glyph < end
24466 && INTEGERP (glyph->object)
24467 && glyph->charpos < 0;
24468 ++glyph)
24469 x += glyph->pixel_width;
24470
24471 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24472 or DISPLAY_STRING, and the first glyph from buffer whose
24473 position is between START_CHARPOS and END_CHARPOS. */
24474 for (; glyph < end
24475 && !INTEGERP (glyph->object)
24476 && !EQ (glyph->object, display_string)
24477 && !(BUFFERP (glyph->object)
24478 && (glyph->charpos >= start_charpos
24479 && glyph->charpos < end_charpos));
24480 ++glyph)
24481 {
24482 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24483 are present at buffer positions between START_CHARPOS and
24484 END_CHARPOS, or if they come from an overlay. */
24485 if (EQ (glyph->object, before_string))
24486 {
24487 pos = string_buffer_position (w, before_string,
24488 start_charpos);
24489 /* If pos == 0, it means before_string came from an
24490 overlay, not from a buffer position. */
24491 if (!pos || (pos >= start_charpos && pos < end_charpos))
24492 break;
24493 }
24494 else if (EQ (glyph->object, after_string))
24495 {
24496 pos = string_buffer_position (w, after_string, end_charpos);
24497 if (!pos || (pos >= start_charpos && pos < end_charpos))
24498 break;
24499 }
24500 x += glyph->pixel_width;
24501 }
24502 hlinfo->mouse_face_beg_x = x;
24503 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24504 }
24505 else
24506 {
24507 /* This row is in a right to left paragraph. Scan it right to
24508 left. */
24509 struct glyph *g;
24510
24511 end = r1->glyphs[TEXT_AREA] - 1;
24512 glyph = end + r1->used[TEXT_AREA];
24513
24514 /* Skip truncation glyphs at the start of the glyph row. */
24515 if (r1->displays_text_p)
24516 for (; glyph > end
24517 && INTEGERP (glyph->object)
24518 && glyph->charpos < 0;
24519 --glyph)
24520 ;
24521
24522 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24523 or DISPLAY_STRING, and the first glyph from buffer whose
24524 position is between START_CHARPOS and END_CHARPOS. */
24525 for (; glyph > end
24526 && !INTEGERP (glyph->object)
24527 && !EQ (glyph->object, display_string)
24528 && !(BUFFERP (glyph->object)
24529 && (glyph->charpos >= start_charpos
24530 && glyph->charpos < end_charpos));
24531 --glyph)
24532 {
24533 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24534 are present at buffer positions between START_CHARPOS and
24535 END_CHARPOS, or if they come from an overlay. */
24536 if (EQ (glyph->object, before_string))
24537 {
24538 pos = string_buffer_position (w, before_string, start_charpos);
24539 /* If pos == 0, it means before_string came from an
24540 overlay, not from a buffer position. */
24541 if (!pos || (pos >= start_charpos && pos < end_charpos))
24542 break;
24543 }
24544 else if (EQ (glyph->object, after_string))
24545 {
24546 pos = string_buffer_position (w, after_string, end_charpos);
24547 if (!pos || (pos >= start_charpos && pos < end_charpos))
24548 break;
24549 }
24550 }
24551
24552 glyph++; /* first glyph to the right of the highlighted area */
24553 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
24554 x += g->pixel_width;
24555 hlinfo->mouse_face_beg_x = x;
24556 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24557 }
24558
24559 /* If the highlight ends in a different row, compute GLYPH and END
24560 for the end row. Otherwise, reuse the values computed above for
24561 the row where the highlight begins. */
24562 if (r2 != r1)
24563 {
24564 if (!r2->reversed_p)
24565 {
24566 glyph = r2->glyphs[TEXT_AREA];
24567 end = glyph + r2->used[TEXT_AREA];
24568 x = r2->x;
24569 }
24570 else
24571 {
24572 end = r2->glyphs[TEXT_AREA] - 1;
24573 glyph = end + r2->used[TEXT_AREA];
24574 }
24575 }
24576
24577 if (!r2->reversed_p)
24578 {
24579 /* Skip truncation and continuation glyphs near the end of the
24580 row, and also blanks and stretch glyphs inserted by
24581 extend_face_to_end_of_line. */
24582 while (end > glyph
24583 && INTEGERP ((end - 1)->object)
24584 && (end - 1)->charpos <= 0)
24585 --end;
24586 /* Scan the rest of the glyph row from the end, looking for the
24587 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24588 DISPLAY_STRING, or whose position is between START_CHARPOS
24589 and END_CHARPOS */
24590 for (--end;
24591 end > glyph
24592 && !INTEGERP (end->object)
24593 && !EQ (end->object, display_string)
24594 && !(BUFFERP (end->object)
24595 && (end->charpos >= start_charpos
24596 && end->charpos < end_charpos));
24597 --end)
24598 {
24599 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24600 are present at buffer positions between START_CHARPOS and
24601 END_CHARPOS, or if they come from an overlay. */
24602 if (EQ (end->object, before_string))
24603 {
24604 pos = string_buffer_position (w, before_string, start_charpos);
24605 if (!pos || (pos >= start_charpos && pos < end_charpos))
24606 break;
24607 }
24608 else if (EQ (end->object, after_string))
24609 {
24610 pos = string_buffer_position (w, after_string, end_charpos);
24611 if (!pos || (pos >= start_charpos && pos < end_charpos))
24612 break;
24613 }
24614 }
24615 /* Find the X coordinate of the last glyph to be highlighted. */
24616 for (; glyph <= end; ++glyph)
24617 x += glyph->pixel_width;
24618
24619 hlinfo->mouse_face_end_x = x;
24620 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
24621 }
24622 else
24623 {
24624 /* Skip truncation and continuation glyphs near the end of the
24625 row, and also blanks and stretch glyphs inserted by
24626 extend_face_to_end_of_line. */
24627 x = r2->x;
24628 end++;
24629 while (end < glyph
24630 && INTEGERP (end->object)
24631 && end->charpos <= 0)
24632 {
24633 x += end->pixel_width;
24634 ++end;
24635 }
24636 /* Scan the rest of the glyph row from the end, looking for the
24637 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24638 DISPLAY_STRING, or whose position is between START_CHARPOS
24639 and END_CHARPOS */
24640 for ( ;
24641 end < glyph
24642 && !INTEGERP (end->object)
24643 && !EQ (end->object, display_string)
24644 && !(BUFFERP (end->object)
24645 && (end->charpos >= start_charpos
24646 && end->charpos < end_charpos));
24647 ++end)
24648 {
24649 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24650 are present at buffer positions between START_CHARPOS and
24651 END_CHARPOS, or if they come from an overlay. */
24652 if (EQ (end->object, before_string))
24653 {
24654 pos = string_buffer_position (w, before_string, start_charpos);
24655 if (!pos || (pos >= start_charpos && pos < end_charpos))
24656 break;
24657 }
24658 else if (EQ (end->object, after_string))
24659 {
24660 pos = string_buffer_position (w, after_string, end_charpos);
24661 if (!pos || (pos >= start_charpos && pos < end_charpos))
24662 break;
24663 }
24664 x += end->pixel_width;
24665 }
24666 hlinfo->mouse_face_end_x = x;
24667 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
24668 }
24669
24670 hlinfo->mouse_face_window = window;
24671 hlinfo->mouse_face_face_id
24672 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
24673 mouse_charpos + 1,
24674 !hlinfo->mouse_face_hidden, -1);
24675 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
24676 }
24677
24678 /* The following function is not used anymore (replaced with
24679 mouse_face_from_string_pos), but I leave it here for the time
24680 being, in case someone would. */
24681
24682 #if 0 /* not used */
24683
24684 /* Find the position of the glyph for position POS in OBJECT in
24685 window W's current matrix, and return in *X, *Y the pixel
24686 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
24687
24688 RIGHT_P non-zero means return the position of the right edge of the
24689 glyph, RIGHT_P zero means return the left edge position.
24690
24691 If no glyph for POS exists in the matrix, return the position of
24692 the glyph with the next smaller position that is in the matrix, if
24693 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
24694 exists in the matrix, return the position of the glyph with the
24695 next larger position in OBJECT.
24696
24697 Value is non-zero if a glyph was found. */
24698
24699 static int
24700 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
24701 int *hpos, int *vpos, int *x, int *y, int right_p)
24702 {
24703 int yb = window_text_bottom_y (w);
24704 struct glyph_row *r;
24705 struct glyph *best_glyph = NULL;
24706 struct glyph_row *best_row = NULL;
24707 int best_x = 0;
24708
24709 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24710 r->enabled_p && r->y < yb;
24711 ++r)
24712 {
24713 struct glyph *g = r->glyphs[TEXT_AREA];
24714 struct glyph *e = g + r->used[TEXT_AREA];
24715 int gx;
24716
24717 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24718 if (EQ (g->object, object))
24719 {
24720 if (g->charpos == pos)
24721 {
24722 best_glyph = g;
24723 best_x = gx;
24724 best_row = r;
24725 goto found;
24726 }
24727 else if (best_glyph == NULL
24728 || ((eabs (g->charpos - pos)
24729 < eabs (best_glyph->charpos - pos))
24730 && (right_p
24731 ? g->charpos < pos
24732 : g->charpos > pos)))
24733 {
24734 best_glyph = g;
24735 best_x = gx;
24736 best_row = r;
24737 }
24738 }
24739 }
24740
24741 found:
24742
24743 if (best_glyph)
24744 {
24745 *x = best_x;
24746 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
24747
24748 if (right_p)
24749 {
24750 *x += best_glyph->pixel_width;
24751 ++*hpos;
24752 }
24753
24754 *y = best_row->y;
24755 *vpos = best_row - w->current_matrix->rows;
24756 }
24757
24758 return best_glyph != NULL;
24759 }
24760 #endif /* not used */
24761
24762 /* Find the positions of the first and the last glyphs in window W's
24763 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
24764 (assumed to be a string), and return in HLINFO's mouse_face_*
24765 members the pixel and column/row coordinates of those glyphs. */
24766
24767 static void
24768 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
24769 Lisp_Object object,
24770 EMACS_INT startpos, EMACS_INT endpos)
24771 {
24772 int yb = window_text_bottom_y (w);
24773 struct glyph_row *r;
24774 struct glyph *g, *e;
24775 int gx;
24776 int found = 0;
24777
24778 /* Find the glyph row with at least one position in the range
24779 [STARTPOS..ENDPOS], and the first glyph in that row whose
24780 position belongs to that range. */
24781 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24782 r->enabled_p && r->y < yb;
24783 ++r)
24784 {
24785 if (!r->reversed_p)
24786 {
24787 g = r->glyphs[TEXT_AREA];
24788 e = g + r->used[TEXT_AREA];
24789 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24790 if (EQ (g->object, object)
24791 && startpos <= g->charpos && g->charpos <= endpos)
24792 {
24793 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24794 hlinfo->mouse_face_beg_y = r->y;
24795 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24796 hlinfo->mouse_face_beg_x = gx;
24797 found = 1;
24798 break;
24799 }
24800 }
24801 else
24802 {
24803 struct glyph *g1;
24804
24805 e = r->glyphs[TEXT_AREA];
24806 g = e + r->used[TEXT_AREA];
24807 for ( ; g > e; --g)
24808 if (EQ ((g-1)->object, object)
24809 && startpos <= (g-1)->charpos && (g-1)->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 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
24815 gx += g1->pixel_width;
24816 hlinfo->mouse_face_beg_x = gx;
24817 found = 1;
24818 break;
24819 }
24820 }
24821 if (found)
24822 break;
24823 }
24824
24825 if (!found)
24826 return;
24827
24828 /* Starting with the next row, look for the first row which does NOT
24829 include any glyphs whose positions are in the range. */
24830 for (++r; r->enabled_p && r->y < yb; ++r)
24831 {
24832 g = r->glyphs[TEXT_AREA];
24833 e = g + r->used[TEXT_AREA];
24834 found = 0;
24835 for ( ; g < e; ++g)
24836 if (EQ (g->object, object)
24837 && startpos <= g->charpos && g->charpos <= endpos)
24838 {
24839 found = 1;
24840 break;
24841 }
24842 if (!found)
24843 break;
24844 }
24845
24846 /* The highlighted region ends on the previous row. */
24847 r--;
24848
24849 /* Set the end row and its vertical pixel coordinate. */
24850 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
24851 hlinfo->mouse_face_end_y = r->y;
24852
24853 /* Compute and set the end column and the end column's horizontal
24854 pixel coordinate. */
24855 if (!r->reversed_p)
24856 {
24857 g = r->glyphs[TEXT_AREA];
24858 e = g + r->used[TEXT_AREA];
24859 for ( ; e > g; --e)
24860 if (EQ ((e-1)->object, object)
24861 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
24862 break;
24863 hlinfo->mouse_face_end_col = e - g;
24864
24865 for (gx = r->x; g < e; ++g)
24866 gx += g->pixel_width;
24867 hlinfo->mouse_face_end_x = gx;
24868 }
24869 else
24870 {
24871 e = r->glyphs[TEXT_AREA];
24872 g = e + r->used[TEXT_AREA];
24873 for (gx = r->x ; e < g; ++e)
24874 {
24875 if (EQ (e->object, object)
24876 && startpos <= e->charpos && e->charpos <= endpos)
24877 break;
24878 gx += e->pixel_width;
24879 }
24880 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
24881 hlinfo->mouse_face_end_x = gx;
24882 }
24883 }
24884
24885 #ifdef HAVE_WINDOW_SYSTEM
24886
24887 /* See if position X, Y is within a hot-spot of an image. */
24888
24889 static int
24890 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
24891 {
24892 if (!CONSP (hot_spot))
24893 return 0;
24894
24895 if (EQ (XCAR (hot_spot), Qrect))
24896 {
24897 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24898 Lisp_Object rect = XCDR (hot_spot);
24899 Lisp_Object tem;
24900 if (!CONSP (rect))
24901 return 0;
24902 if (!CONSP (XCAR (rect)))
24903 return 0;
24904 if (!CONSP (XCDR (rect)))
24905 return 0;
24906 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24907 return 0;
24908 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24909 return 0;
24910 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24911 return 0;
24912 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24913 return 0;
24914 return 1;
24915 }
24916 else if (EQ (XCAR (hot_spot), Qcircle))
24917 {
24918 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24919 Lisp_Object circ = XCDR (hot_spot);
24920 Lisp_Object lr, lx0, ly0;
24921 if (CONSP (circ)
24922 && CONSP (XCAR (circ))
24923 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24924 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24925 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24926 {
24927 double r = XFLOATINT (lr);
24928 double dx = XINT (lx0) - x;
24929 double dy = XINT (ly0) - y;
24930 return (dx * dx + dy * dy <= r * r);
24931 }
24932 }
24933 else if (EQ (XCAR (hot_spot), Qpoly))
24934 {
24935 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24936 if (VECTORP (XCDR (hot_spot)))
24937 {
24938 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24939 Lisp_Object *poly = v->contents;
24940 int n = v->size;
24941 int i;
24942 int inside = 0;
24943 Lisp_Object lx, ly;
24944 int x0, y0;
24945
24946 /* Need an even number of coordinates, and at least 3 edges. */
24947 if (n < 6 || n & 1)
24948 return 0;
24949
24950 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24951 If count is odd, we are inside polygon. Pixels on edges
24952 may or may not be included depending on actual geometry of the
24953 polygon. */
24954 if ((lx = poly[n-2], !INTEGERP (lx))
24955 || (ly = poly[n-1], !INTEGERP (lx)))
24956 return 0;
24957 x0 = XINT (lx), y0 = XINT (ly);
24958 for (i = 0; i < n; i += 2)
24959 {
24960 int x1 = x0, y1 = y0;
24961 if ((lx = poly[i], !INTEGERP (lx))
24962 || (ly = poly[i+1], !INTEGERP (ly)))
24963 return 0;
24964 x0 = XINT (lx), y0 = XINT (ly);
24965
24966 /* Does this segment cross the X line? */
24967 if (x0 >= x)
24968 {
24969 if (x1 >= x)
24970 continue;
24971 }
24972 else if (x1 < x)
24973 continue;
24974 if (y > y0 && y > y1)
24975 continue;
24976 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24977 inside = !inside;
24978 }
24979 return inside;
24980 }
24981 }
24982 return 0;
24983 }
24984
24985 Lisp_Object
24986 find_hot_spot (Lisp_Object map, int x, int y)
24987 {
24988 while (CONSP (map))
24989 {
24990 if (CONSP (XCAR (map))
24991 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
24992 return XCAR (map);
24993 map = XCDR (map);
24994 }
24995
24996 return Qnil;
24997 }
24998
24999 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25000 3, 3, 0,
25001 doc: /* Lookup in image map MAP coordinates X and Y.
25002 An image map is an alist where each element has the format (AREA ID PLIST).
25003 An AREA is specified as either a rectangle, a circle, or a polygon:
25004 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25005 pixel coordinates of the upper left and bottom right corners.
25006 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25007 and the radius of the circle; r may be a float or integer.
25008 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25009 vector describes one corner in the polygon.
25010 Returns the alist element for the first matching AREA in MAP. */)
25011 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25012 {
25013 if (NILP (map))
25014 return Qnil;
25015
25016 CHECK_NUMBER (x);
25017 CHECK_NUMBER (y);
25018
25019 return find_hot_spot (map, XINT (x), XINT (y));
25020 }
25021
25022
25023 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25024 static void
25025 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25026 {
25027 /* Do not change cursor shape while dragging mouse. */
25028 if (!NILP (do_mouse_tracking))
25029 return;
25030
25031 if (!NILP (pointer))
25032 {
25033 if (EQ (pointer, Qarrow))
25034 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25035 else if (EQ (pointer, Qhand))
25036 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25037 else if (EQ (pointer, Qtext))
25038 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25039 else if (EQ (pointer, intern ("hdrag")))
25040 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25041 #ifdef HAVE_X_WINDOWS
25042 else if (EQ (pointer, intern ("vdrag")))
25043 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25044 #endif
25045 else if (EQ (pointer, intern ("hourglass")))
25046 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25047 else if (EQ (pointer, Qmodeline))
25048 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25049 else
25050 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25051 }
25052
25053 if (cursor != No_Cursor)
25054 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25055 }
25056
25057 #endif /* HAVE_WINDOW_SYSTEM */
25058
25059 /* Take proper action when mouse has moved to the mode or header line
25060 or marginal area AREA of window W, x-position X and y-position Y.
25061 X is relative to the start of the text display area of W, so the
25062 width of bitmap areas and scroll bars must be subtracted to get a
25063 position relative to the start of the mode line. */
25064
25065 static void
25066 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25067 enum window_part area)
25068 {
25069 struct window *w = XWINDOW (window);
25070 struct frame *f = XFRAME (w->frame);
25071 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25072 #ifdef HAVE_WINDOW_SYSTEM
25073 Display_Info *dpyinfo;
25074 #endif
25075 Cursor cursor = No_Cursor;
25076 Lisp_Object pointer = Qnil;
25077 int dx, dy, width, height;
25078 EMACS_INT charpos;
25079 Lisp_Object string, object = Qnil;
25080 Lisp_Object pos, help;
25081
25082 Lisp_Object mouse_face;
25083 int original_x_pixel = x;
25084 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25085 struct glyph_row *row;
25086
25087 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25088 {
25089 int x0;
25090 struct glyph *end;
25091
25092 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25093 returns them in row/column units! */
25094 string = mode_line_string (w, area, &x, &y, &charpos,
25095 &object, &dx, &dy, &width, &height);
25096
25097 row = (area == ON_MODE_LINE
25098 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25099 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25100
25101 /* Find the glyph under the mouse pointer. */
25102 if (row->mode_line_p && row->enabled_p)
25103 {
25104 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25105 end = glyph + row->used[TEXT_AREA];
25106
25107 for (x0 = original_x_pixel;
25108 glyph < end && x0 >= glyph->pixel_width;
25109 ++glyph)
25110 x0 -= glyph->pixel_width;
25111
25112 if (glyph >= end)
25113 glyph = NULL;
25114 }
25115 }
25116 else
25117 {
25118 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25119 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25120 returns them in row/column units! */
25121 string = marginal_area_string (w, area, &x, &y, &charpos,
25122 &object, &dx, &dy, &width, &height);
25123 }
25124
25125 help = Qnil;
25126
25127 #ifdef HAVE_WINDOW_SYSTEM
25128 if (IMAGEP (object))
25129 {
25130 Lisp_Object image_map, hotspot;
25131 if ((image_map = Fplist_get (XCDR (object), QCmap),
25132 !NILP (image_map))
25133 && (hotspot = find_hot_spot (image_map, dx, dy),
25134 CONSP (hotspot))
25135 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25136 {
25137 Lisp_Object area_id, plist;
25138
25139 area_id = XCAR (hotspot);
25140 /* Could check AREA_ID to see if we enter/leave this hot-spot.
25141 If so, we could look for mouse-enter, mouse-leave
25142 properties in PLIST (and do something...). */
25143 hotspot = XCDR (hotspot);
25144 if (CONSP (hotspot)
25145 && (plist = XCAR (hotspot), CONSP (plist)))
25146 {
25147 pointer = Fplist_get (plist, Qpointer);
25148 if (NILP (pointer))
25149 pointer = Qhand;
25150 help = Fplist_get (plist, Qhelp_echo);
25151 if (!NILP (help))
25152 {
25153 help_echo_string = help;
25154 /* Is this correct? ++kfs */
25155 XSETWINDOW (help_echo_window, w);
25156 help_echo_object = w->buffer;
25157 help_echo_pos = charpos;
25158 }
25159 }
25160 }
25161 if (NILP (pointer))
25162 pointer = Fplist_get (XCDR (object), QCpointer);
25163 }
25164 #endif /* HAVE_WINDOW_SYSTEM */
25165
25166 if (STRINGP (string))
25167 {
25168 pos = make_number (charpos);
25169 /* If we're on a string with `help-echo' text property, arrange
25170 for the help to be displayed. This is done by setting the
25171 global variable help_echo_string to the help string. */
25172 if (NILP (help))
25173 {
25174 help = Fget_text_property (pos, Qhelp_echo, string);
25175 if (!NILP (help))
25176 {
25177 help_echo_string = help;
25178 XSETWINDOW (help_echo_window, w);
25179 help_echo_object = string;
25180 help_echo_pos = charpos;
25181 }
25182 }
25183
25184 #ifdef HAVE_WINDOW_SYSTEM
25185 if (FRAME_WINDOW_P (f))
25186 {
25187 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25188 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25189 if (NILP (pointer))
25190 pointer = Fget_text_property (pos, Qpointer, string);
25191
25192 /* Change the mouse pointer according to what is under X/Y. */
25193 if (NILP (pointer)
25194 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25195 {
25196 Lisp_Object map;
25197 map = Fget_text_property (pos, Qlocal_map, string);
25198 if (!KEYMAPP (map))
25199 map = Fget_text_property (pos, Qkeymap, string);
25200 if (!KEYMAPP (map))
25201 cursor = dpyinfo->vertical_scroll_bar_cursor;
25202 }
25203 }
25204 #endif
25205
25206 /* Change the mouse face according to what is under X/Y. */
25207 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25208 if (!NILP (mouse_face)
25209 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25210 && glyph)
25211 {
25212 Lisp_Object b, e;
25213
25214 struct glyph * tmp_glyph;
25215
25216 int gpos;
25217 int gseq_length;
25218 int total_pixel_width;
25219 EMACS_INT begpos, endpos, ignore;
25220
25221 int vpos, hpos;
25222
25223 b = Fprevious_single_property_change (make_number (charpos + 1),
25224 Qmouse_face, string, Qnil);
25225 if (NILP (b))
25226 begpos = 0;
25227 else
25228 begpos = XINT (b);
25229
25230 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25231 if (NILP (e))
25232 endpos = SCHARS (string);
25233 else
25234 endpos = XINT (e);
25235
25236 /* Calculate the glyph position GPOS of GLYPH in the
25237 displayed string, relative to the beginning of the
25238 highlighted part of the string.
25239
25240 Note: GPOS is different from CHARPOS. CHARPOS is the
25241 position of GLYPH in the internal string object. A mode
25242 line string format has structures which are converted to
25243 a flattened string by the Emacs Lisp interpreter. The
25244 internal string is an element of those structures. The
25245 displayed string is the flattened string. */
25246 tmp_glyph = row_start_glyph;
25247 while (tmp_glyph < glyph
25248 && (!(EQ (tmp_glyph->object, glyph->object)
25249 && begpos <= tmp_glyph->charpos
25250 && tmp_glyph->charpos < endpos)))
25251 tmp_glyph++;
25252 gpos = glyph - tmp_glyph;
25253
25254 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25255 the highlighted part of the displayed string to which
25256 GLYPH belongs. Note: GSEQ_LENGTH is different from
25257 SCHARS (STRING), because the latter returns the length of
25258 the internal string. */
25259 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25260 tmp_glyph > glyph
25261 && (!(EQ (tmp_glyph->object, glyph->object)
25262 && begpos <= tmp_glyph->charpos
25263 && tmp_glyph->charpos < endpos));
25264 tmp_glyph--)
25265 ;
25266 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25267
25268 /* Calculate the total pixel width of all the glyphs between
25269 the beginning of the highlighted area and GLYPH. */
25270 total_pixel_width = 0;
25271 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25272 total_pixel_width += tmp_glyph->pixel_width;
25273
25274 /* Pre calculation of re-rendering position. Note: X is in
25275 column units here, after the call to mode_line_string or
25276 marginal_area_string. */
25277 hpos = x - gpos;
25278 vpos = (area == ON_MODE_LINE
25279 ? (w->current_matrix)->nrows - 1
25280 : 0);
25281
25282 /* If GLYPH's position is included in the region that is
25283 already drawn in mouse face, we have nothing to do. */
25284 if ( EQ (window, hlinfo->mouse_face_window)
25285 && (!row->reversed_p
25286 ? (hlinfo->mouse_face_beg_col <= hpos
25287 && hpos < hlinfo->mouse_face_end_col)
25288 /* In R2L rows we swap BEG and END, see below. */
25289 : (hlinfo->mouse_face_end_col <= hpos
25290 && hpos < hlinfo->mouse_face_beg_col))
25291 && hlinfo->mouse_face_beg_row == vpos )
25292 return;
25293
25294 if (clear_mouse_face (hlinfo))
25295 cursor = No_Cursor;
25296
25297 if (!row->reversed_p)
25298 {
25299 hlinfo->mouse_face_beg_col = hpos;
25300 hlinfo->mouse_face_beg_x = original_x_pixel
25301 - (total_pixel_width + dx);
25302 hlinfo->mouse_face_end_col = hpos + gseq_length;
25303 hlinfo->mouse_face_end_x = 0;
25304 }
25305 else
25306 {
25307 /* In R2L rows, show_mouse_face expects BEG and END
25308 coordinates to be swapped. */
25309 hlinfo->mouse_face_end_col = hpos;
25310 hlinfo->mouse_face_end_x = original_x_pixel
25311 - (total_pixel_width + dx);
25312 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25313 hlinfo->mouse_face_beg_x = 0;
25314 }
25315
25316 hlinfo->mouse_face_beg_row = vpos;
25317 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25318 hlinfo->mouse_face_beg_y = 0;
25319 hlinfo->mouse_face_end_y = 0;
25320 hlinfo->mouse_face_past_end = 0;
25321 hlinfo->mouse_face_window = window;
25322
25323 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25324 charpos,
25325 0, 0, 0,
25326 &ignore,
25327 glyph->face_id,
25328 1);
25329 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25330
25331 if (NILP (pointer))
25332 pointer = Qhand;
25333 }
25334 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25335 clear_mouse_face (hlinfo);
25336 }
25337 #ifdef HAVE_WINDOW_SYSTEM
25338 if (FRAME_WINDOW_P (f))
25339 define_frame_cursor1 (f, cursor, pointer);
25340 #endif
25341 }
25342
25343
25344 /* EXPORT:
25345 Take proper action when the mouse has moved to position X, Y on
25346 frame F as regards highlighting characters that have mouse-face
25347 properties. Also de-highlighting chars where the mouse was before.
25348 X and Y can be negative or out of range. */
25349
25350 void
25351 note_mouse_highlight (struct frame *f, int x, int y)
25352 {
25353 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25354 enum window_part part;
25355 Lisp_Object window;
25356 struct window *w;
25357 Cursor cursor = No_Cursor;
25358 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25359 struct buffer *b;
25360
25361 /* When a menu is active, don't highlight because this looks odd. */
25362 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25363 if (popup_activated ())
25364 return;
25365 #endif
25366
25367 if (NILP (Vmouse_highlight)
25368 || !f->glyphs_initialized_p
25369 || f->pointer_invisible)
25370 return;
25371
25372 hlinfo->mouse_face_mouse_x = x;
25373 hlinfo->mouse_face_mouse_y = y;
25374 hlinfo->mouse_face_mouse_frame = f;
25375
25376 if (hlinfo->mouse_face_defer)
25377 return;
25378
25379 if (gc_in_progress)
25380 {
25381 hlinfo->mouse_face_deferred_gc = 1;
25382 return;
25383 }
25384
25385 /* Which window is that in? */
25386 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
25387
25388 /* If we were displaying active text in another window, clear that.
25389 Also clear if we move out of text area in same window. */
25390 if (! EQ (window, hlinfo->mouse_face_window)
25391 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
25392 && !NILP (hlinfo->mouse_face_window)))
25393 clear_mouse_face (hlinfo);
25394
25395 /* Not on a window -> return. */
25396 if (!WINDOWP (window))
25397 return;
25398
25399 /* Reset help_echo_string. It will get recomputed below. */
25400 help_echo_string = Qnil;
25401
25402 /* Convert to window-relative pixel coordinates. */
25403 w = XWINDOW (window);
25404 frame_to_window_pixel_xy (w, &x, &y);
25405
25406 #ifdef HAVE_WINDOW_SYSTEM
25407 /* Handle tool-bar window differently since it doesn't display a
25408 buffer. */
25409 if (EQ (window, f->tool_bar_window))
25410 {
25411 note_tool_bar_highlight (f, x, y);
25412 return;
25413 }
25414 #endif
25415
25416 /* Mouse is on the mode, header line or margin? */
25417 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
25418 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
25419 {
25420 note_mode_line_or_margin_highlight (window, x, y, part);
25421 return;
25422 }
25423
25424 #ifdef HAVE_WINDOW_SYSTEM
25425 if (part == ON_VERTICAL_BORDER)
25426 {
25427 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25428 help_echo_string = build_string ("drag-mouse-1: resize");
25429 }
25430 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
25431 || part == ON_SCROLL_BAR)
25432 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25433 else
25434 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25435 #endif
25436
25437 /* Are we in a window whose display is up to date?
25438 And verify the buffer's text has not changed. */
25439 b = XBUFFER (w->buffer);
25440 if (part == ON_TEXT
25441 && EQ (w->window_end_valid, w->buffer)
25442 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
25443 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
25444 {
25445 int hpos, vpos, i, dx, dy, area;
25446 EMACS_INT pos;
25447 struct glyph *glyph;
25448 Lisp_Object object;
25449 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
25450 Lisp_Object *overlay_vec = NULL;
25451 int noverlays;
25452 struct buffer *obuf;
25453 EMACS_INT obegv, ozv;
25454 int same_region;
25455
25456 /* Find the glyph under X/Y. */
25457 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
25458
25459 #ifdef HAVE_WINDOW_SYSTEM
25460 /* Look for :pointer property on image. */
25461 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25462 {
25463 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25464 if (img != NULL && IMAGEP (img->spec))
25465 {
25466 Lisp_Object image_map, hotspot;
25467 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
25468 !NILP (image_map))
25469 && (hotspot = find_hot_spot (image_map,
25470 glyph->slice.img.x + dx,
25471 glyph->slice.img.y + dy),
25472 CONSP (hotspot))
25473 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25474 {
25475 Lisp_Object area_id, plist;
25476
25477 area_id = XCAR (hotspot);
25478 /* Could check AREA_ID to see if we enter/leave this hot-spot.
25479 If so, we could look for mouse-enter, mouse-leave
25480 properties in PLIST (and do something...). */
25481 hotspot = XCDR (hotspot);
25482 if (CONSP (hotspot)
25483 && (plist = XCAR (hotspot), CONSP (plist)))
25484 {
25485 pointer = Fplist_get (plist, Qpointer);
25486 if (NILP (pointer))
25487 pointer = Qhand;
25488 help_echo_string = Fplist_get (plist, Qhelp_echo);
25489 if (!NILP (help_echo_string))
25490 {
25491 help_echo_window = window;
25492 help_echo_object = glyph->object;
25493 help_echo_pos = glyph->charpos;
25494 }
25495 }
25496 }
25497 if (NILP (pointer))
25498 pointer = Fplist_get (XCDR (img->spec), QCpointer);
25499 }
25500 }
25501 #endif /* HAVE_WINDOW_SYSTEM */
25502
25503 /* Clear mouse face if X/Y not over text. */
25504 if (glyph == NULL
25505 || area != TEXT_AREA
25506 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
25507 /* Glyph's OBJECT is an integer for glyphs inserted by the
25508 display engine for its internal purposes, like truncation
25509 and continuation glyphs and blanks beyond the end of
25510 line's text on text terminals. If we are over such a
25511 glyph, we are not over any text. */
25512 || INTEGERP (glyph->object)
25513 /* R2L rows have a stretch glyph at their front, which
25514 stands for no text, whereas L2R rows have no glyphs at
25515 all beyond the end of text. Treat such stretch glyphs
25516 like we do with NULL glyphs in L2R rows. */
25517 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
25518 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
25519 && glyph->type == STRETCH_GLYPH
25520 && glyph->avoid_cursor_p))
25521 {
25522 if (clear_mouse_face (hlinfo))
25523 cursor = No_Cursor;
25524 #ifdef HAVE_WINDOW_SYSTEM
25525 if (FRAME_WINDOW_P (f) && NILP (pointer))
25526 {
25527 if (area != TEXT_AREA)
25528 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25529 else
25530 pointer = Vvoid_text_area_pointer;
25531 }
25532 #endif
25533 goto set_cursor;
25534 }
25535
25536 pos = glyph->charpos;
25537 object = glyph->object;
25538 if (!STRINGP (object) && !BUFFERP (object))
25539 goto set_cursor;
25540
25541 /* If we get an out-of-range value, return now; avoid an error. */
25542 if (BUFFERP (object) && pos > BUF_Z (b))
25543 goto set_cursor;
25544
25545 /* Make the window's buffer temporarily current for
25546 overlays_at and compute_char_face. */
25547 obuf = current_buffer;
25548 current_buffer = b;
25549 obegv = BEGV;
25550 ozv = ZV;
25551 BEGV = BEG;
25552 ZV = Z;
25553
25554 /* Is this char mouse-active or does it have help-echo? */
25555 position = make_number (pos);
25556
25557 if (BUFFERP (object))
25558 {
25559 /* Put all the overlays we want in a vector in overlay_vec. */
25560 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
25561 /* Sort overlays into increasing priority order. */
25562 noverlays = sort_overlays (overlay_vec, noverlays, w);
25563 }
25564 else
25565 noverlays = 0;
25566
25567 same_region = coords_in_mouse_face_p (w, hpos, vpos);
25568
25569 if (same_region)
25570 cursor = No_Cursor;
25571
25572 /* Check mouse-face highlighting. */
25573 if (! same_region
25574 /* If there exists an overlay with mouse-face overlapping
25575 the one we are currently highlighting, we have to
25576 check if we enter the overlapping overlay, and then
25577 highlight only that. */
25578 || (OVERLAYP (hlinfo->mouse_face_overlay)
25579 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
25580 {
25581 /* Find the highest priority overlay with a mouse-face. */
25582 overlay = Qnil;
25583 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
25584 {
25585 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
25586 if (!NILP (mouse_face))
25587 overlay = overlay_vec[i];
25588 }
25589
25590 /* If we're highlighting the same overlay as before, there's
25591 no need to do that again. */
25592 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
25593 goto check_help_echo;
25594 hlinfo->mouse_face_overlay = overlay;
25595
25596 /* Clear the display of the old active region, if any. */
25597 if (clear_mouse_face (hlinfo))
25598 cursor = No_Cursor;
25599
25600 /* If no overlay applies, get a text property. */
25601 if (NILP (overlay))
25602 mouse_face = Fget_text_property (position, Qmouse_face, object);
25603
25604 /* Next, compute the bounds of the mouse highlighting and
25605 display it. */
25606 if (!NILP (mouse_face) && STRINGP (object))
25607 {
25608 /* The mouse-highlighting comes from a display string
25609 with a mouse-face. */
25610 Lisp_Object b, e;
25611 EMACS_INT ignore;
25612
25613 b = Fprevious_single_property_change
25614 (make_number (pos + 1), Qmouse_face, object, Qnil);
25615 e = Fnext_single_property_change
25616 (position, Qmouse_face, object, Qnil);
25617 if (NILP (b))
25618 b = make_number (0);
25619 if (NILP (e))
25620 e = make_number (SCHARS (object) - 1);
25621 mouse_face_from_string_pos (w, hlinfo, object,
25622 XINT (b), XINT (e));
25623 hlinfo->mouse_face_past_end = 0;
25624 hlinfo->mouse_face_window = window;
25625 hlinfo->mouse_face_face_id
25626 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
25627 glyph->face_id, 1);
25628 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25629 cursor = No_Cursor;
25630 }
25631 else
25632 {
25633 /* The mouse-highlighting, if any, comes from an overlay
25634 or text property in the buffer. */
25635 Lisp_Object buffer, display_string;
25636
25637 if (STRINGP (object))
25638 {
25639 /* If we are on a display string with no mouse-face,
25640 check if the text under it has one. */
25641 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
25642 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25643 pos = string_buffer_position (w, object, start);
25644 if (pos > 0)
25645 {
25646 mouse_face = get_char_property_and_overlay
25647 (make_number (pos), Qmouse_face, w->buffer, &overlay);
25648 buffer = w->buffer;
25649 display_string = object;
25650 }
25651 }
25652 else
25653 {
25654 buffer = object;
25655 display_string = Qnil;
25656 }
25657
25658 if (!NILP (mouse_face))
25659 {
25660 Lisp_Object before, after;
25661 Lisp_Object before_string, after_string;
25662 /* To correctly find the limits of mouse highlight
25663 in a bidi-reordered buffer, we must not use the
25664 optimization of limiting the search in
25665 previous-single-property-change and
25666 next-single-property-change, because
25667 rows_from_pos_range needs the real start and end
25668 positions to DTRT in this case. That's because
25669 the first row visible in a window does not
25670 necessarily display the character whose position
25671 is the smallest. */
25672 Lisp_Object lim1 =
25673 NILP (XBUFFER (buffer)->bidi_display_reordering)
25674 ? Fmarker_position (w->start)
25675 : Qnil;
25676 Lisp_Object lim2 =
25677 NILP (XBUFFER (buffer)->bidi_display_reordering)
25678 ? make_number (BUF_Z (XBUFFER (buffer))
25679 - XFASTINT (w->window_end_pos))
25680 : Qnil;
25681
25682 if (NILP (overlay))
25683 {
25684 /* Handle the text property case. */
25685 before = Fprevious_single_property_change
25686 (make_number (pos + 1), Qmouse_face, buffer, lim1);
25687 after = Fnext_single_property_change
25688 (make_number (pos), Qmouse_face, buffer, lim2);
25689 before_string = after_string = Qnil;
25690 }
25691 else
25692 {
25693 /* Handle the overlay case. */
25694 before = Foverlay_start (overlay);
25695 after = Foverlay_end (overlay);
25696 before_string = Foverlay_get (overlay, Qbefore_string);
25697 after_string = Foverlay_get (overlay, Qafter_string);
25698
25699 if (!STRINGP (before_string)) before_string = Qnil;
25700 if (!STRINGP (after_string)) after_string = Qnil;
25701 }
25702
25703 mouse_face_from_buffer_pos (window, hlinfo, pos,
25704 XFASTINT (before),
25705 XFASTINT (after),
25706 before_string, after_string,
25707 display_string);
25708 cursor = No_Cursor;
25709 }
25710 }
25711 }
25712
25713 check_help_echo:
25714
25715 /* Look for a `help-echo' property. */
25716 if (NILP (help_echo_string)) {
25717 Lisp_Object help, overlay;
25718
25719 /* Check overlays first. */
25720 help = overlay = Qnil;
25721 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
25722 {
25723 overlay = overlay_vec[i];
25724 help = Foverlay_get (overlay, Qhelp_echo);
25725 }
25726
25727 if (!NILP (help))
25728 {
25729 help_echo_string = help;
25730 help_echo_window = window;
25731 help_echo_object = overlay;
25732 help_echo_pos = pos;
25733 }
25734 else
25735 {
25736 Lisp_Object object = glyph->object;
25737 EMACS_INT charpos = glyph->charpos;
25738
25739 /* Try text properties. */
25740 if (STRINGP (object)
25741 && charpos >= 0
25742 && charpos < SCHARS (object))
25743 {
25744 help = Fget_text_property (make_number (charpos),
25745 Qhelp_echo, object);
25746 if (NILP (help))
25747 {
25748 /* If the string itself doesn't specify a help-echo,
25749 see if the buffer text ``under'' it does. */
25750 struct glyph_row *r
25751 = MATRIX_ROW (w->current_matrix, vpos);
25752 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25753 EMACS_INT pos = string_buffer_position (w, object, start);
25754 if (pos > 0)
25755 {
25756 help = Fget_char_property (make_number (pos),
25757 Qhelp_echo, w->buffer);
25758 if (!NILP (help))
25759 {
25760 charpos = pos;
25761 object = w->buffer;
25762 }
25763 }
25764 }
25765 }
25766 else if (BUFFERP (object)
25767 && charpos >= BEGV
25768 && charpos < ZV)
25769 help = Fget_text_property (make_number (charpos), Qhelp_echo,
25770 object);
25771
25772 if (!NILP (help))
25773 {
25774 help_echo_string = help;
25775 help_echo_window = window;
25776 help_echo_object = object;
25777 help_echo_pos = charpos;
25778 }
25779 }
25780 }
25781
25782 #ifdef HAVE_WINDOW_SYSTEM
25783 /* Look for a `pointer' property. */
25784 if (FRAME_WINDOW_P (f) && NILP (pointer))
25785 {
25786 /* Check overlays first. */
25787 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
25788 pointer = Foverlay_get (overlay_vec[i], Qpointer);
25789
25790 if (NILP (pointer))
25791 {
25792 Lisp_Object object = glyph->object;
25793 EMACS_INT charpos = glyph->charpos;
25794
25795 /* Try text properties. */
25796 if (STRINGP (object)
25797 && charpos >= 0
25798 && charpos < SCHARS (object))
25799 {
25800 pointer = Fget_text_property (make_number (charpos),
25801 Qpointer, object);
25802 if (NILP (pointer))
25803 {
25804 /* If the string itself doesn't specify a pointer,
25805 see if the buffer text ``under'' it does. */
25806 struct glyph_row *r
25807 = MATRIX_ROW (w->current_matrix, vpos);
25808 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25809 EMACS_INT pos = string_buffer_position (w, object,
25810 start);
25811 if (pos > 0)
25812 pointer = Fget_char_property (make_number (pos),
25813 Qpointer, w->buffer);
25814 }
25815 }
25816 else if (BUFFERP (object)
25817 && charpos >= BEGV
25818 && charpos < ZV)
25819 pointer = Fget_text_property (make_number (charpos),
25820 Qpointer, object);
25821 }
25822 }
25823 #endif /* HAVE_WINDOW_SYSTEM */
25824
25825 BEGV = obegv;
25826 ZV = ozv;
25827 current_buffer = obuf;
25828 }
25829
25830 set_cursor:
25831
25832 #ifdef HAVE_WINDOW_SYSTEM
25833 if (FRAME_WINDOW_P (f))
25834 define_frame_cursor1 (f, cursor, pointer);
25835 #else
25836 /* This is here to prevent a compiler error, about "label at end of
25837 compound statement". */
25838 return;
25839 #endif
25840 }
25841
25842
25843 /* EXPORT for RIF:
25844 Clear any mouse-face on window W. This function is part of the
25845 redisplay interface, and is called from try_window_id and similar
25846 functions to ensure the mouse-highlight is off. */
25847
25848 void
25849 x_clear_window_mouse_face (struct window *w)
25850 {
25851 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25852 Lisp_Object window;
25853
25854 BLOCK_INPUT;
25855 XSETWINDOW (window, w);
25856 if (EQ (window, hlinfo->mouse_face_window))
25857 clear_mouse_face (hlinfo);
25858 UNBLOCK_INPUT;
25859 }
25860
25861
25862 /* EXPORT:
25863 Just discard the mouse face information for frame F, if any.
25864 This is used when the size of F is changed. */
25865
25866 void
25867 cancel_mouse_face (struct frame *f)
25868 {
25869 Lisp_Object window;
25870 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25871
25872 window = hlinfo->mouse_face_window;
25873 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
25874 {
25875 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25876 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25877 hlinfo->mouse_face_window = Qnil;
25878 }
25879 }
25880
25881
25882 \f
25883 /***********************************************************************
25884 Exposure Events
25885 ***********************************************************************/
25886
25887 #ifdef HAVE_WINDOW_SYSTEM
25888
25889 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
25890 which intersects rectangle R. R is in window-relative coordinates. */
25891
25892 static void
25893 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
25894 enum glyph_row_area area)
25895 {
25896 struct glyph *first = row->glyphs[area];
25897 struct glyph *end = row->glyphs[area] + row->used[area];
25898 struct glyph *last;
25899 int first_x, start_x, x;
25900
25901 if (area == TEXT_AREA && row->fill_line_p)
25902 /* If row extends face to end of line write the whole line. */
25903 draw_glyphs (w, 0, row, area,
25904 0, row->used[area],
25905 DRAW_NORMAL_TEXT, 0);
25906 else
25907 {
25908 /* Set START_X to the window-relative start position for drawing glyphs of
25909 AREA. The first glyph of the text area can be partially visible.
25910 The first glyphs of other areas cannot. */
25911 start_x = window_box_left_offset (w, area);
25912 x = start_x;
25913 if (area == TEXT_AREA)
25914 x += row->x;
25915
25916 /* Find the first glyph that must be redrawn. */
25917 while (first < end
25918 && x + first->pixel_width < r->x)
25919 {
25920 x += first->pixel_width;
25921 ++first;
25922 }
25923
25924 /* Find the last one. */
25925 last = first;
25926 first_x = x;
25927 while (last < end
25928 && x < r->x + r->width)
25929 {
25930 x += last->pixel_width;
25931 ++last;
25932 }
25933
25934 /* Repaint. */
25935 if (last > first)
25936 draw_glyphs (w, first_x - start_x, row, area,
25937 first - row->glyphs[area], last - row->glyphs[area],
25938 DRAW_NORMAL_TEXT, 0);
25939 }
25940 }
25941
25942
25943 /* Redraw the parts of the glyph row ROW on window W intersecting
25944 rectangle R. R is in window-relative coordinates. Value is
25945 non-zero if mouse-face was overwritten. */
25946
25947 static int
25948 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
25949 {
25950 xassert (row->enabled_p);
25951
25952 if (row->mode_line_p || w->pseudo_window_p)
25953 draw_glyphs (w, 0, row, TEXT_AREA,
25954 0, row->used[TEXT_AREA],
25955 DRAW_NORMAL_TEXT, 0);
25956 else
25957 {
25958 if (row->used[LEFT_MARGIN_AREA])
25959 expose_area (w, row, r, LEFT_MARGIN_AREA);
25960 if (row->used[TEXT_AREA])
25961 expose_area (w, row, r, TEXT_AREA);
25962 if (row->used[RIGHT_MARGIN_AREA])
25963 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25964 draw_row_fringe_bitmaps (w, row);
25965 }
25966
25967 return row->mouse_face_p;
25968 }
25969
25970
25971 /* Redraw those parts of glyphs rows during expose event handling that
25972 overlap other rows. Redrawing of an exposed line writes over parts
25973 of lines overlapping that exposed line; this function fixes that.
25974
25975 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25976 row in W's current matrix that is exposed and overlaps other rows.
25977 LAST_OVERLAPPING_ROW is the last such row. */
25978
25979 static void
25980 expose_overlaps (struct window *w,
25981 struct glyph_row *first_overlapping_row,
25982 struct glyph_row *last_overlapping_row,
25983 XRectangle *r)
25984 {
25985 struct glyph_row *row;
25986
25987 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
25988 if (row->overlapping_p)
25989 {
25990 xassert (row->enabled_p && !row->mode_line_p);
25991
25992 row->clip = r;
25993 if (row->used[LEFT_MARGIN_AREA])
25994 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
25995
25996 if (row->used[TEXT_AREA])
25997 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
25998
25999 if (row->used[RIGHT_MARGIN_AREA])
26000 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26001 row->clip = NULL;
26002 }
26003 }
26004
26005
26006 /* Return non-zero if W's cursor intersects rectangle R. */
26007
26008 static int
26009 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26010 {
26011 XRectangle cr, result;
26012 struct glyph *cursor_glyph;
26013 struct glyph_row *row;
26014
26015 if (w->phys_cursor.vpos >= 0
26016 && w->phys_cursor.vpos < w->current_matrix->nrows
26017 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26018 row->enabled_p)
26019 && row->cursor_in_fringe_p)
26020 {
26021 /* Cursor is in the fringe. */
26022 cr.x = window_box_right_offset (w,
26023 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26024 ? RIGHT_MARGIN_AREA
26025 : TEXT_AREA));
26026 cr.y = row->y;
26027 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26028 cr.height = row->height;
26029 return x_intersect_rectangles (&cr, r, &result);
26030 }
26031
26032 cursor_glyph = get_phys_cursor_glyph (w);
26033 if (cursor_glyph)
26034 {
26035 /* r is relative to W's box, but w->phys_cursor.x is relative
26036 to left edge of W's TEXT area. Adjust it. */
26037 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26038 cr.y = w->phys_cursor.y;
26039 cr.width = cursor_glyph->pixel_width;
26040 cr.height = w->phys_cursor_height;
26041 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26042 I assume the effect is the same -- and this is portable. */
26043 return x_intersect_rectangles (&cr, r, &result);
26044 }
26045 /* If we don't understand the format, pretend we're not in the hot-spot. */
26046 return 0;
26047 }
26048
26049
26050 /* EXPORT:
26051 Draw a vertical window border to the right of window W if W doesn't
26052 have vertical scroll bars. */
26053
26054 void
26055 x_draw_vertical_border (struct window *w)
26056 {
26057 struct frame *f = XFRAME (WINDOW_FRAME (w));
26058
26059 /* We could do better, if we knew what type of scroll-bar the adjacent
26060 windows (on either side) have... But we don't :-(
26061 However, I think this works ok. ++KFS 2003-04-25 */
26062
26063 /* Redraw borders between horizontally adjacent windows. Don't
26064 do it for frames with vertical scroll bars because either the
26065 right scroll bar of a window, or the left scroll bar of its
26066 neighbor will suffice as a border. */
26067 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26068 return;
26069
26070 if (!WINDOW_RIGHTMOST_P (w)
26071 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26072 {
26073 int x0, x1, y0, y1;
26074
26075 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26076 y1 -= 1;
26077
26078 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26079 x1 -= 1;
26080
26081 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26082 }
26083 else if (!WINDOW_LEFTMOST_P (w)
26084 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26085 {
26086 int x0, x1, y0, y1;
26087
26088 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26089 y1 -= 1;
26090
26091 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26092 x0 -= 1;
26093
26094 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26095 }
26096 }
26097
26098
26099 /* Redraw the part of window W intersection rectangle FR. Pixel
26100 coordinates in FR are frame-relative. Call this function with
26101 input blocked. Value is non-zero if the exposure overwrites
26102 mouse-face. */
26103
26104 static int
26105 expose_window (struct window *w, XRectangle *fr)
26106 {
26107 struct frame *f = XFRAME (w->frame);
26108 XRectangle wr, r;
26109 int mouse_face_overwritten_p = 0;
26110
26111 /* If window is not yet fully initialized, do nothing. This can
26112 happen when toolkit scroll bars are used and a window is split.
26113 Reconfiguring the scroll bar will generate an expose for a newly
26114 created window. */
26115 if (w->current_matrix == NULL)
26116 return 0;
26117
26118 /* When we're currently updating the window, display and current
26119 matrix usually don't agree. Arrange for a thorough display
26120 later. */
26121 if (w == updated_window)
26122 {
26123 SET_FRAME_GARBAGED (f);
26124 return 0;
26125 }
26126
26127 /* Frame-relative pixel rectangle of W. */
26128 wr.x = WINDOW_LEFT_EDGE_X (w);
26129 wr.y = WINDOW_TOP_EDGE_Y (w);
26130 wr.width = WINDOW_TOTAL_WIDTH (w);
26131 wr.height = WINDOW_TOTAL_HEIGHT (w);
26132
26133 if (x_intersect_rectangles (fr, &wr, &r))
26134 {
26135 int yb = window_text_bottom_y (w);
26136 struct glyph_row *row;
26137 int cursor_cleared_p;
26138 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26139
26140 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26141 r.x, r.y, r.width, r.height));
26142
26143 /* Convert to window coordinates. */
26144 r.x -= WINDOW_LEFT_EDGE_X (w);
26145 r.y -= WINDOW_TOP_EDGE_Y (w);
26146
26147 /* Turn off the cursor. */
26148 if (!w->pseudo_window_p
26149 && phys_cursor_in_rect_p (w, &r))
26150 {
26151 x_clear_cursor (w);
26152 cursor_cleared_p = 1;
26153 }
26154 else
26155 cursor_cleared_p = 0;
26156
26157 /* Update lines intersecting rectangle R. */
26158 first_overlapping_row = last_overlapping_row = NULL;
26159 for (row = w->current_matrix->rows;
26160 row->enabled_p;
26161 ++row)
26162 {
26163 int y0 = row->y;
26164 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26165
26166 if ((y0 >= r.y && y0 < r.y + r.height)
26167 || (y1 > r.y && y1 < r.y + r.height)
26168 || (r.y >= y0 && r.y < y1)
26169 || (r.y + r.height > y0 && r.y + r.height < y1))
26170 {
26171 /* A header line may be overlapping, but there is no need
26172 to fix overlapping areas for them. KFS 2005-02-12 */
26173 if (row->overlapping_p && !row->mode_line_p)
26174 {
26175 if (first_overlapping_row == NULL)
26176 first_overlapping_row = row;
26177 last_overlapping_row = row;
26178 }
26179
26180 row->clip = fr;
26181 if (expose_line (w, row, &r))
26182 mouse_face_overwritten_p = 1;
26183 row->clip = NULL;
26184 }
26185 else if (row->overlapping_p)
26186 {
26187 /* We must redraw a row overlapping the exposed area. */
26188 if (y0 < r.y
26189 ? y0 + row->phys_height > r.y
26190 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26191 {
26192 if (first_overlapping_row == NULL)
26193 first_overlapping_row = row;
26194 last_overlapping_row = row;
26195 }
26196 }
26197
26198 if (y1 >= yb)
26199 break;
26200 }
26201
26202 /* Display the mode line if there is one. */
26203 if (WINDOW_WANTS_MODELINE_P (w)
26204 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26205 row->enabled_p)
26206 && row->y < r.y + r.height)
26207 {
26208 if (expose_line (w, row, &r))
26209 mouse_face_overwritten_p = 1;
26210 }
26211
26212 if (!w->pseudo_window_p)
26213 {
26214 /* Fix the display of overlapping rows. */
26215 if (first_overlapping_row)
26216 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26217 fr);
26218
26219 /* Draw border between windows. */
26220 x_draw_vertical_border (w);
26221
26222 /* Turn the cursor on again. */
26223 if (cursor_cleared_p)
26224 update_window_cursor (w, 1);
26225 }
26226 }
26227
26228 return mouse_face_overwritten_p;
26229 }
26230
26231
26232
26233 /* Redraw (parts) of all windows in the window tree rooted at W that
26234 intersect R. R contains frame pixel coordinates. Value is
26235 non-zero if the exposure overwrites mouse-face. */
26236
26237 static int
26238 expose_window_tree (struct window *w, XRectangle *r)
26239 {
26240 struct frame *f = XFRAME (w->frame);
26241 int mouse_face_overwritten_p = 0;
26242
26243 while (w && !FRAME_GARBAGED_P (f))
26244 {
26245 if (!NILP (w->hchild))
26246 mouse_face_overwritten_p
26247 |= expose_window_tree (XWINDOW (w->hchild), r);
26248 else if (!NILP (w->vchild))
26249 mouse_face_overwritten_p
26250 |= expose_window_tree (XWINDOW (w->vchild), r);
26251 else
26252 mouse_face_overwritten_p |= expose_window (w, r);
26253
26254 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26255 }
26256
26257 return mouse_face_overwritten_p;
26258 }
26259
26260
26261 /* EXPORT:
26262 Redisplay an exposed area of frame F. X and Y are the upper-left
26263 corner of the exposed rectangle. W and H are width and height of
26264 the exposed area. All are pixel values. W or H zero means redraw
26265 the entire frame. */
26266
26267 void
26268 expose_frame (struct frame *f, int x, int y, int w, int h)
26269 {
26270 XRectangle r;
26271 int mouse_face_overwritten_p = 0;
26272
26273 TRACE ((stderr, "expose_frame "));
26274
26275 /* No need to redraw if frame will be redrawn soon. */
26276 if (FRAME_GARBAGED_P (f))
26277 {
26278 TRACE ((stderr, " garbaged\n"));
26279 return;
26280 }
26281
26282 /* If basic faces haven't been realized yet, there is no point in
26283 trying to redraw anything. This can happen when we get an expose
26284 event while Emacs is starting, e.g. by moving another window. */
26285 if (FRAME_FACE_CACHE (f) == NULL
26286 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26287 {
26288 TRACE ((stderr, " no faces\n"));
26289 return;
26290 }
26291
26292 if (w == 0 || h == 0)
26293 {
26294 r.x = r.y = 0;
26295 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26296 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26297 }
26298 else
26299 {
26300 r.x = x;
26301 r.y = y;
26302 r.width = w;
26303 r.height = h;
26304 }
26305
26306 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26307 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26308
26309 if (WINDOWP (f->tool_bar_window))
26310 mouse_face_overwritten_p
26311 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26312
26313 #ifdef HAVE_X_WINDOWS
26314 #ifndef MSDOS
26315 #ifndef USE_X_TOOLKIT
26316 if (WINDOWP (f->menu_bar_window))
26317 mouse_face_overwritten_p
26318 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26319 #endif /* not USE_X_TOOLKIT */
26320 #endif
26321 #endif
26322
26323 /* Some window managers support a focus-follows-mouse style with
26324 delayed raising of frames. Imagine a partially obscured frame,
26325 and moving the mouse into partially obscured mouse-face on that
26326 frame. The visible part of the mouse-face will be highlighted,
26327 then the WM raises the obscured frame. With at least one WM, KDE
26328 2.1, Emacs is not getting any event for the raising of the frame
26329 (even tried with SubstructureRedirectMask), only Expose events.
26330 These expose events will draw text normally, i.e. not
26331 highlighted. Which means we must redo the highlight here.
26332 Subsume it under ``we love X''. --gerd 2001-08-15 */
26333 /* Included in Windows version because Windows most likely does not
26334 do the right thing if any third party tool offers
26335 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26336 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26337 {
26338 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26339 if (f == hlinfo->mouse_face_mouse_frame)
26340 {
26341 int x = hlinfo->mouse_face_mouse_x;
26342 int y = hlinfo->mouse_face_mouse_y;
26343 clear_mouse_face (hlinfo);
26344 note_mouse_highlight (f, x, y);
26345 }
26346 }
26347 }
26348
26349
26350 /* EXPORT:
26351 Determine the intersection of two rectangles R1 and R2. Return
26352 the intersection in *RESULT. Value is non-zero if RESULT is not
26353 empty. */
26354
26355 int
26356 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26357 {
26358 XRectangle *left, *right;
26359 XRectangle *upper, *lower;
26360 int intersection_p = 0;
26361
26362 /* Rearrange so that R1 is the left-most rectangle. */
26363 if (r1->x < r2->x)
26364 left = r1, right = r2;
26365 else
26366 left = r2, right = r1;
26367
26368 /* X0 of the intersection is right.x0, if this is inside R1,
26369 otherwise there is no intersection. */
26370 if (right->x <= left->x + left->width)
26371 {
26372 result->x = right->x;
26373
26374 /* The right end of the intersection is the minimum of the
26375 the right ends of left and right. */
26376 result->width = (min (left->x + left->width, right->x + right->width)
26377 - result->x);
26378
26379 /* Same game for Y. */
26380 if (r1->y < r2->y)
26381 upper = r1, lower = r2;
26382 else
26383 upper = r2, lower = r1;
26384
26385 /* The upper end of the intersection is lower.y0, if this is inside
26386 of upper. Otherwise, there is no intersection. */
26387 if (lower->y <= upper->y + upper->height)
26388 {
26389 result->y = lower->y;
26390
26391 /* The lower end of the intersection is the minimum of the lower
26392 ends of upper and lower. */
26393 result->height = (min (lower->y + lower->height,
26394 upper->y + upper->height)
26395 - result->y);
26396 intersection_p = 1;
26397 }
26398 }
26399
26400 return intersection_p;
26401 }
26402
26403 #endif /* HAVE_WINDOW_SYSTEM */
26404
26405 \f
26406 /***********************************************************************
26407 Initialization
26408 ***********************************************************************/
26409
26410 void
26411 syms_of_xdisp (void)
26412 {
26413 Vwith_echo_area_save_vector = Qnil;
26414 staticpro (&Vwith_echo_area_save_vector);
26415
26416 Vmessage_stack = Qnil;
26417 staticpro (&Vmessage_stack);
26418
26419 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
26420 staticpro (&Qinhibit_redisplay);
26421
26422 message_dolog_marker1 = Fmake_marker ();
26423 staticpro (&message_dolog_marker1);
26424 message_dolog_marker2 = Fmake_marker ();
26425 staticpro (&message_dolog_marker2);
26426 message_dolog_marker3 = Fmake_marker ();
26427 staticpro (&message_dolog_marker3);
26428
26429 #if GLYPH_DEBUG
26430 defsubr (&Sdump_frame_glyph_matrix);
26431 defsubr (&Sdump_glyph_matrix);
26432 defsubr (&Sdump_glyph_row);
26433 defsubr (&Sdump_tool_bar_row);
26434 defsubr (&Strace_redisplay);
26435 defsubr (&Strace_to_stderr);
26436 #endif
26437 #ifdef HAVE_WINDOW_SYSTEM
26438 defsubr (&Stool_bar_lines_needed);
26439 defsubr (&Slookup_image_map);
26440 #endif
26441 defsubr (&Sformat_mode_line);
26442 defsubr (&Sinvisible_p);
26443 defsubr (&Scurrent_bidi_paragraph_direction);
26444
26445 staticpro (&Qmenu_bar_update_hook);
26446 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
26447
26448 staticpro (&Qoverriding_terminal_local_map);
26449 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
26450
26451 staticpro (&Qoverriding_local_map);
26452 Qoverriding_local_map = intern_c_string ("overriding-local-map");
26453
26454 staticpro (&Qwindow_scroll_functions);
26455 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
26456
26457 staticpro (&Qwindow_text_change_functions);
26458 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
26459
26460 staticpro (&Qredisplay_end_trigger_functions);
26461 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
26462
26463 staticpro (&Qinhibit_point_motion_hooks);
26464 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
26465
26466 Qeval = intern_c_string ("eval");
26467 staticpro (&Qeval);
26468
26469 QCdata = intern_c_string (":data");
26470 staticpro (&QCdata);
26471 Qdisplay = intern_c_string ("display");
26472 staticpro (&Qdisplay);
26473 Qspace_width = intern_c_string ("space-width");
26474 staticpro (&Qspace_width);
26475 Qraise = intern_c_string ("raise");
26476 staticpro (&Qraise);
26477 Qslice = intern_c_string ("slice");
26478 staticpro (&Qslice);
26479 Qspace = intern_c_string ("space");
26480 staticpro (&Qspace);
26481 Qmargin = intern_c_string ("margin");
26482 staticpro (&Qmargin);
26483 Qpointer = intern_c_string ("pointer");
26484 staticpro (&Qpointer);
26485 Qleft_margin = intern_c_string ("left-margin");
26486 staticpro (&Qleft_margin);
26487 Qright_margin = intern_c_string ("right-margin");
26488 staticpro (&Qright_margin);
26489 Qcenter = intern_c_string ("center");
26490 staticpro (&Qcenter);
26491 Qline_height = intern_c_string ("line-height");
26492 staticpro (&Qline_height);
26493 QCalign_to = intern_c_string (":align-to");
26494 staticpro (&QCalign_to);
26495 QCrelative_width = intern_c_string (":relative-width");
26496 staticpro (&QCrelative_width);
26497 QCrelative_height = intern_c_string (":relative-height");
26498 staticpro (&QCrelative_height);
26499 QCeval = intern_c_string (":eval");
26500 staticpro (&QCeval);
26501 QCpropertize = intern_c_string (":propertize");
26502 staticpro (&QCpropertize);
26503 QCfile = intern_c_string (":file");
26504 staticpro (&QCfile);
26505 Qfontified = intern_c_string ("fontified");
26506 staticpro (&Qfontified);
26507 Qfontification_functions = intern_c_string ("fontification-functions");
26508 staticpro (&Qfontification_functions);
26509 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
26510 staticpro (&Qtrailing_whitespace);
26511 Qescape_glyph = intern_c_string ("escape-glyph");
26512 staticpro (&Qescape_glyph);
26513 Qnobreak_space = intern_c_string ("nobreak-space");
26514 staticpro (&Qnobreak_space);
26515 Qimage = intern_c_string ("image");
26516 staticpro (&Qimage);
26517 Qtext = intern_c_string ("text");
26518 staticpro (&Qtext);
26519 Qboth = intern_c_string ("both");
26520 staticpro (&Qboth);
26521 Qboth_horiz = intern_c_string ("both-horiz");
26522 staticpro (&Qboth_horiz);
26523 Qtext_image_horiz = intern_c_string ("text-image-horiz");
26524 staticpro (&Qtext_image_horiz);
26525 QCmap = intern_c_string (":map");
26526 staticpro (&QCmap);
26527 QCpointer = intern_c_string (":pointer");
26528 staticpro (&QCpointer);
26529 Qrect = intern_c_string ("rect");
26530 staticpro (&Qrect);
26531 Qcircle = intern_c_string ("circle");
26532 staticpro (&Qcircle);
26533 Qpoly = intern_c_string ("poly");
26534 staticpro (&Qpoly);
26535 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
26536 staticpro (&Qmessage_truncate_lines);
26537 Qgrow_only = intern_c_string ("grow-only");
26538 staticpro (&Qgrow_only);
26539 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
26540 staticpro (&Qinhibit_menubar_update);
26541 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
26542 staticpro (&Qinhibit_eval_during_redisplay);
26543 Qposition = intern_c_string ("position");
26544 staticpro (&Qposition);
26545 Qbuffer_position = intern_c_string ("buffer-position");
26546 staticpro (&Qbuffer_position);
26547 Qobject = intern_c_string ("object");
26548 staticpro (&Qobject);
26549 Qbar = intern_c_string ("bar");
26550 staticpro (&Qbar);
26551 Qhbar = intern_c_string ("hbar");
26552 staticpro (&Qhbar);
26553 Qbox = intern_c_string ("box");
26554 staticpro (&Qbox);
26555 Qhollow = intern_c_string ("hollow");
26556 staticpro (&Qhollow);
26557 Qhand = intern_c_string ("hand");
26558 staticpro (&Qhand);
26559 Qarrow = intern_c_string ("arrow");
26560 staticpro (&Qarrow);
26561 Qtext = intern_c_string ("text");
26562 staticpro (&Qtext);
26563 Qrisky_local_variable = intern_c_string ("risky-local-variable");
26564 staticpro (&Qrisky_local_variable);
26565 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
26566 staticpro (&Qinhibit_free_realized_faces);
26567
26568 list_of_error = Fcons (Fcons (intern_c_string ("error"),
26569 Fcons (intern_c_string ("void-variable"), Qnil)),
26570 Qnil);
26571 staticpro (&list_of_error);
26572
26573 Qlast_arrow_position = intern_c_string ("last-arrow-position");
26574 staticpro (&Qlast_arrow_position);
26575 Qlast_arrow_string = intern_c_string ("last-arrow-string");
26576 staticpro (&Qlast_arrow_string);
26577
26578 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
26579 staticpro (&Qoverlay_arrow_string);
26580 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
26581 staticpro (&Qoverlay_arrow_bitmap);
26582
26583 echo_buffer[0] = echo_buffer[1] = Qnil;
26584 staticpro (&echo_buffer[0]);
26585 staticpro (&echo_buffer[1]);
26586
26587 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
26588 staticpro (&echo_area_buffer[0]);
26589 staticpro (&echo_area_buffer[1]);
26590
26591 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
26592 staticpro (&Vmessages_buffer_name);
26593
26594 mode_line_proptrans_alist = Qnil;
26595 staticpro (&mode_line_proptrans_alist);
26596 mode_line_string_list = Qnil;
26597 staticpro (&mode_line_string_list);
26598 mode_line_string_face = Qnil;
26599 staticpro (&mode_line_string_face);
26600 mode_line_string_face_prop = Qnil;
26601 staticpro (&mode_line_string_face_prop);
26602 Vmode_line_unwind_vector = Qnil;
26603 staticpro (&Vmode_line_unwind_vector);
26604
26605 help_echo_string = Qnil;
26606 staticpro (&help_echo_string);
26607 help_echo_object = Qnil;
26608 staticpro (&help_echo_object);
26609 help_echo_window = Qnil;
26610 staticpro (&help_echo_window);
26611 previous_help_echo_string = Qnil;
26612 staticpro (&previous_help_echo_string);
26613 help_echo_pos = -1;
26614
26615 Qright_to_left = intern_c_string ("right-to-left");
26616 staticpro (&Qright_to_left);
26617 Qleft_to_right = intern_c_string ("left-to-right");
26618 staticpro (&Qleft_to_right);
26619
26620 #ifdef HAVE_WINDOW_SYSTEM
26621 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
26622 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
26623 For example, if a block cursor is over a tab, it will be drawn as
26624 wide as that tab on the display. */);
26625 x_stretch_cursor_p = 0;
26626 #endif
26627
26628 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
26629 doc: /* *Non-nil means highlight trailing whitespace.
26630 The face used for trailing whitespace is `trailing-whitespace'. */);
26631 Vshow_trailing_whitespace = Qnil;
26632
26633 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
26634 doc: /* *Control highlighting of nobreak space and soft hyphen.
26635 A value of t means highlight the character itself (for nobreak space,
26636 use face `nobreak-space').
26637 A value of nil means no highlighting.
26638 Other values mean display the escape glyph followed by an ordinary
26639 space or ordinary hyphen. */);
26640 Vnobreak_char_display = Qt;
26641
26642 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
26643 doc: /* *The pointer shape to show in void text areas.
26644 A value of nil means to show the text pointer. Other options are `arrow',
26645 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
26646 Vvoid_text_area_pointer = Qarrow;
26647
26648 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
26649 doc: /* Non-nil means don't actually do any redisplay.
26650 This is used for internal purposes. */);
26651 Vinhibit_redisplay = Qnil;
26652
26653 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
26654 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
26655 Vglobal_mode_string = Qnil;
26656
26657 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
26658 doc: /* Marker for where to display an arrow on top of the buffer text.
26659 This must be the beginning of a line in order to work.
26660 See also `overlay-arrow-string'. */);
26661 Voverlay_arrow_position = Qnil;
26662
26663 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
26664 doc: /* String to display as an arrow in non-window frames.
26665 See also `overlay-arrow-position'. */);
26666 Voverlay_arrow_string = make_pure_c_string ("=>");
26667
26668 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
26669 doc: /* List of variables (symbols) which hold markers for overlay arrows.
26670 The symbols on this list are examined during redisplay to determine
26671 where to display overlay arrows. */);
26672 Voverlay_arrow_variable_list
26673 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
26674
26675 DEFVAR_INT ("scroll-step", &scroll_step,
26676 doc: /* *The number of lines to try scrolling a window by when point moves out.
26677 If that fails to bring point back on frame, point is centered instead.
26678 If this is zero, point is always centered after it moves off frame.
26679 If you want scrolling to always be a line at a time, you should set
26680 `scroll-conservatively' to a large value rather than set this to 1. */);
26681
26682 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
26683 doc: /* *Scroll up to this many lines, to bring point back on screen.
26684 If point moves off-screen, redisplay will scroll by up to
26685 `scroll-conservatively' lines in order to bring point just barely
26686 onto the screen again. If that cannot be done, then redisplay
26687 recenters point as usual.
26688
26689 A value of zero means always recenter point if it moves off screen. */);
26690 scroll_conservatively = 0;
26691
26692 DEFVAR_INT ("scroll-margin", &scroll_margin,
26693 doc: /* *Number of lines of margin at the top and bottom of a window.
26694 Recenter the window whenever point gets within this many lines
26695 of the top or bottom of the window. */);
26696 scroll_margin = 0;
26697
26698 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
26699 doc: /* Pixels per inch value for non-window system displays.
26700 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
26701 Vdisplay_pixels_per_inch = make_float (72.0);
26702
26703 #if GLYPH_DEBUG
26704 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
26705 #endif
26706
26707 DEFVAR_LISP ("truncate-partial-width-windows",
26708 &Vtruncate_partial_width_windows,
26709 doc: /* Non-nil means truncate lines in windows narrower than the frame.
26710 For an integer value, truncate lines in each window narrower than the
26711 full frame width, provided the window width is less than that integer;
26712 otherwise, respect the value of `truncate-lines'.
26713
26714 For any other non-nil value, truncate lines in all windows that do
26715 not span the full frame width.
26716
26717 A value of nil means to respect the value of `truncate-lines'.
26718
26719 If `word-wrap' is enabled, you might want to reduce this. */);
26720 Vtruncate_partial_width_windows = make_number (50);
26721
26722 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
26723 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
26724 Any other value means to use the appropriate face, `mode-line',
26725 `header-line', or `menu' respectively. */);
26726 mode_line_inverse_video = 1;
26727
26728 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
26729 doc: /* *Maximum buffer size for which line number should be displayed.
26730 If the buffer is bigger than this, the line number does not appear
26731 in the mode line. A value of nil means no limit. */);
26732 Vline_number_display_limit = Qnil;
26733
26734 DEFVAR_INT ("line-number-display-limit-width",
26735 &line_number_display_limit_width,
26736 doc: /* *Maximum line width (in characters) for line number display.
26737 If the average length of the lines near point is bigger than this, then the
26738 line number may be omitted from the mode line. */);
26739 line_number_display_limit_width = 200;
26740
26741 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
26742 doc: /* *Non-nil means highlight region even in nonselected windows. */);
26743 highlight_nonselected_windows = 0;
26744
26745 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
26746 doc: /* Non-nil if more than one frame is visible on this display.
26747 Minibuffer-only frames don't count, but iconified frames do.
26748 This variable is not guaranteed to be accurate except while processing
26749 `frame-title-format' and `icon-title-format'. */);
26750
26751 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
26752 doc: /* Template for displaying the title bar of visible frames.
26753 \(Assuming the window manager supports this feature.)
26754
26755 This variable has the same structure as `mode-line-format', except that
26756 the %c and %l constructs are ignored. It is used only on frames for
26757 which no explicit name has been set \(see `modify-frame-parameters'). */);
26758
26759 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
26760 doc: /* Template for displaying the title bar of an iconified frame.
26761 \(Assuming the window manager supports this feature.)
26762 This variable has the same structure as `mode-line-format' (which see),
26763 and is used only on frames for which no explicit name has been set
26764 \(see `modify-frame-parameters'). */);
26765 Vicon_title_format
26766 = Vframe_title_format
26767 = pure_cons (intern_c_string ("multiple-frames"),
26768 pure_cons (make_pure_c_string ("%b"),
26769 pure_cons (pure_cons (empty_unibyte_string,
26770 pure_cons (intern_c_string ("invocation-name"),
26771 pure_cons (make_pure_c_string ("@"),
26772 pure_cons (intern_c_string ("system-name"),
26773 Qnil)))),
26774 Qnil)));
26775
26776 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
26777 doc: /* Maximum number of lines to keep in the message log buffer.
26778 If nil, disable message logging. If t, log messages but don't truncate
26779 the buffer when it becomes large. */);
26780 Vmessage_log_max = make_number (100);
26781
26782 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
26783 doc: /* Functions called before redisplay, if window sizes have changed.
26784 The value should be a list of functions that take one argument.
26785 Just before redisplay, for each frame, if any of its windows have changed
26786 size since the last redisplay, or have been split or deleted,
26787 all the functions in the list are called, with the frame as argument. */);
26788 Vwindow_size_change_functions = Qnil;
26789
26790 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
26791 doc: /* List of functions to call before redisplaying a window with scrolling.
26792 Each function is called with two arguments, the window and its new
26793 display-start position. Note that these functions are also called by
26794 `set-window-buffer'. Also note that the value of `window-end' is not
26795 valid when these functions are called. */);
26796 Vwindow_scroll_functions = Qnil;
26797
26798 DEFVAR_LISP ("window-text-change-functions",
26799 &Vwindow_text_change_functions,
26800 doc: /* Functions to call in redisplay when text in the window might change. */);
26801 Vwindow_text_change_functions = Qnil;
26802
26803 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
26804 doc: /* Functions called when redisplay of a window reaches the end trigger.
26805 Each function is called with two arguments, the window and the end trigger value.
26806 See `set-window-redisplay-end-trigger'. */);
26807 Vredisplay_end_trigger_functions = Qnil;
26808
26809 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
26810 doc: /* *Non-nil means autoselect window with mouse pointer.
26811 If nil, do not autoselect windows.
26812 A positive number means delay autoselection by that many seconds: a
26813 window is autoselected only after the mouse has remained in that
26814 window for the duration of the delay.
26815 A negative number has a similar effect, but causes windows to be
26816 autoselected only after the mouse has stopped moving. \(Because of
26817 the way Emacs compares mouse events, you will occasionally wait twice
26818 that time before the window gets selected.\)
26819 Any other value means to autoselect window instantaneously when the
26820 mouse pointer enters it.
26821
26822 Autoselection selects the minibuffer only if it is active, and never
26823 unselects the minibuffer if it is active.
26824
26825 When customizing this variable make sure that the actual value of
26826 `focus-follows-mouse' matches the behavior of your window manager. */);
26827 Vmouse_autoselect_window = Qnil;
26828
26829 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
26830 doc: /* *Non-nil means automatically resize tool-bars.
26831 This dynamically changes the tool-bar's height to the minimum height
26832 that is needed to make all tool-bar items visible.
26833 If value is `grow-only', the tool-bar's height is only increased
26834 automatically; to decrease the tool-bar height, use \\[recenter]. */);
26835 Vauto_resize_tool_bars = Qt;
26836
26837 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
26838 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
26839 auto_raise_tool_bar_buttons_p = 1;
26840
26841 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
26842 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
26843 make_cursor_line_fully_visible_p = 1;
26844
26845 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
26846 doc: /* *Border below tool-bar in pixels.
26847 If an integer, use it as the height of the border.
26848 If it is one of `internal-border-width' or `border-width', use the
26849 value of the corresponding frame parameter.
26850 Otherwise, no border is added below the tool-bar. */);
26851 Vtool_bar_border = Qinternal_border_width;
26852
26853 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
26854 doc: /* *Margin around tool-bar buttons in pixels.
26855 If an integer, use that for both horizontal and vertical margins.
26856 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
26857 HORZ specifying the horizontal margin, and VERT specifying the
26858 vertical margin. */);
26859 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
26860
26861 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
26862 doc: /* *Relief thickness of tool-bar buttons. */);
26863 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
26864
26865 DEFVAR_LISP ("tool-bar-style", &Vtool_bar_style,
26866 doc: /* *Tool bar style to use.
26867 It can be one of
26868 image - show images only
26869 text - show text only
26870 both - show both, text below image
26871 both-horiz - show text to the right of the image
26872 text-image-horiz - show text to the left of the image
26873 any other - use system default or image if no system default. */);
26874 Vtool_bar_style = Qnil;
26875
26876 DEFVAR_INT ("tool-bar-max-label-size", &tool_bar_max_label_size,
26877 doc: /* *Maximum number of characters a label can have to be shown.
26878 The tool bar style must also show labels for this to have any effect, see
26879 `tool-bar-style'. */);
26880 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
26881
26882 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
26883 doc: /* List of functions to call to fontify regions of text.
26884 Each function is called with one argument POS. Functions must
26885 fontify a region starting at POS in the current buffer, and give
26886 fontified regions the property `fontified'. */);
26887 Vfontification_functions = Qnil;
26888 Fmake_variable_buffer_local (Qfontification_functions);
26889
26890 DEFVAR_BOOL ("unibyte-display-via-language-environment",
26891 &unibyte_display_via_language_environment,
26892 doc: /* *Non-nil means display unibyte text according to language environment.
26893 Specifically, this means that raw bytes in the range 160-255 decimal
26894 are displayed by converting them to the equivalent multibyte characters
26895 according to the current language environment. As a result, they are
26896 displayed according to the current fontset.
26897
26898 Note that this variable affects only how these bytes are displayed,
26899 but does not change the fact they are interpreted as raw bytes. */);
26900 unibyte_display_via_language_environment = 0;
26901
26902 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
26903 doc: /* *Maximum height for resizing mini-windows.
26904 If a float, it specifies a fraction of the mini-window frame's height.
26905 If an integer, it specifies a number of lines. */);
26906 Vmax_mini_window_height = make_float (0.25);
26907
26908 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
26909 doc: /* *How to resize mini-windows.
26910 A value of nil means don't automatically resize mini-windows.
26911 A value of t means resize them to fit the text displayed in them.
26912 A value of `grow-only', the default, means let mini-windows grow
26913 only, until their display becomes empty, at which point the windows
26914 go back to their normal size. */);
26915 Vresize_mini_windows = Qgrow_only;
26916
26917 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
26918 doc: /* Alist specifying how to blink the cursor off.
26919 Each element has the form (ON-STATE . OFF-STATE). Whenever the
26920 `cursor-type' frame-parameter or variable equals ON-STATE,
26921 comparing using `equal', Emacs uses OFF-STATE to specify
26922 how to blink it off. ON-STATE and OFF-STATE are values for
26923 the `cursor-type' frame parameter.
26924
26925 If a frame's ON-STATE has no entry in this list,
26926 the frame's other specifications determine how to blink the cursor off. */);
26927 Vblink_cursor_alist = Qnil;
26928
26929 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
26930 doc: /* Allow or disallow automatic horizontal scrolling of windows.
26931 If non-nil, windows are automatically scrolled horizontally to make
26932 point visible. */);
26933 automatic_hscrolling_p = 1;
26934 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
26935 staticpro (&Qauto_hscroll_mode);
26936
26937 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
26938 doc: /* *How many columns away from the window edge point is allowed to get
26939 before automatic hscrolling will horizontally scroll the window. */);
26940 hscroll_margin = 5;
26941
26942 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
26943 doc: /* *How many columns to scroll the window when point gets too close to the edge.
26944 When point is less than `hscroll-margin' columns from the window
26945 edge, automatic hscrolling will scroll the window by the amount of columns
26946 determined by this variable. If its value is a positive integer, scroll that
26947 many columns. If it's a positive floating-point number, it specifies the
26948 fraction of the window's width to scroll. If it's nil or zero, point will be
26949 centered horizontally after the scroll. Any other value, including negative
26950 numbers, are treated as if the value were zero.
26951
26952 Automatic hscrolling always moves point outside the scroll margin, so if
26953 point was more than scroll step columns inside the margin, the window will
26954 scroll more than the value given by the scroll step.
26955
26956 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26957 and `scroll-right' overrides this variable's effect. */);
26958 Vhscroll_step = make_number (0);
26959
26960 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
26961 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26962 Bind this around calls to `message' to let it take effect. */);
26963 message_truncate_lines = 0;
26964
26965 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
26966 doc: /* Normal hook run to update the menu bar definitions.
26967 Redisplay runs this hook before it redisplays the menu bar.
26968 This is used to update submenus such as Buffers,
26969 whose contents depend on various data. */);
26970 Vmenu_bar_update_hook = Qnil;
26971
26972 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
26973 doc: /* Frame for which we are updating a menu.
26974 The enable predicate for a menu binding should check this variable. */);
26975 Vmenu_updating_frame = Qnil;
26976
26977 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
26978 doc: /* Non-nil means don't update menu bars. Internal use only. */);
26979 inhibit_menubar_update = 0;
26980
26981 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
26982 doc: /* Prefix prepended to all continuation lines at display time.
26983 The value may be a string, an image, or a stretch-glyph; it is
26984 interpreted in the same way as the value of a `display' text property.
26985
26986 This variable is overridden by any `wrap-prefix' text or overlay
26987 property.
26988
26989 To add a prefix to non-continuation lines, use `line-prefix'. */);
26990 Vwrap_prefix = Qnil;
26991 staticpro (&Qwrap_prefix);
26992 Qwrap_prefix = intern_c_string ("wrap-prefix");
26993 Fmake_variable_buffer_local (Qwrap_prefix);
26994
26995 DEFVAR_LISP ("line-prefix", &Vline_prefix,
26996 doc: /* Prefix prepended to all non-continuation lines at display time.
26997 The value may be a string, an image, or a stretch-glyph; it is
26998 interpreted in the same way as the value of a `display' text property.
26999
27000 This variable is overridden by any `line-prefix' text or overlay
27001 property.
27002
27003 To add a prefix to continuation lines, use `wrap-prefix'. */);
27004 Vline_prefix = Qnil;
27005 staticpro (&Qline_prefix);
27006 Qline_prefix = intern_c_string ("line-prefix");
27007 Fmake_variable_buffer_local (Qline_prefix);
27008
27009 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
27010 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27011 inhibit_eval_during_redisplay = 0;
27012
27013 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
27014 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27015 inhibit_free_realized_faces = 0;
27016
27017 #if GLYPH_DEBUG
27018 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
27019 doc: /* Inhibit try_window_id display optimization. */);
27020 inhibit_try_window_id = 0;
27021
27022 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
27023 doc: /* Inhibit try_window_reusing display optimization. */);
27024 inhibit_try_window_reusing = 0;
27025
27026 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
27027 doc: /* Inhibit try_cursor_movement display optimization. */);
27028 inhibit_try_cursor_movement = 0;
27029 #endif /* GLYPH_DEBUG */
27030
27031 DEFVAR_INT ("overline-margin", &overline_margin,
27032 doc: /* *Space between overline and text, in pixels.
27033 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27034 margin to the caracter height. */);
27035 overline_margin = 2;
27036
27037 DEFVAR_INT ("underline-minimum-offset",
27038 &underline_minimum_offset,
27039 doc: /* Minimum distance between baseline and underline.
27040 This can improve legibility of underlined text at small font sizes,
27041 particularly when using variable `x-use-underline-position-properties'
27042 with fonts that specify an UNDERLINE_POSITION relatively close to the
27043 baseline. The default value is 1. */);
27044 underline_minimum_offset = 1;
27045
27046 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
27047 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27048 This feature only works when on a window system that can change
27049 cursor shapes. */);
27050 display_hourglass_p = 1;
27051
27052 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
27053 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27054 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27055
27056 hourglass_atimer = NULL;
27057 hourglass_shown_p = 0;
27058
27059 DEFSYM (Qglyphless_char, "glyphless-char");
27060 DEFSYM (Qhexa_code, "hexa-code");
27061 DEFSYM (Qempty_box, "empty-box");
27062 DEFSYM (Qthin_space, "thin-space");
27063 DEFSYM (Qzero_width, "zero-width");
27064
27065 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27066 /* Intern this now in case it isn't already done.
27067 Setting this variable twice is harmless.
27068 But don't staticpro it here--that is done in alloc.c. */
27069 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27070 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27071
27072 DEFVAR_LISP ("glyphless-char-display", &Vglyphless_char_display,
27073 doc: /* Char-table to control displaying of glyphless characters.
27074 Each element, if non-nil, is an ASCII acronym string (displayed in a box)
27075 or one of these symbols:
27076 hexa-code: display with hexadecimal character code in a box
27077 empty-box: display with an empty box
27078 thin-space: display with 1-pixel width space
27079 zero-width: don't display
27080
27081 It has one extra slot to control the display of a character for which
27082 no font is found. The value of the slot is `hexa-code' or `empty-box'.
27083 The default is `empty-box'. */);
27084 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27085 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27086 Qempty_box);
27087 }
27088
27089
27090 /* Initialize this module when Emacs starts. */
27091
27092 void
27093 init_xdisp (void)
27094 {
27095 Lisp_Object root_window;
27096 struct window *mini_w;
27097
27098 current_header_line_height = current_mode_line_height = -1;
27099
27100 CHARPOS (this_line_start_pos) = 0;
27101
27102 mini_w = XWINDOW (minibuf_window);
27103 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
27104
27105 if (!noninteractive)
27106 {
27107 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
27108 int i;
27109
27110 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
27111 set_window_height (root_window,
27112 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27113 0);
27114 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27115 set_window_height (minibuf_window, 1, 0);
27116
27117 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27118 mini_w->total_cols = make_number (FRAME_COLS (f));
27119
27120 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27121 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27122 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27123
27124 /* The default ellipsis glyphs `...'. */
27125 for (i = 0; i < 3; ++i)
27126 default_invis_vector[i] = make_number ('.');
27127 }
27128
27129 {
27130 /* Allocate the buffer for frame titles.
27131 Also used for `format-mode-line'. */
27132 int size = 100;
27133 mode_line_noprop_buf = (char *) xmalloc (size);
27134 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27135 mode_line_noprop_ptr = mode_line_noprop_buf;
27136 mode_line_target = MODE_LINE_DISPLAY;
27137 }
27138
27139 help_echo_showing_p = 0;
27140 }
27141
27142 /* Since w32 does not support atimers, it defines its own implementation of
27143 the following three functions in w32fns.c. */
27144 #ifndef WINDOWSNT
27145
27146 /* Platform-independent portion of hourglass implementation. */
27147
27148 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27149 int
27150 hourglass_started (void)
27151 {
27152 return hourglass_shown_p || hourglass_atimer != NULL;
27153 }
27154
27155 /* Cancel a currently active hourglass timer, and start a new one. */
27156 void
27157 start_hourglass (void)
27158 {
27159 #if defined (HAVE_WINDOW_SYSTEM)
27160 EMACS_TIME delay;
27161 int secs, usecs = 0;
27162
27163 cancel_hourglass ();
27164
27165 if (INTEGERP (Vhourglass_delay)
27166 && XINT (Vhourglass_delay) > 0)
27167 secs = XFASTINT (Vhourglass_delay);
27168 else if (FLOATP (Vhourglass_delay)
27169 && XFLOAT_DATA (Vhourglass_delay) > 0)
27170 {
27171 Lisp_Object tem;
27172 tem = Ftruncate (Vhourglass_delay, Qnil);
27173 secs = XFASTINT (tem);
27174 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27175 }
27176 else
27177 secs = DEFAULT_HOURGLASS_DELAY;
27178
27179 EMACS_SET_SECS_USECS (delay, secs, usecs);
27180 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27181 show_hourglass, NULL);
27182 #endif
27183 }
27184
27185
27186 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27187 shown. */
27188 void
27189 cancel_hourglass (void)
27190 {
27191 #if defined (HAVE_WINDOW_SYSTEM)
27192 if (hourglass_atimer)
27193 {
27194 cancel_atimer (hourglass_atimer);
27195 hourglass_atimer = NULL;
27196 }
27197
27198 if (hourglass_shown_p)
27199 hide_hourglass ();
27200 #endif
27201 }
27202 #endif /* ! WINDOWSNT */
27203