Initial partial support for mouse highlight in bidi-reordered text.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 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 with 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 Note one important detail mentioned above: that the bidi reordering
223 engine, driven by the iterator, produces characters in R2L rows
224 starting at the character that will be the rightmost on display.
225 As far as the iterator is concerned, the geometry of such rows is
226 still left to right, i.e. the iterator "thinks" the first character
227 is at the leftmost pixel position. The iterator does not know that
228 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
229 delivers. This is important when functions from the the move_it_*
230 family are used to get to certain screen position or to match
231 screen coordinates with buffer coordinates: these functions use the
232 iterator geometry, which is left to right even in R2L paragraphs.
233 This works well with most callers of move_it_*, because they need
234 to get to a specific column, and columns are still numbered in the
235 reading order, i.e. the rightmost character in a R2L paragraph is
236 still column zero. But some callers do not get well with this; a
237 notable example is mouse clicks that need to find the character
238 that corresponds to certain pixel coordinates. See
239 buffer_posn_from_coords in dispnew.c for how this is handled. */
240
241 #include <config.h>
242 #include <stdio.h>
243 #include <limits.h>
244 #include <setjmp.h>
245
246 #include "lisp.h"
247 #include "keyboard.h"
248 #include "frame.h"
249 #include "window.h"
250 #include "termchar.h"
251 #include "dispextern.h"
252 #include "buffer.h"
253 #include "character.h"
254 #include "charset.h"
255 #include "indent.h"
256 #include "commands.h"
257 #include "keymap.h"
258 #include "macros.h"
259 #include "disptab.h"
260 #include "termhooks.h"
261 #include "termopts.h"
262 #include "intervals.h"
263 #include "coding.h"
264 #include "process.h"
265 #include "region-cache.h"
266 #include "font.h"
267 #include "fontset.h"
268 #include "blockinput.h"
269
270 #ifdef HAVE_X_WINDOWS
271 #include "xterm.h"
272 #endif
273 #ifdef WINDOWSNT
274 #include "w32term.h"
275 #endif
276 #ifdef HAVE_NS
277 #include "nsterm.h"
278 #endif
279 #ifdef USE_GTK
280 #include "gtkutil.h"
281 #endif
282
283 #include "font.h"
284
285 #ifndef FRAME_X_OUTPUT
286 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
287 #endif
288
289 #define INFINITY 10000000
290
291 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
292 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
293 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
294 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
295 Lisp_Object Qinhibit_point_motion_hooks;
296 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
297 Lisp_Object Qfontified;
298 Lisp_Object Qgrow_only;
299 Lisp_Object Qinhibit_eval_during_redisplay;
300 Lisp_Object Qbuffer_position, Qposition, Qobject;
301 Lisp_Object Qright_to_left, Qleft_to_right;
302
303 /* Cursor shapes */
304 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
305
306 /* Pointer shapes */
307 Lisp_Object Qarrow, Qhand, Qtext;
308
309 Lisp_Object Qrisky_local_variable;
310
311 /* Holds the list (error). */
312 Lisp_Object list_of_error;
313
314 /* Functions called to fontify regions of text. */
315
316 Lisp_Object Vfontification_functions;
317 Lisp_Object Qfontification_functions;
318
319 /* Non-nil means automatically select any window when the mouse
320 cursor moves into it. */
321 Lisp_Object Vmouse_autoselect_window;
322
323 Lisp_Object Vwrap_prefix, Qwrap_prefix;
324 Lisp_Object Vline_prefix, Qline_prefix;
325
326 /* Non-zero means draw tool bar buttons raised when the mouse moves
327 over them. */
328
329 int auto_raise_tool_bar_buttons_p;
330
331 /* Non-zero means to reposition window if cursor line is only partially visible. */
332
333 int make_cursor_line_fully_visible_p;
334
335 /* Margin below tool bar in pixels. 0 or nil means no margin.
336 If value is `internal-border-width' or `border-width',
337 the corresponding frame parameter is used. */
338
339 Lisp_Object Vtool_bar_border;
340
341 /* Margin around tool bar buttons in pixels. */
342
343 Lisp_Object Vtool_bar_button_margin;
344
345 /* Thickness of shadow to draw around tool bar buttons. */
346
347 EMACS_INT tool_bar_button_relief;
348
349 /* Non-nil means automatically resize tool-bars so that all tool-bar
350 items are visible, and no blank lines remain.
351
352 If value is `grow-only', only make tool-bar bigger. */
353
354 Lisp_Object Vauto_resize_tool_bars;
355
356 /* Type of tool bar. Can be symbols image, text, both or both-hroiz. */
357
358 Lisp_Object Vtool_bar_style;
359
360 /* Maximum number of characters a label can have to be shown. */
361
362 EMACS_INT tool_bar_max_label_size;
363
364 /* Non-zero means draw block and hollow cursor as wide as the glyph
365 under it. For example, if a block cursor is over a tab, it will be
366 drawn as wide as that tab on the display. */
367
368 int x_stretch_cursor_p;
369
370 /* Non-nil means don't actually do any redisplay. */
371
372 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
373
374 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
375
376 int inhibit_eval_during_redisplay;
377
378 /* Names of text properties relevant for redisplay. */
379
380 Lisp_Object Qdisplay;
381
382 /* Symbols used in text property values. */
383
384 Lisp_Object Vdisplay_pixels_per_inch;
385 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
386 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
387 Lisp_Object Qslice;
388 Lisp_Object Qcenter;
389 Lisp_Object Qmargin, Qpointer;
390 Lisp_Object Qline_height;
391
392 /* Non-nil means highlight trailing whitespace. */
393
394 Lisp_Object Vshow_trailing_whitespace;
395
396 /* Non-nil means escape non-break space and hyphens. */
397
398 Lisp_Object Vnobreak_char_display;
399
400 #ifdef HAVE_WINDOW_SYSTEM
401
402 /* Test if overflow newline into fringe. Called with iterator IT
403 at or past right window margin, and with IT->current_x set. */
404
405 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
406 (!NILP (Voverflow_newline_into_fringe) \
407 && FRAME_WINDOW_P ((IT)->f) \
408 && ((IT)->bidi_it.paragraph_dir == R2L \
409 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
410 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
411 && (IT)->current_x == (IT)->last_visible_x \
412 && (IT)->line_wrap != WORD_WRAP)
413
414 #else /* !HAVE_WINDOW_SYSTEM */
415 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
416 #endif /* HAVE_WINDOW_SYSTEM */
417
418 /* Test if the display element loaded in IT is a space or tab
419 character. This is used to determine word wrapping. */
420
421 #define IT_DISPLAYING_WHITESPACE(it) \
422 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
423
424 /* Non-nil means show the text cursor in void text areas
425 i.e. in blank areas after eol and eob. This used to be
426 the default in 21.3. */
427
428 Lisp_Object Vvoid_text_area_pointer;
429
430 /* Name of the face used to highlight trailing whitespace. */
431
432 Lisp_Object Qtrailing_whitespace;
433
434 /* Name and number of the face used to highlight escape glyphs. */
435
436 Lisp_Object Qescape_glyph;
437
438 /* Name and number of the face used to highlight non-breaking spaces. */
439
440 Lisp_Object Qnobreak_space;
441
442 /* The symbol `image' which is the car of the lists used to represent
443 images in Lisp. Also a tool bar style. */
444
445 Lisp_Object Qimage;
446
447 /* The image map types. */
448 Lisp_Object QCmap, QCpointer;
449 Lisp_Object Qrect, Qcircle, Qpoly;
450
451 /* Tool bar styles */
452 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
453
454 /* Non-zero means print newline to stdout before next mini-buffer
455 message. */
456
457 int noninteractive_need_newline;
458
459 /* Non-zero means print newline to message log before next message. */
460
461 static int message_log_need_newline;
462
463 /* Three markers that message_dolog uses.
464 It could allocate them itself, but that causes trouble
465 in handling memory-full errors. */
466 static Lisp_Object message_dolog_marker1;
467 static Lisp_Object message_dolog_marker2;
468 static Lisp_Object message_dolog_marker3;
469 \f
470 /* The buffer position of the first character appearing entirely or
471 partially on the line of the selected window which contains the
472 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
473 redisplay optimization in redisplay_internal. */
474
475 static struct text_pos this_line_start_pos;
476
477 /* Number of characters past the end of the line above, including the
478 terminating newline. */
479
480 static struct text_pos this_line_end_pos;
481
482 /* The vertical positions and the height of this line. */
483
484 static int this_line_vpos;
485 static int this_line_y;
486 static int this_line_pixel_height;
487
488 /* X position at which this display line starts. Usually zero;
489 negative if first character is partially visible. */
490
491 static int this_line_start_x;
492
493 /* Buffer that this_line_.* variables are referring to. */
494
495 static struct buffer *this_line_buffer;
496
497 /* Nonzero means truncate lines in all windows less wide than the
498 frame. */
499
500 Lisp_Object Vtruncate_partial_width_windows;
501
502 /* A flag to control how to display unibyte 8-bit character. */
503
504 int unibyte_display_via_language_environment;
505
506 /* Nonzero means we have more than one non-mini-buffer-only frame.
507 Not guaranteed to be accurate except while parsing
508 frame-title-format. */
509
510 int multiple_frames;
511
512 Lisp_Object Vglobal_mode_string;
513
514
515 /* List of variables (symbols) which hold markers for overlay arrows.
516 The symbols on this list are examined during redisplay to determine
517 where to display overlay arrows. */
518
519 Lisp_Object Voverlay_arrow_variable_list;
520
521 /* Marker for where to display an arrow on top of the buffer text. */
522
523 Lisp_Object Voverlay_arrow_position;
524
525 /* String to display for the arrow. Only used on terminal frames. */
526
527 Lisp_Object Voverlay_arrow_string;
528
529 /* Values of those variables at last redisplay are stored as
530 properties on `overlay-arrow-position' symbol. However, if
531 Voverlay_arrow_position is a marker, last-arrow-position is its
532 numerical position. */
533
534 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
535
536 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
537 properties on a symbol in overlay-arrow-variable-list. */
538
539 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
540
541 /* Like mode-line-format, but for the title bar on a visible frame. */
542
543 Lisp_Object Vframe_title_format;
544
545 /* Like mode-line-format, but for the title bar on an iconified frame. */
546
547 Lisp_Object Vicon_title_format;
548
549 /* List of functions to call when a window's size changes. These
550 functions get one arg, a frame on which one or more windows' sizes
551 have changed. */
552
553 static Lisp_Object Vwindow_size_change_functions;
554
555 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
556
557 /* Nonzero if an overlay arrow has been displayed in this window. */
558
559 static int overlay_arrow_seen;
560
561 /* Nonzero means highlight the region even in nonselected windows. */
562
563 int highlight_nonselected_windows;
564
565 /* If cursor motion alone moves point off frame, try scrolling this
566 many lines up or down if that will bring it back. */
567
568 static EMACS_INT scroll_step;
569
570 /* Nonzero means scroll just far enough to bring point back on the
571 screen, when appropriate. */
572
573 static EMACS_INT scroll_conservatively;
574
575 /* Recenter the window whenever point gets within this many lines of
576 the top or bottom of the window. This value is translated into a
577 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
578 that there is really a fixed pixel height scroll margin. */
579
580 EMACS_INT scroll_margin;
581
582 /* Number of windows showing the buffer of the selected window (or
583 another buffer with the same base buffer). keyboard.c refers to
584 this. */
585
586 int buffer_shared;
587
588 /* Vector containing glyphs for an ellipsis `...'. */
589
590 static Lisp_Object default_invis_vector[3];
591
592 /* Zero means display the mode-line/header-line/menu-bar in the default face
593 (this slightly odd definition is for compatibility with previous versions
594 of emacs), non-zero means display them using their respective faces.
595
596 This variable is deprecated. */
597
598 int mode_line_inverse_video;
599
600 /* Prompt to display in front of the mini-buffer contents. */
601
602 Lisp_Object minibuf_prompt;
603
604 /* Width of current mini-buffer prompt. Only set after display_line
605 of the line that contains the prompt. */
606
607 int minibuf_prompt_width;
608
609 /* This is the window where the echo area message was displayed. It
610 is always a mini-buffer window, but it may not be the same window
611 currently active as a mini-buffer. */
612
613 Lisp_Object echo_area_window;
614
615 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
616 pushes the current message and the value of
617 message_enable_multibyte on the stack, the function restore_message
618 pops the stack and displays MESSAGE again. */
619
620 Lisp_Object Vmessage_stack;
621
622 /* Nonzero means multibyte characters were enabled when the echo area
623 message was specified. */
624
625 int message_enable_multibyte;
626
627 /* Nonzero if we should redraw the mode lines on the next redisplay. */
628
629 int update_mode_lines;
630
631 /* Nonzero if window sizes or contents have changed since last
632 redisplay that finished. */
633
634 int windows_or_buffers_changed;
635
636 /* Nonzero means a frame's cursor type has been changed. */
637
638 int cursor_type_changed;
639
640 /* Nonzero after display_mode_line if %l was used and it displayed a
641 line number. */
642
643 int line_number_displayed;
644
645 /* Maximum buffer size for which to display line numbers. */
646
647 Lisp_Object Vline_number_display_limit;
648
649 /* Line width to consider when repositioning for line number display. */
650
651 static EMACS_INT line_number_display_limit_width;
652
653 /* Number of lines to keep in the message log buffer. t means
654 infinite. nil means don't log at all. */
655
656 Lisp_Object Vmessage_log_max;
657
658 /* The name of the *Messages* buffer, a string. */
659
660 static Lisp_Object Vmessages_buffer_name;
661
662 /* Current, index 0, and last displayed echo area message. Either
663 buffers from echo_buffers, or nil to indicate no message. */
664
665 Lisp_Object echo_area_buffer[2];
666
667 /* The buffers referenced from echo_area_buffer. */
668
669 static Lisp_Object echo_buffer[2];
670
671 /* A vector saved used in with_area_buffer to reduce consing. */
672
673 static Lisp_Object Vwith_echo_area_save_vector;
674
675 /* Non-zero means display_echo_area should display the last echo area
676 message again. Set by redisplay_preserve_echo_area. */
677
678 static int display_last_displayed_message_p;
679
680 /* Nonzero if echo area is being used by print; zero if being used by
681 message. */
682
683 int message_buf_print;
684
685 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
686
687 Lisp_Object Qinhibit_menubar_update;
688 int inhibit_menubar_update;
689
690 /* When evaluating expressions from menu bar items (enable conditions,
691 for instance), this is the frame they are being processed for. */
692
693 Lisp_Object Vmenu_updating_frame;
694
695 /* Maximum height for resizing mini-windows. Either a float
696 specifying a fraction of the available height, or an integer
697 specifying a number of lines. */
698
699 Lisp_Object Vmax_mini_window_height;
700
701 /* Non-zero means messages should be displayed with truncated
702 lines instead of being continued. */
703
704 int message_truncate_lines;
705 Lisp_Object Qmessage_truncate_lines;
706
707 /* Set to 1 in clear_message to make redisplay_internal aware
708 of an emptied echo area. */
709
710 static int message_cleared_p;
711
712 /* How to blink the default frame cursor off. */
713 Lisp_Object Vblink_cursor_alist;
714
715 /* A scratch glyph row with contents used for generating truncation
716 glyphs. Also used in direct_output_for_insert. */
717
718 #define MAX_SCRATCH_GLYPHS 100
719 struct glyph_row scratch_glyph_row;
720 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
721
722 /* Ascent and height of the last line processed by move_it_to. */
723
724 static int last_max_ascent, last_height;
725
726 /* Non-zero if there's a help-echo in the echo area. */
727
728 int help_echo_showing_p;
729
730 /* If >= 0, computed, exact values of mode-line and header-line height
731 to use in the macros CURRENT_MODE_LINE_HEIGHT and
732 CURRENT_HEADER_LINE_HEIGHT. */
733
734 int current_mode_line_height, current_header_line_height;
735
736 /* The maximum distance to look ahead for text properties. Values
737 that are too small let us call compute_char_face and similar
738 functions too often which is expensive. Values that are too large
739 let us call compute_char_face and alike too often because we
740 might not be interested in text properties that far away. */
741
742 #define TEXT_PROP_DISTANCE_LIMIT 100
743
744 #if GLYPH_DEBUG
745
746 /* Variables to turn off display optimizations from Lisp. */
747
748 int inhibit_try_window_id, inhibit_try_window_reusing;
749 int inhibit_try_cursor_movement;
750
751 /* Non-zero means print traces of redisplay if compiled with
752 GLYPH_DEBUG != 0. */
753
754 int trace_redisplay_p;
755
756 #endif /* GLYPH_DEBUG */
757
758 #ifdef DEBUG_TRACE_MOVE
759 /* Non-zero means trace with TRACE_MOVE to stderr. */
760 int trace_move;
761
762 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
763 #else
764 #define TRACE_MOVE(x) (void) 0
765 #endif
766
767 /* Non-zero means automatically scroll windows horizontally to make
768 point visible. */
769
770 int automatic_hscrolling_p;
771 Lisp_Object Qauto_hscroll_mode;
772
773 /* How close to the margin can point get before the window is scrolled
774 horizontally. */
775 EMACS_INT hscroll_margin;
776
777 /* How much to scroll horizontally when point is inside the above margin. */
778 Lisp_Object Vhscroll_step;
779
780 /* The variable `resize-mini-windows'. If nil, don't resize
781 mini-windows. If t, always resize them to fit the text they
782 display. If `grow-only', let mini-windows grow only until they
783 become empty. */
784
785 Lisp_Object Vresize_mini_windows;
786
787 /* Buffer being redisplayed -- for redisplay_window_error. */
788
789 struct buffer *displayed_buffer;
790
791 /* Space between overline and text. */
792
793 EMACS_INT overline_margin;
794
795 /* Require underline to be at least this many screen pixels below baseline
796 This to avoid underline "merging" with the base of letters at small
797 font sizes, particularly when x_use_underline_position_properties is on. */
798
799 EMACS_INT underline_minimum_offset;
800
801 /* Value returned from text property handlers (see below). */
802
803 enum prop_handled
804 {
805 HANDLED_NORMALLY,
806 HANDLED_RECOMPUTE_PROPS,
807 HANDLED_OVERLAY_STRING_CONSUMED,
808 HANDLED_RETURN
809 };
810
811 /* A description of text properties that redisplay is interested
812 in. */
813
814 struct props
815 {
816 /* The name of the property. */
817 Lisp_Object *name;
818
819 /* A unique index for the property. */
820 enum prop_idx idx;
821
822 /* A handler function called to set up iterator IT from the property
823 at IT's current position. Value is used to steer handle_stop. */
824 enum prop_handled (*handler) (struct it *it);
825 };
826
827 static enum prop_handled handle_face_prop (struct it *);
828 static enum prop_handled handle_invisible_prop (struct it *);
829 static enum prop_handled handle_display_prop (struct it *);
830 static enum prop_handled handle_composition_prop (struct it *);
831 static enum prop_handled handle_overlay_change (struct it *);
832 static enum prop_handled handle_fontified_prop (struct it *);
833
834 /* Properties handled by iterators. */
835
836 static struct props it_props[] =
837 {
838 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
839 /* Handle `face' before `display' because some sub-properties of
840 `display' need to know the face. */
841 {&Qface, FACE_PROP_IDX, handle_face_prop},
842 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
843 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
844 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
845 {NULL, 0, NULL}
846 };
847
848 /* Value is the position described by X. If X is a marker, value is
849 the marker_position of X. Otherwise, value is X. */
850
851 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
852
853 /* Enumeration returned by some move_it_.* functions internally. */
854
855 enum move_it_result
856 {
857 /* Not used. Undefined value. */
858 MOVE_UNDEFINED,
859
860 /* Move ended at the requested buffer position or ZV. */
861 MOVE_POS_MATCH_OR_ZV,
862
863 /* Move ended at the requested X pixel position. */
864 MOVE_X_REACHED,
865
866 /* Move within a line ended at the end of a line that must be
867 continued. */
868 MOVE_LINE_CONTINUED,
869
870 /* Move within a line ended at the end of a line that would
871 be displayed truncated. */
872 MOVE_LINE_TRUNCATED,
873
874 /* Move within a line ended at a line end. */
875 MOVE_NEWLINE_OR_CR
876 };
877
878 /* This counter is used to clear the face cache every once in a while
879 in redisplay_internal. It is incremented for each redisplay.
880 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
881 cleared. */
882
883 #define CLEAR_FACE_CACHE_COUNT 500
884 static int clear_face_cache_count;
885
886 /* Similarly for the image cache. */
887
888 #ifdef HAVE_WINDOW_SYSTEM
889 #define CLEAR_IMAGE_CACHE_COUNT 101
890 static int clear_image_cache_count;
891 #endif
892
893 /* Non-zero while redisplay_internal is in progress. */
894
895 int redisplaying_p;
896
897 /* Non-zero means don't free realized faces. Bound while freeing
898 realized faces is dangerous because glyph matrices might still
899 reference them. */
900
901 int inhibit_free_realized_faces;
902 Lisp_Object Qinhibit_free_realized_faces;
903
904 /* If a string, XTread_socket generates an event to display that string.
905 (The display is done in read_char.) */
906
907 Lisp_Object help_echo_string;
908 Lisp_Object help_echo_window;
909 Lisp_Object help_echo_object;
910 int help_echo_pos;
911
912 /* Temporary variable for XTread_socket. */
913
914 Lisp_Object previous_help_echo_string;
915
916 /* Null glyph slice */
917
918 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
919
920 /* Platform-independent portion of hourglass implementation. */
921
922 /* Non-zero means we're allowed to display a hourglass pointer. */
923 int display_hourglass_p;
924
925 /* Non-zero means an hourglass cursor is currently shown. */
926 int hourglass_shown_p;
927
928 /* If non-null, an asynchronous timer that, when it expires, displays
929 an hourglass cursor on all frames. */
930 struct atimer *hourglass_atimer;
931
932 /* Number of seconds to wait before displaying an hourglass cursor. */
933 Lisp_Object Vhourglass_delay;
934
935 /* Default number of seconds to wait before displaying an hourglass
936 cursor. */
937 #define DEFAULT_HOURGLASS_DELAY 1
938
939 \f
940 /* Function prototypes. */
941
942 static void setup_for_ellipsis (struct it *, int);
943 static void mark_window_display_accurate_1 (struct window *, int);
944 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
945 static int display_prop_string_p (Lisp_Object, Lisp_Object);
946 static int cursor_row_p (struct window *, struct glyph_row *);
947 static int redisplay_mode_lines (Lisp_Object, int);
948 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
949
950 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
951
952 static void handle_line_prefix (struct it *);
953
954 static void pint2str (char *, int, int);
955 static void pint2hrstr (char *, int, int);
956 static struct text_pos run_window_scroll_functions (Lisp_Object,
957 struct text_pos);
958 static void reconsider_clip_changes (struct window *, struct buffer *);
959 static int text_outside_line_unchanged_p (struct window *, int, int);
960 static void store_mode_line_noprop_char (char);
961 static int store_mode_line_noprop (const unsigned char *, int, int);
962 static void x_consider_frame_title (Lisp_Object);
963 static void handle_stop (struct it *);
964 static void handle_stop_backwards (struct it *, EMACS_INT);
965 static int tool_bar_lines_needed (struct frame *, int *);
966 static int single_display_spec_intangible_p (Lisp_Object);
967 static void ensure_echo_area_buffers (void);
968 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
969 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
970 static int with_echo_area_buffer (struct window *, int,
971 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
972 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
973 static void clear_garbaged_frames (void);
974 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
975 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
976 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
977 static int display_echo_area (struct window *);
978 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
979 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
980 static Lisp_Object unwind_redisplay (Lisp_Object);
981 static int string_char_and_length (const unsigned char *, int *);
982 static struct text_pos display_prop_end (struct it *, Lisp_Object,
983 struct text_pos);
984 static int compute_window_start_on_continuation_line (struct window *);
985 static Lisp_Object safe_eval_handler (Lisp_Object);
986 static void insert_left_trunc_glyphs (struct it *);
987 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
988 Lisp_Object);
989 static void extend_face_to_end_of_line (struct it *);
990 static int append_space_for_newline (struct it *, int);
991 static int cursor_row_fully_visible_p (struct window *, int, int);
992 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
993 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
994 static int trailing_whitespace_p (int);
995 static int message_log_check_duplicate (int, int, int, int);
996 static void push_it (struct it *);
997 static void pop_it (struct it *);
998 static void sync_frame_with_window_matrix_rows (struct window *);
999 static void select_frame_for_redisplay (Lisp_Object);
1000 static void redisplay_internal (int);
1001 static int echo_area_display (int);
1002 static void redisplay_windows (Lisp_Object);
1003 static void redisplay_window (Lisp_Object, int);
1004 static Lisp_Object redisplay_window_error (Lisp_Object);
1005 static Lisp_Object redisplay_window_0 (Lisp_Object);
1006 static Lisp_Object redisplay_window_1 (Lisp_Object);
1007 static int update_menu_bar (struct frame *, int, int);
1008 static int try_window_reusing_current_matrix (struct window *);
1009 static int try_window_id (struct window *);
1010 static int display_line (struct it *);
1011 static int display_mode_lines (struct window *);
1012 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
1013 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
1014 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
1015 static const char *decode_mode_spec (struct window *, int, int, int,
1016 Lisp_Object *);
1017 static void display_menu_bar (struct window *);
1018 static int display_count_lines (int, int, int, int, int *);
1019 static int display_string (const unsigned char *, Lisp_Object, Lisp_Object,
1020 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
1021 static void compute_line_metrics (struct it *);
1022 static void run_redisplay_end_trigger_hook (struct it *);
1023 static int get_overlay_strings (struct it *, int);
1024 static int get_overlay_strings_1 (struct it *, int, int);
1025 static void next_overlay_string (struct it *);
1026 static void reseat (struct it *, struct text_pos, int);
1027 static void reseat_1 (struct it *, struct text_pos, int);
1028 static void back_to_previous_visible_line_start (struct it *);
1029 void reseat_at_previous_visible_line_start (struct it *);
1030 static void reseat_at_next_visible_line_start (struct it *, int);
1031 static int next_element_from_ellipsis (struct it *);
1032 static int next_element_from_display_vector (struct it *);
1033 static int next_element_from_string (struct it *);
1034 static int next_element_from_c_string (struct it *);
1035 static int next_element_from_buffer (struct it *);
1036 static int next_element_from_composition (struct it *);
1037 static int next_element_from_image (struct it *);
1038 static int next_element_from_stretch (struct it *);
1039 static void load_overlay_strings (struct it *, int);
1040 static int init_from_display_pos (struct it *, struct window *,
1041 struct display_pos *);
1042 static void reseat_to_string (struct it *, const unsigned char *,
1043 Lisp_Object, int, int, int, int);
1044 static enum move_it_result
1045 move_it_in_display_line_to (struct it *, EMACS_INT, int,
1046 enum move_operation_enum);
1047 void move_it_vertically_backward (struct it *, int);
1048 static void init_to_row_start (struct it *, struct window *,
1049 struct glyph_row *);
1050 static int init_to_row_end (struct it *, struct window *,
1051 struct glyph_row *);
1052 static void back_to_previous_line_start (struct it *);
1053 static int forward_to_next_line_start (struct it *, int *);
1054 static struct text_pos string_pos_nchars_ahead (struct text_pos,
1055 Lisp_Object, int);
1056 static struct text_pos string_pos (int, Lisp_Object);
1057 static struct text_pos c_string_pos (int, const unsigned char *, int);
1058 static int number_of_chars (const unsigned char *, int);
1059 static void compute_stop_pos (struct it *);
1060 static void compute_string_pos (struct text_pos *, struct text_pos,
1061 Lisp_Object);
1062 static int face_before_or_after_it_pos (struct it *, int);
1063 static EMACS_INT next_overlay_change (EMACS_INT);
1064 static int handle_single_display_spec (struct it *, Lisp_Object,
1065 Lisp_Object, Lisp_Object,
1066 struct text_pos *, int);
1067 static int underlying_face_id (struct it *);
1068 static int in_ellipses_for_invisible_text_p (struct display_pos *,
1069 struct window *);
1070
1071 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1072 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1073
1074 #ifdef HAVE_WINDOW_SYSTEM
1075
1076 static void update_tool_bar (struct frame *, int);
1077 static void build_desired_tool_bar_string (struct frame *f);
1078 static int redisplay_tool_bar (struct frame *);
1079 static void display_tool_bar_line (struct it *, int);
1080 static void notice_overwritten_cursor (struct window *,
1081 enum glyph_row_area,
1082 int, int, int, int);
1083 static void append_stretch_glyph (struct it *, Lisp_Object,
1084 int, int, int);
1085
1086
1087
1088 #endif /* HAVE_WINDOW_SYSTEM */
1089
1090 \f
1091 /***********************************************************************
1092 Window display dimensions
1093 ***********************************************************************/
1094
1095 /* Return the bottom boundary y-position for text lines in window W.
1096 This is the first y position at which a line cannot start.
1097 It is relative to the top of the window.
1098
1099 This is the height of W minus the height of a mode line, if any. */
1100
1101 INLINE int
1102 window_text_bottom_y (struct window *w)
1103 {
1104 int height = WINDOW_TOTAL_HEIGHT (w);
1105
1106 if (WINDOW_WANTS_MODELINE_P (w))
1107 height -= CURRENT_MODE_LINE_HEIGHT (w);
1108 return height;
1109 }
1110
1111 /* Return the pixel width of display area AREA of window W. AREA < 0
1112 means return the total width of W, not including fringes to
1113 the left and right of the window. */
1114
1115 INLINE int
1116 window_box_width (struct window *w, int area)
1117 {
1118 int cols = XFASTINT (w->total_cols);
1119 int pixels = 0;
1120
1121 if (!w->pseudo_window_p)
1122 {
1123 cols -= WINDOW_SCROLL_BAR_COLS (w);
1124
1125 if (area == TEXT_AREA)
1126 {
1127 if (INTEGERP (w->left_margin_cols))
1128 cols -= XFASTINT (w->left_margin_cols);
1129 if (INTEGERP (w->right_margin_cols))
1130 cols -= XFASTINT (w->right_margin_cols);
1131 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1132 }
1133 else if (area == LEFT_MARGIN_AREA)
1134 {
1135 cols = (INTEGERP (w->left_margin_cols)
1136 ? XFASTINT (w->left_margin_cols) : 0);
1137 pixels = 0;
1138 }
1139 else if (area == RIGHT_MARGIN_AREA)
1140 {
1141 cols = (INTEGERP (w->right_margin_cols)
1142 ? XFASTINT (w->right_margin_cols) : 0);
1143 pixels = 0;
1144 }
1145 }
1146
1147 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1148 }
1149
1150
1151 /* Return the pixel height of the display area of window W, not
1152 including mode lines of W, if any. */
1153
1154 INLINE int
1155 window_box_height (struct window *w)
1156 {
1157 struct frame *f = XFRAME (w->frame);
1158 int height = WINDOW_TOTAL_HEIGHT (w);
1159
1160 xassert (height >= 0);
1161
1162 /* Note: the code below that determines the mode-line/header-line
1163 height is essentially the same as that contained in the macro
1164 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1165 the appropriate glyph row has its `mode_line_p' flag set,
1166 and if it doesn't, uses estimate_mode_line_height instead. */
1167
1168 if (WINDOW_WANTS_MODELINE_P (w))
1169 {
1170 struct glyph_row *ml_row
1171 = (w->current_matrix && w->current_matrix->rows
1172 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1173 : 0);
1174 if (ml_row && ml_row->mode_line_p)
1175 height -= ml_row->height;
1176 else
1177 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1178 }
1179
1180 if (WINDOW_WANTS_HEADER_LINE_P (w))
1181 {
1182 struct glyph_row *hl_row
1183 = (w->current_matrix && w->current_matrix->rows
1184 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1185 : 0);
1186 if (hl_row && hl_row->mode_line_p)
1187 height -= hl_row->height;
1188 else
1189 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1190 }
1191
1192 /* With a very small font and a mode-line that's taller than
1193 default, we might end up with a negative height. */
1194 return max (0, height);
1195 }
1196
1197 /* Return the window-relative coordinate of the left edge of display
1198 area AREA of window W. AREA < 0 means return the left edge of the
1199 whole window, to the right of the left fringe of W. */
1200
1201 INLINE int
1202 window_box_left_offset (struct window *w, int area)
1203 {
1204 int x;
1205
1206 if (w->pseudo_window_p)
1207 return 0;
1208
1209 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1210
1211 if (area == TEXT_AREA)
1212 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1213 + window_box_width (w, LEFT_MARGIN_AREA));
1214 else if (area == RIGHT_MARGIN_AREA)
1215 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1216 + window_box_width (w, LEFT_MARGIN_AREA)
1217 + window_box_width (w, TEXT_AREA)
1218 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1219 ? 0
1220 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1221 else if (area == LEFT_MARGIN_AREA
1222 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1223 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1224
1225 return x;
1226 }
1227
1228
1229 /* Return the window-relative coordinate of the right edge of display
1230 area AREA of window W. AREA < 0 means return the right edge of the
1231 whole window, to the left of the right fringe of W. */
1232
1233 INLINE int
1234 window_box_right_offset (struct window *w, int area)
1235 {
1236 return window_box_left_offset (w, area) + window_box_width (w, area);
1237 }
1238
1239 /* Return the frame-relative coordinate of the left edge of display
1240 area AREA of window W. AREA < 0 means return the left edge of the
1241 whole window, to the right of the left fringe of W. */
1242
1243 INLINE int
1244 window_box_left (struct window *w, int area)
1245 {
1246 struct frame *f = XFRAME (w->frame);
1247 int x;
1248
1249 if (w->pseudo_window_p)
1250 return FRAME_INTERNAL_BORDER_WIDTH (f);
1251
1252 x = (WINDOW_LEFT_EDGE_X (w)
1253 + window_box_left_offset (w, area));
1254
1255 return x;
1256 }
1257
1258
1259 /* Return the frame-relative coordinate of the right edge of display
1260 area AREA of window W. AREA < 0 means return the right edge of the
1261 whole window, to the left of the right fringe of W. */
1262
1263 INLINE int
1264 window_box_right (struct window *w, int area)
1265 {
1266 return window_box_left (w, area) + window_box_width (w, area);
1267 }
1268
1269 /* Get the bounding box of the display area AREA of window W, without
1270 mode lines, in frame-relative coordinates. AREA < 0 means the
1271 whole window, not including the left and right fringes of
1272 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1273 coordinates of the upper-left corner of the box. Return in
1274 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1275
1276 INLINE void
1277 window_box (struct window *w, int area, int *box_x, int *box_y,
1278 int *box_width, int *box_height)
1279 {
1280 if (box_width)
1281 *box_width = window_box_width (w, area);
1282 if (box_height)
1283 *box_height = window_box_height (w);
1284 if (box_x)
1285 *box_x = window_box_left (w, area);
1286 if (box_y)
1287 {
1288 *box_y = WINDOW_TOP_EDGE_Y (w);
1289 if (WINDOW_WANTS_HEADER_LINE_P (w))
1290 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1291 }
1292 }
1293
1294
1295 /* Get the bounding box of the display area AREA of window W, without
1296 mode lines. AREA < 0 means the whole window, not including the
1297 left and right fringe of the window. Return in *TOP_LEFT_X
1298 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1299 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1300 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1301 box. */
1302
1303 INLINE void
1304 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1305 int *bottom_right_x, int *bottom_right_y)
1306 {
1307 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1308 bottom_right_y);
1309 *bottom_right_x += *top_left_x;
1310 *bottom_right_y += *top_left_y;
1311 }
1312
1313
1314 \f
1315 /***********************************************************************
1316 Utilities
1317 ***********************************************************************/
1318
1319 /* Return the bottom y-position of the line the iterator IT is in.
1320 This can modify IT's settings. */
1321
1322 int
1323 line_bottom_y (struct it *it)
1324 {
1325 int line_height = it->max_ascent + it->max_descent;
1326 int line_top_y = it->current_y;
1327
1328 if (line_height == 0)
1329 {
1330 if (last_height)
1331 line_height = last_height;
1332 else if (IT_CHARPOS (*it) < ZV)
1333 {
1334 move_it_by_lines (it, 1, 1);
1335 line_height = (it->max_ascent || it->max_descent
1336 ? it->max_ascent + it->max_descent
1337 : last_height);
1338 }
1339 else
1340 {
1341 struct glyph_row *row = it->glyph_row;
1342
1343 /* Use the default character height. */
1344 it->glyph_row = NULL;
1345 it->what = IT_CHARACTER;
1346 it->c = ' ';
1347 it->len = 1;
1348 PRODUCE_GLYPHS (it);
1349 line_height = it->ascent + it->descent;
1350 it->glyph_row = row;
1351 }
1352 }
1353
1354 return line_top_y + line_height;
1355 }
1356
1357
1358 /* Return 1 if position CHARPOS is visible in window W.
1359 CHARPOS < 0 means return info about WINDOW_END position.
1360 If visible, set *X and *Y to pixel coordinates of top left corner.
1361 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1362 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1363
1364 int
1365 pos_visible_p (struct window *w, int charpos, int *x, int *y,
1366 int *rtop, int *rbot, int *rowh, int *vpos)
1367 {
1368 struct it it;
1369 struct text_pos top;
1370 int visible_p = 0;
1371 struct buffer *old_buffer = NULL;
1372
1373 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1374 return visible_p;
1375
1376 if (XBUFFER (w->buffer) != current_buffer)
1377 {
1378 old_buffer = current_buffer;
1379 set_buffer_internal_1 (XBUFFER (w->buffer));
1380 }
1381
1382 SET_TEXT_POS_FROM_MARKER (top, w->start);
1383
1384 /* Compute exact mode line heights. */
1385 if (WINDOW_WANTS_MODELINE_P (w))
1386 current_mode_line_height
1387 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1388 current_buffer->mode_line_format);
1389
1390 if (WINDOW_WANTS_HEADER_LINE_P (w))
1391 current_header_line_height
1392 = display_mode_line (w, HEADER_LINE_FACE_ID,
1393 current_buffer->header_line_format);
1394
1395 start_display (&it, w, top);
1396 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1397 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1398
1399 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1400 {
1401 /* We have reached CHARPOS, or passed it. How the call to
1402 move_it_to can overshoot: (i) If CHARPOS is on invisible
1403 text, move_it_to stops at the end of the invisible text,
1404 after CHARPOS. (ii) If CHARPOS is in a display vector,
1405 move_it_to stops on its last glyph. */
1406 int top_x = it.current_x;
1407 int top_y = it.current_y;
1408 enum it_method it_method = it.method;
1409 /* Calling line_bottom_y may change it.method, it.position, etc. */
1410 int bottom_y = (last_height = 0, line_bottom_y (&it));
1411 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1412
1413 if (top_y < window_top_y)
1414 visible_p = bottom_y > window_top_y;
1415 else if (top_y < it.last_visible_y)
1416 visible_p = 1;
1417 if (visible_p)
1418 {
1419 if (it_method == GET_FROM_DISPLAY_VECTOR)
1420 {
1421 /* We stopped on the last glyph of a display vector.
1422 Try and recompute. Hack alert! */
1423 if (charpos < 2 || top.charpos >= charpos)
1424 top_x = it.glyph_row->x;
1425 else
1426 {
1427 struct it it2;
1428 start_display (&it2, w, top);
1429 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1430 get_next_display_element (&it2);
1431 PRODUCE_GLYPHS (&it2);
1432 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1433 || it2.current_x > it2.last_visible_x)
1434 top_x = it.glyph_row->x;
1435 else
1436 {
1437 top_x = it2.current_x;
1438 top_y = it2.current_y;
1439 }
1440 }
1441 }
1442
1443 *x = top_x;
1444 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1445 *rtop = max (0, window_top_y - top_y);
1446 *rbot = max (0, bottom_y - it.last_visible_y);
1447 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1448 - max (top_y, window_top_y)));
1449 *vpos = it.vpos;
1450 }
1451 }
1452 else
1453 {
1454 struct it it2;
1455
1456 it2 = it;
1457 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1458 move_it_by_lines (&it, 1, 0);
1459 if (charpos < IT_CHARPOS (it)
1460 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1461 {
1462 visible_p = 1;
1463 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1464 *x = it2.current_x;
1465 *y = it2.current_y + it2.max_ascent - it2.ascent;
1466 *rtop = max (0, -it2.current_y);
1467 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1468 - it.last_visible_y));
1469 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1470 it.last_visible_y)
1471 - max (it2.current_y,
1472 WINDOW_HEADER_LINE_HEIGHT (w))));
1473 *vpos = it2.vpos;
1474 }
1475 }
1476
1477 if (old_buffer)
1478 set_buffer_internal_1 (old_buffer);
1479
1480 current_header_line_height = current_mode_line_height = -1;
1481
1482 if (visible_p && XFASTINT (w->hscroll) > 0)
1483 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1484
1485 #if 0
1486 /* Debugging code. */
1487 if (visible_p)
1488 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1489 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1490 else
1491 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1492 #endif
1493
1494 return visible_p;
1495 }
1496
1497
1498 /* Return the next character from STR which is MAXLEN bytes long.
1499 Return in *LEN the length of the character. This is like
1500 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1501 we find one, we return a `?', but with the length of the invalid
1502 character. */
1503
1504 static INLINE int
1505 string_char_and_length (const unsigned char *str, int *len)
1506 {
1507 int c;
1508
1509 c = STRING_CHAR_AND_LENGTH (str, *len);
1510 if (!CHAR_VALID_P (c, 1))
1511 /* We may not change the length here because other places in Emacs
1512 don't use this function, i.e. they silently accept invalid
1513 characters. */
1514 c = '?';
1515
1516 return c;
1517 }
1518
1519
1520
1521 /* Given a position POS containing a valid character and byte position
1522 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1523
1524 static struct text_pos
1525 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, int nchars)
1526 {
1527 xassert (STRINGP (string) && nchars >= 0);
1528
1529 if (STRING_MULTIBYTE (string))
1530 {
1531 int rest = SBYTES (string) - BYTEPOS (pos);
1532 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1533 int len;
1534
1535 while (nchars--)
1536 {
1537 string_char_and_length (p, &len);
1538 p += len, rest -= len;
1539 xassert (rest >= 0);
1540 CHARPOS (pos) += 1;
1541 BYTEPOS (pos) += len;
1542 }
1543 }
1544 else
1545 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1546
1547 return pos;
1548 }
1549
1550
1551 /* Value is the text position, i.e. character and byte position,
1552 for character position CHARPOS in STRING. */
1553
1554 static INLINE struct text_pos
1555 string_pos (int charpos, Lisp_Object string)
1556 {
1557 struct text_pos pos;
1558 xassert (STRINGP (string));
1559 xassert (charpos >= 0);
1560 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1561 return pos;
1562 }
1563
1564
1565 /* Value is a text position, i.e. character and byte position, for
1566 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1567 means recognize multibyte characters. */
1568
1569 static struct text_pos
1570 c_string_pos (int charpos, const unsigned char *s, int multibyte_p)
1571 {
1572 struct text_pos pos;
1573
1574 xassert (s != NULL);
1575 xassert (charpos >= 0);
1576
1577 if (multibyte_p)
1578 {
1579 int rest = strlen (s), len;
1580
1581 SET_TEXT_POS (pos, 0, 0);
1582 while (charpos--)
1583 {
1584 string_char_and_length (s, &len);
1585 s += len, rest -= len;
1586 xassert (rest >= 0);
1587 CHARPOS (pos) += 1;
1588 BYTEPOS (pos) += len;
1589 }
1590 }
1591 else
1592 SET_TEXT_POS (pos, charpos, charpos);
1593
1594 return pos;
1595 }
1596
1597
1598 /* Value is the number of characters in C string S. MULTIBYTE_P
1599 non-zero means recognize multibyte characters. */
1600
1601 static int
1602 number_of_chars (const unsigned char *s, int multibyte_p)
1603 {
1604 int nchars;
1605
1606 if (multibyte_p)
1607 {
1608 int rest = strlen (s), len;
1609 unsigned char *p = (unsigned char *) s;
1610
1611 for (nchars = 0; rest > 0; ++nchars)
1612 {
1613 string_char_and_length (p, &len);
1614 rest -= len, p += len;
1615 }
1616 }
1617 else
1618 nchars = strlen (s);
1619
1620 return nchars;
1621 }
1622
1623
1624 /* Compute byte position NEWPOS->bytepos corresponding to
1625 NEWPOS->charpos. POS is a known position in string STRING.
1626 NEWPOS->charpos must be >= POS.charpos. */
1627
1628 static void
1629 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1630 {
1631 xassert (STRINGP (string));
1632 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1633
1634 if (STRING_MULTIBYTE (string))
1635 *newpos = string_pos_nchars_ahead (pos, string,
1636 CHARPOS (*newpos) - CHARPOS (pos));
1637 else
1638 BYTEPOS (*newpos) = CHARPOS (*newpos);
1639 }
1640
1641 /* EXPORT:
1642 Return an estimation of the pixel height of mode or header lines on
1643 frame F. FACE_ID specifies what line's height to estimate. */
1644
1645 int
1646 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1647 {
1648 #ifdef HAVE_WINDOW_SYSTEM
1649 if (FRAME_WINDOW_P (f))
1650 {
1651 int height = FONT_HEIGHT (FRAME_FONT (f));
1652
1653 /* This function is called so early when Emacs starts that the face
1654 cache and mode line face are not yet initialized. */
1655 if (FRAME_FACE_CACHE (f))
1656 {
1657 struct face *face = FACE_FROM_ID (f, face_id);
1658 if (face)
1659 {
1660 if (face->font)
1661 height = FONT_HEIGHT (face->font);
1662 if (face->box_line_width > 0)
1663 height += 2 * face->box_line_width;
1664 }
1665 }
1666
1667 return height;
1668 }
1669 #endif
1670
1671 return 1;
1672 }
1673
1674 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1675 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1676 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1677 not force the value into range. */
1678
1679 void
1680 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1681 int *x, int *y, NativeRectangle *bounds, int noclip)
1682 {
1683
1684 #ifdef HAVE_WINDOW_SYSTEM
1685 if (FRAME_WINDOW_P (f))
1686 {
1687 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1688 even for negative values. */
1689 if (pix_x < 0)
1690 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1691 if (pix_y < 0)
1692 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1693
1694 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1695 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1696
1697 if (bounds)
1698 STORE_NATIVE_RECT (*bounds,
1699 FRAME_COL_TO_PIXEL_X (f, pix_x),
1700 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1701 FRAME_COLUMN_WIDTH (f) - 1,
1702 FRAME_LINE_HEIGHT (f) - 1);
1703
1704 if (!noclip)
1705 {
1706 if (pix_x < 0)
1707 pix_x = 0;
1708 else if (pix_x > FRAME_TOTAL_COLS (f))
1709 pix_x = FRAME_TOTAL_COLS (f);
1710
1711 if (pix_y < 0)
1712 pix_y = 0;
1713 else if (pix_y > FRAME_LINES (f))
1714 pix_y = FRAME_LINES (f);
1715 }
1716 }
1717 #endif
1718
1719 *x = pix_x;
1720 *y = pix_y;
1721 }
1722
1723
1724 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1725 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1726 can't tell the positions because W's display is not up to date,
1727 return 0. */
1728
1729 int
1730 glyph_to_pixel_coords (struct window *w, int hpos, int vpos,
1731 int *frame_x, int *frame_y)
1732 {
1733 #ifdef HAVE_WINDOW_SYSTEM
1734 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1735 {
1736 int success_p;
1737
1738 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1739 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1740
1741 if (display_completed)
1742 {
1743 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1744 struct glyph *glyph = row->glyphs[TEXT_AREA];
1745 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1746
1747 hpos = row->x;
1748 vpos = row->y;
1749 while (glyph < end)
1750 {
1751 hpos += glyph->pixel_width;
1752 ++glyph;
1753 }
1754
1755 /* If first glyph is partially visible, its first visible position is still 0. */
1756 if (hpos < 0)
1757 hpos = 0;
1758
1759 success_p = 1;
1760 }
1761 else
1762 {
1763 hpos = vpos = 0;
1764 success_p = 0;
1765 }
1766
1767 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1768 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1769 return success_p;
1770 }
1771 #endif
1772
1773 *frame_x = hpos;
1774 *frame_y = vpos;
1775 return 1;
1776 }
1777
1778
1779 #ifdef HAVE_WINDOW_SYSTEM
1780
1781 /* Find the glyph under window-relative coordinates X/Y in window W.
1782 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1783 strings. Return in *HPOS and *VPOS the row and column number of
1784 the glyph found. Return in *AREA the glyph area containing X.
1785 Value is a pointer to the glyph found or null if X/Y is not on
1786 text, or we can't tell because W's current matrix is not up to
1787 date. */
1788
1789 static
1790 struct glyph *
1791 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1792 int *dx, int *dy, int *area)
1793 {
1794 struct glyph *glyph, *end;
1795 struct glyph_row *row = NULL;
1796 int x0, i;
1797
1798 /* Find row containing Y. Give up if some row is not enabled. */
1799 for (i = 0; i < w->current_matrix->nrows; ++i)
1800 {
1801 row = MATRIX_ROW (w->current_matrix, i);
1802 if (!row->enabled_p)
1803 return NULL;
1804 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1805 break;
1806 }
1807
1808 *vpos = i;
1809 *hpos = 0;
1810
1811 /* Give up if Y is not in the window. */
1812 if (i == w->current_matrix->nrows)
1813 return NULL;
1814
1815 /* Get the glyph area containing X. */
1816 if (w->pseudo_window_p)
1817 {
1818 *area = TEXT_AREA;
1819 x0 = 0;
1820 }
1821 else
1822 {
1823 if (x < window_box_left_offset (w, TEXT_AREA))
1824 {
1825 *area = LEFT_MARGIN_AREA;
1826 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1827 }
1828 else if (x < window_box_right_offset (w, TEXT_AREA))
1829 {
1830 *area = TEXT_AREA;
1831 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1832 }
1833 else
1834 {
1835 *area = RIGHT_MARGIN_AREA;
1836 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1837 }
1838 }
1839
1840 /* Find glyph containing X. */
1841 glyph = row->glyphs[*area];
1842 end = glyph + row->used[*area];
1843 x -= x0;
1844 while (glyph < end && x >= glyph->pixel_width)
1845 {
1846 x -= glyph->pixel_width;
1847 ++glyph;
1848 }
1849
1850 if (glyph == end)
1851 return NULL;
1852
1853 if (dx)
1854 {
1855 *dx = x;
1856 *dy = y - (row->y + row->ascent - glyph->ascent);
1857 }
1858
1859 *hpos = glyph - row->glyphs[*area];
1860 return glyph;
1861 }
1862
1863
1864 /* EXPORT:
1865 Convert frame-relative x/y to coordinates relative to window W.
1866 Takes pseudo-windows into account. */
1867
1868 void
1869 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1870 {
1871 if (w->pseudo_window_p)
1872 {
1873 /* A pseudo-window is always full-width, and starts at the
1874 left edge of the frame, plus a frame border. */
1875 struct frame *f = XFRAME (w->frame);
1876 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1877 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1878 }
1879 else
1880 {
1881 *x -= WINDOW_LEFT_EDGE_X (w);
1882 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1883 }
1884 }
1885
1886 /* EXPORT:
1887 Return in RECTS[] at most N clipping rectangles for glyph string S.
1888 Return the number of stored rectangles. */
1889
1890 int
1891 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1892 {
1893 XRectangle r;
1894
1895 if (n <= 0)
1896 return 0;
1897
1898 if (s->row->full_width_p)
1899 {
1900 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1901 r.x = WINDOW_LEFT_EDGE_X (s->w);
1902 r.width = WINDOW_TOTAL_WIDTH (s->w);
1903
1904 /* Unless displaying a mode or menu bar line, which are always
1905 fully visible, clip to the visible part of the row. */
1906 if (s->w->pseudo_window_p)
1907 r.height = s->row->visible_height;
1908 else
1909 r.height = s->height;
1910 }
1911 else
1912 {
1913 /* This is a text line that may be partially visible. */
1914 r.x = window_box_left (s->w, s->area);
1915 r.width = window_box_width (s->w, s->area);
1916 r.height = s->row->visible_height;
1917 }
1918
1919 if (s->clip_head)
1920 if (r.x < s->clip_head->x)
1921 {
1922 if (r.width >= s->clip_head->x - r.x)
1923 r.width -= s->clip_head->x - r.x;
1924 else
1925 r.width = 0;
1926 r.x = s->clip_head->x;
1927 }
1928 if (s->clip_tail)
1929 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1930 {
1931 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1932 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1933 else
1934 r.width = 0;
1935 }
1936
1937 /* If S draws overlapping rows, it's sufficient to use the top and
1938 bottom of the window for clipping because this glyph string
1939 intentionally draws over other lines. */
1940 if (s->for_overlaps)
1941 {
1942 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1943 r.height = window_text_bottom_y (s->w) - r.y;
1944
1945 /* Alas, the above simple strategy does not work for the
1946 environments with anti-aliased text: if the same text is
1947 drawn onto the same place multiple times, it gets thicker.
1948 If the overlap we are processing is for the erased cursor, we
1949 take the intersection with the rectagle of the cursor. */
1950 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1951 {
1952 XRectangle rc, r_save = r;
1953
1954 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1955 rc.y = s->w->phys_cursor.y;
1956 rc.width = s->w->phys_cursor_width;
1957 rc.height = s->w->phys_cursor_height;
1958
1959 x_intersect_rectangles (&r_save, &rc, &r);
1960 }
1961 }
1962 else
1963 {
1964 /* Don't use S->y for clipping because it doesn't take partially
1965 visible lines into account. For example, it can be negative for
1966 partially visible lines at the top of a window. */
1967 if (!s->row->full_width_p
1968 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1969 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1970 else
1971 r.y = max (0, s->row->y);
1972 }
1973
1974 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1975
1976 /* If drawing the cursor, don't let glyph draw outside its
1977 advertised boundaries. Cleartype does this under some circumstances. */
1978 if (s->hl == DRAW_CURSOR)
1979 {
1980 struct glyph *glyph = s->first_glyph;
1981 int height, max_y;
1982
1983 if (s->x > r.x)
1984 {
1985 r.width -= s->x - r.x;
1986 r.x = s->x;
1987 }
1988 r.width = min (r.width, glyph->pixel_width);
1989
1990 /* If r.y is below window bottom, ensure that we still see a cursor. */
1991 height = min (glyph->ascent + glyph->descent,
1992 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1993 max_y = window_text_bottom_y (s->w) - height;
1994 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1995 if (s->ybase - glyph->ascent > max_y)
1996 {
1997 r.y = max_y;
1998 r.height = height;
1999 }
2000 else
2001 {
2002 /* Don't draw cursor glyph taller than our actual glyph. */
2003 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2004 if (height < r.height)
2005 {
2006 max_y = r.y + r.height;
2007 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2008 r.height = min (max_y - r.y, height);
2009 }
2010 }
2011 }
2012
2013 if (s->row->clip)
2014 {
2015 XRectangle r_save = r;
2016
2017 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2018 r.width = 0;
2019 }
2020
2021 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2022 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2023 {
2024 #ifdef CONVERT_FROM_XRECT
2025 CONVERT_FROM_XRECT (r, *rects);
2026 #else
2027 *rects = r;
2028 #endif
2029 return 1;
2030 }
2031 else
2032 {
2033 /* If we are processing overlapping and allowed to return
2034 multiple clipping rectangles, we exclude the row of the glyph
2035 string from the clipping rectangle. This is to avoid drawing
2036 the same text on the environment with anti-aliasing. */
2037 #ifdef CONVERT_FROM_XRECT
2038 XRectangle rs[2];
2039 #else
2040 XRectangle *rs = rects;
2041 #endif
2042 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2043
2044 if (s->for_overlaps & OVERLAPS_PRED)
2045 {
2046 rs[i] = r;
2047 if (r.y + r.height > row_y)
2048 {
2049 if (r.y < row_y)
2050 rs[i].height = row_y - r.y;
2051 else
2052 rs[i].height = 0;
2053 }
2054 i++;
2055 }
2056 if (s->for_overlaps & OVERLAPS_SUCC)
2057 {
2058 rs[i] = r;
2059 if (r.y < row_y + s->row->visible_height)
2060 {
2061 if (r.y + r.height > row_y + s->row->visible_height)
2062 {
2063 rs[i].y = row_y + s->row->visible_height;
2064 rs[i].height = r.y + r.height - rs[i].y;
2065 }
2066 else
2067 rs[i].height = 0;
2068 }
2069 i++;
2070 }
2071
2072 n = i;
2073 #ifdef CONVERT_FROM_XRECT
2074 for (i = 0; i < n; i++)
2075 CONVERT_FROM_XRECT (rs[i], rects[i]);
2076 #endif
2077 return n;
2078 }
2079 }
2080
2081 /* EXPORT:
2082 Return in *NR the clipping rectangle for glyph string S. */
2083
2084 void
2085 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2086 {
2087 get_glyph_string_clip_rects (s, nr, 1);
2088 }
2089
2090
2091 /* EXPORT:
2092 Return the position and height of the phys cursor in window W.
2093 Set w->phys_cursor_width to width of phys cursor.
2094 */
2095
2096 void
2097 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2098 struct glyph *glyph, int *xp, int *yp, int *heightp)
2099 {
2100 struct frame *f = XFRAME (WINDOW_FRAME (w));
2101 int x, y, wd, h, h0, y0;
2102
2103 /* Compute the width of the rectangle to draw. If on a stretch
2104 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2105 rectangle as wide as the glyph, but use a canonical character
2106 width instead. */
2107 wd = glyph->pixel_width - 1;
2108 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2109 wd++; /* Why? */
2110 #endif
2111
2112 x = w->phys_cursor.x;
2113 if (x < 0)
2114 {
2115 wd += x;
2116 x = 0;
2117 }
2118
2119 if (glyph->type == STRETCH_GLYPH
2120 && !x_stretch_cursor_p)
2121 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2122 w->phys_cursor_width = wd;
2123
2124 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2125
2126 /* If y is below window bottom, ensure that we still see a cursor. */
2127 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2128
2129 h = max (h0, glyph->ascent + glyph->descent);
2130 h0 = min (h0, glyph->ascent + glyph->descent);
2131
2132 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2133 if (y < y0)
2134 {
2135 h = max (h - (y0 - y) + 1, h0);
2136 y = y0 - 1;
2137 }
2138 else
2139 {
2140 y0 = window_text_bottom_y (w) - h0;
2141 if (y > y0)
2142 {
2143 h += y - y0;
2144 y = y0;
2145 }
2146 }
2147
2148 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2149 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2150 *heightp = h;
2151 }
2152
2153 /*
2154 * Remember which glyph the mouse is over.
2155 */
2156
2157 void
2158 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2159 {
2160 Lisp_Object window;
2161 struct window *w;
2162 struct glyph_row *r, *gr, *end_row;
2163 enum window_part part;
2164 enum glyph_row_area area;
2165 int x, y, width, height;
2166
2167 /* Try to determine frame pixel position and size of the glyph under
2168 frame pixel coordinates X/Y on frame F. */
2169
2170 if (!f->glyphs_initialized_p
2171 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2172 NILP (window)))
2173 {
2174 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2175 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2176 goto virtual_glyph;
2177 }
2178
2179 w = XWINDOW (window);
2180 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2181 height = WINDOW_FRAME_LINE_HEIGHT (w);
2182
2183 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2184 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2185
2186 if (w->pseudo_window_p)
2187 {
2188 area = TEXT_AREA;
2189 part = ON_MODE_LINE; /* Don't adjust margin. */
2190 goto text_glyph;
2191 }
2192
2193 switch (part)
2194 {
2195 case ON_LEFT_MARGIN:
2196 area = LEFT_MARGIN_AREA;
2197 goto text_glyph;
2198
2199 case ON_RIGHT_MARGIN:
2200 area = RIGHT_MARGIN_AREA;
2201 goto text_glyph;
2202
2203 case ON_HEADER_LINE:
2204 case ON_MODE_LINE:
2205 gr = (part == ON_HEADER_LINE
2206 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2207 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2208 gy = gr->y;
2209 area = TEXT_AREA;
2210 goto text_glyph_row_found;
2211
2212 case ON_TEXT:
2213 area = TEXT_AREA;
2214
2215 text_glyph:
2216 gr = 0; gy = 0;
2217 for (; r <= end_row && r->enabled_p; ++r)
2218 if (r->y + r->height > y)
2219 {
2220 gr = r; gy = r->y;
2221 break;
2222 }
2223
2224 text_glyph_row_found:
2225 if (gr && gy <= y)
2226 {
2227 struct glyph *g = gr->glyphs[area];
2228 struct glyph *end = g + gr->used[area];
2229
2230 height = gr->height;
2231 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2232 if (gx + g->pixel_width > x)
2233 break;
2234
2235 if (g < end)
2236 {
2237 if (g->type == IMAGE_GLYPH)
2238 {
2239 /* Don't remember when mouse is over image, as
2240 image may have hot-spots. */
2241 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2242 return;
2243 }
2244 width = g->pixel_width;
2245 }
2246 else
2247 {
2248 /* Use nominal char spacing at end of line. */
2249 x -= gx;
2250 gx += (x / width) * width;
2251 }
2252
2253 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2254 gx += window_box_left_offset (w, area);
2255 }
2256 else
2257 {
2258 /* Use nominal line height at end of window. */
2259 gx = (x / width) * width;
2260 y -= gy;
2261 gy += (y / height) * height;
2262 }
2263 break;
2264
2265 case ON_LEFT_FRINGE:
2266 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2267 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2268 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2269 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2270 goto row_glyph;
2271
2272 case ON_RIGHT_FRINGE:
2273 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2274 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2275 : window_box_right_offset (w, TEXT_AREA));
2276 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2277 goto row_glyph;
2278
2279 case ON_SCROLL_BAR:
2280 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2281 ? 0
2282 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2283 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2284 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2285 : 0)));
2286 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2287
2288 row_glyph:
2289 gr = 0, gy = 0;
2290 for (; r <= end_row && r->enabled_p; ++r)
2291 if (r->y + r->height > y)
2292 {
2293 gr = r; gy = r->y;
2294 break;
2295 }
2296
2297 if (gr && gy <= y)
2298 height = gr->height;
2299 else
2300 {
2301 /* Use nominal line height at end of window. */
2302 y -= gy;
2303 gy += (y / height) * height;
2304 }
2305 break;
2306
2307 default:
2308 ;
2309 virtual_glyph:
2310 /* If there is no glyph under the mouse, then we divide the screen
2311 into a grid of the smallest glyph in the frame, and use that
2312 as our "glyph". */
2313
2314 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2315 round down even for negative values. */
2316 if (gx < 0)
2317 gx -= width - 1;
2318 if (gy < 0)
2319 gy -= height - 1;
2320
2321 gx = (gx / width) * width;
2322 gy = (gy / height) * height;
2323
2324 goto store_rect;
2325 }
2326
2327 gx += WINDOW_LEFT_EDGE_X (w);
2328 gy += WINDOW_TOP_EDGE_Y (w);
2329
2330 store_rect:
2331 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2332
2333 /* Visible feedback for debugging. */
2334 #if 0
2335 #if HAVE_X_WINDOWS
2336 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2337 f->output_data.x->normal_gc,
2338 gx, gy, width, height);
2339 #endif
2340 #endif
2341 }
2342
2343
2344 #endif /* HAVE_WINDOW_SYSTEM */
2345
2346 \f
2347 /***********************************************************************
2348 Lisp form evaluation
2349 ***********************************************************************/
2350
2351 /* Error handler for safe_eval and safe_call. */
2352
2353 static Lisp_Object
2354 safe_eval_handler (Lisp_Object arg)
2355 {
2356 add_to_log ("Error during redisplay: %s", arg, Qnil);
2357 return Qnil;
2358 }
2359
2360
2361 /* Evaluate SEXPR and return the result, or nil if something went
2362 wrong. Prevent redisplay during the evaluation. */
2363
2364 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2365 Return the result, or nil if something went wrong. Prevent
2366 redisplay during the evaluation. */
2367
2368 Lisp_Object
2369 safe_call (int nargs, Lisp_Object *args)
2370 {
2371 Lisp_Object val;
2372
2373 if (inhibit_eval_during_redisplay)
2374 val = Qnil;
2375 else
2376 {
2377 int count = SPECPDL_INDEX ();
2378 struct gcpro gcpro1;
2379
2380 GCPRO1 (args[0]);
2381 gcpro1.nvars = nargs;
2382 specbind (Qinhibit_redisplay, Qt);
2383 /* Use Qt to ensure debugger does not run,
2384 so there is no possibility of wanting to redisplay. */
2385 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2386 safe_eval_handler);
2387 UNGCPRO;
2388 val = unbind_to (count, val);
2389 }
2390
2391 return val;
2392 }
2393
2394
2395 /* Call function FN with one argument ARG.
2396 Return the result, or nil if something went wrong. */
2397
2398 Lisp_Object
2399 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2400 {
2401 Lisp_Object args[2];
2402 args[0] = fn;
2403 args[1] = arg;
2404 return safe_call (2, args);
2405 }
2406
2407 static Lisp_Object Qeval;
2408
2409 Lisp_Object
2410 safe_eval (Lisp_Object sexpr)
2411 {
2412 return safe_call1 (Qeval, sexpr);
2413 }
2414
2415 /* Call function FN with one argument ARG.
2416 Return the result, or nil if something went wrong. */
2417
2418 Lisp_Object
2419 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2420 {
2421 Lisp_Object args[3];
2422 args[0] = fn;
2423 args[1] = arg1;
2424 args[2] = arg2;
2425 return safe_call (3, args);
2426 }
2427
2428
2429 \f
2430 /***********************************************************************
2431 Debugging
2432 ***********************************************************************/
2433
2434 #if 0
2435
2436 /* Define CHECK_IT to perform sanity checks on iterators.
2437 This is for debugging. It is too slow to do unconditionally. */
2438
2439 static void
2440 check_it (it)
2441 struct it *it;
2442 {
2443 if (it->method == GET_FROM_STRING)
2444 {
2445 xassert (STRINGP (it->string));
2446 xassert (IT_STRING_CHARPOS (*it) >= 0);
2447 }
2448 else
2449 {
2450 xassert (IT_STRING_CHARPOS (*it) < 0);
2451 if (it->method == GET_FROM_BUFFER)
2452 {
2453 /* Check that character and byte positions agree. */
2454 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2455 }
2456 }
2457
2458 if (it->dpvec)
2459 xassert (it->current.dpvec_index >= 0);
2460 else
2461 xassert (it->current.dpvec_index < 0);
2462 }
2463
2464 #define CHECK_IT(IT) check_it ((IT))
2465
2466 #else /* not 0 */
2467
2468 #define CHECK_IT(IT) (void) 0
2469
2470 #endif /* not 0 */
2471
2472
2473 #if GLYPH_DEBUG
2474
2475 /* Check that the window end of window W is what we expect it
2476 to be---the last row in the current matrix displaying text. */
2477
2478 static void
2479 check_window_end (w)
2480 struct window *w;
2481 {
2482 if (!MINI_WINDOW_P (w)
2483 && !NILP (w->window_end_valid))
2484 {
2485 struct glyph_row *row;
2486 xassert ((row = MATRIX_ROW (w->current_matrix,
2487 XFASTINT (w->window_end_vpos)),
2488 !row->enabled_p
2489 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2490 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2491 }
2492 }
2493
2494 #define CHECK_WINDOW_END(W) check_window_end ((W))
2495
2496 #else /* not GLYPH_DEBUG */
2497
2498 #define CHECK_WINDOW_END(W) (void) 0
2499
2500 #endif /* not GLYPH_DEBUG */
2501
2502
2503 \f
2504 /***********************************************************************
2505 Iterator initialization
2506 ***********************************************************************/
2507
2508 /* Initialize IT for displaying current_buffer in window W, starting
2509 at character position CHARPOS. CHARPOS < 0 means that no buffer
2510 position is specified which is useful when the iterator is assigned
2511 a position later. BYTEPOS is the byte position corresponding to
2512 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2513
2514 If ROW is not null, calls to produce_glyphs with IT as parameter
2515 will produce glyphs in that row.
2516
2517 BASE_FACE_ID is the id of a base face to use. It must be one of
2518 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2519 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2520 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2521
2522 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2523 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2524 will be initialized to use the corresponding mode line glyph row of
2525 the desired matrix of W. */
2526
2527 void
2528 init_iterator (struct it *it, struct window *w,
2529 EMACS_INT charpos, EMACS_INT bytepos,
2530 struct glyph_row *row, enum face_id base_face_id)
2531 {
2532 int highlight_region_p;
2533 enum face_id remapped_base_face_id = base_face_id;
2534
2535 /* Some precondition checks. */
2536 xassert (w != NULL && it != NULL);
2537 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2538 && charpos <= ZV));
2539
2540 /* If face attributes have been changed since the last redisplay,
2541 free realized faces now because they depend on face definitions
2542 that might have changed. Don't free faces while there might be
2543 desired matrices pending which reference these faces. */
2544 if (face_change_count && !inhibit_free_realized_faces)
2545 {
2546 face_change_count = 0;
2547 free_all_realized_faces (Qnil);
2548 }
2549
2550 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2551 if (! NILP (Vface_remapping_alist))
2552 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2553
2554 /* Use one of the mode line rows of W's desired matrix if
2555 appropriate. */
2556 if (row == NULL)
2557 {
2558 if (base_face_id == MODE_LINE_FACE_ID
2559 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2560 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2561 else if (base_face_id == HEADER_LINE_FACE_ID)
2562 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2563 }
2564
2565 /* Clear IT. */
2566 memset (it, 0, sizeof *it);
2567 it->current.overlay_string_index = -1;
2568 it->current.dpvec_index = -1;
2569 it->base_face_id = remapped_base_face_id;
2570 it->string = Qnil;
2571 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2572
2573 /* The window in which we iterate over current_buffer: */
2574 XSETWINDOW (it->window, w);
2575 it->w = w;
2576 it->f = XFRAME (w->frame);
2577
2578 it->cmp_it.id = -1;
2579
2580 /* Extra space between lines (on window systems only). */
2581 if (base_face_id == DEFAULT_FACE_ID
2582 && FRAME_WINDOW_P (it->f))
2583 {
2584 if (NATNUMP (current_buffer->extra_line_spacing))
2585 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2586 else if (FLOATP (current_buffer->extra_line_spacing))
2587 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2588 * FRAME_LINE_HEIGHT (it->f));
2589 else if (it->f->extra_line_spacing > 0)
2590 it->extra_line_spacing = it->f->extra_line_spacing;
2591 it->max_extra_line_spacing = 0;
2592 }
2593
2594 /* If realized faces have been removed, e.g. because of face
2595 attribute changes of named faces, recompute them. When running
2596 in batch mode, the face cache of the initial frame is null. If
2597 we happen to get called, make a dummy face cache. */
2598 if (FRAME_FACE_CACHE (it->f) == NULL)
2599 init_frame_faces (it->f);
2600 if (FRAME_FACE_CACHE (it->f)->used == 0)
2601 recompute_basic_faces (it->f);
2602
2603 /* Current value of the `slice', `space-width', and 'height' properties. */
2604 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2605 it->space_width = Qnil;
2606 it->font_height = Qnil;
2607 it->override_ascent = -1;
2608
2609 /* Are control characters displayed as `^C'? */
2610 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2611
2612 /* -1 means everything between a CR and the following line end
2613 is invisible. >0 means lines indented more than this value are
2614 invisible. */
2615 it->selective = (INTEGERP (current_buffer->selective_display)
2616 ? XFASTINT (current_buffer->selective_display)
2617 : (!NILP (current_buffer->selective_display)
2618 ? -1 : 0));
2619 it->selective_display_ellipsis_p
2620 = !NILP (current_buffer->selective_display_ellipses);
2621
2622 /* Display table to use. */
2623 it->dp = window_display_table (w);
2624
2625 /* Are multibyte characters enabled in current_buffer? */
2626 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2627
2628 /* Do we need to reorder bidirectional text? Not if this is a
2629 unibyte buffer: by definition, none of the single-byte characters
2630 are strong R2L, so no reordering is needed. And bidi.c doesn't
2631 support unibyte buffers anyway. */
2632 it->bidi_p
2633 = !NILP (current_buffer->bidi_display_reordering) && it->multibyte_p;
2634
2635 /* Non-zero if we should highlight the region. */
2636 highlight_region_p
2637 = (!NILP (Vtransient_mark_mode)
2638 && !NILP (current_buffer->mark_active)
2639 && XMARKER (current_buffer->mark)->buffer != 0);
2640
2641 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2642 start and end of a visible region in window IT->w. Set both to
2643 -1 to indicate no region. */
2644 if (highlight_region_p
2645 /* Maybe highlight only in selected window. */
2646 && (/* Either show region everywhere. */
2647 highlight_nonselected_windows
2648 /* Or show region in the selected window. */
2649 || w == XWINDOW (selected_window)
2650 /* Or show the region if we are in the mini-buffer and W is
2651 the window the mini-buffer refers to. */
2652 || (MINI_WINDOW_P (XWINDOW (selected_window))
2653 && WINDOWP (minibuf_selected_window)
2654 && w == XWINDOW (minibuf_selected_window))))
2655 {
2656 int charpos = marker_position (current_buffer->mark);
2657 it->region_beg_charpos = min (PT, charpos);
2658 it->region_end_charpos = max (PT, charpos);
2659 }
2660 else
2661 it->region_beg_charpos = it->region_end_charpos = -1;
2662
2663 /* Get the position at which the redisplay_end_trigger hook should
2664 be run, if it is to be run at all. */
2665 if (MARKERP (w->redisplay_end_trigger)
2666 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2667 it->redisplay_end_trigger_charpos
2668 = marker_position (w->redisplay_end_trigger);
2669 else if (INTEGERP (w->redisplay_end_trigger))
2670 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2671
2672 /* Correct bogus values of tab_width. */
2673 it->tab_width = XINT (current_buffer->tab_width);
2674 if (it->tab_width <= 0 || it->tab_width > 1000)
2675 it->tab_width = 8;
2676
2677 /* Are lines in the display truncated? */
2678 if (base_face_id != DEFAULT_FACE_ID
2679 || XINT (it->w->hscroll)
2680 || (! WINDOW_FULL_WIDTH_P (it->w)
2681 && ((!NILP (Vtruncate_partial_width_windows)
2682 && !INTEGERP (Vtruncate_partial_width_windows))
2683 || (INTEGERP (Vtruncate_partial_width_windows)
2684 && (WINDOW_TOTAL_COLS (it->w)
2685 < XINT (Vtruncate_partial_width_windows))))))
2686 it->line_wrap = TRUNCATE;
2687 else if (NILP (current_buffer->truncate_lines))
2688 it->line_wrap = NILP (current_buffer->word_wrap)
2689 ? WINDOW_WRAP : WORD_WRAP;
2690 else
2691 it->line_wrap = TRUNCATE;
2692
2693 /* Get dimensions of truncation and continuation glyphs. These are
2694 displayed as fringe bitmaps under X, so we don't need them for such
2695 frames. */
2696 if (!FRAME_WINDOW_P (it->f))
2697 {
2698 if (it->line_wrap == TRUNCATE)
2699 {
2700 /* We will need the truncation glyph. */
2701 xassert (it->glyph_row == NULL);
2702 produce_special_glyphs (it, IT_TRUNCATION);
2703 it->truncation_pixel_width = it->pixel_width;
2704 }
2705 else
2706 {
2707 /* We will need the continuation glyph. */
2708 xassert (it->glyph_row == NULL);
2709 produce_special_glyphs (it, IT_CONTINUATION);
2710 it->continuation_pixel_width = it->pixel_width;
2711 }
2712
2713 /* Reset these values to zero because the produce_special_glyphs
2714 above has changed them. */
2715 it->pixel_width = it->ascent = it->descent = 0;
2716 it->phys_ascent = it->phys_descent = 0;
2717 }
2718
2719 /* Set this after getting the dimensions of truncation and
2720 continuation glyphs, so that we don't produce glyphs when calling
2721 produce_special_glyphs, above. */
2722 it->glyph_row = row;
2723 it->area = TEXT_AREA;
2724
2725 /* Forget any previous info about this row being reversed. */
2726 if (it->glyph_row)
2727 it->glyph_row->reversed_p = 0;
2728
2729 /* Get the dimensions of the display area. The display area
2730 consists of the visible window area plus a horizontally scrolled
2731 part to the left of the window. All x-values are relative to the
2732 start of this total display area. */
2733 if (base_face_id != DEFAULT_FACE_ID)
2734 {
2735 /* Mode lines, menu bar in terminal frames. */
2736 it->first_visible_x = 0;
2737 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2738 }
2739 else
2740 {
2741 it->first_visible_x
2742 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2743 it->last_visible_x = (it->first_visible_x
2744 + window_box_width (w, TEXT_AREA));
2745
2746 /* If we truncate lines, leave room for the truncator glyph(s) at
2747 the right margin. Otherwise, leave room for the continuation
2748 glyph(s). Truncation and continuation glyphs are not inserted
2749 for window-based redisplay. */
2750 if (!FRAME_WINDOW_P (it->f))
2751 {
2752 if (it->line_wrap == TRUNCATE)
2753 it->last_visible_x -= it->truncation_pixel_width;
2754 else
2755 it->last_visible_x -= it->continuation_pixel_width;
2756 }
2757
2758 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2759 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2760 }
2761
2762 /* Leave room for a border glyph. */
2763 if (!FRAME_WINDOW_P (it->f)
2764 && !WINDOW_RIGHTMOST_P (it->w))
2765 it->last_visible_x -= 1;
2766
2767 it->last_visible_y = window_text_bottom_y (w);
2768
2769 /* For mode lines and alike, arrange for the first glyph having a
2770 left box line if the face specifies a box. */
2771 if (base_face_id != DEFAULT_FACE_ID)
2772 {
2773 struct face *face;
2774
2775 it->face_id = remapped_base_face_id;
2776
2777 /* If we have a boxed mode line, make the first character appear
2778 with a left box line. */
2779 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2780 if (face->box != FACE_NO_BOX)
2781 it->start_of_box_run_p = 1;
2782 }
2783
2784 /* If we are to reorder bidirectional text, init the bidi
2785 iterator. */
2786 if (it->bidi_p)
2787 {
2788 /* Note the paragraph direction that this buffer wants to
2789 use. */
2790 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2791 it->paragraph_embedding = L2R;
2792 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2793 it->paragraph_embedding = R2L;
2794 else
2795 it->paragraph_embedding = NEUTRAL_DIR;
2796 bidi_init_it (charpos, bytepos, &it->bidi_it);
2797 }
2798
2799 /* If a buffer position was specified, set the iterator there,
2800 getting overlays and face properties from that position. */
2801 if (charpos >= BUF_BEG (current_buffer))
2802 {
2803 it->end_charpos = ZV;
2804 it->face_id = -1;
2805 IT_CHARPOS (*it) = charpos;
2806
2807 /* Compute byte position if not specified. */
2808 if (bytepos < charpos)
2809 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2810 else
2811 IT_BYTEPOS (*it) = bytepos;
2812
2813 it->start = it->current;
2814
2815 /* Compute faces etc. */
2816 reseat (it, it->current.pos, 1);
2817 }
2818
2819 CHECK_IT (it);
2820 }
2821
2822
2823 /* Initialize IT for the display of window W with window start POS. */
2824
2825 void
2826 start_display (struct it *it, struct window *w, struct text_pos pos)
2827 {
2828 struct glyph_row *row;
2829 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2830
2831 row = w->desired_matrix->rows + first_vpos;
2832 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2833 it->first_vpos = first_vpos;
2834
2835 /* Don't reseat to previous visible line start if current start
2836 position is in a string or image. */
2837 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2838 {
2839 int start_at_line_beg_p;
2840 int first_y = it->current_y;
2841
2842 /* If window start is not at a line start, skip forward to POS to
2843 get the correct continuation lines width. */
2844 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2845 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2846 if (!start_at_line_beg_p)
2847 {
2848 int new_x;
2849
2850 reseat_at_previous_visible_line_start (it);
2851 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2852
2853 new_x = it->current_x + it->pixel_width;
2854
2855 /* If lines are continued, this line may end in the middle
2856 of a multi-glyph character (e.g. a control character
2857 displayed as \003, or in the middle of an overlay
2858 string). In this case move_it_to above will not have
2859 taken us to the start of the continuation line but to the
2860 end of the continued line. */
2861 if (it->current_x > 0
2862 && it->line_wrap != TRUNCATE /* Lines are continued. */
2863 && (/* And glyph doesn't fit on the line. */
2864 new_x > it->last_visible_x
2865 /* Or it fits exactly and we're on a window
2866 system frame. */
2867 || (new_x == it->last_visible_x
2868 && FRAME_WINDOW_P (it->f))))
2869 {
2870 if (it->current.dpvec_index >= 0
2871 || it->current.overlay_string_index >= 0)
2872 {
2873 set_iterator_to_next (it, 1);
2874 move_it_in_display_line_to (it, -1, -1, 0);
2875 }
2876
2877 it->continuation_lines_width += it->current_x;
2878 }
2879
2880 /* We're starting a new display line, not affected by the
2881 height of the continued line, so clear the appropriate
2882 fields in the iterator structure. */
2883 it->max_ascent = it->max_descent = 0;
2884 it->max_phys_ascent = it->max_phys_descent = 0;
2885
2886 it->current_y = first_y;
2887 it->vpos = 0;
2888 it->current_x = it->hpos = 0;
2889 }
2890 }
2891 }
2892
2893
2894 /* Return 1 if POS is a position in ellipses displayed for invisible
2895 text. W is the window we display, for text property lookup. */
2896
2897 static int
2898 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2899 {
2900 Lisp_Object prop, window;
2901 int ellipses_p = 0;
2902 int charpos = CHARPOS (pos->pos);
2903
2904 /* If POS specifies a position in a display vector, this might
2905 be for an ellipsis displayed for invisible text. We won't
2906 get the iterator set up for delivering that ellipsis unless
2907 we make sure that it gets aware of the invisible text. */
2908 if (pos->dpvec_index >= 0
2909 && pos->overlay_string_index < 0
2910 && CHARPOS (pos->string_pos) < 0
2911 && charpos > BEGV
2912 && (XSETWINDOW (window, w),
2913 prop = Fget_char_property (make_number (charpos),
2914 Qinvisible, window),
2915 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2916 {
2917 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2918 window);
2919 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2920 }
2921
2922 return ellipses_p;
2923 }
2924
2925
2926 /* Initialize IT for stepping through current_buffer in window W,
2927 starting at position POS that includes overlay string and display
2928 vector/ control character translation position information. Value
2929 is zero if there are overlay strings with newlines at POS. */
2930
2931 static int
2932 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2933 {
2934 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2935 int i, overlay_strings_with_newlines = 0;
2936
2937 /* If POS specifies a position in a display vector, this might
2938 be for an ellipsis displayed for invisible text. We won't
2939 get the iterator set up for delivering that ellipsis unless
2940 we make sure that it gets aware of the invisible text. */
2941 if (in_ellipses_for_invisible_text_p (pos, w))
2942 {
2943 --charpos;
2944 bytepos = 0;
2945 }
2946
2947 /* Keep in mind: the call to reseat in init_iterator skips invisible
2948 text, so we might end up at a position different from POS. This
2949 is only a problem when POS is a row start after a newline and an
2950 overlay starts there with an after-string, and the overlay has an
2951 invisible property. Since we don't skip invisible text in
2952 display_line and elsewhere immediately after consuming the
2953 newline before the row start, such a POS will not be in a string,
2954 but the call to init_iterator below will move us to the
2955 after-string. */
2956 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2957
2958 /* This only scans the current chunk -- it should scan all chunks.
2959 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2960 to 16 in 22.1 to make this a lesser problem. */
2961 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2962 {
2963 const char *s = SDATA (it->overlay_strings[i]);
2964 const char *e = s + SBYTES (it->overlay_strings[i]);
2965
2966 while (s < e && *s != '\n')
2967 ++s;
2968
2969 if (s < e)
2970 {
2971 overlay_strings_with_newlines = 1;
2972 break;
2973 }
2974 }
2975
2976 /* If position is within an overlay string, set up IT to the right
2977 overlay string. */
2978 if (pos->overlay_string_index >= 0)
2979 {
2980 int relative_index;
2981
2982 /* If the first overlay string happens to have a `display'
2983 property for an image, the iterator will be set up for that
2984 image, and we have to undo that setup first before we can
2985 correct the overlay string index. */
2986 if (it->method == GET_FROM_IMAGE)
2987 pop_it (it);
2988
2989 /* We already have the first chunk of overlay strings in
2990 IT->overlay_strings. Load more until the one for
2991 pos->overlay_string_index is in IT->overlay_strings. */
2992 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2993 {
2994 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2995 it->current.overlay_string_index = 0;
2996 while (n--)
2997 {
2998 load_overlay_strings (it, 0);
2999 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3000 }
3001 }
3002
3003 it->current.overlay_string_index = pos->overlay_string_index;
3004 relative_index = (it->current.overlay_string_index
3005 % OVERLAY_STRING_CHUNK_SIZE);
3006 it->string = it->overlay_strings[relative_index];
3007 xassert (STRINGP (it->string));
3008 it->current.string_pos = pos->string_pos;
3009 it->method = GET_FROM_STRING;
3010 }
3011
3012 if (CHARPOS (pos->string_pos) >= 0)
3013 {
3014 /* Recorded position is not in an overlay string, but in another
3015 string. This can only be a string from a `display' property.
3016 IT should already be filled with that string. */
3017 it->current.string_pos = pos->string_pos;
3018 xassert (STRINGP (it->string));
3019 }
3020
3021 /* Restore position in display vector translations, control
3022 character translations or ellipses. */
3023 if (pos->dpvec_index >= 0)
3024 {
3025 if (it->dpvec == NULL)
3026 get_next_display_element (it);
3027 xassert (it->dpvec && it->current.dpvec_index == 0);
3028 it->current.dpvec_index = pos->dpvec_index;
3029 }
3030
3031 CHECK_IT (it);
3032 return !overlay_strings_with_newlines;
3033 }
3034
3035
3036 /* Initialize IT for stepping through current_buffer in window W
3037 starting at ROW->start. */
3038
3039 static void
3040 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3041 {
3042 init_from_display_pos (it, w, &row->start);
3043 it->start = row->start;
3044 it->continuation_lines_width = row->continuation_lines_width;
3045 CHECK_IT (it);
3046 }
3047
3048
3049 /* Initialize IT for stepping through current_buffer in window W
3050 starting in the line following ROW, i.e. starting at ROW->end.
3051 Value is zero if there are overlay strings with newlines at ROW's
3052 end position. */
3053
3054 static int
3055 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3056 {
3057 int success = 0;
3058
3059 if (init_from_display_pos (it, w, &row->end))
3060 {
3061 if (row->continued_p)
3062 it->continuation_lines_width
3063 = row->continuation_lines_width + row->pixel_width;
3064 CHECK_IT (it);
3065 success = 1;
3066 }
3067
3068 return success;
3069 }
3070
3071
3072
3073 \f
3074 /***********************************************************************
3075 Text properties
3076 ***********************************************************************/
3077
3078 /* Called when IT reaches IT->stop_charpos. Handle text property and
3079 overlay changes. Set IT->stop_charpos to the next position where
3080 to stop. */
3081
3082 static void
3083 handle_stop (struct it *it)
3084 {
3085 enum prop_handled handled;
3086 int handle_overlay_change_p;
3087 struct props *p;
3088
3089 it->dpvec = NULL;
3090 it->current.dpvec_index = -1;
3091 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3092 it->ignore_overlay_strings_at_pos_p = 0;
3093 it->ellipsis_p = 0;
3094
3095 /* Use face of preceding text for ellipsis (if invisible) */
3096 if (it->selective_display_ellipsis_p)
3097 it->saved_face_id = it->face_id;
3098
3099 do
3100 {
3101 handled = HANDLED_NORMALLY;
3102
3103 /* Call text property handlers. */
3104 for (p = it_props; p->handler; ++p)
3105 {
3106 handled = p->handler (it);
3107
3108 if (handled == HANDLED_RECOMPUTE_PROPS)
3109 break;
3110 else if (handled == HANDLED_RETURN)
3111 {
3112 /* We still want to show before and after strings from
3113 overlays even if the actual buffer text is replaced. */
3114 if (!handle_overlay_change_p
3115 || it->sp > 1
3116 || !get_overlay_strings_1 (it, 0, 0))
3117 {
3118 if (it->ellipsis_p)
3119 setup_for_ellipsis (it, 0);
3120 /* When handling a display spec, we might load an
3121 empty string. In that case, discard it here. We
3122 used to discard it in handle_single_display_spec,
3123 but that causes get_overlay_strings_1, above, to
3124 ignore overlay strings that we must check. */
3125 if (STRINGP (it->string) && !SCHARS (it->string))
3126 pop_it (it);
3127 return;
3128 }
3129 else if (STRINGP (it->string) && !SCHARS (it->string))
3130 pop_it (it);
3131 else
3132 {
3133 it->ignore_overlay_strings_at_pos_p = 1;
3134 it->string_from_display_prop_p = 0;
3135 handle_overlay_change_p = 0;
3136 }
3137 handled = HANDLED_RECOMPUTE_PROPS;
3138 break;
3139 }
3140 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3141 handle_overlay_change_p = 0;
3142 }
3143
3144 if (handled != HANDLED_RECOMPUTE_PROPS)
3145 {
3146 /* Don't check for overlay strings below when set to deliver
3147 characters from a display vector. */
3148 if (it->method == GET_FROM_DISPLAY_VECTOR)
3149 handle_overlay_change_p = 0;
3150
3151 /* Handle overlay changes.
3152 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3153 if it finds overlays. */
3154 if (handle_overlay_change_p)
3155 handled = handle_overlay_change (it);
3156 }
3157
3158 if (it->ellipsis_p)
3159 {
3160 setup_for_ellipsis (it, 0);
3161 break;
3162 }
3163 }
3164 while (handled == HANDLED_RECOMPUTE_PROPS);
3165
3166 /* Determine where to stop next. */
3167 if (handled == HANDLED_NORMALLY)
3168 compute_stop_pos (it);
3169 }
3170
3171
3172 /* Compute IT->stop_charpos from text property and overlay change
3173 information for IT's current position. */
3174
3175 static void
3176 compute_stop_pos (struct it *it)
3177 {
3178 register INTERVAL iv, next_iv;
3179 Lisp_Object object, limit, position;
3180 EMACS_INT charpos, bytepos;
3181
3182 /* If nowhere else, stop at the end. */
3183 it->stop_charpos = it->end_charpos;
3184
3185 if (STRINGP (it->string))
3186 {
3187 /* Strings are usually short, so don't limit the search for
3188 properties. */
3189 object = it->string;
3190 limit = Qnil;
3191 charpos = IT_STRING_CHARPOS (*it);
3192 bytepos = IT_STRING_BYTEPOS (*it);
3193 }
3194 else
3195 {
3196 EMACS_INT pos;
3197
3198 /* If next overlay change is in front of the current stop pos
3199 (which is IT->end_charpos), stop there. Note: value of
3200 next_overlay_change is point-max if no overlay change
3201 follows. */
3202 charpos = IT_CHARPOS (*it);
3203 bytepos = IT_BYTEPOS (*it);
3204 pos = next_overlay_change (charpos);
3205 if (pos < it->stop_charpos)
3206 it->stop_charpos = pos;
3207
3208 /* If showing the region, we have to stop at the region
3209 start or end because the face might change there. */
3210 if (it->region_beg_charpos > 0)
3211 {
3212 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3213 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3214 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3215 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3216 }
3217
3218 /* Set up variables for computing the stop position from text
3219 property changes. */
3220 XSETBUFFER (object, current_buffer);
3221 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3222 }
3223
3224 /* Get the interval containing IT's position. Value is a null
3225 interval if there isn't such an interval. */
3226 position = make_number (charpos);
3227 iv = validate_interval_range (object, &position, &position, 0);
3228 if (!NULL_INTERVAL_P (iv))
3229 {
3230 Lisp_Object values_here[LAST_PROP_IDX];
3231 struct props *p;
3232
3233 /* Get properties here. */
3234 for (p = it_props; p->handler; ++p)
3235 values_here[p->idx] = textget (iv->plist, *p->name);
3236
3237 /* Look for an interval following iv that has different
3238 properties. */
3239 for (next_iv = next_interval (iv);
3240 (!NULL_INTERVAL_P (next_iv)
3241 && (NILP (limit)
3242 || XFASTINT (limit) > next_iv->position));
3243 next_iv = next_interval (next_iv))
3244 {
3245 for (p = it_props; p->handler; ++p)
3246 {
3247 Lisp_Object new_value;
3248
3249 new_value = textget (next_iv->plist, *p->name);
3250 if (!EQ (values_here[p->idx], new_value))
3251 break;
3252 }
3253
3254 if (p->handler)
3255 break;
3256 }
3257
3258 if (!NULL_INTERVAL_P (next_iv))
3259 {
3260 if (INTEGERP (limit)
3261 && next_iv->position >= XFASTINT (limit))
3262 /* No text property change up to limit. */
3263 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3264 else
3265 /* Text properties change in next_iv. */
3266 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3267 }
3268 }
3269
3270 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3271 it->stop_charpos, it->string);
3272
3273 xassert (STRINGP (it->string)
3274 || (it->stop_charpos >= BEGV
3275 && it->stop_charpos >= IT_CHARPOS (*it)));
3276 }
3277
3278
3279 /* Return the position of the next overlay change after POS in
3280 current_buffer. Value is point-max if no overlay change
3281 follows. This is like `next-overlay-change' but doesn't use
3282 xmalloc. */
3283
3284 static EMACS_INT
3285 next_overlay_change (EMACS_INT pos)
3286 {
3287 int noverlays;
3288 EMACS_INT endpos;
3289 Lisp_Object *overlays;
3290 int i;
3291
3292 /* Get all overlays at the given position. */
3293 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3294
3295 /* If any of these overlays ends before endpos,
3296 use its ending point instead. */
3297 for (i = 0; i < noverlays; ++i)
3298 {
3299 Lisp_Object oend;
3300 EMACS_INT oendpos;
3301
3302 oend = OVERLAY_END (overlays[i]);
3303 oendpos = OVERLAY_POSITION (oend);
3304 endpos = min (endpos, oendpos);
3305 }
3306
3307 return endpos;
3308 }
3309
3310
3311 \f
3312 /***********************************************************************
3313 Fontification
3314 ***********************************************************************/
3315
3316 /* Handle changes in the `fontified' property of the current buffer by
3317 calling hook functions from Qfontification_functions to fontify
3318 regions of text. */
3319
3320 static enum prop_handled
3321 handle_fontified_prop (struct it *it)
3322 {
3323 Lisp_Object prop, pos;
3324 enum prop_handled handled = HANDLED_NORMALLY;
3325
3326 if (!NILP (Vmemory_full))
3327 return handled;
3328
3329 /* Get the value of the `fontified' property at IT's current buffer
3330 position. (The `fontified' property doesn't have a special
3331 meaning in strings.) If the value is nil, call functions from
3332 Qfontification_functions. */
3333 if (!STRINGP (it->string)
3334 && it->s == NULL
3335 && !NILP (Vfontification_functions)
3336 && !NILP (Vrun_hooks)
3337 && (pos = make_number (IT_CHARPOS (*it)),
3338 prop = Fget_char_property (pos, Qfontified, Qnil),
3339 /* Ignore the special cased nil value always present at EOB since
3340 no amount of fontifying will be able to change it. */
3341 NILP (prop) && IT_CHARPOS (*it) < Z))
3342 {
3343 int count = SPECPDL_INDEX ();
3344 Lisp_Object val;
3345
3346 val = Vfontification_functions;
3347 specbind (Qfontification_functions, Qnil);
3348
3349 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3350 safe_call1 (val, pos);
3351 else
3352 {
3353 Lisp_Object globals, fn;
3354 struct gcpro gcpro1, gcpro2;
3355
3356 globals = Qnil;
3357 GCPRO2 (val, globals);
3358
3359 for (; CONSP (val); val = XCDR (val))
3360 {
3361 fn = XCAR (val);
3362
3363 if (EQ (fn, Qt))
3364 {
3365 /* A value of t indicates this hook has a local
3366 binding; it means to run the global binding too.
3367 In a global value, t should not occur. If it
3368 does, we must ignore it to avoid an endless
3369 loop. */
3370 for (globals = Fdefault_value (Qfontification_functions);
3371 CONSP (globals);
3372 globals = XCDR (globals))
3373 {
3374 fn = XCAR (globals);
3375 if (!EQ (fn, Qt))
3376 safe_call1 (fn, pos);
3377 }
3378 }
3379 else
3380 safe_call1 (fn, pos);
3381 }
3382
3383 UNGCPRO;
3384 }
3385
3386 unbind_to (count, Qnil);
3387
3388 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3389 something. This avoids an endless loop if they failed to
3390 fontify the text for which reason ever. */
3391 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3392 handled = HANDLED_RECOMPUTE_PROPS;
3393 }
3394
3395 return handled;
3396 }
3397
3398
3399 \f
3400 /***********************************************************************
3401 Faces
3402 ***********************************************************************/
3403
3404 /* Set up iterator IT from face properties at its current position.
3405 Called from handle_stop. */
3406
3407 static enum prop_handled
3408 handle_face_prop (struct it *it)
3409 {
3410 int new_face_id;
3411 EMACS_INT next_stop;
3412
3413 if (!STRINGP (it->string))
3414 {
3415 new_face_id
3416 = face_at_buffer_position (it->w,
3417 IT_CHARPOS (*it),
3418 it->region_beg_charpos,
3419 it->region_end_charpos,
3420 &next_stop,
3421 (IT_CHARPOS (*it)
3422 + TEXT_PROP_DISTANCE_LIMIT),
3423 0, it->base_face_id);
3424
3425 /* Is this a start of a run of characters with box face?
3426 Caveat: this can be called for a freshly initialized
3427 iterator; face_id is -1 in this case. We know that the new
3428 face will not change until limit, i.e. if the new face has a
3429 box, all characters up to limit will have one. But, as
3430 usual, we don't know whether limit is really the end. */
3431 if (new_face_id != it->face_id)
3432 {
3433 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3434
3435 /* If new face has a box but old face has not, this is
3436 the start of a run of characters with box, i.e. it has
3437 a shadow on the left side. The value of face_id of the
3438 iterator will be -1 if this is the initial call that gets
3439 the face. In this case, we have to look in front of IT's
3440 position and see whether there is a face != new_face_id. */
3441 it->start_of_box_run_p
3442 = (new_face->box != FACE_NO_BOX
3443 && (it->face_id >= 0
3444 || IT_CHARPOS (*it) == BEG
3445 || new_face_id != face_before_it_pos (it)));
3446 it->face_box_p = new_face->box != FACE_NO_BOX;
3447 }
3448 }
3449 else
3450 {
3451 int base_face_id, bufpos;
3452 int i;
3453 Lisp_Object from_overlay
3454 = (it->current.overlay_string_index >= 0
3455 ? it->string_overlays[it->current.overlay_string_index]
3456 : Qnil);
3457
3458 /* See if we got to this string directly or indirectly from
3459 an overlay property. That includes the before-string or
3460 after-string of an overlay, strings in display properties
3461 provided by an overlay, their text properties, etc.
3462
3463 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3464 if (! NILP (from_overlay))
3465 for (i = it->sp - 1; i >= 0; i--)
3466 {
3467 if (it->stack[i].current.overlay_string_index >= 0)
3468 from_overlay
3469 = it->string_overlays[it->stack[i].current.overlay_string_index];
3470 else if (! NILP (it->stack[i].from_overlay))
3471 from_overlay = it->stack[i].from_overlay;
3472
3473 if (!NILP (from_overlay))
3474 break;
3475 }
3476
3477 if (! NILP (from_overlay))
3478 {
3479 bufpos = IT_CHARPOS (*it);
3480 /* For a string from an overlay, the base face depends
3481 only on text properties and ignores overlays. */
3482 base_face_id
3483 = face_for_overlay_string (it->w,
3484 IT_CHARPOS (*it),
3485 it->region_beg_charpos,
3486 it->region_end_charpos,
3487 &next_stop,
3488 (IT_CHARPOS (*it)
3489 + TEXT_PROP_DISTANCE_LIMIT),
3490 0,
3491 from_overlay);
3492 }
3493 else
3494 {
3495 bufpos = 0;
3496
3497 /* For strings from a `display' property, use the face at
3498 IT's current buffer position as the base face to merge
3499 with, so that overlay strings appear in the same face as
3500 surrounding text, unless they specify their own
3501 faces. */
3502 base_face_id = underlying_face_id (it);
3503 }
3504
3505 new_face_id = face_at_string_position (it->w,
3506 it->string,
3507 IT_STRING_CHARPOS (*it),
3508 bufpos,
3509 it->region_beg_charpos,
3510 it->region_end_charpos,
3511 &next_stop,
3512 base_face_id, 0);
3513
3514 /* Is this a start of a run of characters with box? Caveat:
3515 this can be called for a freshly allocated iterator; face_id
3516 is -1 is this case. We know that the new face will not
3517 change until the next check pos, i.e. if the new face has a
3518 box, all characters up to that position will have a
3519 box. But, as usual, we don't know whether that position
3520 is really the end. */
3521 if (new_face_id != it->face_id)
3522 {
3523 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3524 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3525
3526 /* If new face has a box but old face hasn't, this is the
3527 start of a run of characters with box, i.e. it has a
3528 shadow on the left side. */
3529 it->start_of_box_run_p
3530 = new_face->box && (old_face == NULL || !old_face->box);
3531 it->face_box_p = new_face->box != FACE_NO_BOX;
3532 }
3533 }
3534
3535 it->face_id = new_face_id;
3536 return HANDLED_NORMALLY;
3537 }
3538
3539
3540 /* Return the ID of the face ``underlying'' IT's current position,
3541 which is in a string. If the iterator is associated with a
3542 buffer, return the face at IT's current buffer position.
3543 Otherwise, use the iterator's base_face_id. */
3544
3545 static int
3546 underlying_face_id (struct it *it)
3547 {
3548 int face_id = it->base_face_id, i;
3549
3550 xassert (STRINGP (it->string));
3551
3552 for (i = it->sp - 1; i >= 0; --i)
3553 if (NILP (it->stack[i].string))
3554 face_id = it->stack[i].face_id;
3555
3556 return face_id;
3557 }
3558
3559
3560 /* Compute the face one character before or after the current position
3561 of IT. BEFORE_P non-zero means get the face in front of IT's
3562 position. Value is the id of the face. */
3563
3564 static int
3565 face_before_or_after_it_pos (struct it *it, int before_p)
3566 {
3567 int face_id, limit;
3568 EMACS_INT next_check_charpos;
3569 struct text_pos pos;
3570
3571 xassert (it->s == NULL);
3572
3573 if (STRINGP (it->string))
3574 {
3575 int bufpos, base_face_id;
3576
3577 /* No face change past the end of the string (for the case
3578 we are padding with spaces). No face change before the
3579 string start. */
3580 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3581 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3582 return it->face_id;
3583
3584 /* Set pos to the position before or after IT's current position. */
3585 if (before_p)
3586 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3587 else
3588 /* For composition, we must check the character after the
3589 composition. */
3590 pos = (it->what == IT_COMPOSITION
3591 ? string_pos (IT_STRING_CHARPOS (*it)
3592 + it->cmp_it.nchars, it->string)
3593 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3594
3595 if (it->current.overlay_string_index >= 0)
3596 bufpos = IT_CHARPOS (*it);
3597 else
3598 bufpos = 0;
3599
3600 base_face_id = underlying_face_id (it);
3601
3602 /* Get the face for ASCII, or unibyte. */
3603 face_id = face_at_string_position (it->w,
3604 it->string,
3605 CHARPOS (pos),
3606 bufpos,
3607 it->region_beg_charpos,
3608 it->region_end_charpos,
3609 &next_check_charpos,
3610 base_face_id, 0);
3611
3612 /* Correct the face for charsets different from ASCII. Do it
3613 for the multibyte case only. The face returned above is
3614 suitable for unibyte text if IT->string is unibyte. */
3615 if (STRING_MULTIBYTE (it->string))
3616 {
3617 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3618 int rest = SBYTES (it->string) - BYTEPOS (pos);
3619 int c, len;
3620 struct face *face = FACE_FROM_ID (it->f, face_id);
3621
3622 c = string_char_and_length (p, &len);
3623 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3624 }
3625 }
3626 else
3627 {
3628 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3629 || (IT_CHARPOS (*it) <= BEGV && before_p))
3630 return it->face_id;
3631
3632 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3633 pos = it->current.pos;
3634
3635 if (before_p)
3636 DEC_TEXT_POS (pos, it->multibyte_p);
3637 else
3638 {
3639 if (it->what == IT_COMPOSITION)
3640 /* For composition, we must check the position after the
3641 composition. */
3642 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3643 else
3644 INC_TEXT_POS (pos, it->multibyte_p);
3645 }
3646
3647 /* Determine face for CHARSET_ASCII, or unibyte. */
3648 face_id = face_at_buffer_position (it->w,
3649 CHARPOS (pos),
3650 it->region_beg_charpos,
3651 it->region_end_charpos,
3652 &next_check_charpos,
3653 limit, 0, -1);
3654
3655 /* Correct the face for charsets different from ASCII. Do it
3656 for the multibyte case only. The face returned above is
3657 suitable for unibyte text if current_buffer is unibyte. */
3658 if (it->multibyte_p)
3659 {
3660 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3661 struct face *face = FACE_FROM_ID (it->f, face_id);
3662 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3663 }
3664 }
3665
3666 return face_id;
3667 }
3668
3669
3670 \f
3671 /***********************************************************************
3672 Invisible text
3673 ***********************************************************************/
3674
3675 /* Set up iterator IT from invisible properties at its current
3676 position. Called from handle_stop. */
3677
3678 static enum prop_handled
3679 handle_invisible_prop (struct it *it)
3680 {
3681 enum prop_handled handled = HANDLED_NORMALLY;
3682
3683 if (STRINGP (it->string))
3684 {
3685 Lisp_Object prop, end_charpos, limit, charpos;
3686
3687 /* Get the value of the invisible text property at the
3688 current position. Value will be nil if there is no such
3689 property. */
3690 charpos = make_number (IT_STRING_CHARPOS (*it));
3691 prop = Fget_text_property (charpos, Qinvisible, it->string);
3692
3693 if (!NILP (prop)
3694 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3695 {
3696 handled = HANDLED_RECOMPUTE_PROPS;
3697
3698 /* Get the position at which the next change of the
3699 invisible text property can be found in IT->string.
3700 Value will be nil if the property value is the same for
3701 all the rest of IT->string. */
3702 XSETINT (limit, SCHARS (it->string));
3703 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3704 it->string, limit);
3705
3706 /* Text at current position is invisible. The next
3707 change in the property is at position end_charpos.
3708 Move IT's current position to that position. */
3709 if (INTEGERP (end_charpos)
3710 && XFASTINT (end_charpos) < XFASTINT (limit))
3711 {
3712 struct text_pos old;
3713 old = it->current.string_pos;
3714 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3715 compute_string_pos (&it->current.string_pos, old, it->string);
3716 }
3717 else
3718 {
3719 /* The rest of the string is invisible. If this is an
3720 overlay string, proceed with the next overlay string
3721 or whatever comes and return a character from there. */
3722 if (it->current.overlay_string_index >= 0)
3723 {
3724 next_overlay_string (it);
3725 /* Don't check for overlay strings when we just
3726 finished processing them. */
3727 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3728 }
3729 else
3730 {
3731 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3732 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3733 }
3734 }
3735 }
3736 }
3737 else
3738 {
3739 int invis_p;
3740 EMACS_INT newpos, next_stop, start_charpos, tem;
3741 Lisp_Object pos, prop, overlay;
3742
3743 /* First of all, is there invisible text at this position? */
3744 tem = start_charpos = IT_CHARPOS (*it);
3745 pos = make_number (tem);
3746 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3747 &overlay);
3748 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3749
3750 /* If we are on invisible text, skip over it. */
3751 if (invis_p && start_charpos < it->end_charpos)
3752 {
3753 /* Record whether we have to display an ellipsis for the
3754 invisible text. */
3755 int display_ellipsis_p = invis_p == 2;
3756
3757 handled = HANDLED_RECOMPUTE_PROPS;
3758
3759 /* Loop skipping over invisible text. The loop is left at
3760 ZV or with IT on the first char being visible again. */
3761 do
3762 {
3763 /* Try to skip some invisible text. Return value is the
3764 position reached which can be equal to where we start
3765 if there is nothing invisible there. This skips both
3766 over invisible text properties and overlays with
3767 invisible property. */
3768 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3769
3770 /* If we skipped nothing at all we weren't at invisible
3771 text in the first place. If everything to the end of
3772 the buffer was skipped, end the loop. */
3773 if (newpos == tem || newpos >= ZV)
3774 invis_p = 0;
3775 else
3776 {
3777 /* We skipped some characters but not necessarily
3778 all there are. Check if we ended up on visible
3779 text. Fget_char_property returns the property of
3780 the char before the given position, i.e. if we
3781 get invis_p = 0, this means that the char at
3782 newpos is visible. */
3783 pos = make_number (newpos);
3784 prop = Fget_char_property (pos, Qinvisible, it->window);
3785 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3786 }
3787
3788 /* If we ended up on invisible text, proceed to
3789 skip starting with next_stop. */
3790 if (invis_p)
3791 tem = next_stop;
3792
3793 /* If there are adjacent invisible texts, don't lose the
3794 second one's ellipsis. */
3795 if (invis_p == 2)
3796 display_ellipsis_p = 1;
3797 }
3798 while (invis_p);
3799
3800 /* The position newpos is now either ZV or on visible text. */
3801 if (it->bidi_p && newpos < ZV)
3802 {
3803 /* With bidi iteration, the region of invisible text
3804 could start and/or end in the middle of a non-base
3805 embedding level. Therefore, we need to skip
3806 invisible text using the bidi iterator, starting at
3807 IT's current position, until we find ourselves
3808 outside the invisible text. Skipping invisible text
3809 _after_ bidi iteration avoids affecting the visual
3810 order of the displayed text when invisible properties
3811 are added or removed. */
3812 if (it->bidi_it.first_elt)
3813 {
3814 /* If we were `reseat'ed to a new paragraph,
3815 determine the paragraph base direction. We need
3816 to do it now because next_element_from_buffer may
3817 not have a chance to do it, if we are going to
3818 skip any text at the beginning, which resets the
3819 FIRST_ELT flag. */
3820 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
3821 }
3822 do
3823 {
3824 bidi_move_to_visually_next (&it->bidi_it);
3825 }
3826 while (it->stop_charpos <= it->bidi_it.charpos
3827 && it->bidi_it.charpos < newpos);
3828 IT_CHARPOS (*it) = it->bidi_it.charpos;
3829 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3830 /* If we overstepped NEWPOS, record its position in the
3831 iterator, so that we skip invisible text if later the
3832 bidi iteration lands us in the invisible region
3833 again. */
3834 if (IT_CHARPOS (*it) >= newpos)
3835 it->prev_stop = newpos;
3836 }
3837 else
3838 {
3839 IT_CHARPOS (*it) = newpos;
3840 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3841 }
3842
3843 /* If there are before-strings at the start of invisible
3844 text, and the text is invisible because of a text
3845 property, arrange to show before-strings because 20.x did
3846 it that way. (If the text is invisible because of an
3847 overlay property instead of a text property, this is
3848 already handled in the overlay code.) */
3849 if (NILP (overlay)
3850 && get_overlay_strings (it, it->stop_charpos))
3851 {
3852 handled = HANDLED_RECOMPUTE_PROPS;
3853 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3854 }
3855 else if (display_ellipsis_p)
3856 {
3857 /* Make sure that the glyphs of the ellipsis will get
3858 correct `charpos' values. If we would not update
3859 it->position here, the glyphs would belong to the
3860 last visible character _before_ the invisible
3861 text, which confuses `set_cursor_from_row'.
3862
3863 We use the last invisible position instead of the
3864 first because this way the cursor is always drawn on
3865 the first "." of the ellipsis, whenever PT is inside
3866 the invisible text. Otherwise the cursor would be
3867 placed _after_ the ellipsis when the point is after the
3868 first invisible character. */
3869 if (!STRINGP (it->object))
3870 {
3871 it->position.charpos = newpos - 1;
3872 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3873 }
3874 it->ellipsis_p = 1;
3875 /* Let the ellipsis display before
3876 considering any properties of the following char.
3877 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3878 handled = HANDLED_RETURN;
3879 }
3880 }
3881 }
3882
3883 return handled;
3884 }
3885
3886
3887 /* Make iterator IT return `...' next.
3888 Replaces LEN characters from buffer. */
3889
3890 static void
3891 setup_for_ellipsis (struct it *it, int len)
3892 {
3893 /* Use the display table definition for `...'. Invalid glyphs
3894 will be handled by the method returning elements from dpvec. */
3895 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3896 {
3897 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3898 it->dpvec = v->contents;
3899 it->dpend = v->contents + v->size;
3900 }
3901 else
3902 {
3903 /* Default `...'. */
3904 it->dpvec = default_invis_vector;
3905 it->dpend = default_invis_vector + 3;
3906 }
3907
3908 it->dpvec_char_len = len;
3909 it->current.dpvec_index = 0;
3910 it->dpvec_face_id = -1;
3911
3912 /* Remember the current face id in case glyphs specify faces.
3913 IT's face is restored in set_iterator_to_next.
3914 saved_face_id was set to preceding char's face in handle_stop. */
3915 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3916 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3917
3918 it->method = GET_FROM_DISPLAY_VECTOR;
3919 it->ellipsis_p = 1;
3920 }
3921
3922
3923 \f
3924 /***********************************************************************
3925 'display' property
3926 ***********************************************************************/
3927
3928 /* Set up iterator IT from `display' property at its current position.
3929 Called from handle_stop.
3930 We return HANDLED_RETURN if some part of the display property
3931 overrides the display of the buffer text itself.
3932 Otherwise we return HANDLED_NORMALLY. */
3933
3934 static enum prop_handled
3935 handle_display_prop (struct it *it)
3936 {
3937 Lisp_Object prop, object, overlay;
3938 struct text_pos *position;
3939 /* Nonzero if some property replaces the display of the text itself. */
3940 int display_replaced_p = 0;
3941
3942 if (STRINGP (it->string))
3943 {
3944 object = it->string;
3945 position = &it->current.string_pos;
3946 }
3947 else
3948 {
3949 XSETWINDOW (object, it->w);
3950 position = &it->current.pos;
3951 }
3952
3953 /* Reset those iterator values set from display property values. */
3954 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3955 it->space_width = Qnil;
3956 it->font_height = Qnil;
3957 it->voffset = 0;
3958
3959 /* We don't support recursive `display' properties, i.e. string
3960 values that have a string `display' property, that have a string
3961 `display' property etc. */
3962 if (!it->string_from_display_prop_p)
3963 it->area = TEXT_AREA;
3964
3965 prop = get_char_property_and_overlay (make_number (position->charpos),
3966 Qdisplay, object, &overlay);
3967 if (NILP (prop))
3968 return HANDLED_NORMALLY;
3969 /* Now OVERLAY is the overlay that gave us this property, or nil
3970 if it was a text property. */
3971
3972 if (!STRINGP (it->string))
3973 object = it->w->buffer;
3974
3975 if (CONSP (prop)
3976 /* Simple properties. */
3977 && !EQ (XCAR (prop), Qimage)
3978 && !EQ (XCAR (prop), Qspace)
3979 && !EQ (XCAR (prop), Qwhen)
3980 && !EQ (XCAR (prop), Qslice)
3981 && !EQ (XCAR (prop), Qspace_width)
3982 && !EQ (XCAR (prop), Qheight)
3983 && !EQ (XCAR (prop), Qraise)
3984 /* Marginal area specifications. */
3985 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3986 && !EQ (XCAR (prop), Qleft_fringe)
3987 && !EQ (XCAR (prop), Qright_fringe)
3988 && !NILP (XCAR (prop)))
3989 {
3990 for (; CONSP (prop); prop = XCDR (prop))
3991 {
3992 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3993 position, display_replaced_p))
3994 {
3995 display_replaced_p = 1;
3996 /* If some text in a string is replaced, `position' no
3997 longer points to the position of `object'. */
3998 if (STRINGP (object))
3999 break;
4000 }
4001 }
4002 }
4003 else if (VECTORP (prop))
4004 {
4005 int i;
4006 for (i = 0; i < ASIZE (prop); ++i)
4007 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4008 position, display_replaced_p))
4009 {
4010 display_replaced_p = 1;
4011 /* If some text in a string is replaced, `position' no
4012 longer points to the position of `object'. */
4013 if (STRINGP (object))
4014 break;
4015 }
4016 }
4017 else
4018 {
4019 if (handle_single_display_spec (it, prop, object, overlay,
4020 position, 0))
4021 display_replaced_p = 1;
4022 }
4023
4024 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4025 }
4026
4027
4028 /* Value is the position of the end of the `display' property starting
4029 at START_POS in OBJECT. */
4030
4031 static struct text_pos
4032 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4033 {
4034 Lisp_Object end;
4035 struct text_pos end_pos;
4036
4037 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4038 Qdisplay, object, Qnil);
4039 CHARPOS (end_pos) = XFASTINT (end);
4040 if (STRINGP (object))
4041 compute_string_pos (&end_pos, start_pos, it->string);
4042 else
4043 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4044
4045 return end_pos;
4046 }
4047
4048
4049 /* Set up IT from a single `display' specification PROP. OBJECT
4050 is the object in which the `display' property was found. *POSITION
4051 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4052 means that we previously saw a display specification which already
4053 replaced text display with something else, for example an image;
4054 we ignore such properties after the first one has been processed.
4055
4056 OVERLAY is the overlay this `display' property came from,
4057 or nil if it was a text property.
4058
4059 If PROP is a `space' or `image' specification, and in some other
4060 cases too, set *POSITION to the position where the `display'
4061 property ends.
4062
4063 Value is non-zero if something was found which replaces the display
4064 of buffer or string text. */
4065
4066 static int
4067 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4068 Lisp_Object overlay, struct text_pos *position,
4069 int display_replaced_before_p)
4070 {
4071 Lisp_Object form;
4072 Lisp_Object location, value;
4073 struct text_pos start_pos, save_pos;
4074 int valid_p;
4075
4076 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4077 If the result is non-nil, use VALUE instead of SPEC. */
4078 form = Qt;
4079 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4080 {
4081 spec = XCDR (spec);
4082 if (!CONSP (spec))
4083 return 0;
4084 form = XCAR (spec);
4085 spec = XCDR (spec);
4086 }
4087
4088 if (!NILP (form) && !EQ (form, Qt))
4089 {
4090 int count = SPECPDL_INDEX ();
4091 struct gcpro gcpro1;
4092
4093 /* Bind `object' to the object having the `display' property, a
4094 buffer or string. Bind `position' to the position in the
4095 object where the property was found, and `buffer-position'
4096 to the current position in the buffer. */
4097 specbind (Qobject, object);
4098 specbind (Qposition, make_number (CHARPOS (*position)));
4099 specbind (Qbuffer_position,
4100 make_number (STRINGP (object)
4101 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4102 GCPRO1 (form);
4103 form = safe_eval (form);
4104 UNGCPRO;
4105 unbind_to (count, Qnil);
4106 }
4107
4108 if (NILP (form))
4109 return 0;
4110
4111 /* Handle `(height HEIGHT)' specifications. */
4112 if (CONSP (spec)
4113 && EQ (XCAR (spec), Qheight)
4114 && CONSP (XCDR (spec)))
4115 {
4116 if (!FRAME_WINDOW_P (it->f))
4117 return 0;
4118
4119 it->font_height = XCAR (XCDR (spec));
4120 if (!NILP (it->font_height))
4121 {
4122 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4123 int new_height = -1;
4124
4125 if (CONSP (it->font_height)
4126 && (EQ (XCAR (it->font_height), Qplus)
4127 || EQ (XCAR (it->font_height), Qminus))
4128 && CONSP (XCDR (it->font_height))
4129 && INTEGERP (XCAR (XCDR (it->font_height))))
4130 {
4131 /* `(+ N)' or `(- N)' where N is an integer. */
4132 int steps = XINT (XCAR (XCDR (it->font_height)));
4133 if (EQ (XCAR (it->font_height), Qplus))
4134 steps = - steps;
4135 it->face_id = smaller_face (it->f, it->face_id, steps);
4136 }
4137 else if (FUNCTIONP (it->font_height))
4138 {
4139 /* Call function with current height as argument.
4140 Value is the new height. */
4141 Lisp_Object height;
4142 height = safe_call1 (it->font_height,
4143 face->lface[LFACE_HEIGHT_INDEX]);
4144 if (NUMBERP (height))
4145 new_height = XFLOATINT (height);
4146 }
4147 else if (NUMBERP (it->font_height))
4148 {
4149 /* Value is a multiple of the canonical char height. */
4150 struct face *face;
4151
4152 face = FACE_FROM_ID (it->f,
4153 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4154 new_height = (XFLOATINT (it->font_height)
4155 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4156 }
4157 else
4158 {
4159 /* Evaluate IT->font_height with `height' bound to the
4160 current specified height to get the new height. */
4161 int count = SPECPDL_INDEX ();
4162
4163 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4164 value = safe_eval (it->font_height);
4165 unbind_to (count, Qnil);
4166
4167 if (NUMBERP (value))
4168 new_height = XFLOATINT (value);
4169 }
4170
4171 if (new_height > 0)
4172 it->face_id = face_with_height (it->f, it->face_id, new_height);
4173 }
4174
4175 return 0;
4176 }
4177
4178 /* Handle `(space-width WIDTH)'. */
4179 if (CONSP (spec)
4180 && EQ (XCAR (spec), Qspace_width)
4181 && CONSP (XCDR (spec)))
4182 {
4183 if (!FRAME_WINDOW_P (it->f))
4184 return 0;
4185
4186 value = XCAR (XCDR (spec));
4187 if (NUMBERP (value) && XFLOATINT (value) > 0)
4188 it->space_width = value;
4189
4190 return 0;
4191 }
4192
4193 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4194 if (CONSP (spec)
4195 && EQ (XCAR (spec), Qslice))
4196 {
4197 Lisp_Object tem;
4198
4199 if (!FRAME_WINDOW_P (it->f))
4200 return 0;
4201
4202 if (tem = XCDR (spec), CONSP (tem))
4203 {
4204 it->slice.x = XCAR (tem);
4205 if (tem = XCDR (tem), CONSP (tem))
4206 {
4207 it->slice.y = XCAR (tem);
4208 if (tem = XCDR (tem), CONSP (tem))
4209 {
4210 it->slice.width = XCAR (tem);
4211 if (tem = XCDR (tem), CONSP (tem))
4212 it->slice.height = XCAR (tem);
4213 }
4214 }
4215 }
4216
4217 return 0;
4218 }
4219
4220 /* Handle `(raise FACTOR)'. */
4221 if (CONSP (spec)
4222 && EQ (XCAR (spec), Qraise)
4223 && CONSP (XCDR (spec)))
4224 {
4225 if (!FRAME_WINDOW_P (it->f))
4226 return 0;
4227
4228 #ifdef HAVE_WINDOW_SYSTEM
4229 value = XCAR (XCDR (spec));
4230 if (NUMBERP (value))
4231 {
4232 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4233 it->voffset = - (XFLOATINT (value)
4234 * (FONT_HEIGHT (face->font)));
4235 }
4236 #endif /* HAVE_WINDOW_SYSTEM */
4237
4238 return 0;
4239 }
4240
4241 /* Don't handle the other kinds of display specifications
4242 inside a string that we got from a `display' property. */
4243 if (it->string_from_display_prop_p)
4244 return 0;
4245
4246 /* Characters having this form of property are not displayed, so
4247 we have to find the end of the property. */
4248 start_pos = *position;
4249 *position = display_prop_end (it, object, start_pos);
4250 value = Qnil;
4251
4252 /* Stop the scan at that end position--we assume that all
4253 text properties change there. */
4254 it->stop_charpos = position->charpos;
4255
4256 /* Handle `(left-fringe BITMAP [FACE])'
4257 and `(right-fringe BITMAP [FACE])'. */
4258 if (CONSP (spec)
4259 && (EQ (XCAR (spec), Qleft_fringe)
4260 || EQ (XCAR (spec), Qright_fringe))
4261 && CONSP (XCDR (spec)))
4262 {
4263 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4264 int fringe_bitmap;
4265
4266 if (!FRAME_WINDOW_P (it->f))
4267 /* If we return here, POSITION has been advanced
4268 across the text with this property. */
4269 return 0;
4270
4271 #ifdef HAVE_WINDOW_SYSTEM
4272 value = XCAR (XCDR (spec));
4273 if (!SYMBOLP (value)
4274 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4275 /* If we return here, POSITION has been advanced
4276 across the text with this property. */
4277 return 0;
4278
4279 if (CONSP (XCDR (XCDR (spec))))
4280 {
4281 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4282 int face_id2 = lookup_derived_face (it->f, face_name,
4283 FRINGE_FACE_ID, 0);
4284 if (face_id2 >= 0)
4285 face_id = face_id2;
4286 }
4287
4288 /* Save current settings of IT so that we can restore them
4289 when we are finished with the glyph property value. */
4290
4291 save_pos = it->position;
4292 it->position = *position;
4293 push_it (it);
4294 it->position = save_pos;
4295
4296 it->area = TEXT_AREA;
4297 it->what = IT_IMAGE;
4298 it->image_id = -1; /* no image */
4299 it->position = start_pos;
4300 it->object = NILP (object) ? it->w->buffer : object;
4301 it->method = GET_FROM_IMAGE;
4302 it->from_overlay = Qnil;
4303 it->face_id = face_id;
4304
4305 /* Say that we haven't consumed the characters with
4306 `display' property yet. The call to pop_it in
4307 set_iterator_to_next will clean this up. */
4308 *position = start_pos;
4309
4310 if (EQ (XCAR (spec), Qleft_fringe))
4311 {
4312 it->left_user_fringe_bitmap = fringe_bitmap;
4313 it->left_user_fringe_face_id = face_id;
4314 }
4315 else
4316 {
4317 it->right_user_fringe_bitmap = fringe_bitmap;
4318 it->right_user_fringe_face_id = face_id;
4319 }
4320 #endif /* HAVE_WINDOW_SYSTEM */
4321 return 1;
4322 }
4323
4324 /* Prepare to handle `((margin left-margin) ...)',
4325 `((margin right-margin) ...)' and `((margin nil) ...)'
4326 prefixes for display specifications. */
4327 location = Qunbound;
4328 if (CONSP (spec) && CONSP (XCAR (spec)))
4329 {
4330 Lisp_Object tem;
4331
4332 value = XCDR (spec);
4333 if (CONSP (value))
4334 value = XCAR (value);
4335
4336 tem = XCAR (spec);
4337 if (EQ (XCAR (tem), Qmargin)
4338 && (tem = XCDR (tem),
4339 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4340 (NILP (tem)
4341 || EQ (tem, Qleft_margin)
4342 || EQ (tem, Qright_margin))))
4343 location = tem;
4344 }
4345
4346 if (EQ (location, Qunbound))
4347 {
4348 location = Qnil;
4349 value = spec;
4350 }
4351
4352 /* After this point, VALUE is the property after any
4353 margin prefix has been stripped. It must be a string,
4354 an image specification, or `(space ...)'.
4355
4356 LOCATION specifies where to display: `left-margin',
4357 `right-margin' or nil. */
4358
4359 valid_p = (STRINGP (value)
4360 #ifdef HAVE_WINDOW_SYSTEM
4361 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4362 #endif /* not HAVE_WINDOW_SYSTEM */
4363 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4364
4365 if (valid_p && !display_replaced_before_p)
4366 {
4367 /* Save current settings of IT so that we can restore them
4368 when we are finished with the glyph property value. */
4369 save_pos = it->position;
4370 it->position = *position;
4371 push_it (it);
4372 it->position = save_pos;
4373 it->from_overlay = overlay;
4374
4375 if (NILP (location))
4376 it->area = TEXT_AREA;
4377 else if (EQ (location, Qleft_margin))
4378 it->area = LEFT_MARGIN_AREA;
4379 else
4380 it->area = RIGHT_MARGIN_AREA;
4381
4382 if (STRINGP (value))
4383 {
4384 it->string = value;
4385 it->multibyte_p = STRING_MULTIBYTE (it->string);
4386 it->current.overlay_string_index = -1;
4387 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4388 it->end_charpos = it->string_nchars = SCHARS (it->string);
4389 it->method = GET_FROM_STRING;
4390 it->stop_charpos = 0;
4391 it->string_from_display_prop_p = 1;
4392 /* Say that we haven't consumed the characters with
4393 `display' property yet. The call to pop_it in
4394 set_iterator_to_next will clean this up. */
4395 if (BUFFERP (object))
4396 *position = start_pos;
4397 }
4398 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4399 {
4400 it->method = GET_FROM_STRETCH;
4401 it->object = value;
4402 *position = it->position = start_pos;
4403 }
4404 #ifdef HAVE_WINDOW_SYSTEM
4405 else
4406 {
4407 it->what = IT_IMAGE;
4408 it->image_id = lookup_image (it->f, value);
4409 it->position = start_pos;
4410 it->object = NILP (object) ? it->w->buffer : object;
4411 it->method = GET_FROM_IMAGE;
4412
4413 /* Say that we haven't consumed the characters with
4414 `display' property yet. The call to pop_it in
4415 set_iterator_to_next will clean this up. */
4416 *position = start_pos;
4417 }
4418 #endif /* HAVE_WINDOW_SYSTEM */
4419
4420 return 1;
4421 }
4422
4423 /* Invalid property or property not supported. Restore
4424 POSITION to what it was before. */
4425 *position = start_pos;
4426 return 0;
4427 }
4428
4429
4430 /* Check if SPEC is a display sub-property value whose text should be
4431 treated as intangible. */
4432
4433 static int
4434 single_display_spec_intangible_p (Lisp_Object prop)
4435 {
4436 /* Skip over `when FORM'. */
4437 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4438 {
4439 prop = XCDR (prop);
4440 if (!CONSP (prop))
4441 return 0;
4442 prop = XCDR (prop);
4443 }
4444
4445 if (STRINGP (prop))
4446 return 1;
4447
4448 if (!CONSP (prop))
4449 return 0;
4450
4451 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4452 we don't need to treat text as intangible. */
4453 if (EQ (XCAR (prop), Qmargin))
4454 {
4455 prop = XCDR (prop);
4456 if (!CONSP (prop))
4457 return 0;
4458
4459 prop = XCDR (prop);
4460 if (!CONSP (prop)
4461 || EQ (XCAR (prop), Qleft_margin)
4462 || EQ (XCAR (prop), Qright_margin))
4463 return 0;
4464 }
4465
4466 return (CONSP (prop)
4467 && (EQ (XCAR (prop), Qimage)
4468 || EQ (XCAR (prop), Qspace)));
4469 }
4470
4471
4472 /* Check if PROP is a display property value whose text should be
4473 treated as intangible. */
4474
4475 int
4476 display_prop_intangible_p (Lisp_Object prop)
4477 {
4478 if (CONSP (prop)
4479 && CONSP (XCAR (prop))
4480 && !EQ (Qmargin, XCAR (XCAR (prop))))
4481 {
4482 /* A list of sub-properties. */
4483 while (CONSP (prop))
4484 {
4485 if (single_display_spec_intangible_p (XCAR (prop)))
4486 return 1;
4487 prop = XCDR (prop);
4488 }
4489 }
4490 else if (VECTORP (prop))
4491 {
4492 /* A vector of sub-properties. */
4493 int i;
4494 for (i = 0; i < ASIZE (prop); ++i)
4495 if (single_display_spec_intangible_p (AREF (prop, i)))
4496 return 1;
4497 }
4498 else
4499 return single_display_spec_intangible_p (prop);
4500
4501 return 0;
4502 }
4503
4504
4505 /* Return 1 if PROP is a display sub-property value containing STRING. */
4506
4507 static int
4508 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4509 {
4510 if (EQ (string, prop))
4511 return 1;
4512
4513 /* Skip over `when FORM'. */
4514 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4515 {
4516 prop = XCDR (prop);
4517 if (!CONSP (prop))
4518 return 0;
4519 prop = XCDR (prop);
4520 }
4521
4522 if (CONSP (prop))
4523 /* Skip over `margin LOCATION'. */
4524 if (EQ (XCAR (prop), Qmargin))
4525 {
4526 prop = XCDR (prop);
4527 if (!CONSP (prop))
4528 return 0;
4529
4530 prop = XCDR (prop);
4531 if (!CONSP (prop))
4532 return 0;
4533 }
4534
4535 return CONSP (prop) && EQ (XCAR (prop), string);
4536 }
4537
4538
4539 /* Return 1 if STRING appears in the `display' property PROP. */
4540
4541 static int
4542 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4543 {
4544 if (CONSP (prop)
4545 && CONSP (XCAR (prop))
4546 && !EQ (Qmargin, XCAR (XCAR (prop))))
4547 {
4548 /* A list of sub-properties. */
4549 while (CONSP (prop))
4550 {
4551 if (single_display_spec_string_p (XCAR (prop), string))
4552 return 1;
4553 prop = XCDR (prop);
4554 }
4555 }
4556 else if (VECTORP (prop))
4557 {
4558 /* A vector of sub-properties. */
4559 int i;
4560 for (i = 0; i < ASIZE (prop); ++i)
4561 if (single_display_spec_string_p (AREF (prop, i), string))
4562 return 1;
4563 }
4564 else
4565 return single_display_spec_string_p (prop, string);
4566
4567 return 0;
4568 }
4569
4570 /* Look for STRING in overlays and text properties in W's buffer,
4571 between character positions FROM and TO (excluding TO).
4572 BACK_P non-zero means look back (in this case, TO is supposed to be
4573 less than FROM).
4574 Value is the first character position where STRING was found, or
4575 zero if it wasn't found before hitting TO.
4576
4577 W's buffer must be current.
4578
4579 This function may only use code that doesn't eval because it is
4580 called asynchronously from note_mouse_highlight. */
4581
4582 static EMACS_INT
4583 string_buffer_position_lim (struct window *w, Lisp_Object string,
4584 EMACS_INT from, EMACS_INT to, int back_p)
4585 {
4586 Lisp_Object limit, prop, pos;
4587 int found = 0;
4588
4589 pos = make_number (from);
4590
4591 if (!back_p) /* looking forward */
4592 {
4593 limit = make_number (min (to, ZV));
4594 while (!found && !EQ (pos, limit))
4595 {
4596 prop = Fget_char_property (pos, Qdisplay, Qnil);
4597 if (!NILP (prop) && display_prop_string_p (prop, string))
4598 found = 1;
4599 else
4600 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4601 limit);
4602 }
4603 }
4604 else /* looking back */
4605 {
4606 limit = make_number (max (to, BEGV));
4607 while (!found && !EQ (pos, limit))
4608 {
4609 prop = Fget_char_property (pos, Qdisplay, Qnil);
4610 if (!NILP (prop) && display_prop_string_p (prop, string))
4611 found = 1;
4612 else
4613 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4614 limit);
4615 }
4616 }
4617
4618 return found ? XINT (pos) : 0;
4619 }
4620
4621 /* Determine which buffer position in W's buffer STRING comes from.
4622 AROUND_CHARPOS is an approximate position where it could come from.
4623 Value is the buffer position or 0 if it couldn't be determined.
4624
4625 W's buffer must be current.
4626
4627 This function is necessary because we don't record buffer positions
4628 in glyphs generated from strings (to keep struct glyph small).
4629 This function may only use code that doesn't eval because it is
4630 called asynchronously from note_mouse_highlight. */
4631
4632 EMACS_INT
4633 string_buffer_position (struct window *w, Lisp_Object string, EMACS_INT around_charpos)
4634 {
4635 Lisp_Object limit, prop, pos;
4636 const int MAX_DISTANCE = 1000;
4637 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4638 around_charpos + MAX_DISTANCE,
4639 0);
4640
4641 if (!found)
4642 found = string_buffer_position_lim (w, string, around_charpos,
4643 around_charpos - MAX_DISTANCE, 1);
4644 return found;
4645 }
4646
4647
4648 \f
4649 /***********************************************************************
4650 `composition' property
4651 ***********************************************************************/
4652
4653 /* Set up iterator IT from `composition' property at its current
4654 position. Called from handle_stop. */
4655
4656 static enum prop_handled
4657 handle_composition_prop (struct it *it)
4658 {
4659 Lisp_Object prop, string;
4660 EMACS_INT pos, pos_byte, start, end;
4661
4662 if (STRINGP (it->string))
4663 {
4664 unsigned char *s;
4665
4666 pos = IT_STRING_CHARPOS (*it);
4667 pos_byte = IT_STRING_BYTEPOS (*it);
4668 string = it->string;
4669 s = SDATA (string) + pos_byte;
4670 it->c = STRING_CHAR (s);
4671 }
4672 else
4673 {
4674 pos = IT_CHARPOS (*it);
4675 pos_byte = IT_BYTEPOS (*it);
4676 string = Qnil;
4677 it->c = FETCH_CHAR (pos_byte);
4678 }
4679
4680 /* If there's a valid composition and point is not inside of the
4681 composition (in the case that the composition is from the current
4682 buffer), draw a glyph composed from the composition components. */
4683 if (find_composition (pos, -1, &start, &end, &prop, string)
4684 && COMPOSITION_VALID_P (start, end, prop)
4685 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4686 {
4687 if (start != pos)
4688 {
4689 if (STRINGP (it->string))
4690 pos_byte = string_char_to_byte (it->string, start);
4691 else
4692 pos_byte = CHAR_TO_BYTE (start);
4693 }
4694 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4695 prop, string);
4696
4697 if (it->cmp_it.id >= 0)
4698 {
4699 it->cmp_it.ch = -1;
4700 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4701 it->cmp_it.nglyphs = -1;
4702 }
4703 }
4704
4705 return HANDLED_NORMALLY;
4706 }
4707
4708
4709 \f
4710 /***********************************************************************
4711 Overlay strings
4712 ***********************************************************************/
4713
4714 /* The following structure is used to record overlay strings for
4715 later sorting in load_overlay_strings. */
4716
4717 struct overlay_entry
4718 {
4719 Lisp_Object overlay;
4720 Lisp_Object string;
4721 int priority;
4722 int after_string_p;
4723 };
4724
4725
4726 /* Set up iterator IT from overlay strings at its current position.
4727 Called from handle_stop. */
4728
4729 static enum prop_handled
4730 handle_overlay_change (struct it *it)
4731 {
4732 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4733 return HANDLED_RECOMPUTE_PROPS;
4734 else
4735 return HANDLED_NORMALLY;
4736 }
4737
4738
4739 /* Set up the next overlay string for delivery by IT, if there is an
4740 overlay string to deliver. Called by set_iterator_to_next when the
4741 end of the current overlay string is reached. If there are more
4742 overlay strings to display, IT->string and
4743 IT->current.overlay_string_index are set appropriately here.
4744 Otherwise IT->string is set to nil. */
4745
4746 static void
4747 next_overlay_string (struct it *it)
4748 {
4749 ++it->current.overlay_string_index;
4750 if (it->current.overlay_string_index == it->n_overlay_strings)
4751 {
4752 /* No more overlay strings. Restore IT's settings to what
4753 they were before overlay strings were processed, and
4754 continue to deliver from current_buffer. */
4755
4756 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4757 pop_it (it);
4758 xassert (it->sp > 0
4759 || (NILP (it->string)
4760 && it->method == GET_FROM_BUFFER
4761 && it->stop_charpos >= BEGV
4762 && it->stop_charpos <= it->end_charpos));
4763 it->current.overlay_string_index = -1;
4764 it->n_overlay_strings = 0;
4765
4766 /* If we're at the end of the buffer, record that we have
4767 processed the overlay strings there already, so that
4768 next_element_from_buffer doesn't try it again. */
4769 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4770 it->overlay_strings_at_end_processed_p = 1;
4771 }
4772 else
4773 {
4774 /* There are more overlay strings to process. If
4775 IT->current.overlay_string_index has advanced to a position
4776 where we must load IT->overlay_strings with more strings, do
4777 it. */
4778 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4779
4780 if (it->current.overlay_string_index && i == 0)
4781 load_overlay_strings (it, 0);
4782
4783 /* Initialize IT to deliver display elements from the overlay
4784 string. */
4785 it->string = it->overlay_strings[i];
4786 it->multibyte_p = STRING_MULTIBYTE (it->string);
4787 SET_TEXT_POS (it->current.string_pos, 0, 0);
4788 it->method = GET_FROM_STRING;
4789 it->stop_charpos = 0;
4790 if (it->cmp_it.stop_pos >= 0)
4791 it->cmp_it.stop_pos = 0;
4792 }
4793
4794 CHECK_IT (it);
4795 }
4796
4797
4798 /* Compare two overlay_entry structures E1 and E2. Used as a
4799 comparison function for qsort in load_overlay_strings. Overlay
4800 strings for the same position are sorted so that
4801
4802 1. All after-strings come in front of before-strings, except
4803 when they come from the same overlay.
4804
4805 2. Within after-strings, strings are sorted so that overlay strings
4806 from overlays with higher priorities come first.
4807
4808 2. Within before-strings, strings are sorted so that overlay
4809 strings from overlays with higher priorities come last.
4810
4811 Value is analogous to strcmp. */
4812
4813
4814 static int
4815 compare_overlay_entries (const void *e1, const void *e2)
4816 {
4817 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4818 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4819 int result;
4820
4821 if (entry1->after_string_p != entry2->after_string_p)
4822 {
4823 /* Let after-strings appear in front of before-strings if
4824 they come from different overlays. */
4825 if (EQ (entry1->overlay, entry2->overlay))
4826 result = entry1->after_string_p ? 1 : -1;
4827 else
4828 result = entry1->after_string_p ? -1 : 1;
4829 }
4830 else if (entry1->after_string_p)
4831 /* After-strings sorted in order of decreasing priority. */
4832 result = entry2->priority - entry1->priority;
4833 else
4834 /* Before-strings sorted in order of increasing priority. */
4835 result = entry1->priority - entry2->priority;
4836
4837 return result;
4838 }
4839
4840
4841 /* Load the vector IT->overlay_strings with overlay strings from IT's
4842 current buffer position, or from CHARPOS if that is > 0. Set
4843 IT->n_overlays to the total number of overlay strings found.
4844
4845 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4846 a time. On entry into load_overlay_strings,
4847 IT->current.overlay_string_index gives the number of overlay
4848 strings that have already been loaded by previous calls to this
4849 function.
4850
4851 IT->add_overlay_start contains an additional overlay start
4852 position to consider for taking overlay strings from, if non-zero.
4853 This position comes into play when the overlay has an `invisible'
4854 property, and both before and after-strings. When we've skipped to
4855 the end of the overlay, because of its `invisible' property, we
4856 nevertheless want its before-string to appear.
4857 IT->add_overlay_start will contain the overlay start position
4858 in this case.
4859
4860 Overlay strings are sorted so that after-string strings come in
4861 front of before-string strings. Within before and after-strings,
4862 strings are sorted by overlay priority. See also function
4863 compare_overlay_entries. */
4864
4865 static void
4866 load_overlay_strings (struct it *it, int charpos)
4867 {
4868 Lisp_Object overlay, window, str, invisible;
4869 struct Lisp_Overlay *ov;
4870 int start, end;
4871 int size = 20;
4872 int n = 0, i, j, invis_p;
4873 struct overlay_entry *entries
4874 = (struct overlay_entry *) alloca (size * sizeof *entries);
4875
4876 if (charpos <= 0)
4877 charpos = IT_CHARPOS (*it);
4878
4879 /* Append the overlay string STRING of overlay OVERLAY to vector
4880 `entries' which has size `size' and currently contains `n'
4881 elements. AFTER_P non-zero means STRING is an after-string of
4882 OVERLAY. */
4883 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4884 do \
4885 { \
4886 Lisp_Object priority; \
4887 \
4888 if (n == size) \
4889 { \
4890 int new_size = 2 * size; \
4891 struct overlay_entry *old = entries; \
4892 entries = \
4893 (struct overlay_entry *) alloca (new_size \
4894 * sizeof *entries); \
4895 memcpy (entries, old, size * sizeof *entries); \
4896 size = new_size; \
4897 } \
4898 \
4899 entries[n].string = (STRING); \
4900 entries[n].overlay = (OVERLAY); \
4901 priority = Foverlay_get ((OVERLAY), Qpriority); \
4902 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4903 entries[n].after_string_p = (AFTER_P); \
4904 ++n; \
4905 } \
4906 while (0)
4907
4908 /* Process overlay before the overlay center. */
4909 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4910 {
4911 XSETMISC (overlay, ov);
4912 xassert (OVERLAYP (overlay));
4913 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4914 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4915
4916 if (end < charpos)
4917 break;
4918
4919 /* Skip this overlay if it doesn't start or end at IT's current
4920 position. */
4921 if (end != charpos && start != charpos)
4922 continue;
4923
4924 /* Skip this overlay if it doesn't apply to IT->w. */
4925 window = Foverlay_get (overlay, Qwindow);
4926 if (WINDOWP (window) && XWINDOW (window) != it->w)
4927 continue;
4928
4929 /* If the text ``under'' the overlay is invisible, both before-
4930 and after-strings from this overlay are visible; start and
4931 end position are indistinguishable. */
4932 invisible = Foverlay_get (overlay, Qinvisible);
4933 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4934
4935 /* If overlay has a non-empty before-string, record it. */
4936 if ((start == charpos || (end == charpos && invis_p))
4937 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4938 && SCHARS (str))
4939 RECORD_OVERLAY_STRING (overlay, str, 0);
4940
4941 /* If overlay has a non-empty after-string, record it. */
4942 if ((end == charpos || (start == charpos && invis_p))
4943 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4944 && SCHARS (str))
4945 RECORD_OVERLAY_STRING (overlay, str, 1);
4946 }
4947
4948 /* Process overlays after the overlay center. */
4949 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4950 {
4951 XSETMISC (overlay, ov);
4952 xassert (OVERLAYP (overlay));
4953 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4954 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4955
4956 if (start > charpos)
4957 break;
4958
4959 /* Skip this overlay if it doesn't start or end at IT's current
4960 position. */
4961 if (end != charpos && start != charpos)
4962 continue;
4963
4964 /* Skip this overlay if it doesn't apply to IT->w. */
4965 window = Foverlay_get (overlay, Qwindow);
4966 if (WINDOWP (window) && XWINDOW (window) != it->w)
4967 continue;
4968
4969 /* If the text ``under'' the overlay is invisible, it has a zero
4970 dimension, and both before- and after-strings apply. */
4971 invisible = Foverlay_get (overlay, Qinvisible);
4972 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4973
4974 /* If overlay has a non-empty before-string, record it. */
4975 if ((start == charpos || (end == charpos && invis_p))
4976 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4977 && SCHARS (str))
4978 RECORD_OVERLAY_STRING (overlay, str, 0);
4979
4980 /* If overlay has a non-empty after-string, record it. */
4981 if ((end == charpos || (start == charpos && invis_p))
4982 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4983 && SCHARS (str))
4984 RECORD_OVERLAY_STRING (overlay, str, 1);
4985 }
4986
4987 #undef RECORD_OVERLAY_STRING
4988
4989 /* Sort entries. */
4990 if (n > 1)
4991 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4992
4993 /* Record the total number of strings to process. */
4994 it->n_overlay_strings = n;
4995
4996 /* IT->current.overlay_string_index is the number of overlay strings
4997 that have already been consumed by IT. Copy some of the
4998 remaining overlay strings to IT->overlay_strings. */
4999 i = 0;
5000 j = it->current.overlay_string_index;
5001 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5002 {
5003 it->overlay_strings[i] = entries[j].string;
5004 it->string_overlays[i++] = entries[j++].overlay;
5005 }
5006
5007 CHECK_IT (it);
5008 }
5009
5010
5011 /* Get the first chunk of overlay strings at IT's current buffer
5012 position, or at CHARPOS if that is > 0. Value is non-zero if at
5013 least one overlay string was found. */
5014
5015 static int
5016 get_overlay_strings_1 (struct it *it, int charpos, int compute_stop_p)
5017 {
5018 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5019 process. This fills IT->overlay_strings with strings, and sets
5020 IT->n_overlay_strings to the total number of strings to process.
5021 IT->pos.overlay_string_index has to be set temporarily to zero
5022 because load_overlay_strings needs this; it must be set to -1
5023 when no overlay strings are found because a zero value would
5024 indicate a position in the first overlay string. */
5025 it->current.overlay_string_index = 0;
5026 load_overlay_strings (it, charpos);
5027
5028 /* If we found overlay strings, set up IT to deliver display
5029 elements from the first one. Otherwise set up IT to deliver
5030 from current_buffer. */
5031 if (it->n_overlay_strings)
5032 {
5033 /* Make sure we know settings in current_buffer, so that we can
5034 restore meaningful values when we're done with the overlay
5035 strings. */
5036 if (compute_stop_p)
5037 compute_stop_pos (it);
5038 xassert (it->face_id >= 0);
5039
5040 /* Save IT's settings. They are restored after all overlay
5041 strings have been processed. */
5042 xassert (!compute_stop_p || it->sp == 0);
5043
5044 /* When called from handle_stop, there might be an empty display
5045 string loaded. In that case, don't bother saving it. */
5046 if (!STRINGP (it->string) || SCHARS (it->string))
5047 push_it (it);
5048
5049 /* Set up IT to deliver display elements from the first overlay
5050 string. */
5051 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5052 it->string = it->overlay_strings[0];
5053 it->from_overlay = Qnil;
5054 it->stop_charpos = 0;
5055 xassert (STRINGP (it->string));
5056 it->end_charpos = SCHARS (it->string);
5057 it->multibyte_p = STRING_MULTIBYTE (it->string);
5058 it->method = GET_FROM_STRING;
5059 return 1;
5060 }
5061
5062 it->current.overlay_string_index = -1;
5063 return 0;
5064 }
5065
5066 static int
5067 get_overlay_strings (struct it *it, int charpos)
5068 {
5069 it->string = Qnil;
5070 it->method = GET_FROM_BUFFER;
5071
5072 (void) get_overlay_strings_1 (it, charpos, 1);
5073
5074 CHECK_IT (it);
5075
5076 /* Value is non-zero if we found at least one overlay string. */
5077 return STRINGP (it->string);
5078 }
5079
5080
5081 \f
5082 /***********************************************************************
5083 Saving and restoring state
5084 ***********************************************************************/
5085
5086 /* Save current settings of IT on IT->stack. Called, for example,
5087 before setting up IT for an overlay string, to be able to restore
5088 IT's settings to what they were after the overlay string has been
5089 processed. */
5090
5091 static void
5092 push_it (struct it *it)
5093 {
5094 struct iterator_stack_entry *p;
5095
5096 xassert (it->sp < IT_STACK_SIZE);
5097 p = it->stack + it->sp;
5098
5099 p->stop_charpos = it->stop_charpos;
5100 p->prev_stop = it->prev_stop;
5101 p->base_level_stop = it->base_level_stop;
5102 p->cmp_it = it->cmp_it;
5103 xassert (it->face_id >= 0);
5104 p->face_id = it->face_id;
5105 p->string = it->string;
5106 p->method = it->method;
5107 p->from_overlay = it->from_overlay;
5108 switch (p->method)
5109 {
5110 case GET_FROM_IMAGE:
5111 p->u.image.object = it->object;
5112 p->u.image.image_id = it->image_id;
5113 p->u.image.slice = it->slice;
5114 break;
5115 case GET_FROM_STRETCH:
5116 p->u.stretch.object = it->object;
5117 break;
5118 }
5119 p->position = it->position;
5120 p->current = it->current;
5121 p->end_charpos = it->end_charpos;
5122 p->string_nchars = it->string_nchars;
5123 p->area = it->area;
5124 p->multibyte_p = it->multibyte_p;
5125 p->avoid_cursor_p = it->avoid_cursor_p;
5126 p->space_width = it->space_width;
5127 p->font_height = it->font_height;
5128 p->voffset = it->voffset;
5129 p->string_from_display_prop_p = it->string_from_display_prop_p;
5130 p->display_ellipsis_p = 0;
5131 p->line_wrap = it->line_wrap;
5132 ++it->sp;
5133 }
5134
5135 static void
5136 iterate_out_of_display_property (struct it *it)
5137 {
5138 /* Maybe initialize paragraph direction. If we are at the beginning
5139 of a new paragraph, next_element_from_buffer may not have a
5140 chance to do that. */
5141 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
5142 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
5143 /* prev_stop can be zero, so check against BEGV as well. */
5144 while (it->bidi_it.charpos >= BEGV
5145 && it->prev_stop <= it->bidi_it.charpos
5146 && it->bidi_it.charpos < CHARPOS (it->position))
5147 bidi_move_to_visually_next (&it->bidi_it);
5148 /* Record the stop_pos we just crossed, for when we cross it
5149 back, maybe. */
5150 if (it->bidi_it.charpos > CHARPOS (it->position))
5151 it->prev_stop = CHARPOS (it->position);
5152 /* If we ended up not where pop_it put us, resync IT's
5153 positional members with the bidi iterator. */
5154 if (it->bidi_it.charpos != CHARPOS (it->position))
5155 {
5156 SET_TEXT_POS (it->position,
5157 it->bidi_it.charpos, it->bidi_it.bytepos);
5158 it->current.pos = it->position;
5159 }
5160 }
5161
5162 /* Restore IT's settings from IT->stack. Called, for example, when no
5163 more overlay strings must be processed, and we return to delivering
5164 display elements from a buffer, or when the end of a string from a
5165 `display' property is reached and we return to delivering display
5166 elements from an overlay string, or from a buffer. */
5167
5168 static void
5169 pop_it (struct it *it)
5170 {
5171 struct iterator_stack_entry *p;
5172
5173 xassert (it->sp > 0);
5174 --it->sp;
5175 p = it->stack + it->sp;
5176 it->stop_charpos = p->stop_charpos;
5177 it->prev_stop = p->prev_stop;
5178 it->base_level_stop = p->base_level_stop;
5179 it->cmp_it = p->cmp_it;
5180 it->face_id = p->face_id;
5181 it->current = p->current;
5182 it->position = p->position;
5183 it->string = p->string;
5184 it->from_overlay = p->from_overlay;
5185 if (NILP (it->string))
5186 SET_TEXT_POS (it->current.string_pos, -1, -1);
5187 it->method = p->method;
5188 switch (it->method)
5189 {
5190 case GET_FROM_IMAGE:
5191 it->image_id = p->u.image.image_id;
5192 it->object = p->u.image.object;
5193 it->slice = p->u.image.slice;
5194 break;
5195 case GET_FROM_STRETCH:
5196 it->object = p->u.comp.object;
5197 break;
5198 case GET_FROM_BUFFER:
5199 it->object = it->w->buffer;
5200 if (it->bidi_p)
5201 {
5202 /* Bidi-iterate until we get out of the portion of text, if
5203 any, covered by a `display' text property or an overlay
5204 with `display' property. (We cannot just jump there,
5205 because the internal coherency of the bidi iterator state
5206 can not be preserved across such jumps.) We also must
5207 determine the paragraph base direction if the overlay we
5208 just processed is at the beginning of a new
5209 paragraph. */
5210 iterate_out_of_display_property (it);
5211 }
5212 break;
5213 case GET_FROM_STRING:
5214 it->object = it->string;
5215 break;
5216 case GET_FROM_DISPLAY_VECTOR:
5217 if (it->s)
5218 it->method = GET_FROM_C_STRING;
5219 else if (STRINGP (it->string))
5220 it->method = GET_FROM_STRING;
5221 else
5222 {
5223 it->method = GET_FROM_BUFFER;
5224 it->object = it->w->buffer;
5225 }
5226 }
5227 it->end_charpos = p->end_charpos;
5228 it->string_nchars = p->string_nchars;
5229 it->area = p->area;
5230 it->multibyte_p = p->multibyte_p;
5231 it->avoid_cursor_p = p->avoid_cursor_p;
5232 it->space_width = p->space_width;
5233 it->font_height = p->font_height;
5234 it->voffset = p->voffset;
5235 it->string_from_display_prop_p = p->string_from_display_prop_p;
5236 it->line_wrap = p->line_wrap;
5237 }
5238
5239
5240 \f
5241 /***********************************************************************
5242 Moving over lines
5243 ***********************************************************************/
5244
5245 /* Set IT's current position to the previous line start. */
5246
5247 static void
5248 back_to_previous_line_start (struct it *it)
5249 {
5250 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5251 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5252 }
5253
5254
5255 /* Move IT to the next line start.
5256
5257 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5258 we skipped over part of the text (as opposed to moving the iterator
5259 continuously over the text). Otherwise, don't change the value
5260 of *SKIPPED_P.
5261
5262 Newlines may come from buffer text, overlay strings, or strings
5263 displayed via the `display' property. That's the reason we can't
5264 simply use find_next_newline_no_quit.
5265
5266 Note that this function may not skip over invisible text that is so
5267 because of text properties and immediately follows a newline. If
5268 it would, function reseat_at_next_visible_line_start, when called
5269 from set_iterator_to_next, would effectively make invisible
5270 characters following a newline part of the wrong glyph row, which
5271 leads to wrong cursor motion. */
5272
5273 static int
5274 forward_to_next_line_start (struct it *it, int *skipped_p)
5275 {
5276 int old_selective, newline_found_p, n;
5277 const int MAX_NEWLINE_DISTANCE = 500;
5278
5279 /* If already on a newline, just consume it to avoid unintended
5280 skipping over invisible text below. */
5281 if (it->what == IT_CHARACTER
5282 && it->c == '\n'
5283 && CHARPOS (it->position) == IT_CHARPOS (*it))
5284 {
5285 set_iterator_to_next (it, 0);
5286 it->c = 0;
5287 return 1;
5288 }
5289
5290 /* Don't handle selective display in the following. It's (a)
5291 unnecessary because it's done by the caller, and (b) leads to an
5292 infinite recursion because next_element_from_ellipsis indirectly
5293 calls this function. */
5294 old_selective = it->selective;
5295 it->selective = 0;
5296
5297 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5298 from buffer text. */
5299 for (n = newline_found_p = 0;
5300 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5301 n += STRINGP (it->string) ? 0 : 1)
5302 {
5303 if (!get_next_display_element (it))
5304 return 0;
5305 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5306 set_iterator_to_next (it, 0);
5307 }
5308
5309 /* If we didn't find a newline near enough, see if we can use a
5310 short-cut. */
5311 if (!newline_found_p)
5312 {
5313 int start = IT_CHARPOS (*it);
5314 int limit = find_next_newline_no_quit (start, 1);
5315 Lisp_Object pos;
5316
5317 xassert (!STRINGP (it->string));
5318
5319 /* If there isn't any `display' property in sight, and no
5320 overlays, we can just use the position of the newline in
5321 buffer text. */
5322 if (it->stop_charpos >= limit
5323 || ((pos = Fnext_single_property_change (make_number (start),
5324 Qdisplay,
5325 Qnil, make_number (limit)),
5326 NILP (pos))
5327 && next_overlay_change (start) == ZV))
5328 {
5329 IT_CHARPOS (*it) = limit;
5330 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5331 *skipped_p = newline_found_p = 1;
5332 }
5333 else
5334 {
5335 while (get_next_display_element (it)
5336 && !newline_found_p)
5337 {
5338 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5339 set_iterator_to_next (it, 0);
5340 }
5341 }
5342 }
5343
5344 it->selective = old_selective;
5345 return newline_found_p;
5346 }
5347
5348
5349 /* Set IT's current position to the previous visible line start. Skip
5350 invisible text that is so either due to text properties or due to
5351 selective display. Caution: this does not change IT->current_x and
5352 IT->hpos. */
5353
5354 static void
5355 back_to_previous_visible_line_start (struct it *it)
5356 {
5357 while (IT_CHARPOS (*it) > BEGV)
5358 {
5359 back_to_previous_line_start (it);
5360
5361 if (IT_CHARPOS (*it) <= BEGV)
5362 break;
5363
5364 /* If selective > 0, then lines indented more than its value are
5365 invisible. */
5366 if (it->selective > 0
5367 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5368 (double) it->selective)) /* iftc */
5369 continue;
5370
5371 /* Check the newline before point for invisibility. */
5372 {
5373 Lisp_Object prop;
5374 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5375 Qinvisible, it->window);
5376 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5377 continue;
5378 }
5379
5380 if (IT_CHARPOS (*it) <= BEGV)
5381 break;
5382
5383 {
5384 struct it it2;
5385 int pos;
5386 EMACS_INT beg, end;
5387 Lisp_Object val, overlay;
5388
5389 /* If newline is part of a composition, continue from start of composition */
5390 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5391 && beg < IT_CHARPOS (*it))
5392 goto replaced;
5393
5394 /* If newline is replaced by a display property, find start of overlay
5395 or interval and continue search from that point. */
5396 it2 = *it;
5397 pos = --IT_CHARPOS (it2);
5398 --IT_BYTEPOS (it2);
5399 it2.sp = 0;
5400 it2.string_from_display_prop_p = 0;
5401 if (handle_display_prop (&it2) == HANDLED_RETURN
5402 && !NILP (val = get_char_property_and_overlay
5403 (make_number (pos), Qdisplay, Qnil, &overlay))
5404 && (OVERLAYP (overlay)
5405 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5406 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5407 goto replaced;
5408
5409 /* Newline is not replaced by anything -- so we are done. */
5410 break;
5411
5412 replaced:
5413 if (beg < BEGV)
5414 beg = BEGV;
5415 IT_CHARPOS (*it) = beg;
5416 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5417 }
5418 }
5419
5420 it->continuation_lines_width = 0;
5421
5422 xassert (IT_CHARPOS (*it) >= BEGV);
5423 xassert (IT_CHARPOS (*it) == BEGV
5424 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5425 CHECK_IT (it);
5426 }
5427
5428
5429 /* Reseat iterator IT at the previous visible line start. Skip
5430 invisible text that is so either due to text properties or due to
5431 selective display. At the end, update IT's overlay information,
5432 face information etc. */
5433
5434 void
5435 reseat_at_previous_visible_line_start (struct it *it)
5436 {
5437 back_to_previous_visible_line_start (it);
5438 reseat (it, it->current.pos, 1);
5439 CHECK_IT (it);
5440 }
5441
5442
5443 /* Reseat iterator IT on the next visible line start in the current
5444 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5445 preceding the line start. Skip over invisible text that is so
5446 because of selective display. Compute faces, overlays etc at the
5447 new position. Note that this function does not skip over text that
5448 is invisible because of text properties. */
5449
5450 static void
5451 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5452 {
5453 int newline_found_p, skipped_p = 0;
5454
5455 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5456
5457 /* Skip over lines that are invisible because they are indented
5458 more than the value of IT->selective. */
5459 if (it->selective > 0)
5460 while (IT_CHARPOS (*it) < ZV
5461 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5462 (double) it->selective)) /* iftc */
5463 {
5464 xassert (IT_BYTEPOS (*it) == BEGV
5465 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5466 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5467 }
5468
5469 /* Position on the newline if that's what's requested. */
5470 if (on_newline_p && newline_found_p)
5471 {
5472 if (STRINGP (it->string))
5473 {
5474 if (IT_STRING_CHARPOS (*it) > 0)
5475 {
5476 --IT_STRING_CHARPOS (*it);
5477 --IT_STRING_BYTEPOS (*it);
5478 }
5479 }
5480 else if (IT_CHARPOS (*it) > BEGV)
5481 {
5482 --IT_CHARPOS (*it);
5483 --IT_BYTEPOS (*it);
5484 reseat (it, it->current.pos, 0);
5485 }
5486 }
5487 else if (skipped_p)
5488 reseat (it, it->current.pos, 0);
5489
5490 CHECK_IT (it);
5491 }
5492
5493
5494 \f
5495 /***********************************************************************
5496 Changing an iterator's position
5497 ***********************************************************************/
5498
5499 /* Change IT's current position to POS in current_buffer. If FORCE_P
5500 is non-zero, always check for text properties at the new position.
5501 Otherwise, text properties are only looked up if POS >=
5502 IT->check_charpos of a property. */
5503
5504 static void
5505 reseat (struct it *it, struct text_pos pos, int force_p)
5506 {
5507 int original_pos = IT_CHARPOS (*it);
5508
5509 reseat_1 (it, pos, 0);
5510
5511 /* Determine where to check text properties. Avoid doing it
5512 where possible because text property lookup is very expensive. */
5513 if (force_p
5514 || CHARPOS (pos) > it->stop_charpos
5515 || CHARPOS (pos) < original_pos)
5516 {
5517 if (it->bidi_p)
5518 {
5519 /* For bidi iteration, we need to prime prev_stop and
5520 base_level_stop with our best estimations. */
5521 if (CHARPOS (pos) < it->prev_stop)
5522 {
5523 handle_stop_backwards (it, BEGV);
5524 if (CHARPOS (pos) < it->base_level_stop)
5525 it->base_level_stop = 0;
5526 }
5527 else if (CHARPOS (pos) > it->stop_charpos
5528 && it->stop_charpos >= BEGV)
5529 handle_stop_backwards (it, it->stop_charpos);
5530 else /* force_p */
5531 handle_stop (it);
5532 }
5533 else
5534 {
5535 handle_stop (it);
5536 it->prev_stop = it->base_level_stop = 0;
5537 }
5538
5539 }
5540
5541 CHECK_IT (it);
5542 }
5543
5544
5545 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5546 IT->stop_pos to POS, also. */
5547
5548 static void
5549 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5550 {
5551 /* Don't call this function when scanning a C string. */
5552 xassert (it->s == NULL);
5553
5554 /* POS must be a reasonable value. */
5555 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5556
5557 it->current.pos = it->position = pos;
5558 it->end_charpos = ZV;
5559 it->dpvec = NULL;
5560 it->current.dpvec_index = -1;
5561 it->current.overlay_string_index = -1;
5562 IT_STRING_CHARPOS (*it) = -1;
5563 IT_STRING_BYTEPOS (*it) = -1;
5564 it->string = Qnil;
5565 it->string_from_display_prop_p = 0;
5566 it->method = GET_FROM_BUFFER;
5567 it->object = it->w->buffer;
5568 it->area = TEXT_AREA;
5569 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5570 it->sp = 0;
5571 it->string_from_display_prop_p = 0;
5572 it->face_before_selective_p = 0;
5573 if (it->bidi_p)
5574 it->bidi_it.first_elt = 1;
5575
5576 if (set_stop_p)
5577 {
5578 it->stop_charpos = CHARPOS (pos);
5579 it->base_level_stop = CHARPOS (pos);
5580 }
5581 }
5582
5583
5584 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5585 If S is non-null, it is a C string to iterate over. Otherwise,
5586 STRING gives a Lisp string to iterate over.
5587
5588 If PRECISION > 0, don't return more then PRECISION number of
5589 characters from the string.
5590
5591 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5592 characters have been returned. FIELD_WIDTH < 0 means an infinite
5593 field width.
5594
5595 MULTIBYTE = 0 means disable processing of multibyte characters,
5596 MULTIBYTE > 0 means enable it,
5597 MULTIBYTE < 0 means use IT->multibyte_p.
5598
5599 IT must be initialized via a prior call to init_iterator before
5600 calling this function. */
5601
5602 static void
5603 reseat_to_string (struct it *it, const unsigned char *s, Lisp_Object string,
5604 int charpos, int precision, int field_width, int multibyte)
5605 {
5606 /* No region in strings. */
5607 it->region_beg_charpos = it->region_end_charpos = -1;
5608
5609 /* No text property checks performed by default, but see below. */
5610 it->stop_charpos = -1;
5611
5612 /* Set iterator position and end position. */
5613 memset (&it->current, 0, sizeof it->current);
5614 it->current.overlay_string_index = -1;
5615 it->current.dpvec_index = -1;
5616 xassert (charpos >= 0);
5617
5618 /* If STRING is specified, use its multibyteness, otherwise use the
5619 setting of MULTIBYTE, if specified. */
5620 if (multibyte >= 0)
5621 it->multibyte_p = multibyte > 0;
5622
5623 if (s == NULL)
5624 {
5625 xassert (STRINGP (string));
5626 it->string = string;
5627 it->s = NULL;
5628 it->end_charpos = it->string_nchars = SCHARS (string);
5629 it->method = GET_FROM_STRING;
5630 it->current.string_pos = string_pos (charpos, string);
5631 }
5632 else
5633 {
5634 it->s = s;
5635 it->string = Qnil;
5636
5637 /* Note that we use IT->current.pos, not it->current.string_pos,
5638 for displaying C strings. */
5639 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5640 if (it->multibyte_p)
5641 {
5642 it->current.pos = c_string_pos (charpos, s, 1);
5643 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5644 }
5645 else
5646 {
5647 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5648 it->end_charpos = it->string_nchars = strlen (s);
5649 }
5650
5651 it->method = GET_FROM_C_STRING;
5652 }
5653
5654 /* PRECISION > 0 means don't return more than PRECISION characters
5655 from the string. */
5656 if (precision > 0 && it->end_charpos - charpos > precision)
5657 it->end_charpos = it->string_nchars = charpos + precision;
5658
5659 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5660 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5661 FIELD_WIDTH < 0 means infinite field width. This is useful for
5662 padding with `-' at the end of a mode line. */
5663 if (field_width < 0)
5664 field_width = INFINITY;
5665 if (field_width > it->end_charpos - charpos)
5666 it->end_charpos = charpos + field_width;
5667
5668 /* Use the standard display table for displaying strings. */
5669 if (DISP_TABLE_P (Vstandard_display_table))
5670 it->dp = XCHAR_TABLE (Vstandard_display_table);
5671
5672 it->stop_charpos = charpos;
5673 if (s == NULL && it->multibyte_p)
5674 {
5675 EMACS_INT endpos = SCHARS (it->string);
5676 if (endpos > it->end_charpos)
5677 endpos = it->end_charpos;
5678 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5679 it->string);
5680 }
5681 CHECK_IT (it);
5682 }
5683
5684
5685 \f
5686 /***********************************************************************
5687 Iteration
5688 ***********************************************************************/
5689
5690 /* Map enum it_method value to corresponding next_element_from_* function. */
5691
5692 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5693 {
5694 next_element_from_buffer,
5695 next_element_from_display_vector,
5696 next_element_from_string,
5697 next_element_from_c_string,
5698 next_element_from_image,
5699 next_element_from_stretch
5700 };
5701
5702 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5703
5704
5705 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5706 (possibly with the following characters). */
5707
5708 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5709 ((IT)->cmp_it.id >= 0 \
5710 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5711 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5712 END_CHARPOS, (IT)->w, \
5713 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5714 (IT)->string)))
5715
5716
5717 /* Load IT's display element fields with information about the next
5718 display element from the current position of IT. Value is zero if
5719 end of buffer (or C string) is reached. */
5720
5721 static struct frame *last_escape_glyph_frame = NULL;
5722 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5723 static int last_escape_glyph_merged_face_id = 0;
5724
5725 int
5726 get_next_display_element (struct it *it)
5727 {
5728 /* Non-zero means that we found a display element. Zero means that
5729 we hit the end of what we iterate over. Performance note: the
5730 function pointer `method' used here turns out to be faster than
5731 using a sequence of if-statements. */
5732 int success_p;
5733
5734 get_next:
5735 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5736
5737 if (it->what == IT_CHARACTER)
5738 {
5739 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5740 and only if (a) the resolved directionality of that character
5741 is R..." */
5742 /* FIXME: Do we need an exception for characters from display
5743 tables? */
5744 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5745 it->c = bidi_mirror_char (it->c);
5746 /* Map via display table or translate control characters.
5747 IT->c, IT->len etc. have been set to the next character by
5748 the function call above. If we have a display table, and it
5749 contains an entry for IT->c, translate it. Don't do this if
5750 IT->c itself comes from a display table, otherwise we could
5751 end up in an infinite recursion. (An alternative could be to
5752 count the recursion depth of this function and signal an
5753 error when a certain maximum depth is reached.) Is it worth
5754 it? */
5755 if (success_p && it->dpvec == NULL)
5756 {
5757 Lisp_Object dv;
5758 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5759 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5760 nbsp_or_shy = char_is_other;
5761 int decoded = it->c;
5762
5763 if (it->dp
5764 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5765 VECTORP (dv)))
5766 {
5767 struct Lisp_Vector *v = XVECTOR (dv);
5768
5769 /* Return the first character from the display table
5770 entry, if not empty. If empty, don't display the
5771 current character. */
5772 if (v->size)
5773 {
5774 it->dpvec_char_len = it->len;
5775 it->dpvec = v->contents;
5776 it->dpend = v->contents + v->size;
5777 it->current.dpvec_index = 0;
5778 it->dpvec_face_id = -1;
5779 it->saved_face_id = it->face_id;
5780 it->method = GET_FROM_DISPLAY_VECTOR;
5781 it->ellipsis_p = 0;
5782 }
5783 else
5784 {
5785 set_iterator_to_next (it, 0);
5786 }
5787 goto get_next;
5788 }
5789
5790 if (unibyte_display_via_language_environment
5791 && !ASCII_CHAR_P (it->c))
5792 decoded = DECODE_CHAR (unibyte, it->c);
5793
5794 if (it->c >= 0x80 && ! NILP (Vnobreak_char_display))
5795 {
5796 if (it->multibyte_p)
5797 nbsp_or_shy = (it->c == 0xA0 ? char_is_nbsp
5798 : it->c == 0xAD ? char_is_soft_hyphen
5799 : char_is_other);
5800 else if (unibyte_display_via_language_environment)
5801 nbsp_or_shy = (decoded == 0xA0 ? char_is_nbsp
5802 : decoded == 0xAD ? char_is_soft_hyphen
5803 : char_is_other);
5804 }
5805
5806 /* Translate control characters into `\003' or `^C' form.
5807 Control characters coming from a display table entry are
5808 currently not translated because we use IT->dpvec to hold
5809 the translation. This could easily be changed but I
5810 don't believe that it is worth doing.
5811
5812 If it->multibyte_p is nonzero, non-printable non-ASCII
5813 characters are also translated to octal form.
5814
5815 If it->multibyte_p is zero, eight-bit characters that
5816 don't have corresponding multibyte char code are also
5817 translated to octal form. */
5818 if ((it->c < ' '
5819 ? (it->area != TEXT_AREA
5820 /* In mode line, treat \n, \t like other crl chars. */
5821 || (it->c != '\t'
5822 && it->glyph_row
5823 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5824 || (it->c != '\n' && it->c != '\t'))
5825 : (nbsp_or_shy
5826 || (it->multibyte_p
5827 ? ! CHAR_PRINTABLE_P (it->c)
5828 : (! unibyte_display_via_language_environment
5829 ? it->c >= 0x80
5830 : (decoded >= 0x80 && decoded < 0xA0))))))
5831 {
5832 /* IT->c is a control character which must be displayed
5833 either as '\003' or as `^C' where the '\\' and '^'
5834 can be defined in the display table. Fill
5835 IT->ctl_chars with glyphs for what we have to
5836 display. Then, set IT->dpvec to these glyphs. */
5837 Lisp_Object gc;
5838 int ctl_len;
5839 int face_id, lface_id = 0 ;
5840 int escape_glyph;
5841
5842 /* Handle control characters with ^. */
5843
5844 if (it->c < 128 && it->ctl_arrow_p)
5845 {
5846 int g;
5847
5848 g = '^'; /* default glyph for Control */
5849 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5850 if (it->dp
5851 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5852 && GLYPH_CODE_CHAR_VALID_P (gc))
5853 {
5854 g = GLYPH_CODE_CHAR (gc);
5855 lface_id = GLYPH_CODE_FACE (gc);
5856 }
5857 if (lface_id)
5858 {
5859 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5860 }
5861 else if (it->f == last_escape_glyph_frame
5862 && it->face_id == last_escape_glyph_face_id)
5863 {
5864 face_id = last_escape_glyph_merged_face_id;
5865 }
5866 else
5867 {
5868 /* Merge the escape-glyph face into the current face. */
5869 face_id = merge_faces (it->f, Qescape_glyph, 0,
5870 it->face_id);
5871 last_escape_glyph_frame = it->f;
5872 last_escape_glyph_face_id = it->face_id;
5873 last_escape_glyph_merged_face_id = face_id;
5874 }
5875
5876 XSETINT (it->ctl_chars[0], g);
5877 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5878 ctl_len = 2;
5879 goto display_control;
5880 }
5881
5882 /* Handle non-break space in the mode where it only gets
5883 highlighting. */
5884
5885 if (EQ (Vnobreak_char_display, Qt)
5886 && nbsp_or_shy == char_is_nbsp)
5887 {
5888 /* Merge the no-break-space face into the current face. */
5889 face_id = merge_faces (it->f, Qnobreak_space, 0,
5890 it->face_id);
5891
5892 it->c = ' ';
5893 XSETINT (it->ctl_chars[0], ' ');
5894 ctl_len = 1;
5895 goto display_control;
5896 }
5897
5898 /* Handle sequences that start with the "escape glyph". */
5899
5900 /* the default escape glyph is \. */
5901 escape_glyph = '\\';
5902
5903 if (it->dp
5904 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5905 && GLYPH_CODE_CHAR_VALID_P (gc))
5906 {
5907 escape_glyph = GLYPH_CODE_CHAR (gc);
5908 lface_id = GLYPH_CODE_FACE (gc);
5909 }
5910 if (lface_id)
5911 {
5912 /* The display table specified a face.
5913 Merge it into face_id and also into escape_glyph. */
5914 face_id = merge_faces (it->f, Qt, lface_id,
5915 it->face_id);
5916 }
5917 else if (it->f == last_escape_glyph_frame
5918 && it->face_id == last_escape_glyph_face_id)
5919 {
5920 face_id = last_escape_glyph_merged_face_id;
5921 }
5922 else
5923 {
5924 /* Merge the escape-glyph face into the current face. */
5925 face_id = merge_faces (it->f, Qescape_glyph, 0,
5926 it->face_id);
5927 last_escape_glyph_frame = it->f;
5928 last_escape_glyph_face_id = it->face_id;
5929 last_escape_glyph_merged_face_id = face_id;
5930 }
5931
5932 /* Handle soft hyphens in the mode where they only get
5933 highlighting. */
5934
5935 if (EQ (Vnobreak_char_display, Qt)
5936 && nbsp_or_shy == char_is_soft_hyphen)
5937 {
5938 it->c = '-';
5939 XSETINT (it->ctl_chars[0], '-');
5940 ctl_len = 1;
5941 goto display_control;
5942 }
5943
5944 /* Handle non-break space and soft hyphen
5945 with the escape glyph. */
5946
5947 if (nbsp_or_shy)
5948 {
5949 XSETINT (it->ctl_chars[0], escape_glyph);
5950 it->c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5951 XSETINT (it->ctl_chars[1], it->c);
5952 ctl_len = 2;
5953 goto display_control;
5954 }
5955
5956 {
5957 unsigned char str[MAX_MULTIBYTE_LENGTH];
5958 int len;
5959 int i;
5960
5961 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5962 if (CHAR_BYTE8_P (it->c))
5963 {
5964 str[0] = CHAR_TO_BYTE8 (it->c);
5965 len = 1;
5966 }
5967 else if (it->c < 256)
5968 {
5969 str[0] = it->c;
5970 len = 1;
5971 }
5972 else
5973 {
5974 /* It's an invalid character, which shouldn't
5975 happen actually, but due to bugs it may
5976 happen. Let's print the char as is, there's
5977 not much meaningful we can do with it. */
5978 str[0] = it->c;
5979 str[1] = it->c >> 8;
5980 str[2] = it->c >> 16;
5981 str[3] = it->c >> 24;
5982 len = 4;
5983 }
5984
5985 for (i = 0; i < len; i++)
5986 {
5987 int g;
5988 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5989 /* Insert three more glyphs into IT->ctl_chars for
5990 the octal display of the character. */
5991 g = ((str[i] >> 6) & 7) + '0';
5992 XSETINT (it->ctl_chars[i * 4 + 1], g);
5993 g = ((str[i] >> 3) & 7) + '0';
5994 XSETINT (it->ctl_chars[i * 4 + 2], g);
5995 g = (str[i] & 7) + '0';
5996 XSETINT (it->ctl_chars[i * 4 + 3], g);
5997 }
5998 ctl_len = len * 4;
5999 }
6000
6001 display_control:
6002 /* Set up IT->dpvec and return first character from it. */
6003 it->dpvec_char_len = it->len;
6004 it->dpvec = it->ctl_chars;
6005 it->dpend = it->dpvec + ctl_len;
6006 it->current.dpvec_index = 0;
6007 it->dpvec_face_id = face_id;
6008 it->saved_face_id = it->face_id;
6009 it->method = GET_FROM_DISPLAY_VECTOR;
6010 it->ellipsis_p = 0;
6011 goto get_next;
6012 }
6013 }
6014 }
6015
6016 #ifdef HAVE_WINDOW_SYSTEM
6017 /* Adjust face id for a multibyte character. There are no multibyte
6018 character in unibyte text. */
6019 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6020 && it->multibyte_p
6021 && success_p
6022 && FRAME_WINDOW_P (it->f))
6023 {
6024 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6025
6026 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6027 {
6028 /* Automatic composition with glyph-string. */
6029 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6030
6031 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6032 }
6033 else
6034 {
6035 int pos = (it->s ? -1
6036 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6037 : IT_CHARPOS (*it));
6038
6039 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
6040 }
6041 }
6042 #endif
6043
6044 /* Is this character the last one of a run of characters with
6045 box? If yes, set IT->end_of_box_run_p to 1. */
6046 if (it->face_box_p
6047 && it->s == NULL)
6048 {
6049 if (it->method == GET_FROM_STRING && it->sp)
6050 {
6051 int face_id = underlying_face_id (it);
6052 struct face *face = FACE_FROM_ID (it->f, face_id);
6053
6054 if (face)
6055 {
6056 if (face->box == FACE_NO_BOX)
6057 {
6058 /* If the box comes from face properties in a
6059 display string, check faces in that string. */
6060 int string_face_id = face_after_it_pos (it);
6061 it->end_of_box_run_p
6062 = (FACE_FROM_ID (it->f, string_face_id)->box
6063 == FACE_NO_BOX);
6064 }
6065 /* Otherwise, the box comes from the underlying face.
6066 If this is the last string character displayed, check
6067 the next buffer location. */
6068 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6069 && (it->current.overlay_string_index
6070 == it->n_overlay_strings - 1))
6071 {
6072 EMACS_INT ignore;
6073 int next_face_id;
6074 struct text_pos pos = it->current.pos;
6075 INC_TEXT_POS (pos, it->multibyte_p);
6076
6077 next_face_id = face_at_buffer_position
6078 (it->w, CHARPOS (pos), it->region_beg_charpos,
6079 it->region_end_charpos, &ignore,
6080 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6081 -1);
6082 it->end_of_box_run_p
6083 = (FACE_FROM_ID (it->f, next_face_id)->box
6084 == FACE_NO_BOX);
6085 }
6086 }
6087 }
6088 else
6089 {
6090 int face_id = face_after_it_pos (it);
6091 it->end_of_box_run_p
6092 = (face_id != it->face_id
6093 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6094 }
6095 }
6096
6097 /* Value is 0 if end of buffer or string reached. */
6098 return success_p;
6099 }
6100
6101
6102 /* Move IT to the next display element.
6103
6104 RESEAT_P non-zero means if called on a newline in buffer text,
6105 skip to the next visible line start.
6106
6107 Functions get_next_display_element and set_iterator_to_next are
6108 separate because I find this arrangement easier to handle than a
6109 get_next_display_element function that also increments IT's
6110 position. The way it is we can first look at an iterator's current
6111 display element, decide whether it fits on a line, and if it does,
6112 increment the iterator position. The other way around we probably
6113 would either need a flag indicating whether the iterator has to be
6114 incremented the next time, or we would have to implement a
6115 decrement position function which would not be easy to write. */
6116
6117 void
6118 set_iterator_to_next (struct it *it, int reseat_p)
6119 {
6120 /* Reset flags indicating start and end of a sequence of characters
6121 with box. Reset them at the start of this function because
6122 moving the iterator to a new position might set them. */
6123 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6124
6125 switch (it->method)
6126 {
6127 case GET_FROM_BUFFER:
6128 /* The current display element of IT is a character from
6129 current_buffer. Advance in the buffer, and maybe skip over
6130 invisible lines that are so because of selective display. */
6131 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6132 reseat_at_next_visible_line_start (it, 0);
6133 else if (it->cmp_it.id >= 0)
6134 {
6135 /* We are currently getting glyphs from a composition. */
6136 int i;
6137
6138 if (! it->bidi_p)
6139 {
6140 IT_CHARPOS (*it) += it->cmp_it.nchars;
6141 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6142 if (it->cmp_it.to < it->cmp_it.nglyphs)
6143 {
6144 it->cmp_it.from = it->cmp_it.to;
6145 }
6146 else
6147 {
6148 it->cmp_it.id = -1;
6149 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6150 IT_BYTEPOS (*it),
6151 it->stop_charpos, Qnil);
6152 }
6153 }
6154 else if (! it->cmp_it.reversed_p)
6155 {
6156 /* Composition created while scanning forward. */
6157 /* Update IT's char/byte positions to point to the first
6158 character of the next grapheme cluster, or to the
6159 character visually after the current composition. */
6160 for (i = 0; i < it->cmp_it.nchars; i++)
6161 bidi_move_to_visually_next (&it->bidi_it);
6162 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6163 IT_CHARPOS (*it) = it->bidi_it.charpos;
6164
6165 if (it->cmp_it.to < it->cmp_it.nglyphs)
6166 {
6167 /* Proceed to the next grapheme cluster. */
6168 it->cmp_it.from = it->cmp_it.to;
6169 }
6170 else
6171 {
6172 /* No more grapheme clusters in this composition.
6173 Find the next stop position. */
6174 EMACS_INT stop = it->stop_charpos;
6175 if (it->bidi_it.scan_dir < 0)
6176 /* Now we are scanning backward and don't know
6177 where to stop. */
6178 stop = -1;
6179 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6180 IT_BYTEPOS (*it), stop, Qnil);
6181 }
6182 }
6183 else
6184 {
6185 /* Composition created while scanning backward. */
6186 /* Update IT's char/byte positions to point to the last
6187 character of the previous grapheme cluster, or the
6188 character visually after the current composition. */
6189 for (i = 0; i < it->cmp_it.nchars; i++)
6190 bidi_move_to_visually_next (&it->bidi_it);
6191 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6192 IT_CHARPOS (*it) = it->bidi_it.charpos;
6193 if (it->cmp_it.from > 0)
6194 {
6195 /* Proceed to the previous grapheme cluster. */
6196 it->cmp_it.to = it->cmp_it.from;
6197 }
6198 else
6199 {
6200 /* No more grapheme clusters in this composition.
6201 Find the next stop position. */
6202 EMACS_INT stop = it->stop_charpos;
6203 if (it->bidi_it.scan_dir < 0)
6204 /* Now we are scanning backward and don't know
6205 where to stop. */
6206 stop = -1;
6207 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6208 IT_BYTEPOS (*it), stop, Qnil);
6209 }
6210 }
6211 }
6212 else
6213 {
6214 xassert (it->len != 0);
6215
6216 if (!it->bidi_p)
6217 {
6218 IT_BYTEPOS (*it) += it->len;
6219 IT_CHARPOS (*it) += 1;
6220 }
6221 else
6222 {
6223 int prev_scan_dir = it->bidi_it.scan_dir;
6224 /* If this is a new paragraph, determine its base
6225 direction (a.k.a. its base embedding level). */
6226 if (it->bidi_it.new_paragraph)
6227 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6228 bidi_move_to_visually_next (&it->bidi_it);
6229 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6230 IT_CHARPOS (*it) = it->bidi_it.charpos;
6231 if (prev_scan_dir != it->bidi_it.scan_dir)
6232 {
6233 /* As the scan direction was changed, we must
6234 re-compute the stop position for composition. */
6235 EMACS_INT stop = it->stop_charpos;
6236 if (it->bidi_it.scan_dir < 0)
6237 stop = -1;
6238 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6239 IT_BYTEPOS (*it), stop, Qnil);
6240 }
6241 }
6242 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6243 }
6244 break;
6245
6246 case GET_FROM_C_STRING:
6247 /* Current display element of IT is from a C string. */
6248 IT_BYTEPOS (*it) += it->len;
6249 IT_CHARPOS (*it) += 1;
6250 break;
6251
6252 case GET_FROM_DISPLAY_VECTOR:
6253 /* Current display element of IT is from a display table entry.
6254 Advance in the display table definition. Reset it to null if
6255 end reached, and continue with characters from buffers/
6256 strings. */
6257 ++it->current.dpvec_index;
6258
6259 /* Restore face of the iterator to what they were before the
6260 display vector entry (these entries may contain faces). */
6261 it->face_id = it->saved_face_id;
6262
6263 if (it->dpvec + it->current.dpvec_index == it->dpend)
6264 {
6265 int recheck_faces = it->ellipsis_p;
6266
6267 if (it->s)
6268 it->method = GET_FROM_C_STRING;
6269 else if (STRINGP (it->string))
6270 it->method = GET_FROM_STRING;
6271 else
6272 {
6273 it->method = GET_FROM_BUFFER;
6274 it->object = it->w->buffer;
6275 }
6276
6277 it->dpvec = NULL;
6278 it->current.dpvec_index = -1;
6279
6280 /* Skip over characters which were displayed via IT->dpvec. */
6281 if (it->dpvec_char_len < 0)
6282 reseat_at_next_visible_line_start (it, 1);
6283 else if (it->dpvec_char_len > 0)
6284 {
6285 if (it->method == GET_FROM_STRING
6286 && it->n_overlay_strings > 0)
6287 it->ignore_overlay_strings_at_pos_p = 1;
6288 it->len = it->dpvec_char_len;
6289 set_iterator_to_next (it, reseat_p);
6290 }
6291
6292 /* Maybe recheck faces after display vector */
6293 if (recheck_faces)
6294 it->stop_charpos = IT_CHARPOS (*it);
6295 }
6296 break;
6297
6298 case GET_FROM_STRING:
6299 /* Current display element is a character from a Lisp string. */
6300 xassert (it->s == NULL && STRINGP (it->string));
6301 if (it->cmp_it.id >= 0)
6302 {
6303 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6304 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6305 if (it->cmp_it.to < it->cmp_it.nglyphs)
6306 it->cmp_it.from = it->cmp_it.to;
6307 else
6308 {
6309 it->cmp_it.id = -1;
6310 composition_compute_stop_pos (&it->cmp_it,
6311 IT_STRING_CHARPOS (*it),
6312 IT_STRING_BYTEPOS (*it),
6313 it->stop_charpos, it->string);
6314 }
6315 }
6316 else
6317 {
6318 IT_STRING_BYTEPOS (*it) += it->len;
6319 IT_STRING_CHARPOS (*it) += 1;
6320 }
6321
6322 consider_string_end:
6323
6324 if (it->current.overlay_string_index >= 0)
6325 {
6326 /* IT->string is an overlay string. Advance to the
6327 next, if there is one. */
6328 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6329 {
6330 it->ellipsis_p = 0;
6331 next_overlay_string (it);
6332 if (it->ellipsis_p)
6333 setup_for_ellipsis (it, 0);
6334 }
6335 }
6336 else
6337 {
6338 /* IT->string is not an overlay string. If we reached
6339 its end, and there is something on IT->stack, proceed
6340 with what is on the stack. This can be either another
6341 string, this time an overlay string, or a buffer. */
6342 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6343 && it->sp > 0)
6344 {
6345 pop_it (it);
6346 if (it->method == GET_FROM_STRING)
6347 goto consider_string_end;
6348 }
6349 }
6350 break;
6351
6352 case GET_FROM_IMAGE:
6353 case GET_FROM_STRETCH:
6354 /* The position etc with which we have to proceed are on
6355 the stack. The position may be at the end of a string,
6356 if the `display' property takes up the whole string. */
6357 xassert (it->sp > 0);
6358 pop_it (it);
6359 if (it->method == GET_FROM_STRING)
6360 goto consider_string_end;
6361 break;
6362
6363 default:
6364 /* There are no other methods defined, so this should be a bug. */
6365 abort ();
6366 }
6367
6368 xassert (it->method != GET_FROM_STRING
6369 || (STRINGP (it->string)
6370 && IT_STRING_CHARPOS (*it) >= 0));
6371 }
6372
6373 /* Load IT's display element fields with information about the next
6374 display element which comes from a display table entry or from the
6375 result of translating a control character to one of the forms `^C'
6376 or `\003'.
6377
6378 IT->dpvec holds the glyphs to return as characters.
6379 IT->saved_face_id holds the face id before the display vector--it
6380 is restored into IT->face_id in set_iterator_to_next. */
6381
6382 static int
6383 next_element_from_display_vector (struct it *it)
6384 {
6385 Lisp_Object gc;
6386
6387 /* Precondition. */
6388 xassert (it->dpvec && it->current.dpvec_index >= 0);
6389
6390 it->face_id = it->saved_face_id;
6391
6392 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6393 That seemed totally bogus - so I changed it... */
6394 gc = it->dpvec[it->current.dpvec_index];
6395
6396 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6397 {
6398 it->c = GLYPH_CODE_CHAR (gc);
6399 it->len = CHAR_BYTES (it->c);
6400
6401 /* The entry may contain a face id to use. Such a face id is
6402 the id of a Lisp face, not a realized face. A face id of
6403 zero means no face is specified. */
6404 if (it->dpvec_face_id >= 0)
6405 it->face_id = it->dpvec_face_id;
6406 else
6407 {
6408 int lface_id = GLYPH_CODE_FACE (gc);
6409 if (lface_id > 0)
6410 it->face_id = merge_faces (it->f, Qt, lface_id,
6411 it->saved_face_id);
6412 }
6413 }
6414 else
6415 /* Display table entry is invalid. Return a space. */
6416 it->c = ' ', it->len = 1;
6417
6418 /* Don't change position and object of the iterator here. They are
6419 still the values of the character that had this display table
6420 entry or was translated, and that's what we want. */
6421 it->what = IT_CHARACTER;
6422 return 1;
6423 }
6424
6425
6426 /* Load IT with the next display element from Lisp string IT->string.
6427 IT->current.string_pos is the current position within the string.
6428 If IT->current.overlay_string_index >= 0, the Lisp string is an
6429 overlay string. */
6430
6431 static int
6432 next_element_from_string (struct it *it)
6433 {
6434 struct text_pos position;
6435
6436 xassert (STRINGP (it->string));
6437 xassert (IT_STRING_CHARPOS (*it) >= 0);
6438 position = it->current.string_pos;
6439
6440 /* Time to check for invisible text? */
6441 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6442 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6443 {
6444 handle_stop (it);
6445
6446 /* Since a handler may have changed IT->method, we must
6447 recurse here. */
6448 return GET_NEXT_DISPLAY_ELEMENT (it);
6449 }
6450
6451 if (it->current.overlay_string_index >= 0)
6452 {
6453 /* Get the next character from an overlay string. In overlay
6454 strings, There is no field width or padding with spaces to
6455 do. */
6456 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6457 {
6458 it->what = IT_EOB;
6459 return 0;
6460 }
6461 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6462 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6463 && next_element_from_composition (it))
6464 {
6465 return 1;
6466 }
6467 else if (STRING_MULTIBYTE (it->string))
6468 {
6469 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6470 const unsigned char *s = (SDATA (it->string)
6471 + IT_STRING_BYTEPOS (*it));
6472 it->c = string_char_and_length (s, &it->len);
6473 }
6474 else
6475 {
6476 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6477 it->len = 1;
6478 }
6479 }
6480 else
6481 {
6482 /* Get the next character from a Lisp string that is not an
6483 overlay string. Such strings come from the mode line, for
6484 example. We may have to pad with spaces, or truncate the
6485 string. See also next_element_from_c_string. */
6486 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6487 {
6488 it->what = IT_EOB;
6489 return 0;
6490 }
6491 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6492 {
6493 /* Pad with spaces. */
6494 it->c = ' ', it->len = 1;
6495 CHARPOS (position) = BYTEPOS (position) = -1;
6496 }
6497 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6498 IT_STRING_BYTEPOS (*it), it->string_nchars)
6499 && next_element_from_composition (it))
6500 {
6501 return 1;
6502 }
6503 else if (STRING_MULTIBYTE (it->string))
6504 {
6505 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6506 const unsigned char *s = (SDATA (it->string)
6507 + IT_STRING_BYTEPOS (*it));
6508 it->c = string_char_and_length (s, &it->len);
6509 }
6510 else
6511 {
6512 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6513 it->len = 1;
6514 }
6515 }
6516
6517 /* Record what we have and where it came from. */
6518 it->what = IT_CHARACTER;
6519 it->object = it->string;
6520 it->position = position;
6521 return 1;
6522 }
6523
6524
6525 /* Load IT with next display element from C string IT->s.
6526 IT->string_nchars is the maximum number of characters to return
6527 from the string. IT->end_charpos may be greater than
6528 IT->string_nchars when this function is called, in which case we
6529 may have to return padding spaces. Value is zero if end of string
6530 reached, including padding spaces. */
6531
6532 static int
6533 next_element_from_c_string (struct it *it)
6534 {
6535 int success_p = 1;
6536
6537 xassert (it->s);
6538 it->what = IT_CHARACTER;
6539 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6540 it->object = Qnil;
6541
6542 /* IT's position can be greater IT->string_nchars in case a field
6543 width or precision has been specified when the iterator was
6544 initialized. */
6545 if (IT_CHARPOS (*it) >= it->end_charpos)
6546 {
6547 /* End of the game. */
6548 it->what = IT_EOB;
6549 success_p = 0;
6550 }
6551 else if (IT_CHARPOS (*it) >= it->string_nchars)
6552 {
6553 /* Pad with spaces. */
6554 it->c = ' ', it->len = 1;
6555 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6556 }
6557 else if (it->multibyte_p)
6558 {
6559 /* Implementation note: The calls to strlen apparently aren't a
6560 performance problem because there is no noticeable performance
6561 difference between Emacs running in unibyte or multibyte mode. */
6562 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6563 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6564 }
6565 else
6566 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6567
6568 return success_p;
6569 }
6570
6571
6572 /* Set up IT to return characters from an ellipsis, if appropriate.
6573 The definition of the ellipsis glyphs may come from a display table
6574 entry. This function fills IT with the first glyph from the
6575 ellipsis if an ellipsis is to be displayed. */
6576
6577 static int
6578 next_element_from_ellipsis (struct it *it)
6579 {
6580 if (it->selective_display_ellipsis_p)
6581 setup_for_ellipsis (it, it->len);
6582 else
6583 {
6584 /* The face at the current position may be different from the
6585 face we find after the invisible text. Remember what it
6586 was in IT->saved_face_id, and signal that it's there by
6587 setting face_before_selective_p. */
6588 it->saved_face_id = it->face_id;
6589 it->method = GET_FROM_BUFFER;
6590 it->object = it->w->buffer;
6591 reseat_at_next_visible_line_start (it, 1);
6592 it->face_before_selective_p = 1;
6593 }
6594
6595 return GET_NEXT_DISPLAY_ELEMENT (it);
6596 }
6597
6598
6599 /* Deliver an image display element. The iterator IT is already
6600 filled with image information (done in handle_display_prop). Value
6601 is always 1. */
6602
6603
6604 static int
6605 next_element_from_image (struct it *it)
6606 {
6607 it->what = IT_IMAGE;
6608 it->ignore_overlay_strings_at_pos_p = 0;
6609 return 1;
6610 }
6611
6612
6613 /* Fill iterator IT with next display element from a stretch glyph
6614 property. IT->object is the value of the text property. Value is
6615 always 1. */
6616
6617 static int
6618 next_element_from_stretch (struct it *it)
6619 {
6620 it->what = IT_STRETCH;
6621 return 1;
6622 }
6623
6624 /* Scan forward from CHARPOS in the current buffer, until we find a
6625 stop position > current IT's position. Then handle the stop
6626 position before that. This is called when we bump into a stop
6627 position while reordering bidirectional text. CHARPOS should be
6628 the last previously processed stop_pos (or BEGV, if none were
6629 processed yet) whose position is less that IT's current
6630 position. */
6631
6632 static void
6633 handle_stop_backwards (struct it *it, EMACS_INT charpos)
6634 {
6635 EMACS_INT where_we_are = IT_CHARPOS (*it);
6636 struct display_pos save_current = it->current;
6637 struct text_pos save_position = it->position;
6638 struct text_pos pos1;
6639 EMACS_INT next_stop;
6640
6641 /* Scan in strict logical order. */
6642 it->bidi_p = 0;
6643 do
6644 {
6645 it->prev_stop = charpos;
6646 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6647 reseat_1 (it, pos1, 0);
6648 compute_stop_pos (it);
6649 /* We must advance forward, right? */
6650 if (it->stop_charpos <= it->prev_stop)
6651 abort ();
6652 charpos = it->stop_charpos;
6653 }
6654 while (charpos <= where_we_are);
6655
6656 next_stop = it->stop_charpos;
6657 it->stop_charpos = it->prev_stop;
6658 it->bidi_p = 1;
6659 it->current = save_current;
6660 it->position = save_position;
6661 handle_stop (it);
6662 it->stop_charpos = next_stop;
6663 }
6664
6665 /* Load IT with the next display element from current_buffer. Value
6666 is zero if end of buffer reached. IT->stop_charpos is the next
6667 position at which to stop and check for text properties or buffer
6668 end. */
6669
6670 static int
6671 next_element_from_buffer (struct it *it)
6672 {
6673 int success_p = 1;
6674
6675 xassert (IT_CHARPOS (*it) >= BEGV);
6676
6677 /* With bidi reordering, the character to display might not be the
6678 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6679 we were reseat()ed to a new buffer position, which is potentially
6680 a different paragraph. */
6681 if (it->bidi_p && it->bidi_it.first_elt)
6682 {
6683 it->bidi_it.charpos = IT_CHARPOS (*it);
6684 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6685 if (it->bidi_it.bytepos == ZV_BYTE)
6686 {
6687 /* Nothing to do, but reset the FIRST_ELT flag, like
6688 bidi_paragraph_init does, because we are not going to
6689 call it. */
6690 it->bidi_it.first_elt = 0;
6691 }
6692 else if (it->bidi_it.bytepos == BEGV_BYTE
6693 /* FIXME: Should support all Unicode line separators. */
6694 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6695 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6696 {
6697 /* If we are at the beginning of a line, we can produce the
6698 next element right away. */
6699 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6700 bidi_move_to_visually_next (&it->bidi_it);
6701 }
6702 else
6703 {
6704 int orig_bytepos = IT_BYTEPOS (*it);
6705
6706 /* We need to prime the bidi iterator starting at the line's
6707 beginning, before we will be able to produce the next
6708 element. */
6709 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6710 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6711 it->bidi_it.charpos = IT_CHARPOS (*it);
6712 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6713 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6714 do
6715 {
6716 /* Now return to buffer position where we were asked to
6717 get the next display element, and produce that. */
6718 bidi_move_to_visually_next (&it->bidi_it);
6719 }
6720 while (it->bidi_it.bytepos != orig_bytepos
6721 && it->bidi_it.bytepos < ZV_BYTE);
6722 }
6723
6724 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6725 /* Adjust IT's position information to where we ended up. */
6726 IT_CHARPOS (*it) = it->bidi_it.charpos;
6727 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6728 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6729 {
6730 EMACS_INT stop = it->stop_charpos;
6731 if (it->bidi_it.scan_dir < 0)
6732 stop = -1;
6733 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6734 IT_BYTEPOS (*it), stop, Qnil);
6735 }
6736 }
6737
6738 if (IT_CHARPOS (*it) >= it->stop_charpos)
6739 {
6740 if (IT_CHARPOS (*it) >= it->end_charpos)
6741 {
6742 int overlay_strings_follow_p;
6743
6744 /* End of the game, except when overlay strings follow that
6745 haven't been returned yet. */
6746 if (it->overlay_strings_at_end_processed_p)
6747 overlay_strings_follow_p = 0;
6748 else
6749 {
6750 it->overlay_strings_at_end_processed_p = 1;
6751 overlay_strings_follow_p = get_overlay_strings (it, 0);
6752 }
6753
6754 if (overlay_strings_follow_p)
6755 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6756 else
6757 {
6758 it->what = IT_EOB;
6759 it->position = it->current.pos;
6760 success_p = 0;
6761 }
6762 }
6763 else if (!(!it->bidi_p
6764 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6765 || IT_CHARPOS (*it) == it->stop_charpos))
6766 {
6767 /* With bidi non-linear iteration, we could find ourselves
6768 far beyond the last computed stop_charpos, with several
6769 other stop positions in between that we missed. Scan
6770 them all now, in buffer's logical order, until we find
6771 and handle the last stop_charpos that precedes our
6772 current position. */
6773 handle_stop_backwards (it, it->stop_charpos);
6774 return GET_NEXT_DISPLAY_ELEMENT (it);
6775 }
6776 else
6777 {
6778 if (it->bidi_p)
6779 {
6780 /* Take note of the stop position we just moved across,
6781 for when we will move back across it. */
6782 it->prev_stop = it->stop_charpos;
6783 /* If we are at base paragraph embedding level, take
6784 note of the last stop position seen at this
6785 level. */
6786 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6787 it->base_level_stop = it->stop_charpos;
6788 }
6789 handle_stop (it);
6790 return GET_NEXT_DISPLAY_ELEMENT (it);
6791 }
6792 }
6793 else if (it->bidi_p
6794 /* We can sometimes back up for reasons that have nothing
6795 to do with bidi reordering. E.g., compositions. The
6796 code below is only needed when we are above the base
6797 embedding level, so test for that explicitly. */
6798 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6799 && IT_CHARPOS (*it) < it->prev_stop)
6800 {
6801 if (it->base_level_stop <= 0)
6802 it->base_level_stop = BEGV;
6803 if (IT_CHARPOS (*it) < it->base_level_stop)
6804 abort ();
6805 handle_stop_backwards (it, it->base_level_stop);
6806 return GET_NEXT_DISPLAY_ELEMENT (it);
6807 }
6808 else
6809 {
6810 /* No face changes, overlays etc. in sight, so just return a
6811 character from current_buffer. */
6812 unsigned char *p;
6813 EMACS_INT stop;
6814
6815 /* Maybe run the redisplay end trigger hook. Performance note:
6816 This doesn't seem to cost measurable time. */
6817 if (it->redisplay_end_trigger_charpos
6818 && it->glyph_row
6819 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6820 run_redisplay_end_trigger_hook (it);
6821
6822 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
6823 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6824 stop)
6825 && next_element_from_composition (it))
6826 {
6827 return 1;
6828 }
6829
6830 /* Get the next character, maybe multibyte. */
6831 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6832 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6833 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6834 else
6835 it->c = *p, it->len = 1;
6836
6837 /* Record what we have and where it came from. */
6838 it->what = IT_CHARACTER;
6839 it->object = it->w->buffer;
6840 it->position = it->current.pos;
6841
6842 /* Normally we return the character found above, except when we
6843 really want to return an ellipsis for selective display. */
6844 if (it->selective)
6845 {
6846 if (it->c == '\n')
6847 {
6848 /* A value of selective > 0 means hide lines indented more
6849 than that number of columns. */
6850 if (it->selective > 0
6851 && IT_CHARPOS (*it) + 1 < ZV
6852 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6853 IT_BYTEPOS (*it) + 1,
6854 (double) it->selective)) /* iftc */
6855 {
6856 success_p = next_element_from_ellipsis (it);
6857 it->dpvec_char_len = -1;
6858 }
6859 }
6860 else if (it->c == '\r' && it->selective == -1)
6861 {
6862 /* A value of selective == -1 means that everything from the
6863 CR to the end of the line is invisible, with maybe an
6864 ellipsis displayed for it. */
6865 success_p = next_element_from_ellipsis (it);
6866 it->dpvec_char_len = -1;
6867 }
6868 }
6869 }
6870
6871 /* Value is zero if end of buffer reached. */
6872 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6873 return success_p;
6874 }
6875
6876
6877 /* Run the redisplay end trigger hook for IT. */
6878
6879 static void
6880 run_redisplay_end_trigger_hook (struct it *it)
6881 {
6882 Lisp_Object args[3];
6883
6884 /* IT->glyph_row should be non-null, i.e. we should be actually
6885 displaying something, or otherwise we should not run the hook. */
6886 xassert (it->glyph_row);
6887
6888 /* Set up hook arguments. */
6889 args[0] = Qredisplay_end_trigger_functions;
6890 args[1] = it->window;
6891 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6892 it->redisplay_end_trigger_charpos = 0;
6893
6894 /* Since we are *trying* to run these functions, don't try to run
6895 them again, even if they get an error. */
6896 it->w->redisplay_end_trigger = Qnil;
6897 Frun_hook_with_args (3, args);
6898
6899 /* Notice if it changed the face of the character we are on. */
6900 handle_face_prop (it);
6901 }
6902
6903
6904 /* Deliver a composition display element. Unlike the other
6905 next_element_from_XXX, this function is not registered in the array
6906 get_next_element[]. It is called from next_element_from_buffer and
6907 next_element_from_string when necessary. */
6908
6909 static int
6910 next_element_from_composition (struct it *it)
6911 {
6912 it->what = IT_COMPOSITION;
6913 it->len = it->cmp_it.nbytes;
6914 if (STRINGP (it->string))
6915 {
6916 if (it->c < 0)
6917 {
6918 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6919 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6920 return 0;
6921 }
6922 it->position = it->current.string_pos;
6923 it->object = it->string;
6924 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6925 IT_STRING_BYTEPOS (*it), it->string);
6926 }
6927 else
6928 {
6929 if (it->c < 0)
6930 {
6931 IT_CHARPOS (*it) += it->cmp_it.nchars;
6932 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6933 if (it->bidi_p)
6934 {
6935 if (it->bidi_it.new_paragraph)
6936 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6937 /* Resync the bidi iterator with IT's new position.
6938 FIXME: this doesn't support bidirectional text. */
6939 while (it->bidi_it.charpos < IT_CHARPOS (*it))
6940 bidi_move_to_visually_next (&it->bidi_it);
6941 }
6942 return 0;
6943 }
6944 it->position = it->current.pos;
6945 it->object = it->w->buffer;
6946 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6947 IT_BYTEPOS (*it), Qnil);
6948 }
6949 return 1;
6950 }
6951
6952
6953 \f
6954 /***********************************************************************
6955 Moving an iterator without producing glyphs
6956 ***********************************************************************/
6957
6958 /* Check if iterator is at a position corresponding to a valid buffer
6959 position after some move_it_ call. */
6960
6961 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6962 ((it)->method == GET_FROM_STRING \
6963 ? IT_STRING_CHARPOS (*it) == 0 \
6964 : 1)
6965
6966
6967 /* Move iterator IT to a specified buffer or X position within one
6968 line on the display without producing glyphs.
6969
6970 OP should be a bit mask including some or all of these bits:
6971 MOVE_TO_X: Stop upon reaching x-position TO_X.
6972 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
6973 Regardless of OP's value, stop upon reaching the end of the display line.
6974
6975 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6976 This means, in particular, that TO_X includes window's horizontal
6977 scroll amount.
6978
6979 The return value has several possible values that
6980 say what condition caused the scan to stop:
6981
6982 MOVE_POS_MATCH_OR_ZV
6983 - when TO_POS or ZV was reached.
6984
6985 MOVE_X_REACHED
6986 -when TO_X was reached before TO_POS or ZV were reached.
6987
6988 MOVE_LINE_CONTINUED
6989 - when we reached the end of the display area and the line must
6990 be continued.
6991
6992 MOVE_LINE_TRUNCATED
6993 - when we reached the end of the display area and the line is
6994 truncated.
6995
6996 MOVE_NEWLINE_OR_CR
6997 - when we stopped at a line end, i.e. a newline or a CR and selective
6998 display is on. */
6999
7000 static enum move_it_result
7001 move_it_in_display_line_to (struct it *it,
7002 EMACS_INT to_charpos, int to_x,
7003 enum move_operation_enum op)
7004 {
7005 enum move_it_result result = MOVE_UNDEFINED;
7006 struct glyph_row *saved_glyph_row;
7007 struct it wrap_it, atpos_it, atx_it;
7008 int may_wrap = 0;
7009 enum it_method prev_method = it->method;
7010 EMACS_INT prev_pos = IT_CHARPOS (*it);
7011
7012 /* Don't produce glyphs in produce_glyphs. */
7013 saved_glyph_row = it->glyph_row;
7014 it->glyph_row = NULL;
7015
7016 /* Use wrap_it to save a copy of IT wherever a word wrap could
7017 occur. Use atpos_it to save a copy of IT at the desired buffer
7018 position, if found, so that we can scan ahead and check if the
7019 word later overshoots the window edge. Use atx_it similarly, for
7020 pixel positions. */
7021 wrap_it.sp = -1;
7022 atpos_it.sp = -1;
7023 atx_it.sp = -1;
7024
7025 #define BUFFER_POS_REACHED_P() \
7026 ((op & MOVE_TO_POS) != 0 \
7027 && BUFFERP (it->object) \
7028 && (IT_CHARPOS (*it) == to_charpos \
7029 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7030 && (it->method == GET_FROM_BUFFER \
7031 || (it->method == GET_FROM_DISPLAY_VECTOR \
7032 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7033
7034 /* If there's a line-/wrap-prefix, handle it. */
7035 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7036 && it->current_y < it->last_visible_y)
7037 handle_line_prefix (it);
7038
7039 while (1)
7040 {
7041 int x, i, ascent = 0, descent = 0;
7042
7043 /* Utility macro to reset an iterator with x, ascent, and descent. */
7044 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7045 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7046 (IT)->max_descent = descent)
7047
7048 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7049 glyph). */
7050 if ((op & MOVE_TO_POS) != 0
7051 && BUFFERP (it->object)
7052 && it->method == GET_FROM_BUFFER
7053 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7054 || (it->bidi_p
7055 && (prev_method == GET_FROM_IMAGE
7056 || prev_method == GET_FROM_STRETCH)
7057 /* Passed TO_CHARPOS from left to right. */
7058 && ((prev_pos < to_charpos
7059 && IT_CHARPOS (*it) > to_charpos)
7060 /* Passed TO_CHARPOS from right to left. */
7061 || (prev_pos > to_charpos
7062 && IT_CHARPOS (*it) < to_charpos)))))
7063 {
7064 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7065 {
7066 result = MOVE_POS_MATCH_OR_ZV;
7067 break;
7068 }
7069 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7070 /* If wrap_it is valid, the current position might be in a
7071 word that is wrapped. So, save the iterator in
7072 atpos_it and continue to see if wrapping happens. */
7073 atpos_it = *it;
7074 }
7075
7076 prev_method = it->method;
7077 if (it->method == GET_FROM_BUFFER)
7078 prev_pos = IT_CHARPOS (*it);
7079 /* Stop when ZV reached.
7080 We used to stop here when TO_CHARPOS reached as well, but that is
7081 too soon if this glyph does not fit on this line. So we handle it
7082 explicitly below. */
7083 if (!get_next_display_element (it))
7084 {
7085 result = MOVE_POS_MATCH_OR_ZV;
7086 break;
7087 }
7088
7089 if (it->line_wrap == TRUNCATE)
7090 {
7091 if (BUFFER_POS_REACHED_P ())
7092 {
7093 result = MOVE_POS_MATCH_OR_ZV;
7094 break;
7095 }
7096 }
7097 else
7098 {
7099 if (it->line_wrap == WORD_WRAP)
7100 {
7101 if (IT_DISPLAYING_WHITESPACE (it))
7102 may_wrap = 1;
7103 else if (may_wrap)
7104 {
7105 /* We have reached a glyph that follows one or more
7106 whitespace characters. If the position is
7107 already found, we are done. */
7108 if (atpos_it.sp >= 0)
7109 {
7110 *it = atpos_it;
7111 result = MOVE_POS_MATCH_OR_ZV;
7112 goto done;
7113 }
7114 if (atx_it.sp >= 0)
7115 {
7116 *it = atx_it;
7117 result = MOVE_X_REACHED;
7118 goto done;
7119 }
7120 /* Otherwise, we can wrap here. */
7121 wrap_it = *it;
7122 may_wrap = 0;
7123 }
7124 }
7125 }
7126
7127 /* Remember the line height for the current line, in case
7128 the next element doesn't fit on the line. */
7129 ascent = it->max_ascent;
7130 descent = it->max_descent;
7131
7132 /* The call to produce_glyphs will get the metrics of the
7133 display element IT is loaded with. Record the x-position
7134 before this display element, in case it doesn't fit on the
7135 line. */
7136 x = it->current_x;
7137
7138 PRODUCE_GLYPHS (it);
7139
7140 if (it->area != TEXT_AREA)
7141 {
7142 set_iterator_to_next (it, 1);
7143 continue;
7144 }
7145
7146 /* The number of glyphs we get back in IT->nglyphs will normally
7147 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7148 character on a terminal frame, or (iii) a line end. For the
7149 second case, IT->nglyphs - 1 padding glyphs will be present.
7150 (On X frames, there is only one glyph produced for a
7151 composite character.)
7152
7153 The behavior implemented below means, for continuation lines,
7154 that as many spaces of a TAB as fit on the current line are
7155 displayed there. For terminal frames, as many glyphs of a
7156 multi-glyph character are displayed in the current line, too.
7157 This is what the old redisplay code did, and we keep it that
7158 way. Under X, the whole shape of a complex character must
7159 fit on the line or it will be completely displayed in the
7160 next line.
7161
7162 Note that both for tabs and padding glyphs, all glyphs have
7163 the same width. */
7164 if (it->nglyphs)
7165 {
7166 /* More than one glyph or glyph doesn't fit on line. All
7167 glyphs have the same width. */
7168 int single_glyph_width = it->pixel_width / it->nglyphs;
7169 int new_x;
7170 int x_before_this_char = x;
7171 int hpos_before_this_char = it->hpos;
7172
7173 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7174 {
7175 new_x = x + single_glyph_width;
7176
7177 /* We want to leave anything reaching TO_X to the caller. */
7178 if ((op & MOVE_TO_X) && new_x > to_x)
7179 {
7180 if (BUFFER_POS_REACHED_P ())
7181 {
7182 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7183 goto buffer_pos_reached;
7184 if (atpos_it.sp < 0)
7185 {
7186 atpos_it = *it;
7187 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7188 }
7189 }
7190 else
7191 {
7192 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7193 {
7194 it->current_x = x;
7195 result = MOVE_X_REACHED;
7196 break;
7197 }
7198 if (atx_it.sp < 0)
7199 {
7200 atx_it = *it;
7201 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7202 }
7203 }
7204 }
7205
7206 if (/* Lines are continued. */
7207 it->line_wrap != TRUNCATE
7208 && (/* And glyph doesn't fit on the line. */
7209 new_x > it->last_visible_x
7210 /* Or it fits exactly and we're on a window
7211 system frame. */
7212 || (new_x == it->last_visible_x
7213 && FRAME_WINDOW_P (it->f))))
7214 {
7215 if (/* IT->hpos == 0 means the very first glyph
7216 doesn't fit on the line, e.g. a wide image. */
7217 it->hpos == 0
7218 || (new_x == it->last_visible_x
7219 && FRAME_WINDOW_P (it->f)))
7220 {
7221 ++it->hpos;
7222 it->current_x = new_x;
7223
7224 /* The character's last glyph just barely fits
7225 in this row. */
7226 if (i == it->nglyphs - 1)
7227 {
7228 /* If this is the destination position,
7229 return a position *before* it in this row,
7230 now that we know it fits in this row. */
7231 if (BUFFER_POS_REACHED_P ())
7232 {
7233 if (it->line_wrap != WORD_WRAP
7234 || wrap_it.sp < 0)
7235 {
7236 it->hpos = hpos_before_this_char;
7237 it->current_x = x_before_this_char;
7238 result = MOVE_POS_MATCH_OR_ZV;
7239 break;
7240 }
7241 if (it->line_wrap == WORD_WRAP
7242 && atpos_it.sp < 0)
7243 {
7244 atpos_it = *it;
7245 atpos_it.current_x = x_before_this_char;
7246 atpos_it.hpos = hpos_before_this_char;
7247 }
7248 }
7249
7250 set_iterator_to_next (it, 1);
7251 /* On graphical terminals, newlines may
7252 "overflow" into the fringe if
7253 overflow-newline-into-fringe is non-nil.
7254 On text-only terminals, newlines may
7255 overflow into the last glyph on the
7256 display line.*/
7257 if (!FRAME_WINDOW_P (it->f)
7258 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7259 {
7260 if (!get_next_display_element (it))
7261 {
7262 result = MOVE_POS_MATCH_OR_ZV;
7263 break;
7264 }
7265 if (BUFFER_POS_REACHED_P ())
7266 {
7267 if (ITERATOR_AT_END_OF_LINE_P (it))
7268 result = MOVE_POS_MATCH_OR_ZV;
7269 else
7270 result = MOVE_LINE_CONTINUED;
7271 break;
7272 }
7273 if (ITERATOR_AT_END_OF_LINE_P (it))
7274 {
7275 result = MOVE_NEWLINE_OR_CR;
7276 break;
7277 }
7278 }
7279 }
7280 }
7281 else
7282 IT_RESET_X_ASCENT_DESCENT (it);
7283
7284 if (wrap_it.sp >= 0)
7285 {
7286 *it = wrap_it;
7287 atpos_it.sp = -1;
7288 atx_it.sp = -1;
7289 }
7290
7291 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7292 IT_CHARPOS (*it)));
7293 result = MOVE_LINE_CONTINUED;
7294 break;
7295 }
7296
7297 if (BUFFER_POS_REACHED_P ())
7298 {
7299 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7300 goto buffer_pos_reached;
7301 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7302 {
7303 atpos_it = *it;
7304 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7305 }
7306 }
7307
7308 if (new_x > it->first_visible_x)
7309 {
7310 /* Glyph is visible. Increment number of glyphs that
7311 would be displayed. */
7312 ++it->hpos;
7313 }
7314 }
7315
7316 if (result != MOVE_UNDEFINED)
7317 break;
7318 }
7319 else if (BUFFER_POS_REACHED_P ())
7320 {
7321 buffer_pos_reached:
7322 IT_RESET_X_ASCENT_DESCENT (it);
7323 result = MOVE_POS_MATCH_OR_ZV;
7324 break;
7325 }
7326 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7327 {
7328 /* Stop when TO_X specified and reached. This check is
7329 necessary here because of lines consisting of a line end,
7330 only. The line end will not produce any glyphs and we
7331 would never get MOVE_X_REACHED. */
7332 xassert (it->nglyphs == 0);
7333 result = MOVE_X_REACHED;
7334 break;
7335 }
7336
7337 /* Is this a line end? If yes, we're done. */
7338 if (ITERATOR_AT_END_OF_LINE_P (it))
7339 {
7340 result = MOVE_NEWLINE_OR_CR;
7341 break;
7342 }
7343
7344 if (it->method == GET_FROM_BUFFER)
7345 prev_pos = IT_CHARPOS (*it);
7346 /* The current display element has been consumed. Advance
7347 to the next. */
7348 set_iterator_to_next (it, 1);
7349
7350 /* Stop if lines are truncated and IT's current x-position is
7351 past the right edge of the window now. */
7352 if (it->line_wrap == TRUNCATE
7353 && it->current_x >= it->last_visible_x)
7354 {
7355 if (!FRAME_WINDOW_P (it->f)
7356 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7357 {
7358 if (!get_next_display_element (it)
7359 || BUFFER_POS_REACHED_P ())
7360 {
7361 result = MOVE_POS_MATCH_OR_ZV;
7362 break;
7363 }
7364 if (ITERATOR_AT_END_OF_LINE_P (it))
7365 {
7366 result = MOVE_NEWLINE_OR_CR;
7367 break;
7368 }
7369 }
7370 result = MOVE_LINE_TRUNCATED;
7371 break;
7372 }
7373 #undef IT_RESET_X_ASCENT_DESCENT
7374 }
7375
7376 #undef BUFFER_POS_REACHED_P
7377
7378 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7379 restore the saved iterator. */
7380 if (atpos_it.sp >= 0)
7381 *it = atpos_it;
7382 else if (atx_it.sp >= 0)
7383 *it = atx_it;
7384
7385 done:
7386
7387 /* Restore the iterator settings altered at the beginning of this
7388 function. */
7389 it->glyph_row = saved_glyph_row;
7390 return result;
7391 }
7392
7393 /* For external use. */
7394 void
7395 move_it_in_display_line (struct it *it,
7396 EMACS_INT to_charpos, int to_x,
7397 enum move_operation_enum op)
7398 {
7399 if (it->line_wrap == WORD_WRAP
7400 && (op & MOVE_TO_X))
7401 {
7402 struct it save_it = *it;
7403 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7404 /* When word-wrap is on, TO_X may lie past the end
7405 of a wrapped line. Then it->current is the
7406 character on the next line, so backtrack to the
7407 space before the wrap point. */
7408 if (skip == MOVE_LINE_CONTINUED)
7409 {
7410 int prev_x = max (it->current_x - 1, 0);
7411 *it = save_it;
7412 move_it_in_display_line_to
7413 (it, -1, prev_x, MOVE_TO_X);
7414 }
7415 }
7416 else
7417 move_it_in_display_line_to (it, to_charpos, to_x, op);
7418 }
7419
7420
7421 /* Move IT forward until it satisfies one or more of the criteria in
7422 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7423
7424 OP is a bit-mask that specifies where to stop, and in particular,
7425 which of those four position arguments makes a difference. See the
7426 description of enum move_operation_enum.
7427
7428 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7429 screen line, this function will set IT to the next position >
7430 TO_CHARPOS. */
7431
7432 void
7433 move_it_to (struct it *it, int to_charpos, int to_x, int to_y, int to_vpos, int op)
7434 {
7435 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7436 int line_height, line_start_x = 0, reached = 0;
7437
7438 for (;;)
7439 {
7440 if (op & MOVE_TO_VPOS)
7441 {
7442 /* If no TO_CHARPOS and no TO_X specified, stop at the
7443 start of the line TO_VPOS. */
7444 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7445 {
7446 if (it->vpos == to_vpos)
7447 {
7448 reached = 1;
7449 break;
7450 }
7451 else
7452 skip = move_it_in_display_line_to (it, -1, -1, 0);
7453 }
7454 else
7455 {
7456 /* TO_VPOS >= 0 means stop at TO_X in the line at
7457 TO_VPOS, or at TO_POS, whichever comes first. */
7458 if (it->vpos == to_vpos)
7459 {
7460 reached = 2;
7461 break;
7462 }
7463
7464 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7465
7466 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7467 {
7468 reached = 3;
7469 break;
7470 }
7471 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7472 {
7473 /* We have reached TO_X but not in the line we want. */
7474 skip = move_it_in_display_line_to (it, to_charpos,
7475 -1, MOVE_TO_POS);
7476 if (skip == MOVE_POS_MATCH_OR_ZV)
7477 {
7478 reached = 4;
7479 break;
7480 }
7481 }
7482 }
7483 }
7484 else if (op & MOVE_TO_Y)
7485 {
7486 struct it it_backup;
7487
7488 if (it->line_wrap == WORD_WRAP)
7489 it_backup = *it;
7490
7491 /* TO_Y specified means stop at TO_X in the line containing
7492 TO_Y---or at TO_CHARPOS if this is reached first. The
7493 problem is that we can't really tell whether the line
7494 contains TO_Y before we have completely scanned it, and
7495 this may skip past TO_X. What we do is to first scan to
7496 TO_X.
7497
7498 If TO_X is not specified, use a TO_X of zero. The reason
7499 is to make the outcome of this function more predictable.
7500 If we didn't use TO_X == 0, we would stop at the end of
7501 the line which is probably not what a caller would expect
7502 to happen. */
7503 skip = move_it_in_display_line_to
7504 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7505 (MOVE_TO_X | (op & MOVE_TO_POS)));
7506
7507 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7508 if (skip == MOVE_POS_MATCH_OR_ZV)
7509 reached = 5;
7510 else if (skip == MOVE_X_REACHED)
7511 {
7512 /* If TO_X was reached, we want to know whether TO_Y is
7513 in the line. We know this is the case if the already
7514 scanned glyphs make the line tall enough. Otherwise,
7515 we must check by scanning the rest of the line. */
7516 line_height = it->max_ascent + it->max_descent;
7517 if (to_y >= it->current_y
7518 && to_y < it->current_y + line_height)
7519 {
7520 reached = 6;
7521 break;
7522 }
7523 it_backup = *it;
7524 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7525 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7526 op & MOVE_TO_POS);
7527 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7528 line_height = it->max_ascent + it->max_descent;
7529 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7530
7531 if (to_y >= it->current_y
7532 && to_y < it->current_y + line_height)
7533 {
7534 /* If TO_Y is in this line and TO_X was reached
7535 above, we scanned too far. We have to restore
7536 IT's settings to the ones before skipping. */
7537 *it = it_backup;
7538 reached = 6;
7539 }
7540 else
7541 {
7542 skip = skip2;
7543 if (skip == MOVE_POS_MATCH_OR_ZV)
7544 reached = 7;
7545 }
7546 }
7547 else
7548 {
7549 /* Check whether TO_Y is in this line. */
7550 line_height = it->max_ascent + it->max_descent;
7551 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7552
7553 if (to_y >= it->current_y
7554 && to_y < it->current_y + line_height)
7555 {
7556 /* When word-wrap is on, TO_X may lie past the end
7557 of a wrapped line. Then it->current is the
7558 character on the next line, so backtrack to the
7559 space before the wrap point. */
7560 if (skip == MOVE_LINE_CONTINUED
7561 && it->line_wrap == WORD_WRAP)
7562 {
7563 int prev_x = max (it->current_x - 1, 0);
7564 *it = it_backup;
7565 skip = move_it_in_display_line_to
7566 (it, -1, prev_x, MOVE_TO_X);
7567 }
7568 reached = 6;
7569 }
7570 }
7571
7572 if (reached)
7573 break;
7574 }
7575 else if (BUFFERP (it->object)
7576 && (it->method == GET_FROM_BUFFER
7577 || it->method == GET_FROM_STRETCH)
7578 && IT_CHARPOS (*it) >= to_charpos)
7579 skip = MOVE_POS_MATCH_OR_ZV;
7580 else
7581 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7582
7583 switch (skip)
7584 {
7585 case MOVE_POS_MATCH_OR_ZV:
7586 reached = 8;
7587 goto out;
7588
7589 case MOVE_NEWLINE_OR_CR:
7590 set_iterator_to_next (it, 1);
7591 it->continuation_lines_width = 0;
7592 break;
7593
7594 case MOVE_LINE_TRUNCATED:
7595 it->continuation_lines_width = 0;
7596 reseat_at_next_visible_line_start (it, 0);
7597 if ((op & MOVE_TO_POS) != 0
7598 && IT_CHARPOS (*it) > to_charpos)
7599 {
7600 reached = 9;
7601 goto out;
7602 }
7603 break;
7604
7605 case MOVE_LINE_CONTINUED:
7606 /* For continued lines ending in a tab, some of the glyphs
7607 associated with the tab are displayed on the current
7608 line. Since it->current_x does not include these glyphs,
7609 we use it->last_visible_x instead. */
7610 if (it->c == '\t')
7611 {
7612 it->continuation_lines_width += it->last_visible_x;
7613 /* When moving by vpos, ensure that the iterator really
7614 advances to the next line (bug#847, bug#969). Fixme:
7615 do we need to do this in other circumstances? */
7616 if (it->current_x != it->last_visible_x
7617 && (op & MOVE_TO_VPOS)
7618 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7619 {
7620 line_start_x = it->current_x + it->pixel_width
7621 - it->last_visible_x;
7622 set_iterator_to_next (it, 0);
7623 }
7624 }
7625 else
7626 it->continuation_lines_width += it->current_x;
7627 break;
7628
7629 default:
7630 abort ();
7631 }
7632
7633 /* Reset/increment for the next run. */
7634 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7635 it->current_x = line_start_x;
7636 line_start_x = 0;
7637 it->hpos = 0;
7638 it->current_y += it->max_ascent + it->max_descent;
7639 ++it->vpos;
7640 last_height = it->max_ascent + it->max_descent;
7641 last_max_ascent = it->max_ascent;
7642 it->max_ascent = it->max_descent = 0;
7643 }
7644
7645 out:
7646
7647 /* On text terminals, we may stop at the end of a line in the middle
7648 of a multi-character glyph. If the glyph itself is continued,
7649 i.e. it is actually displayed on the next line, don't treat this
7650 stopping point as valid; move to the next line instead (unless
7651 that brings us offscreen). */
7652 if (!FRAME_WINDOW_P (it->f)
7653 && op & MOVE_TO_POS
7654 && IT_CHARPOS (*it) == to_charpos
7655 && it->what == IT_CHARACTER
7656 && it->nglyphs > 1
7657 && it->line_wrap == WINDOW_WRAP
7658 && it->current_x == it->last_visible_x - 1
7659 && it->c != '\n'
7660 && it->c != '\t'
7661 && it->vpos < XFASTINT (it->w->window_end_vpos))
7662 {
7663 it->continuation_lines_width += it->current_x;
7664 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7665 it->current_y += it->max_ascent + it->max_descent;
7666 ++it->vpos;
7667 last_height = it->max_ascent + it->max_descent;
7668 last_max_ascent = it->max_ascent;
7669 }
7670
7671 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7672 }
7673
7674
7675 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7676
7677 If DY > 0, move IT backward at least that many pixels. DY = 0
7678 means move IT backward to the preceding line start or BEGV. This
7679 function may move over more than DY pixels if IT->current_y - DY
7680 ends up in the middle of a line; in this case IT->current_y will be
7681 set to the top of the line moved to. */
7682
7683 void
7684 move_it_vertically_backward (struct it *it, int dy)
7685 {
7686 int nlines, h;
7687 struct it it2, it3;
7688 int start_pos;
7689
7690 move_further_back:
7691 xassert (dy >= 0);
7692
7693 start_pos = IT_CHARPOS (*it);
7694
7695 /* Estimate how many newlines we must move back. */
7696 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7697
7698 /* Set the iterator's position that many lines back. */
7699 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7700 back_to_previous_visible_line_start (it);
7701
7702 /* Reseat the iterator here. When moving backward, we don't want
7703 reseat to skip forward over invisible text, set up the iterator
7704 to deliver from overlay strings at the new position etc. So,
7705 use reseat_1 here. */
7706 reseat_1 (it, it->current.pos, 1);
7707
7708 /* We are now surely at a line start. */
7709 it->current_x = it->hpos = 0;
7710 it->continuation_lines_width = 0;
7711
7712 /* Move forward and see what y-distance we moved. First move to the
7713 start of the next line so that we get its height. We need this
7714 height to be able to tell whether we reached the specified
7715 y-distance. */
7716 it2 = *it;
7717 it2.max_ascent = it2.max_descent = 0;
7718 do
7719 {
7720 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7721 MOVE_TO_POS | MOVE_TO_VPOS);
7722 }
7723 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7724 xassert (IT_CHARPOS (*it) >= BEGV);
7725 it3 = it2;
7726
7727 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7728 xassert (IT_CHARPOS (*it) >= BEGV);
7729 /* H is the actual vertical distance from the position in *IT
7730 and the starting position. */
7731 h = it2.current_y - it->current_y;
7732 /* NLINES is the distance in number of lines. */
7733 nlines = it2.vpos - it->vpos;
7734
7735 /* Correct IT's y and vpos position
7736 so that they are relative to the starting point. */
7737 it->vpos -= nlines;
7738 it->current_y -= h;
7739
7740 if (dy == 0)
7741 {
7742 /* DY == 0 means move to the start of the screen line. The
7743 value of nlines is > 0 if continuation lines were involved. */
7744 if (nlines > 0)
7745 move_it_by_lines (it, nlines, 1);
7746 }
7747 else
7748 {
7749 /* The y-position we try to reach, relative to *IT.
7750 Note that H has been subtracted in front of the if-statement. */
7751 int target_y = it->current_y + h - dy;
7752 int y0 = it3.current_y;
7753 int y1 = line_bottom_y (&it3);
7754 int line_height = y1 - y0;
7755
7756 /* If we did not reach target_y, try to move further backward if
7757 we can. If we moved too far backward, try to move forward. */
7758 if (target_y < it->current_y
7759 /* This is heuristic. In a window that's 3 lines high, with
7760 a line height of 13 pixels each, recentering with point
7761 on the bottom line will try to move -39/2 = 19 pixels
7762 backward. Try to avoid moving into the first line. */
7763 && (it->current_y - target_y
7764 > min (window_box_height (it->w), line_height * 2 / 3))
7765 && IT_CHARPOS (*it) > BEGV)
7766 {
7767 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7768 target_y - it->current_y));
7769 dy = it->current_y - target_y;
7770 goto move_further_back;
7771 }
7772 else if (target_y >= it->current_y + line_height
7773 && IT_CHARPOS (*it) < ZV)
7774 {
7775 /* Should move forward by at least one line, maybe more.
7776
7777 Note: Calling move_it_by_lines can be expensive on
7778 terminal frames, where compute_motion is used (via
7779 vmotion) to do the job, when there are very long lines
7780 and truncate-lines is nil. That's the reason for
7781 treating terminal frames specially here. */
7782
7783 if (!FRAME_WINDOW_P (it->f))
7784 move_it_vertically (it, target_y - (it->current_y + line_height));
7785 else
7786 {
7787 do
7788 {
7789 move_it_by_lines (it, 1, 1);
7790 }
7791 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7792 }
7793 }
7794 }
7795 }
7796
7797
7798 /* Move IT by a specified amount of pixel lines DY. DY negative means
7799 move backwards. DY = 0 means move to start of screen line. At the
7800 end, IT will be on the start of a screen line. */
7801
7802 void
7803 move_it_vertically (struct it *it, int dy)
7804 {
7805 if (dy <= 0)
7806 move_it_vertically_backward (it, -dy);
7807 else
7808 {
7809 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7810 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7811 MOVE_TO_POS | MOVE_TO_Y);
7812 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7813
7814 /* If buffer ends in ZV without a newline, move to the start of
7815 the line to satisfy the post-condition. */
7816 if (IT_CHARPOS (*it) == ZV
7817 && ZV > BEGV
7818 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7819 move_it_by_lines (it, 0, 0);
7820 }
7821 }
7822
7823
7824 /* Move iterator IT past the end of the text line it is in. */
7825
7826 void
7827 move_it_past_eol (struct it *it)
7828 {
7829 enum move_it_result rc;
7830
7831 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7832 if (rc == MOVE_NEWLINE_OR_CR)
7833 set_iterator_to_next (it, 0);
7834 }
7835
7836
7837 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7838 negative means move up. DVPOS == 0 means move to the start of the
7839 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7840 NEED_Y_P is zero, IT->current_y will be left unchanged.
7841
7842 Further optimization ideas: If we would know that IT->f doesn't use
7843 a face with proportional font, we could be faster for
7844 truncate-lines nil. */
7845
7846 void
7847 move_it_by_lines (struct it *it, int dvpos, int need_y_p)
7848 {
7849 struct position pos;
7850
7851 /* The commented-out optimization uses vmotion on terminals. This
7852 gives bad results, because elements like it->what, on which
7853 callers such as pos_visible_p rely, aren't updated. */
7854 /* if (!FRAME_WINDOW_P (it->f))
7855 {
7856 struct text_pos textpos;
7857
7858 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7859 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7860 reseat (it, textpos, 1);
7861 it->vpos += pos.vpos;
7862 it->current_y += pos.vpos;
7863 }
7864 else */
7865
7866 if (dvpos == 0)
7867 {
7868 /* DVPOS == 0 means move to the start of the screen line. */
7869 move_it_vertically_backward (it, 0);
7870 xassert (it->current_x == 0 && it->hpos == 0);
7871 /* Let next call to line_bottom_y calculate real line height */
7872 last_height = 0;
7873 }
7874 else if (dvpos > 0)
7875 {
7876 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7877 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7878 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7879 }
7880 else
7881 {
7882 struct it it2;
7883 int start_charpos, i;
7884
7885 /* Start at the beginning of the screen line containing IT's
7886 position. This may actually move vertically backwards,
7887 in case of overlays, so adjust dvpos accordingly. */
7888 dvpos += it->vpos;
7889 move_it_vertically_backward (it, 0);
7890 dvpos -= it->vpos;
7891
7892 /* Go back -DVPOS visible lines and reseat the iterator there. */
7893 start_charpos = IT_CHARPOS (*it);
7894 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7895 back_to_previous_visible_line_start (it);
7896 reseat (it, it->current.pos, 1);
7897
7898 /* Move further back if we end up in a string or an image. */
7899 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7900 {
7901 /* First try to move to start of display line. */
7902 dvpos += it->vpos;
7903 move_it_vertically_backward (it, 0);
7904 dvpos -= it->vpos;
7905 if (IT_POS_VALID_AFTER_MOVE_P (it))
7906 break;
7907 /* If start of line is still in string or image,
7908 move further back. */
7909 back_to_previous_visible_line_start (it);
7910 reseat (it, it->current.pos, 1);
7911 dvpos--;
7912 }
7913
7914 it->current_x = it->hpos = 0;
7915
7916 /* Above call may have moved too far if continuation lines
7917 are involved. Scan forward and see if it did. */
7918 it2 = *it;
7919 it2.vpos = it2.current_y = 0;
7920 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7921 it->vpos -= it2.vpos;
7922 it->current_y -= it2.current_y;
7923 it->current_x = it->hpos = 0;
7924
7925 /* If we moved too far back, move IT some lines forward. */
7926 if (it2.vpos > -dvpos)
7927 {
7928 int delta = it2.vpos + dvpos;
7929 it2 = *it;
7930 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7931 /* Move back again if we got too far ahead. */
7932 if (IT_CHARPOS (*it) >= start_charpos)
7933 *it = it2;
7934 }
7935 }
7936 }
7937
7938 /* Return 1 if IT points into the middle of a display vector. */
7939
7940 int
7941 in_display_vector_p (struct it *it)
7942 {
7943 return (it->method == GET_FROM_DISPLAY_VECTOR
7944 && it->current.dpvec_index > 0
7945 && it->dpvec + it->current.dpvec_index != it->dpend);
7946 }
7947
7948 \f
7949 /***********************************************************************
7950 Messages
7951 ***********************************************************************/
7952
7953
7954 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7955 to *Messages*. */
7956
7957 void
7958 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
7959 {
7960 Lisp_Object args[3];
7961 Lisp_Object msg, fmt;
7962 char *buffer;
7963 int len;
7964 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7965 USE_SAFE_ALLOCA;
7966
7967 /* Do nothing if called asynchronously. Inserting text into
7968 a buffer may call after-change-functions and alike and
7969 that would means running Lisp asynchronously. */
7970 if (handling_signal)
7971 return;
7972
7973 fmt = msg = Qnil;
7974 GCPRO4 (fmt, msg, arg1, arg2);
7975
7976 args[0] = fmt = build_string (format);
7977 args[1] = arg1;
7978 args[2] = arg2;
7979 msg = Fformat (3, args);
7980
7981 len = SBYTES (msg) + 1;
7982 SAFE_ALLOCA (buffer, char *, len);
7983 memcpy (buffer, SDATA (msg), len);
7984
7985 message_dolog (buffer, len - 1, 1, 0);
7986 SAFE_FREE ();
7987
7988 UNGCPRO;
7989 }
7990
7991
7992 /* Output a newline in the *Messages* buffer if "needs" one. */
7993
7994 void
7995 message_log_maybe_newline (void)
7996 {
7997 if (message_log_need_newline)
7998 message_dolog ("", 0, 1, 0);
7999 }
8000
8001
8002 /* Add a string M of length NBYTES to the message log, optionally
8003 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8004 nonzero, means interpret the contents of M as multibyte. This
8005 function calls low-level routines in order to bypass text property
8006 hooks, etc. which might not be safe to run.
8007
8008 This may GC (insert may run before/after change hooks),
8009 so the buffer M must NOT point to a Lisp string. */
8010
8011 void
8012 message_dolog (const char *m, int nbytes, int nlflag, int multibyte)
8013 {
8014 if (!NILP (Vmemory_full))
8015 return;
8016
8017 if (!NILP (Vmessage_log_max))
8018 {
8019 struct buffer *oldbuf;
8020 Lisp_Object oldpoint, oldbegv, oldzv;
8021 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8022 int point_at_end = 0;
8023 int zv_at_end = 0;
8024 Lisp_Object old_deactivate_mark, tem;
8025 struct gcpro gcpro1;
8026
8027 old_deactivate_mark = Vdeactivate_mark;
8028 oldbuf = current_buffer;
8029 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8030 current_buffer->undo_list = Qt;
8031
8032 oldpoint = message_dolog_marker1;
8033 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8034 oldbegv = message_dolog_marker2;
8035 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8036 oldzv = message_dolog_marker3;
8037 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8038 GCPRO1 (old_deactivate_mark);
8039
8040 if (PT == Z)
8041 point_at_end = 1;
8042 if (ZV == Z)
8043 zv_at_end = 1;
8044
8045 BEGV = BEG;
8046 BEGV_BYTE = BEG_BYTE;
8047 ZV = Z;
8048 ZV_BYTE = Z_BYTE;
8049 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8050
8051 /* Insert the string--maybe converting multibyte to single byte
8052 or vice versa, so that all the text fits the buffer. */
8053 if (multibyte
8054 && NILP (current_buffer->enable_multibyte_characters))
8055 {
8056 int i, c, char_bytes;
8057 unsigned char work[1];
8058
8059 /* Convert a multibyte string to single-byte
8060 for the *Message* buffer. */
8061 for (i = 0; i < nbytes; i += char_bytes)
8062 {
8063 c = string_char_and_length (m + i, &char_bytes);
8064 work[0] = (ASCII_CHAR_P (c)
8065 ? c
8066 : multibyte_char_to_unibyte (c, Qnil));
8067 insert_1_both (work, 1, 1, 1, 0, 0);
8068 }
8069 }
8070 else if (! multibyte
8071 && ! NILP (current_buffer->enable_multibyte_characters))
8072 {
8073 int i, c, char_bytes;
8074 unsigned char *msg = (unsigned char *) m;
8075 unsigned char str[MAX_MULTIBYTE_LENGTH];
8076 /* Convert a single-byte string to multibyte
8077 for the *Message* buffer. */
8078 for (i = 0; i < nbytes; i++)
8079 {
8080 c = msg[i];
8081 MAKE_CHAR_MULTIBYTE (c);
8082 char_bytes = CHAR_STRING (c, str);
8083 insert_1_both (str, 1, char_bytes, 1, 0, 0);
8084 }
8085 }
8086 else if (nbytes)
8087 insert_1 (m, nbytes, 1, 0, 0);
8088
8089 if (nlflag)
8090 {
8091 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
8092 insert_1 ("\n", 1, 1, 0, 0);
8093
8094 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8095 this_bol = PT;
8096 this_bol_byte = PT_BYTE;
8097
8098 /* See if this line duplicates the previous one.
8099 If so, combine duplicates. */
8100 if (this_bol > BEG)
8101 {
8102 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8103 prev_bol = PT;
8104 prev_bol_byte = PT_BYTE;
8105
8106 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8107 this_bol, this_bol_byte);
8108 if (dup)
8109 {
8110 del_range_both (prev_bol, prev_bol_byte,
8111 this_bol, this_bol_byte, 0);
8112 if (dup > 1)
8113 {
8114 char dupstr[40];
8115 int duplen;
8116
8117 /* If you change this format, don't forget to also
8118 change message_log_check_duplicate. */
8119 sprintf (dupstr, " [%d times]", dup);
8120 duplen = strlen (dupstr);
8121 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8122 insert_1 (dupstr, duplen, 1, 0, 1);
8123 }
8124 }
8125 }
8126
8127 /* If we have more than the desired maximum number of lines
8128 in the *Messages* buffer now, delete the oldest ones.
8129 This is safe because we don't have undo in this buffer. */
8130
8131 if (NATNUMP (Vmessage_log_max))
8132 {
8133 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8134 -XFASTINT (Vmessage_log_max) - 1, 0);
8135 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8136 }
8137 }
8138 BEGV = XMARKER (oldbegv)->charpos;
8139 BEGV_BYTE = marker_byte_position (oldbegv);
8140
8141 if (zv_at_end)
8142 {
8143 ZV = Z;
8144 ZV_BYTE = Z_BYTE;
8145 }
8146 else
8147 {
8148 ZV = XMARKER (oldzv)->charpos;
8149 ZV_BYTE = marker_byte_position (oldzv);
8150 }
8151
8152 if (point_at_end)
8153 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8154 else
8155 /* We can't do Fgoto_char (oldpoint) because it will run some
8156 Lisp code. */
8157 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8158 XMARKER (oldpoint)->bytepos);
8159
8160 UNGCPRO;
8161 unchain_marker (XMARKER (oldpoint));
8162 unchain_marker (XMARKER (oldbegv));
8163 unchain_marker (XMARKER (oldzv));
8164
8165 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8166 set_buffer_internal (oldbuf);
8167 if (NILP (tem))
8168 windows_or_buffers_changed = old_windows_or_buffers_changed;
8169 message_log_need_newline = !nlflag;
8170 Vdeactivate_mark = old_deactivate_mark;
8171 }
8172 }
8173
8174
8175 /* We are at the end of the buffer after just having inserted a newline.
8176 (Note: We depend on the fact we won't be crossing the gap.)
8177 Check to see if the most recent message looks a lot like the previous one.
8178 Return 0 if different, 1 if the new one should just replace it, or a
8179 value N > 1 if we should also append " [N times]". */
8180
8181 static int
8182 message_log_check_duplicate (int prev_bol, int prev_bol_byte,
8183 int this_bol, int this_bol_byte)
8184 {
8185 int i;
8186 int len = Z_BYTE - 1 - this_bol_byte;
8187 int seen_dots = 0;
8188 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8189 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8190
8191 for (i = 0; i < len; i++)
8192 {
8193 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8194 seen_dots = 1;
8195 if (p1[i] != p2[i])
8196 return seen_dots;
8197 }
8198 p1 += len;
8199 if (*p1 == '\n')
8200 return 2;
8201 if (*p1++ == ' ' && *p1++ == '[')
8202 {
8203 int n = 0;
8204 while (*p1 >= '0' && *p1 <= '9')
8205 n = n * 10 + *p1++ - '0';
8206 if (strncmp (p1, " times]\n", 8) == 0)
8207 return n+1;
8208 }
8209 return 0;
8210 }
8211 \f
8212
8213 /* Display an echo area message M with a specified length of NBYTES
8214 bytes. The string may include null characters. If M is 0, clear
8215 out any existing message, and let the mini-buffer text show
8216 through.
8217
8218 This may GC, so the buffer M must NOT point to a Lisp string. */
8219
8220 void
8221 message2 (const char *m, int nbytes, int multibyte)
8222 {
8223 /* First flush out any partial line written with print. */
8224 message_log_maybe_newline ();
8225 if (m)
8226 message_dolog (m, nbytes, 1, multibyte);
8227 message2_nolog (m, nbytes, multibyte);
8228 }
8229
8230
8231 /* The non-logging counterpart of message2. */
8232
8233 void
8234 message2_nolog (const char *m, int nbytes, int multibyte)
8235 {
8236 struct frame *sf = SELECTED_FRAME ();
8237 message_enable_multibyte = multibyte;
8238
8239 if (FRAME_INITIAL_P (sf))
8240 {
8241 if (noninteractive_need_newline)
8242 putc ('\n', stderr);
8243 noninteractive_need_newline = 0;
8244 if (m)
8245 fwrite (m, nbytes, 1, stderr);
8246 if (cursor_in_echo_area == 0)
8247 fprintf (stderr, "\n");
8248 fflush (stderr);
8249 }
8250 /* A null message buffer means that the frame hasn't really been
8251 initialized yet. Error messages get reported properly by
8252 cmd_error, so this must be just an informative message; toss it. */
8253 else if (INTERACTIVE
8254 && sf->glyphs_initialized_p
8255 && FRAME_MESSAGE_BUF (sf))
8256 {
8257 Lisp_Object mini_window;
8258 struct frame *f;
8259
8260 /* Get the frame containing the mini-buffer
8261 that the selected frame is using. */
8262 mini_window = FRAME_MINIBUF_WINDOW (sf);
8263 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8264
8265 FRAME_SAMPLE_VISIBILITY (f);
8266 if (FRAME_VISIBLE_P (sf)
8267 && ! FRAME_VISIBLE_P (f))
8268 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8269
8270 if (m)
8271 {
8272 set_message (m, Qnil, nbytes, multibyte);
8273 if (minibuffer_auto_raise)
8274 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8275 }
8276 else
8277 clear_message (1, 1);
8278
8279 do_pending_window_change (0);
8280 echo_area_display (1);
8281 do_pending_window_change (0);
8282 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8283 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8284 }
8285 }
8286
8287
8288 /* Display an echo area message M with a specified length of NBYTES
8289 bytes. The string may include null characters. If M is not a
8290 string, clear out any existing message, and let the mini-buffer
8291 text show through.
8292
8293 This function cancels echoing. */
8294
8295 void
8296 message3 (Lisp_Object m, int nbytes, int multibyte)
8297 {
8298 struct gcpro gcpro1;
8299
8300 GCPRO1 (m);
8301 clear_message (1,1);
8302 cancel_echoing ();
8303
8304 /* First flush out any partial line written with print. */
8305 message_log_maybe_newline ();
8306 if (STRINGP (m))
8307 {
8308 char *buffer;
8309 USE_SAFE_ALLOCA;
8310
8311 SAFE_ALLOCA (buffer, char *, nbytes);
8312 memcpy (buffer, SDATA (m), nbytes);
8313 message_dolog (buffer, nbytes, 1, multibyte);
8314 SAFE_FREE ();
8315 }
8316 message3_nolog (m, nbytes, multibyte);
8317
8318 UNGCPRO;
8319 }
8320
8321
8322 /* The non-logging version of message3.
8323 This does not cancel echoing, because it is used for echoing.
8324 Perhaps we need to make a separate function for echoing
8325 and make this cancel echoing. */
8326
8327 void
8328 message3_nolog (Lisp_Object m, int nbytes, int multibyte)
8329 {
8330 struct frame *sf = SELECTED_FRAME ();
8331 message_enable_multibyte = multibyte;
8332
8333 if (FRAME_INITIAL_P (sf))
8334 {
8335 if (noninteractive_need_newline)
8336 putc ('\n', stderr);
8337 noninteractive_need_newline = 0;
8338 if (STRINGP (m))
8339 fwrite (SDATA (m), nbytes, 1, stderr);
8340 if (cursor_in_echo_area == 0)
8341 fprintf (stderr, "\n");
8342 fflush (stderr);
8343 }
8344 /* A null message buffer means that the frame hasn't really been
8345 initialized yet. Error messages get reported properly by
8346 cmd_error, so this must be just an informative message; toss it. */
8347 else if (INTERACTIVE
8348 && sf->glyphs_initialized_p
8349 && FRAME_MESSAGE_BUF (sf))
8350 {
8351 Lisp_Object mini_window;
8352 Lisp_Object frame;
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 frame = XWINDOW (mini_window)->frame;
8359 f = XFRAME (frame);
8360
8361 FRAME_SAMPLE_VISIBILITY (f);
8362 if (FRAME_VISIBLE_P (sf)
8363 && !FRAME_VISIBLE_P (f))
8364 Fmake_frame_visible (frame);
8365
8366 if (STRINGP (m) && SCHARS (m) > 0)
8367 {
8368 set_message (NULL, m, nbytes, multibyte);
8369 if (minibuffer_auto_raise)
8370 Fraise_frame (frame);
8371 /* Assume we are not echoing.
8372 (If we are, echo_now will override this.) */
8373 echo_message_buffer = Qnil;
8374 }
8375 else
8376 clear_message (1, 1);
8377
8378 do_pending_window_change (0);
8379 echo_area_display (1);
8380 do_pending_window_change (0);
8381 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8382 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8383 }
8384 }
8385
8386
8387 /* Display a null-terminated echo area message M. If M is 0, clear
8388 out any existing message, and let the mini-buffer text show through.
8389
8390 The buffer M must continue to exist until after the echo area gets
8391 cleared or some other message gets displayed there. Do not pass
8392 text that is stored in a Lisp string. Do not pass text in a buffer
8393 that was alloca'd. */
8394
8395 void
8396 message1 (const char *m)
8397 {
8398 message2 (m, (m ? strlen (m) : 0), 0);
8399 }
8400
8401
8402 /* The non-logging counterpart of message1. */
8403
8404 void
8405 message1_nolog (const char *m)
8406 {
8407 message2_nolog (m, (m ? strlen (m) : 0), 0);
8408 }
8409
8410 /* Display a message M which contains a single %s
8411 which gets replaced with STRING. */
8412
8413 void
8414 message_with_string (const char *m, Lisp_Object string, int log)
8415 {
8416 CHECK_STRING (string);
8417
8418 if (noninteractive)
8419 {
8420 if (m)
8421 {
8422 if (noninteractive_need_newline)
8423 putc ('\n', stderr);
8424 noninteractive_need_newline = 0;
8425 fprintf (stderr, m, SDATA (string));
8426 if (!cursor_in_echo_area)
8427 fprintf (stderr, "\n");
8428 fflush (stderr);
8429 }
8430 }
8431 else if (INTERACTIVE)
8432 {
8433 /* The frame whose minibuffer we're going to display the message on.
8434 It may be larger than the selected frame, so we need
8435 to use its buffer, not the selected frame's buffer. */
8436 Lisp_Object mini_window;
8437 struct frame *f, *sf = SELECTED_FRAME ();
8438
8439 /* Get the frame containing the minibuffer
8440 that the selected frame is using. */
8441 mini_window = FRAME_MINIBUF_WINDOW (sf);
8442 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8443
8444 /* A null message buffer means that the frame hasn't really been
8445 initialized yet. Error messages get reported properly by
8446 cmd_error, so this must be just an informative message; toss it. */
8447 if (FRAME_MESSAGE_BUF (f))
8448 {
8449 Lisp_Object args[2], message;
8450 struct gcpro gcpro1, gcpro2;
8451
8452 args[0] = build_string (m);
8453 args[1] = message = string;
8454 GCPRO2 (args[0], message);
8455 gcpro1.nvars = 2;
8456
8457 message = Fformat (2, args);
8458
8459 if (log)
8460 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8461 else
8462 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8463
8464 UNGCPRO;
8465
8466 /* Print should start at the beginning of the message
8467 buffer next time. */
8468 message_buf_print = 0;
8469 }
8470 }
8471 }
8472
8473
8474 /* Dump an informative message to the minibuf. If M is 0, clear out
8475 any existing message, and let the mini-buffer text show through. */
8476
8477 static void
8478 vmessage (const char *m, va_list ap)
8479 {
8480 if (noninteractive)
8481 {
8482 if (m)
8483 {
8484 if (noninteractive_need_newline)
8485 putc ('\n', stderr);
8486 noninteractive_need_newline = 0;
8487 vfprintf (stderr, m, ap);
8488 if (cursor_in_echo_area == 0)
8489 fprintf (stderr, "\n");
8490 fflush (stderr);
8491 }
8492 }
8493 else if (INTERACTIVE)
8494 {
8495 /* The frame whose mini-buffer we're going to display the message
8496 on. It may be larger than the selected frame, so we need to
8497 use its buffer, not the selected frame's buffer. */
8498 Lisp_Object mini_window;
8499 struct frame *f, *sf = SELECTED_FRAME ();
8500
8501 /* Get the frame containing the mini-buffer
8502 that the selected frame is using. */
8503 mini_window = FRAME_MINIBUF_WINDOW (sf);
8504 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8505
8506 /* A null message buffer means that the frame hasn't really been
8507 initialized yet. Error messages get reported properly by
8508 cmd_error, so this must be just an informative message; toss
8509 it. */
8510 if (FRAME_MESSAGE_BUF (f))
8511 {
8512 if (m)
8513 {
8514 int len;
8515
8516 len = doprnt (FRAME_MESSAGE_BUF (f),
8517 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8518
8519 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8520 }
8521 else
8522 message1 (0);
8523
8524 /* Print should start at the beginning of the message
8525 buffer next time. */
8526 message_buf_print = 0;
8527 }
8528 }
8529 }
8530
8531 void
8532 message (const char *m, ...)
8533 {
8534 va_list ap;
8535 va_start (ap, m);
8536 vmessage (m, ap);
8537 va_end (ap);
8538 }
8539
8540
8541 /* The non-logging version of message. */
8542
8543 void
8544 message_nolog (const char *m, ...)
8545 {
8546 Lisp_Object old_log_max;
8547 va_list ap;
8548 va_start (ap, m);
8549 old_log_max = Vmessage_log_max;
8550 Vmessage_log_max = Qnil;
8551 vmessage (m, ap);
8552 Vmessage_log_max = old_log_max;
8553 va_end (ap);
8554 }
8555
8556
8557 /* Display the current message in the current mini-buffer. This is
8558 only called from error handlers in process.c, and is not time
8559 critical. */
8560
8561 void
8562 update_echo_area (void)
8563 {
8564 if (!NILP (echo_area_buffer[0]))
8565 {
8566 Lisp_Object string;
8567 string = Fcurrent_message ();
8568 message3 (string, SBYTES (string),
8569 !NILP (current_buffer->enable_multibyte_characters));
8570 }
8571 }
8572
8573
8574 /* Make sure echo area buffers in `echo_buffers' are live.
8575 If they aren't, make new ones. */
8576
8577 static void
8578 ensure_echo_area_buffers (void)
8579 {
8580 int i;
8581
8582 for (i = 0; i < 2; ++i)
8583 if (!BUFFERP (echo_buffer[i])
8584 || NILP (XBUFFER (echo_buffer[i])->name))
8585 {
8586 char name[30];
8587 Lisp_Object old_buffer;
8588 int j;
8589
8590 old_buffer = echo_buffer[i];
8591 sprintf (name, " *Echo Area %d*", i);
8592 echo_buffer[i] = Fget_buffer_create (build_string (name));
8593 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8594 /* to force word wrap in echo area -
8595 it was decided to postpone this*/
8596 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8597
8598 for (j = 0; j < 2; ++j)
8599 if (EQ (old_buffer, echo_area_buffer[j]))
8600 echo_area_buffer[j] = echo_buffer[i];
8601 }
8602 }
8603
8604
8605 /* Call FN with args A1..A4 with either the current or last displayed
8606 echo_area_buffer as current buffer.
8607
8608 WHICH zero means use the current message buffer
8609 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8610 from echo_buffer[] and clear it.
8611
8612 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8613 suitable buffer from echo_buffer[] and clear it.
8614
8615 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8616 that the current message becomes the last displayed one, make
8617 choose a suitable buffer for echo_area_buffer[0], and clear it.
8618
8619 Value is what FN returns. */
8620
8621 static int
8622 with_echo_area_buffer (struct window *w, int which,
8623 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
8624 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8625 {
8626 Lisp_Object buffer;
8627 int this_one, the_other, clear_buffer_p, rc;
8628 int count = SPECPDL_INDEX ();
8629
8630 /* If buffers aren't live, make new ones. */
8631 ensure_echo_area_buffers ();
8632
8633 clear_buffer_p = 0;
8634
8635 if (which == 0)
8636 this_one = 0, the_other = 1;
8637 else if (which > 0)
8638 this_one = 1, the_other = 0;
8639 else
8640 {
8641 this_one = 0, the_other = 1;
8642 clear_buffer_p = 1;
8643
8644 /* We need a fresh one in case the current echo buffer equals
8645 the one containing the last displayed echo area message. */
8646 if (!NILP (echo_area_buffer[this_one])
8647 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8648 echo_area_buffer[this_one] = Qnil;
8649 }
8650
8651 /* Choose a suitable buffer from echo_buffer[] is we don't
8652 have one. */
8653 if (NILP (echo_area_buffer[this_one]))
8654 {
8655 echo_area_buffer[this_one]
8656 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8657 ? echo_buffer[the_other]
8658 : echo_buffer[this_one]);
8659 clear_buffer_p = 1;
8660 }
8661
8662 buffer = echo_area_buffer[this_one];
8663
8664 /* Don't get confused by reusing the buffer used for echoing
8665 for a different purpose. */
8666 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8667 cancel_echoing ();
8668
8669 record_unwind_protect (unwind_with_echo_area_buffer,
8670 with_echo_area_buffer_unwind_data (w));
8671
8672 /* Make the echo area buffer current. Note that for display
8673 purposes, it is not necessary that the displayed window's buffer
8674 == current_buffer, except for text property lookup. So, let's
8675 only set that buffer temporarily here without doing a full
8676 Fset_window_buffer. We must also change w->pointm, though,
8677 because otherwise an assertions in unshow_buffer fails, and Emacs
8678 aborts. */
8679 set_buffer_internal_1 (XBUFFER (buffer));
8680 if (w)
8681 {
8682 w->buffer = buffer;
8683 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8684 }
8685
8686 current_buffer->undo_list = Qt;
8687 current_buffer->read_only = Qnil;
8688 specbind (Qinhibit_read_only, Qt);
8689 specbind (Qinhibit_modification_hooks, Qt);
8690
8691 if (clear_buffer_p && Z > BEG)
8692 del_range (BEG, Z);
8693
8694 xassert (BEGV >= BEG);
8695 xassert (ZV <= Z && ZV >= BEGV);
8696
8697 rc = fn (a1, a2, a3, a4);
8698
8699 xassert (BEGV >= BEG);
8700 xassert (ZV <= Z && ZV >= BEGV);
8701
8702 unbind_to (count, Qnil);
8703 return rc;
8704 }
8705
8706
8707 /* Save state that should be preserved around the call to the function
8708 FN called in with_echo_area_buffer. */
8709
8710 static Lisp_Object
8711 with_echo_area_buffer_unwind_data (struct window *w)
8712 {
8713 int i = 0;
8714 Lisp_Object vector, tmp;
8715
8716 /* Reduce consing by keeping one vector in
8717 Vwith_echo_area_save_vector. */
8718 vector = Vwith_echo_area_save_vector;
8719 Vwith_echo_area_save_vector = Qnil;
8720
8721 if (NILP (vector))
8722 vector = Fmake_vector (make_number (7), Qnil);
8723
8724 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8725 ASET (vector, i, Vdeactivate_mark); ++i;
8726 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8727
8728 if (w)
8729 {
8730 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8731 ASET (vector, i, w->buffer); ++i;
8732 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8733 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8734 }
8735 else
8736 {
8737 int end = i + 4;
8738 for (; i < end; ++i)
8739 ASET (vector, i, Qnil);
8740 }
8741
8742 xassert (i == ASIZE (vector));
8743 return vector;
8744 }
8745
8746
8747 /* Restore global state from VECTOR which was created by
8748 with_echo_area_buffer_unwind_data. */
8749
8750 static Lisp_Object
8751 unwind_with_echo_area_buffer (Lisp_Object vector)
8752 {
8753 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8754 Vdeactivate_mark = AREF (vector, 1);
8755 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8756
8757 if (WINDOWP (AREF (vector, 3)))
8758 {
8759 struct window *w;
8760 Lisp_Object buffer, charpos, bytepos;
8761
8762 w = XWINDOW (AREF (vector, 3));
8763 buffer = AREF (vector, 4);
8764 charpos = AREF (vector, 5);
8765 bytepos = AREF (vector, 6);
8766
8767 w->buffer = buffer;
8768 set_marker_both (w->pointm, buffer,
8769 XFASTINT (charpos), XFASTINT (bytepos));
8770 }
8771
8772 Vwith_echo_area_save_vector = vector;
8773 return Qnil;
8774 }
8775
8776
8777 /* Set up the echo area for use by print functions. MULTIBYTE_P
8778 non-zero means we will print multibyte. */
8779
8780 void
8781 setup_echo_area_for_printing (int multibyte_p)
8782 {
8783 /* If we can't find an echo area any more, exit. */
8784 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8785 Fkill_emacs (Qnil);
8786
8787 ensure_echo_area_buffers ();
8788
8789 if (!message_buf_print)
8790 {
8791 /* A message has been output since the last time we printed.
8792 Choose a fresh echo area buffer. */
8793 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8794 echo_area_buffer[0] = echo_buffer[1];
8795 else
8796 echo_area_buffer[0] = echo_buffer[0];
8797
8798 /* Switch to that buffer and clear it. */
8799 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8800 current_buffer->truncate_lines = Qnil;
8801
8802 if (Z > BEG)
8803 {
8804 int count = SPECPDL_INDEX ();
8805 specbind (Qinhibit_read_only, Qt);
8806 /* Note that undo recording is always disabled. */
8807 del_range (BEG, Z);
8808 unbind_to (count, Qnil);
8809 }
8810 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8811
8812 /* Set up the buffer for the multibyteness we need. */
8813 if (multibyte_p
8814 != !NILP (current_buffer->enable_multibyte_characters))
8815 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8816
8817 /* Raise the frame containing the echo area. */
8818 if (minibuffer_auto_raise)
8819 {
8820 struct frame *sf = SELECTED_FRAME ();
8821 Lisp_Object mini_window;
8822 mini_window = FRAME_MINIBUF_WINDOW (sf);
8823 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8824 }
8825
8826 message_log_maybe_newline ();
8827 message_buf_print = 1;
8828 }
8829 else
8830 {
8831 if (NILP (echo_area_buffer[0]))
8832 {
8833 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8834 echo_area_buffer[0] = echo_buffer[1];
8835 else
8836 echo_area_buffer[0] = echo_buffer[0];
8837 }
8838
8839 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8840 {
8841 /* Someone switched buffers between print requests. */
8842 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8843 current_buffer->truncate_lines = Qnil;
8844 }
8845 }
8846 }
8847
8848
8849 /* Display an echo area message in window W. Value is non-zero if W's
8850 height is changed. If display_last_displayed_message_p is
8851 non-zero, display the message that was last displayed, otherwise
8852 display the current message. */
8853
8854 static int
8855 display_echo_area (struct window *w)
8856 {
8857 int i, no_message_p, window_height_changed_p, count;
8858
8859 /* Temporarily disable garbage collections while displaying the echo
8860 area. This is done because a GC can print a message itself.
8861 That message would modify the echo area buffer's contents while a
8862 redisplay of the buffer is going on, and seriously confuse
8863 redisplay. */
8864 count = inhibit_garbage_collection ();
8865
8866 /* If there is no message, we must call display_echo_area_1
8867 nevertheless because it resizes the window. But we will have to
8868 reset the echo_area_buffer in question to nil at the end because
8869 with_echo_area_buffer will sets it to an empty buffer. */
8870 i = display_last_displayed_message_p ? 1 : 0;
8871 no_message_p = NILP (echo_area_buffer[i]);
8872
8873 window_height_changed_p
8874 = with_echo_area_buffer (w, display_last_displayed_message_p,
8875 display_echo_area_1,
8876 (EMACS_INT) w, Qnil, 0, 0);
8877
8878 if (no_message_p)
8879 echo_area_buffer[i] = Qnil;
8880
8881 unbind_to (count, Qnil);
8882 return window_height_changed_p;
8883 }
8884
8885
8886 /* Helper for display_echo_area. Display the current buffer which
8887 contains the current echo area message in window W, a mini-window,
8888 a pointer to which is passed in A1. A2..A4 are currently not used.
8889 Change the height of W so that all of the message is displayed.
8890 Value is non-zero if height of W was changed. */
8891
8892 static int
8893 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8894 {
8895 struct window *w = (struct window *) a1;
8896 Lisp_Object window;
8897 struct text_pos start;
8898 int window_height_changed_p = 0;
8899
8900 /* Do this before displaying, so that we have a large enough glyph
8901 matrix for the display. If we can't get enough space for the
8902 whole text, display the last N lines. That works by setting w->start. */
8903 window_height_changed_p = resize_mini_window (w, 0);
8904
8905 /* Use the starting position chosen by resize_mini_window. */
8906 SET_TEXT_POS_FROM_MARKER (start, w->start);
8907
8908 /* Display. */
8909 clear_glyph_matrix (w->desired_matrix);
8910 XSETWINDOW (window, w);
8911 try_window (window, start, 0);
8912
8913 return window_height_changed_p;
8914 }
8915
8916
8917 /* Resize the echo area window to exactly the size needed for the
8918 currently displayed message, if there is one. If a mini-buffer
8919 is active, don't shrink it. */
8920
8921 void
8922 resize_echo_area_exactly (void)
8923 {
8924 if (BUFFERP (echo_area_buffer[0])
8925 && WINDOWP (echo_area_window))
8926 {
8927 struct window *w = XWINDOW (echo_area_window);
8928 int resized_p;
8929 Lisp_Object resize_exactly;
8930
8931 if (minibuf_level == 0)
8932 resize_exactly = Qt;
8933 else
8934 resize_exactly = Qnil;
8935
8936 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8937 (EMACS_INT) w, resize_exactly, 0, 0);
8938 if (resized_p)
8939 {
8940 ++windows_or_buffers_changed;
8941 ++update_mode_lines;
8942 redisplay_internal (0);
8943 }
8944 }
8945 }
8946
8947
8948 /* Callback function for with_echo_area_buffer, when used from
8949 resize_echo_area_exactly. A1 contains a pointer to the window to
8950 resize, EXACTLY non-nil means resize the mini-window exactly to the
8951 size of the text displayed. A3 and A4 are not used. Value is what
8952 resize_mini_window returns. */
8953
8954 static int
8955 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
8956 {
8957 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8958 }
8959
8960
8961 /* Resize mini-window W to fit the size of its contents. EXACT_P
8962 means size the window exactly to the size needed. Otherwise, it's
8963 only enlarged until W's buffer is empty.
8964
8965 Set W->start to the right place to begin display. If the whole
8966 contents fit, start at the beginning. Otherwise, start so as
8967 to make the end of the contents appear. This is particularly
8968 important for y-or-n-p, but seems desirable generally.
8969
8970 Value is non-zero if the window height has been changed. */
8971
8972 int
8973 resize_mini_window (struct window *w, int exact_p)
8974 {
8975 struct frame *f = XFRAME (w->frame);
8976 int window_height_changed_p = 0;
8977
8978 xassert (MINI_WINDOW_P (w));
8979
8980 /* By default, start display at the beginning. */
8981 set_marker_both (w->start, w->buffer,
8982 BUF_BEGV (XBUFFER (w->buffer)),
8983 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8984
8985 /* Don't resize windows while redisplaying a window; it would
8986 confuse redisplay functions when the size of the window they are
8987 displaying changes from under them. Such a resizing can happen,
8988 for instance, when which-func prints a long message while
8989 we are running fontification-functions. We're running these
8990 functions with safe_call which binds inhibit-redisplay to t. */
8991 if (!NILP (Vinhibit_redisplay))
8992 return 0;
8993
8994 /* Nil means don't try to resize. */
8995 if (NILP (Vresize_mini_windows)
8996 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8997 return 0;
8998
8999 if (!FRAME_MINIBUF_ONLY_P (f))
9000 {
9001 struct it it;
9002 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9003 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9004 int height, max_height;
9005 int unit = FRAME_LINE_HEIGHT (f);
9006 struct text_pos start;
9007 struct buffer *old_current_buffer = NULL;
9008
9009 if (current_buffer != XBUFFER (w->buffer))
9010 {
9011 old_current_buffer = current_buffer;
9012 set_buffer_internal (XBUFFER (w->buffer));
9013 }
9014
9015 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9016
9017 /* Compute the max. number of lines specified by the user. */
9018 if (FLOATP (Vmax_mini_window_height))
9019 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9020 else if (INTEGERP (Vmax_mini_window_height))
9021 max_height = XINT (Vmax_mini_window_height);
9022 else
9023 max_height = total_height / 4;
9024
9025 /* Correct that max. height if it's bogus. */
9026 max_height = max (1, max_height);
9027 max_height = min (total_height, max_height);
9028
9029 /* Find out the height of the text in the window. */
9030 if (it.line_wrap == TRUNCATE)
9031 height = 1;
9032 else
9033 {
9034 last_height = 0;
9035 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9036 if (it.max_ascent == 0 && it.max_descent == 0)
9037 height = it.current_y + last_height;
9038 else
9039 height = it.current_y + it.max_ascent + it.max_descent;
9040 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9041 height = (height + unit - 1) / unit;
9042 }
9043
9044 /* Compute a suitable window start. */
9045 if (height > max_height)
9046 {
9047 height = max_height;
9048 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9049 move_it_vertically_backward (&it, (height - 1) * unit);
9050 start = it.current.pos;
9051 }
9052 else
9053 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9054 SET_MARKER_FROM_TEXT_POS (w->start, start);
9055
9056 if (EQ (Vresize_mini_windows, Qgrow_only))
9057 {
9058 /* Let it grow only, until we display an empty message, in which
9059 case the window shrinks again. */
9060 if (height > WINDOW_TOTAL_LINES (w))
9061 {
9062 int old_height = WINDOW_TOTAL_LINES (w);
9063 freeze_window_starts (f, 1);
9064 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9065 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9066 }
9067 else if (height < WINDOW_TOTAL_LINES (w)
9068 && (exact_p || BEGV == ZV))
9069 {
9070 int old_height = WINDOW_TOTAL_LINES (w);
9071 freeze_window_starts (f, 0);
9072 shrink_mini_window (w);
9073 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9074 }
9075 }
9076 else
9077 {
9078 /* Always resize to exact size needed. */
9079 if (height > WINDOW_TOTAL_LINES (w))
9080 {
9081 int old_height = WINDOW_TOTAL_LINES (w);
9082 freeze_window_starts (f, 1);
9083 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9084 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9085 }
9086 else if (height < WINDOW_TOTAL_LINES (w))
9087 {
9088 int old_height = WINDOW_TOTAL_LINES (w);
9089 freeze_window_starts (f, 0);
9090 shrink_mini_window (w);
9091
9092 if (height)
9093 {
9094 freeze_window_starts (f, 1);
9095 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9096 }
9097
9098 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9099 }
9100 }
9101
9102 if (old_current_buffer)
9103 set_buffer_internal (old_current_buffer);
9104 }
9105
9106 return window_height_changed_p;
9107 }
9108
9109
9110 /* Value is the current message, a string, or nil if there is no
9111 current message. */
9112
9113 Lisp_Object
9114 current_message (void)
9115 {
9116 Lisp_Object msg;
9117
9118 if (!BUFFERP (echo_area_buffer[0]))
9119 msg = Qnil;
9120 else
9121 {
9122 with_echo_area_buffer (0, 0, current_message_1,
9123 (EMACS_INT) &msg, Qnil, 0, 0);
9124 if (NILP (msg))
9125 echo_area_buffer[0] = Qnil;
9126 }
9127
9128 return msg;
9129 }
9130
9131
9132 static int
9133 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9134 {
9135 Lisp_Object *msg = (Lisp_Object *) a1;
9136
9137 if (Z > BEG)
9138 *msg = make_buffer_string (BEG, Z, 1);
9139 else
9140 *msg = Qnil;
9141 return 0;
9142 }
9143
9144
9145 /* Push the current message on Vmessage_stack for later restauration
9146 by restore_message. Value is non-zero if the current message isn't
9147 empty. This is a relatively infrequent operation, so it's not
9148 worth optimizing. */
9149
9150 int
9151 push_message (void)
9152 {
9153 Lisp_Object msg;
9154 msg = current_message ();
9155 Vmessage_stack = Fcons (msg, Vmessage_stack);
9156 return STRINGP (msg);
9157 }
9158
9159
9160 /* Restore message display from the top of Vmessage_stack. */
9161
9162 void
9163 restore_message (void)
9164 {
9165 Lisp_Object msg;
9166
9167 xassert (CONSP (Vmessage_stack));
9168 msg = XCAR (Vmessage_stack);
9169 if (STRINGP (msg))
9170 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9171 else
9172 message3_nolog (msg, 0, 0);
9173 }
9174
9175
9176 /* Handler for record_unwind_protect calling pop_message. */
9177
9178 Lisp_Object
9179 pop_message_unwind (Lisp_Object dummy)
9180 {
9181 pop_message ();
9182 return Qnil;
9183 }
9184
9185 /* Pop the top-most entry off Vmessage_stack. */
9186
9187 void
9188 pop_message (void)
9189 {
9190 xassert (CONSP (Vmessage_stack));
9191 Vmessage_stack = XCDR (Vmessage_stack);
9192 }
9193
9194
9195 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9196 exits. If the stack is not empty, we have a missing pop_message
9197 somewhere. */
9198
9199 void
9200 check_message_stack (void)
9201 {
9202 if (!NILP (Vmessage_stack))
9203 abort ();
9204 }
9205
9206
9207 /* Truncate to NCHARS what will be displayed in the echo area the next
9208 time we display it---but don't redisplay it now. */
9209
9210 void
9211 truncate_echo_area (int nchars)
9212 {
9213 if (nchars == 0)
9214 echo_area_buffer[0] = Qnil;
9215 /* A null message buffer means that the frame hasn't really been
9216 initialized yet. Error messages get reported properly by
9217 cmd_error, so this must be just an informative message; toss it. */
9218 else if (!noninteractive
9219 && INTERACTIVE
9220 && !NILP (echo_area_buffer[0]))
9221 {
9222 struct frame *sf = SELECTED_FRAME ();
9223 if (FRAME_MESSAGE_BUF (sf))
9224 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9225 }
9226 }
9227
9228
9229 /* Helper function for truncate_echo_area. Truncate the current
9230 message to at most NCHARS characters. */
9231
9232 static int
9233 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9234 {
9235 if (BEG + nchars < Z)
9236 del_range (BEG + nchars, Z);
9237 if (Z == BEG)
9238 echo_area_buffer[0] = Qnil;
9239 return 0;
9240 }
9241
9242
9243 /* Set the current message to a substring of S or STRING.
9244
9245 If STRING is a Lisp string, set the message to the first NBYTES
9246 bytes from STRING. NBYTES zero means use the whole string. If
9247 STRING is multibyte, the message will be displayed multibyte.
9248
9249 If S is not null, set the message to the first LEN bytes of S. LEN
9250 zero means use the whole string. MULTIBYTE_P non-zero means S is
9251 multibyte. Display the message multibyte in that case.
9252
9253 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9254 to t before calling set_message_1 (which calls insert).
9255 */
9256
9257 void
9258 set_message (const char *s, Lisp_Object string, int nbytes, int multibyte_p)
9259 {
9260 message_enable_multibyte
9261 = ((s && multibyte_p)
9262 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9263
9264 with_echo_area_buffer (0, -1, set_message_1,
9265 (EMACS_INT) s, string, nbytes, multibyte_p);
9266 message_buf_print = 0;
9267 help_echo_showing_p = 0;
9268 }
9269
9270
9271 /* Helper function for set_message. Arguments have the same meaning
9272 as there, with A1 corresponding to S and A2 corresponding to STRING
9273 This function is called with the echo area buffer being
9274 current. */
9275
9276 static int
9277 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9278 {
9279 const char *s = (const char *) a1;
9280 Lisp_Object string = a2;
9281
9282 /* Change multibyteness of the echo buffer appropriately. */
9283 if (message_enable_multibyte
9284 != !NILP (current_buffer->enable_multibyte_characters))
9285 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9286
9287 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9288
9289 /* Insert new message at BEG. */
9290 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9291
9292 if (STRINGP (string))
9293 {
9294 int nchars;
9295
9296 if (nbytes == 0)
9297 nbytes = SBYTES (string);
9298 nchars = string_byte_to_char (string, nbytes);
9299
9300 /* This function takes care of single/multibyte conversion. We
9301 just have to ensure that the echo area buffer has the right
9302 setting of enable_multibyte_characters. */
9303 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9304 }
9305 else if (s)
9306 {
9307 if (nbytes == 0)
9308 nbytes = strlen (s);
9309
9310 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9311 {
9312 /* Convert from multi-byte to single-byte. */
9313 int i, c, n;
9314 unsigned char work[1];
9315
9316 /* Convert a multibyte string to single-byte. */
9317 for (i = 0; i < nbytes; i += n)
9318 {
9319 c = string_char_and_length (s + i, &n);
9320 work[0] = (ASCII_CHAR_P (c)
9321 ? c
9322 : multibyte_char_to_unibyte (c, Qnil));
9323 insert_1_both (work, 1, 1, 1, 0, 0);
9324 }
9325 }
9326 else if (!multibyte_p
9327 && !NILP (current_buffer->enable_multibyte_characters))
9328 {
9329 /* Convert from single-byte to multi-byte. */
9330 int i, c, n;
9331 const unsigned char *msg = (const unsigned char *) s;
9332 unsigned char str[MAX_MULTIBYTE_LENGTH];
9333
9334 /* Convert a single-byte string to multibyte. */
9335 for (i = 0; i < nbytes; i++)
9336 {
9337 c = msg[i];
9338 MAKE_CHAR_MULTIBYTE (c);
9339 n = CHAR_STRING (c, str);
9340 insert_1_both (str, 1, n, 1, 0, 0);
9341 }
9342 }
9343 else
9344 insert_1 (s, nbytes, 1, 0, 0);
9345 }
9346
9347 return 0;
9348 }
9349
9350
9351 /* Clear messages. CURRENT_P non-zero means clear the current
9352 message. LAST_DISPLAYED_P non-zero means clear the message
9353 last displayed. */
9354
9355 void
9356 clear_message (int current_p, int last_displayed_p)
9357 {
9358 if (current_p)
9359 {
9360 echo_area_buffer[0] = Qnil;
9361 message_cleared_p = 1;
9362 }
9363
9364 if (last_displayed_p)
9365 echo_area_buffer[1] = Qnil;
9366
9367 message_buf_print = 0;
9368 }
9369
9370 /* Clear garbaged frames.
9371
9372 This function is used where the old redisplay called
9373 redraw_garbaged_frames which in turn called redraw_frame which in
9374 turn called clear_frame. The call to clear_frame was a source of
9375 flickering. I believe a clear_frame is not necessary. It should
9376 suffice in the new redisplay to invalidate all current matrices,
9377 and ensure a complete redisplay of all windows. */
9378
9379 static void
9380 clear_garbaged_frames (void)
9381 {
9382 if (frame_garbaged)
9383 {
9384 Lisp_Object tail, frame;
9385 int changed_count = 0;
9386
9387 FOR_EACH_FRAME (tail, frame)
9388 {
9389 struct frame *f = XFRAME (frame);
9390
9391 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9392 {
9393 if (f->resized_p)
9394 {
9395 Fredraw_frame (frame);
9396 f->force_flush_display_p = 1;
9397 }
9398 clear_current_matrices (f);
9399 changed_count++;
9400 f->garbaged = 0;
9401 f->resized_p = 0;
9402 }
9403 }
9404
9405 frame_garbaged = 0;
9406 if (changed_count)
9407 ++windows_or_buffers_changed;
9408 }
9409 }
9410
9411
9412 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9413 is non-zero update selected_frame. Value is non-zero if the
9414 mini-windows height has been changed. */
9415
9416 static int
9417 echo_area_display (int update_frame_p)
9418 {
9419 Lisp_Object mini_window;
9420 struct window *w;
9421 struct frame *f;
9422 int window_height_changed_p = 0;
9423 struct frame *sf = SELECTED_FRAME ();
9424
9425 mini_window = FRAME_MINIBUF_WINDOW (sf);
9426 w = XWINDOW (mini_window);
9427 f = XFRAME (WINDOW_FRAME (w));
9428
9429 /* Don't display if frame is invisible or not yet initialized. */
9430 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9431 return 0;
9432
9433 #ifdef HAVE_WINDOW_SYSTEM
9434 /* When Emacs starts, selected_frame may be the initial terminal
9435 frame. If we let this through, a message would be displayed on
9436 the terminal. */
9437 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9438 return 0;
9439 #endif /* HAVE_WINDOW_SYSTEM */
9440
9441 /* Redraw garbaged frames. */
9442 if (frame_garbaged)
9443 clear_garbaged_frames ();
9444
9445 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9446 {
9447 echo_area_window = mini_window;
9448 window_height_changed_p = display_echo_area (w);
9449 w->must_be_updated_p = 1;
9450
9451 /* Update the display, unless called from redisplay_internal.
9452 Also don't update the screen during redisplay itself. The
9453 update will happen at the end of redisplay, and an update
9454 here could cause confusion. */
9455 if (update_frame_p && !redisplaying_p)
9456 {
9457 int n = 0;
9458
9459 /* If the display update has been interrupted by pending
9460 input, update mode lines in the frame. Due to the
9461 pending input, it might have been that redisplay hasn't
9462 been called, so that mode lines above the echo area are
9463 garbaged. This looks odd, so we prevent it here. */
9464 if (!display_completed)
9465 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9466
9467 if (window_height_changed_p
9468 /* Don't do this if Emacs is shutting down. Redisplay
9469 needs to run hooks. */
9470 && !NILP (Vrun_hooks))
9471 {
9472 /* Must update other windows. Likewise as in other
9473 cases, don't let this update be interrupted by
9474 pending input. */
9475 int count = SPECPDL_INDEX ();
9476 specbind (Qredisplay_dont_pause, Qt);
9477 windows_or_buffers_changed = 1;
9478 redisplay_internal (0);
9479 unbind_to (count, Qnil);
9480 }
9481 else if (FRAME_WINDOW_P (f) && n == 0)
9482 {
9483 /* Window configuration is the same as before.
9484 Can do with a display update of the echo area,
9485 unless we displayed some mode lines. */
9486 update_single_window (w, 1);
9487 FRAME_RIF (f)->flush_display (f);
9488 }
9489 else
9490 update_frame (f, 1, 1);
9491
9492 /* If cursor is in the echo area, make sure that the next
9493 redisplay displays the minibuffer, so that the cursor will
9494 be replaced with what the minibuffer wants. */
9495 if (cursor_in_echo_area)
9496 ++windows_or_buffers_changed;
9497 }
9498 }
9499 else if (!EQ (mini_window, selected_window))
9500 windows_or_buffers_changed++;
9501
9502 /* Last displayed message is now the current message. */
9503 echo_area_buffer[1] = echo_area_buffer[0];
9504 /* Inform read_char that we're not echoing. */
9505 echo_message_buffer = Qnil;
9506
9507 /* Prevent redisplay optimization in redisplay_internal by resetting
9508 this_line_start_pos. This is done because the mini-buffer now
9509 displays the message instead of its buffer text. */
9510 if (EQ (mini_window, selected_window))
9511 CHARPOS (this_line_start_pos) = 0;
9512
9513 return window_height_changed_p;
9514 }
9515
9516
9517 \f
9518 /***********************************************************************
9519 Mode Lines and Frame Titles
9520 ***********************************************************************/
9521
9522 /* A buffer for constructing non-propertized mode-line strings and
9523 frame titles in it; allocated from the heap in init_xdisp and
9524 resized as needed in store_mode_line_noprop_char. */
9525
9526 static char *mode_line_noprop_buf;
9527
9528 /* The buffer's end, and a current output position in it. */
9529
9530 static char *mode_line_noprop_buf_end;
9531 static char *mode_line_noprop_ptr;
9532
9533 #define MODE_LINE_NOPROP_LEN(start) \
9534 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9535
9536 static enum {
9537 MODE_LINE_DISPLAY = 0,
9538 MODE_LINE_TITLE,
9539 MODE_LINE_NOPROP,
9540 MODE_LINE_STRING
9541 } mode_line_target;
9542
9543 /* Alist that caches the results of :propertize.
9544 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9545 static Lisp_Object mode_line_proptrans_alist;
9546
9547 /* List of strings making up the mode-line. */
9548 static Lisp_Object mode_line_string_list;
9549
9550 /* Base face property when building propertized mode line string. */
9551 static Lisp_Object mode_line_string_face;
9552 static Lisp_Object mode_line_string_face_prop;
9553
9554
9555 /* Unwind data for mode line strings */
9556
9557 static Lisp_Object Vmode_line_unwind_vector;
9558
9559 static Lisp_Object
9560 format_mode_line_unwind_data (struct buffer *obuf,
9561 Lisp_Object owin,
9562 int save_proptrans)
9563 {
9564 Lisp_Object vector, tmp;
9565
9566 /* Reduce consing by keeping one vector in
9567 Vwith_echo_area_save_vector. */
9568 vector = Vmode_line_unwind_vector;
9569 Vmode_line_unwind_vector = Qnil;
9570
9571 if (NILP (vector))
9572 vector = Fmake_vector (make_number (8), Qnil);
9573
9574 ASET (vector, 0, make_number (mode_line_target));
9575 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9576 ASET (vector, 2, mode_line_string_list);
9577 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9578 ASET (vector, 4, mode_line_string_face);
9579 ASET (vector, 5, mode_line_string_face_prop);
9580
9581 if (obuf)
9582 XSETBUFFER (tmp, obuf);
9583 else
9584 tmp = Qnil;
9585 ASET (vector, 6, tmp);
9586 ASET (vector, 7, owin);
9587
9588 return vector;
9589 }
9590
9591 static Lisp_Object
9592 unwind_format_mode_line (Lisp_Object vector)
9593 {
9594 mode_line_target = XINT (AREF (vector, 0));
9595 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9596 mode_line_string_list = AREF (vector, 2);
9597 if (! EQ (AREF (vector, 3), Qt))
9598 mode_line_proptrans_alist = AREF (vector, 3);
9599 mode_line_string_face = AREF (vector, 4);
9600 mode_line_string_face_prop = AREF (vector, 5);
9601
9602 if (!NILP (AREF (vector, 7)))
9603 /* Select window before buffer, since it may change the buffer. */
9604 Fselect_window (AREF (vector, 7), Qt);
9605
9606 if (!NILP (AREF (vector, 6)))
9607 {
9608 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9609 ASET (vector, 6, Qnil);
9610 }
9611
9612 Vmode_line_unwind_vector = vector;
9613 return Qnil;
9614 }
9615
9616
9617 /* Store a single character C for the frame title in mode_line_noprop_buf.
9618 Re-allocate mode_line_noprop_buf if necessary. */
9619
9620 static void
9621 store_mode_line_noprop_char (char c)
9622 {
9623 /* If output position has reached the end of the allocated buffer,
9624 double the buffer's size. */
9625 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9626 {
9627 int len = MODE_LINE_NOPROP_LEN (0);
9628 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9629 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9630 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9631 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9632 }
9633
9634 *mode_line_noprop_ptr++ = c;
9635 }
9636
9637
9638 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9639 mode_line_noprop_ptr. STR is the string to store. Do not copy
9640 characters that yield more columns than PRECISION; PRECISION <= 0
9641 means copy the whole string. Pad with spaces until FIELD_WIDTH
9642 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9643 pad. Called from display_mode_element when it is used to build a
9644 frame title. */
9645
9646 static int
9647 store_mode_line_noprop (const unsigned char *str, int field_width, int precision)
9648 {
9649 int n = 0;
9650 int dummy, nbytes;
9651
9652 /* Copy at most PRECISION chars from STR. */
9653 nbytes = strlen (str);
9654 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9655 while (nbytes--)
9656 store_mode_line_noprop_char (*str++);
9657
9658 /* Fill up with spaces until FIELD_WIDTH reached. */
9659 while (field_width > 0
9660 && n < field_width)
9661 {
9662 store_mode_line_noprop_char (' ');
9663 ++n;
9664 }
9665
9666 return n;
9667 }
9668
9669 /***********************************************************************
9670 Frame Titles
9671 ***********************************************************************/
9672
9673 #ifdef HAVE_WINDOW_SYSTEM
9674
9675 /* Set the title of FRAME, if it has changed. The title format is
9676 Vicon_title_format if FRAME is iconified, otherwise it is
9677 frame_title_format. */
9678
9679 static void
9680 x_consider_frame_title (Lisp_Object frame)
9681 {
9682 struct frame *f = XFRAME (frame);
9683
9684 if (FRAME_WINDOW_P (f)
9685 || FRAME_MINIBUF_ONLY_P (f)
9686 || f->explicit_name)
9687 {
9688 /* Do we have more than one visible frame on this X display? */
9689 Lisp_Object tail;
9690 Lisp_Object fmt;
9691 int title_start;
9692 char *title;
9693 int len;
9694 struct it it;
9695 int count = SPECPDL_INDEX ();
9696
9697 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9698 {
9699 Lisp_Object other_frame = XCAR (tail);
9700 struct frame *tf = XFRAME (other_frame);
9701
9702 if (tf != f
9703 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9704 && !FRAME_MINIBUF_ONLY_P (tf)
9705 && !EQ (other_frame, tip_frame)
9706 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9707 break;
9708 }
9709
9710 /* Set global variable indicating that multiple frames exist. */
9711 multiple_frames = CONSP (tail);
9712
9713 /* Switch to the buffer of selected window of the frame. Set up
9714 mode_line_target so that display_mode_element will output into
9715 mode_line_noprop_buf; then display the title. */
9716 record_unwind_protect (unwind_format_mode_line,
9717 format_mode_line_unwind_data
9718 (current_buffer, selected_window, 0));
9719
9720 Fselect_window (f->selected_window, Qt);
9721 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9722 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9723
9724 mode_line_target = MODE_LINE_TITLE;
9725 title_start = MODE_LINE_NOPROP_LEN (0);
9726 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9727 NULL, DEFAULT_FACE_ID);
9728 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9729 len = MODE_LINE_NOPROP_LEN (title_start);
9730 title = mode_line_noprop_buf + title_start;
9731 unbind_to (count, Qnil);
9732
9733 /* Set the title only if it's changed. This avoids consing in
9734 the common case where it hasn't. (If it turns out that we've
9735 already wasted too much time by walking through the list with
9736 display_mode_element, then we might need to optimize at a
9737 higher level than this.) */
9738 if (! STRINGP (f->name)
9739 || SBYTES (f->name) != len
9740 || memcmp (title, SDATA (f->name), len) != 0)
9741 x_implicitly_set_name (f, make_string (title, len), Qnil);
9742 }
9743 }
9744
9745 #endif /* not HAVE_WINDOW_SYSTEM */
9746
9747
9748
9749 \f
9750 /***********************************************************************
9751 Menu Bars
9752 ***********************************************************************/
9753
9754
9755 /* Prepare for redisplay by updating menu-bar item lists when
9756 appropriate. This can call eval. */
9757
9758 void
9759 prepare_menu_bars (void)
9760 {
9761 int all_windows;
9762 struct gcpro gcpro1, gcpro2;
9763 struct frame *f;
9764 Lisp_Object tooltip_frame;
9765
9766 #ifdef HAVE_WINDOW_SYSTEM
9767 tooltip_frame = tip_frame;
9768 #else
9769 tooltip_frame = Qnil;
9770 #endif
9771
9772 /* Update all frame titles based on their buffer names, etc. We do
9773 this before the menu bars so that the buffer-menu will show the
9774 up-to-date frame titles. */
9775 #ifdef HAVE_WINDOW_SYSTEM
9776 if (windows_or_buffers_changed || update_mode_lines)
9777 {
9778 Lisp_Object tail, frame;
9779
9780 FOR_EACH_FRAME (tail, frame)
9781 {
9782 f = XFRAME (frame);
9783 if (!EQ (frame, tooltip_frame)
9784 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9785 x_consider_frame_title (frame);
9786 }
9787 }
9788 #endif /* HAVE_WINDOW_SYSTEM */
9789
9790 /* Update the menu bar item lists, if appropriate. This has to be
9791 done before any actual redisplay or generation of display lines. */
9792 all_windows = (update_mode_lines
9793 || buffer_shared > 1
9794 || windows_or_buffers_changed);
9795 if (all_windows)
9796 {
9797 Lisp_Object tail, frame;
9798 int count = SPECPDL_INDEX ();
9799 /* 1 means that update_menu_bar has run its hooks
9800 so any further calls to update_menu_bar shouldn't do so again. */
9801 int menu_bar_hooks_run = 0;
9802
9803 record_unwind_save_match_data ();
9804
9805 FOR_EACH_FRAME (tail, frame)
9806 {
9807 f = XFRAME (frame);
9808
9809 /* Ignore tooltip frame. */
9810 if (EQ (frame, tooltip_frame))
9811 continue;
9812
9813 /* If a window on this frame changed size, report that to
9814 the user and clear the size-change flag. */
9815 if (FRAME_WINDOW_SIZES_CHANGED (f))
9816 {
9817 Lisp_Object functions;
9818
9819 /* Clear flag first in case we get an error below. */
9820 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9821 functions = Vwindow_size_change_functions;
9822 GCPRO2 (tail, functions);
9823
9824 while (CONSP (functions))
9825 {
9826 if (!EQ (XCAR (functions), Qt))
9827 call1 (XCAR (functions), frame);
9828 functions = XCDR (functions);
9829 }
9830 UNGCPRO;
9831 }
9832
9833 GCPRO1 (tail);
9834 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9835 #ifdef HAVE_WINDOW_SYSTEM
9836 update_tool_bar (f, 0);
9837 #endif
9838 #ifdef HAVE_NS
9839 if (windows_or_buffers_changed
9840 && FRAME_NS_P (f))
9841 ns_set_doc_edited (f, Fbuffer_modified_p
9842 (XWINDOW (f->selected_window)->buffer));
9843 #endif
9844 UNGCPRO;
9845 }
9846
9847 unbind_to (count, Qnil);
9848 }
9849 else
9850 {
9851 struct frame *sf = SELECTED_FRAME ();
9852 update_menu_bar (sf, 1, 0);
9853 #ifdef HAVE_WINDOW_SYSTEM
9854 update_tool_bar (sf, 1);
9855 #endif
9856 }
9857 }
9858
9859
9860 /* Update the menu bar item list for frame F. This has to be done
9861 before we start to fill in any display lines, because it can call
9862 eval.
9863
9864 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9865
9866 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9867 already ran the menu bar hooks for this redisplay, so there
9868 is no need to run them again. The return value is the
9869 updated value of this flag, to pass to the next call. */
9870
9871 static int
9872 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
9873 {
9874 Lisp_Object window;
9875 register struct window *w;
9876
9877 /* If called recursively during a menu update, do nothing. This can
9878 happen when, for instance, an activate-menubar-hook causes a
9879 redisplay. */
9880 if (inhibit_menubar_update)
9881 return hooks_run;
9882
9883 window = FRAME_SELECTED_WINDOW (f);
9884 w = XWINDOW (window);
9885
9886 if (FRAME_WINDOW_P (f)
9887 ?
9888 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9889 || defined (HAVE_NS) || defined (USE_GTK)
9890 FRAME_EXTERNAL_MENU_BAR (f)
9891 #else
9892 FRAME_MENU_BAR_LINES (f) > 0
9893 #endif
9894 : FRAME_MENU_BAR_LINES (f) > 0)
9895 {
9896 /* If the user has switched buffers or windows, we need to
9897 recompute to reflect the new bindings. But we'll
9898 recompute when update_mode_lines is set too; that means
9899 that people can use force-mode-line-update to request
9900 that the menu bar be recomputed. The adverse effect on
9901 the rest of the redisplay algorithm is about the same as
9902 windows_or_buffers_changed anyway. */
9903 if (windows_or_buffers_changed
9904 /* This used to test w->update_mode_line, but we believe
9905 there is no need to recompute the menu in that case. */
9906 || update_mode_lines
9907 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9908 < BUF_MODIFF (XBUFFER (w->buffer)))
9909 != !NILP (w->last_had_star))
9910 || ((!NILP (Vtransient_mark_mode)
9911 && !NILP (XBUFFER (w->buffer)->mark_active))
9912 != !NILP (w->region_showing)))
9913 {
9914 struct buffer *prev = current_buffer;
9915 int count = SPECPDL_INDEX ();
9916
9917 specbind (Qinhibit_menubar_update, Qt);
9918
9919 set_buffer_internal_1 (XBUFFER (w->buffer));
9920 if (save_match_data)
9921 record_unwind_save_match_data ();
9922 if (NILP (Voverriding_local_map_menu_flag))
9923 {
9924 specbind (Qoverriding_terminal_local_map, Qnil);
9925 specbind (Qoverriding_local_map, Qnil);
9926 }
9927
9928 if (!hooks_run)
9929 {
9930 /* Run the Lucid hook. */
9931 safe_run_hooks (Qactivate_menubar_hook);
9932
9933 /* If it has changed current-menubar from previous value,
9934 really recompute the menu-bar from the value. */
9935 if (! NILP (Vlucid_menu_bar_dirty_flag))
9936 call0 (Qrecompute_lucid_menubar);
9937
9938 safe_run_hooks (Qmenu_bar_update_hook);
9939
9940 hooks_run = 1;
9941 }
9942
9943 XSETFRAME (Vmenu_updating_frame, f);
9944 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9945
9946 /* Redisplay the menu bar in case we changed it. */
9947 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9948 || defined (HAVE_NS) || defined (USE_GTK)
9949 if (FRAME_WINDOW_P (f))
9950 {
9951 #if defined (HAVE_NS)
9952 /* All frames on Mac OS share the same menubar. So only
9953 the selected frame should be allowed to set it. */
9954 if (f == SELECTED_FRAME ())
9955 #endif
9956 set_frame_menubar (f, 0, 0);
9957 }
9958 else
9959 /* On a terminal screen, the menu bar is an ordinary screen
9960 line, and this makes it get updated. */
9961 w->update_mode_line = Qt;
9962 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9963 /* In the non-toolkit version, the menu bar is an ordinary screen
9964 line, and this makes it get updated. */
9965 w->update_mode_line = Qt;
9966 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9967
9968 unbind_to (count, Qnil);
9969 set_buffer_internal_1 (prev);
9970 }
9971 }
9972
9973 return hooks_run;
9974 }
9975
9976
9977 \f
9978 /***********************************************************************
9979 Output Cursor
9980 ***********************************************************************/
9981
9982 #ifdef HAVE_WINDOW_SYSTEM
9983
9984 /* EXPORT:
9985 Nominal cursor position -- where to draw output.
9986 HPOS and VPOS are window relative glyph matrix coordinates.
9987 X and Y are window relative pixel coordinates. */
9988
9989 struct cursor_pos output_cursor;
9990
9991
9992 /* EXPORT:
9993 Set the global variable output_cursor to CURSOR. All cursor
9994 positions are relative to updated_window. */
9995
9996 void
9997 set_output_cursor (struct cursor_pos *cursor)
9998 {
9999 output_cursor.hpos = cursor->hpos;
10000 output_cursor.vpos = cursor->vpos;
10001 output_cursor.x = cursor->x;
10002 output_cursor.y = cursor->y;
10003 }
10004
10005
10006 /* EXPORT for RIF:
10007 Set a nominal cursor position.
10008
10009 HPOS and VPOS are column/row positions in a window glyph matrix. X
10010 and Y are window text area relative pixel positions.
10011
10012 If this is done during an update, updated_window will contain the
10013 window that is being updated and the position is the future output
10014 cursor position for that window. If updated_window is null, use
10015 selected_window and display the cursor at the given position. */
10016
10017 void
10018 x_cursor_to (int vpos, int hpos, int y, int x)
10019 {
10020 struct window *w;
10021
10022 /* If updated_window is not set, work on selected_window. */
10023 if (updated_window)
10024 w = updated_window;
10025 else
10026 w = XWINDOW (selected_window);
10027
10028 /* Set the output cursor. */
10029 output_cursor.hpos = hpos;
10030 output_cursor.vpos = vpos;
10031 output_cursor.x = x;
10032 output_cursor.y = y;
10033
10034 /* If not called as part of an update, really display the cursor.
10035 This will also set the cursor position of W. */
10036 if (updated_window == NULL)
10037 {
10038 BLOCK_INPUT;
10039 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10040 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10041 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10042 UNBLOCK_INPUT;
10043 }
10044 }
10045
10046 #endif /* HAVE_WINDOW_SYSTEM */
10047
10048 \f
10049 /***********************************************************************
10050 Tool-bars
10051 ***********************************************************************/
10052
10053 #ifdef HAVE_WINDOW_SYSTEM
10054
10055 /* Where the mouse was last time we reported a mouse event. */
10056
10057 FRAME_PTR last_mouse_frame;
10058
10059 /* Tool-bar item index of the item on which a mouse button was pressed
10060 or -1. */
10061
10062 int last_tool_bar_item;
10063
10064
10065 static Lisp_Object
10066 update_tool_bar_unwind (Lisp_Object frame)
10067 {
10068 selected_frame = frame;
10069 return Qnil;
10070 }
10071
10072 /* Update the tool-bar item list for frame F. This has to be done
10073 before we start to fill in any display lines. Called from
10074 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10075 and restore it here. */
10076
10077 static void
10078 update_tool_bar (struct frame *f, int save_match_data)
10079 {
10080 #if defined (USE_GTK) || defined (HAVE_NS)
10081 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10082 #else
10083 int do_update = WINDOWP (f->tool_bar_window)
10084 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10085 #endif
10086
10087 if (do_update)
10088 {
10089 Lisp_Object window;
10090 struct window *w;
10091
10092 window = FRAME_SELECTED_WINDOW (f);
10093 w = XWINDOW (window);
10094
10095 /* If the user has switched buffers or windows, we need to
10096 recompute to reflect the new bindings. But we'll
10097 recompute when update_mode_lines is set too; that means
10098 that people can use force-mode-line-update to request
10099 that the menu bar be recomputed. The adverse effect on
10100 the rest of the redisplay algorithm is about the same as
10101 windows_or_buffers_changed anyway. */
10102 if (windows_or_buffers_changed
10103 || !NILP (w->update_mode_line)
10104 || update_mode_lines
10105 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10106 < BUF_MODIFF (XBUFFER (w->buffer)))
10107 != !NILP (w->last_had_star))
10108 || ((!NILP (Vtransient_mark_mode)
10109 && !NILP (XBUFFER (w->buffer)->mark_active))
10110 != !NILP (w->region_showing)))
10111 {
10112 struct buffer *prev = current_buffer;
10113 int count = SPECPDL_INDEX ();
10114 Lisp_Object frame, new_tool_bar;
10115 int new_n_tool_bar;
10116 struct gcpro gcpro1;
10117
10118 /* Set current_buffer to the buffer of the selected
10119 window of the frame, so that we get the right local
10120 keymaps. */
10121 set_buffer_internal_1 (XBUFFER (w->buffer));
10122
10123 /* Save match data, if we must. */
10124 if (save_match_data)
10125 record_unwind_save_match_data ();
10126
10127 /* Make sure that we don't accidentally use bogus keymaps. */
10128 if (NILP (Voverriding_local_map_menu_flag))
10129 {
10130 specbind (Qoverriding_terminal_local_map, Qnil);
10131 specbind (Qoverriding_local_map, Qnil);
10132 }
10133
10134 GCPRO1 (new_tool_bar);
10135
10136 /* We must temporarily set the selected frame to this frame
10137 before calling tool_bar_items, because the calculation of
10138 the tool-bar keymap uses the selected frame (see
10139 `tool-bar-make-keymap' in tool-bar.el). */
10140 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10141 XSETFRAME (frame, f);
10142 selected_frame = frame;
10143
10144 /* Build desired tool-bar items from keymaps. */
10145 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10146 &new_n_tool_bar);
10147
10148 /* Redisplay the tool-bar if we changed it. */
10149 if (new_n_tool_bar != f->n_tool_bar_items
10150 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10151 {
10152 /* Redisplay that happens asynchronously due to an expose event
10153 may access f->tool_bar_items. Make sure we update both
10154 variables within BLOCK_INPUT so no such event interrupts. */
10155 BLOCK_INPUT;
10156 f->tool_bar_items = new_tool_bar;
10157 f->n_tool_bar_items = new_n_tool_bar;
10158 w->update_mode_line = Qt;
10159 UNBLOCK_INPUT;
10160 }
10161
10162 UNGCPRO;
10163
10164 unbind_to (count, Qnil);
10165 set_buffer_internal_1 (prev);
10166 }
10167 }
10168 }
10169
10170
10171 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10172 F's desired tool-bar contents. F->tool_bar_items must have
10173 been set up previously by calling prepare_menu_bars. */
10174
10175 static void
10176 build_desired_tool_bar_string (struct frame *f)
10177 {
10178 int i, size, size_needed;
10179 struct gcpro gcpro1, gcpro2, gcpro3;
10180 Lisp_Object image, plist, props;
10181
10182 image = plist = props = Qnil;
10183 GCPRO3 (image, plist, props);
10184
10185 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10186 Otherwise, make a new string. */
10187
10188 /* The size of the string we might be able to reuse. */
10189 size = (STRINGP (f->desired_tool_bar_string)
10190 ? SCHARS (f->desired_tool_bar_string)
10191 : 0);
10192
10193 /* We need one space in the string for each image. */
10194 size_needed = f->n_tool_bar_items;
10195
10196 /* Reuse f->desired_tool_bar_string, if possible. */
10197 if (size < size_needed || NILP (f->desired_tool_bar_string))
10198 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10199 make_number (' '));
10200 else
10201 {
10202 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10203 Fremove_text_properties (make_number (0), make_number (size),
10204 props, f->desired_tool_bar_string);
10205 }
10206
10207 /* Put a `display' property on the string for the images to display,
10208 put a `menu_item' property on tool-bar items with a value that
10209 is the index of the item in F's tool-bar item vector. */
10210 for (i = 0; i < f->n_tool_bar_items; ++i)
10211 {
10212 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10213
10214 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10215 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10216 int hmargin, vmargin, relief, idx, end;
10217
10218 /* If image is a vector, choose the image according to the
10219 button state. */
10220 image = PROP (TOOL_BAR_ITEM_IMAGES);
10221 if (VECTORP (image))
10222 {
10223 if (enabled_p)
10224 idx = (selected_p
10225 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10226 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10227 else
10228 idx = (selected_p
10229 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10230 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10231
10232 xassert (ASIZE (image) >= idx);
10233 image = AREF (image, idx);
10234 }
10235 else
10236 idx = -1;
10237
10238 /* Ignore invalid image specifications. */
10239 if (!valid_image_p (image))
10240 continue;
10241
10242 /* Display the tool-bar button pressed, or depressed. */
10243 plist = Fcopy_sequence (XCDR (image));
10244
10245 /* Compute margin and relief to draw. */
10246 relief = (tool_bar_button_relief >= 0
10247 ? tool_bar_button_relief
10248 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10249 hmargin = vmargin = relief;
10250
10251 if (INTEGERP (Vtool_bar_button_margin)
10252 && XINT (Vtool_bar_button_margin) > 0)
10253 {
10254 hmargin += XFASTINT (Vtool_bar_button_margin);
10255 vmargin += XFASTINT (Vtool_bar_button_margin);
10256 }
10257 else if (CONSP (Vtool_bar_button_margin))
10258 {
10259 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10260 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10261 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10262
10263 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10264 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10265 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10266 }
10267
10268 if (auto_raise_tool_bar_buttons_p)
10269 {
10270 /* Add a `:relief' property to the image spec if the item is
10271 selected. */
10272 if (selected_p)
10273 {
10274 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10275 hmargin -= relief;
10276 vmargin -= relief;
10277 }
10278 }
10279 else
10280 {
10281 /* If image is selected, display it pressed, i.e. with a
10282 negative relief. If it's not selected, display it with a
10283 raised relief. */
10284 plist = Fplist_put (plist, QCrelief,
10285 (selected_p
10286 ? make_number (-relief)
10287 : make_number (relief)));
10288 hmargin -= relief;
10289 vmargin -= relief;
10290 }
10291
10292 /* Put a margin around the image. */
10293 if (hmargin || vmargin)
10294 {
10295 if (hmargin == vmargin)
10296 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10297 else
10298 plist = Fplist_put (plist, QCmargin,
10299 Fcons (make_number (hmargin),
10300 make_number (vmargin)));
10301 }
10302
10303 /* If button is not enabled, and we don't have special images
10304 for the disabled state, make the image appear disabled by
10305 applying an appropriate algorithm to it. */
10306 if (!enabled_p && idx < 0)
10307 plist = Fplist_put (plist, QCconversion, Qdisabled);
10308
10309 /* Put a `display' text property on the string for the image to
10310 display. Put a `menu-item' property on the string that gives
10311 the start of this item's properties in the tool-bar items
10312 vector. */
10313 image = Fcons (Qimage, plist);
10314 props = list4 (Qdisplay, image,
10315 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10316
10317 /* Let the last image hide all remaining spaces in the tool bar
10318 string. The string can be longer than needed when we reuse a
10319 previous string. */
10320 if (i + 1 == f->n_tool_bar_items)
10321 end = SCHARS (f->desired_tool_bar_string);
10322 else
10323 end = i + 1;
10324 Fadd_text_properties (make_number (i), make_number (end),
10325 props, f->desired_tool_bar_string);
10326 #undef PROP
10327 }
10328
10329 UNGCPRO;
10330 }
10331
10332
10333 /* Display one line of the tool-bar of frame IT->f.
10334
10335 HEIGHT specifies the desired height of the tool-bar line.
10336 If the actual height of the glyph row is less than HEIGHT, the
10337 row's height is increased to HEIGHT, and the icons are centered
10338 vertically in the new height.
10339
10340 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10341 count a final empty row in case the tool-bar width exactly matches
10342 the window width.
10343 */
10344
10345 static void
10346 display_tool_bar_line (struct it *it, int height)
10347 {
10348 struct glyph_row *row = it->glyph_row;
10349 int max_x = it->last_visible_x;
10350 struct glyph *last;
10351
10352 prepare_desired_row (row);
10353 row->y = it->current_y;
10354
10355 /* Note that this isn't made use of if the face hasn't a box,
10356 so there's no need to check the face here. */
10357 it->start_of_box_run_p = 1;
10358
10359 while (it->current_x < max_x)
10360 {
10361 int x, n_glyphs_before, i, nglyphs;
10362 struct it it_before;
10363
10364 /* Get the next display element. */
10365 if (!get_next_display_element (it))
10366 {
10367 /* Don't count empty row if we are counting needed tool-bar lines. */
10368 if (height < 0 && !it->hpos)
10369 return;
10370 break;
10371 }
10372
10373 /* Produce glyphs. */
10374 n_glyphs_before = row->used[TEXT_AREA];
10375 it_before = *it;
10376
10377 PRODUCE_GLYPHS (it);
10378
10379 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10380 i = 0;
10381 x = it_before.current_x;
10382 while (i < nglyphs)
10383 {
10384 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10385
10386 if (x + glyph->pixel_width > max_x)
10387 {
10388 /* Glyph doesn't fit on line. Backtrack. */
10389 row->used[TEXT_AREA] = n_glyphs_before;
10390 *it = it_before;
10391 /* If this is the only glyph on this line, it will never fit on the
10392 toolbar, so skip it. But ensure there is at least one glyph,
10393 so we don't accidentally disable the tool-bar. */
10394 if (n_glyphs_before == 0
10395 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10396 break;
10397 goto out;
10398 }
10399
10400 ++it->hpos;
10401 x += glyph->pixel_width;
10402 ++i;
10403 }
10404
10405 /* Stop at line ends. */
10406 if (ITERATOR_AT_END_OF_LINE_P (it))
10407 break;
10408
10409 set_iterator_to_next (it, 1);
10410 }
10411
10412 out:;
10413
10414 row->displays_text_p = row->used[TEXT_AREA] != 0;
10415
10416 /* Use default face for the border below the tool bar.
10417
10418 FIXME: When auto-resize-tool-bars is grow-only, there is
10419 no additional border below the possibly empty tool-bar lines.
10420 So to make the extra empty lines look "normal", we have to
10421 use the tool-bar face for the border too. */
10422 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10423 it->face_id = DEFAULT_FACE_ID;
10424
10425 extend_face_to_end_of_line (it);
10426 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10427 last->right_box_line_p = 1;
10428 if (last == row->glyphs[TEXT_AREA])
10429 last->left_box_line_p = 1;
10430
10431 /* Make line the desired height and center it vertically. */
10432 if ((height -= it->max_ascent + it->max_descent) > 0)
10433 {
10434 /* Don't add more than one line height. */
10435 height %= FRAME_LINE_HEIGHT (it->f);
10436 it->max_ascent += height / 2;
10437 it->max_descent += (height + 1) / 2;
10438 }
10439
10440 compute_line_metrics (it);
10441
10442 /* If line is empty, make it occupy the rest of the tool-bar. */
10443 if (!row->displays_text_p)
10444 {
10445 row->height = row->phys_height = it->last_visible_y - row->y;
10446 row->visible_height = row->height;
10447 row->ascent = row->phys_ascent = 0;
10448 row->extra_line_spacing = 0;
10449 }
10450
10451 row->full_width_p = 1;
10452 row->continued_p = 0;
10453 row->truncated_on_left_p = 0;
10454 row->truncated_on_right_p = 0;
10455
10456 it->current_x = it->hpos = 0;
10457 it->current_y += row->height;
10458 ++it->vpos;
10459 ++it->glyph_row;
10460 }
10461
10462
10463 /* Max tool-bar height. */
10464
10465 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10466 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10467
10468 /* Value is the number of screen lines needed to make all tool-bar
10469 items of frame F visible. The number of actual rows needed is
10470 returned in *N_ROWS if non-NULL. */
10471
10472 static int
10473 tool_bar_lines_needed (struct frame *f, int *n_rows)
10474 {
10475 struct window *w = XWINDOW (f->tool_bar_window);
10476 struct it it;
10477 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10478 the desired matrix, so use (unused) mode-line row as temporary row to
10479 avoid destroying the first tool-bar row. */
10480 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10481
10482 /* Initialize an iterator for iteration over
10483 F->desired_tool_bar_string in the tool-bar window of frame F. */
10484 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10485 it.first_visible_x = 0;
10486 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10487 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10488
10489 while (!ITERATOR_AT_END_P (&it))
10490 {
10491 clear_glyph_row (temp_row);
10492 it.glyph_row = temp_row;
10493 display_tool_bar_line (&it, -1);
10494 }
10495 clear_glyph_row (temp_row);
10496
10497 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10498 if (n_rows)
10499 *n_rows = it.vpos > 0 ? it.vpos : -1;
10500
10501 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10502 }
10503
10504
10505 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10506 0, 1, 0,
10507 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10508 (Lisp_Object frame)
10509 {
10510 struct frame *f;
10511 struct window *w;
10512 int nlines = 0;
10513
10514 if (NILP (frame))
10515 frame = selected_frame;
10516 else
10517 CHECK_FRAME (frame);
10518 f = XFRAME (frame);
10519
10520 if (WINDOWP (f->tool_bar_window)
10521 || (w = XWINDOW (f->tool_bar_window),
10522 WINDOW_TOTAL_LINES (w) > 0))
10523 {
10524 update_tool_bar (f, 1);
10525 if (f->n_tool_bar_items)
10526 {
10527 build_desired_tool_bar_string (f);
10528 nlines = tool_bar_lines_needed (f, NULL);
10529 }
10530 }
10531
10532 return make_number (nlines);
10533 }
10534
10535
10536 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10537 height should be changed. */
10538
10539 static int
10540 redisplay_tool_bar (struct frame *f)
10541 {
10542 struct window *w;
10543 struct it it;
10544 struct glyph_row *row;
10545
10546 #if defined (USE_GTK) || defined (HAVE_NS)
10547 if (FRAME_EXTERNAL_TOOL_BAR (f))
10548 update_frame_tool_bar (f);
10549 return 0;
10550 #endif
10551
10552 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10553 do anything. This means you must start with tool-bar-lines
10554 non-zero to get the auto-sizing effect. Or in other words, you
10555 can turn off tool-bars by specifying tool-bar-lines zero. */
10556 if (!WINDOWP (f->tool_bar_window)
10557 || (w = XWINDOW (f->tool_bar_window),
10558 WINDOW_TOTAL_LINES (w) == 0))
10559 return 0;
10560
10561 /* Set up an iterator for the tool-bar window. */
10562 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10563 it.first_visible_x = 0;
10564 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10565 row = it.glyph_row;
10566
10567 /* Build a string that represents the contents of the tool-bar. */
10568 build_desired_tool_bar_string (f);
10569 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10570
10571 if (f->n_tool_bar_rows == 0)
10572 {
10573 int nlines;
10574
10575 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10576 nlines != WINDOW_TOTAL_LINES (w)))
10577 {
10578 Lisp_Object frame;
10579 int old_height = WINDOW_TOTAL_LINES (w);
10580
10581 XSETFRAME (frame, f);
10582 Fmodify_frame_parameters (frame,
10583 Fcons (Fcons (Qtool_bar_lines,
10584 make_number (nlines)),
10585 Qnil));
10586 if (WINDOW_TOTAL_LINES (w) != old_height)
10587 {
10588 clear_glyph_matrix (w->desired_matrix);
10589 fonts_changed_p = 1;
10590 return 1;
10591 }
10592 }
10593 }
10594
10595 /* Display as many lines as needed to display all tool-bar items. */
10596
10597 if (f->n_tool_bar_rows > 0)
10598 {
10599 int border, rows, height, extra;
10600
10601 if (INTEGERP (Vtool_bar_border))
10602 border = XINT (Vtool_bar_border);
10603 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10604 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10605 else if (EQ (Vtool_bar_border, Qborder_width))
10606 border = f->border_width;
10607 else
10608 border = 0;
10609 if (border < 0)
10610 border = 0;
10611
10612 rows = f->n_tool_bar_rows;
10613 height = max (1, (it.last_visible_y - border) / rows);
10614 extra = it.last_visible_y - border - height * rows;
10615
10616 while (it.current_y < it.last_visible_y)
10617 {
10618 int h = 0;
10619 if (extra > 0 && rows-- > 0)
10620 {
10621 h = (extra + rows - 1) / rows;
10622 extra -= h;
10623 }
10624 display_tool_bar_line (&it, height + h);
10625 }
10626 }
10627 else
10628 {
10629 while (it.current_y < it.last_visible_y)
10630 display_tool_bar_line (&it, 0);
10631 }
10632
10633 /* It doesn't make much sense to try scrolling in the tool-bar
10634 window, so don't do it. */
10635 w->desired_matrix->no_scrolling_p = 1;
10636 w->must_be_updated_p = 1;
10637
10638 if (!NILP (Vauto_resize_tool_bars))
10639 {
10640 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10641 int change_height_p = 0;
10642
10643 /* If we couldn't display everything, change the tool-bar's
10644 height if there is room for more. */
10645 if (IT_STRING_CHARPOS (it) < it.end_charpos
10646 && it.current_y < max_tool_bar_height)
10647 change_height_p = 1;
10648
10649 row = it.glyph_row - 1;
10650
10651 /* If there are blank lines at the end, except for a partially
10652 visible blank line at the end that is smaller than
10653 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10654 if (!row->displays_text_p
10655 && row->height >= FRAME_LINE_HEIGHT (f))
10656 change_height_p = 1;
10657
10658 /* If row displays tool-bar items, but is partially visible,
10659 change the tool-bar's height. */
10660 if (row->displays_text_p
10661 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10662 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10663 change_height_p = 1;
10664
10665 /* Resize windows as needed by changing the `tool-bar-lines'
10666 frame parameter. */
10667 if (change_height_p)
10668 {
10669 Lisp_Object frame;
10670 int old_height = WINDOW_TOTAL_LINES (w);
10671 int nrows;
10672 int nlines = tool_bar_lines_needed (f, &nrows);
10673
10674 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10675 && !f->minimize_tool_bar_window_p)
10676 ? (nlines > old_height)
10677 : (nlines != old_height));
10678 f->minimize_tool_bar_window_p = 0;
10679
10680 if (change_height_p)
10681 {
10682 XSETFRAME (frame, f);
10683 Fmodify_frame_parameters (frame,
10684 Fcons (Fcons (Qtool_bar_lines,
10685 make_number (nlines)),
10686 Qnil));
10687 if (WINDOW_TOTAL_LINES (w) != old_height)
10688 {
10689 clear_glyph_matrix (w->desired_matrix);
10690 f->n_tool_bar_rows = nrows;
10691 fonts_changed_p = 1;
10692 return 1;
10693 }
10694 }
10695 }
10696 }
10697
10698 f->minimize_tool_bar_window_p = 0;
10699 return 0;
10700 }
10701
10702
10703 /* Get information about the tool-bar item which is displayed in GLYPH
10704 on frame F. Return in *PROP_IDX the index where tool-bar item
10705 properties start in F->tool_bar_items. Value is zero if
10706 GLYPH doesn't display a tool-bar item. */
10707
10708 static int
10709 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10710 {
10711 Lisp_Object prop;
10712 int success_p;
10713 int charpos;
10714
10715 /* This function can be called asynchronously, which means we must
10716 exclude any possibility that Fget_text_property signals an
10717 error. */
10718 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10719 charpos = max (0, charpos);
10720
10721 /* Get the text property `menu-item' at pos. The value of that
10722 property is the start index of this item's properties in
10723 F->tool_bar_items. */
10724 prop = Fget_text_property (make_number (charpos),
10725 Qmenu_item, f->current_tool_bar_string);
10726 if (INTEGERP (prop))
10727 {
10728 *prop_idx = XINT (prop);
10729 success_p = 1;
10730 }
10731 else
10732 success_p = 0;
10733
10734 return success_p;
10735 }
10736
10737 \f
10738 /* Get information about the tool-bar item at position X/Y on frame F.
10739 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10740 the current matrix of the tool-bar window of F, or NULL if not
10741 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10742 item in F->tool_bar_items. Value is
10743
10744 -1 if X/Y is not on a tool-bar item
10745 0 if X/Y is on the same item that was highlighted before.
10746 1 otherwise. */
10747
10748 static int
10749 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
10750 int *hpos, int *vpos, int *prop_idx)
10751 {
10752 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10753 struct window *w = XWINDOW (f->tool_bar_window);
10754 int area;
10755
10756 /* Find the glyph under X/Y. */
10757 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10758 if (*glyph == NULL)
10759 return -1;
10760
10761 /* Get the start of this tool-bar item's properties in
10762 f->tool_bar_items. */
10763 if (!tool_bar_item_info (f, *glyph, prop_idx))
10764 return -1;
10765
10766 /* Is mouse on the highlighted item? */
10767 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10768 && *vpos >= dpyinfo->mouse_face_beg_row
10769 && *vpos <= dpyinfo->mouse_face_end_row
10770 && (*vpos > dpyinfo->mouse_face_beg_row
10771 || *hpos >= dpyinfo->mouse_face_beg_col)
10772 && (*vpos < dpyinfo->mouse_face_end_row
10773 || *hpos < dpyinfo->mouse_face_end_col
10774 || dpyinfo->mouse_face_past_end))
10775 return 0;
10776
10777 return 1;
10778 }
10779
10780
10781 /* EXPORT:
10782 Handle mouse button event on the tool-bar of frame F, at
10783 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10784 0 for button release. MODIFIERS is event modifiers for button
10785 release. */
10786
10787 void
10788 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
10789 unsigned int modifiers)
10790 {
10791 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10792 struct window *w = XWINDOW (f->tool_bar_window);
10793 int hpos, vpos, prop_idx;
10794 struct glyph *glyph;
10795 Lisp_Object enabled_p;
10796
10797 /* If not on the highlighted tool-bar item, return. */
10798 frame_to_window_pixel_xy (w, &x, &y);
10799 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10800 return;
10801
10802 /* If item is disabled, do nothing. */
10803 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10804 if (NILP (enabled_p))
10805 return;
10806
10807 if (down_p)
10808 {
10809 /* Show item in pressed state. */
10810 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10811 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10812 last_tool_bar_item = prop_idx;
10813 }
10814 else
10815 {
10816 Lisp_Object key, frame;
10817 struct input_event event;
10818 EVENT_INIT (event);
10819
10820 /* Show item in released state. */
10821 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10822 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10823
10824 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10825
10826 XSETFRAME (frame, f);
10827 event.kind = TOOL_BAR_EVENT;
10828 event.frame_or_window = frame;
10829 event.arg = frame;
10830 kbd_buffer_store_event (&event);
10831
10832 event.kind = TOOL_BAR_EVENT;
10833 event.frame_or_window = frame;
10834 event.arg = key;
10835 event.modifiers = modifiers;
10836 kbd_buffer_store_event (&event);
10837 last_tool_bar_item = -1;
10838 }
10839 }
10840
10841
10842 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10843 tool-bar window-relative coordinates X/Y. Called from
10844 note_mouse_highlight. */
10845
10846 static void
10847 note_tool_bar_highlight (struct frame *f, int x, int y)
10848 {
10849 Lisp_Object window = f->tool_bar_window;
10850 struct window *w = XWINDOW (window);
10851 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10852 int hpos, vpos;
10853 struct glyph *glyph;
10854 struct glyph_row *row;
10855 int i;
10856 Lisp_Object enabled_p;
10857 int prop_idx;
10858 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10859 int mouse_down_p, rc;
10860
10861 /* Function note_mouse_highlight is called with negative x(y
10862 values when mouse moves outside of the frame. */
10863 if (x <= 0 || y <= 0)
10864 {
10865 clear_mouse_face (dpyinfo);
10866 return;
10867 }
10868
10869 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10870 if (rc < 0)
10871 {
10872 /* Not on tool-bar item. */
10873 clear_mouse_face (dpyinfo);
10874 return;
10875 }
10876 else if (rc == 0)
10877 /* On same tool-bar item as before. */
10878 goto set_help_echo;
10879
10880 clear_mouse_face (dpyinfo);
10881
10882 /* Mouse is down, but on different tool-bar item? */
10883 mouse_down_p = (dpyinfo->grabbed
10884 && f == last_mouse_frame
10885 && FRAME_LIVE_P (f));
10886 if (mouse_down_p
10887 && last_tool_bar_item != prop_idx)
10888 return;
10889
10890 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10891 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10892
10893 /* If tool-bar item is not enabled, don't highlight it. */
10894 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10895 if (!NILP (enabled_p))
10896 {
10897 /* Compute the x-position of the glyph. In front and past the
10898 image is a space. We include this in the highlighted area. */
10899 row = MATRIX_ROW (w->current_matrix, vpos);
10900 for (i = x = 0; i < hpos; ++i)
10901 x += row->glyphs[TEXT_AREA][i].pixel_width;
10902
10903 /* Record this as the current active region. */
10904 dpyinfo->mouse_face_beg_col = hpos;
10905 dpyinfo->mouse_face_beg_row = vpos;
10906 dpyinfo->mouse_face_beg_x = x;
10907 dpyinfo->mouse_face_beg_y = row->y;
10908 dpyinfo->mouse_face_past_end = 0;
10909
10910 dpyinfo->mouse_face_end_col = hpos + 1;
10911 dpyinfo->mouse_face_end_row = vpos;
10912 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10913 dpyinfo->mouse_face_end_y = row->y;
10914 dpyinfo->mouse_face_window = window;
10915 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10916
10917 /* Display it as active. */
10918 show_mouse_face (dpyinfo, draw);
10919 dpyinfo->mouse_face_image_state = draw;
10920 }
10921
10922 set_help_echo:
10923
10924 /* Set help_echo_string to a help string to display for this tool-bar item.
10925 XTread_socket does the rest. */
10926 help_echo_object = help_echo_window = Qnil;
10927 help_echo_pos = -1;
10928 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10929 if (NILP (help_echo_string))
10930 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10931 }
10932
10933 #endif /* HAVE_WINDOW_SYSTEM */
10934
10935
10936 \f
10937 /************************************************************************
10938 Horizontal scrolling
10939 ************************************************************************/
10940
10941 static int hscroll_window_tree (Lisp_Object);
10942 static int hscroll_windows (Lisp_Object);
10943
10944 /* For all leaf windows in the window tree rooted at WINDOW, set their
10945 hscroll value so that PT is (i) visible in the window, and (ii) so
10946 that it is not within a certain margin at the window's left and
10947 right border. Value is non-zero if any window's hscroll has been
10948 changed. */
10949
10950 static int
10951 hscroll_window_tree (Lisp_Object window)
10952 {
10953 int hscrolled_p = 0;
10954 int hscroll_relative_p = FLOATP (Vhscroll_step);
10955 int hscroll_step_abs = 0;
10956 double hscroll_step_rel = 0;
10957
10958 if (hscroll_relative_p)
10959 {
10960 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10961 if (hscroll_step_rel < 0)
10962 {
10963 hscroll_relative_p = 0;
10964 hscroll_step_abs = 0;
10965 }
10966 }
10967 else if (INTEGERP (Vhscroll_step))
10968 {
10969 hscroll_step_abs = XINT (Vhscroll_step);
10970 if (hscroll_step_abs < 0)
10971 hscroll_step_abs = 0;
10972 }
10973 else
10974 hscroll_step_abs = 0;
10975
10976 while (WINDOWP (window))
10977 {
10978 struct window *w = XWINDOW (window);
10979
10980 if (WINDOWP (w->hchild))
10981 hscrolled_p |= hscroll_window_tree (w->hchild);
10982 else if (WINDOWP (w->vchild))
10983 hscrolled_p |= hscroll_window_tree (w->vchild);
10984 else if (w->cursor.vpos >= 0)
10985 {
10986 int h_margin;
10987 int text_area_width;
10988 struct glyph_row *current_cursor_row
10989 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10990 struct glyph_row *desired_cursor_row
10991 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10992 struct glyph_row *cursor_row
10993 = (desired_cursor_row->enabled_p
10994 ? desired_cursor_row
10995 : current_cursor_row);
10996
10997 text_area_width = window_box_width (w, TEXT_AREA);
10998
10999 /* Scroll when cursor is inside this scroll margin. */
11000 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11001
11002 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11003 && ((XFASTINT (w->hscroll)
11004 && w->cursor.x <= h_margin)
11005 || (cursor_row->enabled_p
11006 && cursor_row->truncated_on_right_p
11007 && (w->cursor.x >= text_area_width - h_margin))))
11008 {
11009 struct it it;
11010 int hscroll;
11011 struct buffer *saved_current_buffer;
11012 int pt;
11013 int wanted_x;
11014
11015 /* Find point in a display of infinite width. */
11016 saved_current_buffer = current_buffer;
11017 current_buffer = XBUFFER (w->buffer);
11018
11019 if (w == XWINDOW (selected_window))
11020 pt = BUF_PT (current_buffer);
11021 else
11022 {
11023 pt = marker_position (w->pointm);
11024 pt = max (BEGV, pt);
11025 pt = min (ZV, pt);
11026 }
11027
11028 /* Move iterator to pt starting at cursor_row->start in
11029 a line with infinite width. */
11030 init_to_row_start (&it, w, cursor_row);
11031 it.last_visible_x = INFINITY;
11032 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11033 current_buffer = saved_current_buffer;
11034
11035 /* Position cursor in window. */
11036 if (!hscroll_relative_p && hscroll_step_abs == 0)
11037 hscroll = max (0, (it.current_x
11038 - (ITERATOR_AT_END_OF_LINE_P (&it)
11039 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11040 : (text_area_width / 2))))
11041 / FRAME_COLUMN_WIDTH (it.f);
11042 else if (w->cursor.x >= text_area_width - h_margin)
11043 {
11044 if (hscroll_relative_p)
11045 wanted_x = text_area_width * (1 - hscroll_step_rel)
11046 - h_margin;
11047 else
11048 wanted_x = text_area_width
11049 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11050 - h_margin;
11051 hscroll
11052 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11053 }
11054 else
11055 {
11056 if (hscroll_relative_p)
11057 wanted_x = text_area_width * hscroll_step_rel
11058 + h_margin;
11059 else
11060 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11061 + h_margin;
11062 hscroll
11063 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11064 }
11065 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11066
11067 /* Don't call Fset_window_hscroll if value hasn't
11068 changed because it will prevent redisplay
11069 optimizations. */
11070 if (XFASTINT (w->hscroll) != hscroll)
11071 {
11072 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11073 w->hscroll = make_number (hscroll);
11074 hscrolled_p = 1;
11075 }
11076 }
11077 }
11078
11079 window = w->next;
11080 }
11081
11082 /* Value is non-zero if hscroll of any leaf window has been changed. */
11083 return hscrolled_p;
11084 }
11085
11086
11087 /* Set hscroll so that cursor is visible and not inside horizontal
11088 scroll margins for all windows in the tree rooted at WINDOW. See
11089 also hscroll_window_tree above. Value is non-zero if any window's
11090 hscroll has been changed. If it has, desired matrices on the frame
11091 of WINDOW are cleared. */
11092
11093 static int
11094 hscroll_windows (Lisp_Object window)
11095 {
11096 int hscrolled_p = hscroll_window_tree (window);
11097 if (hscrolled_p)
11098 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11099 return hscrolled_p;
11100 }
11101
11102
11103 \f
11104 /************************************************************************
11105 Redisplay
11106 ************************************************************************/
11107
11108 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11109 to a non-zero value. This is sometimes handy to have in a debugger
11110 session. */
11111
11112 #if GLYPH_DEBUG
11113
11114 /* First and last unchanged row for try_window_id. */
11115
11116 int debug_first_unchanged_at_end_vpos;
11117 int debug_last_unchanged_at_beg_vpos;
11118
11119 /* Delta vpos and y. */
11120
11121 int debug_dvpos, debug_dy;
11122
11123 /* Delta in characters and bytes for try_window_id. */
11124
11125 int debug_delta, debug_delta_bytes;
11126
11127 /* Values of window_end_pos and window_end_vpos at the end of
11128 try_window_id. */
11129
11130 EMACS_INT debug_end_pos, debug_end_vpos;
11131
11132 /* Append a string to W->desired_matrix->method. FMT is a printf
11133 format string. A1...A9 are a supplement for a variable-length
11134 argument list. If trace_redisplay_p is non-zero also printf the
11135 resulting string to stderr. */
11136
11137 static void
11138 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11139 struct window *w;
11140 char *fmt;
11141 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11142 {
11143 char buffer[512];
11144 char *method = w->desired_matrix->method;
11145 int len = strlen (method);
11146 int size = sizeof w->desired_matrix->method;
11147 int remaining = size - len - 1;
11148
11149 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11150 if (len && remaining)
11151 {
11152 method[len] = '|';
11153 --remaining, ++len;
11154 }
11155
11156 strncpy (method + len, buffer, remaining);
11157
11158 if (trace_redisplay_p)
11159 fprintf (stderr, "%p (%s): %s\n",
11160 w,
11161 ((BUFFERP (w->buffer)
11162 && STRINGP (XBUFFER (w->buffer)->name))
11163 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11164 : "no buffer"),
11165 buffer);
11166 }
11167
11168 #endif /* GLYPH_DEBUG */
11169
11170
11171 /* Value is non-zero if all changes in window W, which displays
11172 current_buffer, are in the text between START and END. START is a
11173 buffer position, END is given as a distance from Z. Used in
11174 redisplay_internal for display optimization. */
11175
11176 static INLINE int
11177 text_outside_line_unchanged_p (struct window *w, int start, int end)
11178 {
11179 int unchanged_p = 1;
11180
11181 /* If text or overlays have changed, see where. */
11182 if (XFASTINT (w->last_modified) < MODIFF
11183 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11184 {
11185 /* Gap in the line? */
11186 if (GPT < start || Z - GPT < end)
11187 unchanged_p = 0;
11188
11189 /* Changes start in front of the line, or end after it? */
11190 if (unchanged_p
11191 && (BEG_UNCHANGED < start - 1
11192 || END_UNCHANGED < end))
11193 unchanged_p = 0;
11194
11195 /* If selective display, can't optimize if changes start at the
11196 beginning of the line. */
11197 if (unchanged_p
11198 && INTEGERP (current_buffer->selective_display)
11199 && XINT (current_buffer->selective_display) > 0
11200 && (BEG_UNCHANGED < start || GPT <= start))
11201 unchanged_p = 0;
11202
11203 /* If there are overlays at the start or end of the line, these
11204 may have overlay strings with newlines in them. A change at
11205 START, for instance, may actually concern the display of such
11206 overlay strings as well, and they are displayed on different
11207 lines. So, quickly rule out this case. (For the future, it
11208 might be desirable to implement something more telling than
11209 just BEG/END_UNCHANGED.) */
11210 if (unchanged_p)
11211 {
11212 if (BEG + BEG_UNCHANGED == start
11213 && overlay_touches_p (start))
11214 unchanged_p = 0;
11215 if (END_UNCHANGED == end
11216 && overlay_touches_p (Z - end))
11217 unchanged_p = 0;
11218 }
11219
11220 /* Under bidi reordering, adding or deleting a character in the
11221 beginning of a paragraph, before the first strong directional
11222 character, can change the base direction of the paragraph (unless
11223 the buffer specifies a fixed paragraph direction), which will
11224 require to redisplay the whole paragraph. It might be worthwhile
11225 to find the paragraph limits and widen the range of redisplayed
11226 lines to that, but for now just give up this optimization. */
11227 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11228 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11229 unchanged_p = 0;
11230 }
11231
11232 return unchanged_p;
11233 }
11234
11235
11236 /* Do a frame update, taking possible shortcuts into account. This is
11237 the main external entry point for redisplay.
11238
11239 If the last redisplay displayed an echo area message and that message
11240 is no longer requested, we clear the echo area or bring back the
11241 mini-buffer if that is in use. */
11242
11243 void
11244 redisplay (void)
11245 {
11246 redisplay_internal (0);
11247 }
11248
11249
11250 static Lisp_Object
11251 overlay_arrow_string_or_property (Lisp_Object var)
11252 {
11253 Lisp_Object val;
11254
11255 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11256 return val;
11257
11258 return Voverlay_arrow_string;
11259 }
11260
11261 /* Return 1 if there are any overlay-arrows in current_buffer. */
11262 static int
11263 overlay_arrow_in_current_buffer_p (void)
11264 {
11265 Lisp_Object vlist;
11266
11267 for (vlist = Voverlay_arrow_variable_list;
11268 CONSP (vlist);
11269 vlist = XCDR (vlist))
11270 {
11271 Lisp_Object var = XCAR (vlist);
11272 Lisp_Object val;
11273
11274 if (!SYMBOLP (var))
11275 continue;
11276 val = find_symbol_value (var);
11277 if (MARKERP (val)
11278 && current_buffer == XMARKER (val)->buffer)
11279 return 1;
11280 }
11281 return 0;
11282 }
11283
11284
11285 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11286 has changed. */
11287
11288 static int
11289 overlay_arrows_changed_p (void)
11290 {
11291 Lisp_Object vlist;
11292
11293 for (vlist = Voverlay_arrow_variable_list;
11294 CONSP (vlist);
11295 vlist = XCDR (vlist))
11296 {
11297 Lisp_Object var = XCAR (vlist);
11298 Lisp_Object val, pstr;
11299
11300 if (!SYMBOLP (var))
11301 continue;
11302 val = find_symbol_value (var);
11303 if (!MARKERP (val))
11304 continue;
11305 if (! EQ (COERCE_MARKER (val),
11306 Fget (var, Qlast_arrow_position))
11307 || ! (pstr = overlay_arrow_string_or_property (var),
11308 EQ (pstr, Fget (var, Qlast_arrow_string))))
11309 return 1;
11310 }
11311 return 0;
11312 }
11313
11314 /* Mark overlay arrows to be updated on next redisplay. */
11315
11316 static void
11317 update_overlay_arrows (int up_to_date)
11318 {
11319 Lisp_Object vlist;
11320
11321 for (vlist = Voverlay_arrow_variable_list;
11322 CONSP (vlist);
11323 vlist = XCDR (vlist))
11324 {
11325 Lisp_Object var = XCAR (vlist);
11326
11327 if (!SYMBOLP (var))
11328 continue;
11329
11330 if (up_to_date > 0)
11331 {
11332 Lisp_Object val = find_symbol_value (var);
11333 Fput (var, Qlast_arrow_position,
11334 COERCE_MARKER (val));
11335 Fput (var, Qlast_arrow_string,
11336 overlay_arrow_string_or_property (var));
11337 }
11338 else if (up_to_date < 0
11339 || !NILP (Fget (var, Qlast_arrow_position)))
11340 {
11341 Fput (var, Qlast_arrow_position, Qt);
11342 Fput (var, Qlast_arrow_string, Qt);
11343 }
11344 }
11345 }
11346
11347
11348 /* Return overlay arrow string to display at row.
11349 Return integer (bitmap number) for arrow bitmap in left fringe.
11350 Return nil if no overlay arrow. */
11351
11352 static Lisp_Object
11353 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11354 {
11355 Lisp_Object vlist;
11356
11357 for (vlist = Voverlay_arrow_variable_list;
11358 CONSP (vlist);
11359 vlist = XCDR (vlist))
11360 {
11361 Lisp_Object var = XCAR (vlist);
11362 Lisp_Object val;
11363
11364 if (!SYMBOLP (var))
11365 continue;
11366
11367 val = find_symbol_value (var);
11368
11369 if (MARKERP (val)
11370 && current_buffer == XMARKER (val)->buffer
11371 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11372 {
11373 if (FRAME_WINDOW_P (it->f)
11374 /* FIXME: if ROW->reversed_p is set, this should test
11375 the right fringe, not the left one. */
11376 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11377 {
11378 #ifdef HAVE_WINDOW_SYSTEM
11379 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11380 {
11381 int fringe_bitmap;
11382 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11383 return make_number (fringe_bitmap);
11384 }
11385 #endif
11386 return make_number (-1); /* Use default arrow bitmap */
11387 }
11388 return overlay_arrow_string_or_property (var);
11389 }
11390 }
11391
11392 return Qnil;
11393 }
11394
11395 /* Return 1 if point moved out of or into a composition. Otherwise
11396 return 0. PREV_BUF and PREV_PT are the last point buffer and
11397 position. BUF and PT are the current point buffer and position. */
11398
11399 int
11400 check_point_in_composition (struct buffer *prev_buf, int prev_pt,
11401 struct buffer *buf, int pt)
11402 {
11403 EMACS_INT start, end;
11404 Lisp_Object prop;
11405 Lisp_Object buffer;
11406
11407 XSETBUFFER (buffer, buf);
11408 /* Check a composition at the last point if point moved within the
11409 same buffer. */
11410 if (prev_buf == buf)
11411 {
11412 if (prev_pt == pt)
11413 /* Point didn't move. */
11414 return 0;
11415
11416 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11417 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11418 && COMPOSITION_VALID_P (start, end, prop)
11419 && start < prev_pt && end > prev_pt)
11420 /* The last point was within the composition. Return 1 iff
11421 point moved out of the composition. */
11422 return (pt <= start || pt >= end);
11423 }
11424
11425 /* Check a composition at the current point. */
11426 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11427 && find_composition (pt, -1, &start, &end, &prop, buffer)
11428 && COMPOSITION_VALID_P (start, end, prop)
11429 && start < pt && end > pt);
11430 }
11431
11432
11433 /* Reconsider the setting of B->clip_changed which is displayed
11434 in window W. */
11435
11436 static INLINE void
11437 reconsider_clip_changes (struct window *w, struct buffer *b)
11438 {
11439 if (b->clip_changed
11440 && !NILP (w->window_end_valid)
11441 && w->current_matrix->buffer == b
11442 && w->current_matrix->zv == BUF_ZV (b)
11443 && w->current_matrix->begv == BUF_BEGV (b))
11444 b->clip_changed = 0;
11445
11446 /* If display wasn't paused, and W is not a tool bar window, see if
11447 point has been moved into or out of a composition. In that case,
11448 we set b->clip_changed to 1 to force updating the screen. If
11449 b->clip_changed has already been set to 1, we can skip this
11450 check. */
11451 if (!b->clip_changed
11452 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11453 {
11454 int pt;
11455
11456 if (w == XWINDOW (selected_window))
11457 pt = BUF_PT (current_buffer);
11458 else
11459 pt = marker_position (w->pointm);
11460
11461 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11462 || pt != XINT (w->last_point))
11463 && check_point_in_composition (w->current_matrix->buffer,
11464 XINT (w->last_point),
11465 XBUFFER (w->buffer), pt))
11466 b->clip_changed = 1;
11467 }
11468 }
11469 \f
11470
11471 /* Select FRAME to forward the values of frame-local variables into C
11472 variables so that the redisplay routines can access those values
11473 directly. */
11474
11475 static void
11476 select_frame_for_redisplay (Lisp_Object frame)
11477 {
11478 Lisp_Object tail, tem;
11479 Lisp_Object old = selected_frame;
11480 struct Lisp_Symbol *sym;
11481
11482 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11483
11484 selected_frame = frame;
11485
11486 do {
11487 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11488 if (CONSP (XCAR (tail))
11489 && (tem = XCAR (XCAR (tail)),
11490 SYMBOLP (tem))
11491 && (sym = indirect_variable (XSYMBOL (tem)),
11492 sym->redirect == SYMBOL_LOCALIZED)
11493 && sym->val.blv->frame_local)
11494 /* Use find_symbol_value rather than Fsymbol_value
11495 to avoid an error if it is void. */
11496 find_symbol_value (tem);
11497 } while (!EQ (frame, old) && (frame = old, 1));
11498 }
11499
11500
11501 #define STOP_POLLING \
11502 do { if (! polling_stopped_here) stop_polling (); \
11503 polling_stopped_here = 1; } while (0)
11504
11505 #define RESUME_POLLING \
11506 do { if (polling_stopped_here) start_polling (); \
11507 polling_stopped_here = 0; } while (0)
11508
11509
11510 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11511 response to any user action; therefore, we should preserve the echo
11512 area. (Actually, our caller does that job.) Perhaps in the future
11513 avoid recentering windows if it is not necessary; currently that
11514 causes some problems. */
11515
11516 static void
11517 redisplay_internal (int preserve_echo_area)
11518 {
11519 struct window *w = XWINDOW (selected_window);
11520 struct frame *f;
11521 int pause;
11522 int must_finish = 0;
11523 struct text_pos tlbufpos, tlendpos;
11524 int number_of_visible_frames;
11525 int count, count1;
11526 struct frame *sf;
11527 int polling_stopped_here = 0;
11528 Lisp_Object old_frame = selected_frame;
11529
11530 /* Non-zero means redisplay has to consider all windows on all
11531 frames. Zero means, only selected_window is considered. */
11532 int consider_all_windows_p;
11533
11534 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11535
11536 /* No redisplay if running in batch mode or frame is not yet fully
11537 initialized, or redisplay is explicitly turned off by setting
11538 Vinhibit_redisplay. */
11539 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11540 || !NILP (Vinhibit_redisplay))
11541 return;
11542
11543 /* Don't examine these until after testing Vinhibit_redisplay.
11544 When Emacs is shutting down, perhaps because its connection to
11545 X has dropped, we should not look at them at all. */
11546 f = XFRAME (w->frame);
11547 sf = SELECTED_FRAME ();
11548
11549 if (!f->glyphs_initialized_p)
11550 return;
11551
11552 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11553 if (popup_activated ())
11554 return;
11555 #endif
11556
11557 /* I don't think this happens but let's be paranoid. */
11558 if (redisplaying_p)
11559 return;
11560
11561 /* Record a function that resets redisplaying_p to its old value
11562 when we leave this function. */
11563 count = SPECPDL_INDEX ();
11564 record_unwind_protect (unwind_redisplay,
11565 Fcons (make_number (redisplaying_p), selected_frame));
11566 ++redisplaying_p;
11567 specbind (Qinhibit_free_realized_faces, Qnil);
11568
11569 {
11570 Lisp_Object tail, frame;
11571
11572 FOR_EACH_FRAME (tail, frame)
11573 {
11574 struct frame *f = XFRAME (frame);
11575 f->already_hscrolled_p = 0;
11576 }
11577 }
11578
11579 retry:
11580 if (!EQ (old_frame, selected_frame)
11581 && FRAME_LIVE_P (XFRAME (old_frame)))
11582 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11583 selected_frame and selected_window to be temporarily out-of-sync so
11584 when we come back here via `goto retry', we need to resync because we
11585 may need to run Elisp code (via prepare_menu_bars). */
11586 select_frame_for_redisplay (old_frame);
11587
11588 pause = 0;
11589 reconsider_clip_changes (w, current_buffer);
11590 last_escape_glyph_frame = NULL;
11591 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11592
11593 /* If new fonts have been loaded that make a glyph matrix adjustment
11594 necessary, do it. */
11595 if (fonts_changed_p)
11596 {
11597 adjust_glyphs (NULL);
11598 ++windows_or_buffers_changed;
11599 fonts_changed_p = 0;
11600 }
11601
11602 /* If face_change_count is non-zero, init_iterator will free all
11603 realized faces, which includes the faces referenced from current
11604 matrices. So, we can't reuse current matrices in this case. */
11605 if (face_change_count)
11606 ++windows_or_buffers_changed;
11607
11608 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11609 && FRAME_TTY (sf)->previous_frame != sf)
11610 {
11611 /* Since frames on a single ASCII terminal share the same
11612 display area, displaying a different frame means redisplay
11613 the whole thing. */
11614 windows_or_buffers_changed++;
11615 SET_FRAME_GARBAGED (sf);
11616 #ifndef DOS_NT
11617 set_tty_color_mode (FRAME_TTY (sf), sf);
11618 #endif
11619 FRAME_TTY (sf)->previous_frame = sf;
11620 }
11621
11622 /* Set the visible flags for all frames. Do this before checking
11623 for resized or garbaged frames; they want to know if their frames
11624 are visible. See the comment in frame.h for
11625 FRAME_SAMPLE_VISIBILITY. */
11626 {
11627 Lisp_Object tail, frame;
11628
11629 number_of_visible_frames = 0;
11630
11631 FOR_EACH_FRAME (tail, frame)
11632 {
11633 struct frame *f = XFRAME (frame);
11634
11635 FRAME_SAMPLE_VISIBILITY (f);
11636 if (FRAME_VISIBLE_P (f))
11637 ++number_of_visible_frames;
11638 clear_desired_matrices (f);
11639 }
11640 }
11641
11642 /* Notice any pending interrupt request to change frame size. */
11643 do_pending_window_change (1);
11644
11645 /* Clear frames marked as garbaged. */
11646 if (frame_garbaged)
11647 clear_garbaged_frames ();
11648
11649 /* Build menubar and tool-bar items. */
11650 if (NILP (Vmemory_full))
11651 prepare_menu_bars ();
11652
11653 if (windows_or_buffers_changed)
11654 update_mode_lines++;
11655
11656 /* Detect case that we need to write or remove a star in the mode line. */
11657 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11658 {
11659 w->update_mode_line = Qt;
11660 if (buffer_shared > 1)
11661 update_mode_lines++;
11662 }
11663
11664 /* Avoid invocation of point motion hooks by `current_column' below. */
11665 count1 = SPECPDL_INDEX ();
11666 specbind (Qinhibit_point_motion_hooks, Qt);
11667
11668 /* If %c is in the mode line, update it if needed. */
11669 if (!NILP (w->column_number_displayed)
11670 /* This alternative quickly identifies a common case
11671 where no change is needed. */
11672 && !(PT == XFASTINT (w->last_point)
11673 && XFASTINT (w->last_modified) >= MODIFF
11674 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11675 && (XFASTINT (w->column_number_displayed)
11676 != (int) current_column ())) /* iftc */
11677 w->update_mode_line = Qt;
11678
11679 unbind_to (count1, Qnil);
11680
11681 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11682
11683 /* The variable buffer_shared is set in redisplay_window and
11684 indicates that we redisplay a buffer in different windows. See
11685 there. */
11686 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11687 || cursor_type_changed);
11688
11689 /* If specs for an arrow have changed, do thorough redisplay
11690 to ensure we remove any arrow that should no longer exist. */
11691 if (overlay_arrows_changed_p ())
11692 consider_all_windows_p = windows_or_buffers_changed = 1;
11693
11694 /* Normally the message* functions will have already displayed and
11695 updated the echo area, but the frame may have been trashed, or
11696 the update may have been preempted, so display the echo area
11697 again here. Checking message_cleared_p captures the case that
11698 the echo area should be cleared. */
11699 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11700 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11701 || (message_cleared_p
11702 && minibuf_level == 0
11703 /* If the mini-window is currently selected, this means the
11704 echo-area doesn't show through. */
11705 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11706 {
11707 int window_height_changed_p = echo_area_display (0);
11708 must_finish = 1;
11709
11710 /* If we don't display the current message, don't clear the
11711 message_cleared_p flag, because, if we did, we wouldn't clear
11712 the echo area in the next redisplay which doesn't preserve
11713 the echo area. */
11714 if (!display_last_displayed_message_p)
11715 message_cleared_p = 0;
11716
11717 if (fonts_changed_p)
11718 goto retry;
11719 else if (window_height_changed_p)
11720 {
11721 consider_all_windows_p = 1;
11722 ++update_mode_lines;
11723 ++windows_or_buffers_changed;
11724
11725 /* If window configuration was changed, frames may have been
11726 marked garbaged. Clear them or we will experience
11727 surprises wrt scrolling. */
11728 if (frame_garbaged)
11729 clear_garbaged_frames ();
11730 }
11731 }
11732 else if (EQ (selected_window, minibuf_window)
11733 && (current_buffer->clip_changed
11734 || XFASTINT (w->last_modified) < MODIFF
11735 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11736 && resize_mini_window (w, 0))
11737 {
11738 /* Resized active mini-window to fit the size of what it is
11739 showing if its contents might have changed. */
11740 must_finish = 1;
11741 /* FIXME: this causes all frames to be updated, which seems unnecessary
11742 since only the current frame needs to be considered. This function needs
11743 to be rewritten with two variables, consider_all_windows and
11744 consider_all_frames. */
11745 consider_all_windows_p = 1;
11746 ++windows_or_buffers_changed;
11747 ++update_mode_lines;
11748
11749 /* If window configuration was changed, frames may have been
11750 marked garbaged. Clear them or we will experience
11751 surprises wrt scrolling. */
11752 if (frame_garbaged)
11753 clear_garbaged_frames ();
11754 }
11755
11756
11757 /* If showing the region, and mark has changed, we must redisplay
11758 the whole window. The assignment to this_line_start_pos prevents
11759 the optimization directly below this if-statement. */
11760 if (((!NILP (Vtransient_mark_mode)
11761 && !NILP (XBUFFER (w->buffer)->mark_active))
11762 != !NILP (w->region_showing))
11763 || (!NILP (w->region_showing)
11764 && !EQ (w->region_showing,
11765 Fmarker_position (XBUFFER (w->buffer)->mark))))
11766 CHARPOS (this_line_start_pos) = 0;
11767
11768 /* Optimize the case that only the line containing the cursor in the
11769 selected window has changed. Variables starting with this_ are
11770 set in display_line and record information about the line
11771 containing the cursor. */
11772 tlbufpos = this_line_start_pos;
11773 tlendpos = this_line_end_pos;
11774 if (!consider_all_windows_p
11775 && CHARPOS (tlbufpos) > 0
11776 && NILP (w->update_mode_line)
11777 && !current_buffer->clip_changed
11778 && !current_buffer->prevent_redisplay_optimizations_p
11779 && FRAME_VISIBLE_P (XFRAME (w->frame))
11780 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11781 /* Make sure recorded data applies to current buffer, etc. */
11782 && this_line_buffer == current_buffer
11783 && current_buffer == XBUFFER (w->buffer)
11784 && NILP (w->force_start)
11785 && NILP (w->optional_new_start)
11786 /* Point must be on the line that we have info recorded about. */
11787 && PT >= CHARPOS (tlbufpos)
11788 && PT <= Z - CHARPOS (tlendpos)
11789 /* All text outside that line, including its final newline,
11790 must be unchanged. */
11791 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11792 CHARPOS (tlendpos)))
11793 {
11794 if (CHARPOS (tlbufpos) > BEGV
11795 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11796 && (CHARPOS (tlbufpos) == ZV
11797 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11798 /* Former continuation line has disappeared by becoming empty. */
11799 goto cancel;
11800 else if (XFASTINT (w->last_modified) < MODIFF
11801 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11802 || MINI_WINDOW_P (w))
11803 {
11804 /* We have to handle the case of continuation around a
11805 wide-column character (see the comment in indent.c around
11806 line 1340).
11807
11808 For instance, in the following case:
11809
11810 -------- Insert --------
11811 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11812 J_I_ ==> J_I_ `^^' are cursors.
11813 ^^ ^^
11814 -------- --------
11815
11816 As we have to redraw the line above, we cannot use this
11817 optimization. */
11818
11819 struct it it;
11820 int line_height_before = this_line_pixel_height;
11821
11822 /* Note that start_display will handle the case that the
11823 line starting at tlbufpos is a continuation line. */
11824 start_display (&it, w, tlbufpos);
11825
11826 /* Implementation note: It this still necessary? */
11827 if (it.current_x != this_line_start_x)
11828 goto cancel;
11829
11830 TRACE ((stderr, "trying display optimization 1\n"));
11831 w->cursor.vpos = -1;
11832 overlay_arrow_seen = 0;
11833 it.vpos = this_line_vpos;
11834 it.current_y = this_line_y;
11835 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11836 display_line (&it);
11837
11838 /* If line contains point, is not continued,
11839 and ends at same distance from eob as before, we win. */
11840 if (w->cursor.vpos >= 0
11841 /* Line is not continued, otherwise this_line_start_pos
11842 would have been set to 0 in display_line. */
11843 && CHARPOS (this_line_start_pos)
11844 /* Line ends as before. */
11845 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11846 /* Line has same height as before. Otherwise other lines
11847 would have to be shifted up or down. */
11848 && this_line_pixel_height == line_height_before)
11849 {
11850 /* If this is not the window's last line, we must adjust
11851 the charstarts of the lines below. */
11852 if (it.current_y < it.last_visible_y)
11853 {
11854 struct glyph_row *row
11855 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11856 int delta, delta_bytes;
11857
11858 /* We used to distinguish between two cases here,
11859 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11860 when the line ends in a newline or the end of the
11861 buffer's accessible portion. But both cases did
11862 the same, so they were collapsed. */
11863 delta = (Z
11864 - CHARPOS (tlendpos)
11865 - MATRIX_ROW_START_CHARPOS (row));
11866 delta_bytes = (Z_BYTE
11867 - BYTEPOS (tlendpos)
11868 - MATRIX_ROW_START_BYTEPOS (row));
11869
11870 increment_matrix_positions (w->current_matrix,
11871 this_line_vpos + 1,
11872 w->current_matrix->nrows,
11873 delta, delta_bytes);
11874 }
11875
11876 /* If this row displays text now but previously didn't,
11877 or vice versa, w->window_end_vpos may have to be
11878 adjusted. */
11879 if ((it.glyph_row - 1)->displays_text_p)
11880 {
11881 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11882 XSETINT (w->window_end_vpos, this_line_vpos);
11883 }
11884 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11885 && this_line_vpos > 0)
11886 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11887 w->window_end_valid = Qnil;
11888
11889 /* Update hint: No need to try to scroll in update_window. */
11890 w->desired_matrix->no_scrolling_p = 1;
11891
11892 #if GLYPH_DEBUG
11893 *w->desired_matrix->method = 0;
11894 debug_method_add (w, "optimization 1");
11895 #endif
11896 #ifdef HAVE_WINDOW_SYSTEM
11897 update_window_fringes (w, 0);
11898 #endif
11899 goto update;
11900 }
11901 else
11902 goto cancel;
11903 }
11904 else if (/* Cursor position hasn't changed. */
11905 PT == XFASTINT (w->last_point)
11906 /* Make sure the cursor was last displayed
11907 in this window. Otherwise we have to reposition it. */
11908 && 0 <= w->cursor.vpos
11909 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11910 {
11911 if (!must_finish)
11912 {
11913 do_pending_window_change (1);
11914
11915 /* We used to always goto end_of_redisplay here, but this
11916 isn't enough if we have a blinking cursor. */
11917 if (w->cursor_off_p == w->last_cursor_off_p)
11918 goto end_of_redisplay;
11919 }
11920 goto update;
11921 }
11922 /* If highlighting the region, or if the cursor is in the echo area,
11923 then we can't just move the cursor. */
11924 else if (! (!NILP (Vtransient_mark_mode)
11925 && !NILP (current_buffer->mark_active))
11926 && (EQ (selected_window, current_buffer->last_selected_window)
11927 || highlight_nonselected_windows)
11928 && NILP (w->region_showing)
11929 && NILP (Vshow_trailing_whitespace)
11930 && !cursor_in_echo_area)
11931 {
11932 struct it it;
11933 struct glyph_row *row;
11934
11935 /* Skip from tlbufpos to PT and see where it is. Note that
11936 PT may be in invisible text. If so, we will end at the
11937 next visible position. */
11938 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11939 NULL, DEFAULT_FACE_ID);
11940 it.current_x = this_line_start_x;
11941 it.current_y = this_line_y;
11942 it.vpos = this_line_vpos;
11943
11944 /* The call to move_it_to stops in front of PT, but
11945 moves over before-strings. */
11946 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11947
11948 if (it.vpos == this_line_vpos
11949 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11950 row->enabled_p))
11951 {
11952 xassert (this_line_vpos == it.vpos);
11953 xassert (this_line_y == it.current_y);
11954 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11955 #if GLYPH_DEBUG
11956 *w->desired_matrix->method = 0;
11957 debug_method_add (w, "optimization 3");
11958 #endif
11959 goto update;
11960 }
11961 else
11962 goto cancel;
11963 }
11964
11965 cancel:
11966 /* Text changed drastically or point moved off of line. */
11967 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11968 }
11969
11970 CHARPOS (this_line_start_pos) = 0;
11971 consider_all_windows_p |= buffer_shared > 1;
11972 ++clear_face_cache_count;
11973 #ifdef HAVE_WINDOW_SYSTEM
11974 ++clear_image_cache_count;
11975 #endif
11976
11977 /* Build desired matrices, and update the display. If
11978 consider_all_windows_p is non-zero, do it for all windows on all
11979 frames. Otherwise do it for selected_window, only. */
11980
11981 if (consider_all_windows_p)
11982 {
11983 Lisp_Object tail, frame;
11984
11985 FOR_EACH_FRAME (tail, frame)
11986 XFRAME (frame)->updated_p = 0;
11987
11988 /* Recompute # windows showing selected buffer. This will be
11989 incremented each time such a window is displayed. */
11990 buffer_shared = 0;
11991
11992 FOR_EACH_FRAME (tail, frame)
11993 {
11994 struct frame *f = XFRAME (frame);
11995
11996 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11997 {
11998 if (! EQ (frame, selected_frame))
11999 /* Select the frame, for the sake of frame-local
12000 variables. */
12001 select_frame_for_redisplay (frame);
12002
12003 /* Mark all the scroll bars to be removed; we'll redeem
12004 the ones we want when we redisplay their windows. */
12005 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12006 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12007
12008 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12009 redisplay_windows (FRAME_ROOT_WINDOW (f));
12010
12011 /* The X error handler may have deleted that frame. */
12012 if (!FRAME_LIVE_P (f))
12013 continue;
12014
12015 /* Any scroll bars which redisplay_windows should have
12016 nuked should now go away. */
12017 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12018 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12019
12020 /* If fonts changed, display again. */
12021 /* ??? rms: I suspect it is a mistake to jump all the way
12022 back to retry here. It should just retry this frame. */
12023 if (fonts_changed_p)
12024 goto retry;
12025
12026 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12027 {
12028 /* See if we have to hscroll. */
12029 if (!f->already_hscrolled_p)
12030 {
12031 f->already_hscrolled_p = 1;
12032 if (hscroll_windows (f->root_window))
12033 goto retry;
12034 }
12035
12036 /* Prevent various kinds of signals during display
12037 update. stdio is not robust about handling
12038 signals, which can cause an apparent I/O
12039 error. */
12040 if (interrupt_input)
12041 unrequest_sigio ();
12042 STOP_POLLING;
12043
12044 /* Update the display. */
12045 set_window_update_flags (XWINDOW (f->root_window), 1);
12046 pause |= update_frame (f, 0, 0);
12047 f->updated_p = 1;
12048 }
12049 }
12050 }
12051
12052 if (!EQ (old_frame, selected_frame)
12053 && FRAME_LIVE_P (XFRAME (old_frame)))
12054 /* We played a bit fast-and-loose above and allowed selected_frame
12055 and selected_window to be temporarily out-of-sync but let's make
12056 sure this stays contained. */
12057 select_frame_for_redisplay (old_frame);
12058 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12059
12060 if (!pause)
12061 {
12062 /* Do the mark_window_display_accurate after all windows have
12063 been redisplayed because this call resets flags in buffers
12064 which are needed for proper redisplay. */
12065 FOR_EACH_FRAME (tail, frame)
12066 {
12067 struct frame *f = XFRAME (frame);
12068 if (f->updated_p)
12069 {
12070 mark_window_display_accurate (f->root_window, 1);
12071 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12072 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12073 }
12074 }
12075 }
12076 }
12077 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12078 {
12079 Lisp_Object mini_window;
12080 struct frame *mini_frame;
12081
12082 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12083 /* Use list_of_error, not Qerror, so that
12084 we catch only errors and don't run the debugger. */
12085 internal_condition_case_1 (redisplay_window_1, selected_window,
12086 list_of_error,
12087 redisplay_window_error);
12088
12089 /* Compare desired and current matrices, perform output. */
12090
12091 update:
12092 /* If fonts changed, display again. */
12093 if (fonts_changed_p)
12094 goto retry;
12095
12096 /* Prevent various kinds of signals during display update.
12097 stdio is not robust about handling signals,
12098 which can cause an apparent I/O error. */
12099 if (interrupt_input)
12100 unrequest_sigio ();
12101 STOP_POLLING;
12102
12103 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12104 {
12105 if (hscroll_windows (selected_window))
12106 goto retry;
12107
12108 XWINDOW (selected_window)->must_be_updated_p = 1;
12109 pause = update_frame (sf, 0, 0);
12110 }
12111
12112 /* We may have called echo_area_display at the top of this
12113 function. If the echo area is on another frame, that may
12114 have put text on a frame other than the selected one, so the
12115 above call to update_frame would not have caught it. Catch
12116 it here. */
12117 mini_window = FRAME_MINIBUF_WINDOW (sf);
12118 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12119
12120 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12121 {
12122 XWINDOW (mini_window)->must_be_updated_p = 1;
12123 pause |= update_frame (mini_frame, 0, 0);
12124 if (!pause && hscroll_windows (mini_window))
12125 goto retry;
12126 }
12127 }
12128
12129 /* If display was paused because of pending input, make sure we do a
12130 thorough update the next time. */
12131 if (pause)
12132 {
12133 /* Prevent the optimization at the beginning of
12134 redisplay_internal that tries a single-line update of the
12135 line containing the cursor in the selected window. */
12136 CHARPOS (this_line_start_pos) = 0;
12137
12138 /* Let the overlay arrow be updated the next time. */
12139 update_overlay_arrows (0);
12140
12141 /* If we pause after scrolling, some rows in the current
12142 matrices of some windows are not valid. */
12143 if (!WINDOW_FULL_WIDTH_P (w)
12144 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12145 update_mode_lines = 1;
12146 }
12147 else
12148 {
12149 if (!consider_all_windows_p)
12150 {
12151 /* This has already been done above if
12152 consider_all_windows_p is set. */
12153 mark_window_display_accurate_1 (w, 1);
12154
12155 /* Say overlay arrows are up to date. */
12156 update_overlay_arrows (1);
12157
12158 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12159 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12160 }
12161
12162 update_mode_lines = 0;
12163 windows_or_buffers_changed = 0;
12164 cursor_type_changed = 0;
12165 }
12166
12167 /* Start SIGIO interrupts coming again. Having them off during the
12168 code above makes it less likely one will discard output, but not
12169 impossible, since there might be stuff in the system buffer here.
12170 But it is much hairier to try to do anything about that. */
12171 if (interrupt_input)
12172 request_sigio ();
12173 RESUME_POLLING;
12174
12175 /* If a frame has become visible which was not before, redisplay
12176 again, so that we display it. Expose events for such a frame
12177 (which it gets when becoming visible) don't call the parts of
12178 redisplay constructing glyphs, so simply exposing a frame won't
12179 display anything in this case. So, we have to display these
12180 frames here explicitly. */
12181 if (!pause)
12182 {
12183 Lisp_Object tail, frame;
12184 int new_count = 0;
12185
12186 FOR_EACH_FRAME (tail, frame)
12187 {
12188 int this_is_visible = 0;
12189
12190 if (XFRAME (frame)->visible)
12191 this_is_visible = 1;
12192 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12193 if (XFRAME (frame)->visible)
12194 this_is_visible = 1;
12195
12196 if (this_is_visible)
12197 new_count++;
12198 }
12199
12200 if (new_count != number_of_visible_frames)
12201 windows_or_buffers_changed++;
12202 }
12203
12204 /* Change frame size now if a change is pending. */
12205 do_pending_window_change (1);
12206
12207 /* If we just did a pending size change, or have additional
12208 visible frames, redisplay again. */
12209 if (windows_or_buffers_changed && !pause)
12210 goto retry;
12211
12212 /* Clear the face and image caches.
12213
12214 We used to do this only if consider_all_windows_p. But the cache
12215 needs to be cleared if a timer creates images in the current
12216 buffer (e.g. the test case in Bug#6230). */
12217
12218 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12219 {
12220 clear_face_cache (0);
12221 clear_face_cache_count = 0;
12222 }
12223
12224 #ifdef HAVE_WINDOW_SYSTEM
12225 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12226 {
12227 clear_image_caches (Qnil);
12228 clear_image_cache_count = 0;
12229 }
12230 #endif /* HAVE_WINDOW_SYSTEM */
12231
12232 end_of_redisplay:
12233 unbind_to (count, Qnil);
12234 RESUME_POLLING;
12235 }
12236
12237
12238 /* Redisplay, but leave alone any recent echo area message unless
12239 another message has been requested in its place.
12240
12241 This is useful in situations where you need to redisplay but no
12242 user action has occurred, making it inappropriate for the message
12243 area to be cleared. See tracking_off and
12244 wait_reading_process_output for examples of these situations.
12245
12246 FROM_WHERE is an integer saying from where this function was
12247 called. This is useful for debugging. */
12248
12249 void
12250 redisplay_preserve_echo_area (int from_where)
12251 {
12252 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12253
12254 if (!NILP (echo_area_buffer[1]))
12255 {
12256 /* We have a previously displayed message, but no current
12257 message. Redisplay the previous message. */
12258 display_last_displayed_message_p = 1;
12259 redisplay_internal (1);
12260 display_last_displayed_message_p = 0;
12261 }
12262 else
12263 redisplay_internal (1);
12264
12265 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12266 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12267 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12268 }
12269
12270
12271 /* Function registered with record_unwind_protect in
12272 redisplay_internal. Reset redisplaying_p to the value it had
12273 before redisplay_internal was called, and clear
12274 prevent_freeing_realized_faces_p. It also selects the previously
12275 selected frame, unless it has been deleted (by an X connection
12276 failure during redisplay, for example). */
12277
12278 static Lisp_Object
12279 unwind_redisplay (Lisp_Object val)
12280 {
12281 Lisp_Object old_redisplaying_p, old_frame;
12282
12283 old_redisplaying_p = XCAR (val);
12284 redisplaying_p = XFASTINT (old_redisplaying_p);
12285 old_frame = XCDR (val);
12286 if (! EQ (old_frame, selected_frame)
12287 && FRAME_LIVE_P (XFRAME (old_frame)))
12288 select_frame_for_redisplay (old_frame);
12289 return Qnil;
12290 }
12291
12292
12293 /* Mark the display of window W as accurate or inaccurate. If
12294 ACCURATE_P is non-zero mark display of W as accurate. If
12295 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12296 redisplay_internal is called. */
12297
12298 static void
12299 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12300 {
12301 if (BUFFERP (w->buffer))
12302 {
12303 struct buffer *b = XBUFFER (w->buffer);
12304
12305 w->last_modified
12306 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12307 w->last_overlay_modified
12308 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12309 w->last_had_star
12310 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12311
12312 if (accurate_p)
12313 {
12314 b->clip_changed = 0;
12315 b->prevent_redisplay_optimizations_p = 0;
12316
12317 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12318 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12319 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12320 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12321
12322 w->current_matrix->buffer = b;
12323 w->current_matrix->begv = BUF_BEGV (b);
12324 w->current_matrix->zv = BUF_ZV (b);
12325
12326 w->last_cursor = w->cursor;
12327 w->last_cursor_off_p = w->cursor_off_p;
12328
12329 if (w == XWINDOW (selected_window))
12330 w->last_point = make_number (BUF_PT (b));
12331 else
12332 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12333 }
12334 }
12335
12336 if (accurate_p)
12337 {
12338 w->window_end_valid = w->buffer;
12339 w->update_mode_line = Qnil;
12340 }
12341 }
12342
12343
12344 /* Mark the display of windows in the window tree rooted at WINDOW as
12345 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12346 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12347 be redisplayed the next time redisplay_internal is called. */
12348
12349 void
12350 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12351 {
12352 struct window *w;
12353
12354 for (; !NILP (window); window = w->next)
12355 {
12356 w = XWINDOW (window);
12357 mark_window_display_accurate_1 (w, accurate_p);
12358
12359 if (!NILP (w->vchild))
12360 mark_window_display_accurate (w->vchild, accurate_p);
12361 if (!NILP (w->hchild))
12362 mark_window_display_accurate (w->hchild, accurate_p);
12363 }
12364
12365 if (accurate_p)
12366 {
12367 update_overlay_arrows (1);
12368 }
12369 else
12370 {
12371 /* Force a thorough redisplay the next time by setting
12372 last_arrow_position and last_arrow_string to t, which is
12373 unequal to any useful value of Voverlay_arrow_... */
12374 update_overlay_arrows (-1);
12375 }
12376 }
12377
12378
12379 /* Return value in display table DP (Lisp_Char_Table *) for character
12380 C. Since a display table doesn't have any parent, we don't have to
12381 follow parent. Do not call this function directly but use the
12382 macro DISP_CHAR_VECTOR. */
12383
12384 Lisp_Object
12385 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12386 {
12387 Lisp_Object val;
12388
12389 if (ASCII_CHAR_P (c))
12390 {
12391 val = dp->ascii;
12392 if (SUB_CHAR_TABLE_P (val))
12393 val = XSUB_CHAR_TABLE (val)->contents[c];
12394 }
12395 else
12396 {
12397 Lisp_Object table;
12398
12399 XSETCHAR_TABLE (table, dp);
12400 val = char_table_ref (table, c);
12401 }
12402 if (NILP (val))
12403 val = dp->defalt;
12404 return val;
12405 }
12406
12407
12408 \f
12409 /***********************************************************************
12410 Window Redisplay
12411 ***********************************************************************/
12412
12413 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12414
12415 static void
12416 redisplay_windows (Lisp_Object window)
12417 {
12418 while (!NILP (window))
12419 {
12420 struct window *w = XWINDOW (window);
12421
12422 if (!NILP (w->hchild))
12423 redisplay_windows (w->hchild);
12424 else if (!NILP (w->vchild))
12425 redisplay_windows (w->vchild);
12426 else if (!NILP (w->buffer))
12427 {
12428 displayed_buffer = XBUFFER (w->buffer);
12429 /* Use list_of_error, not Qerror, so that
12430 we catch only errors and don't run the debugger. */
12431 internal_condition_case_1 (redisplay_window_0, window,
12432 list_of_error,
12433 redisplay_window_error);
12434 }
12435
12436 window = w->next;
12437 }
12438 }
12439
12440 static Lisp_Object
12441 redisplay_window_error (Lisp_Object ignore)
12442 {
12443 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12444 return Qnil;
12445 }
12446
12447 static Lisp_Object
12448 redisplay_window_0 (Lisp_Object window)
12449 {
12450 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12451 redisplay_window (window, 0);
12452 return Qnil;
12453 }
12454
12455 static Lisp_Object
12456 redisplay_window_1 (Lisp_Object window)
12457 {
12458 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12459 redisplay_window (window, 1);
12460 return Qnil;
12461 }
12462 \f
12463
12464 /* Increment GLYPH until it reaches END or CONDITION fails while
12465 adding (GLYPH)->pixel_width to X. */
12466
12467 #define SKIP_GLYPHS(glyph, end, x, condition) \
12468 do \
12469 { \
12470 (x) += (glyph)->pixel_width; \
12471 ++(glyph); \
12472 } \
12473 while ((glyph) < (end) && (condition))
12474
12475
12476 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12477 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12478 which positions recorded in ROW differ from current buffer
12479 positions.
12480
12481 Return 0 if cursor is not on this row, 1 otherwise. */
12482
12483 int
12484 set_cursor_from_row (struct window *w, struct glyph_row *row,
12485 struct glyph_matrix *matrix, int delta, int delta_bytes,
12486 int dy, int dvpos)
12487 {
12488 struct glyph *glyph = row->glyphs[TEXT_AREA];
12489 struct glyph *end = glyph + row->used[TEXT_AREA];
12490 struct glyph *cursor = NULL;
12491 /* The last known character position in row. */
12492 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12493 int x = row->x;
12494 EMACS_INT pt_old = PT - delta;
12495 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12496 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12497 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12498 /* A glyph beyond the edge of TEXT_AREA which we should never
12499 touch. */
12500 struct glyph *glyphs_end = end;
12501 /* Non-zero means we've found a match for cursor position, but that
12502 glyph has the avoid_cursor_p flag set. */
12503 int match_with_avoid_cursor = 0;
12504 /* Non-zero means we've seen at least one glyph that came from a
12505 display string. */
12506 int string_seen = 0;
12507 /* Largest buffer position seen so far during scan of glyph row. */
12508 EMACS_INT bpos_max = last_pos;
12509 /* Last buffer position covered by an overlay string with an integer
12510 `cursor' property. */
12511 EMACS_INT bpos_covered = 0;
12512
12513 /* Skip over glyphs not having an object at the start and the end of
12514 the row. These are special glyphs like truncation marks on
12515 terminal frames. */
12516 if (row->displays_text_p)
12517 {
12518 if (!row->reversed_p)
12519 {
12520 while (glyph < end
12521 && INTEGERP (glyph->object)
12522 && glyph->charpos < 0)
12523 {
12524 x += glyph->pixel_width;
12525 ++glyph;
12526 }
12527 while (end > glyph
12528 && INTEGERP ((end - 1)->object)
12529 /* CHARPOS is zero for blanks and stretch glyphs
12530 inserted by extend_face_to_end_of_line. */
12531 && (end - 1)->charpos <= 0)
12532 --end;
12533 glyph_before = glyph - 1;
12534 glyph_after = end;
12535 }
12536 else
12537 {
12538 struct glyph *g;
12539
12540 /* If the glyph row is reversed, we need to process it from back
12541 to front, so swap the edge pointers. */
12542 glyphs_end = end = glyph - 1;
12543 glyph += row->used[TEXT_AREA] - 1;
12544
12545 while (glyph > end + 1
12546 && INTEGERP (glyph->object)
12547 && glyph->charpos < 0)
12548 {
12549 --glyph;
12550 x -= glyph->pixel_width;
12551 }
12552 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12553 --glyph;
12554 /* By default, in reversed rows we put the cursor on the
12555 rightmost (first in the reading order) glyph. */
12556 for (g = end + 1; g < glyph; g++)
12557 x += g->pixel_width;
12558 while (end < glyph
12559 && INTEGERP ((end + 1)->object)
12560 && (end + 1)->charpos <= 0)
12561 ++end;
12562 glyph_before = glyph + 1;
12563 glyph_after = end;
12564 }
12565 }
12566 else if (row->reversed_p)
12567 {
12568 /* In R2L rows that don't display text, put the cursor on the
12569 rightmost glyph. Case in point: an empty last line that is
12570 part of an R2L paragraph. */
12571 cursor = end - 1;
12572 /* Avoid placing the cursor on the last glyph of the row, where
12573 on terminal frames we hold the vertical border between
12574 adjacent windows. */
12575 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12576 && !WINDOW_RIGHTMOST_P (w)
12577 && cursor == row->glyphs[LAST_AREA] - 1)
12578 cursor--;
12579 x = -1; /* will be computed below, at label compute_x */
12580 }
12581
12582 /* Step 1: Try to find the glyph whose character position
12583 corresponds to point. If that's not possible, find 2 glyphs
12584 whose character positions are the closest to point, one before
12585 point, the other after it. */
12586 if (!row->reversed_p)
12587 while (/* not marched to end of glyph row */
12588 glyph < end
12589 /* glyph was not inserted by redisplay for internal purposes */
12590 && !INTEGERP (glyph->object))
12591 {
12592 if (BUFFERP (glyph->object))
12593 {
12594 EMACS_INT dpos = glyph->charpos - pt_old;
12595
12596 if (glyph->charpos > bpos_max)
12597 bpos_max = glyph->charpos;
12598 if (!glyph->avoid_cursor_p)
12599 {
12600 /* If we hit point, we've found the glyph on which to
12601 display the cursor. */
12602 if (dpos == 0)
12603 {
12604 match_with_avoid_cursor = 0;
12605 break;
12606 }
12607 /* See if we've found a better approximation to
12608 POS_BEFORE or to POS_AFTER. Note that we want the
12609 first (leftmost) glyph of all those that are the
12610 closest from below, and the last (rightmost) of all
12611 those from above. */
12612 if (0 > dpos && dpos > pos_before - pt_old)
12613 {
12614 pos_before = glyph->charpos;
12615 glyph_before = glyph;
12616 }
12617 else if (0 < dpos && dpos <= pos_after - pt_old)
12618 {
12619 pos_after = glyph->charpos;
12620 glyph_after = glyph;
12621 }
12622 }
12623 else if (dpos == 0)
12624 match_with_avoid_cursor = 1;
12625 }
12626 else if (STRINGP (glyph->object))
12627 {
12628 Lisp_Object chprop;
12629 int glyph_pos = glyph->charpos;
12630
12631 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12632 glyph->object);
12633 if (INTEGERP (chprop))
12634 {
12635 bpos_covered = bpos_max + XINT (chprop);
12636 /* If the `cursor' property covers buffer positions up
12637 to and including point, we should display cursor on
12638 this glyph. Note that overlays and text properties
12639 with string values stop bidi reordering, so every
12640 buffer position to the left of the string is always
12641 smaller than any position to the right of the
12642 string. Therefore, if a `cursor' property on one
12643 of the string's characters has an integer value, we
12644 will break out of the loop below _before_ we get to
12645 the position match above. IOW, integer values of
12646 the `cursor' property override the "exact match for
12647 point" strategy of positioning the cursor. */
12648 /* Implementation note: bpos_max == pt_old when, e.g.,
12649 we are in an empty line, where bpos_max is set to
12650 MATRIX_ROW_START_CHARPOS, see above. */
12651 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12652 {
12653 cursor = glyph;
12654 break;
12655 }
12656 }
12657
12658 string_seen = 1;
12659 }
12660 x += glyph->pixel_width;
12661 ++glyph;
12662 }
12663 else if (glyph > end) /* row is reversed */
12664 while (!INTEGERP (glyph->object))
12665 {
12666 if (BUFFERP (glyph->object))
12667 {
12668 EMACS_INT dpos = glyph->charpos - pt_old;
12669
12670 if (glyph->charpos > bpos_max)
12671 bpos_max = glyph->charpos;
12672 if (!glyph->avoid_cursor_p)
12673 {
12674 if (dpos == 0)
12675 {
12676 match_with_avoid_cursor = 0;
12677 break;
12678 }
12679 if (0 > dpos && dpos > pos_before - pt_old)
12680 {
12681 pos_before = glyph->charpos;
12682 glyph_before = glyph;
12683 }
12684 else if (0 < dpos && dpos <= pos_after - pt_old)
12685 {
12686 pos_after = glyph->charpos;
12687 glyph_after = glyph;
12688 }
12689 }
12690 else if (dpos == 0)
12691 match_with_avoid_cursor = 1;
12692 }
12693 else if (STRINGP (glyph->object))
12694 {
12695 Lisp_Object chprop;
12696 int glyph_pos = glyph->charpos;
12697
12698 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12699 glyph->object);
12700 if (INTEGERP (chprop))
12701 {
12702 bpos_covered = bpos_max + XINT (chprop);
12703 /* If the `cursor' property covers buffer positions up
12704 to and including point, we should display cursor on
12705 this glyph. */
12706 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12707 {
12708 cursor = glyph;
12709 break;
12710 }
12711 }
12712 string_seen = 1;
12713 }
12714 --glyph;
12715 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12716 {
12717 x--; /* can't use any pixel_width */
12718 break;
12719 }
12720 x -= glyph->pixel_width;
12721 }
12722
12723 /* Step 2: If we didn't find an exact match for point, we need to
12724 look for a proper place to put the cursor among glyphs between
12725 GLYPH_BEFORE and GLYPH_AFTER. */
12726 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12727 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12728 && bpos_covered < pt_old)
12729 {
12730 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12731 {
12732 EMACS_INT ellipsis_pos;
12733
12734 /* Scan back over the ellipsis glyphs. */
12735 if (!row->reversed_p)
12736 {
12737 ellipsis_pos = (glyph - 1)->charpos;
12738 while (glyph > row->glyphs[TEXT_AREA]
12739 && (glyph - 1)->charpos == ellipsis_pos)
12740 glyph--, x -= glyph->pixel_width;
12741 /* That loop always goes one position too far, including
12742 the glyph before the ellipsis. So scan forward over
12743 that one. */
12744 x += glyph->pixel_width;
12745 glyph++;
12746 }
12747 else /* row is reversed */
12748 {
12749 ellipsis_pos = (glyph + 1)->charpos;
12750 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12751 && (glyph + 1)->charpos == ellipsis_pos)
12752 glyph++, x += glyph->pixel_width;
12753 x -= glyph->pixel_width;
12754 glyph--;
12755 }
12756 }
12757 else if (match_with_avoid_cursor
12758 /* zero-width characters produce no glyphs */
12759 || ((row->reversed_p
12760 ? glyph_after > glyphs_end
12761 : glyph_after < glyphs_end)
12762 && eabs (glyph_after - glyph_before) == 1))
12763 {
12764 cursor = glyph_after;
12765 x = -1;
12766 }
12767 else if (string_seen)
12768 {
12769 int incr = row->reversed_p ? -1 : +1;
12770
12771 /* Need to find the glyph that came out of a string which is
12772 present at point. That glyph is somewhere between
12773 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12774 positioned between POS_BEFORE and POS_AFTER in the
12775 buffer. */
12776 struct glyph *stop = glyph_after;
12777 EMACS_INT pos = pos_before;
12778
12779 x = -1;
12780 for (glyph = glyph_before + incr;
12781 row->reversed_p ? glyph > stop : glyph < stop; )
12782 {
12783
12784 /* Any glyphs that come from the buffer are here because
12785 of bidi reordering. Skip them, and only pay
12786 attention to glyphs that came from some string. */
12787 if (STRINGP (glyph->object))
12788 {
12789 Lisp_Object str;
12790 EMACS_INT tem;
12791
12792 str = glyph->object;
12793 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12794 if (tem == 0 /* from overlay */
12795 || pos <= tem)
12796 {
12797 /* If the string from which this glyph came is
12798 found in the buffer at point, then we've
12799 found the glyph we've been looking for. If
12800 it comes from an overlay (tem == 0), and it
12801 has the `cursor' property on one of its
12802 glyphs, record that glyph as a candidate for
12803 displaying the cursor. (As in the
12804 unidirectional version, we will display the
12805 cursor on the last candidate we find.) */
12806 if (tem == 0 || tem == pt_old)
12807 {
12808 /* The glyphs from this string could have
12809 been reordered. Find the one with the
12810 smallest string position. Or there could
12811 be a character in the string with the
12812 `cursor' property, which means display
12813 cursor on that character's glyph. */
12814 int strpos = glyph->charpos;
12815
12816 cursor = glyph;
12817 for (glyph += incr;
12818 (row->reversed_p ? glyph > stop : glyph < stop)
12819 && EQ (glyph->object, str);
12820 glyph += incr)
12821 {
12822 Lisp_Object cprop;
12823 int gpos = glyph->charpos;
12824
12825 cprop = Fget_char_property (make_number (gpos),
12826 Qcursor,
12827 glyph->object);
12828 if (!NILP (cprop))
12829 {
12830 cursor = glyph;
12831 break;
12832 }
12833 if (glyph->charpos < strpos)
12834 {
12835 strpos = glyph->charpos;
12836 cursor = glyph;
12837 }
12838 }
12839
12840 if (tem == pt_old)
12841 goto compute_x;
12842 }
12843 if (tem)
12844 pos = tem + 1; /* don't find previous instances */
12845 }
12846 /* This string is not what we want; skip all of the
12847 glyphs that came from it. */
12848 do
12849 glyph += incr;
12850 while ((row->reversed_p ? glyph > stop : glyph < stop)
12851 && EQ (glyph->object, str));
12852 }
12853 else
12854 glyph += incr;
12855 }
12856
12857 /* If we reached the end of the line, and END was from a string,
12858 the cursor is not on this line. */
12859 if (cursor == NULL
12860 && (row->reversed_p ? glyph <= end : glyph >= end)
12861 && STRINGP (end->object)
12862 && row->continued_p)
12863 return 0;
12864 }
12865 }
12866
12867 compute_x:
12868 if (cursor != NULL)
12869 glyph = cursor;
12870 if (x < 0)
12871 {
12872 struct glyph *g;
12873
12874 /* Need to compute x that corresponds to GLYPH. */
12875 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12876 {
12877 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12878 abort ();
12879 x += g->pixel_width;
12880 }
12881 }
12882
12883 /* ROW could be part of a continued line, which, under bidi
12884 reordering, might have other rows whose start and end charpos
12885 occlude point. Only set w->cursor if we found a better
12886 approximation to the cursor position than we have from previously
12887 examined candidate rows belonging to the same continued line. */
12888 if (/* we already have a candidate row */
12889 w->cursor.vpos >= 0
12890 /* that candidate is not the row we are processing */
12891 && MATRIX_ROW (matrix, w->cursor.vpos) != row
12892 /* the row we are processing is part of a continued line */
12893 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
12894 /* Make sure cursor.vpos specifies a row whose start and end
12895 charpos occlude point. This is because some callers of this
12896 function leave cursor.vpos at the row where the cursor was
12897 displayed during the last redisplay cycle. */
12898 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
12899 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
12900 {
12901 struct glyph *g1 =
12902 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
12903
12904 /* Don't consider glyphs that are outside TEXT_AREA. */
12905 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
12906 return 0;
12907 /* Keep the candidate whose buffer position is the closest to
12908 point. */
12909 if (/* previous candidate is a glyph in TEXT_AREA of that row */
12910 w->cursor.hpos >= 0
12911 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
12912 && BUFFERP (g1->object)
12913 && (g1->charpos == pt_old /* an exact match always wins */
12914 || (BUFFERP (glyph->object)
12915 && eabs (g1->charpos - pt_old)
12916 < eabs (glyph->charpos - pt_old))))
12917 return 0;
12918 /* If this candidate gives an exact match, use that. */
12919 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12920 /* Otherwise, keep the candidate that comes from a row
12921 spanning less buffer positions. This may win when one or
12922 both candidate positions are on glyphs that came from
12923 display strings, for which we cannot compare buffer
12924 positions. */
12925 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12926 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12927 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
12928 return 0;
12929 }
12930 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12931 w->cursor.x = x;
12932 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12933 w->cursor.y = row->y + dy;
12934
12935 if (w == XWINDOW (selected_window))
12936 {
12937 if (!row->continued_p
12938 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12939 && row->x == 0)
12940 {
12941 this_line_buffer = XBUFFER (w->buffer);
12942
12943 CHARPOS (this_line_start_pos)
12944 = MATRIX_ROW_START_CHARPOS (row) + delta;
12945 BYTEPOS (this_line_start_pos)
12946 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12947
12948 CHARPOS (this_line_end_pos)
12949 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12950 BYTEPOS (this_line_end_pos)
12951 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12952
12953 this_line_y = w->cursor.y;
12954 this_line_pixel_height = row->height;
12955 this_line_vpos = w->cursor.vpos;
12956 this_line_start_x = row->x;
12957 }
12958 else
12959 CHARPOS (this_line_start_pos) = 0;
12960 }
12961
12962 return 1;
12963 }
12964
12965
12966 /* Run window scroll functions, if any, for WINDOW with new window
12967 start STARTP. Sets the window start of WINDOW to that position.
12968
12969 We assume that the window's buffer is really current. */
12970
12971 static INLINE struct text_pos
12972 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
12973 {
12974 struct window *w = XWINDOW (window);
12975 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12976
12977 if (current_buffer != XBUFFER (w->buffer))
12978 abort ();
12979
12980 if (!NILP (Vwindow_scroll_functions))
12981 {
12982 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12983 make_number (CHARPOS (startp)));
12984 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12985 /* In case the hook functions switch buffers. */
12986 if (current_buffer != XBUFFER (w->buffer))
12987 set_buffer_internal_1 (XBUFFER (w->buffer));
12988 }
12989
12990 return startp;
12991 }
12992
12993
12994 /* Make sure the line containing the cursor is fully visible.
12995 A value of 1 means there is nothing to be done.
12996 (Either the line is fully visible, or it cannot be made so,
12997 or we cannot tell.)
12998
12999 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13000 is higher than window.
13001
13002 A value of 0 means the caller should do scrolling
13003 as if point had gone off the screen. */
13004
13005 static int
13006 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13007 {
13008 struct glyph_matrix *matrix;
13009 struct glyph_row *row;
13010 int window_height;
13011
13012 if (!make_cursor_line_fully_visible_p)
13013 return 1;
13014
13015 /* It's not always possible to find the cursor, e.g, when a window
13016 is full of overlay strings. Don't do anything in that case. */
13017 if (w->cursor.vpos < 0)
13018 return 1;
13019
13020 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13021 row = MATRIX_ROW (matrix, w->cursor.vpos);
13022
13023 /* If the cursor row is not partially visible, there's nothing to do. */
13024 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13025 return 1;
13026
13027 /* If the row the cursor is in is taller than the window's height,
13028 it's not clear what to do, so do nothing. */
13029 window_height = window_box_height (w);
13030 if (row->height >= window_height)
13031 {
13032 if (!force_p || MINI_WINDOW_P (w)
13033 || w->vscroll || w->cursor.vpos == 0)
13034 return 1;
13035 }
13036 return 0;
13037 }
13038
13039
13040 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13041 non-zero means only WINDOW is redisplayed in redisplay_internal.
13042 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13043 in redisplay_window to bring a partially visible line into view in
13044 the case that only the cursor has moved.
13045
13046 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13047 last screen line's vertical height extends past the end of the screen.
13048
13049 Value is
13050
13051 1 if scrolling succeeded
13052
13053 0 if scrolling didn't find point.
13054
13055 -1 if new fonts have been loaded so that we must interrupt
13056 redisplay, adjust glyph matrices, and try again. */
13057
13058 enum
13059 {
13060 SCROLLING_SUCCESS,
13061 SCROLLING_FAILED,
13062 SCROLLING_NEED_LARGER_MATRICES
13063 };
13064
13065 static int
13066 try_scrolling (Lisp_Object window, int just_this_one_p,
13067 EMACS_INT scroll_conservatively, EMACS_INT scroll_step,
13068 int temp_scroll_step, int last_line_misfit)
13069 {
13070 struct window *w = XWINDOW (window);
13071 struct frame *f = XFRAME (w->frame);
13072 struct text_pos pos, startp;
13073 struct it it;
13074 int this_scroll_margin, scroll_max, rc, height;
13075 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13076 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13077 Lisp_Object aggressive;
13078 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13079
13080 #if GLYPH_DEBUG
13081 debug_method_add (w, "try_scrolling");
13082 #endif
13083
13084 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13085
13086 /* Compute scroll margin height in pixels. We scroll when point is
13087 within this distance from the top or bottom of the window. */
13088 if (scroll_margin > 0)
13089 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13090 * FRAME_LINE_HEIGHT (f);
13091 else
13092 this_scroll_margin = 0;
13093
13094 /* Force scroll_conservatively to have a reasonable value, to avoid
13095 overflow while computing how much to scroll. Note that the user
13096 can supply scroll-conservatively equal to `most-positive-fixnum',
13097 which can be larger than INT_MAX. */
13098 if (scroll_conservatively > scroll_limit)
13099 {
13100 scroll_conservatively = scroll_limit;
13101 scroll_max = INT_MAX;
13102 }
13103 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13104 /* Compute how much we should try to scroll maximally to bring
13105 point into view. */
13106 scroll_max = (max (scroll_step,
13107 max (scroll_conservatively, temp_scroll_step))
13108 * FRAME_LINE_HEIGHT (f));
13109 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13110 || NUMBERP (current_buffer->scroll_up_aggressively))
13111 /* We're trying to scroll because of aggressive scrolling but no
13112 scroll_step is set. Choose an arbitrary one. */
13113 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13114 else
13115 scroll_max = 0;
13116
13117 too_near_end:
13118
13119 /* Decide whether to scroll down. */
13120 if (PT > CHARPOS (startp))
13121 {
13122 int scroll_margin_y;
13123
13124 /* Compute the pixel ypos of the scroll margin, then move it to
13125 either that ypos or PT, whichever comes first. */
13126 start_display (&it, w, startp);
13127 scroll_margin_y = it.last_visible_y - this_scroll_margin
13128 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13129 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13130 (MOVE_TO_POS | MOVE_TO_Y));
13131
13132 if (PT > CHARPOS (it.current.pos))
13133 {
13134 int y0 = line_bottom_y (&it);
13135 /* Compute how many pixels below window bottom to stop searching
13136 for PT. This avoids costly search for PT that is far away if
13137 the user limited scrolling by a small number of lines, but
13138 always finds PT if scroll_conservatively is set to a large
13139 number, such as most-positive-fixnum. */
13140 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13141 int y_to_move =
13142 slack >= INT_MAX - it.last_visible_y
13143 ? INT_MAX
13144 : it.last_visible_y + slack;
13145
13146 /* Compute the distance from the scroll margin to PT or to
13147 the scroll limit, whichever comes first. This should
13148 include the height of the cursor line, to make that line
13149 fully visible. */
13150 move_it_to (&it, PT, -1, y_to_move,
13151 -1, MOVE_TO_POS | MOVE_TO_Y);
13152 dy = line_bottom_y (&it) - y0;
13153
13154 if (dy > scroll_max)
13155 return SCROLLING_FAILED;
13156
13157 scroll_down_p = 1;
13158 }
13159 }
13160
13161 if (scroll_down_p)
13162 {
13163 /* Point is in or below the bottom scroll margin, so move the
13164 window start down. If scrolling conservatively, move it just
13165 enough down to make point visible. If scroll_step is set,
13166 move it down by scroll_step. */
13167 if (scroll_conservatively)
13168 amount_to_scroll
13169 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13170 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13171 else if (scroll_step || temp_scroll_step)
13172 amount_to_scroll = scroll_max;
13173 else
13174 {
13175 aggressive = current_buffer->scroll_up_aggressively;
13176 height = WINDOW_BOX_TEXT_HEIGHT (w);
13177 if (NUMBERP (aggressive))
13178 {
13179 double float_amount = XFLOATINT (aggressive) * height;
13180 amount_to_scroll = float_amount;
13181 if (amount_to_scroll == 0 && float_amount > 0)
13182 amount_to_scroll = 1;
13183 }
13184 }
13185
13186 if (amount_to_scroll <= 0)
13187 return SCROLLING_FAILED;
13188
13189 start_display (&it, w, startp);
13190 if (scroll_max < INT_MAX)
13191 move_it_vertically (&it, amount_to_scroll);
13192 else
13193 {
13194 /* Extra precision for users who set scroll-conservatively
13195 to most-positive-fixnum: make sure the amount we scroll
13196 the window start is never less than amount_to_scroll,
13197 which was computed as distance from window bottom to
13198 point. This matters when lines at window top and lines
13199 below window bottom have different height. */
13200 struct it it1 = it;
13201 /* We use a temporary it1 because line_bottom_y can modify
13202 its argument, if it moves one line down; see there. */
13203 int start_y = line_bottom_y (&it1);
13204
13205 do {
13206 move_it_by_lines (&it, 1, 1);
13207 it1 = it;
13208 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13209 }
13210
13211 /* If STARTP is unchanged, move it down another screen line. */
13212 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13213 move_it_by_lines (&it, 1, 1);
13214 startp = it.current.pos;
13215 }
13216 else
13217 {
13218 struct text_pos scroll_margin_pos = startp;
13219
13220 /* See if point is inside the scroll margin at the top of the
13221 window. */
13222 if (this_scroll_margin)
13223 {
13224 start_display (&it, w, startp);
13225 move_it_vertically (&it, this_scroll_margin);
13226 scroll_margin_pos = it.current.pos;
13227 }
13228
13229 if (PT < CHARPOS (scroll_margin_pos))
13230 {
13231 /* Point is in the scroll margin at the top of the window or
13232 above what is displayed in the window. */
13233 int y0;
13234
13235 /* Compute the vertical distance from PT to the scroll
13236 margin position. Give up if distance is greater than
13237 scroll_max. */
13238 SET_TEXT_POS (pos, PT, PT_BYTE);
13239 start_display (&it, w, pos);
13240 y0 = it.current_y;
13241 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13242 it.last_visible_y, -1,
13243 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13244 dy = it.current_y - y0;
13245 if (dy > scroll_max)
13246 return SCROLLING_FAILED;
13247
13248 /* Compute new window start. */
13249 start_display (&it, w, startp);
13250
13251 if (scroll_conservatively)
13252 amount_to_scroll
13253 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13254 else if (scroll_step || temp_scroll_step)
13255 amount_to_scroll = scroll_max;
13256 else
13257 {
13258 aggressive = current_buffer->scroll_down_aggressively;
13259 height = WINDOW_BOX_TEXT_HEIGHT (w);
13260 if (NUMBERP (aggressive))
13261 {
13262 double float_amount = XFLOATINT (aggressive) * height;
13263 amount_to_scroll = float_amount;
13264 if (amount_to_scroll == 0 && float_amount > 0)
13265 amount_to_scroll = 1;
13266 }
13267 }
13268
13269 if (amount_to_scroll <= 0)
13270 return SCROLLING_FAILED;
13271
13272 move_it_vertically_backward (&it, amount_to_scroll);
13273 startp = it.current.pos;
13274 }
13275 }
13276
13277 /* Run window scroll functions. */
13278 startp = run_window_scroll_functions (window, startp);
13279
13280 /* Display the window. Give up if new fonts are loaded, or if point
13281 doesn't appear. */
13282 if (!try_window (window, startp, 0))
13283 rc = SCROLLING_NEED_LARGER_MATRICES;
13284 else if (w->cursor.vpos < 0)
13285 {
13286 clear_glyph_matrix (w->desired_matrix);
13287 rc = SCROLLING_FAILED;
13288 }
13289 else
13290 {
13291 /* Maybe forget recorded base line for line number display. */
13292 if (!just_this_one_p
13293 || current_buffer->clip_changed
13294 || BEG_UNCHANGED < CHARPOS (startp))
13295 w->base_line_number = Qnil;
13296
13297 /* If cursor ends up on a partially visible line,
13298 treat that as being off the bottom of the screen. */
13299 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13300 {
13301 clear_glyph_matrix (w->desired_matrix);
13302 ++extra_scroll_margin_lines;
13303 goto too_near_end;
13304 }
13305 rc = SCROLLING_SUCCESS;
13306 }
13307
13308 return rc;
13309 }
13310
13311
13312 /* Compute a suitable window start for window W if display of W starts
13313 on a continuation line. Value is non-zero if a new window start
13314 was computed.
13315
13316 The new window start will be computed, based on W's width, starting
13317 from the start of the continued line. It is the start of the
13318 screen line with the minimum distance from the old start W->start. */
13319
13320 static int
13321 compute_window_start_on_continuation_line (struct window *w)
13322 {
13323 struct text_pos pos, start_pos;
13324 int window_start_changed_p = 0;
13325
13326 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13327
13328 /* If window start is on a continuation line... Window start may be
13329 < BEGV in case there's invisible text at the start of the
13330 buffer (M-x rmail, for example). */
13331 if (CHARPOS (start_pos) > BEGV
13332 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13333 {
13334 struct it it;
13335 struct glyph_row *row;
13336
13337 /* Handle the case that the window start is out of range. */
13338 if (CHARPOS (start_pos) < BEGV)
13339 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13340 else if (CHARPOS (start_pos) > ZV)
13341 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13342
13343 /* Find the start of the continued line. This should be fast
13344 because scan_buffer is fast (newline cache). */
13345 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13346 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13347 row, DEFAULT_FACE_ID);
13348 reseat_at_previous_visible_line_start (&it);
13349
13350 /* If the line start is "too far" away from the window start,
13351 say it takes too much time to compute a new window start. */
13352 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13353 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13354 {
13355 int min_distance, distance;
13356
13357 /* Move forward by display lines to find the new window
13358 start. If window width was enlarged, the new start can
13359 be expected to be > the old start. If window width was
13360 decreased, the new window start will be < the old start.
13361 So, we're looking for the display line start with the
13362 minimum distance from the old window start. */
13363 pos = it.current.pos;
13364 min_distance = INFINITY;
13365 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13366 distance < min_distance)
13367 {
13368 min_distance = distance;
13369 pos = it.current.pos;
13370 move_it_by_lines (&it, 1, 0);
13371 }
13372
13373 /* Set the window start there. */
13374 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13375 window_start_changed_p = 1;
13376 }
13377 }
13378
13379 return window_start_changed_p;
13380 }
13381
13382
13383 /* Try cursor movement in case text has not changed in window WINDOW,
13384 with window start STARTP. Value is
13385
13386 CURSOR_MOVEMENT_SUCCESS if successful
13387
13388 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13389
13390 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13391 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13392 we want to scroll as if scroll-step were set to 1. See the code.
13393
13394 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13395 which case we have to abort this redisplay, and adjust matrices
13396 first. */
13397
13398 enum
13399 {
13400 CURSOR_MOVEMENT_SUCCESS,
13401 CURSOR_MOVEMENT_CANNOT_BE_USED,
13402 CURSOR_MOVEMENT_MUST_SCROLL,
13403 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13404 };
13405
13406 static int
13407 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13408 {
13409 struct window *w = XWINDOW (window);
13410 struct frame *f = XFRAME (w->frame);
13411 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13412
13413 #if GLYPH_DEBUG
13414 if (inhibit_try_cursor_movement)
13415 return rc;
13416 #endif
13417
13418 /* Handle case where text has not changed, only point, and it has
13419 not moved off the frame. */
13420 if (/* Point may be in this window. */
13421 PT >= CHARPOS (startp)
13422 /* Selective display hasn't changed. */
13423 && !current_buffer->clip_changed
13424 /* Function force-mode-line-update is used to force a thorough
13425 redisplay. It sets either windows_or_buffers_changed or
13426 update_mode_lines. So don't take a shortcut here for these
13427 cases. */
13428 && !update_mode_lines
13429 && !windows_or_buffers_changed
13430 && !cursor_type_changed
13431 /* Can't use this case if highlighting a region. When a
13432 region exists, cursor movement has to do more than just
13433 set the cursor. */
13434 && !(!NILP (Vtransient_mark_mode)
13435 && !NILP (current_buffer->mark_active))
13436 && NILP (w->region_showing)
13437 && NILP (Vshow_trailing_whitespace)
13438 /* Right after splitting windows, last_point may be nil. */
13439 && INTEGERP (w->last_point)
13440 /* This code is not used for mini-buffer for the sake of the case
13441 of redisplaying to replace an echo area message; since in
13442 that case the mini-buffer contents per se are usually
13443 unchanged. This code is of no real use in the mini-buffer
13444 since the handling of this_line_start_pos, etc., in redisplay
13445 handles the same cases. */
13446 && !EQ (window, minibuf_window)
13447 /* When splitting windows or for new windows, it happens that
13448 redisplay is called with a nil window_end_vpos or one being
13449 larger than the window. This should really be fixed in
13450 window.c. I don't have this on my list, now, so we do
13451 approximately the same as the old redisplay code. --gerd. */
13452 && INTEGERP (w->window_end_vpos)
13453 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13454 && (FRAME_WINDOW_P (f)
13455 || !overlay_arrow_in_current_buffer_p ()))
13456 {
13457 int this_scroll_margin, top_scroll_margin;
13458 struct glyph_row *row = NULL;
13459
13460 #if GLYPH_DEBUG
13461 debug_method_add (w, "cursor movement");
13462 #endif
13463
13464 /* Scroll if point within this distance from the top or bottom
13465 of the window. This is a pixel value. */
13466 if (scroll_margin > 0)
13467 {
13468 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13469 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13470 }
13471 else
13472 this_scroll_margin = 0;
13473
13474 top_scroll_margin = this_scroll_margin;
13475 if (WINDOW_WANTS_HEADER_LINE_P (w))
13476 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13477
13478 /* Start with the row the cursor was displayed during the last
13479 not paused redisplay. Give up if that row is not valid. */
13480 if (w->last_cursor.vpos < 0
13481 || w->last_cursor.vpos >= w->current_matrix->nrows)
13482 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13483 else
13484 {
13485 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13486 if (row->mode_line_p)
13487 ++row;
13488 if (!row->enabled_p)
13489 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13490 }
13491
13492 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13493 {
13494 int scroll_p = 0, must_scroll = 0;
13495 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13496
13497 if (PT > XFASTINT (w->last_point))
13498 {
13499 /* Point has moved forward. */
13500 while (MATRIX_ROW_END_CHARPOS (row) < PT
13501 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13502 {
13503 xassert (row->enabled_p);
13504 ++row;
13505 }
13506
13507 /* If the end position of a row equals the start
13508 position of the next row, and PT is at that position,
13509 we would rather display cursor in the next line. */
13510 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13511 && MATRIX_ROW_END_CHARPOS (row) == PT
13512 && row < w->current_matrix->rows
13513 + w->current_matrix->nrows - 1
13514 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13515 && !cursor_row_p (w, row))
13516 ++row;
13517
13518 /* If within the scroll margin, scroll. Note that
13519 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13520 the next line would be drawn, and that
13521 this_scroll_margin can be zero. */
13522 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13523 || PT > MATRIX_ROW_END_CHARPOS (row)
13524 /* Line is completely visible last line in window
13525 and PT is to be set in the next line. */
13526 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13527 && PT == MATRIX_ROW_END_CHARPOS (row)
13528 && !row->ends_at_zv_p
13529 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13530 scroll_p = 1;
13531 }
13532 else if (PT < XFASTINT (w->last_point))
13533 {
13534 /* Cursor has to be moved backward. Note that PT >=
13535 CHARPOS (startp) because of the outer if-statement. */
13536 while (!row->mode_line_p
13537 && (MATRIX_ROW_START_CHARPOS (row) > PT
13538 || (MATRIX_ROW_START_CHARPOS (row) == PT
13539 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13540 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13541 row > w->current_matrix->rows
13542 && (row-1)->ends_in_newline_from_string_p))))
13543 && (row->y > top_scroll_margin
13544 || CHARPOS (startp) == BEGV))
13545 {
13546 xassert (row->enabled_p);
13547 --row;
13548 }
13549
13550 /* Consider the following case: Window starts at BEGV,
13551 there is invisible, intangible text at BEGV, so that
13552 display starts at some point START > BEGV. It can
13553 happen that we are called with PT somewhere between
13554 BEGV and START. Try to handle that case. */
13555 if (row < w->current_matrix->rows
13556 || row->mode_line_p)
13557 {
13558 row = w->current_matrix->rows;
13559 if (row->mode_line_p)
13560 ++row;
13561 }
13562
13563 /* Due to newlines in overlay strings, we may have to
13564 skip forward over overlay strings. */
13565 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13566 && MATRIX_ROW_END_CHARPOS (row) == PT
13567 && !cursor_row_p (w, row))
13568 ++row;
13569
13570 /* If within the scroll margin, scroll. */
13571 if (row->y < top_scroll_margin
13572 && CHARPOS (startp) != BEGV)
13573 scroll_p = 1;
13574 }
13575 else
13576 {
13577 /* Cursor did not move. So don't scroll even if cursor line
13578 is partially visible, as it was so before. */
13579 rc = CURSOR_MOVEMENT_SUCCESS;
13580 }
13581
13582 if (PT < MATRIX_ROW_START_CHARPOS (row)
13583 || PT > MATRIX_ROW_END_CHARPOS (row))
13584 {
13585 /* if PT is not in the glyph row, give up. */
13586 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13587 must_scroll = 1;
13588 }
13589 else if (rc != CURSOR_MOVEMENT_SUCCESS
13590 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13591 {
13592 /* If rows are bidi-reordered and point moved, back up
13593 until we find a row that does not belong to a
13594 continuation line. This is because we must consider
13595 all rows of a continued line as candidates for the
13596 new cursor positioning, since row start and end
13597 positions change non-linearly with vertical position
13598 in such rows. */
13599 /* FIXME: Revisit this when glyph ``spilling'' in
13600 continuation lines' rows is implemented for
13601 bidi-reordered rows. */
13602 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13603 {
13604 xassert (row->enabled_p);
13605 --row;
13606 /* If we hit the beginning of the displayed portion
13607 without finding the first row of a continued
13608 line, give up. */
13609 if (row <= w->current_matrix->rows)
13610 {
13611 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13612 break;
13613 }
13614
13615 }
13616 }
13617 if (must_scroll)
13618 ;
13619 else if (rc != CURSOR_MOVEMENT_SUCCESS
13620 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13621 && make_cursor_line_fully_visible_p)
13622 {
13623 if (PT == MATRIX_ROW_END_CHARPOS (row)
13624 && !row->ends_at_zv_p
13625 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13626 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13627 else if (row->height > window_box_height (w))
13628 {
13629 /* If we end up in a partially visible line, let's
13630 make it fully visible, except when it's taller
13631 than the window, in which case we can't do much
13632 about it. */
13633 *scroll_step = 1;
13634 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13635 }
13636 else
13637 {
13638 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13639 if (!cursor_row_fully_visible_p (w, 0, 1))
13640 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13641 else
13642 rc = CURSOR_MOVEMENT_SUCCESS;
13643 }
13644 }
13645 else if (scroll_p)
13646 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13647 else if (rc != CURSOR_MOVEMENT_SUCCESS
13648 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13649 {
13650 /* With bidi-reordered rows, there could be more than
13651 one candidate row whose start and end positions
13652 occlude point. We need to let set_cursor_from_row
13653 find the best candidate. */
13654 /* FIXME: Revisit this when glyph ``spilling'' in
13655 continuation lines' rows is implemented for
13656 bidi-reordered rows. */
13657 int rv = 0;
13658
13659 do
13660 {
13661 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13662 && PT <= MATRIX_ROW_END_CHARPOS (row)
13663 && cursor_row_p (w, row))
13664 rv |= set_cursor_from_row (w, row, w->current_matrix,
13665 0, 0, 0, 0);
13666 /* As soon as we've found the first suitable row
13667 whose ends_at_zv_p flag is set, we are done. */
13668 if (rv
13669 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13670 {
13671 rc = CURSOR_MOVEMENT_SUCCESS;
13672 break;
13673 }
13674 ++row;
13675 }
13676 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
13677 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13678 || (MATRIX_ROW_START_CHARPOS (row) == PT
13679 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
13680 /* If we didn't find any candidate rows, or exited the
13681 loop before all the candidates were examined, signal
13682 to the caller that this method failed. */
13683 if (rc != CURSOR_MOVEMENT_SUCCESS
13684 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
13685 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13686 else if (rv)
13687 rc = CURSOR_MOVEMENT_SUCCESS;
13688 }
13689 else
13690 {
13691 do
13692 {
13693 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13694 {
13695 rc = CURSOR_MOVEMENT_SUCCESS;
13696 break;
13697 }
13698 ++row;
13699 }
13700 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13701 && MATRIX_ROW_START_CHARPOS (row) == PT
13702 && cursor_row_p (w, row));
13703 }
13704 }
13705 }
13706
13707 return rc;
13708 }
13709
13710 void
13711 set_vertical_scroll_bar (struct window *w)
13712 {
13713 int start, end, whole;
13714
13715 /* Calculate the start and end positions for the current window.
13716 At some point, it would be nice to choose between scrollbars
13717 which reflect the whole buffer size, with special markers
13718 indicating narrowing, and scrollbars which reflect only the
13719 visible region.
13720
13721 Note that mini-buffers sometimes aren't displaying any text. */
13722 if (!MINI_WINDOW_P (w)
13723 || (w == XWINDOW (minibuf_window)
13724 && NILP (echo_area_buffer[0])))
13725 {
13726 struct buffer *buf = XBUFFER (w->buffer);
13727 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13728 start = marker_position (w->start) - BUF_BEGV (buf);
13729 /* I don't think this is guaranteed to be right. For the
13730 moment, we'll pretend it is. */
13731 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13732
13733 if (end < start)
13734 end = start;
13735 if (whole < (end - start))
13736 whole = end - start;
13737 }
13738 else
13739 start = end = whole = 0;
13740
13741 /* Indicate what this scroll bar ought to be displaying now. */
13742 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13743 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13744 (w, end - start, whole, start);
13745 }
13746
13747
13748 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13749 selected_window is redisplayed.
13750
13751 We can return without actually redisplaying the window if
13752 fonts_changed_p is nonzero. In that case, redisplay_internal will
13753 retry. */
13754
13755 static void
13756 redisplay_window (Lisp_Object window, int just_this_one_p)
13757 {
13758 struct window *w = XWINDOW (window);
13759 struct frame *f = XFRAME (w->frame);
13760 struct buffer *buffer = XBUFFER (w->buffer);
13761 struct buffer *old = current_buffer;
13762 struct text_pos lpoint, opoint, startp;
13763 int update_mode_line;
13764 int tem;
13765 struct it it;
13766 /* Record it now because it's overwritten. */
13767 int current_matrix_up_to_date_p = 0;
13768 int used_current_matrix_p = 0;
13769 /* This is less strict than current_matrix_up_to_date_p.
13770 It indictes that the buffer contents and narrowing are unchanged. */
13771 int buffer_unchanged_p = 0;
13772 int temp_scroll_step = 0;
13773 int count = SPECPDL_INDEX ();
13774 int rc;
13775 int centering_position = -1;
13776 int last_line_misfit = 0;
13777 int beg_unchanged, end_unchanged;
13778
13779 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13780 opoint = lpoint;
13781
13782 /* W must be a leaf window here. */
13783 xassert (!NILP (w->buffer));
13784 #if GLYPH_DEBUG
13785 *w->desired_matrix->method = 0;
13786 #endif
13787
13788 restart:
13789 reconsider_clip_changes (w, buffer);
13790
13791 /* Has the mode line to be updated? */
13792 update_mode_line = (!NILP (w->update_mode_line)
13793 || update_mode_lines
13794 || buffer->clip_changed
13795 || buffer->prevent_redisplay_optimizations_p);
13796
13797 if (MINI_WINDOW_P (w))
13798 {
13799 if (w == XWINDOW (echo_area_window)
13800 && !NILP (echo_area_buffer[0]))
13801 {
13802 if (update_mode_line)
13803 /* We may have to update a tty frame's menu bar or a
13804 tool-bar. Example `M-x C-h C-h C-g'. */
13805 goto finish_menu_bars;
13806 else
13807 /* We've already displayed the echo area glyphs in this window. */
13808 goto finish_scroll_bars;
13809 }
13810 else if ((w != XWINDOW (minibuf_window)
13811 || minibuf_level == 0)
13812 /* When buffer is nonempty, redisplay window normally. */
13813 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13814 /* Quail displays non-mini buffers in minibuffer window.
13815 In that case, redisplay the window normally. */
13816 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13817 {
13818 /* W is a mini-buffer window, but it's not active, so clear
13819 it. */
13820 int yb = window_text_bottom_y (w);
13821 struct glyph_row *row;
13822 int y;
13823
13824 for (y = 0, row = w->desired_matrix->rows;
13825 y < yb;
13826 y += row->height, ++row)
13827 blank_row (w, row, y);
13828 goto finish_scroll_bars;
13829 }
13830
13831 clear_glyph_matrix (w->desired_matrix);
13832 }
13833
13834 /* Otherwise set up data on this window; select its buffer and point
13835 value. */
13836 /* Really select the buffer, for the sake of buffer-local
13837 variables. */
13838 set_buffer_internal_1 (XBUFFER (w->buffer));
13839
13840 current_matrix_up_to_date_p
13841 = (!NILP (w->window_end_valid)
13842 && !current_buffer->clip_changed
13843 && !current_buffer->prevent_redisplay_optimizations_p
13844 && XFASTINT (w->last_modified) >= MODIFF
13845 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13846
13847 /* Run the window-bottom-change-functions
13848 if it is possible that the text on the screen has changed
13849 (either due to modification of the text, or any other reason). */
13850 if (!current_matrix_up_to_date_p
13851 && !NILP (Vwindow_text_change_functions))
13852 {
13853 safe_run_hooks (Qwindow_text_change_functions);
13854 goto restart;
13855 }
13856
13857 beg_unchanged = BEG_UNCHANGED;
13858 end_unchanged = END_UNCHANGED;
13859
13860 SET_TEXT_POS (opoint, PT, PT_BYTE);
13861
13862 specbind (Qinhibit_point_motion_hooks, Qt);
13863
13864 buffer_unchanged_p
13865 = (!NILP (w->window_end_valid)
13866 && !current_buffer->clip_changed
13867 && XFASTINT (w->last_modified) >= MODIFF
13868 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13869
13870 /* When windows_or_buffers_changed is non-zero, we can't rely on
13871 the window end being valid, so set it to nil there. */
13872 if (windows_or_buffers_changed)
13873 {
13874 /* If window starts on a continuation line, maybe adjust the
13875 window start in case the window's width changed. */
13876 if (XMARKER (w->start)->buffer == current_buffer)
13877 compute_window_start_on_continuation_line (w);
13878
13879 w->window_end_valid = Qnil;
13880 }
13881
13882 /* Some sanity checks. */
13883 CHECK_WINDOW_END (w);
13884 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13885 abort ();
13886 if (BYTEPOS (opoint) < CHARPOS (opoint))
13887 abort ();
13888
13889 /* If %c is in mode line, update it if needed. */
13890 if (!NILP (w->column_number_displayed)
13891 /* This alternative quickly identifies a common case
13892 where no change is needed. */
13893 && !(PT == XFASTINT (w->last_point)
13894 && XFASTINT (w->last_modified) >= MODIFF
13895 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13896 && (XFASTINT (w->column_number_displayed)
13897 != (int) current_column ())) /* iftc */
13898 update_mode_line = 1;
13899
13900 /* Count number of windows showing the selected buffer. An indirect
13901 buffer counts as its base buffer. */
13902 if (!just_this_one_p)
13903 {
13904 struct buffer *current_base, *window_base;
13905 current_base = current_buffer;
13906 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13907 if (current_base->base_buffer)
13908 current_base = current_base->base_buffer;
13909 if (window_base->base_buffer)
13910 window_base = window_base->base_buffer;
13911 if (current_base == window_base)
13912 buffer_shared++;
13913 }
13914
13915 /* Point refers normally to the selected window. For any other
13916 window, set up appropriate value. */
13917 if (!EQ (window, selected_window))
13918 {
13919 int new_pt = XMARKER (w->pointm)->charpos;
13920 int new_pt_byte = marker_byte_position (w->pointm);
13921 if (new_pt < BEGV)
13922 {
13923 new_pt = BEGV;
13924 new_pt_byte = BEGV_BYTE;
13925 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13926 }
13927 else if (new_pt > (ZV - 1))
13928 {
13929 new_pt = ZV;
13930 new_pt_byte = ZV_BYTE;
13931 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13932 }
13933
13934 /* We don't use SET_PT so that the point-motion hooks don't run. */
13935 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13936 }
13937
13938 /* If any of the character widths specified in the display table
13939 have changed, invalidate the width run cache. It's true that
13940 this may be a bit late to catch such changes, but the rest of
13941 redisplay goes (non-fatally) haywire when the display table is
13942 changed, so why should we worry about doing any better? */
13943 if (current_buffer->width_run_cache)
13944 {
13945 struct Lisp_Char_Table *disptab = buffer_display_table ();
13946
13947 if (! disptab_matches_widthtab (disptab,
13948 XVECTOR (current_buffer->width_table)))
13949 {
13950 invalidate_region_cache (current_buffer,
13951 current_buffer->width_run_cache,
13952 BEG, Z);
13953 recompute_width_table (current_buffer, disptab);
13954 }
13955 }
13956
13957 /* If window-start is screwed up, choose a new one. */
13958 if (XMARKER (w->start)->buffer != current_buffer)
13959 goto recenter;
13960
13961 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13962
13963 /* If someone specified a new starting point but did not insist,
13964 check whether it can be used. */
13965 if (!NILP (w->optional_new_start)
13966 && CHARPOS (startp) >= BEGV
13967 && CHARPOS (startp) <= ZV)
13968 {
13969 w->optional_new_start = Qnil;
13970 start_display (&it, w, startp);
13971 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13972 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13973 if (IT_CHARPOS (it) == PT)
13974 w->force_start = Qt;
13975 /* IT may overshoot PT if text at PT is invisible. */
13976 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13977 w->force_start = Qt;
13978 }
13979
13980 force_start:
13981
13982 /* Handle case where place to start displaying has been specified,
13983 unless the specified location is outside the accessible range. */
13984 if (!NILP (w->force_start)
13985 || w->frozen_window_start_p)
13986 {
13987 /* We set this later on if we have to adjust point. */
13988 int new_vpos = -1;
13989
13990 w->force_start = Qnil;
13991 w->vscroll = 0;
13992 w->window_end_valid = Qnil;
13993
13994 /* Forget any recorded base line for line number display. */
13995 if (!buffer_unchanged_p)
13996 w->base_line_number = Qnil;
13997
13998 /* Redisplay the mode line. Select the buffer properly for that.
13999 Also, run the hook window-scroll-functions
14000 because we have scrolled. */
14001 /* Note, we do this after clearing force_start because
14002 if there's an error, it is better to forget about force_start
14003 than to get into an infinite loop calling the hook functions
14004 and having them get more errors. */
14005 if (!update_mode_line
14006 || ! NILP (Vwindow_scroll_functions))
14007 {
14008 update_mode_line = 1;
14009 w->update_mode_line = Qt;
14010 startp = run_window_scroll_functions (window, startp);
14011 }
14012
14013 w->last_modified = make_number (0);
14014 w->last_overlay_modified = make_number (0);
14015 if (CHARPOS (startp) < BEGV)
14016 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14017 else if (CHARPOS (startp) > ZV)
14018 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14019
14020 /* Redisplay, then check if cursor has been set during the
14021 redisplay. Give up if new fonts were loaded. */
14022 /* We used to issue a CHECK_MARGINS argument to try_window here,
14023 but this causes scrolling to fail when point begins inside
14024 the scroll margin (bug#148) -- cyd */
14025 if (!try_window (window, startp, 0))
14026 {
14027 w->force_start = Qt;
14028 clear_glyph_matrix (w->desired_matrix);
14029 goto need_larger_matrices;
14030 }
14031
14032 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14033 {
14034 /* If point does not appear, try to move point so it does
14035 appear. The desired matrix has been built above, so we
14036 can use it here. */
14037 new_vpos = window_box_height (w) / 2;
14038 }
14039
14040 if (!cursor_row_fully_visible_p (w, 0, 0))
14041 {
14042 /* Point does appear, but on a line partly visible at end of window.
14043 Move it back to a fully-visible line. */
14044 new_vpos = window_box_height (w);
14045 }
14046
14047 /* If we need to move point for either of the above reasons,
14048 now actually do it. */
14049 if (new_vpos >= 0)
14050 {
14051 struct glyph_row *row;
14052
14053 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14054 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14055 ++row;
14056
14057 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14058 MATRIX_ROW_START_BYTEPOS (row));
14059
14060 if (w != XWINDOW (selected_window))
14061 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14062 else if (current_buffer == old)
14063 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14064
14065 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14066
14067 /* If we are highlighting the region, then we just changed
14068 the region, so redisplay to show it. */
14069 if (!NILP (Vtransient_mark_mode)
14070 && !NILP (current_buffer->mark_active))
14071 {
14072 clear_glyph_matrix (w->desired_matrix);
14073 if (!try_window (window, startp, 0))
14074 goto need_larger_matrices;
14075 }
14076 }
14077
14078 #if GLYPH_DEBUG
14079 debug_method_add (w, "forced window start");
14080 #endif
14081 goto done;
14082 }
14083
14084 /* Handle case where text has not changed, only point, and it has
14085 not moved off the frame, and we are not retrying after hscroll.
14086 (current_matrix_up_to_date_p is nonzero when retrying.) */
14087 if (current_matrix_up_to_date_p
14088 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14089 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14090 {
14091 switch (rc)
14092 {
14093 case CURSOR_MOVEMENT_SUCCESS:
14094 used_current_matrix_p = 1;
14095 goto done;
14096
14097 case CURSOR_MOVEMENT_MUST_SCROLL:
14098 goto try_to_scroll;
14099
14100 default:
14101 abort ();
14102 }
14103 }
14104 /* If current starting point was originally the beginning of a line
14105 but no longer is, find a new starting point. */
14106 else if (!NILP (w->start_at_line_beg)
14107 && !(CHARPOS (startp) <= BEGV
14108 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14109 {
14110 #if GLYPH_DEBUG
14111 debug_method_add (w, "recenter 1");
14112 #endif
14113 goto recenter;
14114 }
14115
14116 /* Try scrolling with try_window_id. Value is > 0 if update has
14117 been done, it is -1 if we know that the same window start will
14118 not work. It is 0 if unsuccessful for some other reason. */
14119 else if ((tem = try_window_id (w)) != 0)
14120 {
14121 #if GLYPH_DEBUG
14122 debug_method_add (w, "try_window_id %d", tem);
14123 #endif
14124
14125 if (fonts_changed_p)
14126 goto need_larger_matrices;
14127 if (tem > 0)
14128 goto done;
14129
14130 /* Otherwise try_window_id has returned -1 which means that we
14131 don't want the alternative below this comment to execute. */
14132 }
14133 else if (CHARPOS (startp) >= BEGV
14134 && CHARPOS (startp) <= ZV
14135 && PT >= CHARPOS (startp)
14136 && (CHARPOS (startp) < ZV
14137 /* Avoid starting at end of buffer. */
14138 || CHARPOS (startp) == BEGV
14139 || (XFASTINT (w->last_modified) >= MODIFF
14140 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14141 {
14142
14143 /* If first window line is a continuation line, and window start
14144 is inside the modified region, but the first change is before
14145 current window start, we must select a new window start.
14146
14147 However, if this is the result of a down-mouse event (e.g. by
14148 extending the mouse-drag-overlay), we don't want to select a
14149 new window start, since that would change the position under
14150 the mouse, resulting in an unwanted mouse-movement rather
14151 than a simple mouse-click. */
14152 if (NILP (w->start_at_line_beg)
14153 && NILP (do_mouse_tracking)
14154 && CHARPOS (startp) > BEGV
14155 && CHARPOS (startp) > BEG + beg_unchanged
14156 && CHARPOS (startp) <= Z - end_unchanged
14157 /* Even if w->start_at_line_beg is nil, a new window may
14158 start at a line_beg, since that's how set_buffer_window
14159 sets it. So, we need to check the return value of
14160 compute_window_start_on_continuation_line. (See also
14161 bug#197). */
14162 && XMARKER (w->start)->buffer == current_buffer
14163 && compute_window_start_on_continuation_line (w))
14164 {
14165 w->force_start = Qt;
14166 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14167 goto force_start;
14168 }
14169
14170 #if GLYPH_DEBUG
14171 debug_method_add (w, "same window start");
14172 #endif
14173
14174 /* Try to redisplay starting at same place as before.
14175 If point has not moved off frame, accept the results. */
14176 if (!current_matrix_up_to_date_p
14177 /* Don't use try_window_reusing_current_matrix in this case
14178 because a window scroll function can have changed the
14179 buffer. */
14180 || !NILP (Vwindow_scroll_functions)
14181 || MINI_WINDOW_P (w)
14182 || !(used_current_matrix_p
14183 = try_window_reusing_current_matrix (w)))
14184 {
14185 IF_DEBUG (debug_method_add (w, "1"));
14186 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14187 /* -1 means we need to scroll.
14188 0 means we need new matrices, but fonts_changed_p
14189 is set in that case, so we will detect it below. */
14190 goto try_to_scroll;
14191 }
14192
14193 if (fonts_changed_p)
14194 goto need_larger_matrices;
14195
14196 if (w->cursor.vpos >= 0)
14197 {
14198 if (!just_this_one_p
14199 || current_buffer->clip_changed
14200 || BEG_UNCHANGED < CHARPOS (startp))
14201 /* Forget any recorded base line for line number display. */
14202 w->base_line_number = Qnil;
14203
14204 if (!cursor_row_fully_visible_p (w, 1, 0))
14205 {
14206 clear_glyph_matrix (w->desired_matrix);
14207 last_line_misfit = 1;
14208 }
14209 /* Drop through and scroll. */
14210 else
14211 goto done;
14212 }
14213 else
14214 clear_glyph_matrix (w->desired_matrix);
14215 }
14216
14217 try_to_scroll:
14218
14219 w->last_modified = make_number (0);
14220 w->last_overlay_modified = make_number (0);
14221
14222 /* Redisplay the mode line. Select the buffer properly for that. */
14223 if (!update_mode_line)
14224 {
14225 update_mode_line = 1;
14226 w->update_mode_line = Qt;
14227 }
14228
14229 /* Try to scroll by specified few lines. */
14230 if ((scroll_conservatively
14231 || scroll_step
14232 || temp_scroll_step
14233 || NUMBERP (current_buffer->scroll_up_aggressively)
14234 || NUMBERP (current_buffer->scroll_down_aggressively))
14235 && !current_buffer->clip_changed
14236 && CHARPOS (startp) >= BEGV
14237 && CHARPOS (startp) <= ZV)
14238 {
14239 /* The function returns -1 if new fonts were loaded, 1 if
14240 successful, 0 if not successful. */
14241 int rc = try_scrolling (window, just_this_one_p,
14242 scroll_conservatively,
14243 scroll_step,
14244 temp_scroll_step, last_line_misfit);
14245 switch (rc)
14246 {
14247 case SCROLLING_SUCCESS:
14248 goto done;
14249
14250 case SCROLLING_NEED_LARGER_MATRICES:
14251 goto need_larger_matrices;
14252
14253 case SCROLLING_FAILED:
14254 break;
14255
14256 default:
14257 abort ();
14258 }
14259 }
14260
14261 /* Finally, just choose place to start which centers point */
14262
14263 recenter:
14264 if (centering_position < 0)
14265 centering_position = window_box_height (w) / 2;
14266
14267 #if GLYPH_DEBUG
14268 debug_method_add (w, "recenter");
14269 #endif
14270
14271 /* w->vscroll = 0; */
14272
14273 /* Forget any previously recorded base line for line number display. */
14274 if (!buffer_unchanged_p)
14275 w->base_line_number = Qnil;
14276
14277 /* Move backward half the height of the window. */
14278 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14279 it.current_y = it.last_visible_y;
14280 move_it_vertically_backward (&it, centering_position);
14281 xassert (IT_CHARPOS (it) >= BEGV);
14282
14283 /* The function move_it_vertically_backward may move over more
14284 than the specified y-distance. If it->w is small, e.g. a
14285 mini-buffer window, we may end up in front of the window's
14286 display area. Start displaying at the start of the line
14287 containing PT in this case. */
14288 if (it.current_y <= 0)
14289 {
14290 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14291 move_it_vertically_backward (&it, 0);
14292 it.current_y = 0;
14293 }
14294
14295 it.current_x = it.hpos = 0;
14296
14297 /* Set startp here explicitly in case that helps avoid an infinite loop
14298 in case the window-scroll-functions functions get errors. */
14299 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14300
14301 /* Run scroll hooks. */
14302 startp = run_window_scroll_functions (window, it.current.pos);
14303
14304 /* Redisplay the window. */
14305 if (!current_matrix_up_to_date_p
14306 || windows_or_buffers_changed
14307 || cursor_type_changed
14308 /* Don't use try_window_reusing_current_matrix in this case
14309 because it can have changed the buffer. */
14310 || !NILP (Vwindow_scroll_functions)
14311 || !just_this_one_p
14312 || MINI_WINDOW_P (w)
14313 || !(used_current_matrix_p
14314 = try_window_reusing_current_matrix (w)))
14315 try_window (window, startp, 0);
14316
14317 /* If new fonts have been loaded (due to fontsets), give up. We
14318 have to start a new redisplay since we need to re-adjust glyph
14319 matrices. */
14320 if (fonts_changed_p)
14321 goto need_larger_matrices;
14322
14323 /* If cursor did not appear assume that the middle of the window is
14324 in the first line of the window. Do it again with the next line.
14325 (Imagine a window of height 100, displaying two lines of height
14326 60. Moving back 50 from it->last_visible_y will end in the first
14327 line.) */
14328 if (w->cursor.vpos < 0)
14329 {
14330 if (!NILP (w->window_end_valid)
14331 && PT >= Z - XFASTINT (w->window_end_pos))
14332 {
14333 clear_glyph_matrix (w->desired_matrix);
14334 move_it_by_lines (&it, 1, 0);
14335 try_window (window, it.current.pos, 0);
14336 }
14337 else if (PT < IT_CHARPOS (it))
14338 {
14339 clear_glyph_matrix (w->desired_matrix);
14340 move_it_by_lines (&it, -1, 0);
14341 try_window (window, it.current.pos, 0);
14342 }
14343 else
14344 {
14345 /* Not much we can do about it. */
14346 }
14347 }
14348
14349 /* Consider the following case: Window starts at BEGV, there is
14350 invisible, intangible text at BEGV, so that display starts at
14351 some point START > BEGV. It can happen that we are called with
14352 PT somewhere between BEGV and START. Try to handle that case. */
14353 if (w->cursor.vpos < 0)
14354 {
14355 struct glyph_row *row = w->current_matrix->rows;
14356 if (row->mode_line_p)
14357 ++row;
14358 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14359 }
14360
14361 if (!cursor_row_fully_visible_p (w, 0, 0))
14362 {
14363 /* If vscroll is enabled, disable it and try again. */
14364 if (w->vscroll)
14365 {
14366 w->vscroll = 0;
14367 clear_glyph_matrix (w->desired_matrix);
14368 goto recenter;
14369 }
14370
14371 /* If centering point failed to make the whole line visible,
14372 put point at the top instead. That has to make the whole line
14373 visible, if it can be done. */
14374 if (centering_position == 0)
14375 goto done;
14376
14377 clear_glyph_matrix (w->desired_matrix);
14378 centering_position = 0;
14379 goto recenter;
14380 }
14381
14382 done:
14383
14384 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14385 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14386 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14387 ? Qt : Qnil);
14388
14389 /* Display the mode line, if we must. */
14390 if ((update_mode_line
14391 /* If window not full width, must redo its mode line
14392 if (a) the window to its side is being redone and
14393 (b) we do a frame-based redisplay. This is a consequence
14394 of how inverted lines are drawn in frame-based redisplay. */
14395 || (!just_this_one_p
14396 && !FRAME_WINDOW_P (f)
14397 && !WINDOW_FULL_WIDTH_P (w))
14398 /* Line number to display. */
14399 || INTEGERP (w->base_line_pos)
14400 /* Column number is displayed and different from the one displayed. */
14401 || (!NILP (w->column_number_displayed)
14402 && (XFASTINT (w->column_number_displayed)
14403 != (int) current_column ()))) /* iftc */
14404 /* This means that the window has a mode line. */
14405 && (WINDOW_WANTS_MODELINE_P (w)
14406 || WINDOW_WANTS_HEADER_LINE_P (w)))
14407 {
14408 display_mode_lines (w);
14409
14410 /* If mode line height has changed, arrange for a thorough
14411 immediate redisplay using the correct mode line height. */
14412 if (WINDOW_WANTS_MODELINE_P (w)
14413 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14414 {
14415 fonts_changed_p = 1;
14416 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14417 = DESIRED_MODE_LINE_HEIGHT (w);
14418 }
14419
14420 /* If header line height has changed, arrange for a thorough
14421 immediate redisplay using the correct header line height. */
14422 if (WINDOW_WANTS_HEADER_LINE_P (w)
14423 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14424 {
14425 fonts_changed_p = 1;
14426 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14427 = DESIRED_HEADER_LINE_HEIGHT (w);
14428 }
14429
14430 if (fonts_changed_p)
14431 goto need_larger_matrices;
14432 }
14433
14434 if (!line_number_displayed
14435 && !BUFFERP (w->base_line_pos))
14436 {
14437 w->base_line_pos = Qnil;
14438 w->base_line_number = Qnil;
14439 }
14440
14441 finish_menu_bars:
14442
14443 /* When we reach a frame's selected window, redo the frame's menu bar. */
14444 if (update_mode_line
14445 && EQ (FRAME_SELECTED_WINDOW (f), window))
14446 {
14447 int redisplay_menu_p = 0;
14448 int redisplay_tool_bar_p = 0;
14449
14450 if (FRAME_WINDOW_P (f))
14451 {
14452 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14453 || defined (HAVE_NS) || defined (USE_GTK)
14454 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14455 #else
14456 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14457 #endif
14458 }
14459 else
14460 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14461
14462 if (redisplay_menu_p)
14463 display_menu_bar (w);
14464
14465 #ifdef HAVE_WINDOW_SYSTEM
14466 if (FRAME_WINDOW_P (f))
14467 {
14468 #if defined (USE_GTK) || defined (HAVE_NS)
14469 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14470 #else
14471 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14472 && (FRAME_TOOL_BAR_LINES (f) > 0
14473 || !NILP (Vauto_resize_tool_bars));
14474 #endif
14475
14476 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14477 {
14478 ignore_mouse_drag_p = 1;
14479 }
14480 }
14481 #endif
14482 }
14483
14484 #ifdef HAVE_WINDOW_SYSTEM
14485 if (FRAME_WINDOW_P (f)
14486 && update_window_fringes (w, (just_this_one_p
14487 || (!used_current_matrix_p && !overlay_arrow_seen)
14488 || w->pseudo_window_p)))
14489 {
14490 update_begin (f);
14491 BLOCK_INPUT;
14492 if (draw_window_fringes (w, 1))
14493 x_draw_vertical_border (w);
14494 UNBLOCK_INPUT;
14495 update_end (f);
14496 }
14497 #endif /* HAVE_WINDOW_SYSTEM */
14498
14499 /* We go to this label, with fonts_changed_p nonzero,
14500 if it is necessary to try again using larger glyph matrices.
14501 We have to redeem the scroll bar even in this case,
14502 because the loop in redisplay_internal expects that. */
14503 need_larger_matrices:
14504 ;
14505 finish_scroll_bars:
14506
14507 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14508 {
14509 /* Set the thumb's position and size. */
14510 set_vertical_scroll_bar (w);
14511
14512 /* Note that we actually used the scroll bar attached to this
14513 window, so it shouldn't be deleted at the end of redisplay. */
14514 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14515 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14516 }
14517
14518 /* Restore current_buffer and value of point in it. The window
14519 update may have changed the buffer, so first make sure `opoint'
14520 is still valid (Bug#6177). */
14521 if (CHARPOS (opoint) < BEGV)
14522 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14523 else if (CHARPOS (opoint) > ZV)
14524 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14525 else
14526 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14527
14528 set_buffer_internal_1 (old);
14529 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14530 shorter. This can be caused by log truncation in *Messages*. */
14531 if (CHARPOS (lpoint) <= ZV)
14532 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14533
14534 unbind_to (count, Qnil);
14535 }
14536
14537
14538 /* Build the complete desired matrix of WINDOW with a window start
14539 buffer position POS.
14540
14541 Value is 1 if successful. It is zero if fonts were loaded during
14542 redisplay which makes re-adjusting glyph matrices necessary, and -1
14543 if point would appear in the scroll margins.
14544 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14545 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14546 set in FLAGS.) */
14547
14548 int
14549 try_window (Lisp_Object window, struct text_pos pos, int flags)
14550 {
14551 struct window *w = XWINDOW (window);
14552 struct it it;
14553 struct glyph_row *last_text_row = NULL;
14554 struct frame *f = XFRAME (w->frame);
14555
14556 /* Make POS the new window start. */
14557 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14558
14559 /* Mark cursor position as unknown. No overlay arrow seen. */
14560 w->cursor.vpos = -1;
14561 overlay_arrow_seen = 0;
14562
14563 /* Initialize iterator and info to start at POS. */
14564 start_display (&it, w, pos);
14565
14566 /* Display all lines of W. */
14567 while (it.current_y < it.last_visible_y)
14568 {
14569 if (display_line (&it))
14570 last_text_row = it.glyph_row - 1;
14571 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14572 return 0;
14573 }
14574
14575 /* Don't let the cursor end in the scroll margins. */
14576 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14577 && !MINI_WINDOW_P (w))
14578 {
14579 int this_scroll_margin;
14580
14581 if (scroll_margin > 0)
14582 {
14583 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14584 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14585 }
14586 else
14587 this_scroll_margin = 0;
14588
14589 if ((w->cursor.y >= 0 /* not vscrolled */
14590 && w->cursor.y < this_scroll_margin
14591 && CHARPOS (pos) > BEGV
14592 && IT_CHARPOS (it) < ZV)
14593 /* rms: considering make_cursor_line_fully_visible_p here
14594 seems to give wrong results. We don't want to recenter
14595 when the last line is partly visible, we want to allow
14596 that case to be handled in the usual way. */
14597 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14598 {
14599 w->cursor.vpos = -1;
14600 clear_glyph_matrix (w->desired_matrix);
14601 return -1;
14602 }
14603 }
14604
14605 /* If bottom moved off end of frame, change mode line percentage. */
14606 if (XFASTINT (w->window_end_pos) <= 0
14607 && Z != IT_CHARPOS (it))
14608 w->update_mode_line = Qt;
14609
14610 /* Set window_end_pos to the offset of the last character displayed
14611 on the window from the end of current_buffer. Set
14612 window_end_vpos to its row number. */
14613 if (last_text_row)
14614 {
14615 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14616 w->window_end_bytepos
14617 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14618 w->window_end_pos
14619 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14620 w->window_end_vpos
14621 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14622 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14623 ->displays_text_p);
14624 }
14625 else
14626 {
14627 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14628 w->window_end_pos = make_number (Z - ZV);
14629 w->window_end_vpos = make_number (0);
14630 }
14631
14632 /* But that is not valid info until redisplay finishes. */
14633 w->window_end_valid = Qnil;
14634 return 1;
14635 }
14636
14637
14638 \f
14639 /************************************************************************
14640 Window redisplay reusing current matrix when buffer has not changed
14641 ************************************************************************/
14642
14643 /* Try redisplay of window W showing an unchanged buffer with a
14644 different window start than the last time it was displayed by
14645 reusing its current matrix. Value is non-zero if successful.
14646 W->start is the new window start. */
14647
14648 static int
14649 try_window_reusing_current_matrix (struct window *w)
14650 {
14651 struct frame *f = XFRAME (w->frame);
14652 struct glyph_row *row, *bottom_row;
14653 struct it it;
14654 struct run run;
14655 struct text_pos start, new_start;
14656 int nrows_scrolled, i;
14657 struct glyph_row *last_text_row;
14658 struct glyph_row *last_reused_text_row;
14659 struct glyph_row *start_row;
14660 int start_vpos, min_y, max_y;
14661
14662 #if GLYPH_DEBUG
14663 if (inhibit_try_window_reusing)
14664 return 0;
14665 #endif
14666
14667 if (/* This function doesn't handle terminal frames. */
14668 !FRAME_WINDOW_P (f)
14669 /* Don't try to reuse the display if windows have been split
14670 or such. */
14671 || windows_or_buffers_changed
14672 || cursor_type_changed)
14673 return 0;
14674
14675 /* Can't do this if region may have changed. */
14676 if ((!NILP (Vtransient_mark_mode)
14677 && !NILP (current_buffer->mark_active))
14678 || !NILP (w->region_showing)
14679 || !NILP (Vshow_trailing_whitespace))
14680 return 0;
14681
14682 /* If top-line visibility has changed, give up. */
14683 if (WINDOW_WANTS_HEADER_LINE_P (w)
14684 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14685 return 0;
14686
14687 /* Give up if old or new display is scrolled vertically. We could
14688 make this function handle this, but right now it doesn't. */
14689 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14690 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14691 return 0;
14692
14693 /* The variable new_start now holds the new window start. The old
14694 start `start' can be determined from the current matrix. */
14695 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14696 start = start_row->minpos;
14697 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14698
14699 /* Clear the desired matrix for the display below. */
14700 clear_glyph_matrix (w->desired_matrix);
14701
14702 if (CHARPOS (new_start) <= CHARPOS (start))
14703 {
14704 int first_row_y;
14705
14706 /* Don't use this method if the display starts with an ellipsis
14707 displayed for invisible text. It's not easy to handle that case
14708 below, and it's certainly not worth the effort since this is
14709 not a frequent case. */
14710 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14711 return 0;
14712
14713 IF_DEBUG (debug_method_add (w, "twu1"));
14714
14715 /* Display up to a row that can be reused. The variable
14716 last_text_row is set to the last row displayed that displays
14717 text. Note that it.vpos == 0 if or if not there is a
14718 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14719 start_display (&it, w, new_start);
14720 first_row_y = it.current_y;
14721 w->cursor.vpos = -1;
14722 last_text_row = last_reused_text_row = NULL;
14723
14724 while (it.current_y < it.last_visible_y
14725 && !fonts_changed_p)
14726 {
14727 /* If we have reached into the characters in the START row,
14728 that means the line boundaries have changed. So we
14729 can't start copying with the row START. Maybe it will
14730 work to start copying with the following row. */
14731 while (IT_CHARPOS (it) > CHARPOS (start))
14732 {
14733 /* Advance to the next row as the "start". */
14734 start_row++;
14735 start = start_row->minpos;
14736 /* If there are no more rows to try, or just one, give up. */
14737 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14738 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14739 || CHARPOS (start) == ZV)
14740 {
14741 clear_glyph_matrix (w->desired_matrix);
14742 return 0;
14743 }
14744
14745 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14746 }
14747 /* If we have reached alignment,
14748 we can copy the rest of the rows. */
14749 if (IT_CHARPOS (it) == CHARPOS (start))
14750 break;
14751
14752 if (display_line (&it))
14753 last_text_row = it.glyph_row - 1;
14754 }
14755
14756 /* A value of current_y < last_visible_y means that we stopped
14757 at the previous window start, which in turn means that we
14758 have at least one reusable row. */
14759 if (it.current_y < it.last_visible_y)
14760 {
14761 /* IT.vpos always starts from 0; it counts text lines. */
14762 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14763
14764 /* Find PT if not already found in the lines displayed. */
14765 if (w->cursor.vpos < 0)
14766 {
14767 int dy = it.current_y - start_row->y;
14768
14769 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14770 row = row_containing_pos (w, PT, row, NULL, dy);
14771 if (row)
14772 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14773 dy, nrows_scrolled);
14774 else
14775 {
14776 clear_glyph_matrix (w->desired_matrix);
14777 return 0;
14778 }
14779 }
14780
14781 /* Scroll the display. Do it before the current matrix is
14782 changed. The problem here is that update has not yet
14783 run, i.e. part of the current matrix is not up to date.
14784 scroll_run_hook will clear the cursor, and use the
14785 current matrix to get the height of the row the cursor is
14786 in. */
14787 run.current_y = start_row->y;
14788 run.desired_y = it.current_y;
14789 run.height = it.last_visible_y - it.current_y;
14790
14791 if (run.height > 0 && run.current_y != run.desired_y)
14792 {
14793 update_begin (f);
14794 FRAME_RIF (f)->update_window_begin_hook (w);
14795 FRAME_RIF (f)->clear_window_mouse_face (w);
14796 FRAME_RIF (f)->scroll_run_hook (w, &run);
14797 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14798 update_end (f);
14799 }
14800
14801 /* Shift current matrix down by nrows_scrolled lines. */
14802 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14803 rotate_matrix (w->current_matrix,
14804 start_vpos,
14805 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14806 nrows_scrolled);
14807
14808 /* Disable lines that must be updated. */
14809 for (i = 0; i < nrows_scrolled; ++i)
14810 (start_row + i)->enabled_p = 0;
14811
14812 /* Re-compute Y positions. */
14813 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14814 max_y = it.last_visible_y;
14815 for (row = start_row + nrows_scrolled;
14816 row < bottom_row;
14817 ++row)
14818 {
14819 row->y = it.current_y;
14820 row->visible_height = row->height;
14821
14822 if (row->y < min_y)
14823 row->visible_height -= min_y - row->y;
14824 if (row->y + row->height > max_y)
14825 row->visible_height -= row->y + row->height - max_y;
14826 row->redraw_fringe_bitmaps_p = 1;
14827
14828 it.current_y += row->height;
14829
14830 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14831 last_reused_text_row = row;
14832 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14833 break;
14834 }
14835
14836 /* Disable lines in the current matrix which are now
14837 below the window. */
14838 for (++row; row < bottom_row; ++row)
14839 row->enabled_p = row->mode_line_p = 0;
14840 }
14841
14842 /* Update window_end_pos etc.; last_reused_text_row is the last
14843 reused row from the current matrix containing text, if any.
14844 The value of last_text_row is the last displayed line
14845 containing text. */
14846 if (last_reused_text_row)
14847 {
14848 w->window_end_bytepos
14849 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14850 w->window_end_pos
14851 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14852 w->window_end_vpos
14853 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14854 w->current_matrix));
14855 }
14856 else if (last_text_row)
14857 {
14858 w->window_end_bytepos
14859 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14860 w->window_end_pos
14861 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14862 w->window_end_vpos
14863 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14864 }
14865 else
14866 {
14867 /* This window must be completely empty. */
14868 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14869 w->window_end_pos = make_number (Z - ZV);
14870 w->window_end_vpos = make_number (0);
14871 }
14872 w->window_end_valid = Qnil;
14873
14874 /* Update hint: don't try scrolling again in update_window. */
14875 w->desired_matrix->no_scrolling_p = 1;
14876
14877 #if GLYPH_DEBUG
14878 debug_method_add (w, "try_window_reusing_current_matrix 1");
14879 #endif
14880 return 1;
14881 }
14882 else if (CHARPOS (new_start) > CHARPOS (start))
14883 {
14884 struct glyph_row *pt_row, *row;
14885 struct glyph_row *first_reusable_row;
14886 struct glyph_row *first_row_to_display;
14887 int dy;
14888 int yb = window_text_bottom_y (w);
14889
14890 /* Find the row starting at new_start, if there is one. Don't
14891 reuse a partially visible line at the end. */
14892 first_reusable_row = start_row;
14893 while (first_reusable_row->enabled_p
14894 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14895 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14896 < CHARPOS (new_start)))
14897 ++first_reusable_row;
14898
14899 /* Give up if there is no row to reuse. */
14900 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14901 || !first_reusable_row->enabled_p
14902 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14903 != CHARPOS (new_start)))
14904 return 0;
14905
14906 /* We can reuse fully visible rows beginning with
14907 first_reusable_row to the end of the window. Set
14908 first_row_to_display to the first row that cannot be reused.
14909 Set pt_row to the row containing point, if there is any. */
14910 pt_row = NULL;
14911 for (first_row_to_display = first_reusable_row;
14912 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14913 ++first_row_to_display)
14914 {
14915 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14916 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14917 pt_row = first_row_to_display;
14918 }
14919
14920 /* Start displaying at the start of first_row_to_display. */
14921 xassert (first_row_to_display->y < yb);
14922 init_to_row_start (&it, w, first_row_to_display);
14923
14924 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14925 - start_vpos);
14926 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14927 - nrows_scrolled);
14928 it.current_y = (first_row_to_display->y - first_reusable_row->y
14929 + WINDOW_HEADER_LINE_HEIGHT (w));
14930
14931 /* Display lines beginning with first_row_to_display in the
14932 desired matrix. Set last_text_row to the last row displayed
14933 that displays text. */
14934 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14935 if (pt_row == NULL)
14936 w->cursor.vpos = -1;
14937 last_text_row = NULL;
14938 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14939 if (display_line (&it))
14940 last_text_row = it.glyph_row - 1;
14941
14942 /* If point is in a reused row, adjust y and vpos of the cursor
14943 position. */
14944 if (pt_row)
14945 {
14946 w->cursor.vpos -= nrows_scrolled;
14947 w->cursor.y -= first_reusable_row->y - start_row->y;
14948 }
14949
14950 /* Give up if point isn't in a row displayed or reused. (This
14951 also handles the case where w->cursor.vpos < nrows_scrolled
14952 after the calls to display_line, which can happen with scroll
14953 margins. See bug#1295.) */
14954 if (w->cursor.vpos < 0)
14955 {
14956 clear_glyph_matrix (w->desired_matrix);
14957 return 0;
14958 }
14959
14960 /* Scroll the display. */
14961 run.current_y = first_reusable_row->y;
14962 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14963 run.height = it.last_visible_y - run.current_y;
14964 dy = run.current_y - run.desired_y;
14965
14966 if (run.height)
14967 {
14968 update_begin (f);
14969 FRAME_RIF (f)->update_window_begin_hook (w);
14970 FRAME_RIF (f)->clear_window_mouse_face (w);
14971 FRAME_RIF (f)->scroll_run_hook (w, &run);
14972 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14973 update_end (f);
14974 }
14975
14976 /* Adjust Y positions of reused rows. */
14977 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14978 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14979 max_y = it.last_visible_y;
14980 for (row = first_reusable_row; row < first_row_to_display; ++row)
14981 {
14982 row->y -= dy;
14983 row->visible_height = row->height;
14984 if (row->y < min_y)
14985 row->visible_height -= min_y - row->y;
14986 if (row->y + row->height > max_y)
14987 row->visible_height -= row->y + row->height - max_y;
14988 row->redraw_fringe_bitmaps_p = 1;
14989 }
14990
14991 /* Scroll the current matrix. */
14992 xassert (nrows_scrolled > 0);
14993 rotate_matrix (w->current_matrix,
14994 start_vpos,
14995 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14996 -nrows_scrolled);
14997
14998 /* Disable rows not reused. */
14999 for (row -= nrows_scrolled; row < bottom_row; ++row)
15000 row->enabled_p = 0;
15001
15002 /* Point may have moved to a different line, so we cannot assume that
15003 the previous cursor position is valid; locate the correct row. */
15004 if (pt_row)
15005 {
15006 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15007 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15008 row++)
15009 {
15010 w->cursor.vpos++;
15011 w->cursor.y = row->y;
15012 }
15013 if (row < bottom_row)
15014 {
15015 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15016 struct glyph *end = glyph + row->used[TEXT_AREA];
15017
15018 /* Can't use this optimization with bidi-reordered glyph
15019 rows, unless cursor is already at point. */
15020 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15021 {
15022 if (!(w->cursor.hpos >= 0
15023 && w->cursor.hpos < row->used[TEXT_AREA]
15024 && BUFFERP (glyph->object)
15025 && glyph->charpos == PT))
15026 return 0;
15027 }
15028 else
15029 for (; glyph < end
15030 && (!BUFFERP (glyph->object)
15031 || glyph->charpos < PT);
15032 glyph++)
15033 {
15034 w->cursor.hpos++;
15035 w->cursor.x += glyph->pixel_width;
15036 }
15037 }
15038 }
15039
15040 /* Adjust window end. A null value of last_text_row means that
15041 the window end is in reused rows which in turn means that
15042 only its vpos can have changed. */
15043 if (last_text_row)
15044 {
15045 w->window_end_bytepos
15046 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15047 w->window_end_pos
15048 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15049 w->window_end_vpos
15050 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15051 }
15052 else
15053 {
15054 w->window_end_vpos
15055 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15056 }
15057
15058 w->window_end_valid = Qnil;
15059 w->desired_matrix->no_scrolling_p = 1;
15060
15061 #if GLYPH_DEBUG
15062 debug_method_add (w, "try_window_reusing_current_matrix 2");
15063 #endif
15064 return 1;
15065 }
15066
15067 return 0;
15068 }
15069
15070
15071 \f
15072 /************************************************************************
15073 Window redisplay reusing current matrix when buffer has changed
15074 ************************************************************************/
15075
15076 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15077 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15078 int *, int *);
15079 static struct glyph_row *
15080 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15081 struct glyph_row *);
15082
15083
15084 /* Return the last row in MATRIX displaying text. If row START is
15085 non-null, start searching with that row. IT gives the dimensions
15086 of the display. Value is null if matrix is empty; otherwise it is
15087 a pointer to the row found. */
15088
15089 static struct glyph_row *
15090 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15091 struct glyph_row *start)
15092 {
15093 struct glyph_row *row, *row_found;
15094
15095 /* Set row_found to the last row in IT->w's current matrix
15096 displaying text. The loop looks funny but think of partially
15097 visible lines. */
15098 row_found = NULL;
15099 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15100 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15101 {
15102 xassert (row->enabled_p);
15103 row_found = row;
15104 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15105 break;
15106 ++row;
15107 }
15108
15109 return row_found;
15110 }
15111
15112
15113 /* Return the last row in the current matrix of W that is not affected
15114 by changes at the start of current_buffer that occurred since W's
15115 current matrix was built. Value is null if no such row exists.
15116
15117 BEG_UNCHANGED us the number of characters unchanged at the start of
15118 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15119 first changed character in current_buffer. Characters at positions <
15120 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15121 when the current matrix was built. */
15122
15123 static struct glyph_row *
15124 find_last_unchanged_at_beg_row (struct window *w)
15125 {
15126 int first_changed_pos = BEG + BEG_UNCHANGED;
15127 struct glyph_row *row;
15128 struct glyph_row *row_found = NULL;
15129 int yb = window_text_bottom_y (w);
15130
15131 /* Find the last row displaying unchanged text. */
15132 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15133 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15134 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15135 ++row)
15136 {
15137 if (/* If row ends before first_changed_pos, it is unchanged,
15138 except in some case. */
15139 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15140 /* When row ends in ZV and we write at ZV it is not
15141 unchanged. */
15142 && !row->ends_at_zv_p
15143 /* When first_changed_pos is the end of a continued line,
15144 row is not unchanged because it may be no longer
15145 continued. */
15146 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15147 && (row->continued_p
15148 || row->exact_window_width_line_p)))
15149 row_found = row;
15150
15151 /* Stop if last visible row. */
15152 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15153 break;
15154 }
15155
15156 return row_found;
15157 }
15158
15159
15160 /* Find the first glyph row in the current matrix of W that is not
15161 affected by changes at the end of current_buffer since the
15162 time W's current matrix was built.
15163
15164 Return in *DELTA the number of chars by which buffer positions in
15165 unchanged text at the end of current_buffer must be adjusted.
15166
15167 Return in *DELTA_BYTES the corresponding number of bytes.
15168
15169 Value is null if no such row exists, i.e. all rows are affected by
15170 changes. */
15171
15172 static struct glyph_row *
15173 find_first_unchanged_at_end_row (struct window *w, int *delta, int *delta_bytes)
15174 {
15175 struct glyph_row *row;
15176 struct glyph_row *row_found = NULL;
15177
15178 *delta = *delta_bytes = 0;
15179
15180 /* Display must not have been paused, otherwise the current matrix
15181 is not up to date. */
15182 eassert (!NILP (w->window_end_valid));
15183
15184 /* A value of window_end_pos >= END_UNCHANGED means that the window
15185 end is in the range of changed text. If so, there is no
15186 unchanged row at the end of W's current matrix. */
15187 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15188 return NULL;
15189
15190 /* Set row to the last row in W's current matrix displaying text. */
15191 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15192
15193 /* If matrix is entirely empty, no unchanged row exists. */
15194 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15195 {
15196 /* The value of row is the last glyph row in the matrix having a
15197 meaningful buffer position in it. The end position of row
15198 corresponds to window_end_pos. This allows us to translate
15199 buffer positions in the current matrix to current buffer
15200 positions for characters not in changed text. */
15201 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15202 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15203 int last_unchanged_pos, last_unchanged_pos_old;
15204 struct glyph_row *first_text_row
15205 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15206
15207 *delta = Z - Z_old;
15208 *delta_bytes = Z_BYTE - Z_BYTE_old;
15209
15210 /* Set last_unchanged_pos to the buffer position of the last
15211 character in the buffer that has not been changed. Z is the
15212 index + 1 of the last character in current_buffer, i.e. by
15213 subtracting END_UNCHANGED we get the index of the last
15214 unchanged character, and we have to add BEG to get its buffer
15215 position. */
15216 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15217 last_unchanged_pos_old = last_unchanged_pos - *delta;
15218
15219 /* Search backward from ROW for a row displaying a line that
15220 starts at a minimum position >= last_unchanged_pos_old. */
15221 for (; row > first_text_row; --row)
15222 {
15223 /* This used to abort, but it can happen.
15224 It is ok to just stop the search instead here. KFS. */
15225 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15226 break;
15227
15228 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15229 row_found = row;
15230 }
15231 }
15232
15233 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15234
15235 return row_found;
15236 }
15237
15238
15239 /* Make sure that glyph rows in the current matrix of window W
15240 reference the same glyph memory as corresponding rows in the
15241 frame's frame matrix. This function is called after scrolling W's
15242 current matrix on a terminal frame in try_window_id and
15243 try_window_reusing_current_matrix. */
15244
15245 static void
15246 sync_frame_with_window_matrix_rows (struct window *w)
15247 {
15248 struct frame *f = XFRAME (w->frame);
15249 struct glyph_row *window_row, *window_row_end, *frame_row;
15250
15251 /* Preconditions: W must be a leaf window and full-width. Its frame
15252 must have a frame matrix. */
15253 xassert (NILP (w->hchild) && NILP (w->vchild));
15254 xassert (WINDOW_FULL_WIDTH_P (w));
15255 xassert (!FRAME_WINDOW_P (f));
15256
15257 /* If W is a full-width window, glyph pointers in W's current matrix
15258 have, by definition, to be the same as glyph pointers in the
15259 corresponding frame matrix. Note that frame matrices have no
15260 marginal areas (see build_frame_matrix). */
15261 window_row = w->current_matrix->rows;
15262 window_row_end = window_row + w->current_matrix->nrows;
15263 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15264 while (window_row < window_row_end)
15265 {
15266 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15267 struct glyph *end = window_row->glyphs[LAST_AREA];
15268
15269 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15270 frame_row->glyphs[TEXT_AREA] = start;
15271 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15272 frame_row->glyphs[LAST_AREA] = end;
15273
15274 /* Disable frame rows whose corresponding window rows have
15275 been disabled in try_window_id. */
15276 if (!window_row->enabled_p)
15277 frame_row->enabled_p = 0;
15278
15279 ++window_row, ++frame_row;
15280 }
15281 }
15282
15283
15284 /* Find the glyph row in window W containing CHARPOS. Consider all
15285 rows between START and END (not inclusive). END null means search
15286 all rows to the end of the display area of W. Value is the row
15287 containing CHARPOS or null. */
15288
15289 struct glyph_row *
15290 row_containing_pos (struct window *w, int charpos, struct glyph_row *start,
15291 struct glyph_row *end, int dy)
15292 {
15293 struct glyph_row *row = start;
15294 struct glyph_row *best_row = NULL;
15295 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15296 int last_y;
15297
15298 /* If we happen to start on a header-line, skip that. */
15299 if (row->mode_line_p)
15300 ++row;
15301
15302 if ((end && row >= end) || !row->enabled_p)
15303 return NULL;
15304
15305 last_y = window_text_bottom_y (w) - dy;
15306
15307 while (1)
15308 {
15309 /* Give up if we have gone too far. */
15310 if (end && row >= end)
15311 return NULL;
15312 /* This formerly returned if they were equal.
15313 I think that both quantities are of a "last plus one" type;
15314 if so, when they are equal, the row is within the screen. -- rms. */
15315 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15316 return NULL;
15317
15318 /* If it is in this row, return this row. */
15319 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15320 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15321 /* The end position of a row equals the start
15322 position of the next row. If CHARPOS is there, we
15323 would rather display it in the next line, except
15324 when this line ends in ZV. */
15325 && !row->ends_at_zv_p
15326 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15327 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15328 {
15329 struct glyph *g;
15330
15331 if (NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15332 return row;
15333 /* In bidi-reordered rows, there could be several rows
15334 occluding point. We need to find the one which fits
15335 CHARPOS the best. */
15336 for (g = row->glyphs[TEXT_AREA];
15337 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15338 g++)
15339 {
15340 if (!STRINGP (g->object))
15341 {
15342 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15343 {
15344 mindif = eabs (g->charpos - charpos);
15345 best_row = row;
15346 }
15347 }
15348 }
15349 }
15350 else if (best_row)
15351 return best_row;
15352 ++row;
15353 }
15354 }
15355
15356
15357 /* Try to redisplay window W by reusing its existing display. W's
15358 current matrix must be up to date when this function is called,
15359 i.e. window_end_valid must not be nil.
15360
15361 Value is
15362
15363 1 if display has been updated
15364 0 if otherwise unsuccessful
15365 -1 if redisplay with same window start is known not to succeed
15366
15367 The following steps are performed:
15368
15369 1. Find the last row in the current matrix of W that is not
15370 affected by changes at the start of current_buffer. If no such row
15371 is found, give up.
15372
15373 2. Find the first row in W's current matrix that is not affected by
15374 changes at the end of current_buffer. Maybe there is no such row.
15375
15376 3. Display lines beginning with the row + 1 found in step 1 to the
15377 row found in step 2 or, if step 2 didn't find a row, to the end of
15378 the window.
15379
15380 4. If cursor is not known to appear on the window, give up.
15381
15382 5. If display stopped at the row found in step 2, scroll the
15383 display and current matrix as needed.
15384
15385 6. Maybe display some lines at the end of W, if we must. This can
15386 happen under various circumstances, like a partially visible line
15387 becoming fully visible, or because newly displayed lines are displayed
15388 in smaller font sizes.
15389
15390 7. Update W's window end information. */
15391
15392 static int
15393 try_window_id (struct window *w)
15394 {
15395 struct frame *f = XFRAME (w->frame);
15396 struct glyph_matrix *current_matrix = w->current_matrix;
15397 struct glyph_matrix *desired_matrix = w->desired_matrix;
15398 struct glyph_row *last_unchanged_at_beg_row;
15399 struct glyph_row *first_unchanged_at_end_row;
15400 struct glyph_row *row;
15401 struct glyph_row *bottom_row;
15402 int bottom_vpos;
15403 struct it it;
15404 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
15405 struct text_pos start_pos;
15406 struct run run;
15407 int first_unchanged_at_end_vpos = 0;
15408 struct glyph_row *last_text_row, *last_text_row_at_end;
15409 struct text_pos start;
15410 int first_changed_charpos, last_changed_charpos;
15411
15412 #if GLYPH_DEBUG
15413 if (inhibit_try_window_id)
15414 return 0;
15415 #endif
15416
15417 /* This is handy for debugging. */
15418 #if 0
15419 #define GIVE_UP(X) \
15420 do { \
15421 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15422 return 0; \
15423 } while (0)
15424 #else
15425 #define GIVE_UP(X) return 0
15426 #endif
15427
15428 SET_TEXT_POS_FROM_MARKER (start, w->start);
15429
15430 /* Don't use this for mini-windows because these can show
15431 messages and mini-buffers, and we don't handle that here. */
15432 if (MINI_WINDOW_P (w))
15433 GIVE_UP (1);
15434
15435 /* This flag is used to prevent redisplay optimizations. */
15436 if (windows_or_buffers_changed || cursor_type_changed)
15437 GIVE_UP (2);
15438
15439 /* Verify that narrowing has not changed.
15440 Also verify that we were not told to prevent redisplay optimizations.
15441 It would be nice to further
15442 reduce the number of cases where this prevents try_window_id. */
15443 if (current_buffer->clip_changed
15444 || current_buffer->prevent_redisplay_optimizations_p)
15445 GIVE_UP (3);
15446
15447 /* Window must either use window-based redisplay or be full width. */
15448 if (!FRAME_WINDOW_P (f)
15449 && (!FRAME_LINE_INS_DEL_OK (f)
15450 || !WINDOW_FULL_WIDTH_P (w)))
15451 GIVE_UP (4);
15452
15453 /* Give up if point is known NOT to appear in W. */
15454 if (PT < CHARPOS (start))
15455 GIVE_UP (5);
15456
15457 /* Another way to prevent redisplay optimizations. */
15458 if (XFASTINT (w->last_modified) == 0)
15459 GIVE_UP (6);
15460
15461 /* Verify that window is not hscrolled. */
15462 if (XFASTINT (w->hscroll) != 0)
15463 GIVE_UP (7);
15464
15465 /* Verify that display wasn't paused. */
15466 if (NILP (w->window_end_valid))
15467 GIVE_UP (8);
15468
15469 /* Can't use this if highlighting a region because a cursor movement
15470 will do more than just set the cursor. */
15471 if (!NILP (Vtransient_mark_mode)
15472 && !NILP (current_buffer->mark_active))
15473 GIVE_UP (9);
15474
15475 /* Likewise if highlighting trailing whitespace. */
15476 if (!NILP (Vshow_trailing_whitespace))
15477 GIVE_UP (11);
15478
15479 /* Likewise if showing a region. */
15480 if (!NILP (w->region_showing))
15481 GIVE_UP (10);
15482
15483 /* Can't use this if overlay arrow position and/or string have
15484 changed. */
15485 if (overlay_arrows_changed_p ())
15486 GIVE_UP (12);
15487
15488 /* When word-wrap is on, adding a space to the first word of a
15489 wrapped line can change the wrap position, altering the line
15490 above it. It might be worthwhile to handle this more
15491 intelligently, but for now just redisplay from scratch. */
15492 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15493 GIVE_UP (21);
15494
15495 /* Under bidi reordering, adding or deleting a character in the
15496 beginning of a paragraph, before the first strong directional
15497 character, can change the base direction of the paragraph (unless
15498 the buffer specifies a fixed paragraph direction), which will
15499 require to redisplay the whole paragraph. It might be worthwhile
15500 to find the paragraph limits and widen the range of redisplayed
15501 lines to that, but for now just give up this optimization and
15502 redisplay from scratch. */
15503 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15504 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15505 GIVE_UP (22);
15506
15507 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15508 only if buffer has really changed. The reason is that the gap is
15509 initially at Z for freshly visited files. The code below would
15510 set end_unchanged to 0 in that case. */
15511 if (MODIFF > SAVE_MODIFF
15512 /* This seems to happen sometimes after saving a buffer. */
15513 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15514 {
15515 if (GPT - BEG < BEG_UNCHANGED)
15516 BEG_UNCHANGED = GPT - BEG;
15517 if (Z - GPT < END_UNCHANGED)
15518 END_UNCHANGED = Z - GPT;
15519 }
15520
15521 /* The position of the first and last character that has been changed. */
15522 first_changed_charpos = BEG + BEG_UNCHANGED;
15523 last_changed_charpos = Z - END_UNCHANGED;
15524
15525 /* If window starts after a line end, and the last change is in
15526 front of that newline, then changes don't affect the display.
15527 This case happens with stealth-fontification. Note that although
15528 the display is unchanged, glyph positions in the matrix have to
15529 be adjusted, of course. */
15530 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15531 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15532 && ((last_changed_charpos < CHARPOS (start)
15533 && CHARPOS (start) == BEGV)
15534 || (last_changed_charpos < CHARPOS (start) - 1
15535 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15536 {
15537 int Z_old, delta, Z_BYTE_old, delta_bytes;
15538 struct glyph_row *r0;
15539
15540 /* Compute how many chars/bytes have been added to or removed
15541 from the buffer. */
15542 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15543 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15544 delta = Z - Z_old;
15545 delta_bytes = Z_BYTE - Z_BYTE_old;
15546
15547 /* Give up if PT is not in the window. Note that it already has
15548 been checked at the start of try_window_id that PT is not in
15549 front of the window start. */
15550 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15551 GIVE_UP (13);
15552
15553 /* If window start is unchanged, we can reuse the whole matrix
15554 as is, after adjusting glyph positions. No need to compute
15555 the window end again, since its offset from Z hasn't changed. */
15556 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15557 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15558 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15559 /* PT must not be in a partially visible line. */
15560 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15561 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15562 {
15563 /* Adjust positions in the glyph matrix. */
15564 if (delta || delta_bytes)
15565 {
15566 struct glyph_row *r1
15567 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15568 increment_matrix_positions (w->current_matrix,
15569 MATRIX_ROW_VPOS (r0, current_matrix),
15570 MATRIX_ROW_VPOS (r1, current_matrix),
15571 delta, delta_bytes);
15572 }
15573
15574 /* Set the cursor. */
15575 row = row_containing_pos (w, PT, r0, NULL, 0);
15576 if (row)
15577 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15578 else
15579 abort ();
15580 return 1;
15581 }
15582 }
15583
15584 /* Handle the case that changes are all below what is displayed in
15585 the window, and that PT is in the window. This shortcut cannot
15586 be taken if ZV is visible in the window, and text has been added
15587 there that is visible in the window. */
15588 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15589 /* ZV is not visible in the window, or there are no
15590 changes at ZV, actually. */
15591 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15592 || first_changed_charpos == last_changed_charpos))
15593 {
15594 struct glyph_row *r0;
15595
15596 /* Give up if PT is not in the window. Note that it already has
15597 been checked at the start of try_window_id that PT is not in
15598 front of the window start. */
15599 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15600 GIVE_UP (14);
15601
15602 /* If window start is unchanged, we can reuse the whole matrix
15603 as is, without changing glyph positions since no text has
15604 been added/removed in front of the window end. */
15605 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15606 if (TEXT_POS_EQUAL_P (start, r0->minpos)
15607 /* PT must not be in a partially visible line. */
15608 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15609 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15610 {
15611 /* We have to compute the window end anew since text
15612 could have been added/removed after it. */
15613 w->window_end_pos
15614 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15615 w->window_end_bytepos
15616 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15617
15618 /* Set the cursor. */
15619 row = row_containing_pos (w, PT, r0, NULL, 0);
15620 if (row)
15621 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15622 else
15623 abort ();
15624 return 2;
15625 }
15626 }
15627
15628 /* Give up if window start is in the changed area.
15629
15630 The condition used to read
15631
15632 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15633
15634 but why that was tested escapes me at the moment. */
15635 if (CHARPOS (start) >= first_changed_charpos
15636 && CHARPOS (start) <= last_changed_charpos)
15637 GIVE_UP (15);
15638
15639 /* Check that window start agrees with the start of the first glyph
15640 row in its current matrix. Check this after we know the window
15641 start is not in changed text, otherwise positions would not be
15642 comparable. */
15643 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15644 if (!TEXT_POS_EQUAL_P (start, row->minpos))
15645 GIVE_UP (16);
15646
15647 /* Give up if the window ends in strings. Overlay strings
15648 at the end are difficult to handle, so don't try. */
15649 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15650 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15651 GIVE_UP (20);
15652
15653 /* Compute the position at which we have to start displaying new
15654 lines. Some of the lines at the top of the window might be
15655 reusable because they are not displaying changed text. Find the
15656 last row in W's current matrix not affected by changes at the
15657 start of current_buffer. Value is null if changes start in the
15658 first line of window. */
15659 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15660 if (last_unchanged_at_beg_row)
15661 {
15662 /* Avoid starting to display in the moddle of a character, a TAB
15663 for instance. This is easier than to set up the iterator
15664 exactly, and it's not a frequent case, so the additional
15665 effort wouldn't really pay off. */
15666 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15667 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15668 && last_unchanged_at_beg_row > w->current_matrix->rows)
15669 --last_unchanged_at_beg_row;
15670
15671 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15672 GIVE_UP (17);
15673
15674 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15675 GIVE_UP (18);
15676 start_pos = it.current.pos;
15677
15678 /* Start displaying new lines in the desired matrix at the same
15679 vpos we would use in the current matrix, i.e. below
15680 last_unchanged_at_beg_row. */
15681 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15682 current_matrix);
15683 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15684 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15685
15686 xassert (it.hpos == 0 && it.current_x == 0);
15687 }
15688 else
15689 {
15690 /* There are no reusable lines at the start of the window.
15691 Start displaying in the first text line. */
15692 start_display (&it, w, start);
15693 it.vpos = it.first_vpos;
15694 start_pos = it.current.pos;
15695 }
15696
15697 /* Find the first row that is not affected by changes at the end of
15698 the buffer. Value will be null if there is no unchanged row, in
15699 which case we must redisplay to the end of the window. delta
15700 will be set to the value by which buffer positions beginning with
15701 first_unchanged_at_end_row have to be adjusted due to text
15702 changes. */
15703 first_unchanged_at_end_row
15704 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15705 IF_DEBUG (debug_delta = delta);
15706 IF_DEBUG (debug_delta_bytes = delta_bytes);
15707
15708 /* Set stop_pos to the buffer position up to which we will have to
15709 display new lines. If first_unchanged_at_end_row != NULL, this
15710 is the buffer position of the start of the line displayed in that
15711 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15712 that we don't stop at a buffer position. */
15713 stop_pos = 0;
15714 if (first_unchanged_at_end_row)
15715 {
15716 xassert (last_unchanged_at_beg_row == NULL
15717 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15718
15719 /* If this is a continuation line, move forward to the next one
15720 that isn't. Changes in lines above affect this line.
15721 Caution: this may move first_unchanged_at_end_row to a row
15722 not displaying text. */
15723 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15724 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15725 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15726 < it.last_visible_y))
15727 ++first_unchanged_at_end_row;
15728
15729 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15730 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15731 >= it.last_visible_y))
15732 first_unchanged_at_end_row = NULL;
15733 else
15734 {
15735 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15736 + delta);
15737 first_unchanged_at_end_vpos
15738 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15739 xassert (stop_pos >= Z - END_UNCHANGED);
15740 }
15741 }
15742 else if (last_unchanged_at_beg_row == NULL)
15743 GIVE_UP (19);
15744
15745
15746 #if GLYPH_DEBUG
15747
15748 /* Either there is no unchanged row at the end, or the one we have
15749 now displays text. This is a necessary condition for the window
15750 end pos calculation at the end of this function. */
15751 xassert (first_unchanged_at_end_row == NULL
15752 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15753
15754 debug_last_unchanged_at_beg_vpos
15755 = (last_unchanged_at_beg_row
15756 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15757 : -1);
15758 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15759
15760 #endif /* GLYPH_DEBUG != 0 */
15761
15762
15763 /* Display new lines. Set last_text_row to the last new line
15764 displayed which has text on it, i.e. might end up as being the
15765 line where the window_end_vpos is. */
15766 w->cursor.vpos = -1;
15767 last_text_row = NULL;
15768 overlay_arrow_seen = 0;
15769 while (it.current_y < it.last_visible_y
15770 && !fonts_changed_p
15771 && (first_unchanged_at_end_row == NULL
15772 || IT_CHARPOS (it) < stop_pos))
15773 {
15774 if (display_line (&it))
15775 last_text_row = it.glyph_row - 1;
15776 }
15777
15778 if (fonts_changed_p)
15779 return -1;
15780
15781
15782 /* Compute differences in buffer positions, y-positions etc. for
15783 lines reused at the bottom of the window. Compute what we can
15784 scroll. */
15785 if (first_unchanged_at_end_row
15786 /* No lines reused because we displayed everything up to the
15787 bottom of the window. */
15788 && it.current_y < it.last_visible_y)
15789 {
15790 dvpos = (it.vpos
15791 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15792 current_matrix));
15793 dy = it.current_y - first_unchanged_at_end_row->y;
15794 run.current_y = first_unchanged_at_end_row->y;
15795 run.desired_y = run.current_y + dy;
15796 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15797 }
15798 else
15799 {
15800 delta = delta_bytes = dvpos = dy
15801 = run.current_y = run.desired_y = run.height = 0;
15802 first_unchanged_at_end_row = NULL;
15803 }
15804 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15805
15806
15807 /* Find the cursor if not already found. We have to decide whether
15808 PT will appear on this window (it sometimes doesn't, but this is
15809 not a very frequent case.) This decision has to be made before
15810 the current matrix is altered. A value of cursor.vpos < 0 means
15811 that PT is either in one of the lines beginning at
15812 first_unchanged_at_end_row or below the window. Don't care for
15813 lines that might be displayed later at the window end; as
15814 mentioned, this is not a frequent case. */
15815 if (w->cursor.vpos < 0)
15816 {
15817 /* Cursor in unchanged rows at the top? */
15818 if (PT < CHARPOS (start_pos)
15819 && last_unchanged_at_beg_row)
15820 {
15821 row = row_containing_pos (w, PT,
15822 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15823 last_unchanged_at_beg_row + 1, 0);
15824 if (row)
15825 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15826 }
15827
15828 /* Start from first_unchanged_at_end_row looking for PT. */
15829 else if (first_unchanged_at_end_row)
15830 {
15831 row = row_containing_pos (w, PT - delta,
15832 first_unchanged_at_end_row, NULL, 0);
15833 if (row)
15834 set_cursor_from_row (w, row, w->current_matrix, delta,
15835 delta_bytes, dy, dvpos);
15836 }
15837
15838 /* Give up if cursor was not found. */
15839 if (w->cursor.vpos < 0)
15840 {
15841 clear_glyph_matrix (w->desired_matrix);
15842 return -1;
15843 }
15844 }
15845
15846 /* Don't let the cursor end in the scroll margins. */
15847 {
15848 int this_scroll_margin, cursor_height;
15849
15850 this_scroll_margin = max (0, scroll_margin);
15851 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15852 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15853 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15854
15855 if ((w->cursor.y < this_scroll_margin
15856 && CHARPOS (start) > BEGV)
15857 /* Old redisplay didn't take scroll margin into account at the bottom,
15858 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15859 || (w->cursor.y + (make_cursor_line_fully_visible_p
15860 ? cursor_height + this_scroll_margin
15861 : 1)) > it.last_visible_y)
15862 {
15863 w->cursor.vpos = -1;
15864 clear_glyph_matrix (w->desired_matrix);
15865 return -1;
15866 }
15867 }
15868
15869 /* Scroll the display. Do it before changing the current matrix so
15870 that xterm.c doesn't get confused about where the cursor glyph is
15871 found. */
15872 if (dy && run.height)
15873 {
15874 update_begin (f);
15875
15876 if (FRAME_WINDOW_P (f))
15877 {
15878 FRAME_RIF (f)->update_window_begin_hook (w);
15879 FRAME_RIF (f)->clear_window_mouse_face (w);
15880 FRAME_RIF (f)->scroll_run_hook (w, &run);
15881 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15882 }
15883 else
15884 {
15885 /* Terminal frame. In this case, dvpos gives the number of
15886 lines to scroll by; dvpos < 0 means scroll up. */
15887 int first_unchanged_at_end_vpos
15888 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15889 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15890 int end = (WINDOW_TOP_EDGE_LINE (w)
15891 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15892 + window_internal_height (w));
15893
15894 /* Perform the operation on the screen. */
15895 if (dvpos > 0)
15896 {
15897 /* Scroll last_unchanged_at_beg_row to the end of the
15898 window down dvpos lines. */
15899 set_terminal_window (f, end);
15900
15901 /* On dumb terminals delete dvpos lines at the end
15902 before inserting dvpos empty lines. */
15903 if (!FRAME_SCROLL_REGION_OK (f))
15904 ins_del_lines (f, end - dvpos, -dvpos);
15905
15906 /* Insert dvpos empty lines in front of
15907 last_unchanged_at_beg_row. */
15908 ins_del_lines (f, from, dvpos);
15909 }
15910 else if (dvpos < 0)
15911 {
15912 /* Scroll up last_unchanged_at_beg_vpos to the end of
15913 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15914 set_terminal_window (f, end);
15915
15916 /* Delete dvpos lines in front of
15917 last_unchanged_at_beg_vpos. ins_del_lines will set
15918 the cursor to the given vpos and emit |dvpos| delete
15919 line sequences. */
15920 ins_del_lines (f, from + dvpos, dvpos);
15921
15922 /* On a dumb terminal insert dvpos empty lines at the
15923 end. */
15924 if (!FRAME_SCROLL_REGION_OK (f))
15925 ins_del_lines (f, end + dvpos, -dvpos);
15926 }
15927
15928 set_terminal_window (f, 0);
15929 }
15930
15931 update_end (f);
15932 }
15933
15934 /* Shift reused rows of the current matrix to the right position.
15935 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15936 text. */
15937 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15938 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15939 if (dvpos < 0)
15940 {
15941 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15942 bottom_vpos, dvpos);
15943 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15944 bottom_vpos, 0);
15945 }
15946 else if (dvpos > 0)
15947 {
15948 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15949 bottom_vpos, dvpos);
15950 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15951 first_unchanged_at_end_vpos + dvpos, 0);
15952 }
15953
15954 /* For frame-based redisplay, make sure that current frame and window
15955 matrix are in sync with respect to glyph memory. */
15956 if (!FRAME_WINDOW_P (f))
15957 sync_frame_with_window_matrix_rows (w);
15958
15959 /* Adjust buffer positions in reused rows. */
15960 if (delta || delta_bytes)
15961 increment_matrix_positions (current_matrix,
15962 first_unchanged_at_end_vpos + dvpos,
15963 bottom_vpos, delta, delta_bytes);
15964
15965 /* Adjust Y positions. */
15966 if (dy)
15967 shift_glyph_matrix (w, current_matrix,
15968 first_unchanged_at_end_vpos + dvpos,
15969 bottom_vpos, dy);
15970
15971 if (first_unchanged_at_end_row)
15972 {
15973 first_unchanged_at_end_row += dvpos;
15974 if (first_unchanged_at_end_row->y >= it.last_visible_y
15975 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15976 first_unchanged_at_end_row = NULL;
15977 }
15978
15979 /* If scrolling up, there may be some lines to display at the end of
15980 the window. */
15981 last_text_row_at_end = NULL;
15982 if (dy < 0)
15983 {
15984 /* Scrolling up can leave for example a partially visible line
15985 at the end of the window to be redisplayed. */
15986 /* Set last_row to the glyph row in the current matrix where the
15987 window end line is found. It has been moved up or down in
15988 the matrix by dvpos. */
15989 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15990 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15991
15992 /* If last_row is the window end line, it should display text. */
15993 xassert (last_row->displays_text_p);
15994
15995 /* If window end line was partially visible before, begin
15996 displaying at that line. Otherwise begin displaying with the
15997 line following it. */
15998 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15999 {
16000 init_to_row_start (&it, w, last_row);
16001 it.vpos = last_vpos;
16002 it.current_y = last_row->y;
16003 }
16004 else
16005 {
16006 init_to_row_end (&it, w, last_row);
16007 it.vpos = 1 + last_vpos;
16008 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16009 ++last_row;
16010 }
16011
16012 /* We may start in a continuation line. If so, we have to
16013 get the right continuation_lines_width and current_x. */
16014 it.continuation_lines_width = last_row->continuation_lines_width;
16015 it.hpos = it.current_x = 0;
16016
16017 /* Display the rest of the lines at the window end. */
16018 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16019 while (it.current_y < it.last_visible_y
16020 && !fonts_changed_p)
16021 {
16022 /* Is it always sure that the display agrees with lines in
16023 the current matrix? I don't think so, so we mark rows
16024 displayed invalid in the current matrix by setting their
16025 enabled_p flag to zero. */
16026 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16027 if (display_line (&it))
16028 last_text_row_at_end = it.glyph_row - 1;
16029 }
16030 }
16031
16032 /* Update window_end_pos and window_end_vpos. */
16033 if (first_unchanged_at_end_row
16034 && !last_text_row_at_end)
16035 {
16036 /* Window end line if one of the preserved rows from the current
16037 matrix. Set row to the last row displaying text in current
16038 matrix starting at first_unchanged_at_end_row, after
16039 scrolling. */
16040 xassert (first_unchanged_at_end_row->displays_text_p);
16041 row = find_last_row_displaying_text (w->current_matrix, &it,
16042 first_unchanged_at_end_row);
16043 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16044
16045 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16046 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16047 w->window_end_vpos
16048 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16049 xassert (w->window_end_bytepos >= 0);
16050 IF_DEBUG (debug_method_add (w, "A"));
16051 }
16052 else if (last_text_row_at_end)
16053 {
16054 w->window_end_pos
16055 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16056 w->window_end_bytepos
16057 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16058 w->window_end_vpos
16059 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16060 xassert (w->window_end_bytepos >= 0);
16061 IF_DEBUG (debug_method_add (w, "B"));
16062 }
16063 else if (last_text_row)
16064 {
16065 /* We have displayed either to the end of the window or at the
16066 end of the window, i.e. the last row with text is to be found
16067 in the desired matrix. */
16068 w->window_end_pos
16069 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16070 w->window_end_bytepos
16071 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16072 w->window_end_vpos
16073 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16074 xassert (w->window_end_bytepos >= 0);
16075 }
16076 else if (first_unchanged_at_end_row == NULL
16077 && last_text_row == NULL
16078 && last_text_row_at_end == NULL)
16079 {
16080 /* Displayed to end of window, but no line containing text was
16081 displayed. Lines were deleted at the end of the window. */
16082 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16083 int vpos = XFASTINT (w->window_end_vpos);
16084 struct glyph_row *current_row = current_matrix->rows + vpos;
16085 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16086
16087 for (row = NULL;
16088 row == NULL && vpos >= first_vpos;
16089 --vpos, --current_row, --desired_row)
16090 {
16091 if (desired_row->enabled_p)
16092 {
16093 if (desired_row->displays_text_p)
16094 row = desired_row;
16095 }
16096 else if (current_row->displays_text_p)
16097 row = current_row;
16098 }
16099
16100 xassert (row != NULL);
16101 w->window_end_vpos = make_number (vpos + 1);
16102 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16103 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16104 xassert (w->window_end_bytepos >= 0);
16105 IF_DEBUG (debug_method_add (w, "C"));
16106 }
16107 else
16108 abort ();
16109
16110 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16111 debug_end_vpos = XFASTINT (w->window_end_vpos));
16112
16113 /* Record that display has not been completed. */
16114 w->window_end_valid = Qnil;
16115 w->desired_matrix->no_scrolling_p = 1;
16116 return 3;
16117
16118 #undef GIVE_UP
16119 }
16120
16121
16122 \f
16123 /***********************************************************************
16124 More debugging support
16125 ***********************************************************************/
16126
16127 #if GLYPH_DEBUG
16128
16129 void dump_glyph_row (struct glyph_row *, int, int);
16130 void dump_glyph_matrix (struct glyph_matrix *, int);
16131 void dump_glyph (struct glyph_row *, struct glyph *, int);
16132
16133
16134 /* Dump the contents of glyph matrix MATRIX on stderr.
16135
16136 GLYPHS 0 means don't show glyph contents.
16137 GLYPHS 1 means show glyphs in short form
16138 GLYPHS > 1 means show glyphs in long form. */
16139
16140 void
16141 dump_glyph_matrix (matrix, glyphs)
16142 struct glyph_matrix *matrix;
16143 int glyphs;
16144 {
16145 int i;
16146 for (i = 0; i < matrix->nrows; ++i)
16147 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16148 }
16149
16150
16151 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16152 the glyph row and area where the glyph comes from. */
16153
16154 void
16155 dump_glyph (row, glyph, area)
16156 struct glyph_row *row;
16157 struct glyph *glyph;
16158 int area;
16159 {
16160 if (glyph->type == CHAR_GLYPH)
16161 {
16162 fprintf (stderr,
16163 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16164 glyph - row->glyphs[TEXT_AREA],
16165 'C',
16166 glyph->charpos,
16167 (BUFFERP (glyph->object)
16168 ? 'B'
16169 : (STRINGP (glyph->object)
16170 ? 'S'
16171 : '-')),
16172 glyph->pixel_width,
16173 glyph->u.ch,
16174 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16175 ? glyph->u.ch
16176 : '.'),
16177 glyph->face_id,
16178 glyph->left_box_line_p,
16179 glyph->right_box_line_p);
16180 }
16181 else if (glyph->type == STRETCH_GLYPH)
16182 {
16183 fprintf (stderr,
16184 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16185 glyph - row->glyphs[TEXT_AREA],
16186 'S',
16187 glyph->charpos,
16188 (BUFFERP (glyph->object)
16189 ? 'B'
16190 : (STRINGP (glyph->object)
16191 ? 'S'
16192 : '-')),
16193 glyph->pixel_width,
16194 0,
16195 '.',
16196 glyph->face_id,
16197 glyph->left_box_line_p,
16198 glyph->right_box_line_p);
16199 }
16200 else if (glyph->type == IMAGE_GLYPH)
16201 {
16202 fprintf (stderr,
16203 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16204 glyph - row->glyphs[TEXT_AREA],
16205 'I',
16206 glyph->charpos,
16207 (BUFFERP (glyph->object)
16208 ? 'B'
16209 : (STRINGP (glyph->object)
16210 ? 'S'
16211 : '-')),
16212 glyph->pixel_width,
16213 glyph->u.img_id,
16214 '.',
16215 glyph->face_id,
16216 glyph->left_box_line_p,
16217 glyph->right_box_line_p);
16218 }
16219 else if (glyph->type == COMPOSITE_GLYPH)
16220 {
16221 fprintf (stderr,
16222 " %5d %4c %6d %c %3d 0x%05x",
16223 glyph - row->glyphs[TEXT_AREA],
16224 '+',
16225 glyph->charpos,
16226 (BUFFERP (glyph->object)
16227 ? 'B'
16228 : (STRINGP (glyph->object)
16229 ? 'S'
16230 : '-')),
16231 glyph->pixel_width,
16232 glyph->u.cmp.id);
16233 if (glyph->u.cmp.automatic)
16234 fprintf (stderr,
16235 "[%d-%d]",
16236 glyph->u.cmp.from, glyph->u.cmp.to);
16237 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16238 glyph->face_id,
16239 glyph->left_box_line_p,
16240 glyph->right_box_line_p);
16241 }
16242 }
16243
16244
16245 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16246 GLYPHS 0 means don't show glyph contents.
16247 GLYPHS 1 means show glyphs in short form
16248 GLYPHS > 1 means show glyphs in long form. */
16249
16250 void
16251 dump_glyph_row (row, vpos, glyphs)
16252 struct glyph_row *row;
16253 int vpos, glyphs;
16254 {
16255 if (glyphs != 1)
16256 {
16257 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16258 fprintf (stderr, "======================================================================\n");
16259
16260 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16261 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16262 vpos,
16263 MATRIX_ROW_START_CHARPOS (row),
16264 MATRIX_ROW_END_CHARPOS (row),
16265 row->used[TEXT_AREA],
16266 row->contains_overlapping_glyphs_p,
16267 row->enabled_p,
16268 row->truncated_on_left_p,
16269 row->truncated_on_right_p,
16270 row->continued_p,
16271 MATRIX_ROW_CONTINUATION_LINE_P (row),
16272 row->displays_text_p,
16273 row->ends_at_zv_p,
16274 row->fill_line_p,
16275 row->ends_in_middle_of_char_p,
16276 row->starts_in_middle_of_char_p,
16277 row->mouse_face_p,
16278 row->x,
16279 row->y,
16280 row->pixel_width,
16281 row->height,
16282 row->visible_height,
16283 row->ascent,
16284 row->phys_ascent);
16285 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16286 row->end.overlay_string_index,
16287 row->continuation_lines_width);
16288 fprintf (stderr, "%9d %5d\n",
16289 CHARPOS (row->start.string_pos),
16290 CHARPOS (row->end.string_pos));
16291 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16292 row->end.dpvec_index);
16293 }
16294
16295 if (glyphs > 1)
16296 {
16297 int area;
16298
16299 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16300 {
16301 struct glyph *glyph = row->glyphs[area];
16302 struct glyph *glyph_end = glyph + row->used[area];
16303
16304 /* Glyph for a line end in text. */
16305 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16306 ++glyph_end;
16307
16308 if (glyph < glyph_end)
16309 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16310
16311 for (; glyph < glyph_end; ++glyph)
16312 dump_glyph (row, glyph, area);
16313 }
16314 }
16315 else if (glyphs == 1)
16316 {
16317 int area;
16318
16319 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16320 {
16321 char *s = (char *) alloca (row->used[area] + 1);
16322 int i;
16323
16324 for (i = 0; i < row->used[area]; ++i)
16325 {
16326 struct glyph *glyph = row->glyphs[area] + i;
16327 if (glyph->type == CHAR_GLYPH
16328 && glyph->u.ch < 0x80
16329 && glyph->u.ch >= ' ')
16330 s[i] = glyph->u.ch;
16331 else
16332 s[i] = '.';
16333 }
16334
16335 s[i] = '\0';
16336 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16337 }
16338 }
16339 }
16340
16341
16342 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16343 Sdump_glyph_matrix, 0, 1, "p",
16344 doc: /* Dump the current matrix of the selected window to stderr.
16345 Shows contents of glyph row structures. With non-nil
16346 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16347 glyphs in short form, otherwise show glyphs in long form. */)
16348 (Lisp_Object glyphs)
16349 {
16350 struct window *w = XWINDOW (selected_window);
16351 struct buffer *buffer = XBUFFER (w->buffer);
16352
16353 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16354 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16355 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16356 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16357 fprintf (stderr, "=============================================\n");
16358 dump_glyph_matrix (w->current_matrix,
16359 NILP (glyphs) ? 0 : XINT (glyphs));
16360 return Qnil;
16361 }
16362
16363
16364 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16365 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16366 (void)
16367 {
16368 struct frame *f = XFRAME (selected_frame);
16369 dump_glyph_matrix (f->current_matrix, 1);
16370 return Qnil;
16371 }
16372
16373
16374 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16375 doc: /* Dump glyph row ROW to stderr.
16376 GLYPH 0 means don't dump glyphs.
16377 GLYPH 1 means dump glyphs in short form.
16378 GLYPH > 1 or omitted means dump glyphs in long form. */)
16379 (Lisp_Object row, Lisp_Object glyphs)
16380 {
16381 struct glyph_matrix *matrix;
16382 int vpos;
16383
16384 CHECK_NUMBER (row);
16385 matrix = XWINDOW (selected_window)->current_matrix;
16386 vpos = XINT (row);
16387 if (vpos >= 0 && vpos < matrix->nrows)
16388 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16389 vpos,
16390 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16391 return Qnil;
16392 }
16393
16394
16395 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16396 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16397 GLYPH 0 means don't dump glyphs.
16398 GLYPH 1 means dump glyphs in short form.
16399 GLYPH > 1 or omitted means dump glyphs in long form. */)
16400 (Lisp_Object row, Lisp_Object glyphs)
16401 {
16402 struct frame *sf = SELECTED_FRAME ();
16403 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16404 int vpos;
16405
16406 CHECK_NUMBER (row);
16407 vpos = XINT (row);
16408 if (vpos >= 0 && vpos < m->nrows)
16409 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16410 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16411 return Qnil;
16412 }
16413
16414
16415 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16416 doc: /* Toggle tracing of redisplay.
16417 With ARG, turn tracing on if and only if ARG is positive. */)
16418 (Lisp_Object arg)
16419 {
16420 if (NILP (arg))
16421 trace_redisplay_p = !trace_redisplay_p;
16422 else
16423 {
16424 arg = Fprefix_numeric_value (arg);
16425 trace_redisplay_p = XINT (arg) > 0;
16426 }
16427
16428 return Qnil;
16429 }
16430
16431
16432 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16433 doc: /* Like `format', but print result to stderr.
16434 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16435 (int nargs, Lisp_Object *args)
16436 {
16437 Lisp_Object s = Fformat (nargs, args);
16438 fprintf (stderr, "%s", SDATA (s));
16439 return Qnil;
16440 }
16441
16442 #endif /* GLYPH_DEBUG */
16443
16444
16445 \f
16446 /***********************************************************************
16447 Building Desired Matrix Rows
16448 ***********************************************************************/
16449
16450 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16451 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16452
16453 static struct glyph_row *
16454 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16455 {
16456 struct frame *f = XFRAME (WINDOW_FRAME (w));
16457 struct buffer *buffer = XBUFFER (w->buffer);
16458 struct buffer *old = current_buffer;
16459 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16460 int arrow_len = SCHARS (overlay_arrow_string);
16461 const unsigned char *arrow_end = arrow_string + arrow_len;
16462 const unsigned char *p;
16463 struct it it;
16464 int multibyte_p;
16465 int n_glyphs_before;
16466
16467 set_buffer_temp (buffer);
16468 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16469 it.glyph_row->used[TEXT_AREA] = 0;
16470 SET_TEXT_POS (it.position, 0, 0);
16471
16472 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16473 p = arrow_string;
16474 while (p < arrow_end)
16475 {
16476 Lisp_Object face, ilisp;
16477
16478 /* Get the next character. */
16479 if (multibyte_p)
16480 it.c = string_char_and_length (p, &it.len);
16481 else
16482 it.c = *p, it.len = 1;
16483 p += it.len;
16484
16485 /* Get its face. */
16486 ilisp = make_number (p - arrow_string);
16487 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16488 it.face_id = compute_char_face (f, it.c, face);
16489
16490 /* Compute its width, get its glyphs. */
16491 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16492 SET_TEXT_POS (it.position, -1, -1);
16493 PRODUCE_GLYPHS (&it);
16494
16495 /* If this character doesn't fit any more in the line, we have
16496 to remove some glyphs. */
16497 if (it.current_x > it.last_visible_x)
16498 {
16499 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16500 break;
16501 }
16502 }
16503
16504 set_buffer_temp (old);
16505 return it.glyph_row;
16506 }
16507
16508
16509 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16510 glyphs are only inserted for terminal frames since we can't really
16511 win with truncation glyphs when partially visible glyphs are
16512 involved. Which glyphs to insert is determined by
16513 produce_special_glyphs. */
16514
16515 static void
16516 insert_left_trunc_glyphs (struct it *it)
16517 {
16518 struct it truncate_it;
16519 struct glyph *from, *end, *to, *toend;
16520
16521 xassert (!FRAME_WINDOW_P (it->f));
16522
16523 /* Get the truncation glyphs. */
16524 truncate_it = *it;
16525 truncate_it.current_x = 0;
16526 truncate_it.face_id = DEFAULT_FACE_ID;
16527 truncate_it.glyph_row = &scratch_glyph_row;
16528 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16529 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16530 truncate_it.object = make_number (0);
16531 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16532
16533 /* Overwrite glyphs from IT with truncation glyphs. */
16534 if (!it->glyph_row->reversed_p)
16535 {
16536 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16537 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16538 to = it->glyph_row->glyphs[TEXT_AREA];
16539 toend = to + it->glyph_row->used[TEXT_AREA];
16540
16541 while (from < end)
16542 *to++ = *from++;
16543
16544 /* There may be padding glyphs left over. Overwrite them too. */
16545 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16546 {
16547 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16548 while (from < end)
16549 *to++ = *from++;
16550 }
16551
16552 if (to > toend)
16553 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16554 }
16555 else
16556 {
16557 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
16558 that back to front. */
16559 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
16560 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16561 toend = it->glyph_row->glyphs[TEXT_AREA];
16562 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
16563
16564 while (from >= end && to >= toend)
16565 *to-- = *from--;
16566 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
16567 {
16568 from =
16569 truncate_it.glyph_row->glyphs[TEXT_AREA]
16570 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16571 while (from >= end && to >= toend)
16572 *to-- = *from--;
16573 }
16574 if (from >= end)
16575 {
16576 /* Need to free some room before prepending additional
16577 glyphs. */
16578 int move_by = from - end + 1;
16579 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
16580 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
16581
16582 for ( ; g >= g0; g--)
16583 g[move_by] = *g;
16584 while (from >= end)
16585 *to-- = *from--;
16586 it->glyph_row->used[TEXT_AREA] += move_by;
16587 }
16588 }
16589 }
16590
16591
16592 /* Compute the pixel height and width of IT->glyph_row.
16593
16594 Most of the time, ascent and height of a display line will be equal
16595 to the max_ascent and max_height values of the display iterator
16596 structure. This is not the case if
16597
16598 1. We hit ZV without displaying anything. In this case, max_ascent
16599 and max_height will be zero.
16600
16601 2. We have some glyphs that don't contribute to the line height.
16602 (The glyph row flag contributes_to_line_height_p is for future
16603 pixmap extensions).
16604
16605 The first case is easily covered by using default values because in
16606 these cases, the line height does not really matter, except that it
16607 must not be zero. */
16608
16609 static void
16610 compute_line_metrics (struct it *it)
16611 {
16612 struct glyph_row *row = it->glyph_row;
16613 int area, i;
16614
16615 if (FRAME_WINDOW_P (it->f))
16616 {
16617 int i, min_y, max_y;
16618
16619 /* The line may consist of one space only, that was added to
16620 place the cursor on it. If so, the row's height hasn't been
16621 computed yet. */
16622 if (row->height == 0)
16623 {
16624 if (it->max_ascent + it->max_descent == 0)
16625 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16626 row->ascent = it->max_ascent;
16627 row->height = it->max_ascent + it->max_descent;
16628 row->phys_ascent = it->max_phys_ascent;
16629 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16630 row->extra_line_spacing = it->max_extra_line_spacing;
16631 }
16632
16633 /* Compute the width of this line. */
16634 row->pixel_width = row->x;
16635 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16636 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16637
16638 xassert (row->pixel_width >= 0);
16639 xassert (row->ascent >= 0 && row->height > 0);
16640
16641 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16642 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16643
16644 /* If first line's physical ascent is larger than its logical
16645 ascent, use the physical ascent, and make the row taller.
16646 This makes accented characters fully visible. */
16647 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16648 && row->phys_ascent > row->ascent)
16649 {
16650 row->height += row->phys_ascent - row->ascent;
16651 row->ascent = row->phys_ascent;
16652 }
16653
16654 /* Compute how much of the line is visible. */
16655 row->visible_height = row->height;
16656
16657 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16658 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16659
16660 if (row->y < min_y)
16661 row->visible_height -= min_y - row->y;
16662 if (row->y + row->height > max_y)
16663 row->visible_height -= row->y + row->height - max_y;
16664 }
16665 else
16666 {
16667 row->pixel_width = row->used[TEXT_AREA];
16668 if (row->continued_p)
16669 row->pixel_width -= it->continuation_pixel_width;
16670 else if (row->truncated_on_right_p)
16671 row->pixel_width -= it->truncation_pixel_width;
16672 row->ascent = row->phys_ascent = 0;
16673 row->height = row->phys_height = row->visible_height = 1;
16674 row->extra_line_spacing = 0;
16675 }
16676
16677 /* Compute a hash code for this row. */
16678 row->hash = 0;
16679 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16680 for (i = 0; i < row->used[area]; ++i)
16681 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16682 + row->glyphs[area][i].u.val
16683 + row->glyphs[area][i].face_id
16684 + row->glyphs[area][i].padding_p
16685 + (row->glyphs[area][i].type << 2));
16686
16687 it->max_ascent = it->max_descent = 0;
16688 it->max_phys_ascent = it->max_phys_descent = 0;
16689 }
16690
16691
16692 /* Append one space to the glyph row of iterator IT if doing a
16693 window-based redisplay. The space has the same face as
16694 IT->face_id. Value is non-zero if a space was added.
16695
16696 This function is called to make sure that there is always one glyph
16697 at the end of a glyph row that the cursor can be set on under
16698 window-systems. (If there weren't such a glyph we would not know
16699 how wide and tall a box cursor should be displayed).
16700
16701 At the same time this space let's a nicely handle clearing to the
16702 end of the line if the row ends in italic text. */
16703
16704 static int
16705 append_space_for_newline (struct it *it, int default_face_p)
16706 {
16707 if (FRAME_WINDOW_P (it->f))
16708 {
16709 int n = it->glyph_row->used[TEXT_AREA];
16710
16711 if (it->glyph_row->glyphs[TEXT_AREA] + n
16712 < it->glyph_row->glyphs[1 + TEXT_AREA])
16713 {
16714 /* Save some values that must not be changed.
16715 Must save IT->c and IT->len because otherwise
16716 ITERATOR_AT_END_P wouldn't work anymore after
16717 append_space_for_newline has been called. */
16718 enum display_element_type saved_what = it->what;
16719 int saved_c = it->c, saved_len = it->len;
16720 int saved_x = it->current_x;
16721 int saved_face_id = it->face_id;
16722 struct text_pos saved_pos;
16723 Lisp_Object saved_object;
16724 struct face *face;
16725
16726 saved_object = it->object;
16727 saved_pos = it->position;
16728
16729 it->what = IT_CHARACTER;
16730 memset (&it->position, 0, sizeof it->position);
16731 it->object = make_number (0);
16732 it->c = ' ';
16733 it->len = 1;
16734
16735 if (default_face_p)
16736 it->face_id = DEFAULT_FACE_ID;
16737 else if (it->face_before_selective_p)
16738 it->face_id = it->saved_face_id;
16739 face = FACE_FROM_ID (it->f, it->face_id);
16740 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16741
16742 PRODUCE_GLYPHS (it);
16743
16744 it->override_ascent = -1;
16745 it->constrain_row_ascent_descent_p = 0;
16746 it->current_x = saved_x;
16747 it->object = saved_object;
16748 it->position = saved_pos;
16749 it->what = saved_what;
16750 it->face_id = saved_face_id;
16751 it->len = saved_len;
16752 it->c = saved_c;
16753 return 1;
16754 }
16755 }
16756
16757 return 0;
16758 }
16759
16760
16761 /* Extend the face of the last glyph in the text area of IT->glyph_row
16762 to the end of the display line. Called from display_line. If the
16763 glyph row is empty, add a space glyph to it so that we know the
16764 face to draw. Set the glyph row flag fill_line_p. If the glyph
16765 row is R2L, prepend a stretch glyph to cover the empty space to the
16766 left of the leftmost glyph. */
16767
16768 static void
16769 extend_face_to_end_of_line (struct it *it)
16770 {
16771 struct face *face;
16772 struct frame *f = it->f;
16773
16774 /* If line is already filled, do nothing. Non window-system frames
16775 get a grace of one more ``pixel'' because their characters are
16776 1-``pixel'' wide, so they hit the equality too early. This grace
16777 is needed only for R2L rows that are not continued, to produce
16778 one extra blank where we could display the cursor. */
16779 if (it->current_x >= it->last_visible_x
16780 + (!FRAME_WINDOW_P (f)
16781 && it->glyph_row->reversed_p
16782 && !it->glyph_row->continued_p))
16783 return;
16784
16785 /* Face extension extends the background and box of IT->face_id
16786 to the end of the line. If the background equals the background
16787 of the frame, we don't have to do anything. */
16788 if (it->face_before_selective_p)
16789 face = FACE_FROM_ID (f, it->saved_face_id);
16790 else
16791 face = FACE_FROM_ID (f, it->face_id);
16792
16793 if (FRAME_WINDOW_P (f)
16794 && it->glyph_row->displays_text_p
16795 && face->box == FACE_NO_BOX
16796 && face->background == FRAME_BACKGROUND_PIXEL (f)
16797 && !face->stipple
16798 && !it->glyph_row->reversed_p)
16799 return;
16800
16801 /* Set the glyph row flag indicating that the face of the last glyph
16802 in the text area has to be drawn to the end of the text area. */
16803 it->glyph_row->fill_line_p = 1;
16804
16805 /* If current character of IT is not ASCII, make sure we have the
16806 ASCII face. This will be automatically undone the next time
16807 get_next_display_element returns a multibyte character. Note
16808 that the character will always be single byte in unibyte
16809 text. */
16810 if (!ASCII_CHAR_P (it->c))
16811 {
16812 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16813 }
16814
16815 if (FRAME_WINDOW_P (f))
16816 {
16817 /* If the row is empty, add a space with the current face of IT,
16818 so that we know which face to draw. */
16819 if (it->glyph_row->used[TEXT_AREA] == 0)
16820 {
16821 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16822 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16823 it->glyph_row->used[TEXT_AREA] = 1;
16824 }
16825 #ifdef HAVE_WINDOW_SYSTEM
16826 if (it->glyph_row->reversed_p)
16827 {
16828 /* Prepend a stretch glyph to the row, such that the
16829 rightmost glyph will be drawn flushed all the way to the
16830 right margin of the window. The stretch glyph that will
16831 occupy the empty space, if any, to the left of the
16832 glyphs. */
16833 struct font *font = face->font ? face->font : FRAME_FONT (f);
16834 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
16835 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
16836 struct glyph *g;
16837 int row_width, stretch_ascent, stretch_width;
16838 struct text_pos saved_pos;
16839 int saved_face_id, saved_avoid_cursor;
16840
16841 for (row_width = 0, g = row_start; g < row_end; g++)
16842 row_width += g->pixel_width;
16843 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
16844 if (stretch_width > 0)
16845 {
16846 stretch_ascent =
16847 (((it->ascent + it->descent)
16848 * FONT_BASE (font)) / FONT_HEIGHT (font));
16849 saved_pos = it->position;
16850 memset (&it->position, 0, sizeof it->position);
16851 saved_avoid_cursor = it->avoid_cursor_p;
16852 it->avoid_cursor_p = 1;
16853 saved_face_id = it->face_id;
16854 /* The last row's stretch glyph should get the default
16855 face, to avoid painting the rest of the window with
16856 the region face, if the region ends at ZV. */
16857 if (it->glyph_row->ends_at_zv_p)
16858 it->face_id = DEFAULT_FACE_ID;
16859 else
16860 it->face_id = face->id;
16861 append_stretch_glyph (it, make_number (0), stretch_width,
16862 it->ascent + it->descent, stretch_ascent);
16863 it->position = saved_pos;
16864 it->avoid_cursor_p = saved_avoid_cursor;
16865 it->face_id = saved_face_id;
16866 }
16867 }
16868 #endif /* HAVE_WINDOW_SYSTEM */
16869 }
16870 else
16871 {
16872 /* Save some values that must not be changed. */
16873 int saved_x = it->current_x;
16874 struct text_pos saved_pos;
16875 Lisp_Object saved_object;
16876 enum display_element_type saved_what = it->what;
16877 int saved_face_id = it->face_id;
16878
16879 saved_object = it->object;
16880 saved_pos = it->position;
16881
16882 it->what = IT_CHARACTER;
16883 memset (&it->position, 0, sizeof it->position);
16884 it->object = make_number (0);
16885 it->c = ' ';
16886 it->len = 1;
16887 /* The last row's blank glyphs should get the default face, to
16888 avoid painting the rest of the window with the region face,
16889 if the region ends at ZV. */
16890 if (it->glyph_row->ends_at_zv_p)
16891 it->face_id = DEFAULT_FACE_ID;
16892 else
16893 it->face_id = face->id;
16894
16895 PRODUCE_GLYPHS (it);
16896
16897 while (it->current_x <= it->last_visible_x)
16898 PRODUCE_GLYPHS (it);
16899
16900 /* Don't count these blanks really. It would let us insert a left
16901 truncation glyph below and make us set the cursor on them, maybe. */
16902 it->current_x = saved_x;
16903 it->object = saved_object;
16904 it->position = saved_pos;
16905 it->what = saved_what;
16906 it->face_id = saved_face_id;
16907 }
16908 }
16909
16910
16911 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16912 trailing whitespace. */
16913
16914 static int
16915 trailing_whitespace_p (int charpos)
16916 {
16917 int bytepos = CHAR_TO_BYTE (charpos);
16918 int c = 0;
16919
16920 while (bytepos < ZV_BYTE
16921 && (c = FETCH_CHAR (bytepos),
16922 c == ' ' || c == '\t'))
16923 ++bytepos;
16924
16925 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16926 {
16927 if (bytepos != PT_BYTE)
16928 return 1;
16929 }
16930 return 0;
16931 }
16932
16933
16934 /* Highlight trailing whitespace, if any, in ROW. */
16935
16936 void
16937 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
16938 {
16939 int used = row->used[TEXT_AREA];
16940
16941 if (used)
16942 {
16943 struct glyph *start = row->glyphs[TEXT_AREA];
16944 struct glyph *glyph = start + used - 1;
16945
16946 if (row->reversed_p)
16947 {
16948 /* Right-to-left rows need to be processed in the opposite
16949 direction, so swap the edge pointers. */
16950 glyph = start;
16951 start = row->glyphs[TEXT_AREA] + used - 1;
16952 }
16953
16954 /* Skip over glyphs inserted to display the cursor at the
16955 end of a line, for extending the face of the last glyph
16956 to the end of the line on terminals, and for truncation
16957 and continuation glyphs. */
16958 if (!row->reversed_p)
16959 {
16960 while (glyph >= start
16961 && glyph->type == CHAR_GLYPH
16962 && INTEGERP (glyph->object))
16963 --glyph;
16964 }
16965 else
16966 {
16967 while (glyph <= start
16968 && glyph->type == CHAR_GLYPH
16969 && INTEGERP (glyph->object))
16970 ++glyph;
16971 }
16972
16973 /* If last glyph is a space or stretch, and it's trailing
16974 whitespace, set the face of all trailing whitespace glyphs in
16975 IT->glyph_row to `trailing-whitespace'. */
16976 if ((row->reversed_p ? glyph <= start : glyph >= start)
16977 && BUFFERP (glyph->object)
16978 && (glyph->type == STRETCH_GLYPH
16979 || (glyph->type == CHAR_GLYPH
16980 && glyph->u.ch == ' '))
16981 && trailing_whitespace_p (glyph->charpos))
16982 {
16983 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16984 if (face_id < 0)
16985 return;
16986
16987 if (!row->reversed_p)
16988 {
16989 while (glyph >= start
16990 && BUFFERP (glyph->object)
16991 && (glyph->type == STRETCH_GLYPH
16992 || (glyph->type == CHAR_GLYPH
16993 && glyph->u.ch == ' ')))
16994 (glyph--)->face_id = face_id;
16995 }
16996 else
16997 {
16998 while (glyph <= start
16999 && BUFFERP (glyph->object)
17000 && (glyph->type == STRETCH_GLYPH
17001 || (glyph->type == CHAR_GLYPH
17002 && glyph->u.ch == ' ')))
17003 (glyph++)->face_id = face_id;
17004 }
17005 }
17006 }
17007 }
17008
17009
17010 /* Value is non-zero if glyph row ROW in window W should be
17011 used to hold the cursor. */
17012
17013 static int
17014 cursor_row_p (struct window *w, struct glyph_row *row)
17015 {
17016 int cursor_row_p = 1;
17017
17018 if (PT == CHARPOS (row->end.pos))
17019 {
17020 /* Suppose the row ends on a string.
17021 Unless the row is continued, that means it ends on a newline
17022 in the string. If it's anything other than a display string
17023 (e.g. a before-string from an overlay), we don't want the
17024 cursor there. (This heuristic seems to give the optimal
17025 behavior for the various types of multi-line strings.) */
17026 if (CHARPOS (row->end.string_pos) >= 0)
17027 {
17028 if (row->continued_p)
17029 cursor_row_p = 1;
17030 else
17031 {
17032 /* Check for `display' property. */
17033 struct glyph *beg = row->glyphs[TEXT_AREA];
17034 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17035 struct glyph *glyph;
17036
17037 cursor_row_p = 0;
17038 for (glyph = end; glyph >= beg; --glyph)
17039 if (STRINGP (glyph->object))
17040 {
17041 Lisp_Object prop
17042 = Fget_char_property (make_number (PT),
17043 Qdisplay, Qnil);
17044 cursor_row_p =
17045 (!NILP (prop)
17046 && display_prop_string_p (prop, glyph->object));
17047 break;
17048 }
17049 }
17050 }
17051 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17052 {
17053 /* If the row ends in middle of a real character,
17054 and the line is continued, we want the cursor here.
17055 That's because CHARPOS (ROW->end.pos) would equal
17056 PT if PT is before the character. */
17057 if (!row->ends_in_ellipsis_p)
17058 cursor_row_p = row->continued_p;
17059 else
17060 /* If the row ends in an ellipsis, then
17061 CHARPOS (ROW->end.pos) will equal point after the
17062 invisible text. We want that position to be displayed
17063 after the ellipsis. */
17064 cursor_row_p = 0;
17065 }
17066 /* If the row ends at ZV, display the cursor at the end of that
17067 row instead of at the start of the row below. */
17068 else if (row->ends_at_zv_p)
17069 cursor_row_p = 1;
17070 else
17071 cursor_row_p = 0;
17072 }
17073
17074 return cursor_row_p;
17075 }
17076
17077 \f
17078
17079 /* Push the display property PROP so that it will be rendered at the
17080 current position in IT. Return 1 if PROP was successfully pushed,
17081 0 otherwise. */
17082
17083 static int
17084 push_display_prop (struct it *it, Lisp_Object prop)
17085 {
17086 push_it (it);
17087
17088 if (STRINGP (prop))
17089 {
17090 if (SCHARS (prop) == 0)
17091 {
17092 pop_it (it);
17093 return 0;
17094 }
17095
17096 it->string = prop;
17097 it->multibyte_p = STRING_MULTIBYTE (it->string);
17098 it->current.overlay_string_index = -1;
17099 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17100 it->end_charpos = it->string_nchars = SCHARS (it->string);
17101 it->method = GET_FROM_STRING;
17102 it->stop_charpos = 0;
17103 }
17104 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17105 {
17106 it->method = GET_FROM_STRETCH;
17107 it->object = prop;
17108 }
17109 #ifdef HAVE_WINDOW_SYSTEM
17110 else if (IMAGEP (prop))
17111 {
17112 it->what = IT_IMAGE;
17113 it->image_id = lookup_image (it->f, prop);
17114 it->method = GET_FROM_IMAGE;
17115 }
17116 #endif /* HAVE_WINDOW_SYSTEM */
17117 else
17118 {
17119 pop_it (it); /* bogus display property, give up */
17120 return 0;
17121 }
17122
17123 return 1;
17124 }
17125
17126 /* Return the character-property PROP at the current position in IT. */
17127
17128 static Lisp_Object
17129 get_it_property (struct it *it, Lisp_Object prop)
17130 {
17131 Lisp_Object position;
17132
17133 if (STRINGP (it->object))
17134 position = make_number (IT_STRING_CHARPOS (*it));
17135 else if (BUFFERP (it->object))
17136 position = make_number (IT_CHARPOS (*it));
17137 else
17138 return Qnil;
17139
17140 return Fget_char_property (position, prop, it->object);
17141 }
17142
17143 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17144
17145 static void
17146 handle_line_prefix (struct it *it)
17147 {
17148 Lisp_Object prefix;
17149 if (it->continuation_lines_width > 0)
17150 {
17151 prefix = get_it_property (it, Qwrap_prefix);
17152 if (NILP (prefix))
17153 prefix = Vwrap_prefix;
17154 }
17155 else
17156 {
17157 prefix = get_it_property (it, Qline_prefix);
17158 if (NILP (prefix))
17159 prefix = Vline_prefix;
17160 }
17161 if (! NILP (prefix) && push_display_prop (it, prefix))
17162 {
17163 /* If the prefix is wider than the window, and we try to wrap
17164 it, it would acquire its own wrap prefix, and so on till the
17165 iterator stack overflows. So, don't wrap the prefix. */
17166 it->line_wrap = TRUNCATE;
17167 it->avoid_cursor_p = 1;
17168 }
17169 }
17170
17171 \f
17172
17173 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17174 only for R2L lines from display_line, when it decides that too many
17175 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be
17176 continued. */
17177 static void
17178 unproduce_glyphs (struct it *it, int n)
17179 {
17180 struct glyph *glyph, *end;
17181
17182 xassert (it->glyph_row);
17183 xassert (it->glyph_row->reversed_p);
17184 xassert (it->area == TEXT_AREA);
17185 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17186
17187 if (n > it->glyph_row->used[TEXT_AREA])
17188 n = it->glyph_row->used[TEXT_AREA];
17189 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17190 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17191 for ( ; glyph < end; glyph++)
17192 glyph[-n] = *glyph;
17193 }
17194
17195 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17196 and ROW->maxpos. */
17197 static void
17198 find_row_edges (struct it *it, struct glyph_row *row,
17199 EMACS_INT min_pos, EMACS_INT min_bpos,
17200 EMACS_INT max_pos, EMACS_INT max_bpos)
17201 {
17202 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17203 lines' rows is implemented for bidi-reordered rows. */
17204
17205 /* ROW->minpos is the value of min_pos, the minimal buffer position
17206 we have in ROW. */
17207 if (min_pos <= ZV)
17208 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17209 else
17210 {
17211 /* We didn't find _any_ valid buffer positions in any of the
17212 glyphs, so we must trust the iterator's computed
17213 positions. */
17214 row->minpos = row->start.pos;
17215 max_pos = CHARPOS (it->current.pos);
17216 max_bpos = BYTEPOS (it->current.pos);
17217 }
17218
17219 if (!max_pos)
17220 abort ();
17221
17222 /* Here are the various use-cases for ending the row, and the
17223 corresponding values for ROW->maxpos:
17224
17225 Line ends in a newline from buffer eol_pos + 1
17226 Line is continued from buffer max_pos + 1
17227 Line is truncated on right it->current.pos
17228 Line ends in a newline from string max_pos
17229 Line is continued from string max_pos
17230 Line is continued from display vector max_pos
17231 Line is entirely from a string min_pos == max_pos
17232 Line is entirely from a display vector min_pos == max_pos
17233 Line that ends at ZV ZV
17234
17235 If you discover other use-cases, please add them here as
17236 appropriate. */
17237 if (row->ends_at_zv_p)
17238 row->maxpos = it->current.pos;
17239 else if (row->used[TEXT_AREA])
17240 {
17241 if (row->ends_in_newline_from_string_p)
17242 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17243 else if (CHARPOS (it->eol_pos) > 0)
17244 SET_TEXT_POS (row->maxpos,
17245 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17246 else if (row->continued_p)
17247 {
17248 /* If max_pos is different from IT's current position, it
17249 means IT->method does not belong to the display element
17250 at max_pos. However, it also means that the display
17251 element at max_pos was displayed in its entirety on this
17252 line, which is equivalent to saying that the next line
17253 starts at the next buffer position. */
17254 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17255 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17256 else
17257 {
17258 INC_BOTH (max_pos, max_bpos);
17259 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17260 }
17261 }
17262 else if (row->truncated_on_right_p)
17263 /* display_line already called reseat_at_next_visible_line_start,
17264 which puts the iterator at the beginning of the next line, in
17265 the logical order. */
17266 row->maxpos = it->current.pos;
17267 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17268 /* A line that is entirely from a string/image/stretch... */
17269 row->maxpos = row->minpos;
17270 else
17271 abort ();
17272 }
17273 else
17274 row->maxpos = it->current.pos;
17275 }
17276
17277 /* Construct the glyph row IT->glyph_row in the desired matrix of
17278 IT->w from text at the current position of IT. See dispextern.h
17279 for an overview of struct it. Value is non-zero if
17280 IT->glyph_row displays text, as opposed to a line displaying ZV
17281 only. */
17282
17283 static int
17284 display_line (struct it *it)
17285 {
17286 struct glyph_row *row = it->glyph_row;
17287 Lisp_Object overlay_arrow_string;
17288 struct it wrap_it;
17289 int may_wrap = 0, wrap_x;
17290 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17291 int wrap_row_phys_ascent, wrap_row_phys_height;
17292 int wrap_row_extra_line_spacing;
17293 EMACS_INT wrap_row_min_pos, wrap_row_min_bpos;
17294 EMACS_INT wrap_row_max_pos, wrap_row_max_bpos;
17295 int cvpos;
17296 EMACS_INT min_pos = ZV + 1, min_bpos, max_pos = 0, max_bpos;
17297
17298 /* We always start displaying at hpos zero even if hscrolled. */
17299 xassert (it->hpos == 0 && it->current_x == 0);
17300
17301 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17302 >= it->w->desired_matrix->nrows)
17303 {
17304 it->w->nrows_scale_factor++;
17305 fonts_changed_p = 1;
17306 return 0;
17307 }
17308
17309 /* Is IT->w showing the region? */
17310 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17311
17312 /* Clear the result glyph row and enable it. */
17313 prepare_desired_row (row);
17314
17315 row->y = it->current_y;
17316 row->start = it->start;
17317 row->continuation_lines_width = it->continuation_lines_width;
17318 row->displays_text_p = 1;
17319 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17320 it->starts_in_middle_of_char_p = 0;
17321
17322 /* Arrange the overlays nicely for our purposes. Usually, we call
17323 display_line on only one line at a time, in which case this
17324 can't really hurt too much, or we call it on lines which appear
17325 one after another in the buffer, in which case all calls to
17326 recenter_overlay_lists but the first will be pretty cheap. */
17327 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17328
17329 /* Move over display elements that are not visible because we are
17330 hscrolled. This may stop at an x-position < IT->first_visible_x
17331 if the first glyph is partially visible or if we hit a line end. */
17332 if (it->current_x < it->first_visible_x)
17333 {
17334 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17335 MOVE_TO_POS | MOVE_TO_X);
17336 }
17337 else
17338 {
17339 /* We only do this when not calling `move_it_in_display_line_to'
17340 above, because move_it_in_display_line_to calls
17341 handle_line_prefix itself. */
17342 handle_line_prefix (it);
17343 }
17344
17345 /* Get the initial row height. This is either the height of the
17346 text hscrolled, if there is any, or zero. */
17347 row->ascent = it->max_ascent;
17348 row->height = it->max_ascent + it->max_descent;
17349 row->phys_ascent = it->max_phys_ascent;
17350 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17351 row->extra_line_spacing = it->max_extra_line_spacing;
17352
17353 /* Utility macro to record max and min buffer positions seen until now. */
17354 #define RECORD_MAX_MIN_POS(IT) \
17355 do \
17356 { \
17357 if (IT_CHARPOS (*(IT)) < min_pos) \
17358 { \
17359 min_pos = IT_CHARPOS (*(IT)); \
17360 min_bpos = IT_BYTEPOS (*(IT)); \
17361 } \
17362 if (IT_CHARPOS (*(IT)) > max_pos) \
17363 { \
17364 max_pos = IT_CHARPOS (*(IT)); \
17365 max_bpos = IT_BYTEPOS (*(IT)); \
17366 } \
17367 } \
17368 while (0)
17369
17370 /* Loop generating characters. The loop is left with IT on the next
17371 character to display. */
17372 while (1)
17373 {
17374 int n_glyphs_before, hpos_before, x_before;
17375 int x, i, nglyphs;
17376 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17377
17378 /* Retrieve the next thing to display. Value is zero if end of
17379 buffer reached. */
17380 if (!get_next_display_element (it))
17381 {
17382 /* Maybe add a space at the end of this line that is used to
17383 display the cursor there under X. Set the charpos of the
17384 first glyph of blank lines not corresponding to any text
17385 to -1. */
17386 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17387 row->exact_window_width_line_p = 1;
17388 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17389 || row->used[TEXT_AREA] == 0)
17390 {
17391 row->glyphs[TEXT_AREA]->charpos = -1;
17392 row->displays_text_p = 0;
17393
17394 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17395 && (!MINI_WINDOW_P (it->w)
17396 || (minibuf_level && EQ (it->window, minibuf_window))))
17397 row->indicate_empty_line_p = 1;
17398 }
17399
17400 it->continuation_lines_width = 0;
17401 row->ends_at_zv_p = 1;
17402 /* A row that displays right-to-left text must always have
17403 its last face extended all the way to the end of line,
17404 even if this row ends in ZV, because we still write to
17405 the screen left to right. */
17406 if (row->reversed_p)
17407 extend_face_to_end_of_line (it);
17408 break;
17409 }
17410
17411 /* Now, get the metrics of what we want to display. This also
17412 generates glyphs in `row' (which is IT->glyph_row). */
17413 n_glyphs_before = row->used[TEXT_AREA];
17414 x = it->current_x;
17415
17416 /* Remember the line height so far in case the next element doesn't
17417 fit on the line. */
17418 if (it->line_wrap != TRUNCATE)
17419 {
17420 ascent = it->max_ascent;
17421 descent = it->max_descent;
17422 phys_ascent = it->max_phys_ascent;
17423 phys_descent = it->max_phys_descent;
17424
17425 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17426 {
17427 if (IT_DISPLAYING_WHITESPACE (it))
17428 may_wrap = 1;
17429 else if (may_wrap)
17430 {
17431 wrap_it = *it;
17432 wrap_x = x;
17433 wrap_row_used = row->used[TEXT_AREA];
17434 wrap_row_ascent = row->ascent;
17435 wrap_row_height = row->height;
17436 wrap_row_phys_ascent = row->phys_ascent;
17437 wrap_row_phys_height = row->phys_height;
17438 wrap_row_extra_line_spacing = row->extra_line_spacing;
17439 wrap_row_min_pos = min_pos;
17440 wrap_row_min_bpos = min_bpos;
17441 wrap_row_max_pos = max_pos;
17442 wrap_row_max_bpos = max_bpos;
17443 may_wrap = 0;
17444 }
17445 }
17446 }
17447
17448 PRODUCE_GLYPHS (it);
17449
17450 /* If this display element was in marginal areas, continue with
17451 the next one. */
17452 if (it->area != TEXT_AREA)
17453 {
17454 row->ascent = max (row->ascent, it->max_ascent);
17455 row->height = max (row->height, it->max_ascent + it->max_descent);
17456 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17457 row->phys_height = max (row->phys_height,
17458 it->max_phys_ascent + it->max_phys_descent);
17459 row->extra_line_spacing = max (row->extra_line_spacing,
17460 it->max_extra_line_spacing);
17461 set_iterator_to_next (it, 1);
17462 continue;
17463 }
17464
17465 /* Does the display element fit on the line? If we truncate
17466 lines, we should draw past the right edge of the window. If
17467 we don't truncate, we want to stop so that we can display the
17468 continuation glyph before the right margin. If lines are
17469 continued, there are two possible strategies for characters
17470 resulting in more than 1 glyph (e.g. tabs): Display as many
17471 glyphs as possible in this line and leave the rest for the
17472 continuation line, or display the whole element in the next
17473 line. Original redisplay did the former, so we do it also. */
17474 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17475 hpos_before = it->hpos;
17476 x_before = x;
17477
17478 if (/* Not a newline. */
17479 nglyphs > 0
17480 /* Glyphs produced fit entirely in the line. */
17481 && it->current_x < it->last_visible_x)
17482 {
17483 it->hpos += nglyphs;
17484 row->ascent = max (row->ascent, it->max_ascent);
17485 row->height = max (row->height, it->max_ascent + it->max_descent);
17486 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17487 row->phys_height = max (row->phys_height,
17488 it->max_phys_ascent + it->max_phys_descent);
17489 row->extra_line_spacing = max (row->extra_line_spacing,
17490 it->max_extra_line_spacing);
17491 if (it->current_x - it->pixel_width < it->first_visible_x)
17492 row->x = x - it->first_visible_x;
17493 /* Record the maximum and minimum buffer positions seen so
17494 far in glyphs that will be displayed by this row. */
17495 if (it->bidi_p)
17496 RECORD_MAX_MIN_POS (it);
17497 }
17498 else
17499 {
17500 int new_x;
17501 struct glyph *glyph;
17502
17503 for (i = 0; i < nglyphs; ++i, x = new_x)
17504 {
17505 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17506 new_x = x + glyph->pixel_width;
17507
17508 if (/* Lines are continued. */
17509 it->line_wrap != TRUNCATE
17510 && (/* Glyph doesn't fit on the line. */
17511 new_x > it->last_visible_x
17512 /* Or it fits exactly on a window system frame. */
17513 || (new_x == it->last_visible_x
17514 && FRAME_WINDOW_P (it->f))))
17515 {
17516 /* End of a continued line. */
17517
17518 if (it->hpos == 0
17519 || (new_x == it->last_visible_x
17520 && FRAME_WINDOW_P (it->f)))
17521 {
17522 /* Current glyph is the only one on the line or
17523 fits exactly on the line. We must continue
17524 the line because we can't draw the cursor
17525 after the glyph. */
17526 row->continued_p = 1;
17527 it->current_x = new_x;
17528 it->continuation_lines_width += new_x;
17529 ++it->hpos;
17530 /* Record the maximum and minimum buffer
17531 positions seen so far in glyphs that will be
17532 displayed by this row. */
17533 if (it->bidi_p)
17534 RECORD_MAX_MIN_POS (it);
17535 if (i == nglyphs - 1)
17536 {
17537 /* If line-wrap is on, check if a previous
17538 wrap point was found. */
17539 if (wrap_row_used > 0
17540 /* Even if there is a previous wrap
17541 point, continue the line here as
17542 usual, if (i) the previous character
17543 was a space or tab AND (ii) the
17544 current character is not. */
17545 && (!may_wrap
17546 || IT_DISPLAYING_WHITESPACE (it)))
17547 goto back_to_wrap;
17548
17549 set_iterator_to_next (it, 1);
17550 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17551 {
17552 if (!get_next_display_element (it))
17553 {
17554 row->exact_window_width_line_p = 1;
17555 it->continuation_lines_width = 0;
17556 row->continued_p = 0;
17557 row->ends_at_zv_p = 1;
17558 }
17559 else if (ITERATOR_AT_END_OF_LINE_P (it))
17560 {
17561 row->continued_p = 0;
17562 row->exact_window_width_line_p = 1;
17563 }
17564 }
17565 }
17566 }
17567 else if (CHAR_GLYPH_PADDING_P (*glyph)
17568 && !FRAME_WINDOW_P (it->f))
17569 {
17570 /* A padding glyph that doesn't fit on this line.
17571 This means the whole character doesn't fit
17572 on the line. */
17573 if (row->reversed_p)
17574 unproduce_glyphs (it, row->used[TEXT_AREA]
17575 - n_glyphs_before);
17576 row->used[TEXT_AREA] = n_glyphs_before;
17577
17578 /* Fill the rest of the row with continuation
17579 glyphs like in 20.x. */
17580 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17581 < row->glyphs[1 + TEXT_AREA])
17582 produce_special_glyphs (it, IT_CONTINUATION);
17583
17584 row->continued_p = 1;
17585 it->current_x = x_before;
17586 it->continuation_lines_width += x_before;
17587
17588 /* Restore the height to what it was before the
17589 element not fitting on the line. */
17590 it->max_ascent = ascent;
17591 it->max_descent = descent;
17592 it->max_phys_ascent = phys_ascent;
17593 it->max_phys_descent = phys_descent;
17594 }
17595 else if (wrap_row_used > 0)
17596 {
17597 back_to_wrap:
17598 if (row->reversed_p)
17599 unproduce_glyphs (it,
17600 row->used[TEXT_AREA] - wrap_row_used);
17601 *it = wrap_it;
17602 it->continuation_lines_width += wrap_x;
17603 row->used[TEXT_AREA] = wrap_row_used;
17604 row->ascent = wrap_row_ascent;
17605 row->height = wrap_row_height;
17606 row->phys_ascent = wrap_row_phys_ascent;
17607 row->phys_height = wrap_row_phys_height;
17608 row->extra_line_spacing = wrap_row_extra_line_spacing;
17609 min_pos = wrap_row_min_pos;
17610 min_bpos = wrap_row_min_bpos;
17611 max_pos = wrap_row_max_pos;
17612 max_bpos = wrap_row_max_bpos;
17613 row->continued_p = 1;
17614 row->ends_at_zv_p = 0;
17615 row->exact_window_width_line_p = 0;
17616 it->continuation_lines_width += x;
17617
17618 /* Make sure that a non-default face is extended
17619 up to the right margin of the window. */
17620 extend_face_to_end_of_line (it);
17621 }
17622 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17623 {
17624 /* A TAB that extends past the right edge of the
17625 window. This produces a single glyph on
17626 window system frames. We leave the glyph in
17627 this row and let it fill the row, but don't
17628 consume the TAB. */
17629 it->continuation_lines_width += it->last_visible_x;
17630 row->ends_in_middle_of_char_p = 1;
17631 row->continued_p = 1;
17632 glyph->pixel_width = it->last_visible_x - x;
17633 it->starts_in_middle_of_char_p = 1;
17634 }
17635 else
17636 {
17637 /* Something other than a TAB that draws past
17638 the right edge of the window. Restore
17639 positions to values before the element. */
17640 if (row->reversed_p)
17641 unproduce_glyphs (it, row->used[TEXT_AREA]
17642 - (n_glyphs_before + i));
17643 row->used[TEXT_AREA] = n_glyphs_before + i;
17644
17645 /* Display continuation glyphs. */
17646 if (!FRAME_WINDOW_P (it->f))
17647 produce_special_glyphs (it, IT_CONTINUATION);
17648 row->continued_p = 1;
17649
17650 it->current_x = x_before;
17651 it->continuation_lines_width += x;
17652 extend_face_to_end_of_line (it);
17653
17654 if (nglyphs > 1 && i > 0)
17655 {
17656 row->ends_in_middle_of_char_p = 1;
17657 it->starts_in_middle_of_char_p = 1;
17658 }
17659
17660 /* Restore the height to what it was before the
17661 element not fitting on the line. */
17662 it->max_ascent = ascent;
17663 it->max_descent = descent;
17664 it->max_phys_ascent = phys_ascent;
17665 it->max_phys_descent = phys_descent;
17666 }
17667
17668 break;
17669 }
17670 else if (new_x > it->first_visible_x)
17671 {
17672 /* Increment number of glyphs actually displayed. */
17673 ++it->hpos;
17674
17675 /* Record the maximum and minimum buffer positions
17676 seen so far in glyphs that will be displayed by
17677 this row. */
17678 if (it->bidi_p)
17679 RECORD_MAX_MIN_POS (it);
17680
17681 if (x < it->first_visible_x)
17682 /* Glyph is partially visible, i.e. row starts at
17683 negative X position. */
17684 row->x = x - it->first_visible_x;
17685 }
17686 else
17687 {
17688 /* Glyph is completely off the left margin of the
17689 window. This should not happen because of the
17690 move_it_in_display_line at the start of this
17691 function, unless the text display area of the
17692 window is empty. */
17693 xassert (it->first_visible_x <= it->last_visible_x);
17694 }
17695 }
17696
17697 row->ascent = max (row->ascent, it->max_ascent);
17698 row->height = max (row->height, it->max_ascent + it->max_descent);
17699 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17700 row->phys_height = max (row->phys_height,
17701 it->max_phys_ascent + it->max_phys_descent);
17702 row->extra_line_spacing = max (row->extra_line_spacing,
17703 it->max_extra_line_spacing);
17704
17705 /* End of this display line if row is continued. */
17706 if (row->continued_p || row->ends_at_zv_p)
17707 break;
17708 }
17709
17710 at_end_of_line:
17711 /* Is this a line end? If yes, we're also done, after making
17712 sure that a non-default face is extended up to the right
17713 margin of the window. */
17714 if (ITERATOR_AT_END_OF_LINE_P (it))
17715 {
17716 int used_before = row->used[TEXT_AREA];
17717
17718 row->ends_in_newline_from_string_p = STRINGP (it->object);
17719
17720 /* Add a space at the end of the line that is used to
17721 display the cursor there. */
17722 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17723 append_space_for_newline (it, 0);
17724
17725 /* Extend the face to the end of the line. */
17726 extend_face_to_end_of_line (it);
17727
17728 /* Make sure we have the position. */
17729 if (used_before == 0)
17730 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17731
17732 /* Record the position of the newline, for use in
17733 find_row_edges. */
17734 it->eol_pos = it->current.pos;
17735
17736 /* Consume the line end. This skips over invisible lines. */
17737 set_iterator_to_next (it, 1);
17738 it->continuation_lines_width = 0;
17739 break;
17740 }
17741
17742 /* Proceed with next display element. Note that this skips
17743 over lines invisible because of selective display. */
17744 set_iterator_to_next (it, 1);
17745
17746 /* If we truncate lines, we are done when the last displayed
17747 glyphs reach past the right margin of the window. */
17748 if (it->line_wrap == TRUNCATE
17749 && (FRAME_WINDOW_P (it->f)
17750 ? (it->current_x >= it->last_visible_x)
17751 : (it->current_x > it->last_visible_x)))
17752 {
17753 /* Maybe add truncation glyphs. */
17754 if (!FRAME_WINDOW_P (it->f))
17755 {
17756 int i, n;
17757
17758 if (!row->reversed_p)
17759 {
17760 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17761 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17762 break;
17763 }
17764 else
17765 {
17766 for (i = 0; i < row->used[TEXT_AREA]; i++)
17767 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17768 break;
17769 /* Remove any padding glyphs at the front of ROW, to
17770 make room for the truncation glyphs we will be
17771 adding below. The loop below always inserts at
17772 least one truncation glyph, so also remove the
17773 last glyph added to ROW. */
17774 unproduce_glyphs (it, i + 1);
17775 /* Adjust i for the loop below. */
17776 i = row->used[TEXT_AREA] - (i + 1);
17777 }
17778
17779 for (n = row->used[TEXT_AREA]; i < n; ++i)
17780 {
17781 row->used[TEXT_AREA] = i;
17782 produce_special_glyphs (it, IT_TRUNCATION);
17783 }
17784 }
17785 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17786 {
17787 /* Don't truncate if we can overflow newline into fringe. */
17788 if (!get_next_display_element (it))
17789 {
17790 it->continuation_lines_width = 0;
17791 row->ends_at_zv_p = 1;
17792 row->exact_window_width_line_p = 1;
17793 break;
17794 }
17795 if (ITERATOR_AT_END_OF_LINE_P (it))
17796 {
17797 row->exact_window_width_line_p = 1;
17798 goto at_end_of_line;
17799 }
17800 }
17801
17802 row->truncated_on_right_p = 1;
17803 it->continuation_lines_width = 0;
17804 reseat_at_next_visible_line_start (it, 0);
17805 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17806 it->hpos = hpos_before;
17807 it->current_x = x_before;
17808 break;
17809 }
17810 }
17811
17812 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17813 at the left window margin. */
17814 if (it->first_visible_x
17815 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
17816 {
17817 if (!FRAME_WINDOW_P (it->f))
17818 insert_left_trunc_glyphs (it);
17819 row->truncated_on_left_p = 1;
17820 }
17821
17822 /* Remember the position at which this line ends.
17823
17824 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
17825 cannot be before the call to find_row_edges below, since that is
17826 where these positions are determined. */
17827 row->end = it->current;
17828 if (!it->bidi_p)
17829 {
17830 row->minpos = row->start.pos;
17831 row->maxpos = row->end.pos;
17832 }
17833 else
17834 {
17835 /* ROW->minpos and ROW->maxpos must be the smallest and
17836 `1 + the largest' buffer positions in ROW. But if ROW was
17837 bidi-reordered, these two positions can be anywhere in the
17838 row, so we must determine them now. */
17839 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
17840 }
17841
17842 /* If the start of this line is the overlay arrow-position, then
17843 mark this glyph row as the one containing the overlay arrow.
17844 This is clearly a mess with variable size fonts. It would be
17845 better to let it be displayed like cursors under X. */
17846 if ((row->displays_text_p || !overlay_arrow_seen)
17847 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17848 !NILP (overlay_arrow_string)))
17849 {
17850 /* Overlay arrow in window redisplay is a fringe bitmap. */
17851 if (STRINGP (overlay_arrow_string))
17852 {
17853 struct glyph_row *arrow_row
17854 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17855 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17856 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17857 struct glyph *p = row->glyphs[TEXT_AREA];
17858 struct glyph *p2, *end;
17859
17860 /* Copy the arrow glyphs. */
17861 while (glyph < arrow_end)
17862 *p++ = *glyph++;
17863
17864 /* Throw away padding glyphs. */
17865 p2 = p;
17866 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17867 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17868 ++p2;
17869 if (p2 > p)
17870 {
17871 while (p2 < end)
17872 *p++ = *p2++;
17873 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17874 }
17875 }
17876 else
17877 {
17878 xassert (INTEGERP (overlay_arrow_string));
17879 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17880 }
17881 overlay_arrow_seen = 1;
17882 }
17883
17884 /* Compute pixel dimensions of this line. */
17885 compute_line_metrics (it);
17886
17887 /* Record whether this row ends inside an ellipsis. */
17888 row->ends_in_ellipsis_p
17889 = (it->method == GET_FROM_DISPLAY_VECTOR
17890 && it->ellipsis_p);
17891
17892 /* Save fringe bitmaps in this row. */
17893 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17894 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17895 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17896 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17897
17898 it->left_user_fringe_bitmap = 0;
17899 it->left_user_fringe_face_id = 0;
17900 it->right_user_fringe_bitmap = 0;
17901 it->right_user_fringe_face_id = 0;
17902
17903 /* Maybe set the cursor. */
17904 cvpos = it->w->cursor.vpos;
17905 if ((cvpos < 0
17906 /* In bidi-reordered rows, keep checking for proper cursor
17907 position even if one has been found already, because buffer
17908 positions in such rows change non-linearly with ROW->VPOS,
17909 when a line is continued. One exception: when we are at ZV,
17910 display cursor on the first suitable glyph row, since all
17911 the empty rows after that also have their position set to ZV. */
17912 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17913 lines' rows is implemented for bidi-reordered rows. */
17914 || (it->bidi_p
17915 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
17916 && PT >= MATRIX_ROW_START_CHARPOS (row)
17917 && PT <= MATRIX_ROW_END_CHARPOS (row)
17918 && cursor_row_p (it->w, row))
17919 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17920
17921 /* Highlight trailing whitespace. */
17922 if (!NILP (Vshow_trailing_whitespace))
17923 highlight_trailing_whitespace (it->f, it->glyph_row);
17924
17925 /* Prepare for the next line. This line starts horizontally at (X
17926 HPOS) = (0 0). Vertical positions are incremented. As a
17927 convenience for the caller, IT->glyph_row is set to the next
17928 row to be used. */
17929 it->current_x = it->hpos = 0;
17930 it->current_y += row->height;
17931 SET_TEXT_POS (it->eol_pos, 0, 0);
17932 ++it->vpos;
17933 ++it->glyph_row;
17934 /* The next row should by default use the same value of the
17935 reversed_p flag as this one. set_iterator_to_next decides when
17936 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
17937 the flag accordingly. */
17938 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
17939 it->glyph_row->reversed_p = row->reversed_p;
17940 it->start = row->end;
17941 return row->displays_text_p;
17942
17943 #undef RECORD_MAX_MIN_POS
17944 }
17945
17946 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
17947 Scurrent_bidi_paragraph_direction, 0, 1, 0,
17948 doc: /* Return paragraph direction at point in BUFFER.
17949 Value is either `left-to-right' or `right-to-left'.
17950 If BUFFER is omitted or nil, it defaults to the current buffer.
17951
17952 Paragraph direction determines how the text in the paragraph is displayed.
17953 In left-to-right paragraphs, text begins at the left margin of the window
17954 and the reading direction is generally left to right. In right-to-left
17955 paragraphs, text begins at the right margin and is read from right to left.
17956
17957 See also `bidi-paragraph-direction'. */)
17958 (Lisp_Object buffer)
17959 {
17960 struct buffer *buf;
17961 struct buffer *old;
17962
17963 if (NILP (buffer))
17964 buf = current_buffer;
17965 else
17966 {
17967 CHECK_BUFFER (buffer);
17968 buf = XBUFFER (buffer);
17969 old = current_buffer;
17970 }
17971
17972 if (NILP (buf->bidi_display_reordering))
17973 return Qleft_to_right;
17974 else if (!NILP (buf->bidi_paragraph_direction))
17975 return buf->bidi_paragraph_direction;
17976 else
17977 {
17978 /* Determine the direction from buffer text. We could try to
17979 use current_matrix if it is up to date, but this seems fast
17980 enough as it is. */
17981 struct bidi_it itb;
17982 EMACS_INT pos = BUF_PT (buf);
17983 EMACS_INT bytepos = BUF_PT_BYTE (buf);
17984 int c;
17985
17986 if (buf != current_buffer)
17987 set_buffer_temp (buf);
17988 /* bidi_paragraph_init finds the base direction of the paragraph
17989 by searching forward from paragraph start. We need the base
17990 direction of the current or _previous_ paragraph, so we need
17991 to make sure we are within that paragraph. To that end, find
17992 the previous non-empty line. */
17993 if (pos >= ZV && pos > BEGV)
17994 {
17995 pos--;
17996 bytepos = CHAR_TO_BYTE (pos);
17997 }
17998 while ((c = FETCH_BYTE (bytepos)) == '\n'
17999 || c == ' ' || c == '\t' || c == '\f')
18000 {
18001 if (bytepos <= BEGV_BYTE)
18002 break;
18003 bytepos--;
18004 pos--;
18005 }
18006 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18007 bytepos--;
18008 itb.charpos = pos;
18009 itb.bytepos = bytepos;
18010 itb.first_elt = 1;
18011 itb.separator_limit = -1;
18012
18013 bidi_paragraph_init (NEUTRAL_DIR, &itb);
18014 if (buf != current_buffer)
18015 set_buffer_temp (old);
18016 switch (itb.paragraph_dir)
18017 {
18018 case L2R:
18019 return Qleft_to_right;
18020 break;
18021 case R2L:
18022 return Qright_to_left;
18023 break;
18024 default:
18025 abort ();
18026 }
18027 }
18028 }
18029
18030
18031 \f
18032 /***********************************************************************
18033 Menu Bar
18034 ***********************************************************************/
18035
18036 /* Redisplay the menu bar in the frame for window W.
18037
18038 The menu bar of X frames that don't have X toolkit support is
18039 displayed in a special window W->frame->menu_bar_window.
18040
18041 The menu bar of terminal frames is treated specially as far as
18042 glyph matrices are concerned. Menu bar lines are not part of
18043 windows, so the update is done directly on the frame matrix rows
18044 for the menu bar. */
18045
18046 static void
18047 display_menu_bar (struct window *w)
18048 {
18049 struct frame *f = XFRAME (WINDOW_FRAME (w));
18050 struct it it;
18051 Lisp_Object items;
18052 int i;
18053
18054 /* Don't do all this for graphical frames. */
18055 #ifdef HAVE_NTGUI
18056 if (FRAME_W32_P (f))
18057 return;
18058 #endif
18059 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18060 if (FRAME_X_P (f))
18061 return;
18062 #endif
18063
18064 #ifdef HAVE_NS
18065 if (FRAME_NS_P (f))
18066 return;
18067 #endif /* HAVE_NS */
18068
18069 #ifdef USE_X_TOOLKIT
18070 xassert (!FRAME_WINDOW_P (f));
18071 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18072 it.first_visible_x = 0;
18073 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18074 #else /* not USE_X_TOOLKIT */
18075 if (FRAME_WINDOW_P (f))
18076 {
18077 /* Menu bar lines are displayed in the desired matrix of the
18078 dummy window menu_bar_window. */
18079 struct window *menu_w;
18080 xassert (WINDOWP (f->menu_bar_window));
18081 menu_w = XWINDOW (f->menu_bar_window);
18082 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18083 MENU_FACE_ID);
18084 it.first_visible_x = 0;
18085 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18086 }
18087 else
18088 {
18089 /* This is a TTY frame, i.e. character hpos/vpos are used as
18090 pixel x/y. */
18091 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18092 MENU_FACE_ID);
18093 it.first_visible_x = 0;
18094 it.last_visible_x = FRAME_COLS (f);
18095 }
18096 #endif /* not USE_X_TOOLKIT */
18097
18098 if (! mode_line_inverse_video)
18099 /* Force the menu-bar to be displayed in the default face. */
18100 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18101
18102 /* Clear all rows of the menu bar. */
18103 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18104 {
18105 struct glyph_row *row = it.glyph_row + i;
18106 clear_glyph_row (row);
18107 row->enabled_p = 1;
18108 row->full_width_p = 1;
18109 }
18110
18111 /* Display all items of the menu bar. */
18112 items = FRAME_MENU_BAR_ITEMS (it.f);
18113 for (i = 0; i < XVECTOR (items)->size; i += 4)
18114 {
18115 Lisp_Object string;
18116
18117 /* Stop at nil string. */
18118 string = AREF (items, i + 1);
18119 if (NILP (string))
18120 break;
18121
18122 /* Remember where item was displayed. */
18123 ASET (items, i + 3, make_number (it.hpos));
18124
18125 /* Display the item, pad with one space. */
18126 if (it.current_x < it.last_visible_x)
18127 display_string (NULL, string, Qnil, 0, 0, &it,
18128 SCHARS (string) + 1, 0, 0, -1);
18129 }
18130
18131 /* Fill out the line with spaces. */
18132 if (it.current_x < it.last_visible_x)
18133 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18134
18135 /* Compute the total height of the lines. */
18136 compute_line_metrics (&it);
18137 }
18138
18139
18140 \f
18141 /***********************************************************************
18142 Mode Line
18143 ***********************************************************************/
18144
18145 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18146 FORCE is non-zero, redisplay mode lines unconditionally.
18147 Otherwise, redisplay only mode lines that are garbaged. Value is
18148 the number of windows whose mode lines were redisplayed. */
18149
18150 static int
18151 redisplay_mode_lines (Lisp_Object window, int force)
18152 {
18153 int nwindows = 0;
18154
18155 while (!NILP (window))
18156 {
18157 struct window *w = XWINDOW (window);
18158
18159 if (WINDOWP (w->hchild))
18160 nwindows += redisplay_mode_lines (w->hchild, force);
18161 else if (WINDOWP (w->vchild))
18162 nwindows += redisplay_mode_lines (w->vchild, force);
18163 else if (force
18164 || FRAME_GARBAGED_P (XFRAME (w->frame))
18165 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18166 {
18167 struct text_pos lpoint;
18168 struct buffer *old = current_buffer;
18169
18170 /* Set the window's buffer for the mode line display. */
18171 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18172 set_buffer_internal_1 (XBUFFER (w->buffer));
18173
18174 /* Point refers normally to the selected window. For any
18175 other window, set up appropriate value. */
18176 if (!EQ (window, selected_window))
18177 {
18178 struct text_pos pt;
18179
18180 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18181 if (CHARPOS (pt) < BEGV)
18182 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18183 else if (CHARPOS (pt) > (ZV - 1))
18184 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18185 else
18186 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18187 }
18188
18189 /* Display mode lines. */
18190 clear_glyph_matrix (w->desired_matrix);
18191 if (display_mode_lines (w))
18192 {
18193 ++nwindows;
18194 w->must_be_updated_p = 1;
18195 }
18196
18197 /* Restore old settings. */
18198 set_buffer_internal_1 (old);
18199 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18200 }
18201
18202 window = w->next;
18203 }
18204
18205 return nwindows;
18206 }
18207
18208
18209 /* Display the mode and/or header line of window W. Value is the
18210 sum number of mode lines and header lines displayed. */
18211
18212 static int
18213 display_mode_lines (struct window *w)
18214 {
18215 Lisp_Object old_selected_window, old_selected_frame;
18216 int n = 0;
18217
18218 old_selected_frame = selected_frame;
18219 selected_frame = w->frame;
18220 old_selected_window = selected_window;
18221 XSETWINDOW (selected_window, w);
18222
18223 /* These will be set while the mode line specs are processed. */
18224 line_number_displayed = 0;
18225 w->column_number_displayed = Qnil;
18226
18227 if (WINDOW_WANTS_MODELINE_P (w))
18228 {
18229 struct window *sel_w = XWINDOW (old_selected_window);
18230
18231 /* Select mode line face based on the real selected window. */
18232 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18233 current_buffer->mode_line_format);
18234 ++n;
18235 }
18236
18237 if (WINDOW_WANTS_HEADER_LINE_P (w))
18238 {
18239 display_mode_line (w, HEADER_LINE_FACE_ID,
18240 current_buffer->header_line_format);
18241 ++n;
18242 }
18243
18244 selected_frame = old_selected_frame;
18245 selected_window = old_selected_window;
18246 return n;
18247 }
18248
18249
18250 /* Display mode or header line of window W. FACE_ID specifies which
18251 line to display; it is either MODE_LINE_FACE_ID or
18252 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18253 display. Value is the pixel height of the mode/header line
18254 displayed. */
18255
18256 static int
18257 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18258 {
18259 struct it it;
18260 struct face *face;
18261 int count = SPECPDL_INDEX ();
18262
18263 init_iterator (&it, w, -1, -1, NULL, face_id);
18264 /* Don't extend on a previously drawn mode-line.
18265 This may happen if called from pos_visible_p. */
18266 it.glyph_row->enabled_p = 0;
18267 prepare_desired_row (it.glyph_row);
18268
18269 it.glyph_row->mode_line_p = 1;
18270
18271 if (! mode_line_inverse_video)
18272 /* Force the mode-line to be displayed in the default face. */
18273 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18274
18275 record_unwind_protect (unwind_format_mode_line,
18276 format_mode_line_unwind_data (NULL, Qnil, 0));
18277
18278 mode_line_target = MODE_LINE_DISPLAY;
18279
18280 /* Temporarily make frame's keyboard the current kboard so that
18281 kboard-local variables in the mode_line_format will get the right
18282 values. */
18283 push_kboard (FRAME_KBOARD (it.f));
18284 record_unwind_save_match_data ();
18285 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18286 pop_kboard ();
18287
18288 unbind_to (count, Qnil);
18289
18290 /* Fill up with spaces. */
18291 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18292
18293 compute_line_metrics (&it);
18294 it.glyph_row->full_width_p = 1;
18295 it.glyph_row->continued_p = 0;
18296 it.glyph_row->truncated_on_left_p = 0;
18297 it.glyph_row->truncated_on_right_p = 0;
18298
18299 /* Make a 3D mode-line have a shadow at its right end. */
18300 face = FACE_FROM_ID (it.f, face_id);
18301 extend_face_to_end_of_line (&it);
18302 if (face->box != FACE_NO_BOX)
18303 {
18304 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18305 + it.glyph_row->used[TEXT_AREA] - 1);
18306 last->right_box_line_p = 1;
18307 }
18308
18309 return it.glyph_row->height;
18310 }
18311
18312 /* Move element ELT in LIST to the front of LIST.
18313 Return the updated list. */
18314
18315 static Lisp_Object
18316 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18317 {
18318 register Lisp_Object tail, prev;
18319 register Lisp_Object tem;
18320
18321 tail = list;
18322 prev = Qnil;
18323 while (CONSP (tail))
18324 {
18325 tem = XCAR (tail);
18326
18327 if (EQ (elt, tem))
18328 {
18329 /* Splice out the link TAIL. */
18330 if (NILP (prev))
18331 list = XCDR (tail);
18332 else
18333 Fsetcdr (prev, XCDR (tail));
18334
18335 /* Now make it the first. */
18336 Fsetcdr (tail, list);
18337 return tail;
18338 }
18339 else
18340 prev = tail;
18341 tail = XCDR (tail);
18342 QUIT;
18343 }
18344
18345 /* Not found--return unchanged LIST. */
18346 return list;
18347 }
18348
18349 /* Contribute ELT to the mode line for window IT->w. How it
18350 translates into text depends on its data type.
18351
18352 IT describes the display environment in which we display, as usual.
18353
18354 DEPTH is the depth in recursion. It is used to prevent
18355 infinite recursion here.
18356
18357 FIELD_WIDTH is the number of characters the display of ELT should
18358 occupy in the mode line, and PRECISION is the maximum number of
18359 characters to display from ELT's representation. See
18360 display_string for details.
18361
18362 Returns the hpos of the end of the text generated by ELT.
18363
18364 PROPS is a property list to add to any string we encounter.
18365
18366 If RISKY is nonzero, remove (disregard) any properties in any string
18367 we encounter, and ignore :eval and :propertize.
18368
18369 The global variable `mode_line_target' determines whether the
18370 output is passed to `store_mode_line_noprop',
18371 `store_mode_line_string', or `display_string'. */
18372
18373 static int
18374 display_mode_element (struct it *it, int depth, int field_width, int precision,
18375 Lisp_Object elt, Lisp_Object props, int risky)
18376 {
18377 int n = 0, field, prec;
18378 int literal = 0;
18379
18380 tail_recurse:
18381 if (depth > 100)
18382 elt = build_string ("*too-deep*");
18383
18384 depth++;
18385
18386 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18387 {
18388 case Lisp_String:
18389 {
18390 /* A string: output it and check for %-constructs within it. */
18391 unsigned char c;
18392 int offset = 0;
18393
18394 if (SCHARS (elt) > 0
18395 && (!NILP (props) || risky))
18396 {
18397 Lisp_Object oprops, aelt;
18398 oprops = Ftext_properties_at (make_number (0), elt);
18399
18400 /* If the starting string's properties are not what
18401 we want, translate the string. Also, if the string
18402 is risky, do that anyway. */
18403
18404 if (NILP (Fequal (props, oprops)) || risky)
18405 {
18406 /* If the starting string has properties,
18407 merge the specified ones onto the existing ones. */
18408 if (! NILP (oprops) && !risky)
18409 {
18410 Lisp_Object tem;
18411
18412 oprops = Fcopy_sequence (oprops);
18413 tem = props;
18414 while (CONSP (tem))
18415 {
18416 oprops = Fplist_put (oprops, XCAR (tem),
18417 XCAR (XCDR (tem)));
18418 tem = XCDR (XCDR (tem));
18419 }
18420 props = oprops;
18421 }
18422
18423 aelt = Fassoc (elt, mode_line_proptrans_alist);
18424 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18425 {
18426 /* AELT is what we want. Move it to the front
18427 without consing. */
18428 elt = XCAR (aelt);
18429 mode_line_proptrans_alist
18430 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18431 }
18432 else
18433 {
18434 Lisp_Object tem;
18435
18436 /* If AELT has the wrong props, it is useless.
18437 so get rid of it. */
18438 if (! NILP (aelt))
18439 mode_line_proptrans_alist
18440 = Fdelq (aelt, mode_line_proptrans_alist);
18441
18442 elt = Fcopy_sequence (elt);
18443 Fset_text_properties (make_number (0), Flength (elt),
18444 props, elt);
18445 /* Add this item to mode_line_proptrans_alist. */
18446 mode_line_proptrans_alist
18447 = Fcons (Fcons (elt, props),
18448 mode_line_proptrans_alist);
18449 /* Truncate mode_line_proptrans_alist
18450 to at most 50 elements. */
18451 tem = Fnthcdr (make_number (50),
18452 mode_line_proptrans_alist);
18453 if (! NILP (tem))
18454 XSETCDR (tem, Qnil);
18455 }
18456 }
18457 }
18458
18459 offset = 0;
18460
18461 if (literal)
18462 {
18463 prec = precision - n;
18464 switch (mode_line_target)
18465 {
18466 case MODE_LINE_NOPROP:
18467 case MODE_LINE_TITLE:
18468 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18469 break;
18470 case MODE_LINE_STRING:
18471 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18472 break;
18473 case MODE_LINE_DISPLAY:
18474 n += display_string (NULL, elt, Qnil, 0, 0, it,
18475 0, prec, 0, STRING_MULTIBYTE (elt));
18476 break;
18477 }
18478
18479 break;
18480 }
18481
18482 /* Handle the non-literal case. */
18483
18484 while ((precision <= 0 || n < precision)
18485 && SREF (elt, offset) != 0
18486 && (mode_line_target != MODE_LINE_DISPLAY
18487 || it->current_x < it->last_visible_x))
18488 {
18489 int last_offset = offset;
18490
18491 /* Advance to end of string or next format specifier. */
18492 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18493 ;
18494
18495 if (offset - 1 != last_offset)
18496 {
18497 int nchars, nbytes;
18498
18499 /* Output to end of string or up to '%'. Field width
18500 is length of string. Don't output more than
18501 PRECISION allows us. */
18502 offset--;
18503
18504 prec = c_string_width (SDATA (elt) + last_offset,
18505 offset - last_offset, precision - n,
18506 &nchars, &nbytes);
18507
18508 switch (mode_line_target)
18509 {
18510 case MODE_LINE_NOPROP:
18511 case MODE_LINE_TITLE:
18512 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18513 break;
18514 case MODE_LINE_STRING:
18515 {
18516 int bytepos = last_offset;
18517 int charpos = string_byte_to_char (elt, bytepos);
18518 int endpos = (precision <= 0
18519 ? string_byte_to_char (elt, offset)
18520 : charpos + nchars);
18521
18522 n += store_mode_line_string (NULL,
18523 Fsubstring (elt, make_number (charpos),
18524 make_number (endpos)),
18525 0, 0, 0, Qnil);
18526 }
18527 break;
18528 case MODE_LINE_DISPLAY:
18529 {
18530 int bytepos = last_offset;
18531 int charpos = string_byte_to_char (elt, bytepos);
18532
18533 if (precision <= 0)
18534 nchars = string_byte_to_char (elt, offset) - charpos;
18535 n += display_string (NULL, elt, Qnil, 0, charpos,
18536 it, 0, nchars, 0,
18537 STRING_MULTIBYTE (elt));
18538 }
18539 break;
18540 }
18541 }
18542 else /* c == '%' */
18543 {
18544 int percent_position = offset;
18545
18546 /* Get the specified minimum width. Zero means
18547 don't pad. */
18548 field = 0;
18549 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18550 field = field * 10 + c - '0';
18551
18552 /* Don't pad beyond the total padding allowed. */
18553 if (field_width - n > 0 && field > field_width - n)
18554 field = field_width - n;
18555
18556 /* Note that either PRECISION <= 0 or N < PRECISION. */
18557 prec = precision - n;
18558
18559 if (c == 'M')
18560 n += display_mode_element (it, depth, field, prec,
18561 Vglobal_mode_string, props,
18562 risky);
18563 else if (c != 0)
18564 {
18565 int multibyte;
18566 int bytepos, charpos;
18567 const unsigned char *spec;
18568 Lisp_Object string;
18569
18570 bytepos = percent_position;
18571 charpos = (STRING_MULTIBYTE (elt)
18572 ? string_byte_to_char (elt, bytepos)
18573 : bytepos);
18574 spec = decode_mode_spec (it->w, c, field, prec, &string);
18575 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18576
18577 switch (mode_line_target)
18578 {
18579 case MODE_LINE_NOPROP:
18580 case MODE_LINE_TITLE:
18581 n += store_mode_line_noprop (spec, field, prec);
18582 break;
18583 case MODE_LINE_STRING:
18584 {
18585 int len = strlen (spec);
18586 Lisp_Object tem = make_string (spec, len);
18587 props = Ftext_properties_at (make_number (charpos), elt);
18588 /* Should only keep face property in props */
18589 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18590 }
18591 break;
18592 case MODE_LINE_DISPLAY:
18593 {
18594 int nglyphs_before, nwritten;
18595
18596 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18597 nwritten = display_string (spec, string, elt,
18598 charpos, 0, it,
18599 field, prec, 0,
18600 multibyte);
18601
18602 /* Assign to the glyphs written above the
18603 string where the `%x' came from, position
18604 of the `%'. */
18605 if (nwritten > 0)
18606 {
18607 struct glyph *glyph
18608 = (it->glyph_row->glyphs[TEXT_AREA]
18609 + nglyphs_before);
18610 int i;
18611
18612 for (i = 0; i < nwritten; ++i)
18613 {
18614 glyph[i].object = elt;
18615 glyph[i].charpos = charpos;
18616 }
18617
18618 n += nwritten;
18619 }
18620 }
18621 break;
18622 }
18623 }
18624 else /* c == 0 */
18625 break;
18626 }
18627 }
18628 }
18629 break;
18630
18631 case Lisp_Symbol:
18632 /* A symbol: process the value of the symbol recursively
18633 as if it appeared here directly. Avoid error if symbol void.
18634 Special case: if value of symbol is a string, output the string
18635 literally. */
18636 {
18637 register Lisp_Object tem;
18638
18639 /* If the variable is not marked as risky to set
18640 then its contents are risky to use. */
18641 if (NILP (Fget (elt, Qrisky_local_variable)))
18642 risky = 1;
18643
18644 tem = Fboundp (elt);
18645 if (!NILP (tem))
18646 {
18647 tem = Fsymbol_value (elt);
18648 /* If value is a string, output that string literally:
18649 don't check for % within it. */
18650 if (STRINGP (tem))
18651 literal = 1;
18652
18653 if (!EQ (tem, elt))
18654 {
18655 /* Give up right away for nil or t. */
18656 elt = tem;
18657 goto tail_recurse;
18658 }
18659 }
18660 }
18661 break;
18662
18663 case Lisp_Cons:
18664 {
18665 register Lisp_Object car, tem;
18666
18667 /* A cons cell: five distinct cases.
18668 If first element is :eval or :propertize, do something special.
18669 If first element is a string or a cons, process all the elements
18670 and effectively concatenate them.
18671 If first element is a negative number, truncate displaying cdr to
18672 at most that many characters. If positive, pad (with spaces)
18673 to at least that many characters.
18674 If first element is a symbol, process the cadr or caddr recursively
18675 according to whether the symbol's value is non-nil or nil. */
18676 car = XCAR (elt);
18677 if (EQ (car, QCeval))
18678 {
18679 /* An element of the form (:eval FORM) means evaluate FORM
18680 and use the result as mode line elements. */
18681
18682 if (risky)
18683 break;
18684
18685 if (CONSP (XCDR (elt)))
18686 {
18687 Lisp_Object spec;
18688 spec = safe_eval (XCAR (XCDR (elt)));
18689 n += display_mode_element (it, depth, field_width - n,
18690 precision - n, spec, props,
18691 risky);
18692 }
18693 }
18694 else if (EQ (car, QCpropertize))
18695 {
18696 /* An element of the form (:propertize ELT PROPS...)
18697 means display ELT but applying properties PROPS. */
18698
18699 if (risky)
18700 break;
18701
18702 if (CONSP (XCDR (elt)))
18703 n += display_mode_element (it, depth, field_width - n,
18704 precision - n, XCAR (XCDR (elt)),
18705 XCDR (XCDR (elt)), risky);
18706 }
18707 else if (SYMBOLP (car))
18708 {
18709 tem = Fboundp (car);
18710 elt = XCDR (elt);
18711 if (!CONSP (elt))
18712 goto invalid;
18713 /* elt is now the cdr, and we know it is a cons cell.
18714 Use its car if CAR has a non-nil value. */
18715 if (!NILP (tem))
18716 {
18717 tem = Fsymbol_value (car);
18718 if (!NILP (tem))
18719 {
18720 elt = XCAR (elt);
18721 goto tail_recurse;
18722 }
18723 }
18724 /* Symbol's value is nil (or symbol is unbound)
18725 Get the cddr of the original list
18726 and if possible find the caddr and use that. */
18727 elt = XCDR (elt);
18728 if (NILP (elt))
18729 break;
18730 else if (!CONSP (elt))
18731 goto invalid;
18732 elt = XCAR (elt);
18733 goto tail_recurse;
18734 }
18735 else if (INTEGERP (car))
18736 {
18737 register int lim = XINT (car);
18738 elt = XCDR (elt);
18739 if (lim < 0)
18740 {
18741 /* Negative int means reduce maximum width. */
18742 if (precision <= 0)
18743 precision = -lim;
18744 else
18745 precision = min (precision, -lim);
18746 }
18747 else if (lim > 0)
18748 {
18749 /* Padding specified. Don't let it be more than
18750 current maximum. */
18751 if (precision > 0)
18752 lim = min (precision, lim);
18753
18754 /* If that's more padding than already wanted, queue it.
18755 But don't reduce padding already specified even if
18756 that is beyond the current truncation point. */
18757 field_width = max (lim, field_width);
18758 }
18759 goto tail_recurse;
18760 }
18761 else if (STRINGP (car) || CONSP (car))
18762 {
18763 Lisp_Object halftail = elt;
18764 int len = 0;
18765
18766 while (CONSP (elt)
18767 && (precision <= 0 || n < precision))
18768 {
18769 n += display_mode_element (it, depth,
18770 /* Do padding only after the last
18771 element in the list. */
18772 (! CONSP (XCDR (elt))
18773 ? field_width - n
18774 : 0),
18775 precision - n, XCAR (elt),
18776 props, risky);
18777 elt = XCDR (elt);
18778 len++;
18779 if ((len & 1) == 0)
18780 halftail = XCDR (halftail);
18781 /* Check for cycle. */
18782 if (EQ (halftail, elt))
18783 break;
18784 }
18785 }
18786 }
18787 break;
18788
18789 default:
18790 invalid:
18791 elt = build_string ("*invalid*");
18792 goto tail_recurse;
18793 }
18794
18795 /* Pad to FIELD_WIDTH. */
18796 if (field_width > 0 && n < field_width)
18797 {
18798 switch (mode_line_target)
18799 {
18800 case MODE_LINE_NOPROP:
18801 case MODE_LINE_TITLE:
18802 n += store_mode_line_noprop ("", field_width - n, 0);
18803 break;
18804 case MODE_LINE_STRING:
18805 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18806 break;
18807 case MODE_LINE_DISPLAY:
18808 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18809 0, 0, 0);
18810 break;
18811 }
18812 }
18813
18814 return n;
18815 }
18816
18817 /* Store a mode-line string element in mode_line_string_list.
18818
18819 If STRING is non-null, display that C string. Otherwise, the Lisp
18820 string LISP_STRING is displayed.
18821
18822 FIELD_WIDTH is the minimum number of output glyphs to produce.
18823 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18824 with spaces. FIELD_WIDTH <= 0 means don't pad.
18825
18826 PRECISION is the maximum number of characters to output from
18827 STRING. PRECISION <= 0 means don't truncate the string.
18828
18829 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18830 properties to the string.
18831
18832 PROPS are the properties to add to the string.
18833 The mode_line_string_face face property is always added to the string.
18834 */
18835
18836 static int
18837 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
18838 int field_width, int precision, Lisp_Object props)
18839 {
18840 int len;
18841 int n = 0;
18842
18843 if (string != NULL)
18844 {
18845 len = strlen (string);
18846 if (precision > 0 && len > precision)
18847 len = precision;
18848 lisp_string = make_string (string, len);
18849 if (NILP (props))
18850 props = mode_line_string_face_prop;
18851 else if (!NILP (mode_line_string_face))
18852 {
18853 Lisp_Object face = Fplist_get (props, Qface);
18854 props = Fcopy_sequence (props);
18855 if (NILP (face))
18856 face = mode_line_string_face;
18857 else
18858 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18859 props = Fplist_put (props, Qface, face);
18860 }
18861 Fadd_text_properties (make_number (0), make_number (len),
18862 props, lisp_string);
18863 }
18864 else
18865 {
18866 len = XFASTINT (Flength (lisp_string));
18867 if (precision > 0 && len > precision)
18868 {
18869 len = precision;
18870 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
18871 precision = -1;
18872 }
18873 if (!NILP (mode_line_string_face))
18874 {
18875 Lisp_Object face;
18876 if (NILP (props))
18877 props = Ftext_properties_at (make_number (0), lisp_string);
18878 face = Fplist_get (props, Qface);
18879 if (NILP (face))
18880 face = mode_line_string_face;
18881 else
18882 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18883 props = Fcons (Qface, Fcons (face, Qnil));
18884 if (copy_string)
18885 lisp_string = Fcopy_sequence (lisp_string);
18886 }
18887 if (!NILP (props))
18888 Fadd_text_properties (make_number (0), make_number (len),
18889 props, lisp_string);
18890 }
18891
18892 if (len > 0)
18893 {
18894 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18895 n += len;
18896 }
18897
18898 if (field_width > len)
18899 {
18900 field_width -= len;
18901 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
18902 if (!NILP (props))
18903 Fadd_text_properties (make_number (0), make_number (field_width),
18904 props, lisp_string);
18905 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18906 n += field_width;
18907 }
18908
18909 return n;
18910 }
18911
18912
18913 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
18914 1, 4, 0,
18915 doc: /* Format a string out of a mode line format specification.
18916 First arg FORMAT specifies the mode line format (see `mode-line-format'
18917 for details) to use.
18918
18919 Optional second arg FACE specifies the face property to put
18920 on all characters for which no face is specified.
18921 The value t means whatever face the window's mode line currently uses
18922 \(either `mode-line' or `mode-line-inactive', depending).
18923 A value of nil means the default is no face property.
18924 If FACE is an integer, the value string has no text properties.
18925
18926 Optional third and fourth args WINDOW and BUFFER specify the window
18927 and buffer to use as the context for the formatting (defaults
18928 are the selected window and the window's buffer). */)
18929 (Lisp_Object format, Lisp_Object face, Lisp_Object window, Lisp_Object buffer)
18930 {
18931 struct it it;
18932 int len;
18933 struct window *w;
18934 struct buffer *old_buffer = NULL;
18935 int face_id = -1;
18936 int no_props = INTEGERP (face);
18937 int count = SPECPDL_INDEX ();
18938 Lisp_Object str;
18939 int string_start = 0;
18940
18941 if (NILP (window))
18942 window = selected_window;
18943 CHECK_WINDOW (window);
18944 w = XWINDOW (window);
18945
18946 if (NILP (buffer))
18947 buffer = w->buffer;
18948 CHECK_BUFFER (buffer);
18949
18950 /* Make formatting the modeline a non-op when noninteractive, otherwise
18951 there will be problems later caused by a partially initialized frame. */
18952 if (NILP (format) || noninteractive)
18953 return empty_unibyte_string;
18954
18955 if (no_props)
18956 face = Qnil;
18957
18958 if (!NILP (face))
18959 {
18960 if (EQ (face, Qt))
18961 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18962 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18963 }
18964
18965 if (face_id < 0)
18966 face_id = DEFAULT_FACE_ID;
18967
18968 if (XBUFFER (buffer) != current_buffer)
18969 old_buffer = current_buffer;
18970
18971 /* Save things including mode_line_proptrans_alist,
18972 and set that to nil so that we don't alter the outer value. */
18973 record_unwind_protect (unwind_format_mode_line,
18974 format_mode_line_unwind_data
18975 (old_buffer, selected_window, 1));
18976 mode_line_proptrans_alist = Qnil;
18977
18978 Fselect_window (window, Qt);
18979 if (old_buffer)
18980 set_buffer_internal_1 (XBUFFER (buffer));
18981
18982 init_iterator (&it, w, -1, -1, NULL, face_id);
18983
18984 if (no_props)
18985 {
18986 mode_line_target = MODE_LINE_NOPROP;
18987 mode_line_string_face_prop = Qnil;
18988 mode_line_string_list = Qnil;
18989 string_start = MODE_LINE_NOPROP_LEN (0);
18990 }
18991 else
18992 {
18993 mode_line_target = MODE_LINE_STRING;
18994 mode_line_string_list = Qnil;
18995 mode_line_string_face = face;
18996 mode_line_string_face_prop
18997 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18998 }
18999
19000 push_kboard (FRAME_KBOARD (it.f));
19001 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19002 pop_kboard ();
19003
19004 if (no_props)
19005 {
19006 len = MODE_LINE_NOPROP_LEN (string_start);
19007 str = make_string (mode_line_noprop_buf + string_start, len);
19008 }
19009 else
19010 {
19011 mode_line_string_list = Fnreverse (mode_line_string_list);
19012 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19013 empty_unibyte_string);
19014 }
19015
19016 unbind_to (count, Qnil);
19017 return str;
19018 }
19019
19020 /* Write a null-terminated, right justified decimal representation of
19021 the positive integer D to BUF using a minimal field width WIDTH. */
19022
19023 static void
19024 pint2str (register char *buf, register int width, register int d)
19025 {
19026 register char *p = buf;
19027
19028 if (d <= 0)
19029 *p++ = '0';
19030 else
19031 {
19032 while (d > 0)
19033 {
19034 *p++ = d % 10 + '0';
19035 d /= 10;
19036 }
19037 }
19038
19039 for (width -= (int) (p - buf); width > 0; --width)
19040 *p++ = ' ';
19041 *p-- = '\0';
19042 while (p > buf)
19043 {
19044 d = *buf;
19045 *buf++ = *p;
19046 *p-- = d;
19047 }
19048 }
19049
19050 /* Write a null-terminated, right justified decimal and "human
19051 readable" representation of the nonnegative integer D to BUF using
19052 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19053
19054 static const char power_letter[] =
19055 {
19056 0, /* not used */
19057 'k', /* kilo */
19058 'M', /* mega */
19059 'G', /* giga */
19060 'T', /* tera */
19061 'P', /* peta */
19062 'E', /* exa */
19063 'Z', /* zetta */
19064 'Y' /* yotta */
19065 };
19066
19067 static void
19068 pint2hrstr (char *buf, int width, int d)
19069 {
19070 /* We aim to represent the nonnegative integer D as
19071 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19072 int quotient = d;
19073 int remainder = 0;
19074 /* -1 means: do not use TENTHS. */
19075 int tenths = -1;
19076 int exponent = 0;
19077
19078 /* Length of QUOTIENT.TENTHS as a string. */
19079 int length;
19080
19081 char * psuffix;
19082 char * p;
19083
19084 if (1000 <= quotient)
19085 {
19086 /* Scale to the appropriate EXPONENT. */
19087 do
19088 {
19089 remainder = quotient % 1000;
19090 quotient /= 1000;
19091 exponent++;
19092 }
19093 while (1000 <= quotient);
19094
19095 /* Round to nearest and decide whether to use TENTHS or not. */
19096 if (quotient <= 9)
19097 {
19098 tenths = remainder / 100;
19099 if (50 <= remainder % 100)
19100 {
19101 if (tenths < 9)
19102 tenths++;
19103 else
19104 {
19105 quotient++;
19106 if (quotient == 10)
19107 tenths = -1;
19108 else
19109 tenths = 0;
19110 }
19111 }
19112 }
19113 else
19114 if (500 <= remainder)
19115 {
19116 if (quotient < 999)
19117 quotient++;
19118 else
19119 {
19120 quotient = 1;
19121 exponent++;
19122 tenths = 0;
19123 }
19124 }
19125 }
19126
19127 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19128 if (tenths == -1 && quotient <= 99)
19129 if (quotient <= 9)
19130 length = 1;
19131 else
19132 length = 2;
19133 else
19134 length = 3;
19135 p = psuffix = buf + max (width, length);
19136
19137 /* Print EXPONENT. */
19138 if (exponent)
19139 *psuffix++ = power_letter[exponent];
19140 *psuffix = '\0';
19141
19142 /* Print TENTHS. */
19143 if (tenths >= 0)
19144 {
19145 *--p = '0' + tenths;
19146 *--p = '.';
19147 }
19148
19149 /* Print QUOTIENT. */
19150 do
19151 {
19152 int digit = quotient % 10;
19153 *--p = '0' + digit;
19154 }
19155 while ((quotient /= 10) != 0);
19156
19157 /* Print leading spaces. */
19158 while (buf < p)
19159 *--p = ' ';
19160 }
19161
19162 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19163 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19164 type of CODING_SYSTEM. Return updated pointer into BUF. */
19165
19166 static unsigned char invalid_eol_type[] = "(*invalid*)";
19167
19168 static char *
19169 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19170 {
19171 Lisp_Object val;
19172 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
19173 const unsigned char *eol_str;
19174 int eol_str_len;
19175 /* The EOL conversion we are using. */
19176 Lisp_Object eoltype;
19177
19178 val = CODING_SYSTEM_SPEC (coding_system);
19179 eoltype = Qnil;
19180
19181 if (!VECTORP (val)) /* Not yet decided. */
19182 {
19183 if (multibyte)
19184 *buf++ = '-';
19185 if (eol_flag)
19186 eoltype = eol_mnemonic_undecided;
19187 /* Don't mention EOL conversion if it isn't decided. */
19188 }
19189 else
19190 {
19191 Lisp_Object attrs;
19192 Lisp_Object eolvalue;
19193
19194 attrs = AREF (val, 0);
19195 eolvalue = AREF (val, 2);
19196
19197 if (multibyte)
19198 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19199
19200 if (eol_flag)
19201 {
19202 /* The EOL conversion that is normal on this system. */
19203
19204 if (NILP (eolvalue)) /* Not yet decided. */
19205 eoltype = eol_mnemonic_undecided;
19206 else if (VECTORP (eolvalue)) /* Not yet decided. */
19207 eoltype = eol_mnemonic_undecided;
19208 else /* eolvalue is Qunix, Qdos, or Qmac. */
19209 eoltype = (EQ (eolvalue, Qunix)
19210 ? eol_mnemonic_unix
19211 : (EQ (eolvalue, Qdos) == 1
19212 ? eol_mnemonic_dos : eol_mnemonic_mac));
19213 }
19214 }
19215
19216 if (eol_flag)
19217 {
19218 /* Mention the EOL conversion if it is not the usual one. */
19219 if (STRINGP (eoltype))
19220 {
19221 eol_str = SDATA (eoltype);
19222 eol_str_len = SBYTES (eoltype);
19223 }
19224 else if (CHARACTERP (eoltype))
19225 {
19226 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19227 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19228 eol_str = tmp;
19229 }
19230 else
19231 {
19232 eol_str = invalid_eol_type;
19233 eol_str_len = sizeof (invalid_eol_type) - 1;
19234 }
19235 memcpy (buf, eol_str, eol_str_len);
19236 buf += eol_str_len;
19237 }
19238
19239 return buf;
19240 }
19241
19242 /* Return a string for the output of a mode line %-spec for window W,
19243 generated by character C. PRECISION >= 0 means don't return a
19244 string longer than that value. FIELD_WIDTH > 0 means pad the
19245 string returned with spaces to that value. Return a Lisp string in
19246 *STRING if the resulting string is taken from that Lisp string.
19247
19248 Note we operate on the current buffer for most purposes,
19249 the exception being w->base_line_pos. */
19250
19251 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19252
19253 static const char *
19254 decode_mode_spec (struct window *w, register int c, int field_width,
19255 int precision, Lisp_Object *string)
19256 {
19257 Lisp_Object obj;
19258 struct frame *f = XFRAME (WINDOW_FRAME (w));
19259 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19260 struct buffer *b = current_buffer;
19261
19262 obj = Qnil;
19263 *string = Qnil;
19264
19265 switch (c)
19266 {
19267 case '*':
19268 if (!NILP (b->read_only))
19269 return "%";
19270 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19271 return "*";
19272 return "-";
19273
19274 case '+':
19275 /* This differs from %* only for a modified read-only buffer. */
19276 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19277 return "*";
19278 if (!NILP (b->read_only))
19279 return "%";
19280 return "-";
19281
19282 case '&':
19283 /* This differs from %* in ignoring read-only-ness. */
19284 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19285 return "*";
19286 return "-";
19287
19288 case '%':
19289 return "%";
19290
19291 case '[':
19292 {
19293 int i;
19294 char *p;
19295
19296 if (command_loop_level > 5)
19297 return "[[[... ";
19298 p = decode_mode_spec_buf;
19299 for (i = 0; i < command_loop_level; i++)
19300 *p++ = '[';
19301 *p = 0;
19302 return decode_mode_spec_buf;
19303 }
19304
19305 case ']':
19306 {
19307 int i;
19308 char *p;
19309
19310 if (command_loop_level > 5)
19311 return " ...]]]";
19312 p = decode_mode_spec_buf;
19313 for (i = 0; i < command_loop_level; i++)
19314 *p++ = ']';
19315 *p = 0;
19316 return decode_mode_spec_buf;
19317 }
19318
19319 case '-':
19320 {
19321 register int i;
19322
19323 /* Let lots_of_dashes be a string of infinite length. */
19324 if (mode_line_target == MODE_LINE_NOPROP ||
19325 mode_line_target == MODE_LINE_STRING)
19326 return "--";
19327 if (field_width <= 0
19328 || field_width > sizeof (lots_of_dashes))
19329 {
19330 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19331 decode_mode_spec_buf[i] = '-';
19332 decode_mode_spec_buf[i] = '\0';
19333 return decode_mode_spec_buf;
19334 }
19335 else
19336 return lots_of_dashes;
19337 }
19338
19339 case 'b':
19340 obj = b->name;
19341 break;
19342
19343 case 'c':
19344 /* %c and %l are ignored in `frame-title-format'.
19345 (In redisplay_internal, the frame title is drawn _before_ the
19346 windows are updated, so the stuff which depends on actual
19347 window contents (such as %l) may fail to render properly, or
19348 even crash emacs.) */
19349 if (mode_line_target == MODE_LINE_TITLE)
19350 return "";
19351 else
19352 {
19353 int col = (int) current_column (); /* iftc */
19354 w->column_number_displayed = make_number (col);
19355 pint2str (decode_mode_spec_buf, field_width, col);
19356 return decode_mode_spec_buf;
19357 }
19358
19359 case 'e':
19360 #ifndef SYSTEM_MALLOC
19361 {
19362 if (NILP (Vmemory_full))
19363 return "";
19364 else
19365 return "!MEM FULL! ";
19366 }
19367 #else
19368 return "";
19369 #endif
19370
19371 case 'F':
19372 /* %F displays the frame name. */
19373 if (!NILP (f->title))
19374 return (char *) SDATA (f->title);
19375 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19376 return (char *) SDATA (f->name);
19377 return "Emacs";
19378
19379 case 'f':
19380 obj = b->filename;
19381 break;
19382
19383 case 'i':
19384 {
19385 int size = ZV - BEGV;
19386 pint2str (decode_mode_spec_buf, field_width, size);
19387 return decode_mode_spec_buf;
19388 }
19389
19390 case 'I':
19391 {
19392 int size = ZV - BEGV;
19393 pint2hrstr (decode_mode_spec_buf, field_width, size);
19394 return decode_mode_spec_buf;
19395 }
19396
19397 case 'l':
19398 {
19399 int startpos, startpos_byte, line, linepos, linepos_byte;
19400 int topline, nlines, junk, height;
19401
19402 /* %c and %l are ignored in `frame-title-format'. */
19403 if (mode_line_target == MODE_LINE_TITLE)
19404 return "";
19405
19406 startpos = XMARKER (w->start)->charpos;
19407 startpos_byte = marker_byte_position (w->start);
19408 height = WINDOW_TOTAL_LINES (w);
19409
19410 /* If we decided that this buffer isn't suitable for line numbers,
19411 don't forget that too fast. */
19412 if (EQ (w->base_line_pos, w->buffer))
19413 goto no_value;
19414 /* But do forget it, if the window shows a different buffer now. */
19415 else if (BUFFERP (w->base_line_pos))
19416 w->base_line_pos = Qnil;
19417
19418 /* If the buffer is very big, don't waste time. */
19419 if (INTEGERP (Vline_number_display_limit)
19420 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19421 {
19422 w->base_line_pos = Qnil;
19423 w->base_line_number = Qnil;
19424 goto no_value;
19425 }
19426
19427 if (INTEGERP (w->base_line_number)
19428 && INTEGERP (w->base_line_pos)
19429 && XFASTINT (w->base_line_pos) <= startpos)
19430 {
19431 line = XFASTINT (w->base_line_number);
19432 linepos = XFASTINT (w->base_line_pos);
19433 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19434 }
19435 else
19436 {
19437 line = 1;
19438 linepos = BUF_BEGV (b);
19439 linepos_byte = BUF_BEGV_BYTE (b);
19440 }
19441
19442 /* Count lines from base line to window start position. */
19443 nlines = display_count_lines (linepos, linepos_byte,
19444 startpos_byte,
19445 startpos, &junk);
19446
19447 topline = nlines + line;
19448
19449 /* Determine a new base line, if the old one is too close
19450 or too far away, or if we did not have one.
19451 "Too close" means it's plausible a scroll-down would
19452 go back past it. */
19453 if (startpos == BUF_BEGV (b))
19454 {
19455 w->base_line_number = make_number (topline);
19456 w->base_line_pos = make_number (BUF_BEGV (b));
19457 }
19458 else if (nlines < height + 25 || nlines > height * 3 + 50
19459 || linepos == BUF_BEGV (b))
19460 {
19461 int limit = BUF_BEGV (b);
19462 int limit_byte = BUF_BEGV_BYTE (b);
19463 int position;
19464 int distance = (height * 2 + 30) * line_number_display_limit_width;
19465
19466 if (startpos - distance > limit)
19467 {
19468 limit = startpos - distance;
19469 limit_byte = CHAR_TO_BYTE (limit);
19470 }
19471
19472 nlines = display_count_lines (startpos, startpos_byte,
19473 limit_byte,
19474 - (height * 2 + 30),
19475 &position);
19476 /* If we couldn't find the lines we wanted within
19477 line_number_display_limit_width chars per line,
19478 give up on line numbers for this window. */
19479 if (position == limit_byte && limit == startpos - distance)
19480 {
19481 w->base_line_pos = w->buffer;
19482 w->base_line_number = Qnil;
19483 goto no_value;
19484 }
19485
19486 w->base_line_number = make_number (topline - nlines);
19487 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19488 }
19489
19490 /* Now count lines from the start pos to point. */
19491 nlines = display_count_lines (startpos, startpos_byte,
19492 PT_BYTE, PT, &junk);
19493
19494 /* Record that we did display the line number. */
19495 line_number_displayed = 1;
19496
19497 /* Make the string to show. */
19498 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19499 return decode_mode_spec_buf;
19500 no_value:
19501 {
19502 char* p = decode_mode_spec_buf;
19503 int pad = field_width - 2;
19504 while (pad-- > 0)
19505 *p++ = ' ';
19506 *p++ = '?';
19507 *p++ = '?';
19508 *p = '\0';
19509 return decode_mode_spec_buf;
19510 }
19511 }
19512 break;
19513
19514 case 'm':
19515 obj = b->mode_name;
19516 break;
19517
19518 case 'n':
19519 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19520 return " Narrow";
19521 break;
19522
19523 case 'p':
19524 {
19525 int pos = marker_position (w->start);
19526 int total = BUF_ZV (b) - BUF_BEGV (b);
19527
19528 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19529 {
19530 if (pos <= BUF_BEGV (b))
19531 return "All";
19532 else
19533 return "Bottom";
19534 }
19535 else if (pos <= BUF_BEGV (b))
19536 return "Top";
19537 else
19538 {
19539 if (total > 1000000)
19540 /* Do it differently for a large value, to avoid overflow. */
19541 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19542 else
19543 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19544 /* We can't normally display a 3-digit number,
19545 so get us a 2-digit number that is close. */
19546 if (total == 100)
19547 total = 99;
19548 sprintf (decode_mode_spec_buf, "%2d%%", total);
19549 return decode_mode_spec_buf;
19550 }
19551 }
19552
19553 /* Display percentage of size above the bottom of the screen. */
19554 case 'P':
19555 {
19556 int toppos = marker_position (w->start);
19557 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19558 int total = BUF_ZV (b) - BUF_BEGV (b);
19559
19560 if (botpos >= BUF_ZV (b))
19561 {
19562 if (toppos <= BUF_BEGV (b))
19563 return "All";
19564 else
19565 return "Bottom";
19566 }
19567 else
19568 {
19569 if (total > 1000000)
19570 /* Do it differently for a large value, to avoid overflow. */
19571 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19572 else
19573 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19574 /* We can't normally display a 3-digit number,
19575 so get us a 2-digit number that is close. */
19576 if (total == 100)
19577 total = 99;
19578 if (toppos <= BUF_BEGV (b))
19579 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
19580 else
19581 sprintf (decode_mode_spec_buf, "%2d%%", total);
19582 return decode_mode_spec_buf;
19583 }
19584 }
19585
19586 case 's':
19587 /* status of process */
19588 obj = Fget_buffer_process (Fcurrent_buffer ());
19589 if (NILP (obj))
19590 return "no process";
19591 #ifndef MSDOS
19592 obj = Fsymbol_name (Fprocess_status (obj));
19593 #endif
19594 break;
19595
19596 case '@':
19597 {
19598 int count = inhibit_garbage_collection ();
19599 Lisp_Object val = call1 (intern ("file-remote-p"),
19600 current_buffer->directory);
19601 unbind_to (count, Qnil);
19602
19603 if (NILP (val))
19604 return "-";
19605 else
19606 return "@";
19607 }
19608
19609 case 't': /* indicate TEXT or BINARY */
19610 #ifdef MODE_LINE_BINARY_TEXT
19611 return MODE_LINE_BINARY_TEXT (b);
19612 #else
19613 return "T";
19614 #endif
19615
19616 case 'z':
19617 /* coding-system (not including end-of-line format) */
19618 case 'Z':
19619 /* coding-system (including end-of-line type) */
19620 {
19621 int eol_flag = (c == 'Z');
19622 char *p = decode_mode_spec_buf;
19623
19624 if (! FRAME_WINDOW_P (f))
19625 {
19626 /* No need to mention EOL here--the terminal never needs
19627 to do EOL conversion. */
19628 p = decode_mode_spec_coding (CODING_ID_NAME
19629 (FRAME_KEYBOARD_CODING (f)->id),
19630 p, 0);
19631 p = decode_mode_spec_coding (CODING_ID_NAME
19632 (FRAME_TERMINAL_CODING (f)->id),
19633 p, 0);
19634 }
19635 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19636 p, eol_flag);
19637
19638 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19639 #ifdef subprocesses
19640 obj = Fget_buffer_process (Fcurrent_buffer ());
19641 if (PROCESSP (obj))
19642 {
19643 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19644 p, eol_flag);
19645 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19646 p, eol_flag);
19647 }
19648 #endif /* subprocesses */
19649 #endif /* 0 */
19650 *p = 0;
19651 return decode_mode_spec_buf;
19652 }
19653 }
19654
19655 if (STRINGP (obj))
19656 {
19657 *string = obj;
19658 return (char *) SDATA (obj);
19659 }
19660 else
19661 return "";
19662 }
19663
19664
19665 /* Count up to COUNT lines starting from START / START_BYTE.
19666 But don't go beyond LIMIT_BYTE.
19667 Return the number of lines thus found (always nonnegative).
19668
19669 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19670
19671 static int
19672 display_count_lines (int start, int start_byte, int limit_byte, int count,
19673 int *byte_pos_ptr)
19674 {
19675 register unsigned char *cursor;
19676 unsigned char *base;
19677
19678 register int ceiling;
19679 register unsigned char *ceiling_addr;
19680 int orig_count = count;
19681
19682 /* If we are not in selective display mode,
19683 check only for newlines. */
19684 int selective_display = (!NILP (current_buffer->selective_display)
19685 && !INTEGERP (current_buffer->selective_display));
19686
19687 if (count > 0)
19688 {
19689 while (start_byte < limit_byte)
19690 {
19691 ceiling = BUFFER_CEILING_OF (start_byte);
19692 ceiling = min (limit_byte - 1, ceiling);
19693 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19694 base = (cursor = BYTE_POS_ADDR (start_byte));
19695 while (1)
19696 {
19697 if (selective_display)
19698 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19699 ;
19700 else
19701 while (*cursor != '\n' && ++cursor != ceiling_addr)
19702 ;
19703
19704 if (cursor != ceiling_addr)
19705 {
19706 if (--count == 0)
19707 {
19708 start_byte += cursor - base + 1;
19709 *byte_pos_ptr = start_byte;
19710 return orig_count;
19711 }
19712 else
19713 if (++cursor == ceiling_addr)
19714 break;
19715 }
19716 else
19717 break;
19718 }
19719 start_byte += cursor - base;
19720 }
19721 }
19722 else
19723 {
19724 while (start_byte > limit_byte)
19725 {
19726 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19727 ceiling = max (limit_byte, ceiling);
19728 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19729 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19730 while (1)
19731 {
19732 if (selective_display)
19733 while (--cursor != ceiling_addr
19734 && *cursor != '\n' && *cursor != 015)
19735 ;
19736 else
19737 while (--cursor != ceiling_addr && *cursor != '\n')
19738 ;
19739
19740 if (cursor != ceiling_addr)
19741 {
19742 if (++count == 0)
19743 {
19744 start_byte += cursor - base + 1;
19745 *byte_pos_ptr = start_byte;
19746 /* When scanning backwards, we should
19747 not count the newline posterior to which we stop. */
19748 return - orig_count - 1;
19749 }
19750 }
19751 else
19752 break;
19753 }
19754 /* Here we add 1 to compensate for the last decrement
19755 of CURSOR, which took it past the valid range. */
19756 start_byte += cursor - base + 1;
19757 }
19758 }
19759
19760 *byte_pos_ptr = limit_byte;
19761
19762 if (count < 0)
19763 return - orig_count + count;
19764 return orig_count - count;
19765
19766 }
19767
19768
19769 \f
19770 /***********************************************************************
19771 Displaying strings
19772 ***********************************************************************/
19773
19774 /* Display a NUL-terminated string, starting with index START.
19775
19776 If STRING is non-null, display that C string. Otherwise, the Lisp
19777 string LISP_STRING is displayed. There's a case that STRING is
19778 non-null and LISP_STRING is not nil. It means STRING is a string
19779 data of LISP_STRING. In that case, we display LISP_STRING while
19780 ignoring its text properties.
19781
19782 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19783 FACE_STRING. Display STRING or LISP_STRING with the face at
19784 FACE_STRING_POS in FACE_STRING:
19785
19786 Display the string in the environment given by IT, but use the
19787 standard display table, temporarily.
19788
19789 FIELD_WIDTH is the minimum number of output glyphs to produce.
19790 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19791 with spaces. If STRING has more characters, more than FIELD_WIDTH
19792 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19793
19794 PRECISION is the maximum number of characters to output from
19795 STRING. PRECISION < 0 means don't truncate the string.
19796
19797 This is roughly equivalent to printf format specifiers:
19798
19799 FIELD_WIDTH PRECISION PRINTF
19800 ----------------------------------------
19801 -1 -1 %s
19802 -1 10 %.10s
19803 10 -1 %10s
19804 20 10 %20.10s
19805
19806 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19807 display them, and < 0 means obey the current buffer's value of
19808 enable_multibyte_characters.
19809
19810 Value is the number of columns displayed. */
19811
19812 static int
19813 display_string (const unsigned char *string, Lisp_Object lisp_string, Lisp_Object face_string,
19814 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
19815 int field_width, int precision, int max_x, int multibyte)
19816 {
19817 int hpos_at_start = it->hpos;
19818 int saved_face_id = it->face_id;
19819 struct glyph_row *row = it->glyph_row;
19820
19821 /* Initialize the iterator IT for iteration over STRING beginning
19822 with index START. */
19823 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19824 precision, field_width, multibyte);
19825 if (string && STRINGP (lisp_string))
19826 /* LISP_STRING is the one returned by decode_mode_spec. We should
19827 ignore its text properties. */
19828 it->stop_charpos = -1;
19829
19830 /* If displaying STRING, set up the face of the iterator
19831 from LISP_STRING, if that's given. */
19832 if (STRINGP (face_string))
19833 {
19834 EMACS_INT endptr;
19835 struct face *face;
19836
19837 it->face_id
19838 = face_at_string_position (it->w, face_string, face_string_pos,
19839 0, it->region_beg_charpos,
19840 it->region_end_charpos,
19841 &endptr, it->base_face_id, 0);
19842 face = FACE_FROM_ID (it->f, it->face_id);
19843 it->face_box_p = face->box != FACE_NO_BOX;
19844 }
19845
19846 /* Set max_x to the maximum allowed X position. Don't let it go
19847 beyond the right edge of the window. */
19848 if (max_x <= 0)
19849 max_x = it->last_visible_x;
19850 else
19851 max_x = min (max_x, it->last_visible_x);
19852
19853 /* Skip over display elements that are not visible. because IT->w is
19854 hscrolled. */
19855 if (it->current_x < it->first_visible_x)
19856 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19857 MOVE_TO_POS | MOVE_TO_X);
19858
19859 row->ascent = it->max_ascent;
19860 row->height = it->max_ascent + it->max_descent;
19861 row->phys_ascent = it->max_phys_ascent;
19862 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19863 row->extra_line_spacing = it->max_extra_line_spacing;
19864
19865 /* This condition is for the case that we are called with current_x
19866 past last_visible_x. */
19867 while (it->current_x < max_x)
19868 {
19869 int x_before, x, n_glyphs_before, i, nglyphs;
19870
19871 /* Get the next display element. */
19872 if (!get_next_display_element (it))
19873 break;
19874
19875 /* Produce glyphs. */
19876 x_before = it->current_x;
19877 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
19878 PRODUCE_GLYPHS (it);
19879
19880 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
19881 i = 0;
19882 x = x_before;
19883 while (i < nglyphs)
19884 {
19885 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19886
19887 if (it->line_wrap != TRUNCATE
19888 && x + glyph->pixel_width > max_x)
19889 {
19890 /* End of continued line or max_x reached. */
19891 if (CHAR_GLYPH_PADDING_P (*glyph))
19892 {
19893 /* A wide character is unbreakable. */
19894 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
19895 it->current_x = x_before;
19896 }
19897 else
19898 {
19899 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
19900 it->current_x = x;
19901 }
19902 break;
19903 }
19904 else if (x + glyph->pixel_width >= it->first_visible_x)
19905 {
19906 /* Glyph is at least partially visible. */
19907 ++it->hpos;
19908 if (x < it->first_visible_x)
19909 it->glyph_row->x = x - it->first_visible_x;
19910 }
19911 else
19912 {
19913 /* Glyph is off the left margin of the display area.
19914 Should not happen. */
19915 abort ();
19916 }
19917
19918 row->ascent = max (row->ascent, it->max_ascent);
19919 row->height = max (row->height, it->max_ascent + it->max_descent);
19920 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19921 row->phys_height = max (row->phys_height,
19922 it->max_phys_ascent + it->max_phys_descent);
19923 row->extra_line_spacing = max (row->extra_line_spacing,
19924 it->max_extra_line_spacing);
19925 x += glyph->pixel_width;
19926 ++i;
19927 }
19928
19929 /* Stop if max_x reached. */
19930 if (i < nglyphs)
19931 break;
19932
19933 /* Stop at line ends. */
19934 if (ITERATOR_AT_END_OF_LINE_P (it))
19935 {
19936 it->continuation_lines_width = 0;
19937 break;
19938 }
19939
19940 set_iterator_to_next (it, 1);
19941
19942 /* Stop if truncating at the right edge. */
19943 if (it->line_wrap == TRUNCATE
19944 && it->current_x >= it->last_visible_x)
19945 {
19946 /* Add truncation mark, but don't do it if the line is
19947 truncated at a padding space. */
19948 if (IT_CHARPOS (*it) < it->string_nchars)
19949 {
19950 if (!FRAME_WINDOW_P (it->f))
19951 {
19952 int i, n;
19953
19954 if (it->current_x > it->last_visible_x)
19955 {
19956 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19957 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19958 break;
19959 for (n = row->used[TEXT_AREA]; i < n; ++i)
19960 {
19961 row->used[TEXT_AREA] = i;
19962 produce_special_glyphs (it, IT_TRUNCATION);
19963 }
19964 }
19965 produce_special_glyphs (it, IT_TRUNCATION);
19966 }
19967 it->glyph_row->truncated_on_right_p = 1;
19968 }
19969 break;
19970 }
19971 }
19972
19973 /* Maybe insert a truncation at the left. */
19974 if (it->first_visible_x
19975 && IT_CHARPOS (*it) > 0)
19976 {
19977 if (!FRAME_WINDOW_P (it->f))
19978 insert_left_trunc_glyphs (it);
19979 it->glyph_row->truncated_on_left_p = 1;
19980 }
19981
19982 it->face_id = saved_face_id;
19983
19984 /* Value is number of columns displayed. */
19985 return it->hpos - hpos_at_start;
19986 }
19987
19988
19989 \f
19990 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19991 appears as an element of LIST or as the car of an element of LIST.
19992 If PROPVAL is a list, compare each element against LIST in that
19993 way, and return 1/2 if any element of PROPVAL is found in LIST.
19994 Otherwise return 0. This function cannot quit.
19995 The return value is 2 if the text is invisible but with an ellipsis
19996 and 1 if it's invisible and without an ellipsis. */
19997
19998 int
19999 invisible_p (register Lisp_Object propval, Lisp_Object list)
20000 {
20001 register Lisp_Object tail, proptail;
20002
20003 for (tail = list; CONSP (tail); tail = XCDR (tail))
20004 {
20005 register Lisp_Object tem;
20006 tem = XCAR (tail);
20007 if (EQ (propval, tem))
20008 return 1;
20009 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20010 return NILP (XCDR (tem)) ? 1 : 2;
20011 }
20012
20013 if (CONSP (propval))
20014 {
20015 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20016 {
20017 Lisp_Object propelt;
20018 propelt = XCAR (proptail);
20019 for (tail = list; CONSP (tail); tail = XCDR (tail))
20020 {
20021 register Lisp_Object tem;
20022 tem = XCAR (tail);
20023 if (EQ (propelt, tem))
20024 return 1;
20025 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20026 return NILP (XCDR (tem)) ? 1 : 2;
20027 }
20028 }
20029 }
20030
20031 return 0;
20032 }
20033
20034 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20035 doc: /* Non-nil if the property makes the text invisible.
20036 POS-OR-PROP can be a marker or number, in which case it is taken to be
20037 a position in the current buffer and the value of the `invisible' property
20038 is checked; or it can be some other value, which is then presumed to be the
20039 value of the `invisible' property of the text of interest.
20040 The non-nil value returned can be t for truly invisible text or something
20041 else if the text is replaced by an ellipsis. */)
20042 (Lisp_Object pos_or_prop)
20043 {
20044 Lisp_Object prop
20045 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20046 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20047 : pos_or_prop);
20048 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20049 return (invis == 0 ? Qnil
20050 : invis == 1 ? Qt
20051 : make_number (invis));
20052 }
20053
20054 /* Calculate a width or height in pixels from a specification using
20055 the following elements:
20056
20057 SPEC ::=
20058 NUM - a (fractional) multiple of the default font width/height
20059 (NUM) - specifies exactly NUM pixels
20060 UNIT - a fixed number of pixels, see below.
20061 ELEMENT - size of a display element in pixels, see below.
20062 (NUM . SPEC) - equals NUM * SPEC
20063 (+ SPEC SPEC ...) - add pixel values
20064 (- SPEC SPEC ...) - subtract pixel values
20065 (- SPEC) - negate pixel value
20066
20067 NUM ::=
20068 INT or FLOAT - a number constant
20069 SYMBOL - use symbol's (buffer local) variable binding.
20070
20071 UNIT ::=
20072 in - pixels per inch *)
20073 mm - pixels per 1/1000 meter *)
20074 cm - pixels per 1/100 meter *)
20075 width - width of current font in pixels.
20076 height - height of current font in pixels.
20077
20078 *) using the ratio(s) defined in display-pixels-per-inch.
20079
20080 ELEMENT ::=
20081
20082 left-fringe - left fringe width in pixels
20083 right-fringe - right fringe width in pixels
20084
20085 left-margin - left margin width in pixels
20086 right-margin - right margin width in pixels
20087
20088 scroll-bar - scroll-bar area width in pixels
20089
20090 Examples:
20091
20092 Pixels corresponding to 5 inches:
20093 (5 . in)
20094
20095 Total width of non-text areas on left side of window (if scroll-bar is on left):
20096 '(space :width (+ left-fringe left-margin scroll-bar))
20097
20098 Align to first text column (in header line):
20099 '(space :align-to 0)
20100
20101 Align to middle of text area minus half the width of variable `my-image'
20102 containing a loaded image:
20103 '(space :align-to (0.5 . (- text my-image)))
20104
20105 Width of left margin minus width of 1 character in the default font:
20106 '(space :width (- left-margin 1))
20107
20108 Width of left margin minus width of 2 characters in the current font:
20109 '(space :width (- left-margin (2 . width)))
20110
20111 Center 1 character over left-margin (in header line):
20112 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20113
20114 Different ways to express width of left fringe plus left margin minus one pixel:
20115 '(space :width (- (+ left-fringe left-margin) (1)))
20116 '(space :width (+ left-fringe left-margin (- (1))))
20117 '(space :width (+ left-fringe left-margin (-1)))
20118
20119 */
20120
20121 #define NUMVAL(X) \
20122 ((INTEGERP (X) || FLOATP (X)) \
20123 ? XFLOATINT (X) \
20124 : - 1)
20125
20126 int
20127 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20128 struct font *font, int width_p, int *align_to)
20129 {
20130 double pixels;
20131
20132 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20133 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20134
20135 if (NILP (prop))
20136 return OK_PIXELS (0);
20137
20138 xassert (FRAME_LIVE_P (it->f));
20139
20140 if (SYMBOLP (prop))
20141 {
20142 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20143 {
20144 char *unit = SDATA (SYMBOL_NAME (prop));
20145
20146 if (unit[0] == 'i' && unit[1] == 'n')
20147 pixels = 1.0;
20148 else if (unit[0] == 'm' && unit[1] == 'm')
20149 pixels = 25.4;
20150 else if (unit[0] == 'c' && unit[1] == 'm')
20151 pixels = 2.54;
20152 else
20153 pixels = 0;
20154 if (pixels > 0)
20155 {
20156 double ppi;
20157 #ifdef HAVE_WINDOW_SYSTEM
20158 if (FRAME_WINDOW_P (it->f)
20159 && (ppi = (width_p
20160 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20161 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20162 ppi > 0))
20163 return OK_PIXELS (ppi / pixels);
20164 #endif
20165
20166 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20167 || (CONSP (Vdisplay_pixels_per_inch)
20168 && (ppi = (width_p
20169 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20170 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20171 ppi > 0)))
20172 return OK_PIXELS (ppi / pixels);
20173
20174 return 0;
20175 }
20176 }
20177
20178 #ifdef HAVE_WINDOW_SYSTEM
20179 if (EQ (prop, Qheight))
20180 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20181 if (EQ (prop, Qwidth))
20182 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20183 #else
20184 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20185 return OK_PIXELS (1);
20186 #endif
20187
20188 if (EQ (prop, Qtext))
20189 return OK_PIXELS (width_p
20190 ? window_box_width (it->w, TEXT_AREA)
20191 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20192
20193 if (align_to && *align_to < 0)
20194 {
20195 *res = 0;
20196 if (EQ (prop, Qleft))
20197 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20198 if (EQ (prop, Qright))
20199 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20200 if (EQ (prop, Qcenter))
20201 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20202 + window_box_width (it->w, TEXT_AREA) / 2);
20203 if (EQ (prop, Qleft_fringe))
20204 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20205 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20206 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20207 if (EQ (prop, Qright_fringe))
20208 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20209 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20210 : window_box_right_offset (it->w, TEXT_AREA));
20211 if (EQ (prop, Qleft_margin))
20212 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20213 if (EQ (prop, Qright_margin))
20214 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20215 if (EQ (prop, Qscroll_bar))
20216 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20217 ? 0
20218 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20219 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20220 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20221 : 0)));
20222 }
20223 else
20224 {
20225 if (EQ (prop, Qleft_fringe))
20226 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20227 if (EQ (prop, Qright_fringe))
20228 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20229 if (EQ (prop, Qleft_margin))
20230 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20231 if (EQ (prop, Qright_margin))
20232 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20233 if (EQ (prop, Qscroll_bar))
20234 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20235 }
20236
20237 prop = Fbuffer_local_value (prop, it->w->buffer);
20238 }
20239
20240 if (INTEGERP (prop) || FLOATP (prop))
20241 {
20242 int base_unit = (width_p
20243 ? FRAME_COLUMN_WIDTH (it->f)
20244 : FRAME_LINE_HEIGHT (it->f));
20245 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20246 }
20247
20248 if (CONSP (prop))
20249 {
20250 Lisp_Object car = XCAR (prop);
20251 Lisp_Object cdr = XCDR (prop);
20252
20253 if (SYMBOLP (car))
20254 {
20255 #ifdef HAVE_WINDOW_SYSTEM
20256 if (FRAME_WINDOW_P (it->f)
20257 && valid_image_p (prop))
20258 {
20259 int id = lookup_image (it->f, prop);
20260 struct image *img = IMAGE_FROM_ID (it->f, id);
20261
20262 return OK_PIXELS (width_p ? img->width : img->height);
20263 }
20264 #endif
20265 if (EQ (car, Qplus) || EQ (car, Qminus))
20266 {
20267 int first = 1;
20268 double px;
20269
20270 pixels = 0;
20271 while (CONSP (cdr))
20272 {
20273 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20274 font, width_p, align_to))
20275 return 0;
20276 if (first)
20277 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20278 else
20279 pixels += px;
20280 cdr = XCDR (cdr);
20281 }
20282 if (EQ (car, Qminus))
20283 pixels = -pixels;
20284 return OK_PIXELS (pixels);
20285 }
20286
20287 car = Fbuffer_local_value (car, it->w->buffer);
20288 }
20289
20290 if (INTEGERP (car) || FLOATP (car))
20291 {
20292 double fact;
20293 pixels = XFLOATINT (car);
20294 if (NILP (cdr))
20295 return OK_PIXELS (pixels);
20296 if (calc_pixel_width_or_height (&fact, it, cdr,
20297 font, width_p, align_to))
20298 return OK_PIXELS (pixels * fact);
20299 return 0;
20300 }
20301
20302 return 0;
20303 }
20304
20305 return 0;
20306 }
20307
20308 \f
20309 /***********************************************************************
20310 Glyph Display
20311 ***********************************************************************/
20312
20313 #ifdef HAVE_WINDOW_SYSTEM
20314
20315 #if GLYPH_DEBUG
20316
20317 void
20318 dump_glyph_string (s)
20319 struct glyph_string *s;
20320 {
20321 fprintf (stderr, "glyph string\n");
20322 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20323 s->x, s->y, s->width, s->height);
20324 fprintf (stderr, " ybase = %d\n", s->ybase);
20325 fprintf (stderr, " hl = %d\n", s->hl);
20326 fprintf (stderr, " left overhang = %d, right = %d\n",
20327 s->left_overhang, s->right_overhang);
20328 fprintf (stderr, " nchars = %d\n", s->nchars);
20329 fprintf (stderr, " extends to end of line = %d\n",
20330 s->extends_to_end_of_line_p);
20331 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20332 fprintf (stderr, " bg width = %d\n", s->background_width);
20333 }
20334
20335 #endif /* GLYPH_DEBUG */
20336
20337 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20338 of XChar2b structures for S; it can't be allocated in
20339 init_glyph_string because it must be allocated via `alloca'. W
20340 is the window on which S is drawn. ROW and AREA are the glyph row
20341 and area within the row from which S is constructed. START is the
20342 index of the first glyph structure covered by S. HL is a
20343 face-override for drawing S. */
20344
20345 #ifdef HAVE_NTGUI
20346 #define OPTIONAL_HDC(hdc) HDC hdc,
20347 #define DECLARE_HDC(hdc) HDC hdc;
20348 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20349 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20350 #endif
20351
20352 #ifndef OPTIONAL_HDC
20353 #define OPTIONAL_HDC(hdc)
20354 #define DECLARE_HDC(hdc)
20355 #define ALLOCATE_HDC(hdc, f)
20356 #define RELEASE_HDC(hdc, f)
20357 #endif
20358
20359 static void
20360 init_glyph_string (struct glyph_string *s,
20361 OPTIONAL_HDC (hdc)
20362 XChar2b *char2b, struct window *w, struct glyph_row *row,
20363 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20364 {
20365 memset (s, 0, sizeof *s);
20366 s->w = w;
20367 s->f = XFRAME (w->frame);
20368 #ifdef HAVE_NTGUI
20369 s->hdc = hdc;
20370 #endif
20371 s->display = FRAME_X_DISPLAY (s->f);
20372 s->window = FRAME_X_WINDOW (s->f);
20373 s->char2b = char2b;
20374 s->hl = hl;
20375 s->row = row;
20376 s->area = area;
20377 s->first_glyph = row->glyphs[area] + start;
20378 s->height = row->height;
20379 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20380 s->ybase = s->y + row->ascent;
20381 }
20382
20383
20384 /* Append the list of glyph strings with head H and tail T to the list
20385 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20386
20387 static INLINE void
20388 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20389 struct glyph_string *h, struct glyph_string *t)
20390 {
20391 if (h)
20392 {
20393 if (*head)
20394 (*tail)->next = h;
20395 else
20396 *head = h;
20397 h->prev = *tail;
20398 *tail = t;
20399 }
20400 }
20401
20402
20403 /* Prepend the list of glyph strings with head H and tail T to the
20404 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20405 result. */
20406
20407 static INLINE void
20408 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20409 struct glyph_string *h, struct glyph_string *t)
20410 {
20411 if (h)
20412 {
20413 if (*head)
20414 (*head)->prev = t;
20415 else
20416 *tail = t;
20417 t->next = *head;
20418 *head = h;
20419 }
20420 }
20421
20422
20423 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20424 Set *HEAD and *TAIL to the resulting list. */
20425
20426 static INLINE void
20427 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20428 struct glyph_string *s)
20429 {
20430 s->next = s->prev = NULL;
20431 append_glyph_string_lists (head, tail, s, s);
20432 }
20433
20434
20435 /* Get face and two-byte form of character C in face FACE_ID on frame
20436 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20437 means we want to display multibyte text. DISPLAY_P non-zero means
20438 make sure that X resources for the face returned are allocated.
20439 Value is a pointer to a realized face that is ready for display if
20440 DISPLAY_P is non-zero. */
20441
20442 static INLINE struct face *
20443 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20444 XChar2b *char2b, int multibyte_p, int display_p)
20445 {
20446 struct face *face = FACE_FROM_ID (f, face_id);
20447
20448 if (face->font)
20449 {
20450 unsigned code = face->font->driver->encode_char (face->font, c);
20451
20452 if (code != FONT_INVALID_CODE)
20453 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20454 else
20455 STORE_XCHAR2B (char2b, 0, 0);
20456 }
20457
20458 /* Make sure X resources of the face are allocated. */
20459 #ifdef HAVE_X_WINDOWS
20460 if (display_p)
20461 #endif
20462 {
20463 xassert (face != NULL);
20464 PREPARE_FACE_FOR_DISPLAY (f, face);
20465 }
20466
20467 return face;
20468 }
20469
20470
20471 /* Get face and two-byte form of character glyph GLYPH on frame F.
20472 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20473 a pointer to a realized face that is ready for display. */
20474
20475 static INLINE struct face *
20476 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20477 XChar2b *char2b, int *two_byte_p)
20478 {
20479 struct face *face;
20480
20481 xassert (glyph->type == CHAR_GLYPH);
20482 face = FACE_FROM_ID (f, glyph->face_id);
20483
20484 if (two_byte_p)
20485 *two_byte_p = 0;
20486
20487 if (face->font)
20488 {
20489 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
20490
20491 if (code != FONT_INVALID_CODE)
20492 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20493 else
20494 STORE_XCHAR2B (char2b, 0, 0);
20495 }
20496
20497 /* Make sure X resources of the face are allocated. */
20498 xassert (face != NULL);
20499 PREPARE_FACE_FOR_DISPLAY (f, face);
20500 return face;
20501 }
20502
20503
20504 /* Fill glyph string S with composition components specified by S->cmp.
20505
20506 BASE_FACE is the base face of the composition.
20507 S->cmp_from is the index of the first component for S.
20508
20509 OVERLAPS non-zero means S should draw the foreground only, and use
20510 its physical height for clipping. See also draw_glyphs.
20511
20512 Value is the index of a component not in S. */
20513
20514 static int
20515 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20516 int overlaps)
20517 {
20518 int i;
20519 /* For all glyphs of this composition, starting at the offset
20520 S->cmp_from, until we reach the end of the definition or encounter a
20521 glyph that requires the different face, add it to S. */
20522 struct face *face;
20523
20524 xassert (s);
20525
20526 s->for_overlaps = overlaps;
20527 s->face = NULL;
20528 s->font = NULL;
20529 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20530 {
20531 int c = COMPOSITION_GLYPH (s->cmp, i);
20532
20533 if (c != '\t')
20534 {
20535 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20536 -1, Qnil);
20537
20538 face = get_char_face_and_encoding (s->f, c, face_id,
20539 s->char2b + i, 1, 1);
20540 if (face)
20541 {
20542 if (! s->face)
20543 {
20544 s->face = face;
20545 s->font = s->face->font;
20546 }
20547 else if (s->face != face)
20548 break;
20549 }
20550 }
20551 ++s->nchars;
20552 }
20553 s->cmp_to = i;
20554
20555 /* All glyph strings for the same composition has the same width,
20556 i.e. the width set for the first component of the composition. */
20557 s->width = s->first_glyph->pixel_width;
20558
20559 /* If the specified font could not be loaded, use the frame's
20560 default font, but record the fact that we couldn't load it in
20561 the glyph string so that we can draw rectangles for the
20562 characters of the glyph string. */
20563 if (s->font == NULL)
20564 {
20565 s->font_not_found_p = 1;
20566 s->font = FRAME_FONT (s->f);
20567 }
20568
20569 /* Adjust base line for subscript/superscript text. */
20570 s->ybase += s->first_glyph->voffset;
20571
20572 /* This glyph string must always be drawn with 16-bit functions. */
20573 s->two_byte_p = 1;
20574
20575 return s->cmp_to;
20576 }
20577
20578 static int
20579 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
20580 int start, int end, int overlaps)
20581 {
20582 struct glyph *glyph, *last;
20583 Lisp_Object lgstring;
20584 int i;
20585
20586 s->for_overlaps = overlaps;
20587 glyph = s->row->glyphs[s->area] + start;
20588 last = s->row->glyphs[s->area] + end;
20589 s->cmp_id = glyph->u.cmp.id;
20590 s->cmp_from = glyph->u.cmp.from;
20591 s->cmp_to = glyph->u.cmp.to + 1;
20592 s->face = FACE_FROM_ID (s->f, face_id);
20593 lgstring = composition_gstring_from_id (s->cmp_id);
20594 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20595 glyph++;
20596 while (glyph < last
20597 && glyph->u.cmp.automatic
20598 && glyph->u.cmp.id == s->cmp_id
20599 && s->cmp_to == glyph->u.cmp.from)
20600 s->cmp_to = (glyph++)->u.cmp.to + 1;
20601
20602 for (i = s->cmp_from; i < s->cmp_to; i++)
20603 {
20604 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20605 unsigned code = LGLYPH_CODE (lglyph);
20606
20607 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20608 }
20609 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20610 return glyph - s->row->glyphs[s->area];
20611 }
20612
20613
20614 /* Fill glyph string S from a sequence of character glyphs.
20615
20616 FACE_ID is the face id of the string. START is the index of the
20617 first glyph to consider, END is the index of the last + 1.
20618 OVERLAPS non-zero means S should draw the foreground only, and use
20619 its physical height for clipping. See also draw_glyphs.
20620
20621 Value is the index of the first glyph not in S. */
20622
20623 static int
20624 fill_glyph_string (struct glyph_string *s, int face_id,
20625 int start, int end, int overlaps)
20626 {
20627 struct glyph *glyph, *last;
20628 int voffset;
20629 int glyph_not_available_p;
20630
20631 xassert (s->f == XFRAME (s->w->frame));
20632 xassert (s->nchars == 0);
20633 xassert (start >= 0 && end > start);
20634
20635 s->for_overlaps = overlaps;
20636 glyph = s->row->glyphs[s->area] + start;
20637 last = s->row->glyphs[s->area] + end;
20638 voffset = glyph->voffset;
20639 s->padding_p = glyph->padding_p;
20640 glyph_not_available_p = glyph->glyph_not_available_p;
20641
20642 while (glyph < last
20643 && glyph->type == CHAR_GLYPH
20644 && glyph->voffset == voffset
20645 /* Same face id implies same font, nowadays. */
20646 && glyph->face_id == face_id
20647 && glyph->glyph_not_available_p == glyph_not_available_p)
20648 {
20649 int two_byte_p;
20650
20651 s->face = get_glyph_face_and_encoding (s->f, glyph,
20652 s->char2b + s->nchars,
20653 &two_byte_p);
20654 s->two_byte_p = two_byte_p;
20655 ++s->nchars;
20656 xassert (s->nchars <= end - start);
20657 s->width += glyph->pixel_width;
20658 if (glyph++->padding_p != s->padding_p)
20659 break;
20660 }
20661
20662 s->font = s->face->font;
20663
20664 /* If the specified font could not be loaded, use the frame's font,
20665 but record the fact that we couldn't load it in
20666 S->font_not_found_p so that we can draw rectangles for the
20667 characters of the glyph string. */
20668 if (s->font == NULL || glyph_not_available_p)
20669 {
20670 s->font_not_found_p = 1;
20671 s->font = FRAME_FONT (s->f);
20672 }
20673
20674 /* Adjust base line for subscript/superscript text. */
20675 s->ybase += voffset;
20676
20677 xassert (s->face && s->face->gc);
20678 return glyph - s->row->glyphs[s->area];
20679 }
20680
20681
20682 /* Fill glyph string S from image glyph S->first_glyph. */
20683
20684 static void
20685 fill_image_glyph_string (struct glyph_string *s)
20686 {
20687 xassert (s->first_glyph->type == IMAGE_GLYPH);
20688 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20689 xassert (s->img);
20690 s->slice = s->first_glyph->slice;
20691 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20692 s->font = s->face->font;
20693 s->width = s->first_glyph->pixel_width;
20694
20695 /* Adjust base line for subscript/superscript text. */
20696 s->ybase += s->first_glyph->voffset;
20697 }
20698
20699
20700 /* Fill glyph string S from a sequence of stretch glyphs.
20701
20702 ROW is the glyph row in which the glyphs are found, AREA is the
20703 area within the row. START is the index of the first glyph to
20704 consider, END is the index of the last + 1.
20705
20706 Value is the index of the first glyph not in S. */
20707
20708 static int
20709 fill_stretch_glyph_string (struct glyph_string *s, struct glyph_row *row,
20710 enum glyph_row_area area, int start, int end)
20711 {
20712 struct glyph *glyph, *last;
20713 int voffset, face_id;
20714
20715 xassert (s->first_glyph->type == STRETCH_GLYPH);
20716
20717 glyph = s->row->glyphs[s->area] + start;
20718 last = s->row->glyphs[s->area] + end;
20719 face_id = glyph->face_id;
20720 s->face = FACE_FROM_ID (s->f, face_id);
20721 s->font = s->face->font;
20722 s->width = glyph->pixel_width;
20723 s->nchars = 1;
20724 voffset = glyph->voffset;
20725
20726 for (++glyph;
20727 (glyph < last
20728 && glyph->type == STRETCH_GLYPH
20729 && glyph->voffset == voffset
20730 && glyph->face_id == face_id);
20731 ++glyph)
20732 s->width += glyph->pixel_width;
20733
20734 /* Adjust base line for subscript/superscript text. */
20735 s->ybase += voffset;
20736
20737 /* The case that face->gc == 0 is handled when drawing the glyph
20738 string by calling PREPARE_FACE_FOR_DISPLAY. */
20739 xassert (s->face);
20740 return glyph - s->row->glyphs[s->area];
20741 }
20742
20743 static struct font_metrics *
20744 get_per_char_metric (struct frame *f, struct font *font, XChar2b *char2b)
20745 {
20746 static struct font_metrics metrics;
20747 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20748
20749 if (! font || code == FONT_INVALID_CODE)
20750 return NULL;
20751 font->driver->text_extents (font, &code, 1, &metrics);
20752 return &metrics;
20753 }
20754
20755 /* EXPORT for RIF:
20756 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20757 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20758 assumed to be zero. */
20759
20760 void
20761 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
20762 {
20763 *left = *right = 0;
20764
20765 if (glyph->type == CHAR_GLYPH)
20766 {
20767 struct face *face;
20768 XChar2b char2b;
20769 struct font_metrics *pcm;
20770
20771 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20772 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20773 {
20774 if (pcm->rbearing > pcm->width)
20775 *right = pcm->rbearing - pcm->width;
20776 if (pcm->lbearing < 0)
20777 *left = -pcm->lbearing;
20778 }
20779 }
20780 else if (glyph->type == COMPOSITE_GLYPH)
20781 {
20782 if (! glyph->u.cmp.automatic)
20783 {
20784 struct composition *cmp = composition_table[glyph->u.cmp.id];
20785
20786 if (cmp->rbearing > cmp->pixel_width)
20787 *right = cmp->rbearing - cmp->pixel_width;
20788 if (cmp->lbearing < 0)
20789 *left = - cmp->lbearing;
20790 }
20791 else
20792 {
20793 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20794 struct font_metrics metrics;
20795
20796 composition_gstring_width (gstring, glyph->u.cmp.from,
20797 glyph->u.cmp.to + 1, &metrics);
20798 if (metrics.rbearing > metrics.width)
20799 *right = metrics.rbearing - metrics.width;
20800 if (metrics.lbearing < 0)
20801 *left = - metrics.lbearing;
20802 }
20803 }
20804 }
20805
20806
20807 /* Return the index of the first glyph preceding glyph string S that
20808 is overwritten by S because of S's left overhang. Value is -1
20809 if no glyphs are overwritten. */
20810
20811 static int
20812 left_overwritten (struct glyph_string *s)
20813 {
20814 int k;
20815
20816 if (s->left_overhang)
20817 {
20818 int x = 0, i;
20819 struct glyph *glyphs = s->row->glyphs[s->area];
20820 int first = s->first_glyph - glyphs;
20821
20822 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
20823 x -= glyphs[i].pixel_width;
20824
20825 k = i + 1;
20826 }
20827 else
20828 k = -1;
20829
20830 return k;
20831 }
20832
20833
20834 /* Return the index of the first glyph preceding glyph string S that
20835 is overwriting S because of its right overhang. Value is -1 if no
20836 glyph in front of S overwrites S. */
20837
20838 static int
20839 left_overwriting (struct glyph_string *s)
20840 {
20841 int i, k, x;
20842 struct glyph *glyphs = s->row->glyphs[s->area];
20843 int first = s->first_glyph - glyphs;
20844
20845 k = -1;
20846 x = 0;
20847 for (i = first - 1; i >= 0; --i)
20848 {
20849 int left, right;
20850 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20851 if (x + right > 0)
20852 k = i;
20853 x -= glyphs[i].pixel_width;
20854 }
20855
20856 return k;
20857 }
20858
20859
20860 /* Return the index of the last glyph following glyph string S that is
20861 overwritten by S because of S's right overhang. Value is -1 if
20862 no such glyph is found. */
20863
20864 static int
20865 right_overwritten (struct glyph_string *s)
20866 {
20867 int k = -1;
20868
20869 if (s->right_overhang)
20870 {
20871 int x = 0, i;
20872 struct glyph *glyphs = s->row->glyphs[s->area];
20873 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20874 int end = s->row->used[s->area];
20875
20876 for (i = first; i < end && s->right_overhang > x; ++i)
20877 x += glyphs[i].pixel_width;
20878
20879 k = i;
20880 }
20881
20882 return k;
20883 }
20884
20885
20886 /* Return the index of the last glyph following glyph string S that
20887 overwrites S because of its left overhang. Value is negative
20888 if no such glyph is found. */
20889
20890 static int
20891 right_overwriting (struct glyph_string *s)
20892 {
20893 int i, k, x;
20894 int end = s->row->used[s->area];
20895 struct glyph *glyphs = s->row->glyphs[s->area];
20896 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20897
20898 k = -1;
20899 x = 0;
20900 for (i = first; i < end; ++i)
20901 {
20902 int left, right;
20903 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20904 if (x - left < 0)
20905 k = i;
20906 x += glyphs[i].pixel_width;
20907 }
20908
20909 return k;
20910 }
20911
20912
20913 /* Set background width of glyph string S. START is the index of the
20914 first glyph following S. LAST_X is the right-most x-position + 1
20915 in the drawing area. */
20916
20917 static INLINE void
20918 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
20919 {
20920 /* If the face of this glyph string has to be drawn to the end of
20921 the drawing area, set S->extends_to_end_of_line_p. */
20922
20923 if (start == s->row->used[s->area]
20924 && s->area == TEXT_AREA
20925 && ((s->row->fill_line_p
20926 && (s->hl == DRAW_NORMAL_TEXT
20927 || s->hl == DRAW_IMAGE_RAISED
20928 || s->hl == DRAW_IMAGE_SUNKEN))
20929 || s->hl == DRAW_MOUSE_FACE))
20930 s->extends_to_end_of_line_p = 1;
20931
20932 /* If S extends its face to the end of the line, set its
20933 background_width to the distance to the right edge of the drawing
20934 area. */
20935 if (s->extends_to_end_of_line_p)
20936 s->background_width = last_x - s->x + 1;
20937 else
20938 s->background_width = s->width;
20939 }
20940
20941
20942 /* Compute overhangs and x-positions for glyph string S and its
20943 predecessors, or successors. X is the starting x-position for S.
20944 BACKWARD_P non-zero means process predecessors. */
20945
20946 static void
20947 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
20948 {
20949 if (backward_p)
20950 {
20951 while (s)
20952 {
20953 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20954 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20955 x -= s->width;
20956 s->x = x;
20957 s = s->prev;
20958 }
20959 }
20960 else
20961 {
20962 while (s)
20963 {
20964 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20965 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20966 s->x = x;
20967 x += s->width;
20968 s = s->next;
20969 }
20970 }
20971 }
20972
20973
20974
20975 /* The following macros are only called from draw_glyphs below.
20976 They reference the following parameters of that function directly:
20977 `w', `row', `area', and `overlap_p'
20978 as well as the following local variables:
20979 `s', `f', and `hdc' (in W32) */
20980
20981 #ifdef HAVE_NTGUI
20982 /* On W32, silently add local `hdc' variable to argument list of
20983 init_glyph_string. */
20984 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20985 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20986 #else
20987 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20988 init_glyph_string (s, char2b, w, row, area, start, hl)
20989 #endif
20990
20991 /* Add a glyph string for a stretch glyph to the list of strings
20992 between HEAD and TAIL. START is the index of the stretch glyph in
20993 row area AREA of glyph row ROW. END is the index of the last glyph
20994 in that glyph row area. X is the current output position assigned
20995 to the new glyph string constructed. HL overrides that face of the
20996 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20997 is the right-most x-position of the drawing area. */
20998
20999 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21000 and below -- keep them on one line. */
21001 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21002 do \
21003 { \
21004 s = (struct glyph_string *) alloca (sizeof *s); \
21005 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21006 START = fill_stretch_glyph_string (s, row, area, START, END); \
21007 append_glyph_string (&HEAD, &TAIL, s); \
21008 s->x = (X); \
21009 } \
21010 while (0)
21011
21012
21013 /* Add a glyph string for an image glyph to the list of strings
21014 between HEAD and TAIL. START is the index of the image glyph in
21015 row area AREA of glyph row ROW. END is the index of the last glyph
21016 in that glyph row area. X is the current output position assigned
21017 to the new glyph string constructed. HL overrides that face of the
21018 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21019 is the right-most x-position of the drawing area. */
21020
21021 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21022 do \
21023 { \
21024 s = (struct glyph_string *) alloca (sizeof *s); \
21025 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21026 fill_image_glyph_string (s); \
21027 append_glyph_string (&HEAD, &TAIL, s); \
21028 ++START; \
21029 s->x = (X); \
21030 } \
21031 while (0)
21032
21033
21034 /* Add a glyph string for a sequence of character glyphs to the list
21035 of strings between HEAD and TAIL. START is the index of the first
21036 glyph in row area AREA of glyph row ROW that is part of the new
21037 glyph string. END is the index of the last glyph in that glyph row
21038 area. X is the current output position assigned to the new glyph
21039 string constructed. HL overrides that face of the glyph; e.g. it
21040 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21041 right-most x-position of the drawing area. */
21042
21043 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21044 do \
21045 { \
21046 int face_id; \
21047 XChar2b *char2b; \
21048 \
21049 face_id = (row)->glyphs[area][START].face_id; \
21050 \
21051 s = (struct glyph_string *) alloca (sizeof *s); \
21052 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21053 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21054 append_glyph_string (&HEAD, &TAIL, s); \
21055 s->x = (X); \
21056 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21057 } \
21058 while (0)
21059
21060
21061 /* Add a glyph string for a composite sequence to the list of strings
21062 between HEAD and TAIL. START is the index of the first glyph in
21063 row area AREA of glyph row ROW that is part of the new glyph
21064 string. END is the index of the last glyph in that glyph row area.
21065 X is the current output position assigned to the new glyph string
21066 constructed. HL overrides that face of the glyph; e.g. it is
21067 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21068 x-position of the drawing area. */
21069
21070 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21071 do { \
21072 int face_id = (row)->glyphs[area][START].face_id; \
21073 struct face *base_face = FACE_FROM_ID (f, face_id); \
21074 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21075 struct composition *cmp = composition_table[cmp_id]; \
21076 XChar2b *char2b; \
21077 struct glyph_string *first_s; \
21078 int n; \
21079 \
21080 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21081 \
21082 /* Make glyph_strings for each glyph sequence that is drawable by \
21083 the same face, and append them to HEAD/TAIL. */ \
21084 for (n = 0; n < cmp->glyph_len;) \
21085 { \
21086 s = (struct glyph_string *) alloca (sizeof *s); \
21087 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21088 append_glyph_string (&(HEAD), &(TAIL), s); \
21089 s->cmp = cmp; \
21090 s->cmp_from = n; \
21091 s->x = (X); \
21092 if (n == 0) \
21093 first_s = s; \
21094 n = fill_composite_glyph_string (s, base_face, overlaps); \
21095 } \
21096 \
21097 ++START; \
21098 s = first_s; \
21099 } while (0)
21100
21101
21102 /* Add a glyph string for a glyph-string sequence to the list of strings
21103 between HEAD and TAIL. */
21104
21105 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21106 do { \
21107 int face_id; \
21108 XChar2b *char2b; \
21109 Lisp_Object gstring; \
21110 \
21111 face_id = (row)->glyphs[area][START].face_id; \
21112 gstring = (composition_gstring_from_id \
21113 ((row)->glyphs[area][START].u.cmp.id)); \
21114 s = (struct glyph_string *) alloca (sizeof *s); \
21115 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21116 * LGSTRING_GLYPH_LEN (gstring)); \
21117 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21118 append_glyph_string (&(HEAD), &(TAIL), s); \
21119 s->x = (X); \
21120 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21121 } while (0)
21122
21123
21124 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21125 of AREA of glyph row ROW on window W between indices START and END.
21126 HL overrides the face for drawing glyph strings, e.g. it is
21127 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21128 x-positions of the drawing area.
21129
21130 This is an ugly monster macro construct because we must use alloca
21131 to allocate glyph strings (because draw_glyphs can be called
21132 asynchronously). */
21133
21134 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21135 do \
21136 { \
21137 HEAD = TAIL = NULL; \
21138 while (START < END) \
21139 { \
21140 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21141 switch (first_glyph->type) \
21142 { \
21143 case CHAR_GLYPH: \
21144 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21145 HL, X, LAST_X); \
21146 break; \
21147 \
21148 case COMPOSITE_GLYPH: \
21149 if (first_glyph->u.cmp.automatic) \
21150 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21151 HL, X, LAST_X); \
21152 else \
21153 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21154 HL, X, LAST_X); \
21155 break; \
21156 \
21157 case STRETCH_GLYPH: \
21158 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21159 HL, X, LAST_X); \
21160 break; \
21161 \
21162 case IMAGE_GLYPH: \
21163 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21164 HL, X, LAST_X); \
21165 break; \
21166 \
21167 default: \
21168 abort (); \
21169 } \
21170 \
21171 if (s) \
21172 { \
21173 set_glyph_string_background_width (s, START, LAST_X); \
21174 (X) += s->width; \
21175 } \
21176 } \
21177 } while (0)
21178
21179
21180 /* Draw glyphs between START and END in AREA of ROW on window W,
21181 starting at x-position X. X is relative to AREA in W. HL is a
21182 face-override with the following meaning:
21183
21184 DRAW_NORMAL_TEXT draw normally
21185 DRAW_CURSOR draw in cursor face
21186 DRAW_MOUSE_FACE draw in mouse face.
21187 DRAW_INVERSE_VIDEO draw in mode line face
21188 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21189 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21190
21191 If OVERLAPS is non-zero, draw only the foreground of characters and
21192 clip to the physical height of ROW. Non-zero value also defines
21193 the overlapping part to be drawn:
21194
21195 OVERLAPS_PRED overlap with preceding rows
21196 OVERLAPS_SUCC overlap with succeeding rows
21197 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21198 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21199
21200 Value is the x-position reached, relative to AREA of W. */
21201
21202 static int
21203 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21204 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21205 enum draw_glyphs_face hl, int overlaps)
21206 {
21207 struct glyph_string *head, *tail;
21208 struct glyph_string *s;
21209 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21210 int i, j, x_reached, last_x, area_left = 0;
21211 struct frame *f = XFRAME (WINDOW_FRAME (w));
21212 DECLARE_HDC (hdc);
21213
21214 ALLOCATE_HDC (hdc, f);
21215
21216 /* Let's rather be paranoid than getting a SEGV. */
21217 end = min (end, row->used[area]);
21218 start = max (0, start);
21219 start = min (end, start);
21220
21221 /* Translate X to frame coordinates. Set last_x to the right
21222 end of the drawing area. */
21223 if (row->full_width_p)
21224 {
21225 /* X is relative to the left edge of W, without scroll bars
21226 or fringes. */
21227 area_left = WINDOW_LEFT_EDGE_X (w);
21228 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21229 }
21230 else
21231 {
21232 area_left = window_box_left (w, area);
21233 last_x = area_left + window_box_width (w, area);
21234 }
21235 x += area_left;
21236
21237 /* Build a doubly-linked list of glyph_string structures between
21238 head and tail from what we have to draw. Note that the macro
21239 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21240 the reason we use a separate variable `i'. */
21241 i = start;
21242 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21243 if (tail)
21244 x_reached = tail->x + tail->background_width;
21245 else
21246 x_reached = x;
21247
21248 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21249 the row, redraw some glyphs in front or following the glyph
21250 strings built above. */
21251 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21252 {
21253 struct glyph_string *h, *t;
21254 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21255 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
21256 int dummy_x = 0;
21257
21258 /* If mouse highlighting is on, we may need to draw adjacent
21259 glyphs using mouse-face highlighting. */
21260 if (area == TEXT_AREA && row->mouse_face_p)
21261 {
21262 struct glyph_row *mouse_beg_row, *mouse_end_row;
21263
21264 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21265 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21266
21267 if (row >= mouse_beg_row && row <= mouse_end_row)
21268 {
21269 check_mouse_face = 1;
21270 mouse_beg_col = (row == mouse_beg_row)
21271 ? dpyinfo->mouse_face_beg_col : 0;
21272 mouse_end_col = (row == mouse_end_row)
21273 ? dpyinfo->mouse_face_end_col
21274 : row->used[TEXT_AREA];
21275 }
21276 }
21277
21278 /* Compute overhangs for all glyph strings. */
21279 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21280 for (s = head; s; s = s->next)
21281 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21282
21283 /* Prepend glyph strings for glyphs in front of the first glyph
21284 string that are overwritten because of the first glyph
21285 string's left overhang. The background of all strings
21286 prepended must be drawn because the first glyph string
21287 draws over it. */
21288 i = left_overwritten (head);
21289 if (i >= 0)
21290 {
21291 enum draw_glyphs_face overlap_hl;
21292
21293 /* If this row contains mouse highlighting, attempt to draw
21294 the overlapped glyphs with the correct highlight. This
21295 code fails if the overlap encompasses more than one glyph
21296 and mouse-highlight spans only some of these glyphs.
21297 However, making it work perfectly involves a lot more
21298 code, and I don't know if the pathological case occurs in
21299 practice, so we'll stick to this for now. --- cyd */
21300 if (check_mouse_face
21301 && mouse_beg_col < start && mouse_end_col > i)
21302 overlap_hl = DRAW_MOUSE_FACE;
21303 else
21304 overlap_hl = DRAW_NORMAL_TEXT;
21305
21306 j = i;
21307 BUILD_GLYPH_STRINGS (j, start, h, t,
21308 overlap_hl, dummy_x, last_x);
21309 start = i;
21310 compute_overhangs_and_x (t, head->x, 1);
21311 prepend_glyph_string_lists (&head, &tail, h, t);
21312 clip_head = head;
21313 }
21314
21315 /* Prepend glyph strings for glyphs in front of the first glyph
21316 string that overwrite that glyph string because of their
21317 right overhang. For these strings, only the foreground must
21318 be drawn, because it draws over the glyph string at `head'.
21319 The background must not be drawn because this would overwrite
21320 right overhangs of preceding glyphs for which no glyph
21321 strings exist. */
21322 i = left_overwriting (head);
21323 if (i >= 0)
21324 {
21325 enum draw_glyphs_face overlap_hl;
21326
21327 if (check_mouse_face
21328 && mouse_beg_col < start && mouse_end_col > i)
21329 overlap_hl = DRAW_MOUSE_FACE;
21330 else
21331 overlap_hl = DRAW_NORMAL_TEXT;
21332
21333 clip_head = head;
21334 BUILD_GLYPH_STRINGS (i, start, h, t,
21335 overlap_hl, dummy_x, last_x);
21336 for (s = h; s; s = s->next)
21337 s->background_filled_p = 1;
21338 compute_overhangs_and_x (t, head->x, 1);
21339 prepend_glyph_string_lists (&head, &tail, h, t);
21340 }
21341
21342 /* Append glyphs strings for glyphs following the last glyph
21343 string tail that are overwritten by tail. The background of
21344 these strings has to be drawn because tail's foreground draws
21345 over it. */
21346 i = right_overwritten (tail);
21347 if (i >= 0)
21348 {
21349 enum draw_glyphs_face overlap_hl;
21350
21351 if (check_mouse_face
21352 && mouse_beg_col < i && mouse_end_col > end)
21353 overlap_hl = DRAW_MOUSE_FACE;
21354 else
21355 overlap_hl = DRAW_NORMAL_TEXT;
21356
21357 BUILD_GLYPH_STRINGS (end, i, h, t,
21358 overlap_hl, x, last_x);
21359 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21360 we don't have `end = i;' here. */
21361 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21362 append_glyph_string_lists (&head, &tail, h, t);
21363 clip_tail = tail;
21364 }
21365
21366 /* Append glyph strings for glyphs following the last glyph
21367 string tail that overwrite tail. The foreground of such
21368 glyphs has to be drawn because it writes into the background
21369 of tail. The background must not be drawn because it could
21370 paint over the foreground of following glyphs. */
21371 i = right_overwriting (tail);
21372 if (i >= 0)
21373 {
21374 enum draw_glyphs_face overlap_hl;
21375 if (check_mouse_face
21376 && mouse_beg_col < i && mouse_end_col > end)
21377 overlap_hl = DRAW_MOUSE_FACE;
21378 else
21379 overlap_hl = DRAW_NORMAL_TEXT;
21380
21381 clip_tail = tail;
21382 i++; /* We must include the Ith glyph. */
21383 BUILD_GLYPH_STRINGS (end, i, h, t,
21384 overlap_hl, x, last_x);
21385 for (s = h; s; s = s->next)
21386 s->background_filled_p = 1;
21387 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21388 append_glyph_string_lists (&head, &tail, h, t);
21389 }
21390 if (clip_head || clip_tail)
21391 for (s = head; s; s = s->next)
21392 {
21393 s->clip_head = clip_head;
21394 s->clip_tail = clip_tail;
21395 }
21396 }
21397
21398 /* Draw all strings. */
21399 for (s = head; s; s = s->next)
21400 FRAME_RIF (f)->draw_glyph_string (s);
21401
21402 #ifndef HAVE_NS
21403 /* When focus a sole frame and move horizontally, this sets on_p to 0
21404 causing a failure to erase prev cursor position. */
21405 if (area == TEXT_AREA
21406 && !row->full_width_p
21407 /* When drawing overlapping rows, only the glyph strings'
21408 foreground is drawn, which doesn't erase a cursor
21409 completely. */
21410 && !overlaps)
21411 {
21412 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21413 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21414 : (tail ? tail->x + tail->background_width : x));
21415 x0 -= area_left;
21416 x1 -= area_left;
21417
21418 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21419 row->y, MATRIX_ROW_BOTTOM_Y (row));
21420 }
21421 #endif
21422
21423 /* Value is the x-position up to which drawn, relative to AREA of W.
21424 This doesn't include parts drawn because of overhangs. */
21425 if (row->full_width_p)
21426 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21427 else
21428 x_reached -= area_left;
21429
21430 RELEASE_HDC (hdc, f);
21431
21432 return x_reached;
21433 }
21434
21435 /* Expand row matrix if too narrow. Don't expand if area
21436 is not present. */
21437
21438 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21439 { \
21440 if (!fonts_changed_p \
21441 && (it->glyph_row->glyphs[area] \
21442 < it->glyph_row->glyphs[area + 1])) \
21443 { \
21444 it->w->ncols_scale_factor++; \
21445 fonts_changed_p = 1; \
21446 } \
21447 }
21448
21449 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21450 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21451
21452 static INLINE void
21453 append_glyph (struct it *it)
21454 {
21455 struct glyph *glyph;
21456 enum glyph_row_area area = it->area;
21457
21458 xassert (it->glyph_row);
21459 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21460
21461 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21462 if (glyph < it->glyph_row->glyphs[area + 1])
21463 {
21464 /* If the glyph row is reversed, we need to prepend the glyph
21465 rather than append it. */
21466 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21467 {
21468 struct glyph *g;
21469
21470 /* Make room for the additional glyph. */
21471 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21472 g[1] = *g;
21473 glyph = it->glyph_row->glyphs[area];
21474 }
21475 glyph->charpos = CHARPOS (it->position);
21476 glyph->object = it->object;
21477 if (it->pixel_width > 0)
21478 {
21479 glyph->pixel_width = it->pixel_width;
21480 glyph->padding_p = 0;
21481 }
21482 else
21483 {
21484 /* Assure at least 1-pixel width. Otherwise, cursor can't
21485 be displayed correctly. */
21486 glyph->pixel_width = 1;
21487 glyph->padding_p = 1;
21488 }
21489 glyph->ascent = it->ascent;
21490 glyph->descent = it->descent;
21491 glyph->voffset = it->voffset;
21492 glyph->type = CHAR_GLYPH;
21493 glyph->avoid_cursor_p = it->avoid_cursor_p;
21494 glyph->multibyte_p = it->multibyte_p;
21495 glyph->left_box_line_p = it->start_of_box_run_p;
21496 glyph->right_box_line_p = it->end_of_box_run_p;
21497 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21498 || it->phys_descent > it->descent);
21499 glyph->glyph_not_available_p = it->glyph_not_available_p;
21500 glyph->face_id = it->face_id;
21501 glyph->u.ch = it->char_to_display;
21502 glyph->slice = null_glyph_slice;
21503 glyph->font_type = FONT_TYPE_UNKNOWN;
21504 if (it->bidi_p)
21505 {
21506 glyph->resolved_level = it->bidi_it.resolved_level;
21507 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21508 abort ();
21509 glyph->bidi_type = it->bidi_it.type;
21510 }
21511 else
21512 {
21513 glyph->resolved_level = 0;
21514 glyph->bidi_type = UNKNOWN_BT;
21515 }
21516 ++it->glyph_row->used[area];
21517 }
21518 else
21519 IT_EXPAND_MATRIX_WIDTH (it, area);
21520 }
21521
21522 /* Store one glyph for the composition IT->cmp_it.id in
21523 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21524 non-null. */
21525
21526 static INLINE void
21527 append_composite_glyph (struct it *it)
21528 {
21529 struct glyph *glyph;
21530 enum glyph_row_area area = it->area;
21531
21532 xassert (it->glyph_row);
21533
21534 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21535 if (glyph < it->glyph_row->glyphs[area + 1])
21536 {
21537 /* If the glyph row is reversed, we need to prepend the glyph
21538 rather than append it. */
21539 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
21540 {
21541 struct glyph *g;
21542
21543 /* Make room for the new glyph. */
21544 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
21545 g[1] = *g;
21546 glyph = it->glyph_row->glyphs[it->area];
21547 }
21548 glyph->charpos = it->cmp_it.charpos;
21549 glyph->object = it->object;
21550 glyph->pixel_width = it->pixel_width;
21551 glyph->ascent = it->ascent;
21552 glyph->descent = it->descent;
21553 glyph->voffset = it->voffset;
21554 glyph->type = COMPOSITE_GLYPH;
21555 if (it->cmp_it.ch < 0)
21556 {
21557 glyph->u.cmp.automatic = 0;
21558 glyph->u.cmp.id = it->cmp_it.id;
21559 }
21560 else
21561 {
21562 glyph->u.cmp.automatic = 1;
21563 glyph->u.cmp.id = it->cmp_it.id;
21564 glyph->u.cmp.from = it->cmp_it.from;
21565 glyph->u.cmp.to = it->cmp_it.to - 1;
21566 }
21567 glyph->avoid_cursor_p = it->avoid_cursor_p;
21568 glyph->multibyte_p = it->multibyte_p;
21569 glyph->left_box_line_p = it->start_of_box_run_p;
21570 glyph->right_box_line_p = it->end_of_box_run_p;
21571 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21572 || it->phys_descent > it->descent);
21573 glyph->padding_p = 0;
21574 glyph->glyph_not_available_p = 0;
21575 glyph->face_id = it->face_id;
21576 glyph->slice = null_glyph_slice;
21577 glyph->font_type = FONT_TYPE_UNKNOWN;
21578 if (it->bidi_p)
21579 {
21580 glyph->resolved_level = it->bidi_it.resolved_level;
21581 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21582 abort ();
21583 glyph->bidi_type = it->bidi_it.type;
21584 }
21585 ++it->glyph_row->used[area];
21586 }
21587 else
21588 IT_EXPAND_MATRIX_WIDTH (it, area);
21589 }
21590
21591
21592 /* Change IT->ascent and IT->height according to the setting of
21593 IT->voffset. */
21594
21595 static INLINE void
21596 take_vertical_position_into_account (struct it *it)
21597 {
21598 if (it->voffset)
21599 {
21600 if (it->voffset < 0)
21601 /* Increase the ascent so that we can display the text higher
21602 in the line. */
21603 it->ascent -= it->voffset;
21604 else
21605 /* Increase the descent so that we can display the text lower
21606 in the line. */
21607 it->descent += it->voffset;
21608 }
21609 }
21610
21611
21612 /* Produce glyphs/get display metrics for the image IT is loaded with.
21613 See the description of struct display_iterator in dispextern.h for
21614 an overview of struct display_iterator. */
21615
21616 static void
21617 produce_image_glyph (struct it *it)
21618 {
21619 struct image *img;
21620 struct face *face;
21621 int glyph_ascent, crop;
21622 struct glyph_slice slice;
21623
21624 xassert (it->what == IT_IMAGE);
21625
21626 face = FACE_FROM_ID (it->f, it->face_id);
21627 xassert (face);
21628 /* Make sure X resources of the face is loaded. */
21629 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21630
21631 if (it->image_id < 0)
21632 {
21633 /* Fringe bitmap. */
21634 it->ascent = it->phys_ascent = 0;
21635 it->descent = it->phys_descent = 0;
21636 it->pixel_width = 0;
21637 it->nglyphs = 0;
21638 return;
21639 }
21640
21641 img = IMAGE_FROM_ID (it->f, it->image_id);
21642 xassert (img);
21643 /* Make sure X resources of the image is loaded. */
21644 prepare_image_for_display (it->f, img);
21645
21646 slice.x = slice.y = 0;
21647 slice.width = img->width;
21648 slice.height = img->height;
21649
21650 if (INTEGERP (it->slice.x))
21651 slice.x = XINT (it->slice.x);
21652 else if (FLOATP (it->slice.x))
21653 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21654
21655 if (INTEGERP (it->slice.y))
21656 slice.y = XINT (it->slice.y);
21657 else if (FLOATP (it->slice.y))
21658 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21659
21660 if (INTEGERP (it->slice.width))
21661 slice.width = XINT (it->slice.width);
21662 else if (FLOATP (it->slice.width))
21663 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21664
21665 if (INTEGERP (it->slice.height))
21666 slice.height = XINT (it->slice.height);
21667 else if (FLOATP (it->slice.height))
21668 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21669
21670 if (slice.x >= img->width)
21671 slice.x = img->width;
21672 if (slice.y >= img->height)
21673 slice.y = img->height;
21674 if (slice.x + slice.width >= img->width)
21675 slice.width = img->width - slice.x;
21676 if (slice.y + slice.height > img->height)
21677 slice.height = img->height - slice.y;
21678
21679 if (slice.width == 0 || slice.height == 0)
21680 return;
21681
21682 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21683
21684 it->descent = slice.height - glyph_ascent;
21685 if (slice.y == 0)
21686 it->descent += img->vmargin;
21687 if (slice.y + slice.height == img->height)
21688 it->descent += img->vmargin;
21689 it->phys_descent = it->descent;
21690
21691 it->pixel_width = slice.width;
21692 if (slice.x == 0)
21693 it->pixel_width += img->hmargin;
21694 if (slice.x + slice.width == img->width)
21695 it->pixel_width += img->hmargin;
21696
21697 /* It's quite possible for images to have an ascent greater than
21698 their height, so don't get confused in that case. */
21699 if (it->descent < 0)
21700 it->descent = 0;
21701
21702 it->nglyphs = 1;
21703
21704 if (face->box != FACE_NO_BOX)
21705 {
21706 if (face->box_line_width > 0)
21707 {
21708 if (slice.y == 0)
21709 it->ascent += face->box_line_width;
21710 if (slice.y + slice.height == img->height)
21711 it->descent += face->box_line_width;
21712 }
21713
21714 if (it->start_of_box_run_p && slice.x == 0)
21715 it->pixel_width += eabs (face->box_line_width);
21716 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21717 it->pixel_width += eabs (face->box_line_width);
21718 }
21719
21720 take_vertical_position_into_account (it);
21721
21722 /* Automatically crop wide image glyphs at right edge so we can
21723 draw the cursor on same display row. */
21724 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21725 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21726 {
21727 it->pixel_width -= crop;
21728 slice.width -= crop;
21729 }
21730
21731 if (it->glyph_row)
21732 {
21733 struct glyph *glyph;
21734 enum glyph_row_area area = it->area;
21735
21736 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21737 if (glyph < it->glyph_row->glyphs[area + 1])
21738 {
21739 glyph->charpos = CHARPOS (it->position);
21740 glyph->object = it->object;
21741 glyph->pixel_width = it->pixel_width;
21742 glyph->ascent = glyph_ascent;
21743 glyph->descent = it->descent;
21744 glyph->voffset = it->voffset;
21745 glyph->type = IMAGE_GLYPH;
21746 glyph->avoid_cursor_p = it->avoid_cursor_p;
21747 glyph->multibyte_p = it->multibyte_p;
21748 glyph->left_box_line_p = it->start_of_box_run_p;
21749 glyph->right_box_line_p = it->end_of_box_run_p;
21750 glyph->overlaps_vertically_p = 0;
21751 glyph->padding_p = 0;
21752 glyph->glyph_not_available_p = 0;
21753 glyph->face_id = it->face_id;
21754 glyph->u.img_id = img->id;
21755 glyph->slice = slice;
21756 glyph->font_type = FONT_TYPE_UNKNOWN;
21757 if (it->bidi_p)
21758 {
21759 glyph->resolved_level = it->bidi_it.resolved_level;
21760 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21761 abort ();
21762 glyph->bidi_type = it->bidi_it.type;
21763 }
21764 ++it->glyph_row->used[area];
21765 }
21766 else
21767 IT_EXPAND_MATRIX_WIDTH (it, area);
21768 }
21769 }
21770
21771
21772 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
21773 of the glyph, WIDTH and HEIGHT are the width and height of the
21774 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
21775
21776 static void
21777 append_stretch_glyph (struct it *it, Lisp_Object object,
21778 int width, int height, int ascent)
21779 {
21780 struct glyph *glyph;
21781 enum glyph_row_area area = it->area;
21782
21783 xassert (ascent >= 0 && ascent <= height);
21784
21785 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21786 if (glyph < it->glyph_row->glyphs[area + 1])
21787 {
21788 /* If the glyph row is reversed, we need to prepend the glyph
21789 rather than append it. */
21790 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21791 {
21792 struct glyph *g;
21793
21794 /* Make room for the additional glyph. */
21795 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21796 g[1] = *g;
21797 glyph = it->glyph_row->glyphs[area];
21798 }
21799 glyph->charpos = CHARPOS (it->position);
21800 glyph->object = object;
21801 glyph->pixel_width = width;
21802 glyph->ascent = ascent;
21803 glyph->descent = height - ascent;
21804 glyph->voffset = it->voffset;
21805 glyph->type = STRETCH_GLYPH;
21806 glyph->avoid_cursor_p = it->avoid_cursor_p;
21807 glyph->multibyte_p = it->multibyte_p;
21808 glyph->left_box_line_p = it->start_of_box_run_p;
21809 glyph->right_box_line_p = it->end_of_box_run_p;
21810 glyph->overlaps_vertically_p = 0;
21811 glyph->padding_p = 0;
21812 glyph->glyph_not_available_p = 0;
21813 glyph->face_id = it->face_id;
21814 glyph->u.stretch.ascent = ascent;
21815 glyph->u.stretch.height = height;
21816 glyph->slice = null_glyph_slice;
21817 glyph->font_type = FONT_TYPE_UNKNOWN;
21818 if (it->bidi_p)
21819 {
21820 glyph->resolved_level = it->bidi_it.resolved_level;
21821 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21822 abort ();
21823 glyph->bidi_type = it->bidi_it.type;
21824 }
21825 else
21826 {
21827 glyph->resolved_level = 0;
21828 glyph->bidi_type = UNKNOWN_BT;
21829 }
21830 ++it->glyph_row->used[area];
21831 }
21832 else
21833 IT_EXPAND_MATRIX_WIDTH (it, area);
21834 }
21835
21836
21837 /* Produce a stretch glyph for iterator IT. IT->object is the value
21838 of the glyph property displayed. The value must be a list
21839 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
21840 being recognized:
21841
21842 1. `:width WIDTH' specifies that the space should be WIDTH *
21843 canonical char width wide. WIDTH may be an integer or floating
21844 point number.
21845
21846 2. `:relative-width FACTOR' specifies that the width of the stretch
21847 should be computed from the width of the first character having the
21848 `glyph' property, and should be FACTOR times that width.
21849
21850 3. `:align-to HPOS' specifies that the space should be wide enough
21851 to reach HPOS, a value in canonical character units.
21852
21853 Exactly one of the above pairs must be present.
21854
21855 4. `:height HEIGHT' specifies that the height of the stretch produced
21856 should be HEIGHT, measured in canonical character units.
21857
21858 5. `:relative-height FACTOR' specifies that the height of the
21859 stretch should be FACTOR times the height of the characters having
21860 the glyph property.
21861
21862 Either none or exactly one of 4 or 5 must be present.
21863
21864 6. `:ascent ASCENT' specifies that ASCENT percent of the height
21865 of the stretch should be used for the ascent of the stretch.
21866 ASCENT must be in the range 0 <= ASCENT <= 100. */
21867
21868 static void
21869 produce_stretch_glyph (struct it *it)
21870 {
21871 /* (space :width WIDTH :height HEIGHT ...) */
21872 Lisp_Object prop, plist;
21873 int width = 0, height = 0, align_to = -1;
21874 int zero_width_ok_p = 0, zero_height_ok_p = 0;
21875 int ascent = 0;
21876 double tem;
21877 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21878 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
21879
21880 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21881
21882 /* List should start with `space'. */
21883 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
21884 plist = XCDR (it->object);
21885
21886 /* Compute the width of the stretch. */
21887 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
21888 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
21889 {
21890 /* Absolute width `:width WIDTH' specified and valid. */
21891 zero_width_ok_p = 1;
21892 width = (int)tem;
21893 }
21894 else if (prop = Fplist_get (plist, QCrelative_width),
21895 NUMVAL (prop) > 0)
21896 {
21897 /* Relative width `:relative-width FACTOR' specified and valid.
21898 Compute the width of the characters having the `glyph'
21899 property. */
21900 struct it it2;
21901 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
21902
21903 it2 = *it;
21904 if (it->multibyte_p)
21905 {
21906 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
21907 - IT_BYTEPOS (*it));
21908 it2.c = STRING_CHAR_AND_LENGTH (p, it2.len);
21909 }
21910 else
21911 it2.c = *p, it2.len = 1;
21912
21913 it2.glyph_row = NULL;
21914 it2.what = IT_CHARACTER;
21915 x_produce_glyphs (&it2);
21916 width = NUMVAL (prop) * it2.pixel_width;
21917 }
21918 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
21919 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
21920 {
21921 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
21922 align_to = (align_to < 0
21923 ? 0
21924 : align_to - window_box_left_offset (it->w, TEXT_AREA));
21925 else if (align_to < 0)
21926 align_to = window_box_left_offset (it->w, TEXT_AREA);
21927 width = max (0, (int)tem + align_to - it->current_x);
21928 zero_width_ok_p = 1;
21929 }
21930 else
21931 /* Nothing specified -> width defaults to canonical char width. */
21932 width = FRAME_COLUMN_WIDTH (it->f);
21933
21934 if (width <= 0 && (width < 0 || !zero_width_ok_p))
21935 width = 1;
21936
21937 /* Compute height. */
21938 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
21939 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21940 {
21941 height = (int)tem;
21942 zero_height_ok_p = 1;
21943 }
21944 else if (prop = Fplist_get (plist, QCrelative_height),
21945 NUMVAL (prop) > 0)
21946 height = FONT_HEIGHT (font) * NUMVAL (prop);
21947 else
21948 height = FONT_HEIGHT (font);
21949
21950 if (height <= 0 && (height < 0 || !zero_height_ok_p))
21951 height = 1;
21952
21953 /* Compute percentage of height used for ascent. If
21954 `:ascent ASCENT' is present and valid, use that. Otherwise,
21955 derive the ascent from the font in use. */
21956 if (prop = Fplist_get (plist, QCascent),
21957 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
21958 ascent = height * NUMVAL (prop) / 100.0;
21959 else if (!NILP (prop)
21960 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21961 ascent = min (max (0, (int)tem), height);
21962 else
21963 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
21964
21965 if (width > 0 && it->line_wrap != TRUNCATE
21966 && it->current_x + width > it->last_visible_x)
21967 width = it->last_visible_x - it->current_x - 1;
21968
21969 if (width > 0 && height > 0 && it->glyph_row)
21970 {
21971 Lisp_Object object = it->stack[it->sp - 1].string;
21972 if (!STRINGP (object))
21973 object = it->w->buffer;
21974 append_stretch_glyph (it, object, width, height, ascent);
21975 }
21976
21977 it->pixel_width = width;
21978 it->ascent = it->phys_ascent = ascent;
21979 it->descent = it->phys_descent = height - it->ascent;
21980 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21981
21982 take_vertical_position_into_account (it);
21983 }
21984
21985 /* Calculate line-height and line-spacing properties.
21986 An integer value specifies explicit pixel value.
21987 A float value specifies relative value to current face height.
21988 A cons (float . face-name) specifies relative value to
21989 height of specified face font.
21990
21991 Returns height in pixels, or nil. */
21992
21993
21994 static Lisp_Object
21995 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
21996 int boff, int override)
21997 {
21998 Lisp_Object face_name = Qnil;
21999 int ascent, descent, height;
22000
22001 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22002 return val;
22003
22004 if (CONSP (val))
22005 {
22006 face_name = XCAR (val);
22007 val = XCDR (val);
22008 if (!NUMBERP (val))
22009 val = make_number (1);
22010 if (NILP (face_name))
22011 {
22012 height = it->ascent + it->descent;
22013 goto scale;
22014 }
22015 }
22016
22017 if (NILP (face_name))
22018 {
22019 font = FRAME_FONT (it->f);
22020 boff = FRAME_BASELINE_OFFSET (it->f);
22021 }
22022 else if (EQ (face_name, Qt))
22023 {
22024 override = 0;
22025 }
22026 else
22027 {
22028 int face_id;
22029 struct face *face;
22030
22031 face_id = lookup_named_face (it->f, face_name, 0);
22032 if (face_id < 0)
22033 return make_number (-1);
22034
22035 face = FACE_FROM_ID (it->f, face_id);
22036 font = face->font;
22037 if (font == NULL)
22038 return make_number (-1);
22039 boff = font->baseline_offset;
22040 if (font->vertical_centering)
22041 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22042 }
22043
22044 ascent = FONT_BASE (font) + boff;
22045 descent = FONT_DESCENT (font) - boff;
22046
22047 if (override)
22048 {
22049 it->override_ascent = ascent;
22050 it->override_descent = descent;
22051 it->override_boff = boff;
22052 }
22053
22054 height = ascent + descent;
22055
22056 scale:
22057 if (FLOATP (val))
22058 height = (int)(XFLOAT_DATA (val) * height);
22059 else if (INTEGERP (val))
22060 height *= XINT (val);
22061
22062 return make_number (height);
22063 }
22064
22065
22066 /* RIF:
22067 Produce glyphs/get display metrics for the display element IT is
22068 loaded with. See the description of struct it in dispextern.h
22069 for an overview of struct it. */
22070
22071 void
22072 x_produce_glyphs (struct it *it)
22073 {
22074 int extra_line_spacing = it->extra_line_spacing;
22075
22076 it->glyph_not_available_p = 0;
22077
22078 if (it->what == IT_CHARACTER)
22079 {
22080 XChar2b char2b;
22081 struct font *font;
22082 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22083 struct font_metrics *pcm;
22084 int font_not_found_p;
22085 int boff; /* baseline offset */
22086 /* We may change it->multibyte_p upon unibyte<->multibyte
22087 conversion. So, save the current value now and restore it
22088 later.
22089
22090 Note: It seems that we don't have to record multibyte_p in
22091 struct glyph because the character code itself tells whether
22092 or not the character is multibyte. Thus, in the future, we
22093 must consider eliminating the field `multibyte_p' in the
22094 struct glyph. */
22095 int saved_multibyte_p = it->multibyte_p;
22096
22097 /* Maybe translate single-byte characters to multibyte, or the
22098 other way. */
22099 it->char_to_display = it->c;
22100 if (!ASCII_BYTE_P (it->c)
22101 && ! it->multibyte_p)
22102 {
22103 if (SINGLE_BYTE_CHAR_P (it->c)
22104 && unibyte_display_via_language_environment)
22105 {
22106 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
22107
22108 /* get_next_display_element assures that this decoding
22109 never fails. */
22110 it->char_to_display = DECODE_CHAR (unibyte, it->c);
22111 it->multibyte_p = 1;
22112 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
22113 -1, Qnil);
22114 face = FACE_FROM_ID (it->f, it->face_id);
22115 }
22116 }
22117
22118 /* Get font to use. Encode IT->char_to_display. */
22119 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
22120 &char2b, it->multibyte_p, 0);
22121 font = face->font;
22122
22123 font_not_found_p = font == NULL;
22124 if (font_not_found_p)
22125 {
22126 /* When no suitable font found, display an empty box based
22127 on the metrics of the font of the default face (or what
22128 remapped). */
22129 struct face *no_font_face
22130 = FACE_FROM_ID (it->f,
22131 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
22132 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
22133 font = no_font_face->font;
22134 boff = font->baseline_offset;
22135 }
22136 else
22137 {
22138 boff = font->baseline_offset;
22139 if (font->vertical_centering)
22140 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22141 }
22142
22143 if (it->char_to_display >= ' '
22144 && (!it->multibyte_p || it->char_to_display < 128))
22145 {
22146 /* Either unibyte or ASCII. */
22147 int stretched_p;
22148
22149 it->nglyphs = 1;
22150
22151 pcm = get_per_char_metric (it->f, font, &char2b);
22152
22153 if (it->override_ascent >= 0)
22154 {
22155 it->ascent = it->override_ascent;
22156 it->descent = it->override_descent;
22157 boff = it->override_boff;
22158 }
22159 else
22160 {
22161 it->ascent = FONT_BASE (font) + boff;
22162 it->descent = FONT_DESCENT (font) - boff;
22163 }
22164
22165 if (pcm)
22166 {
22167 it->phys_ascent = pcm->ascent + boff;
22168 it->phys_descent = pcm->descent - boff;
22169 it->pixel_width = pcm->width;
22170 }
22171 else
22172 {
22173 it->glyph_not_available_p = 1;
22174 it->phys_ascent = it->ascent;
22175 it->phys_descent = it->descent;
22176 it->pixel_width = FONT_WIDTH (font);
22177 }
22178
22179 if (it->constrain_row_ascent_descent_p)
22180 {
22181 if (it->descent > it->max_descent)
22182 {
22183 it->ascent += it->descent - it->max_descent;
22184 it->descent = it->max_descent;
22185 }
22186 if (it->ascent > it->max_ascent)
22187 {
22188 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22189 it->ascent = it->max_ascent;
22190 }
22191 it->phys_ascent = min (it->phys_ascent, it->ascent);
22192 it->phys_descent = min (it->phys_descent, it->descent);
22193 extra_line_spacing = 0;
22194 }
22195
22196 /* If this is a space inside a region of text with
22197 `space-width' property, change its width. */
22198 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22199 if (stretched_p)
22200 it->pixel_width *= XFLOATINT (it->space_width);
22201
22202 /* If face has a box, add the box thickness to the character
22203 height. If character has a box line to the left and/or
22204 right, add the box line width to the character's width. */
22205 if (face->box != FACE_NO_BOX)
22206 {
22207 int thick = face->box_line_width;
22208
22209 if (thick > 0)
22210 {
22211 it->ascent += thick;
22212 it->descent += thick;
22213 }
22214 else
22215 thick = -thick;
22216
22217 if (it->start_of_box_run_p)
22218 it->pixel_width += thick;
22219 if (it->end_of_box_run_p)
22220 it->pixel_width += thick;
22221 }
22222
22223 /* If face has an overline, add the height of the overline
22224 (1 pixel) and a 1 pixel margin to the character height. */
22225 if (face->overline_p)
22226 it->ascent += overline_margin;
22227
22228 if (it->constrain_row_ascent_descent_p)
22229 {
22230 if (it->ascent > it->max_ascent)
22231 it->ascent = it->max_ascent;
22232 if (it->descent > it->max_descent)
22233 it->descent = it->max_descent;
22234 }
22235
22236 take_vertical_position_into_account (it);
22237
22238 /* If we have to actually produce glyphs, do it. */
22239 if (it->glyph_row)
22240 {
22241 if (stretched_p)
22242 {
22243 /* Translate a space with a `space-width' property
22244 into a stretch glyph. */
22245 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22246 / FONT_HEIGHT (font));
22247 append_stretch_glyph (it, it->object, it->pixel_width,
22248 it->ascent + it->descent, ascent);
22249 }
22250 else
22251 append_glyph (it);
22252
22253 /* If characters with lbearing or rbearing are displayed
22254 in this line, record that fact in a flag of the
22255 glyph row. This is used to optimize X output code. */
22256 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22257 it->glyph_row->contains_overlapping_glyphs_p = 1;
22258 }
22259 if (! stretched_p && it->pixel_width == 0)
22260 /* We assure that all visible glyphs have at least 1-pixel
22261 width. */
22262 it->pixel_width = 1;
22263 }
22264 else if (it->char_to_display == '\n')
22265 {
22266 /* A newline has no width, but we need the height of the
22267 line. But if previous part of the line sets a height,
22268 don't increase that height */
22269
22270 Lisp_Object height;
22271 Lisp_Object total_height = Qnil;
22272
22273 it->override_ascent = -1;
22274 it->pixel_width = 0;
22275 it->nglyphs = 0;
22276
22277 height = get_it_property (it, Qline_height);
22278 /* Split (line-height total-height) list */
22279 if (CONSP (height)
22280 && CONSP (XCDR (height))
22281 && NILP (XCDR (XCDR (height))))
22282 {
22283 total_height = XCAR (XCDR (height));
22284 height = XCAR (height);
22285 }
22286 height = calc_line_height_property (it, height, font, boff, 1);
22287
22288 if (it->override_ascent >= 0)
22289 {
22290 it->ascent = it->override_ascent;
22291 it->descent = it->override_descent;
22292 boff = it->override_boff;
22293 }
22294 else
22295 {
22296 it->ascent = FONT_BASE (font) + boff;
22297 it->descent = FONT_DESCENT (font) - boff;
22298 }
22299
22300 if (EQ (height, Qt))
22301 {
22302 if (it->descent > it->max_descent)
22303 {
22304 it->ascent += it->descent - it->max_descent;
22305 it->descent = it->max_descent;
22306 }
22307 if (it->ascent > it->max_ascent)
22308 {
22309 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22310 it->ascent = it->max_ascent;
22311 }
22312 it->phys_ascent = min (it->phys_ascent, it->ascent);
22313 it->phys_descent = min (it->phys_descent, it->descent);
22314 it->constrain_row_ascent_descent_p = 1;
22315 extra_line_spacing = 0;
22316 }
22317 else
22318 {
22319 Lisp_Object spacing;
22320
22321 it->phys_ascent = it->ascent;
22322 it->phys_descent = it->descent;
22323
22324 if ((it->max_ascent > 0 || it->max_descent > 0)
22325 && face->box != FACE_NO_BOX
22326 && face->box_line_width > 0)
22327 {
22328 it->ascent += face->box_line_width;
22329 it->descent += face->box_line_width;
22330 }
22331 if (!NILP (height)
22332 && XINT (height) > it->ascent + it->descent)
22333 it->ascent = XINT (height) - it->descent;
22334
22335 if (!NILP (total_height))
22336 spacing = calc_line_height_property (it, total_height, font, boff, 0);
22337 else
22338 {
22339 spacing = get_it_property (it, Qline_spacing);
22340 spacing = calc_line_height_property (it, spacing, font, boff, 0);
22341 }
22342 if (INTEGERP (spacing))
22343 {
22344 extra_line_spacing = XINT (spacing);
22345 if (!NILP (total_height))
22346 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22347 }
22348 }
22349 }
22350 else if (it->char_to_display == '\t')
22351 {
22352 if (font->space_width > 0)
22353 {
22354 int tab_width = it->tab_width * font->space_width;
22355 int x = it->current_x + it->continuation_lines_width;
22356 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22357
22358 /* If the distance from the current position to the next tab
22359 stop is less than a space character width, use the
22360 tab stop after that. */
22361 if (next_tab_x - x < font->space_width)
22362 next_tab_x += tab_width;
22363
22364 it->pixel_width = next_tab_x - x;
22365 it->nglyphs = 1;
22366 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22367 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22368
22369 if (it->glyph_row)
22370 {
22371 append_stretch_glyph (it, it->object, it->pixel_width,
22372 it->ascent + it->descent, it->ascent);
22373 }
22374 }
22375 else
22376 {
22377 it->pixel_width = 0;
22378 it->nglyphs = 1;
22379 }
22380 }
22381 else
22382 {
22383 /* A multi-byte character. Assume that the display width of the
22384 character is the width of the character multiplied by the
22385 width of the font. */
22386
22387 /* If we found a font, this font should give us the right
22388 metrics. If we didn't find a font, use the frame's
22389 default font and calculate the width of the character by
22390 multiplying the width of font by the width of the
22391 character. */
22392
22393 pcm = get_per_char_metric (it->f, font, &char2b);
22394
22395 if (font_not_found_p || !pcm)
22396 {
22397 int char_width = CHAR_WIDTH (it->char_to_display);
22398
22399 if (char_width == 0)
22400 /* This is a non spacing character. But, as we are
22401 going to display an empty box, the box must occupy
22402 at least one column. */
22403 char_width = 1;
22404 it->glyph_not_available_p = 1;
22405 it->pixel_width = font->space_width * char_width;
22406 it->phys_ascent = FONT_BASE (font) + boff;
22407 it->phys_descent = FONT_DESCENT (font) - boff;
22408 }
22409 else
22410 {
22411 it->pixel_width = pcm->width;
22412 it->phys_ascent = pcm->ascent + boff;
22413 it->phys_descent = pcm->descent - boff;
22414 if (it->glyph_row
22415 && (pcm->lbearing < 0
22416 || pcm->rbearing > pcm->width))
22417 it->glyph_row->contains_overlapping_glyphs_p = 1;
22418 }
22419 it->nglyphs = 1;
22420 it->ascent = FONT_BASE (font) + boff;
22421 it->descent = FONT_DESCENT (font) - boff;
22422 if (face->box != FACE_NO_BOX)
22423 {
22424 int thick = face->box_line_width;
22425
22426 if (thick > 0)
22427 {
22428 it->ascent += thick;
22429 it->descent += thick;
22430 }
22431 else
22432 thick = - thick;
22433
22434 if (it->start_of_box_run_p)
22435 it->pixel_width += thick;
22436 if (it->end_of_box_run_p)
22437 it->pixel_width += thick;
22438 }
22439
22440 /* If face has an overline, add the height of the overline
22441 (1 pixel) and a 1 pixel margin to the character height. */
22442 if (face->overline_p)
22443 it->ascent += overline_margin;
22444
22445 take_vertical_position_into_account (it);
22446
22447 if (it->ascent < 0)
22448 it->ascent = 0;
22449 if (it->descent < 0)
22450 it->descent = 0;
22451
22452 if (it->glyph_row)
22453 append_glyph (it);
22454 if (it->pixel_width == 0)
22455 /* We assure that all visible glyphs have at least 1-pixel
22456 width. */
22457 it->pixel_width = 1;
22458 }
22459 it->multibyte_p = saved_multibyte_p;
22460 }
22461 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22462 {
22463 /* A static composition.
22464
22465 Note: A composition is represented as one glyph in the
22466 glyph matrix. There are no padding glyphs.
22467
22468 Important note: pixel_width, ascent, and descent are the
22469 values of what is drawn by draw_glyphs (i.e. the values of
22470 the overall glyphs composed). */
22471 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22472 int boff; /* baseline offset */
22473 struct composition *cmp = composition_table[it->cmp_it.id];
22474 int glyph_len = cmp->glyph_len;
22475 struct font *font = face->font;
22476
22477 it->nglyphs = 1;
22478
22479 /* If we have not yet calculated pixel size data of glyphs of
22480 the composition for the current face font, calculate them
22481 now. Theoretically, we have to check all fonts for the
22482 glyphs, but that requires much time and memory space. So,
22483 here we check only the font of the first glyph. This may
22484 lead to incorrect display, but it's very rare, and C-l
22485 (recenter-top-bottom) can correct the display anyway. */
22486 if (! cmp->font || cmp->font != font)
22487 {
22488 /* Ascent and descent of the font of the first character
22489 of this composition (adjusted by baseline offset).
22490 Ascent and descent of overall glyphs should not be less
22491 than these, respectively. */
22492 int font_ascent, font_descent, font_height;
22493 /* Bounding box of the overall glyphs. */
22494 int leftmost, rightmost, lowest, highest;
22495 int lbearing, rbearing;
22496 int i, width, ascent, descent;
22497 int left_padded = 0, right_padded = 0;
22498 int c;
22499 XChar2b char2b;
22500 struct font_metrics *pcm;
22501 int font_not_found_p;
22502 int pos;
22503
22504 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22505 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22506 break;
22507 if (glyph_len < cmp->glyph_len)
22508 right_padded = 1;
22509 for (i = 0; i < glyph_len; i++)
22510 {
22511 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22512 break;
22513 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22514 }
22515 if (i > 0)
22516 left_padded = 1;
22517
22518 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22519 : IT_CHARPOS (*it));
22520 /* If no suitable font is found, use the default font. */
22521 font_not_found_p = font == NULL;
22522 if (font_not_found_p)
22523 {
22524 face = face->ascii_face;
22525 font = face->font;
22526 }
22527 boff = font->baseline_offset;
22528 if (font->vertical_centering)
22529 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22530 font_ascent = FONT_BASE (font) + boff;
22531 font_descent = FONT_DESCENT (font) - boff;
22532 font_height = FONT_HEIGHT (font);
22533
22534 cmp->font = (void *) font;
22535
22536 pcm = NULL;
22537 if (! font_not_found_p)
22538 {
22539 get_char_face_and_encoding (it->f, c, it->face_id,
22540 &char2b, it->multibyte_p, 0);
22541 pcm = get_per_char_metric (it->f, font, &char2b);
22542 }
22543
22544 /* Initialize the bounding box. */
22545 if (pcm)
22546 {
22547 width = pcm->width;
22548 ascent = pcm->ascent;
22549 descent = pcm->descent;
22550 lbearing = pcm->lbearing;
22551 rbearing = pcm->rbearing;
22552 }
22553 else
22554 {
22555 width = FONT_WIDTH (font);
22556 ascent = FONT_BASE (font);
22557 descent = FONT_DESCENT (font);
22558 lbearing = 0;
22559 rbearing = width;
22560 }
22561
22562 rightmost = width;
22563 leftmost = 0;
22564 lowest = - descent + boff;
22565 highest = ascent + boff;
22566
22567 if (! font_not_found_p
22568 && font->default_ascent
22569 && CHAR_TABLE_P (Vuse_default_ascent)
22570 && !NILP (Faref (Vuse_default_ascent,
22571 make_number (it->char_to_display))))
22572 highest = font->default_ascent + boff;
22573
22574 /* Draw the first glyph at the normal position. It may be
22575 shifted to right later if some other glyphs are drawn
22576 at the left. */
22577 cmp->offsets[i * 2] = 0;
22578 cmp->offsets[i * 2 + 1] = boff;
22579 cmp->lbearing = lbearing;
22580 cmp->rbearing = rbearing;
22581
22582 /* Set cmp->offsets for the remaining glyphs. */
22583 for (i++; i < glyph_len; i++)
22584 {
22585 int left, right, btm, top;
22586 int ch = COMPOSITION_GLYPH (cmp, i);
22587 int face_id;
22588 struct face *this_face;
22589 int this_boff;
22590
22591 if (ch == '\t')
22592 ch = ' ';
22593 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22594 this_face = FACE_FROM_ID (it->f, face_id);
22595 font = this_face->font;
22596
22597 if (font == NULL)
22598 pcm = NULL;
22599 else
22600 {
22601 this_boff = font->baseline_offset;
22602 if (font->vertical_centering)
22603 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22604 get_char_face_and_encoding (it->f, ch, face_id,
22605 &char2b, it->multibyte_p, 0);
22606 pcm = get_per_char_metric (it->f, font, &char2b);
22607 }
22608 if (! pcm)
22609 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22610 else
22611 {
22612 width = pcm->width;
22613 ascent = pcm->ascent;
22614 descent = pcm->descent;
22615 lbearing = pcm->lbearing;
22616 rbearing = pcm->rbearing;
22617 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22618 {
22619 /* Relative composition with or without
22620 alternate chars. */
22621 left = (leftmost + rightmost - width) / 2;
22622 btm = - descent + boff;
22623 if (font->relative_compose
22624 && (! CHAR_TABLE_P (Vignore_relative_composition)
22625 || NILP (Faref (Vignore_relative_composition,
22626 make_number (ch)))))
22627 {
22628
22629 if (- descent >= font->relative_compose)
22630 /* One extra pixel between two glyphs. */
22631 btm = highest + 1;
22632 else if (ascent <= 0)
22633 /* One extra pixel between two glyphs. */
22634 btm = lowest - 1 - ascent - descent;
22635 }
22636 }
22637 else
22638 {
22639 /* A composition rule is specified by an integer
22640 value that encodes global and new reference
22641 points (GREF and NREF). GREF and NREF are
22642 specified by numbers as below:
22643
22644 0---1---2 -- ascent
22645 | |
22646 | |
22647 | |
22648 9--10--11 -- center
22649 | |
22650 ---3---4---5--- baseline
22651 | |
22652 6---7---8 -- descent
22653 */
22654 int rule = COMPOSITION_RULE (cmp, i);
22655 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22656
22657 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22658 grefx = gref % 3, nrefx = nref % 3;
22659 grefy = gref / 3, nrefy = nref / 3;
22660 if (xoff)
22661 xoff = font_height * (xoff - 128) / 256;
22662 if (yoff)
22663 yoff = font_height * (yoff - 128) / 256;
22664
22665 left = (leftmost
22666 + grefx * (rightmost - leftmost) / 2
22667 - nrefx * width / 2
22668 + xoff);
22669
22670 btm = ((grefy == 0 ? highest
22671 : grefy == 1 ? 0
22672 : grefy == 2 ? lowest
22673 : (highest + lowest) / 2)
22674 - (nrefy == 0 ? ascent + descent
22675 : nrefy == 1 ? descent - boff
22676 : nrefy == 2 ? 0
22677 : (ascent + descent) / 2)
22678 + yoff);
22679 }
22680
22681 cmp->offsets[i * 2] = left;
22682 cmp->offsets[i * 2 + 1] = btm + descent;
22683
22684 /* Update the bounding box of the overall glyphs. */
22685 if (width > 0)
22686 {
22687 right = left + width;
22688 if (left < leftmost)
22689 leftmost = left;
22690 if (right > rightmost)
22691 rightmost = right;
22692 }
22693 top = btm + descent + ascent;
22694 if (top > highest)
22695 highest = top;
22696 if (btm < lowest)
22697 lowest = btm;
22698
22699 if (cmp->lbearing > left + lbearing)
22700 cmp->lbearing = left + lbearing;
22701 if (cmp->rbearing < left + rbearing)
22702 cmp->rbearing = left + rbearing;
22703 }
22704 }
22705
22706 /* If there are glyphs whose x-offsets are negative,
22707 shift all glyphs to the right and make all x-offsets
22708 non-negative. */
22709 if (leftmost < 0)
22710 {
22711 for (i = 0; i < cmp->glyph_len; i++)
22712 cmp->offsets[i * 2] -= leftmost;
22713 rightmost -= leftmost;
22714 cmp->lbearing -= leftmost;
22715 cmp->rbearing -= leftmost;
22716 }
22717
22718 if (left_padded && cmp->lbearing < 0)
22719 {
22720 for (i = 0; i < cmp->glyph_len; i++)
22721 cmp->offsets[i * 2] -= cmp->lbearing;
22722 rightmost -= cmp->lbearing;
22723 cmp->rbearing -= cmp->lbearing;
22724 cmp->lbearing = 0;
22725 }
22726 if (right_padded && rightmost < cmp->rbearing)
22727 {
22728 rightmost = cmp->rbearing;
22729 }
22730
22731 cmp->pixel_width = rightmost;
22732 cmp->ascent = highest;
22733 cmp->descent = - lowest;
22734 if (cmp->ascent < font_ascent)
22735 cmp->ascent = font_ascent;
22736 if (cmp->descent < font_descent)
22737 cmp->descent = font_descent;
22738 }
22739
22740 if (it->glyph_row
22741 && (cmp->lbearing < 0
22742 || cmp->rbearing > cmp->pixel_width))
22743 it->glyph_row->contains_overlapping_glyphs_p = 1;
22744
22745 it->pixel_width = cmp->pixel_width;
22746 it->ascent = it->phys_ascent = cmp->ascent;
22747 it->descent = it->phys_descent = cmp->descent;
22748 if (face->box != FACE_NO_BOX)
22749 {
22750 int thick = face->box_line_width;
22751
22752 if (thick > 0)
22753 {
22754 it->ascent += thick;
22755 it->descent += thick;
22756 }
22757 else
22758 thick = - thick;
22759
22760 if (it->start_of_box_run_p)
22761 it->pixel_width += thick;
22762 if (it->end_of_box_run_p)
22763 it->pixel_width += thick;
22764 }
22765
22766 /* If face has an overline, add the height of the overline
22767 (1 pixel) and a 1 pixel margin to the character height. */
22768 if (face->overline_p)
22769 it->ascent += overline_margin;
22770
22771 take_vertical_position_into_account (it);
22772 if (it->ascent < 0)
22773 it->ascent = 0;
22774 if (it->descent < 0)
22775 it->descent = 0;
22776
22777 if (it->glyph_row)
22778 append_composite_glyph (it);
22779 }
22780 else if (it->what == IT_COMPOSITION)
22781 {
22782 /* A dynamic (automatic) composition. */
22783 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22784 Lisp_Object gstring;
22785 struct font_metrics metrics;
22786
22787 gstring = composition_gstring_from_id (it->cmp_it.id);
22788 it->pixel_width
22789 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
22790 &metrics);
22791 if (it->glyph_row
22792 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
22793 it->glyph_row->contains_overlapping_glyphs_p = 1;
22794 it->ascent = it->phys_ascent = metrics.ascent;
22795 it->descent = it->phys_descent = metrics.descent;
22796 if (face->box != FACE_NO_BOX)
22797 {
22798 int thick = face->box_line_width;
22799
22800 if (thick > 0)
22801 {
22802 it->ascent += thick;
22803 it->descent += thick;
22804 }
22805 else
22806 thick = - thick;
22807
22808 if (it->start_of_box_run_p)
22809 it->pixel_width += thick;
22810 if (it->end_of_box_run_p)
22811 it->pixel_width += thick;
22812 }
22813 /* If face has an overline, add the height of the overline
22814 (1 pixel) and a 1 pixel margin to the character height. */
22815 if (face->overline_p)
22816 it->ascent += overline_margin;
22817 take_vertical_position_into_account (it);
22818 if (it->ascent < 0)
22819 it->ascent = 0;
22820 if (it->descent < 0)
22821 it->descent = 0;
22822
22823 if (it->glyph_row)
22824 append_composite_glyph (it);
22825 }
22826 else if (it->what == IT_IMAGE)
22827 produce_image_glyph (it);
22828 else if (it->what == IT_STRETCH)
22829 produce_stretch_glyph (it);
22830
22831 /* Accumulate dimensions. Note: can't assume that it->descent > 0
22832 because this isn't true for images with `:ascent 100'. */
22833 xassert (it->ascent >= 0 && it->descent >= 0);
22834 if (it->area == TEXT_AREA)
22835 it->current_x += it->pixel_width;
22836
22837 if (extra_line_spacing > 0)
22838 {
22839 it->descent += extra_line_spacing;
22840 if (extra_line_spacing > it->max_extra_line_spacing)
22841 it->max_extra_line_spacing = extra_line_spacing;
22842 }
22843
22844 it->max_ascent = max (it->max_ascent, it->ascent);
22845 it->max_descent = max (it->max_descent, it->descent);
22846 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
22847 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
22848 }
22849
22850 /* EXPORT for RIF:
22851 Output LEN glyphs starting at START at the nominal cursor position.
22852 Advance the nominal cursor over the text. The global variable
22853 updated_window contains the window being updated, updated_row is
22854 the glyph row being updated, and updated_area is the area of that
22855 row being updated. */
22856
22857 void
22858 x_write_glyphs (struct glyph *start, int len)
22859 {
22860 int x, hpos;
22861
22862 xassert (updated_window && updated_row);
22863 BLOCK_INPUT;
22864
22865 /* Write glyphs. */
22866
22867 hpos = start - updated_row->glyphs[updated_area];
22868 x = draw_glyphs (updated_window, output_cursor.x,
22869 updated_row, updated_area,
22870 hpos, hpos + len,
22871 DRAW_NORMAL_TEXT, 0);
22872
22873 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
22874 if (updated_area == TEXT_AREA
22875 && updated_window->phys_cursor_on_p
22876 && updated_window->phys_cursor.vpos == output_cursor.vpos
22877 && updated_window->phys_cursor.hpos >= hpos
22878 && updated_window->phys_cursor.hpos < hpos + len)
22879 updated_window->phys_cursor_on_p = 0;
22880
22881 UNBLOCK_INPUT;
22882
22883 /* Advance the output cursor. */
22884 output_cursor.hpos += len;
22885 output_cursor.x = x;
22886 }
22887
22888
22889 /* EXPORT for RIF:
22890 Insert LEN glyphs from START at the nominal cursor position. */
22891
22892 void
22893 x_insert_glyphs (struct glyph *start, int len)
22894 {
22895 struct frame *f;
22896 struct window *w;
22897 int line_height, shift_by_width, shifted_region_width;
22898 struct glyph_row *row;
22899 struct glyph *glyph;
22900 int frame_x, frame_y;
22901 EMACS_INT hpos;
22902
22903 xassert (updated_window && updated_row);
22904 BLOCK_INPUT;
22905 w = updated_window;
22906 f = XFRAME (WINDOW_FRAME (w));
22907
22908 /* Get the height of the line we are in. */
22909 row = updated_row;
22910 line_height = row->height;
22911
22912 /* Get the width of the glyphs to insert. */
22913 shift_by_width = 0;
22914 for (glyph = start; glyph < start + len; ++glyph)
22915 shift_by_width += glyph->pixel_width;
22916
22917 /* Get the width of the region to shift right. */
22918 shifted_region_width = (window_box_width (w, updated_area)
22919 - output_cursor.x
22920 - shift_by_width);
22921
22922 /* Shift right. */
22923 frame_x = window_box_left (w, updated_area) + output_cursor.x;
22924 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
22925
22926 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
22927 line_height, shift_by_width);
22928
22929 /* Write the glyphs. */
22930 hpos = start - row->glyphs[updated_area];
22931 draw_glyphs (w, output_cursor.x, row, updated_area,
22932 hpos, hpos + len,
22933 DRAW_NORMAL_TEXT, 0);
22934
22935 /* Advance the output cursor. */
22936 output_cursor.hpos += len;
22937 output_cursor.x += shift_by_width;
22938 UNBLOCK_INPUT;
22939 }
22940
22941
22942 /* EXPORT for RIF:
22943 Erase the current text line from the nominal cursor position
22944 (inclusive) to pixel column TO_X (exclusive). The idea is that
22945 everything from TO_X onward is already erased.
22946
22947 TO_X is a pixel position relative to updated_area of
22948 updated_window. TO_X == -1 means clear to the end of this area. */
22949
22950 void
22951 x_clear_end_of_line (int to_x)
22952 {
22953 struct frame *f;
22954 struct window *w = updated_window;
22955 int max_x, min_y, max_y;
22956 int from_x, from_y, to_y;
22957
22958 xassert (updated_window && updated_row);
22959 f = XFRAME (w->frame);
22960
22961 if (updated_row->full_width_p)
22962 max_x = WINDOW_TOTAL_WIDTH (w);
22963 else
22964 max_x = window_box_width (w, updated_area);
22965 max_y = window_text_bottom_y (w);
22966
22967 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22968 of window. For TO_X > 0, truncate to end of drawing area. */
22969 if (to_x == 0)
22970 return;
22971 else if (to_x < 0)
22972 to_x = max_x;
22973 else
22974 to_x = min (to_x, max_x);
22975
22976 to_y = min (max_y, output_cursor.y + updated_row->height);
22977
22978 /* Notice if the cursor will be cleared by this operation. */
22979 if (!updated_row->full_width_p)
22980 notice_overwritten_cursor (w, updated_area,
22981 output_cursor.x, -1,
22982 updated_row->y,
22983 MATRIX_ROW_BOTTOM_Y (updated_row));
22984
22985 from_x = output_cursor.x;
22986
22987 /* Translate to frame coordinates. */
22988 if (updated_row->full_width_p)
22989 {
22990 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22991 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22992 }
22993 else
22994 {
22995 int area_left = window_box_left (w, updated_area);
22996 from_x += area_left;
22997 to_x += area_left;
22998 }
22999
23000 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23001 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23002 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23003
23004 /* Prevent inadvertently clearing to end of the X window. */
23005 if (to_x > from_x && to_y > from_y)
23006 {
23007 BLOCK_INPUT;
23008 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23009 to_x - from_x, to_y - from_y);
23010 UNBLOCK_INPUT;
23011 }
23012 }
23013
23014 #endif /* HAVE_WINDOW_SYSTEM */
23015
23016
23017 \f
23018 /***********************************************************************
23019 Cursor types
23020 ***********************************************************************/
23021
23022 /* Value is the internal representation of the specified cursor type
23023 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23024 of the bar cursor. */
23025
23026 static enum text_cursor_kinds
23027 get_specified_cursor_type (Lisp_Object arg, int *width)
23028 {
23029 enum text_cursor_kinds type;
23030
23031 if (NILP (arg))
23032 return NO_CURSOR;
23033
23034 if (EQ (arg, Qbox))
23035 return FILLED_BOX_CURSOR;
23036
23037 if (EQ (arg, Qhollow))
23038 return HOLLOW_BOX_CURSOR;
23039
23040 if (EQ (arg, Qbar))
23041 {
23042 *width = 2;
23043 return BAR_CURSOR;
23044 }
23045
23046 if (CONSP (arg)
23047 && EQ (XCAR (arg), Qbar)
23048 && INTEGERP (XCDR (arg))
23049 && XINT (XCDR (arg)) >= 0)
23050 {
23051 *width = XINT (XCDR (arg));
23052 return BAR_CURSOR;
23053 }
23054
23055 if (EQ (arg, Qhbar))
23056 {
23057 *width = 2;
23058 return HBAR_CURSOR;
23059 }
23060
23061 if (CONSP (arg)
23062 && EQ (XCAR (arg), Qhbar)
23063 && INTEGERP (XCDR (arg))
23064 && XINT (XCDR (arg)) >= 0)
23065 {
23066 *width = XINT (XCDR (arg));
23067 return HBAR_CURSOR;
23068 }
23069
23070 /* Treat anything unknown as "hollow box cursor".
23071 It was bad to signal an error; people have trouble fixing
23072 .Xdefaults with Emacs, when it has something bad in it. */
23073 type = HOLLOW_BOX_CURSOR;
23074
23075 return type;
23076 }
23077
23078 /* Set the default cursor types for specified frame. */
23079 void
23080 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23081 {
23082 int width;
23083 Lisp_Object tem;
23084
23085 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23086 FRAME_CURSOR_WIDTH (f) = width;
23087
23088 /* By default, set up the blink-off state depending on the on-state. */
23089
23090 tem = Fassoc (arg, Vblink_cursor_alist);
23091 if (!NILP (tem))
23092 {
23093 FRAME_BLINK_OFF_CURSOR (f)
23094 = get_specified_cursor_type (XCDR (tem), &width);
23095 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23096 }
23097 else
23098 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23099 }
23100
23101
23102 /* Return the cursor we want to be displayed in window W. Return
23103 width of bar/hbar cursor through WIDTH arg. Return with
23104 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23105 (i.e. if the `system caret' should track this cursor).
23106
23107 In a mini-buffer window, we want the cursor only to appear if we
23108 are reading input from this window. For the selected window, we
23109 want the cursor type given by the frame parameter or buffer local
23110 setting of cursor-type. If explicitly marked off, draw no cursor.
23111 In all other cases, we want a hollow box cursor. */
23112
23113 static enum text_cursor_kinds
23114 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23115 int *active_cursor)
23116 {
23117 struct frame *f = XFRAME (w->frame);
23118 struct buffer *b = XBUFFER (w->buffer);
23119 int cursor_type = DEFAULT_CURSOR;
23120 Lisp_Object alt_cursor;
23121 int non_selected = 0;
23122
23123 *active_cursor = 1;
23124
23125 /* Echo area */
23126 if (cursor_in_echo_area
23127 && FRAME_HAS_MINIBUF_P (f)
23128 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23129 {
23130 if (w == XWINDOW (echo_area_window))
23131 {
23132 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
23133 {
23134 *width = FRAME_CURSOR_WIDTH (f);
23135 return FRAME_DESIRED_CURSOR (f);
23136 }
23137 else
23138 return get_specified_cursor_type (b->cursor_type, width);
23139 }
23140
23141 *active_cursor = 0;
23142 non_selected = 1;
23143 }
23144
23145 /* Detect a nonselected window or nonselected frame. */
23146 else if (w != XWINDOW (f->selected_window)
23147 #ifdef HAVE_WINDOW_SYSTEM
23148 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
23149 #endif
23150 )
23151 {
23152 *active_cursor = 0;
23153
23154 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23155 return NO_CURSOR;
23156
23157 non_selected = 1;
23158 }
23159
23160 /* Never display a cursor in a window in which cursor-type is nil. */
23161 if (NILP (b->cursor_type))
23162 return NO_CURSOR;
23163
23164 /* Get the normal cursor type for this window. */
23165 if (EQ (b->cursor_type, Qt))
23166 {
23167 cursor_type = FRAME_DESIRED_CURSOR (f);
23168 *width = FRAME_CURSOR_WIDTH (f);
23169 }
23170 else
23171 cursor_type = get_specified_cursor_type (b->cursor_type, width);
23172
23173 /* Use cursor-in-non-selected-windows instead
23174 for non-selected window or frame. */
23175 if (non_selected)
23176 {
23177 alt_cursor = b->cursor_in_non_selected_windows;
23178 if (!EQ (Qt, alt_cursor))
23179 return get_specified_cursor_type (alt_cursor, width);
23180 /* t means modify the normal cursor type. */
23181 if (cursor_type == FILLED_BOX_CURSOR)
23182 cursor_type = HOLLOW_BOX_CURSOR;
23183 else if (cursor_type == BAR_CURSOR && *width > 1)
23184 --*width;
23185 return cursor_type;
23186 }
23187
23188 /* Use normal cursor if not blinked off. */
23189 if (!w->cursor_off_p)
23190 {
23191 #ifdef HAVE_WINDOW_SYSTEM
23192 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23193 {
23194 if (cursor_type == FILLED_BOX_CURSOR)
23195 {
23196 /* Using a block cursor on large images can be very annoying.
23197 So use a hollow cursor for "large" images.
23198 If image is not transparent (no mask), also use hollow cursor. */
23199 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23200 if (img != NULL && IMAGEP (img->spec))
23201 {
23202 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23203 where N = size of default frame font size.
23204 This should cover most of the "tiny" icons people may use. */
23205 if (!img->mask
23206 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23207 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23208 cursor_type = HOLLOW_BOX_CURSOR;
23209 }
23210 }
23211 else if (cursor_type != NO_CURSOR)
23212 {
23213 /* Display current only supports BOX and HOLLOW cursors for images.
23214 So for now, unconditionally use a HOLLOW cursor when cursor is
23215 not a solid box cursor. */
23216 cursor_type = HOLLOW_BOX_CURSOR;
23217 }
23218 }
23219 #endif
23220 return cursor_type;
23221 }
23222
23223 /* Cursor is blinked off, so determine how to "toggle" it. */
23224
23225 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23226 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
23227 return get_specified_cursor_type (XCDR (alt_cursor), width);
23228
23229 /* Then see if frame has specified a specific blink off cursor type. */
23230 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23231 {
23232 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23233 return FRAME_BLINK_OFF_CURSOR (f);
23234 }
23235
23236 #if 0
23237 /* Some people liked having a permanently visible blinking cursor,
23238 while others had very strong opinions against it. So it was
23239 decided to remove it. KFS 2003-09-03 */
23240
23241 /* Finally perform built-in cursor blinking:
23242 filled box <-> hollow box
23243 wide [h]bar <-> narrow [h]bar
23244 narrow [h]bar <-> no cursor
23245 other type <-> no cursor */
23246
23247 if (cursor_type == FILLED_BOX_CURSOR)
23248 return HOLLOW_BOX_CURSOR;
23249
23250 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23251 {
23252 *width = 1;
23253 return cursor_type;
23254 }
23255 #endif
23256
23257 return NO_CURSOR;
23258 }
23259
23260
23261 #ifdef HAVE_WINDOW_SYSTEM
23262
23263 /* Notice when the text cursor of window W has been completely
23264 overwritten by a drawing operation that outputs glyphs in AREA
23265 starting at X0 and ending at X1 in the line starting at Y0 and
23266 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23267 the rest of the line after X0 has been written. Y coordinates
23268 are window-relative. */
23269
23270 static void
23271 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23272 int x0, int x1, int y0, int y1)
23273 {
23274 int cx0, cx1, cy0, cy1;
23275 struct glyph_row *row;
23276
23277 if (!w->phys_cursor_on_p)
23278 return;
23279 if (area != TEXT_AREA)
23280 return;
23281
23282 if (w->phys_cursor.vpos < 0
23283 || w->phys_cursor.vpos >= w->current_matrix->nrows
23284 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23285 !(row->enabled_p && row->displays_text_p)))
23286 return;
23287
23288 if (row->cursor_in_fringe_p)
23289 {
23290 row->cursor_in_fringe_p = 0;
23291 draw_fringe_bitmap (w, row, row->reversed_p);
23292 w->phys_cursor_on_p = 0;
23293 return;
23294 }
23295
23296 cx0 = w->phys_cursor.x;
23297 cx1 = cx0 + w->phys_cursor_width;
23298 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23299 return;
23300
23301 /* The cursor image will be completely removed from the
23302 screen if the output area intersects the cursor area in
23303 y-direction. When we draw in [y0 y1[, and some part of
23304 the cursor is at y < y0, that part must have been drawn
23305 before. When scrolling, the cursor is erased before
23306 actually scrolling, so we don't come here. When not
23307 scrolling, the rows above the old cursor row must have
23308 changed, and in this case these rows must have written
23309 over the cursor image.
23310
23311 Likewise if part of the cursor is below y1, with the
23312 exception of the cursor being in the first blank row at
23313 the buffer and window end because update_text_area
23314 doesn't draw that row. (Except when it does, but
23315 that's handled in update_text_area.) */
23316
23317 cy0 = w->phys_cursor.y;
23318 cy1 = cy0 + w->phys_cursor_height;
23319 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23320 return;
23321
23322 w->phys_cursor_on_p = 0;
23323 }
23324
23325 #endif /* HAVE_WINDOW_SYSTEM */
23326
23327 \f
23328 /************************************************************************
23329 Mouse Face
23330 ************************************************************************/
23331
23332 #ifdef HAVE_WINDOW_SYSTEM
23333
23334 /* EXPORT for RIF:
23335 Fix the display of area AREA of overlapping row ROW in window W
23336 with respect to the overlapping part OVERLAPS. */
23337
23338 void
23339 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
23340 enum glyph_row_area area, int overlaps)
23341 {
23342 int i, x;
23343
23344 BLOCK_INPUT;
23345
23346 x = 0;
23347 for (i = 0; i < row->used[area];)
23348 {
23349 if (row->glyphs[area][i].overlaps_vertically_p)
23350 {
23351 int start = i, start_x = x;
23352
23353 do
23354 {
23355 x += row->glyphs[area][i].pixel_width;
23356 ++i;
23357 }
23358 while (i < row->used[area]
23359 && row->glyphs[area][i].overlaps_vertically_p);
23360
23361 draw_glyphs (w, start_x, row, area,
23362 start, i,
23363 DRAW_NORMAL_TEXT, overlaps);
23364 }
23365 else
23366 {
23367 x += row->glyphs[area][i].pixel_width;
23368 ++i;
23369 }
23370 }
23371
23372 UNBLOCK_INPUT;
23373 }
23374
23375
23376 /* EXPORT:
23377 Draw the cursor glyph of window W in glyph row ROW. See the
23378 comment of draw_glyphs for the meaning of HL. */
23379
23380 void
23381 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23382 enum draw_glyphs_face hl)
23383 {
23384 /* If cursor hpos is out of bounds, don't draw garbage. This can
23385 happen in mini-buffer windows when switching between echo area
23386 glyphs and mini-buffer. */
23387 if ((row->reversed_p
23388 ? (w->phys_cursor.hpos >= 0)
23389 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23390 {
23391 int on_p = w->phys_cursor_on_p;
23392 int x1;
23393 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23394 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23395 hl, 0);
23396 w->phys_cursor_on_p = on_p;
23397
23398 if (hl == DRAW_CURSOR)
23399 w->phys_cursor_width = x1 - w->phys_cursor.x;
23400 /* When we erase the cursor, and ROW is overlapped by other
23401 rows, make sure that these overlapping parts of other rows
23402 are redrawn. */
23403 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23404 {
23405 w->phys_cursor_width = x1 - w->phys_cursor.x;
23406
23407 if (row > w->current_matrix->rows
23408 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23409 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23410 OVERLAPS_ERASED_CURSOR);
23411
23412 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23413 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23414 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23415 OVERLAPS_ERASED_CURSOR);
23416 }
23417 }
23418 }
23419
23420
23421 /* EXPORT:
23422 Erase the image of a cursor of window W from the screen. */
23423
23424 void
23425 erase_phys_cursor (struct window *w)
23426 {
23427 struct frame *f = XFRAME (w->frame);
23428 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23429 int hpos = w->phys_cursor.hpos;
23430 int vpos = w->phys_cursor.vpos;
23431 int mouse_face_here_p = 0;
23432 struct glyph_matrix *active_glyphs = w->current_matrix;
23433 struct glyph_row *cursor_row;
23434 struct glyph *cursor_glyph;
23435 enum draw_glyphs_face hl;
23436
23437 /* No cursor displayed or row invalidated => nothing to do on the
23438 screen. */
23439 if (w->phys_cursor_type == NO_CURSOR)
23440 goto mark_cursor_off;
23441
23442 /* VPOS >= active_glyphs->nrows means that window has been resized.
23443 Don't bother to erase the cursor. */
23444 if (vpos >= active_glyphs->nrows)
23445 goto mark_cursor_off;
23446
23447 /* If row containing cursor is marked invalid, there is nothing we
23448 can do. */
23449 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23450 if (!cursor_row->enabled_p)
23451 goto mark_cursor_off;
23452
23453 /* If line spacing is > 0, old cursor may only be partially visible in
23454 window after split-window. So adjust visible height. */
23455 cursor_row->visible_height = min (cursor_row->visible_height,
23456 window_text_bottom_y (w) - cursor_row->y);
23457
23458 /* If row is completely invisible, don't attempt to delete a cursor which
23459 isn't there. This can happen if cursor is at top of a window, and
23460 we switch to a buffer with a header line in that window. */
23461 if (cursor_row->visible_height <= 0)
23462 goto mark_cursor_off;
23463
23464 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23465 if (cursor_row->cursor_in_fringe_p)
23466 {
23467 cursor_row->cursor_in_fringe_p = 0;
23468 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23469 goto mark_cursor_off;
23470 }
23471
23472 /* This can happen when the new row is shorter than the old one.
23473 In this case, either draw_glyphs or clear_end_of_line
23474 should have cleared the cursor. Note that we wouldn't be
23475 able to erase the cursor in this case because we don't have a
23476 cursor glyph at hand. */
23477 if ((cursor_row->reversed_p
23478 ? (w->phys_cursor.hpos < 0)
23479 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23480 goto mark_cursor_off;
23481
23482 /* If the cursor is in the mouse face area, redisplay that when
23483 we clear the cursor. */
23484 if (! NILP (dpyinfo->mouse_face_window)
23485 && w == XWINDOW (dpyinfo->mouse_face_window)
23486 && (vpos > dpyinfo->mouse_face_beg_row
23487 || (vpos == dpyinfo->mouse_face_beg_row
23488 && hpos >= dpyinfo->mouse_face_beg_col))
23489 && (vpos < dpyinfo->mouse_face_end_row
23490 || (vpos == dpyinfo->mouse_face_end_row
23491 && hpos < dpyinfo->mouse_face_end_col))
23492 /* Don't redraw the cursor's spot in mouse face if it is at the
23493 end of a line (on a newline). The cursor appears there, but
23494 mouse highlighting does not. */
23495 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
23496 mouse_face_here_p = 1;
23497
23498 /* Maybe clear the display under the cursor. */
23499 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23500 {
23501 int x, y, left_x;
23502 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23503 int width;
23504
23505 cursor_glyph = get_phys_cursor_glyph (w);
23506 if (cursor_glyph == NULL)
23507 goto mark_cursor_off;
23508
23509 width = cursor_glyph->pixel_width;
23510 left_x = window_box_left_offset (w, TEXT_AREA);
23511 x = w->phys_cursor.x;
23512 if (x < left_x)
23513 width -= left_x - x;
23514 width = min (width, window_box_width (w, TEXT_AREA) - x);
23515 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23516 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23517
23518 if (width > 0)
23519 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23520 }
23521
23522 /* Erase the cursor by redrawing the character underneath it. */
23523 if (mouse_face_here_p)
23524 hl = DRAW_MOUSE_FACE;
23525 else
23526 hl = DRAW_NORMAL_TEXT;
23527 draw_phys_cursor_glyph (w, cursor_row, hl);
23528
23529 mark_cursor_off:
23530 w->phys_cursor_on_p = 0;
23531 w->phys_cursor_type = NO_CURSOR;
23532 }
23533
23534
23535 /* EXPORT:
23536 Display or clear cursor of window W. If ON is zero, clear the
23537 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23538 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23539
23540 void
23541 display_and_set_cursor (struct window *w, int on,
23542 int hpos, int vpos, int x, int y)
23543 {
23544 struct frame *f = XFRAME (w->frame);
23545 int new_cursor_type;
23546 int new_cursor_width;
23547 int active_cursor;
23548 struct glyph_row *glyph_row;
23549 struct glyph *glyph;
23550
23551 /* This is pointless on invisible frames, and dangerous on garbaged
23552 windows and frames; in the latter case, the frame or window may
23553 be in the midst of changing its size, and x and y may be off the
23554 window. */
23555 if (! FRAME_VISIBLE_P (f)
23556 || FRAME_GARBAGED_P (f)
23557 || vpos >= w->current_matrix->nrows
23558 || hpos >= w->current_matrix->matrix_w)
23559 return;
23560
23561 /* If cursor is off and we want it off, return quickly. */
23562 if (!on && !w->phys_cursor_on_p)
23563 return;
23564
23565 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23566 /* If cursor row is not enabled, we don't really know where to
23567 display the cursor. */
23568 if (!glyph_row->enabled_p)
23569 {
23570 w->phys_cursor_on_p = 0;
23571 return;
23572 }
23573
23574 glyph = NULL;
23575 if (!glyph_row->exact_window_width_line_p
23576 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
23577 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23578
23579 xassert (interrupt_input_blocked);
23580
23581 /* Set new_cursor_type to the cursor we want to be displayed. */
23582 new_cursor_type = get_window_cursor_type (w, glyph,
23583 &new_cursor_width, &active_cursor);
23584
23585 /* If cursor is currently being shown and we don't want it to be or
23586 it is in the wrong place, or the cursor type is not what we want,
23587 erase it. */
23588 if (w->phys_cursor_on_p
23589 && (!on
23590 || w->phys_cursor.x != x
23591 || w->phys_cursor.y != y
23592 || new_cursor_type != w->phys_cursor_type
23593 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23594 && new_cursor_width != w->phys_cursor_width)))
23595 erase_phys_cursor (w);
23596
23597 /* Don't check phys_cursor_on_p here because that flag is only set
23598 to zero in some cases where we know that the cursor has been
23599 completely erased, to avoid the extra work of erasing the cursor
23600 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23601 still not be visible, or it has only been partly erased. */
23602 if (on)
23603 {
23604 w->phys_cursor_ascent = glyph_row->ascent;
23605 w->phys_cursor_height = glyph_row->height;
23606
23607 /* Set phys_cursor_.* before x_draw_.* is called because some
23608 of them may need the information. */
23609 w->phys_cursor.x = x;
23610 w->phys_cursor.y = glyph_row->y;
23611 w->phys_cursor.hpos = hpos;
23612 w->phys_cursor.vpos = vpos;
23613 }
23614
23615 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23616 new_cursor_type, new_cursor_width,
23617 on, active_cursor);
23618 }
23619
23620
23621 /* Switch the display of W's cursor on or off, according to the value
23622 of ON. */
23623
23624 void
23625 update_window_cursor (struct window *w, int on)
23626 {
23627 /* Don't update cursor in windows whose frame is in the process
23628 of being deleted. */
23629 if (w->current_matrix)
23630 {
23631 BLOCK_INPUT;
23632 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23633 w->phys_cursor.x, w->phys_cursor.y);
23634 UNBLOCK_INPUT;
23635 }
23636 }
23637
23638
23639 /* Call update_window_cursor with parameter ON_P on all leaf windows
23640 in the window tree rooted at W. */
23641
23642 static void
23643 update_cursor_in_window_tree (struct window *w, int on_p)
23644 {
23645 while (w)
23646 {
23647 if (!NILP (w->hchild))
23648 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23649 else if (!NILP (w->vchild))
23650 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23651 else
23652 update_window_cursor (w, on_p);
23653
23654 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23655 }
23656 }
23657
23658
23659 /* EXPORT:
23660 Display the cursor on window W, or clear it, according to ON_P.
23661 Don't change the cursor's position. */
23662
23663 void
23664 x_update_cursor (struct frame *f, int on_p)
23665 {
23666 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23667 }
23668
23669
23670 /* EXPORT:
23671 Clear the cursor of window W to background color, and mark the
23672 cursor as not shown. This is used when the text where the cursor
23673 is about to be rewritten. */
23674
23675 void
23676 x_clear_cursor (struct window *w)
23677 {
23678 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
23679 update_window_cursor (w, 0);
23680 }
23681
23682
23683 /* EXPORT:
23684 Display the active region described by mouse_face_* according to DRAW. */
23685
23686 void
23687 show_mouse_face (Display_Info *dpyinfo, enum draw_glyphs_face draw)
23688 {
23689 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
23690 struct frame *f = XFRAME (WINDOW_FRAME (w));
23691
23692 if (/* If window is in the process of being destroyed, don't bother
23693 to do anything. */
23694 w->current_matrix != NULL
23695 /* Don't update mouse highlight if hidden */
23696 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
23697 /* Recognize when we are called to operate on rows that don't exist
23698 anymore. This can happen when a window is split. */
23699 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
23700 {
23701 int phys_cursor_on_p = w->phys_cursor_on_p;
23702 struct glyph_row *row, *first, *last;
23703
23704 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
23705 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
23706
23707 for (row = first; row <= last && row->enabled_p; ++row)
23708 {
23709 int start_hpos, end_hpos, start_x;
23710
23711 /* For all but the first row, the highlight starts at column 0. */
23712 if (row == first)
23713 {
23714 start_hpos = dpyinfo->mouse_face_beg_col;
23715 start_x = dpyinfo->mouse_face_beg_x;
23716 }
23717 else
23718 {
23719 start_hpos = 0;
23720 start_x = 0;
23721 }
23722
23723 if (row == last)
23724 end_hpos = dpyinfo->mouse_face_end_col;
23725 else
23726 {
23727 end_hpos = row->used[TEXT_AREA];
23728 if (draw == DRAW_NORMAL_TEXT)
23729 row->fill_line_p = 1; /* Clear to end of line */
23730 }
23731
23732 if (end_hpos > start_hpos)
23733 {
23734 draw_glyphs (w, start_x, row, TEXT_AREA,
23735 start_hpos, end_hpos,
23736 draw, 0);
23737
23738 row->mouse_face_p
23739 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
23740 }
23741 }
23742
23743 /* When we've written over the cursor, arrange for it to
23744 be displayed again. */
23745 if (phys_cursor_on_p && !w->phys_cursor_on_p)
23746 {
23747 BLOCK_INPUT;
23748 display_and_set_cursor (w, 1,
23749 w->phys_cursor.hpos, w->phys_cursor.vpos,
23750 w->phys_cursor.x, w->phys_cursor.y);
23751 UNBLOCK_INPUT;
23752 }
23753 }
23754
23755 /* Change the mouse cursor. */
23756 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
23757 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
23758 else if (draw == DRAW_MOUSE_FACE)
23759 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
23760 else
23761 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
23762 }
23763
23764 /* EXPORT:
23765 Clear out the mouse-highlighted active region.
23766 Redraw it un-highlighted first. Value is non-zero if mouse
23767 face was actually drawn unhighlighted. */
23768
23769 int
23770 clear_mouse_face (Display_Info *dpyinfo)
23771 {
23772 int cleared = 0;
23773
23774 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
23775 {
23776 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
23777 cleared = 1;
23778 }
23779
23780 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23781 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23782 dpyinfo->mouse_face_window = Qnil;
23783 dpyinfo->mouse_face_overlay = Qnil;
23784 return cleared;
23785 }
23786
23787
23788 /* EXPORT:
23789 Non-zero if physical cursor of window W is within mouse face. */
23790
23791 int
23792 cursor_in_mouse_face_p (struct window *w)
23793 {
23794 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23795 int in_mouse_face = 0;
23796
23797 if (WINDOWP (dpyinfo->mouse_face_window)
23798 && XWINDOW (dpyinfo->mouse_face_window) == w)
23799 {
23800 int hpos = w->phys_cursor.hpos;
23801 int vpos = w->phys_cursor.vpos;
23802
23803 if (vpos >= dpyinfo->mouse_face_beg_row
23804 && vpos <= dpyinfo->mouse_face_end_row
23805 && (vpos > dpyinfo->mouse_face_beg_row
23806 || hpos >= dpyinfo->mouse_face_beg_col)
23807 && (vpos < dpyinfo->mouse_face_end_row
23808 || hpos < dpyinfo->mouse_face_end_col
23809 || dpyinfo->mouse_face_past_end))
23810 in_mouse_face = 1;
23811 }
23812
23813 return in_mouse_face;
23814 }
23815
23816
23817
23818 \f
23819 /* This function sets the mouse_face_* elements of DPYINFO, assuming
23820 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
23821 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
23822 for the overlay or run of text properties specifying the mouse
23823 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
23824 before-string and after-string that must also be highlighted.
23825 DISPLAY_STRING, if non-nil, is a display string that may cover some
23826 or all of the highlighted text. */
23827
23828 static void
23829 mouse_face_from_buffer_pos (Lisp_Object window,
23830 Display_Info *dpyinfo,
23831 EMACS_INT mouse_charpos,
23832 EMACS_INT start_charpos,
23833 EMACS_INT end_charpos,
23834 Lisp_Object before_string,
23835 Lisp_Object after_string,
23836 Lisp_Object display_string)
23837 {
23838 struct window *w = XWINDOW (window);
23839 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23840 struct glyph_row *row;
23841 struct glyph *glyph, *end;
23842 EMACS_INT ignore, pos;
23843 int x;
23844
23845 xassert (NILP (display_string) || STRINGP (display_string));
23846 xassert (NILP (before_string) || STRINGP (before_string));
23847 xassert (NILP (after_string) || STRINGP (after_string));
23848
23849 /* Find the first highlighted glyph. */
23850 if (start_charpos < MATRIX_ROW_START_CHARPOS (first)
23851 && (NILP (XBUFFER (w->buffer)->bidi_display_reordering)
23852 || row_containing_pos (w, start_charpos, first, NULL, 0) == NULL))
23853 {
23854 dpyinfo->mouse_face_beg_col = 0;
23855 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
23856 dpyinfo->mouse_face_beg_x = first->x;
23857 dpyinfo->mouse_face_beg_y = first->y;
23858 }
23859 else
23860 {
23861 /* FIXME: this assumes that START_CHARPOS is in beg_row. This
23862 is false for reordered lines that are continued. Need to
23863 compute beg_row and end_row separately from beg_col and
23864 end_col. */
23865 row = row_containing_pos (w, start_charpos, first, NULL, 0);
23866 if (row == NULL)
23867 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23868
23869 /* If the before-string or display-string contains newlines,
23870 row_containing_pos skips to its last row. Move back. */
23871 if (!NILP (before_string) || !NILP (display_string))
23872 {
23873 struct glyph_row *prev;
23874 while ((prev = row - 1, prev >= first)
23875 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
23876 && prev->used[TEXT_AREA] > 0)
23877 {
23878 struct glyph *beg = prev->glyphs[TEXT_AREA];
23879 glyph = beg + prev->used[TEXT_AREA];
23880 while (--glyph >= beg && INTEGERP (glyph->object));
23881 if (glyph < beg
23882 || !(EQ (glyph->object, before_string)
23883 || EQ (glyph->object, display_string)))
23884 break;
23885 row = prev;
23886 }
23887 }
23888
23889 dpyinfo->mouse_face_beg_y = row->y;
23890 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23891
23892 /* For a bidi-reordered row, the positions of BEFORE_STRING,
23893 AFTER_STRING, DISPLAY_STRING, START_CHARPOS, and END_CHARPOS
23894 could be anywhere in the row and in any order. The strategy
23895 below is to find the leftmost and the rightmost glyph that
23896 belongs to either of these 3 strings, or whose position is
23897 between START_CHARPOS and END_CHARPOS, and highlight all the
23898 glyphs between those two. This may cover more than just the
23899 text between START_CHARPOS and END_CHARPOS if the range of
23900 characters strides the the bidi level boundary, e.g. if the
23901 beginning is in R2L text while the end is in L2R text or vice
23902 versa. */
23903 if (!row->reversed_p)
23904 {
23905 /* This row is in a left to right paragraph. Scan it left
23906 to right. */
23907 glyph = row->glyphs[TEXT_AREA];
23908 end = glyph + row->used[TEXT_AREA];
23909 x = row->x;
23910
23911 /* Skip truncation glyphs at the start of the glyph row. */
23912 if (row->displays_text_p)
23913 for (; glyph < end
23914 && INTEGERP (glyph->object)
23915 && glyph->charpos < 0;
23916 ++glyph)
23917 x += glyph->pixel_width;
23918
23919 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
23920 or DISPLAY_STRING, and the first glyph whose position is
23921 between START_CHARPOS and END_CHARPOS. */
23922 for (; glyph < end
23923 && !INTEGERP (glyph->object)
23924 && !EQ (glyph->object, display_string)
23925 && !(BUFFERP (glyph->object)
23926 && (glyph->charpos >= start_charpos
23927 && glyph->charpos < end_charpos));
23928 ++glyph)
23929 {
23930 /* BEFORE_STRING or AFTER_STRING are only relevant if
23931 they are present at buffer positions between
23932 START_CHARPOS and END_CHARPOS. */
23933 if (EQ (glyph->object, before_string))
23934 {
23935 pos = string_buffer_position (w, before_string,
23936 start_charpos);
23937 if (pos && pos >= start_charpos && pos < end_charpos)
23938 break;
23939 }
23940 else if (EQ (glyph->object, after_string))
23941 {
23942 pos = string_buffer_position (w, after_string, end_charpos);
23943 if (pos && pos >= start_charpos && pos < end_charpos)
23944 break;
23945 }
23946 x += glyph->pixel_width;
23947 }
23948 dpyinfo->mouse_face_beg_x = x;
23949 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
23950 }
23951 else
23952 {
23953 /* FIXME! */
23954 }
23955 }
23956
23957 /* Find the last highlighted glyph. */
23958 row = row_containing_pos (w, end_charpos, first, NULL, 0);
23959 if (row == NULL)
23960 {
23961 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23962 dpyinfo->mouse_face_past_end = 1;
23963 }
23964 else if (!NILP (after_string))
23965 {
23966 /* If the after-string has newlines, advance to its last row. */
23967 struct glyph_row *next;
23968 struct glyph_row *last
23969 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23970
23971 for (next = row + 1;
23972 next <= last
23973 && next->used[TEXT_AREA] > 0
23974 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
23975 ++next)
23976 row = next;
23977 }
23978
23979 dpyinfo->mouse_face_end_y = row->y;
23980 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23981
23982 if (!row->reversed_p)
23983 {
23984 /* Skip truncation and continuation glyphs near the end of the
23985 row, and also blanks and stretch glyphs inserted by
23986 extend_face_to_end_of_line. */
23987 while (end > glyph
23988 && INTEGERP ((end - 1)->object)
23989 && (end - 1)->charpos <= 0)
23990 --end;
23991 /* Scan the rest of the glyph row from the end, looking for the
23992 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
23993 DISPLAY_STRING, or whose position is between START_CHARPOS
23994 and END_CHARPOS */
23995 for (--end;
23996 end > glyph
23997 && !INTEGERP (end->object)
23998 && !EQ (end->object, display_string)
23999 && !(BUFFERP (end->object)
24000 && (end->charpos >= start_charpos
24001 && end->charpos < end_charpos));
24002 --end)
24003 {
24004 /* BEFORE_STRING or AFTER_STRING are only relevant if
24005 they are present at buffer positions between
24006 START_CHARPOS and END_CHARPOS. */
24007 if (EQ (end->object, before_string))
24008 {
24009 pos = string_buffer_position (w, before_string, start_charpos);
24010 if (pos && pos >= start_charpos && pos < end_charpos)
24011 break;
24012 }
24013 else if (EQ (end->object, after_string))
24014 {
24015 pos = string_buffer_position (w, after_string, end_charpos);
24016 if (pos && pos >= start_charpos && pos < end_charpos)
24017 break;
24018 }
24019 }
24020 /* Find the X coordinate of the last glyph to be highlighted. */
24021 for (; glyph <= end; ++glyph)
24022 x += glyph->pixel_width;
24023 }
24024 else
24025 {
24026 /* FIXME! */
24027 }
24028
24029 dpyinfo->mouse_face_end_x = x;
24030 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
24031 dpyinfo->mouse_face_window = window;
24032 dpyinfo->mouse_face_face_id
24033 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
24034 mouse_charpos + 1,
24035 !dpyinfo->mouse_face_hidden, -1);
24036 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24037 }
24038
24039
24040 /* Find the position of the glyph for position POS in OBJECT in
24041 window W's current matrix, and return in *X, *Y the pixel
24042 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
24043
24044 RIGHT_P non-zero means return the position of the right edge of the
24045 glyph, RIGHT_P zero means return the left edge position.
24046
24047 If no glyph for POS exists in the matrix, return the position of
24048 the glyph with the next smaller position that is in the matrix, if
24049 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
24050 exists in the matrix, return the position of the glyph with the
24051 next larger position in OBJECT.
24052
24053 Value is non-zero if a glyph was found. */
24054
24055 static int
24056 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
24057 int *hpos, int *vpos, int *x, int *y, int right_p)
24058 {
24059 int yb = window_text_bottom_y (w);
24060 struct glyph_row *r;
24061 struct glyph *best_glyph = NULL;
24062 struct glyph_row *best_row = NULL;
24063 int best_x = 0;
24064
24065 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24066 r->enabled_p && r->y < yb;
24067 ++r)
24068 {
24069 struct glyph *g = r->glyphs[TEXT_AREA];
24070 struct glyph *e = g + r->used[TEXT_AREA];
24071 int gx;
24072
24073 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24074 if (EQ (g->object, object))
24075 {
24076 if (g->charpos == pos)
24077 {
24078 best_glyph = g;
24079 best_x = gx;
24080 best_row = r;
24081 goto found;
24082 }
24083 else if (best_glyph == NULL
24084 || ((eabs (g->charpos - pos)
24085 < eabs (best_glyph->charpos - pos))
24086 && (right_p
24087 ? g->charpos < pos
24088 : g->charpos > pos)))
24089 {
24090 best_glyph = g;
24091 best_x = gx;
24092 best_row = r;
24093 }
24094 }
24095 }
24096
24097 found:
24098
24099 if (best_glyph)
24100 {
24101 *x = best_x;
24102 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
24103
24104 if (right_p)
24105 {
24106 *x += best_glyph->pixel_width;
24107 ++*hpos;
24108 }
24109
24110 *y = best_row->y;
24111 *vpos = best_row - w->current_matrix->rows;
24112 }
24113
24114 return best_glyph != NULL;
24115 }
24116
24117
24118 /* See if position X, Y is within a hot-spot of an image. */
24119
24120 static int
24121 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
24122 {
24123 if (!CONSP (hot_spot))
24124 return 0;
24125
24126 if (EQ (XCAR (hot_spot), Qrect))
24127 {
24128 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24129 Lisp_Object rect = XCDR (hot_spot);
24130 Lisp_Object tem;
24131 if (!CONSP (rect))
24132 return 0;
24133 if (!CONSP (XCAR (rect)))
24134 return 0;
24135 if (!CONSP (XCDR (rect)))
24136 return 0;
24137 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24138 return 0;
24139 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24140 return 0;
24141 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24142 return 0;
24143 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24144 return 0;
24145 return 1;
24146 }
24147 else if (EQ (XCAR (hot_spot), Qcircle))
24148 {
24149 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24150 Lisp_Object circ = XCDR (hot_spot);
24151 Lisp_Object lr, lx0, ly0;
24152 if (CONSP (circ)
24153 && CONSP (XCAR (circ))
24154 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24155 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24156 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24157 {
24158 double r = XFLOATINT (lr);
24159 double dx = XINT (lx0) - x;
24160 double dy = XINT (ly0) - y;
24161 return (dx * dx + dy * dy <= r * r);
24162 }
24163 }
24164 else if (EQ (XCAR (hot_spot), Qpoly))
24165 {
24166 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24167 if (VECTORP (XCDR (hot_spot)))
24168 {
24169 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24170 Lisp_Object *poly = v->contents;
24171 int n = v->size;
24172 int i;
24173 int inside = 0;
24174 Lisp_Object lx, ly;
24175 int x0, y0;
24176
24177 /* Need an even number of coordinates, and at least 3 edges. */
24178 if (n < 6 || n & 1)
24179 return 0;
24180
24181 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24182 If count is odd, we are inside polygon. Pixels on edges
24183 may or may not be included depending on actual geometry of the
24184 polygon. */
24185 if ((lx = poly[n-2], !INTEGERP (lx))
24186 || (ly = poly[n-1], !INTEGERP (lx)))
24187 return 0;
24188 x0 = XINT (lx), y0 = XINT (ly);
24189 for (i = 0; i < n; i += 2)
24190 {
24191 int x1 = x0, y1 = y0;
24192 if ((lx = poly[i], !INTEGERP (lx))
24193 || (ly = poly[i+1], !INTEGERP (ly)))
24194 return 0;
24195 x0 = XINT (lx), y0 = XINT (ly);
24196
24197 /* Does this segment cross the X line? */
24198 if (x0 >= x)
24199 {
24200 if (x1 >= x)
24201 continue;
24202 }
24203 else if (x1 < x)
24204 continue;
24205 if (y > y0 && y > y1)
24206 continue;
24207 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24208 inside = !inside;
24209 }
24210 return inside;
24211 }
24212 }
24213 return 0;
24214 }
24215
24216 Lisp_Object
24217 find_hot_spot (Lisp_Object map, int x, int y)
24218 {
24219 while (CONSP (map))
24220 {
24221 if (CONSP (XCAR (map))
24222 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
24223 return XCAR (map);
24224 map = XCDR (map);
24225 }
24226
24227 return Qnil;
24228 }
24229
24230 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
24231 3, 3, 0,
24232 doc: /* Lookup in image map MAP coordinates X and Y.
24233 An image map is an alist where each element has the format (AREA ID PLIST).
24234 An AREA is specified as either a rectangle, a circle, or a polygon:
24235 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
24236 pixel coordinates of the upper left and bottom right corners.
24237 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
24238 and the radius of the circle; r may be a float or integer.
24239 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
24240 vector describes one corner in the polygon.
24241 Returns the alist element for the first matching AREA in MAP. */)
24242 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
24243 {
24244 if (NILP (map))
24245 return Qnil;
24246
24247 CHECK_NUMBER (x);
24248 CHECK_NUMBER (y);
24249
24250 return find_hot_spot (map, XINT (x), XINT (y));
24251 }
24252
24253
24254 /* Display frame CURSOR, optionally using shape defined by POINTER. */
24255 static void
24256 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
24257 {
24258 /* Do not change cursor shape while dragging mouse. */
24259 if (!NILP (do_mouse_tracking))
24260 return;
24261
24262 if (!NILP (pointer))
24263 {
24264 if (EQ (pointer, Qarrow))
24265 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24266 else if (EQ (pointer, Qhand))
24267 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
24268 else if (EQ (pointer, Qtext))
24269 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24270 else if (EQ (pointer, intern ("hdrag")))
24271 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24272 #ifdef HAVE_X_WINDOWS
24273 else if (EQ (pointer, intern ("vdrag")))
24274 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
24275 #endif
24276 else if (EQ (pointer, intern ("hourglass")))
24277 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
24278 else if (EQ (pointer, Qmodeline))
24279 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
24280 else
24281 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24282 }
24283
24284 if (cursor != No_Cursor)
24285 FRAME_RIF (f)->define_frame_cursor (f, cursor);
24286 }
24287
24288 /* Take proper action when mouse has moved to the mode or header line
24289 or marginal area AREA of window W, x-position X and y-position Y.
24290 X is relative to the start of the text display area of W, so the
24291 width of bitmap areas and scroll bars must be subtracted to get a
24292 position relative to the start of the mode line. */
24293
24294 static void
24295 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
24296 enum window_part area)
24297 {
24298 struct window *w = XWINDOW (window);
24299 struct frame *f = XFRAME (w->frame);
24300 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24301 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24302 Lisp_Object pointer = Qnil;
24303 int charpos, dx, dy, width, height;
24304 Lisp_Object string, object = Qnil;
24305 Lisp_Object pos, help;
24306
24307 Lisp_Object mouse_face;
24308 int original_x_pixel = x;
24309 struct glyph * glyph = NULL, * row_start_glyph = NULL;
24310 struct glyph_row *row;
24311
24312 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
24313 {
24314 int x0;
24315 struct glyph *end;
24316
24317 string = mode_line_string (w, area, &x, &y, &charpos,
24318 &object, &dx, &dy, &width, &height);
24319
24320 row = (area == ON_MODE_LINE
24321 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
24322 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
24323
24324 /* Find glyph */
24325 if (row->mode_line_p && row->enabled_p)
24326 {
24327 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
24328 end = glyph + row->used[TEXT_AREA];
24329
24330 for (x0 = original_x_pixel;
24331 glyph < end && x0 >= glyph->pixel_width;
24332 ++glyph)
24333 x0 -= glyph->pixel_width;
24334
24335 if (glyph >= end)
24336 glyph = NULL;
24337 }
24338 }
24339 else
24340 {
24341 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
24342 string = marginal_area_string (w, area, &x, &y, &charpos,
24343 &object, &dx, &dy, &width, &height);
24344 }
24345
24346 help = Qnil;
24347
24348 if (IMAGEP (object))
24349 {
24350 Lisp_Object image_map, hotspot;
24351 if ((image_map = Fplist_get (XCDR (object), QCmap),
24352 !NILP (image_map))
24353 && (hotspot = find_hot_spot (image_map, dx, dy),
24354 CONSP (hotspot))
24355 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24356 {
24357 Lisp_Object area_id, plist;
24358
24359 area_id = XCAR (hotspot);
24360 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24361 If so, we could look for mouse-enter, mouse-leave
24362 properties in PLIST (and do something...). */
24363 hotspot = XCDR (hotspot);
24364 if (CONSP (hotspot)
24365 && (plist = XCAR (hotspot), CONSP (plist)))
24366 {
24367 pointer = Fplist_get (plist, Qpointer);
24368 if (NILP (pointer))
24369 pointer = Qhand;
24370 help = Fplist_get (plist, Qhelp_echo);
24371 if (!NILP (help))
24372 {
24373 help_echo_string = help;
24374 /* Is this correct? ++kfs */
24375 XSETWINDOW (help_echo_window, w);
24376 help_echo_object = w->buffer;
24377 help_echo_pos = charpos;
24378 }
24379 }
24380 }
24381 if (NILP (pointer))
24382 pointer = Fplist_get (XCDR (object), QCpointer);
24383 }
24384
24385 if (STRINGP (string))
24386 {
24387 pos = make_number (charpos);
24388 /* If we're on a string with `help-echo' text property, arrange
24389 for the help to be displayed. This is done by setting the
24390 global variable help_echo_string to the help string. */
24391 if (NILP (help))
24392 {
24393 help = Fget_text_property (pos, Qhelp_echo, string);
24394 if (!NILP (help))
24395 {
24396 help_echo_string = help;
24397 XSETWINDOW (help_echo_window, w);
24398 help_echo_object = string;
24399 help_echo_pos = charpos;
24400 }
24401 }
24402
24403 if (NILP (pointer))
24404 pointer = Fget_text_property (pos, Qpointer, string);
24405
24406 /* Change the mouse pointer according to what is under X/Y. */
24407 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
24408 {
24409 Lisp_Object map;
24410 map = Fget_text_property (pos, Qlocal_map, string);
24411 if (!KEYMAPP (map))
24412 map = Fget_text_property (pos, Qkeymap, string);
24413 if (!KEYMAPP (map))
24414 cursor = dpyinfo->vertical_scroll_bar_cursor;
24415 }
24416
24417 /* Change the mouse face according to what is under X/Y. */
24418 mouse_face = Fget_text_property (pos, Qmouse_face, string);
24419 if (!NILP (mouse_face)
24420 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24421 && glyph)
24422 {
24423 Lisp_Object b, e;
24424
24425 struct glyph * tmp_glyph;
24426
24427 int gpos;
24428 int gseq_length;
24429 int total_pixel_width;
24430 EMACS_INT ignore;
24431
24432 int vpos, hpos;
24433
24434 b = Fprevious_single_property_change (make_number (charpos + 1),
24435 Qmouse_face, string, Qnil);
24436 if (NILP (b))
24437 b = make_number (0);
24438
24439 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
24440 if (NILP (e))
24441 e = make_number (SCHARS (string));
24442
24443 /* Calculate the position(glyph position: GPOS) of GLYPH in
24444 displayed string. GPOS is different from CHARPOS.
24445
24446 CHARPOS is the position of glyph in internal string
24447 object. A mode line string format has structures which
24448 is converted to a flatten by emacs lisp interpreter.
24449 The internal string is an element of the structures.
24450 The displayed string is the flatten string. */
24451 gpos = 0;
24452 if (glyph > row_start_glyph)
24453 {
24454 tmp_glyph = glyph - 1;
24455 while (tmp_glyph >= row_start_glyph
24456 && tmp_glyph->charpos >= XINT (b)
24457 && EQ (tmp_glyph->object, glyph->object))
24458 {
24459 tmp_glyph--;
24460 gpos++;
24461 }
24462 }
24463
24464 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
24465 displayed string holding GLYPH.
24466
24467 GSEQ_LENGTH is different from SCHARS (STRING).
24468 SCHARS (STRING) returns the length of the internal string. */
24469 for (tmp_glyph = glyph, gseq_length = gpos;
24470 tmp_glyph->charpos < XINT (e);
24471 tmp_glyph++, gseq_length++)
24472 {
24473 if (!EQ (tmp_glyph->object, glyph->object))
24474 break;
24475 }
24476
24477 total_pixel_width = 0;
24478 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
24479 total_pixel_width += tmp_glyph->pixel_width;
24480
24481 /* Pre calculation of re-rendering position */
24482 vpos = (x - gpos);
24483 hpos = (area == ON_MODE_LINE
24484 ? (w->current_matrix)->nrows - 1
24485 : 0);
24486
24487 /* If the re-rendering position is included in the last
24488 re-rendering area, we should do nothing. */
24489 if ( EQ (window, dpyinfo->mouse_face_window)
24490 && dpyinfo->mouse_face_beg_col <= vpos
24491 && vpos < dpyinfo->mouse_face_end_col
24492 && dpyinfo->mouse_face_beg_row == hpos )
24493 return;
24494
24495 if (clear_mouse_face (dpyinfo))
24496 cursor = No_Cursor;
24497
24498 dpyinfo->mouse_face_beg_col = vpos;
24499 dpyinfo->mouse_face_beg_row = hpos;
24500
24501 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
24502 dpyinfo->mouse_face_beg_y = 0;
24503
24504 dpyinfo->mouse_face_end_col = vpos + gseq_length;
24505 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
24506
24507 dpyinfo->mouse_face_end_x = 0;
24508 dpyinfo->mouse_face_end_y = 0;
24509
24510 dpyinfo->mouse_face_past_end = 0;
24511 dpyinfo->mouse_face_window = window;
24512
24513 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
24514 charpos,
24515 0, 0, 0, &ignore,
24516 glyph->face_id, 1);
24517 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24518
24519 if (NILP (pointer))
24520 pointer = Qhand;
24521 }
24522 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24523 clear_mouse_face (dpyinfo);
24524 }
24525 define_frame_cursor1 (f, cursor, pointer);
24526 }
24527
24528
24529 /* EXPORT:
24530 Take proper action when the mouse has moved to position X, Y on
24531 frame F as regards highlighting characters that have mouse-face
24532 properties. Also de-highlighting chars where the mouse was before.
24533 X and Y can be negative or out of range. */
24534
24535 void
24536 note_mouse_highlight (struct frame *f, int x, int y)
24537 {
24538 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24539 enum window_part part;
24540 Lisp_Object window;
24541 struct window *w;
24542 Cursor cursor = No_Cursor;
24543 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
24544 struct buffer *b;
24545
24546 /* When a menu is active, don't highlight because this looks odd. */
24547 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
24548 if (popup_activated ())
24549 return;
24550 #endif
24551
24552 if (NILP (Vmouse_highlight)
24553 || !f->glyphs_initialized_p
24554 || f->pointer_invisible)
24555 return;
24556
24557 dpyinfo->mouse_face_mouse_x = x;
24558 dpyinfo->mouse_face_mouse_y = y;
24559 dpyinfo->mouse_face_mouse_frame = f;
24560
24561 if (dpyinfo->mouse_face_defer)
24562 return;
24563
24564 if (gc_in_progress)
24565 {
24566 dpyinfo->mouse_face_deferred_gc = 1;
24567 return;
24568 }
24569
24570 /* Which window is that in? */
24571 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
24572
24573 /* If we were displaying active text in another window, clear that.
24574 Also clear if we move out of text area in same window. */
24575 if (! EQ (window, dpyinfo->mouse_face_window)
24576 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
24577 && !NILP (dpyinfo->mouse_face_window)))
24578 clear_mouse_face (dpyinfo);
24579
24580 /* Not on a window -> return. */
24581 if (!WINDOWP (window))
24582 return;
24583
24584 /* Reset help_echo_string. It will get recomputed below. */
24585 help_echo_string = Qnil;
24586
24587 /* Convert to window-relative pixel coordinates. */
24588 w = XWINDOW (window);
24589 frame_to_window_pixel_xy (w, &x, &y);
24590
24591 /* Handle tool-bar window differently since it doesn't display a
24592 buffer. */
24593 if (EQ (window, f->tool_bar_window))
24594 {
24595 note_tool_bar_highlight (f, x, y);
24596 return;
24597 }
24598
24599 /* Mouse is on the mode, header line or margin? */
24600 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
24601 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
24602 {
24603 note_mode_line_or_margin_highlight (window, x, y, part);
24604 return;
24605 }
24606
24607 if (part == ON_VERTICAL_BORDER)
24608 {
24609 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24610 help_echo_string = build_string ("drag-mouse-1: resize");
24611 }
24612 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
24613 || part == ON_SCROLL_BAR)
24614 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24615 else
24616 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24617
24618 /* Are we in a window whose display is up to date?
24619 And verify the buffer's text has not changed. */
24620 b = XBUFFER (w->buffer);
24621 if (part == ON_TEXT
24622 && EQ (w->window_end_valid, w->buffer)
24623 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
24624 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
24625 {
24626 int hpos, vpos, i, dx, dy, area;
24627 EMACS_INT pos;
24628 struct glyph *glyph;
24629 Lisp_Object object;
24630 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
24631 Lisp_Object *overlay_vec = NULL;
24632 int noverlays;
24633 struct buffer *obuf;
24634 int obegv, ozv, same_region;
24635
24636 /* Find the glyph under X/Y. */
24637 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
24638
24639 /* Look for :pointer property on image. */
24640 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24641 {
24642 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24643 if (img != NULL && IMAGEP (img->spec))
24644 {
24645 Lisp_Object image_map, hotspot;
24646 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
24647 !NILP (image_map))
24648 && (hotspot = find_hot_spot (image_map,
24649 glyph->slice.x + dx,
24650 glyph->slice.y + dy),
24651 CONSP (hotspot))
24652 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24653 {
24654 Lisp_Object area_id, plist;
24655
24656 area_id = XCAR (hotspot);
24657 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24658 If so, we could look for mouse-enter, mouse-leave
24659 properties in PLIST (and do something...). */
24660 hotspot = XCDR (hotspot);
24661 if (CONSP (hotspot)
24662 && (plist = XCAR (hotspot), CONSP (plist)))
24663 {
24664 pointer = Fplist_get (plist, Qpointer);
24665 if (NILP (pointer))
24666 pointer = Qhand;
24667 help_echo_string = Fplist_get (plist, Qhelp_echo);
24668 if (!NILP (help_echo_string))
24669 {
24670 help_echo_window = window;
24671 help_echo_object = glyph->object;
24672 help_echo_pos = glyph->charpos;
24673 }
24674 }
24675 }
24676 if (NILP (pointer))
24677 pointer = Fplist_get (XCDR (img->spec), QCpointer);
24678 }
24679 }
24680
24681 /* Clear mouse face if X/Y not over text. */
24682 if (glyph == NULL
24683 || area != TEXT_AREA
24684 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
24685 {
24686 if (clear_mouse_face (dpyinfo))
24687 cursor = No_Cursor;
24688 if (NILP (pointer))
24689 {
24690 if (area != TEXT_AREA)
24691 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24692 else
24693 pointer = Vvoid_text_area_pointer;
24694 }
24695 goto set_cursor;
24696 }
24697
24698 pos = glyph->charpos;
24699 object = glyph->object;
24700 if (!STRINGP (object) && !BUFFERP (object))
24701 goto set_cursor;
24702
24703 /* If we get an out-of-range value, return now; avoid an error. */
24704 if (BUFFERP (object) && pos > BUF_Z (b))
24705 goto set_cursor;
24706
24707 /* Make the window's buffer temporarily current for
24708 overlays_at and compute_char_face. */
24709 obuf = current_buffer;
24710 current_buffer = b;
24711 obegv = BEGV;
24712 ozv = ZV;
24713 BEGV = BEG;
24714 ZV = Z;
24715
24716 /* Is this char mouse-active or does it have help-echo? */
24717 position = make_number (pos);
24718
24719 if (BUFFERP (object))
24720 {
24721 /* Put all the overlays we want in a vector in overlay_vec. */
24722 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
24723 /* Sort overlays into increasing priority order. */
24724 noverlays = sort_overlays (overlay_vec, noverlays, w);
24725 }
24726 else
24727 noverlays = 0;
24728
24729 same_region = (EQ (window, dpyinfo->mouse_face_window)
24730 && vpos >= dpyinfo->mouse_face_beg_row
24731 && vpos <= dpyinfo->mouse_face_end_row
24732 && (vpos > dpyinfo->mouse_face_beg_row
24733 || hpos >= dpyinfo->mouse_face_beg_col)
24734 && (vpos < dpyinfo->mouse_face_end_row
24735 || hpos < dpyinfo->mouse_face_end_col
24736 || dpyinfo->mouse_face_past_end));
24737
24738 if (same_region)
24739 cursor = No_Cursor;
24740
24741 /* Check mouse-face highlighting. */
24742 if (! same_region
24743 /* If there exists an overlay with mouse-face overlapping
24744 the one we are currently highlighting, we have to
24745 check if we enter the overlapping overlay, and then
24746 highlight only that. */
24747 || (OVERLAYP (dpyinfo->mouse_face_overlay)
24748 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
24749 {
24750 /* Find the highest priority overlay with a mouse-face. */
24751 overlay = Qnil;
24752 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
24753 {
24754 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
24755 if (!NILP (mouse_face))
24756 overlay = overlay_vec[i];
24757 }
24758
24759 /* If we're highlighting the same overlay as before, there's
24760 no need to do that again. */
24761 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
24762 goto check_help_echo;
24763 dpyinfo->mouse_face_overlay = overlay;
24764
24765 /* Clear the display of the old active region, if any. */
24766 if (clear_mouse_face (dpyinfo))
24767 cursor = No_Cursor;
24768
24769 /* If no overlay applies, get a text property. */
24770 if (NILP (overlay))
24771 mouse_face = Fget_text_property (position, Qmouse_face, object);
24772
24773 /* Next, compute the bounds of the mouse highlighting and
24774 display it. */
24775 if (!NILP (mouse_face) && STRINGP (object))
24776 {
24777 /* The mouse-highlighting comes from a display string
24778 with a mouse-face. */
24779 Lisp_Object b, e;
24780 EMACS_INT ignore;
24781
24782 b = Fprevious_single_property_change
24783 (make_number (pos + 1), Qmouse_face, object, Qnil);
24784 e = Fnext_single_property_change
24785 (position, Qmouse_face, object, Qnil);
24786 if (NILP (b))
24787 b = make_number (0);
24788 if (NILP (e))
24789 e = make_number (SCHARS (object) - 1);
24790
24791 fast_find_string_pos (w, XINT (b), object,
24792 &dpyinfo->mouse_face_beg_col,
24793 &dpyinfo->mouse_face_beg_row,
24794 &dpyinfo->mouse_face_beg_x,
24795 &dpyinfo->mouse_face_beg_y, 0);
24796 fast_find_string_pos (w, XINT (e), object,
24797 &dpyinfo->mouse_face_end_col,
24798 &dpyinfo->mouse_face_end_row,
24799 &dpyinfo->mouse_face_end_x,
24800 &dpyinfo->mouse_face_end_y, 1);
24801 dpyinfo->mouse_face_past_end = 0;
24802 dpyinfo->mouse_face_window = window;
24803 dpyinfo->mouse_face_face_id
24804 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
24805 glyph->face_id, 1);
24806 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24807 cursor = No_Cursor;
24808 }
24809 else
24810 {
24811 /* The mouse-highlighting, if any, comes from an overlay
24812 or text property in the buffer. */
24813 Lisp_Object buffer, display_string;
24814
24815 if (STRINGP (object))
24816 {
24817 /* If we are on a display string with no mouse-face,
24818 check if the text under it has one. */
24819 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
24820 int start = MATRIX_ROW_START_CHARPOS (r);
24821 pos = string_buffer_position (w, object, start);
24822 if (pos > 0)
24823 {
24824 mouse_face = get_char_property_and_overlay
24825 (make_number (pos), Qmouse_face, w->buffer, &overlay);
24826 buffer = w->buffer;
24827 display_string = object;
24828 }
24829 }
24830 else
24831 {
24832 buffer = object;
24833 display_string = Qnil;
24834 }
24835
24836 if (!NILP (mouse_face))
24837 {
24838 Lisp_Object before, after;
24839 Lisp_Object before_string, after_string;
24840
24841 if (NILP (overlay))
24842 {
24843 /* Handle the text property case. */
24844 before = Fprevious_single_property_change
24845 (make_number (pos + 1), Qmouse_face, buffer,
24846 Fmarker_position (w->start));
24847 after = Fnext_single_property_change
24848 (make_number (pos), Qmouse_face, buffer,
24849 make_number (BUF_Z (XBUFFER (buffer))
24850 - XFASTINT (w->window_end_pos)));
24851 before_string = after_string = Qnil;
24852 }
24853 else
24854 {
24855 /* Handle the overlay case. */
24856 before = Foverlay_start (overlay);
24857 after = Foverlay_end (overlay);
24858 before_string = Foverlay_get (overlay, Qbefore_string);
24859 after_string = Foverlay_get (overlay, Qafter_string);
24860
24861 if (!STRINGP (before_string)) before_string = Qnil;
24862 if (!STRINGP (after_string)) after_string = Qnil;
24863 }
24864
24865 mouse_face_from_buffer_pos (window, dpyinfo, pos,
24866 XFASTINT (before),
24867 XFASTINT (after),
24868 before_string, after_string,
24869 display_string);
24870 cursor = No_Cursor;
24871 }
24872 }
24873 }
24874
24875 check_help_echo:
24876
24877 /* Look for a `help-echo' property. */
24878 if (NILP (help_echo_string)) {
24879 Lisp_Object help, overlay;
24880
24881 /* Check overlays first. */
24882 help = overlay = Qnil;
24883 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
24884 {
24885 overlay = overlay_vec[i];
24886 help = Foverlay_get (overlay, Qhelp_echo);
24887 }
24888
24889 if (!NILP (help))
24890 {
24891 help_echo_string = help;
24892 help_echo_window = window;
24893 help_echo_object = overlay;
24894 help_echo_pos = pos;
24895 }
24896 else
24897 {
24898 Lisp_Object object = glyph->object;
24899 int charpos = glyph->charpos;
24900
24901 /* Try text properties. */
24902 if (STRINGP (object)
24903 && charpos >= 0
24904 && charpos < SCHARS (object))
24905 {
24906 help = Fget_text_property (make_number (charpos),
24907 Qhelp_echo, object);
24908 if (NILP (help))
24909 {
24910 /* If the string itself doesn't specify a help-echo,
24911 see if the buffer text ``under'' it does. */
24912 struct glyph_row *r
24913 = MATRIX_ROW (w->current_matrix, vpos);
24914 int start = MATRIX_ROW_START_CHARPOS (r);
24915 EMACS_INT pos = string_buffer_position (w, object, start);
24916 if (pos > 0)
24917 {
24918 help = Fget_char_property (make_number (pos),
24919 Qhelp_echo, w->buffer);
24920 if (!NILP (help))
24921 {
24922 charpos = pos;
24923 object = w->buffer;
24924 }
24925 }
24926 }
24927 }
24928 else if (BUFFERP (object)
24929 && charpos >= BEGV
24930 && charpos < ZV)
24931 help = Fget_text_property (make_number (charpos), Qhelp_echo,
24932 object);
24933
24934 if (!NILP (help))
24935 {
24936 help_echo_string = help;
24937 help_echo_window = window;
24938 help_echo_object = object;
24939 help_echo_pos = charpos;
24940 }
24941 }
24942 }
24943
24944 /* Look for a `pointer' property. */
24945 if (NILP (pointer))
24946 {
24947 /* Check overlays first. */
24948 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
24949 pointer = Foverlay_get (overlay_vec[i], Qpointer);
24950
24951 if (NILP (pointer))
24952 {
24953 Lisp_Object object = glyph->object;
24954 int charpos = glyph->charpos;
24955
24956 /* Try text properties. */
24957 if (STRINGP (object)
24958 && charpos >= 0
24959 && charpos < SCHARS (object))
24960 {
24961 pointer = Fget_text_property (make_number (charpos),
24962 Qpointer, object);
24963 if (NILP (pointer))
24964 {
24965 /* If the string itself doesn't specify a pointer,
24966 see if the buffer text ``under'' it does. */
24967 struct glyph_row *r
24968 = MATRIX_ROW (w->current_matrix, vpos);
24969 int start = MATRIX_ROW_START_CHARPOS (r);
24970 EMACS_INT pos = string_buffer_position (w, object,
24971 start);
24972 if (pos > 0)
24973 pointer = Fget_char_property (make_number (pos),
24974 Qpointer, w->buffer);
24975 }
24976 }
24977 else if (BUFFERP (object)
24978 && charpos >= BEGV
24979 && charpos < ZV)
24980 pointer = Fget_text_property (make_number (charpos),
24981 Qpointer, object);
24982 }
24983 }
24984
24985 BEGV = obegv;
24986 ZV = ozv;
24987 current_buffer = obuf;
24988 }
24989
24990 set_cursor:
24991
24992 define_frame_cursor1 (f, cursor, pointer);
24993 }
24994
24995
24996 /* EXPORT for RIF:
24997 Clear any mouse-face on window W. This function is part of the
24998 redisplay interface, and is called from try_window_id and similar
24999 functions to ensure the mouse-highlight is off. */
25000
25001 void
25002 x_clear_window_mouse_face (struct window *w)
25003 {
25004 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
25005 Lisp_Object window;
25006
25007 BLOCK_INPUT;
25008 XSETWINDOW (window, w);
25009 if (EQ (window, dpyinfo->mouse_face_window))
25010 clear_mouse_face (dpyinfo);
25011 UNBLOCK_INPUT;
25012 }
25013
25014
25015 /* EXPORT:
25016 Just discard the mouse face information for frame F, if any.
25017 This is used when the size of F is changed. */
25018
25019 void
25020 cancel_mouse_face (struct frame *f)
25021 {
25022 Lisp_Object window;
25023 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
25024
25025 window = dpyinfo->mouse_face_window;
25026 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
25027 {
25028 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
25029 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
25030 dpyinfo->mouse_face_window = Qnil;
25031 }
25032 }
25033
25034
25035 #endif /* HAVE_WINDOW_SYSTEM */
25036
25037 \f
25038 /***********************************************************************
25039 Exposure Events
25040 ***********************************************************************/
25041
25042 #ifdef HAVE_WINDOW_SYSTEM
25043
25044 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
25045 which intersects rectangle R. R is in window-relative coordinates. */
25046
25047 static void
25048 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
25049 enum glyph_row_area area)
25050 {
25051 struct glyph *first = row->glyphs[area];
25052 struct glyph *end = row->glyphs[area] + row->used[area];
25053 struct glyph *last;
25054 int first_x, start_x, x;
25055
25056 if (area == TEXT_AREA && row->fill_line_p)
25057 /* If row extends face to end of line write the whole line. */
25058 draw_glyphs (w, 0, row, area,
25059 0, row->used[area],
25060 DRAW_NORMAL_TEXT, 0);
25061 else
25062 {
25063 /* Set START_X to the window-relative start position for drawing glyphs of
25064 AREA. The first glyph of the text area can be partially visible.
25065 The first glyphs of other areas cannot. */
25066 start_x = window_box_left_offset (w, area);
25067 x = start_x;
25068 if (area == TEXT_AREA)
25069 x += row->x;
25070
25071 /* Find the first glyph that must be redrawn. */
25072 while (first < end
25073 && x + first->pixel_width < r->x)
25074 {
25075 x += first->pixel_width;
25076 ++first;
25077 }
25078
25079 /* Find the last one. */
25080 last = first;
25081 first_x = x;
25082 while (last < end
25083 && x < r->x + r->width)
25084 {
25085 x += last->pixel_width;
25086 ++last;
25087 }
25088
25089 /* Repaint. */
25090 if (last > first)
25091 draw_glyphs (w, first_x - start_x, row, area,
25092 first - row->glyphs[area], last - row->glyphs[area],
25093 DRAW_NORMAL_TEXT, 0);
25094 }
25095 }
25096
25097
25098 /* Redraw the parts of the glyph row ROW on window W intersecting
25099 rectangle R. R is in window-relative coordinates. Value is
25100 non-zero if mouse-face was overwritten. */
25101
25102 static int
25103 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
25104 {
25105 xassert (row->enabled_p);
25106
25107 if (row->mode_line_p || w->pseudo_window_p)
25108 draw_glyphs (w, 0, row, TEXT_AREA,
25109 0, row->used[TEXT_AREA],
25110 DRAW_NORMAL_TEXT, 0);
25111 else
25112 {
25113 if (row->used[LEFT_MARGIN_AREA])
25114 expose_area (w, row, r, LEFT_MARGIN_AREA);
25115 if (row->used[TEXT_AREA])
25116 expose_area (w, row, r, TEXT_AREA);
25117 if (row->used[RIGHT_MARGIN_AREA])
25118 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25119 draw_row_fringe_bitmaps (w, row);
25120 }
25121
25122 return row->mouse_face_p;
25123 }
25124
25125
25126 /* Redraw those parts of glyphs rows during expose event handling that
25127 overlap other rows. Redrawing of an exposed line writes over parts
25128 of lines overlapping that exposed line; this function fixes that.
25129
25130 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25131 row in W's current matrix that is exposed and overlaps other rows.
25132 LAST_OVERLAPPING_ROW is the last such row. */
25133
25134 static void
25135 expose_overlaps (struct window *w,
25136 struct glyph_row *first_overlapping_row,
25137 struct glyph_row *last_overlapping_row,
25138 XRectangle *r)
25139 {
25140 struct glyph_row *row;
25141
25142 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
25143 if (row->overlapping_p)
25144 {
25145 xassert (row->enabled_p && !row->mode_line_p);
25146
25147 row->clip = r;
25148 if (row->used[LEFT_MARGIN_AREA])
25149 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
25150
25151 if (row->used[TEXT_AREA])
25152 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
25153
25154 if (row->used[RIGHT_MARGIN_AREA])
25155 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
25156 row->clip = NULL;
25157 }
25158 }
25159
25160
25161 /* Return non-zero if W's cursor intersects rectangle R. */
25162
25163 static int
25164 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
25165 {
25166 XRectangle cr, result;
25167 struct glyph *cursor_glyph;
25168 struct glyph_row *row;
25169
25170 if (w->phys_cursor.vpos >= 0
25171 && w->phys_cursor.vpos < w->current_matrix->nrows
25172 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
25173 row->enabled_p)
25174 && row->cursor_in_fringe_p)
25175 {
25176 /* Cursor is in the fringe. */
25177 cr.x = window_box_right_offset (w,
25178 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
25179 ? RIGHT_MARGIN_AREA
25180 : TEXT_AREA));
25181 cr.y = row->y;
25182 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
25183 cr.height = row->height;
25184 return x_intersect_rectangles (&cr, r, &result);
25185 }
25186
25187 cursor_glyph = get_phys_cursor_glyph (w);
25188 if (cursor_glyph)
25189 {
25190 /* r is relative to W's box, but w->phys_cursor.x is relative
25191 to left edge of W's TEXT area. Adjust it. */
25192 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
25193 cr.y = w->phys_cursor.y;
25194 cr.width = cursor_glyph->pixel_width;
25195 cr.height = w->phys_cursor_height;
25196 /* ++KFS: W32 version used W32-specific IntersectRect here, but
25197 I assume the effect is the same -- and this is portable. */
25198 return x_intersect_rectangles (&cr, r, &result);
25199 }
25200 /* If we don't understand the format, pretend we're not in the hot-spot. */
25201 return 0;
25202 }
25203
25204
25205 /* EXPORT:
25206 Draw a vertical window border to the right of window W if W doesn't
25207 have vertical scroll bars. */
25208
25209 void
25210 x_draw_vertical_border (struct window *w)
25211 {
25212 struct frame *f = XFRAME (WINDOW_FRAME (w));
25213
25214 /* We could do better, if we knew what type of scroll-bar the adjacent
25215 windows (on either side) have... But we don't :-(
25216 However, I think this works ok. ++KFS 2003-04-25 */
25217
25218 /* Redraw borders between horizontally adjacent windows. Don't
25219 do it for frames with vertical scroll bars because either the
25220 right scroll bar of a window, or the left scroll bar of its
25221 neighbor will suffice as a border. */
25222 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
25223 return;
25224
25225 if (!WINDOW_RIGHTMOST_P (w)
25226 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
25227 {
25228 int x0, x1, y0, y1;
25229
25230 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25231 y1 -= 1;
25232
25233 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25234 x1 -= 1;
25235
25236 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
25237 }
25238 else if (!WINDOW_LEFTMOST_P (w)
25239 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
25240 {
25241 int x0, x1, y0, y1;
25242
25243 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25244 y1 -= 1;
25245
25246 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25247 x0 -= 1;
25248
25249 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
25250 }
25251 }
25252
25253
25254 /* Redraw the part of window W intersection rectangle FR. Pixel
25255 coordinates in FR are frame-relative. Call this function with
25256 input blocked. Value is non-zero if the exposure overwrites
25257 mouse-face. */
25258
25259 static int
25260 expose_window (struct window *w, XRectangle *fr)
25261 {
25262 struct frame *f = XFRAME (w->frame);
25263 XRectangle wr, r;
25264 int mouse_face_overwritten_p = 0;
25265
25266 /* If window is not yet fully initialized, do nothing. This can
25267 happen when toolkit scroll bars are used and a window is split.
25268 Reconfiguring the scroll bar will generate an expose for a newly
25269 created window. */
25270 if (w->current_matrix == NULL)
25271 return 0;
25272
25273 /* When we're currently updating the window, display and current
25274 matrix usually don't agree. Arrange for a thorough display
25275 later. */
25276 if (w == updated_window)
25277 {
25278 SET_FRAME_GARBAGED (f);
25279 return 0;
25280 }
25281
25282 /* Frame-relative pixel rectangle of W. */
25283 wr.x = WINDOW_LEFT_EDGE_X (w);
25284 wr.y = WINDOW_TOP_EDGE_Y (w);
25285 wr.width = WINDOW_TOTAL_WIDTH (w);
25286 wr.height = WINDOW_TOTAL_HEIGHT (w);
25287
25288 if (x_intersect_rectangles (fr, &wr, &r))
25289 {
25290 int yb = window_text_bottom_y (w);
25291 struct glyph_row *row;
25292 int cursor_cleared_p;
25293 struct glyph_row *first_overlapping_row, *last_overlapping_row;
25294
25295 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
25296 r.x, r.y, r.width, r.height));
25297
25298 /* Convert to window coordinates. */
25299 r.x -= WINDOW_LEFT_EDGE_X (w);
25300 r.y -= WINDOW_TOP_EDGE_Y (w);
25301
25302 /* Turn off the cursor. */
25303 if (!w->pseudo_window_p
25304 && phys_cursor_in_rect_p (w, &r))
25305 {
25306 x_clear_cursor (w);
25307 cursor_cleared_p = 1;
25308 }
25309 else
25310 cursor_cleared_p = 0;
25311
25312 /* Update lines intersecting rectangle R. */
25313 first_overlapping_row = last_overlapping_row = NULL;
25314 for (row = w->current_matrix->rows;
25315 row->enabled_p;
25316 ++row)
25317 {
25318 int y0 = row->y;
25319 int y1 = MATRIX_ROW_BOTTOM_Y (row);
25320
25321 if ((y0 >= r.y && y0 < r.y + r.height)
25322 || (y1 > r.y && y1 < r.y + r.height)
25323 || (r.y >= y0 && r.y < y1)
25324 || (r.y + r.height > y0 && r.y + r.height < y1))
25325 {
25326 /* A header line may be overlapping, but there is no need
25327 to fix overlapping areas for them. KFS 2005-02-12 */
25328 if (row->overlapping_p && !row->mode_line_p)
25329 {
25330 if (first_overlapping_row == NULL)
25331 first_overlapping_row = row;
25332 last_overlapping_row = row;
25333 }
25334
25335 row->clip = fr;
25336 if (expose_line (w, row, &r))
25337 mouse_face_overwritten_p = 1;
25338 row->clip = NULL;
25339 }
25340 else if (row->overlapping_p)
25341 {
25342 /* We must redraw a row overlapping the exposed area. */
25343 if (y0 < r.y
25344 ? y0 + row->phys_height > r.y
25345 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
25346 {
25347 if (first_overlapping_row == NULL)
25348 first_overlapping_row = row;
25349 last_overlapping_row = row;
25350 }
25351 }
25352
25353 if (y1 >= yb)
25354 break;
25355 }
25356
25357 /* Display the mode line if there is one. */
25358 if (WINDOW_WANTS_MODELINE_P (w)
25359 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
25360 row->enabled_p)
25361 && row->y < r.y + r.height)
25362 {
25363 if (expose_line (w, row, &r))
25364 mouse_face_overwritten_p = 1;
25365 }
25366
25367 if (!w->pseudo_window_p)
25368 {
25369 /* Fix the display of overlapping rows. */
25370 if (first_overlapping_row)
25371 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
25372 fr);
25373
25374 /* Draw border between windows. */
25375 x_draw_vertical_border (w);
25376
25377 /* Turn the cursor on again. */
25378 if (cursor_cleared_p)
25379 update_window_cursor (w, 1);
25380 }
25381 }
25382
25383 return mouse_face_overwritten_p;
25384 }
25385
25386
25387
25388 /* Redraw (parts) of all windows in the window tree rooted at W that
25389 intersect R. R contains frame pixel coordinates. Value is
25390 non-zero if the exposure overwrites mouse-face. */
25391
25392 static int
25393 expose_window_tree (struct window *w, XRectangle *r)
25394 {
25395 struct frame *f = XFRAME (w->frame);
25396 int mouse_face_overwritten_p = 0;
25397
25398 while (w && !FRAME_GARBAGED_P (f))
25399 {
25400 if (!NILP (w->hchild))
25401 mouse_face_overwritten_p
25402 |= expose_window_tree (XWINDOW (w->hchild), r);
25403 else if (!NILP (w->vchild))
25404 mouse_face_overwritten_p
25405 |= expose_window_tree (XWINDOW (w->vchild), r);
25406 else
25407 mouse_face_overwritten_p |= expose_window (w, r);
25408
25409 w = NILP (w->next) ? NULL : XWINDOW (w->next);
25410 }
25411
25412 return mouse_face_overwritten_p;
25413 }
25414
25415
25416 /* EXPORT:
25417 Redisplay an exposed area of frame F. X and Y are the upper-left
25418 corner of the exposed rectangle. W and H are width and height of
25419 the exposed area. All are pixel values. W or H zero means redraw
25420 the entire frame. */
25421
25422 void
25423 expose_frame (struct frame *f, int x, int y, int w, int h)
25424 {
25425 XRectangle r;
25426 int mouse_face_overwritten_p = 0;
25427
25428 TRACE ((stderr, "expose_frame "));
25429
25430 /* No need to redraw if frame will be redrawn soon. */
25431 if (FRAME_GARBAGED_P (f))
25432 {
25433 TRACE ((stderr, " garbaged\n"));
25434 return;
25435 }
25436
25437 /* If basic faces haven't been realized yet, there is no point in
25438 trying to redraw anything. This can happen when we get an expose
25439 event while Emacs is starting, e.g. by moving another window. */
25440 if (FRAME_FACE_CACHE (f) == NULL
25441 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
25442 {
25443 TRACE ((stderr, " no faces\n"));
25444 return;
25445 }
25446
25447 if (w == 0 || h == 0)
25448 {
25449 r.x = r.y = 0;
25450 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
25451 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
25452 }
25453 else
25454 {
25455 r.x = x;
25456 r.y = y;
25457 r.width = w;
25458 r.height = h;
25459 }
25460
25461 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
25462 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
25463
25464 if (WINDOWP (f->tool_bar_window))
25465 mouse_face_overwritten_p
25466 |= expose_window (XWINDOW (f->tool_bar_window), &r);
25467
25468 #ifdef HAVE_X_WINDOWS
25469 #ifndef MSDOS
25470 #ifndef USE_X_TOOLKIT
25471 if (WINDOWP (f->menu_bar_window))
25472 mouse_face_overwritten_p
25473 |= expose_window (XWINDOW (f->menu_bar_window), &r);
25474 #endif /* not USE_X_TOOLKIT */
25475 #endif
25476 #endif
25477
25478 /* Some window managers support a focus-follows-mouse style with
25479 delayed raising of frames. Imagine a partially obscured frame,
25480 and moving the mouse into partially obscured mouse-face on that
25481 frame. The visible part of the mouse-face will be highlighted,
25482 then the WM raises the obscured frame. With at least one WM, KDE
25483 2.1, Emacs is not getting any event for the raising of the frame
25484 (even tried with SubstructureRedirectMask), only Expose events.
25485 These expose events will draw text normally, i.e. not
25486 highlighted. Which means we must redo the highlight here.
25487 Subsume it under ``we love X''. --gerd 2001-08-15 */
25488 /* Included in Windows version because Windows most likely does not
25489 do the right thing if any third party tool offers
25490 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
25491 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
25492 {
25493 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
25494 if (f == dpyinfo->mouse_face_mouse_frame)
25495 {
25496 int x = dpyinfo->mouse_face_mouse_x;
25497 int y = dpyinfo->mouse_face_mouse_y;
25498 clear_mouse_face (dpyinfo);
25499 note_mouse_highlight (f, x, y);
25500 }
25501 }
25502 }
25503
25504
25505 /* EXPORT:
25506 Determine the intersection of two rectangles R1 and R2. Return
25507 the intersection in *RESULT. Value is non-zero if RESULT is not
25508 empty. */
25509
25510 int
25511 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
25512 {
25513 XRectangle *left, *right;
25514 XRectangle *upper, *lower;
25515 int intersection_p = 0;
25516
25517 /* Rearrange so that R1 is the left-most rectangle. */
25518 if (r1->x < r2->x)
25519 left = r1, right = r2;
25520 else
25521 left = r2, right = r1;
25522
25523 /* X0 of the intersection is right.x0, if this is inside R1,
25524 otherwise there is no intersection. */
25525 if (right->x <= left->x + left->width)
25526 {
25527 result->x = right->x;
25528
25529 /* The right end of the intersection is the minimum of the
25530 the right ends of left and right. */
25531 result->width = (min (left->x + left->width, right->x + right->width)
25532 - result->x);
25533
25534 /* Same game for Y. */
25535 if (r1->y < r2->y)
25536 upper = r1, lower = r2;
25537 else
25538 upper = r2, lower = r1;
25539
25540 /* The upper end of the intersection is lower.y0, if this is inside
25541 of upper. Otherwise, there is no intersection. */
25542 if (lower->y <= upper->y + upper->height)
25543 {
25544 result->y = lower->y;
25545
25546 /* The lower end of the intersection is the minimum of the lower
25547 ends of upper and lower. */
25548 result->height = (min (lower->y + lower->height,
25549 upper->y + upper->height)
25550 - result->y);
25551 intersection_p = 1;
25552 }
25553 }
25554
25555 return intersection_p;
25556 }
25557
25558 #endif /* HAVE_WINDOW_SYSTEM */
25559
25560 \f
25561 /***********************************************************************
25562 Initialization
25563 ***********************************************************************/
25564
25565 void
25566 syms_of_xdisp (void)
25567 {
25568 Vwith_echo_area_save_vector = Qnil;
25569 staticpro (&Vwith_echo_area_save_vector);
25570
25571 Vmessage_stack = Qnil;
25572 staticpro (&Vmessage_stack);
25573
25574 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
25575 staticpro (&Qinhibit_redisplay);
25576
25577 message_dolog_marker1 = Fmake_marker ();
25578 staticpro (&message_dolog_marker1);
25579 message_dolog_marker2 = Fmake_marker ();
25580 staticpro (&message_dolog_marker2);
25581 message_dolog_marker3 = Fmake_marker ();
25582 staticpro (&message_dolog_marker3);
25583
25584 #if GLYPH_DEBUG
25585 defsubr (&Sdump_frame_glyph_matrix);
25586 defsubr (&Sdump_glyph_matrix);
25587 defsubr (&Sdump_glyph_row);
25588 defsubr (&Sdump_tool_bar_row);
25589 defsubr (&Strace_redisplay);
25590 defsubr (&Strace_to_stderr);
25591 #endif
25592 #ifdef HAVE_WINDOW_SYSTEM
25593 defsubr (&Stool_bar_lines_needed);
25594 defsubr (&Slookup_image_map);
25595 #endif
25596 defsubr (&Sformat_mode_line);
25597 defsubr (&Sinvisible_p);
25598 defsubr (&Scurrent_bidi_paragraph_direction);
25599
25600 staticpro (&Qmenu_bar_update_hook);
25601 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
25602
25603 staticpro (&Qoverriding_terminal_local_map);
25604 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
25605
25606 staticpro (&Qoverriding_local_map);
25607 Qoverriding_local_map = intern_c_string ("overriding-local-map");
25608
25609 staticpro (&Qwindow_scroll_functions);
25610 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
25611
25612 staticpro (&Qwindow_text_change_functions);
25613 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
25614
25615 staticpro (&Qredisplay_end_trigger_functions);
25616 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
25617
25618 staticpro (&Qinhibit_point_motion_hooks);
25619 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
25620
25621 Qeval = intern_c_string ("eval");
25622 staticpro (&Qeval);
25623
25624 QCdata = intern_c_string (":data");
25625 staticpro (&QCdata);
25626 Qdisplay = intern_c_string ("display");
25627 staticpro (&Qdisplay);
25628 Qspace_width = intern_c_string ("space-width");
25629 staticpro (&Qspace_width);
25630 Qraise = intern_c_string ("raise");
25631 staticpro (&Qraise);
25632 Qslice = intern_c_string ("slice");
25633 staticpro (&Qslice);
25634 Qspace = intern_c_string ("space");
25635 staticpro (&Qspace);
25636 Qmargin = intern_c_string ("margin");
25637 staticpro (&Qmargin);
25638 Qpointer = intern_c_string ("pointer");
25639 staticpro (&Qpointer);
25640 Qleft_margin = intern_c_string ("left-margin");
25641 staticpro (&Qleft_margin);
25642 Qright_margin = intern_c_string ("right-margin");
25643 staticpro (&Qright_margin);
25644 Qcenter = intern_c_string ("center");
25645 staticpro (&Qcenter);
25646 Qline_height = intern_c_string ("line-height");
25647 staticpro (&Qline_height);
25648 QCalign_to = intern_c_string (":align-to");
25649 staticpro (&QCalign_to);
25650 QCrelative_width = intern_c_string (":relative-width");
25651 staticpro (&QCrelative_width);
25652 QCrelative_height = intern_c_string (":relative-height");
25653 staticpro (&QCrelative_height);
25654 QCeval = intern_c_string (":eval");
25655 staticpro (&QCeval);
25656 QCpropertize = intern_c_string (":propertize");
25657 staticpro (&QCpropertize);
25658 QCfile = intern_c_string (":file");
25659 staticpro (&QCfile);
25660 Qfontified = intern_c_string ("fontified");
25661 staticpro (&Qfontified);
25662 Qfontification_functions = intern_c_string ("fontification-functions");
25663 staticpro (&Qfontification_functions);
25664 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
25665 staticpro (&Qtrailing_whitespace);
25666 Qescape_glyph = intern_c_string ("escape-glyph");
25667 staticpro (&Qescape_glyph);
25668 Qnobreak_space = intern_c_string ("nobreak-space");
25669 staticpro (&Qnobreak_space);
25670 Qimage = intern_c_string ("image");
25671 staticpro (&Qimage);
25672 Qtext = intern_c_string ("text");
25673 staticpro (&Qtext);
25674 Qboth = intern_c_string ("both");
25675 staticpro (&Qboth);
25676 Qboth_horiz = intern_c_string ("both-horiz");
25677 staticpro (&Qboth_horiz);
25678 Qtext_image_horiz = intern_c_string ("text-image-horiz");
25679 staticpro (&Qtext_image_horiz);
25680 QCmap = intern_c_string (":map");
25681 staticpro (&QCmap);
25682 QCpointer = intern_c_string (":pointer");
25683 staticpro (&QCpointer);
25684 Qrect = intern_c_string ("rect");
25685 staticpro (&Qrect);
25686 Qcircle = intern_c_string ("circle");
25687 staticpro (&Qcircle);
25688 Qpoly = intern_c_string ("poly");
25689 staticpro (&Qpoly);
25690 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
25691 staticpro (&Qmessage_truncate_lines);
25692 Qgrow_only = intern_c_string ("grow-only");
25693 staticpro (&Qgrow_only);
25694 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
25695 staticpro (&Qinhibit_menubar_update);
25696 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
25697 staticpro (&Qinhibit_eval_during_redisplay);
25698 Qposition = intern_c_string ("position");
25699 staticpro (&Qposition);
25700 Qbuffer_position = intern_c_string ("buffer-position");
25701 staticpro (&Qbuffer_position);
25702 Qobject = intern_c_string ("object");
25703 staticpro (&Qobject);
25704 Qbar = intern_c_string ("bar");
25705 staticpro (&Qbar);
25706 Qhbar = intern_c_string ("hbar");
25707 staticpro (&Qhbar);
25708 Qbox = intern_c_string ("box");
25709 staticpro (&Qbox);
25710 Qhollow = intern_c_string ("hollow");
25711 staticpro (&Qhollow);
25712 Qhand = intern_c_string ("hand");
25713 staticpro (&Qhand);
25714 Qarrow = intern_c_string ("arrow");
25715 staticpro (&Qarrow);
25716 Qtext = intern_c_string ("text");
25717 staticpro (&Qtext);
25718 Qrisky_local_variable = intern_c_string ("risky-local-variable");
25719 staticpro (&Qrisky_local_variable);
25720 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
25721 staticpro (&Qinhibit_free_realized_faces);
25722
25723 list_of_error = Fcons (Fcons (intern_c_string ("error"),
25724 Fcons (intern_c_string ("void-variable"), Qnil)),
25725 Qnil);
25726 staticpro (&list_of_error);
25727
25728 Qlast_arrow_position = intern_c_string ("last-arrow-position");
25729 staticpro (&Qlast_arrow_position);
25730 Qlast_arrow_string = intern_c_string ("last-arrow-string");
25731 staticpro (&Qlast_arrow_string);
25732
25733 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
25734 staticpro (&Qoverlay_arrow_string);
25735 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
25736 staticpro (&Qoverlay_arrow_bitmap);
25737
25738 echo_buffer[0] = echo_buffer[1] = Qnil;
25739 staticpro (&echo_buffer[0]);
25740 staticpro (&echo_buffer[1]);
25741
25742 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
25743 staticpro (&echo_area_buffer[0]);
25744 staticpro (&echo_area_buffer[1]);
25745
25746 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
25747 staticpro (&Vmessages_buffer_name);
25748
25749 mode_line_proptrans_alist = Qnil;
25750 staticpro (&mode_line_proptrans_alist);
25751 mode_line_string_list = Qnil;
25752 staticpro (&mode_line_string_list);
25753 mode_line_string_face = Qnil;
25754 staticpro (&mode_line_string_face);
25755 mode_line_string_face_prop = Qnil;
25756 staticpro (&mode_line_string_face_prop);
25757 Vmode_line_unwind_vector = Qnil;
25758 staticpro (&Vmode_line_unwind_vector);
25759
25760 help_echo_string = Qnil;
25761 staticpro (&help_echo_string);
25762 help_echo_object = Qnil;
25763 staticpro (&help_echo_object);
25764 help_echo_window = Qnil;
25765 staticpro (&help_echo_window);
25766 previous_help_echo_string = Qnil;
25767 staticpro (&previous_help_echo_string);
25768 help_echo_pos = -1;
25769
25770 Qright_to_left = intern_c_string ("right-to-left");
25771 staticpro (&Qright_to_left);
25772 Qleft_to_right = intern_c_string ("left-to-right");
25773 staticpro (&Qleft_to_right);
25774
25775 #ifdef HAVE_WINDOW_SYSTEM
25776 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
25777 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
25778 For example, if a block cursor is over a tab, it will be drawn as
25779 wide as that tab on the display. */);
25780 x_stretch_cursor_p = 0;
25781 #endif
25782
25783 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
25784 doc: /* *Non-nil means highlight trailing whitespace.
25785 The face used for trailing whitespace is `trailing-whitespace'. */);
25786 Vshow_trailing_whitespace = Qnil;
25787
25788 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
25789 doc: /* *Control highlighting of nobreak space and soft hyphen.
25790 A value of t means highlight the character itself (for nobreak space,
25791 use face `nobreak-space').
25792 A value of nil means no highlighting.
25793 Other values mean display the escape glyph followed by an ordinary
25794 space or ordinary hyphen. */);
25795 Vnobreak_char_display = Qt;
25796
25797 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
25798 doc: /* *The pointer shape to show in void text areas.
25799 A value of nil means to show the text pointer. Other options are `arrow',
25800 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
25801 Vvoid_text_area_pointer = Qarrow;
25802
25803 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
25804 doc: /* Non-nil means don't actually do any redisplay.
25805 This is used for internal purposes. */);
25806 Vinhibit_redisplay = Qnil;
25807
25808 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
25809 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
25810 Vglobal_mode_string = Qnil;
25811
25812 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
25813 doc: /* Marker for where to display an arrow on top of the buffer text.
25814 This must be the beginning of a line in order to work.
25815 See also `overlay-arrow-string'. */);
25816 Voverlay_arrow_position = Qnil;
25817
25818 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
25819 doc: /* String to display as an arrow in non-window frames.
25820 See also `overlay-arrow-position'. */);
25821 Voverlay_arrow_string = make_pure_c_string ("=>");
25822
25823 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
25824 doc: /* List of variables (symbols) which hold markers for overlay arrows.
25825 The symbols on this list are examined during redisplay to determine
25826 where to display overlay arrows. */);
25827 Voverlay_arrow_variable_list
25828 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
25829
25830 DEFVAR_INT ("scroll-step", &scroll_step,
25831 doc: /* *The number of lines to try scrolling a window by when point moves out.
25832 If that fails to bring point back on frame, point is centered instead.
25833 If this is zero, point is always centered after it moves off frame.
25834 If you want scrolling to always be a line at a time, you should set
25835 `scroll-conservatively' to a large value rather than set this to 1. */);
25836
25837 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
25838 doc: /* *Scroll up to this many lines, to bring point back on screen.
25839 If point moves off-screen, redisplay will scroll by up to
25840 `scroll-conservatively' lines in order to bring point just barely
25841 onto the screen again. If that cannot be done, then redisplay
25842 recenters point as usual.
25843
25844 A value of zero means always recenter point if it moves off screen. */);
25845 scroll_conservatively = 0;
25846
25847 DEFVAR_INT ("scroll-margin", &scroll_margin,
25848 doc: /* *Number of lines of margin at the top and bottom of a window.
25849 Recenter the window whenever point gets within this many lines
25850 of the top or bottom of the window. */);
25851 scroll_margin = 0;
25852
25853 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
25854 doc: /* Pixels per inch value for non-window system displays.
25855 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
25856 Vdisplay_pixels_per_inch = make_float (72.0);
25857
25858 #if GLYPH_DEBUG
25859 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
25860 #endif
25861
25862 DEFVAR_LISP ("truncate-partial-width-windows",
25863 &Vtruncate_partial_width_windows,
25864 doc: /* Non-nil means truncate lines in windows narrower than the frame.
25865 For an integer value, truncate lines in each window narrower than the
25866 full frame width, provided the window width is less than that integer;
25867 otherwise, respect the value of `truncate-lines'.
25868
25869 For any other non-nil value, truncate lines in all windows that do
25870 not span the full frame width.
25871
25872 A value of nil means to respect the value of `truncate-lines'.
25873
25874 If `word-wrap' is enabled, you might want to reduce this. */);
25875 Vtruncate_partial_width_windows = make_number (50);
25876
25877 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
25878 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
25879 Any other value means to use the appropriate face, `mode-line',
25880 `header-line', or `menu' respectively. */);
25881 mode_line_inverse_video = 1;
25882
25883 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
25884 doc: /* *Maximum buffer size for which line number should be displayed.
25885 If the buffer is bigger than this, the line number does not appear
25886 in the mode line. A value of nil means no limit. */);
25887 Vline_number_display_limit = Qnil;
25888
25889 DEFVAR_INT ("line-number-display-limit-width",
25890 &line_number_display_limit_width,
25891 doc: /* *Maximum line width (in characters) for line number display.
25892 If the average length of the lines near point is bigger than this, then the
25893 line number may be omitted from the mode line. */);
25894 line_number_display_limit_width = 200;
25895
25896 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
25897 doc: /* *Non-nil means highlight region even in nonselected windows. */);
25898 highlight_nonselected_windows = 0;
25899
25900 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
25901 doc: /* Non-nil if more than one frame is visible on this display.
25902 Minibuffer-only frames don't count, but iconified frames do.
25903 This variable is not guaranteed to be accurate except while processing
25904 `frame-title-format' and `icon-title-format'. */);
25905
25906 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
25907 doc: /* Template for displaying the title bar of visible frames.
25908 \(Assuming the window manager supports this feature.)
25909
25910 This variable has the same structure as `mode-line-format', except that
25911 the %c and %l constructs are ignored. It is used only on frames for
25912 which no explicit name has been set \(see `modify-frame-parameters'). */);
25913
25914 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
25915 doc: /* Template for displaying the title bar of an iconified frame.
25916 \(Assuming the window manager supports this feature.)
25917 This variable has the same structure as `mode-line-format' (which see),
25918 and is used only on frames for which no explicit name has been set
25919 \(see `modify-frame-parameters'). */);
25920 Vicon_title_format
25921 = Vframe_title_format
25922 = pure_cons (intern_c_string ("multiple-frames"),
25923 pure_cons (make_pure_c_string ("%b"),
25924 pure_cons (pure_cons (empty_unibyte_string,
25925 pure_cons (intern_c_string ("invocation-name"),
25926 pure_cons (make_pure_c_string ("@"),
25927 pure_cons (intern_c_string ("system-name"),
25928 Qnil)))),
25929 Qnil)));
25930
25931 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
25932 doc: /* Maximum number of lines to keep in the message log buffer.
25933 If nil, disable message logging. If t, log messages but don't truncate
25934 the buffer when it becomes large. */);
25935 Vmessage_log_max = make_number (100);
25936
25937 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
25938 doc: /* Functions called before redisplay, if window sizes have changed.
25939 The value should be a list of functions that take one argument.
25940 Just before redisplay, for each frame, if any of its windows have changed
25941 size since the last redisplay, or have been split or deleted,
25942 all the functions in the list are called, with the frame as argument. */);
25943 Vwindow_size_change_functions = Qnil;
25944
25945 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
25946 doc: /* List of functions to call before redisplaying a window with scrolling.
25947 Each function is called with two arguments, the window and its new
25948 display-start position. Note that these functions are also called by
25949 `set-window-buffer'. Also note that the value of `window-end' is not
25950 valid when these functions are called. */);
25951 Vwindow_scroll_functions = Qnil;
25952
25953 DEFVAR_LISP ("window-text-change-functions",
25954 &Vwindow_text_change_functions,
25955 doc: /* Functions to call in redisplay when text in the window might change. */);
25956 Vwindow_text_change_functions = Qnil;
25957
25958 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
25959 doc: /* Functions called when redisplay of a window reaches the end trigger.
25960 Each function is called with two arguments, the window and the end trigger value.
25961 See `set-window-redisplay-end-trigger'. */);
25962 Vredisplay_end_trigger_functions = Qnil;
25963
25964 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25965 doc: /* *Non-nil means autoselect window with mouse pointer.
25966 If nil, do not autoselect windows.
25967 A positive number means delay autoselection by that many seconds: a
25968 window is autoselected only after the mouse has remained in that
25969 window for the duration of the delay.
25970 A negative number has a similar effect, but causes windows to be
25971 autoselected only after the mouse has stopped moving. \(Because of
25972 the way Emacs compares mouse events, you will occasionally wait twice
25973 that time before the window gets selected.\)
25974 Any other value means to autoselect window instantaneously when the
25975 mouse pointer enters it.
25976
25977 Autoselection selects the minibuffer only if it is active, and never
25978 unselects the minibuffer if it is active.
25979
25980 When customizing this variable make sure that the actual value of
25981 `focus-follows-mouse' matches the behavior of your window manager. */);
25982 Vmouse_autoselect_window = Qnil;
25983
25984 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25985 doc: /* *Non-nil means automatically resize tool-bars.
25986 This dynamically changes the tool-bar's height to the minimum height
25987 that is needed to make all tool-bar items visible.
25988 If value is `grow-only', the tool-bar's height is only increased
25989 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25990 Vauto_resize_tool_bars = Qt;
25991
25992 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25993 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25994 auto_raise_tool_bar_buttons_p = 1;
25995
25996 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25997 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25998 make_cursor_line_fully_visible_p = 1;
25999
26000 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
26001 doc: /* *Border below tool-bar in pixels.
26002 If an integer, use it as the height of the border.
26003 If it is one of `internal-border-width' or `border-width', use the
26004 value of the corresponding frame parameter.
26005 Otherwise, no border is added below the tool-bar. */);
26006 Vtool_bar_border = Qinternal_border_width;
26007
26008 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
26009 doc: /* *Margin around tool-bar buttons in pixels.
26010 If an integer, use that for both horizontal and vertical margins.
26011 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
26012 HORZ specifying the horizontal margin, and VERT specifying the
26013 vertical margin. */);
26014 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
26015
26016 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
26017 doc: /* *Relief thickness of tool-bar buttons. */);
26018 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
26019
26020 DEFVAR_LISP ("tool-bar-style", &Vtool_bar_style,
26021 doc: /* *Tool bar style to use.
26022 It can be one of
26023 image - show images only
26024 text - show text only
26025 both - show both, text below image
26026 both-horiz - show text to the right of the image
26027 text-image-horiz - show text to the left of the image
26028 any other - use system default or image if no system default. */);
26029 Vtool_bar_style = Qnil;
26030
26031 DEFVAR_INT ("tool-bar-max-label-size", &tool_bar_max_label_size,
26032 doc: /* *Maximum number of characters a label can have to be shown.
26033 The tool bar style must also show labels for this to have any effect, see
26034 `tool-bar-style'. */);
26035 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
26036
26037 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
26038 doc: /* List of functions to call to fontify regions of text.
26039 Each function is called with one argument POS. Functions must
26040 fontify a region starting at POS in the current buffer, and give
26041 fontified regions the property `fontified'. */);
26042 Vfontification_functions = Qnil;
26043 Fmake_variable_buffer_local (Qfontification_functions);
26044
26045 DEFVAR_BOOL ("unibyte-display-via-language-environment",
26046 &unibyte_display_via_language_environment,
26047 doc: /* *Non-nil means display unibyte text according to language environment.
26048 Specifically, this means that raw bytes in the range 160-255 decimal
26049 are displayed by converting them to the equivalent multibyte characters
26050 according to the current language environment. As a result, they are
26051 displayed according to the current fontset.
26052
26053 Note that this variable affects only how these bytes are displayed,
26054 but does not change the fact they are interpreted as raw bytes. */);
26055 unibyte_display_via_language_environment = 0;
26056
26057 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
26058 doc: /* *Maximum height for resizing mini-windows.
26059 If a float, it specifies a fraction of the mini-window frame's height.
26060 If an integer, it specifies a number of lines. */);
26061 Vmax_mini_window_height = make_float (0.25);
26062
26063 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
26064 doc: /* *How to resize mini-windows.
26065 A value of nil means don't automatically resize mini-windows.
26066 A value of t means resize them to fit the text displayed in them.
26067 A value of `grow-only', the default, means let mini-windows grow
26068 only, until their display becomes empty, at which point the windows
26069 go back to their normal size. */);
26070 Vresize_mini_windows = Qgrow_only;
26071
26072 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
26073 doc: /* Alist specifying how to blink the cursor off.
26074 Each element has the form (ON-STATE . OFF-STATE). Whenever the
26075 `cursor-type' frame-parameter or variable equals ON-STATE,
26076 comparing using `equal', Emacs uses OFF-STATE to specify
26077 how to blink it off. ON-STATE and OFF-STATE are values for
26078 the `cursor-type' frame parameter.
26079
26080 If a frame's ON-STATE has no entry in this list,
26081 the frame's other specifications determine how to blink the cursor off. */);
26082 Vblink_cursor_alist = Qnil;
26083
26084 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
26085 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
26086 automatic_hscrolling_p = 1;
26087 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
26088 staticpro (&Qauto_hscroll_mode);
26089
26090 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
26091 doc: /* *How many columns away from the window edge point is allowed to get
26092 before automatic hscrolling will horizontally scroll the window. */);
26093 hscroll_margin = 5;
26094
26095 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
26096 doc: /* *How many columns to scroll the window when point gets too close to the edge.
26097 When point is less than `hscroll-margin' columns from the window
26098 edge, automatic hscrolling will scroll the window by the amount of columns
26099 determined by this variable. If its value is a positive integer, scroll that
26100 many columns. If it's a positive floating-point number, it specifies the
26101 fraction of the window's width to scroll. If it's nil or zero, point will be
26102 centered horizontally after the scroll. Any other value, including negative
26103 numbers, are treated as if the value were zero.
26104
26105 Automatic hscrolling always moves point outside the scroll margin, so if
26106 point was more than scroll step columns inside the margin, the window will
26107 scroll more than the value given by the scroll step.
26108
26109 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26110 and `scroll-right' overrides this variable's effect. */);
26111 Vhscroll_step = make_number (0);
26112
26113 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
26114 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26115 Bind this around calls to `message' to let it take effect. */);
26116 message_truncate_lines = 0;
26117
26118 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
26119 doc: /* Normal hook run to update the menu bar definitions.
26120 Redisplay runs this hook before it redisplays the menu bar.
26121 This is used to update submenus such as Buffers,
26122 whose contents depend on various data. */);
26123 Vmenu_bar_update_hook = Qnil;
26124
26125 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
26126 doc: /* Frame for which we are updating a menu.
26127 The enable predicate for a menu binding should check this variable. */);
26128 Vmenu_updating_frame = Qnil;
26129
26130 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
26131 doc: /* Non-nil means don't update menu bars. Internal use only. */);
26132 inhibit_menubar_update = 0;
26133
26134 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
26135 doc: /* Prefix prepended to all continuation lines at display time.
26136 The value may be a string, an image, or a stretch-glyph; it is
26137 interpreted in the same way as the value of a `display' text property.
26138
26139 This variable is overridden by any `wrap-prefix' text or overlay
26140 property.
26141
26142 To add a prefix to non-continuation lines, use `line-prefix'. */);
26143 Vwrap_prefix = Qnil;
26144 staticpro (&Qwrap_prefix);
26145 Qwrap_prefix = intern_c_string ("wrap-prefix");
26146 Fmake_variable_buffer_local (Qwrap_prefix);
26147
26148 DEFVAR_LISP ("line-prefix", &Vline_prefix,
26149 doc: /* Prefix prepended to all non-continuation lines at display time.
26150 The value may be a string, an image, or a stretch-glyph; it is
26151 interpreted in the same way as the value of a `display' text property.
26152
26153 This variable is overridden by any `line-prefix' text or overlay
26154 property.
26155
26156 To add a prefix to continuation lines, use `wrap-prefix'. */);
26157 Vline_prefix = Qnil;
26158 staticpro (&Qline_prefix);
26159 Qline_prefix = intern_c_string ("line-prefix");
26160 Fmake_variable_buffer_local (Qline_prefix);
26161
26162 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
26163 doc: /* Non-nil means don't eval Lisp during redisplay. */);
26164 inhibit_eval_during_redisplay = 0;
26165
26166 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
26167 doc: /* Non-nil means don't free realized faces. Internal use only. */);
26168 inhibit_free_realized_faces = 0;
26169
26170 #if GLYPH_DEBUG
26171 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
26172 doc: /* Inhibit try_window_id display optimization. */);
26173 inhibit_try_window_id = 0;
26174
26175 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
26176 doc: /* Inhibit try_window_reusing display optimization. */);
26177 inhibit_try_window_reusing = 0;
26178
26179 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
26180 doc: /* Inhibit try_cursor_movement display optimization. */);
26181 inhibit_try_cursor_movement = 0;
26182 #endif /* GLYPH_DEBUG */
26183
26184 DEFVAR_INT ("overline-margin", &overline_margin,
26185 doc: /* *Space between overline and text, in pixels.
26186 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
26187 margin to the caracter height. */);
26188 overline_margin = 2;
26189
26190 DEFVAR_INT ("underline-minimum-offset",
26191 &underline_minimum_offset,
26192 doc: /* Minimum distance between baseline and underline.
26193 This can improve legibility of underlined text at small font sizes,
26194 particularly when using variable `x-use-underline-position-properties'
26195 with fonts that specify an UNDERLINE_POSITION relatively close to the
26196 baseline. The default value is 1. */);
26197 underline_minimum_offset = 1;
26198
26199 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
26200 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
26201 display_hourglass_p = 1;
26202
26203 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
26204 doc: /* *Seconds to wait before displaying an hourglass pointer.
26205 Value must be an integer or float. */);
26206 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
26207
26208 hourglass_atimer = NULL;
26209 hourglass_shown_p = 0;
26210 }
26211
26212
26213 /* Initialize this module when Emacs starts. */
26214
26215 void
26216 init_xdisp (void)
26217 {
26218 Lisp_Object root_window;
26219 struct window *mini_w;
26220
26221 current_header_line_height = current_mode_line_height = -1;
26222
26223 CHARPOS (this_line_start_pos) = 0;
26224
26225 mini_w = XWINDOW (minibuf_window);
26226 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
26227
26228 if (!noninteractive)
26229 {
26230 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
26231 int i;
26232
26233 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
26234 set_window_height (root_window,
26235 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
26236 0);
26237 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
26238 set_window_height (minibuf_window, 1, 0);
26239
26240 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
26241 mini_w->total_cols = make_number (FRAME_COLS (f));
26242
26243 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
26244 scratch_glyph_row.glyphs[TEXT_AREA + 1]
26245 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
26246
26247 /* The default ellipsis glyphs `...'. */
26248 for (i = 0; i < 3; ++i)
26249 default_invis_vector[i] = make_number ('.');
26250 }
26251
26252 {
26253 /* Allocate the buffer for frame titles.
26254 Also used for `format-mode-line'. */
26255 int size = 100;
26256 mode_line_noprop_buf = (char *) xmalloc (size);
26257 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
26258 mode_line_noprop_ptr = mode_line_noprop_buf;
26259 mode_line_target = MODE_LINE_DISPLAY;
26260 }
26261
26262 help_echo_showing_p = 0;
26263 }
26264
26265 /* Since w32 does not support atimers, it defines its own implementation of
26266 the following three functions in w32fns.c. */
26267 #ifndef WINDOWSNT
26268
26269 /* Platform-independent portion of hourglass implementation. */
26270
26271 /* Return non-zero if houglass timer has been started or hourglass is shown. */
26272 int
26273 hourglass_started (void)
26274 {
26275 return hourglass_shown_p || hourglass_atimer != NULL;
26276 }
26277
26278 /* Cancel a currently active hourglass timer, and start a new one. */
26279 void
26280 start_hourglass (void)
26281 {
26282 #if defined (HAVE_WINDOW_SYSTEM)
26283 EMACS_TIME delay;
26284 int secs, usecs = 0;
26285
26286 cancel_hourglass ();
26287
26288 if (INTEGERP (Vhourglass_delay)
26289 && XINT (Vhourglass_delay) > 0)
26290 secs = XFASTINT (Vhourglass_delay);
26291 else if (FLOATP (Vhourglass_delay)
26292 && XFLOAT_DATA (Vhourglass_delay) > 0)
26293 {
26294 Lisp_Object tem;
26295 tem = Ftruncate (Vhourglass_delay, Qnil);
26296 secs = XFASTINT (tem);
26297 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
26298 }
26299 else
26300 secs = DEFAULT_HOURGLASS_DELAY;
26301
26302 EMACS_SET_SECS_USECS (delay, secs, usecs);
26303 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
26304 show_hourglass, NULL);
26305 #endif
26306 }
26307
26308
26309 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
26310 shown. */
26311 void
26312 cancel_hourglass (void)
26313 {
26314 #if defined (HAVE_WINDOW_SYSTEM)
26315 if (hourglass_atimer)
26316 {
26317 cancel_atimer (hourglass_atimer);
26318 hourglass_atimer = NULL;
26319 }
26320
26321 if (hourglass_shown_p)
26322 hide_hourglass ();
26323 #endif
26324 }
26325 #endif /* ! WINDOWSNT */
26326
26327 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
26328 (do not change this comment) */