Merge changes from emacs-23 branch
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5 2010 Free Software Foundation, Inc.
6
7 This file is part of GNU Emacs.
8
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code.
36
37 The following diagram shows how redisplay code is invoked. As you
38 can see, Lisp calls redisplay and vice versa. Under window systems
39 like X, some portions of the redisplay code are also called
40 asynchronously during mouse movement or expose events. It is very
41 important that these code parts do NOT use the C library (malloc,
42 free) because many C libraries under Unix are not reentrant. They
43 may also NOT call functions of the Lisp interpreter which could
44 change the interpreter's state. If you don't follow these rules,
45 you will encounter bugs which are very hard to explain.
46
47 +--------------+ redisplay +----------------+
48 | Lisp machine |---------------->| Redisplay code |<--+
49 +--------------+ (xdisp.c) +----------------+ |
50 ^ | |
51 +----------------------------------+ |
52 Don't use this path when called |
53 asynchronously! |
54 |
55 expose_window (asynchronous) |
56 |
57 X expose events -----+
58
59 What does redisplay do? Obviously, it has to figure out somehow what
60 has been changed since the last time the display has been updated,
61 and to make these changes visible. Preferably it would do that in
62 a moderately intelligent way, i.e. fast.
63
64 Changes in buffer text can be deduced from window and buffer
65 structures, and from some global variables like `beg_unchanged' and
66 `end_unchanged'. The contents of the display are additionally
67 recorded in a `glyph matrix', a two-dimensional matrix of glyph
68 structures. Each row in such a matrix corresponds to a line on the
69 display, and each glyph in a row corresponds to a column displaying
70 a character, an image, or what else. This matrix is called the
71 `current glyph matrix' or `current matrix' in redisplay
72 terminology.
73
74 For buffer parts that have been changed since the last update, a
75 second glyph matrix is constructed, the so called `desired glyph
76 matrix' or short `desired matrix'. Current and desired matrix are
77 then compared to find a cheap way to update the display, e.g. by
78 reusing part of the display by scrolling lines.
79
80 You will find a lot of redisplay optimizations when you start
81 looking at the innards of redisplay. The overall goal of all these
82 optimizations is to make redisplay fast because it is done
83 frequently. Some of these optimizations are implemented by the
84 following functions:
85
86 . try_cursor_movement
87
88 This function tries to update the display if the text in the
89 window did not change and did not scroll, only point moved, and
90 it did not move off the displayed portion of the text.
91
92 . try_window_reusing_current_matrix
93
94 This function reuses the current matrix of a window when text
95 has not changed, but the window start changed (e.g., due to
96 scrolling).
97
98 . try_window_id
99
100 This function attempts to redisplay a window by reusing parts of
101 its existing display. It finds and reuses the part that was not
102 changed, and redraws the rest.
103
104 . try_window
105
106 This function performs the full redisplay of a single window
107 assuming that its fonts were not changed and that the cursor
108 will not end up in the scroll margins. (Loading fonts requires
109 re-adjustment of dimensions of glyph matrices, which makes this
110 method impossible to use.)
111
112 These optimizations are tried in sequence (some can be skipped if
113 it is known that they are not applicable). If none of the
114 optimizations were successful, redisplay calls redisplay_windows,
115 which performs a full redisplay of all windows.
116
117 Desired matrices.
118
119 Desired matrices are always built per Emacs window. The function
120 `display_line' is the central function to look at if you are
121 interested. It constructs one row in a desired matrix given an
122 iterator structure containing both a buffer position and a
123 description of the environment in which the text is to be
124 displayed. But this is too early, read on.
125
126 Characters and pixmaps displayed for a range of buffer text depend
127 on various settings of buffers and windows, on overlays and text
128 properties, on display tables, on selective display. The good news
129 is that all this hairy stuff is hidden behind a small set of
130 interface functions taking an iterator structure (struct it)
131 argument.
132
133 Iteration over things to be displayed is then simple. It is
134 started by initializing an iterator with a call to init_iterator.
135 Calls to get_next_display_element fill the iterator structure with
136 relevant information about the next thing to display. Calls to
137 set_iterator_to_next move the iterator to the next thing.
138
139 Besides this, an iterator also contains information about the
140 display environment in which glyphs for display elements are to be
141 produced. It has fields for the width and height of the display,
142 the information whether long lines are truncated or continued, a
143 current X and Y position, and lots of other stuff you can better
144 see in dispextern.h.
145
146 Glyphs in a desired matrix are normally constructed in a loop
147 calling get_next_display_element and then PRODUCE_GLYPHS. The call
148 to PRODUCE_GLYPHS will fill the iterator structure with pixel
149 information about the element being displayed and at the same time
150 produce glyphs for it. If the display element fits on the line
151 being displayed, set_iterator_to_next is called next, otherwise the
152 glyphs produced are discarded. The function display_line is the
153 workhorse of filling glyph rows in the desired matrix with glyphs.
154 In addition to producing glyphs, it also handles line truncation
155 and continuation, word wrap, and cursor positioning (for the
156 latter, see also set_cursor_from_row).
157
158 Frame matrices.
159
160 That just couldn't be all, could it? What about terminal types not
161 supporting operations on sub-windows of the screen? To update the
162 display on such a terminal, window-based glyph matrices are not
163 well suited. To be able to reuse part of the display (scrolling
164 lines up and down), we must instead have a view of the whole
165 screen. This is what `frame matrices' are for. They are a trick.
166
167 Frames on terminals like above have a glyph pool. Windows on such
168 a frame sub-allocate their glyph memory from their frame's glyph
169 pool. The frame itself is given its own glyph matrices. By
170 coincidence---or maybe something else---rows in window glyph
171 matrices are slices of corresponding rows in frame matrices. Thus
172 writing to window matrices implicitly updates a frame matrix which
173 provides us with the view of the whole screen that we originally
174 wanted to have without having to move many bytes around. To be
175 honest, there is a little bit more done, but not much more. If you
176 plan to extend that code, take a look at dispnew.c. The function
177 build_frame_matrix is a good starting point.
178
179 Bidirectional display.
180
181 Bidirectional display adds quite some hair to this already complex
182 design. The good news are that a large portion of that hairy stuff
183 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
184 reordering engine which is called by set_iterator_to_next and
185 returns the next character to display in the visual order. See
186 commentary on bidi.c for more details. As far as redisplay is
187 concerned, the effect of calling bidi_move_to_visually_next, the
188 main interface of the reordering engine, is that the iterator gets
189 magically placed on the buffer or string position that is to be
190 displayed next. In other words, a linear iteration through the
191 buffer/string is replaced with a non-linear one. All the rest of
192 the redisplay is oblivious to the bidi reordering.
193
194 Well, almost oblivious---there are still complications, most of
195 them due to the fact that buffer and string positions no longer
196 change monotonously with glyph indices in a glyph row. Moreover,
197 for continued lines, the buffer positions may not even be
198 monotonously changing with vertical positions. Also, accounting
199 for face changes, overlays, etc. becomes more complex because
200 non-linear iteration could potentially skip many positions with
201 changes, and then cross them again on the way back...
202
203 One other prominent effect of bidirectional display is that some
204 paragraphs of text need to be displayed starting at the right
205 margin of the window---the so-called right-to-left, or R2L
206 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
207 which have their reversed_p flag set. The bidi reordering engine
208 produces characters in such rows starting from the character which
209 should be the rightmost on display. PRODUCE_GLYPHS then reverses
210 the order, when it fills up the glyph row whose reversed_p flag is
211 set, by prepending each new glyph to what is already there, instead
212 of appending it. When the glyph row is complete, the function
213 extend_face_to_end_of_line fills the empty space to the left of the
214 leftmost character with special glyphs, which will display as,
215 well, empty. On text terminals, these special glyphs are simply
216 blank characters. On graphics terminals, there's a single stretch
217 glyph 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 EMACS_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 *,
960 EMACS_INT, EMACS_INT);
961 static void store_mode_line_noprop_char (char);
962 static int store_mode_line_noprop (const unsigned char *, int, int);
963 static void x_consider_frame_title (Lisp_Object);
964 static void handle_stop (struct it *);
965 static void handle_stop_backwards (struct it *, EMACS_INT);
966 static int tool_bar_lines_needed (struct frame *, int *);
967 static int single_display_spec_intangible_p (Lisp_Object);
968 static void ensure_echo_area_buffers (void);
969 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
970 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
971 static int with_echo_area_buffer (struct window *, int,
972 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
973 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
974 static void clear_garbaged_frames (void);
975 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
976 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
977 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
978 static int display_echo_area (struct window *);
979 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
980 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
981 static Lisp_Object unwind_redisplay (Lisp_Object);
982 static int string_char_and_length (const unsigned char *, int *);
983 static struct text_pos display_prop_end (struct it *, Lisp_Object,
984 struct text_pos);
985 static int compute_window_start_on_continuation_line (struct window *);
986 static Lisp_Object safe_eval_handler (Lisp_Object);
987 static void insert_left_trunc_glyphs (struct it *);
988 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
989 Lisp_Object);
990 static void extend_face_to_end_of_line (struct it *);
991 static int append_space_for_newline (struct it *, int);
992 static int cursor_row_fully_visible_p (struct window *, int, int);
993 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
994 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
995 static int trailing_whitespace_p (EMACS_INT);
996 static int message_log_check_duplicate (EMACS_INT, EMACS_INT,
997 EMACS_INT, EMACS_INT);
998 static void push_it (struct it *);
999 static void pop_it (struct it *);
1000 static void sync_frame_with_window_matrix_rows (struct window *);
1001 static void select_frame_for_redisplay (Lisp_Object);
1002 static void redisplay_internal (int);
1003 static int echo_area_display (int);
1004 static void redisplay_windows (Lisp_Object);
1005 static void redisplay_window (Lisp_Object, int);
1006 static Lisp_Object redisplay_window_error (Lisp_Object);
1007 static Lisp_Object redisplay_window_0 (Lisp_Object);
1008 static Lisp_Object redisplay_window_1 (Lisp_Object);
1009 static int update_menu_bar (struct frame *, int, int);
1010 static int try_window_reusing_current_matrix (struct window *);
1011 static int try_window_id (struct window *);
1012 static int display_line (struct it *);
1013 static int display_mode_lines (struct window *);
1014 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
1015 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
1016 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
1017 static const char *decode_mode_spec (struct window *, int, int, int,
1018 Lisp_Object *);
1019 static void display_menu_bar (struct window *);
1020 static int display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT, int,
1021 EMACS_INT *);
1022 static int display_string (const unsigned char *, Lisp_Object, Lisp_Object,
1023 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
1024 static void compute_line_metrics (struct it *);
1025 static void run_redisplay_end_trigger_hook (struct it *);
1026 static int get_overlay_strings (struct it *, EMACS_INT);
1027 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
1028 static void next_overlay_string (struct it *);
1029 static void reseat (struct it *, struct text_pos, int);
1030 static void reseat_1 (struct it *, struct text_pos, int);
1031 static void back_to_previous_visible_line_start (struct it *);
1032 void reseat_at_previous_visible_line_start (struct it *);
1033 static void reseat_at_next_visible_line_start (struct it *, int);
1034 static int next_element_from_ellipsis (struct it *);
1035 static int next_element_from_display_vector (struct it *);
1036 static int next_element_from_string (struct it *);
1037 static int next_element_from_c_string (struct it *);
1038 static int next_element_from_buffer (struct it *);
1039 static int next_element_from_composition (struct it *);
1040 static int next_element_from_image (struct it *);
1041 static int next_element_from_stretch (struct it *);
1042 static void load_overlay_strings (struct it *, EMACS_INT);
1043 static int init_from_display_pos (struct it *, struct window *,
1044 struct display_pos *);
1045 static void reseat_to_string (struct it *, const unsigned char *,
1046 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
1047 static enum move_it_result
1048 move_it_in_display_line_to (struct it *, EMACS_INT, int,
1049 enum move_operation_enum);
1050 void move_it_vertically_backward (struct it *, int);
1051 static void init_to_row_start (struct it *, struct window *,
1052 struct glyph_row *);
1053 static int init_to_row_end (struct it *, struct window *,
1054 struct glyph_row *);
1055 static void back_to_previous_line_start (struct it *);
1056 static int forward_to_next_line_start (struct it *, int *);
1057 static struct text_pos string_pos_nchars_ahead (struct text_pos,
1058 Lisp_Object, EMACS_INT);
1059 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
1060 static struct text_pos c_string_pos (EMACS_INT, const unsigned char *, int);
1061 static EMACS_INT number_of_chars (const unsigned char *, int);
1062 static void compute_stop_pos (struct it *);
1063 static void compute_string_pos (struct text_pos *, struct text_pos,
1064 Lisp_Object);
1065 static int face_before_or_after_it_pos (struct it *, int);
1066 static EMACS_INT next_overlay_change (EMACS_INT);
1067 static int handle_single_display_spec (struct it *, Lisp_Object,
1068 Lisp_Object, Lisp_Object,
1069 struct text_pos *, int);
1070 static int underlying_face_id (struct it *);
1071 static int in_ellipses_for_invisible_text_p (struct display_pos *,
1072 struct window *);
1073
1074 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1075 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1076
1077 #ifdef HAVE_WINDOW_SYSTEM
1078
1079 static void update_tool_bar (struct frame *, int);
1080 static void build_desired_tool_bar_string (struct frame *f);
1081 static int redisplay_tool_bar (struct frame *);
1082 static void display_tool_bar_line (struct it *, int);
1083 static void notice_overwritten_cursor (struct window *,
1084 enum glyph_row_area,
1085 int, int, int, int);
1086 static void append_stretch_glyph (struct it *, Lisp_Object,
1087 int, int, int);
1088 static int coords_in_mouse_face_p (struct window *, int, int);
1089
1090
1091
1092 #endif /* HAVE_WINDOW_SYSTEM */
1093
1094 \f
1095 /***********************************************************************
1096 Window display dimensions
1097 ***********************************************************************/
1098
1099 /* Return the bottom boundary y-position for text lines in window W.
1100 This is the first y position at which a line cannot start.
1101 It is relative to the top of the window.
1102
1103 This is the height of W minus the height of a mode line, if any. */
1104
1105 INLINE int
1106 window_text_bottom_y (struct window *w)
1107 {
1108 int height = WINDOW_TOTAL_HEIGHT (w);
1109
1110 if (WINDOW_WANTS_MODELINE_P (w))
1111 height -= CURRENT_MODE_LINE_HEIGHT (w);
1112 return height;
1113 }
1114
1115 /* Return the pixel width of display area AREA of window W. AREA < 0
1116 means return the total width of W, not including fringes to
1117 the left and right of the window. */
1118
1119 INLINE int
1120 window_box_width (struct window *w, int area)
1121 {
1122 int cols = XFASTINT (w->total_cols);
1123 int pixels = 0;
1124
1125 if (!w->pseudo_window_p)
1126 {
1127 cols -= WINDOW_SCROLL_BAR_COLS (w);
1128
1129 if (area == TEXT_AREA)
1130 {
1131 if (INTEGERP (w->left_margin_cols))
1132 cols -= XFASTINT (w->left_margin_cols);
1133 if (INTEGERP (w->right_margin_cols))
1134 cols -= XFASTINT (w->right_margin_cols);
1135 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1136 }
1137 else if (area == LEFT_MARGIN_AREA)
1138 {
1139 cols = (INTEGERP (w->left_margin_cols)
1140 ? XFASTINT (w->left_margin_cols) : 0);
1141 pixels = 0;
1142 }
1143 else if (area == RIGHT_MARGIN_AREA)
1144 {
1145 cols = (INTEGERP (w->right_margin_cols)
1146 ? XFASTINT (w->right_margin_cols) : 0);
1147 pixels = 0;
1148 }
1149 }
1150
1151 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1152 }
1153
1154
1155 /* Return the pixel height of the display area of window W, not
1156 including mode lines of W, if any. */
1157
1158 INLINE int
1159 window_box_height (struct window *w)
1160 {
1161 struct frame *f = XFRAME (w->frame);
1162 int height = WINDOW_TOTAL_HEIGHT (w);
1163
1164 xassert (height >= 0);
1165
1166 /* Note: the code below that determines the mode-line/header-line
1167 height is essentially the same as that contained in the macro
1168 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1169 the appropriate glyph row has its `mode_line_p' flag set,
1170 and if it doesn't, uses estimate_mode_line_height instead. */
1171
1172 if (WINDOW_WANTS_MODELINE_P (w))
1173 {
1174 struct glyph_row *ml_row
1175 = (w->current_matrix && w->current_matrix->rows
1176 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1177 : 0);
1178 if (ml_row && ml_row->mode_line_p)
1179 height -= ml_row->height;
1180 else
1181 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1182 }
1183
1184 if (WINDOW_WANTS_HEADER_LINE_P (w))
1185 {
1186 struct glyph_row *hl_row
1187 = (w->current_matrix && w->current_matrix->rows
1188 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1189 : 0);
1190 if (hl_row && hl_row->mode_line_p)
1191 height -= hl_row->height;
1192 else
1193 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1194 }
1195
1196 /* With a very small font and a mode-line that's taller than
1197 default, we might end up with a negative height. */
1198 return max (0, height);
1199 }
1200
1201 /* Return the window-relative coordinate of the left edge of display
1202 area AREA of window W. AREA < 0 means return the left edge of the
1203 whole window, to the right of the left fringe of W. */
1204
1205 INLINE int
1206 window_box_left_offset (struct window *w, int area)
1207 {
1208 int x;
1209
1210 if (w->pseudo_window_p)
1211 return 0;
1212
1213 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1214
1215 if (area == TEXT_AREA)
1216 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1217 + window_box_width (w, LEFT_MARGIN_AREA));
1218 else if (area == RIGHT_MARGIN_AREA)
1219 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1220 + window_box_width (w, LEFT_MARGIN_AREA)
1221 + window_box_width (w, TEXT_AREA)
1222 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1223 ? 0
1224 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1225 else if (area == LEFT_MARGIN_AREA
1226 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1227 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1228
1229 return x;
1230 }
1231
1232
1233 /* Return the window-relative coordinate of the right edge of display
1234 area AREA of window W. AREA < 0 means return the right edge of the
1235 whole window, to the left of the right fringe of W. */
1236
1237 INLINE int
1238 window_box_right_offset (struct window *w, int area)
1239 {
1240 return window_box_left_offset (w, area) + window_box_width (w, area);
1241 }
1242
1243 /* Return the frame-relative coordinate of the left edge of display
1244 area AREA of window W. AREA < 0 means return the left edge of the
1245 whole window, to the right of the left fringe of W. */
1246
1247 INLINE int
1248 window_box_left (struct window *w, int area)
1249 {
1250 struct frame *f = XFRAME (w->frame);
1251 int x;
1252
1253 if (w->pseudo_window_p)
1254 return FRAME_INTERNAL_BORDER_WIDTH (f);
1255
1256 x = (WINDOW_LEFT_EDGE_X (w)
1257 + window_box_left_offset (w, area));
1258
1259 return x;
1260 }
1261
1262
1263 /* Return the frame-relative coordinate of the right edge of display
1264 area AREA of window W. AREA < 0 means return the right edge of the
1265 whole window, to the left of the right fringe of W. */
1266
1267 INLINE int
1268 window_box_right (struct window *w, int area)
1269 {
1270 return window_box_left (w, area) + window_box_width (w, area);
1271 }
1272
1273 /* Get the bounding box of the display area AREA of window W, without
1274 mode lines, in frame-relative coordinates. AREA < 0 means the
1275 whole window, not including the left and right fringes of
1276 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1277 coordinates of the upper-left corner of the box. Return in
1278 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1279
1280 INLINE void
1281 window_box (struct window *w, int area, int *box_x, int *box_y,
1282 int *box_width, int *box_height)
1283 {
1284 if (box_width)
1285 *box_width = window_box_width (w, area);
1286 if (box_height)
1287 *box_height = window_box_height (w);
1288 if (box_x)
1289 *box_x = window_box_left (w, area);
1290 if (box_y)
1291 {
1292 *box_y = WINDOW_TOP_EDGE_Y (w);
1293 if (WINDOW_WANTS_HEADER_LINE_P (w))
1294 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1295 }
1296 }
1297
1298
1299 /* Get the bounding box of the display area AREA of window W, without
1300 mode lines. AREA < 0 means the whole window, not including the
1301 left and right fringe of the window. Return in *TOP_LEFT_X
1302 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1303 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1304 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1305 box. */
1306
1307 INLINE void
1308 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1309 int *bottom_right_x, int *bottom_right_y)
1310 {
1311 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1312 bottom_right_y);
1313 *bottom_right_x += *top_left_x;
1314 *bottom_right_y += *top_left_y;
1315 }
1316
1317
1318 \f
1319 /***********************************************************************
1320 Utilities
1321 ***********************************************************************/
1322
1323 /* Return the bottom y-position of the line the iterator IT is in.
1324 This can modify IT's settings. */
1325
1326 int
1327 line_bottom_y (struct it *it)
1328 {
1329 int line_height = it->max_ascent + it->max_descent;
1330 int line_top_y = it->current_y;
1331
1332 if (line_height == 0)
1333 {
1334 if (last_height)
1335 line_height = last_height;
1336 else if (IT_CHARPOS (*it) < ZV)
1337 {
1338 move_it_by_lines (it, 1, 1);
1339 line_height = (it->max_ascent || it->max_descent
1340 ? it->max_ascent + it->max_descent
1341 : last_height);
1342 }
1343 else
1344 {
1345 struct glyph_row *row = it->glyph_row;
1346
1347 /* Use the default character height. */
1348 it->glyph_row = NULL;
1349 it->what = IT_CHARACTER;
1350 it->c = ' ';
1351 it->len = 1;
1352 PRODUCE_GLYPHS (it);
1353 line_height = it->ascent + it->descent;
1354 it->glyph_row = row;
1355 }
1356 }
1357
1358 return line_top_y + line_height;
1359 }
1360
1361
1362 /* Return 1 if position CHARPOS is visible in window W.
1363 CHARPOS < 0 means return info about WINDOW_END position.
1364 If visible, set *X and *Y to pixel coordinates of top left corner.
1365 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1366 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1367
1368 int
1369 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1370 int *rtop, int *rbot, int *rowh, int *vpos)
1371 {
1372 struct it it;
1373 struct text_pos top;
1374 int visible_p = 0;
1375 struct buffer *old_buffer = NULL;
1376
1377 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1378 return visible_p;
1379
1380 if (XBUFFER (w->buffer) != current_buffer)
1381 {
1382 old_buffer = current_buffer;
1383 set_buffer_internal_1 (XBUFFER (w->buffer));
1384 }
1385
1386 SET_TEXT_POS_FROM_MARKER (top, w->start);
1387
1388 /* Compute exact mode line heights. */
1389 if (WINDOW_WANTS_MODELINE_P (w))
1390 current_mode_line_height
1391 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1392 current_buffer->mode_line_format);
1393
1394 if (WINDOW_WANTS_HEADER_LINE_P (w))
1395 current_header_line_height
1396 = display_mode_line (w, HEADER_LINE_FACE_ID,
1397 current_buffer->header_line_format);
1398
1399 start_display (&it, w, top);
1400 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1401 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1402
1403 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1404 {
1405 /* We have reached CHARPOS, or passed it. How the call to
1406 move_it_to can overshoot: (i) If CHARPOS is on invisible
1407 text, move_it_to stops at the end of the invisible text,
1408 after CHARPOS. (ii) If CHARPOS is in a display vector,
1409 move_it_to stops on its last glyph. */
1410 int top_x = it.current_x;
1411 int top_y = it.current_y;
1412 enum it_method it_method = it.method;
1413 /* Calling line_bottom_y may change it.method, it.position, etc. */
1414 int bottom_y = (last_height = 0, line_bottom_y (&it));
1415 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1416
1417 if (top_y < window_top_y)
1418 visible_p = bottom_y > window_top_y;
1419 else if (top_y < it.last_visible_y)
1420 visible_p = 1;
1421 if (visible_p)
1422 {
1423 if (it_method == GET_FROM_DISPLAY_VECTOR)
1424 {
1425 /* We stopped on the last glyph of a display vector.
1426 Try and recompute. Hack alert! */
1427 if (charpos < 2 || top.charpos >= charpos)
1428 top_x = it.glyph_row->x;
1429 else
1430 {
1431 struct it it2;
1432 start_display (&it2, w, top);
1433 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1434 get_next_display_element (&it2);
1435 PRODUCE_GLYPHS (&it2);
1436 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1437 || it2.current_x > it2.last_visible_x)
1438 top_x = it.glyph_row->x;
1439 else
1440 {
1441 top_x = it2.current_x;
1442 top_y = it2.current_y;
1443 }
1444 }
1445 }
1446
1447 *x = top_x;
1448 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1449 *rtop = max (0, window_top_y - top_y);
1450 *rbot = max (0, bottom_y - it.last_visible_y);
1451 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1452 - max (top_y, window_top_y)));
1453 *vpos = it.vpos;
1454 }
1455 }
1456 else
1457 {
1458 struct it it2;
1459
1460 it2 = it;
1461 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1462 move_it_by_lines (&it, 1, 0);
1463 if (charpos < IT_CHARPOS (it)
1464 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1465 {
1466 visible_p = 1;
1467 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1468 *x = it2.current_x;
1469 *y = it2.current_y + it2.max_ascent - it2.ascent;
1470 *rtop = max (0, -it2.current_y);
1471 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1472 - it.last_visible_y));
1473 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1474 it.last_visible_y)
1475 - max (it2.current_y,
1476 WINDOW_HEADER_LINE_HEIGHT (w))));
1477 *vpos = it2.vpos;
1478 }
1479 }
1480
1481 if (old_buffer)
1482 set_buffer_internal_1 (old_buffer);
1483
1484 current_header_line_height = current_mode_line_height = -1;
1485
1486 if (visible_p && XFASTINT (w->hscroll) > 0)
1487 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1488
1489 #if 0
1490 /* Debugging code. */
1491 if (visible_p)
1492 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1493 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1494 else
1495 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1496 #endif
1497
1498 return visible_p;
1499 }
1500
1501
1502 /* Return the next character from STR which is MAXLEN bytes long.
1503 Return in *LEN the length of the character. This is like
1504 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1505 we find one, we return a `?', but with the length of the invalid
1506 character. */
1507
1508 static INLINE int
1509 string_char_and_length (const unsigned char *str, int *len)
1510 {
1511 int c;
1512
1513 c = STRING_CHAR_AND_LENGTH (str, *len);
1514 if (!CHAR_VALID_P (c, 1))
1515 /* We may not change the length here because other places in Emacs
1516 don't use this function, i.e. they silently accept invalid
1517 characters. */
1518 c = '?';
1519
1520 return c;
1521 }
1522
1523
1524
1525 /* Given a position POS containing a valid character and byte position
1526 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1527
1528 static struct text_pos
1529 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1530 {
1531 xassert (STRINGP (string) && nchars >= 0);
1532
1533 if (STRING_MULTIBYTE (string))
1534 {
1535 EMACS_INT rest = SBYTES (string) - BYTEPOS (pos);
1536 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1537 int len;
1538
1539 while (nchars--)
1540 {
1541 string_char_and_length (p, &len);
1542 p += len, rest -= len;
1543 xassert (rest >= 0);
1544 CHARPOS (pos) += 1;
1545 BYTEPOS (pos) += len;
1546 }
1547 }
1548 else
1549 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1550
1551 return pos;
1552 }
1553
1554
1555 /* Value is the text position, i.e. character and byte position,
1556 for character position CHARPOS in STRING. */
1557
1558 static INLINE struct text_pos
1559 string_pos (EMACS_INT charpos, Lisp_Object string)
1560 {
1561 struct text_pos pos;
1562 xassert (STRINGP (string));
1563 xassert (charpos >= 0);
1564 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1565 return pos;
1566 }
1567
1568
1569 /* Value is a text position, i.e. character and byte position, for
1570 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1571 means recognize multibyte characters. */
1572
1573 static struct text_pos
1574 c_string_pos (EMACS_INT charpos, const unsigned char *s, int multibyte_p)
1575 {
1576 struct text_pos pos;
1577
1578 xassert (s != NULL);
1579 xassert (charpos >= 0);
1580
1581 if (multibyte_p)
1582 {
1583 EMACS_INT rest = strlen (s);
1584 int len;
1585
1586 SET_TEXT_POS (pos, 0, 0);
1587 while (charpos--)
1588 {
1589 string_char_and_length (s, &len);
1590 s += len, rest -= len;
1591 xassert (rest >= 0);
1592 CHARPOS (pos) += 1;
1593 BYTEPOS (pos) += len;
1594 }
1595 }
1596 else
1597 SET_TEXT_POS (pos, charpos, charpos);
1598
1599 return pos;
1600 }
1601
1602
1603 /* Value is the number of characters in C string S. MULTIBYTE_P
1604 non-zero means recognize multibyte characters. */
1605
1606 static EMACS_INT
1607 number_of_chars (const unsigned char *s, int multibyte_p)
1608 {
1609 EMACS_INT nchars;
1610
1611 if (multibyte_p)
1612 {
1613 EMACS_INT rest = strlen (s);
1614 int len;
1615 unsigned char *p = (unsigned char *) s;
1616
1617 for (nchars = 0; rest > 0; ++nchars)
1618 {
1619 string_char_and_length (p, &len);
1620 rest -= len, p += len;
1621 }
1622 }
1623 else
1624 nchars = strlen (s);
1625
1626 return nchars;
1627 }
1628
1629
1630 /* Compute byte position NEWPOS->bytepos corresponding to
1631 NEWPOS->charpos. POS is a known position in string STRING.
1632 NEWPOS->charpos must be >= POS.charpos. */
1633
1634 static void
1635 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1636 {
1637 xassert (STRINGP (string));
1638 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1639
1640 if (STRING_MULTIBYTE (string))
1641 *newpos = string_pos_nchars_ahead (pos, string,
1642 CHARPOS (*newpos) - CHARPOS (pos));
1643 else
1644 BYTEPOS (*newpos) = CHARPOS (*newpos);
1645 }
1646
1647 /* EXPORT:
1648 Return an estimation of the pixel height of mode or header lines on
1649 frame F. FACE_ID specifies what line's height to estimate. */
1650
1651 int
1652 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1653 {
1654 #ifdef HAVE_WINDOW_SYSTEM
1655 if (FRAME_WINDOW_P (f))
1656 {
1657 int height = FONT_HEIGHT (FRAME_FONT (f));
1658
1659 /* This function is called so early when Emacs starts that the face
1660 cache and mode line face are not yet initialized. */
1661 if (FRAME_FACE_CACHE (f))
1662 {
1663 struct face *face = FACE_FROM_ID (f, face_id);
1664 if (face)
1665 {
1666 if (face->font)
1667 height = FONT_HEIGHT (face->font);
1668 if (face->box_line_width > 0)
1669 height += 2 * face->box_line_width;
1670 }
1671 }
1672
1673 return height;
1674 }
1675 #endif
1676
1677 return 1;
1678 }
1679
1680 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1681 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1682 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1683 not force the value into range. */
1684
1685 void
1686 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1687 int *x, int *y, NativeRectangle *bounds, int noclip)
1688 {
1689
1690 #ifdef HAVE_WINDOW_SYSTEM
1691 if (FRAME_WINDOW_P (f))
1692 {
1693 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1694 even for negative values. */
1695 if (pix_x < 0)
1696 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1697 if (pix_y < 0)
1698 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1699
1700 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1701 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1702
1703 if (bounds)
1704 STORE_NATIVE_RECT (*bounds,
1705 FRAME_COL_TO_PIXEL_X (f, pix_x),
1706 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1707 FRAME_COLUMN_WIDTH (f) - 1,
1708 FRAME_LINE_HEIGHT (f) - 1);
1709
1710 if (!noclip)
1711 {
1712 if (pix_x < 0)
1713 pix_x = 0;
1714 else if (pix_x > FRAME_TOTAL_COLS (f))
1715 pix_x = FRAME_TOTAL_COLS (f);
1716
1717 if (pix_y < 0)
1718 pix_y = 0;
1719 else if (pix_y > FRAME_LINES (f))
1720 pix_y = FRAME_LINES (f);
1721 }
1722 }
1723 #endif
1724
1725 *x = pix_x;
1726 *y = pix_y;
1727 }
1728
1729
1730 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1731 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1732 can't tell the positions because W's display is not up to date,
1733 return 0. */
1734
1735 int
1736 glyph_to_pixel_coords (struct window *w, int hpos, int vpos,
1737 int *frame_x, int *frame_y)
1738 {
1739 #ifdef HAVE_WINDOW_SYSTEM
1740 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1741 {
1742 int success_p;
1743
1744 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1745 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1746
1747 if (display_completed)
1748 {
1749 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1750 struct glyph *glyph = row->glyphs[TEXT_AREA];
1751 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1752
1753 hpos = row->x;
1754 vpos = row->y;
1755 while (glyph < end)
1756 {
1757 hpos += glyph->pixel_width;
1758 ++glyph;
1759 }
1760
1761 /* If first glyph is partially visible, its first visible position is still 0. */
1762 if (hpos < 0)
1763 hpos = 0;
1764
1765 success_p = 1;
1766 }
1767 else
1768 {
1769 hpos = vpos = 0;
1770 success_p = 0;
1771 }
1772
1773 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1774 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1775 return success_p;
1776 }
1777 #endif
1778
1779 *frame_x = hpos;
1780 *frame_y = vpos;
1781 return 1;
1782 }
1783
1784
1785 #ifdef HAVE_WINDOW_SYSTEM
1786
1787 /* Find the glyph under window-relative coordinates X/Y in window W.
1788 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1789 strings. Return in *HPOS and *VPOS the row and column number of
1790 the glyph found. Return in *AREA the glyph area containing X.
1791 Value is a pointer to the glyph found or null if X/Y is not on
1792 text, or we can't tell because W's current matrix is not up to
1793 date. */
1794
1795 static
1796 struct glyph *
1797 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1798 int *dx, int *dy, int *area)
1799 {
1800 struct glyph *glyph, *end;
1801 struct glyph_row *row = NULL;
1802 int x0, i;
1803
1804 /* Find row containing Y. Give up if some row is not enabled. */
1805 for (i = 0; i < w->current_matrix->nrows; ++i)
1806 {
1807 row = MATRIX_ROW (w->current_matrix, i);
1808 if (!row->enabled_p)
1809 return NULL;
1810 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1811 break;
1812 }
1813
1814 *vpos = i;
1815 *hpos = 0;
1816
1817 /* Give up if Y is not in the window. */
1818 if (i == w->current_matrix->nrows)
1819 return NULL;
1820
1821 /* Get the glyph area containing X. */
1822 if (w->pseudo_window_p)
1823 {
1824 *area = TEXT_AREA;
1825 x0 = 0;
1826 }
1827 else
1828 {
1829 if (x < window_box_left_offset (w, TEXT_AREA))
1830 {
1831 *area = LEFT_MARGIN_AREA;
1832 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1833 }
1834 else if (x < window_box_right_offset (w, TEXT_AREA))
1835 {
1836 *area = TEXT_AREA;
1837 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1838 }
1839 else
1840 {
1841 *area = RIGHT_MARGIN_AREA;
1842 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1843 }
1844 }
1845
1846 /* Find glyph containing X. */
1847 glyph = row->glyphs[*area];
1848 end = glyph + row->used[*area];
1849 x -= x0;
1850 while (glyph < end && x >= glyph->pixel_width)
1851 {
1852 x -= glyph->pixel_width;
1853 ++glyph;
1854 }
1855
1856 if (glyph == end)
1857 return NULL;
1858
1859 if (dx)
1860 {
1861 *dx = x;
1862 *dy = y - (row->y + row->ascent - glyph->ascent);
1863 }
1864
1865 *hpos = glyph - row->glyphs[*area];
1866 return glyph;
1867 }
1868
1869
1870 /* EXPORT:
1871 Convert frame-relative x/y to coordinates relative to window W.
1872 Takes pseudo-windows into account. */
1873
1874 void
1875 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1876 {
1877 if (w->pseudo_window_p)
1878 {
1879 /* A pseudo-window is always full-width, and starts at the
1880 left edge of the frame, plus a frame border. */
1881 struct frame *f = XFRAME (w->frame);
1882 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1883 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1884 }
1885 else
1886 {
1887 *x -= WINDOW_LEFT_EDGE_X (w);
1888 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1889 }
1890 }
1891
1892 /* EXPORT:
1893 Return in RECTS[] at most N clipping rectangles for glyph string S.
1894 Return the number of stored rectangles. */
1895
1896 int
1897 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1898 {
1899 XRectangle r;
1900
1901 if (n <= 0)
1902 return 0;
1903
1904 if (s->row->full_width_p)
1905 {
1906 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1907 r.x = WINDOW_LEFT_EDGE_X (s->w);
1908 r.width = WINDOW_TOTAL_WIDTH (s->w);
1909
1910 /* Unless displaying a mode or menu bar line, which are always
1911 fully visible, clip to the visible part of the row. */
1912 if (s->w->pseudo_window_p)
1913 r.height = s->row->visible_height;
1914 else
1915 r.height = s->height;
1916 }
1917 else
1918 {
1919 /* This is a text line that may be partially visible. */
1920 r.x = window_box_left (s->w, s->area);
1921 r.width = window_box_width (s->w, s->area);
1922 r.height = s->row->visible_height;
1923 }
1924
1925 if (s->clip_head)
1926 if (r.x < s->clip_head->x)
1927 {
1928 if (r.width >= s->clip_head->x - r.x)
1929 r.width -= s->clip_head->x - r.x;
1930 else
1931 r.width = 0;
1932 r.x = s->clip_head->x;
1933 }
1934 if (s->clip_tail)
1935 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1936 {
1937 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1938 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1939 else
1940 r.width = 0;
1941 }
1942
1943 /* If S draws overlapping rows, it's sufficient to use the top and
1944 bottom of the window for clipping because this glyph string
1945 intentionally draws over other lines. */
1946 if (s->for_overlaps)
1947 {
1948 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1949 r.height = window_text_bottom_y (s->w) - r.y;
1950
1951 /* Alas, the above simple strategy does not work for the
1952 environments with anti-aliased text: if the same text is
1953 drawn onto the same place multiple times, it gets thicker.
1954 If the overlap we are processing is for the erased cursor, we
1955 take the intersection with the rectagle of the cursor. */
1956 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1957 {
1958 XRectangle rc, r_save = r;
1959
1960 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1961 rc.y = s->w->phys_cursor.y;
1962 rc.width = s->w->phys_cursor_width;
1963 rc.height = s->w->phys_cursor_height;
1964
1965 x_intersect_rectangles (&r_save, &rc, &r);
1966 }
1967 }
1968 else
1969 {
1970 /* Don't use S->y for clipping because it doesn't take partially
1971 visible lines into account. For example, it can be negative for
1972 partially visible lines at the top of a window. */
1973 if (!s->row->full_width_p
1974 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1975 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1976 else
1977 r.y = max (0, s->row->y);
1978 }
1979
1980 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1981
1982 /* If drawing the cursor, don't let glyph draw outside its
1983 advertised boundaries. Cleartype does this under some circumstances. */
1984 if (s->hl == DRAW_CURSOR)
1985 {
1986 struct glyph *glyph = s->first_glyph;
1987 int height, max_y;
1988
1989 if (s->x > r.x)
1990 {
1991 r.width -= s->x - r.x;
1992 r.x = s->x;
1993 }
1994 r.width = min (r.width, glyph->pixel_width);
1995
1996 /* If r.y is below window bottom, ensure that we still see a cursor. */
1997 height = min (glyph->ascent + glyph->descent,
1998 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1999 max_y = window_text_bottom_y (s->w) - height;
2000 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2001 if (s->ybase - glyph->ascent > max_y)
2002 {
2003 r.y = max_y;
2004 r.height = height;
2005 }
2006 else
2007 {
2008 /* Don't draw cursor glyph taller than our actual glyph. */
2009 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2010 if (height < r.height)
2011 {
2012 max_y = r.y + r.height;
2013 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2014 r.height = min (max_y - r.y, height);
2015 }
2016 }
2017 }
2018
2019 if (s->row->clip)
2020 {
2021 XRectangle r_save = r;
2022
2023 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2024 r.width = 0;
2025 }
2026
2027 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2028 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2029 {
2030 #ifdef CONVERT_FROM_XRECT
2031 CONVERT_FROM_XRECT (r, *rects);
2032 #else
2033 *rects = r;
2034 #endif
2035 return 1;
2036 }
2037 else
2038 {
2039 /* If we are processing overlapping and allowed to return
2040 multiple clipping rectangles, we exclude the row of the glyph
2041 string from the clipping rectangle. This is to avoid drawing
2042 the same text on the environment with anti-aliasing. */
2043 #ifdef CONVERT_FROM_XRECT
2044 XRectangle rs[2];
2045 #else
2046 XRectangle *rs = rects;
2047 #endif
2048 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2049
2050 if (s->for_overlaps & OVERLAPS_PRED)
2051 {
2052 rs[i] = r;
2053 if (r.y + r.height > row_y)
2054 {
2055 if (r.y < row_y)
2056 rs[i].height = row_y - r.y;
2057 else
2058 rs[i].height = 0;
2059 }
2060 i++;
2061 }
2062 if (s->for_overlaps & OVERLAPS_SUCC)
2063 {
2064 rs[i] = r;
2065 if (r.y < row_y + s->row->visible_height)
2066 {
2067 if (r.y + r.height > row_y + s->row->visible_height)
2068 {
2069 rs[i].y = row_y + s->row->visible_height;
2070 rs[i].height = r.y + r.height - rs[i].y;
2071 }
2072 else
2073 rs[i].height = 0;
2074 }
2075 i++;
2076 }
2077
2078 n = i;
2079 #ifdef CONVERT_FROM_XRECT
2080 for (i = 0; i < n; i++)
2081 CONVERT_FROM_XRECT (rs[i], rects[i]);
2082 #endif
2083 return n;
2084 }
2085 }
2086
2087 /* EXPORT:
2088 Return in *NR the clipping rectangle for glyph string S. */
2089
2090 void
2091 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2092 {
2093 get_glyph_string_clip_rects (s, nr, 1);
2094 }
2095
2096
2097 /* EXPORT:
2098 Return the position and height of the phys cursor in window W.
2099 Set w->phys_cursor_width to width of phys cursor.
2100 */
2101
2102 void
2103 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2104 struct glyph *glyph, int *xp, int *yp, int *heightp)
2105 {
2106 struct frame *f = XFRAME (WINDOW_FRAME (w));
2107 int x, y, wd, h, h0, y0;
2108
2109 /* Compute the width of the rectangle to draw. If on a stretch
2110 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2111 rectangle as wide as the glyph, but use a canonical character
2112 width instead. */
2113 wd = glyph->pixel_width - 1;
2114 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2115 wd++; /* Why? */
2116 #endif
2117
2118 x = w->phys_cursor.x;
2119 if (x < 0)
2120 {
2121 wd += x;
2122 x = 0;
2123 }
2124
2125 if (glyph->type == STRETCH_GLYPH
2126 && !x_stretch_cursor_p)
2127 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2128 w->phys_cursor_width = wd;
2129
2130 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2131
2132 /* If y is below window bottom, ensure that we still see a cursor. */
2133 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2134
2135 h = max (h0, glyph->ascent + glyph->descent);
2136 h0 = min (h0, glyph->ascent + glyph->descent);
2137
2138 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2139 if (y < y0)
2140 {
2141 h = max (h - (y0 - y) + 1, h0);
2142 y = y0 - 1;
2143 }
2144 else
2145 {
2146 y0 = window_text_bottom_y (w) - h0;
2147 if (y > y0)
2148 {
2149 h += y - y0;
2150 y = y0;
2151 }
2152 }
2153
2154 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2155 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2156 *heightp = h;
2157 }
2158
2159 /*
2160 * Remember which glyph the mouse is over.
2161 */
2162
2163 void
2164 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2165 {
2166 Lisp_Object window;
2167 struct window *w;
2168 struct glyph_row *r, *gr, *end_row;
2169 enum window_part part;
2170 enum glyph_row_area area;
2171 int x, y, width, height;
2172
2173 /* Try to determine frame pixel position and size of the glyph under
2174 frame pixel coordinates X/Y on frame F. */
2175
2176 if (!f->glyphs_initialized_p
2177 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2178 NILP (window)))
2179 {
2180 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2181 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2182 goto virtual_glyph;
2183 }
2184
2185 w = XWINDOW (window);
2186 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2187 height = WINDOW_FRAME_LINE_HEIGHT (w);
2188
2189 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2190 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2191
2192 if (w->pseudo_window_p)
2193 {
2194 area = TEXT_AREA;
2195 part = ON_MODE_LINE; /* Don't adjust margin. */
2196 goto text_glyph;
2197 }
2198
2199 switch (part)
2200 {
2201 case ON_LEFT_MARGIN:
2202 area = LEFT_MARGIN_AREA;
2203 goto text_glyph;
2204
2205 case ON_RIGHT_MARGIN:
2206 area = RIGHT_MARGIN_AREA;
2207 goto text_glyph;
2208
2209 case ON_HEADER_LINE:
2210 case ON_MODE_LINE:
2211 gr = (part == ON_HEADER_LINE
2212 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2213 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2214 gy = gr->y;
2215 area = TEXT_AREA;
2216 goto text_glyph_row_found;
2217
2218 case ON_TEXT:
2219 area = TEXT_AREA;
2220
2221 text_glyph:
2222 gr = 0; gy = 0;
2223 for (; r <= end_row && r->enabled_p; ++r)
2224 if (r->y + r->height > y)
2225 {
2226 gr = r; gy = r->y;
2227 break;
2228 }
2229
2230 text_glyph_row_found:
2231 if (gr && gy <= y)
2232 {
2233 struct glyph *g = gr->glyphs[area];
2234 struct glyph *end = g + gr->used[area];
2235
2236 height = gr->height;
2237 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2238 if (gx + g->pixel_width > x)
2239 break;
2240
2241 if (g < end)
2242 {
2243 if (g->type == IMAGE_GLYPH)
2244 {
2245 /* Don't remember when mouse is over image, as
2246 image may have hot-spots. */
2247 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2248 return;
2249 }
2250 width = g->pixel_width;
2251 }
2252 else
2253 {
2254 /* Use nominal char spacing at end of line. */
2255 x -= gx;
2256 gx += (x / width) * width;
2257 }
2258
2259 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2260 gx += window_box_left_offset (w, area);
2261 }
2262 else
2263 {
2264 /* Use nominal line height at end of window. */
2265 gx = (x / width) * width;
2266 y -= gy;
2267 gy += (y / height) * height;
2268 }
2269 break;
2270
2271 case ON_LEFT_FRINGE:
2272 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2273 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2274 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2275 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2276 goto row_glyph;
2277
2278 case ON_RIGHT_FRINGE:
2279 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2280 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2281 : window_box_right_offset (w, TEXT_AREA));
2282 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2283 goto row_glyph;
2284
2285 case ON_SCROLL_BAR:
2286 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2287 ? 0
2288 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2289 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2290 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2291 : 0)));
2292 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2293
2294 row_glyph:
2295 gr = 0, gy = 0;
2296 for (; r <= end_row && r->enabled_p; ++r)
2297 if (r->y + r->height > y)
2298 {
2299 gr = r; gy = r->y;
2300 break;
2301 }
2302
2303 if (gr && gy <= y)
2304 height = gr->height;
2305 else
2306 {
2307 /* Use nominal line height at end of window. */
2308 y -= gy;
2309 gy += (y / height) * height;
2310 }
2311 break;
2312
2313 default:
2314 ;
2315 virtual_glyph:
2316 /* If there is no glyph under the mouse, then we divide the screen
2317 into a grid of the smallest glyph in the frame, and use that
2318 as our "glyph". */
2319
2320 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2321 round down even for negative values. */
2322 if (gx < 0)
2323 gx -= width - 1;
2324 if (gy < 0)
2325 gy -= height - 1;
2326
2327 gx = (gx / width) * width;
2328 gy = (gy / height) * height;
2329
2330 goto store_rect;
2331 }
2332
2333 gx += WINDOW_LEFT_EDGE_X (w);
2334 gy += WINDOW_TOP_EDGE_Y (w);
2335
2336 store_rect:
2337 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2338
2339 /* Visible feedback for debugging. */
2340 #if 0
2341 #if HAVE_X_WINDOWS
2342 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2343 f->output_data.x->normal_gc,
2344 gx, gy, width, height);
2345 #endif
2346 #endif
2347 }
2348
2349
2350 #endif /* HAVE_WINDOW_SYSTEM */
2351
2352 \f
2353 /***********************************************************************
2354 Lisp form evaluation
2355 ***********************************************************************/
2356
2357 /* Error handler for safe_eval and safe_call. */
2358
2359 static Lisp_Object
2360 safe_eval_handler (Lisp_Object arg)
2361 {
2362 add_to_log ("Error during redisplay: %s", arg, Qnil);
2363 return Qnil;
2364 }
2365
2366
2367 /* Evaluate SEXPR and return the result, or nil if something went
2368 wrong. Prevent redisplay during the evaluation. */
2369
2370 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2371 Return the result, or nil if something went wrong. Prevent
2372 redisplay during the evaluation. */
2373
2374 Lisp_Object
2375 safe_call (int nargs, Lisp_Object *args)
2376 {
2377 Lisp_Object val;
2378
2379 if (inhibit_eval_during_redisplay)
2380 val = Qnil;
2381 else
2382 {
2383 int count = SPECPDL_INDEX ();
2384 struct gcpro gcpro1;
2385
2386 GCPRO1 (args[0]);
2387 gcpro1.nvars = nargs;
2388 specbind (Qinhibit_redisplay, Qt);
2389 /* Use Qt to ensure debugger does not run,
2390 so there is no possibility of wanting to redisplay. */
2391 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2392 safe_eval_handler);
2393 UNGCPRO;
2394 val = unbind_to (count, val);
2395 }
2396
2397 return val;
2398 }
2399
2400
2401 /* Call function FN with one argument ARG.
2402 Return the result, or nil if something went wrong. */
2403
2404 Lisp_Object
2405 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2406 {
2407 Lisp_Object args[2];
2408 args[0] = fn;
2409 args[1] = arg;
2410 return safe_call (2, args);
2411 }
2412
2413 static Lisp_Object Qeval;
2414
2415 Lisp_Object
2416 safe_eval (Lisp_Object sexpr)
2417 {
2418 return safe_call1 (Qeval, sexpr);
2419 }
2420
2421 /* Call function FN with one argument ARG.
2422 Return the result, or nil if something went wrong. */
2423
2424 Lisp_Object
2425 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2426 {
2427 Lisp_Object args[3];
2428 args[0] = fn;
2429 args[1] = arg1;
2430 args[2] = arg2;
2431 return safe_call (3, args);
2432 }
2433
2434
2435 \f
2436 /***********************************************************************
2437 Debugging
2438 ***********************************************************************/
2439
2440 #if 0
2441
2442 /* Define CHECK_IT to perform sanity checks on iterators.
2443 This is for debugging. It is too slow to do unconditionally. */
2444
2445 static void
2446 check_it (it)
2447 struct it *it;
2448 {
2449 if (it->method == GET_FROM_STRING)
2450 {
2451 xassert (STRINGP (it->string));
2452 xassert (IT_STRING_CHARPOS (*it) >= 0);
2453 }
2454 else
2455 {
2456 xassert (IT_STRING_CHARPOS (*it) < 0);
2457 if (it->method == GET_FROM_BUFFER)
2458 {
2459 /* Check that character and byte positions agree. */
2460 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2461 }
2462 }
2463
2464 if (it->dpvec)
2465 xassert (it->current.dpvec_index >= 0);
2466 else
2467 xassert (it->current.dpvec_index < 0);
2468 }
2469
2470 #define CHECK_IT(IT) check_it ((IT))
2471
2472 #else /* not 0 */
2473
2474 #define CHECK_IT(IT) (void) 0
2475
2476 #endif /* not 0 */
2477
2478
2479 #if GLYPH_DEBUG
2480
2481 /* Check that the window end of window W is what we expect it
2482 to be---the last row in the current matrix displaying text. */
2483
2484 static void
2485 check_window_end (w)
2486 struct window *w;
2487 {
2488 if (!MINI_WINDOW_P (w)
2489 && !NILP (w->window_end_valid))
2490 {
2491 struct glyph_row *row;
2492 xassert ((row = MATRIX_ROW (w->current_matrix,
2493 XFASTINT (w->window_end_vpos)),
2494 !row->enabled_p
2495 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2496 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2497 }
2498 }
2499
2500 #define CHECK_WINDOW_END(W) check_window_end ((W))
2501
2502 #else /* not GLYPH_DEBUG */
2503
2504 #define CHECK_WINDOW_END(W) (void) 0
2505
2506 #endif /* not GLYPH_DEBUG */
2507
2508
2509 \f
2510 /***********************************************************************
2511 Iterator initialization
2512 ***********************************************************************/
2513
2514 /* Initialize IT for displaying current_buffer in window W, starting
2515 at character position CHARPOS. CHARPOS < 0 means that no buffer
2516 position is specified which is useful when the iterator is assigned
2517 a position later. BYTEPOS is the byte position corresponding to
2518 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2519
2520 If ROW is not null, calls to produce_glyphs with IT as parameter
2521 will produce glyphs in that row.
2522
2523 BASE_FACE_ID is the id of a base face to use. It must be one of
2524 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2525 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2526 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2527
2528 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2529 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2530 will be initialized to use the corresponding mode line glyph row of
2531 the desired matrix of W. */
2532
2533 void
2534 init_iterator (struct it *it, struct window *w,
2535 EMACS_INT charpos, EMACS_INT bytepos,
2536 struct glyph_row *row, enum face_id base_face_id)
2537 {
2538 int highlight_region_p;
2539 enum face_id remapped_base_face_id = base_face_id;
2540
2541 /* Some precondition checks. */
2542 xassert (w != NULL && it != NULL);
2543 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2544 && charpos <= ZV));
2545
2546 /* If face attributes have been changed since the last redisplay,
2547 free realized faces now because they depend on face definitions
2548 that might have changed. Don't free faces while there might be
2549 desired matrices pending which reference these faces. */
2550 if (face_change_count && !inhibit_free_realized_faces)
2551 {
2552 face_change_count = 0;
2553 free_all_realized_faces (Qnil);
2554 }
2555
2556 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2557 if (! NILP (Vface_remapping_alist))
2558 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2559
2560 /* Use one of the mode line rows of W's desired matrix if
2561 appropriate. */
2562 if (row == NULL)
2563 {
2564 if (base_face_id == MODE_LINE_FACE_ID
2565 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2566 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2567 else if (base_face_id == HEADER_LINE_FACE_ID)
2568 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2569 }
2570
2571 /* Clear IT. */
2572 memset (it, 0, sizeof *it);
2573 it->current.overlay_string_index = -1;
2574 it->current.dpvec_index = -1;
2575 it->base_face_id = remapped_base_face_id;
2576 it->string = Qnil;
2577 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2578
2579 /* The window in which we iterate over current_buffer: */
2580 XSETWINDOW (it->window, w);
2581 it->w = w;
2582 it->f = XFRAME (w->frame);
2583
2584 it->cmp_it.id = -1;
2585
2586 /* Extra space between lines (on window systems only). */
2587 if (base_face_id == DEFAULT_FACE_ID
2588 && FRAME_WINDOW_P (it->f))
2589 {
2590 if (NATNUMP (current_buffer->extra_line_spacing))
2591 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2592 else if (FLOATP (current_buffer->extra_line_spacing))
2593 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2594 * FRAME_LINE_HEIGHT (it->f));
2595 else if (it->f->extra_line_spacing > 0)
2596 it->extra_line_spacing = it->f->extra_line_spacing;
2597 it->max_extra_line_spacing = 0;
2598 }
2599
2600 /* If realized faces have been removed, e.g. because of face
2601 attribute changes of named faces, recompute them. When running
2602 in batch mode, the face cache of the initial frame is null. If
2603 we happen to get called, make a dummy face cache. */
2604 if (FRAME_FACE_CACHE (it->f) == NULL)
2605 init_frame_faces (it->f);
2606 if (FRAME_FACE_CACHE (it->f)->used == 0)
2607 recompute_basic_faces (it->f);
2608
2609 /* Current value of the `slice', `space-width', and 'height' properties. */
2610 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2611 it->space_width = Qnil;
2612 it->font_height = Qnil;
2613 it->override_ascent = -1;
2614
2615 /* Are control characters displayed as `^C'? */
2616 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2617
2618 /* -1 means everything between a CR and the following line end
2619 is invisible. >0 means lines indented more than this value are
2620 invisible. */
2621 it->selective = (INTEGERP (current_buffer->selective_display)
2622 ? XFASTINT (current_buffer->selective_display)
2623 : (!NILP (current_buffer->selective_display)
2624 ? -1 : 0));
2625 it->selective_display_ellipsis_p
2626 = !NILP (current_buffer->selective_display_ellipses);
2627
2628 /* Display table to use. */
2629 it->dp = window_display_table (w);
2630
2631 /* Are multibyte characters enabled in current_buffer? */
2632 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2633
2634 /* Do we need to reorder bidirectional text? Not if this is a
2635 unibyte buffer: by definition, none of the single-byte characters
2636 are strong R2L, so no reordering is needed. And bidi.c doesn't
2637 support unibyte buffers anyway. */
2638 it->bidi_p
2639 = !NILP (current_buffer->bidi_display_reordering) && it->multibyte_p;
2640
2641 /* Non-zero if we should highlight the region. */
2642 highlight_region_p
2643 = (!NILP (Vtransient_mark_mode)
2644 && !NILP (current_buffer->mark_active)
2645 && XMARKER (current_buffer->mark)->buffer != 0);
2646
2647 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2648 start and end of a visible region in window IT->w. Set both to
2649 -1 to indicate no region. */
2650 if (highlight_region_p
2651 /* Maybe highlight only in selected window. */
2652 && (/* Either show region everywhere. */
2653 highlight_nonselected_windows
2654 /* Or show region in the selected window. */
2655 || w == XWINDOW (selected_window)
2656 /* Or show the region if we are in the mini-buffer and W is
2657 the window the mini-buffer refers to. */
2658 || (MINI_WINDOW_P (XWINDOW (selected_window))
2659 && WINDOWP (minibuf_selected_window)
2660 && w == XWINDOW (minibuf_selected_window))))
2661 {
2662 EMACS_INT charpos = marker_position (current_buffer->mark);
2663 it->region_beg_charpos = min (PT, charpos);
2664 it->region_end_charpos = max (PT, charpos);
2665 }
2666 else
2667 it->region_beg_charpos = it->region_end_charpos = -1;
2668
2669 /* Get the position at which the redisplay_end_trigger hook should
2670 be run, if it is to be run at all. */
2671 if (MARKERP (w->redisplay_end_trigger)
2672 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2673 it->redisplay_end_trigger_charpos
2674 = marker_position (w->redisplay_end_trigger);
2675 else if (INTEGERP (w->redisplay_end_trigger))
2676 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2677
2678 /* Correct bogus values of tab_width. */
2679 it->tab_width = XINT (current_buffer->tab_width);
2680 if (it->tab_width <= 0 || it->tab_width > 1000)
2681 it->tab_width = 8;
2682
2683 /* Are lines in the display truncated? */
2684 if (base_face_id != DEFAULT_FACE_ID
2685 || XINT (it->w->hscroll)
2686 || (! WINDOW_FULL_WIDTH_P (it->w)
2687 && ((!NILP (Vtruncate_partial_width_windows)
2688 && !INTEGERP (Vtruncate_partial_width_windows))
2689 || (INTEGERP (Vtruncate_partial_width_windows)
2690 && (WINDOW_TOTAL_COLS (it->w)
2691 < XINT (Vtruncate_partial_width_windows))))))
2692 it->line_wrap = TRUNCATE;
2693 else if (NILP (current_buffer->truncate_lines))
2694 it->line_wrap = NILP (current_buffer->word_wrap)
2695 ? WINDOW_WRAP : WORD_WRAP;
2696 else
2697 it->line_wrap = TRUNCATE;
2698
2699 /* Get dimensions of truncation and continuation glyphs. These are
2700 displayed as fringe bitmaps under X, so we don't need them for such
2701 frames. */
2702 if (!FRAME_WINDOW_P (it->f))
2703 {
2704 if (it->line_wrap == TRUNCATE)
2705 {
2706 /* We will need the truncation glyph. */
2707 xassert (it->glyph_row == NULL);
2708 produce_special_glyphs (it, IT_TRUNCATION);
2709 it->truncation_pixel_width = it->pixel_width;
2710 }
2711 else
2712 {
2713 /* We will need the continuation glyph. */
2714 xassert (it->glyph_row == NULL);
2715 produce_special_glyphs (it, IT_CONTINUATION);
2716 it->continuation_pixel_width = it->pixel_width;
2717 }
2718
2719 /* Reset these values to zero because the produce_special_glyphs
2720 above has changed them. */
2721 it->pixel_width = it->ascent = it->descent = 0;
2722 it->phys_ascent = it->phys_descent = 0;
2723 }
2724
2725 /* Set this after getting the dimensions of truncation and
2726 continuation glyphs, so that we don't produce glyphs when calling
2727 produce_special_glyphs, above. */
2728 it->glyph_row = row;
2729 it->area = TEXT_AREA;
2730
2731 /* Forget any previous info about this row being reversed. */
2732 if (it->glyph_row)
2733 it->glyph_row->reversed_p = 0;
2734
2735 /* Get the dimensions of the display area. The display area
2736 consists of the visible window area plus a horizontally scrolled
2737 part to the left of the window. All x-values are relative to the
2738 start of this total display area. */
2739 if (base_face_id != DEFAULT_FACE_ID)
2740 {
2741 /* Mode lines, menu bar in terminal frames. */
2742 it->first_visible_x = 0;
2743 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2744 }
2745 else
2746 {
2747 it->first_visible_x
2748 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2749 it->last_visible_x = (it->first_visible_x
2750 + window_box_width (w, TEXT_AREA));
2751
2752 /* If we truncate lines, leave room for the truncator glyph(s) at
2753 the right margin. Otherwise, leave room for the continuation
2754 glyph(s). Truncation and continuation glyphs are not inserted
2755 for window-based redisplay. */
2756 if (!FRAME_WINDOW_P (it->f))
2757 {
2758 if (it->line_wrap == TRUNCATE)
2759 it->last_visible_x -= it->truncation_pixel_width;
2760 else
2761 it->last_visible_x -= it->continuation_pixel_width;
2762 }
2763
2764 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2765 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2766 }
2767
2768 /* Leave room for a border glyph. */
2769 if (!FRAME_WINDOW_P (it->f)
2770 && !WINDOW_RIGHTMOST_P (it->w))
2771 it->last_visible_x -= 1;
2772
2773 it->last_visible_y = window_text_bottom_y (w);
2774
2775 /* For mode lines and alike, arrange for the first glyph having a
2776 left box line if the face specifies a box. */
2777 if (base_face_id != DEFAULT_FACE_ID)
2778 {
2779 struct face *face;
2780
2781 it->face_id = remapped_base_face_id;
2782
2783 /* If we have a boxed mode line, make the first character appear
2784 with a left box line. */
2785 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2786 if (face->box != FACE_NO_BOX)
2787 it->start_of_box_run_p = 1;
2788 }
2789
2790 /* If we are to reorder bidirectional text, init the bidi
2791 iterator. */
2792 if (it->bidi_p)
2793 {
2794 /* Note the paragraph direction that this buffer wants to
2795 use. */
2796 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2797 it->paragraph_embedding = L2R;
2798 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2799 it->paragraph_embedding = R2L;
2800 else
2801 it->paragraph_embedding = NEUTRAL_DIR;
2802 bidi_init_it (charpos, bytepos, &it->bidi_it);
2803 }
2804
2805 /* If a buffer position was specified, set the iterator there,
2806 getting overlays and face properties from that position. */
2807 if (charpos >= BUF_BEG (current_buffer))
2808 {
2809 it->end_charpos = ZV;
2810 it->face_id = -1;
2811 IT_CHARPOS (*it) = charpos;
2812
2813 /* Compute byte position if not specified. */
2814 if (bytepos < charpos)
2815 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2816 else
2817 IT_BYTEPOS (*it) = bytepos;
2818
2819 it->start = it->current;
2820
2821 /* Compute faces etc. */
2822 reseat (it, it->current.pos, 1);
2823 }
2824
2825 CHECK_IT (it);
2826 }
2827
2828
2829 /* Initialize IT for the display of window W with window start POS. */
2830
2831 void
2832 start_display (struct it *it, struct window *w, struct text_pos pos)
2833 {
2834 struct glyph_row *row;
2835 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2836
2837 row = w->desired_matrix->rows + first_vpos;
2838 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2839 it->first_vpos = first_vpos;
2840
2841 /* Don't reseat to previous visible line start if current start
2842 position is in a string or image. */
2843 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2844 {
2845 int start_at_line_beg_p;
2846 int first_y = it->current_y;
2847
2848 /* If window start is not at a line start, skip forward to POS to
2849 get the correct continuation lines width. */
2850 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2851 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2852 if (!start_at_line_beg_p)
2853 {
2854 int new_x;
2855
2856 reseat_at_previous_visible_line_start (it);
2857 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2858
2859 new_x = it->current_x + it->pixel_width;
2860
2861 /* If lines are continued, this line may end in the middle
2862 of a multi-glyph character (e.g. a control character
2863 displayed as \003, or in the middle of an overlay
2864 string). In this case move_it_to above will not have
2865 taken us to the start of the continuation line but to the
2866 end of the continued line. */
2867 if (it->current_x > 0
2868 && it->line_wrap != TRUNCATE /* Lines are continued. */
2869 && (/* And glyph doesn't fit on the line. */
2870 new_x > it->last_visible_x
2871 /* Or it fits exactly and we're on a window
2872 system frame. */
2873 || (new_x == it->last_visible_x
2874 && FRAME_WINDOW_P (it->f))))
2875 {
2876 if (it->current.dpvec_index >= 0
2877 || it->current.overlay_string_index >= 0)
2878 {
2879 set_iterator_to_next (it, 1);
2880 move_it_in_display_line_to (it, -1, -1, 0);
2881 }
2882
2883 it->continuation_lines_width += it->current_x;
2884 }
2885
2886 /* We're starting a new display line, not affected by the
2887 height of the continued line, so clear the appropriate
2888 fields in the iterator structure. */
2889 it->max_ascent = it->max_descent = 0;
2890 it->max_phys_ascent = it->max_phys_descent = 0;
2891
2892 it->current_y = first_y;
2893 it->vpos = 0;
2894 it->current_x = it->hpos = 0;
2895 }
2896 }
2897 }
2898
2899
2900 /* Return 1 if POS is a position in ellipses displayed for invisible
2901 text. W is the window we display, for text property lookup. */
2902
2903 static int
2904 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2905 {
2906 Lisp_Object prop, window;
2907 int ellipses_p = 0;
2908 EMACS_INT charpos = CHARPOS (pos->pos);
2909
2910 /* If POS specifies a position in a display vector, this might
2911 be for an ellipsis displayed for invisible text. We won't
2912 get the iterator set up for delivering that ellipsis unless
2913 we make sure that it gets aware of the invisible text. */
2914 if (pos->dpvec_index >= 0
2915 && pos->overlay_string_index < 0
2916 && CHARPOS (pos->string_pos) < 0
2917 && charpos > BEGV
2918 && (XSETWINDOW (window, w),
2919 prop = Fget_char_property (make_number (charpos),
2920 Qinvisible, window),
2921 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2922 {
2923 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2924 window);
2925 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2926 }
2927
2928 return ellipses_p;
2929 }
2930
2931
2932 /* Initialize IT for stepping through current_buffer in window W,
2933 starting at position POS that includes overlay string and display
2934 vector/ control character translation position information. Value
2935 is zero if there are overlay strings with newlines at POS. */
2936
2937 static int
2938 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2939 {
2940 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2941 int i, overlay_strings_with_newlines = 0;
2942
2943 /* If POS specifies a position in a display vector, this might
2944 be for an ellipsis displayed for invisible text. We won't
2945 get the iterator set up for delivering that ellipsis unless
2946 we make sure that it gets aware of the invisible text. */
2947 if (in_ellipses_for_invisible_text_p (pos, w))
2948 {
2949 --charpos;
2950 bytepos = 0;
2951 }
2952
2953 /* Keep in mind: the call to reseat in init_iterator skips invisible
2954 text, so we might end up at a position different from POS. This
2955 is only a problem when POS is a row start after a newline and an
2956 overlay starts there with an after-string, and the overlay has an
2957 invisible property. Since we don't skip invisible text in
2958 display_line and elsewhere immediately after consuming the
2959 newline before the row start, such a POS will not be in a string,
2960 but the call to init_iterator below will move us to the
2961 after-string. */
2962 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2963
2964 /* This only scans the current chunk -- it should scan all chunks.
2965 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2966 to 16 in 22.1 to make this a lesser problem. */
2967 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2968 {
2969 const char *s = SDATA (it->overlay_strings[i]);
2970 const char *e = s + SBYTES (it->overlay_strings[i]);
2971
2972 while (s < e && *s != '\n')
2973 ++s;
2974
2975 if (s < e)
2976 {
2977 overlay_strings_with_newlines = 1;
2978 break;
2979 }
2980 }
2981
2982 /* If position is within an overlay string, set up IT to the right
2983 overlay string. */
2984 if (pos->overlay_string_index >= 0)
2985 {
2986 int relative_index;
2987
2988 /* If the first overlay string happens to have a `display'
2989 property for an image, the iterator will be set up for that
2990 image, and we have to undo that setup first before we can
2991 correct the overlay string index. */
2992 if (it->method == GET_FROM_IMAGE)
2993 pop_it (it);
2994
2995 /* We already have the first chunk of overlay strings in
2996 IT->overlay_strings. Load more until the one for
2997 pos->overlay_string_index is in IT->overlay_strings. */
2998 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2999 {
3000 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3001 it->current.overlay_string_index = 0;
3002 while (n--)
3003 {
3004 load_overlay_strings (it, 0);
3005 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3006 }
3007 }
3008
3009 it->current.overlay_string_index = pos->overlay_string_index;
3010 relative_index = (it->current.overlay_string_index
3011 % OVERLAY_STRING_CHUNK_SIZE);
3012 it->string = it->overlay_strings[relative_index];
3013 xassert (STRINGP (it->string));
3014 it->current.string_pos = pos->string_pos;
3015 it->method = GET_FROM_STRING;
3016 }
3017
3018 if (CHARPOS (pos->string_pos) >= 0)
3019 {
3020 /* Recorded position is not in an overlay string, but in another
3021 string. This can only be a string from a `display' property.
3022 IT should already be filled with that string. */
3023 it->current.string_pos = pos->string_pos;
3024 xassert (STRINGP (it->string));
3025 }
3026
3027 /* Restore position in display vector translations, control
3028 character translations or ellipses. */
3029 if (pos->dpvec_index >= 0)
3030 {
3031 if (it->dpvec == NULL)
3032 get_next_display_element (it);
3033 xassert (it->dpvec && it->current.dpvec_index == 0);
3034 it->current.dpvec_index = pos->dpvec_index;
3035 }
3036
3037 CHECK_IT (it);
3038 return !overlay_strings_with_newlines;
3039 }
3040
3041
3042 /* Initialize IT for stepping through current_buffer in window W
3043 starting at ROW->start. */
3044
3045 static void
3046 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3047 {
3048 init_from_display_pos (it, w, &row->start);
3049 it->start = row->start;
3050 it->continuation_lines_width = row->continuation_lines_width;
3051 CHECK_IT (it);
3052 }
3053
3054
3055 /* Initialize IT for stepping through current_buffer in window W
3056 starting in the line following ROW, i.e. starting at ROW->end.
3057 Value is zero if there are overlay strings with newlines at ROW's
3058 end position. */
3059
3060 static int
3061 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3062 {
3063 int success = 0;
3064
3065 if (init_from_display_pos (it, w, &row->end))
3066 {
3067 if (row->continued_p)
3068 it->continuation_lines_width
3069 = row->continuation_lines_width + row->pixel_width;
3070 CHECK_IT (it);
3071 success = 1;
3072 }
3073
3074 return success;
3075 }
3076
3077
3078
3079 \f
3080 /***********************************************************************
3081 Text properties
3082 ***********************************************************************/
3083
3084 /* Called when IT reaches IT->stop_charpos. Handle text property and
3085 overlay changes. Set IT->stop_charpos to the next position where
3086 to stop. */
3087
3088 static void
3089 handle_stop (struct it *it)
3090 {
3091 enum prop_handled handled;
3092 int handle_overlay_change_p;
3093 struct props *p;
3094
3095 it->dpvec = NULL;
3096 it->current.dpvec_index = -1;
3097 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3098 it->ignore_overlay_strings_at_pos_p = 0;
3099 it->ellipsis_p = 0;
3100
3101 /* Use face of preceding text for ellipsis (if invisible) */
3102 if (it->selective_display_ellipsis_p)
3103 it->saved_face_id = it->face_id;
3104
3105 do
3106 {
3107 handled = HANDLED_NORMALLY;
3108
3109 /* Call text property handlers. */
3110 for (p = it_props; p->handler; ++p)
3111 {
3112 handled = p->handler (it);
3113
3114 if (handled == HANDLED_RECOMPUTE_PROPS)
3115 break;
3116 else if (handled == HANDLED_RETURN)
3117 {
3118 /* We still want to show before and after strings from
3119 overlays even if the actual buffer text is replaced. */
3120 if (!handle_overlay_change_p
3121 || it->sp > 1
3122 || !get_overlay_strings_1 (it, 0, 0))
3123 {
3124 if (it->ellipsis_p)
3125 setup_for_ellipsis (it, 0);
3126 /* When handling a display spec, we might load an
3127 empty string. In that case, discard it here. We
3128 used to discard it in handle_single_display_spec,
3129 but that causes get_overlay_strings_1, above, to
3130 ignore overlay strings that we must check. */
3131 if (STRINGP (it->string) && !SCHARS (it->string))
3132 pop_it (it);
3133 return;
3134 }
3135 else if (STRINGP (it->string) && !SCHARS (it->string))
3136 pop_it (it);
3137 else
3138 {
3139 it->ignore_overlay_strings_at_pos_p = 1;
3140 it->string_from_display_prop_p = 0;
3141 handle_overlay_change_p = 0;
3142 }
3143 handled = HANDLED_RECOMPUTE_PROPS;
3144 break;
3145 }
3146 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3147 handle_overlay_change_p = 0;
3148 }
3149
3150 if (handled != HANDLED_RECOMPUTE_PROPS)
3151 {
3152 /* Don't check for overlay strings below when set to deliver
3153 characters from a display vector. */
3154 if (it->method == GET_FROM_DISPLAY_VECTOR)
3155 handle_overlay_change_p = 0;
3156
3157 /* Handle overlay changes.
3158 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3159 if it finds overlays. */
3160 if (handle_overlay_change_p)
3161 handled = handle_overlay_change (it);
3162 }
3163
3164 if (it->ellipsis_p)
3165 {
3166 setup_for_ellipsis (it, 0);
3167 break;
3168 }
3169 }
3170 while (handled == HANDLED_RECOMPUTE_PROPS);
3171
3172 /* Determine where to stop next. */
3173 if (handled == HANDLED_NORMALLY)
3174 compute_stop_pos (it);
3175 }
3176
3177
3178 /* Compute IT->stop_charpos from text property and overlay change
3179 information for IT's current position. */
3180
3181 static void
3182 compute_stop_pos (struct it *it)
3183 {
3184 register INTERVAL iv, next_iv;
3185 Lisp_Object object, limit, position;
3186 EMACS_INT charpos, bytepos;
3187
3188 /* If nowhere else, stop at the end. */
3189 it->stop_charpos = it->end_charpos;
3190
3191 if (STRINGP (it->string))
3192 {
3193 /* Strings are usually short, so don't limit the search for
3194 properties. */
3195 object = it->string;
3196 limit = Qnil;
3197 charpos = IT_STRING_CHARPOS (*it);
3198 bytepos = IT_STRING_BYTEPOS (*it);
3199 }
3200 else
3201 {
3202 EMACS_INT pos;
3203
3204 /* If next overlay change is in front of the current stop pos
3205 (which is IT->end_charpos), stop there. Note: value of
3206 next_overlay_change is point-max if no overlay change
3207 follows. */
3208 charpos = IT_CHARPOS (*it);
3209 bytepos = IT_BYTEPOS (*it);
3210 pos = next_overlay_change (charpos);
3211 if (pos < it->stop_charpos)
3212 it->stop_charpos = pos;
3213
3214 /* If showing the region, we have to stop at the region
3215 start or end because the face might change there. */
3216 if (it->region_beg_charpos > 0)
3217 {
3218 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3219 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3220 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3221 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3222 }
3223
3224 /* Set up variables for computing the stop position from text
3225 property changes. */
3226 XSETBUFFER (object, current_buffer);
3227 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3228 }
3229
3230 /* Get the interval containing IT's position. Value is a null
3231 interval if there isn't such an interval. */
3232 position = make_number (charpos);
3233 iv = validate_interval_range (object, &position, &position, 0);
3234 if (!NULL_INTERVAL_P (iv))
3235 {
3236 Lisp_Object values_here[LAST_PROP_IDX];
3237 struct props *p;
3238
3239 /* Get properties here. */
3240 for (p = it_props; p->handler; ++p)
3241 values_here[p->idx] = textget (iv->plist, *p->name);
3242
3243 /* Look for an interval following iv that has different
3244 properties. */
3245 for (next_iv = next_interval (iv);
3246 (!NULL_INTERVAL_P (next_iv)
3247 && (NILP (limit)
3248 || XFASTINT (limit) > next_iv->position));
3249 next_iv = next_interval (next_iv))
3250 {
3251 for (p = it_props; p->handler; ++p)
3252 {
3253 Lisp_Object new_value;
3254
3255 new_value = textget (next_iv->plist, *p->name);
3256 if (!EQ (values_here[p->idx], new_value))
3257 break;
3258 }
3259
3260 if (p->handler)
3261 break;
3262 }
3263
3264 if (!NULL_INTERVAL_P (next_iv))
3265 {
3266 if (INTEGERP (limit)
3267 && next_iv->position >= XFASTINT (limit))
3268 /* No text property change up to limit. */
3269 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3270 else
3271 /* Text properties change in next_iv. */
3272 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3273 }
3274 }
3275
3276 if (it->cmp_it.id < 0)
3277 {
3278 EMACS_INT stoppos = it->end_charpos;
3279
3280 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3281 stoppos = -1;
3282 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3283 stoppos, it->string);
3284 }
3285
3286 xassert (STRINGP (it->string)
3287 || (it->stop_charpos >= BEGV
3288 && it->stop_charpos >= IT_CHARPOS (*it)));
3289 }
3290
3291
3292 /* Return the position of the next overlay change after POS in
3293 current_buffer. Value is point-max if no overlay change
3294 follows. This is like `next-overlay-change' but doesn't use
3295 xmalloc. */
3296
3297 static EMACS_INT
3298 next_overlay_change (EMACS_INT pos)
3299 {
3300 int noverlays;
3301 EMACS_INT endpos;
3302 Lisp_Object *overlays;
3303 int i;
3304
3305 /* Get all overlays at the given position. */
3306 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3307
3308 /* If any of these overlays ends before endpos,
3309 use its ending point instead. */
3310 for (i = 0; i < noverlays; ++i)
3311 {
3312 Lisp_Object oend;
3313 EMACS_INT oendpos;
3314
3315 oend = OVERLAY_END (overlays[i]);
3316 oendpos = OVERLAY_POSITION (oend);
3317 endpos = min (endpos, oendpos);
3318 }
3319
3320 return endpos;
3321 }
3322
3323
3324 \f
3325 /***********************************************************************
3326 Fontification
3327 ***********************************************************************/
3328
3329 /* Handle changes in the `fontified' property of the current buffer by
3330 calling hook functions from Qfontification_functions to fontify
3331 regions of text. */
3332
3333 static enum prop_handled
3334 handle_fontified_prop (struct it *it)
3335 {
3336 Lisp_Object prop, pos;
3337 enum prop_handled handled = HANDLED_NORMALLY;
3338
3339 if (!NILP (Vmemory_full))
3340 return handled;
3341
3342 /* Get the value of the `fontified' property at IT's current buffer
3343 position. (The `fontified' property doesn't have a special
3344 meaning in strings.) If the value is nil, call functions from
3345 Qfontification_functions. */
3346 if (!STRINGP (it->string)
3347 && it->s == NULL
3348 && !NILP (Vfontification_functions)
3349 && !NILP (Vrun_hooks)
3350 && (pos = make_number (IT_CHARPOS (*it)),
3351 prop = Fget_char_property (pos, Qfontified, Qnil),
3352 /* Ignore the special cased nil value always present at EOB since
3353 no amount of fontifying will be able to change it. */
3354 NILP (prop) && IT_CHARPOS (*it) < Z))
3355 {
3356 int count = SPECPDL_INDEX ();
3357 Lisp_Object val;
3358
3359 val = Vfontification_functions;
3360 specbind (Qfontification_functions, Qnil);
3361
3362 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3363 safe_call1 (val, pos);
3364 else
3365 {
3366 Lisp_Object globals, fn;
3367 struct gcpro gcpro1, gcpro2;
3368
3369 globals = Qnil;
3370 GCPRO2 (val, globals);
3371
3372 for (; CONSP (val); val = XCDR (val))
3373 {
3374 fn = XCAR (val);
3375
3376 if (EQ (fn, Qt))
3377 {
3378 /* A value of t indicates this hook has a local
3379 binding; it means to run the global binding too.
3380 In a global value, t should not occur. If it
3381 does, we must ignore it to avoid an endless
3382 loop. */
3383 for (globals = Fdefault_value (Qfontification_functions);
3384 CONSP (globals);
3385 globals = XCDR (globals))
3386 {
3387 fn = XCAR (globals);
3388 if (!EQ (fn, Qt))
3389 safe_call1 (fn, pos);
3390 }
3391 }
3392 else
3393 safe_call1 (fn, pos);
3394 }
3395
3396 UNGCPRO;
3397 }
3398
3399 unbind_to (count, Qnil);
3400
3401 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3402 something. This avoids an endless loop if they failed to
3403 fontify the text for which reason ever. */
3404 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3405 handled = HANDLED_RECOMPUTE_PROPS;
3406 }
3407
3408 return handled;
3409 }
3410
3411
3412 \f
3413 /***********************************************************************
3414 Faces
3415 ***********************************************************************/
3416
3417 /* Set up iterator IT from face properties at its current position.
3418 Called from handle_stop. */
3419
3420 static enum prop_handled
3421 handle_face_prop (struct it *it)
3422 {
3423 int new_face_id;
3424 EMACS_INT next_stop;
3425
3426 if (!STRINGP (it->string))
3427 {
3428 new_face_id
3429 = face_at_buffer_position (it->w,
3430 IT_CHARPOS (*it),
3431 it->region_beg_charpos,
3432 it->region_end_charpos,
3433 &next_stop,
3434 (IT_CHARPOS (*it)
3435 + TEXT_PROP_DISTANCE_LIMIT),
3436 0, it->base_face_id);
3437
3438 /* Is this a start of a run of characters with box face?
3439 Caveat: this can be called for a freshly initialized
3440 iterator; face_id is -1 in this case. We know that the new
3441 face will not change until limit, i.e. if the new face has a
3442 box, all characters up to limit will have one. But, as
3443 usual, we don't know whether limit is really the end. */
3444 if (new_face_id != it->face_id)
3445 {
3446 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3447
3448 /* If new face has a box but old face has not, this is
3449 the start of a run of characters with box, i.e. it has
3450 a shadow on the left side. The value of face_id of the
3451 iterator will be -1 if this is the initial call that gets
3452 the face. In this case, we have to look in front of IT's
3453 position and see whether there is a face != new_face_id. */
3454 it->start_of_box_run_p
3455 = (new_face->box != FACE_NO_BOX
3456 && (it->face_id >= 0
3457 || IT_CHARPOS (*it) == BEG
3458 || new_face_id != face_before_it_pos (it)));
3459 it->face_box_p = new_face->box != FACE_NO_BOX;
3460 }
3461 }
3462 else
3463 {
3464 int base_face_id;
3465 EMACS_INT bufpos;
3466 int i;
3467 Lisp_Object from_overlay
3468 = (it->current.overlay_string_index >= 0
3469 ? it->string_overlays[it->current.overlay_string_index]
3470 : Qnil);
3471
3472 /* See if we got to this string directly or indirectly from
3473 an overlay property. That includes the before-string or
3474 after-string of an overlay, strings in display properties
3475 provided by an overlay, their text properties, etc.
3476
3477 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3478 if (! NILP (from_overlay))
3479 for (i = it->sp - 1; i >= 0; i--)
3480 {
3481 if (it->stack[i].current.overlay_string_index >= 0)
3482 from_overlay
3483 = it->string_overlays[it->stack[i].current.overlay_string_index];
3484 else if (! NILP (it->stack[i].from_overlay))
3485 from_overlay = it->stack[i].from_overlay;
3486
3487 if (!NILP (from_overlay))
3488 break;
3489 }
3490
3491 if (! NILP (from_overlay))
3492 {
3493 bufpos = IT_CHARPOS (*it);
3494 /* For a string from an overlay, the base face depends
3495 only on text properties and ignores overlays. */
3496 base_face_id
3497 = face_for_overlay_string (it->w,
3498 IT_CHARPOS (*it),
3499 it->region_beg_charpos,
3500 it->region_end_charpos,
3501 &next_stop,
3502 (IT_CHARPOS (*it)
3503 + TEXT_PROP_DISTANCE_LIMIT),
3504 0,
3505 from_overlay);
3506 }
3507 else
3508 {
3509 bufpos = 0;
3510
3511 /* For strings from a `display' property, use the face at
3512 IT's current buffer position as the base face to merge
3513 with, so that overlay strings appear in the same face as
3514 surrounding text, unless they specify their own
3515 faces. */
3516 base_face_id = underlying_face_id (it);
3517 }
3518
3519 new_face_id = face_at_string_position (it->w,
3520 it->string,
3521 IT_STRING_CHARPOS (*it),
3522 bufpos,
3523 it->region_beg_charpos,
3524 it->region_end_charpos,
3525 &next_stop,
3526 base_face_id, 0);
3527
3528 /* Is this a start of a run of characters with box? Caveat:
3529 this can be called for a freshly allocated iterator; face_id
3530 is -1 is this case. We know that the new face will not
3531 change until the next check pos, i.e. if the new face has a
3532 box, all characters up to that position will have a
3533 box. But, as usual, we don't know whether that position
3534 is really the end. */
3535 if (new_face_id != it->face_id)
3536 {
3537 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3538 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3539
3540 /* If new face has a box but old face hasn't, this is the
3541 start of a run of characters with box, i.e. it has a
3542 shadow on the left side. */
3543 it->start_of_box_run_p
3544 = new_face->box && (old_face == NULL || !old_face->box);
3545 it->face_box_p = new_face->box != FACE_NO_BOX;
3546 }
3547 }
3548
3549 it->face_id = new_face_id;
3550 return HANDLED_NORMALLY;
3551 }
3552
3553
3554 /* Return the ID of the face ``underlying'' IT's current position,
3555 which is in a string. If the iterator is associated with a
3556 buffer, return the face at IT's current buffer position.
3557 Otherwise, use the iterator's base_face_id. */
3558
3559 static int
3560 underlying_face_id (struct it *it)
3561 {
3562 int face_id = it->base_face_id, i;
3563
3564 xassert (STRINGP (it->string));
3565
3566 for (i = it->sp - 1; i >= 0; --i)
3567 if (NILP (it->stack[i].string))
3568 face_id = it->stack[i].face_id;
3569
3570 return face_id;
3571 }
3572
3573
3574 /* Compute the face one character before or after the current position
3575 of IT. BEFORE_P non-zero means get the face in front of IT's
3576 position. Value is the id of the face. */
3577
3578 static int
3579 face_before_or_after_it_pos (struct it *it, int before_p)
3580 {
3581 int face_id, limit;
3582 EMACS_INT next_check_charpos;
3583 struct text_pos pos;
3584
3585 xassert (it->s == NULL);
3586
3587 if (STRINGP (it->string))
3588 {
3589 EMACS_INT bufpos;
3590 int base_face_id;
3591
3592 /* No face change past the end of the string (for the case
3593 we are padding with spaces). No face change before the
3594 string start. */
3595 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3596 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3597 return it->face_id;
3598
3599 /* Set pos to the position before or after IT's current position. */
3600 if (before_p)
3601 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3602 else
3603 /* For composition, we must check the character after the
3604 composition. */
3605 pos = (it->what == IT_COMPOSITION
3606 ? string_pos (IT_STRING_CHARPOS (*it)
3607 + it->cmp_it.nchars, it->string)
3608 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3609
3610 if (it->current.overlay_string_index >= 0)
3611 bufpos = IT_CHARPOS (*it);
3612 else
3613 bufpos = 0;
3614
3615 base_face_id = underlying_face_id (it);
3616
3617 /* Get the face for ASCII, or unibyte. */
3618 face_id = face_at_string_position (it->w,
3619 it->string,
3620 CHARPOS (pos),
3621 bufpos,
3622 it->region_beg_charpos,
3623 it->region_end_charpos,
3624 &next_check_charpos,
3625 base_face_id, 0);
3626
3627 /* Correct the face for charsets different from ASCII. Do it
3628 for the multibyte case only. The face returned above is
3629 suitable for unibyte text if IT->string is unibyte. */
3630 if (STRING_MULTIBYTE (it->string))
3631 {
3632 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3633 int c, len;
3634 struct face *face = FACE_FROM_ID (it->f, face_id);
3635
3636 c = string_char_and_length (p, &len);
3637 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3638 }
3639 }
3640 else
3641 {
3642 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3643 || (IT_CHARPOS (*it) <= BEGV && before_p))
3644 return it->face_id;
3645
3646 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3647 pos = it->current.pos;
3648
3649 if (before_p)
3650 DEC_TEXT_POS (pos, it->multibyte_p);
3651 else
3652 {
3653 if (it->what == IT_COMPOSITION)
3654 /* For composition, we must check the position after the
3655 composition. */
3656 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3657 else
3658 INC_TEXT_POS (pos, it->multibyte_p);
3659 }
3660
3661 /* Determine face for CHARSET_ASCII, or unibyte. */
3662 face_id = face_at_buffer_position (it->w,
3663 CHARPOS (pos),
3664 it->region_beg_charpos,
3665 it->region_end_charpos,
3666 &next_check_charpos,
3667 limit, 0, -1);
3668
3669 /* Correct the face for charsets different from ASCII. Do it
3670 for the multibyte case only. The face returned above is
3671 suitable for unibyte text if current_buffer is unibyte. */
3672 if (it->multibyte_p)
3673 {
3674 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3675 struct face *face = FACE_FROM_ID (it->f, face_id);
3676 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3677 }
3678 }
3679
3680 return face_id;
3681 }
3682
3683
3684 \f
3685 /***********************************************************************
3686 Invisible text
3687 ***********************************************************************/
3688
3689 /* Set up iterator IT from invisible properties at its current
3690 position. Called from handle_stop. */
3691
3692 static enum prop_handled
3693 handle_invisible_prop (struct it *it)
3694 {
3695 enum prop_handled handled = HANDLED_NORMALLY;
3696
3697 if (STRINGP (it->string))
3698 {
3699 Lisp_Object prop, end_charpos, limit, charpos;
3700
3701 /* Get the value of the invisible text property at the
3702 current position. Value will be nil if there is no such
3703 property. */
3704 charpos = make_number (IT_STRING_CHARPOS (*it));
3705 prop = Fget_text_property (charpos, Qinvisible, it->string);
3706
3707 if (!NILP (prop)
3708 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3709 {
3710 handled = HANDLED_RECOMPUTE_PROPS;
3711
3712 /* Get the position at which the next change of the
3713 invisible text property can be found in IT->string.
3714 Value will be nil if the property value is the same for
3715 all the rest of IT->string. */
3716 XSETINT (limit, SCHARS (it->string));
3717 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3718 it->string, limit);
3719
3720 /* Text at current position is invisible. The next
3721 change in the property is at position end_charpos.
3722 Move IT's current position to that position. */
3723 if (INTEGERP (end_charpos)
3724 && XFASTINT (end_charpos) < XFASTINT (limit))
3725 {
3726 struct text_pos old;
3727 old = it->current.string_pos;
3728 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3729 compute_string_pos (&it->current.string_pos, old, it->string);
3730 }
3731 else
3732 {
3733 /* The rest of the string is invisible. If this is an
3734 overlay string, proceed with the next overlay string
3735 or whatever comes and return a character from there. */
3736 if (it->current.overlay_string_index >= 0)
3737 {
3738 next_overlay_string (it);
3739 /* Don't check for overlay strings when we just
3740 finished processing them. */
3741 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3742 }
3743 else
3744 {
3745 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3746 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3747 }
3748 }
3749 }
3750 }
3751 else
3752 {
3753 int invis_p;
3754 EMACS_INT newpos, next_stop, start_charpos, tem;
3755 Lisp_Object pos, prop, overlay;
3756
3757 /* First of all, is there invisible text at this position? */
3758 tem = start_charpos = IT_CHARPOS (*it);
3759 pos = make_number (tem);
3760 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3761 &overlay);
3762 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3763
3764 /* If we are on invisible text, skip over it. */
3765 if (invis_p && start_charpos < it->end_charpos)
3766 {
3767 /* Record whether we have to display an ellipsis for the
3768 invisible text. */
3769 int display_ellipsis_p = invis_p == 2;
3770
3771 handled = HANDLED_RECOMPUTE_PROPS;
3772
3773 /* Loop skipping over invisible text. The loop is left at
3774 ZV or with IT on the first char being visible again. */
3775 do
3776 {
3777 /* Try to skip some invisible text. Return value is the
3778 position reached which can be equal to where we start
3779 if there is nothing invisible there. This skips both
3780 over invisible text properties and overlays with
3781 invisible property. */
3782 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3783
3784 /* If we skipped nothing at all we weren't at invisible
3785 text in the first place. If everything to the end of
3786 the buffer was skipped, end the loop. */
3787 if (newpos == tem || newpos >= ZV)
3788 invis_p = 0;
3789 else
3790 {
3791 /* We skipped some characters but not necessarily
3792 all there are. Check if we ended up on visible
3793 text. Fget_char_property returns the property of
3794 the char before the given position, i.e. if we
3795 get invis_p = 0, this means that the char at
3796 newpos is visible. */
3797 pos = make_number (newpos);
3798 prop = Fget_char_property (pos, Qinvisible, it->window);
3799 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3800 }
3801
3802 /* If we ended up on invisible text, proceed to
3803 skip starting with next_stop. */
3804 if (invis_p)
3805 tem = next_stop;
3806
3807 /* If there are adjacent invisible texts, don't lose the
3808 second one's ellipsis. */
3809 if (invis_p == 2)
3810 display_ellipsis_p = 1;
3811 }
3812 while (invis_p);
3813
3814 /* The position newpos is now either ZV or on visible text. */
3815 if (it->bidi_p && newpos < ZV)
3816 {
3817 /* With bidi iteration, the region of invisible text
3818 could start and/or end in the middle of a non-base
3819 embedding level. Therefore, we need to skip
3820 invisible text using the bidi iterator, starting at
3821 IT's current position, until we find ourselves
3822 outside the invisible text. Skipping invisible text
3823 _after_ bidi iteration avoids affecting the visual
3824 order of the displayed text when invisible properties
3825 are added or removed. */
3826 if (it->bidi_it.first_elt)
3827 {
3828 /* If we were `reseat'ed to a new paragraph,
3829 determine the paragraph base direction. We need
3830 to do it now because next_element_from_buffer may
3831 not have a chance to do it, if we are going to
3832 skip any text at the beginning, which resets the
3833 FIRST_ELT flag. */
3834 bidi_paragraph_init (it->paragraph_embedding,
3835 &it->bidi_it, 1);
3836 }
3837 do
3838 {
3839 bidi_move_to_visually_next (&it->bidi_it);
3840 }
3841 while (it->stop_charpos <= it->bidi_it.charpos
3842 && it->bidi_it.charpos < newpos);
3843 IT_CHARPOS (*it) = it->bidi_it.charpos;
3844 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3845 /* If we overstepped NEWPOS, record its position in the
3846 iterator, so that we skip invisible text if later the
3847 bidi iteration lands us in the invisible region
3848 again. */
3849 if (IT_CHARPOS (*it) >= newpos)
3850 it->prev_stop = newpos;
3851 }
3852 else
3853 {
3854 IT_CHARPOS (*it) = newpos;
3855 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3856 }
3857
3858 /* If there are before-strings at the start of invisible
3859 text, and the text is invisible because of a text
3860 property, arrange to show before-strings because 20.x did
3861 it that way. (If the text is invisible because of an
3862 overlay property instead of a text property, this is
3863 already handled in the overlay code.) */
3864 if (NILP (overlay)
3865 && get_overlay_strings (it, it->stop_charpos))
3866 {
3867 handled = HANDLED_RECOMPUTE_PROPS;
3868 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3869 }
3870 else if (display_ellipsis_p)
3871 {
3872 /* Make sure that the glyphs of the ellipsis will get
3873 correct `charpos' values. If we would not update
3874 it->position here, the glyphs would belong to the
3875 last visible character _before_ the invisible
3876 text, which confuses `set_cursor_from_row'.
3877
3878 We use the last invisible position instead of the
3879 first because this way the cursor is always drawn on
3880 the first "." of the ellipsis, whenever PT is inside
3881 the invisible text. Otherwise the cursor would be
3882 placed _after_ the ellipsis when the point is after the
3883 first invisible character. */
3884 if (!STRINGP (it->object))
3885 {
3886 it->position.charpos = newpos - 1;
3887 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3888 }
3889 it->ellipsis_p = 1;
3890 /* Let the ellipsis display before
3891 considering any properties of the following char.
3892 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3893 handled = HANDLED_RETURN;
3894 }
3895 }
3896 }
3897
3898 return handled;
3899 }
3900
3901
3902 /* Make iterator IT return `...' next.
3903 Replaces LEN characters from buffer. */
3904
3905 static void
3906 setup_for_ellipsis (struct it *it, int len)
3907 {
3908 /* Use the display table definition for `...'. Invalid glyphs
3909 will be handled by the method returning elements from dpvec. */
3910 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3911 {
3912 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3913 it->dpvec = v->contents;
3914 it->dpend = v->contents + v->size;
3915 }
3916 else
3917 {
3918 /* Default `...'. */
3919 it->dpvec = default_invis_vector;
3920 it->dpend = default_invis_vector + 3;
3921 }
3922
3923 it->dpvec_char_len = len;
3924 it->current.dpvec_index = 0;
3925 it->dpvec_face_id = -1;
3926
3927 /* Remember the current face id in case glyphs specify faces.
3928 IT's face is restored in set_iterator_to_next.
3929 saved_face_id was set to preceding char's face in handle_stop. */
3930 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3931 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3932
3933 it->method = GET_FROM_DISPLAY_VECTOR;
3934 it->ellipsis_p = 1;
3935 }
3936
3937
3938 \f
3939 /***********************************************************************
3940 'display' property
3941 ***********************************************************************/
3942
3943 /* Set up iterator IT from `display' property at its current position.
3944 Called from handle_stop.
3945 We return HANDLED_RETURN if some part of the display property
3946 overrides the display of the buffer text itself.
3947 Otherwise we return HANDLED_NORMALLY. */
3948
3949 static enum prop_handled
3950 handle_display_prop (struct it *it)
3951 {
3952 Lisp_Object prop, object, overlay;
3953 struct text_pos *position;
3954 /* Nonzero if some property replaces the display of the text itself. */
3955 int display_replaced_p = 0;
3956
3957 if (STRINGP (it->string))
3958 {
3959 object = it->string;
3960 position = &it->current.string_pos;
3961 }
3962 else
3963 {
3964 XSETWINDOW (object, it->w);
3965 position = &it->current.pos;
3966 }
3967
3968 /* Reset those iterator values set from display property values. */
3969 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3970 it->space_width = Qnil;
3971 it->font_height = Qnil;
3972 it->voffset = 0;
3973
3974 /* We don't support recursive `display' properties, i.e. string
3975 values that have a string `display' property, that have a string
3976 `display' property etc. */
3977 if (!it->string_from_display_prop_p)
3978 it->area = TEXT_AREA;
3979
3980 prop = get_char_property_and_overlay (make_number (position->charpos),
3981 Qdisplay, object, &overlay);
3982 if (NILP (prop))
3983 return HANDLED_NORMALLY;
3984 /* Now OVERLAY is the overlay that gave us this property, or nil
3985 if it was a text property. */
3986
3987 if (!STRINGP (it->string))
3988 object = it->w->buffer;
3989
3990 if (CONSP (prop)
3991 /* Simple properties. */
3992 && !EQ (XCAR (prop), Qimage)
3993 && !EQ (XCAR (prop), Qspace)
3994 && !EQ (XCAR (prop), Qwhen)
3995 && !EQ (XCAR (prop), Qslice)
3996 && !EQ (XCAR (prop), Qspace_width)
3997 && !EQ (XCAR (prop), Qheight)
3998 && !EQ (XCAR (prop), Qraise)
3999 /* Marginal area specifications. */
4000 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
4001 && !EQ (XCAR (prop), Qleft_fringe)
4002 && !EQ (XCAR (prop), Qright_fringe)
4003 && !NILP (XCAR (prop)))
4004 {
4005 for (; CONSP (prop); prop = XCDR (prop))
4006 {
4007 if (handle_single_display_spec (it, XCAR (prop), 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 }
4018 else if (VECTORP (prop))
4019 {
4020 int i;
4021 for (i = 0; i < ASIZE (prop); ++i)
4022 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4023 position, display_replaced_p))
4024 {
4025 display_replaced_p = 1;
4026 /* If some text in a string is replaced, `position' no
4027 longer points to the position of `object'. */
4028 if (STRINGP (object))
4029 break;
4030 }
4031 }
4032 else
4033 {
4034 if (handle_single_display_spec (it, prop, object, overlay,
4035 position, 0))
4036 display_replaced_p = 1;
4037 }
4038
4039 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4040 }
4041
4042
4043 /* Value is the position of the end of the `display' property starting
4044 at START_POS in OBJECT. */
4045
4046 static struct text_pos
4047 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4048 {
4049 Lisp_Object end;
4050 struct text_pos end_pos;
4051
4052 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4053 Qdisplay, object, Qnil);
4054 CHARPOS (end_pos) = XFASTINT (end);
4055 if (STRINGP (object))
4056 compute_string_pos (&end_pos, start_pos, it->string);
4057 else
4058 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4059
4060 return end_pos;
4061 }
4062
4063
4064 /* Set up IT from a single `display' specification PROP. OBJECT
4065 is the object in which the `display' property was found. *POSITION
4066 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4067 means that we previously saw a display specification which already
4068 replaced text display with something else, for example an image;
4069 we ignore such properties after the first one has been processed.
4070
4071 OVERLAY is the overlay this `display' property came from,
4072 or nil if it was a text property.
4073
4074 If PROP is a `space' or `image' specification, and in some other
4075 cases too, set *POSITION to the position where the `display'
4076 property ends.
4077
4078 Value is non-zero if something was found which replaces the display
4079 of buffer or string text. */
4080
4081 static int
4082 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4083 Lisp_Object overlay, struct text_pos *position,
4084 int display_replaced_before_p)
4085 {
4086 Lisp_Object form;
4087 Lisp_Object location, value;
4088 struct text_pos start_pos, save_pos;
4089 int valid_p;
4090
4091 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4092 If the result is non-nil, use VALUE instead of SPEC. */
4093 form = Qt;
4094 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4095 {
4096 spec = XCDR (spec);
4097 if (!CONSP (spec))
4098 return 0;
4099 form = XCAR (spec);
4100 spec = XCDR (spec);
4101 }
4102
4103 if (!NILP (form) && !EQ (form, Qt))
4104 {
4105 int count = SPECPDL_INDEX ();
4106 struct gcpro gcpro1;
4107
4108 /* Bind `object' to the object having the `display' property, a
4109 buffer or string. Bind `position' to the position in the
4110 object where the property was found, and `buffer-position'
4111 to the current position in the buffer. */
4112 specbind (Qobject, object);
4113 specbind (Qposition, make_number (CHARPOS (*position)));
4114 specbind (Qbuffer_position,
4115 make_number (STRINGP (object)
4116 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4117 GCPRO1 (form);
4118 form = safe_eval (form);
4119 UNGCPRO;
4120 unbind_to (count, Qnil);
4121 }
4122
4123 if (NILP (form))
4124 return 0;
4125
4126 /* Handle `(height HEIGHT)' specifications. */
4127 if (CONSP (spec)
4128 && EQ (XCAR (spec), Qheight)
4129 && CONSP (XCDR (spec)))
4130 {
4131 if (!FRAME_WINDOW_P (it->f))
4132 return 0;
4133
4134 it->font_height = XCAR (XCDR (spec));
4135 if (!NILP (it->font_height))
4136 {
4137 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4138 int new_height = -1;
4139
4140 if (CONSP (it->font_height)
4141 && (EQ (XCAR (it->font_height), Qplus)
4142 || EQ (XCAR (it->font_height), Qminus))
4143 && CONSP (XCDR (it->font_height))
4144 && INTEGERP (XCAR (XCDR (it->font_height))))
4145 {
4146 /* `(+ N)' or `(- N)' where N is an integer. */
4147 int steps = XINT (XCAR (XCDR (it->font_height)));
4148 if (EQ (XCAR (it->font_height), Qplus))
4149 steps = - steps;
4150 it->face_id = smaller_face (it->f, it->face_id, steps);
4151 }
4152 else if (FUNCTIONP (it->font_height))
4153 {
4154 /* Call function with current height as argument.
4155 Value is the new height. */
4156 Lisp_Object height;
4157 height = safe_call1 (it->font_height,
4158 face->lface[LFACE_HEIGHT_INDEX]);
4159 if (NUMBERP (height))
4160 new_height = XFLOATINT (height);
4161 }
4162 else if (NUMBERP (it->font_height))
4163 {
4164 /* Value is a multiple of the canonical char height. */
4165 struct face *face;
4166
4167 face = FACE_FROM_ID (it->f,
4168 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4169 new_height = (XFLOATINT (it->font_height)
4170 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4171 }
4172 else
4173 {
4174 /* Evaluate IT->font_height with `height' bound to the
4175 current specified height to get the new height. */
4176 int count = SPECPDL_INDEX ();
4177
4178 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4179 value = safe_eval (it->font_height);
4180 unbind_to (count, Qnil);
4181
4182 if (NUMBERP (value))
4183 new_height = XFLOATINT (value);
4184 }
4185
4186 if (new_height > 0)
4187 it->face_id = face_with_height (it->f, it->face_id, new_height);
4188 }
4189
4190 return 0;
4191 }
4192
4193 /* Handle `(space-width WIDTH)'. */
4194 if (CONSP (spec)
4195 && EQ (XCAR (spec), Qspace_width)
4196 && CONSP (XCDR (spec)))
4197 {
4198 if (!FRAME_WINDOW_P (it->f))
4199 return 0;
4200
4201 value = XCAR (XCDR (spec));
4202 if (NUMBERP (value) && XFLOATINT (value) > 0)
4203 it->space_width = value;
4204
4205 return 0;
4206 }
4207
4208 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4209 if (CONSP (spec)
4210 && EQ (XCAR (spec), Qslice))
4211 {
4212 Lisp_Object tem;
4213
4214 if (!FRAME_WINDOW_P (it->f))
4215 return 0;
4216
4217 if (tem = XCDR (spec), CONSP (tem))
4218 {
4219 it->slice.x = XCAR (tem);
4220 if (tem = XCDR (tem), CONSP (tem))
4221 {
4222 it->slice.y = XCAR (tem);
4223 if (tem = XCDR (tem), CONSP (tem))
4224 {
4225 it->slice.width = XCAR (tem);
4226 if (tem = XCDR (tem), CONSP (tem))
4227 it->slice.height = XCAR (tem);
4228 }
4229 }
4230 }
4231
4232 return 0;
4233 }
4234
4235 /* Handle `(raise FACTOR)'. */
4236 if (CONSP (spec)
4237 && EQ (XCAR (spec), Qraise)
4238 && CONSP (XCDR (spec)))
4239 {
4240 if (!FRAME_WINDOW_P (it->f))
4241 return 0;
4242
4243 #ifdef HAVE_WINDOW_SYSTEM
4244 value = XCAR (XCDR (spec));
4245 if (NUMBERP (value))
4246 {
4247 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4248 it->voffset = - (XFLOATINT (value)
4249 * (FONT_HEIGHT (face->font)));
4250 }
4251 #endif /* HAVE_WINDOW_SYSTEM */
4252
4253 return 0;
4254 }
4255
4256 /* Don't handle the other kinds of display specifications
4257 inside a string that we got from a `display' property. */
4258 if (it->string_from_display_prop_p)
4259 return 0;
4260
4261 /* Characters having this form of property are not displayed, so
4262 we have to find the end of the property. */
4263 start_pos = *position;
4264 *position = display_prop_end (it, object, start_pos);
4265 value = Qnil;
4266
4267 /* Stop the scan at that end position--we assume that all
4268 text properties change there. */
4269 it->stop_charpos = position->charpos;
4270
4271 /* Handle `(left-fringe BITMAP [FACE])'
4272 and `(right-fringe BITMAP [FACE])'. */
4273 if (CONSP (spec)
4274 && (EQ (XCAR (spec), Qleft_fringe)
4275 || EQ (XCAR (spec), Qright_fringe))
4276 && CONSP (XCDR (spec)))
4277 {
4278 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4279 int fringe_bitmap;
4280
4281 if (!FRAME_WINDOW_P (it->f))
4282 /* If we return here, POSITION has been advanced
4283 across the text with this property. */
4284 return 0;
4285
4286 #ifdef HAVE_WINDOW_SYSTEM
4287 value = XCAR (XCDR (spec));
4288 if (!SYMBOLP (value)
4289 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4290 /* If we return here, POSITION has been advanced
4291 across the text with this property. */
4292 return 0;
4293
4294 if (CONSP (XCDR (XCDR (spec))))
4295 {
4296 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4297 int face_id2 = lookup_derived_face (it->f, face_name,
4298 FRINGE_FACE_ID, 0);
4299 if (face_id2 >= 0)
4300 face_id = face_id2;
4301 }
4302
4303 /* Save current settings of IT so that we can restore them
4304 when we are finished with the glyph property value. */
4305
4306 save_pos = it->position;
4307 it->position = *position;
4308 push_it (it);
4309 it->position = save_pos;
4310
4311 it->area = TEXT_AREA;
4312 it->what = IT_IMAGE;
4313 it->image_id = -1; /* no image */
4314 it->position = start_pos;
4315 it->object = NILP (object) ? it->w->buffer : object;
4316 it->method = GET_FROM_IMAGE;
4317 it->from_overlay = Qnil;
4318 it->face_id = face_id;
4319
4320 /* Say that we haven't consumed the characters with
4321 `display' property yet. The call to pop_it in
4322 set_iterator_to_next will clean this up. */
4323 *position = start_pos;
4324
4325 if (EQ (XCAR (spec), Qleft_fringe))
4326 {
4327 it->left_user_fringe_bitmap = fringe_bitmap;
4328 it->left_user_fringe_face_id = face_id;
4329 }
4330 else
4331 {
4332 it->right_user_fringe_bitmap = fringe_bitmap;
4333 it->right_user_fringe_face_id = face_id;
4334 }
4335 #endif /* HAVE_WINDOW_SYSTEM */
4336 return 1;
4337 }
4338
4339 /* Prepare to handle `((margin left-margin) ...)',
4340 `((margin right-margin) ...)' and `((margin nil) ...)'
4341 prefixes for display specifications. */
4342 location = Qunbound;
4343 if (CONSP (spec) && CONSP (XCAR (spec)))
4344 {
4345 Lisp_Object tem;
4346
4347 value = XCDR (spec);
4348 if (CONSP (value))
4349 value = XCAR (value);
4350
4351 tem = XCAR (spec);
4352 if (EQ (XCAR (tem), Qmargin)
4353 && (tem = XCDR (tem),
4354 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4355 (NILP (tem)
4356 || EQ (tem, Qleft_margin)
4357 || EQ (tem, Qright_margin))))
4358 location = tem;
4359 }
4360
4361 if (EQ (location, Qunbound))
4362 {
4363 location = Qnil;
4364 value = spec;
4365 }
4366
4367 /* After this point, VALUE is the property after any
4368 margin prefix has been stripped. It must be a string,
4369 an image specification, or `(space ...)'.
4370
4371 LOCATION specifies where to display: `left-margin',
4372 `right-margin' or nil. */
4373
4374 valid_p = (STRINGP (value)
4375 #ifdef HAVE_WINDOW_SYSTEM
4376 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4377 #endif /* not HAVE_WINDOW_SYSTEM */
4378 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4379
4380 if (valid_p && !display_replaced_before_p)
4381 {
4382 /* Save current settings of IT so that we can restore them
4383 when we are finished with the glyph property value. */
4384 save_pos = it->position;
4385 it->position = *position;
4386 push_it (it);
4387 it->position = save_pos;
4388 it->from_overlay = overlay;
4389
4390 if (NILP (location))
4391 it->area = TEXT_AREA;
4392 else if (EQ (location, Qleft_margin))
4393 it->area = LEFT_MARGIN_AREA;
4394 else
4395 it->area = RIGHT_MARGIN_AREA;
4396
4397 if (STRINGP (value))
4398 {
4399 it->string = value;
4400 it->multibyte_p = STRING_MULTIBYTE (it->string);
4401 it->current.overlay_string_index = -1;
4402 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4403 it->end_charpos = it->string_nchars = SCHARS (it->string);
4404 it->method = GET_FROM_STRING;
4405 it->stop_charpos = 0;
4406 it->string_from_display_prop_p = 1;
4407 /* Say that we haven't consumed the characters with
4408 `display' property yet. The call to pop_it in
4409 set_iterator_to_next will clean this up. */
4410 if (BUFFERP (object))
4411 *position = start_pos;
4412 }
4413 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4414 {
4415 it->method = GET_FROM_STRETCH;
4416 it->object = value;
4417 *position = it->position = start_pos;
4418 }
4419 #ifdef HAVE_WINDOW_SYSTEM
4420 else
4421 {
4422 it->what = IT_IMAGE;
4423 it->image_id = lookup_image (it->f, value);
4424 it->position = start_pos;
4425 it->object = NILP (object) ? it->w->buffer : object;
4426 it->method = GET_FROM_IMAGE;
4427
4428 /* Say that we haven't consumed the characters with
4429 `display' property yet. The call to pop_it in
4430 set_iterator_to_next will clean this up. */
4431 *position = start_pos;
4432 }
4433 #endif /* HAVE_WINDOW_SYSTEM */
4434
4435 return 1;
4436 }
4437
4438 /* Invalid property or property not supported. Restore
4439 POSITION to what it was before. */
4440 *position = start_pos;
4441 return 0;
4442 }
4443
4444
4445 /* Check if SPEC is a display sub-property value whose text should be
4446 treated as intangible. */
4447
4448 static int
4449 single_display_spec_intangible_p (Lisp_Object prop)
4450 {
4451 /* Skip over `when FORM'. */
4452 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4453 {
4454 prop = XCDR (prop);
4455 if (!CONSP (prop))
4456 return 0;
4457 prop = XCDR (prop);
4458 }
4459
4460 if (STRINGP (prop))
4461 return 1;
4462
4463 if (!CONSP (prop))
4464 return 0;
4465
4466 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4467 we don't need to treat text as intangible. */
4468 if (EQ (XCAR (prop), Qmargin))
4469 {
4470 prop = XCDR (prop);
4471 if (!CONSP (prop))
4472 return 0;
4473
4474 prop = XCDR (prop);
4475 if (!CONSP (prop)
4476 || EQ (XCAR (prop), Qleft_margin)
4477 || EQ (XCAR (prop), Qright_margin))
4478 return 0;
4479 }
4480
4481 return (CONSP (prop)
4482 && (EQ (XCAR (prop), Qimage)
4483 || EQ (XCAR (prop), Qspace)));
4484 }
4485
4486
4487 /* Check if PROP is a display property value whose text should be
4488 treated as intangible. */
4489
4490 int
4491 display_prop_intangible_p (Lisp_Object prop)
4492 {
4493 if (CONSP (prop)
4494 && CONSP (XCAR (prop))
4495 && !EQ (Qmargin, XCAR (XCAR (prop))))
4496 {
4497 /* A list of sub-properties. */
4498 while (CONSP (prop))
4499 {
4500 if (single_display_spec_intangible_p (XCAR (prop)))
4501 return 1;
4502 prop = XCDR (prop);
4503 }
4504 }
4505 else if (VECTORP (prop))
4506 {
4507 /* A vector of sub-properties. */
4508 int i;
4509 for (i = 0; i < ASIZE (prop); ++i)
4510 if (single_display_spec_intangible_p (AREF (prop, i)))
4511 return 1;
4512 }
4513 else
4514 return single_display_spec_intangible_p (prop);
4515
4516 return 0;
4517 }
4518
4519
4520 /* Return 1 if PROP is a display sub-property value containing STRING. */
4521
4522 static int
4523 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4524 {
4525 if (EQ (string, prop))
4526 return 1;
4527
4528 /* Skip over `when FORM'. */
4529 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4530 {
4531 prop = XCDR (prop);
4532 if (!CONSP (prop))
4533 return 0;
4534 prop = XCDR (prop);
4535 }
4536
4537 if (CONSP (prop))
4538 /* Skip over `margin LOCATION'. */
4539 if (EQ (XCAR (prop), Qmargin))
4540 {
4541 prop = XCDR (prop);
4542 if (!CONSP (prop))
4543 return 0;
4544
4545 prop = XCDR (prop);
4546 if (!CONSP (prop))
4547 return 0;
4548 }
4549
4550 return CONSP (prop) && EQ (XCAR (prop), string);
4551 }
4552
4553
4554 /* Return 1 if STRING appears in the `display' property PROP. */
4555
4556 static int
4557 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4558 {
4559 if (CONSP (prop)
4560 && CONSP (XCAR (prop))
4561 && !EQ (Qmargin, XCAR (XCAR (prop))))
4562 {
4563 /* A list of sub-properties. */
4564 while (CONSP (prop))
4565 {
4566 if (single_display_spec_string_p (XCAR (prop), string))
4567 return 1;
4568 prop = XCDR (prop);
4569 }
4570 }
4571 else if (VECTORP (prop))
4572 {
4573 /* A vector of sub-properties. */
4574 int i;
4575 for (i = 0; i < ASIZE (prop); ++i)
4576 if (single_display_spec_string_p (AREF (prop, i), string))
4577 return 1;
4578 }
4579 else
4580 return single_display_spec_string_p (prop, string);
4581
4582 return 0;
4583 }
4584
4585 /* Look for STRING in overlays and text properties in W's buffer,
4586 between character positions FROM and TO (excluding TO).
4587 BACK_P non-zero means look back (in this case, TO is supposed to be
4588 less than FROM).
4589 Value is the first character position where STRING was found, or
4590 zero if it wasn't found before hitting TO.
4591
4592 W's buffer must be current.
4593
4594 This function may only use code that doesn't eval because it is
4595 called asynchronously from note_mouse_highlight. */
4596
4597 static EMACS_INT
4598 string_buffer_position_lim (struct window *w, Lisp_Object string,
4599 EMACS_INT from, EMACS_INT to, int back_p)
4600 {
4601 Lisp_Object limit, prop, pos;
4602 int found = 0;
4603
4604 pos = make_number (from);
4605
4606 if (!back_p) /* looking forward */
4607 {
4608 limit = make_number (min (to, ZV));
4609 while (!found && !EQ (pos, limit))
4610 {
4611 prop = Fget_char_property (pos, Qdisplay, Qnil);
4612 if (!NILP (prop) && display_prop_string_p (prop, string))
4613 found = 1;
4614 else
4615 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4616 limit);
4617 }
4618 }
4619 else /* looking back */
4620 {
4621 limit = make_number (max (to, BEGV));
4622 while (!found && !EQ (pos, limit))
4623 {
4624 prop = Fget_char_property (pos, Qdisplay, Qnil);
4625 if (!NILP (prop) && display_prop_string_p (prop, string))
4626 found = 1;
4627 else
4628 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4629 limit);
4630 }
4631 }
4632
4633 return found ? XINT (pos) : 0;
4634 }
4635
4636 /* Determine which buffer position in W's buffer STRING comes from.
4637 AROUND_CHARPOS is an approximate position where it could come from.
4638 Value is the buffer position or 0 if it couldn't be determined.
4639
4640 W's buffer must be current.
4641
4642 This function is necessary because we don't record buffer positions
4643 in glyphs generated from strings (to keep struct glyph small).
4644 This function may only use code that doesn't eval because it is
4645 called asynchronously from note_mouse_highlight. */
4646
4647 EMACS_INT
4648 string_buffer_position (struct window *w, Lisp_Object string, EMACS_INT around_charpos)
4649 {
4650 const int MAX_DISTANCE = 1000;
4651 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4652 around_charpos + MAX_DISTANCE,
4653 0);
4654
4655 if (!found)
4656 found = string_buffer_position_lim (w, string, around_charpos,
4657 around_charpos - MAX_DISTANCE, 1);
4658 return found;
4659 }
4660
4661
4662 \f
4663 /***********************************************************************
4664 `composition' property
4665 ***********************************************************************/
4666
4667 /* Set up iterator IT from `composition' property at its current
4668 position. Called from handle_stop. */
4669
4670 static enum prop_handled
4671 handle_composition_prop (struct it *it)
4672 {
4673 Lisp_Object prop, string;
4674 EMACS_INT pos, pos_byte, start, end;
4675
4676 if (STRINGP (it->string))
4677 {
4678 unsigned char *s;
4679
4680 pos = IT_STRING_CHARPOS (*it);
4681 pos_byte = IT_STRING_BYTEPOS (*it);
4682 string = it->string;
4683 s = SDATA (string) + pos_byte;
4684 it->c = STRING_CHAR (s);
4685 }
4686 else
4687 {
4688 pos = IT_CHARPOS (*it);
4689 pos_byte = IT_BYTEPOS (*it);
4690 string = Qnil;
4691 it->c = FETCH_CHAR (pos_byte);
4692 }
4693
4694 /* If there's a valid composition and point is not inside of the
4695 composition (in the case that the composition is from the current
4696 buffer), draw a glyph composed from the composition components. */
4697 if (find_composition (pos, -1, &start, &end, &prop, string)
4698 && COMPOSITION_VALID_P (start, end, prop)
4699 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4700 {
4701 if (start != pos)
4702 {
4703 if (STRINGP (it->string))
4704 pos_byte = string_char_to_byte (it->string, start);
4705 else
4706 pos_byte = CHAR_TO_BYTE (start);
4707 }
4708 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4709 prop, string);
4710
4711 if (it->cmp_it.id >= 0)
4712 {
4713 it->cmp_it.ch = -1;
4714 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4715 it->cmp_it.nglyphs = -1;
4716 }
4717 }
4718
4719 return HANDLED_NORMALLY;
4720 }
4721
4722
4723 \f
4724 /***********************************************************************
4725 Overlay strings
4726 ***********************************************************************/
4727
4728 /* The following structure is used to record overlay strings for
4729 later sorting in load_overlay_strings. */
4730
4731 struct overlay_entry
4732 {
4733 Lisp_Object overlay;
4734 Lisp_Object string;
4735 int priority;
4736 int after_string_p;
4737 };
4738
4739
4740 /* Set up iterator IT from overlay strings at its current position.
4741 Called from handle_stop. */
4742
4743 static enum prop_handled
4744 handle_overlay_change (struct it *it)
4745 {
4746 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4747 return HANDLED_RECOMPUTE_PROPS;
4748 else
4749 return HANDLED_NORMALLY;
4750 }
4751
4752
4753 /* Set up the next overlay string for delivery by IT, if there is an
4754 overlay string to deliver. Called by set_iterator_to_next when the
4755 end of the current overlay string is reached. If there are more
4756 overlay strings to display, IT->string and
4757 IT->current.overlay_string_index are set appropriately here.
4758 Otherwise IT->string is set to nil. */
4759
4760 static void
4761 next_overlay_string (struct it *it)
4762 {
4763 ++it->current.overlay_string_index;
4764 if (it->current.overlay_string_index == it->n_overlay_strings)
4765 {
4766 /* No more overlay strings. Restore IT's settings to what
4767 they were before overlay strings were processed, and
4768 continue to deliver from current_buffer. */
4769
4770 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4771 pop_it (it);
4772 xassert (it->sp > 0
4773 || (NILP (it->string)
4774 && it->method == GET_FROM_BUFFER
4775 && it->stop_charpos >= BEGV
4776 && it->stop_charpos <= it->end_charpos));
4777 it->current.overlay_string_index = -1;
4778 it->n_overlay_strings = 0;
4779
4780 /* If we're at the end of the buffer, record that we have
4781 processed the overlay strings there already, so that
4782 next_element_from_buffer doesn't try it again. */
4783 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4784 it->overlay_strings_at_end_processed_p = 1;
4785 }
4786 else
4787 {
4788 /* There are more overlay strings to process. If
4789 IT->current.overlay_string_index has advanced to a position
4790 where we must load IT->overlay_strings with more strings, do
4791 it. */
4792 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4793
4794 if (it->current.overlay_string_index && i == 0)
4795 load_overlay_strings (it, 0);
4796
4797 /* Initialize IT to deliver display elements from the overlay
4798 string. */
4799 it->string = it->overlay_strings[i];
4800 it->multibyte_p = STRING_MULTIBYTE (it->string);
4801 SET_TEXT_POS (it->current.string_pos, 0, 0);
4802 it->method = GET_FROM_STRING;
4803 it->stop_charpos = 0;
4804 if (it->cmp_it.stop_pos >= 0)
4805 it->cmp_it.stop_pos = 0;
4806 }
4807
4808 CHECK_IT (it);
4809 }
4810
4811
4812 /* Compare two overlay_entry structures E1 and E2. Used as a
4813 comparison function for qsort in load_overlay_strings. Overlay
4814 strings for the same position are sorted so that
4815
4816 1. All after-strings come in front of before-strings, except
4817 when they come from the same overlay.
4818
4819 2. Within after-strings, strings are sorted so that overlay strings
4820 from overlays with higher priorities come first.
4821
4822 2. Within before-strings, strings are sorted so that overlay
4823 strings from overlays with higher priorities come last.
4824
4825 Value is analogous to strcmp. */
4826
4827
4828 static int
4829 compare_overlay_entries (const void *e1, const void *e2)
4830 {
4831 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4832 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4833 int result;
4834
4835 if (entry1->after_string_p != entry2->after_string_p)
4836 {
4837 /* Let after-strings appear in front of before-strings if
4838 they come from different overlays. */
4839 if (EQ (entry1->overlay, entry2->overlay))
4840 result = entry1->after_string_p ? 1 : -1;
4841 else
4842 result = entry1->after_string_p ? -1 : 1;
4843 }
4844 else if (entry1->after_string_p)
4845 /* After-strings sorted in order of decreasing priority. */
4846 result = entry2->priority - entry1->priority;
4847 else
4848 /* Before-strings sorted in order of increasing priority. */
4849 result = entry1->priority - entry2->priority;
4850
4851 return result;
4852 }
4853
4854
4855 /* Load the vector IT->overlay_strings with overlay strings from IT's
4856 current buffer position, or from CHARPOS if that is > 0. Set
4857 IT->n_overlays to the total number of overlay strings found.
4858
4859 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4860 a time. On entry into load_overlay_strings,
4861 IT->current.overlay_string_index gives the number of overlay
4862 strings that have already been loaded by previous calls to this
4863 function.
4864
4865 IT->add_overlay_start contains an additional overlay start
4866 position to consider for taking overlay strings from, if non-zero.
4867 This position comes into play when the overlay has an `invisible'
4868 property, and both before and after-strings. When we've skipped to
4869 the end of the overlay, because of its `invisible' property, we
4870 nevertheless want its before-string to appear.
4871 IT->add_overlay_start will contain the overlay start position
4872 in this case.
4873
4874 Overlay strings are sorted so that after-string strings come in
4875 front of before-string strings. Within before and after-strings,
4876 strings are sorted by overlay priority. See also function
4877 compare_overlay_entries. */
4878
4879 static void
4880 load_overlay_strings (struct it *it, EMACS_INT charpos)
4881 {
4882 Lisp_Object overlay, window, str, invisible;
4883 struct Lisp_Overlay *ov;
4884 EMACS_INT start, end;
4885 int size = 20;
4886 int n = 0, i, j, invis_p;
4887 struct overlay_entry *entries
4888 = (struct overlay_entry *) alloca (size * sizeof *entries);
4889
4890 if (charpos <= 0)
4891 charpos = IT_CHARPOS (*it);
4892
4893 /* Append the overlay string STRING of overlay OVERLAY to vector
4894 `entries' which has size `size' and currently contains `n'
4895 elements. AFTER_P non-zero means STRING is an after-string of
4896 OVERLAY. */
4897 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4898 do \
4899 { \
4900 Lisp_Object priority; \
4901 \
4902 if (n == size) \
4903 { \
4904 int new_size = 2 * size; \
4905 struct overlay_entry *old = entries; \
4906 entries = \
4907 (struct overlay_entry *) alloca (new_size \
4908 * sizeof *entries); \
4909 memcpy (entries, old, size * sizeof *entries); \
4910 size = new_size; \
4911 } \
4912 \
4913 entries[n].string = (STRING); \
4914 entries[n].overlay = (OVERLAY); \
4915 priority = Foverlay_get ((OVERLAY), Qpriority); \
4916 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4917 entries[n].after_string_p = (AFTER_P); \
4918 ++n; \
4919 } \
4920 while (0)
4921
4922 /* Process overlay before the overlay center. */
4923 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4924 {
4925 XSETMISC (overlay, ov);
4926 xassert (OVERLAYP (overlay));
4927 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4928 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4929
4930 if (end < charpos)
4931 break;
4932
4933 /* Skip this overlay if it doesn't start or end at IT's current
4934 position. */
4935 if (end != charpos && start != charpos)
4936 continue;
4937
4938 /* Skip this overlay if it doesn't apply to IT->w. */
4939 window = Foverlay_get (overlay, Qwindow);
4940 if (WINDOWP (window) && XWINDOW (window) != it->w)
4941 continue;
4942
4943 /* If the text ``under'' the overlay is invisible, both before-
4944 and after-strings from this overlay are visible; start and
4945 end position are indistinguishable. */
4946 invisible = Foverlay_get (overlay, Qinvisible);
4947 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4948
4949 /* If overlay has a non-empty before-string, record it. */
4950 if ((start == charpos || (end == charpos && invis_p))
4951 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4952 && SCHARS (str))
4953 RECORD_OVERLAY_STRING (overlay, str, 0);
4954
4955 /* If overlay has a non-empty after-string, record it. */
4956 if ((end == charpos || (start == charpos && invis_p))
4957 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4958 && SCHARS (str))
4959 RECORD_OVERLAY_STRING (overlay, str, 1);
4960 }
4961
4962 /* Process overlays after the overlay center. */
4963 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4964 {
4965 XSETMISC (overlay, ov);
4966 xassert (OVERLAYP (overlay));
4967 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4968 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4969
4970 if (start > charpos)
4971 break;
4972
4973 /* Skip this overlay if it doesn't start or end at IT's current
4974 position. */
4975 if (end != charpos && start != charpos)
4976 continue;
4977
4978 /* Skip this overlay if it doesn't apply to IT->w. */
4979 window = Foverlay_get (overlay, Qwindow);
4980 if (WINDOWP (window) && XWINDOW (window) != it->w)
4981 continue;
4982
4983 /* If the text ``under'' the overlay is invisible, it has a zero
4984 dimension, and both before- and after-strings apply. */
4985 invisible = Foverlay_get (overlay, Qinvisible);
4986 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4987
4988 /* If overlay has a non-empty before-string, record it. */
4989 if ((start == charpos || (end == charpos && invis_p))
4990 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4991 && SCHARS (str))
4992 RECORD_OVERLAY_STRING (overlay, str, 0);
4993
4994 /* If overlay has a non-empty after-string, record it. */
4995 if ((end == charpos || (start == charpos && invis_p))
4996 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4997 && SCHARS (str))
4998 RECORD_OVERLAY_STRING (overlay, str, 1);
4999 }
5000
5001 #undef RECORD_OVERLAY_STRING
5002
5003 /* Sort entries. */
5004 if (n > 1)
5005 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5006
5007 /* Record the total number of strings to process. */
5008 it->n_overlay_strings = n;
5009
5010 /* IT->current.overlay_string_index is the number of overlay strings
5011 that have already been consumed by IT. Copy some of the
5012 remaining overlay strings to IT->overlay_strings. */
5013 i = 0;
5014 j = it->current.overlay_string_index;
5015 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5016 {
5017 it->overlay_strings[i] = entries[j].string;
5018 it->string_overlays[i++] = entries[j++].overlay;
5019 }
5020
5021 CHECK_IT (it);
5022 }
5023
5024
5025 /* Get the first chunk of overlay strings at IT's current buffer
5026 position, or at CHARPOS if that is > 0. Value is non-zero if at
5027 least one overlay string was found. */
5028
5029 static int
5030 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5031 {
5032 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5033 process. This fills IT->overlay_strings with strings, and sets
5034 IT->n_overlay_strings to the total number of strings to process.
5035 IT->pos.overlay_string_index has to be set temporarily to zero
5036 because load_overlay_strings needs this; it must be set to -1
5037 when no overlay strings are found because a zero value would
5038 indicate a position in the first overlay string. */
5039 it->current.overlay_string_index = 0;
5040 load_overlay_strings (it, charpos);
5041
5042 /* If we found overlay strings, set up IT to deliver display
5043 elements from the first one. Otherwise set up IT to deliver
5044 from current_buffer. */
5045 if (it->n_overlay_strings)
5046 {
5047 /* Make sure we know settings in current_buffer, so that we can
5048 restore meaningful values when we're done with the overlay
5049 strings. */
5050 if (compute_stop_p)
5051 compute_stop_pos (it);
5052 xassert (it->face_id >= 0);
5053
5054 /* Save IT's settings. They are restored after all overlay
5055 strings have been processed. */
5056 xassert (!compute_stop_p || it->sp == 0);
5057
5058 /* When called from handle_stop, there might be an empty display
5059 string loaded. In that case, don't bother saving it. */
5060 if (!STRINGP (it->string) || SCHARS (it->string))
5061 push_it (it);
5062
5063 /* Set up IT to deliver display elements from the first overlay
5064 string. */
5065 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5066 it->string = it->overlay_strings[0];
5067 it->from_overlay = Qnil;
5068 it->stop_charpos = 0;
5069 xassert (STRINGP (it->string));
5070 it->end_charpos = SCHARS (it->string);
5071 it->multibyte_p = STRING_MULTIBYTE (it->string);
5072 it->method = GET_FROM_STRING;
5073 return 1;
5074 }
5075
5076 it->current.overlay_string_index = -1;
5077 return 0;
5078 }
5079
5080 static int
5081 get_overlay_strings (struct it *it, EMACS_INT charpos)
5082 {
5083 it->string = Qnil;
5084 it->method = GET_FROM_BUFFER;
5085
5086 (void) get_overlay_strings_1 (it, charpos, 1);
5087
5088 CHECK_IT (it);
5089
5090 /* Value is non-zero if we found at least one overlay string. */
5091 return STRINGP (it->string);
5092 }
5093
5094
5095 \f
5096 /***********************************************************************
5097 Saving and restoring state
5098 ***********************************************************************/
5099
5100 /* Save current settings of IT on IT->stack. Called, for example,
5101 before setting up IT for an overlay string, to be able to restore
5102 IT's settings to what they were after the overlay string has been
5103 processed. */
5104
5105 static void
5106 push_it (struct it *it)
5107 {
5108 struct iterator_stack_entry *p;
5109
5110 xassert (it->sp < IT_STACK_SIZE);
5111 p = it->stack + it->sp;
5112
5113 p->stop_charpos = it->stop_charpos;
5114 p->prev_stop = it->prev_stop;
5115 p->base_level_stop = it->base_level_stop;
5116 p->cmp_it = it->cmp_it;
5117 xassert (it->face_id >= 0);
5118 p->face_id = it->face_id;
5119 p->string = it->string;
5120 p->method = it->method;
5121 p->from_overlay = it->from_overlay;
5122 switch (p->method)
5123 {
5124 case GET_FROM_IMAGE:
5125 p->u.image.object = it->object;
5126 p->u.image.image_id = it->image_id;
5127 p->u.image.slice = it->slice;
5128 break;
5129 case GET_FROM_STRETCH:
5130 p->u.stretch.object = it->object;
5131 break;
5132 }
5133 p->position = it->position;
5134 p->current = it->current;
5135 p->end_charpos = it->end_charpos;
5136 p->string_nchars = it->string_nchars;
5137 p->area = it->area;
5138 p->multibyte_p = it->multibyte_p;
5139 p->avoid_cursor_p = it->avoid_cursor_p;
5140 p->space_width = it->space_width;
5141 p->font_height = it->font_height;
5142 p->voffset = it->voffset;
5143 p->string_from_display_prop_p = it->string_from_display_prop_p;
5144 p->display_ellipsis_p = 0;
5145 p->line_wrap = it->line_wrap;
5146 ++it->sp;
5147 }
5148
5149 static void
5150 iterate_out_of_display_property (struct it *it)
5151 {
5152 /* Maybe initialize paragraph direction. If we are at the beginning
5153 of a new paragraph, next_element_from_buffer may not have a
5154 chance to do that. */
5155 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
5156 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5157 /* prev_stop can be zero, so check against BEGV as well. */
5158 while (it->bidi_it.charpos >= BEGV
5159 && it->prev_stop <= it->bidi_it.charpos
5160 && it->bidi_it.charpos < CHARPOS (it->position))
5161 bidi_move_to_visually_next (&it->bidi_it);
5162 /* Record the stop_pos we just crossed, for when we cross it
5163 back, maybe. */
5164 if (it->bidi_it.charpos > CHARPOS (it->position))
5165 it->prev_stop = CHARPOS (it->position);
5166 /* If we ended up not where pop_it put us, resync IT's
5167 positional members with the bidi iterator. */
5168 if (it->bidi_it.charpos != CHARPOS (it->position))
5169 {
5170 SET_TEXT_POS (it->position,
5171 it->bidi_it.charpos, it->bidi_it.bytepos);
5172 it->current.pos = it->position;
5173 }
5174 }
5175
5176 /* Restore IT's settings from IT->stack. Called, for example, when no
5177 more overlay strings must be processed, and we return to delivering
5178 display elements from a buffer, or when the end of a string from a
5179 `display' property is reached and we return to delivering display
5180 elements from an overlay string, or from a buffer. */
5181
5182 static void
5183 pop_it (struct it *it)
5184 {
5185 struct iterator_stack_entry *p;
5186
5187 xassert (it->sp > 0);
5188 --it->sp;
5189 p = it->stack + it->sp;
5190 it->stop_charpos = p->stop_charpos;
5191 it->prev_stop = p->prev_stop;
5192 it->base_level_stop = p->base_level_stop;
5193 it->cmp_it = p->cmp_it;
5194 it->face_id = p->face_id;
5195 it->current = p->current;
5196 it->position = p->position;
5197 it->string = p->string;
5198 it->from_overlay = p->from_overlay;
5199 if (NILP (it->string))
5200 SET_TEXT_POS (it->current.string_pos, -1, -1);
5201 it->method = p->method;
5202 switch (it->method)
5203 {
5204 case GET_FROM_IMAGE:
5205 it->image_id = p->u.image.image_id;
5206 it->object = p->u.image.object;
5207 it->slice = p->u.image.slice;
5208 break;
5209 case GET_FROM_STRETCH:
5210 it->object = p->u.comp.object;
5211 break;
5212 case GET_FROM_BUFFER:
5213 it->object = it->w->buffer;
5214 if (it->bidi_p)
5215 {
5216 /* Bidi-iterate until we get out of the portion of text, if
5217 any, covered by a `display' text property or an overlay
5218 with `display' property. (We cannot just jump there,
5219 because the internal coherency of the bidi iterator state
5220 can not be preserved across such jumps.) We also must
5221 determine the paragraph base direction if the overlay we
5222 just processed is at the beginning of a new
5223 paragraph. */
5224 iterate_out_of_display_property (it);
5225 }
5226 break;
5227 case GET_FROM_STRING:
5228 it->object = it->string;
5229 break;
5230 case GET_FROM_DISPLAY_VECTOR:
5231 if (it->s)
5232 it->method = GET_FROM_C_STRING;
5233 else if (STRINGP (it->string))
5234 it->method = GET_FROM_STRING;
5235 else
5236 {
5237 it->method = GET_FROM_BUFFER;
5238 it->object = it->w->buffer;
5239 }
5240 }
5241 it->end_charpos = p->end_charpos;
5242 it->string_nchars = p->string_nchars;
5243 it->area = p->area;
5244 it->multibyte_p = p->multibyte_p;
5245 it->avoid_cursor_p = p->avoid_cursor_p;
5246 it->space_width = p->space_width;
5247 it->font_height = p->font_height;
5248 it->voffset = p->voffset;
5249 it->string_from_display_prop_p = p->string_from_display_prop_p;
5250 it->line_wrap = p->line_wrap;
5251 }
5252
5253
5254 \f
5255 /***********************************************************************
5256 Moving over lines
5257 ***********************************************************************/
5258
5259 /* Set IT's current position to the previous line start. */
5260
5261 static void
5262 back_to_previous_line_start (struct it *it)
5263 {
5264 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5265 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5266 }
5267
5268
5269 /* Move IT to the next line start.
5270
5271 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5272 we skipped over part of the text (as opposed to moving the iterator
5273 continuously over the text). Otherwise, don't change the value
5274 of *SKIPPED_P.
5275
5276 Newlines may come from buffer text, overlay strings, or strings
5277 displayed via the `display' property. That's the reason we can't
5278 simply use find_next_newline_no_quit.
5279
5280 Note that this function may not skip over invisible text that is so
5281 because of text properties and immediately follows a newline. If
5282 it would, function reseat_at_next_visible_line_start, when called
5283 from set_iterator_to_next, would effectively make invisible
5284 characters following a newline part of the wrong glyph row, which
5285 leads to wrong cursor motion. */
5286
5287 static int
5288 forward_to_next_line_start (struct it *it, int *skipped_p)
5289 {
5290 int old_selective, newline_found_p, n;
5291 const int MAX_NEWLINE_DISTANCE = 500;
5292
5293 /* If already on a newline, just consume it to avoid unintended
5294 skipping over invisible text below. */
5295 if (it->what == IT_CHARACTER
5296 && it->c == '\n'
5297 && CHARPOS (it->position) == IT_CHARPOS (*it))
5298 {
5299 set_iterator_to_next (it, 0);
5300 it->c = 0;
5301 return 1;
5302 }
5303
5304 /* Don't handle selective display in the following. It's (a)
5305 unnecessary because it's done by the caller, and (b) leads to an
5306 infinite recursion because next_element_from_ellipsis indirectly
5307 calls this function. */
5308 old_selective = it->selective;
5309 it->selective = 0;
5310
5311 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5312 from buffer text. */
5313 for (n = newline_found_p = 0;
5314 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5315 n += STRINGP (it->string) ? 0 : 1)
5316 {
5317 if (!get_next_display_element (it))
5318 return 0;
5319 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5320 set_iterator_to_next (it, 0);
5321 }
5322
5323 /* If we didn't find a newline near enough, see if we can use a
5324 short-cut. */
5325 if (!newline_found_p)
5326 {
5327 EMACS_INT start = IT_CHARPOS (*it);
5328 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5329 Lisp_Object pos;
5330
5331 xassert (!STRINGP (it->string));
5332
5333 /* If there isn't any `display' property in sight, and no
5334 overlays, we can just use the position of the newline in
5335 buffer text. */
5336 if (it->stop_charpos >= limit
5337 || ((pos = Fnext_single_property_change (make_number (start),
5338 Qdisplay,
5339 Qnil, make_number (limit)),
5340 NILP (pos))
5341 && next_overlay_change (start) == ZV))
5342 {
5343 IT_CHARPOS (*it) = limit;
5344 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5345 *skipped_p = newline_found_p = 1;
5346 }
5347 else
5348 {
5349 while (get_next_display_element (it)
5350 && !newline_found_p)
5351 {
5352 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5353 set_iterator_to_next (it, 0);
5354 }
5355 }
5356 }
5357
5358 it->selective = old_selective;
5359 return newline_found_p;
5360 }
5361
5362
5363 /* Set IT's current position to the previous visible line start. Skip
5364 invisible text that is so either due to text properties or due to
5365 selective display. Caution: this does not change IT->current_x and
5366 IT->hpos. */
5367
5368 static void
5369 back_to_previous_visible_line_start (struct it *it)
5370 {
5371 while (IT_CHARPOS (*it) > BEGV)
5372 {
5373 back_to_previous_line_start (it);
5374
5375 if (IT_CHARPOS (*it) <= BEGV)
5376 break;
5377
5378 /* If selective > 0, then lines indented more than its value are
5379 invisible. */
5380 if (it->selective > 0
5381 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5382 (double) it->selective)) /* iftc */
5383 continue;
5384
5385 /* Check the newline before point for invisibility. */
5386 {
5387 Lisp_Object prop;
5388 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5389 Qinvisible, it->window);
5390 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5391 continue;
5392 }
5393
5394 if (IT_CHARPOS (*it) <= BEGV)
5395 break;
5396
5397 {
5398 struct it it2;
5399 EMACS_INT pos;
5400 EMACS_INT beg, end;
5401 Lisp_Object val, overlay;
5402
5403 /* If newline is part of a composition, continue from start of composition */
5404 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5405 && beg < IT_CHARPOS (*it))
5406 goto replaced;
5407
5408 /* If newline is replaced by a display property, find start of overlay
5409 or interval and continue search from that point. */
5410 it2 = *it;
5411 pos = --IT_CHARPOS (it2);
5412 --IT_BYTEPOS (it2);
5413 it2.sp = 0;
5414 it2.string_from_display_prop_p = 0;
5415 if (handle_display_prop (&it2) == HANDLED_RETURN
5416 && !NILP (val = get_char_property_and_overlay
5417 (make_number (pos), Qdisplay, Qnil, &overlay))
5418 && (OVERLAYP (overlay)
5419 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5420 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5421 goto replaced;
5422
5423 /* Newline is not replaced by anything -- so we are done. */
5424 break;
5425
5426 replaced:
5427 if (beg < BEGV)
5428 beg = BEGV;
5429 IT_CHARPOS (*it) = beg;
5430 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5431 }
5432 }
5433
5434 it->continuation_lines_width = 0;
5435
5436 xassert (IT_CHARPOS (*it) >= BEGV);
5437 xassert (IT_CHARPOS (*it) == BEGV
5438 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5439 CHECK_IT (it);
5440 }
5441
5442
5443 /* Reseat iterator IT at the previous visible line start. Skip
5444 invisible text that is so either due to text properties or due to
5445 selective display. At the end, update IT's overlay information,
5446 face information etc. */
5447
5448 void
5449 reseat_at_previous_visible_line_start (struct it *it)
5450 {
5451 back_to_previous_visible_line_start (it);
5452 reseat (it, it->current.pos, 1);
5453 CHECK_IT (it);
5454 }
5455
5456
5457 /* Reseat iterator IT on the next visible line start in the current
5458 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5459 preceding the line start. Skip over invisible text that is so
5460 because of selective display. Compute faces, overlays etc at the
5461 new position. Note that this function does not skip over text that
5462 is invisible because of text properties. */
5463
5464 static void
5465 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5466 {
5467 int newline_found_p, skipped_p = 0;
5468
5469 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5470
5471 /* Skip over lines that are invisible because they are indented
5472 more than the value of IT->selective. */
5473 if (it->selective > 0)
5474 while (IT_CHARPOS (*it) < ZV
5475 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5476 (double) it->selective)) /* iftc */
5477 {
5478 xassert (IT_BYTEPOS (*it) == BEGV
5479 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5480 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5481 }
5482
5483 /* Position on the newline if that's what's requested. */
5484 if (on_newline_p && newline_found_p)
5485 {
5486 if (STRINGP (it->string))
5487 {
5488 if (IT_STRING_CHARPOS (*it) > 0)
5489 {
5490 --IT_STRING_CHARPOS (*it);
5491 --IT_STRING_BYTEPOS (*it);
5492 }
5493 }
5494 else if (IT_CHARPOS (*it) > BEGV)
5495 {
5496 --IT_CHARPOS (*it);
5497 --IT_BYTEPOS (*it);
5498 reseat (it, it->current.pos, 0);
5499 }
5500 }
5501 else if (skipped_p)
5502 reseat (it, it->current.pos, 0);
5503
5504 CHECK_IT (it);
5505 }
5506
5507
5508 \f
5509 /***********************************************************************
5510 Changing an iterator's position
5511 ***********************************************************************/
5512
5513 /* Change IT's current position to POS in current_buffer. If FORCE_P
5514 is non-zero, always check for text properties at the new position.
5515 Otherwise, text properties are only looked up if POS >=
5516 IT->check_charpos of a property. */
5517
5518 static void
5519 reseat (struct it *it, struct text_pos pos, int force_p)
5520 {
5521 EMACS_INT original_pos = IT_CHARPOS (*it);
5522
5523 reseat_1 (it, pos, 0);
5524
5525 /* Determine where to check text properties. Avoid doing it
5526 where possible because text property lookup is very expensive. */
5527 if (force_p
5528 || CHARPOS (pos) > it->stop_charpos
5529 || CHARPOS (pos) < original_pos)
5530 {
5531 if (it->bidi_p)
5532 {
5533 /* For bidi iteration, we need to prime prev_stop and
5534 base_level_stop with our best estimations. */
5535 if (CHARPOS (pos) < it->prev_stop)
5536 {
5537 handle_stop_backwards (it, BEGV);
5538 if (CHARPOS (pos) < it->base_level_stop)
5539 it->base_level_stop = 0;
5540 }
5541 else if (CHARPOS (pos) > it->stop_charpos
5542 && it->stop_charpos >= BEGV)
5543 handle_stop_backwards (it, it->stop_charpos);
5544 else /* force_p */
5545 handle_stop (it);
5546 }
5547 else
5548 {
5549 handle_stop (it);
5550 it->prev_stop = it->base_level_stop = 0;
5551 }
5552
5553 }
5554
5555 CHECK_IT (it);
5556 }
5557
5558
5559 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5560 IT->stop_pos to POS, also. */
5561
5562 static void
5563 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5564 {
5565 /* Don't call this function when scanning a C string. */
5566 xassert (it->s == NULL);
5567
5568 /* POS must be a reasonable value. */
5569 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5570
5571 it->current.pos = it->position = pos;
5572 it->end_charpos = ZV;
5573 it->dpvec = NULL;
5574 it->current.dpvec_index = -1;
5575 it->current.overlay_string_index = -1;
5576 IT_STRING_CHARPOS (*it) = -1;
5577 IT_STRING_BYTEPOS (*it) = -1;
5578 it->string = Qnil;
5579 it->string_from_display_prop_p = 0;
5580 it->method = GET_FROM_BUFFER;
5581 it->object = it->w->buffer;
5582 it->area = TEXT_AREA;
5583 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5584 it->sp = 0;
5585 it->string_from_display_prop_p = 0;
5586 it->face_before_selective_p = 0;
5587 if (it->bidi_p)
5588 {
5589 it->bidi_it.first_elt = 1;
5590 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5591 }
5592
5593 if (set_stop_p)
5594 {
5595 it->stop_charpos = CHARPOS (pos);
5596 it->base_level_stop = CHARPOS (pos);
5597 }
5598 }
5599
5600
5601 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5602 If S is non-null, it is a C string to iterate over. Otherwise,
5603 STRING gives a Lisp string to iterate over.
5604
5605 If PRECISION > 0, don't return more then PRECISION number of
5606 characters from the string.
5607
5608 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5609 characters have been returned. FIELD_WIDTH < 0 means an infinite
5610 field width.
5611
5612 MULTIBYTE = 0 means disable processing of multibyte characters,
5613 MULTIBYTE > 0 means enable it,
5614 MULTIBYTE < 0 means use IT->multibyte_p.
5615
5616 IT must be initialized via a prior call to init_iterator before
5617 calling this function. */
5618
5619 static void
5620 reseat_to_string (struct it *it, const unsigned char *s, Lisp_Object string,
5621 EMACS_INT charpos, EMACS_INT precision, int field_width,
5622 int multibyte)
5623 {
5624 /* No region in strings. */
5625 it->region_beg_charpos = it->region_end_charpos = -1;
5626
5627 /* No text property checks performed by default, but see below. */
5628 it->stop_charpos = -1;
5629
5630 /* Set iterator position and end position. */
5631 memset (&it->current, 0, sizeof it->current);
5632 it->current.overlay_string_index = -1;
5633 it->current.dpvec_index = -1;
5634 xassert (charpos >= 0);
5635
5636 /* If STRING is specified, use its multibyteness, otherwise use the
5637 setting of MULTIBYTE, if specified. */
5638 if (multibyte >= 0)
5639 it->multibyte_p = multibyte > 0;
5640
5641 if (s == NULL)
5642 {
5643 xassert (STRINGP (string));
5644 it->string = string;
5645 it->s = NULL;
5646 it->end_charpos = it->string_nchars = SCHARS (string);
5647 it->method = GET_FROM_STRING;
5648 it->current.string_pos = string_pos (charpos, string);
5649 }
5650 else
5651 {
5652 it->s = s;
5653 it->string = Qnil;
5654
5655 /* Note that we use IT->current.pos, not it->current.string_pos,
5656 for displaying C strings. */
5657 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5658 if (it->multibyte_p)
5659 {
5660 it->current.pos = c_string_pos (charpos, s, 1);
5661 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5662 }
5663 else
5664 {
5665 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5666 it->end_charpos = it->string_nchars = strlen (s);
5667 }
5668
5669 it->method = GET_FROM_C_STRING;
5670 }
5671
5672 /* PRECISION > 0 means don't return more than PRECISION characters
5673 from the string. */
5674 if (precision > 0 && it->end_charpos - charpos > precision)
5675 it->end_charpos = it->string_nchars = charpos + precision;
5676
5677 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5678 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5679 FIELD_WIDTH < 0 means infinite field width. This is useful for
5680 padding with `-' at the end of a mode line. */
5681 if (field_width < 0)
5682 field_width = INFINITY;
5683 if (field_width > it->end_charpos - charpos)
5684 it->end_charpos = charpos + field_width;
5685
5686 /* Use the standard display table for displaying strings. */
5687 if (DISP_TABLE_P (Vstandard_display_table))
5688 it->dp = XCHAR_TABLE (Vstandard_display_table);
5689
5690 it->stop_charpos = charpos;
5691 if (s == NULL && it->multibyte_p)
5692 {
5693 EMACS_INT endpos = SCHARS (it->string);
5694 if (endpos > it->end_charpos)
5695 endpos = it->end_charpos;
5696 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5697 it->string);
5698 }
5699 CHECK_IT (it);
5700 }
5701
5702
5703 \f
5704 /***********************************************************************
5705 Iteration
5706 ***********************************************************************/
5707
5708 /* Map enum it_method value to corresponding next_element_from_* function. */
5709
5710 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5711 {
5712 next_element_from_buffer,
5713 next_element_from_display_vector,
5714 next_element_from_string,
5715 next_element_from_c_string,
5716 next_element_from_image,
5717 next_element_from_stretch
5718 };
5719
5720 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5721
5722
5723 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5724 (possibly with the following characters). */
5725
5726 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5727 ((IT)->cmp_it.id >= 0 \
5728 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5729 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5730 END_CHARPOS, (IT)->w, \
5731 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5732 (IT)->string)))
5733
5734
5735 /* Load IT's display element fields with information about the next
5736 display element from the current position of IT. Value is zero if
5737 end of buffer (or C string) is reached. */
5738
5739 static struct frame *last_escape_glyph_frame = NULL;
5740 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5741 static int last_escape_glyph_merged_face_id = 0;
5742
5743 int
5744 get_next_display_element (struct it *it)
5745 {
5746 /* Non-zero means that we found a display element. Zero means that
5747 we hit the end of what we iterate over. Performance note: the
5748 function pointer `method' used here turns out to be faster than
5749 using a sequence of if-statements. */
5750 int success_p;
5751
5752 get_next:
5753 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5754
5755 if (it->what == IT_CHARACTER)
5756 {
5757 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5758 and only if (a) the resolved directionality of that character
5759 is R..." */
5760 /* FIXME: Do we need an exception for characters from display
5761 tables? */
5762 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5763 it->c = bidi_mirror_char (it->c);
5764 /* Map via display table or translate control characters.
5765 IT->c, IT->len etc. have been set to the next character by
5766 the function call above. If we have a display table, and it
5767 contains an entry for IT->c, translate it. Don't do this if
5768 IT->c itself comes from a display table, otherwise we could
5769 end up in an infinite recursion. (An alternative could be to
5770 count the recursion depth of this function and signal an
5771 error when a certain maximum depth is reached.) Is it worth
5772 it? */
5773 if (success_p && it->dpvec == NULL)
5774 {
5775 Lisp_Object dv;
5776 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5777 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5778 nbsp_or_shy = char_is_other;
5779 int c = it->c; /* This is the character to display. */
5780
5781 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5782 {
5783 xassert (SINGLE_BYTE_CHAR_P (c));
5784 if (unibyte_display_via_language_environment)
5785 {
5786 c = DECODE_CHAR (unibyte, c);
5787 if (c < 0)
5788 c = BYTE8_TO_CHAR (it->c);
5789 }
5790 else
5791 c = BYTE8_TO_CHAR (it->c);
5792 }
5793
5794 if (it->dp
5795 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5796 VECTORP (dv)))
5797 {
5798 struct Lisp_Vector *v = XVECTOR (dv);
5799
5800 /* Return the first character from the display table
5801 entry, if not empty. If empty, don't display the
5802 current character. */
5803 if (v->size)
5804 {
5805 it->dpvec_char_len = it->len;
5806 it->dpvec = v->contents;
5807 it->dpend = v->contents + v->size;
5808 it->current.dpvec_index = 0;
5809 it->dpvec_face_id = -1;
5810 it->saved_face_id = it->face_id;
5811 it->method = GET_FROM_DISPLAY_VECTOR;
5812 it->ellipsis_p = 0;
5813 }
5814 else
5815 {
5816 set_iterator_to_next (it, 0);
5817 }
5818 goto get_next;
5819 }
5820
5821 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5822 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5823 : c == 0xAD ? char_is_soft_hyphen
5824 : char_is_other);
5825
5826 /* Translate control characters into `\003' or `^C' form.
5827 Control characters coming from a display table entry are
5828 currently not translated because we use IT->dpvec to hold
5829 the translation. This could easily be changed but I
5830 don't believe that it is worth doing.
5831
5832 NBSP and SOFT-HYPEN are property translated too.
5833
5834 Non-printable characters and raw-byte characters are also
5835 translated to octal form. */
5836 if (((c < ' ' || c == 127) /* ASCII control chars */
5837 ? (it->area != TEXT_AREA
5838 /* In mode line, treat \n, \t like other crl chars. */
5839 || (c != '\t'
5840 && it->glyph_row
5841 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5842 || (c != '\n' && c != '\t'))
5843 : (nbsp_or_shy
5844 || CHAR_BYTE8_P (c)
5845 || ! CHAR_PRINTABLE_P (c))))
5846 {
5847 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5848 or a non-printable character which must be displayed
5849 either as '\003' or as `^C' where the '\\' and '^'
5850 can be defined in the display table. Fill
5851 IT->ctl_chars with glyphs for what we have to
5852 display. Then, set IT->dpvec to these glyphs. */
5853 Lisp_Object gc;
5854 int ctl_len;
5855 int face_id, lface_id = 0 ;
5856 int escape_glyph;
5857
5858 /* Handle control characters with ^. */
5859
5860 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5861 {
5862 int g;
5863
5864 g = '^'; /* default glyph for Control */
5865 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5866 if (it->dp
5867 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5868 && GLYPH_CODE_CHAR_VALID_P (gc))
5869 {
5870 g = GLYPH_CODE_CHAR (gc);
5871 lface_id = GLYPH_CODE_FACE (gc);
5872 }
5873 if (lface_id)
5874 {
5875 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5876 }
5877 else if (it->f == last_escape_glyph_frame
5878 && it->face_id == last_escape_glyph_face_id)
5879 {
5880 face_id = last_escape_glyph_merged_face_id;
5881 }
5882 else
5883 {
5884 /* Merge the escape-glyph face into the current face. */
5885 face_id = merge_faces (it->f, Qescape_glyph, 0,
5886 it->face_id);
5887 last_escape_glyph_frame = it->f;
5888 last_escape_glyph_face_id = it->face_id;
5889 last_escape_glyph_merged_face_id = face_id;
5890 }
5891
5892 XSETINT (it->ctl_chars[0], g);
5893 XSETINT (it->ctl_chars[1], c ^ 0100);
5894 ctl_len = 2;
5895 goto display_control;
5896 }
5897
5898 /* Handle non-break space in the mode where it only gets
5899 highlighting. */
5900
5901 if (EQ (Vnobreak_char_display, Qt)
5902 && nbsp_or_shy == char_is_nbsp)
5903 {
5904 /* Merge the no-break-space face into the current face. */
5905 face_id = merge_faces (it->f, Qnobreak_space, 0,
5906 it->face_id);
5907
5908 c = ' ';
5909 XSETINT (it->ctl_chars[0], ' ');
5910 ctl_len = 1;
5911 goto display_control;
5912 }
5913
5914 /* Handle sequences that start with the "escape glyph". */
5915
5916 /* the default escape glyph is \. */
5917 escape_glyph = '\\';
5918
5919 if (it->dp
5920 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5921 && GLYPH_CODE_CHAR_VALID_P (gc))
5922 {
5923 escape_glyph = GLYPH_CODE_CHAR (gc);
5924 lface_id = GLYPH_CODE_FACE (gc);
5925 }
5926 if (lface_id)
5927 {
5928 /* The display table specified a face.
5929 Merge it into face_id and also into escape_glyph. */
5930 face_id = merge_faces (it->f, Qt, lface_id,
5931 it->face_id);
5932 }
5933 else if (it->f == last_escape_glyph_frame
5934 && it->face_id == last_escape_glyph_face_id)
5935 {
5936 face_id = last_escape_glyph_merged_face_id;
5937 }
5938 else
5939 {
5940 /* Merge the escape-glyph face into the current face. */
5941 face_id = merge_faces (it->f, Qescape_glyph, 0,
5942 it->face_id);
5943 last_escape_glyph_frame = it->f;
5944 last_escape_glyph_face_id = it->face_id;
5945 last_escape_glyph_merged_face_id = face_id;
5946 }
5947
5948 /* Handle soft hyphens in the mode where they only get
5949 highlighting. */
5950
5951 if (EQ (Vnobreak_char_display, Qt)
5952 && nbsp_or_shy == char_is_soft_hyphen)
5953 {
5954 XSETINT (it->ctl_chars[0], '-');
5955 ctl_len = 1;
5956 goto display_control;
5957 }
5958
5959 /* Handle non-break space and soft hyphen
5960 with the escape glyph. */
5961
5962 if (nbsp_or_shy)
5963 {
5964 XSETINT (it->ctl_chars[0], escape_glyph);
5965 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5966 XSETINT (it->ctl_chars[1], c);
5967 ctl_len = 2;
5968 goto display_control;
5969 }
5970
5971 {
5972 char str[10];
5973 int len, i;
5974
5975 if (CHAR_BYTE8_P (c))
5976 /* Display \200 instead of \17777600. */
5977 c = CHAR_TO_BYTE8 (c);
5978 len = sprintf (str, "%03o", c);
5979
5980 XSETINT (it->ctl_chars[0], escape_glyph);
5981 for (i = 0; i < len; i++)
5982 XSETINT (it->ctl_chars[i + 1], str[i]);
5983 ctl_len = len + 1;
5984 }
5985
5986 display_control:
5987 /* Set up IT->dpvec and return first character from it. */
5988 it->dpvec_char_len = it->len;
5989 it->dpvec = it->ctl_chars;
5990 it->dpend = it->dpvec + ctl_len;
5991 it->current.dpvec_index = 0;
5992 it->dpvec_face_id = face_id;
5993 it->saved_face_id = it->face_id;
5994 it->method = GET_FROM_DISPLAY_VECTOR;
5995 it->ellipsis_p = 0;
5996 goto get_next;
5997 }
5998 it->char_to_display = c;
5999 }
6000 else if (success_p)
6001 {
6002 it->char_to_display = it->c;
6003 }
6004 }
6005
6006 #ifdef HAVE_WINDOW_SYSTEM
6007 /* Adjust face id for a multibyte character. There are no multibyte
6008 character in unibyte text. */
6009 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6010 && it->multibyte_p
6011 && success_p
6012 && FRAME_WINDOW_P (it->f))
6013 {
6014 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6015
6016 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6017 {
6018 /* Automatic composition with glyph-string. */
6019 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6020
6021 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6022 }
6023 else
6024 {
6025 EMACS_INT pos = (it->s ? -1
6026 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6027 : IT_CHARPOS (*it));
6028
6029 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6030 it->string);
6031 }
6032 }
6033 #endif
6034
6035 /* Is this character the last one of a run of characters with
6036 box? If yes, set IT->end_of_box_run_p to 1. */
6037 if (it->face_box_p
6038 && it->s == NULL)
6039 {
6040 if (it->method == GET_FROM_STRING && it->sp)
6041 {
6042 int face_id = underlying_face_id (it);
6043 struct face *face = FACE_FROM_ID (it->f, face_id);
6044
6045 if (face)
6046 {
6047 if (face->box == FACE_NO_BOX)
6048 {
6049 /* If the box comes from face properties in a
6050 display string, check faces in that string. */
6051 int string_face_id = face_after_it_pos (it);
6052 it->end_of_box_run_p
6053 = (FACE_FROM_ID (it->f, string_face_id)->box
6054 == FACE_NO_BOX);
6055 }
6056 /* Otherwise, the box comes from the underlying face.
6057 If this is the last string character displayed, check
6058 the next buffer location. */
6059 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6060 && (it->current.overlay_string_index
6061 == it->n_overlay_strings - 1))
6062 {
6063 EMACS_INT ignore;
6064 int next_face_id;
6065 struct text_pos pos = it->current.pos;
6066 INC_TEXT_POS (pos, it->multibyte_p);
6067
6068 next_face_id = face_at_buffer_position
6069 (it->w, CHARPOS (pos), it->region_beg_charpos,
6070 it->region_end_charpos, &ignore,
6071 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6072 -1);
6073 it->end_of_box_run_p
6074 = (FACE_FROM_ID (it->f, next_face_id)->box
6075 == FACE_NO_BOX);
6076 }
6077 }
6078 }
6079 else
6080 {
6081 int face_id = face_after_it_pos (it);
6082 it->end_of_box_run_p
6083 = (face_id != it->face_id
6084 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6085 }
6086 }
6087
6088 /* Value is 0 if end of buffer or string reached. */
6089 return success_p;
6090 }
6091
6092
6093 /* Move IT to the next display element.
6094
6095 RESEAT_P non-zero means if called on a newline in buffer text,
6096 skip to the next visible line start.
6097
6098 Functions get_next_display_element and set_iterator_to_next are
6099 separate because I find this arrangement easier to handle than a
6100 get_next_display_element function that also increments IT's
6101 position. The way it is we can first look at an iterator's current
6102 display element, decide whether it fits on a line, and if it does,
6103 increment the iterator position. The other way around we probably
6104 would either need a flag indicating whether the iterator has to be
6105 incremented the next time, or we would have to implement a
6106 decrement position function which would not be easy to write. */
6107
6108 void
6109 set_iterator_to_next (struct it *it, int reseat_p)
6110 {
6111 /* Reset flags indicating start and end of a sequence of characters
6112 with box. Reset them at the start of this function because
6113 moving the iterator to a new position might set them. */
6114 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6115
6116 switch (it->method)
6117 {
6118 case GET_FROM_BUFFER:
6119 /* The current display element of IT is a character from
6120 current_buffer. Advance in the buffer, and maybe skip over
6121 invisible lines that are so because of selective display. */
6122 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6123 reseat_at_next_visible_line_start (it, 0);
6124 else if (it->cmp_it.id >= 0)
6125 {
6126 /* We are currently getting glyphs from a composition. */
6127 int i;
6128
6129 if (! it->bidi_p)
6130 {
6131 IT_CHARPOS (*it) += it->cmp_it.nchars;
6132 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6133 if (it->cmp_it.to < it->cmp_it.nglyphs)
6134 {
6135 it->cmp_it.from = it->cmp_it.to;
6136 }
6137 else
6138 {
6139 it->cmp_it.id = -1;
6140 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6141 IT_BYTEPOS (*it),
6142 it->end_charpos, Qnil);
6143 }
6144 }
6145 else if (! it->cmp_it.reversed_p)
6146 {
6147 /* Composition created while scanning forward. */
6148 /* Update IT's char/byte positions to point to the first
6149 character of the next grapheme cluster, or to the
6150 character visually after the current composition. */
6151 for (i = 0; i < it->cmp_it.nchars; i++)
6152 bidi_move_to_visually_next (&it->bidi_it);
6153 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6154 IT_CHARPOS (*it) = it->bidi_it.charpos;
6155
6156 if (it->cmp_it.to < it->cmp_it.nglyphs)
6157 {
6158 /* Proceed to the next grapheme cluster. */
6159 it->cmp_it.from = it->cmp_it.to;
6160 }
6161 else
6162 {
6163 /* No more grapheme clusters in this composition.
6164 Find the next stop position. */
6165 EMACS_INT stop = it->end_charpos;
6166 if (it->bidi_it.scan_dir < 0)
6167 /* Now we are scanning backward and don't know
6168 where to stop. */
6169 stop = -1;
6170 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6171 IT_BYTEPOS (*it), stop, Qnil);
6172 }
6173 }
6174 else
6175 {
6176 /* Composition created while scanning backward. */
6177 /* Update IT's char/byte positions to point to the last
6178 character of the previous grapheme cluster, or the
6179 character visually after the current composition. */
6180 for (i = 0; i < it->cmp_it.nchars; i++)
6181 bidi_move_to_visually_next (&it->bidi_it);
6182 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6183 IT_CHARPOS (*it) = it->bidi_it.charpos;
6184 if (it->cmp_it.from > 0)
6185 {
6186 /* Proceed to the previous grapheme cluster. */
6187 it->cmp_it.to = it->cmp_it.from;
6188 }
6189 else
6190 {
6191 /* No more grapheme clusters in this composition.
6192 Find the next stop position. */
6193 EMACS_INT stop = it->end_charpos;
6194 if (it->bidi_it.scan_dir < 0)
6195 /* Now we are scanning backward and don't know
6196 where to stop. */
6197 stop = -1;
6198 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6199 IT_BYTEPOS (*it), stop, Qnil);
6200 }
6201 }
6202 }
6203 else
6204 {
6205 xassert (it->len != 0);
6206
6207 if (!it->bidi_p)
6208 {
6209 IT_BYTEPOS (*it) += it->len;
6210 IT_CHARPOS (*it) += 1;
6211 }
6212 else
6213 {
6214 int prev_scan_dir = it->bidi_it.scan_dir;
6215 /* If this is a new paragraph, determine its base
6216 direction (a.k.a. its base embedding level). */
6217 if (it->bidi_it.new_paragraph)
6218 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6219 bidi_move_to_visually_next (&it->bidi_it);
6220 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6221 IT_CHARPOS (*it) = it->bidi_it.charpos;
6222 if (prev_scan_dir != it->bidi_it.scan_dir)
6223 {
6224 /* As the scan direction was changed, we must
6225 re-compute the stop position for composition. */
6226 EMACS_INT stop = it->end_charpos;
6227 if (it->bidi_it.scan_dir < 0)
6228 stop = -1;
6229 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6230 IT_BYTEPOS (*it), stop, Qnil);
6231 }
6232 }
6233 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6234 }
6235 break;
6236
6237 case GET_FROM_C_STRING:
6238 /* Current display element of IT is from a C string. */
6239 IT_BYTEPOS (*it) += it->len;
6240 IT_CHARPOS (*it) += 1;
6241 break;
6242
6243 case GET_FROM_DISPLAY_VECTOR:
6244 /* Current display element of IT is from a display table entry.
6245 Advance in the display table definition. Reset it to null if
6246 end reached, and continue with characters from buffers/
6247 strings. */
6248 ++it->current.dpvec_index;
6249
6250 /* Restore face of the iterator to what they were before the
6251 display vector entry (these entries may contain faces). */
6252 it->face_id = it->saved_face_id;
6253
6254 if (it->dpvec + it->current.dpvec_index == it->dpend)
6255 {
6256 int recheck_faces = it->ellipsis_p;
6257
6258 if (it->s)
6259 it->method = GET_FROM_C_STRING;
6260 else if (STRINGP (it->string))
6261 it->method = GET_FROM_STRING;
6262 else
6263 {
6264 it->method = GET_FROM_BUFFER;
6265 it->object = it->w->buffer;
6266 }
6267
6268 it->dpvec = NULL;
6269 it->current.dpvec_index = -1;
6270
6271 /* Skip over characters which were displayed via IT->dpvec. */
6272 if (it->dpvec_char_len < 0)
6273 reseat_at_next_visible_line_start (it, 1);
6274 else if (it->dpvec_char_len > 0)
6275 {
6276 if (it->method == GET_FROM_STRING
6277 && it->n_overlay_strings > 0)
6278 it->ignore_overlay_strings_at_pos_p = 1;
6279 it->len = it->dpvec_char_len;
6280 set_iterator_to_next (it, reseat_p);
6281 }
6282
6283 /* Maybe recheck faces after display vector */
6284 if (recheck_faces)
6285 it->stop_charpos = IT_CHARPOS (*it);
6286 }
6287 break;
6288
6289 case GET_FROM_STRING:
6290 /* Current display element is a character from a Lisp string. */
6291 xassert (it->s == NULL && STRINGP (it->string));
6292 if (it->cmp_it.id >= 0)
6293 {
6294 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6295 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6296 if (it->cmp_it.to < it->cmp_it.nglyphs)
6297 it->cmp_it.from = it->cmp_it.to;
6298 else
6299 {
6300 it->cmp_it.id = -1;
6301 composition_compute_stop_pos (&it->cmp_it,
6302 IT_STRING_CHARPOS (*it),
6303 IT_STRING_BYTEPOS (*it),
6304 it->end_charpos, it->string);
6305 }
6306 }
6307 else
6308 {
6309 IT_STRING_BYTEPOS (*it) += it->len;
6310 IT_STRING_CHARPOS (*it) += 1;
6311 }
6312
6313 consider_string_end:
6314
6315 if (it->current.overlay_string_index >= 0)
6316 {
6317 /* IT->string is an overlay string. Advance to the
6318 next, if there is one. */
6319 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6320 {
6321 it->ellipsis_p = 0;
6322 next_overlay_string (it);
6323 if (it->ellipsis_p)
6324 setup_for_ellipsis (it, 0);
6325 }
6326 }
6327 else
6328 {
6329 /* IT->string is not an overlay string. If we reached
6330 its end, and there is something on IT->stack, proceed
6331 with what is on the stack. This can be either another
6332 string, this time an overlay string, or a buffer. */
6333 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6334 && it->sp > 0)
6335 {
6336 pop_it (it);
6337 if (it->method == GET_FROM_STRING)
6338 goto consider_string_end;
6339 }
6340 }
6341 break;
6342
6343 case GET_FROM_IMAGE:
6344 case GET_FROM_STRETCH:
6345 /* The position etc with which we have to proceed are on
6346 the stack. The position may be at the end of a string,
6347 if the `display' property takes up the whole string. */
6348 xassert (it->sp > 0);
6349 pop_it (it);
6350 if (it->method == GET_FROM_STRING)
6351 goto consider_string_end;
6352 break;
6353
6354 default:
6355 /* There are no other methods defined, so this should be a bug. */
6356 abort ();
6357 }
6358
6359 xassert (it->method != GET_FROM_STRING
6360 || (STRINGP (it->string)
6361 && IT_STRING_CHARPOS (*it) >= 0));
6362 }
6363
6364 /* Load IT's display element fields with information about the next
6365 display element which comes from a display table entry or from the
6366 result of translating a control character to one of the forms `^C'
6367 or `\003'.
6368
6369 IT->dpvec holds the glyphs to return as characters.
6370 IT->saved_face_id holds the face id before the display vector--it
6371 is restored into IT->face_id in set_iterator_to_next. */
6372
6373 static int
6374 next_element_from_display_vector (struct it *it)
6375 {
6376 Lisp_Object gc;
6377
6378 /* Precondition. */
6379 xassert (it->dpvec && it->current.dpvec_index >= 0);
6380
6381 it->face_id = it->saved_face_id;
6382
6383 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6384 That seemed totally bogus - so I changed it... */
6385 gc = it->dpvec[it->current.dpvec_index];
6386
6387 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6388 {
6389 it->c = GLYPH_CODE_CHAR (gc);
6390 it->len = CHAR_BYTES (it->c);
6391
6392 /* The entry may contain a face id to use. Such a face id is
6393 the id of a Lisp face, not a realized face. A face id of
6394 zero means no face is specified. */
6395 if (it->dpvec_face_id >= 0)
6396 it->face_id = it->dpvec_face_id;
6397 else
6398 {
6399 int lface_id = GLYPH_CODE_FACE (gc);
6400 if (lface_id > 0)
6401 it->face_id = merge_faces (it->f, Qt, lface_id,
6402 it->saved_face_id);
6403 }
6404 }
6405 else
6406 /* Display table entry is invalid. Return a space. */
6407 it->c = ' ', it->len = 1;
6408
6409 /* Don't change position and object of the iterator here. They are
6410 still the values of the character that had this display table
6411 entry or was translated, and that's what we want. */
6412 it->what = IT_CHARACTER;
6413 return 1;
6414 }
6415
6416
6417 /* Load IT with the next display element from Lisp string IT->string.
6418 IT->current.string_pos is the current position within the string.
6419 If IT->current.overlay_string_index >= 0, the Lisp string is an
6420 overlay string. */
6421
6422 static int
6423 next_element_from_string (struct it *it)
6424 {
6425 struct text_pos position;
6426
6427 xassert (STRINGP (it->string));
6428 xassert (IT_STRING_CHARPOS (*it) >= 0);
6429 position = it->current.string_pos;
6430
6431 /* Time to check for invisible text? */
6432 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6433 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6434 {
6435 handle_stop (it);
6436
6437 /* Since a handler may have changed IT->method, we must
6438 recurse here. */
6439 return GET_NEXT_DISPLAY_ELEMENT (it);
6440 }
6441
6442 if (it->current.overlay_string_index >= 0)
6443 {
6444 /* Get the next character from an overlay string. In overlay
6445 strings, There is no field width or padding with spaces to
6446 do. */
6447 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6448 {
6449 it->what = IT_EOB;
6450 return 0;
6451 }
6452 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6453 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6454 && next_element_from_composition (it))
6455 {
6456 return 1;
6457 }
6458 else if (STRING_MULTIBYTE (it->string))
6459 {
6460 const unsigned char *s = (SDATA (it->string)
6461 + IT_STRING_BYTEPOS (*it));
6462 it->c = string_char_and_length (s, &it->len);
6463 }
6464 else
6465 {
6466 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6467 it->len = 1;
6468 }
6469 }
6470 else
6471 {
6472 /* Get the next character from a Lisp string that is not an
6473 overlay string. Such strings come from the mode line, for
6474 example. We may have to pad with spaces, or truncate the
6475 string. See also next_element_from_c_string. */
6476 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6477 {
6478 it->what = IT_EOB;
6479 return 0;
6480 }
6481 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6482 {
6483 /* Pad with spaces. */
6484 it->c = ' ', it->len = 1;
6485 CHARPOS (position) = BYTEPOS (position) = -1;
6486 }
6487 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6488 IT_STRING_BYTEPOS (*it), it->string_nchars)
6489 && next_element_from_composition (it))
6490 {
6491 return 1;
6492 }
6493 else if (STRING_MULTIBYTE (it->string))
6494 {
6495 const unsigned char *s = (SDATA (it->string)
6496 + IT_STRING_BYTEPOS (*it));
6497 it->c = string_char_and_length (s, &it->len);
6498 }
6499 else
6500 {
6501 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6502 it->len = 1;
6503 }
6504 }
6505
6506 /* Record what we have and where it came from. */
6507 it->what = IT_CHARACTER;
6508 it->object = it->string;
6509 it->position = position;
6510 return 1;
6511 }
6512
6513
6514 /* Load IT with next display element from C string IT->s.
6515 IT->string_nchars is the maximum number of characters to return
6516 from the string. IT->end_charpos may be greater than
6517 IT->string_nchars when this function is called, in which case we
6518 may have to return padding spaces. Value is zero if end of string
6519 reached, including padding spaces. */
6520
6521 static int
6522 next_element_from_c_string (struct it *it)
6523 {
6524 int success_p = 1;
6525
6526 xassert (it->s);
6527 it->what = IT_CHARACTER;
6528 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6529 it->object = Qnil;
6530
6531 /* IT's position can be greater IT->string_nchars in case a field
6532 width or precision has been specified when the iterator was
6533 initialized. */
6534 if (IT_CHARPOS (*it) >= it->end_charpos)
6535 {
6536 /* End of the game. */
6537 it->what = IT_EOB;
6538 success_p = 0;
6539 }
6540 else if (IT_CHARPOS (*it) >= it->string_nchars)
6541 {
6542 /* Pad with spaces. */
6543 it->c = ' ', it->len = 1;
6544 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6545 }
6546 else if (it->multibyte_p)
6547 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6548 else
6549 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6550
6551 return success_p;
6552 }
6553
6554
6555 /* Set up IT to return characters from an ellipsis, if appropriate.
6556 The definition of the ellipsis glyphs may come from a display table
6557 entry. This function fills IT with the first glyph from the
6558 ellipsis if an ellipsis is to be displayed. */
6559
6560 static int
6561 next_element_from_ellipsis (struct it *it)
6562 {
6563 if (it->selective_display_ellipsis_p)
6564 setup_for_ellipsis (it, it->len);
6565 else
6566 {
6567 /* The face at the current position may be different from the
6568 face we find after the invisible text. Remember what it
6569 was in IT->saved_face_id, and signal that it's there by
6570 setting face_before_selective_p. */
6571 it->saved_face_id = it->face_id;
6572 it->method = GET_FROM_BUFFER;
6573 it->object = it->w->buffer;
6574 reseat_at_next_visible_line_start (it, 1);
6575 it->face_before_selective_p = 1;
6576 }
6577
6578 return GET_NEXT_DISPLAY_ELEMENT (it);
6579 }
6580
6581
6582 /* Deliver an image display element. The iterator IT is already
6583 filled with image information (done in handle_display_prop). Value
6584 is always 1. */
6585
6586
6587 static int
6588 next_element_from_image (struct it *it)
6589 {
6590 it->what = IT_IMAGE;
6591 it->ignore_overlay_strings_at_pos_p = 0;
6592 return 1;
6593 }
6594
6595
6596 /* Fill iterator IT with next display element from a stretch glyph
6597 property. IT->object is the value of the text property. Value is
6598 always 1. */
6599
6600 static int
6601 next_element_from_stretch (struct it *it)
6602 {
6603 it->what = IT_STRETCH;
6604 return 1;
6605 }
6606
6607 /* Scan forward from CHARPOS in the current buffer, until we find a
6608 stop position > current IT's position. Then handle the stop
6609 position before that. This is called when we bump into a stop
6610 position while reordering bidirectional text. CHARPOS should be
6611 the last previously processed stop_pos (or BEGV, if none were
6612 processed yet) whose position is less that IT's current
6613 position. */
6614
6615 static void
6616 handle_stop_backwards (struct it *it, EMACS_INT charpos)
6617 {
6618 EMACS_INT where_we_are = IT_CHARPOS (*it);
6619 struct display_pos save_current = it->current;
6620 struct text_pos save_position = it->position;
6621 struct text_pos pos1;
6622 EMACS_INT next_stop;
6623
6624 /* Scan in strict logical order. */
6625 it->bidi_p = 0;
6626 do
6627 {
6628 it->prev_stop = charpos;
6629 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6630 reseat_1 (it, pos1, 0);
6631 compute_stop_pos (it);
6632 /* We must advance forward, right? */
6633 if (it->stop_charpos <= it->prev_stop)
6634 abort ();
6635 charpos = it->stop_charpos;
6636 }
6637 while (charpos <= where_we_are);
6638
6639 next_stop = it->stop_charpos;
6640 it->stop_charpos = it->prev_stop;
6641 it->bidi_p = 1;
6642 it->current = save_current;
6643 it->position = save_position;
6644 handle_stop (it);
6645 it->stop_charpos = next_stop;
6646 }
6647
6648 /* Load IT with the next display element from current_buffer. Value
6649 is zero if end of buffer reached. IT->stop_charpos is the next
6650 position at which to stop and check for text properties or buffer
6651 end. */
6652
6653 static int
6654 next_element_from_buffer (struct it *it)
6655 {
6656 int success_p = 1;
6657
6658 xassert (IT_CHARPOS (*it) >= BEGV);
6659
6660 /* With bidi reordering, the character to display might not be the
6661 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6662 we were reseat()ed to a new buffer position, which is potentially
6663 a different paragraph. */
6664 if (it->bidi_p && it->bidi_it.first_elt)
6665 {
6666 it->bidi_it.charpos = IT_CHARPOS (*it);
6667 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6668 if (it->bidi_it.bytepos == ZV_BYTE)
6669 {
6670 /* Nothing to do, but reset the FIRST_ELT flag, like
6671 bidi_paragraph_init does, because we are not going to
6672 call it. */
6673 it->bidi_it.first_elt = 0;
6674 }
6675 else if (it->bidi_it.bytepos == BEGV_BYTE
6676 /* FIXME: Should support all Unicode line separators. */
6677 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6678 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6679 {
6680 /* If we are at the beginning of a line, we can produce the
6681 next element right away. */
6682 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6683 bidi_move_to_visually_next (&it->bidi_it);
6684 }
6685 else
6686 {
6687 EMACS_INT orig_bytepos = IT_BYTEPOS (*it);
6688
6689 /* We need to prime the bidi iterator starting at the line's
6690 beginning, before we will be able to produce the next
6691 element. */
6692 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6693 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6694 it->bidi_it.charpos = IT_CHARPOS (*it);
6695 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6696 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6697 do
6698 {
6699 /* Now return to buffer position where we were asked to
6700 get the next display element, and produce that. */
6701 bidi_move_to_visually_next (&it->bidi_it);
6702 }
6703 while (it->bidi_it.bytepos != orig_bytepos
6704 && it->bidi_it.bytepos < ZV_BYTE);
6705 }
6706
6707 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6708 /* Adjust IT's position information to where we ended up. */
6709 IT_CHARPOS (*it) = it->bidi_it.charpos;
6710 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6711 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6712 {
6713 EMACS_INT stop = it->end_charpos;
6714 if (it->bidi_it.scan_dir < 0)
6715 stop = -1;
6716 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6717 IT_BYTEPOS (*it), stop, Qnil);
6718 }
6719 }
6720
6721 if (IT_CHARPOS (*it) >= it->stop_charpos)
6722 {
6723 if (IT_CHARPOS (*it) >= it->end_charpos)
6724 {
6725 int overlay_strings_follow_p;
6726
6727 /* End of the game, except when overlay strings follow that
6728 haven't been returned yet. */
6729 if (it->overlay_strings_at_end_processed_p)
6730 overlay_strings_follow_p = 0;
6731 else
6732 {
6733 it->overlay_strings_at_end_processed_p = 1;
6734 overlay_strings_follow_p = get_overlay_strings (it, 0);
6735 }
6736
6737 if (overlay_strings_follow_p)
6738 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6739 else
6740 {
6741 it->what = IT_EOB;
6742 it->position = it->current.pos;
6743 success_p = 0;
6744 }
6745 }
6746 else if (!(!it->bidi_p
6747 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6748 || IT_CHARPOS (*it) == it->stop_charpos))
6749 {
6750 /* With bidi non-linear iteration, we could find ourselves
6751 far beyond the last computed stop_charpos, with several
6752 other stop positions in between that we missed. Scan
6753 them all now, in buffer's logical order, until we find
6754 and handle the last stop_charpos that precedes our
6755 current position. */
6756 handle_stop_backwards (it, it->stop_charpos);
6757 return GET_NEXT_DISPLAY_ELEMENT (it);
6758 }
6759 else
6760 {
6761 if (it->bidi_p)
6762 {
6763 /* Take note of the stop position we just moved across,
6764 for when we will move back across it. */
6765 it->prev_stop = it->stop_charpos;
6766 /* If we are at base paragraph embedding level, take
6767 note of the last stop position seen at this
6768 level. */
6769 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6770 it->base_level_stop = it->stop_charpos;
6771 }
6772 handle_stop (it);
6773 return GET_NEXT_DISPLAY_ELEMENT (it);
6774 }
6775 }
6776 else if (it->bidi_p
6777 /* We can sometimes back up for reasons that have nothing
6778 to do with bidi reordering. E.g., compositions. The
6779 code below is only needed when we are above the base
6780 embedding level, so test for that explicitly. */
6781 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6782 && IT_CHARPOS (*it) < it->prev_stop)
6783 {
6784 if (it->base_level_stop <= 0)
6785 it->base_level_stop = BEGV;
6786 if (IT_CHARPOS (*it) < it->base_level_stop)
6787 abort ();
6788 handle_stop_backwards (it, it->base_level_stop);
6789 return GET_NEXT_DISPLAY_ELEMENT (it);
6790 }
6791 else
6792 {
6793 /* No face changes, overlays etc. in sight, so just return a
6794 character from current_buffer. */
6795 unsigned char *p;
6796 EMACS_INT stop;
6797
6798 /* Maybe run the redisplay end trigger hook. Performance note:
6799 This doesn't seem to cost measurable time. */
6800 if (it->redisplay_end_trigger_charpos
6801 && it->glyph_row
6802 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6803 run_redisplay_end_trigger_hook (it);
6804
6805 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
6806 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6807 stop)
6808 && next_element_from_composition (it))
6809 {
6810 return 1;
6811 }
6812
6813 /* Get the next character, maybe multibyte. */
6814 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6815 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6816 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6817 else
6818 it->c = *p, it->len = 1;
6819
6820 /* Record what we have and where it came from. */
6821 it->what = IT_CHARACTER;
6822 it->object = it->w->buffer;
6823 it->position = it->current.pos;
6824
6825 /* Normally we return the character found above, except when we
6826 really want to return an ellipsis for selective display. */
6827 if (it->selective)
6828 {
6829 if (it->c == '\n')
6830 {
6831 /* A value of selective > 0 means hide lines indented more
6832 than that number of columns. */
6833 if (it->selective > 0
6834 && IT_CHARPOS (*it) + 1 < ZV
6835 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6836 IT_BYTEPOS (*it) + 1,
6837 (double) it->selective)) /* iftc */
6838 {
6839 success_p = next_element_from_ellipsis (it);
6840 it->dpvec_char_len = -1;
6841 }
6842 }
6843 else if (it->c == '\r' && it->selective == -1)
6844 {
6845 /* A value of selective == -1 means that everything from the
6846 CR to the end of the line is invisible, with maybe an
6847 ellipsis displayed for it. */
6848 success_p = next_element_from_ellipsis (it);
6849 it->dpvec_char_len = -1;
6850 }
6851 }
6852 }
6853
6854 /* Value is zero if end of buffer reached. */
6855 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6856 return success_p;
6857 }
6858
6859
6860 /* Run the redisplay end trigger hook for IT. */
6861
6862 static void
6863 run_redisplay_end_trigger_hook (struct it *it)
6864 {
6865 Lisp_Object args[3];
6866
6867 /* IT->glyph_row should be non-null, i.e. we should be actually
6868 displaying something, or otherwise we should not run the hook. */
6869 xassert (it->glyph_row);
6870
6871 /* Set up hook arguments. */
6872 args[0] = Qredisplay_end_trigger_functions;
6873 args[1] = it->window;
6874 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6875 it->redisplay_end_trigger_charpos = 0;
6876
6877 /* Since we are *trying* to run these functions, don't try to run
6878 them again, even if they get an error. */
6879 it->w->redisplay_end_trigger = Qnil;
6880 Frun_hook_with_args (3, args);
6881
6882 /* Notice if it changed the face of the character we are on. */
6883 handle_face_prop (it);
6884 }
6885
6886
6887 /* Deliver a composition display element. Unlike the other
6888 next_element_from_XXX, this function is not registered in the array
6889 get_next_element[]. It is called from next_element_from_buffer and
6890 next_element_from_string when necessary. */
6891
6892 static int
6893 next_element_from_composition (struct it *it)
6894 {
6895 it->what = IT_COMPOSITION;
6896 it->len = it->cmp_it.nbytes;
6897 if (STRINGP (it->string))
6898 {
6899 if (it->c < 0)
6900 {
6901 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6902 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6903 return 0;
6904 }
6905 it->position = it->current.string_pos;
6906 it->object = it->string;
6907 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6908 IT_STRING_BYTEPOS (*it), it->string);
6909 }
6910 else
6911 {
6912 if (it->c < 0)
6913 {
6914 IT_CHARPOS (*it) += it->cmp_it.nchars;
6915 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6916 if (it->bidi_p)
6917 {
6918 if (it->bidi_it.new_paragraph)
6919 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6920 /* Resync the bidi iterator with IT's new position.
6921 FIXME: this doesn't support bidirectional text. */
6922 while (it->bidi_it.charpos < IT_CHARPOS (*it))
6923 bidi_move_to_visually_next (&it->bidi_it);
6924 }
6925 return 0;
6926 }
6927 it->position = it->current.pos;
6928 it->object = it->w->buffer;
6929 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6930 IT_BYTEPOS (*it), Qnil);
6931 }
6932 return 1;
6933 }
6934
6935
6936 \f
6937 /***********************************************************************
6938 Moving an iterator without producing glyphs
6939 ***********************************************************************/
6940
6941 /* Check if iterator is at a position corresponding to a valid buffer
6942 position after some move_it_ call. */
6943
6944 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6945 ((it)->method == GET_FROM_STRING \
6946 ? IT_STRING_CHARPOS (*it) == 0 \
6947 : 1)
6948
6949
6950 /* Move iterator IT to a specified buffer or X position within one
6951 line on the display without producing glyphs.
6952
6953 OP should be a bit mask including some or all of these bits:
6954 MOVE_TO_X: Stop upon reaching x-position TO_X.
6955 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
6956 Regardless of OP's value, stop upon reaching the end of the display line.
6957
6958 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6959 This means, in particular, that TO_X includes window's horizontal
6960 scroll amount.
6961
6962 The return value has several possible values that
6963 say what condition caused the scan to stop:
6964
6965 MOVE_POS_MATCH_OR_ZV
6966 - when TO_POS or ZV was reached.
6967
6968 MOVE_X_REACHED
6969 -when TO_X was reached before TO_POS or ZV were reached.
6970
6971 MOVE_LINE_CONTINUED
6972 - when we reached the end of the display area and the line must
6973 be continued.
6974
6975 MOVE_LINE_TRUNCATED
6976 - when we reached the end of the display area and the line is
6977 truncated.
6978
6979 MOVE_NEWLINE_OR_CR
6980 - when we stopped at a line end, i.e. a newline or a CR and selective
6981 display is on. */
6982
6983 static enum move_it_result
6984 move_it_in_display_line_to (struct it *it,
6985 EMACS_INT to_charpos, int to_x,
6986 enum move_operation_enum op)
6987 {
6988 enum move_it_result result = MOVE_UNDEFINED;
6989 struct glyph_row *saved_glyph_row;
6990 struct it wrap_it, atpos_it, atx_it;
6991 int may_wrap = 0;
6992 enum it_method prev_method = it->method;
6993 EMACS_INT prev_pos = IT_CHARPOS (*it);
6994
6995 /* Don't produce glyphs in produce_glyphs. */
6996 saved_glyph_row = it->glyph_row;
6997 it->glyph_row = NULL;
6998
6999 /* Use wrap_it to save a copy of IT wherever a word wrap could
7000 occur. Use atpos_it to save a copy of IT at the desired buffer
7001 position, if found, so that we can scan ahead and check if the
7002 word later overshoots the window edge. Use atx_it similarly, for
7003 pixel positions. */
7004 wrap_it.sp = -1;
7005 atpos_it.sp = -1;
7006 atx_it.sp = -1;
7007
7008 #define BUFFER_POS_REACHED_P() \
7009 ((op & MOVE_TO_POS) != 0 \
7010 && BUFFERP (it->object) \
7011 && (IT_CHARPOS (*it) == to_charpos \
7012 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7013 && (it->method == GET_FROM_BUFFER \
7014 || (it->method == GET_FROM_DISPLAY_VECTOR \
7015 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7016
7017 /* If there's a line-/wrap-prefix, handle it. */
7018 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7019 && it->current_y < it->last_visible_y)
7020 handle_line_prefix (it);
7021
7022 while (1)
7023 {
7024 int x, i, ascent = 0, descent = 0;
7025
7026 /* Utility macro to reset an iterator with x, ascent, and descent. */
7027 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7028 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7029 (IT)->max_descent = descent)
7030
7031 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7032 glyph). */
7033 if ((op & MOVE_TO_POS) != 0
7034 && BUFFERP (it->object)
7035 && it->method == GET_FROM_BUFFER
7036 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7037 || (it->bidi_p
7038 && (prev_method == GET_FROM_IMAGE
7039 || prev_method == GET_FROM_STRETCH)
7040 /* Passed TO_CHARPOS from left to right. */
7041 && ((prev_pos < to_charpos
7042 && IT_CHARPOS (*it) > to_charpos)
7043 /* Passed TO_CHARPOS from right to left. */
7044 || (prev_pos > to_charpos
7045 && IT_CHARPOS (*it) < to_charpos)))))
7046 {
7047 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7048 {
7049 result = MOVE_POS_MATCH_OR_ZV;
7050 break;
7051 }
7052 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7053 /* If wrap_it is valid, the current position might be in a
7054 word that is wrapped. So, save the iterator in
7055 atpos_it and continue to see if wrapping happens. */
7056 atpos_it = *it;
7057 }
7058
7059 prev_method = it->method;
7060 if (it->method == GET_FROM_BUFFER)
7061 prev_pos = IT_CHARPOS (*it);
7062 /* Stop when ZV reached.
7063 We used to stop here when TO_CHARPOS reached as well, but that is
7064 too soon if this glyph does not fit on this line. So we handle it
7065 explicitly below. */
7066 if (!get_next_display_element (it))
7067 {
7068 result = MOVE_POS_MATCH_OR_ZV;
7069 break;
7070 }
7071
7072 if (it->line_wrap == TRUNCATE)
7073 {
7074 if (BUFFER_POS_REACHED_P ())
7075 {
7076 result = MOVE_POS_MATCH_OR_ZV;
7077 break;
7078 }
7079 }
7080 else
7081 {
7082 if (it->line_wrap == WORD_WRAP)
7083 {
7084 if (IT_DISPLAYING_WHITESPACE (it))
7085 may_wrap = 1;
7086 else if (may_wrap)
7087 {
7088 /* We have reached a glyph that follows one or more
7089 whitespace characters. If the position is
7090 already found, we are done. */
7091 if (atpos_it.sp >= 0)
7092 {
7093 *it = atpos_it;
7094 result = MOVE_POS_MATCH_OR_ZV;
7095 goto done;
7096 }
7097 if (atx_it.sp >= 0)
7098 {
7099 *it = atx_it;
7100 result = MOVE_X_REACHED;
7101 goto done;
7102 }
7103 /* Otherwise, we can wrap here. */
7104 wrap_it = *it;
7105 may_wrap = 0;
7106 }
7107 }
7108 }
7109
7110 /* Remember the line height for the current line, in case
7111 the next element doesn't fit on the line. */
7112 ascent = it->max_ascent;
7113 descent = it->max_descent;
7114
7115 /* The call to produce_glyphs will get the metrics of the
7116 display element IT is loaded with. Record the x-position
7117 before this display element, in case it doesn't fit on the
7118 line. */
7119 x = it->current_x;
7120
7121 PRODUCE_GLYPHS (it);
7122
7123 if (it->area != TEXT_AREA)
7124 {
7125 set_iterator_to_next (it, 1);
7126 continue;
7127 }
7128
7129 /* The number of glyphs we get back in IT->nglyphs will normally
7130 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7131 character on a terminal frame, or (iii) a line end. For the
7132 second case, IT->nglyphs - 1 padding glyphs will be present.
7133 (On X frames, there is only one glyph produced for a
7134 composite character.)
7135
7136 The behavior implemented below means, for continuation lines,
7137 that as many spaces of a TAB as fit on the current line are
7138 displayed there. For terminal frames, as many glyphs of a
7139 multi-glyph character are displayed in the current line, too.
7140 This is what the old redisplay code did, and we keep it that
7141 way. Under X, the whole shape of a complex character must
7142 fit on the line or it will be completely displayed in the
7143 next line.
7144
7145 Note that both for tabs and padding glyphs, all glyphs have
7146 the same width. */
7147 if (it->nglyphs)
7148 {
7149 /* More than one glyph or glyph doesn't fit on line. All
7150 glyphs have the same width. */
7151 int single_glyph_width = it->pixel_width / it->nglyphs;
7152 int new_x;
7153 int x_before_this_char = x;
7154 int hpos_before_this_char = it->hpos;
7155
7156 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7157 {
7158 new_x = x + single_glyph_width;
7159
7160 /* We want to leave anything reaching TO_X to the caller. */
7161 if ((op & MOVE_TO_X) && new_x > to_x)
7162 {
7163 if (BUFFER_POS_REACHED_P ())
7164 {
7165 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7166 goto buffer_pos_reached;
7167 if (atpos_it.sp < 0)
7168 {
7169 atpos_it = *it;
7170 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7171 }
7172 }
7173 else
7174 {
7175 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7176 {
7177 it->current_x = x;
7178 result = MOVE_X_REACHED;
7179 break;
7180 }
7181 if (atx_it.sp < 0)
7182 {
7183 atx_it = *it;
7184 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7185 }
7186 }
7187 }
7188
7189 if (/* Lines are continued. */
7190 it->line_wrap != TRUNCATE
7191 && (/* And glyph doesn't fit on the line. */
7192 new_x > it->last_visible_x
7193 /* Or it fits exactly and we're on a window
7194 system frame. */
7195 || (new_x == it->last_visible_x
7196 && FRAME_WINDOW_P (it->f))))
7197 {
7198 if (/* IT->hpos == 0 means the very first glyph
7199 doesn't fit on the line, e.g. a wide image. */
7200 it->hpos == 0
7201 || (new_x == it->last_visible_x
7202 && FRAME_WINDOW_P (it->f)))
7203 {
7204 ++it->hpos;
7205 it->current_x = new_x;
7206
7207 /* The character's last glyph just barely fits
7208 in this row. */
7209 if (i == it->nglyphs - 1)
7210 {
7211 /* If this is the destination position,
7212 return a position *before* it in this row,
7213 now that we know it fits in this row. */
7214 if (BUFFER_POS_REACHED_P ())
7215 {
7216 if (it->line_wrap != WORD_WRAP
7217 || wrap_it.sp < 0)
7218 {
7219 it->hpos = hpos_before_this_char;
7220 it->current_x = x_before_this_char;
7221 result = MOVE_POS_MATCH_OR_ZV;
7222 break;
7223 }
7224 if (it->line_wrap == WORD_WRAP
7225 && atpos_it.sp < 0)
7226 {
7227 atpos_it = *it;
7228 atpos_it.current_x = x_before_this_char;
7229 atpos_it.hpos = hpos_before_this_char;
7230 }
7231 }
7232
7233 set_iterator_to_next (it, 1);
7234 /* On graphical terminals, newlines may
7235 "overflow" into the fringe if
7236 overflow-newline-into-fringe is non-nil.
7237 On text-only terminals, newlines may
7238 overflow into the last glyph on the
7239 display line.*/
7240 if (!FRAME_WINDOW_P (it->f)
7241 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7242 {
7243 if (!get_next_display_element (it))
7244 {
7245 result = MOVE_POS_MATCH_OR_ZV;
7246 break;
7247 }
7248 if (BUFFER_POS_REACHED_P ())
7249 {
7250 if (ITERATOR_AT_END_OF_LINE_P (it))
7251 result = MOVE_POS_MATCH_OR_ZV;
7252 else
7253 result = MOVE_LINE_CONTINUED;
7254 break;
7255 }
7256 if (ITERATOR_AT_END_OF_LINE_P (it))
7257 {
7258 result = MOVE_NEWLINE_OR_CR;
7259 break;
7260 }
7261 }
7262 }
7263 }
7264 else
7265 IT_RESET_X_ASCENT_DESCENT (it);
7266
7267 if (wrap_it.sp >= 0)
7268 {
7269 *it = wrap_it;
7270 atpos_it.sp = -1;
7271 atx_it.sp = -1;
7272 }
7273
7274 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7275 IT_CHARPOS (*it)));
7276 result = MOVE_LINE_CONTINUED;
7277 break;
7278 }
7279
7280 if (BUFFER_POS_REACHED_P ())
7281 {
7282 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7283 goto buffer_pos_reached;
7284 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7285 {
7286 atpos_it = *it;
7287 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7288 }
7289 }
7290
7291 if (new_x > it->first_visible_x)
7292 {
7293 /* Glyph is visible. Increment number of glyphs that
7294 would be displayed. */
7295 ++it->hpos;
7296 }
7297 }
7298
7299 if (result != MOVE_UNDEFINED)
7300 break;
7301 }
7302 else if (BUFFER_POS_REACHED_P ())
7303 {
7304 buffer_pos_reached:
7305 IT_RESET_X_ASCENT_DESCENT (it);
7306 result = MOVE_POS_MATCH_OR_ZV;
7307 break;
7308 }
7309 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7310 {
7311 /* Stop when TO_X specified and reached. This check is
7312 necessary here because of lines consisting of a line end,
7313 only. The line end will not produce any glyphs and we
7314 would never get MOVE_X_REACHED. */
7315 xassert (it->nglyphs == 0);
7316 result = MOVE_X_REACHED;
7317 break;
7318 }
7319
7320 /* Is this a line end? If yes, we're done. */
7321 if (ITERATOR_AT_END_OF_LINE_P (it))
7322 {
7323 result = MOVE_NEWLINE_OR_CR;
7324 break;
7325 }
7326
7327 if (it->method == GET_FROM_BUFFER)
7328 prev_pos = IT_CHARPOS (*it);
7329 /* The current display element has been consumed. Advance
7330 to the next. */
7331 set_iterator_to_next (it, 1);
7332
7333 /* Stop if lines are truncated and IT's current x-position is
7334 past the right edge of the window now. */
7335 if (it->line_wrap == TRUNCATE
7336 && it->current_x >= it->last_visible_x)
7337 {
7338 if (!FRAME_WINDOW_P (it->f)
7339 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7340 {
7341 if (!get_next_display_element (it)
7342 || BUFFER_POS_REACHED_P ())
7343 {
7344 result = MOVE_POS_MATCH_OR_ZV;
7345 break;
7346 }
7347 if (ITERATOR_AT_END_OF_LINE_P (it))
7348 {
7349 result = MOVE_NEWLINE_OR_CR;
7350 break;
7351 }
7352 }
7353 result = MOVE_LINE_TRUNCATED;
7354 break;
7355 }
7356 #undef IT_RESET_X_ASCENT_DESCENT
7357 }
7358
7359 #undef BUFFER_POS_REACHED_P
7360
7361 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7362 restore the saved iterator. */
7363 if (atpos_it.sp >= 0)
7364 *it = atpos_it;
7365 else if (atx_it.sp >= 0)
7366 *it = atx_it;
7367
7368 done:
7369
7370 /* Restore the iterator settings altered at the beginning of this
7371 function. */
7372 it->glyph_row = saved_glyph_row;
7373 return result;
7374 }
7375
7376 /* For external use. */
7377 void
7378 move_it_in_display_line (struct it *it,
7379 EMACS_INT to_charpos, int to_x,
7380 enum move_operation_enum op)
7381 {
7382 if (it->line_wrap == WORD_WRAP
7383 && (op & MOVE_TO_X))
7384 {
7385 struct it save_it = *it;
7386 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7387 /* When word-wrap is on, TO_X may lie past the end
7388 of a wrapped line. Then it->current is the
7389 character on the next line, so backtrack to the
7390 space before the wrap point. */
7391 if (skip == MOVE_LINE_CONTINUED)
7392 {
7393 int prev_x = max (it->current_x - 1, 0);
7394 *it = save_it;
7395 move_it_in_display_line_to
7396 (it, -1, prev_x, MOVE_TO_X);
7397 }
7398 }
7399 else
7400 move_it_in_display_line_to (it, to_charpos, to_x, op);
7401 }
7402
7403
7404 /* Move IT forward until it satisfies one or more of the criteria in
7405 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7406
7407 OP is a bit-mask that specifies where to stop, and in particular,
7408 which of those four position arguments makes a difference. See the
7409 description of enum move_operation_enum.
7410
7411 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7412 screen line, this function will set IT to the next position >
7413 TO_CHARPOS. */
7414
7415 void
7416 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7417 {
7418 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7419 int line_height, line_start_x = 0, reached = 0;
7420
7421 for (;;)
7422 {
7423 if (op & MOVE_TO_VPOS)
7424 {
7425 /* If no TO_CHARPOS and no TO_X specified, stop at the
7426 start of the line TO_VPOS. */
7427 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7428 {
7429 if (it->vpos == to_vpos)
7430 {
7431 reached = 1;
7432 break;
7433 }
7434 else
7435 skip = move_it_in_display_line_to (it, -1, -1, 0);
7436 }
7437 else
7438 {
7439 /* TO_VPOS >= 0 means stop at TO_X in the line at
7440 TO_VPOS, or at TO_POS, whichever comes first. */
7441 if (it->vpos == to_vpos)
7442 {
7443 reached = 2;
7444 break;
7445 }
7446
7447 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7448
7449 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7450 {
7451 reached = 3;
7452 break;
7453 }
7454 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7455 {
7456 /* We have reached TO_X but not in the line we want. */
7457 skip = move_it_in_display_line_to (it, to_charpos,
7458 -1, MOVE_TO_POS);
7459 if (skip == MOVE_POS_MATCH_OR_ZV)
7460 {
7461 reached = 4;
7462 break;
7463 }
7464 }
7465 }
7466 }
7467 else if (op & MOVE_TO_Y)
7468 {
7469 struct it it_backup;
7470
7471 if (it->line_wrap == WORD_WRAP)
7472 it_backup = *it;
7473
7474 /* TO_Y specified means stop at TO_X in the line containing
7475 TO_Y---or at TO_CHARPOS if this is reached first. The
7476 problem is that we can't really tell whether the line
7477 contains TO_Y before we have completely scanned it, and
7478 this may skip past TO_X. What we do is to first scan to
7479 TO_X.
7480
7481 If TO_X is not specified, use a TO_X of zero. The reason
7482 is to make the outcome of this function more predictable.
7483 If we didn't use TO_X == 0, we would stop at the end of
7484 the line which is probably not what a caller would expect
7485 to happen. */
7486 skip = move_it_in_display_line_to
7487 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7488 (MOVE_TO_X | (op & MOVE_TO_POS)));
7489
7490 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7491 if (skip == MOVE_POS_MATCH_OR_ZV)
7492 reached = 5;
7493 else if (skip == MOVE_X_REACHED)
7494 {
7495 /* If TO_X was reached, we want to know whether TO_Y is
7496 in the line. We know this is the case if the already
7497 scanned glyphs make the line tall enough. Otherwise,
7498 we must check by scanning the rest of the line. */
7499 line_height = it->max_ascent + it->max_descent;
7500 if (to_y >= it->current_y
7501 && to_y < it->current_y + line_height)
7502 {
7503 reached = 6;
7504 break;
7505 }
7506 it_backup = *it;
7507 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7508 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7509 op & MOVE_TO_POS);
7510 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7511 line_height = it->max_ascent + it->max_descent;
7512 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7513
7514 if (to_y >= it->current_y
7515 && to_y < it->current_y + line_height)
7516 {
7517 /* If TO_Y is in this line and TO_X was reached
7518 above, we scanned too far. We have to restore
7519 IT's settings to the ones before skipping. */
7520 *it = it_backup;
7521 reached = 6;
7522 }
7523 else
7524 {
7525 skip = skip2;
7526 if (skip == MOVE_POS_MATCH_OR_ZV)
7527 reached = 7;
7528 }
7529 }
7530 else
7531 {
7532 /* Check whether TO_Y is in this line. */
7533 line_height = it->max_ascent + it->max_descent;
7534 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7535
7536 if (to_y >= it->current_y
7537 && to_y < it->current_y + line_height)
7538 {
7539 /* When word-wrap is on, TO_X may lie past the end
7540 of a wrapped line. Then it->current is the
7541 character on the next line, so backtrack to the
7542 space before the wrap point. */
7543 if (skip == MOVE_LINE_CONTINUED
7544 && it->line_wrap == WORD_WRAP)
7545 {
7546 int prev_x = max (it->current_x - 1, 0);
7547 *it = it_backup;
7548 skip = move_it_in_display_line_to
7549 (it, -1, prev_x, MOVE_TO_X);
7550 }
7551 reached = 6;
7552 }
7553 }
7554
7555 if (reached)
7556 break;
7557 }
7558 else if (BUFFERP (it->object)
7559 && (it->method == GET_FROM_BUFFER
7560 || it->method == GET_FROM_STRETCH)
7561 && IT_CHARPOS (*it) >= to_charpos)
7562 skip = MOVE_POS_MATCH_OR_ZV;
7563 else
7564 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7565
7566 switch (skip)
7567 {
7568 case MOVE_POS_MATCH_OR_ZV:
7569 reached = 8;
7570 goto out;
7571
7572 case MOVE_NEWLINE_OR_CR:
7573 set_iterator_to_next (it, 1);
7574 it->continuation_lines_width = 0;
7575 break;
7576
7577 case MOVE_LINE_TRUNCATED:
7578 it->continuation_lines_width = 0;
7579 reseat_at_next_visible_line_start (it, 0);
7580 if ((op & MOVE_TO_POS) != 0
7581 && IT_CHARPOS (*it) > to_charpos)
7582 {
7583 reached = 9;
7584 goto out;
7585 }
7586 break;
7587
7588 case MOVE_LINE_CONTINUED:
7589 /* For continued lines ending in a tab, some of the glyphs
7590 associated with the tab are displayed on the current
7591 line. Since it->current_x does not include these glyphs,
7592 we use it->last_visible_x instead. */
7593 if (it->c == '\t')
7594 {
7595 it->continuation_lines_width += it->last_visible_x;
7596 /* When moving by vpos, ensure that the iterator really
7597 advances to the next line (bug#847, bug#969). Fixme:
7598 do we need to do this in other circumstances? */
7599 if (it->current_x != it->last_visible_x
7600 && (op & MOVE_TO_VPOS)
7601 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7602 {
7603 line_start_x = it->current_x + it->pixel_width
7604 - it->last_visible_x;
7605 set_iterator_to_next (it, 0);
7606 }
7607 }
7608 else
7609 it->continuation_lines_width += it->current_x;
7610 break;
7611
7612 default:
7613 abort ();
7614 }
7615
7616 /* Reset/increment for the next run. */
7617 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7618 it->current_x = line_start_x;
7619 line_start_x = 0;
7620 it->hpos = 0;
7621 it->current_y += it->max_ascent + it->max_descent;
7622 ++it->vpos;
7623 last_height = it->max_ascent + it->max_descent;
7624 last_max_ascent = it->max_ascent;
7625 it->max_ascent = it->max_descent = 0;
7626 }
7627
7628 out:
7629
7630 /* On text terminals, we may stop at the end of a line in the middle
7631 of a multi-character glyph. If the glyph itself is continued,
7632 i.e. it is actually displayed on the next line, don't treat this
7633 stopping point as valid; move to the next line instead (unless
7634 that brings us offscreen). */
7635 if (!FRAME_WINDOW_P (it->f)
7636 && op & MOVE_TO_POS
7637 && IT_CHARPOS (*it) == to_charpos
7638 && it->what == IT_CHARACTER
7639 && it->nglyphs > 1
7640 && it->line_wrap == WINDOW_WRAP
7641 && it->current_x == it->last_visible_x - 1
7642 && it->c != '\n'
7643 && it->c != '\t'
7644 && it->vpos < XFASTINT (it->w->window_end_vpos))
7645 {
7646 it->continuation_lines_width += it->current_x;
7647 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7648 it->current_y += it->max_ascent + it->max_descent;
7649 ++it->vpos;
7650 last_height = it->max_ascent + it->max_descent;
7651 last_max_ascent = it->max_ascent;
7652 }
7653
7654 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7655 }
7656
7657
7658 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7659
7660 If DY > 0, move IT backward at least that many pixels. DY = 0
7661 means move IT backward to the preceding line start or BEGV. This
7662 function may move over more than DY pixels if IT->current_y - DY
7663 ends up in the middle of a line; in this case IT->current_y will be
7664 set to the top of the line moved to. */
7665
7666 void
7667 move_it_vertically_backward (struct it *it, int dy)
7668 {
7669 int nlines, h;
7670 struct it it2, it3;
7671 EMACS_INT start_pos;
7672
7673 move_further_back:
7674 xassert (dy >= 0);
7675
7676 start_pos = IT_CHARPOS (*it);
7677
7678 /* Estimate how many newlines we must move back. */
7679 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7680
7681 /* Set the iterator's position that many lines back. */
7682 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7683 back_to_previous_visible_line_start (it);
7684
7685 /* Reseat the iterator here. When moving backward, we don't want
7686 reseat to skip forward over invisible text, set up the iterator
7687 to deliver from overlay strings at the new position etc. So,
7688 use reseat_1 here. */
7689 reseat_1 (it, it->current.pos, 1);
7690
7691 /* We are now surely at a line start. */
7692 it->current_x = it->hpos = 0;
7693 it->continuation_lines_width = 0;
7694
7695 /* Move forward and see what y-distance we moved. First move to the
7696 start of the next line so that we get its height. We need this
7697 height to be able to tell whether we reached the specified
7698 y-distance. */
7699 it2 = *it;
7700 it2.max_ascent = it2.max_descent = 0;
7701 do
7702 {
7703 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7704 MOVE_TO_POS | MOVE_TO_VPOS);
7705 }
7706 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7707 xassert (IT_CHARPOS (*it) >= BEGV);
7708 it3 = it2;
7709
7710 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7711 xassert (IT_CHARPOS (*it) >= BEGV);
7712 /* H is the actual vertical distance from the position in *IT
7713 and the starting position. */
7714 h = it2.current_y - it->current_y;
7715 /* NLINES is the distance in number of lines. */
7716 nlines = it2.vpos - it->vpos;
7717
7718 /* Correct IT's y and vpos position
7719 so that they are relative to the starting point. */
7720 it->vpos -= nlines;
7721 it->current_y -= h;
7722
7723 if (dy == 0)
7724 {
7725 /* DY == 0 means move to the start of the screen line. The
7726 value of nlines is > 0 if continuation lines were involved. */
7727 if (nlines > 0)
7728 move_it_by_lines (it, nlines, 1);
7729 }
7730 else
7731 {
7732 /* The y-position we try to reach, relative to *IT.
7733 Note that H has been subtracted in front of the if-statement. */
7734 int target_y = it->current_y + h - dy;
7735 int y0 = it3.current_y;
7736 int y1 = line_bottom_y (&it3);
7737 int line_height = y1 - y0;
7738
7739 /* If we did not reach target_y, try to move further backward if
7740 we can. If we moved too far backward, try to move forward. */
7741 if (target_y < it->current_y
7742 /* This is heuristic. In a window that's 3 lines high, with
7743 a line height of 13 pixels each, recentering with point
7744 on the bottom line will try to move -39/2 = 19 pixels
7745 backward. Try to avoid moving into the first line. */
7746 && (it->current_y - target_y
7747 > min (window_box_height (it->w), line_height * 2 / 3))
7748 && IT_CHARPOS (*it) > BEGV)
7749 {
7750 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7751 target_y - it->current_y));
7752 dy = it->current_y - target_y;
7753 goto move_further_back;
7754 }
7755 else if (target_y >= it->current_y + line_height
7756 && IT_CHARPOS (*it) < ZV)
7757 {
7758 /* Should move forward by at least one line, maybe more.
7759
7760 Note: Calling move_it_by_lines can be expensive on
7761 terminal frames, where compute_motion is used (via
7762 vmotion) to do the job, when there are very long lines
7763 and truncate-lines is nil. That's the reason for
7764 treating terminal frames specially here. */
7765
7766 if (!FRAME_WINDOW_P (it->f))
7767 move_it_vertically (it, target_y - (it->current_y + line_height));
7768 else
7769 {
7770 do
7771 {
7772 move_it_by_lines (it, 1, 1);
7773 }
7774 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7775 }
7776 }
7777 }
7778 }
7779
7780
7781 /* Move IT by a specified amount of pixel lines DY. DY negative means
7782 move backwards. DY = 0 means move to start of screen line. At the
7783 end, IT will be on the start of a screen line. */
7784
7785 void
7786 move_it_vertically (struct it *it, int dy)
7787 {
7788 if (dy <= 0)
7789 move_it_vertically_backward (it, -dy);
7790 else
7791 {
7792 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7793 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7794 MOVE_TO_POS | MOVE_TO_Y);
7795 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7796
7797 /* If buffer ends in ZV without a newline, move to the start of
7798 the line to satisfy the post-condition. */
7799 if (IT_CHARPOS (*it) == ZV
7800 && ZV > BEGV
7801 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7802 move_it_by_lines (it, 0, 0);
7803 }
7804 }
7805
7806
7807 /* Move iterator IT past the end of the text line it is in. */
7808
7809 void
7810 move_it_past_eol (struct it *it)
7811 {
7812 enum move_it_result rc;
7813
7814 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7815 if (rc == MOVE_NEWLINE_OR_CR)
7816 set_iterator_to_next (it, 0);
7817 }
7818
7819
7820 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7821 negative means move up. DVPOS == 0 means move to the start of the
7822 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7823 NEED_Y_P is zero, IT->current_y will be left unchanged.
7824
7825 Further optimization ideas: If we would know that IT->f doesn't use
7826 a face with proportional font, we could be faster for
7827 truncate-lines nil. */
7828
7829 void
7830 move_it_by_lines (struct it *it, int dvpos, int need_y_p)
7831 {
7832
7833 /* The commented-out optimization uses vmotion on terminals. This
7834 gives bad results, because elements like it->what, on which
7835 callers such as pos_visible_p rely, aren't updated. */
7836 /* struct position pos;
7837 if (!FRAME_WINDOW_P (it->f))
7838 {
7839 struct text_pos textpos;
7840
7841 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7842 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7843 reseat (it, textpos, 1);
7844 it->vpos += pos.vpos;
7845 it->current_y += pos.vpos;
7846 }
7847 else */
7848
7849 if (dvpos == 0)
7850 {
7851 /* DVPOS == 0 means move to the start of the screen line. */
7852 move_it_vertically_backward (it, 0);
7853 xassert (it->current_x == 0 && it->hpos == 0);
7854 /* Let next call to line_bottom_y calculate real line height */
7855 last_height = 0;
7856 }
7857 else if (dvpos > 0)
7858 {
7859 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7860 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7861 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7862 }
7863 else
7864 {
7865 struct it it2;
7866 EMACS_INT start_charpos, i;
7867
7868 /* Start at the beginning of the screen line containing IT's
7869 position. This may actually move vertically backwards,
7870 in case of overlays, so adjust dvpos accordingly. */
7871 dvpos += it->vpos;
7872 move_it_vertically_backward (it, 0);
7873 dvpos -= it->vpos;
7874
7875 /* Go back -DVPOS visible lines and reseat the iterator there. */
7876 start_charpos = IT_CHARPOS (*it);
7877 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7878 back_to_previous_visible_line_start (it);
7879 reseat (it, it->current.pos, 1);
7880
7881 /* Move further back if we end up in a string or an image. */
7882 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7883 {
7884 /* First try to move to start of display line. */
7885 dvpos += it->vpos;
7886 move_it_vertically_backward (it, 0);
7887 dvpos -= it->vpos;
7888 if (IT_POS_VALID_AFTER_MOVE_P (it))
7889 break;
7890 /* If start of line is still in string or image,
7891 move further back. */
7892 back_to_previous_visible_line_start (it);
7893 reseat (it, it->current.pos, 1);
7894 dvpos--;
7895 }
7896
7897 it->current_x = it->hpos = 0;
7898
7899 /* Above call may have moved too far if continuation lines
7900 are involved. Scan forward and see if it did. */
7901 it2 = *it;
7902 it2.vpos = it2.current_y = 0;
7903 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7904 it->vpos -= it2.vpos;
7905 it->current_y -= it2.current_y;
7906 it->current_x = it->hpos = 0;
7907
7908 /* If we moved too far back, move IT some lines forward. */
7909 if (it2.vpos > -dvpos)
7910 {
7911 int delta = it2.vpos + dvpos;
7912 it2 = *it;
7913 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7914 /* Move back again if we got too far ahead. */
7915 if (IT_CHARPOS (*it) >= start_charpos)
7916 *it = it2;
7917 }
7918 }
7919 }
7920
7921 /* Return 1 if IT points into the middle of a display vector. */
7922
7923 int
7924 in_display_vector_p (struct it *it)
7925 {
7926 return (it->method == GET_FROM_DISPLAY_VECTOR
7927 && it->current.dpvec_index > 0
7928 && it->dpvec + it->current.dpvec_index != it->dpend);
7929 }
7930
7931 \f
7932 /***********************************************************************
7933 Messages
7934 ***********************************************************************/
7935
7936
7937 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7938 to *Messages*. */
7939
7940 void
7941 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
7942 {
7943 Lisp_Object args[3];
7944 Lisp_Object msg, fmt;
7945 char *buffer;
7946 EMACS_INT len;
7947 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7948 USE_SAFE_ALLOCA;
7949
7950 /* Do nothing if called asynchronously. Inserting text into
7951 a buffer may call after-change-functions and alike and
7952 that would means running Lisp asynchronously. */
7953 if (handling_signal)
7954 return;
7955
7956 fmt = msg = Qnil;
7957 GCPRO4 (fmt, msg, arg1, arg2);
7958
7959 args[0] = fmt = build_string (format);
7960 args[1] = arg1;
7961 args[2] = arg2;
7962 msg = Fformat (3, args);
7963
7964 len = SBYTES (msg) + 1;
7965 SAFE_ALLOCA (buffer, char *, len);
7966 memcpy (buffer, SDATA (msg), len);
7967
7968 message_dolog (buffer, len - 1, 1, 0);
7969 SAFE_FREE ();
7970
7971 UNGCPRO;
7972 }
7973
7974
7975 /* Output a newline in the *Messages* buffer if "needs" one. */
7976
7977 void
7978 message_log_maybe_newline (void)
7979 {
7980 if (message_log_need_newline)
7981 message_dolog ("", 0, 1, 0);
7982 }
7983
7984
7985 /* Add a string M of length NBYTES to the message log, optionally
7986 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7987 nonzero, means interpret the contents of M as multibyte. This
7988 function calls low-level routines in order to bypass text property
7989 hooks, etc. which might not be safe to run.
7990
7991 This may GC (insert may run before/after change hooks),
7992 so the buffer M must NOT point to a Lisp string. */
7993
7994 void
7995 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
7996 {
7997 if (!NILP (Vmemory_full))
7998 return;
7999
8000 if (!NILP (Vmessage_log_max))
8001 {
8002 struct buffer *oldbuf;
8003 Lisp_Object oldpoint, oldbegv, oldzv;
8004 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8005 EMACS_INT point_at_end = 0;
8006 EMACS_INT zv_at_end = 0;
8007 Lisp_Object old_deactivate_mark, tem;
8008 struct gcpro gcpro1;
8009
8010 old_deactivate_mark = Vdeactivate_mark;
8011 oldbuf = current_buffer;
8012 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8013 current_buffer->undo_list = Qt;
8014
8015 oldpoint = message_dolog_marker1;
8016 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8017 oldbegv = message_dolog_marker2;
8018 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8019 oldzv = message_dolog_marker3;
8020 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8021 GCPRO1 (old_deactivate_mark);
8022
8023 if (PT == Z)
8024 point_at_end = 1;
8025 if (ZV == Z)
8026 zv_at_end = 1;
8027
8028 BEGV = BEG;
8029 BEGV_BYTE = BEG_BYTE;
8030 ZV = Z;
8031 ZV_BYTE = Z_BYTE;
8032 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8033
8034 /* Insert the string--maybe converting multibyte to single byte
8035 or vice versa, so that all the text fits the buffer. */
8036 if (multibyte
8037 && NILP (current_buffer->enable_multibyte_characters))
8038 {
8039 EMACS_INT i;
8040 int c, char_bytes;
8041 unsigned char work[1];
8042
8043 /* Convert a multibyte string to single-byte
8044 for the *Message* buffer. */
8045 for (i = 0; i < nbytes; i += char_bytes)
8046 {
8047 c = string_char_and_length (m + i, &char_bytes);
8048 work[0] = (ASCII_CHAR_P (c)
8049 ? c
8050 : multibyte_char_to_unibyte (c, Qnil));
8051 insert_1_both (work, 1, 1, 1, 0, 0);
8052 }
8053 }
8054 else if (! multibyte
8055 && ! NILP (current_buffer->enable_multibyte_characters))
8056 {
8057 EMACS_INT i;
8058 int c, char_bytes;
8059 unsigned char *msg = (unsigned char *) m;
8060 unsigned char str[MAX_MULTIBYTE_LENGTH];
8061 /* Convert a single-byte string to multibyte
8062 for the *Message* buffer. */
8063 for (i = 0; i < nbytes; i++)
8064 {
8065 c = msg[i];
8066 MAKE_CHAR_MULTIBYTE (c);
8067 char_bytes = CHAR_STRING (c, str);
8068 insert_1_both (str, 1, char_bytes, 1, 0, 0);
8069 }
8070 }
8071 else if (nbytes)
8072 insert_1 (m, nbytes, 1, 0, 0);
8073
8074 if (nlflag)
8075 {
8076 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8077 int dup;
8078 insert_1 ("\n", 1, 1, 0, 0);
8079
8080 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8081 this_bol = PT;
8082 this_bol_byte = PT_BYTE;
8083
8084 /* See if this line duplicates the previous one.
8085 If so, combine duplicates. */
8086 if (this_bol > BEG)
8087 {
8088 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8089 prev_bol = PT;
8090 prev_bol_byte = PT_BYTE;
8091
8092 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8093 this_bol, this_bol_byte);
8094 if (dup)
8095 {
8096 del_range_both (prev_bol, prev_bol_byte,
8097 this_bol, this_bol_byte, 0);
8098 if (dup > 1)
8099 {
8100 char dupstr[40];
8101 int duplen;
8102
8103 /* If you change this format, don't forget to also
8104 change message_log_check_duplicate. */
8105 sprintf (dupstr, " [%d times]", dup);
8106 duplen = strlen (dupstr);
8107 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8108 insert_1 (dupstr, duplen, 1, 0, 1);
8109 }
8110 }
8111 }
8112
8113 /* If we have more than the desired maximum number of lines
8114 in the *Messages* buffer now, delete the oldest ones.
8115 This is safe because we don't have undo in this buffer. */
8116
8117 if (NATNUMP (Vmessage_log_max))
8118 {
8119 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8120 -XFASTINT (Vmessage_log_max) - 1, 0);
8121 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8122 }
8123 }
8124 BEGV = XMARKER (oldbegv)->charpos;
8125 BEGV_BYTE = marker_byte_position (oldbegv);
8126
8127 if (zv_at_end)
8128 {
8129 ZV = Z;
8130 ZV_BYTE = Z_BYTE;
8131 }
8132 else
8133 {
8134 ZV = XMARKER (oldzv)->charpos;
8135 ZV_BYTE = marker_byte_position (oldzv);
8136 }
8137
8138 if (point_at_end)
8139 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8140 else
8141 /* We can't do Fgoto_char (oldpoint) because it will run some
8142 Lisp code. */
8143 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8144 XMARKER (oldpoint)->bytepos);
8145
8146 UNGCPRO;
8147 unchain_marker (XMARKER (oldpoint));
8148 unchain_marker (XMARKER (oldbegv));
8149 unchain_marker (XMARKER (oldzv));
8150
8151 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8152 set_buffer_internal (oldbuf);
8153 if (NILP (tem))
8154 windows_or_buffers_changed = old_windows_or_buffers_changed;
8155 message_log_need_newline = !nlflag;
8156 Vdeactivate_mark = old_deactivate_mark;
8157 }
8158 }
8159
8160
8161 /* We are at the end of the buffer after just having inserted a newline.
8162 (Note: We depend on the fact we won't be crossing the gap.)
8163 Check to see if the most recent message looks a lot like the previous one.
8164 Return 0 if different, 1 if the new one should just replace it, or a
8165 value N > 1 if we should also append " [N times]". */
8166
8167 static int
8168 message_log_check_duplicate (EMACS_INT prev_bol, EMACS_INT prev_bol_byte,
8169 EMACS_INT this_bol, EMACS_INT this_bol_byte)
8170 {
8171 EMACS_INT i;
8172 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8173 int seen_dots = 0;
8174 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8175 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8176
8177 for (i = 0; i < len; i++)
8178 {
8179 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8180 seen_dots = 1;
8181 if (p1[i] != p2[i])
8182 return seen_dots;
8183 }
8184 p1 += len;
8185 if (*p1 == '\n')
8186 return 2;
8187 if (*p1++ == ' ' && *p1++ == '[')
8188 {
8189 int n = 0;
8190 while (*p1 >= '0' && *p1 <= '9')
8191 n = n * 10 + *p1++ - '0';
8192 if (strncmp (p1, " times]\n", 8) == 0)
8193 return n+1;
8194 }
8195 return 0;
8196 }
8197 \f
8198
8199 /* Display an echo area message M with a specified length of NBYTES
8200 bytes. The string may include null characters. If M is 0, clear
8201 out any existing message, and let the mini-buffer text show
8202 through.
8203
8204 This may GC, so the buffer M must NOT point to a Lisp string. */
8205
8206 void
8207 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8208 {
8209 /* First flush out any partial line written with print. */
8210 message_log_maybe_newline ();
8211 if (m)
8212 message_dolog (m, nbytes, 1, multibyte);
8213 message2_nolog (m, nbytes, multibyte);
8214 }
8215
8216
8217 /* The non-logging counterpart of message2. */
8218
8219 void
8220 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8221 {
8222 struct frame *sf = SELECTED_FRAME ();
8223 message_enable_multibyte = multibyte;
8224
8225 if (FRAME_INITIAL_P (sf))
8226 {
8227 if (noninteractive_need_newline)
8228 putc ('\n', stderr);
8229 noninteractive_need_newline = 0;
8230 if (m)
8231 fwrite (m, nbytes, 1, stderr);
8232 if (cursor_in_echo_area == 0)
8233 fprintf (stderr, "\n");
8234 fflush (stderr);
8235 }
8236 /* A null message buffer means that the frame hasn't really been
8237 initialized yet. Error messages get reported properly by
8238 cmd_error, so this must be just an informative message; toss it. */
8239 else if (INTERACTIVE
8240 && sf->glyphs_initialized_p
8241 && FRAME_MESSAGE_BUF (sf))
8242 {
8243 Lisp_Object mini_window;
8244 struct frame *f;
8245
8246 /* Get the frame containing the mini-buffer
8247 that the selected frame is using. */
8248 mini_window = FRAME_MINIBUF_WINDOW (sf);
8249 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8250
8251 FRAME_SAMPLE_VISIBILITY (f);
8252 if (FRAME_VISIBLE_P (sf)
8253 && ! FRAME_VISIBLE_P (f))
8254 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8255
8256 if (m)
8257 {
8258 set_message (m, Qnil, nbytes, multibyte);
8259 if (minibuffer_auto_raise)
8260 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8261 }
8262 else
8263 clear_message (1, 1);
8264
8265 do_pending_window_change (0);
8266 echo_area_display (1);
8267 do_pending_window_change (0);
8268 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8269 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8270 }
8271 }
8272
8273
8274 /* Display an echo area message M with a specified length of NBYTES
8275 bytes. The string may include null characters. If M is not a
8276 string, clear out any existing message, and let the mini-buffer
8277 text show through.
8278
8279 This function cancels echoing. */
8280
8281 void
8282 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8283 {
8284 struct gcpro gcpro1;
8285
8286 GCPRO1 (m);
8287 clear_message (1,1);
8288 cancel_echoing ();
8289
8290 /* First flush out any partial line written with print. */
8291 message_log_maybe_newline ();
8292 if (STRINGP (m))
8293 {
8294 char *buffer;
8295 USE_SAFE_ALLOCA;
8296
8297 SAFE_ALLOCA (buffer, char *, nbytes);
8298 memcpy (buffer, SDATA (m), nbytes);
8299 message_dolog (buffer, nbytes, 1, multibyte);
8300 SAFE_FREE ();
8301 }
8302 message3_nolog (m, nbytes, multibyte);
8303
8304 UNGCPRO;
8305 }
8306
8307
8308 /* The non-logging version of message3.
8309 This does not cancel echoing, because it is used for echoing.
8310 Perhaps we need to make a separate function for echoing
8311 and make this cancel echoing. */
8312
8313 void
8314 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8315 {
8316 struct frame *sf = SELECTED_FRAME ();
8317 message_enable_multibyte = multibyte;
8318
8319 if (FRAME_INITIAL_P (sf))
8320 {
8321 if (noninteractive_need_newline)
8322 putc ('\n', stderr);
8323 noninteractive_need_newline = 0;
8324 if (STRINGP (m))
8325 fwrite (SDATA (m), nbytes, 1, stderr);
8326 if (cursor_in_echo_area == 0)
8327 fprintf (stderr, "\n");
8328 fflush (stderr);
8329 }
8330 /* A null message buffer means that the frame hasn't really been
8331 initialized yet. Error messages get reported properly by
8332 cmd_error, so this must be just an informative message; toss it. */
8333 else if (INTERACTIVE
8334 && sf->glyphs_initialized_p
8335 && FRAME_MESSAGE_BUF (sf))
8336 {
8337 Lisp_Object mini_window;
8338 Lisp_Object frame;
8339 struct frame *f;
8340
8341 /* Get the frame containing the mini-buffer
8342 that the selected frame is using. */
8343 mini_window = FRAME_MINIBUF_WINDOW (sf);
8344 frame = XWINDOW (mini_window)->frame;
8345 f = XFRAME (frame);
8346
8347 FRAME_SAMPLE_VISIBILITY (f);
8348 if (FRAME_VISIBLE_P (sf)
8349 && !FRAME_VISIBLE_P (f))
8350 Fmake_frame_visible (frame);
8351
8352 if (STRINGP (m) && SCHARS (m) > 0)
8353 {
8354 set_message (NULL, m, nbytes, multibyte);
8355 if (minibuffer_auto_raise)
8356 Fraise_frame (frame);
8357 /* Assume we are not echoing.
8358 (If we are, echo_now will override this.) */
8359 echo_message_buffer = Qnil;
8360 }
8361 else
8362 clear_message (1, 1);
8363
8364 do_pending_window_change (0);
8365 echo_area_display (1);
8366 do_pending_window_change (0);
8367 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8368 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8369 }
8370 }
8371
8372
8373 /* Display a null-terminated echo area message M. If M is 0, clear
8374 out any existing message, and let the mini-buffer text show through.
8375
8376 The buffer M must continue to exist until after the echo area gets
8377 cleared or some other message gets displayed there. Do not pass
8378 text that is stored in a Lisp string. Do not pass text in a buffer
8379 that was alloca'd. */
8380
8381 void
8382 message1 (const char *m)
8383 {
8384 message2 (m, (m ? strlen (m) : 0), 0);
8385 }
8386
8387
8388 /* The non-logging counterpart of message1. */
8389
8390 void
8391 message1_nolog (const char *m)
8392 {
8393 message2_nolog (m, (m ? strlen (m) : 0), 0);
8394 }
8395
8396 /* Display a message M which contains a single %s
8397 which gets replaced with STRING. */
8398
8399 void
8400 message_with_string (const char *m, Lisp_Object string, int log)
8401 {
8402 CHECK_STRING (string);
8403
8404 if (noninteractive)
8405 {
8406 if (m)
8407 {
8408 if (noninteractive_need_newline)
8409 putc ('\n', stderr);
8410 noninteractive_need_newline = 0;
8411 fprintf (stderr, m, SDATA (string));
8412 if (!cursor_in_echo_area)
8413 fprintf (stderr, "\n");
8414 fflush (stderr);
8415 }
8416 }
8417 else if (INTERACTIVE)
8418 {
8419 /* The frame whose minibuffer we're going to display the message on.
8420 It may be larger than the selected frame, so we need
8421 to use its buffer, not the selected frame's buffer. */
8422 Lisp_Object mini_window;
8423 struct frame *f, *sf = SELECTED_FRAME ();
8424
8425 /* Get the frame containing the minibuffer
8426 that the selected frame is using. */
8427 mini_window = FRAME_MINIBUF_WINDOW (sf);
8428 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8429
8430 /* A null message buffer means that the frame hasn't really been
8431 initialized yet. Error messages get reported properly by
8432 cmd_error, so this must be just an informative message; toss it. */
8433 if (FRAME_MESSAGE_BUF (f))
8434 {
8435 Lisp_Object args[2], message;
8436 struct gcpro gcpro1, gcpro2;
8437
8438 args[0] = build_string (m);
8439 args[1] = message = string;
8440 GCPRO2 (args[0], message);
8441 gcpro1.nvars = 2;
8442
8443 message = Fformat (2, args);
8444
8445 if (log)
8446 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8447 else
8448 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8449
8450 UNGCPRO;
8451
8452 /* Print should start at the beginning of the message
8453 buffer next time. */
8454 message_buf_print = 0;
8455 }
8456 }
8457 }
8458
8459
8460 /* Dump an informative message to the minibuf. If M is 0, clear out
8461 any existing message, and let the mini-buffer text show through. */
8462
8463 static void
8464 vmessage (const char *m, va_list ap)
8465 {
8466 if (noninteractive)
8467 {
8468 if (m)
8469 {
8470 if (noninteractive_need_newline)
8471 putc ('\n', stderr);
8472 noninteractive_need_newline = 0;
8473 vfprintf (stderr, m, ap);
8474 if (cursor_in_echo_area == 0)
8475 fprintf (stderr, "\n");
8476 fflush (stderr);
8477 }
8478 }
8479 else if (INTERACTIVE)
8480 {
8481 /* The frame whose mini-buffer we're going to display the message
8482 on. It may be larger than the selected frame, so we need to
8483 use its buffer, not the selected frame's buffer. */
8484 Lisp_Object mini_window;
8485 struct frame *f, *sf = SELECTED_FRAME ();
8486
8487 /* Get the frame containing the mini-buffer
8488 that the selected frame is using. */
8489 mini_window = FRAME_MINIBUF_WINDOW (sf);
8490 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8491
8492 /* A null message buffer means that the frame hasn't really been
8493 initialized yet. Error messages get reported properly by
8494 cmd_error, so this must be just an informative message; toss
8495 it. */
8496 if (FRAME_MESSAGE_BUF (f))
8497 {
8498 if (m)
8499 {
8500 EMACS_INT len;
8501
8502 len = doprnt (FRAME_MESSAGE_BUF (f),
8503 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8504
8505 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8506 }
8507 else
8508 message1 (0);
8509
8510 /* Print should start at the beginning of the message
8511 buffer next time. */
8512 message_buf_print = 0;
8513 }
8514 }
8515 }
8516
8517 void
8518 message (const char *m, ...)
8519 {
8520 va_list ap;
8521 va_start (ap, m);
8522 vmessage (m, ap);
8523 va_end (ap);
8524 }
8525
8526
8527 /* The non-logging version of message. */
8528
8529 void
8530 message_nolog (const char *m, ...)
8531 {
8532 Lisp_Object old_log_max;
8533 va_list ap;
8534 va_start (ap, m);
8535 old_log_max = Vmessage_log_max;
8536 Vmessage_log_max = Qnil;
8537 vmessage (m, ap);
8538 Vmessage_log_max = old_log_max;
8539 va_end (ap);
8540 }
8541
8542
8543 /* Display the current message in the current mini-buffer. This is
8544 only called from error handlers in process.c, and is not time
8545 critical. */
8546
8547 void
8548 update_echo_area (void)
8549 {
8550 if (!NILP (echo_area_buffer[0]))
8551 {
8552 Lisp_Object string;
8553 string = Fcurrent_message ();
8554 message3 (string, SBYTES (string),
8555 !NILP (current_buffer->enable_multibyte_characters));
8556 }
8557 }
8558
8559
8560 /* Make sure echo area buffers in `echo_buffers' are live.
8561 If they aren't, make new ones. */
8562
8563 static void
8564 ensure_echo_area_buffers (void)
8565 {
8566 int i;
8567
8568 for (i = 0; i < 2; ++i)
8569 if (!BUFFERP (echo_buffer[i])
8570 || NILP (XBUFFER (echo_buffer[i])->name))
8571 {
8572 char name[30];
8573 Lisp_Object old_buffer;
8574 int j;
8575
8576 old_buffer = echo_buffer[i];
8577 sprintf (name, " *Echo Area %d*", i);
8578 echo_buffer[i] = Fget_buffer_create (build_string (name));
8579 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8580 /* to force word wrap in echo area -
8581 it was decided to postpone this*/
8582 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8583
8584 for (j = 0; j < 2; ++j)
8585 if (EQ (old_buffer, echo_area_buffer[j]))
8586 echo_area_buffer[j] = echo_buffer[i];
8587 }
8588 }
8589
8590
8591 /* Call FN with args A1..A4 with either the current or last displayed
8592 echo_area_buffer as current buffer.
8593
8594 WHICH zero means use the current message buffer
8595 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8596 from echo_buffer[] and clear it.
8597
8598 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8599 suitable buffer from echo_buffer[] and clear it.
8600
8601 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8602 that the current message becomes the last displayed one, make
8603 choose a suitable buffer for echo_area_buffer[0], and clear it.
8604
8605 Value is what FN returns. */
8606
8607 static int
8608 with_echo_area_buffer (struct window *w, int which,
8609 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
8610 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8611 {
8612 Lisp_Object buffer;
8613 int this_one, the_other, clear_buffer_p, rc;
8614 int count = SPECPDL_INDEX ();
8615
8616 /* If buffers aren't live, make new ones. */
8617 ensure_echo_area_buffers ();
8618
8619 clear_buffer_p = 0;
8620
8621 if (which == 0)
8622 this_one = 0, the_other = 1;
8623 else if (which > 0)
8624 this_one = 1, the_other = 0;
8625 else
8626 {
8627 this_one = 0, the_other = 1;
8628 clear_buffer_p = 1;
8629
8630 /* We need a fresh one in case the current echo buffer equals
8631 the one containing the last displayed echo area message. */
8632 if (!NILP (echo_area_buffer[this_one])
8633 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8634 echo_area_buffer[this_one] = Qnil;
8635 }
8636
8637 /* Choose a suitable buffer from echo_buffer[] is we don't
8638 have one. */
8639 if (NILP (echo_area_buffer[this_one]))
8640 {
8641 echo_area_buffer[this_one]
8642 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8643 ? echo_buffer[the_other]
8644 : echo_buffer[this_one]);
8645 clear_buffer_p = 1;
8646 }
8647
8648 buffer = echo_area_buffer[this_one];
8649
8650 /* Don't get confused by reusing the buffer used for echoing
8651 for a different purpose. */
8652 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8653 cancel_echoing ();
8654
8655 record_unwind_protect (unwind_with_echo_area_buffer,
8656 with_echo_area_buffer_unwind_data (w));
8657
8658 /* Make the echo area buffer current. Note that for display
8659 purposes, it is not necessary that the displayed window's buffer
8660 == current_buffer, except for text property lookup. So, let's
8661 only set that buffer temporarily here without doing a full
8662 Fset_window_buffer. We must also change w->pointm, though,
8663 because otherwise an assertions in unshow_buffer fails, and Emacs
8664 aborts. */
8665 set_buffer_internal_1 (XBUFFER (buffer));
8666 if (w)
8667 {
8668 w->buffer = buffer;
8669 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8670 }
8671
8672 current_buffer->undo_list = Qt;
8673 current_buffer->read_only = Qnil;
8674 specbind (Qinhibit_read_only, Qt);
8675 specbind (Qinhibit_modification_hooks, Qt);
8676
8677 if (clear_buffer_p && Z > BEG)
8678 del_range (BEG, Z);
8679
8680 xassert (BEGV >= BEG);
8681 xassert (ZV <= Z && ZV >= BEGV);
8682
8683 rc = fn (a1, a2, a3, a4);
8684
8685 xassert (BEGV >= BEG);
8686 xassert (ZV <= Z && ZV >= BEGV);
8687
8688 unbind_to (count, Qnil);
8689 return rc;
8690 }
8691
8692
8693 /* Save state that should be preserved around the call to the function
8694 FN called in with_echo_area_buffer. */
8695
8696 static Lisp_Object
8697 with_echo_area_buffer_unwind_data (struct window *w)
8698 {
8699 int i = 0;
8700 Lisp_Object vector, tmp;
8701
8702 /* Reduce consing by keeping one vector in
8703 Vwith_echo_area_save_vector. */
8704 vector = Vwith_echo_area_save_vector;
8705 Vwith_echo_area_save_vector = Qnil;
8706
8707 if (NILP (vector))
8708 vector = Fmake_vector (make_number (7), Qnil);
8709
8710 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8711 ASET (vector, i, Vdeactivate_mark); ++i;
8712 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8713
8714 if (w)
8715 {
8716 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8717 ASET (vector, i, w->buffer); ++i;
8718 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8719 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8720 }
8721 else
8722 {
8723 int end = i + 4;
8724 for (; i < end; ++i)
8725 ASET (vector, i, Qnil);
8726 }
8727
8728 xassert (i == ASIZE (vector));
8729 return vector;
8730 }
8731
8732
8733 /* Restore global state from VECTOR which was created by
8734 with_echo_area_buffer_unwind_data. */
8735
8736 static Lisp_Object
8737 unwind_with_echo_area_buffer (Lisp_Object vector)
8738 {
8739 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8740 Vdeactivate_mark = AREF (vector, 1);
8741 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8742
8743 if (WINDOWP (AREF (vector, 3)))
8744 {
8745 struct window *w;
8746 Lisp_Object buffer, charpos, bytepos;
8747
8748 w = XWINDOW (AREF (vector, 3));
8749 buffer = AREF (vector, 4);
8750 charpos = AREF (vector, 5);
8751 bytepos = AREF (vector, 6);
8752
8753 w->buffer = buffer;
8754 set_marker_both (w->pointm, buffer,
8755 XFASTINT (charpos), XFASTINT (bytepos));
8756 }
8757
8758 Vwith_echo_area_save_vector = vector;
8759 return Qnil;
8760 }
8761
8762
8763 /* Set up the echo area for use by print functions. MULTIBYTE_P
8764 non-zero means we will print multibyte. */
8765
8766 void
8767 setup_echo_area_for_printing (int multibyte_p)
8768 {
8769 /* If we can't find an echo area any more, exit. */
8770 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8771 Fkill_emacs (Qnil);
8772
8773 ensure_echo_area_buffers ();
8774
8775 if (!message_buf_print)
8776 {
8777 /* A message has been output since the last time we printed.
8778 Choose a fresh echo area buffer. */
8779 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8780 echo_area_buffer[0] = echo_buffer[1];
8781 else
8782 echo_area_buffer[0] = echo_buffer[0];
8783
8784 /* Switch to that buffer and clear it. */
8785 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8786 current_buffer->truncate_lines = Qnil;
8787
8788 if (Z > BEG)
8789 {
8790 int count = SPECPDL_INDEX ();
8791 specbind (Qinhibit_read_only, Qt);
8792 /* Note that undo recording is always disabled. */
8793 del_range (BEG, Z);
8794 unbind_to (count, Qnil);
8795 }
8796 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8797
8798 /* Set up the buffer for the multibyteness we need. */
8799 if (multibyte_p
8800 != !NILP (current_buffer->enable_multibyte_characters))
8801 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8802
8803 /* Raise the frame containing the echo area. */
8804 if (minibuffer_auto_raise)
8805 {
8806 struct frame *sf = SELECTED_FRAME ();
8807 Lisp_Object mini_window;
8808 mini_window = FRAME_MINIBUF_WINDOW (sf);
8809 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8810 }
8811
8812 message_log_maybe_newline ();
8813 message_buf_print = 1;
8814 }
8815 else
8816 {
8817 if (NILP (echo_area_buffer[0]))
8818 {
8819 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8820 echo_area_buffer[0] = echo_buffer[1];
8821 else
8822 echo_area_buffer[0] = echo_buffer[0];
8823 }
8824
8825 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8826 {
8827 /* Someone switched buffers between print requests. */
8828 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8829 current_buffer->truncate_lines = Qnil;
8830 }
8831 }
8832 }
8833
8834
8835 /* Display an echo area message in window W. Value is non-zero if W's
8836 height is changed. If display_last_displayed_message_p is
8837 non-zero, display the message that was last displayed, otherwise
8838 display the current message. */
8839
8840 static int
8841 display_echo_area (struct window *w)
8842 {
8843 int i, no_message_p, window_height_changed_p, count;
8844
8845 /* Temporarily disable garbage collections while displaying the echo
8846 area. This is done because a GC can print a message itself.
8847 That message would modify the echo area buffer's contents while a
8848 redisplay of the buffer is going on, and seriously confuse
8849 redisplay. */
8850 count = inhibit_garbage_collection ();
8851
8852 /* If there is no message, we must call display_echo_area_1
8853 nevertheless because it resizes the window. But we will have to
8854 reset the echo_area_buffer in question to nil at the end because
8855 with_echo_area_buffer will sets it to an empty buffer. */
8856 i = display_last_displayed_message_p ? 1 : 0;
8857 no_message_p = NILP (echo_area_buffer[i]);
8858
8859 window_height_changed_p
8860 = with_echo_area_buffer (w, display_last_displayed_message_p,
8861 display_echo_area_1,
8862 (EMACS_INT) w, Qnil, 0, 0);
8863
8864 if (no_message_p)
8865 echo_area_buffer[i] = Qnil;
8866
8867 unbind_to (count, Qnil);
8868 return window_height_changed_p;
8869 }
8870
8871
8872 /* Helper for display_echo_area. Display the current buffer which
8873 contains the current echo area message in window W, a mini-window,
8874 a pointer to which is passed in A1. A2..A4 are currently not used.
8875 Change the height of W so that all of the message is displayed.
8876 Value is non-zero if height of W was changed. */
8877
8878 static int
8879 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8880 {
8881 struct window *w = (struct window *) a1;
8882 Lisp_Object window;
8883 struct text_pos start;
8884 int window_height_changed_p = 0;
8885
8886 /* Do this before displaying, so that we have a large enough glyph
8887 matrix for the display. If we can't get enough space for the
8888 whole text, display the last N lines. That works by setting w->start. */
8889 window_height_changed_p = resize_mini_window (w, 0);
8890
8891 /* Use the starting position chosen by resize_mini_window. */
8892 SET_TEXT_POS_FROM_MARKER (start, w->start);
8893
8894 /* Display. */
8895 clear_glyph_matrix (w->desired_matrix);
8896 XSETWINDOW (window, w);
8897 try_window (window, start, 0);
8898
8899 return window_height_changed_p;
8900 }
8901
8902
8903 /* Resize the echo area window to exactly the size needed for the
8904 currently displayed message, if there is one. If a mini-buffer
8905 is active, don't shrink it. */
8906
8907 void
8908 resize_echo_area_exactly (void)
8909 {
8910 if (BUFFERP (echo_area_buffer[0])
8911 && WINDOWP (echo_area_window))
8912 {
8913 struct window *w = XWINDOW (echo_area_window);
8914 int resized_p;
8915 Lisp_Object resize_exactly;
8916
8917 if (minibuf_level == 0)
8918 resize_exactly = Qt;
8919 else
8920 resize_exactly = Qnil;
8921
8922 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8923 (EMACS_INT) w, resize_exactly, 0, 0);
8924 if (resized_p)
8925 {
8926 ++windows_or_buffers_changed;
8927 ++update_mode_lines;
8928 redisplay_internal (0);
8929 }
8930 }
8931 }
8932
8933
8934 /* Callback function for with_echo_area_buffer, when used from
8935 resize_echo_area_exactly. A1 contains a pointer to the window to
8936 resize, EXACTLY non-nil means resize the mini-window exactly to the
8937 size of the text displayed. A3 and A4 are not used. Value is what
8938 resize_mini_window returns. */
8939
8940 static int
8941 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
8942 {
8943 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8944 }
8945
8946
8947 /* Resize mini-window W to fit the size of its contents. EXACT_P
8948 means size the window exactly to the size needed. Otherwise, it's
8949 only enlarged until W's buffer is empty.
8950
8951 Set W->start to the right place to begin display. If the whole
8952 contents fit, start at the beginning. Otherwise, start so as
8953 to make the end of the contents appear. This is particularly
8954 important for y-or-n-p, but seems desirable generally.
8955
8956 Value is non-zero if the window height has been changed. */
8957
8958 int
8959 resize_mini_window (struct window *w, int exact_p)
8960 {
8961 struct frame *f = XFRAME (w->frame);
8962 int window_height_changed_p = 0;
8963
8964 xassert (MINI_WINDOW_P (w));
8965
8966 /* By default, start display at the beginning. */
8967 set_marker_both (w->start, w->buffer,
8968 BUF_BEGV (XBUFFER (w->buffer)),
8969 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8970
8971 /* Don't resize windows while redisplaying a window; it would
8972 confuse redisplay functions when the size of the window they are
8973 displaying changes from under them. Such a resizing can happen,
8974 for instance, when which-func prints a long message while
8975 we are running fontification-functions. We're running these
8976 functions with safe_call which binds inhibit-redisplay to t. */
8977 if (!NILP (Vinhibit_redisplay))
8978 return 0;
8979
8980 /* Nil means don't try to resize. */
8981 if (NILP (Vresize_mini_windows)
8982 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8983 return 0;
8984
8985 if (!FRAME_MINIBUF_ONLY_P (f))
8986 {
8987 struct it it;
8988 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8989 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8990 int height, max_height;
8991 int unit = FRAME_LINE_HEIGHT (f);
8992 struct text_pos start;
8993 struct buffer *old_current_buffer = NULL;
8994
8995 if (current_buffer != XBUFFER (w->buffer))
8996 {
8997 old_current_buffer = current_buffer;
8998 set_buffer_internal (XBUFFER (w->buffer));
8999 }
9000
9001 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9002
9003 /* Compute the max. number of lines specified by the user. */
9004 if (FLOATP (Vmax_mini_window_height))
9005 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9006 else if (INTEGERP (Vmax_mini_window_height))
9007 max_height = XINT (Vmax_mini_window_height);
9008 else
9009 max_height = total_height / 4;
9010
9011 /* Correct that max. height if it's bogus. */
9012 max_height = max (1, max_height);
9013 max_height = min (total_height, max_height);
9014
9015 /* Find out the height of the text in the window. */
9016 if (it.line_wrap == TRUNCATE)
9017 height = 1;
9018 else
9019 {
9020 last_height = 0;
9021 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9022 if (it.max_ascent == 0 && it.max_descent == 0)
9023 height = it.current_y + last_height;
9024 else
9025 height = it.current_y + it.max_ascent + it.max_descent;
9026 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9027 height = (height + unit - 1) / unit;
9028 }
9029
9030 /* Compute a suitable window start. */
9031 if (height > max_height)
9032 {
9033 height = max_height;
9034 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9035 move_it_vertically_backward (&it, (height - 1) * unit);
9036 start = it.current.pos;
9037 }
9038 else
9039 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9040 SET_MARKER_FROM_TEXT_POS (w->start, start);
9041
9042 if (EQ (Vresize_mini_windows, Qgrow_only))
9043 {
9044 /* Let it grow only, until we display an empty message, in which
9045 case the window shrinks again. */
9046 if (height > WINDOW_TOTAL_LINES (w))
9047 {
9048 int old_height = WINDOW_TOTAL_LINES (w);
9049 freeze_window_starts (f, 1);
9050 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9051 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9052 }
9053 else if (height < WINDOW_TOTAL_LINES (w)
9054 && (exact_p || BEGV == ZV))
9055 {
9056 int old_height = WINDOW_TOTAL_LINES (w);
9057 freeze_window_starts (f, 0);
9058 shrink_mini_window (w);
9059 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9060 }
9061 }
9062 else
9063 {
9064 /* Always resize to exact size needed. */
9065 if (height > WINDOW_TOTAL_LINES (w))
9066 {
9067 int old_height = WINDOW_TOTAL_LINES (w);
9068 freeze_window_starts (f, 1);
9069 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9070 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9071 }
9072 else if (height < WINDOW_TOTAL_LINES (w))
9073 {
9074 int old_height = WINDOW_TOTAL_LINES (w);
9075 freeze_window_starts (f, 0);
9076 shrink_mini_window (w);
9077
9078 if (height)
9079 {
9080 freeze_window_starts (f, 1);
9081 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9082 }
9083
9084 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9085 }
9086 }
9087
9088 if (old_current_buffer)
9089 set_buffer_internal (old_current_buffer);
9090 }
9091
9092 return window_height_changed_p;
9093 }
9094
9095
9096 /* Value is the current message, a string, or nil if there is no
9097 current message. */
9098
9099 Lisp_Object
9100 current_message (void)
9101 {
9102 Lisp_Object msg;
9103
9104 if (!BUFFERP (echo_area_buffer[0]))
9105 msg = Qnil;
9106 else
9107 {
9108 with_echo_area_buffer (0, 0, current_message_1,
9109 (EMACS_INT) &msg, Qnil, 0, 0);
9110 if (NILP (msg))
9111 echo_area_buffer[0] = Qnil;
9112 }
9113
9114 return msg;
9115 }
9116
9117
9118 static int
9119 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9120 {
9121 Lisp_Object *msg = (Lisp_Object *) a1;
9122
9123 if (Z > BEG)
9124 *msg = make_buffer_string (BEG, Z, 1);
9125 else
9126 *msg = Qnil;
9127 return 0;
9128 }
9129
9130
9131 /* Push the current message on Vmessage_stack for later restauration
9132 by restore_message. Value is non-zero if the current message isn't
9133 empty. This is a relatively infrequent operation, so it's not
9134 worth optimizing. */
9135
9136 int
9137 push_message (void)
9138 {
9139 Lisp_Object msg;
9140 msg = current_message ();
9141 Vmessage_stack = Fcons (msg, Vmessage_stack);
9142 return STRINGP (msg);
9143 }
9144
9145
9146 /* Restore message display from the top of Vmessage_stack. */
9147
9148 void
9149 restore_message (void)
9150 {
9151 Lisp_Object msg;
9152
9153 xassert (CONSP (Vmessage_stack));
9154 msg = XCAR (Vmessage_stack);
9155 if (STRINGP (msg))
9156 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9157 else
9158 message3_nolog (msg, 0, 0);
9159 }
9160
9161
9162 /* Handler for record_unwind_protect calling pop_message. */
9163
9164 Lisp_Object
9165 pop_message_unwind (Lisp_Object dummy)
9166 {
9167 pop_message ();
9168 return Qnil;
9169 }
9170
9171 /* Pop the top-most entry off Vmessage_stack. */
9172
9173 void
9174 pop_message (void)
9175 {
9176 xassert (CONSP (Vmessage_stack));
9177 Vmessage_stack = XCDR (Vmessage_stack);
9178 }
9179
9180
9181 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9182 exits. If the stack is not empty, we have a missing pop_message
9183 somewhere. */
9184
9185 void
9186 check_message_stack (void)
9187 {
9188 if (!NILP (Vmessage_stack))
9189 abort ();
9190 }
9191
9192
9193 /* Truncate to NCHARS what will be displayed in the echo area the next
9194 time we display it---but don't redisplay it now. */
9195
9196 void
9197 truncate_echo_area (EMACS_INT nchars)
9198 {
9199 if (nchars == 0)
9200 echo_area_buffer[0] = Qnil;
9201 /* A null message buffer means that the frame hasn't really been
9202 initialized yet. Error messages get reported properly by
9203 cmd_error, so this must be just an informative message; toss it. */
9204 else if (!noninteractive
9205 && INTERACTIVE
9206 && !NILP (echo_area_buffer[0]))
9207 {
9208 struct frame *sf = SELECTED_FRAME ();
9209 if (FRAME_MESSAGE_BUF (sf))
9210 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9211 }
9212 }
9213
9214
9215 /* Helper function for truncate_echo_area. Truncate the current
9216 message to at most NCHARS characters. */
9217
9218 static int
9219 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9220 {
9221 if (BEG + nchars < Z)
9222 del_range (BEG + nchars, Z);
9223 if (Z == BEG)
9224 echo_area_buffer[0] = Qnil;
9225 return 0;
9226 }
9227
9228
9229 /* Set the current message to a substring of S or STRING.
9230
9231 If STRING is a Lisp string, set the message to the first NBYTES
9232 bytes from STRING. NBYTES zero means use the whole string. If
9233 STRING is multibyte, the message will be displayed multibyte.
9234
9235 If S is not null, set the message to the first LEN bytes of S. LEN
9236 zero means use the whole string. MULTIBYTE_P non-zero means S is
9237 multibyte. Display the message multibyte in that case.
9238
9239 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9240 to t before calling set_message_1 (which calls insert).
9241 */
9242
9243 void
9244 set_message (const char *s, Lisp_Object string,
9245 EMACS_INT nbytes, int multibyte_p)
9246 {
9247 message_enable_multibyte
9248 = ((s && multibyte_p)
9249 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9250
9251 with_echo_area_buffer (0, -1, set_message_1,
9252 (EMACS_INT) s, string, nbytes, multibyte_p);
9253 message_buf_print = 0;
9254 help_echo_showing_p = 0;
9255 }
9256
9257
9258 /* Helper function for set_message. Arguments have the same meaning
9259 as there, with A1 corresponding to S and A2 corresponding to STRING
9260 This function is called with the echo area buffer being
9261 current. */
9262
9263 static int
9264 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9265 {
9266 const char *s = (const char *) a1;
9267 Lisp_Object string = a2;
9268
9269 /* Change multibyteness of the echo buffer appropriately. */
9270 if (message_enable_multibyte
9271 != !NILP (current_buffer->enable_multibyte_characters))
9272 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9273
9274 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9275
9276 /* Insert new message at BEG. */
9277 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9278
9279 if (STRINGP (string))
9280 {
9281 EMACS_INT nchars;
9282
9283 if (nbytes == 0)
9284 nbytes = SBYTES (string);
9285 nchars = string_byte_to_char (string, nbytes);
9286
9287 /* This function takes care of single/multibyte conversion. We
9288 just have to ensure that the echo area buffer has the right
9289 setting of enable_multibyte_characters. */
9290 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9291 }
9292 else if (s)
9293 {
9294 if (nbytes == 0)
9295 nbytes = strlen (s);
9296
9297 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9298 {
9299 /* Convert from multi-byte to single-byte. */
9300 EMACS_INT i;
9301 int c, n;
9302 unsigned char work[1];
9303
9304 /* Convert a multibyte string to single-byte. */
9305 for (i = 0; i < nbytes; i += n)
9306 {
9307 c = string_char_and_length (s + i, &n);
9308 work[0] = (ASCII_CHAR_P (c)
9309 ? c
9310 : multibyte_char_to_unibyte (c, Qnil));
9311 insert_1_both (work, 1, 1, 1, 0, 0);
9312 }
9313 }
9314 else if (!multibyte_p
9315 && !NILP (current_buffer->enable_multibyte_characters))
9316 {
9317 /* Convert from single-byte to multi-byte. */
9318 EMACS_INT i;
9319 int c, n;
9320 const unsigned char *msg = (const unsigned char *) s;
9321 unsigned char str[MAX_MULTIBYTE_LENGTH];
9322
9323 /* Convert a single-byte string to multibyte. */
9324 for (i = 0; i < nbytes; i++)
9325 {
9326 c = msg[i];
9327 MAKE_CHAR_MULTIBYTE (c);
9328 n = CHAR_STRING (c, str);
9329 insert_1_both (str, 1, n, 1, 0, 0);
9330 }
9331 }
9332 else
9333 insert_1 (s, nbytes, 1, 0, 0);
9334 }
9335
9336 return 0;
9337 }
9338
9339
9340 /* Clear messages. CURRENT_P non-zero means clear the current
9341 message. LAST_DISPLAYED_P non-zero means clear the message
9342 last displayed. */
9343
9344 void
9345 clear_message (int current_p, int last_displayed_p)
9346 {
9347 if (current_p)
9348 {
9349 echo_area_buffer[0] = Qnil;
9350 message_cleared_p = 1;
9351 }
9352
9353 if (last_displayed_p)
9354 echo_area_buffer[1] = Qnil;
9355
9356 message_buf_print = 0;
9357 }
9358
9359 /* Clear garbaged frames.
9360
9361 This function is used where the old redisplay called
9362 redraw_garbaged_frames which in turn called redraw_frame which in
9363 turn called clear_frame. The call to clear_frame was a source of
9364 flickering. I believe a clear_frame is not necessary. It should
9365 suffice in the new redisplay to invalidate all current matrices,
9366 and ensure a complete redisplay of all windows. */
9367
9368 static void
9369 clear_garbaged_frames (void)
9370 {
9371 if (frame_garbaged)
9372 {
9373 Lisp_Object tail, frame;
9374 int changed_count = 0;
9375
9376 FOR_EACH_FRAME (tail, frame)
9377 {
9378 struct frame *f = XFRAME (frame);
9379
9380 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9381 {
9382 if (f->resized_p)
9383 {
9384 Fredraw_frame (frame);
9385 f->force_flush_display_p = 1;
9386 }
9387 clear_current_matrices (f);
9388 changed_count++;
9389 f->garbaged = 0;
9390 f->resized_p = 0;
9391 }
9392 }
9393
9394 frame_garbaged = 0;
9395 if (changed_count)
9396 ++windows_or_buffers_changed;
9397 }
9398 }
9399
9400
9401 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9402 is non-zero update selected_frame. Value is non-zero if the
9403 mini-windows height has been changed. */
9404
9405 static int
9406 echo_area_display (int update_frame_p)
9407 {
9408 Lisp_Object mini_window;
9409 struct window *w;
9410 struct frame *f;
9411 int window_height_changed_p = 0;
9412 struct frame *sf = SELECTED_FRAME ();
9413
9414 mini_window = FRAME_MINIBUF_WINDOW (sf);
9415 w = XWINDOW (mini_window);
9416 f = XFRAME (WINDOW_FRAME (w));
9417
9418 /* Don't display if frame is invisible or not yet initialized. */
9419 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9420 return 0;
9421
9422 #ifdef HAVE_WINDOW_SYSTEM
9423 /* When Emacs starts, selected_frame may be the initial terminal
9424 frame. If we let this through, a message would be displayed on
9425 the terminal. */
9426 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9427 return 0;
9428 #endif /* HAVE_WINDOW_SYSTEM */
9429
9430 /* Redraw garbaged frames. */
9431 if (frame_garbaged)
9432 clear_garbaged_frames ();
9433
9434 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9435 {
9436 echo_area_window = mini_window;
9437 window_height_changed_p = display_echo_area (w);
9438 w->must_be_updated_p = 1;
9439
9440 /* Update the display, unless called from redisplay_internal.
9441 Also don't update the screen during redisplay itself. The
9442 update will happen at the end of redisplay, and an update
9443 here could cause confusion. */
9444 if (update_frame_p && !redisplaying_p)
9445 {
9446 int n = 0;
9447
9448 /* If the display update has been interrupted by pending
9449 input, update mode lines in the frame. Due to the
9450 pending input, it might have been that redisplay hasn't
9451 been called, so that mode lines above the echo area are
9452 garbaged. This looks odd, so we prevent it here. */
9453 if (!display_completed)
9454 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9455
9456 if (window_height_changed_p
9457 /* Don't do this if Emacs is shutting down. Redisplay
9458 needs to run hooks. */
9459 && !NILP (Vrun_hooks))
9460 {
9461 /* Must update other windows. Likewise as in other
9462 cases, don't let this update be interrupted by
9463 pending input. */
9464 int count = SPECPDL_INDEX ();
9465 specbind (Qredisplay_dont_pause, Qt);
9466 windows_or_buffers_changed = 1;
9467 redisplay_internal (0);
9468 unbind_to (count, Qnil);
9469 }
9470 else if (FRAME_WINDOW_P (f) && n == 0)
9471 {
9472 /* Window configuration is the same as before.
9473 Can do with a display update of the echo area,
9474 unless we displayed some mode lines. */
9475 update_single_window (w, 1);
9476 FRAME_RIF (f)->flush_display (f);
9477 }
9478 else
9479 update_frame (f, 1, 1);
9480
9481 /* If cursor is in the echo area, make sure that the next
9482 redisplay displays the minibuffer, so that the cursor will
9483 be replaced with what the minibuffer wants. */
9484 if (cursor_in_echo_area)
9485 ++windows_or_buffers_changed;
9486 }
9487 }
9488 else if (!EQ (mini_window, selected_window))
9489 windows_or_buffers_changed++;
9490
9491 /* Last displayed message is now the current message. */
9492 echo_area_buffer[1] = echo_area_buffer[0];
9493 /* Inform read_char that we're not echoing. */
9494 echo_message_buffer = Qnil;
9495
9496 /* Prevent redisplay optimization in redisplay_internal by resetting
9497 this_line_start_pos. This is done because the mini-buffer now
9498 displays the message instead of its buffer text. */
9499 if (EQ (mini_window, selected_window))
9500 CHARPOS (this_line_start_pos) = 0;
9501
9502 return window_height_changed_p;
9503 }
9504
9505
9506 \f
9507 /***********************************************************************
9508 Mode Lines and Frame Titles
9509 ***********************************************************************/
9510
9511 /* A buffer for constructing non-propertized mode-line strings and
9512 frame titles in it; allocated from the heap in init_xdisp and
9513 resized as needed in store_mode_line_noprop_char. */
9514
9515 static char *mode_line_noprop_buf;
9516
9517 /* The buffer's end, and a current output position in it. */
9518
9519 static char *mode_line_noprop_buf_end;
9520 static char *mode_line_noprop_ptr;
9521
9522 #define MODE_LINE_NOPROP_LEN(start) \
9523 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9524
9525 static enum {
9526 MODE_LINE_DISPLAY = 0,
9527 MODE_LINE_TITLE,
9528 MODE_LINE_NOPROP,
9529 MODE_LINE_STRING
9530 } mode_line_target;
9531
9532 /* Alist that caches the results of :propertize.
9533 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9534 static Lisp_Object mode_line_proptrans_alist;
9535
9536 /* List of strings making up the mode-line. */
9537 static Lisp_Object mode_line_string_list;
9538
9539 /* Base face property when building propertized mode line string. */
9540 static Lisp_Object mode_line_string_face;
9541 static Lisp_Object mode_line_string_face_prop;
9542
9543
9544 /* Unwind data for mode line strings */
9545
9546 static Lisp_Object Vmode_line_unwind_vector;
9547
9548 static Lisp_Object
9549 format_mode_line_unwind_data (struct buffer *obuf,
9550 Lisp_Object owin,
9551 int save_proptrans)
9552 {
9553 Lisp_Object vector, tmp;
9554
9555 /* Reduce consing by keeping one vector in
9556 Vwith_echo_area_save_vector. */
9557 vector = Vmode_line_unwind_vector;
9558 Vmode_line_unwind_vector = Qnil;
9559
9560 if (NILP (vector))
9561 vector = Fmake_vector (make_number (8), Qnil);
9562
9563 ASET (vector, 0, make_number (mode_line_target));
9564 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9565 ASET (vector, 2, mode_line_string_list);
9566 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9567 ASET (vector, 4, mode_line_string_face);
9568 ASET (vector, 5, mode_line_string_face_prop);
9569
9570 if (obuf)
9571 XSETBUFFER (tmp, obuf);
9572 else
9573 tmp = Qnil;
9574 ASET (vector, 6, tmp);
9575 ASET (vector, 7, owin);
9576
9577 return vector;
9578 }
9579
9580 static Lisp_Object
9581 unwind_format_mode_line (Lisp_Object vector)
9582 {
9583 mode_line_target = XINT (AREF (vector, 0));
9584 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9585 mode_line_string_list = AREF (vector, 2);
9586 if (! EQ (AREF (vector, 3), Qt))
9587 mode_line_proptrans_alist = AREF (vector, 3);
9588 mode_line_string_face = AREF (vector, 4);
9589 mode_line_string_face_prop = AREF (vector, 5);
9590
9591 if (!NILP (AREF (vector, 7)))
9592 /* Select window before buffer, since it may change the buffer. */
9593 Fselect_window (AREF (vector, 7), Qt);
9594
9595 if (!NILP (AREF (vector, 6)))
9596 {
9597 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9598 ASET (vector, 6, Qnil);
9599 }
9600
9601 Vmode_line_unwind_vector = vector;
9602 return Qnil;
9603 }
9604
9605
9606 /* Store a single character C for the frame title in mode_line_noprop_buf.
9607 Re-allocate mode_line_noprop_buf if necessary. */
9608
9609 static void
9610 store_mode_line_noprop_char (char c)
9611 {
9612 /* If output position has reached the end of the allocated buffer,
9613 double the buffer's size. */
9614 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9615 {
9616 int len = MODE_LINE_NOPROP_LEN (0);
9617 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9618 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9619 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9620 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9621 }
9622
9623 *mode_line_noprop_ptr++ = c;
9624 }
9625
9626
9627 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9628 mode_line_noprop_ptr. STR is the string to store. Do not copy
9629 characters that yield more columns than PRECISION; PRECISION <= 0
9630 means copy the whole string. Pad with spaces until FIELD_WIDTH
9631 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9632 pad. Called from display_mode_element when it is used to build a
9633 frame title. */
9634
9635 static int
9636 store_mode_line_noprop (const unsigned char *str, int field_width, int precision)
9637 {
9638 int n = 0;
9639 EMACS_INT dummy, nbytes;
9640
9641 /* Copy at most PRECISION chars from STR. */
9642 nbytes = strlen (str);
9643 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9644 while (nbytes--)
9645 store_mode_line_noprop_char (*str++);
9646
9647 /* Fill up with spaces until FIELD_WIDTH reached. */
9648 while (field_width > 0
9649 && n < field_width)
9650 {
9651 store_mode_line_noprop_char (' ');
9652 ++n;
9653 }
9654
9655 return n;
9656 }
9657
9658 /***********************************************************************
9659 Frame Titles
9660 ***********************************************************************/
9661
9662 #ifdef HAVE_WINDOW_SYSTEM
9663
9664 /* Set the title of FRAME, if it has changed. The title format is
9665 Vicon_title_format if FRAME is iconified, otherwise it is
9666 frame_title_format. */
9667
9668 static void
9669 x_consider_frame_title (Lisp_Object frame)
9670 {
9671 struct frame *f = XFRAME (frame);
9672
9673 if (FRAME_WINDOW_P (f)
9674 || FRAME_MINIBUF_ONLY_P (f)
9675 || f->explicit_name)
9676 {
9677 /* Do we have more than one visible frame on this X display? */
9678 Lisp_Object tail;
9679 Lisp_Object fmt;
9680 int title_start;
9681 char *title;
9682 int len;
9683 struct it it;
9684 int count = SPECPDL_INDEX ();
9685
9686 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9687 {
9688 Lisp_Object other_frame = XCAR (tail);
9689 struct frame *tf = XFRAME (other_frame);
9690
9691 if (tf != f
9692 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9693 && !FRAME_MINIBUF_ONLY_P (tf)
9694 && !EQ (other_frame, tip_frame)
9695 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9696 break;
9697 }
9698
9699 /* Set global variable indicating that multiple frames exist. */
9700 multiple_frames = CONSP (tail);
9701
9702 /* Switch to the buffer of selected window of the frame. Set up
9703 mode_line_target so that display_mode_element will output into
9704 mode_line_noprop_buf; then display the title. */
9705 record_unwind_protect (unwind_format_mode_line,
9706 format_mode_line_unwind_data
9707 (current_buffer, selected_window, 0));
9708
9709 Fselect_window (f->selected_window, Qt);
9710 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9711 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9712
9713 mode_line_target = MODE_LINE_TITLE;
9714 title_start = MODE_LINE_NOPROP_LEN (0);
9715 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9716 NULL, DEFAULT_FACE_ID);
9717 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9718 len = MODE_LINE_NOPROP_LEN (title_start);
9719 title = mode_line_noprop_buf + title_start;
9720 unbind_to (count, Qnil);
9721
9722 /* Set the title only if it's changed. This avoids consing in
9723 the common case where it hasn't. (If it turns out that we've
9724 already wasted too much time by walking through the list with
9725 display_mode_element, then we might need to optimize at a
9726 higher level than this.) */
9727 if (! STRINGP (f->name)
9728 || SBYTES (f->name) != len
9729 || memcmp (title, SDATA (f->name), len) != 0)
9730 x_implicitly_set_name (f, make_string (title, len), Qnil);
9731 }
9732 }
9733
9734 #endif /* not HAVE_WINDOW_SYSTEM */
9735
9736
9737
9738 \f
9739 /***********************************************************************
9740 Menu Bars
9741 ***********************************************************************/
9742
9743
9744 /* Prepare for redisplay by updating menu-bar item lists when
9745 appropriate. This can call eval. */
9746
9747 void
9748 prepare_menu_bars (void)
9749 {
9750 int all_windows;
9751 struct gcpro gcpro1, gcpro2;
9752 struct frame *f;
9753 Lisp_Object tooltip_frame;
9754
9755 #ifdef HAVE_WINDOW_SYSTEM
9756 tooltip_frame = tip_frame;
9757 #else
9758 tooltip_frame = Qnil;
9759 #endif
9760
9761 /* Update all frame titles based on their buffer names, etc. We do
9762 this before the menu bars so that the buffer-menu will show the
9763 up-to-date frame titles. */
9764 #ifdef HAVE_WINDOW_SYSTEM
9765 if (windows_or_buffers_changed || update_mode_lines)
9766 {
9767 Lisp_Object tail, frame;
9768
9769 FOR_EACH_FRAME (tail, frame)
9770 {
9771 f = XFRAME (frame);
9772 if (!EQ (frame, tooltip_frame)
9773 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9774 x_consider_frame_title (frame);
9775 }
9776 }
9777 #endif /* HAVE_WINDOW_SYSTEM */
9778
9779 /* Update the menu bar item lists, if appropriate. This has to be
9780 done before any actual redisplay or generation of display lines. */
9781 all_windows = (update_mode_lines
9782 || buffer_shared > 1
9783 || windows_or_buffers_changed);
9784 if (all_windows)
9785 {
9786 Lisp_Object tail, frame;
9787 int count = SPECPDL_INDEX ();
9788 /* 1 means that update_menu_bar has run its hooks
9789 so any further calls to update_menu_bar shouldn't do so again. */
9790 int menu_bar_hooks_run = 0;
9791
9792 record_unwind_save_match_data ();
9793
9794 FOR_EACH_FRAME (tail, frame)
9795 {
9796 f = XFRAME (frame);
9797
9798 /* Ignore tooltip frame. */
9799 if (EQ (frame, tooltip_frame))
9800 continue;
9801
9802 /* If a window on this frame changed size, report that to
9803 the user and clear the size-change flag. */
9804 if (FRAME_WINDOW_SIZES_CHANGED (f))
9805 {
9806 Lisp_Object functions;
9807
9808 /* Clear flag first in case we get an error below. */
9809 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9810 functions = Vwindow_size_change_functions;
9811 GCPRO2 (tail, functions);
9812
9813 while (CONSP (functions))
9814 {
9815 if (!EQ (XCAR (functions), Qt))
9816 call1 (XCAR (functions), frame);
9817 functions = XCDR (functions);
9818 }
9819 UNGCPRO;
9820 }
9821
9822 GCPRO1 (tail);
9823 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9824 #ifdef HAVE_WINDOW_SYSTEM
9825 update_tool_bar (f, 0);
9826 #endif
9827 #ifdef HAVE_NS
9828 if (windows_or_buffers_changed
9829 && FRAME_NS_P (f))
9830 ns_set_doc_edited (f, Fbuffer_modified_p
9831 (XWINDOW (f->selected_window)->buffer));
9832 #endif
9833 UNGCPRO;
9834 }
9835
9836 unbind_to (count, Qnil);
9837 }
9838 else
9839 {
9840 struct frame *sf = SELECTED_FRAME ();
9841 update_menu_bar (sf, 1, 0);
9842 #ifdef HAVE_WINDOW_SYSTEM
9843 update_tool_bar (sf, 1);
9844 #endif
9845 }
9846 }
9847
9848
9849 /* Update the menu bar item list for frame F. This has to be done
9850 before we start to fill in any display lines, because it can call
9851 eval.
9852
9853 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9854
9855 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9856 already ran the menu bar hooks for this redisplay, so there
9857 is no need to run them again. The return value is the
9858 updated value of this flag, to pass to the next call. */
9859
9860 static int
9861 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
9862 {
9863 Lisp_Object window;
9864 register struct window *w;
9865
9866 /* If called recursively during a menu update, do nothing. This can
9867 happen when, for instance, an activate-menubar-hook causes a
9868 redisplay. */
9869 if (inhibit_menubar_update)
9870 return hooks_run;
9871
9872 window = FRAME_SELECTED_WINDOW (f);
9873 w = XWINDOW (window);
9874
9875 if (FRAME_WINDOW_P (f)
9876 ?
9877 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9878 || defined (HAVE_NS) || defined (USE_GTK)
9879 FRAME_EXTERNAL_MENU_BAR (f)
9880 #else
9881 FRAME_MENU_BAR_LINES (f) > 0
9882 #endif
9883 : FRAME_MENU_BAR_LINES (f) > 0)
9884 {
9885 /* If the user has switched buffers or windows, we need to
9886 recompute to reflect the new bindings. But we'll
9887 recompute when update_mode_lines is set too; that means
9888 that people can use force-mode-line-update to request
9889 that the menu bar be recomputed. The adverse effect on
9890 the rest of the redisplay algorithm is about the same as
9891 windows_or_buffers_changed anyway. */
9892 if (windows_or_buffers_changed
9893 /* This used to test w->update_mode_line, but we believe
9894 there is no need to recompute the menu in that case. */
9895 || update_mode_lines
9896 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9897 < BUF_MODIFF (XBUFFER (w->buffer)))
9898 != !NILP (w->last_had_star))
9899 || ((!NILP (Vtransient_mark_mode)
9900 && !NILP (XBUFFER (w->buffer)->mark_active))
9901 != !NILP (w->region_showing)))
9902 {
9903 struct buffer *prev = current_buffer;
9904 int count = SPECPDL_INDEX ();
9905
9906 specbind (Qinhibit_menubar_update, Qt);
9907
9908 set_buffer_internal_1 (XBUFFER (w->buffer));
9909 if (save_match_data)
9910 record_unwind_save_match_data ();
9911 if (NILP (Voverriding_local_map_menu_flag))
9912 {
9913 specbind (Qoverriding_terminal_local_map, Qnil);
9914 specbind (Qoverriding_local_map, Qnil);
9915 }
9916
9917 if (!hooks_run)
9918 {
9919 /* Run the Lucid hook. */
9920 safe_run_hooks (Qactivate_menubar_hook);
9921
9922 /* If it has changed current-menubar from previous value,
9923 really recompute the menu-bar from the value. */
9924 if (! NILP (Vlucid_menu_bar_dirty_flag))
9925 call0 (Qrecompute_lucid_menubar);
9926
9927 safe_run_hooks (Qmenu_bar_update_hook);
9928
9929 hooks_run = 1;
9930 }
9931
9932 XSETFRAME (Vmenu_updating_frame, f);
9933 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9934
9935 /* Redisplay the menu bar in case we changed it. */
9936 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9937 || defined (HAVE_NS) || defined (USE_GTK)
9938 if (FRAME_WINDOW_P (f))
9939 {
9940 #if defined (HAVE_NS)
9941 /* All frames on Mac OS share the same menubar. So only
9942 the selected frame should be allowed to set it. */
9943 if (f == SELECTED_FRAME ())
9944 #endif
9945 set_frame_menubar (f, 0, 0);
9946 }
9947 else
9948 /* On a terminal screen, the menu bar is an ordinary screen
9949 line, and this makes it get updated. */
9950 w->update_mode_line = Qt;
9951 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9952 /* In the non-toolkit version, the menu bar is an ordinary screen
9953 line, and this makes it get updated. */
9954 w->update_mode_line = Qt;
9955 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9956
9957 unbind_to (count, Qnil);
9958 set_buffer_internal_1 (prev);
9959 }
9960 }
9961
9962 return hooks_run;
9963 }
9964
9965
9966 \f
9967 /***********************************************************************
9968 Output Cursor
9969 ***********************************************************************/
9970
9971 #ifdef HAVE_WINDOW_SYSTEM
9972
9973 /* EXPORT:
9974 Nominal cursor position -- where to draw output.
9975 HPOS and VPOS are window relative glyph matrix coordinates.
9976 X and Y are window relative pixel coordinates. */
9977
9978 struct cursor_pos output_cursor;
9979
9980
9981 /* EXPORT:
9982 Set the global variable output_cursor to CURSOR. All cursor
9983 positions are relative to updated_window. */
9984
9985 void
9986 set_output_cursor (struct cursor_pos *cursor)
9987 {
9988 output_cursor.hpos = cursor->hpos;
9989 output_cursor.vpos = cursor->vpos;
9990 output_cursor.x = cursor->x;
9991 output_cursor.y = cursor->y;
9992 }
9993
9994
9995 /* EXPORT for RIF:
9996 Set a nominal cursor position.
9997
9998 HPOS and VPOS are column/row positions in a window glyph matrix. X
9999 and Y are window text area relative pixel positions.
10000
10001 If this is done during an update, updated_window will contain the
10002 window that is being updated and the position is the future output
10003 cursor position for that window. If updated_window is null, use
10004 selected_window and display the cursor at the given position. */
10005
10006 void
10007 x_cursor_to (int vpos, int hpos, int y, int x)
10008 {
10009 struct window *w;
10010
10011 /* If updated_window is not set, work on selected_window. */
10012 if (updated_window)
10013 w = updated_window;
10014 else
10015 w = XWINDOW (selected_window);
10016
10017 /* Set the output cursor. */
10018 output_cursor.hpos = hpos;
10019 output_cursor.vpos = vpos;
10020 output_cursor.x = x;
10021 output_cursor.y = y;
10022
10023 /* If not called as part of an update, really display the cursor.
10024 This will also set the cursor position of W. */
10025 if (updated_window == NULL)
10026 {
10027 BLOCK_INPUT;
10028 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10029 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10030 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10031 UNBLOCK_INPUT;
10032 }
10033 }
10034
10035 #endif /* HAVE_WINDOW_SYSTEM */
10036
10037 \f
10038 /***********************************************************************
10039 Tool-bars
10040 ***********************************************************************/
10041
10042 #ifdef HAVE_WINDOW_SYSTEM
10043
10044 /* Where the mouse was last time we reported a mouse event. */
10045
10046 FRAME_PTR last_mouse_frame;
10047
10048 /* Tool-bar item index of the item on which a mouse button was pressed
10049 or -1. */
10050
10051 int last_tool_bar_item;
10052
10053
10054 static Lisp_Object
10055 update_tool_bar_unwind (Lisp_Object frame)
10056 {
10057 selected_frame = frame;
10058 return Qnil;
10059 }
10060
10061 /* Update the tool-bar item list for frame F. This has to be done
10062 before we start to fill in any display lines. Called from
10063 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10064 and restore it here. */
10065
10066 static void
10067 update_tool_bar (struct frame *f, int save_match_data)
10068 {
10069 #if defined (USE_GTK) || defined (HAVE_NS)
10070 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10071 #else
10072 int do_update = WINDOWP (f->tool_bar_window)
10073 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10074 #endif
10075
10076 if (do_update)
10077 {
10078 Lisp_Object window;
10079 struct window *w;
10080
10081 window = FRAME_SELECTED_WINDOW (f);
10082 w = XWINDOW (window);
10083
10084 /* If the user has switched buffers or windows, we need to
10085 recompute to reflect the new bindings. But we'll
10086 recompute when update_mode_lines is set too; that means
10087 that people can use force-mode-line-update to request
10088 that the menu bar be recomputed. The adverse effect on
10089 the rest of the redisplay algorithm is about the same as
10090 windows_or_buffers_changed anyway. */
10091 if (windows_or_buffers_changed
10092 || !NILP (w->update_mode_line)
10093 || update_mode_lines
10094 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10095 < BUF_MODIFF (XBUFFER (w->buffer)))
10096 != !NILP (w->last_had_star))
10097 || ((!NILP (Vtransient_mark_mode)
10098 && !NILP (XBUFFER (w->buffer)->mark_active))
10099 != !NILP (w->region_showing)))
10100 {
10101 struct buffer *prev = current_buffer;
10102 int count = SPECPDL_INDEX ();
10103 Lisp_Object frame, new_tool_bar;
10104 int new_n_tool_bar;
10105 struct gcpro gcpro1;
10106
10107 /* Set current_buffer to the buffer of the selected
10108 window of the frame, so that we get the right local
10109 keymaps. */
10110 set_buffer_internal_1 (XBUFFER (w->buffer));
10111
10112 /* Save match data, if we must. */
10113 if (save_match_data)
10114 record_unwind_save_match_data ();
10115
10116 /* Make sure that we don't accidentally use bogus keymaps. */
10117 if (NILP (Voverriding_local_map_menu_flag))
10118 {
10119 specbind (Qoverriding_terminal_local_map, Qnil);
10120 specbind (Qoverriding_local_map, Qnil);
10121 }
10122
10123 GCPRO1 (new_tool_bar);
10124
10125 /* We must temporarily set the selected frame to this frame
10126 before calling tool_bar_items, because the calculation of
10127 the tool-bar keymap uses the selected frame (see
10128 `tool-bar-make-keymap' in tool-bar.el). */
10129 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10130 XSETFRAME (frame, f);
10131 selected_frame = frame;
10132
10133 /* Build desired tool-bar items from keymaps. */
10134 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10135 &new_n_tool_bar);
10136
10137 /* Redisplay the tool-bar if we changed it. */
10138 if (new_n_tool_bar != f->n_tool_bar_items
10139 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10140 {
10141 /* Redisplay that happens asynchronously due to an expose event
10142 may access f->tool_bar_items. Make sure we update both
10143 variables within BLOCK_INPUT so no such event interrupts. */
10144 BLOCK_INPUT;
10145 f->tool_bar_items = new_tool_bar;
10146 f->n_tool_bar_items = new_n_tool_bar;
10147 w->update_mode_line = Qt;
10148 UNBLOCK_INPUT;
10149 }
10150
10151 UNGCPRO;
10152
10153 unbind_to (count, Qnil);
10154 set_buffer_internal_1 (prev);
10155 }
10156 }
10157 }
10158
10159
10160 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10161 F's desired tool-bar contents. F->tool_bar_items must have
10162 been set up previously by calling prepare_menu_bars. */
10163
10164 static void
10165 build_desired_tool_bar_string (struct frame *f)
10166 {
10167 int i, size, size_needed;
10168 struct gcpro gcpro1, gcpro2, gcpro3;
10169 Lisp_Object image, plist, props;
10170
10171 image = plist = props = Qnil;
10172 GCPRO3 (image, plist, props);
10173
10174 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10175 Otherwise, make a new string. */
10176
10177 /* The size of the string we might be able to reuse. */
10178 size = (STRINGP (f->desired_tool_bar_string)
10179 ? SCHARS (f->desired_tool_bar_string)
10180 : 0);
10181
10182 /* We need one space in the string for each image. */
10183 size_needed = f->n_tool_bar_items;
10184
10185 /* Reuse f->desired_tool_bar_string, if possible. */
10186 if (size < size_needed || NILP (f->desired_tool_bar_string))
10187 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10188 make_number (' '));
10189 else
10190 {
10191 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10192 Fremove_text_properties (make_number (0), make_number (size),
10193 props, f->desired_tool_bar_string);
10194 }
10195
10196 /* Put a `display' property on the string for the images to display,
10197 put a `menu_item' property on tool-bar items with a value that
10198 is the index of the item in F's tool-bar item vector. */
10199 for (i = 0; i < f->n_tool_bar_items; ++i)
10200 {
10201 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10202
10203 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10204 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10205 int hmargin, vmargin, relief, idx, end;
10206
10207 /* If image is a vector, choose the image according to the
10208 button state. */
10209 image = PROP (TOOL_BAR_ITEM_IMAGES);
10210 if (VECTORP (image))
10211 {
10212 if (enabled_p)
10213 idx = (selected_p
10214 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10215 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10216 else
10217 idx = (selected_p
10218 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10219 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10220
10221 xassert (ASIZE (image) >= idx);
10222 image = AREF (image, idx);
10223 }
10224 else
10225 idx = -1;
10226
10227 /* Ignore invalid image specifications. */
10228 if (!valid_image_p (image))
10229 continue;
10230
10231 /* Display the tool-bar button pressed, or depressed. */
10232 plist = Fcopy_sequence (XCDR (image));
10233
10234 /* Compute margin and relief to draw. */
10235 relief = (tool_bar_button_relief >= 0
10236 ? tool_bar_button_relief
10237 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10238 hmargin = vmargin = relief;
10239
10240 if (INTEGERP (Vtool_bar_button_margin)
10241 && XINT (Vtool_bar_button_margin) > 0)
10242 {
10243 hmargin += XFASTINT (Vtool_bar_button_margin);
10244 vmargin += XFASTINT (Vtool_bar_button_margin);
10245 }
10246 else if (CONSP (Vtool_bar_button_margin))
10247 {
10248 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10249 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10250 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10251
10252 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10253 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10254 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10255 }
10256
10257 if (auto_raise_tool_bar_buttons_p)
10258 {
10259 /* Add a `:relief' property to the image spec if the item is
10260 selected. */
10261 if (selected_p)
10262 {
10263 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10264 hmargin -= relief;
10265 vmargin -= relief;
10266 }
10267 }
10268 else
10269 {
10270 /* If image is selected, display it pressed, i.e. with a
10271 negative relief. If it's not selected, display it with a
10272 raised relief. */
10273 plist = Fplist_put (plist, QCrelief,
10274 (selected_p
10275 ? make_number (-relief)
10276 : make_number (relief)));
10277 hmargin -= relief;
10278 vmargin -= relief;
10279 }
10280
10281 /* Put a margin around the image. */
10282 if (hmargin || vmargin)
10283 {
10284 if (hmargin == vmargin)
10285 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10286 else
10287 plist = Fplist_put (plist, QCmargin,
10288 Fcons (make_number (hmargin),
10289 make_number (vmargin)));
10290 }
10291
10292 /* If button is not enabled, and we don't have special images
10293 for the disabled state, make the image appear disabled by
10294 applying an appropriate algorithm to it. */
10295 if (!enabled_p && idx < 0)
10296 plist = Fplist_put (plist, QCconversion, Qdisabled);
10297
10298 /* Put a `display' text property on the string for the image to
10299 display. Put a `menu-item' property on the string that gives
10300 the start of this item's properties in the tool-bar items
10301 vector. */
10302 image = Fcons (Qimage, plist);
10303 props = list4 (Qdisplay, image,
10304 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10305
10306 /* Let the last image hide all remaining spaces in the tool bar
10307 string. The string can be longer than needed when we reuse a
10308 previous string. */
10309 if (i + 1 == f->n_tool_bar_items)
10310 end = SCHARS (f->desired_tool_bar_string);
10311 else
10312 end = i + 1;
10313 Fadd_text_properties (make_number (i), make_number (end),
10314 props, f->desired_tool_bar_string);
10315 #undef PROP
10316 }
10317
10318 UNGCPRO;
10319 }
10320
10321
10322 /* Display one line of the tool-bar of frame IT->f.
10323
10324 HEIGHT specifies the desired height of the tool-bar line.
10325 If the actual height of the glyph row is less than HEIGHT, the
10326 row's height is increased to HEIGHT, and the icons are centered
10327 vertically in the new height.
10328
10329 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10330 count a final empty row in case the tool-bar width exactly matches
10331 the window width.
10332 */
10333
10334 static void
10335 display_tool_bar_line (struct it *it, int height)
10336 {
10337 struct glyph_row *row = it->glyph_row;
10338 int max_x = it->last_visible_x;
10339 struct glyph *last;
10340
10341 prepare_desired_row (row);
10342 row->y = it->current_y;
10343
10344 /* Note that this isn't made use of if the face hasn't a box,
10345 so there's no need to check the face here. */
10346 it->start_of_box_run_p = 1;
10347
10348 while (it->current_x < max_x)
10349 {
10350 int x, n_glyphs_before, i, nglyphs;
10351 struct it it_before;
10352
10353 /* Get the next display element. */
10354 if (!get_next_display_element (it))
10355 {
10356 /* Don't count empty row if we are counting needed tool-bar lines. */
10357 if (height < 0 && !it->hpos)
10358 return;
10359 break;
10360 }
10361
10362 /* Produce glyphs. */
10363 n_glyphs_before = row->used[TEXT_AREA];
10364 it_before = *it;
10365
10366 PRODUCE_GLYPHS (it);
10367
10368 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10369 i = 0;
10370 x = it_before.current_x;
10371 while (i < nglyphs)
10372 {
10373 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10374
10375 if (x + glyph->pixel_width > max_x)
10376 {
10377 /* Glyph doesn't fit on line. Backtrack. */
10378 row->used[TEXT_AREA] = n_glyphs_before;
10379 *it = it_before;
10380 /* If this is the only glyph on this line, it will never fit on the
10381 toolbar, so skip it. But ensure there is at least one glyph,
10382 so we don't accidentally disable the tool-bar. */
10383 if (n_glyphs_before == 0
10384 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10385 break;
10386 goto out;
10387 }
10388
10389 ++it->hpos;
10390 x += glyph->pixel_width;
10391 ++i;
10392 }
10393
10394 /* Stop at line ends. */
10395 if (ITERATOR_AT_END_OF_LINE_P (it))
10396 break;
10397
10398 set_iterator_to_next (it, 1);
10399 }
10400
10401 out:;
10402
10403 row->displays_text_p = row->used[TEXT_AREA] != 0;
10404
10405 /* Use default face for the border below the tool bar.
10406
10407 FIXME: When auto-resize-tool-bars is grow-only, there is
10408 no additional border below the possibly empty tool-bar lines.
10409 So to make the extra empty lines look "normal", we have to
10410 use the tool-bar face for the border too. */
10411 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10412 it->face_id = DEFAULT_FACE_ID;
10413
10414 extend_face_to_end_of_line (it);
10415 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10416 last->right_box_line_p = 1;
10417 if (last == row->glyphs[TEXT_AREA])
10418 last->left_box_line_p = 1;
10419
10420 /* Make line the desired height and center it vertically. */
10421 if ((height -= it->max_ascent + it->max_descent) > 0)
10422 {
10423 /* Don't add more than one line height. */
10424 height %= FRAME_LINE_HEIGHT (it->f);
10425 it->max_ascent += height / 2;
10426 it->max_descent += (height + 1) / 2;
10427 }
10428
10429 compute_line_metrics (it);
10430
10431 /* If line is empty, make it occupy the rest of the tool-bar. */
10432 if (!row->displays_text_p)
10433 {
10434 row->height = row->phys_height = it->last_visible_y - row->y;
10435 row->visible_height = row->height;
10436 row->ascent = row->phys_ascent = 0;
10437 row->extra_line_spacing = 0;
10438 }
10439
10440 row->full_width_p = 1;
10441 row->continued_p = 0;
10442 row->truncated_on_left_p = 0;
10443 row->truncated_on_right_p = 0;
10444
10445 it->current_x = it->hpos = 0;
10446 it->current_y += row->height;
10447 ++it->vpos;
10448 ++it->glyph_row;
10449 }
10450
10451
10452 /* Max tool-bar height. */
10453
10454 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10455 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10456
10457 /* Value is the number of screen lines needed to make all tool-bar
10458 items of frame F visible. The number of actual rows needed is
10459 returned in *N_ROWS if non-NULL. */
10460
10461 static int
10462 tool_bar_lines_needed (struct frame *f, int *n_rows)
10463 {
10464 struct window *w = XWINDOW (f->tool_bar_window);
10465 struct it it;
10466 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10467 the desired matrix, so use (unused) mode-line row as temporary row to
10468 avoid destroying the first tool-bar row. */
10469 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10470
10471 /* Initialize an iterator for iteration over
10472 F->desired_tool_bar_string in the tool-bar window of frame F. */
10473 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10474 it.first_visible_x = 0;
10475 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10476 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10477
10478 while (!ITERATOR_AT_END_P (&it))
10479 {
10480 clear_glyph_row (temp_row);
10481 it.glyph_row = temp_row;
10482 display_tool_bar_line (&it, -1);
10483 }
10484 clear_glyph_row (temp_row);
10485
10486 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10487 if (n_rows)
10488 *n_rows = it.vpos > 0 ? it.vpos : -1;
10489
10490 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10491 }
10492
10493
10494 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10495 0, 1, 0,
10496 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10497 (Lisp_Object frame)
10498 {
10499 struct frame *f;
10500 struct window *w;
10501 int nlines = 0;
10502
10503 if (NILP (frame))
10504 frame = selected_frame;
10505 else
10506 CHECK_FRAME (frame);
10507 f = XFRAME (frame);
10508
10509 if (WINDOWP (f->tool_bar_window)
10510 || (w = XWINDOW (f->tool_bar_window),
10511 WINDOW_TOTAL_LINES (w) > 0))
10512 {
10513 update_tool_bar (f, 1);
10514 if (f->n_tool_bar_items)
10515 {
10516 build_desired_tool_bar_string (f);
10517 nlines = tool_bar_lines_needed (f, NULL);
10518 }
10519 }
10520
10521 return make_number (nlines);
10522 }
10523
10524
10525 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10526 height should be changed. */
10527
10528 static int
10529 redisplay_tool_bar (struct frame *f)
10530 {
10531 struct window *w;
10532 struct it it;
10533 struct glyph_row *row;
10534
10535 #if defined (USE_GTK) || defined (HAVE_NS)
10536 if (FRAME_EXTERNAL_TOOL_BAR (f))
10537 update_frame_tool_bar (f);
10538 return 0;
10539 #endif
10540
10541 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10542 do anything. This means you must start with tool-bar-lines
10543 non-zero to get the auto-sizing effect. Or in other words, you
10544 can turn off tool-bars by specifying tool-bar-lines zero. */
10545 if (!WINDOWP (f->tool_bar_window)
10546 || (w = XWINDOW (f->tool_bar_window),
10547 WINDOW_TOTAL_LINES (w) == 0))
10548 return 0;
10549
10550 /* Set up an iterator for the tool-bar window. */
10551 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10552 it.first_visible_x = 0;
10553 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10554 row = it.glyph_row;
10555
10556 /* Build a string that represents the contents of the tool-bar. */
10557 build_desired_tool_bar_string (f);
10558 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10559
10560 if (f->n_tool_bar_rows == 0)
10561 {
10562 int nlines;
10563
10564 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10565 nlines != WINDOW_TOTAL_LINES (w)))
10566 {
10567 Lisp_Object frame;
10568 int old_height = WINDOW_TOTAL_LINES (w);
10569
10570 XSETFRAME (frame, f);
10571 Fmodify_frame_parameters (frame,
10572 Fcons (Fcons (Qtool_bar_lines,
10573 make_number (nlines)),
10574 Qnil));
10575 if (WINDOW_TOTAL_LINES (w) != old_height)
10576 {
10577 clear_glyph_matrix (w->desired_matrix);
10578 fonts_changed_p = 1;
10579 return 1;
10580 }
10581 }
10582 }
10583
10584 /* Display as many lines as needed to display all tool-bar items. */
10585
10586 if (f->n_tool_bar_rows > 0)
10587 {
10588 int border, rows, height, extra;
10589
10590 if (INTEGERP (Vtool_bar_border))
10591 border = XINT (Vtool_bar_border);
10592 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10593 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10594 else if (EQ (Vtool_bar_border, Qborder_width))
10595 border = f->border_width;
10596 else
10597 border = 0;
10598 if (border < 0)
10599 border = 0;
10600
10601 rows = f->n_tool_bar_rows;
10602 height = max (1, (it.last_visible_y - border) / rows);
10603 extra = it.last_visible_y - border - height * rows;
10604
10605 while (it.current_y < it.last_visible_y)
10606 {
10607 int h = 0;
10608 if (extra > 0 && rows-- > 0)
10609 {
10610 h = (extra + rows - 1) / rows;
10611 extra -= h;
10612 }
10613 display_tool_bar_line (&it, height + h);
10614 }
10615 }
10616 else
10617 {
10618 while (it.current_y < it.last_visible_y)
10619 display_tool_bar_line (&it, 0);
10620 }
10621
10622 /* It doesn't make much sense to try scrolling in the tool-bar
10623 window, so don't do it. */
10624 w->desired_matrix->no_scrolling_p = 1;
10625 w->must_be_updated_p = 1;
10626
10627 if (!NILP (Vauto_resize_tool_bars))
10628 {
10629 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10630 int change_height_p = 0;
10631
10632 /* If we couldn't display everything, change the tool-bar's
10633 height if there is room for more. */
10634 if (IT_STRING_CHARPOS (it) < it.end_charpos
10635 && it.current_y < max_tool_bar_height)
10636 change_height_p = 1;
10637
10638 row = it.glyph_row - 1;
10639
10640 /* If there are blank lines at the end, except for a partially
10641 visible blank line at the end that is smaller than
10642 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10643 if (!row->displays_text_p
10644 && row->height >= FRAME_LINE_HEIGHT (f))
10645 change_height_p = 1;
10646
10647 /* If row displays tool-bar items, but is partially visible,
10648 change the tool-bar's height. */
10649 if (row->displays_text_p
10650 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10651 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10652 change_height_p = 1;
10653
10654 /* Resize windows as needed by changing the `tool-bar-lines'
10655 frame parameter. */
10656 if (change_height_p)
10657 {
10658 Lisp_Object frame;
10659 int old_height = WINDOW_TOTAL_LINES (w);
10660 int nrows;
10661 int nlines = tool_bar_lines_needed (f, &nrows);
10662
10663 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10664 && !f->minimize_tool_bar_window_p)
10665 ? (nlines > old_height)
10666 : (nlines != old_height));
10667 f->minimize_tool_bar_window_p = 0;
10668
10669 if (change_height_p)
10670 {
10671 XSETFRAME (frame, f);
10672 Fmodify_frame_parameters (frame,
10673 Fcons (Fcons (Qtool_bar_lines,
10674 make_number (nlines)),
10675 Qnil));
10676 if (WINDOW_TOTAL_LINES (w) != old_height)
10677 {
10678 clear_glyph_matrix (w->desired_matrix);
10679 f->n_tool_bar_rows = nrows;
10680 fonts_changed_p = 1;
10681 return 1;
10682 }
10683 }
10684 }
10685 }
10686
10687 f->minimize_tool_bar_window_p = 0;
10688 return 0;
10689 }
10690
10691
10692 /* Get information about the tool-bar item which is displayed in GLYPH
10693 on frame F. Return in *PROP_IDX the index where tool-bar item
10694 properties start in F->tool_bar_items. Value is zero if
10695 GLYPH doesn't display a tool-bar item. */
10696
10697 static int
10698 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10699 {
10700 Lisp_Object prop;
10701 int success_p;
10702 int charpos;
10703
10704 /* This function can be called asynchronously, which means we must
10705 exclude any possibility that Fget_text_property signals an
10706 error. */
10707 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10708 charpos = max (0, charpos);
10709
10710 /* Get the text property `menu-item' at pos. The value of that
10711 property is the start index of this item's properties in
10712 F->tool_bar_items. */
10713 prop = Fget_text_property (make_number (charpos),
10714 Qmenu_item, f->current_tool_bar_string);
10715 if (INTEGERP (prop))
10716 {
10717 *prop_idx = XINT (prop);
10718 success_p = 1;
10719 }
10720 else
10721 success_p = 0;
10722
10723 return success_p;
10724 }
10725
10726 \f
10727 /* Get information about the tool-bar item at position X/Y on frame F.
10728 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10729 the current matrix of the tool-bar window of F, or NULL if not
10730 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10731 item in F->tool_bar_items. Value is
10732
10733 -1 if X/Y is not on a tool-bar item
10734 0 if X/Y is on the same item that was highlighted before.
10735 1 otherwise. */
10736
10737 static int
10738 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
10739 int *hpos, int *vpos, int *prop_idx)
10740 {
10741 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10742 struct window *w = XWINDOW (f->tool_bar_window);
10743 int area;
10744
10745 /* Find the glyph under X/Y. */
10746 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10747 if (*glyph == NULL)
10748 return -1;
10749
10750 /* Get the start of this tool-bar item's properties in
10751 f->tool_bar_items. */
10752 if (!tool_bar_item_info (f, *glyph, prop_idx))
10753 return -1;
10754
10755 /* Is mouse on the highlighted item? */
10756 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10757 && *vpos >= dpyinfo->mouse_face_beg_row
10758 && *vpos <= dpyinfo->mouse_face_end_row
10759 && (*vpos > dpyinfo->mouse_face_beg_row
10760 || *hpos >= dpyinfo->mouse_face_beg_col)
10761 && (*vpos < dpyinfo->mouse_face_end_row
10762 || *hpos < dpyinfo->mouse_face_end_col
10763 || dpyinfo->mouse_face_past_end))
10764 return 0;
10765
10766 return 1;
10767 }
10768
10769
10770 /* EXPORT:
10771 Handle mouse button event on the tool-bar of frame F, at
10772 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10773 0 for button release. MODIFIERS is event modifiers for button
10774 release. */
10775
10776 void
10777 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
10778 unsigned int modifiers)
10779 {
10780 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10781 struct window *w = XWINDOW (f->tool_bar_window);
10782 int hpos, vpos, prop_idx;
10783 struct glyph *glyph;
10784 Lisp_Object enabled_p;
10785
10786 /* If not on the highlighted tool-bar item, return. */
10787 frame_to_window_pixel_xy (w, &x, &y);
10788 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10789 return;
10790
10791 /* If item is disabled, do nothing. */
10792 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10793 if (NILP (enabled_p))
10794 return;
10795
10796 if (down_p)
10797 {
10798 /* Show item in pressed state. */
10799 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10800 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10801 last_tool_bar_item = prop_idx;
10802 }
10803 else
10804 {
10805 Lisp_Object key, frame;
10806 struct input_event event;
10807 EVENT_INIT (event);
10808
10809 /* Show item in released state. */
10810 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10811 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10812
10813 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10814
10815 XSETFRAME (frame, f);
10816 event.kind = TOOL_BAR_EVENT;
10817 event.frame_or_window = frame;
10818 event.arg = frame;
10819 kbd_buffer_store_event (&event);
10820
10821 event.kind = TOOL_BAR_EVENT;
10822 event.frame_or_window = frame;
10823 event.arg = key;
10824 event.modifiers = modifiers;
10825 kbd_buffer_store_event (&event);
10826 last_tool_bar_item = -1;
10827 }
10828 }
10829
10830
10831 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10832 tool-bar window-relative coordinates X/Y. Called from
10833 note_mouse_highlight. */
10834
10835 static void
10836 note_tool_bar_highlight (struct frame *f, int x, int y)
10837 {
10838 Lisp_Object window = f->tool_bar_window;
10839 struct window *w = XWINDOW (window);
10840 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10841 int hpos, vpos;
10842 struct glyph *glyph;
10843 struct glyph_row *row;
10844 int i;
10845 Lisp_Object enabled_p;
10846 int prop_idx;
10847 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10848 int mouse_down_p, rc;
10849
10850 /* Function note_mouse_highlight is called with negative X/Y
10851 values when mouse moves outside of the frame. */
10852 if (x <= 0 || y <= 0)
10853 {
10854 clear_mouse_face (dpyinfo);
10855 return;
10856 }
10857
10858 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10859 if (rc < 0)
10860 {
10861 /* Not on tool-bar item. */
10862 clear_mouse_face (dpyinfo);
10863 return;
10864 }
10865 else if (rc == 0)
10866 /* On same tool-bar item as before. */
10867 goto set_help_echo;
10868
10869 clear_mouse_face (dpyinfo);
10870
10871 /* Mouse is down, but on different tool-bar item? */
10872 mouse_down_p = (dpyinfo->grabbed
10873 && f == last_mouse_frame
10874 && FRAME_LIVE_P (f));
10875 if (mouse_down_p
10876 && last_tool_bar_item != prop_idx)
10877 return;
10878
10879 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10880 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10881
10882 /* If tool-bar item is not enabled, don't highlight it. */
10883 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10884 if (!NILP (enabled_p))
10885 {
10886 /* Compute the x-position of the glyph. In front and past the
10887 image is a space. We include this in the highlighted area. */
10888 row = MATRIX_ROW (w->current_matrix, vpos);
10889 for (i = x = 0; i < hpos; ++i)
10890 x += row->glyphs[TEXT_AREA][i].pixel_width;
10891
10892 /* Record this as the current active region. */
10893 dpyinfo->mouse_face_beg_col = hpos;
10894 dpyinfo->mouse_face_beg_row = vpos;
10895 dpyinfo->mouse_face_beg_x = x;
10896 dpyinfo->mouse_face_beg_y = row->y;
10897 dpyinfo->mouse_face_past_end = 0;
10898
10899 dpyinfo->mouse_face_end_col = hpos + 1;
10900 dpyinfo->mouse_face_end_row = vpos;
10901 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10902 dpyinfo->mouse_face_end_y = row->y;
10903 dpyinfo->mouse_face_window = window;
10904 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10905
10906 /* Display it as active. */
10907 show_mouse_face (dpyinfo, draw);
10908 dpyinfo->mouse_face_image_state = draw;
10909 }
10910
10911 set_help_echo:
10912
10913 /* Set help_echo_string to a help string to display for this tool-bar item.
10914 XTread_socket does the rest. */
10915 help_echo_object = help_echo_window = Qnil;
10916 help_echo_pos = -1;
10917 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10918 if (NILP (help_echo_string))
10919 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10920 }
10921
10922 #endif /* HAVE_WINDOW_SYSTEM */
10923
10924
10925 \f
10926 /************************************************************************
10927 Horizontal scrolling
10928 ************************************************************************/
10929
10930 static int hscroll_window_tree (Lisp_Object);
10931 static int hscroll_windows (Lisp_Object);
10932
10933 /* For all leaf windows in the window tree rooted at WINDOW, set their
10934 hscroll value so that PT is (i) visible in the window, and (ii) so
10935 that it is not within a certain margin at the window's left and
10936 right border. Value is non-zero if any window's hscroll has been
10937 changed. */
10938
10939 static int
10940 hscroll_window_tree (Lisp_Object window)
10941 {
10942 int hscrolled_p = 0;
10943 int hscroll_relative_p = FLOATP (Vhscroll_step);
10944 int hscroll_step_abs = 0;
10945 double hscroll_step_rel = 0;
10946
10947 if (hscroll_relative_p)
10948 {
10949 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10950 if (hscroll_step_rel < 0)
10951 {
10952 hscroll_relative_p = 0;
10953 hscroll_step_abs = 0;
10954 }
10955 }
10956 else if (INTEGERP (Vhscroll_step))
10957 {
10958 hscroll_step_abs = XINT (Vhscroll_step);
10959 if (hscroll_step_abs < 0)
10960 hscroll_step_abs = 0;
10961 }
10962 else
10963 hscroll_step_abs = 0;
10964
10965 while (WINDOWP (window))
10966 {
10967 struct window *w = XWINDOW (window);
10968
10969 if (WINDOWP (w->hchild))
10970 hscrolled_p |= hscroll_window_tree (w->hchild);
10971 else if (WINDOWP (w->vchild))
10972 hscrolled_p |= hscroll_window_tree (w->vchild);
10973 else if (w->cursor.vpos >= 0)
10974 {
10975 int h_margin;
10976 int text_area_width;
10977 struct glyph_row *current_cursor_row
10978 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10979 struct glyph_row *desired_cursor_row
10980 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10981 struct glyph_row *cursor_row
10982 = (desired_cursor_row->enabled_p
10983 ? desired_cursor_row
10984 : current_cursor_row);
10985
10986 text_area_width = window_box_width (w, TEXT_AREA);
10987
10988 /* Scroll when cursor is inside this scroll margin. */
10989 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10990
10991 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10992 && ((XFASTINT (w->hscroll)
10993 && w->cursor.x <= h_margin)
10994 || (cursor_row->enabled_p
10995 && cursor_row->truncated_on_right_p
10996 && (w->cursor.x >= text_area_width - h_margin))))
10997 {
10998 struct it it;
10999 int hscroll;
11000 struct buffer *saved_current_buffer;
11001 EMACS_INT pt;
11002 int wanted_x;
11003
11004 /* Find point in a display of infinite width. */
11005 saved_current_buffer = current_buffer;
11006 current_buffer = XBUFFER (w->buffer);
11007
11008 if (w == XWINDOW (selected_window))
11009 pt = BUF_PT (current_buffer);
11010 else
11011 {
11012 pt = marker_position (w->pointm);
11013 pt = max (BEGV, pt);
11014 pt = min (ZV, pt);
11015 }
11016
11017 /* Move iterator to pt starting at cursor_row->start in
11018 a line with infinite width. */
11019 init_to_row_start (&it, w, cursor_row);
11020 it.last_visible_x = INFINITY;
11021 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11022 current_buffer = saved_current_buffer;
11023
11024 /* Position cursor in window. */
11025 if (!hscroll_relative_p && hscroll_step_abs == 0)
11026 hscroll = max (0, (it.current_x
11027 - (ITERATOR_AT_END_OF_LINE_P (&it)
11028 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11029 : (text_area_width / 2))))
11030 / FRAME_COLUMN_WIDTH (it.f);
11031 else if (w->cursor.x >= text_area_width - h_margin)
11032 {
11033 if (hscroll_relative_p)
11034 wanted_x = text_area_width * (1 - hscroll_step_rel)
11035 - h_margin;
11036 else
11037 wanted_x = text_area_width
11038 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11039 - h_margin;
11040 hscroll
11041 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11042 }
11043 else
11044 {
11045 if (hscroll_relative_p)
11046 wanted_x = text_area_width * hscroll_step_rel
11047 + h_margin;
11048 else
11049 wanted_x = 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 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11055
11056 /* Don't call Fset_window_hscroll if value hasn't
11057 changed because it will prevent redisplay
11058 optimizations. */
11059 if (XFASTINT (w->hscroll) != hscroll)
11060 {
11061 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11062 w->hscroll = make_number (hscroll);
11063 hscrolled_p = 1;
11064 }
11065 }
11066 }
11067
11068 window = w->next;
11069 }
11070
11071 /* Value is non-zero if hscroll of any leaf window has been changed. */
11072 return hscrolled_p;
11073 }
11074
11075
11076 /* Set hscroll so that cursor is visible and not inside horizontal
11077 scroll margins for all windows in the tree rooted at WINDOW. See
11078 also hscroll_window_tree above. Value is non-zero if any window's
11079 hscroll has been changed. If it has, desired matrices on the frame
11080 of WINDOW are cleared. */
11081
11082 static int
11083 hscroll_windows (Lisp_Object window)
11084 {
11085 int hscrolled_p = hscroll_window_tree (window);
11086 if (hscrolled_p)
11087 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11088 return hscrolled_p;
11089 }
11090
11091
11092 \f
11093 /************************************************************************
11094 Redisplay
11095 ************************************************************************/
11096
11097 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11098 to a non-zero value. This is sometimes handy to have in a debugger
11099 session. */
11100
11101 #if GLYPH_DEBUG
11102
11103 /* First and last unchanged row for try_window_id. */
11104
11105 int debug_first_unchanged_at_end_vpos;
11106 int debug_last_unchanged_at_beg_vpos;
11107
11108 /* Delta vpos and y. */
11109
11110 int debug_dvpos, debug_dy;
11111
11112 /* Delta in characters and bytes for try_window_id. */
11113
11114 EMACS_INT debug_delta, debug_delta_bytes;
11115
11116 /* Values of window_end_pos and window_end_vpos at the end of
11117 try_window_id. */
11118
11119 EMACS_INT debug_end_pos, debug_end_vpos;
11120
11121 /* Append a string to W->desired_matrix->method. FMT is a printf
11122 format string. A1...A9 are a supplement for a variable-length
11123 argument list. If trace_redisplay_p is non-zero also printf the
11124 resulting string to stderr. */
11125
11126 static void
11127 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11128 struct window *w;
11129 char *fmt;
11130 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11131 {
11132 char buffer[512];
11133 char *method = w->desired_matrix->method;
11134 int len = strlen (method);
11135 int size = sizeof w->desired_matrix->method;
11136 int remaining = size - len - 1;
11137
11138 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11139 if (len && remaining)
11140 {
11141 method[len] = '|';
11142 --remaining, ++len;
11143 }
11144
11145 strncpy (method + len, buffer, remaining);
11146
11147 if (trace_redisplay_p)
11148 fprintf (stderr, "%p (%s): %s\n",
11149 w,
11150 ((BUFFERP (w->buffer)
11151 && STRINGP (XBUFFER (w->buffer)->name))
11152 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11153 : "no buffer"),
11154 buffer);
11155 }
11156
11157 #endif /* GLYPH_DEBUG */
11158
11159
11160 /* Value is non-zero if all changes in window W, which displays
11161 current_buffer, are in the text between START and END. START is a
11162 buffer position, END is given as a distance from Z. Used in
11163 redisplay_internal for display optimization. */
11164
11165 static INLINE int
11166 text_outside_line_unchanged_p (struct window *w,
11167 EMACS_INT start, EMACS_INT end)
11168 {
11169 int unchanged_p = 1;
11170
11171 /* If text or overlays have changed, see where. */
11172 if (XFASTINT (w->last_modified) < MODIFF
11173 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11174 {
11175 /* Gap in the line? */
11176 if (GPT < start || Z - GPT < end)
11177 unchanged_p = 0;
11178
11179 /* Changes start in front of the line, or end after it? */
11180 if (unchanged_p
11181 && (BEG_UNCHANGED < start - 1
11182 || END_UNCHANGED < end))
11183 unchanged_p = 0;
11184
11185 /* If selective display, can't optimize if changes start at the
11186 beginning of the line. */
11187 if (unchanged_p
11188 && INTEGERP (current_buffer->selective_display)
11189 && XINT (current_buffer->selective_display) > 0
11190 && (BEG_UNCHANGED < start || GPT <= start))
11191 unchanged_p = 0;
11192
11193 /* If there are overlays at the start or end of the line, these
11194 may have overlay strings with newlines in them. A change at
11195 START, for instance, may actually concern the display of such
11196 overlay strings as well, and they are displayed on different
11197 lines. So, quickly rule out this case. (For the future, it
11198 might be desirable to implement something more telling than
11199 just BEG/END_UNCHANGED.) */
11200 if (unchanged_p)
11201 {
11202 if (BEG + BEG_UNCHANGED == start
11203 && overlay_touches_p (start))
11204 unchanged_p = 0;
11205 if (END_UNCHANGED == end
11206 && overlay_touches_p (Z - end))
11207 unchanged_p = 0;
11208 }
11209
11210 /* Under bidi reordering, adding or deleting a character in the
11211 beginning of a paragraph, before the first strong directional
11212 character, can change the base direction of the paragraph (unless
11213 the buffer specifies a fixed paragraph direction), which will
11214 require to redisplay the whole paragraph. It might be worthwhile
11215 to find the paragraph limits and widen the range of redisplayed
11216 lines to that, but for now just give up this optimization. */
11217 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11218 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11219 unchanged_p = 0;
11220 }
11221
11222 return unchanged_p;
11223 }
11224
11225
11226 /* Do a frame update, taking possible shortcuts into account. This is
11227 the main external entry point for redisplay.
11228
11229 If the last redisplay displayed an echo area message and that message
11230 is no longer requested, we clear the echo area or bring back the
11231 mini-buffer if that is in use. */
11232
11233 void
11234 redisplay (void)
11235 {
11236 redisplay_internal (0);
11237 }
11238
11239
11240 static Lisp_Object
11241 overlay_arrow_string_or_property (Lisp_Object var)
11242 {
11243 Lisp_Object val;
11244
11245 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11246 return val;
11247
11248 return Voverlay_arrow_string;
11249 }
11250
11251 /* Return 1 if there are any overlay-arrows in current_buffer. */
11252 static int
11253 overlay_arrow_in_current_buffer_p (void)
11254 {
11255 Lisp_Object vlist;
11256
11257 for (vlist = Voverlay_arrow_variable_list;
11258 CONSP (vlist);
11259 vlist = XCDR (vlist))
11260 {
11261 Lisp_Object var = XCAR (vlist);
11262 Lisp_Object val;
11263
11264 if (!SYMBOLP (var))
11265 continue;
11266 val = find_symbol_value (var);
11267 if (MARKERP (val)
11268 && current_buffer == XMARKER (val)->buffer)
11269 return 1;
11270 }
11271 return 0;
11272 }
11273
11274
11275 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11276 has changed. */
11277
11278 static int
11279 overlay_arrows_changed_p (void)
11280 {
11281 Lisp_Object vlist;
11282
11283 for (vlist = Voverlay_arrow_variable_list;
11284 CONSP (vlist);
11285 vlist = XCDR (vlist))
11286 {
11287 Lisp_Object var = XCAR (vlist);
11288 Lisp_Object val, pstr;
11289
11290 if (!SYMBOLP (var))
11291 continue;
11292 val = find_symbol_value (var);
11293 if (!MARKERP (val))
11294 continue;
11295 if (! EQ (COERCE_MARKER (val),
11296 Fget (var, Qlast_arrow_position))
11297 || ! (pstr = overlay_arrow_string_or_property (var),
11298 EQ (pstr, Fget (var, Qlast_arrow_string))))
11299 return 1;
11300 }
11301 return 0;
11302 }
11303
11304 /* Mark overlay arrows to be updated on next redisplay. */
11305
11306 static void
11307 update_overlay_arrows (int up_to_date)
11308 {
11309 Lisp_Object vlist;
11310
11311 for (vlist = Voverlay_arrow_variable_list;
11312 CONSP (vlist);
11313 vlist = XCDR (vlist))
11314 {
11315 Lisp_Object var = XCAR (vlist);
11316
11317 if (!SYMBOLP (var))
11318 continue;
11319
11320 if (up_to_date > 0)
11321 {
11322 Lisp_Object val = find_symbol_value (var);
11323 Fput (var, Qlast_arrow_position,
11324 COERCE_MARKER (val));
11325 Fput (var, Qlast_arrow_string,
11326 overlay_arrow_string_or_property (var));
11327 }
11328 else if (up_to_date < 0
11329 || !NILP (Fget (var, Qlast_arrow_position)))
11330 {
11331 Fput (var, Qlast_arrow_position, Qt);
11332 Fput (var, Qlast_arrow_string, Qt);
11333 }
11334 }
11335 }
11336
11337
11338 /* Return overlay arrow string to display at row.
11339 Return integer (bitmap number) for arrow bitmap in left fringe.
11340 Return nil if no overlay arrow. */
11341
11342 static Lisp_Object
11343 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11344 {
11345 Lisp_Object vlist;
11346
11347 for (vlist = Voverlay_arrow_variable_list;
11348 CONSP (vlist);
11349 vlist = XCDR (vlist))
11350 {
11351 Lisp_Object var = XCAR (vlist);
11352 Lisp_Object val;
11353
11354 if (!SYMBOLP (var))
11355 continue;
11356
11357 val = find_symbol_value (var);
11358
11359 if (MARKERP (val)
11360 && current_buffer == XMARKER (val)->buffer
11361 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11362 {
11363 if (FRAME_WINDOW_P (it->f)
11364 /* FIXME: if ROW->reversed_p is set, this should test
11365 the right fringe, not the left one. */
11366 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11367 {
11368 #ifdef HAVE_WINDOW_SYSTEM
11369 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11370 {
11371 int fringe_bitmap;
11372 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11373 return make_number (fringe_bitmap);
11374 }
11375 #endif
11376 return make_number (-1); /* Use default arrow bitmap */
11377 }
11378 return overlay_arrow_string_or_property (var);
11379 }
11380 }
11381
11382 return Qnil;
11383 }
11384
11385 /* Return 1 if point moved out of or into a composition. Otherwise
11386 return 0. PREV_BUF and PREV_PT are the last point buffer and
11387 position. BUF and PT are the current point buffer and position. */
11388
11389 int
11390 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11391 struct buffer *buf, EMACS_INT pt)
11392 {
11393 EMACS_INT start, end;
11394 Lisp_Object prop;
11395 Lisp_Object buffer;
11396
11397 XSETBUFFER (buffer, buf);
11398 /* Check a composition at the last point if point moved within the
11399 same buffer. */
11400 if (prev_buf == buf)
11401 {
11402 if (prev_pt == pt)
11403 /* Point didn't move. */
11404 return 0;
11405
11406 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11407 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11408 && COMPOSITION_VALID_P (start, end, prop)
11409 && start < prev_pt && end > prev_pt)
11410 /* The last point was within the composition. Return 1 iff
11411 point moved out of the composition. */
11412 return (pt <= start || pt >= end);
11413 }
11414
11415 /* Check a composition at the current point. */
11416 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11417 && find_composition (pt, -1, &start, &end, &prop, buffer)
11418 && COMPOSITION_VALID_P (start, end, prop)
11419 && start < pt && end > pt);
11420 }
11421
11422
11423 /* Reconsider the setting of B->clip_changed which is displayed
11424 in window W. */
11425
11426 static INLINE void
11427 reconsider_clip_changes (struct window *w, struct buffer *b)
11428 {
11429 if (b->clip_changed
11430 && !NILP (w->window_end_valid)
11431 && w->current_matrix->buffer == b
11432 && w->current_matrix->zv == BUF_ZV (b)
11433 && w->current_matrix->begv == BUF_BEGV (b))
11434 b->clip_changed = 0;
11435
11436 /* If display wasn't paused, and W is not a tool bar window, see if
11437 point has been moved into or out of a composition. In that case,
11438 we set b->clip_changed to 1 to force updating the screen. If
11439 b->clip_changed has already been set to 1, we can skip this
11440 check. */
11441 if (!b->clip_changed
11442 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11443 {
11444 EMACS_INT pt;
11445
11446 if (w == XWINDOW (selected_window))
11447 pt = BUF_PT (current_buffer);
11448 else
11449 pt = marker_position (w->pointm);
11450
11451 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11452 || pt != XINT (w->last_point))
11453 && check_point_in_composition (w->current_matrix->buffer,
11454 XINT (w->last_point),
11455 XBUFFER (w->buffer), pt))
11456 b->clip_changed = 1;
11457 }
11458 }
11459 \f
11460
11461 /* Select FRAME to forward the values of frame-local variables into C
11462 variables so that the redisplay routines can access those values
11463 directly. */
11464
11465 static void
11466 select_frame_for_redisplay (Lisp_Object frame)
11467 {
11468 Lisp_Object tail, tem;
11469 Lisp_Object old = selected_frame;
11470 struct Lisp_Symbol *sym;
11471
11472 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11473
11474 selected_frame = frame;
11475
11476 do {
11477 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11478 if (CONSP (XCAR (tail))
11479 && (tem = XCAR (XCAR (tail)),
11480 SYMBOLP (tem))
11481 && (sym = indirect_variable (XSYMBOL (tem)),
11482 sym->redirect == SYMBOL_LOCALIZED)
11483 && sym->val.blv->frame_local)
11484 /* Use find_symbol_value rather than Fsymbol_value
11485 to avoid an error if it is void. */
11486 find_symbol_value (tem);
11487 } while (!EQ (frame, old) && (frame = old, 1));
11488 }
11489
11490
11491 #define STOP_POLLING \
11492 do { if (! polling_stopped_here) stop_polling (); \
11493 polling_stopped_here = 1; } while (0)
11494
11495 #define RESUME_POLLING \
11496 do { if (polling_stopped_here) start_polling (); \
11497 polling_stopped_here = 0; } while (0)
11498
11499
11500 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11501 response to any user action; therefore, we should preserve the echo
11502 area. (Actually, our caller does that job.) Perhaps in the future
11503 avoid recentering windows if it is not necessary; currently that
11504 causes some problems. */
11505
11506 static void
11507 redisplay_internal (int preserve_echo_area)
11508 {
11509 struct window *w = XWINDOW (selected_window);
11510 struct frame *f;
11511 int pause;
11512 int must_finish = 0;
11513 struct text_pos tlbufpos, tlendpos;
11514 int number_of_visible_frames;
11515 int count, count1;
11516 struct frame *sf;
11517 int polling_stopped_here = 0;
11518 Lisp_Object old_frame = selected_frame;
11519
11520 /* Non-zero means redisplay has to consider all windows on all
11521 frames. Zero means, only selected_window is considered. */
11522 int consider_all_windows_p;
11523
11524 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11525
11526 /* No redisplay if running in batch mode or frame is not yet fully
11527 initialized, or redisplay is explicitly turned off by setting
11528 Vinhibit_redisplay. */
11529 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11530 || !NILP (Vinhibit_redisplay))
11531 return;
11532
11533 /* Don't examine these until after testing Vinhibit_redisplay.
11534 When Emacs is shutting down, perhaps because its connection to
11535 X has dropped, we should not look at them at all. */
11536 f = XFRAME (w->frame);
11537 sf = SELECTED_FRAME ();
11538
11539 if (!f->glyphs_initialized_p)
11540 return;
11541
11542 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11543 if (popup_activated ())
11544 return;
11545 #endif
11546
11547 /* I don't think this happens but let's be paranoid. */
11548 if (redisplaying_p)
11549 return;
11550
11551 /* Record a function that resets redisplaying_p to its old value
11552 when we leave this function. */
11553 count = SPECPDL_INDEX ();
11554 record_unwind_protect (unwind_redisplay,
11555 Fcons (make_number (redisplaying_p), selected_frame));
11556 ++redisplaying_p;
11557 specbind (Qinhibit_free_realized_faces, Qnil);
11558
11559 {
11560 Lisp_Object tail, frame;
11561
11562 FOR_EACH_FRAME (tail, frame)
11563 {
11564 struct frame *f = XFRAME (frame);
11565 f->already_hscrolled_p = 0;
11566 }
11567 }
11568
11569 retry:
11570 if (!EQ (old_frame, selected_frame)
11571 && FRAME_LIVE_P (XFRAME (old_frame)))
11572 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11573 selected_frame and selected_window to be temporarily out-of-sync so
11574 when we come back here via `goto retry', we need to resync because we
11575 may need to run Elisp code (via prepare_menu_bars). */
11576 select_frame_for_redisplay (old_frame);
11577
11578 pause = 0;
11579 reconsider_clip_changes (w, current_buffer);
11580 last_escape_glyph_frame = NULL;
11581 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11582
11583 /* If new fonts have been loaded that make a glyph matrix adjustment
11584 necessary, do it. */
11585 if (fonts_changed_p)
11586 {
11587 adjust_glyphs (NULL);
11588 ++windows_or_buffers_changed;
11589 fonts_changed_p = 0;
11590 }
11591
11592 /* If face_change_count is non-zero, init_iterator will free all
11593 realized faces, which includes the faces referenced from current
11594 matrices. So, we can't reuse current matrices in this case. */
11595 if (face_change_count)
11596 ++windows_or_buffers_changed;
11597
11598 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11599 && FRAME_TTY (sf)->previous_frame != sf)
11600 {
11601 /* Since frames on a single ASCII terminal share the same
11602 display area, displaying a different frame means redisplay
11603 the whole thing. */
11604 windows_or_buffers_changed++;
11605 SET_FRAME_GARBAGED (sf);
11606 #ifndef DOS_NT
11607 set_tty_color_mode (FRAME_TTY (sf), sf);
11608 #endif
11609 FRAME_TTY (sf)->previous_frame = sf;
11610 }
11611
11612 /* Set the visible flags for all frames. Do this before checking
11613 for resized or garbaged frames; they want to know if their frames
11614 are visible. See the comment in frame.h for
11615 FRAME_SAMPLE_VISIBILITY. */
11616 {
11617 Lisp_Object tail, frame;
11618
11619 number_of_visible_frames = 0;
11620
11621 FOR_EACH_FRAME (tail, frame)
11622 {
11623 struct frame *f = XFRAME (frame);
11624
11625 FRAME_SAMPLE_VISIBILITY (f);
11626 if (FRAME_VISIBLE_P (f))
11627 ++number_of_visible_frames;
11628 clear_desired_matrices (f);
11629 }
11630 }
11631
11632 /* Notice any pending interrupt request to change frame size. */
11633 do_pending_window_change (1);
11634
11635 /* Clear frames marked as garbaged. */
11636 if (frame_garbaged)
11637 clear_garbaged_frames ();
11638
11639 /* Build menubar and tool-bar items. */
11640 if (NILP (Vmemory_full))
11641 prepare_menu_bars ();
11642
11643 if (windows_or_buffers_changed)
11644 update_mode_lines++;
11645
11646 /* Detect case that we need to write or remove a star in the mode line. */
11647 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11648 {
11649 w->update_mode_line = Qt;
11650 if (buffer_shared > 1)
11651 update_mode_lines++;
11652 }
11653
11654 /* Avoid invocation of point motion hooks by `current_column' below. */
11655 count1 = SPECPDL_INDEX ();
11656 specbind (Qinhibit_point_motion_hooks, Qt);
11657
11658 /* If %c is in the mode line, update it if needed. */
11659 if (!NILP (w->column_number_displayed)
11660 /* This alternative quickly identifies a common case
11661 where no change is needed. */
11662 && !(PT == XFASTINT (w->last_point)
11663 && XFASTINT (w->last_modified) >= MODIFF
11664 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11665 && (XFASTINT (w->column_number_displayed)
11666 != (int) current_column ())) /* iftc */
11667 w->update_mode_line = Qt;
11668
11669 unbind_to (count1, Qnil);
11670
11671 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11672
11673 /* The variable buffer_shared is set in redisplay_window and
11674 indicates that we redisplay a buffer in different windows. See
11675 there. */
11676 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11677 || cursor_type_changed);
11678
11679 /* If specs for an arrow have changed, do thorough redisplay
11680 to ensure we remove any arrow that should no longer exist. */
11681 if (overlay_arrows_changed_p ())
11682 consider_all_windows_p = windows_or_buffers_changed = 1;
11683
11684 /* Normally the message* functions will have already displayed and
11685 updated the echo area, but the frame may have been trashed, or
11686 the update may have been preempted, so display the echo area
11687 again here. Checking message_cleared_p captures the case that
11688 the echo area should be cleared. */
11689 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11690 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11691 || (message_cleared_p
11692 && minibuf_level == 0
11693 /* If the mini-window is currently selected, this means the
11694 echo-area doesn't show through. */
11695 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11696 {
11697 int window_height_changed_p = echo_area_display (0);
11698 must_finish = 1;
11699
11700 /* If we don't display the current message, don't clear the
11701 message_cleared_p flag, because, if we did, we wouldn't clear
11702 the echo area in the next redisplay which doesn't preserve
11703 the echo area. */
11704 if (!display_last_displayed_message_p)
11705 message_cleared_p = 0;
11706
11707 if (fonts_changed_p)
11708 goto retry;
11709 else if (window_height_changed_p)
11710 {
11711 consider_all_windows_p = 1;
11712 ++update_mode_lines;
11713 ++windows_or_buffers_changed;
11714
11715 /* If window configuration was changed, frames may have been
11716 marked garbaged. Clear them or we will experience
11717 surprises wrt scrolling. */
11718 if (frame_garbaged)
11719 clear_garbaged_frames ();
11720 }
11721 }
11722 else if (EQ (selected_window, minibuf_window)
11723 && (current_buffer->clip_changed
11724 || XFASTINT (w->last_modified) < MODIFF
11725 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11726 && resize_mini_window (w, 0))
11727 {
11728 /* Resized active mini-window to fit the size of what it is
11729 showing if its contents might have changed. */
11730 must_finish = 1;
11731 /* FIXME: this causes all frames to be updated, which seems unnecessary
11732 since only the current frame needs to be considered. This function needs
11733 to be rewritten with two variables, consider_all_windows and
11734 consider_all_frames. */
11735 consider_all_windows_p = 1;
11736 ++windows_or_buffers_changed;
11737 ++update_mode_lines;
11738
11739 /* If window configuration was changed, frames may have been
11740 marked garbaged. Clear them or we will experience
11741 surprises wrt scrolling. */
11742 if (frame_garbaged)
11743 clear_garbaged_frames ();
11744 }
11745
11746
11747 /* If showing the region, and mark has changed, we must redisplay
11748 the whole window. The assignment to this_line_start_pos prevents
11749 the optimization directly below this if-statement. */
11750 if (((!NILP (Vtransient_mark_mode)
11751 && !NILP (XBUFFER (w->buffer)->mark_active))
11752 != !NILP (w->region_showing))
11753 || (!NILP (w->region_showing)
11754 && !EQ (w->region_showing,
11755 Fmarker_position (XBUFFER (w->buffer)->mark))))
11756 CHARPOS (this_line_start_pos) = 0;
11757
11758 /* Optimize the case that only the line containing the cursor in the
11759 selected window has changed. Variables starting with this_ are
11760 set in display_line and record information about the line
11761 containing the cursor. */
11762 tlbufpos = this_line_start_pos;
11763 tlendpos = this_line_end_pos;
11764 if (!consider_all_windows_p
11765 && CHARPOS (tlbufpos) > 0
11766 && NILP (w->update_mode_line)
11767 && !current_buffer->clip_changed
11768 && !current_buffer->prevent_redisplay_optimizations_p
11769 && FRAME_VISIBLE_P (XFRAME (w->frame))
11770 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11771 /* Make sure recorded data applies to current buffer, etc. */
11772 && this_line_buffer == current_buffer
11773 && current_buffer == XBUFFER (w->buffer)
11774 && NILP (w->force_start)
11775 && NILP (w->optional_new_start)
11776 /* Point must be on the line that we have info recorded about. */
11777 && PT >= CHARPOS (tlbufpos)
11778 && PT <= Z - CHARPOS (tlendpos)
11779 /* All text outside that line, including its final newline,
11780 must be unchanged. */
11781 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11782 CHARPOS (tlendpos)))
11783 {
11784 if (CHARPOS (tlbufpos) > BEGV
11785 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11786 && (CHARPOS (tlbufpos) == ZV
11787 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11788 /* Former continuation line has disappeared by becoming empty. */
11789 goto cancel;
11790 else if (XFASTINT (w->last_modified) < MODIFF
11791 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11792 || MINI_WINDOW_P (w))
11793 {
11794 /* We have to handle the case of continuation around a
11795 wide-column character (see the comment in indent.c around
11796 line 1340).
11797
11798 For instance, in the following case:
11799
11800 -------- Insert --------
11801 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11802 J_I_ ==> J_I_ `^^' are cursors.
11803 ^^ ^^
11804 -------- --------
11805
11806 As we have to redraw the line above, we cannot use this
11807 optimization. */
11808
11809 struct it it;
11810 int line_height_before = this_line_pixel_height;
11811
11812 /* Note that start_display will handle the case that the
11813 line starting at tlbufpos is a continuation line. */
11814 start_display (&it, w, tlbufpos);
11815
11816 /* Implementation note: It this still necessary? */
11817 if (it.current_x != this_line_start_x)
11818 goto cancel;
11819
11820 TRACE ((stderr, "trying display optimization 1\n"));
11821 w->cursor.vpos = -1;
11822 overlay_arrow_seen = 0;
11823 it.vpos = this_line_vpos;
11824 it.current_y = this_line_y;
11825 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11826 display_line (&it);
11827
11828 /* If line contains point, is not continued,
11829 and ends at same distance from eob as before, we win. */
11830 if (w->cursor.vpos >= 0
11831 /* Line is not continued, otherwise this_line_start_pos
11832 would have been set to 0 in display_line. */
11833 && CHARPOS (this_line_start_pos)
11834 /* Line ends as before. */
11835 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11836 /* Line has same height as before. Otherwise other lines
11837 would have to be shifted up or down. */
11838 && this_line_pixel_height == line_height_before)
11839 {
11840 /* If this is not the window's last line, we must adjust
11841 the charstarts of the lines below. */
11842 if (it.current_y < it.last_visible_y)
11843 {
11844 struct glyph_row *row
11845 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11846 EMACS_INT delta, delta_bytes;
11847
11848 /* We used to distinguish between two cases here,
11849 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11850 when the line ends in a newline or the end of the
11851 buffer's accessible portion. But both cases did
11852 the same, so they were collapsed. */
11853 delta = (Z
11854 - CHARPOS (tlendpos)
11855 - MATRIX_ROW_START_CHARPOS (row));
11856 delta_bytes = (Z_BYTE
11857 - BYTEPOS (tlendpos)
11858 - MATRIX_ROW_START_BYTEPOS (row));
11859
11860 increment_matrix_positions (w->current_matrix,
11861 this_line_vpos + 1,
11862 w->current_matrix->nrows,
11863 delta, delta_bytes);
11864 }
11865
11866 /* If this row displays text now but previously didn't,
11867 or vice versa, w->window_end_vpos may have to be
11868 adjusted. */
11869 if ((it.glyph_row - 1)->displays_text_p)
11870 {
11871 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11872 XSETINT (w->window_end_vpos, this_line_vpos);
11873 }
11874 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11875 && this_line_vpos > 0)
11876 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11877 w->window_end_valid = Qnil;
11878
11879 /* Update hint: No need to try to scroll in update_window. */
11880 w->desired_matrix->no_scrolling_p = 1;
11881
11882 #if GLYPH_DEBUG
11883 *w->desired_matrix->method = 0;
11884 debug_method_add (w, "optimization 1");
11885 #endif
11886 #ifdef HAVE_WINDOW_SYSTEM
11887 update_window_fringes (w, 0);
11888 #endif
11889 goto update;
11890 }
11891 else
11892 goto cancel;
11893 }
11894 else if (/* Cursor position hasn't changed. */
11895 PT == XFASTINT (w->last_point)
11896 /* Make sure the cursor was last displayed
11897 in this window. Otherwise we have to reposition it. */
11898 && 0 <= w->cursor.vpos
11899 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11900 {
11901 if (!must_finish)
11902 {
11903 do_pending_window_change (1);
11904
11905 /* We used to always goto end_of_redisplay here, but this
11906 isn't enough if we have a blinking cursor. */
11907 if (w->cursor_off_p == w->last_cursor_off_p)
11908 goto end_of_redisplay;
11909 }
11910 goto update;
11911 }
11912 /* If highlighting the region, or if the cursor is in the echo area,
11913 then we can't just move the cursor. */
11914 else if (! (!NILP (Vtransient_mark_mode)
11915 && !NILP (current_buffer->mark_active))
11916 && (EQ (selected_window, current_buffer->last_selected_window)
11917 || highlight_nonselected_windows)
11918 && NILP (w->region_showing)
11919 && NILP (Vshow_trailing_whitespace)
11920 && !cursor_in_echo_area)
11921 {
11922 struct it it;
11923 struct glyph_row *row;
11924
11925 /* Skip from tlbufpos to PT and see where it is. Note that
11926 PT may be in invisible text. If so, we will end at the
11927 next visible position. */
11928 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11929 NULL, DEFAULT_FACE_ID);
11930 it.current_x = this_line_start_x;
11931 it.current_y = this_line_y;
11932 it.vpos = this_line_vpos;
11933
11934 /* The call to move_it_to stops in front of PT, but
11935 moves over before-strings. */
11936 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11937
11938 if (it.vpos == this_line_vpos
11939 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11940 row->enabled_p))
11941 {
11942 xassert (this_line_vpos == it.vpos);
11943 xassert (this_line_y == it.current_y);
11944 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11945 #if GLYPH_DEBUG
11946 *w->desired_matrix->method = 0;
11947 debug_method_add (w, "optimization 3");
11948 #endif
11949 goto update;
11950 }
11951 else
11952 goto cancel;
11953 }
11954
11955 cancel:
11956 /* Text changed drastically or point moved off of line. */
11957 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11958 }
11959
11960 CHARPOS (this_line_start_pos) = 0;
11961 consider_all_windows_p |= buffer_shared > 1;
11962 ++clear_face_cache_count;
11963 #ifdef HAVE_WINDOW_SYSTEM
11964 ++clear_image_cache_count;
11965 #endif
11966
11967 /* Build desired matrices, and update the display. If
11968 consider_all_windows_p is non-zero, do it for all windows on all
11969 frames. Otherwise do it for selected_window, only. */
11970
11971 if (consider_all_windows_p)
11972 {
11973 Lisp_Object tail, frame;
11974
11975 FOR_EACH_FRAME (tail, frame)
11976 XFRAME (frame)->updated_p = 0;
11977
11978 /* Recompute # windows showing selected buffer. This will be
11979 incremented each time such a window is displayed. */
11980 buffer_shared = 0;
11981
11982 FOR_EACH_FRAME (tail, frame)
11983 {
11984 struct frame *f = XFRAME (frame);
11985
11986 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11987 {
11988 if (! EQ (frame, selected_frame))
11989 /* Select the frame, for the sake of frame-local
11990 variables. */
11991 select_frame_for_redisplay (frame);
11992
11993 /* Mark all the scroll bars to be removed; we'll redeem
11994 the ones we want when we redisplay their windows. */
11995 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11996 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11997
11998 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11999 redisplay_windows (FRAME_ROOT_WINDOW (f));
12000
12001 /* The X error handler may have deleted that frame. */
12002 if (!FRAME_LIVE_P (f))
12003 continue;
12004
12005 /* Any scroll bars which redisplay_windows should have
12006 nuked should now go away. */
12007 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12008 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12009
12010 /* If fonts changed, display again. */
12011 /* ??? rms: I suspect it is a mistake to jump all the way
12012 back to retry here. It should just retry this frame. */
12013 if (fonts_changed_p)
12014 goto retry;
12015
12016 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12017 {
12018 /* See if we have to hscroll. */
12019 if (!f->already_hscrolled_p)
12020 {
12021 f->already_hscrolled_p = 1;
12022 if (hscroll_windows (f->root_window))
12023 goto retry;
12024 }
12025
12026 /* Prevent various kinds of signals during display
12027 update. stdio is not robust about handling
12028 signals, which can cause an apparent I/O
12029 error. */
12030 if (interrupt_input)
12031 unrequest_sigio ();
12032 STOP_POLLING;
12033
12034 /* Update the display. */
12035 set_window_update_flags (XWINDOW (f->root_window), 1);
12036 pause |= update_frame (f, 0, 0);
12037 f->updated_p = 1;
12038 }
12039 }
12040 }
12041
12042 if (!EQ (old_frame, selected_frame)
12043 && FRAME_LIVE_P (XFRAME (old_frame)))
12044 /* We played a bit fast-and-loose above and allowed selected_frame
12045 and selected_window to be temporarily out-of-sync but let's make
12046 sure this stays contained. */
12047 select_frame_for_redisplay (old_frame);
12048 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12049
12050 if (!pause)
12051 {
12052 /* Do the mark_window_display_accurate after all windows have
12053 been redisplayed because this call resets flags in buffers
12054 which are needed for proper redisplay. */
12055 FOR_EACH_FRAME (tail, frame)
12056 {
12057 struct frame *f = XFRAME (frame);
12058 if (f->updated_p)
12059 {
12060 mark_window_display_accurate (f->root_window, 1);
12061 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12062 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12063 }
12064 }
12065 }
12066 }
12067 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12068 {
12069 Lisp_Object mini_window;
12070 struct frame *mini_frame;
12071
12072 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12073 /* Use list_of_error, not Qerror, so that
12074 we catch only errors and don't run the debugger. */
12075 internal_condition_case_1 (redisplay_window_1, selected_window,
12076 list_of_error,
12077 redisplay_window_error);
12078
12079 /* Compare desired and current matrices, perform output. */
12080
12081 update:
12082 /* If fonts changed, display again. */
12083 if (fonts_changed_p)
12084 goto retry;
12085
12086 /* Prevent various kinds of signals during display update.
12087 stdio is not robust about handling signals,
12088 which can cause an apparent I/O error. */
12089 if (interrupt_input)
12090 unrequest_sigio ();
12091 STOP_POLLING;
12092
12093 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12094 {
12095 if (hscroll_windows (selected_window))
12096 goto retry;
12097
12098 XWINDOW (selected_window)->must_be_updated_p = 1;
12099 pause = update_frame (sf, 0, 0);
12100 }
12101
12102 /* We may have called echo_area_display at the top of this
12103 function. If the echo area is on another frame, that may
12104 have put text on a frame other than the selected one, so the
12105 above call to update_frame would not have caught it. Catch
12106 it here. */
12107 mini_window = FRAME_MINIBUF_WINDOW (sf);
12108 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12109
12110 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12111 {
12112 XWINDOW (mini_window)->must_be_updated_p = 1;
12113 pause |= update_frame (mini_frame, 0, 0);
12114 if (!pause && hscroll_windows (mini_window))
12115 goto retry;
12116 }
12117 }
12118
12119 /* If display was paused because of pending input, make sure we do a
12120 thorough update the next time. */
12121 if (pause)
12122 {
12123 /* Prevent the optimization at the beginning of
12124 redisplay_internal that tries a single-line update of the
12125 line containing the cursor in the selected window. */
12126 CHARPOS (this_line_start_pos) = 0;
12127
12128 /* Let the overlay arrow be updated the next time. */
12129 update_overlay_arrows (0);
12130
12131 /* If we pause after scrolling, some rows in the current
12132 matrices of some windows are not valid. */
12133 if (!WINDOW_FULL_WIDTH_P (w)
12134 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12135 update_mode_lines = 1;
12136 }
12137 else
12138 {
12139 if (!consider_all_windows_p)
12140 {
12141 /* This has already been done above if
12142 consider_all_windows_p is set. */
12143 mark_window_display_accurate_1 (w, 1);
12144
12145 /* Say overlay arrows are up to date. */
12146 update_overlay_arrows (1);
12147
12148 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12149 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12150 }
12151
12152 update_mode_lines = 0;
12153 windows_or_buffers_changed = 0;
12154 cursor_type_changed = 0;
12155 }
12156
12157 /* Start SIGIO interrupts coming again. Having them off during the
12158 code above makes it less likely one will discard output, but not
12159 impossible, since there might be stuff in the system buffer here.
12160 But it is much hairier to try to do anything about that. */
12161 if (interrupt_input)
12162 request_sigio ();
12163 RESUME_POLLING;
12164
12165 /* If a frame has become visible which was not before, redisplay
12166 again, so that we display it. Expose events for such a frame
12167 (which it gets when becoming visible) don't call the parts of
12168 redisplay constructing glyphs, so simply exposing a frame won't
12169 display anything in this case. So, we have to display these
12170 frames here explicitly. */
12171 if (!pause)
12172 {
12173 Lisp_Object tail, frame;
12174 int new_count = 0;
12175
12176 FOR_EACH_FRAME (tail, frame)
12177 {
12178 int this_is_visible = 0;
12179
12180 if (XFRAME (frame)->visible)
12181 this_is_visible = 1;
12182 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12183 if (XFRAME (frame)->visible)
12184 this_is_visible = 1;
12185
12186 if (this_is_visible)
12187 new_count++;
12188 }
12189
12190 if (new_count != number_of_visible_frames)
12191 windows_or_buffers_changed++;
12192 }
12193
12194 /* Change frame size now if a change is pending. */
12195 do_pending_window_change (1);
12196
12197 /* If we just did a pending size change, or have additional
12198 visible frames, redisplay again. */
12199 if (windows_or_buffers_changed && !pause)
12200 goto retry;
12201
12202 /* Clear the face and image caches.
12203
12204 We used to do this only if consider_all_windows_p. But the cache
12205 needs to be cleared if a timer creates images in the current
12206 buffer (e.g. the test case in Bug#6230). */
12207
12208 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12209 {
12210 clear_face_cache (0);
12211 clear_face_cache_count = 0;
12212 }
12213
12214 #ifdef HAVE_WINDOW_SYSTEM
12215 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12216 {
12217 clear_image_caches (Qnil);
12218 clear_image_cache_count = 0;
12219 }
12220 #endif /* HAVE_WINDOW_SYSTEM */
12221
12222 end_of_redisplay:
12223 unbind_to (count, Qnil);
12224 RESUME_POLLING;
12225 }
12226
12227
12228 /* Redisplay, but leave alone any recent echo area message unless
12229 another message has been requested in its place.
12230
12231 This is useful in situations where you need to redisplay but no
12232 user action has occurred, making it inappropriate for the message
12233 area to be cleared. See tracking_off and
12234 wait_reading_process_output for examples of these situations.
12235
12236 FROM_WHERE is an integer saying from where this function was
12237 called. This is useful for debugging. */
12238
12239 void
12240 redisplay_preserve_echo_area (int from_where)
12241 {
12242 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12243
12244 if (!NILP (echo_area_buffer[1]))
12245 {
12246 /* We have a previously displayed message, but no current
12247 message. Redisplay the previous message. */
12248 display_last_displayed_message_p = 1;
12249 redisplay_internal (1);
12250 display_last_displayed_message_p = 0;
12251 }
12252 else
12253 redisplay_internal (1);
12254
12255 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12256 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12257 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12258 }
12259
12260
12261 /* Function registered with record_unwind_protect in
12262 redisplay_internal. Reset redisplaying_p to the value it had
12263 before redisplay_internal was called, and clear
12264 prevent_freeing_realized_faces_p. It also selects the previously
12265 selected frame, unless it has been deleted (by an X connection
12266 failure during redisplay, for example). */
12267
12268 static Lisp_Object
12269 unwind_redisplay (Lisp_Object val)
12270 {
12271 Lisp_Object old_redisplaying_p, old_frame;
12272
12273 old_redisplaying_p = XCAR (val);
12274 redisplaying_p = XFASTINT (old_redisplaying_p);
12275 old_frame = XCDR (val);
12276 if (! EQ (old_frame, selected_frame)
12277 && FRAME_LIVE_P (XFRAME (old_frame)))
12278 select_frame_for_redisplay (old_frame);
12279 return Qnil;
12280 }
12281
12282
12283 /* Mark the display of window W as accurate or inaccurate. If
12284 ACCURATE_P is non-zero mark display of W as accurate. If
12285 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12286 redisplay_internal is called. */
12287
12288 static void
12289 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12290 {
12291 if (BUFFERP (w->buffer))
12292 {
12293 struct buffer *b = XBUFFER (w->buffer);
12294
12295 w->last_modified
12296 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12297 w->last_overlay_modified
12298 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12299 w->last_had_star
12300 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12301
12302 if (accurate_p)
12303 {
12304 b->clip_changed = 0;
12305 b->prevent_redisplay_optimizations_p = 0;
12306
12307 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12308 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12309 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12310 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12311
12312 w->current_matrix->buffer = b;
12313 w->current_matrix->begv = BUF_BEGV (b);
12314 w->current_matrix->zv = BUF_ZV (b);
12315
12316 w->last_cursor = w->cursor;
12317 w->last_cursor_off_p = w->cursor_off_p;
12318
12319 if (w == XWINDOW (selected_window))
12320 w->last_point = make_number (BUF_PT (b));
12321 else
12322 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12323 }
12324 }
12325
12326 if (accurate_p)
12327 {
12328 w->window_end_valid = w->buffer;
12329 w->update_mode_line = Qnil;
12330 }
12331 }
12332
12333
12334 /* Mark the display of windows in the window tree rooted at WINDOW as
12335 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12336 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12337 be redisplayed the next time redisplay_internal is called. */
12338
12339 void
12340 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12341 {
12342 struct window *w;
12343
12344 for (; !NILP (window); window = w->next)
12345 {
12346 w = XWINDOW (window);
12347 mark_window_display_accurate_1 (w, accurate_p);
12348
12349 if (!NILP (w->vchild))
12350 mark_window_display_accurate (w->vchild, accurate_p);
12351 if (!NILP (w->hchild))
12352 mark_window_display_accurate (w->hchild, accurate_p);
12353 }
12354
12355 if (accurate_p)
12356 {
12357 update_overlay_arrows (1);
12358 }
12359 else
12360 {
12361 /* Force a thorough redisplay the next time by setting
12362 last_arrow_position and last_arrow_string to t, which is
12363 unequal to any useful value of Voverlay_arrow_... */
12364 update_overlay_arrows (-1);
12365 }
12366 }
12367
12368
12369 /* Return value in display table DP (Lisp_Char_Table *) for character
12370 C. Since a display table doesn't have any parent, we don't have to
12371 follow parent. Do not call this function directly but use the
12372 macro DISP_CHAR_VECTOR. */
12373
12374 Lisp_Object
12375 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12376 {
12377 Lisp_Object val;
12378
12379 if (ASCII_CHAR_P (c))
12380 {
12381 val = dp->ascii;
12382 if (SUB_CHAR_TABLE_P (val))
12383 val = XSUB_CHAR_TABLE (val)->contents[c];
12384 }
12385 else
12386 {
12387 Lisp_Object table;
12388
12389 XSETCHAR_TABLE (table, dp);
12390 val = char_table_ref (table, c);
12391 }
12392 if (NILP (val))
12393 val = dp->defalt;
12394 return val;
12395 }
12396
12397
12398 \f
12399 /***********************************************************************
12400 Window Redisplay
12401 ***********************************************************************/
12402
12403 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12404
12405 static void
12406 redisplay_windows (Lisp_Object window)
12407 {
12408 while (!NILP (window))
12409 {
12410 struct window *w = XWINDOW (window);
12411
12412 if (!NILP (w->hchild))
12413 redisplay_windows (w->hchild);
12414 else if (!NILP (w->vchild))
12415 redisplay_windows (w->vchild);
12416 else if (!NILP (w->buffer))
12417 {
12418 displayed_buffer = XBUFFER (w->buffer);
12419 /* Use list_of_error, not Qerror, so that
12420 we catch only errors and don't run the debugger. */
12421 internal_condition_case_1 (redisplay_window_0, window,
12422 list_of_error,
12423 redisplay_window_error);
12424 }
12425
12426 window = w->next;
12427 }
12428 }
12429
12430 static Lisp_Object
12431 redisplay_window_error (Lisp_Object ignore)
12432 {
12433 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12434 return Qnil;
12435 }
12436
12437 static Lisp_Object
12438 redisplay_window_0 (Lisp_Object window)
12439 {
12440 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12441 redisplay_window (window, 0);
12442 return Qnil;
12443 }
12444
12445 static Lisp_Object
12446 redisplay_window_1 (Lisp_Object window)
12447 {
12448 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12449 redisplay_window (window, 1);
12450 return Qnil;
12451 }
12452 \f
12453
12454 /* Increment GLYPH until it reaches END or CONDITION fails while
12455 adding (GLYPH)->pixel_width to X. */
12456
12457 #define SKIP_GLYPHS(glyph, end, x, condition) \
12458 do \
12459 { \
12460 (x) += (glyph)->pixel_width; \
12461 ++(glyph); \
12462 } \
12463 while ((glyph) < (end) && (condition))
12464
12465
12466 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12467 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12468 which positions recorded in ROW differ from current buffer
12469 positions.
12470
12471 Return 0 if cursor is not on this row, 1 otherwise. */
12472
12473 int
12474 set_cursor_from_row (struct window *w, struct glyph_row *row,
12475 struct glyph_matrix *matrix,
12476 EMACS_INT delta, EMACS_INT delta_bytes,
12477 int dy, int dvpos)
12478 {
12479 struct glyph *glyph = row->glyphs[TEXT_AREA];
12480 struct glyph *end = glyph + row->used[TEXT_AREA];
12481 struct glyph *cursor = NULL;
12482 /* The last known character position in row. */
12483 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12484 int x = row->x;
12485 EMACS_INT pt_old = PT - delta;
12486 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12487 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12488 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12489 /* A glyph beyond the edge of TEXT_AREA which we should never
12490 touch. */
12491 struct glyph *glyphs_end = end;
12492 /* Non-zero means we've found a match for cursor position, but that
12493 glyph has the avoid_cursor_p flag set. */
12494 int match_with_avoid_cursor = 0;
12495 /* Non-zero means we've seen at least one glyph that came from a
12496 display string. */
12497 int string_seen = 0;
12498 /* Largest and smalles buffer positions seen so far during scan of
12499 glyph row. */
12500 EMACS_INT bpos_max = pos_before;
12501 EMACS_INT bpos_min = pos_after;
12502 /* Last buffer position covered by an overlay string with an integer
12503 `cursor' property. */
12504 EMACS_INT bpos_covered = 0;
12505
12506 /* Skip over glyphs not having an object at the start and the end of
12507 the row. These are special glyphs like truncation marks on
12508 terminal frames. */
12509 if (row->displays_text_p)
12510 {
12511 if (!row->reversed_p)
12512 {
12513 while (glyph < end
12514 && INTEGERP (glyph->object)
12515 && glyph->charpos < 0)
12516 {
12517 x += glyph->pixel_width;
12518 ++glyph;
12519 }
12520 while (end > glyph
12521 && INTEGERP ((end - 1)->object)
12522 /* CHARPOS is zero for blanks and stretch glyphs
12523 inserted by extend_face_to_end_of_line. */
12524 && (end - 1)->charpos <= 0)
12525 --end;
12526 glyph_before = glyph - 1;
12527 glyph_after = end;
12528 }
12529 else
12530 {
12531 struct glyph *g;
12532
12533 /* If the glyph row is reversed, we need to process it from back
12534 to front, so swap the edge pointers. */
12535 glyphs_end = end = glyph - 1;
12536 glyph += row->used[TEXT_AREA] - 1;
12537
12538 while (glyph > end + 1
12539 && INTEGERP (glyph->object)
12540 && glyph->charpos < 0)
12541 {
12542 --glyph;
12543 x -= glyph->pixel_width;
12544 }
12545 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12546 --glyph;
12547 /* By default, in reversed rows we put the cursor on the
12548 rightmost (first in the reading order) glyph. */
12549 for (g = end + 1; g < glyph; g++)
12550 x += g->pixel_width;
12551 while (end < glyph
12552 && INTEGERP ((end + 1)->object)
12553 && (end + 1)->charpos <= 0)
12554 ++end;
12555 glyph_before = glyph + 1;
12556 glyph_after = end;
12557 }
12558 }
12559 else if (row->reversed_p)
12560 {
12561 /* In R2L rows that don't display text, put the cursor on the
12562 rightmost glyph. Case in point: an empty last line that is
12563 part of an R2L paragraph. */
12564 cursor = end - 1;
12565 /* Avoid placing the cursor on the last glyph of the row, where
12566 on terminal frames we hold the vertical border between
12567 adjacent windows. */
12568 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12569 && !WINDOW_RIGHTMOST_P (w)
12570 && cursor == row->glyphs[LAST_AREA] - 1)
12571 cursor--;
12572 x = -1; /* will be computed below, at label compute_x */
12573 }
12574
12575 /* Step 1: Try to find the glyph whose character position
12576 corresponds to point. If that's not possible, find 2 glyphs
12577 whose character positions are the closest to point, one before
12578 point, the other after it. */
12579 if (!row->reversed_p)
12580 while (/* not marched to end of glyph row */
12581 glyph < end
12582 /* glyph was not inserted by redisplay for internal purposes */
12583 && !INTEGERP (glyph->object))
12584 {
12585 if (BUFFERP (glyph->object))
12586 {
12587 EMACS_INT dpos = glyph->charpos - pt_old;
12588
12589 if (glyph->charpos > bpos_max)
12590 bpos_max = glyph->charpos;
12591 if (glyph->charpos < bpos_min)
12592 bpos_min = glyph->charpos;
12593 if (!glyph->avoid_cursor_p)
12594 {
12595 /* If we hit point, we've found the glyph on which to
12596 display the cursor. */
12597 if (dpos == 0)
12598 {
12599 match_with_avoid_cursor = 0;
12600 break;
12601 }
12602 /* See if we've found a better approximation to
12603 POS_BEFORE or to POS_AFTER. Note that we want the
12604 first (leftmost) glyph of all those that are the
12605 closest from below, and the last (rightmost) of all
12606 those from above. */
12607 if (0 > dpos && dpos > pos_before - pt_old)
12608 {
12609 pos_before = glyph->charpos;
12610 glyph_before = glyph;
12611 }
12612 else if (0 < dpos && dpos <= pos_after - pt_old)
12613 {
12614 pos_after = glyph->charpos;
12615 glyph_after = glyph;
12616 }
12617 }
12618 else if (dpos == 0)
12619 match_with_avoid_cursor = 1;
12620 }
12621 else if (STRINGP (glyph->object))
12622 {
12623 Lisp_Object chprop;
12624 EMACS_INT glyph_pos = glyph->charpos;
12625
12626 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12627 glyph->object);
12628 if (INTEGERP (chprop))
12629 {
12630 bpos_covered = bpos_max + XINT (chprop);
12631 /* If the `cursor' property covers buffer positions up
12632 to and including point, we should display cursor on
12633 this glyph. Note that overlays and text properties
12634 with string values stop bidi reordering, so every
12635 buffer position to the left of the string is always
12636 smaller than any position to the right of the
12637 string. Therefore, if a `cursor' property on one
12638 of the string's characters has an integer value, we
12639 will break out of the loop below _before_ we get to
12640 the position match above. IOW, integer values of
12641 the `cursor' property override the "exact match for
12642 point" strategy of positioning the cursor. */
12643 /* Implementation note: bpos_max == pt_old when, e.g.,
12644 we are in an empty line, where bpos_max is set to
12645 MATRIX_ROW_START_CHARPOS, see above. */
12646 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12647 {
12648 cursor = glyph;
12649 break;
12650 }
12651 }
12652
12653 string_seen = 1;
12654 }
12655 x += glyph->pixel_width;
12656 ++glyph;
12657 }
12658 else if (glyph > end) /* row is reversed */
12659 while (!INTEGERP (glyph->object))
12660 {
12661 if (BUFFERP (glyph->object))
12662 {
12663 EMACS_INT dpos = glyph->charpos - pt_old;
12664
12665 if (glyph->charpos > bpos_max)
12666 bpos_max = glyph->charpos;
12667 if (glyph->charpos < bpos_min)
12668 bpos_min = glyph->charpos;
12669 if (!glyph->avoid_cursor_p)
12670 {
12671 if (dpos == 0)
12672 {
12673 match_with_avoid_cursor = 0;
12674 break;
12675 }
12676 if (0 > dpos && dpos > pos_before - pt_old)
12677 {
12678 pos_before = glyph->charpos;
12679 glyph_before = glyph;
12680 }
12681 else if (0 < dpos && dpos <= pos_after - pt_old)
12682 {
12683 pos_after = glyph->charpos;
12684 glyph_after = glyph;
12685 }
12686 }
12687 else if (dpos == 0)
12688 match_with_avoid_cursor = 1;
12689 }
12690 else if (STRINGP (glyph->object))
12691 {
12692 Lisp_Object chprop;
12693 EMACS_INT glyph_pos = glyph->charpos;
12694
12695 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12696 glyph->object);
12697 if (INTEGERP (chprop))
12698 {
12699 bpos_covered = bpos_max + XINT (chprop);
12700 /* If the `cursor' property covers buffer positions up
12701 to and including point, we should display cursor on
12702 this glyph. */
12703 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12704 {
12705 cursor = glyph;
12706 break;
12707 }
12708 }
12709 string_seen = 1;
12710 }
12711 --glyph;
12712 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12713 {
12714 x--; /* can't use any pixel_width */
12715 break;
12716 }
12717 x -= glyph->pixel_width;
12718 }
12719
12720 /* Step 2: If we didn't find an exact match for point, we need to
12721 look for a proper place to put the cursor among glyphs between
12722 GLYPH_BEFORE and GLYPH_AFTER. */
12723 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12724 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12725 && bpos_covered < pt_old)
12726 {
12727 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12728 {
12729 EMACS_INT ellipsis_pos;
12730
12731 /* Scan back over the ellipsis glyphs. */
12732 if (!row->reversed_p)
12733 {
12734 ellipsis_pos = (glyph - 1)->charpos;
12735 while (glyph > row->glyphs[TEXT_AREA]
12736 && (glyph - 1)->charpos == ellipsis_pos)
12737 glyph--, x -= glyph->pixel_width;
12738 /* That loop always goes one position too far, including
12739 the glyph before the ellipsis. So scan forward over
12740 that one. */
12741 x += glyph->pixel_width;
12742 glyph++;
12743 }
12744 else /* row is reversed */
12745 {
12746 ellipsis_pos = (glyph + 1)->charpos;
12747 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12748 && (glyph + 1)->charpos == ellipsis_pos)
12749 glyph++, x += glyph->pixel_width;
12750 x -= glyph->pixel_width;
12751 glyph--;
12752 }
12753 }
12754 else if (match_with_avoid_cursor
12755 /* A truncated row may not include PT among its
12756 character positions. Setting the cursor inside the
12757 scroll margin will trigger recalculation of hscroll
12758 in hscroll_window_tree. */
12759 || (row->truncated_on_left_p && pt_old < bpos_min)
12760 || (row->truncated_on_right_p && pt_old > bpos_max)
12761 /* Zero-width characters produce no glyphs. */
12762 || ((row->reversed_p
12763 ? glyph_after > glyphs_end
12764 : glyph_after < glyphs_end)
12765 && eabs (glyph_after - glyph_before) == 1))
12766 {
12767 cursor = glyph_after;
12768 x = -1;
12769 }
12770 else if (string_seen)
12771 {
12772 int incr = row->reversed_p ? -1 : +1;
12773
12774 /* Need to find the glyph that came out of a string which is
12775 present at point. That glyph is somewhere between
12776 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12777 positioned between POS_BEFORE and POS_AFTER in the
12778 buffer. */
12779 struct glyph *stop = glyph_after;
12780 EMACS_INT pos = pos_before;
12781
12782 x = -1;
12783 for (glyph = glyph_before + incr;
12784 row->reversed_p ? glyph > stop : glyph < stop; )
12785 {
12786
12787 /* Any glyphs that come from the buffer are here because
12788 of bidi reordering. Skip them, and only pay
12789 attention to glyphs that came from some string. */
12790 if (STRINGP (glyph->object))
12791 {
12792 Lisp_Object str;
12793 EMACS_INT tem;
12794
12795 str = glyph->object;
12796 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12797 if (tem == 0 /* from overlay */
12798 || pos <= tem)
12799 {
12800 /* If the string from which this glyph came is
12801 found in the buffer at point, then we've
12802 found the glyph we've been looking for. If
12803 it comes from an overlay (tem == 0), and it
12804 has the `cursor' property on one of its
12805 glyphs, record that glyph as a candidate for
12806 displaying the cursor. (As in the
12807 unidirectional version, we will display the
12808 cursor on the last candidate we find.) */
12809 if (tem == 0 || tem == pt_old)
12810 {
12811 /* The glyphs from this string could have
12812 been reordered. Find the one with the
12813 smallest string position. Or there could
12814 be a character in the string with the
12815 `cursor' property, which means display
12816 cursor on that character's glyph. */
12817 EMACS_INT strpos = glyph->charpos;
12818
12819 cursor = glyph;
12820 for (glyph += incr;
12821 (row->reversed_p ? glyph > stop : glyph < stop)
12822 && EQ (glyph->object, str);
12823 glyph += incr)
12824 {
12825 Lisp_Object cprop;
12826 EMACS_INT gpos = glyph->charpos;
12827
12828 cprop = Fget_char_property (make_number (gpos),
12829 Qcursor,
12830 glyph->object);
12831 if (!NILP (cprop))
12832 {
12833 cursor = glyph;
12834 break;
12835 }
12836 if (glyph->charpos < strpos)
12837 {
12838 strpos = glyph->charpos;
12839 cursor = glyph;
12840 }
12841 }
12842
12843 if (tem == pt_old)
12844 goto compute_x;
12845 }
12846 if (tem)
12847 pos = tem + 1; /* don't find previous instances */
12848 }
12849 /* This string is not what we want; skip all of the
12850 glyphs that came from it. */
12851 do
12852 glyph += incr;
12853 while ((row->reversed_p ? glyph > stop : glyph < stop)
12854 && EQ (glyph->object, str));
12855 }
12856 else
12857 glyph += incr;
12858 }
12859
12860 /* If we reached the end of the line, and END was from a string,
12861 the cursor is not on this line. */
12862 if (cursor == NULL
12863 && (row->reversed_p ? glyph <= end : glyph >= end)
12864 && STRINGP (end->object)
12865 && row->continued_p)
12866 return 0;
12867 }
12868 }
12869
12870 compute_x:
12871 if (cursor != NULL)
12872 glyph = cursor;
12873 if (x < 0)
12874 {
12875 struct glyph *g;
12876
12877 /* Need to compute x that corresponds to GLYPH. */
12878 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12879 {
12880 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12881 abort ();
12882 x += g->pixel_width;
12883 }
12884 }
12885
12886 /* ROW could be part of a continued line, which, under bidi
12887 reordering, might have other rows whose start and end charpos
12888 occlude point. Only set w->cursor if we found a better
12889 approximation to the cursor position than we have from previously
12890 examined candidate rows belonging to the same continued line. */
12891 if (/* we already have a candidate row */
12892 w->cursor.vpos >= 0
12893 /* that candidate is not the row we are processing */
12894 && MATRIX_ROW (matrix, w->cursor.vpos) != row
12895 /* the row we are processing is part of a continued line */
12896 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
12897 /* Make sure cursor.vpos specifies a row whose start and end
12898 charpos occlude point. This is because some callers of this
12899 function leave cursor.vpos at the row where the cursor was
12900 displayed during the last redisplay cycle. */
12901 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
12902 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
12903 {
12904 struct glyph *g1 =
12905 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
12906
12907 /* Don't consider glyphs that are outside TEXT_AREA. */
12908 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
12909 return 0;
12910 /* Keep the candidate whose buffer position is the closest to
12911 point. */
12912 if (/* previous candidate is a glyph in TEXT_AREA of that row */
12913 w->cursor.hpos >= 0
12914 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
12915 && BUFFERP (g1->object)
12916 && (g1->charpos == pt_old /* an exact match always wins */
12917 || (BUFFERP (glyph->object)
12918 && eabs (g1->charpos - pt_old)
12919 < eabs (glyph->charpos - pt_old))))
12920 return 0;
12921 /* If this candidate gives an exact match, use that. */
12922 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12923 /* Otherwise, keep the candidate that comes from a row
12924 spanning less buffer positions. This may win when one or
12925 both candidate positions are on glyphs that came from
12926 display strings, for which we cannot compare buffer
12927 positions. */
12928 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12929 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12930 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
12931 return 0;
12932 }
12933 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12934 w->cursor.x = x;
12935 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12936 w->cursor.y = row->y + dy;
12937
12938 if (w == XWINDOW (selected_window))
12939 {
12940 if (!row->continued_p
12941 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12942 && row->x == 0)
12943 {
12944 this_line_buffer = XBUFFER (w->buffer);
12945
12946 CHARPOS (this_line_start_pos)
12947 = MATRIX_ROW_START_CHARPOS (row) + delta;
12948 BYTEPOS (this_line_start_pos)
12949 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12950
12951 CHARPOS (this_line_end_pos)
12952 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12953 BYTEPOS (this_line_end_pos)
12954 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12955
12956 this_line_y = w->cursor.y;
12957 this_line_pixel_height = row->height;
12958 this_line_vpos = w->cursor.vpos;
12959 this_line_start_x = row->x;
12960 }
12961 else
12962 CHARPOS (this_line_start_pos) = 0;
12963 }
12964
12965 return 1;
12966 }
12967
12968
12969 /* Run window scroll functions, if any, for WINDOW with new window
12970 start STARTP. Sets the window start of WINDOW to that position.
12971
12972 We assume that the window's buffer is really current. */
12973
12974 static INLINE struct text_pos
12975 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
12976 {
12977 struct window *w = XWINDOW (window);
12978 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12979
12980 if (current_buffer != XBUFFER (w->buffer))
12981 abort ();
12982
12983 if (!NILP (Vwindow_scroll_functions))
12984 {
12985 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12986 make_number (CHARPOS (startp)));
12987 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12988 /* In case the hook functions switch buffers. */
12989 if (current_buffer != XBUFFER (w->buffer))
12990 set_buffer_internal_1 (XBUFFER (w->buffer));
12991 }
12992
12993 return startp;
12994 }
12995
12996
12997 /* Make sure the line containing the cursor is fully visible.
12998 A value of 1 means there is nothing to be done.
12999 (Either the line is fully visible, or it cannot be made so,
13000 or we cannot tell.)
13001
13002 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13003 is higher than window.
13004
13005 A value of 0 means the caller should do scrolling
13006 as if point had gone off the screen. */
13007
13008 static int
13009 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13010 {
13011 struct glyph_matrix *matrix;
13012 struct glyph_row *row;
13013 int window_height;
13014
13015 if (!make_cursor_line_fully_visible_p)
13016 return 1;
13017
13018 /* It's not always possible to find the cursor, e.g, when a window
13019 is full of overlay strings. Don't do anything in that case. */
13020 if (w->cursor.vpos < 0)
13021 return 1;
13022
13023 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13024 row = MATRIX_ROW (matrix, w->cursor.vpos);
13025
13026 /* If the cursor row is not partially visible, there's nothing to do. */
13027 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13028 return 1;
13029
13030 /* If the row the cursor is in is taller than the window's height,
13031 it's not clear what to do, so do nothing. */
13032 window_height = window_box_height (w);
13033 if (row->height >= window_height)
13034 {
13035 if (!force_p || MINI_WINDOW_P (w)
13036 || w->vscroll || w->cursor.vpos == 0)
13037 return 1;
13038 }
13039 return 0;
13040 }
13041
13042
13043 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13044 non-zero means only WINDOW is redisplayed in redisplay_internal.
13045 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13046 in redisplay_window to bring a partially visible line into view in
13047 the case that only the cursor has moved.
13048
13049 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13050 last screen line's vertical height extends past the end of the screen.
13051
13052 Value is
13053
13054 1 if scrolling succeeded
13055
13056 0 if scrolling didn't find point.
13057
13058 -1 if new fonts have been loaded so that we must interrupt
13059 redisplay, adjust glyph matrices, and try again. */
13060
13061 enum
13062 {
13063 SCROLLING_SUCCESS,
13064 SCROLLING_FAILED,
13065 SCROLLING_NEED_LARGER_MATRICES
13066 };
13067
13068 static int
13069 try_scrolling (Lisp_Object window, int just_this_one_p,
13070 EMACS_INT scroll_conservatively, EMACS_INT scroll_step,
13071 int temp_scroll_step, int last_line_misfit)
13072 {
13073 struct window *w = XWINDOW (window);
13074 struct frame *f = XFRAME (w->frame);
13075 struct text_pos pos, startp;
13076 struct it it;
13077 int this_scroll_margin, scroll_max, rc, height;
13078 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13079 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13080 Lisp_Object aggressive;
13081 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13082
13083 #if GLYPH_DEBUG
13084 debug_method_add (w, "try_scrolling");
13085 #endif
13086
13087 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13088
13089 /* Compute scroll margin height in pixels. We scroll when point is
13090 within this distance from the top or bottom of the window. */
13091 if (scroll_margin > 0)
13092 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13093 * FRAME_LINE_HEIGHT (f);
13094 else
13095 this_scroll_margin = 0;
13096
13097 /* Force scroll_conservatively to have a reasonable value, to avoid
13098 overflow while computing how much to scroll. Note that the user
13099 can supply scroll-conservatively equal to `most-positive-fixnum',
13100 which can be larger than INT_MAX. */
13101 if (scroll_conservatively > scroll_limit)
13102 {
13103 scroll_conservatively = scroll_limit;
13104 scroll_max = INT_MAX;
13105 }
13106 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13107 /* Compute how much we should try to scroll maximally to bring
13108 point into view. */
13109 scroll_max = (max (scroll_step,
13110 max (scroll_conservatively, temp_scroll_step))
13111 * FRAME_LINE_HEIGHT (f));
13112 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13113 || NUMBERP (current_buffer->scroll_up_aggressively))
13114 /* We're trying to scroll because of aggressive scrolling but no
13115 scroll_step is set. Choose an arbitrary one. */
13116 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13117 else
13118 scroll_max = 0;
13119
13120 too_near_end:
13121
13122 /* Decide whether to scroll down. */
13123 if (PT > CHARPOS (startp))
13124 {
13125 int scroll_margin_y;
13126
13127 /* Compute the pixel ypos of the scroll margin, then move it to
13128 either that ypos or PT, whichever comes first. */
13129 start_display (&it, w, startp);
13130 scroll_margin_y = it.last_visible_y - this_scroll_margin
13131 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13132 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13133 (MOVE_TO_POS | MOVE_TO_Y));
13134
13135 if (PT > CHARPOS (it.current.pos))
13136 {
13137 int y0 = line_bottom_y (&it);
13138 /* Compute how many pixels below window bottom to stop searching
13139 for PT. This avoids costly search for PT that is far away if
13140 the user limited scrolling by a small number of lines, but
13141 always finds PT if scroll_conservatively is set to a large
13142 number, such as most-positive-fixnum. */
13143 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13144 int y_to_move =
13145 slack >= INT_MAX - it.last_visible_y
13146 ? INT_MAX
13147 : it.last_visible_y + slack;
13148
13149 /* Compute the distance from the scroll margin to PT or to
13150 the scroll limit, whichever comes first. This should
13151 include the height of the cursor line, to make that line
13152 fully visible. */
13153 move_it_to (&it, PT, -1, y_to_move,
13154 -1, MOVE_TO_POS | MOVE_TO_Y);
13155 dy = line_bottom_y (&it) - y0;
13156
13157 if (dy > scroll_max)
13158 return SCROLLING_FAILED;
13159
13160 scroll_down_p = 1;
13161 }
13162 }
13163
13164 if (scroll_down_p)
13165 {
13166 /* Point is in or below the bottom scroll margin, so move the
13167 window start down. If scrolling conservatively, move it just
13168 enough down to make point visible. If scroll_step is set,
13169 move it down by scroll_step. */
13170 if (scroll_conservatively)
13171 amount_to_scroll
13172 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13173 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13174 else if (scroll_step || temp_scroll_step)
13175 amount_to_scroll = scroll_max;
13176 else
13177 {
13178 aggressive = current_buffer->scroll_up_aggressively;
13179 height = WINDOW_BOX_TEXT_HEIGHT (w);
13180 if (NUMBERP (aggressive))
13181 {
13182 double float_amount = XFLOATINT (aggressive) * height;
13183 amount_to_scroll = float_amount;
13184 if (amount_to_scroll == 0 && float_amount > 0)
13185 amount_to_scroll = 1;
13186 }
13187 }
13188
13189 if (amount_to_scroll <= 0)
13190 return SCROLLING_FAILED;
13191
13192 start_display (&it, w, startp);
13193 if (scroll_max < INT_MAX)
13194 move_it_vertically (&it, amount_to_scroll);
13195 else
13196 {
13197 /* Extra precision for users who set scroll-conservatively
13198 to most-positive-fixnum: make sure the amount we scroll
13199 the window start is never less than amount_to_scroll,
13200 which was computed as distance from window bottom to
13201 point. This matters when lines at window top and lines
13202 below window bottom have different height. */
13203 struct it it1 = it;
13204 /* We use a temporary it1 because line_bottom_y can modify
13205 its argument, if it moves one line down; see there. */
13206 int start_y = line_bottom_y (&it1);
13207
13208 do {
13209 move_it_by_lines (&it, 1, 1);
13210 it1 = it;
13211 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13212 }
13213
13214 /* If STARTP is unchanged, move it down another screen line. */
13215 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13216 move_it_by_lines (&it, 1, 1);
13217 startp = it.current.pos;
13218 }
13219 else
13220 {
13221 struct text_pos scroll_margin_pos = startp;
13222
13223 /* See if point is inside the scroll margin at the top of the
13224 window. */
13225 if (this_scroll_margin)
13226 {
13227 start_display (&it, w, startp);
13228 move_it_vertically (&it, this_scroll_margin);
13229 scroll_margin_pos = it.current.pos;
13230 }
13231
13232 if (PT < CHARPOS (scroll_margin_pos))
13233 {
13234 /* Point is in the scroll margin at the top of the window or
13235 above what is displayed in the window. */
13236 int y0;
13237
13238 /* Compute the vertical distance from PT to the scroll
13239 margin position. Give up if distance is greater than
13240 scroll_max. */
13241 SET_TEXT_POS (pos, PT, PT_BYTE);
13242 start_display (&it, w, pos);
13243 y0 = it.current_y;
13244 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13245 it.last_visible_y, -1,
13246 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13247 dy = it.current_y - y0;
13248 if (dy > scroll_max)
13249 return SCROLLING_FAILED;
13250
13251 /* Compute new window start. */
13252 start_display (&it, w, startp);
13253
13254 if (scroll_conservatively)
13255 amount_to_scroll
13256 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13257 else if (scroll_step || temp_scroll_step)
13258 amount_to_scroll = scroll_max;
13259 else
13260 {
13261 aggressive = current_buffer->scroll_down_aggressively;
13262 height = WINDOW_BOX_TEXT_HEIGHT (w);
13263 if (NUMBERP (aggressive))
13264 {
13265 double float_amount = XFLOATINT (aggressive) * height;
13266 amount_to_scroll = float_amount;
13267 if (amount_to_scroll == 0 && float_amount > 0)
13268 amount_to_scroll = 1;
13269 }
13270 }
13271
13272 if (amount_to_scroll <= 0)
13273 return SCROLLING_FAILED;
13274
13275 move_it_vertically_backward (&it, amount_to_scroll);
13276 startp = it.current.pos;
13277 }
13278 }
13279
13280 /* Run window scroll functions. */
13281 startp = run_window_scroll_functions (window, startp);
13282
13283 /* Display the window. Give up if new fonts are loaded, or if point
13284 doesn't appear. */
13285 if (!try_window (window, startp, 0))
13286 rc = SCROLLING_NEED_LARGER_MATRICES;
13287 else if (w->cursor.vpos < 0)
13288 {
13289 clear_glyph_matrix (w->desired_matrix);
13290 rc = SCROLLING_FAILED;
13291 }
13292 else
13293 {
13294 /* Maybe forget recorded base line for line number display. */
13295 if (!just_this_one_p
13296 || current_buffer->clip_changed
13297 || BEG_UNCHANGED < CHARPOS (startp))
13298 w->base_line_number = Qnil;
13299
13300 /* If cursor ends up on a partially visible line,
13301 treat that as being off the bottom of the screen. */
13302 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13303 {
13304 clear_glyph_matrix (w->desired_matrix);
13305 ++extra_scroll_margin_lines;
13306 goto too_near_end;
13307 }
13308 rc = SCROLLING_SUCCESS;
13309 }
13310
13311 return rc;
13312 }
13313
13314
13315 /* Compute a suitable window start for window W if display of W starts
13316 on a continuation line. Value is non-zero if a new window start
13317 was computed.
13318
13319 The new window start will be computed, based on W's width, starting
13320 from the start of the continued line. It is the start of the
13321 screen line with the minimum distance from the old start W->start. */
13322
13323 static int
13324 compute_window_start_on_continuation_line (struct window *w)
13325 {
13326 struct text_pos pos, start_pos;
13327 int window_start_changed_p = 0;
13328
13329 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13330
13331 /* If window start is on a continuation line... Window start may be
13332 < BEGV in case there's invisible text at the start of the
13333 buffer (M-x rmail, for example). */
13334 if (CHARPOS (start_pos) > BEGV
13335 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13336 {
13337 struct it it;
13338 struct glyph_row *row;
13339
13340 /* Handle the case that the window start is out of range. */
13341 if (CHARPOS (start_pos) < BEGV)
13342 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13343 else if (CHARPOS (start_pos) > ZV)
13344 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13345
13346 /* Find the start of the continued line. This should be fast
13347 because scan_buffer is fast (newline cache). */
13348 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13349 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13350 row, DEFAULT_FACE_ID);
13351 reseat_at_previous_visible_line_start (&it);
13352
13353 /* If the line start is "too far" away from the window start,
13354 say it takes too much time to compute a new window start. */
13355 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13356 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13357 {
13358 int min_distance, distance;
13359
13360 /* Move forward by display lines to find the new window
13361 start. If window width was enlarged, the new start can
13362 be expected to be > the old start. If window width was
13363 decreased, the new window start will be < the old start.
13364 So, we're looking for the display line start with the
13365 minimum distance from the old window start. */
13366 pos = it.current.pos;
13367 min_distance = INFINITY;
13368 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13369 distance < min_distance)
13370 {
13371 min_distance = distance;
13372 pos = it.current.pos;
13373 move_it_by_lines (&it, 1, 0);
13374 }
13375
13376 /* Set the window start there. */
13377 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13378 window_start_changed_p = 1;
13379 }
13380 }
13381
13382 return window_start_changed_p;
13383 }
13384
13385
13386 /* Try cursor movement in case text has not changed in window WINDOW,
13387 with window start STARTP. Value is
13388
13389 CURSOR_MOVEMENT_SUCCESS if successful
13390
13391 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13392
13393 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13394 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13395 we want to scroll as if scroll-step were set to 1. See the code.
13396
13397 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13398 which case we have to abort this redisplay, and adjust matrices
13399 first. */
13400
13401 enum
13402 {
13403 CURSOR_MOVEMENT_SUCCESS,
13404 CURSOR_MOVEMENT_CANNOT_BE_USED,
13405 CURSOR_MOVEMENT_MUST_SCROLL,
13406 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13407 };
13408
13409 static int
13410 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13411 {
13412 struct window *w = XWINDOW (window);
13413 struct frame *f = XFRAME (w->frame);
13414 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13415
13416 #if GLYPH_DEBUG
13417 if (inhibit_try_cursor_movement)
13418 return rc;
13419 #endif
13420
13421 /* Handle case where text has not changed, only point, and it has
13422 not moved off the frame. */
13423 if (/* Point may be in this window. */
13424 PT >= CHARPOS (startp)
13425 /* Selective display hasn't changed. */
13426 && !current_buffer->clip_changed
13427 /* Function force-mode-line-update is used to force a thorough
13428 redisplay. It sets either windows_or_buffers_changed or
13429 update_mode_lines. So don't take a shortcut here for these
13430 cases. */
13431 && !update_mode_lines
13432 && !windows_or_buffers_changed
13433 && !cursor_type_changed
13434 /* Can't use this case if highlighting a region. When a
13435 region exists, cursor movement has to do more than just
13436 set the cursor. */
13437 && !(!NILP (Vtransient_mark_mode)
13438 && !NILP (current_buffer->mark_active))
13439 && NILP (w->region_showing)
13440 && NILP (Vshow_trailing_whitespace)
13441 /* Right after splitting windows, last_point may be nil. */
13442 && INTEGERP (w->last_point)
13443 /* This code is not used for mini-buffer for the sake of the case
13444 of redisplaying to replace an echo area message; since in
13445 that case the mini-buffer contents per se are usually
13446 unchanged. This code is of no real use in the mini-buffer
13447 since the handling of this_line_start_pos, etc., in redisplay
13448 handles the same cases. */
13449 && !EQ (window, minibuf_window)
13450 /* When splitting windows or for new windows, it happens that
13451 redisplay is called with a nil window_end_vpos or one being
13452 larger than the window. This should really be fixed in
13453 window.c. I don't have this on my list, now, so we do
13454 approximately the same as the old redisplay code. --gerd. */
13455 && INTEGERP (w->window_end_vpos)
13456 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13457 && (FRAME_WINDOW_P (f)
13458 || !overlay_arrow_in_current_buffer_p ()))
13459 {
13460 int this_scroll_margin, top_scroll_margin;
13461 struct glyph_row *row = NULL;
13462
13463 #if GLYPH_DEBUG
13464 debug_method_add (w, "cursor movement");
13465 #endif
13466
13467 /* Scroll if point within this distance from the top or bottom
13468 of the window. This is a pixel value. */
13469 if (scroll_margin > 0)
13470 {
13471 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13472 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13473 }
13474 else
13475 this_scroll_margin = 0;
13476
13477 top_scroll_margin = this_scroll_margin;
13478 if (WINDOW_WANTS_HEADER_LINE_P (w))
13479 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13480
13481 /* Start with the row the cursor was displayed during the last
13482 not paused redisplay. Give up if that row is not valid. */
13483 if (w->last_cursor.vpos < 0
13484 || w->last_cursor.vpos >= w->current_matrix->nrows)
13485 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13486 else
13487 {
13488 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13489 if (row->mode_line_p)
13490 ++row;
13491 if (!row->enabled_p)
13492 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13493 }
13494
13495 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13496 {
13497 int scroll_p = 0, must_scroll = 0;
13498 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13499
13500 if (PT > XFASTINT (w->last_point))
13501 {
13502 /* Point has moved forward. */
13503 while (MATRIX_ROW_END_CHARPOS (row) < PT
13504 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13505 {
13506 xassert (row->enabled_p);
13507 ++row;
13508 }
13509
13510 /* If the end position of a row equals the start
13511 position of the next row, and PT is at that position,
13512 we would rather display cursor in the next line. */
13513 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13514 && MATRIX_ROW_END_CHARPOS (row) == PT
13515 && row < w->current_matrix->rows
13516 + w->current_matrix->nrows - 1
13517 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13518 && !cursor_row_p (w, row))
13519 ++row;
13520
13521 /* If within the scroll margin, scroll. Note that
13522 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13523 the next line would be drawn, and that
13524 this_scroll_margin can be zero. */
13525 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13526 || PT > MATRIX_ROW_END_CHARPOS (row)
13527 /* Line is completely visible last line in window
13528 and PT is to be set in the next line. */
13529 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13530 && PT == MATRIX_ROW_END_CHARPOS (row)
13531 && !row->ends_at_zv_p
13532 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13533 scroll_p = 1;
13534 }
13535 else if (PT < XFASTINT (w->last_point))
13536 {
13537 /* Cursor has to be moved backward. Note that PT >=
13538 CHARPOS (startp) because of the outer if-statement. */
13539 while (!row->mode_line_p
13540 && (MATRIX_ROW_START_CHARPOS (row) > PT
13541 || (MATRIX_ROW_START_CHARPOS (row) == PT
13542 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13543 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13544 row > w->current_matrix->rows
13545 && (row-1)->ends_in_newline_from_string_p))))
13546 && (row->y > top_scroll_margin
13547 || CHARPOS (startp) == BEGV))
13548 {
13549 xassert (row->enabled_p);
13550 --row;
13551 }
13552
13553 /* Consider the following case: Window starts at BEGV,
13554 there is invisible, intangible text at BEGV, so that
13555 display starts at some point START > BEGV. It can
13556 happen that we are called with PT somewhere between
13557 BEGV and START. Try to handle that case. */
13558 if (row < w->current_matrix->rows
13559 || row->mode_line_p)
13560 {
13561 row = w->current_matrix->rows;
13562 if (row->mode_line_p)
13563 ++row;
13564 }
13565
13566 /* Due to newlines in overlay strings, we may have to
13567 skip forward over overlay strings. */
13568 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13569 && MATRIX_ROW_END_CHARPOS (row) == PT
13570 && !cursor_row_p (w, row))
13571 ++row;
13572
13573 /* If within the scroll margin, scroll. */
13574 if (row->y < top_scroll_margin
13575 && CHARPOS (startp) != BEGV)
13576 scroll_p = 1;
13577 }
13578 else
13579 {
13580 /* Cursor did not move. So don't scroll even if cursor line
13581 is partially visible, as it was so before. */
13582 rc = CURSOR_MOVEMENT_SUCCESS;
13583 }
13584
13585 if (PT < MATRIX_ROW_START_CHARPOS (row)
13586 || PT > MATRIX_ROW_END_CHARPOS (row))
13587 {
13588 /* if PT is not in the glyph row, give up. */
13589 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13590 must_scroll = 1;
13591 }
13592 else if (rc != CURSOR_MOVEMENT_SUCCESS
13593 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13594 {
13595 /* If rows are bidi-reordered and point moved, back up
13596 until we find a row that does not belong to a
13597 continuation line. This is because we must consider
13598 all rows of a continued line as candidates for the
13599 new cursor positioning, since row start and end
13600 positions change non-linearly with vertical position
13601 in such rows. */
13602 /* FIXME: Revisit this when glyph ``spilling'' in
13603 continuation lines' rows is implemented for
13604 bidi-reordered rows. */
13605 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13606 {
13607 xassert (row->enabled_p);
13608 --row;
13609 /* If we hit the beginning of the displayed portion
13610 without finding the first row of a continued
13611 line, give up. */
13612 if (row <= w->current_matrix->rows)
13613 {
13614 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13615 break;
13616 }
13617
13618 }
13619 }
13620 if (must_scroll)
13621 ;
13622 else if (rc != CURSOR_MOVEMENT_SUCCESS
13623 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13624 && make_cursor_line_fully_visible_p)
13625 {
13626 if (PT == MATRIX_ROW_END_CHARPOS (row)
13627 && !row->ends_at_zv_p
13628 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13629 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13630 else if (row->height > window_box_height (w))
13631 {
13632 /* If we end up in a partially visible line, let's
13633 make it fully visible, except when it's taller
13634 than the window, in which case we can't do much
13635 about it. */
13636 *scroll_step = 1;
13637 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13638 }
13639 else
13640 {
13641 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13642 if (!cursor_row_fully_visible_p (w, 0, 1))
13643 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13644 else
13645 rc = CURSOR_MOVEMENT_SUCCESS;
13646 }
13647 }
13648 else if (scroll_p)
13649 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13650 else if (rc != CURSOR_MOVEMENT_SUCCESS
13651 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13652 {
13653 /* With bidi-reordered rows, there could be more than
13654 one candidate row whose start and end positions
13655 occlude point. We need to let set_cursor_from_row
13656 find the best candidate. */
13657 /* FIXME: Revisit this when glyph ``spilling'' in
13658 continuation lines' rows is implemented for
13659 bidi-reordered rows. */
13660 int rv = 0;
13661
13662 do
13663 {
13664 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13665 && PT <= MATRIX_ROW_END_CHARPOS (row)
13666 && cursor_row_p (w, row))
13667 rv |= set_cursor_from_row (w, row, w->current_matrix,
13668 0, 0, 0, 0);
13669 /* As soon as we've found the first suitable row
13670 whose ends_at_zv_p flag is set, we are done. */
13671 if (rv
13672 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13673 {
13674 rc = CURSOR_MOVEMENT_SUCCESS;
13675 break;
13676 }
13677 ++row;
13678 }
13679 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
13680 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13681 || (MATRIX_ROW_START_CHARPOS (row) == PT
13682 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
13683 /* If we didn't find any candidate rows, or exited the
13684 loop before all the candidates were examined, signal
13685 to the caller that this method failed. */
13686 if (rc != CURSOR_MOVEMENT_SUCCESS
13687 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
13688 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13689 else if (rv)
13690 rc = CURSOR_MOVEMENT_SUCCESS;
13691 }
13692 else
13693 {
13694 do
13695 {
13696 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13697 {
13698 rc = CURSOR_MOVEMENT_SUCCESS;
13699 break;
13700 }
13701 ++row;
13702 }
13703 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13704 && MATRIX_ROW_START_CHARPOS (row) == PT
13705 && cursor_row_p (w, row));
13706 }
13707 }
13708 }
13709
13710 return rc;
13711 }
13712
13713 void
13714 set_vertical_scroll_bar (struct window *w)
13715 {
13716 EMACS_INT start, end, whole;
13717
13718 /* Calculate the start and end positions for the current window.
13719 At some point, it would be nice to choose between scrollbars
13720 which reflect the whole buffer size, with special markers
13721 indicating narrowing, and scrollbars which reflect only the
13722 visible region.
13723
13724 Note that mini-buffers sometimes aren't displaying any text. */
13725 if (!MINI_WINDOW_P (w)
13726 || (w == XWINDOW (minibuf_window)
13727 && NILP (echo_area_buffer[0])))
13728 {
13729 struct buffer *buf = XBUFFER (w->buffer);
13730 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13731 start = marker_position (w->start) - BUF_BEGV (buf);
13732 /* I don't think this is guaranteed to be right. For the
13733 moment, we'll pretend it is. */
13734 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13735
13736 if (end < start)
13737 end = start;
13738 if (whole < (end - start))
13739 whole = end - start;
13740 }
13741 else
13742 start = end = whole = 0;
13743
13744 /* Indicate what this scroll bar ought to be displaying now. */
13745 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13746 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13747 (w, end - start, whole, start);
13748 }
13749
13750
13751 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13752 selected_window is redisplayed.
13753
13754 We can return without actually redisplaying the window if
13755 fonts_changed_p is nonzero. In that case, redisplay_internal will
13756 retry. */
13757
13758 static void
13759 redisplay_window (Lisp_Object window, int just_this_one_p)
13760 {
13761 struct window *w = XWINDOW (window);
13762 struct frame *f = XFRAME (w->frame);
13763 struct buffer *buffer = XBUFFER (w->buffer);
13764 struct buffer *old = current_buffer;
13765 struct text_pos lpoint, opoint, startp;
13766 int update_mode_line;
13767 int tem;
13768 struct it it;
13769 /* Record it now because it's overwritten. */
13770 int current_matrix_up_to_date_p = 0;
13771 int used_current_matrix_p = 0;
13772 /* This is less strict than current_matrix_up_to_date_p.
13773 It indictes that the buffer contents and narrowing are unchanged. */
13774 int buffer_unchanged_p = 0;
13775 int temp_scroll_step = 0;
13776 int count = SPECPDL_INDEX ();
13777 int rc;
13778 int centering_position = -1;
13779 int last_line_misfit = 0;
13780 EMACS_INT beg_unchanged, end_unchanged;
13781
13782 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13783 opoint = lpoint;
13784
13785 /* W must be a leaf window here. */
13786 xassert (!NILP (w->buffer));
13787 #if GLYPH_DEBUG
13788 *w->desired_matrix->method = 0;
13789 #endif
13790
13791 restart:
13792 reconsider_clip_changes (w, buffer);
13793
13794 /* Has the mode line to be updated? */
13795 update_mode_line = (!NILP (w->update_mode_line)
13796 || update_mode_lines
13797 || buffer->clip_changed
13798 || buffer->prevent_redisplay_optimizations_p);
13799
13800 if (MINI_WINDOW_P (w))
13801 {
13802 if (w == XWINDOW (echo_area_window)
13803 && !NILP (echo_area_buffer[0]))
13804 {
13805 if (update_mode_line)
13806 /* We may have to update a tty frame's menu bar or a
13807 tool-bar. Example `M-x C-h C-h C-g'. */
13808 goto finish_menu_bars;
13809 else
13810 /* We've already displayed the echo area glyphs in this window. */
13811 goto finish_scroll_bars;
13812 }
13813 else if ((w != XWINDOW (minibuf_window)
13814 || minibuf_level == 0)
13815 /* When buffer is nonempty, redisplay window normally. */
13816 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13817 /* Quail displays non-mini buffers in minibuffer window.
13818 In that case, redisplay the window normally. */
13819 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13820 {
13821 /* W is a mini-buffer window, but it's not active, so clear
13822 it. */
13823 int yb = window_text_bottom_y (w);
13824 struct glyph_row *row;
13825 int y;
13826
13827 for (y = 0, row = w->desired_matrix->rows;
13828 y < yb;
13829 y += row->height, ++row)
13830 blank_row (w, row, y);
13831 goto finish_scroll_bars;
13832 }
13833
13834 clear_glyph_matrix (w->desired_matrix);
13835 }
13836
13837 /* Otherwise set up data on this window; select its buffer and point
13838 value. */
13839 /* Really select the buffer, for the sake of buffer-local
13840 variables. */
13841 set_buffer_internal_1 (XBUFFER (w->buffer));
13842
13843 current_matrix_up_to_date_p
13844 = (!NILP (w->window_end_valid)
13845 && !current_buffer->clip_changed
13846 && !current_buffer->prevent_redisplay_optimizations_p
13847 && XFASTINT (w->last_modified) >= MODIFF
13848 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13849
13850 /* Run the window-bottom-change-functions
13851 if it is possible that the text on the screen has changed
13852 (either due to modification of the text, or any other reason). */
13853 if (!current_matrix_up_to_date_p
13854 && !NILP (Vwindow_text_change_functions))
13855 {
13856 safe_run_hooks (Qwindow_text_change_functions);
13857 goto restart;
13858 }
13859
13860 beg_unchanged = BEG_UNCHANGED;
13861 end_unchanged = END_UNCHANGED;
13862
13863 SET_TEXT_POS (opoint, PT, PT_BYTE);
13864
13865 specbind (Qinhibit_point_motion_hooks, Qt);
13866
13867 buffer_unchanged_p
13868 = (!NILP (w->window_end_valid)
13869 && !current_buffer->clip_changed
13870 && XFASTINT (w->last_modified) >= MODIFF
13871 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13872
13873 /* When windows_or_buffers_changed is non-zero, we can't rely on
13874 the window end being valid, so set it to nil there. */
13875 if (windows_or_buffers_changed)
13876 {
13877 /* If window starts on a continuation line, maybe adjust the
13878 window start in case the window's width changed. */
13879 if (XMARKER (w->start)->buffer == current_buffer)
13880 compute_window_start_on_continuation_line (w);
13881
13882 w->window_end_valid = Qnil;
13883 }
13884
13885 /* Some sanity checks. */
13886 CHECK_WINDOW_END (w);
13887 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13888 abort ();
13889 if (BYTEPOS (opoint) < CHARPOS (opoint))
13890 abort ();
13891
13892 /* If %c is in mode line, update it if needed. */
13893 if (!NILP (w->column_number_displayed)
13894 /* This alternative quickly identifies a common case
13895 where no change is needed. */
13896 && !(PT == XFASTINT (w->last_point)
13897 && XFASTINT (w->last_modified) >= MODIFF
13898 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13899 && (XFASTINT (w->column_number_displayed)
13900 != (int) current_column ())) /* iftc */
13901 update_mode_line = 1;
13902
13903 /* Count number of windows showing the selected buffer. An indirect
13904 buffer counts as its base buffer. */
13905 if (!just_this_one_p)
13906 {
13907 struct buffer *current_base, *window_base;
13908 current_base = current_buffer;
13909 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13910 if (current_base->base_buffer)
13911 current_base = current_base->base_buffer;
13912 if (window_base->base_buffer)
13913 window_base = window_base->base_buffer;
13914 if (current_base == window_base)
13915 buffer_shared++;
13916 }
13917
13918 /* Point refers normally to the selected window. For any other
13919 window, set up appropriate value. */
13920 if (!EQ (window, selected_window))
13921 {
13922 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
13923 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
13924 if (new_pt < BEGV)
13925 {
13926 new_pt = BEGV;
13927 new_pt_byte = BEGV_BYTE;
13928 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13929 }
13930 else if (new_pt > (ZV - 1))
13931 {
13932 new_pt = ZV;
13933 new_pt_byte = ZV_BYTE;
13934 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13935 }
13936
13937 /* We don't use SET_PT so that the point-motion hooks don't run. */
13938 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13939 }
13940
13941 /* If any of the character widths specified in the display table
13942 have changed, invalidate the width run cache. It's true that
13943 this may be a bit late to catch such changes, but the rest of
13944 redisplay goes (non-fatally) haywire when the display table is
13945 changed, so why should we worry about doing any better? */
13946 if (current_buffer->width_run_cache)
13947 {
13948 struct Lisp_Char_Table *disptab = buffer_display_table ();
13949
13950 if (! disptab_matches_widthtab (disptab,
13951 XVECTOR (current_buffer->width_table)))
13952 {
13953 invalidate_region_cache (current_buffer,
13954 current_buffer->width_run_cache,
13955 BEG, Z);
13956 recompute_width_table (current_buffer, disptab);
13957 }
13958 }
13959
13960 /* If window-start is screwed up, choose a new one. */
13961 if (XMARKER (w->start)->buffer != current_buffer)
13962 goto recenter;
13963
13964 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13965
13966 /* If someone specified a new starting point but did not insist,
13967 check whether it can be used. */
13968 if (!NILP (w->optional_new_start)
13969 && CHARPOS (startp) >= BEGV
13970 && CHARPOS (startp) <= ZV)
13971 {
13972 w->optional_new_start = Qnil;
13973 start_display (&it, w, startp);
13974 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13975 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13976 if (IT_CHARPOS (it) == PT)
13977 w->force_start = Qt;
13978 /* IT may overshoot PT if text at PT is invisible. */
13979 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13980 w->force_start = Qt;
13981 }
13982
13983 force_start:
13984
13985 /* Handle case where place to start displaying has been specified,
13986 unless the specified location is outside the accessible range. */
13987 if (!NILP (w->force_start)
13988 || w->frozen_window_start_p)
13989 {
13990 /* We set this later on if we have to adjust point. */
13991 int new_vpos = -1;
13992
13993 w->force_start = Qnil;
13994 w->vscroll = 0;
13995 w->window_end_valid = Qnil;
13996
13997 /* Forget any recorded base line for line number display. */
13998 if (!buffer_unchanged_p)
13999 w->base_line_number = Qnil;
14000
14001 /* Redisplay the mode line. Select the buffer properly for that.
14002 Also, run the hook window-scroll-functions
14003 because we have scrolled. */
14004 /* Note, we do this after clearing force_start because
14005 if there's an error, it is better to forget about force_start
14006 than to get into an infinite loop calling the hook functions
14007 and having them get more errors. */
14008 if (!update_mode_line
14009 || ! NILP (Vwindow_scroll_functions))
14010 {
14011 update_mode_line = 1;
14012 w->update_mode_line = Qt;
14013 startp = run_window_scroll_functions (window, startp);
14014 }
14015
14016 w->last_modified = make_number (0);
14017 w->last_overlay_modified = make_number (0);
14018 if (CHARPOS (startp) < BEGV)
14019 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14020 else if (CHARPOS (startp) > ZV)
14021 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14022
14023 /* Redisplay, then check if cursor has been set during the
14024 redisplay. Give up if new fonts were loaded. */
14025 /* We used to issue a CHECK_MARGINS argument to try_window here,
14026 but this causes scrolling to fail when point begins inside
14027 the scroll margin (bug#148) -- cyd */
14028 if (!try_window (window, startp, 0))
14029 {
14030 w->force_start = Qt;
14031 clear_glyph_matrix (w->desired_matrix);
14032 goto need_larger_matrices;
14033 }
14034
14035 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14036 {
14037 /* If point does not appear, try to move point so it does
14038 appear. The desired matrix has been built above, so we
14039 can use it here. */
14040 new_vpos = window_box_height (w) / 2;
14041 }
14042
14043 if (!cursor_row_fully_visible_p (w, 0, 0))
14044 {
14045 /* Point does appear, but on a line partly visible at end of window.
14046 Move it back to a fully-visible line. */
14047 new_vpos = window_box_height (w);
14048 }
14049
14050 /* If we need to move point for either of the above reasons,
14051 now actually do it. */
14052 if (new_vpos >= 0)
14053 {
14054 struct glyph_row *row;
14055
14056 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14057 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14058 ++row;
14059
14060 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14061 MATRIX_ROW_START_BYTEPOS (row));
14062
14063 if (w != XWINDOW (selected_window))
14064 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14065 else if (current_buffer == old)
14066 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14067
14068 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14069
14070 /* If we are highlighting the region, then we just changed
14071 the region, so redisplay to show it. */
14072 if (!NILP (Vtransient_mark_mode)
14073 && !NILP (current_buffer->mark_active))
14074 {
14075 clear_glyph_matrix (w->desired_matrix);
14076 if (!try_window (window, startp, 0))
14077 goto need_larger_matrices;
14078 }
14079 }
14080
14081 #if GLYPH_DEBUG
14082 debug_method_add (w, "forced window start");
14083 #endif
14084 goto done;
14085 }
14086
14087 /* Handle case where text has not changed, only point, and it has
14088 not moved off the frame, and we are not retrying after hscroll.
14089 (current_matrix_up_to_date_p is nonzero when retrying.) */
14090 if (current_matrix_up_to_date_p
14091 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14092 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14093 {
14094 switch (rc)
14095 {
14096 case CURSOR_MOVEMENT_SUCCESS:
14097 used_current_matrix_p = 1;
14098 goto done;
14099
14100 case CURSOR_MOVEMENT_MUST_SCROLL:
14101 goto try_to_scroll;
14102
14103 default:
14104 abort ();
14105 }
14106 }
14107 /* If current starting point was originally the beginning of a line
14108 but no longer is, find a new starting point. */
14109 else if (!NILP (w->start_at_line_beg)
14110 && !(CHARPOS (startp) <= BEGV
14111 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14112 {
14113 #if GLYPH_DEBUG
14114 debug_method_add (w, "recenter 1");
14115 #endif
14116 goto recenter;
14117 }
14118
14119 /* Try scrolling with try_window_id. Value is > 0 if update has
14120 been done, it is -1 if we know that the same window start will
14121 not work. It is 0 if unsuccessful for some other reason. */
14122 else if ((tem = try_window_id (w)) != 0)
14123 {
14124 #if GLYPH_DEBUG
14125 debug_method_add (w, "try_window_id %d", tem);
14126 #endif
14127
14128 if (fonts_changed_p)
14129 goto need_larger_matrices;
14130 if (tem > 0)
14131 goto done;
14132
14133 /* Otherwise try_window_id has returned -1 which means that we
14134 don't want the alternative below this comment to execute. */
14135 }
14136 else if (CHARPOS (startp) >= BEGV
14137 && CHARPOS (startp) <= ZV
14138 && PT >= CHARPOS (startp)
14139 && (CHARPOS (startp) < ZV
14140 /* Avoid starting at end of buffer. */
14141 || CHARPOS (startp) == BEGV
14142 || (XFASTINT (w->last_modified) >= MODIFF
14143 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14144 {
14145
14146 /* If first window line is a continuation line, and window start
14147 is inside the modified region, but the first change is before
14148 current window start, we must select a new window start.
14149
14150 However, if this is the result of a down-mouse event (e.g. by
14151 extending the mouse-drag-overlay), we don't want to select a
14152 new window start, since that would change the position under
14153 the mouse, resulting in an unwanted mouse-movement rather
14154 than a simple mouse-click. */
14155 if (NILP (w->start_at_line_beg)
14156 && NILP (do_mouse_tracking)
14157 && CHARPOS (startp) > BEGV
14158 && CHARPOS (startp) > BEG + beg_unchanged
14159 && CHARPOS (startp) <= Z - end_unchanged
14160 /* Even if w->start_at_line_beg is nil, a new window may
14161 start at a line_beg, since that's how set_buffer_window
14162 sets it. So, we need to check the return value of
14163 compute_window_start_on_continuation_line. (See also
14164 bug#197). */
14165 && XMARKER (w->start)->buffer == current_buffer
14166 && compute_window_start_on_continuation_line (w))
14167 {
14168 w->force_start = Qt;
14169 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14170 goto force_start;
14171 }
14172
14173 #if GLYPH_DEBUG
14174 debug_method_add (w, "same window start");
14175 #endif
14176
14177 /* Try to redisplay starting at same place as before.
14178 If point has not moved off frame, accept the results. */
14179 if (!current_matrix_up_to_date_p
14180 /* Don't use try_window_reusing_current_matrix in this case
14181 because a window scroll function can have changed the
14182 buffer. */
14183 || !NILP (Vwindow_scroll_functions)
14184 || MINI_WINDOW_P (w)
14185 || !(used_current_matrix_p
14186 = try_window_reusing_current_matrix (w)))
14187 {
14188 IF_DEBUG (debug_method_add (w, "1"));
14189 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14190 /* -1 means we need to scroll.
14191 0 means we need new matrices, but fonts_changed_p
14192 is set in that case, so we will detect it below. */
14193 goto try_to_scroll;
14194 }
14195
14196 if (fonts_changed_p)
14197 goto need_larger_matrices;
14198
14199 if (w->cursor.vpos >= 0)
14200 {
14201 if (!just_this_one_p
14202 || current_buffer->clip_changed
14203 || BEG_UNCHANGED < CHARPOS (startp))
14204 /* Forget any recorded base line for line number display. */
14205 w->base_line_number = Qnil;
14206
14207 if (!cursor_row_fully_visible_p (w, 1, 0))
14208 {
14209 clear_glyph_matrix (w->desired_matrix);
14210 last_line_misfit = 1;
14211 }
14212 /* Drop through and scroll. */
14213 else
14214 goto done;
14215 }
14216 else
14217 clear_glyph_matrix (w->desired_matrix);
14218 }
14219
14220 try_to_scroll:
14221
14222 w->last_modified = make_number (0);
14223 w->last_overlay_modified = make_number (0);
14224
14225 /* Redisplay the mode line. Select the buffer properly for that. */
14226 if (!update_mode_line)
14227 {
14228 update_mode_line = 1;
14229 w->update_mode_line = Qt;
14230 }
14231
14232 /* Try to scroll by specified few lines. */
14233 if ((scroll_conservatively
14234 || scroll_step
14235 || temp_scroll_step
14236 || NUMBERP (current_buffer->scroll_up_aggressively)
14237 || NUMBERP (current_buffer->scroll_down_aggressively))
14238 && !current_buffer->clip_changed
14239 && CHARPOS (startp) >= BEGV
14240 && CHARPOS (startp) <= ZV)
14241 {
14242 /* The function returns -1 if new fonts were loaded, 1 if
14243 successful, 0 if not successful. */
14244 int rc = try_scrolling (window, just_this_one_p,
14245 scroll_conservatively,
14246 scroll_step,
14247 temp_scroll_step, last_line_misfit);
14248 switch (rc)
14249 {
14250 case SCROLLING_SUCCESS:
14251 goto done;
14252
14253 case SCROLLING_NEED_LARGER_MATRICES:
14254 goto need_larger_matrices;
14255
14256 case SCROLLING_FAILED:
14257 break;
14258
14259 default:
14260 abort ();
14261 }
14262 }
14263
14264 /* Finally, just choose place to start which centers point */
14265
14266 recenter:
14267 if (centering_position < 0)
14268 centering_position = window_box_height (w) / 2;
14269
14270 #if GLYPH_DEBUG
14271 debug_method_add (w, "recenter");
14272 #endif
14273
14274 /* w->vscroll = 0; */
14275
14276 /* Forget any previously recorded base line for line number display. */
14277 if (!buffer_unchanged_p)
14278 w->base_line_number = Qnil;
14279
14280 /* Move backward half the height of the window. */
14281 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14282 it.current_y = it.last_visible_y;
14283 move_it_vertically_backward (&it, centering_position);
14284 xassert (IT_CHARPOS (it) >= BEGV);
14285
14286 /* The function move_it_vertically_backward may move over more
14287 than the specified y-distance. If it->w is small, e.g. a
14288 mini-buffer window, we may end up in front of the window's
14289 display area. Start displaying at the start of the line
14290 containing PT in this case. */
14291 if (it.current_y <= 0)
14292 {
14293 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14294 move_it_vertically_backward (&it, 0);
14295 it.current_y = 0;
14296 }
14297
14298 it.current_x = it.hpos = 0;
14299
14300 /* Set startp here explicitly in case that helps avoid an infinite loop
14301 in case the window-scroll-functions functions get errors. */
14302 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14303
14304 /* Run scroll hooks. */
14305 startp = run_window_scroll_functions (window, it.current.pos);
14306
14307 /* Redisplay the window. */
14308 if (!current_matrix_up_to_date_p
14309 || windows_or_buffers_changed
14310 || cursor_type_changed
14311 /* Don't use try_window_reusing_current_matrix in this case
14312 because it can have changed the buffer. */
14313 || !NILP (Vwindow_scroll_functions)
14314 || !just_this_one_p
14315 || MINI_WINDOW_P (w)
14316 || !(used_current_matrix_p
14317 = try_window_reusing_current_matrix (w)))
14318 try_window (window, startp, 0);
14319
14320 /* If new fonts have been loaded (due to fontsets), give up. We
14321 have to start a new redisplay since we need to re-adjust glyph
14322 matrices. */
14323 if (fonts_changed_p)
14324 goto need_larger_matrices;
14325
14326 /* If cursor did not appear assume that the middle of the window is
14327 in the first line of the window. Do it again with the next line.
14328 (Imagine a window of height 100, displaying two lines of height
14329 60. Moving back 50 from it->last_visible_y will end in the first
14330 line.) */
14331 if (w->cursor.vpos < 0)
14332 {
14333 if (!NILP (w->window_end_valid)
14334 && PT >= Z - XFASTINT (w->window_end_pos))
14335 {
14336 clear_glyph_matrix (w->desired_matrix);
14337 move_it_by_lines (&it, 1, 0);
14338 try_window (window, it.current.pos, 0);
14339 }
14340 else if (PT < IT_CHARPOS (it))
14341 {
14342 clear_glyph_matrix (w->desired_matrix);
14343 move_it_by_lines (&it, -1, 0);
14344 try_window (window, it.current.pos, 0);
14345 }
14346 else
14347 {
14348 /* Not much we can do about it. */
14349 }
14350 }
14351
14352 /* Consider the following case: Window starts at BEGV, there is
14353 invisible, intangible text at BEGV, so that display starts at
14354 some point START > BEGV. It can happen that we are called with
14355 PT somewhere between BEGV and START. Try to handle that case. */
14356 if (w->cursor.vpos < 0)
14357 {
14358 struct glyph_row *row = w->current_matrix->rows;
14359 if (row->mode_line_p)
14360 ++row;
14361 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14362 }
14363
14364 if (!cursor_row_fully_visible_p (w, 0, 0))
14365 {
14366 /* If vscroll is enabled, disable it and try again. */
14367 if (w->vscroll)
14368 {
14369 w->vscroll = 0;
14370 clear_glyph_matrix (w->desired_matrix);
14371 goto recenter;
14372 }
14373
14374 /* If centering point failed to make the whole line visible,
14375 put point at the top instead. That has to make the whole line
14376 visible, if it can be done. */
14377 if (centering_position == 0)
14378 goto done;
14379
14380 clear_glyph_matrix (w->desired_matrix);
14381 centering_position = 0;
14382 goto recenter;
14383 }
14384
14385 done:
14386
14387 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14388 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14389 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14390 ? Qt : Qnil);
14391
14392 /* Display the mode line, if we must. */
14393 if ((update_mode_line
14394 /* If window not full width, must redo its mode line
14395 if (a) the window to its side is being redone and
14396 (b) we do a frame-based redisplay. This is a consequence
14397 of how inverted lines are drawn in frame-based redisplay. */
14398 || (!just_this_one_p
14399 && !FRAME_WINDOW_P (f)
14400 && !WINDOW_FULL_WIDTH_P (w))
14401 /* Line number to display. */
14402 || INTEGERP (w->base_line_pos)
14403 /* Column number is displayed and different from the one displayed. */
14404 || (!NILP (w->column_number_displayed)
14405 && (XFASTINT (w->column_number_displayed)
14406 != (int) current_column ()))) /* iftc */
14407 /* This means that the window has a mode line. */
14408 && (WINDOW_WANTS_MODELINE_P (w)
14409 || WINDOW_WANTS_HEADER_LINE_P (w)))
14410 {
14411 display_mode_lines (w);
14412
14413 /* If mode line height has changed, arrange for a thorough
14414 immediate redisplay using the correct mode line height. */
14415 if (WINDOW_WANTS_MODELINE_P (w)
14416 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14417 {
14418 fonts_changed_p = 1;
14419 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14420 = DESIRED_MODE_LINE_HEIGHT (w);
14421 }
14422
14423 /* If header line height has changed, arrange for a thorough
14424 immediate redisplay using the correct header line height. */
14425 if (WINDOW_WANTS_HEADER_LINE_P (w)
14426 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14427 {
14428 fonts_changed_p = 1;
14429 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14430 = DESIRED_HEADER_LINE_HEIGHT (w);
14431 }
14432
14433 if (fonts_changed_p)
14434 goto need_larger_matrices;
14435 }
14436
14437 if (!line_number_displayed
14438 && !BUFFERP (w->base_line_pos))
14439 {
14440 w->base_line_pos = Qnil;
14441 w->base_line_number = Qnil;
14442 }
14443
14444 finish_menu_bars:
14445
14446 /* When we reach a frame's selected window, redo the frame's menu bar. */
14447 if (update_mode_line
14448 && EQ (FRAME_SELECTED_WINDOW (f), window))
14449 {
14450 int redisplay_menu_p = 0;
14451 int redisplay_tool_bar_p = 0;
14452
14453 if (FRAME_WINDOW_P (f))
14454 {
14455 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14456 || defined (HAVE_NS) || defined (USE_GTK)
14457 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14458 #else
14459 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14460 #endif
14461 }
14462 else
14463 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14464
14465 if (redisplay_menu_p)
14466 display_menu_bar (w);
14467
14468 #ifdef HAVE_WINDOW_SYSTEM
14469 if (FRAME_WINDOW_P (f))
14470 {
14471 #if defined (USE_GTK) || defined (HAVE_NS)
14472 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14473 #else
14474 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14475 && (FRAME_TOOL_BAR_LINES (f) > 0
14476 || !NILP (Vauto_resize_tool_bars));
14477 #endif
14478
14479 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14480 {
14481 ignore_mouse_drag_p = 1;
14482 }
14483 }
14484 #endif
14485 }
14486
14487 #ifdef HAVE_WINDOW_SYSTEM
14488 if (FRAME_WINDOW_P (f)
14489 && update_window_fringes (w, (just_this_one_p
14490 || (!used_current_matrix_p && !overlay_arrow_seen)
14491 || w->pseudo_window_p)))
14492 {
14493 update_begin (f);
14494 BLOCK_INPUT;
14495 if (draw_window_fringes (w, 1))
14496 x_draw_vertical_border (w);
14497 UNBLOCK_INPUT;
14498 update_end (f);
14499 }
14500 #endif /* HAVE_WINDOW_SYSTEM */
14501
14502 /* We go to this label, with fonts_changed_p nonzero,
14503 if it is necessary to try again using larger glyph matrices.
14504 We have to redeem the scroll bar even in this case,
14505 because the loop in redisplay_internal expects that. */
14506 need_larger_matrices:
14507 ;
14508 finish_scroll_bars:
14509
14510 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14511 {
14512 /* Set the thumb's position and size. */
14513 set_vertical_scroll_bar (w);
14514
14515 /* Note that we actually used the scroll bar attached to this
14516 window, so it shouldn't be deleted at the end of redisplay. */
14517 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14518 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14519 }
14520
14521 /* Restore current_buffer and value of point in it. The window
14522 update may have changed the buffer, so first make sure `opoint'
14523 is still valid (Bug#6177). */
14524 if (CHARPOS (opoint) < BEGV)
14525 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14526 else if (CHARPOS (opoint) > ZV)
14527 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14528 else
14529 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14530
14531 set_buffer_internal_1 (old);
14532 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14533 shorter. This can be caused by log truncation in *Messages*. */
14534 if (CHARPOS (lpoint) <= ZV)
14535 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14536
14537 unbind_to (count, Qnil);
14538 }
14539
14540
14541 /* Build the complete desired matrix of WINDOW with a window start
14542 buffer position POS.
14543
14544 Value is 1 if successful. It is zero if fonts were loaded during
14545 redisplay which makes re-adjusting glyph matrices necessary, and -1
14546 if point would appear in the scroll margins.
14547 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14548 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14549 set in FLAGS.) */
14550
14551 int
14552 try_window (Lisp_Object window, struct text_pos pos, int flags)
14553 {
14554 struct window *w = XWINDOW (window);
14555 struct it it;
14556 struct glyph_row *last_text_row = NULL;
14557 struct frame *f = XFRAME (w->frame);
14558
14559 /* Make POS the new window start. */
14560 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14561
14562 /* Mark cursor position as unknown. No overlay arrow seen. */
14563 w->cursor.vpos = -1;
14564 overlay_arrow_seen = 0;
14565
14566 /* Initialize iterator and info to start at POS. */
14567 start_display (&it, w, pos);
14568
14569 /* Display all lines of W. */
14570 while (it.current_y < it.last_visible_y)
14571 {
14572 if (display_line (&it))
14573 last_text_row = it.glyph_row - 1;
14574 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14575 return 0;
14576 }
14577
14578 /* Don't let the cursor end in the scroll margins. */
14579 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14580 && !MINI_WINDOW_P (w))
14581 {
14582 int this_scroll_margin;
14583
14584 if (scroll_margin > 0)
14585 {
14586 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14587 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14588 }
14589 else
14590 this_scroll_margin = 0;
14591
14592 if ((w->cursor.y >= 0 /* not vscrolled */
14593 && w->cursor.y < this_scroll_margin
14594 && CHARPOS (pos) > BEGV
14595 && IT_CHARPOS (it) < ZV)
14596 /* rms: considering make_cursor_line_fully_visible_p here
14597 seems to give wrong results. We don't want to recenter
14598 when the last line is partly visible, we want to allow
14599 that case to be handled in the usual way. */
14600 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14601 {
14602 w->cursor.vpos = -1;
14603 clear_glyph_matrix (w->desired_matrix);
14604 return -1;
14605 }
14606 }
14607
14608 /* If bottom moved off end of frame, change mode line percentage. */
14609 if (XFASTINT (w->window_end_pos) <= 0
14610 && Z != IT_CHARPOS (it))
14611 w->update_mode_line = Qt;
14612
14613 /* Set window_end_pos to the offset of the last character displayed
14614 on the window from the end of current_buffer. Set
14615 window_end_vpos to its row number. */
14616 if (last_text_row)
14617 {
14618 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14619 w->window_end_bytepos
14620 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14621 w->window_end_pos
14622 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14623 w->window_end_vpos
14624 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14625 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14626 ->displays_text_p);
14627 }
14628 else
14629 {
14630 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14631 w->window_end_pos = make_number (Z - ZV);
14632 w->window_end_vpos = make_number (0);
14633 }
14634
14635 /* But that is not valid info until redisplay finishes. */
14636 w->window_end_valid = Qnil;
14637 return 1;
14638 }
14639
14640
14641 \f
14642 /************************************************************************
14643 Window redisplay reusing current matrix when buffer has not changed
14644 ************************************************************************/
14645
14646 /* Try redisplay of window W showing an unchanged buffer with a
14647 different window start than the last time it was displayed by
14648 reusing its current matrix. Value is non-zero if successful.
14649 W->start is the new window start. */
14650
14651 static int
14652 try_window_reusing_current_matrix (struct window *w)
14653 {
14654 struct frame *f = XFRAME (w->frame);
14655 struct glyph_row *row, *bottom_row;
14656 struct it it;
14657 struct run run;
14658 struct text_pos start, new_start;
14659 int nrows_scrolled, i;
14660 struct glyph_row *last_text_row;
14661 struct glyph_row *last_reused_text_row;
14662 struct glyph_row *start_row;
14663 int start_vpos, min_y, max_y;
14664
14665 #if GLYPH_DEBUG
14666 if (inhibit_try_window_reusing)
14667 return 0;
14668 #endif
14669
14670 if (/* This function doesn't handle terminal frames. */
14671 !FRAME_WINDOW_P (f)
14672 /* Don't try to reuse the display if windows have been split
14673 or such. */
14674 || windows_or_buffers_changed
14675 || cursor_type_changed)
14676 return 0;
14677
14678 /* Can't do this if region may have changed. */
14679 if ((!NILP (Vtransient_mark_mode)
14680 && !NILP (current_buffer->mark_active))
14681 || !NILP (w->region_showing)
14682 || !NILP (Vshow_trailing_whitespace))
14683 return 0;
14684
14685 /* If top-line visibility has changed, give up. */
14686 if (WINDOW_WANTS_HEADER_LINE_P (w)
14687 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14688 return 0;
14689
14690 /* Give up if old or new display is scrolled vertically. We could
14691 make this function handle this, but right now it doesn't. */
14692 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14693 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14694 return 0;
14695
14696 /* The variable new_start now holds the new window start. The old
14697 start `start' can be determined from the current matrix. */
14698 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14699 start = start_row->minpos;
14700 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14701
14702 /* Clear the desired matrix for the display below. */
14703 clear_glyph_matrix (w->desired_matrix);
14704
14705 if (CHARPOS (new_start) <= CHARPOS (start))
14706 {
14707 int first_row_y;
14708
14709 /* Don't use this method if the display starts with an ellipsis
14710 displayed for invisible text. It's not easy to handle that case
14711 below, and it's certainly not worth the effort since this is
14712 not a frequent case. */
14713 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14714 return 0;
14715
14716 IF_DEBUG (debug_method_add (w, "twu1"));
14717
14718 /* Display up to a row that can be reused. The variable
14719 last_text_row is set to the last row displayed that displays
14720 text. Note that it.vpos == 0 if or if not there is a
14721 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14722 start_display (&it, w, new_start);
14723 first_row_y = it.current_y;
14724 w->cursor.vpos = -1;
14725 last_text_row = last_reused_text_row = NULL;
14726
14727 while (it.current_y < it.last_visible_y
14728 && !fonts_changed_p)
14729 {
14730 /* If we have reached into the characters in the START row,
14731 that means the line boundaries have changed. So we
14732 can't start copying with the row START. Maybe it will
14733 work to start copying with the following row. */
14734 while (IT_CHARPOS (it) > CHARPOS (start))
14735 {
14736 /* Advance to the next row as the "start". */
14737 start_row++;
14738 start = start_row->minpos;
14739 /* If there are no more rows to try, or just one, give up. */
14740 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14741 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14742 || CHARPOS (start) == ZV)
14743 {
14744 clear_glyph_matrix (w->desired_matrix);
14745 return 0;
14746 }
14747
14748 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14749 }
14750 /* If we have reached alignment,
14751 we can copy the rest of the rows. */
14752 if (IT_CHARPOS (it) == CHARPOS (start))
14753 break;
14754
14755 if (display_line (&it))
14756 last_text_row = it.glyph_row - 1;
14757 }
14758
14759 /* A value of current_y < last_visible_y means that we stopped
14760 at the previous window start, which in turn means that we
14761 have at least one reusable row. */
14762 if (it.current_y < it.last_visible_y)
14763 {
14764 /* IT.vpos always starts from 0; it counts text lines. */
14765 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14766
14767 /* Find PT if not already found in the lines displayed. */
14768 if (w->cursor.vpos < 0)
14769 {
14770 int dy = it.current_y - start_row->y;
14771
14772 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14773 row = row_containing_pos (w, PT, row, NULL, dy);
14774 if (row)
14775 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14776 dy, nrows_scrolled);
14777 else
14778 {
14779 clear_glyph_matrix (w->desired_matrix);
14780 return 0;
14781 }
14782 }
14783
14784 /* Scroll the display. Do it before the current matrix is
14785 changed. The problem here is that update has not yet
14786 run, i.e. part of the current matrix is not up to date.
14787 scroll_run_hook will clear the cursor, and use the
14788 current matrix to get the height of the row the cursor is
14789 in. */
14790 run.current_y = start_row->y;
14791 run.desired_y = it.current_y;
14792 run.height = it.last_visible_y - it.current_y;
14793
14794 if (run.height > 0 && run.current_y != run.desired_y)
14795 {
14796 update_begin (f);
14797 FRAME_RIF (f)->update_window_begin_hook (w);
14798 FRAME_RIF (f)->clear_window_mouse_face (w);
14799 FRAME_RIF (f)->scroll_run_hook (w, &run);
14800 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14801 update_end (f);
14802 }
14803
14804 /* Shift current matrix down by nrows_scrolled lines. */
14805 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14806 rotate_matrix (w->current_matrix,
14807 start_vpos,
14808 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14809 nrows_scrolled);
14810
14811 /* Disable lines that must be updated. */
14812 for (i = 0; i < nrows_scrolled; ++i)
14813 (start_row + i)->enabled_p = 0;
14814
14815 /* Re-compute Y positions. */
14816 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14817 max_y = it.last_visible_y;
14818 for (row = start_row + nrows_scrolled;
14819 row < bottom_row;
14820 ++row)
14821 {
14822 row->y = it.current_y;
14823 row->visible_height = row->height;
14824
14825 if (row->y < min_y)
14826 row->visible_height -= min_y - row->y;
14827 if (row->y + row->height > max_y)
14828 row->visible_height -= row->y + row->height - max_y;
14829 row->redraw_fringe_bitmaps_p = 1;
14830
14831 it.current_y += row->height;
14832
14833 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14834 last_reused_text_row = row;
14835 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14836 break;
14837 }
14838
14839 /* Disable lines in the current matrix which are now
14840 below the window. */
14841 for (++row; row < bottom_row; ++row)
14842 row->enabled_p = row->mode_line_p = 0;
14843 }
14844
14845 /* Update window_end_pos etc.; last_reused_text_row is the last
14846 reused row from the current matrix containing text, if any.
14847 The value of last_text_row is the last displayed line
14848 containing text. */
14849 if (last_reused_text_row)
14850 {
14851 w->window_end_bytepos
14852 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14853 w->window_end_pos
14854 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14855 w->window_end_vpos
14856 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14857 w->current_matrix));
14858 }
14859 else if (last_text_row)
14860 {
14861 w->window_end_bytepos
14862 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14863 w->window_end_pos
14864 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14865 w->window_end_vpos
14866 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14867 }
14868 else
14869 {
14870 /* This window must be completely empty. */
14871 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14872 w->window_end_pos = make_number (Z - ZV);
14873 w->window_end_vpos = make_number (0);
14874 }
14875 w->window_end_valid = Qnil;
14876
14877 /* Update hint: don't try scrolling again in update_window. */
14878 w->desired_matrix->no_scrolling_p = 1;
14879
14880 #if GLYPH_DEBUG
14881 debug_method_add (w, "try_window_reusing_current_matrix 1");
14882 #endif
14883 return 1;
14884 }
14885 else if (CHARPOS (new_start) > CHARPOS (start))
14886 {
14887 struct glyph_row *pt_row, *row;
14888 struct glyph_row *first_reusable_row;
14889 struct glyph_row *first_row_to_display;
14890 int dy;
14891 int yb = window_text_bottom_y (w);
14892
14893 /* Find the row starting at new_start, if there is one. Don't
14894 reuse a partially visible line at the end. */
14895 first_reusable_row = start_row;
14896 while (first_reusable_row->enabled_p
14897 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14898 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14899 < CHARPOS (new_start)))
14900 ++first_reusable_row;
14901
14902 /* Give up if there is no row to reuse. */
14903 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14904 || !first_reusable_row->enabled_p
14905 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14906 != CHARPOS (new_start)))
14907 return 0;
14908
14909 /* We can reuse fully visible rows beginning with
14910 first_reusable_row to the end of the window. Set
14911 first_row_to_display to the first row that cannot be reused.
14912 Set pt_row to the row containing point, if there is any. */
14913 pt_row = NULL;
14914 for (first_row_to_display = first_reusable_row;
14915 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14916 ++first_row_to_display)
14917 {
14918 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14919 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14920 pt_row = first_row_to_display;
14921 }
14922
14923 /* Start displaying at the start of first_row_to_display. */
14924 xassert (first_row_to_display->y < yb);
14925 init_to_row_start (&it, w, first_row_to_display);
14926
14927 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14928 - start_vpos);
14929 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14930 - nrows_scrolled);
14931 it.current_y = (first_row_to_display->y - first_reusable_row->y
14932 + WINDOW_HEADER_LINE_HEIGHT (w));
14933
14934 /* Display lines beginning with first_row_to_display in the
14935 desired matrix. Set last_text_row to the last row displayed
14936 that displays text. */
14937 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14938 if (pt_row == NULL)
14939 w->cursor.vpos = -1;
14940 last_text_row = NULL;
14941 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14942 if (display_line (&it))
14943 last_text_row = it.glyph_row - 1;
14944
14945 /* If point is in a reused row, adjust y and vpos of the cursor
14946 position. */
14947 if (pt_row)
14948 {
14949 w->cursor.vpos -= nrows_scrolled;
14950 w->cursor.y -= first_reusable_row->y - start_row->y;
14951 }
14952
14953 /* Give up if point isn't in a row displayed or reused. (This
14954 also handles the case where w->cursor.vpos < nrows_scrolled
14955 after the calls to display_line, which can happen with scroll
14956 margins. See bug#1295.) */
14957 if (w->cursor.vpos < 0)
14958 {
14959 clear_glyph_matrix (w->desired_matrix);
14960 return 0;
14961 }
14962
14963 /* Scroll the display. */
14964 run.current_y = first_reusable_row->y;
14965 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14966 run.height = it.last_visible_y - run.current_y;
14967 dy = run.current_y - run.desired_y;
14968
14969 if (run.height)
14970 {
14971 update_begin (f);
14972 FRAME_RIF (f)->update_window_begin_hook (w);
14973 FRAME_RIF (f)->clear_window_mouse_face (w);
14974 FRAME_RIF (f)->scroll_run_hook (w, &run);
14975 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14976 update_end (f);
14977 }
14978
14979 /* Adjust Y positions of reused rows. */
14980 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14981 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14982 max_y = it.last_visible_y;
14983 for (row = first_reusable_row; row < first_row_to_display; ++row)
14984 {
14985 row->y -= dy;
14986 row->visible_height = row->height;
14987 if (row->y < min_y)
14988 row->visible_height -= min_y - row->y;
14989 if (row->y + row->height > max_y)
14990 row->visible_height -= row->y + row->height - max_y;
14991 row->redraw_fringe_bitmaps_p = 1;
14992 }
14993
14994 /* Scroll the current matrix. */
14995 xassert (nrows_scrolled > 0);
14996 rotate_matrix (w->current_matrix,
14997 start_vpos,
14998 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14999 -nrows_scrolled);
15000
15001 /* Disable rows not reused. */
15002 for (row -= nrows_scrolled; row < bottom_row; ++row)
15003 row->enabled_p = 0;
15004
15005 /* Point may have moved to a different line, so we cannot assume that
15006 the previous cursor position is valid; locate the correct row. */
15007 if (pt_row)
15008 {
15009 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15010 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15011 row++)
15012 {
15013 w->cursor.vpos++;
15014 w->cursor.y = row->y;
15015 }
15016 if (row < bottom_row)
15017 {
15018 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15019 struct glyph *end = glyph + row->used[TEXT_AREA];
15020
15021 /* Can't use this optimization with bidi-reordered glyph
15022 rows, unless cursor is already at point. */
15023 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15024 {
15025 if (!(w->cursor.hpos >= 0
15026 && w->cursor.hpos < row->used[TEXT_AREA]
15027 && BUFFERP (glyph->object)
15028 && glyph->charpos == PT))
15029 return 0;
15030 }
15031 else
15032 for (; glyph < end
15033 && (!BUFFERP (glyph->object)
15034 || glyph->charpos < PT);
15035 glyph++)
15036 {
15037 w->cursor.hpos++;
15038 w->cursor.x += glyph->pixel_width;
15039 }
15040 }
15041 }
15042
15043 /* Adjust window end. A null value of last_text_row means that
15044 the window end is in reused rows which in turn means that
15045 only its vpos can have changed. */
15046 if (last_text_row)
15047 {
15048 w->window_end_bytepos
15049 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15050 w->window_end_pos
15051 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15052 w->window_end_vpos
15053 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15054 }
15055 else
15056 {
15057 w->window_end_vpos
15058 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15059 }
15060
15061 w->window_end_valid = Qnil;
15062 w->desired_matrix->no_scrolling_p = 1;
15063
15064 #if GLYPH_DEBUG
15065 debug_method_add (w, "try_window_reusing_current_matrix 2");
15066 #endif
15067 return 1;
15068 }
15069
15070 return 0;
15071 }
15072
15073
15074 \f
15075 /************************************************************************
15076 Window redisplay reusing current matrix when buffer has changed
15077 ************************************************************************/
15078
15079 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15080 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15081 EMACS_INT *, EMACS_INT *);
15082 static struct glyph_row *
15083 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15084 struct glyph_row *);
15085
15086
15087 /* Return the last row in MATRIX displaying text. If row START is
15088 non-null, start searching with that row. IT gives the dimensions
15089 of the display. Value is null if matrix is empty; otherwise it is
15090 a pointer to the row found. */
15091
15092 static struct glyph_row *
15093 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15094 struct glyph_row *start)
15095 {
15096 struct glyph_row *row, *row_found;
15097
15098 /* Set row_found to the last row in IT->w's current matrix
15099 displaying text. The loop looks funny but think of partially
15100 visible lines. */
15101 row_found = NULL;
15102 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15103 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15104 {
15105 xassert (row->enabled_p);
15106 row_found = row;
15107 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15108 break;
15109 ++row;
15110 }
15111
15112 return row_found;
15113 }
15114
15115
15116 /* Return the last row in the current matrix of W that is not affected
15117 by changes at the start of current_buffer that occurred since W's
15118 current matrix was built. Value is null if no such row exists.
15119
15120 BEG_UNCHANGED us the number of characters unchanged at the start of
15121 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15122 first changed character in current_buffer. Characters at positions <
15123 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15124 when the current matrix was built. */
15125
15126 static struct glyph_row *
15127 find_last_unchanged_at_beg_row (struct window *w)
15128 {
15129 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15130 struct glyph_row *row;
15131 struct glyph_row *row_found = NULL;
15132 int yb = window_text_bottom_y (w);
15133
15134 /* Find the last row displaying unchanged text. */
15135 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15136 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15137 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15138 ++row)
15139 {
15140 if (/* If row ends before first_changed_pos, it is unchanged,
15141 except in some case. */
15142 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15143 /* When row ends in ZV and we write at ZV it is not
15144 unchanged. */
15145 && !row->ends_at_zv_p
15146 /* When first_changed_pos is the end of a continued line,
15147 row is not unchanged because it may be no longer
15148 continued. */
15149 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15150 && (row->continued_p
15151 || row->exact_window_width_line_p)))
15152 row_found = row;
15153
15154 /* Stop if last visible row. */
15155 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15156 break;
15157 }
15158
15159 return row_found;
15160 }
15161
15162
15163 /* Find the first glyph row in the current matrix of W that is not
15164 affected by changes at the end of current_buffer since the
15165 time W's current matrix was built.
15166
15167 Return in *DELTA the number of chars by which buffer positions in
15168 unchanged text at the end of current_buffer must be adjusted.
15169
15170 Return in *DELTA_BYTES the corresponding number of bytes.
15171
15172 Value is null if no such row exists, i.e. all rows are affected by
15173 changes. */
15174
15175 static struct glyph_row *
15176 find_first_unchanged_at_end_row (struct window *w,
15177 EMACS_INT *delta, EMACS_INT *delta_bytes)
15178 {
15179 struct glyph_row *row;
15180 struct glyph_row *row_found = NULL;
15181
15182 *delta = *delta_bytes = 0;
15183
15184 /* Display must not have been paused, otherwise the current matrix
15185 is not up to date. */
15186 eassert (!NILP (w->window_end_valid));
15187
15188 /* A value of window_end_pos >= END_UNCHANGED means that the window
15189 end is in the range of changed text. If so, there is no
15190 unchanged row at the end of W's current matrix. */
15191 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15192 return NULL;
15193
15194 /* Set row to the last row in W's current matrix displaying text. */
15195 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15196
15197 /* If matrix is entirely empty, no unchanged row exists. */
15198 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15199 {
15200 /* The value of row is the last glyph row in the matrix having a
15201 meaningful buffer position in it. The end position of row
15202 corresponds to window_end_pos. This allows us to translate
15203 buffer positions in the current matrix to current buffer
15204 positions for characters not in changed text. */
15205 EMACS_INT Z_old =
15206 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15207 EMACS_INT Z_BYTE_old =
15208 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15209 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15210 struct glyph_row *first_text_row
15211 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15212
15213 *delta = Z - Z_old;
15214 *delta_bytes = Z_BYTE - Z_BYTE_old;
15215
15216 /* Set last_unchanged_pos to the buffer position of the last
15217 character in the buffer that has not been changed. Z is the
15218 index + 1 of the last character in current_buffer, i.e. by
15219 subtracting END_UNCHANGED we get the index of the last
15220 unchanged character, and we have to add BEG to get its buffer
15221 position. */
15222 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15223 last_unchanged_pos_old = last_unchanged_pos - *delta;
15224
15225 /* Search backward from ROW for a row displaying a line that
15226 starts at a minimum position >= last_unchanged_pos_old. */
15227 for (; row > first_text_row; --row)
15228 {
15229 /* This used to abort, but it can happen.
15230 It is ok to just stop the search instead here. KFS. */
15231 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15232 break;
15233
15234 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15235 row_found = row;
15236 }
15237 }
15238
15239 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15240
15241 return row_found;
15242 }
15243
15244
15245 /* Make sure that glyph rows in the current matrix of window W
15246 reference the same glyph memory as corresponding rows in the
15247 frame's frame matrix. This function is called after scrolling W's
15248 current matrix on a terminal frame in try_window_id and
15249 try_window_reusing_current_matrix. */
15250
15251 static void
15252 sync_frame_with_window_matrix_rows (struct window *w)
15253 {
15254 struct frame *f = XFRAME (w->frame);
15255 struct glyph_row *window_row, *window_row_end, *frame_row;
15256
15257 /* Preconditions: W must be a leaf window and full-width. Its frame
15258 must have a frame matrix. */
15259 xassert (NILP (w->hchild) && NILP (w->vchild));
15260 xassert (WINDOW_FULL_WIDTH_P (w));
15261 xassert (!FRAME_WINDOW_P (f));
15262
15263 /* If W is a full-width window, glyph pointers in W's current matrix
15264 have, by definition, to be the same as glyph pointers in the
15265 corresponding frame matrix. Note that frame matrices have no
15266 marginal areas (see build_frame_matrix). */
15267 window_row = w->current_matrix->rows;
15268 window_row_end = window_row + w->current_matrix->nrows;
15269 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15270 while (window_row < window_row_end)
15271 {
15272 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15273 struct glyph *end = window_row->glyphs[LAST_AREA];
15274
15275 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15276 frame_row->glyphs[TEXT_AREA] = start;
15277 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15278 frame_row->glyphs[LAST_AREA] = end;
15279
15280 /* Disable frame rows whose corresponding window rows have
15281 been disabled in try_window_id. */
15282 if (!window_row->enabled_p)
15283 frame_row->enabled_p = 0;
15284
15285 ++window_row, ++frame_row;
15286 }
15287 }
15288
15289
15290 /* Find the glyph row in window W containing CHARPOS. Consider all
15291 rows between START and END (not inclusive). END null means search
15292 all rows to the end of the display area of W. Value is the row
15293 containing CHARPOS or null. */
15294
15295 struct glyph_row *
15296 row_containing_pos (struct window *w, EMACS_INT charpos,
15297 struct glyph_row *start, struct glyph_row *end, int dy)
15298 {
15299 struct glyph_row *row = start;
15300 struct glyph_row *best_row = NULL;
15301 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15302 int last_y;
15303
15304 /* If we happen to start on a header-line, skip that. */
15305 if (row->mode_line_p)
15306 ++row;
15307
15308 if ((end && row >= end) || !row->enabled_p)
15309 return NULL;
15310
15311 last_y = window_text_bottom_y (w) - dy;
15312
15313 while (1)
15314 {
15315 /* Give up if we have gone too far. */
15316 if (end && row >= end)
15317 return NULL;
15318 /* This formerly returned if they were equal.
15319 I think that both quantities are of a "last plus one" type;
15320 if so, when they are equal, the row is within the screen. -- rms. */
15321 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15322 return NULL;
15323
15324 /* If it is in this row, return this row. */
15325 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15326 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15327 /* The end position of a row equals the start
15328 position of the next row. If CHARPOS is there, we
15329 would rather display it in the next line, except
15330 when this line ends in ZV. */
15331 && !row->ends_at_zv_p
15332 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15333 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15334 {
15335 struct glyph *g;
15336
15337 if (NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15338 || (!best_row && !row->continued_p))
15339 return row;
15340 /* In bidi-reordered rows, there could be several rows
15341 occluding point, all of them belonging to the same
15342 continued line. We need to find the row which fits
15343 CHARPOS the best. */
15344 for (g = row->glyphs[TEXT_AREA];
15345 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15346 g++)
15347 {
15348 if (!STRINGP (g->object))
15349 {
15350 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15351 {
15352 mindif = eabs (g->charpos - charpos);
15353 best_row = row;
15354 /* Exact match always wins. */
15355 if (mindif == 0)
15356 return best_row;
15357 }
15358 }
15359 }
15360 }
15361 else if (best_row && !row->continued_p)
15362 return best_row;
15363 ++row;
15364 }
15365 }
15366
15367
15368 /* Try to redisplay window W by reusing its existing display. W's
15369 current matrix must be up to date when this function is called,
15370 i.e. window_end_valid must not be nil.
15371
15372 Value is
15373
15374 1 if display has been updated
15375 0 if otherwise unsuccessful
15376 -1 if redisplay with same window start is known not to succeed
15377
15378 The following steps are performed:
15379
15380 1. Find the last row in the current matrix of W that is not
15381 affected by changes at the start of current_buffer. If no such row
15382 is found, give up.
15383
15384 2. Find the first row in W's current matrix that is not affected by
15385 changes at the end of current_buffer. Maybe there is no such row.
15386
15387 3. Display lines beginning with the row + 1 found in step 1 to the
15388 row found in step 2 or, if step 2 didn't find a row, to the end of
15389 the window.
15390
15391 4. If cursor is not known to appear on the window, give up.
15392
15393 5. If display stopped at the row found in step 2, scroll the
15394 display and current matrix as needed.
15395
15396 6. Maybe display some lines at the end of W, if we must. This can
15397 happen under various circumstances, like a partially visible line
15398 becoming fully visible, or because newly displayed lines are displayed
15399 in smaller font sizes.
15400
15401 7. Update W's window end information. */
15402
15403 static int
15404 try_window_id (struct window *w)
15405 {
15406 struct frame *f = XFRAME (w->frame);
15407 struct glyph_matrix *current_matrix = w->current_matrix;
15408 struct glyph_matrix *desired_matrix = w->desired_matrix;
15409 struct glyph_row *last_unchanged_at_beg_row;
15410 struct glyph_row *first_unchanged_at_end_row;
15411 struct glyph_row *row;
15412 struct glyph_row *bottom_row;
15413 int bottom_vpos;
15414 struct it it;
15415 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
15416 int dvpos, dy;
15417 struct text_pos start_pos;
15418 struct run run;
15419 int first_unchanged_at_end_vpos = 0;
15420 struct glyph_row *last_text_row, *last_text_row_at_end;
15421 struct text_pos start;
15422 EMACS_INT first_changed_charpos, last_changed_charpos;
15423
15424 #if GLYPH_DEBUG
15425 if (inhibit_try_window_id)
15426 return 0;
15427 #endif
15428
15429 /* This is handy for debugging. */
15430 #if 0
15431 #define GIVE_UP(X) \
15432 do { \
15433 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15434 return 0; \
15435 } while (0)
15436 #else
15437 #define GIVE_UP(X) return 0
15438 #endif
15439
15440 SET_TEXT_POS_FROM_MARKER (start, w->start);
15441
15442 /* Don't use this for mini-windows because these can show
15443 messages and mini-buffers, and we don't handle that here. */
15444 if (MINI_WINDOW_P (w))
15445 GIVE_UP (1);
15446
15447 /* This flag is used to prevent redisplay optimizations. */
15448 if (windows_or_buffers_changed || cursor_type_changed)
15449 GIVE_UP (2);
15450
15451 /* Verify that narrowing has not changed.
15452 Also verify that we were not told to prevent redisplay optimizations.
15453 It would be nice to further
15454 reduce the number of cases where this prevents try_window_id. */
15455 if (current_buffer->clip_changed
15456 || current_buffer->prevent_redisplay_optimizations_p)
15457 GIVE_UP (3);
15458
15459 /* Window must either use window-based redisplay or be full width. */
15460 if (!FRAME_WINDOW_P (f)
15461 && (!FRAME_LINE_INS_DEL_OK (f)
15462 || !WINDOW_FULL_WIDTH_P (w)))
15463 GIVE_UP (4);
15464
15465 /* Give up if point is known NOT to appear in W. */
15466 if (PT < CHARPOS (start))
15467 GIVE_UP (5);
15468
15469 /* Another way to prevent redisplay optimizations. */
15470 if (XFASTINT (w->last_modified) == 0)
15471 GIVE_UP (6);
15472
15473 /* Verify that window is not hscrolled. */
15474 if (XFASTINT (w->hscroll) != 0)
15475 GIVE_UP (7);
15476
15477 /* Verify that display wasn't paused. */
15478 if (NILP (w->window_end_valid))
15479 GIVE_UP (8);
15480
15481 /* Can't use this if highlighting a region because a cursor movement
15482 will do more than just set the cursor. */
15483 if (!NILP (Vtransient_mark_mode)
15484 && !NILP (current_buffer->mark_active))
15485 GIVE_UP (9);
15486
15487 /* Likewise if highlighting trailing whitespace. */
15488 if (!NILP (Vshow_trailing_whitespace))
15489 GIVE_UP (11);
15490
15491 /* Likewise if showing a region. */
15492 if (!NILP (w->region_showing))
15493 GIVE_UP (10);
15494
15495 /* Can't use this if overlay arrow position and/or string have
15496 changed. */
15497 if (overlay_arrows_changed_p ())
15498 GIVE_UP (12);
15499
15500 /* When word-wrap is on, adding a space to the first word of a
15501 wrapped line can change the wrap position, altering the line
15502 above it. It might be worthwhile to handle this more
15503 intelligently, but for now just redisplay from scratch. */
15504 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15505 GIVE_UP (21);
15506
15507 /* Under bidi reordering, adding or deleting a character in the
15508 beginning of a paragraph, before the first strong directional
15509 character, can change the base direction of the paragraph (unless
15510 the buffer specifies a fixed paragraph direction), which will
15511 require to redisplay the whole paragraph. It might be worthwhile
15512 to find the paragraph limits and widen the range of redisplayed
15513 lines to that, but for now just give up this optimization and
15514 redisplay from scratch. */
15515 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15516 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15517 GIVE_UP (22);
15518
15519 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15520 only if buffer has really changed. The reason is that the gap is
15521 initially at Z for freshly visited files. The code below would
15522 set end_unchanged to 0 in that case. */
15523 if (MODIFF > SAVE_MODIFF
15524 /* This seems to happen sometimes after saving a buffer. */
15525 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15526 {
15527 if (GPT - BEG < BEG_UNCHANGED)
15528 BEG_UNCHANGED = GPT - BEG;
15529 if (Z - GPT < END_UNCHANGED)
15530 END_UNCHANGED = Z - GPT;
15531 }
15532
15533 /* The position of the first and last character that has been changed. */
15534 first_changed_charpos = BEG + BEG_UNCHANGED;
15535 last_changed_charpos = Z - END_UNCHANGED;
15536
15537 /* If window starts after a line end, and the last change is in
15538 front of that newline, then changes don't affect the display.
15539 This case happens with stealth-fontification. Note that although
15540 the display is unchanged, glyph positions in the matrix have to
15541 be adjusted, of course. */
15542 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15543 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15544 && ((last_changed_charpos < CHARPOS (start)
15545 && CHARPOS (start) == BEGV)
15546 || (last_changed_charpos < CHARPOS (start) - 1
15547 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15548 {
15549 EMACS_INT Z_old, delta, Z_BYTE_old, delta_bytes;
15550 struct glyph_row *r0;
15551
15552 /* Compute how many chars/bytes have been added to or removed
15553 from the buffer. */
15554 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15555 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15556 delta = Z - Z_old;
15557 delta_bytes = Z_BYTE - Z_BYTE_old;
15558
15559 /* Give up if PT is not in the window. Note that it already has
15560 been checked at the start of try_window_id that PT is not in
15561 front of the window start. */
15562 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15563 GIVE_UP (13);
15564
15565 /* If window start is unchanged, we can reuse the whole matrix
15566 as is, after adjusting glyph positions. No need to compute
15567 the window end again, since its offset from Z hasn't changed. */
15568 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15569 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15570 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15571 /* PT must not be in a partially visible line. */
15572 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15573 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15574 {
15575 /* Adjust positions in the glyph matrix. */
15576 if (delta || delta_bytes)
15577 {
15578 struct glyph_row *r1
15579 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15580 increment_matrix_positions (w->current_matrix,
15581 MATRIX_ROW_VPOS (r0, current_matrix),
15582 MATRIX_ROW_VPOS (r1, current_matrix),
15583 delta, delta_bytes);
15584 }
15585
15586 /* Set the cursor. */
15587 row = row_containing_pos (w, PT, r0, NULL, 0);
15588 if (row)
15589 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15590 else
15591 abort ();
15592 return 1;
15593 }
15594 }
15595
15596 /* Handle the case that changes are all below what is displayed in
15597 the window, and that PT is in the window. This shortcut cannot
15598 be taken if ZV is visible in the window, and text has been added
15599 there that is visible in the window. */
15600 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15601 /* ZV is not visible in the window, or there are no
15602 changes at ZV, actually. */
15603 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15604 || first_changed_charpos == last_changed_charpos))
15605 {
15606 struct glyph_row *r0;
15607
15608 /* Give up if PT is not in the window. Note that it already has
15609 been checked at the start of try_window_id that PT is not in
15610 front of the window start. */
15611 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15612 GIVE_UP (14);
15613
15614 /* If window start is unchanged, we can reuse the whole matrix
15615 as is, without changing glyph positions since no text has
15616 been added/removed in front of the window end. */
15617 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15618 if (TEXT_POS_EQUAL_P (start, r0->minpos)
15619 /* PT must not be in a partially visible line. */
15620 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15621 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15622 {
15623 /* We have to compute the window end anew since text
15624 could have been added/removed after it. */
15625 w->window_end_pos
15626 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15627 w->window_end_bytepos
15628 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15629
15630 /* Set the cursor. */
15631 row = row_containing_pos (w, PT, r0, NULL, 0);
15632 if (row)
15633 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15634 else
15635 abort ();
15636 return 2;
15637 }
15638 }
15639
15640 /* Give up if window start is in the changed area.
15641
15642 The condition used to read
15643
15644 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15645
15646 but why that was tested escapes me at the moment. */
15647 if (CHARPOS (start) >= first_changed_charpos
15648 && CHARPOS (start) <= last_changed_charpos)
15649 GIVE_UP (15);
15650
15651 /* Check that window start agrees with the start of the first glyph
15652 row in its current matrix. Check this after we know the window
15653 start is not in changed text, otherwise positions would not be
15654 comparable. */
15655 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15656 if (!TEXT_POS_EQUAL_P (start, row->minpos))
15657 GIVE_UP (16);
15658
15659 /* Give up if the window ends in strings. Overlay strings
15660 at the end are difficult to handle, so don't try. */
15661 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15662 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15663 GIVE_UP (20);
15664
15665 /* Compute the position at which we have to start displaying new
15666 lines. Some of the lines at the top of the window might be
15667 reusable because they are not displaying changed text. Find the
15668 last row in W's current matrix not affected by changes at the
15669 start of current_buffer. Value is null if changes start in the
15670 first line of window. */
15671 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15672 if (last_unchanged_at_beg_row)
15673 {
15674 /* Avoid starting to display in the moddle of a character, a TAB
15675 for instance. This is easier than to set up the iterator
15676 exactly, and it's not a frequent case, so the additional
15677 effort wouldn't really pay off. */
15678 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15679 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15680 && last_unchanged_at_beg_row > w->current_matrix->rows)
15681 --last_unchanged_at_beg_row;
15682
15683 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15684 GIVE_UP (17);
15685
15686 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15687 GIVE_UP (18);
15688 start_pos = it.current.pos;
15689
15690 /* Start displaying new lines in the desired matrix at the same
15691 vpos we would use in the current matrix, i.e. below
15692 last_unchanged_at_beg_row. */
15693 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15694 current_matrix);
15695 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15696 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15697
15698 xassert (it.hpos == 0 && it.current_x == 0);
15699 }
15700 else
15701 {
15702 /* There are no reusable lines at the start of the window.
15703 Start displaying in the first text line. */
15704 start_display (&it, w, start);
15705 it.vpos = it.first_vpos;
15706 start_pos = it.current.pos;
15707 }
15708
15709 /* Find the first row that is not affected by changes at the end of
15710 the buffer. Value will be null if there is no unchanged row, in
15711 which case we must redisplay to the end of the window. delta
15712 will be set to the value by which buffer positions beginning with
15713 first_unchanged_at_end_row have to be adjusted due to text
15714 changes. */
15715 first_unchanged_at_end_row
15716 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15717 IF_DEBUG (debug_delta = delta);
15718 IF_DEBUG (debug_delta_bytes = delta_bytes);
15719
15720 /* Set stop_pos to the buffer position up to which we will have to
15721 display new lines. If first_unchanged_at_end_row != NULL, this
15722 is the buffer position of the start of the line displayed in that
15723 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15724 that we don't stop at a buffer position. */
15725 stop_pos = 0;
15726 if (first_unchanged_at_end_row)
15727 {
15728 xassert (last_unchanged_at_beg_row == NULL
15729 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15730
15731 /* If this is a continuation line, move forward to the next one
15732 that isn't. Changes in lines above affect this line.
15733 Caution: this may move first_unchanged_at_end_row to a row
15734 not displaying text. */
15735 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15736 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15737 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15738 < it.last_visible_y))
15739 ++first_unchanged_at_end_row;
15740
15741 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15742 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15743 >= it.last_visible_y))
15744 first_unchanged_at_end_row = NULL;
15745 else
15746 {
15747 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15748 + delta);
15749 first_unchanged_at_end_vpos
15750 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15751 xassert (stop_pos >= Z - END_UNCHANGED);
15752 }
15753 }
15754 else if (last_unchanged_at_beg_row == NULL)
15755 GIVE_UP (19);
15756
15757
15758 #if GLYPH_DEBUG
15759
15760 /* Either there is no unchanged row at the end, or the one we have
15761 now displays text. This is a necessary condition for the window
15762 end pos calculation at the end of this function. */
15763 xassert (first_unchanged_at_end_row == NULL
15764 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15765
15766 debug_last_unchanged_at_beg_vpos
15767 = (last_unchanged_at_beg_row
15768 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15769 : -1);
15770 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15771
15772 #endif /* GLYPH_DEBUG != 0 */
15773
15774
15775 /* Display new lines. Set last_text_row to the last new line
15776 displayed which has text on it, i.e. might end up as being the
15777 line where the window_end_vpos is. */
15778 w->cursor.vpos = -1;
15779 last_text_row = NULL;
15780 overlay_arrow_seen = 0;
15781 while (it.current_y < it.last_visible_y
15782 && !fonts_changed_p
15783 && (first_unchanged_at_end_row == NULL
15784 || IT_CHARPOS (it) < stop_pos))
15785 {
15786 if (display_line (&it))
15787 last_text_row = it.glyph_row - 1;
15788 }
15789
15790 if (fonts_changed_p)
15791 return -1;
15792
15793
15794 /* Compute differences in buffer positions, y-positions etc. for
15795 lines reused at the bottom of the window. Compute what we can
15796 scroll. */
15797 if (first_unchanged_at_end_row
15798 /* No lines reused because we displayed everything up to the
15799 bottom of the window. */
15800 && it.current_y < it.last_visible_y)
15801 {
15802 dvpos = (it.vpos
15803 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15804 current_matrix));
15805 dy = it.current_y - first_unchanged_at_end_row->y;
15806 run.current_y = first_unchanged_at_end_row->y;
15807 run.desired_y = run.current_y + dy;
15808 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15809 }
15810 else
15811 {
15812 delta = delta_bytes = dvpos = dy
15813 = run.current_y = run.desired_y = run.height = 0;
15814 first_unchanged_at_end_row = NULL;
15815 }
15816 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15817
15818
15819 /* Find the cursor if not already found. We have to decide whether
15820 PT will appear on this window (it sometimes doesn't, but this is
15821 not a very frequent case.) This decision has to be made before
15822 the current matrix is altered. A value of cursor.vpos < 0 means
15823 that PT is either in one of the lines beginning at
15824 first_unchanged_at_end_row or below the window. Don't care for
15825 lines that might be displayed later at the window end; as
15826 mentioned, this is not a frequent case. */
15827 if (w->cursor.vpos < 0)
15828 {
15829 /* Cursor in unchanged rows at the top? */
15830 if (PT < CHARPOS (start_pos)
15831 && last_unchanged_at_beg_row)
15832 {
15833 row = row_containing_pos (w, PT,
15834 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15835 last_unchanged_at_beg_row + 1, 0);
15836 if (row)
15837 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15838 }
15839
15840 /* Start from first_unchanged_at_end_row looking for PT. */
15841 else if (first_unchanged_at_end_row)
15842 {
15843 row = row_containing_pos (w, PT - delta,
15844 first_unchanged_at_end_row, NULL, 0);
15845 if (row)
15846 set_cursor_from_row (w, row, w->current_matrix, delta,
15847 delta_bytes, dy, dvpos);
15848 }
15849
15850 /* Give up if cursor was not found. */
15851 if (w->cursor.vpos < 0)
15852 {
15853 clear_glyph_matrix (w->desired_matrix);
15854 return -1;
15855 }
15856 }
15857
15858 /* Don't let the cursor end in the scroll margins. */
15859 {
15860 int this_scroll_margin, cursor_height;
15861
15862 this_scroll_margin = max (0, scroll_margin);
15863 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15864 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15865 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15866
15867 if ((w->cursor.y < this_scroll_margin
15868 && CHARPOS (start) > BEGV)
15869 /* Old redisplay didn't take scroll margin into account at the bottom,
15870 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15871 || (w->cursor.y + (make_cursor_line_fully_visible_p
15872 ? cursor_height + this_scroll_margin
15873 : 1)) > it.last_visible_y)
15874 {
15875 w->cursor.vpos = -1;
15876 clear_glyph_matrix (w->desired_matrix);
15877 return -1;
15878 }
15879 }
15880
15881 /* Scroll the display. Do it before changing the current matrix so
15882 that xterm.c doesn't get confused about where the cursor glyph is
15883 found. */
15884 if (dy && run.height)
15885 {
15886 update_begin (f);
15887
15888 if (FRAME_WINDOW_P (f))
15889 {
15890 FRAME_RIF (f)->update_window_begin_hook (w);
15891 FRAME_RIF (f)->clear_window_mouse_face (w);
15892 FRAME_RIF (f)->scroll_run_hook (w, &run);
15893 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15894 }
15895 else
15896 {
15897 /* Terminal frame. In this case, dvpos gives the number of
15898 lines to scroll by; dvpos < 0 means scroll up. */
15899 int first_unchanged_at_end_vpos
15900 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15901 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15902 int end = (WINDOW_TOP_EDGE_LINE (w)
15903 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15904 + window_internal_height (w));
15905
15906 /* Perform the operation on the screen. */
15907 if (dvpos > 0)
15908 {
15909 /* Scroll last_unchanged_at_beg_row to the end of the
15910 window down dvpos lines. */
15911 set_terminal_window (f, end);
15912
15913 /* On dumb terminals delete dvpos lines at the end
15914 before inserting dvpos empty lines. */
15915 if (!FRAME_SCROLL_REGION_OK (f))
15916 ins_del_lines (f, end - dvpos, -dvpos);
15917
15918 /* Insert dvpos empty lines in front of
15919 last_unchanged_at_beg_row. */
15920 ins_del_lines (f, from, dvpos);
15921 }
15922 else if (dvpos < 0)
15923 {
15924 /* Scroll up last_unchanged_at_beg_vpos to the end of
15925 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15926 set_terminal_window (f, end);
15927
15928 /* Delete dvpos lines in front of
15929 last_unchanged_at_beg_vpos. ins_del_lines will set
15930 the cursor to the given vpos and emit |dvpos| delete
15931 line sequences. */
15932 ins_del_lines (f, from + dvpos, dvpos);
15933
15934 /* On a dumb terminal insert dvpos empty lines at the
15935 end. */
15936 if (!FRAME_SCROLL_REGION_OK (f))
15937 ins_del_lines (f, end + dvpos, -dvpos);
15938 }
15939
15940 set_terminal_window (f, 0);
15941 }
15942
15943 update_end (f);
15944 }
15945
15946 /* Shift reused rows of the current matrix to the right position.
15947 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15948 text. */
15949 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15950 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15951 if (dvpos < 0)
15952 {
15953 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15954 bottom_vpos, dvpos);
15955 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15956 bottom_vpos, 0);
15957 }
15958 else if (dvpos > 0)
15959 {
15960 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15961 bottom_vpos, dvpos);
15962 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15963 first_unchanged_at_end_vpos + dvpos, 0);
15964 }
15965
15966 /* For frame-based redisplay, make sure that current frame and window
15967 matrix are in sync with respect to glyph memory. */
15968 if (!FRAME_WINDOW_P (f))
15969 sync_frame_with_window_matrix_rows (w);
15970
15971 /* Adjust buffer positions in reused rows. */
15972 if (delta || delta_bytes)
15973 increment_matrix_positions (current_matrix,
15974 first_unchanged_at_end_vpos + dvpos,
15975 bottom_vpos, delta, delta_bytes);
15976
15977 /* Adjust Y positions. */
15978 if (dy)
15979 shift_glyph_matrix (w, current_matrix,
15980 first_unchanged_at_end_vpos + dvpos,
15981 bottom_vpos, dy);
15982
15983 if (first_unchanged_at_end_row)
15984 {
15985 first_unchanged_at_end_row += dvpos;
15986 if (first_unchanged_at_end_row->y >= it.last_visible_y
15987 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15988 first_unchanged_at_end_row = NULL;
15989 }
15990
15991 /* If scrolling up, there may be some lines to display at the end of
15992 the window. */
15993 last_text_row_at_end = NULL;
15994 if (dy < 0)
15995 {
15996 /* Scrolling up can leave for example a partially visible line
15997 at the end of the window to be redisplayed. */
15998 /* Set last_row to the glyph row in the current matrix where the
15999 window end line is found. It has been moved up or down in
16000 the matrix by dvpos. */
16001 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16002 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16003
16004 /* If last_row is the window end line, it should display text. */
16005 xassert (last_row->displays_text_p);
16006
16007 /* If window end line was partially visible before, begin
16008 displaying at that line. Otherwise begin displaying with the
16009 line following it. */
16010 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16011 {
16012 init_to_row_start (&it, w, last_row);
16013 it.vpos = last_vpos;
16014 it.current_y = last_row->y;
16015 }
16016 else
16017 {
16018 init_to_row_end (&it, w, last_row);
16019 it.vpos = 1 + last_vpos;
16020 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16021 ++last_row;
16022 }
16023
16024 /* We may start in a continuation line. If so, we have to
16025 get the right continuation_lines_width and current_x. */
16026 it.continuation_lines_width = last_row->continuation_lines_width;
16027 it.hpos = it.current_x = 0;
16028
16029 /* Display the rest of the lines at the window end. */
16030 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16031 while (it.current_y < it.last_visible_y
16032 && !fonts_changed_p)
16033 {
16034 /* Is it always sure that the display agrees with lines in
16035 the current matrix? I don't think so, so we mark rows
16036 displayed invalid in the current matrix by setting their
16037 enabled_p flag to zero. */
16038 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16039 if (display_line (&it))
16040 last_text_row_at_end = it.glyph_row - 1;
16041 }
16042 }
16043
16044 /* Update window_end_pos and window_end_vpos. */
16045 if (first_unchanged_at_end_row
16046 && !last_text_row_at_end)
16047 {
16048 /* Window end line if one of the preserved rows from the current
16049 matrix. Set row to the last row displaying text in current
16050 matrix starting at first_unchanged_at_end_row, after
16051 scrolling. */
16052 xassert (first_unchanged_at_end_row->displays_text_p);
16053 row = find_last_row_displaying_text (w->current_matrix, &it,
16054 first_unchanged_at_end_row);
16055 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16056
16057 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16058 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16059 w->window_end_vpos
16060 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16061 xassert (w->window_end_bytepos >= 0);
16062 IF_DEBUG (debug_method_add (w, "A"));
16063 }
16064 else if (last_text_row_at_end)
16065 {
16066 w->window_end_pos
16067 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16068 w->window_end_bytepos
16069 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16070 w->window_end_vpos
16071 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16072 xassert (w->window_end_bytepos >= 0);
16073 IF_DEBUG (debug_method_add (w, "B"));
16074 }
16075 else if (last_text_row)
16076 {
16077 /* We have displayed either to the end of the window or at the
16078 end of the window, i.e. the last row with text is to be found
16079 in the desired matrix. */
16080 w->window_end_pos
16081 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16082 w->window_end_bytepos
16083 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16084 w->window_end_vpos
16085 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16086 xassert (w->window_end_bytepos >= 0);
16087 }
16088 else if (first_unchanged_at_end_row == NULL
16089 && last_text_row == NULL
16090 && last_text_row_at_end == NULL)
16091 {
16092 /* Displayed to end of window, but no line containing text was
16093 displayed. Lines were deleted at the end of the window. */
16094 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16095 int vpos = XFASTINT (w->window_end_vpos);
16096 struct glyph_row *current_row = current_matrix->rows + vpos;
16097 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16098
16099 for (row = NULL;
16100 row == NULL && vpos >= first_vpos;
16101 --vpos, --current_row, --desired_row)
16102 {
16103 if (desired_row->enabled_p)
16104 {
16105 if (desired_row->displays_text_p)
16106 row = desired_row;
16107 }
16108 else if (current_row->displays_text_p)
16109 row = current_row;
16110 }
16111
16112 xassert (row != NULL);
16113 w->window_end_vpos = make_number (vpos + 1);
16114 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16115 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16116 xassert (w->window_end_bytepos >= 0);
16117 IF_DEBUG (debug_method_add (w, "C"));
16118 }
16119 else
16120 abort ();
16121
16122 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16123 debug_end_vpos = XFASTINT (w->window_end_vpos));
16124
16125 /* Record that display has not been completed. */
16126 w->window_end_valid = Qnil;
16127 w->desired_matrix->no_scrolling_p = 1;
16128 return 3;
16129
16130 #undef GIVE_UP
16131 }
16132
16133
16134 \f
16135 /***********************************************************************
16136 More debugging support
16137 ***********************************************************************/
16138
16139 #if GLYPH_DEBUG
16140
16141 void dump_glyph_row (struct glyph_row *, int, int);
16142 void dump_glyph_matrix (struct glyph_matrix *, int);
16143 void dump_glyph (struct glyph_row *, struct glyph *, int);
16144
16145
16146 /* Dump the contents of glyph matrix MATRIX on stderr.
16147
16148 GLYPHS 0 means don't show glyph contents.
16149 GLYPHS 1 means show glyphs in short form
16150 GLYPHS > 1 means show glyphs in long form. */
16151
16152 void
16153 dump_glyph_matrix (matrix, glyphs)
16154 struct glyph_matrix *matrix;
16155 int glyphs;
16156 {
16157 int i;
16158 for (i = 0; i < matrix->nrows; ++i)
16159 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16160 }
16161
16162
16163 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16164 the glyph row and area where the glyph comes from. */
16165
16166 void
16167 dump_glyph (row, glyph, area)
16168 struct glyph_row *row;
16169 struct glyph *glyph;
16170 int area;
16171 {
16172 if (glyph->type == CHAR_GLYPH)
16173 {
16174 fprintf (stderr,
16175 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16176 glyph - row->glyphs[TEXT_AREA],
16177 'C',
16178 glyph->charpos,
16179 (BUFFERP (glyph->object)
16180 ? 'B'
16181 : (STRINGP (glyph->object)
16182 ? 'S'
16183 : '-')),
16184 glyph->pixel_width,
16185 glyph->u.ch,
16186 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16187 ? glyph->u.ch
16188 : '.'),
16189 glyph->face_id,
16190 glyph->left_box_line_p,
16191 glyph->right_box_line_p);
16192 }
16193 else if (glyph->type == STRETCH_GLYPH)
16194 {
16195 fprintf (stderr,
16196 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16197 glyph - row->glyphs[TEXT_AREA],
16198 'S',
16199 glyph->charpos,
16200 (BUFFERP (glyph->object)
16201 ? 'B'
16202 : (STRINGP (glyph->object)
16203 ? 'S'
16204 : '-')),
16205 glyph->pixel_width,
16206 0,
16207 '.',
16208 glyph->face_id,
16209 glyph->left_box_line_p,
16210 glyph->right_box_line_p);
16211 }
16212 else if (glyph->type == IMAGE_GLYPH)
16213 {
16214 fprintf (stderr,
16215 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16216 glyph - row->glyphs[TEXT_AREA],
16217 'I',
16218 glyph->charpos,
16219 (BUFFERP (glyph->object)
16220 ? 'B'
16221 : (STRINGP (glyph->object)
16222 ? 'S'
16223 : '-')),
16224 glyph->pixel_width,
16225 glyph->u.img_id,
16226 '.',
16227 glyph->face_id,
16228 glyph->left_box_line_p,
16229 glyph->right_box_line_p);
16230 }
16231 else if (glyph->type == COMPOSITE_GLYPH)
16232 {
16233 fprintf (stderr,
16234 " %5d %4c %6d %c %3d 0x%05x",
16235 glyph - row->glyphs[TEXT_AREA],
16236 '+',
16237 glyph->charpos,
16238 (BUFFERP (glyph->object)
16239 ? 'B'
16240 : (STRINGP (glyph->object)
16241 ? 'S'
16242 : '-')),
16243 glyph->pixel_width,
16244 glyph->u.cmp.id);
16245 if (glyph->u.cmp.automatic)
16246 fprintf (stderr,
16247 "[%d-%d]",
16248 glyph->slice.cmp.from, glyph->slice.cmp.to);
16249 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16250 glyph->face_id,
16251 glyph->left_box_line_p,
16252 glyph->right_box_line_p);
16253 }
16254 }
16255
16256
16257 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16258 GLYPHS 0 means don't show glyph contents.
16259 GLYPHS 1 means show glyphs in short form
16260 GLYPHS > 1 means show glyphs in long form. */
16261
16262 void
16263 dump_glyph_row (row, vpos, glyphs)
16264 struct glyph_row *row;
16265 int vpos, glyphs;
16266 {
16267 if (glyphs != 1)
16268 {
16269 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16270 fprintf (stderr, "======================================================================\n");
16271
16272 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16273 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16274 vpos,
16275 MATRIX_ROW_START_CHARPOS (row),
16276 MATRIX_ROW_END_CHARPOS (row),
16277 row->used[TEXT_AREA],
16278 row->contains_overlapping_glyphs_p,
16279 row->enabled_p,
16280 row->truncated_on_left_p,
16281 row->truncated_on_right_p,
16282 row->continued_p,
16283 MATRIX_ROW_CONTINUATION_LINE_P (row),
16284 row->displays_text_p,
16285 row->ends_at_zv_p,
16286 row->fill_line_p,
16287 row->ends_in_middle_of_char_p,
16288 row->starts_in_middle_of_char_p,
16289 row->mouse_face_p,
16290 row->x,
16291 row->y,
16292 row->pixel_width,
16293 row->height,
16294 row->visible_height,
16295 row->ascent,
16296 row->phys_ascent);
16297 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16298 row->end.overlay_string_index,
16299 row->continuation_lines_width);
16300 fprintf (stderr, "%9d %5d\n",
16301 CHARPOS (row->start.string_pos),
16302 CHARPOS (row->end.string_pos));
16303 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16304 row->end.dpvec_index);
16305 }
16306
16307 if (glyphs > 1)
16308 {
16309 int area;
16310
16311 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16312 {
16313 struct glyph *glyph = row->glyphs[area];
16314 struct glyph *glyph_end = glyph + row->used[area];
16315
16316 /* Glyph for a line end in text. */
16317 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16318 ++glyph_end;
16319
16320 if (glyph < glyph_end)
16321 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16322
16323 for (; glyph < glyph_end; ++glyph)
16324 dump_glyph (row, glyph, area);
16325 }
16326 }
16327 else if (glyphs == 1)
16328 {
16329 int area;
16330
16331 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16332 {
16333 char *s = (char *) alloca (row->used[area] + 1);
16334 int i;
16335
16336 for (i = 0; i < row->used[area]; ++i)
16337 {
16338 struct glyph *glyph = row->glyphs[area] + i;
16339 if (glyph->type == CHAR_GLYPH
16340 && glyph->u.ch < 0x80
16341 && glyph->u.ch >= ' ')
16342 s[i] = glyph->u.ch;
16343 else
16344 s[i] = '.';
16345 }
16346
16347 s[i] = '\0';
16348 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16349 }
16350 }
16351 }
16352
16353
16354 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16355 Sdump_glyph_matrix, 0, 1, "p",
16356 doc: /* Dump the current matrix of the selected window to stderr.
16357 Shows contents of glyph row structures. With non-nil
16358 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16359 glyphs in short form, otherwise show glyphs in long form. */)
16360 (Lisp_Object glyphs)
16361 {
16362 struct window *w = XWINDOW (selected_window);
16363 struct buffer *buffer = XBUFFER (w->buffer);
16364
16365 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16366 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16367 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16368 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16369 fprintf (stderr, "=============================================\n");
16370 dump_glyph_matrix (w->current_matrix,
16371 NILP (glyphs) ? 0 : XINT (glyphs));
16372 return Qnil;
16373 }
16374
16375
16376 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16377 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16378 (void)
16379 {
16380 struct frame *f = XFRAME (selected_frame);
16381 dump_glyph_matrix (f->current_matrix, 1);
16382 return Qnil;
16383 }
16384
16385
16386 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16387 doc: /* Dump glyph row ROW to stderr.
16388 GLYPH 0 means don't dump glyphs.
16389 GLYPH 1 means dump glyphs in short form.
16390 GLYPH > 1 or omitted means dump glyphs in long form. */)
16391 (Lisp_Object row, Lisp_Object glyphs)
16392 {
16393 struct glyph_matrix *matrix;
16394 int vpos;
16395
16396 CHECK_NUMBER (row);
16397 matrix = XWINDOW (selected_window)->current_matrix;
16398 vpos = XINT (row);
16399 if (vpos >= 0 && vpos < matrix->nrows)
16400 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16401 vpos,
16402 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16403 return Qnil;
16404 }
16405
16406
16407 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16408 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16409 GLYPH 0 means don't dump glyphs.
16410 GLYPH 1 means dump glyphs in short form.
16411 GLYPH > 1 or omitted means dump glyphs in long form. */)
16412 (Lisp_Object row, Lisp_Object glyphs)
16413 {
16414 struct frame *sf = SELECTED_FRAME ();
16415 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16416 int vpos;
16417
16418 CHECK_NUMBER (row);
16419 vpos = XINT (row);
16420 if (vpos >= 0 && vpos < m->nrows)
16421 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16422 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16423 return Qnil;
16424 }
16425
16426
16427 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16428 doc: /* Toggle tracing of redisplay.
16429 With ARG, turn tracing on if and only if ARG is positive. */)
16430 (Lisp_Object arg)
16431 {
16432 if (NILP (arg))
16433 trace_redisplay_p = !trace_redisplay_p;
16434 else
16435 {
16436 arg = Fprefix_numeric_value (arg);
16437 trace_redisplay_p = XINT (arg) > 0;
16438 }
16439
16440 return Qnil;
16441 }
16442
16443
16444 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16445 doc: /* Like `format', but print result to stderr.
16446 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16447 (int nargs, Lisp_Object *args)
16448 {
16449 Lisp_Object s = Fformat (nargs, args);
16450 fprintf (stderr, "%s", SDATA (s));
16451 return Qnil;
16452 }
16453
16454 #endif /* GLYPH_DEBUG */
16455
16456
16457 \f
16458 /***********************************************************************
16459 Building Desired Matrix Rows
16460 ***********************************************************************/
16461
16462 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16463 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16464
16465 static struct glyph_row *
16466 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16467 {
16468 struct frame *f = XFRAME (WINDOW_FRAME (w));
16469 struct buffer *buffer = XBUFFER (w->buffer);
16470 struct buffer *old = current_buffer;
16471 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16472 int arrow_len = SCHARS (overlay_arrow_string);
16473 const unsigned char *arrow_end = arrow_string + arrow_len;
16474 const unsigned char *p;
16475 struct it it;
16476 int multibyte_p;
16477 int n_glyphs_before;
16478
16479 set_buffer_temp (buffer);
16480 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16481 it.glyph_row->used[TEXT_AREA] = 0;
16482 SET_TEXT_POS (it.position, 0, 0);
16483
16484 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16485 p = arrow_string;
16486 while (p < arrow_end)
16487 {
16488 Lisp_Object face, ilisp;
16489
16490 /* Get the next character. */
16491 if (multibyte_p)
16492 it.c = it.char_to_display = string_char_and_length (p, &it.len);
16493 else
16494 {
16495 it.c = it.char_to_display = *p, it.len = 1;
16496 if (! ASCII_CHAR_P (it.c))
16497 it.char_to_display = BYTE8_TO_CHAR (it.c);
16498 }
16499 p += it.len;
16500
16501 /* Get its face. */
16502 ilisp = make_number (p - arrow_string);
16503 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16504 it.face_id = compute_char_face (f, it.char_to_display, face);
16505
16506 /* Compute its width, get its glyphs. */
16507 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16508 SET_TEXT_POS (it.position, -1, -1);
16509 PRODUCE_GLYPHS (&it);
16510
16511 /* If this character doesn't fit any more in the line, we have
16512 to remove some glyphs. */
16513 if (it.current_x > it.last_visible_x)
16514 {
16515 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16516 break;
16517 }
16518 }
16519
16520 set_buffer_temp (old);
16521 return it.glyph_row;
16522 }
16523
16524
16525 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16526 glyphs are only inserted for terminal frames since we can't really
16527 win with truncation glyphs when partially visible glyphs are
16528 involved. Which glyphs to insert is determined by
16529 produce_special_glyphs. */
16530
16531 static void
16532 insert_left_trunc_glyphs (struct it *it)
16533 {
16534 struct it truncate_it;
16535 struct glyph *from, *end, *to, *toend;
16536
16537 xassert (!FRAME_WINDOW_P (it->f));
16538
16539 /* Get the truncation glyphs. */
16540 truncate_it = *it;
16541 truncate_it.current_x = 0;
16542 truncate_it.face_id = DEFAULT_FACE_ID;
16543 truncate_it.glyph_row = &scratch_glyph_row;
16544 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16545 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16546 truncate_it.object = make_number (0);
16547 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16548
16549 /* Overwrite glyphs from IT with truncation glyphs. */
16550 if (!it->glyph_row->reversed_p)
16551 {
16552 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16553 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16554 to = it->glyph_row->glyphs[TEXT_AREA];
16555 toend = to + it->glyph_row->used[TEXT_AREA];
16556
16557 while (from < end)
16558 *to++ = *from++;
16559
16560 /* There may be padding glyphs left over. Overwrite them too. */
16561 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16562 {
16563 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16564 while (from < end)
16565 *to++ = *from++;
16566 }
16567
16568 if (to > toend)
16569 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16570 }
16571 else
16572 {
16573 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
16574 that back to front. */
16575 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
16576 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16577 toend = it->glyph_row->glyphs[TEXT_AREA];
16578 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
16579
16580 while (from >= end && to >= toend)
16581 *to-- = *from--;
16582 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
16583 {
16584 from =
16585 truncate_it.glyph_row->glyphs[TEXT_AREA]
16586 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16587 while (from >= end && to >= toend)
16588 *to-- = *from--;
16589 }
16590 if (from >= end)
16591 {
16592 /* Need to free some room before prepending additional
16593 glyphs. */
16594 int move_by = from - end + 1;
16595 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
16596 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
16597
16598 for ( ; g >= g0; g--)
16599 g[move_by] = *g;
16600 while (from >= end)
16601 *to-- = *from--;
16602 it->glyph_row->used[TEXT_AREA] += move_by;
16603 }
16604 }
16605 }
16606
16607
16608 /* Compute the pixel height and width of IT->glyph_row.
16609
16610 Most of the time, ascent and height of a display line will be equal
16611 to the max_ascent and max_height values of the display iterator
16612 structure. This is not the case if
16613
16614 1. We hit ZV without displaying anything. In this case, max_ascent
16615 and max_height will be zero.
16616
16617 2. We have some glyphs that don't contribute to the line height.
16618 (The glyph row flag contributes_to_line_height_p is for future
16619 pixmap extensions).
16620
16621 The first case is easily covered by using default values because in
16622 these cases, the line height does not really matter, except that it
16623 must not be zero. */
16624
16625 static void
16626 compute_line_metrics (struct it *it)
16627 {
16628 struct glyph_row *row = it->glyph_row;
16629 int area, i;
16630
16631 if (FRAME_WINDOW_P (it->f))
16632 {
16633 int i, min_y, max_y;
16634
16635 /* The line may consist of one space only, that was added to
16636 place the cursor on it. If so, the row's height hasn't been
16637 computed yet. */
16638 if (row->height == 0)
16639 {
16640 if (it->max_ascent + it->max_descent == 0)
16641 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16642 row->ascent = it->max_ascent;
16643 row->height = it->max_ascent + it->max_descent;
16644 row->phys_ascent = it->max_phys_ascent;
16645 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16646 row->extra_line_spacing = it->max_extra_line_spacing;
16647 }
16648
16649 /* Compute the width of this line. */
16650 row->pixel_width = row->x;
16651 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16652 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16653
16654 xassert (row->pixel_width >= 0);
16655 xassert (row->ascent >= 0 && row->height > 0);
16656
16657 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16658 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16659
16660 /* If first line's physical ascent is larger than its logical
16661 ascent, use the physical ascent, and make the row taller.
16662 This makes accented characters fully visible. */
16663 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16664 && row->phys_ascent > row->ascent)
16665 {
16666 row->height += row->phys_ascent - row->ascent;
16667 row->ascent = row->phys_ascent;
16668 }
16669
16670 /* Compute how much of the line is visible. */
16671 row->visible_height = row->height;
16672
16673 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16674 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16675
16676 if (row->y < min_y)
16677 row->visible_height -= min_y - row->y;
16678 if (row->y + row->height > max_y)
16679 row->visible_height -= row->y + row->height - max_y;
16680 }
16681 else
16682 {
16683 row->pixel_width = row->used[TEXT_AREA];
16684 if (row->continued_p)
16685 row->pixel_width -= it->continuation_pixel_width;
16686 else if (row->truncated_on_right_p)
16687 row->pixel_width -= it->truncation_pixel_width;
16688 row->ascent = row->phys_ascent = 0;
16689 row->height = row->phys_height = row->visible_height = 1;
16690 row->extra_line_spacing = 0;
16691 }
16692
16693 /* Compute a hash code for this row. */
16694 row->hash = 0;
16695 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16696 for (i = 0; i < row->used[area]; ++i)
16697 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16698 + row->glyphs[area][i].u.val
16699 + row->glyphs[area][i].face_id
16700 + row->glyphs[area][i].padding_p
16701 + (row->glyphs[area][i].type << 2));
16702
16703 it->max_ascent = it->max_descent = 0;
16704 it->max_phys_ascent = it->max_phys_descent = 0;
16705 }
16706
16707
16708 /* Append one space to the glyph row of iterator IT if doing a
16709 window-based redisplay. The space has the same face as
16710 IT->face_id. Value is non-zero if a space was added.
16711
16712 This function is called to make sure that there is always one glyph
16713 at the end of a glyph row that the cursor can be set on under
16714 window-systems. (If there weren't such a glyph we would not know
16715 how wide and tall a box cursor should be displayed).
16716
16717 At the same time this space let's a nicely handle clearing to the
16718 end of the line if the row ends in italic text. */
16719
16720 static int
16721 append_space_for_newline (struct it *it, int default_face_p)
16722 {
16723 if (FRAME_WINDOW_P (it->f))
16724 {
16725 int n = it->glyph_row->used[TEXT_AREA];
16726
16727 if (it->glyph_row->glyphs[TEXT_AREA] + n
16728 < it->glyph_row->glyphs[1 + TEXT_AREA])
16729 {
16730 /* Save some values that must not be changed.
16731 Must save IT->c and IT->len because otherwise
16732 ITERATOR_AT_END_P wouldn't work anymore after
16733 append_space_for_newline has been called. */
16734 enum display_element_type saved_what = it->what;
16735 int saved_c = it->c, saved_len = it->len;
16736 int saved_char_to_display = it->char_to_display;
16737 int saved_x = it->current_x;
16738 int saved_face_id = it->face_id;
16739 struct text_pos saved_pos;
16740 Lisp_Object saved_object;
16741 struct face *face;
16742
16743 saved_object = it->object;
16744 saved_pos = it->position;
16745
16746 it->what = IT_CHARACTER;
16747 memset (&it->position, 0, sizeof it->position);
16748 it->object = make_number (0);
16749 it->c = it->char_to_display = ' ';
16750 it->len = 1;
16751
16752 if (default_face_p)
16753 it->face_id = DEFAULT_FACE_ID;
16754 else if (it->face_before_selective_p)
16755 it->face_id = it->saved_face_id;
16756 face = FACE_FROM_ID (it->f, it->face_id);
16757 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16758
16759 PRODUCE_GLYPHS (it);
16760
16761 it->override_ascent = -1;
16762 it->constrain_row_ascent_descent_p = 0;
16763 it->current_x = saved_x;
16764 it->object = saved_object;
16765 it->position = saved_pos;
16766 it->what = saved_what;
16767 it->face_id = saved_face_id;
16768 it->len = saved_len;
16769 it->c = saved_c;
16770 it->char_to_display = saved_char_to_display;
16771 return 1;
16772 }
16773 }
16774
16775 return 0;
16776 }
16777
16778
16779 /* Extend the face of the last glyph in the text area of IT->glyph_row
16780 to the end of the display line. Called from display_line. If the
16781 glyph row is empty, add a space glyph to it so that we know the
16782 face to draw. Set the glyph row flag fill_line_p. If the glyph
16783 row is R2L, prepend a stretch glyph to cover the empty space to the
16784 left of the leftmost glyph. */
16785
16786 static void
16787 extend_face_to_end_of_line (struct it *it)
16788 {
16789 struct face *face;
16790 struct frame *f = it->f;
16791
16792 /* If line is already filled, do nothing. Non window-system frames
16793 get a grace of one more ``pixel'' because their characters are
16794 1-``pixel'' wide, so they hit the equality too early. This grace
16795 is needed only for R2L rows that are not continued, to produce
16796 one extra blank where we could display the cursor. */
16797 if (it->current_x >= it->last_visible_x
16798 + (!FRAME_WINDOW_P (f)
16799 && it->glyph_row->reversed_p
16800 && !it->glyph_row->continued_p))
16801 return;
16802
16803 /* Face extension extends the background and box of IT->face_id
16804 to the end of the line. If the background equals the background
16805 of the frame, we don't have to do anything. */
16806 if (it->face_before_selective_p)
16807 face = FACE_FROM_ID (f, it->saved_face_id);
16808 else
16809 face = FACE_FROM_ID (f, it->face_id);
16810
16811 if (FRAME_WINDOW_P (f)
16812 && it->glyph_row->displays_text_p
16813 && face->box == FACE_NO_BOX
16814 && face->background == FRAME_BACKGROUND_PIXEL (f)
16815 && !face->stipple
16816 && !it->glyph_row->reversed_p)
16817 return;
16818
16819 /* Set the glyph row flag indicating that the face of the last glyph
16820 in the text area has to be drawn to the end of the text area. */
16821 it->glyph_row->fill_line_p = 1;
16822
16823 /* If current character of IT is not ASCII, make sure we have the
16824 ASCII face. This will be automatically undone the next time
16825 get_next_display_element returns a multibyte character. Note
16826 that the character will always be single byte in unibyte
16827 text. */
16828 if (!ASCII_CHAR_P (it->c))
16829 {
16830 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16831 }
16832
16833 if (FRAME_WINDOW_P (f))
16834 {
16835 /* If the row is empty, add a space with the current face of IT,
16836 so that we know which face to draw. */
16837 if (it->glyph_row->used[TEXT_AREA] == 0)
16838 {
16839 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16840 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16841 it->glyph_row->used[TEXT_AREA] = 1;
16842 }
16843 #ifdef HAVE_WINDOW_SYSTEM
16844 if (it->glyph_row->reversed_p)
16845 {
16846 /* Prepend a stretch glyph to the row, such that the
16847 rightmost glyph will be drawn flushed all the way to the
16848 right margin of the window. The stretch glyph that will
16849 occupy the empty space, if any, to the left of the
16850 glyphs. */
16851 struct font *font = face->font ? face->font : FRAME_FONT (f);
16852 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
16853 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
16854 struct glyph *g;
16855 int row_width, stretch_ascent, stretch_width;
16856 struct text_pos saved_pos;
16857 int saved_face_id, saved_avoid_cursor;
16858
16859 for (row_width = 0, g = row_start; g < row_end; g++)
16860 row_width += g->pixel_width;
16861 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
16862 if (stretch_width > 0)
16863 {
16864 stretch_ascent =
16865 (((it->ascent + it->descent)
16866 * FONT_BASE (font)) / FONT_HEIGHT (font));
16867 saved_pos = it->position;
16868 memset (&it->position, 0, sizeof it->position);
16869 saved_avoid_cursor = it->avoid_cursor_p;
16870 it->avoid_cursor_p = 1;
16871 saved_face_id = it->face_id;
16872 /* The last row's stretch glyph should get the default
16873 face, to avoid painting the rest of the window with
16874 the region face, if the region ends at ZV. */
16875 if (it->glyph_row->ends_at_zv_p)
16876 it->face_id = DEFAULT_FACE_ID;
16877 else
16878 it->face_id = face->id;
16879 append_stretch_glyph (it, make_number (0), stretch_width,
16880 it->ascent + it->descent, stretch_ascent);
16881 it->position = saved_pos;
16882 it->avoid_cursor_p = saved_avoid_cursor;
16883 it->face_id = saved_face_id;
16884 }
16885 }
16886 #endif /* HAVE_WINDOW_SYSTEM */
16887 }
16888 else
16889 {
16890 /* Save some values that must not be changed. */
16891 int saved_x = it->current_x;
16892 struct text_pos saved_pos;
16893 Lisp_Object saved_object;
16894 enum display_element_type saved_what = it->what;
16895 int saved_face_id = it->face_id;
16896
16897 saved_object = it->object;
16898 saved_pos = it->position;
16899
16900 it->what = IT_CHARACTER;
16901 memset (&it->position, 0, sizeof it->position);
16902 it->object = make_number (0);
16903 it->c = it->char_to_display = ' ';
16904 it->len = 1;
16905 /* The last row's blank glyphs should get the default face, to
16906 avoid painting the rest of the window with the region face,
16907 if the region ends at ZV. */
16908 if (it->glyph_row->ends_at_zv_p)
16909 it->face_id = DEFAULT_FACE_ID;
16910 else
16911 it->face_id = face->id;
16912
16913 PRODUCE_GLYPHS (it);
16914
16915 while (it->current_x <= it->last_visible_x)
16916 PRODUCE_GLYPHS (it);
16917
16918 /* Don't count these blanks really. It would let us insert a left
16919 truncation glyph below and make us set the cursor on them, maybe. */
16920 it->current_x = saved_x;
16921 it->object = saved_object;
16922 it->position = saved_pos;
16923 it->what = saved_what;
16924 it->face_id = saved_face_id;
16925 }
16926 }
16927
16928
16929 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16930 trailing whitespace. */
16931
16932 static int
16933 trailing_whitespace_p (EMACS_INT charpos)
16934 {
16935 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
16936 int c = 0;
16937
16938 while (bytepos < ZV_BYTE
16939 && (c = FETCH_CHAR (bytepos),
16940 c == ' ' || c == '\t'))
16941 ++bytepos;
16942
16943 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16944 {
16945 if (bytepos != PT_BYTE)
16946 return 1;
16947 }
16948 return 0;
16949 }
16950
16951
16952 /* Highlight trailing whitespace, if any, in ROW. */
16953
16954 void
16955 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
16956 {
16957 int used = row->used[TEXT_AREA];
16958
16959 if (used)
16960 {
16961 struct glyph *start = row->glyphs[TEXT_AREA];
16962 struct glyph *glyph = start + used - 1;
16963
16964 if (row->reversed_p)
16965 {
16966 /* Right-to-left rows need to be processed in the opposite
16967 direction, so swap the edge pointers. */
16968 glyph = start;
16969 start = row->glyphs[TEXT_AREA] + used - 1;
16970 }
16971
16972 /* Skip over glyphs inserted to display the cursor at the
16973 end of a line, for extending the face of the last glyph
16974 to the end of the line on terminals, and for truncation
16975 and continuation glyphs. */
16976 if (!row->reversed_p)
16977 {
16978 while (glyph >= start
16979 && glyph->type == CHAR_GLYPH
16980 && INTEGERP (glyph->object))
16981 --glyph;
16982 }
16983 else
16984 {
16985 while (glyph <= start
16986 && glyph->type == CHAR_GLYPH
16987 && INTEGERP (glyph->object))
16988 ++glyph;
16989 }
16990
16991 /* If last glyph is a space or stretch, and it's trailing
16992 whitespace, set the face of all trailing whitespace glyphs in
16993 IT->glyph_row to `trailing-whitespace'. */
16994 if ((row->reversed_p ? glyph <= start : glyph >= start)
16995 && BUFFERP (glyph->object)
16996 && (glyph->type == STRETCH_GLYPH
16997 || (glyph->type == CHAR_GLYPH
16998 && glyph->u.ch == ' '))
16999 && trailing_whitespace_p (glyph->charpos))
17000 {
17001 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17002 if (face_id < 0)
17003 return;
17004
17005 if (!row->reversed_p)
17006 {
17007 while (glyph >= start
17008 && BUFFERP (glyph->object)
17009 && (glyph->type == STRETCH_GLYPH
17010 || (glyph->type == CHAR_GLYPH
17011 && glyph->u.ch == ' ')))
17012 (glyph--)->face_id = face_id;
17013 }
17014 else
17015 {
17016 while (glyph <= start
17017 && BUFFERP (glyph->object)
17018 && (glyph->type == STRETCH_GLYPH
17019 || (glyph->type == CHAR_GLYPH
17020 && glyph->u.ch == ' ')))
17021 (glyph++)->face_id = face_id;
17022 }
17023 }
17024 }
17025 }
17026
17027
17028 /* Value is non-zero if glyph row ROW in window W should be
17029 used to hold the cursor. */
17030
17031 static int
17032 cursor_row_p (struct window *w, struct glyph_row *row)
17033 {
17034 int cursor_row_p = 1;
17035
17036 if (PT == CHARPOS (row->end.pos))
17037 {
17038 /* Suppose the row ends on a string.
17039 Unless the row is continued, that means it ends on a newline
17040 in the string. If it's anything other than a display string
17041 (e.g. a before-string from an overlay), we don't want the
17042 cursor there. (This heuristic seems to give the optimal
17043 behavior for the various types of multi-line strings.) */
17044 if (CHARPOS (row->end.string_pos) >= 0)
17045 {
17046 if (row->continued_p)
17047 cursor_row_p = 1;
17048 else
17049 {
17050 /* Check for `display' property. */
17051 struct glyph *beg = row->glyphs[TEXT_AREA];
17052 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17053 struct glyph *glyph;
17054
17055 cursor_row_p = 0;
17056 for (glyph = end; glyph >= beg; --glyph)
17057 if (STRINGP (glyph->object))
17058 {
17059 Lisp_Object prop
17060 = Fget_char_property (make_number (PT),
17061 Qdisplay, Qnil);
17062 cursor_row_p =
17063 (!NILP (prop)
17064 && display_prop_string_p (prop, glyph->object));
17065 break;
17066 }
17067 }
17068 }
17069 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17070 {
17071 /* If the row ends in middle of a real character,
17072 and the line is continued, we want the cursor here.
17073 That's because CHARPOS (ROW->end.pos) would equal
17074 PT if PT is before the character. */
17075 if (!row->ends_in_ellipsis_p)
17076 cursor_row_p = row->continued_p;
17077 else
17078 /* If the row ends in an ellipsis, then
17079 CHARPOS (ROW->end.pos) will equal point after the
17080 invisible text. We want that position to be displayed
17081 after the ellipsis. */
17082 cursor_row_p = 0;
17083 }
17084 /* If the row ends at ZV, display the cursor at the end of that
17085 row instead of at the start of the row below. */
17086 else if (row->ends_at_zv_p)
17087 cursor_row_p = 1;
17088 else
17089 cursor_row_p = 0;
17090 }
17091
17092 return cursor_row_p;
17093 }
17094
17095 \f
17096
17097 /* Push the display property PROP so that it will be rendered at the
17098 current position in IT. Return 1 if PROP was successfully pushed,
17099 0 otherwise. */
17100
17101 static int
17102 push_display_prop (struct it *it, Lisp_Object prop)
17103 {
17104 push_it (it);
17105
17106 if (STRINGP (prop))
17107 {
17108 if (SCHARS (prop) == 0)
17109 {
17110 pop_it (it);
17111 return 0;
17112 }
17113
17114 it->string = prop;
17115 it->multibyte_p = STRING_MULTIBYTE (it->string);
17116 it->current.overlay_string_index = -1;
17117 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17118 it->end_charpos = it->string_nchars = SCHARS (it->string);
17119 it->method = GET_FROM_STRING;
17120 it->stop_charpos = 0;
17121 }
17122 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17123 {
17124 it->method = GET_FROM_STRETCH;
17125 it->object = prop;
17126 }
17127 #ifdef HAVE_WINDOW_SYSTEM
17128 else if (IMAGEP (prop))
17129 {
17130 it->what = IT_IMAGE;
17131 it->image_id = lookup_image (it->f, prop);
17132 it->method = GET_FROM_IMAGE;
17133 }
17134 #endif /* HAVE_WINDOW_SYSTEM */
17135 else
17136 {
17137 pop_it (it); /* bogus display property, give up */
17138 return 0;
17139 }
17140
17141 return 1;
17142 }
17143
17144 /* Return the character-property PROP at the current position in IT. */
17145
17146 static Lisp_Object
17147 get_it_property (struct it *it, Lisp_Object prop)
17148 {
17149 Lisp_Object position;
17150
17151 if (STRINGP (it->object))
17152 position = make_number (IT_STRING_CHARPOS (*it));
17153 else if (BUFFERP (it->object))
17154 position = make_number (IT_CHARPOS (*it));
17155 else
17156 return Qnil;
17157
17158 return Fget_char_property (position, prop, it->object);
17159 }
17160
17161 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17162
17163 static void
17164 handle_line_prefix (struct it *it)
17165 {
17166 Lisp_Object prefix;
17167 if (it->continuation_lines_width > 0)
17168 {
17169 prefix = get_it_property (it, Qwrap_prefix);
17170 if (NILP (prefix))
17171 prefix = Vwrap_prefix;
17172 }
17173 else
17174 {
17175 prefix = get_it_property (it, Qline_prefix);
17176 if (NILP (prefix))
17177 prefix = Vline_prefix;
17178 }
17179 if (! NILP (prefix) && push_display_prop (it, prefix))
17180 {
17181 /* If the prefix is wider than the window, and we try to wrap
17182 it, it would acquire its own wrap prefix, and so on till the
17183 iterator stack overflows. So, don't wrap the prefix. */
17184 it->line_wrap = TRUNCATE;
17185 it->avoid_cursor_p = 1;
17186 }
17187 }
17188
17189 \f
17190
17191 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17192 only for R2L lines from display_line, when it decides that too many
17193 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be
17194 continued. */
17195 static void
17196 unproduce_glyphs (struct it *it, int n)
17197 {
17198 struct glyph *glyph, *end;
17199
17200 xassert (it->glyph_row);
17201 xassert (it->glyph_row->reversed_p);
17202 xassert (it->area == TEXT_AREA);
17203 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17204
17205 if (n > it->glyph_row->used[TEXT_AREA])
17206 n = it->glyph_row->used[TEXT_AREA];
17207 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17208 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17209 for ( ; glyph < end; glyph++)
17210 glyph[-n] = *glyph;
17211 }
17212
17213 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17214 and ROW->maxpos. */
17215 static void
17216 find_row_edges (struct it *it, struct glyph_row *row,
17217 EMACS_INT min_pos, EMACS_INT min_bpos,
17218 EMACS_INT max_pos, EMACS_INT max_bpos)
17219 {
17220 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17221 lines' rows is implemented for bidi-reordered rows. */
17222
17223 /* ROW->minpos is the value of min_pos, the minimal buffer position
17224 we have in ROW. */
17225 if (min_pos <= ZV)
17226 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17227 else
17228 {
17229 /* We didn't find _any_ valid buffer positions in any of the
17230 glyphs, so we must trust the iterator's computed
17231 positions. */
17232 row->minpos = row->start.pos;
17233 max_pos = CHARPOS (it->current.pos);
17234 max_bpos = BYTEPOS (it->current.pos);
17235 }
17236
17237 if (!max_pos)
17238 abort ();
17239
17240 /* Here are the various use-cases for ending the row, and the
17241 corresponding values for ROW->maxpos:
17242
17243 Line ends in a newline from buffer eol_pos + 1
17244 Line is continued from buffer max_pos + 1
17245 Line is truncated on right it->current.pos
17246 Line ends in a newline from string max_pos
17247 Line is continued from string max_pos
17248 Line is continued from display vector max_pos
17249 Line is entirely from a string min_pos == max_pos
17250 Line is entirely from a display vector min_pos == max_pos
17251 Line that ends at ZV ZV
17252
17253 If you discover other use-cases, please add them here as
17254 appropriate. */
17255 if (row->ends_at_zv_p)
17256 row->maxpos = it->current.pos;
17257 else if (row->used[TEXT_AREA])
17258 {
17259 if (row->ends_in_newline_from_string_p)
17260 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17261 else if (CHARPOS (it->eol_pos) > 0)
17262 SET_TEXT_POS (row->maxpos,
17263 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17264 else if (row->continued_p)
17265 {
17266 /* If max_pos is different from IT's current position, it
17267 means IT->method does not belong to the display element
17268 at max_pos. However, it also means that the display
17269 element at max_pos was displayed in its entirety on this
17270 line, which is equivalent to saying that the next line
17271 starts at the next buffer position. */
17272 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17273 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17274 else
17275 {
17276 INC_BOTH (max_pos, max_bpos);
17277 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17278 }
17279 }
17280 else if (row->truncated_on_right_p)
17281 /* display_line already called reseat_at_next_visible_line_start,
17282 which puts the iterator at the beginning of the next line, in
17283 the logical order. */
17284 row->maxpos = it->current.pos;
17285 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17286 /* A line that is entirely from a string/image/stretch... */
17287 row->maxpos = row->minpos;
17288 else
17289 abort ();
17290 }
17291 else
17292 row->maxpos = it->current.pos;
17293 }
17294
17295 /* Construct the glyph row IT->glyph_row in the desired matrix of
17296 IT->w from text at the current position of IT. See dispextern.h
17297 for an overview of struct it. Value is non-zero if
17298 IT->glyph_row displays text, as opposed to a line displaying ZV
17299 only. */
17300
17301 static int
17302 display_line (struct it *it)
17303 {
17304 struct glyph_row *row = it->glyph_row;
17305 Lisp_Object overlay_arrow_string;
17306 struct it wrap_it;
17307 int may_wrap = 0, wrap_x;
17308 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17309 int wrap_row_phys_ascent, wrap_row_phys_height;
17310 int wrap_row_extra_line_spacing;
17311 EMACS_INT wrap_row_min_pos, wrap_row_min_bpos;
17312 EMACS_INT wrap_row_max_pos, wrap_row_max_bpos;
17313 int cvpos;
17314 EMACS_INT min_pos = ZV + 1, min_bpos, max_pos = 0, max_bpos;
17315
17316 /* We always start displaying at hpos zero even if hscrolled. */
17317 xassert (it->hpos == 0 && it->current_x == 0);
17318
17319 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17320 >= it->w->desired_matrix->nrows)
17321 {
17322 it->w->nrows_scale_factor++;
17323 fonts_changed_p = 1;
17324 return 0;
17325 }
17326
17327 /* Is IT->w showing the region? */
17328 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17329
17330 /* Clear the result glyph row and enable it. */
17331 prepare_desired_row (row);
17332
17333 row->y = it->current_y;
17334 row->start = it->start;
17335 row->continuation_lines_width = it->continuation_lines_width;
17336 row->displays_text_p = 1;
17337 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17338 it->starts_in_middle_of_char_p = 0;
17339
17340 /* Arrange the overlays nicely for our purposes. Usually, we call
17341 display_line on only one line at a time, in which case this
17342 can't really hurt too much, or we call it on lines which appear
17343 one after another in the buffer, in which case all calls to
17344 recenter_overlay_lists but the first will be pretty cheap. */
17345 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17346
17347 /* Move over display elements that are not visible because we are
17348 hscrolled. This may stop at an x-position < IT->first_visible_x
17349 if the first glyph is partially visible or if we hit a line end. */
17350 if (it->current_x < it->first_visible_x)
17351 {
17352 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17353 MOVE_TO_POS | MOVE_TO_X);
17354 }
17355 else
17356 {
17357 /* We only do this when not calling `move_it_in_display_line_to'
17358 above, because move_it_in_display_line_to calls
17359 handle_line_prefix itself. */
17360 handle_line_prefix (it);
17361 }
17362
17363 /* Get the initial row height. This is either the height of the
17364 text hscrolled, if there is any, or zero. */
17365 row->ascent = it->max_ascent;
17366 row->height = it->max_ascent + it->max_descent;
17367 row->phys_ascent = it->max_phys_ascent;
17368 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17369 row->extra_line_spacing = it->max_extra_line_spacing;
17370
17371 /* Utility macro to record max and min buffer positions seen until now. */
17372 #define RECORD_MAX_MIN_POS(IT) \
17373 do \
17374 { \
17375 if (IT_CHARPOS (*(IT)) < min_pos) \
17376 { \
17377 min_pos = IT_CHARPOS (*(IT)); \
17378 min_bpos = IT_BYTEPOS (*(IT)); \
17379 } \
17380 if (IT_CHARPOS (*(IT)) > max_pos) \
17381 { \
17382 max_pos = IT_CHARPOS (*(IT)); \
17383 max_bpos = IT_BYTEPOS (*(IT)); \
17384 } \
17385 } \
17386 while (0)
17387
17388 /* Loop generating characters. The loop is left with IT on the next
17389 character to display. */
17390 while (1)
17391 {
17392 int n_glyphs_before, hpos_before, x_before;
17393 int x, i, nglyphs;
17394 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17395
17396 /* Retrieve the next thing to display. Value is zero if end of
17397 buffer reached. */
17398 if (!get_next_display_element (it))
17399 {
17400 /* Maybe add a space at the end of this line that is used to
17401 display the cursor there under X. Set the charpos of the
17402 first glyph of blank lines not corresponding to any text
17403 to -1. */
17404 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17405 row->exact_window_width_line_p = 1;
17406 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17407 || row->used[TEXT_AREA] == 0)
17408 {
17409 row->glyphs[TEXT_AREA]->charpos = -1;
17410 row->displays_text_p = 0;
17411
17412 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17413 && (!MINI_WINDOW_P (it->w)
17414 || (minibuf_level && EQ (it->window, minibuf_window))))
17415 row->indicate_empty_line_p = 1;
17416 }
17417
17418 it->continuation_lines_width = 0;
17419 row->ends_at_zv_p = 1;
17420 /* A row that displays right-to-left text must always have
17421 its last face extended all the way to the end of line,
17422 even if this row ends in ZV, because we still write to
17423 the screen left to right. */
17424 if (row->reversed_p)
17425 extend_face_to_end_of_line (it);
17426 break;
17427 }
17428
17429 /* Now, get the metrics of what we want to display. This also
17430 generates glyphs in `row' (which is IT->glyph_row). */
17431 n_glyphs_before = row->used[TEXT_AREA];
17432 x = it->current_x;
17433
17434 /* Remember the line height so far in case the next element doesn't
17435 fit on the line. */
17436 if (it->line_wrap != TRUNCATE)
17437 {
17438 ascent = it->max_ascent;
17439 descent = it->max_descent;
17440 phys_ascent = it->max_phys_ascent;
17441 phys_descent = it->max_phys_descent;
17442
17443 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17444 {
17445 if (IT_DISPLAYING_WHITESPACE (it))
17446 may_wrap = 1;
17447 else if (may_wrap)
17448 {
17449 wrap_it = *it;
17450 wrap_x = x;
17451 wrap_row_used = row->used[TEXT_AREA];
17452 wrap_row_ascent = row->ascent;
17453 wrap_row_height = row->height;
17454 wrap_row_phys_ascent = row->phys_ascent;
17455 wrap_row_phys_height = row->phys_height;
17456 wrap_row_extra_line_spacing = row->extra_line_spacing;
17457 wrap_row_min_pos = min_pos;
17458 wrap_row_min_bpos = min_bpos;
17459 wrap_row_max_pos = max_pos;
17460 wrap_row_max_bpos = max_bpos;
17461 may_wrap = 0;
17462 }
17463 }
17464 }
17465
17466 PRODUCE_GLYPHS (it);
17467
17468 /* If this display element was in marginal areas, continue with
17469 the next one. */
17470 if (it->area != TEXT_AREA)
17471 {
17472 row->ascent = max (row->ascent, it->max_ascent);
17473 row->height = max (row->height, it->max_ascent + it->max_descent);
17474 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17475 row->phys_height = max (row->phys_height,
17476 it->max_phys_ascent + it->max_phys_descent);
17477 row->extra_line_spacing = max (row->extra_line_spacing,
17478 it->max_extra_line_spacing);
17479 set_iterator_to_next (it, 1);
17480 continue;
17481 }
17482
17483 /* Does the display element fit on the line? If we truncate
17484 lines, we should draw past the right edge of the window. If
17485 we don't truncate, we want to stop so that we can display the
17486 continuation glyph before the right margin. If lines are
17487 continued, there are two possible strategies for characters
17488 resulting in more than 1 glyph (e.g. tabs): Display as many
17489 glyphs as possible in this line and leave the rest for the
17490 continuation line, or display the whole element in the next
17491 line. Original redisplay did the former, so we do it also. */
17492 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17493 hpos_before = it->hpos;
17494 x_before = x;
17495
17496 if (/* Not a newline. */
17497 nglyphs > 0
17498 /* Glyphs produced fit entirely in the line. */
17499 && it->current_x < it->last_visible_x)
17500 {
17501 it->hpos += nglyphs;
17502 row->ascent = max (row->ascent, it->max_ascent);
17503 row->height = max (row->height, it->max_ascent + it->max_descent);
17504 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17505 row->phys_height = max (row->phys_height,
17506 it->max_phys_ascent + it->max_phys_descent);
17507 row->extra_line_spacing = max (row->extra_line_spacing,
17508 it->max_extra_line_spacing);
17509 if (it->current_x - it->pixel_width < it->first_visible_x)
17510 row->x = x - it->first_visible_x;
17511 /* Record the maximum and minimum buffer positions seen so
17512 far in glyphs that will be displayed by this row. */
17513 if (it->bidi_p)
17514 RECORD_MAX_MIN_POS (it);
17515 }
17516 else
17517 {
17518 int new_x;
17519 struct glyph *glyph;
17520
17521 for (i = 0; i < nglyphs; ++i, x = new_x)
17522 {
17523 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17524 new_x = x + glyph->pixel_width;
17525
17526 if (/* Lines are continued. */
17527 it->line_wrap != TRUNCATE
17528 && (/* Glyph doesn't fit on the line. */
17529 new_x > it->last_visible_x
17530 /* Or it fits exactly on a window system frame. */
17531 || (new_x == it->last_visible_x
17532 && FRAME_WINDOW_P (it->f))))
17533 {
17534 /* End of a continued line. */
17535
17536 if (it->hpos == 0
17537 || (new_x == it->last_visible_x
17538 && FRAME_WINDOW_P (it->f)))
17539 {
17540 /* Current glyph is the only one on the line or
17541 fits exactly on the line. We must continue
17542 the line because we can't draw the cursor
17543 after the glyph. */
17544 row->continued_p = 1;
17545 it->current_x = new_x;
17546 it->continuation_lines_width += new_x;
17547 ++it->hpos;
17548 /* Record the maximum and minimum buffer
17549 positions seen so far in glyphs that will be
17550 displayed by this row. */
17551 if (it->bidi_p)
17552 RECORD_MAX_MIN_POS (it);
17553 if (i == nglyphs - 1)
17554 {
17555 /* If line-wrap is on, check if a previous
17556 wrap point was found. */
17557 if (wrap_row_used > 0
17558 /* Even if there is a previous wrap
17559 point, continue the line here as
17560 usual, if (i) the previous character
17561 was a space or tab AND (ii) the
17562 current character is not. */
17563 && (!may_wrap
17564 || IT_DISPLAYING_WHITESPACE (it)))
17565 goto back_to_wrap;
17566
17567 set_iterator_to_next (it, 1);
17568 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17569 {
17570 if (!get_next_display_element (it))
17571 {
17572 row->exact_window_width_line_p = 1;
17573 it->continuation_lines_width = 0;
17574 row->continued_p = 0;
17575 row->ends_at_zv_p = 1;
17576 }
17577 else if (ITERATOR_AT_END_OF_LINE_P (it))
17578 {
17579 row->continued_p = 0;
17580 row->exact_window_width_line_p = 1;
17581 }
17582 }
17583 }
17584 }
17585 else if (CHAR_GLYPH_PADDING_P (*glyph)
17586 && !FRAME_WINDOW_P (it->f))
17587 {
17588 /* A padding glyph that doesn't fit on this line.
17589 This means the whole character doesn't fit
17590 on the line. */
17591 if (row->reversed_p)
17592 unproduce_glyphs (it, row->used[TEXT_AREA]
17593 - n_glyphs_before);
17594 row->used[TEXT_AREA] = n_glyphs_before;
17595
17596 /* Fill the rest of the row with continuation
17597 glyphs like in 20.x. */
17598 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17599 < row->glyphs[1 + TEXT_AREA])
17600 produce_special_glyphs (it, IT_CONTINUATION);
17601
17602 row->continued_p = 1;
17603 it->current_x = x_before;
17604 it->continuation_lines_width += x_before;
17605
17606 /* Restore the height to what it was before the
17607 element not fitting on the line. */
17608 it->max_ascent = ascent;
17609 it->max_descent = descent;
17610 it->max_phys_ascent = phys_ascent;
17611 it->max_phys_descent = phys_descent;
17612 }
17613 else if (wrap_row_used > 0)
17614 {
17615 back_to_wrap:
17616 if (row->reversed_p)
17617 unproduce_glyphs (it,
17618 row->used[TEXT_AREA] - wrap_row_used);
17619 *it = wrap_it;
17620 it->continuation_lines_width += wrap_x;
17621 row->used[TEXT_AREA] = wrap_row_used;
17622 row->ascent = wrap_row_ascent;
17623 row->height = wrap_row_height;
17624 row->phys_ascent = wrap_row_phys_ascent;
17625 row->phys_height = wrap_row_phys_height;
17626 row->extra_line_spacing = wrap_row_extra_line_spacing;
17627 min_pos = wrap_row_min_pos;
17628 min_bpos = wrap_row_min_bpos;
17629 max_pos = wrap_row_max_pos;
17630 max_bpos = wrap_row_max_bpos;
17631 row->continued_p = 1;
17632 row->ends_at_zv_p = 0;
17633 row->exact_window_width_line_p = 0;
17634 it->continuation_lines_width += x;
17635
17636 /* Make sure that a non-default face is extended
17637 up to the right margin of the window. */
17638 extend_face_to_end_of_line (it);
17639 }
17640 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17641 {
17642 /* A TAB that extends past the right edge of the
17643 window. This produces a single glyph on
17644 window system frames. We leave the glyph in
17645 this row and let it fill the row, but don't
17646 consume the TAB. */
17647 it->continuation_lines_width += it->last_visible_x;
17648 row->ends_in_middle_of_char_p = 1;
17649 row->continued_p = 1;
17650 glyph->pixel_width = it->last_visible_x - x;
17651 it->starts_in_middle_of_char_p = 1;
17652 }
17653 else
17654 {
17655 /* Something other than a TAB that draws past
17656 the right edge of the window. Restore
17657 positions to values before the element. */
17658 if (row->reversed_p)
17659 unproduce_glyphs (it, row->used[TEXT_AREA]
17660 - (n_glyphs_before + i));
17661 row->used[TEXT_AREA] = n_glyphs_before + i;
17662
17663 /* Display continuation glyphs. */
17664 if (!FRAME_WINDOW_P (it->f))
17665 produce_special_glyphs (it, IT_CONTINUATION);
17666 row->continued_p = 1;
17667
17668 it->current_x = x_before;
17669 it->continuation_lines_width += x;
17670 extend_face_to_end_of_line (it);
17671
17672 if (nglyphs > 1 && i > 0)
17673 {
17674 row->ends_in_middle_of_char_p = 1;
17675 it->starts_in_middle_of_char_p = 1;
17676 }
17677
17678 /* Restore the height to what it was before the
17679 element not fitting on the line. */
17680 it->max_ascent = ascent;
17681 it->max_descent = descent;
17682 it->max_phys_ascent = phys_ascent;
17683 it->max_phys_descent = phys_descent;
17684 }
17685
17686 break;
17687 }
17688 else if (new_x > it->first_visible_x)
17689 {
17690 /* Increment number of glyphs actually displayed. */
17691 ++it->hpos;
17692
17693 /* Record the maximum and minimum buffer positions
17694 seen so far in glyphs that will be displayed by
17695 this row. */
17696 if (it->bidi_p)
17697 RECORD_MAX_MIN_POS (it);
17698
17699 if (x < it->first_visible_x)
17700 /* Glyph is partially visible, i.e. row starts at
17701 negative X position. */
17702 row->x = x - it->first_visible_x;
17703 }
17704 else
17705 {
17706 /* Glyph is completely off the left margin of the
17707 window. This should not happen because of the
17708 move_it_in_display_line at the start of this
17709 function, unless the text display area of the
17710 window is empty. */
17711 xassert (it->first_visible_x <= it->last_visible_x);
17712 }
17713 }
17714
17715 row->ascent = max (row->ascent, it->max_ascent);
17716 row->height = max (row->height, it->max_ascent + it->max_descent);
17717 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17718 row->phys_height = max (row->phys_height,
17719 it->max_phys_ascent + it->max_phys_descent);
17720 row->extra_line_spacing = max (row->extra_line_spacing,
17721 it->max_extra_line_spacing);
17722
17723 /* End of this display line if row is continued. */
17724 if (row->continued_p || row->ends_at_zv_p)
17725 break;
17726 }
17727
17728 at_end_of_line:
17729 /* Is this a line end? If yes, we're also done, after making
17730 sure that a non-default face is extended up to the right
17731 margin of the window. */
17732 if (ITERATOR_AT_END_OF_LINE_P (it))
17733 {
17734 int used_before = row->used[TEXT_AREA];
17735
17736 row->ends_in_newline_from_string_p = STRINGP (it->object);
17737
17738 /* Add a space at the end of the line that is used to
17739 display the cursor there. */
17740 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17741 append_space_for_newline (it, 0);
17742
17743 /* Extend the face to the end of the line. */
17744 extend_face_to_end_of_line (it);
17745
17746 /* Make sure we have the position. */
17747 if (used_before == 0)
17748 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17749
17750 /* Record the position of the newline, for use in
17751 find_row_edges. */
17752 it->eol_pos = it->current.pos;
17753
17754 /* Consume the line end. This skips over invisible lines. */
17755 set_iterator_to_next (it, 1);
17756 it->continuation_lines_width = 0;
17757 break;
17758 }
17759
17760 /* Proceed with next display element. Note that this skips
17761 over lines invisible because of selective display. */
17762 set_iterator_to_next (it, 1);
17763
17764 /* If we truncate lines, we are done when the last displayed
17765 glyphs reach past the right margin of the window. */
17766 if (it->line_wrap == TRUNCATE
17767 && (FRAME_WINDOW_P (it->f)
17768 ? (it->current_x >= it->last_visible_x)
17769 : (it->current_x > it->last_visible_x)))
17770 {
17771 /* Maybe add truncation glyphs. */
17772 if (!FRAME_WINDOW_P (it->f))
17773 {
17774 int i, n;
17775
17776 if (!row->reversed_p)
17777 {
17778 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17779 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17780 break;
17781 }
17782 else
17783 {
17784 for (i = 0; i < row->used[TEXT_AREA]; i++)
17785 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17786 break;
17787 /* Remove any padding glyphs at the front of ROW, to
17788 make room for the truncation glyphs we will be
17789 adding below. The loop below always inserts at
17790 least one truncation glyph, so also remove the
17791 last glyph added to ROW. */
17792 unproduce_glyphs (it, i + 1);
17793 /* Adjust i for the loop below. */
17794 i = row->used[TEXT_AREA] - (i + 1);
17795 }
17796
17797 for (n = row->used[TEXT_AREA]; i < n; ++i)
17798 {
17799 row->used[TEXT_AREA] = i;
17800 produce_special_glyphs (it, IT_TRUNCATION);
17801 }
17802 }
17803 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17804 {
17805 /* Don't truncate if we can overflow newline into fringe. */
17806 if (!get_next_display_element (it))
17807 {
17808 it->continuation_lines_width = 0;
17809 row->ends_at_zv_p = 1;
17810 row->exact_window_width_line_p = 1;
17811 break;
17812 }
17813 if (ITERATOR_AT_END_OF_LINE_P (it))
17814 {
17815 row->exact_window_width_line_p = 1;
17816 goto at_end_of_line;
17817 }
17818 }
17819
17820 row->truncated_on_right_p = 1;
17821 it->continuation_lines_width = 0;
17822 reseat_at_next_visible_line_start (it, 0);
17823 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17824 it->hpos = hpos_before;
17825 it->current_x = x_before;
17826 break;
17827 }
17828 }
17829
17830 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17831 at the left window margin. */
17832 if (it->first_visible_x
17833 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
17834 {
17835 if (!FRAME_WINDOW_P (it->f))
17836 insert_left_trunc_glyphs (it);
17837 row->truncated_on_left_p = 1;
17838 }
17839
17840 /* Remember the position at which this line ends.
17841
17842 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
17843 cannot be before the call to find_row_edges below, since that is
17844 where these positions are determined. */
17845 row->end = it->current;
17846 if (!it->bidi_p)
17847 {
17848 row->minpos = row->start.pos;
17849 row->maxpos = row->end.pos;
17850 }
17851 else
17852 {
17853 /* ROW->minpos and ROW->maxpos must be the smallest and
17854 `1 + the largest' buffer positions in ROW. But if ROW was
17855 bidi-reordered, these two positions can be anywhere in the
17856 row, so we must determine them now. */
17857 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
17858 }
17859
17860 /* If the start of this line is the overlay arrow-position, then
17861 mark this glyph row as the one containing the overlay arrow.
17862 This is clearly a mess with variable size fonts. It would be
17863 better to let it be displayed like cursors under X. */
17864 if ((row->displays_text_p || !overlay_arrow_seen)
17865 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17866 !NILP (overlay_arrow_string)))
17867 {
17868 /* Overlay arrow in window redisplay is a fringe bitmap. */
17869 if (STRINGP (overlay_arrow_string))
17870 {
17871 struct glyph_row *arrow_row
17872 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17873 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17874 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17875 struct glyph *p = row->glyphs[TEXT_AREA];
17876 struct glyph *p2, *end;
17877
17878 /* Copy the arrow glyphs. */
17879 while (glyph < arrow_end)
17880 *p++ = *glyph++;
17881
17882 /* Throw away padding glyphs. */
17883 p2 = p;
17884 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17885 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17886 ++p2;
17887 if (p2 > p)
17888 {
17889 while (p2 < end)
17890 *p++ = *p2++;
17891 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17892 }
17893 }
17894 else
17895 {
17896 xassert (INTEGERP (overlay_arrow_string));
17897 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17898 }
17899 overlay_arrow_seen = 1;
17900 }
17901
17902 /* Compute pixel dimensions of this line. */
17903 compute_line_metrics (it);
17904
17905 /* Record whether this row ends inside an ellipsis. */
17906 row->ends_in_ellipsis_p
17907 = (it->method == GET_FROM_DISPLAY_VECTOR
17908 && it->ellipsis_p);
17909
17910 /* Save fringe bitmaps in this row. */
17911 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17912 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17913 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17914 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17915
17916 it->left_user_fringe_bitmap = 0;
17917 it->left_user_fringe_face_id = 0;
17918 it->right_user_fringe_bitmap = 0;
17919 it->right_user_fringe_face_id = 0;
17920
17921 /* Maybe set the cursor. */
17922 cvpos = it->w->cursor.vpos;
17923 if ((cvpos < 0
17924 /* In bidi-reordered rows, keep checking for proper cursor
17925 position even if one has been found already, because buffer
17926 positions in such rows change non-linearly with ROW->VPOS,
17927 when a line is continued. One exception: when we are at ZV,
17928 display cursor on the first suitable glyph row, since all
17929 the empty rows after that also have their position set to ZV. */
17930 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17931 lines' rows is implemented for bidi-reordered rows. */
17932 || (it->bidi_p
17933 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
17934 && PT >= MATRIX_ROW_START_CHARPOS (row)
17935 && PT <= MATRIX_ROW_END_CHARPOS (row)
17936 && cursor_row_p (it->w, row))
17937 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17938
17939 /* Highlight trailing whitespace. */
17940 if (!NILP (Vshow_trailing_whitespace))
17941 highlight_trailing_whitespace (it->f, it->glyph_row);
17942
17943 /* Prepare for the next line. This line starts horizontally at (X
17944 HPOS) = (0 0). Vertical positions are incremented. As a
17945 convenience for the caller, IT->glyph_row is set to the next
17946 row to be used. */
17947 it->current_x = it->hpos = 0;
17948 it->current_y += row->height;
17949 SET_TEXT_POS (it->eol_pos, 0, 0);
17950 ++it->vpos;
17951 ++it->glyph_row;
17952 /* The next row should by default use the same value of the
17953 reversed_p flag as this one. set_iterator_to_next decides when
17954 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
17955 the flag accordingly. */
17956 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
17957 it->glyph_row->reversed_p = row->reversed_p;
17958 it->start = row->end;
17959 return row->displays_text_p;
17960
17961 #undef RECORD_MAX_MIN_POS
17962 }
17963
17964 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
17965 Scurrent_bidi_paragraph_direction, 0, 1, 0,
17966 doc: /* Return paragraph direction at point in BUFFER.
17967 Value is either `left-to-right' or `right-to-left'.
17968 If BUFFER is omitted or nil, it defaults to the current buffer.
17969
17970 Paragraph direction determines how the text in the paragraph is displayed.
17971 In left-to-right paragraphs, text begins at the left margin of the window
17972 and the reading direction is generally left to right. In right-to-left
17973 paragraphs, text begins at the right margin and is read from right to left.
17974
17975 See also `bidi-paragraph-direction'. */)
17976 (Lisp_Object buffer)
17977 {
17978 struct buffer *buf;
17979 struct buffer *old;
17980
17981 if (NILP (buffer))
17982 buf = current_buffer;
17983 else
17984 {
17985 CHECK_BUFFER (buffer);
17986 buf = XBUFFER (buffer);
17987 old = current_buffer;
17988 }
17989
17990 if (NILP (buf->bidi_display_reordering))
17991 return Qleft_to_right;
17992 else if (!NILP (buf->bidi_paragraph_direction))
17993 return buf->bidi_paragraph_direction;
17994 else
17995 {
17996 /* Determine the direction from buffer text. We could try to
17997 use current_matrix if it is up to date, but this seems fast
17998 enough as it is. */
17999 struct bidi_it itb;
18000 EMACS_INT pos = BUF_PT (buf);
18001 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18002 int c;
18003
18004 if (buf != current_buffer)
18005 set_buffer_temp (buf);
18006 /* bidi_paragraph_init finds the base direction of the paragraph
18007 by searching forward from paragraph start. We need the base
18008 direction of the current or _previous_ paragraph, so we need
18009 to make sure we are within that paragraph. To that end, find
18010 the previous non-empty line. */
18011 if (pos >= ZV && pos > BEGV)
18012 {
18013 pos--;
18014 bytepos = CHAR_TO_BYTE (pos);
18015 }
18016 while ((c = FETCH_BYTE (bytepos)) == '\n'
18017 || c == ' ' || c == '\t' || c == '\f')
18018 {
18019 if (bytepos <= BEGV_BYTE)
18020 break;
18021 bytepos--;
18022 pos--;
18023 }
18024 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18025 bytepos--;
18026 itb.charpos = pos;
18027 itb.bytepos = bytepos;
18028 itb.first_elt = 1;
18029 itb.separator_limit = -1;
18030 itb.paragraph_dir = NEUTRAL_DIR;
18031
18032 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18033 if (buf != current_buffer)
18034 set_buffer_temp (old);
18035 switch (itb.paragraph_dir)
18036 {
18037 case L2R:
18038 return Qleft_to_right;
18039 break;
18040 case R2L:
18041 return Qright_to_left;
18042 break;
18043 default:
18044 abort ();
18045 }
18046 }
18047 }
18048
18049
18050 \f
18051 /***********************************************************************
18052 Menu Bar
18053 ***********************************************************************/
18054
18055 /* Redisplay the menu bar in the frame for window W.
18056
18057 The menu bar of X frames that don't have X toolkit support is
18058 displayed in a special window W->frame->menu_bar_window.
18059
18060 The menu bar of terminal frames is treated specially as far as
18061 glyph matrices are concerned. Menu bar lines are not part of
18062 windows, so the update is done directly on the frame matrix rows
18063 for the menu bar. */
18064
18065 static void
18066 display_menu_bar (struct window *w)
18067 {
18068 struct frame *f = XFRAME (WINDOW_FRAME (w));
18069 struct it it;
18070 Lisp_Object items;
18071 int i;
18072
18073 /* Don't do all this for graphical frames. */
18074 #ifdef HAVE_NTGUI
18075 if (FRAME_W32_P (f))
18076 return;
18077 #endif
18078 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18079 if (FRAME_X_P (f))
18080 return;
18081 #endif
18082
18083 #ifdef HAVE_NS
18084 if (FRAME_NS_P (f))
18085 return;
18086 #endif /* HAVE_NS */
18087
18088 #ifdef USE_X_TOOLKIT
18089 xassert (!FRAME_WINDOW_P (f));
18090 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18091 it.first_visible_x = 0;
18092 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18093 #else /* not USE_X_TOOLKIT */
18094 if (FRAME_WINDOW_P (f))
18095 {
18096 /* Menu bar lines are displayed in the desired matrix of the
18097 dummy window menu_bar_window. */
18098 struct window *menu_w;
18099 xassert (WINDOWP (f->menu_bar_window));
18100 menu_w = XWINDOW (f->menu_bar_window);
18101 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18102 MENU_FACE_ID);
18103 it.first_visible_x = 0;
18104 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18105 }
18106 else
18107 {
18108 /* This is a TTY frame, i.e. character hpos/vpos are used as
18109 pixel x/y. */
18110 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18111 MENU_FACE_ID);
18112 it.first_visible_x = 0;
18113 it.last_visible_x = FRAME_COLS (f);
18114 }
18115 #endif /* not USE_X_TOOLKIT */
18116
18117 if (! mode_line_inverse_video)
18118 /* Force the menu-bar to be displayed in the default face. */
18119 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18120
18121 /* Clear all rows of the menu bar. */
18122 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18123 {
18124 struct glyph_row *row = it.glyph_row + i;
18125 clear_glyph_row (row);
18126 row->enabled_p = 1;
18127 row->full_width_p = 1;
18128 }
18129
18130 /* Display all items of the menu bar. */
18131 items = FRAME_MENU_BAR_ITEMS (it.f);
18132 for (i = 0; i < XVECTOR (items)->size; i += 4)
18133 {
18134 Lisp_Object string;
18135
18136 /* Stop at nil string. */
18137 string = AREF (items, i + 1);
18138 if (NILP (string))
18139 break;
18140
18141 /* Remember where item was displayed. */
18142 ASET (items, i + 3, make_number (it.hpos));
18143
18144 /* Display the item, pad with one space. */
18145 if (it.current_x < it.last_visible_x)
18146 display_string (NULL, string, Qnil, 0, 0, &it,
18147 SCHARS (string) + 1, 0, 0, -1);
18148 }
18149
18150 /* Fill out the line with spaces. */
18151 if (it.current_x < it.last_visible_x)
18152 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18153
18154 /* Compute the total height of the lines. */
18155 compute_line_metrics (&it);
18156 }
18157
18158
18159 \f
18160 /***********************************************************************
18161 Mode Line
18162 ***********************************************************************/
18163
18164 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18165 FORCE is non-zero, redisplay mode lines unconditionally.
18166 Otherwise, redisplay only mode lines that are garbaged. Value is
18167 the number of windows whose mode lines were redisplayed. */
18168
18169 static int
18170 redisplay_mode_lines (Lisp_Object window, int force)
18171 {
18172 int nwindows = 0;
18173
18174 while (!NILP (window))
18175 {
18176 struct window *w = XWINDOW (window);
18177
18178 if (WINDOWP (w->hchild))
18179 nwindows += redisplay_mode_lines (w->hchild, force);
18180 else if (WINDOWP (w->vchild))
18181 nwindows += redisplay_mode_lines (w->vchild, force);
18182 else if (force
18183 || FRAME_GARBAGED_P (XFRAME (w->frame))
18184 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18185 {
18186 struct text_pos lpoint;
18187 struct buffer *old = current_buffer;
18188
18189 /* Set the window's buffer for the mode line display. */
18190 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18191 set_buffer_internal_1 (XBUFFER (w->buffer));
18192
18193 /* Point refers normally to the selected window. For any
18194 other window, set up appropriate value. */
18195 if (!EQ (window, selected_window))
18196 {
18197 struct text_pos pt;
18198
18199 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18200 if (CHARPOS (pt) < BEGV)
18201 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18202 else if (CHARPOS (pt) > (ZV - 1))
18203 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18204 else
18205 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18206 }
18207
18208 /* Display mode lines. */
18209 clear_glyph_matrix (w->desired_matrix);
18210 if (display_mode_lines (w))
18211 {
18212 ++nwindows;
18213 w->must_be_updated_p = 1;
18214 }
18215
18216 /* Restore old settings. */
18217 set_buffer_internal_1 (old);
18218 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18219 }
18220
18221 window = w->next;
18222 }
18223
18224 return nwindows;
18225 }
18226
18227
18228 /* Display the mode and/or header line of window W. Value is the
18229 sum number of mode lines and header lines displayed. */
18230
18231 static int
18232 display_mode_lines (struct window *w)
18233 {
18234 Lisp_Object old_selected_window, old_selected_frame;
18235 int n = 0;
18236
18237 old_selected_frame = selected_frame;
18238 selected_frame = w->frame;
18239 old_selected_window = selected_window;
18240 XSETWINDOW (selected_window, w);
18241
18242 /* These will be set while the mode line specs are processed. */
18243 line_number_displayed = 0;
18244 w->column_number_displayed = Qnil;
18245
18246 if (WINDOW_WANTS_MODELINE_P (w))
18247 {
18248 struct window *sel_w = XWINDOW (old_selected_window);
18249
18250 /* Select mode line face based on the real selected window. */
18251 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18252 current_buffer->mode_line_format);
18253 ++n;
18254 }
18255
18256 if (WINDOW_WANTS_HEADER_LINE_P (w))
18257 {
18258 display_mode_line (w, HEADER_LINE_FACE_ID,
18259 current_buffer->header_line_format);
18260 ++n;
18261 }
18262
18263 selected_frame = old_selected_frame;
18264 selected_window = old_selected_window;
18265 return n;
18266 }
18267
18268
18269 /* Display mode or header line of window W. FACE_ID specifies which
18270 line to display; it is either MODE_LINE_FACE_ID or
18271 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18272 display. Value is the pixel height of the mode/header line
18273 displayed. */
18274
18275 static int
18276 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18277 {
18278 struct it it;
18279 struct face *face;
18280 int count = SPECPDL_INDEX ();
18281
18282 init_iterator (&it, w, -1, -1, NULL, face_id);
18283 /* Don't extend on a previously drawn mode-line.
18284 This may happen if called from pos_visible_p. */
18285 it.glyph_row->enabled_p = 0;
18286 prepare_desired_row (it.glyph_row);
18287
18288 it.glyph_row->mode_line_p = 1;
18289
18290 if (! mode_line_inverse_video)
18291 /* Force the mode-line to be displayed in the default face. */
18292 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18293
18294 record_unwind_protect (unwind_format_mode_line,
18295 format_mode_line_unwind_data (NULL, Qnil, 0));
18296
18297 mode_line_target = MODE_LINE_DISPLAY;
18298
18299 /* Temporarily make frame's keyboard the current kboard so that
18300 kboard-local variables in the mode_line_format will get the right
18301 values. */
18302 push_kboard (FRAME_KBOARD (it.f));
18303 record_unwind_save_match_data ();
18304 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18305 pop_kboard ();
18306
18307 unbind_to (count, Qnil);
18308
18309 /* Fill up with spaces. */
18310 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18311
18312 compute_line_metrics (&it);
18313 it.glyph_row->full_width_p = 1;
18314 it.glyph_row->continued_p = 0;
18315 it.glyph_row->truncated_on_left_p = 0;
18316 it.glyph_row->truncated_on_right_p = 0;
18317
18318 /* Make a 3D mode-line have a shadow at its right end. */
18319 face = FACE_FROM_ID (it.f, face_id);
18320 extend_face_to_end_of_line (&it);
18321 if (face->box != FACE_NO_BOX)
18322 {
18323 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18324 + it.glyph_row->used[TEXT_AREA] - 1);
18325 last->right_box_line_p = 1;
18326 }
18327
18328 return it.glyph_row->height;
18329 }
18330
18331 /* Move element ELT in LIST to the front of LIST.
18332 Return the updated list. */
18333
18334 static Lisp_Object
18335 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18336 {
18337 register Lisp_Object tail, prev;
18338 register Lisp_Object tem;
18339
18340 tail = list;
18341 prev = Qnil;
18342 while (CONSP (tail))
18343 {
18344 tem = XCAR (tail);
18345
18346 if (EQ (elt, tem))
18347 {
18348 /* Splice out the link TAIL. */
18349 if (NILP (prev))
18350 list = XCDR (tail);
18351 else
18352 Fsetcdr (prev, XCDR (tail));
18353
18354 /* Now make it the first. */
18355 Fsetcdr (tail, list);
18356 return tail;
18357 }
18358 else
18359 prev = tail;
18360 tail = XCDR (tail);
18361 QUIT;
18362 }
18363
18364 /* Not found--return unchanged LIST. */
18365 return list;
18366 }
18367
18368 /* Contribute ELT to the mode line for window IT->w. How it
18369 translates into text depends on its data type.
18370
18371 IT describes the display environment in which we display, as usual.
18372
18373 DEPTH is the depth in recursion. It is used to prevent
18374 infinite recursion here.
18375
18376 FIELD_WIDTH is the number of characters the display of ELT should
18377 occupy in the mode line, and PRECISION is the maximum number of
18378 characters to display from ELT's representation. See
18379 display_string for details.
18380
18381 Returns the hpos of the end of the text generated by ELT.
18382
18383 PROPS is a property list to add to any string we encounter.
18384
18385 If RISKY is nonzero, remove (disregard) any properties in any string
18386 we encounter, and ignore :eval and :propertize.
18387
18388 The global variable `mode_line_target' determines whether the
18389 output is passed to `store_mode_line_noprop',
18390 `store_mode_line_string', or `display_string'. */
18391
18392 static int
18393 display_mode_element (struct it *it, int depth, int field_width, int precision,
18394 Lisp_Object elt, Lisp_Object props, int risky)
18395 {
18396 int n = 0, field, prec;
18397 int literal = 0;
18398
18399 tail_recurse:
18400 if (depth > 100)
18401 elt = build_string ("*too-deep*");
18402
18403 depth++;
18404
18405 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18406 {
18407 case Lisp_String:
18408 {
18409 /* A string: output it and check for %-constructs within it. */
18410 unsigned char c;
18411 EMACS_INT offset = 0;
18412
18413 if (SCHARS (elt) > 0
18414 && (!NILP (props) || risky))
18415 {
18416 Lisp_Object oprops, aelt;
18417 oprops = Ftext_properties_at (make_number (0), elt);
18418
18419 /* If the starting string's properties are not what
18420 we want, translate the string. Also, if the string
18421 is risky, do that anyway. */
18422
18423 if (NILP (Fequal (props, oprops)) || risky)
18424 {
18425 /* If the starting string has properties,
18426 merge the specified ones onto the existing ones. */
18427 if (! NILP (oprops) && !risky)
18428 {
18429 Lisp_Object tem;
18430
18431 oprops = Fcopy_sequence (oprops);
18432 tem = props;
18433 while (CONSP (tem))
18434 {
18435 oprops = Fplist_put (oprops, XCAR (tem),
18436 XCAR (XCDR (tem)));
18437 tem = XCDR (XCDR (tem));
18438 }
18439 props = oprops;
18440 }
18441
18442 aelt = Fassoc (elt, mode_line_proptrans_alist);
18443 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18444 {
18445 /* AELT is what we want. Move it to the front
18446 without consing. */
18447 elt = XCAR (aelt);
18448 mode_line_proptrans_alist
18449 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18450 }
18451 else
18452 {
18453 Lisp_Object tem;
18454
18455 /* If AELT has the wrong props, it is useless.
18456 so get rid of it. */
18457 if (! NILP (aelt))
18458 mode_line_proptrans_alist
18459 = Fdelq (aelt, mode_line_proptrans_alist);
18460
18461 elt = Fcopy_sequence (elt);
18462 Fset_text_properties (make_number (0), Flength (elt),
18463 props, elt);
18464 /* Add this item to mode_line_proptrans_alist. */
18465 mode_line_proptrans_alist
18466 = Fcons (Fcons (elt, props),
18467 mode_line_proptrans_alist);
18468 /* Truncate mode_line_proptrans_alist
18469 to at most 50 elements. */
18470 tem = Fnthcdr (make_number (50),
18471 mode_line_proptrans_alist);
18472 if (! NILP (tem))
18473 XSETCDR (tem, Qnil);
18474 }
18475 }
18476 }
18477
18478 offset = 0;
18479
18480 if (literal)
18481 {
18482 prec = precision - n;
18483 switch (mode_line_target)
18484 {
18485 case MODE_LINE_NOPROP:
18486 case MODE_LINE_TITLE:
18487 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18488 break;
18489 case MODE_LINE_STRING:
18490 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18491 break;
18492 case MODE_LINE_DISPLAY:
18493 n += display_string (NULL, elt, Qnil, 0, 0, it,
18494 0, prec, 0, STRING_MULTIBYTE (elt));
18495 break;
18496 }
18497
18498 break;
18499 }
18500
18501 /* Handle the non-literal case. */
18502
18503 while ((precision <= 0 || n < precision)
18504 && SREF (elt, offset) != 0
18505 && (mode_line_target != MODE_LINE_DISPLAY
18506 || it->current_x < it->last_visible_x))
18507 {
18508 EMACS_INT last_offset = offset;
18509
18510 /* Advance to end of string or next format specifier. */
18511 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18512 ;
18513
18514 if (offset - 1 != last_offset)
18515 {
18516 EMACS_INT nchars, nbytes;
18517
18518 /* Output to end of string or up to '%'. Field width
18519 is length of string. Don't output more than
18520 PRECISION allows us. */
18521 offset--;
18522
18523 prec = c_string_width (SDATA (elt) + last_offset,
18524 offset - last_offset, precision - n,
18525 &nchars, &nbytes);
18526
18527 switch (mode_line_target)
18528 {
18529 case MODE_LINE_NOPROP:
18530 case MODE_LINE_TITLE:
18531 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18532 break;
18533 case MODE_LINE_STRING:
18534 {
18535 EMACS_INT bytepos = last_offset;
18536 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18537 EMACS_INT endpos = (precision <= 0
18538 ? string_byte_to_char (elt, offset)
18539 : charpos + nchars);
18540
18541 n += store_mode_line_string (NULL,
18542 Fsubstring (elt, make_number (charpos),
18543 make_number (endpos)),
18544 0, 0, 0, Qnil);
18545 }
18546 break;
18547 case MODE_LINE_DISPLAY:
18548 {
18549 EMACS_INT bytepos = last_offset;
18550 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18551
18552 if (precision <= 0)
18553 nchars = string_byte_to_char (elt, offset) - charpos;
18554 n += display_string (NULL, elt, Qnil, 0, charpos,
18555 it, 0, nchars, 0,
18556 STRING_MULTIBYTE (elt));
18557 }
18558 break;
18559 }
18560 }
18561 else /* c == '%' */
18562 {
18563 EMACS_INT percent_position = offset;
18564
18565 /* Get the specified minimum width. Zero means
18566 don't pad. */
18567 field = 0;
18568 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18569 field = field * 10 + c - '0';
18570
18571 /* Don't pad beyond the total padding allowed. */
18572 if (field_width - n > 0 && field > field_width - n)
18573 field = field_width - n;
18574
18575 /* Note that either PRECISION <= 0 or N < PRECISION. */
18576 prec = precision - n;
18577
18578 if (c == 'M')
18579 n += display_mode_element (it, depth, field, prec,
18580 Vglobal_mode_string, props,
18581 risky);
18582 else if (c != 0)
18583 {
18584 int multibyte;
18585 EMACS_INT bytepos, charpos;
18586 const unsigned char *spec;
18587 Lisp_Object string;
18588
18589 bytepos = percent_position;
18590 charpos = (STRING_MULTIBYTE (elt)
18591 ? string_byte_to_char (elt, bytepos)
18592 : bytepos);
18593 spec = decode_mode_spec (it->w, c, field, prec, &string);
18594 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18595
18596 switch (mode_line_target)
18597 {
18598 case MODE_LINE_NOPROP:
18599 case MODE_LINE_TITLE:
18600 n += store_mode_line_noprop (spec, field, prec);
18601 break;
18602 case MODE_LINE_STRING:
18603 {
18604 int len = strlen (spec);
18605 Lisp_Object tem = make_string (spec, len);
18606 props = Ftext_properties_at (make_number (charpos), elt);
18607 /* Should only keep face property in props */
18608 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18609 }
18610 break;
18611 case MODE_LINE_DISPLAY:
18612 {
18613 int nglyphs_before, nwritten;
18614
18615 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18616 nwritten = display_string (spec, string, elt,
18617 charpos, 0, it,
18618 field, prec, 0,
18619 multibyte);
18620
18621 /* Assign to the glyphs written above the
18622 string where the `%x' came from, position
18623 of the `%'. */
18624 if (nwritten > 0)
18625 {
18626 struct glyph *glyph
18627 = (it->glyph_row->glyphs[TEXT_AREA]
18628 + nglyphs_before);
18629 int i;
18630
18631 for (i = 0; i < nwritten; ++i)
18632 {
18633 glyph[i].object = elt;
18634 glyph[i].charpos = charpos;
18635 }
18636
18637 n += nwritten;
18638 }
18639 }
18640 break;
18641 }
18642 }
18643 else /* c == 0 */
18644 break;
18645 }
18646 }
18647 }
18648 break;
18649
18650 case Lisp_Symbol:
18651 /* A symbol: process the value of the symbol recursively
18652 as if it appeared here directly. Avoid error if symbol void.
18653 Special case: if value of symbol is a string, output the string
18654 literally. */
18655 {
18656 register Lisp_Object tem;
18657
18658 /* If the variable is not marked as risky to set
18659 then its contents are risky to use. */
18660 if (NILP (Fget (elt, Qrisky_local_variable)))
18661 risky = 1;
18662
18663 tem = Fboundp (elt);
18664 if (!NILP (tem))
18665 {
18666 tem = Fsymbol_value (elt);
18667 /* If value is a string, output that string literally:
18668 don't check for % within it. */
18669 if (STRINGP (tem))
18670 literal = 1;
18671
18672 if (!EQ (tem, elt))
18673 {
18674 /* Give up right away for nil or t. */
18675 elt = tem;
18676 goto tail_recurse;
18677 }
18678 }
18679 }
18680 break;
18681
18682 case Lisp_Cons:
18683 {
18684 register Lisp_Object car, tem;
18685
18686 /* A cons cell: five distinct cases.
18687 If first element is :eval or :propertize, do something special.
18688 If first element is a string or a cons, process all the elements
18689 and effectively concatenate them.
18690 If first element is a negative number, truncate displaying cdr to
18691 at most that many characters. If positive, pad (with spaces)
18692 to at least that many characters.
18693 If first element is a symbol, process the cadr or caddr recursively
18694 according to whether the symbol's value is non-nil or nil. */
18695 car = XCAR (elt);
18696 if (EQ (car, QCeval))
18697 {
18698 /* An element of the form (:eval FORM) means evaluate FORM
18699 and use the result as mode line elements. */
18700
18701 if (risky)
18702 break;
18703
18704 if (CONSP (XCDR (elt)))
18705 {
18706 Lisp_Object spec;
18707 spec = safe_eval (XCAR (XCDR (elt)));
18708 n += display_mode_element (it, depth, field_width - n,
18709 precision - n, spec, props,
18710 risky);
18711 }
18712 }
18713 else if (EQ (car, QCpropertize))
18714 {
18715 /* An element of the form (:propertize ELT PROPS...)
18716 means display ELT but applying properties PROPS. */
18717
18718 if (risky)
18719 break;
18720
18721 if (CONSP (XCDR (elt)))
18722 n += display_mode_element (it, depth, field_width - n,
18723 precision - n, XCAR (XCDR (elt)),
18724 XCDR (XCDR (elt)), risky);
18725 }
18726 else if (SYMBOLP (car))
18727 {
18728 tem = Fboundp (car);
18729 elt = XCDR (elt);
18730 if (!CONSP (elt))
18731 goto invalid;
18732 /* elt is now the cdr, and we know it is a cons cell.
18733 Use its car if CAR has a non-nil value. */
18734 if (!NILP (tem))
18735 {
18736 tem = Fsymbol_value (car);
18737 if (!NILP (tem))
18738 {
18739 elt = XCAR (elt);
18740 goto tail_recurse;
18741 }
18742 }
18743 /* Symbol's value is nil (or symbol is unbound)
18744 Get the cddr of the original list
18745 and if possible find the caddr and use that. */
18746 elt = XCDR (elt);
18747 if (NILP (elt))
18748 break;
18749 else if (!CONSP (elt))
18750 goto invalid;
18751 elt = XCAR (elt);
18752 goto tail_recurse;
18753 }
18754 else if (INTEGERP (car))
18755 {
18756 register int lim = XINT (car);
18757 elt = XCDR (elt);
18758 if (lim < 0)
18759 {
18760 /* Negative int means reduce maximum width. */
18761 if (precision <= 0)
18762 precision = -lim;
18763 else
18764 precision = min (precision, -lim);
18765 }
18766 else if (lim > 0)
18767 {
18768 /* Padding specified. Don't let it be more than
18769 current maximum. */
18770 if (precision > 0)
18771 lim = min (precision, lim);
18772
18773 /* If that's more padding than already wanted, queue it.
18774 But don't reduce padding already specified even if
18775 that is beyond the current truncation point. */
18776 field_width = max (lim, field_width);
18777 }
18778 goto tail_recurse;
18779 }
18780 else if (STRINGP (car) || CONSP (car))
18781 {
18782 Lisp_Object halftail = elt;
18783 int len = 0;
18784
18785 while (CONSP (elt)
18786 && (precision <= 0 || n < precision))
18787 {
18788 n += display_mode_element (it, depth,
18789 /* Do padding only after the last
18790 element in the list. */
18791 (! CONSP (XCDR (elt))
18792 ? field_width - n
18793 : 0),
18794 precision - n, XCAR (elt),
18795 props, risky);
18796 elt = XCDR (elt);
18797 len++;
18798 if ((len & 1) == 0)
18799 halftail = XCDR (halftail);
18800 /* Check for cycle. */
18801 if (EQ (halftail, elt))
18802 break;
18803 }
18804 }
18805 }
18806 break;
18807
18808 default:
18809 invalid:
18810 elt = build_string ("*invalid*");
18811 goto tail_recurse;
18812 }
18813
18814 /* Pad to FIELD_WIDTH. */
18815 if (field_width > 0 && n < field_width)
18816 {
18817 switch (mode_line_target)
18818 {
18819 case MODE_LINE_NOPROP:
18820 case MODE_LINE_TITLE:
18821 n += store_mode_line_noprop ("", field_width - n, 0);
18822 break;
18823 case MODE_LINE_STRING:
18824 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18825 break;
18826 case MODE_LINE_DISPLAY:
18827 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18828 0, 0, 0);
18829 break;
18830 }
18831 }
18832
18833 return n;
18834 }
18835
18836 /* Store a mode-line string element in mode_line_string_list.
18837
18838 If STRING is non-null, display that C string. Otherwise, the Lisp
18839 string LISP_STRING is displayed.
18840
18841 FIELD_WIDTH is the minimum number of output glyphs to produce.
18842 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18843 with spaces. FIELD_WIDTH <= 0 means don't pad.
18844
18845 PRECISION is the maximum number of characters to output from
18846 STRING. PRECISION <= 0 means don't truncate the string.
18847
18848 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18849 properties to the string.
18850
18851 PROPS are the properties to add to the string.
18852 The mode_line_string_face face property is always added to the string.
18853 */
18854
18855 static int
18856 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
18857 int field_width, int precision, Lisp_Object props)
18858 {
18859 EMACS_INT len;
18860 int n = 0;
18861
18862 if (string != NULL)
18863 {
18864 len = strlen (string);
18865 if (precision > 0 && len > precision)
18866 len = precision;
18867 lisp_string = make_string (string, len);
18868 if (NILP (props))
18869 props = mode_line_string_face_prop;
18870 else if (!NILP (mode_line_string_face))
18871 {
18872 Lisp_Object face = Fplist_get (props, Qface);
18873 props = Fcopy_sequence (props);
18874 if (NILP (face))
18875 face = mode_line_string_face;
18876 else
18877 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18878 props = Fplist_put (props, Qface, face);
18879 }
18880 Fadd_text_properties (make_number (0), make_number (len),
18881 props, lisp_string);
18882 }
18883 else
18884 {
18885 len = XFASTINT (Flength (lisp_string));
18886 if (precision > 0 && len > precision)
18887 {
18888 len = precision;
18889 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
18890 precision = -1;
18891 }
18892 if (!NILP (mode_line_string_face))
18893 {
18894 Lisp_Object face;
18895 if (NILP (props))
18896 props = Ftext_properties_at (make_number (0), lisp_string);
18897 face = Fplist_get (props, Qface);
18898 if (NILP (face))
18899 face = mode_line_string_face;
18900 else
18901 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18902 props = Fcons (Qface, Fcons (face, Qnil));
18903 if (copy_string)
18904 lisp_string = Fcopy_sequence (lisp_string);
18905 }
18906 if (!NILP (props))
18907 Fadd_text_properties (make_number (0), make_number (len),
18908 props, lisp_string);
18909 }
18910
18911 if (len > 0)
18912 {
18913 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18914 n += len;
18915 }
18916
18917 if (field_width > len)
18918 {
18919 field_width -= len;
18920 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
18921 if (!NILP (props))
18922 Fadd_text_properties (make_number (0), make_number (field_width),
18923 props, lisp_string);
18924 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18925 n += field_width;
18926 }
18927
18928 return n;
18929 }
18930
18931
18932 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
18933 1, 4, 0,
18934 doc: /* Format a string out of a mode line format specification.
18935 First arg FORMAT specifies the mode line format (see `mode-line-format'
18936 for details) to use.
18937
18938 Optional second arg FACE specifies the face property to put
18939 on all characters for which no face is specified.
18940 The value t means whatever face the window's mode line currently uses
18941 \(either `mode-line' or `mode-line-inactive', depending).
18942 A value of nil means the default is no face property.
18943 If FACE is an integer, the value string has no text properties.
18944
18945 Optional third and fourth args WINDOW and BUFFER specify the window
18946 and buffer to use as the context for the formatting (defaults
18947 are the selected window and the window's buffer). */)
18948 (Lisp_Object format, Lisp_Object face, Lisp_Object window, Lisp_Object buffer)
18949 {
18950 struct it it;
18951 int len;
18952 struct window *w;
18953 struct buffer *old_buffer = NULL;
18954 int face_id = -1;
18955 int no_props = INTEGERP (face);
18956 int count = SPECPDL_INDEX ();
18957 Lisp_Object str;
18958 int string_start = 0;
18959
18960 if (NILP (window))
18961 window = selected_window;
18962 CHECK_WINDOW (window);
18963 w = XWINDOW (window);
18964
18965 if (NILP (buffer))
18966 buffer = w->buffer;
18967 CHECK_BUFFER (buffer);
18968
18969 /* Make formatting the modeline a non-op when noninteractive, otherwise
18970 there will be problems later caused by a partially initialized frame. */
18971 if (NILP (format) || noninteractive)
18972 return empty_unibyte_string;
18973
18974 if (no_props)
18975 face = Qnil;
18976
18977 if (!NILP (face))
18978 {
18979 if (EQ (face, Qt))
18980 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18981 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18982 }
18983
18984 if (face_id < 0)
18985 face_id = DEFAULT_FACE_ID;
18986
18987 if (XBUFFER (buffer) != current_buffer)
18988 old_buffer = current_buffer;
18989
18990 /* Save things including mode_line_proptrans_alist,
18991 and set that to nil so that we don't alter the outer value. */
18992 record_unwind_protect (unwind_format_mode_line,
18993 format_mode_line_unwind_data
18994 (old_buffer, selected_window, 1));
18995 mode_line_proptrans_alist = Qnil;
18996
18997 Fselect_window (window, Qt);
18998 if (old_buffer)
18999 set_buffer_internal_1 (XBUFFER (buffer));
19000
19001 init_iterator (&it, w, -1, -1, NULL, face_id);
19002
19003 if (no_props)
19004 {
19005 mode_line_target = MODE_LINE_NOPROP;
19006 mode_line_string_face_prop = Qnil;
19007 mode_line_string_list = Qnil;
19008 string_start = MODE_LINE_NOPROP_LEN (0);
19009 }
19010 else
19011 {
19012 mode_line_target = MODE_LINE_STRING;
19013 mode_line_string_list = Qnil;
19014 mode_line_string_face = face;
19015 mode_line_string_face_prop
19016 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19017 }
19018
19019 push_kboard (FRAME_KBOARD (it.f));
19020 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19021 pop_kboard ();
19022
19023 if (no_props)
19024 {
19025 len = MODE_LINE_NOPROP_LEN (string_start);
19026 str = make_string (mode_line_noprop_buf + string_start, len);
19027 }
19028 else
19029 {
19030 mode_line_string_list = Fnreverse (mode_line_string_list);
19031 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19032 empty_unibyte_string);
19033 }
19034
19035 unbind_to (count, Qnil);
19036 return str;
19037 }
19038
19039 /* Write a null-terminated, right justified decimal representation of
19040 the positive integer D to BUF using a minimal field width WIDTH. */
19041
19042 static void
19043 pint2str (register char *buf, register int width, register int d)
19044 {
19045 register char *p = buf;
19046
19047 if (d <= 0)
19048 *p++ = '0';
19049 else
19050 {
19051 while (d > 0)
19052 {
19053 *p++ = d % 10 + '0';
19054 d /= 10;
19055 }
19056 }
19057
19058 for (width -= (int) (p - buf); width > 0; --width)
19059 *p++ = ' ';
19060 *p-- = '\0';
19061 while (p > buf)
19062 {
19063 d = *buf;
19064 *buf++ = *p;
19065 *p-- = d;
19066 }
19067 }
19068
19069 /* Write a null-terminated, right justified decimal and "human
19070 readable" representation of the nonnegative integer D to BUF using
19071 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19072
19073 static const char power_letter[] =
19074 {
19075 0, /* not used */
19076 'k', /* kilo */
19077 'M', /* mega */
19078 'G', /* giga */
19079 'T', /* tera */
19080 'P', /* peta */
19081 'E', /* exa */
19082 'Z', /* zetta */
19083 'Y' /* yotta */
19084 };
19085
19086 static void
19087 pint2hrstr (char *buf, int width, int d)
19088 {
19089 /* We aim to represent the nonnegative integer D as
19090 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19091 int quotient = d;
19092 int remainder = 0;
19093 /* -1 means: do not use TENTHS. */
19094 int tenths = -1;
19095 int exponent = 0;
19096
19097 /* Length of QUOTIENT.TENTHS as a string. */
19098 int length;
19099
19100 char * psuffix;
19101 char * p;
19102
19103 if (1000 <= quotient)
19104 {
19105 /* Scale to the appropriate EXPONENT. */
19106 do
19107 {
19108 remainder = quotient % 1000;
19109 quotient /= 1000;
19110 exponent++;
19111 }
19112 while (1000 <= quotient);
19113
19114 /* Round to nearest and decide whether to use TENTHS or not. */
19115 if (quotient <= 9)
19116 {
19117 tenths = remainder / 100;
19118 if (50 <= remainder % 100)
19119 {
19120 if (tenths < 9)
19121 tenths++;
19122 else
19123 {
19124 quotient++;
19125 if (quotient == 10)
19126 tenths = -1;
19127 else
19128 tenths = 0;
19129 }
19130 }
19131 }
19132 else
19133 if (500 <= remainder)
19134 {
19135 if (quotient < 999)
19136 quotient++;
19137 else
19138 {
19139 quotient = 1;
19140 exponent++;
19141 tenths = 0;
19142 }
19143 }
19144 }
19145
19146 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19147 if (tenths == -1 && quotient <= 99)
19148 if (quotient <= 9)
19149 length = 1;
19150 else
19151 length = 2;
19152 else
19153 length = 3;
19154 p = psuffix = buf + max (width, length);
19155
19156 /* Print EXPONENT. */
19157 if (exponent)
19158 *psuffix++ = power_letter[exponent];
19159 *psuffix = '\0';
19160
19161 /* Print TENTHS. */
19162 if (tenths >= 0)
19163 {
19164 *--p = '0' + tenths;
19165 *--p = '.';
19166 }
19167
19168 /* Print QUOTIENT. */
19169 do
19170 {
19171 int digit = quotient % 10;
19172 *--p = '0' + digit;
19173 }
19174 while ((quotient /= 10) != 0);
19175
19176 /* Print leading spaces. */
19177 while (buf < p)
19178 *--p = ' ';
19179 }
19180
19181 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19182 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19183 type of CODING_SYSTEM. Return updated pointer into BUF. */
19184
19185 static unsigned char invalid_eol_type[] = "(*invalid*)";
19186
19187 static char *
19188 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19189 {
19190 Lisp_Object val;
19191 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
19192 const unsigned char *eol_str;
19193 int eol_str_len;
19194 /* The EOL conversion we are using. */
19195 Lisp_Object eoltype;
19196
19197 val = CODING_SYSTEM_SPEC (coding_system);
19198 eoltype = Qnil;
19199
19200 if (!VECTORP (val)) /* Not yet decided. */
19201 {
19202 if (multibyte)
19203 *buf++ = '-';
19204 if (eol_flag)
19205 eoltype = eol_mnemonic_undecided;
19206 /* Don't mention EOL conversion if it isn't decided. */
19207 }
19208 else
19209 {
19210 Lisp_Object attrs;
19211 Lisp_Object eolvalue;
19212
19213 attrs = AREF (val, 0);
19214 eolvalue = AREF (val, 2);
19215
19216 if (multibyte)
19217 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19218
19219 if (eol_flag)
19220 {
19221 /* The EOL conversion that is normal on this system. */
19222
19223 if (NILP (eolvalue)) /* Not yet decided. */
19224 eoltype = eol_mnemonic_undecided;
19225 else if (VECTORP (eolvalue)) /* Not yet decided. */
19226 eoltype = eol_mnemonic_undecided;
19227 else /* eolvalue is Qunix, Qdos, or Qmac. */
19228 eoltype = (EQ (eolvalue, Qunix)
19229 ? eol_mnemonic_unix
19230 : (EQ (eolvalue, Qdos) == 1
19231 ? eol_mnemonic_dos : eol_mnemonic_mac));
19232 }
19233 }
19234
19235 if (eol_flag)
19236 {
19237 /* Mention the EOL conversion if it is not the usual one. */
19238 if (STRINGP (eoltype))
19239 {
19240 eol_str = SDATA (eoltype);
19241 eol_str_len = SBYTES (eoltype);
19242 }
19243 else if (CHARACTERP (eoltype))
19244 {
19245 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19246 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19247 eol_str = tmp;
19248 }
19249 else
19250 {
19251 eol_str = invalid_eol_type;
19252 eol_str_len = sizeof (invalid_eol_type) - 1;
19253 }
19254 memcpy (buf, eol_str, eol_str_len);
19255 buf += eol_str_len;
19256 }
19257
19258 return buf;
19259 }
19260
19261 /* Return a string for the output of a mode line %-spec for window W,
19262 generated by character C. PRECISION >= 0 means don't return a
19263 string longer than that value. FIELD_WIDTH > 0 means pad the
19264 string returned with spaces to that value. Return a Lisp string in
19265 *STRING if the resulting string is taken from that Lisp string.
19266
19267 Note we operate on the current buffer for most purposes,
19268 the exception being w->base_line_pos. */
19269
19270 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19271
19272 static const char *
19273 decode_mode_spec (struct window *w, register int c, int field_width,
19274 int precision, Lisp_Object *string)
19275 {
19276 Lisp_Object obj;
19277 struct frame *f = XFRAME (WINDOW_FRAME (w));
19278 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19279 struct buffer *b = current_buffer;
19280
19281 obj = Qnil;
19282 *string = Qnil;
19283
19284 switch (c)
19285 {
19286 case '*':
19287 if (!NILP (b->read_only))
19288 return "%";
19289 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19290 return "*";
19291 return "-";
19292
19293 case '+':
19294 /* This differs from %* only for a modified read-only buffer. */
19295 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19296 return "*";
19297 if (!NILP (b->read_only))
19298 return "%";
19299 return "-";
19300
19301 case '&':
19302 /* This differs from %* in ignoring read-only-ness. */
19303 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19304 return "*";
19305 return "-";
19306
19307 case '%':
19308 return "%";
19309
19310 case '[':
19311 {
19312 int i;
19313 char *p;
19314
19315 if (command_loop_level > 5)
19316 return "[[[... ";
19317 p = decode_mode_spec_buf;
19318 for (i = 0; i < command_loop_level; i++)
19319 *p++ = '[';
19320 *p = 0;
19321 return decode_mode_spec_buf;
19322 }
19323
19324 case ']':
19325 {
19326 int i;
19327 char *p;
19328
19329 if (command_loop_level > 5)
19330 return " ...]]]";
19331 p = decode_mode_spec_buf;
19332 for (i = 0; i < command_loop_level; i++)
19333 *p++ = ']';
19334 *p = 0;
19335 return decode_mode_spec_buf;
19336 }
19337
19338 case '-':
19339 {
19340 register int i;
19341
19342 /* Let lots_of_dashes be a string of infinite length. */
19343 if (mode_line_target == MODE_LINE_NOPROP ||
19344 mode_line_target == MODE_LINE_STRING)
19345 return "--";
19346 if (field_width <= 0
19347 || field_width > sizeof (lots_of_dashes))
19348 {
19349 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19350 decode_mode_spec_buf[i] = '-';
19351 decode_mode_spec_buf[i] = '\0';
19352 return decode_mode_spec_buf;
19353 }
19354 else
19355 return lots_of_dashes;
19356 }
19357
19358 case 'b':
19359 obj = b->name;
19360 break;
19361
19362 case 'c':
19363 /* %c and %l are ignored in `frame-title-format'.
19364 (In redisplay_internal, the frame title is drawn _before_ the
19365 windows are updated, so the stuff which depends on actual
19366 window contents (such as %l) may fail to render properly, or
19367 even crash emacs.) */
19368 if (mode_line_target == MODE_LINE_TITLE)
19369 return "";
19370 else
19371 {
19372 int col = (int) current_column (); /* iftc */
19373 w->column_number_displayed = make_number (col);
19374 pint2str (decode_mode_spec_buf, field_width, col);
19375 return decode_mode_spec_buf;
19376 }
19377
19378 case 'e':
19379 #ifndef SYSTEM_MALLOC
19380 {
19381 if (NILP (Vmemory_full))
19382 return "";
19383 else
19384 return "!MEM FULL! ";
19385 }
19386 #else
19387 return "";
19388 #endif
19389
19390 case 'F':
19391 /* %F displays the frame name. */
19392 if (!NILP (f->title))
19393 return (char *) SDATA (f->title);
19394 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19395 return (char *) SDATA (f->name);
19396 return "Emacs";
19397
19398 case 'f':
19399 obj = b->filename;
19400 break;
19401
19402 case 'i':
19403 {
19404 EMACS_INT size = ZV - BEGV;
19405 pint2str (decode_mode_spec_buf, field_width, size);
19406 return decode_mode_spec_buf;
19407 }
19408
19409 case 'I':
19410 {
19411 EMACS_INT size = ZV - BEGV;
19412 pint2hrstr (decode_mode_spec_buf, field_width, size);
19413 return decode_mode_spec_buf;
19414 }
19415
19416 case 'l':
19417 {
19418 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
19419 int topline, nlines, height;
19420 EMACS_INT junk;
19421
19422 /* %c and %l are ignored in `frame-title-format'. */
19423 if (mode_line_target == MODE_LINE_TITLE)
19424 return "";
19425
19426 startpos = XMARKER (w->start)->charpos;
19427 startpos_byte = marker_byte_position (w->start);
19428 height = WINDOW_TOTAL_LINES (w);
19429
19430 /* If we decided that this buffer isn't suitable for line numbers,
19431 don't forget that too fast. */
19432 if (EQ (w->base_line_pos, w->buffer))
19433 goto no_value;
19434 /* But do forget it, if the window shows a different buffer now. */
19435 else if (BUFFERP (w->base_line_pos))
19436 w->base_line_pos = Qnil;
19437
19438 /* If the buffer is very big, don't waste time. */
19439 if (INTEGERP (Vline_number_display_limit)
19440 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19441 {
19442 w->base_line_pos = Qnil;
19443 w->base_line_number = Qnil;
19444 goto no_value;
19445 }
19446
19447 if (INTEGERP (w->base_line_number)
19448 && INTEGERP (w->base_line_pos)
19449 && XFASTINT (w->base_line_pos) <= startpos)
19450 {
19451 line = XFASTINT (w->base_line_number);
19452 linepos = XFASTINT (w->base_line_pos);
19453 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19454 }
19455 else
19456 {
19457 line = 1;
19458 linepos = BUF_BEGV (b);
19459 linepos_byte = BUF_BEGV_BYTE (b);
19460 }
19461
19462 /* Count lines from base line to window start position. */
19463 nlines = display_count_lines (linepos, linepos_byte,
19464 startpos_byte,
19465 startpos, &junk);
19466
19467 topline = nlines + line;
19468
19469 /* Determine a new base line, if the old one is too close
19470 or too far away, or if we did not have one.
19471 "Too close" means it's plausible a scroll-down would
19472 go back past it. */
19473 if (startpos == BUF_BEGV (b))
19474 {
19475 w->base_line_number = make_number (topline);
19476 w->base_line_pos = make_number (BUF_BEGV (b));
19477 }
19478 else if (nlines < height + 25 || nlines > height * 3 + 50
19479 || linepos == BUF_BEGV (b))
19480 {
19481 EMACS_INT limit = BUF_BEGV (b);
19482 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
19483 EMACS_INT position;
19484 int distance = (height * 2 + 30) * line_number_display_limit_width;
19485
19486 if (startpos - distance > limit)
19487 {
19488 limit = startpos - distance;
19489 limit_byte = CHAR_TO_BYTE (limit);
19490 }
19491
19492 nlines = display_count_lines (startpos, startpos_byte,
19493 limit_byte,
19494 - (height * 2 + 30),
19495 &position);
19496 /* If we couldn't find the lines we wanted within
19497 line_number_display_limit_width chars per line,
19498 give up on line numbers for this window. */
19499 if (position == limit_byte && limit == startpos - distance)
19500 {
19501 w->base_line_pos = w->buffer;
19502 w->base_line_number = Qnil;
19503 goto no_value;
19504 }
19505
19506 w->base_line_number = make_number (topline - nlines);
19507 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19508 }
19509
19510 /* Now count lines from the start pos to point. */
19511 nlines = display_count_lines (startpos, startpos_byte,
19512 PT_BYTE, PT, &junk);
19513
19514 /* Record that we did display the line number. */
19515 line_number_displayed = 1;
19516
19517 /* Make the string to show. */
19518 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19519 return decode_mode_spec_buf;
19520 no_value:
19521 {
19522 char* p = decode_mode_spec_buf;
19523 int pad = field_width - 2;
19524 while (pad-- > 0)
19525 *p++ = ' ';
19526 *p++ = '?';
19527 *p++ = '?';
19528 *p = '\0';
19529 return decode_mode_spec_buf;
19530 }
19531 }
19532 break;
19533
19534 case 'm':
19535 obj = b->mode_name;
19536 break;
19537
19538 case 'n':
19539 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19540 return " Narrow";
19541 break;
19542
19543 case 'p':
19544 {
19545 EMACS_INT pos = marker_position (w->start);
19546 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19547
19548 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19549 {
19550 if (pos <= BUF_BEGV (b))
19551 return "All";
19552 else
19553 return "Bottom";
19554 }
19555 else if (pos <= BUF_BEGV (b))
19556 return "Top";
19557 else
19558 {
19559 if (total > 1000000)
19560 /* Do it differently for a large value, to avoid overflow. */
19561 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19562 else
19563 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19564 /* We can't normally display a 3-digit number,
19565 so get us a 2-digit number that is close. */
19566 if (total == 100)
19567 total = 99;
19568 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19569 return decode_mode_spec_buf;
19570 }
19571 }
19572
19573 /* Display percentage of size above the bottom of the screen. */
19574 case 'P':
19575 {
19576 EMACS_INT toppos = marker_position (w->start);
19577 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19578 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19579
19580 if (botpos >= BUF_ZV (b))
19581 {
19582 if (toppos <= BUF_BEGV (b))
19583 return "All";
19584 else
19585 return "Bottom";
19586 }
19587 else
19588 {
19589 if (total > 1000000)
19590 /* Do it differently for a large value, to avoid overflow. */
19591 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19592 else
19593 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19594 /* We can't normally display a 3-digit number,
19595 so get us a 2-digit number that is close. */
19596 if (total == 100)
19597 total = 99;
19598 if (toppos <= BUF_BEGV (b))
19599 sprintf (decode_mode_spec_buf, "Top%2ld%%", (long)total);
19600 else
19601 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19602 return decode_mode_spec_buf;
19603 }
19604 }
19605
19606 case 's':
19607 /* status of process */
19608 obj = Fget_buffer_process (Fcurrent_buffer ());
19609 if (NILP (obj))
19610 return "no process";
19611 #ifndef MSDOS
19612 obj = Fsymbol_name (Fprocess_status (obj));
19613 #endif
19614 break;
19615
19616 case '@':
19617 {
19618 int count = inhibit_garbage_collection ();
19619 Lisp_Object val = call1 (intern ("file-remote-p"),
19620 current_buffer->directory);
19621 unbind_to (count, Qnil);
19622
19623 if (NILP (val))
19624 return "-";
19625 else
19626 return "@";
19627 }
19628
19629 case 't': /* indicate TEXT or BINARY */
19630 #ifdef MODE_LINE_BINARY_TEXT
19631 return MODE_LINE_BINARY_TEXT (b);
19632 #else
19633 return "T";
19634 #endif
19635
19636 case 'z':
19637 /* coding-system (not including end-of-line format) */
19638 case 'Z':
19639 /* coding-system (including end-of-line type) */
19640 {
19641 int eol_flag = (c == 'Z');
19642 char *p = decode_mode_spec_buf;
19643
19644 if (! FRAME_WINDOW_P (f))
19645 {
19646 /* No need to mention EOL here--the terminal never needs
19647 to do EOL conversion. */
19648 p = decode_mode_spec_coding (CODING_ID_NAME
19649 (FRAME_KEYBOARD_CODING (f)->id),
19650 p, 0);
19651 p = decode_mode_spec_coding (CODING_ID_NAME
19652 (FRAME_TERMINAL_CODING (f)->id),
19653 p, 0);
19654 }
19655 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19656 p, eol_flag);
19657
19658 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19659 #ifdef subprocesses
19660 obj = Fget_buffer_process (Fcurrent_buffer ());
19661 if (PROCESSP (obj))
19662 {
19663 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19664 p, eol_flag);
19665 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19666 p, eol_flag);
19667 }
19668 #endif /* subprocesses */
19669 #endif /* 0 */
19670 *p = 0;
19671 return decode_mode_spec_buf;
19672 }
19673 }
19674
19675 if (STRINGP (obj))
19676 {
19677 *string = obj;
19678 return (char *) SDATA (obj);
19679 }
19680 else
19681 return "";
19682 }
19683
19684
19685 /* Count up to COUNT lines starting from START / START_BYTE.
19686 But don't go beyond LIMIT_BYTE.
19687 Return the number of lines thus found (always nonnegative).
19688
19689 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19690
19691 static int
19692 display_count_lines (EMACS_INT start, EMACS_INT start_byte,
19693 EMACS_INT limit_byte, int count,
19694 EMACS_INT *byte_pos_ptr)
19695 {
19696 register unsigned char *cursor;
19697 unsigned char *base;
19698
19699 register int ceiling;
19700 register unsigned char *ceiling_addr;
19701 int orig_count = count;
19702
19703 /* If we are not in selective display mode,
19704 check only for newlines. */
19705 int selective_display = (!NILP (current_buffer->selective_display)
19706 && !INTEGERP (current_buffer->selective_display));
19707
19708 if (count > 0)
19709 {
19710 while (start_byte < limit_byte)
19711 {
19712 ceiling = BUFFER_CEILING_OF (start_byte);
19713 ceiling = min (limit_byte - 1, ceiling);
19714 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19715 base = (cursor = BYTE_POS_ADDR (start_byte));
19716 while (1)
19717 {
19718 if (selective_display)
19719 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19720 ;
19721 else
19722 while (*cursor != '\n' && ++cursor != ceiling_addr)
19723 ;
19724
19725 if (cursor != ceiling_addr)
19726 {
19727 if (--count == 0)
19728 {
19729 start_byte += cursor - base + 1;
19730 *byte_pos_ptr = start_byte;
19731 return orig_count;
19732 }
19733 else
19734 if (++cursor == ceiling_addr)
19735 break;
19736 }
19737 else
19738 break;
19739 }
19740 start_byte += cursor - base;
19741 }
19742 }
19743 else
19744 {
19745 while (start_byte > limit_byte)
19746 {
19747 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19748 ceiling = max (limit_byte, ceiling);
19749 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19750 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19751 while (1)
19752 {
19753 if (selective_display)
19754 while (--cursor != ceiling_addr
19755 && *cursor != '\n' && *cursor != 015)
19756 ;
19757 else
19758 while (--cursor != ceiling_addr && *cursor != '\n')
19759 ;
19760
19761 if (cursor != ceiling_addr)
19762 {
19763 if (++count == 0)
19764 {
19765 start_byte += cursor - base + 1;
19766 *byte_pos_ptr = start_byte;
19767 /* When scanning backwards, we should
19768 not count the newline posterior to which we stop. */
19769 return - orig_count - 1;
19770 }
19771 }
19772 else
19773 break;
19774 }
19775 /* Here we add 1 to compensate for the last decrement
19776 of CURSOR, which took it past the valid range. */
19777 start_byte += cursor - base + 1;
19778 }
19779 }
19780
19781 *byte_pos_ptr = limit_byte;
19782
19783 if (count < 0)
19784 return - orig_count + count;
19785 return orig_count - count;
19786
19787 }
19788
19789
19790 \f
19791 /***********************************************************************
19792 Displaying strings
19793 ***********************************************************************/
19794
19795 /* Display a NUL-terminated string, starting with index START.
19796
19797 If STRING is non-null, display that C string. Otherwise, the Lisp
19798 string LISP_STRING is displayed. There's a case that STRING is
19799 non-null and LISP_STRING is not nil. It means STRING is a string
19800 data of LISP_STRING. In that case, we display LISP_STRING while
19801 ignoring its text properties.
19802
19803 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19804 FACE_STRING. Display STRING or LISP_STRING with the face at
19805 FACE_STRING_POS in FACE_STRING:
19806
19807 Display the string in the environment given by IT, but use the
19808 standard display table, temporarily.
19809
19810 FIELD_WIDTH is the minimum number of output glyphs to produce.
19811 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19812 with spaces. If STRING has more characters, more than FIELD_WIDTH
19813 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19814
19815 PRECISION is the maximum number of characters to output from
19816 STRING. PRECISION < 0 means don't truncate the string.
19817
19818 This is roughly equivalent to printf format specifiers:
19819
19820 FIELD_WIDTH PRECISION PRINTF
19821 ----------------------------------------
19822 -1 -1 %s
19823 -1 10 %.10s
19824 10 -1 %10s
19825 20 10 %20.10s
19826
19827 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19828 display them, and < 0 means obey the current buffer's value of
19829 enable_multibyte_characters.
19830
19831 Value is the number of columns displayed. */
19832
19833 static int
19834 display_string (const unsigned char *string, Lisp_Object lisp_string, Lisp_Object face_string,
19835 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
19836 int field_width, int precision, int max_x, int multibyte)
19837 {
19838 int hpos_at_start = it->hpos;
19839 int saved_face_id = it->face_id;
19840 struct glyph_row *row = it->glyph_row;
19841
19842 /* Initialize the iterator IT for iteration over STRING beginning
19843 with index START. */
19844 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19845 precision, field_width, multibyte);
19846 if (string && STRINGP (lisp_string))
19847 /* LISP_STRING is the one returned by decode_mode_spec. We should
19848 ignore its text properties. */
19849 it->stop_charpos = -1;
19850
19851 /* If displaying STRING, set up the face of the iterator
19852 from LISP_STRING, if that's given. */
19853 if (STRINGP (face_string))
19854 {
19855 EMACS_INT endptr;
19856 struct face *face;
19857
19858 it->face_id
19859 = face_at_string_position (it->w, face_string, face_string_pos,
19860 0, it->region_beg_charpos,
19861 it->region_end_charpos,
19862 &endptr, it->base_face_id, 0);
19863 face = FACE_FROM_ID (it->f, it->face_id);
19864 it->face_box_p = face->box != FACE_NO_BOX;
19865 }
19866
19867 /* Set max_x to the maximum allowed X position. Don't let it go
19868 beyond the right edge of the window. */
19869 if (max_x <= 0)
19870 max_x = it->last_visible_x;
19871 else
19872 max_x = min (max_x, it->last_visible_x);
19873
19874 /* Skip over display elements that are not visible. because IT->w is
19875 hscrolled. */
19876 if (it->current_x < it->first_visible_x)
19877 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19878 MOVE_TO_POS | MOVE_TO_X);
19879
19880 row->ascent = it->max_ascent;
19881 row->height = it->max_ascent + it->max_descent;
19882 row->phys_ascent = it->max_phys_ascent;
19883 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19884 row->extra_line_spacing = it->max_extra_line_spacing;
19885
19886 /* This condition is for the case that we are called with current_x
19887 past last_visible_x. */
19888 while (it->current_x < max_x)
19889 {
19890 int x_before, x, n_glyphs_before, i, nglyphs;
19891
19892 /* Get the next display element. */
19893 if (!get_next_display_element (it))
19894 break;
19895
19896 /* Produce glyphs. */
19897 x_before = it->current_x;
19898 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
19899 PRODUCE_GLYPHS (it);
19900
19901 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
19902 i = 0;
19903 x = x_before;
19904 while (i < nglyphs)
19905 {
19906 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19907
19908 if (it->line_wrap != TRUNCATE
19909 && x + glyph->pixel_width > max_x)
19910 {
19911 /* End of continued line or max_x reached. */
19912 if (CHAR_GLYPH_PADDING_P (*glyph))
19913 {
19914 /* A wide character is unbreakable. */
19915 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
19916 it->current_x = x_before;
19917 }
19918 else
19919 {
19920 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
19921 it->current_x = x;
19922 }
19923 break;
19924 }
19925 else if (x + glyph->pixel_width >= it->first_visible_x)
19926 {
19927 /* Glyph is at least partially visible. */
19928 ++it->hpos;
19929 if (x < it->first_visible_x)
19930 it->glyph_row->x = x - it->first_visible_x;
19931 }
19932 else
19933 {
19934 /* Glyph is off the left margin of the display area.
19935 Should not happen. */
19936 abort ();
19937 }
19938
19939 row->ascent = max (row->ascent, it->max_ascent);
19940 row->height = max (row->height, it->max_ascent + it->max_descent);
19941 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19942 row->phys_height = max (row->phys_height,
19943 it->max_phys_ascent + it->max_phys_descent);
19944 row->extra_line_spacing = max (row->extra_line_spacing,
19945 it->max_extra_line_spacing);
19946 x += glyph->pixel_width;
19947 ++i;
19948 }
19949
19950 /* Stop if max_x reached. */
19951 if (i < nglyphs)
19952 break;
19953
19954 /* Stop at line ends. */
19955 if (ITERATOR_AT_END_OF_LINE_P (it))
19956 {
19957 it->continuation_lines_width = 0;
19958 break;
19959 }
19960
19961 set_iterator_to_next (it, 1);
19962
19963 /* Stop if truncating at the right edge. */
19964 if (it->line_wrap == TRUNCATE
19965 && it->current_x >= it->last_visible_x)
19966 {
19967 /* Add truncation mark, but don't do it if the line is
19968 truncated at a padding space. */
19969 if (IT_CHARPOS (*it) < it->string_nchars)
19970 {
19971 if (!FRAME_WINDOW_P (it->f))
19972 {
19973 int i, n;
19974
19975 if (it->current_x > it->last_visible_x)
19976 {
19977 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19978 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19979 break;
19980 for (n = row->used[TEXT_AREA]; i < n; ++i)
19981 {
19982 row->used[TEXT_AREA] = i;
19983 produce_special_glyphs (it, IT_TRUNCATION);
19984 }
19985 }
19986 produce_special_glyphs (it, IT_TRUNCATION);
19987 }
19988 it->glyph_row->truncated_on_right_p = 1;
19989 }
19990 break;
19991 }
19992 }
19993
19994 /* Maybe insert a truncation at the left. */
19995 if (it->first_visible_x
19996 && IT_CHARPOS (*it) > 0)
19997 {
19998 if (!FRAME_WINDOW_P (it->f))
19999 insert_left_trunc_glyphs (it);
20000 it->glyph_row->truncated_on_left_p = 1;
20001 }
20002
20003 it->face_id = saved_face_id;
20004
20005 /* Value is number of columns displayed. */
20006 return it->hpos - hpos_at_start;
20007 }
20008
20009
20010 \f
20011 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20012 appears as an element of LIST or as the car of an element of LIST.
20013 If PROPVAL is a list, compare each element against LIST in that
20014 way, and return 1/2 if any element of PROPVAL is found in LIST.
20015 Otherwise return 0. This function cannot quit.
20016 The return value is 2 if the text is invisible but with an ellipsis
20017 and 1 if it's invisible and without an ellipsis. */
20018
20019 int
20020 invisible_p (register Lisp_Object propval, Lisp_Object list)
20021 {
20022 register Lisp_Object tail, proptail;
20023
20024 for (tail = list; CONSP (tail); tail = XCDR (tail))
20025 {
20026 register Lisp_Object tem;
20027 tem = XCAR (tail);
20028 if (EQ (propval, tem))
20029 return 1;
20030 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20031 return NILP (XCDR (tem)) ? 1 : 2;
20032 }
20033
20034 if (CONSP (propval))
20035 {
20036 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20037 {
20038 Lisp_Object propelt;
20039 propelt = XCAR (proptail);
20040 for (tail = list; CONSP (tail); tail = XCDR (tail))
20041 {
20042 register Lisp_Object tem;
20043 tem = XCAR (tail);
20044 if (EQ (propelt, tem))
20045 return 1;
20046 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20047 return NILP (XCDR (tem)) ? 1 : 2;
20048 }
20049 }
20050 }
20051
20052 return 0;
20053 }
20054
20055 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20056 doc: /* Non-nil if the property makes the text invisible.
20057 POS-OR-PROP can be a marker or number, in which case it is taken to be
20058 a position in the current buffer and the value of the `invisible' property
20059 is checked; or it can be some other value, which is then presumed to be the
20060 value of the `invisible' property of the text of interest.
20061 The non-nil value returned can be t for truly invisible text or something
20062 else if the text is replaced by an ellipsis. */)
20063 (Lisp_Object pos_or_prop)
20064 {
20065 Lisp_Object prop
20066 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20067 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20068 : pos_or_prop);
20069 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20070 return (invis == 0 ? Qnil
20071 : invis == 1 ? Qt
20072 : make_number (invis));
20073 }
20074
20075 /* Calculate a width or height in pixels from a specification using
20076 the following elements:
20077
20078 SPEC ::=
20079 NUM - a (fractional) multiple of the default font width/height
20080 (NUM) - specifies exactly NUM pixels
20081 UNIT - a fixed number of pixels, see below.
20082 ELEMENT - size of a display element in pixels, see below.
20083 (NUM . SPEC) - equals NUM * SPEC
20084 (+ SPEC SPEC ...) - add pixel values
20085 (- SPEC SPEC ...) - subtract pixel values
20086 (- SPEC) - negate pixel value
20087
20088 NUM ::=
20089 INT or FLOAT - a number constant
20090 SYMBOL - use symbol's (buffer local) variable binding.
20091
20092 UNIT ::=
20093 in - pixels per inch *)
20094 mm - pixels per 1/1000 meter *)
20095 cm - pixels per 1/100 meter *)
20096 width - width of current font in pixels.
20097 height - height of current font in pixels.
20098
20099 *) using the ratio(s) defined in display-pixels-per-inch.
20100
20101 ELEMENT ::=
20102
20103 left-fringe - left fringe width in pixels
20104 right-fringe - right fringe width in pixels
20105
20106 left-margin - left margin width in pixels
20107 right-margin - right margin width in pixels
20108
20109 scroll-bar - scroll-bar area width in pixels
20110
20111 Examples:
20112
20113 Pixels corresponding to 5 inches:
20114 (5 . in)
20115
20116 Total width of non-text areas on left side of window (if scroll-bar is on left):
20117 '(space :width (+ left-fringe left-margin scroll-bar))
20118
20119 Align to first text column (in header line):
20120 '(space :align-to 0)
20121
20122 Align to middle of text area minus half the width of variable `my-image'
20123 containing a loaded image:
20124 '(space :align-to (0.5 . (- text my-image)))
20125
20126 Width of left margin minus width of 1 character in the default font:
20127 '(space :width (- left-margin 1))
20128
20129 Width of left margin minus width of 2 characters in the current font:
20130 '(space :width (- left-margin (2 . width)))
20131
20132 Center 1 character over left-margin (in header line):
20133 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20134
20135 Different ways to express width of left fringe plus left margin minus one pixel:
20136 '(space :width (- (+ left-fringe left-margin) (1)))
20137 '(space :width (+ left-fringe left-margin (- (1))))
20138 '(space :width (+ left-fringe left-margin (-1)))
20139
20140 */
20141
20142 #define NUMVAL(X) \
20143 ((INTEGERP (X) || FLOATP (X)) \
20144 ? XFLOATINT (X) \
20145 : - 1)
20146
20147 int
20148 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20149 struct font *font, int width_p, int *align_to)
20150 {
20151 double pixels;
20152
20153 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20154 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20155
20156 if (NILP (prop))
20157 return OK_PIXELS (0);
20158
20159 xassert (FRAME_LIVE_P (it->f));
20160
20161 if (SYMBOLP (prop))
20162 {
20163 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20164 {
20165 char *unit = SDATA (SYMBOL_NAME (prop));
20166
20167 if (unit[0] == 'i' && unit[1] == 'n')
20168 pixels = 1.0;
20169 else if (unit[0] == 'm' && unit[1] == 'm')
20170 pixels = 25.4;
20171 else if (unit[0] == 'c' && unit[1] == 'm')
20172 pixels = 2.54;
20173 else
20174 pixels = 0;
20175 if (pixels > 0)
20176 {
20177 double ppi;
20178 #ifdef HAVE_WINDOW_SYSTEM
20179 if (FRAME_WINDOW_P (it->f)
20180 && (ppi = (width_p
20181 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20182 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20183 ppi > 0))
20184 return OK_PIXELS (ppi / pixels);
20185 #endif
20186
20187 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20188 || (CONSP (Vdisplay_pixels_per_inch)
20189 && (ppi = (width_p
20190 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20191 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20192 ppi > 0)))
20193 return OK_PIXELS (ppi / pixels);
20194
20195 return 0;
20196 }
20197 }
20198
20199 #ifdef HAVE_WINDOW_SYSTEM
20200 if (EQ (prop, Qheight))
20201 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20202 if (EQ (prop, Qwidth))
20203 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20204 #else
20205 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20206 return OK_PIXELS (1);
20207 #endif
20208
20209 if (EQ (prop, Qtext))
20210 return OK_PIXELS (width_p
20211 ? window_box_width (it->w, TEXT_AREA)
20212 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20213
20214 if (align_to && *align_to < 0)
20215 {
20216 *res = 0;
20217 if (EQ (prop, Qleft))
20218 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20219 if (EQ (prop, Qright))
20220 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20221 if (EQ (prop, Qcenter))
20222 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20223 + window_box_width (it->w, TEXT_AREA) / 2);
20224 if (EQ (prop, Qleft_fringe))
20225 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20226 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20227 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20228 if (EQ (prop, Qright_fringe))
20229 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20230 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20231 : window_box_right_offset (it->w, TEXT_AREA));
20232 if (EQ (prop, Qleft_margin))
20233 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20234 if (EQ (prop, Qright_margin))
20235 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20236 if (EQ (prop, Qscroll_bar))
20237 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20238 ? 0
20239 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20240 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20241 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20242 : 0)));
20243 }
20244 else
20245 {
20246 if (EQ (prop, Qleft_fringe))
20247 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20248 if (EQ (prop, Qright_fringe))
20249 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20250 if (EQ (prop, Qleft_margin))
20251 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20252 if (EQ (prop, Qright_margin))
20253 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20254 if (EQ (prop, Qscroll_bar))
20255 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20256 }
20257
20258 prop = Fbuffer_local_value (prop, it->w->buffer);
20259 }
20260
20261 if (INTEGERP (prop) || FLOATP (prop))
20262 {
20263 int base_unit = (width_p
20264 ? FRAME_COLUMN_WIDTH (it->f)
20265 : FRAME_LINE_HEIGHT (it->f));
20266 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20267 }
20268
20269 if (CONSP (prop))
20270 {
20271 Lisp_Object car = XCAR (prop);
20272 Lisp_Object cdr = XCDR (prop);
20273
20274 if (SYMBOLP (car))
20275 {
20276 #ifdef HAVE_WINDOW_SYSTEM
20277 if (FRAME_WINDOW_P (it->f)
20278 && valid_image_p (prop))
20279 {
20280 int id = lookup_image (it->f, prop);
20281 struct image *img = IMAGE_FROM_ID (it->f, id);
20282
20283 return OK_PIXELS (width_p ? img->width : img->height);
20284 }
20285 #endif
20286 if (EQ (car, Qplus) || EQ (car, Qminus))
20287 {
20288 int first = 1;
20289 double px;
20290
20291 pixels = 0;
20292 while (CONSP (cdr))
20293 {
20294 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20295 font, width_p, align_to))
20296 return 0;
20297 if (first)
20298 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20299 else
20300 pixels += px;
20301 cdr = XCDR (cdr);
20302 }
20303 if (EQ (car, Qminus))
20304 pixels = -pixels;
20305 return OK_PIXELS (pixels);
20306 }
20307
20308 car = Fbuffer_local_value (car, it->w->buffer);
20309 }
20310
20311 if (INTEGERP (car) || FLOATP (car))
20312 {
20313 double fact;
20314 pixels = XFLOATINT (car);
20315 if (NILP (cdr))
20316 return OK_PIXELS (pixels);
20317 if (calc_pixel_width_or_height (&fact, it, cdr,
20318 font, width_p, align_to))
20319 return OK_PIXELS (pixels * fact);
20320 return 0;
20321 }
20322
20323 return 0;
20324 }
20325
20326 return 0;
20327 }
20328
20329 \f
20330 /***********************************************************************
20331 Glyph Display
20332 ***********************************************************************/
20333
20334 #ifdef HAVE_WINDOW_SYSTEM
20335
20336 #if GLYPH_DEBUG
20337
20338 void
20339 dump_glyph_string (s)
20340 struct glyph_string *s;
20341 {
20342 fprintf (stderr, "glyph string\n");
20343 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20344 s->x, s->y, s->width, s->height);
20345 fprintf (stderr, " ybase = %d\n", s->ybase);
20346 fprintf (stderr, " hl = %d\n", s->hl);
20347 fprintf (stderr, " left overhang = %d, right = %d\n",
20348 s->left_overhang, s->right_overhang);
20349 fprintf (stderr, " nchars = %d\n", s->nchars);
20350 fprintf (stderr, " extends to end of line = %d\n",
20351 s->extends_to_end_of_line_p);
20352 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20353 fprintf (stderr, " bg width = %d\n", s->background_width);
20354 }
20355
20356 #endif /* GLYPH_DEBUG */
20357
20358 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20359 of XChar2b structures for S; it can't be allocated in
20360 init_glyph_string because it must be allocated via `alloca'. W
20361 is the window on which S is drawn. ROW and AREA are the glyph row
20362 and area within the row from which S is constructed. START is the
20363 index of the first glyph structure covered by S. HL is a
20364 face-override for drawing S. */
20365
20366 #ifdef HAVE_NTGUI
20367 #define OPTIONAL_HDC(hdc) HDC hdc,
20368 #define DECLARE_HDC(hdc) HDC hdc;
20369 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20370 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20371 #endif
20372
20373 #ifndef OPTIONAL_HDC
20374 #define OPTIONAL_HDC(hdc)
20375 #define DECLARE_HDC(hdc)
20376 #define ALLOCATE_HDC(hdc, f)
20377 #define RELEASE_HDC(hdc, f)
20378 #endif
20379
20380 static void
20381 init_glyph_string (struct glyph_string *s,
20382 OPTIONAL_HDC (hdc)
20383 XChar2b *char2b, struct window *w, struct glyph_row *row,
20384 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20385 {
20386 memset (s, 0, sizeof *s);
20387 s->w = w;
20388 s->f = XFRAME (w->frame);
20389 #ifdef HAVE_NTGUI
20390 s->hdc = hdc;
20391 #endif
20392 s->display = FRAME_X_DISPLAY (s->f);
20393 s->window = FRAME_X_WINDOW (s->f);
20394 s->char2b = char2b;
20395 s->hl = hl;
20396 s->row = row;
20397 s->area = area;
20398 s->first_glyph = row->glyphs[area] + start;
20399 s->height = row->height;
20400 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20401 s->ybase = s->y + row->ascent;
20402 }
20403
20404
20405 /* Append the list of glyph strings with head H and tail T to the list
20406 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20407
20408 static INLINE void
20409 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20410 struct glyph_string *h, struct glyph_string *t)
20411 {
20412 if (h)
20413 {
20414 if (*head)
20415 (*tail)->next = h;
20416 else
20417 *head = h;
20418 h->prev = *tail;
20419 *tail = t;
20420 }
20421 }
20422
20423
20424 /* Prepend the list of glyph strings with head H and tail T to the
20425 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20426 result. */
20427
20428 static INLINE void
20429 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20430 struct glyph_string *h, struct glyph_string *t)
20431 {
20432 if (h)
20433 {
20434 if (*head)
20435 (*head)->prev = t;
20436 else
20437 *tail = t;
20438 t->next = *head;
20439 *head = h;
20440 }
20441 }
20442
20443
20444 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20445 Set *HEAD and *TAIL to the resulting list. */
20446
20447 static INLINE void
20448 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20449 struct glyph_string *s)
20450 {
20451 s->next = s->prev = NULL;
20452 append_glyph_string_lists (head, tail, s, s);
20453 }
20454
20455
20456 /* Get face and two-byte form of character C in face FACE_ID on frame
20457 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20458 means we want to display multibyte text. DISPLAY_P non-zero means
20459 make sure that X resources for the face returned are allocated.
20460 Value is a pointer to a realized face that is ready for display if
20461 DISPLAY_P is non-zero. */
20462
20463 static INLINE struct face *
20464 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20465 XChar2b *char2b, int multibyte_p, int display_p)
20466 {
20467 struct face *face = FACE_FROM_ID (f, face_id);
20468
20469 if (face->font)
20470 {
20471 unsigned code = face->font->driver->encode_char (face->font, c);
20472
20473 if (code != FONT_INVALID_CODE)
20474 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20475 else
20476 STORE_XCHAR2B (char2b, 0, 0);
20477 }
20478
20479 /* Make sure X resources of the face are allocated. */
20480 #ifdef HAVE_X_WINDOWS
20481 if (display_p)
20482 #endif
20483 {
20484 xassert (face != NULL);
20485 PREPARE_FACE_FOR_DISPLAY (f, face);
20486 }
20487
20488 return face;
20489 }
20490
20491
20492 /* Get face and two-byte form of character glyph GLYPH on frame F.
20493 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20494 a pointer to a realized face that is ready for display. */
20495
20496 static INLINE struct face *
20497 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20498 XChar2b *char2b, int *two_byte_p)
20499 {
20500 struct face *face;
20501
20502 xassert (glyph->type == CHAR_GLYPH);
20503 face = FACE_FROM_ID (f, glyph->face_id);
20504
20505 if (two_byte_p)
20506 *two_byte_p = 0;
20507
20508 if (face->font)
20509 {
20510 unsigned code;
20511
20512 if (CHAR_BYTE8_P (glyph->u.ch))
20513 code = CHAR_TO_BYTE8 (glyph->u.ch);
20514 else
20515 code = face->font->driver->encode_char (face->font, glyph->u.ch);
20516
20517 if (code != FONT_INVALID_CODE)
20518 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20519 else
20520 STORE_XCHAR2B (char2b, 0, 0);
20521 }
20522
20523 /* Make sure X resources of the face are allocated. */
20524 xassert (face != NULL);
20525 PREPARE_FACE_FOR_DISPLAY (f, face);
20526 return face;
20527 }
20528
20529
20530 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
20531 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
20532
20533 static INLINE int
20534 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
20535 {
20536 unsigned code;
20537
20538 if (CHAR_BYTE8_P (c))
20539 code = CHAR_TO_BYTE8 (c);
20540 else
20541 code = font->driver->encode_char (font, c);
20542
20543 if (code == FONT_INVALID_CODE)
20544 return 0;
20545 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20546 return 1;
20547 }
20548
20549
20550 /* Fill glyph string S with composition components specified by S->cmp.
20551
20552 BASE_FACE is the base face of the composition.
20553 S->cmp_from is the index of the first component for S.
20554
20555 OVERLAPS non-zero means S should draw the foreground only, and use
20556 its physical height for clipping. See also draw_glyphs.
20557
20558 Value is the index of a component not in S. */
20559
20560 static int
20561 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20562 int overlaps)
20563 {
20564 int i;
20565 /* For all glyphs of this composition, starting at the offset
20566 S->cmp_from, until we reach the end of the definition or encounter a
20567 glyph that requires the different face, add it to S. */
20568 struct face *face;
20569
20570 xassert (s);
20571
20572 s->for_overlaps = overlaps;
20573 s->face = NULL;
20574 s->font = NULL;
20575 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20576 {
20577 int c = COMPOSITION_GLYPH (s->cmp, i);
20578
20579 if (c != '\t')
20580 {
20581 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20582 -1, Qnil);
20583
20584 face = get_char_face_and_encoding (s->f, c, face_id,
20585 s->char2b + i, 1, 1);
20586 if (face)
20587 {
20588 if (! s->face)
20589 {
20590 s->face = face;
20591 s->font = s->face->font;
20592 }
20593 else if (s->face != face)
20594 break;
20595 }
20596 }
20597 ++s->nchars;
20598 }
20599 s->cmp_to = i;
20600
20601 /* All glyph strings for the same composition has the same width,
20602 i.e. the width set for the first component of the composition. */
20603 s->width = s->first_glyph->pixel_width;
20604
20605 /* If the specified font could not be loaded, use the frame's
20606 default font, but record the fact that we couldn't load it in
20607 the glyph string so that we can draw rectangles for the
20608 characters of the glyph string. */
20609 if (s->font == NULL)
20610 {
20611 s->font_not_found_p = 1;
20612 s->font = FRAME_FONT (s->f);
20613 }
20614
20615 /* Adjust base line for subscript/superscript text. */
20616 s->ybase += s->first_glyph->voffset;
20617
20618 /* This glyph string must always be drawn with 16-bit functions. */
20619 s->two_byte_p = 1;
20620
20621 return s->cmp_to;
20622 }
20623
20624 static int
20625 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
20626 int start, int end, int overlaps)
20627 {
20628 struct glyph *glyph, *last;
20629 Lisp_Object lgstring;
20630 int i;
20631
20632 s->for_overlaps = overlaps;
20633 glyph = s->row->glyphs[s->area] + start;
20634 last = s->row->glyphs[s->area] + end;
20635 s->cmp_id = glyph->u.cmp.id;
20636 s->cmp_from = glyph->slice.cmp.from;
20637 s->cmp_to = glyph->slice.cmp.to + 1;
20638 s->face = FACE_FROM_ID (s->f, face_id);
20639 lgstring = composition_gstring_from_id (s->cmp_id);
20640 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20641 glyph++;
20642 while (glyph < last
20643 && glyph->u.cmp.automatic
20644 && glyph->u.cmp.id == s->cmp_id
20645 && s->cmp_to == glyph->slice.cmp.from)
20646 s->cmp_to = (glyph++)->slice.cmp.to + 1;
20647
20648 for (i = s->cmp_from; i < s->cmp_to; i++)
20649 {
20650 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20651 unsigned code = LGLYPH_CODE (lglyph);
20652
20653 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20654 }
20655 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20656 return glyph - s->row->glyphs[s->area];
20657 }
20658
20659
20660 /* Fill glyph string S from a sequence of character glyphs.
20661
20662 FACE_ID is the face id of the string. START is the index of the
20663 first glyph to consider, END is the index of the last + 1.
20664 OVERLAPS non-zero means S should draw the foreground only, and use
20665 its physical height for clipping. See also draw_glyphs.
20666
20667 Value is the index of the first glyph not in S. */
20668
20669 static int
20670 fill_glyph_string (struct glyph_string *s, int face_id,
20671 int start, int end, int overlaps)
20672 {
20673 struct glyph *glyph, *last;
20674 int voffset;
20675 int glyph_not_available_p;
20676
20677 xassert (s->f == XFRAME (s->w->frame));
20678 xassert (s->nchars == 0);
20679 xassert (start >= 0 && end > start);
20680
20681 s->for_overlaps = overlaps;
20682 glyph = s->row->glyphs[s->area] + start;
20683 last = s->row->glyphs[s->area] + end;
20684 voffset = glyph->voffset;
20685 s->padding_p = glyph->padding_p;
20686 glyph_not_available_p = glyph->glyph_not_available_p;
20687
20688 while (glyph < last
20689 && glyph->type == CHAR_GLYPH
20690 && glyph->voffset == voffset
20691 /* Same face id implies same font, nowadays. */
20692 && glyph->face_id == face_id
20693 && glyph->glyph_not_available_p == glyph_not_available_p)
20694 {
20695 int two_byte_p;
20696
20697 s->face = get_glyph_face_and_encoding (s->f, glyph,
20698 s->char2b + s->nchars,
20699 &two_byte_p);
20700 s->two_byte_p = two_byte_p;
20701 ++s->nchars;
20702 xassert (s->nchars <= end - start);
20703 s->width += glyph->pixel_width;
20704 if (glyph++->padding_p != s->padding_p)
20705 break;
20706 }
20707
20708 s->font = s->face->font;
20709
20710 /* If the specified font could not be loaded, use the frame's font,
20711 but record the fact that we couldn't load it in
20712 S->font_not_found_p so that we can draw rectangles for the
20713 characters of the glyph string. */
20714 if (s->font == NULL || glyph_not_available_p)
20715 {
20716 s->font_not_found_p = 1;
20717 s->font = FRAME_FONT (s->f);
20718 }
20719
20720 /* Adjust base line for subscript/superscript text. */
20721 s->ybase += voffset;
20722
20723 xassert (s->face && s->face->gc);
20724 return glyph - s->row->glyphs[s->area];
20725 }
20726
20727
20728 /* Fill glyph string S from image glyph S->first_glyph. */
20729
20730 static void
20731 fill_image_glyph_string (struct glyph_string *s)
20732 {
20733 xassert (s->first_glyph->type == IMAGE_GLYPH);
20734 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20735 xassert (s->img);
20736 s->slice = s->first_glyph->slice.img;
20737 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20738 s->font = s->face->font;
20739 s->width = s->first_glyph->pixel_width;
20740
20741 /* Adjust base line for subscript/superscript text. */
20742 s->ybase += s->first_glyph->voffset;
20743 }
20744
20745
20746 /* Fill glyph string S from a sequence of stretch glyphs.
20747
20748 ROW is the glyph row in which the glyphs are found, AREA is the
20749 area within the row. START is the index of the first glyph to
20750 consider, END is the index of the last + 1.
20751
20752 Value is the index of the first glyph not in S. */
20753
20754 static int
20755 fill_stretch_glyph_string (struct glyph_string *s, struct glyph_row *row,
20756 enum glyph_row_area area, int start, int end)
20757 {
20758 struct glyph *glyph, *last;
20759 int voffset, face_id;
20760
20761 xassert (s->first_glyph->type == STRETCH_GLYPH);
20762
20763 glyph = s->row->glyphs[s->area] + start;
20764 last = s->row->glyphs[s->area] + end;
20765 face_id = glyph->face_id;
20766 s->face = FACE_FROM_ID (s->f, face_id);
20767 s->font = s->face->font;
20768 s->width = glyph->pixel_width;
20769 s->nchars = 1;
20770 voffset = glyph->voffset;
20771
20772 for (++glyph;
20773 (glyph < last
20774 && glyph->type == STRETCH_GLYPH
20775 && glyph->voffset == voffset
20776 && glyph->face_id == face_id);
20777 ++glyph)
20778 s->width += glyph->pixel_width;
20779
20780 /* Adjust base line for subscript/superscript text. */
20781 s->ybase += voffset;
20782
20783 /* The case that face->gc == 0 is handled when drawing the glyph
20784 string by calling PREPARE_FACE_FOR_DISPLAY. */
20785 xassert (s->face);
20786 return glyph - s->row->glyphs[s->area];
20787 }
20788
20789 static struct font_metrics *
20790 get_per_char_metric (struct frame *f, struct font *font, XChar2b *char2b)
20791 {
20792 static struct font_metrics metrics;
20793 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20794
20795 if (! font || code == FONT_INVALID_CODE)
20796 return NULL;
20797 font->driver->text_extents (font, &code, 1, &metrics);
20798 return &metrics;
20799 }
20800
20801 /* EXPORT for RIF:
20802 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20803 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20804 assumed to be zero. */
20805
20806 void
20807 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
20808 {
20809 *left = *right = 0;
20810
20811 if (glyph->type == CHAR_GLYPH)
20812 {
20813 struct face *face;
20814 XChar2b char2b;
20815 struct font_metrics *pcm;
20816
20817 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20818 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20819 {
20820 if (pcm->rbearing > pcm->width)
20821 *right = pcm->rbearing - pcm->width;
20822 if (pcm->lbearing < 0)
20823 *left = -pcm->lbearing;
20824 }
20825 }
20826 else if (glyph->type == COMPOSITE_GLYPH)
20827 {
20828 if (! glyph->u.cmp.automatic)
20829 {
20830 struct composition *cmp = composition_table[glyph->u.cmp.id];
20831
20832 if (cmp->rbearing > cmp->pixel_width)
20833 *right = cmp->rbearing - cmp->pixel_width;
20834 if (cmp->lbearing < 0)
20835 *left = - cmp->lbearing;
20836 }
20837 else
20838 {
20839 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20840 struct font_metrics metrics;
20841
20842 composition_gstring_width (gstring, glyph->slice.cmp.from,
20843 glyph->slice.cmp.to + 1, &metrics);
20844 if (metrics.rbearing > metrics.width)
20845 *right = metrics.rbearing - metrics.width;
20846 if (metrics.lbearing < 0)
20847 *left = - metrics.lbearing;
20848 }
20849 }
20850 }
20851
20852
20853 /* Return the index of the first glyph preceding glyph string S that
20854 is overwritten by S because of S's left overhang. Value is -1
20855 if no glyphs are overwritten. */
20856
20857 static int
20858 left_overwritten (struct glyph_string *s)
20859 {
20860 int k;
20861
20862 if (s->left_overhang)
20863 {
20864 int x = 0, i;
20865 struct glyph *glyphs = s->row->glyphs[s->area];
20866 int first = s->first_glyph - glyphs;
20867
20868 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
20869 x -= glyphs[i].pixel_width;
20870
20871 k = i + 1;
20872 }
20873 else
20874 k = -1;
20875
20876 return k;
20877 }
20878
20879
20880 /* Return the index of the first glyph preceding glyph string S that
20881 is overwriting S because of its right overhang. Value is -1 if no
20882 glyph in front of S overwrites S. */
20883
20884 static int
20885 left_overwriting (struct glyph_string *s)
20886 {
20887 int i, k, x;
20888 struct glyph *glyphs = s->row->glyphs[s->area];
20889 int first = s->first_glyph - glyphs;
20890
20891 k = -1;
20892 x = 0;
20893 for (i = first - 1; i >= 0; --i)
20894 {
20895 int left, right;
20896 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20897 if (x + right > 0)
20898 k = i;
20899 x -= glyphs[i].pixel_width;
20900 }
20901
20902 return k;
20903 }
20904
20905
20906 /* Return the index of the last glyph following glyph string S that is
20907 overwritten by S because of S's right overhang. Value is -1 if
20908 no such glyph is found. */
20909
20910 static int
20911 right_overwritten (struct glyph_string *s)
20912 {
20913 int k = -1;
20914
20915 if (s->right_overhang)
20916 {
20917 int x = 0, i;
20918 struct glyph *glyphs = s->row->glyphs[s->area];
20919 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20920 int end = s->row->used[s->area];
20921
20922 for (i = first; i < end && s->right_overhang > x; ++i)
20923 x += glyphs[i].pixel_width;
20924
20925 k = i;
20926 }
20927
20928 return k;
20929 }
20930
20931
20932 /* Return the index of the last glyph following glyph string S that
20933 overwrites S because of its left overhang. Value is negative
20934 if no such glyph is found. */
20935
20936 static int
20937 right_overwriting (struct glyph_string *s)
20938 {
20939 int i, k, x;
20940 int end = s->row->used[s->area];
20941 struct glyph *glyphs = s->row->glyphs[s->area];
20942 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20943
20944 k = -1;
20945 x = 0;
20946 for (i = first; i < end; ++i)
20947 {
20948 int left, right;
20949 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20950 if (x - left < 0)
20951 k = i;
20952 x += glyphs[i].pixel_width;
20953 }
20954
20955 return k;
20956 }
20957
20958
20959 /* Set background width of glyph string S. START is the index of the
20960 first glyph following S. LAST_X is the right-most x-position + 1
20961 in the drawing area. */
20962
20963 static INLINE void
20964 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
20965 {
20966 /* If the face of this glyph string has to be drawn to the end of
20967 the drawing area, set S->extends_to_end_of_line_p. */
20968
20969 if (start == s->row->used[s->area]
20970 && s->area == TEXT_AREA
20971 && ((s->row->fill_line_p
20972 && (s->hl == DRAW_NORMAL_TEXT
20973 || s->hl == DRAW_IMAGE_RAISED
20974 || s->hl == DRAW_IMAGE_SUNKEN))
20975 || s->hl == DRAW_MOUSE_FACE))
20976 s->extends_to_end_of_line_p = 1;
20977
20978 /* If S extends its face to the end of the line, set its
20979 background_width to the distance to the right edge of the drawing
20980 area. */
20981 if (s->extends_to_end_of_line_p)
20982 s->background_width = last_x - s->x + 1;
20983 else
20984 s->background_width = s->width;
20985 }
20986
20987
20988 /* Compute overhangs and x-positions for glyph string S and its
20989 predecessors, or successors. X is the starting x-position for S.
20990 BACKWARD_P non-zero means process predecessors. */
20991
20992 static void
20993 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
20994 {
20995 if (backward_p)
20996 {
20997 while (s)
20998 {
20999 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21000 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21001 x -= s->width;
21002 s->x = x;
21003 s = s->prev;
21004 }
21005 }
21006 else
21007 {
21008 while (s)
21009 {
21010 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21011 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21012 s->x = x;
21013 x += s->width;
21014 s = s->next;
21015 }
21016 }
21017 }
21018
21019
21020
21021 /* The following macros are only called from draw_glyphs below.
21022 They reference the following parameters of that function directly:
21023 `w', `row', `area', and `overlap_p'
21024 as well as the following local variables:
21025 `s', `f', and `hdc' (in W32) */
21026
21027 #ifdef HAVE_NTGUI
21028 /* On W32, silently add local `hdc' variable to argument list of
21029 init_glyph_string. */
21030 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21031 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21032 #else
21033 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21034 init_glyph_string (s, char2b, w, row, area, start, hl)
21035 #endif
21036
21037 /* Add a glyph string for a stretch glyph to the list of strings
21038 between HEAD and TAIL. START is the index of the stretch glyph in
21039 row area AREA of glyph row ROW. END is the index of the last glyph
21040 in that glyph row area. X is the current output position assigned
21041 to the new glyph string constructed. HL overrides that face of the
21042 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21043 is the right-most x-position of the drawing area. */
21044
21045 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21046 and below -- keep them on one line. */
21047 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21048 do \
21049 { \
21050 s = (struct glyph_string *) alloca (sizeof *s); \
21051 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21052 START = fill_stretch_glyph_string (s, row, area, START, END); \
21053 append_glyph_string (&HEAD, &TAIL, s); \
21054 s->x = (X); \
21055 } \
21056 while (0)
21057
21058
21059 /* Add a glyph string for an image glyph to the list of strings
21060 between HEAD and TAIL. START is the index of the image glyph in
21061 row area AREA of glyph row ROW. END is the index of the last glyph
21062 in that glyph row area. X is the current output position assigned
21063 to the new glyph string constructed. HL overrides that face of the
21064 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21065 is the right-most x-position of the drawing area. */
21066
21067 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21068 do \
21069 { \
21070 s = (struct glyph_string *) alloca (sizeof *s); \
21071 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21072 fill_image_glyph_string (s); \
21073 append_glyph_string (&HEAD, &TAIL, s); \
21074 ++START; \
21075 s->x = (X); \
21076 } \
21077 while (0)
21078
21079
21080 /* Add a glyph string for a sequence of character glyphs to the list
21081 of strings between HEAD and TAIL. START is the index of the first
21082 glyph in row area AREA of glyph row ROW that is part of the new
21083 glyph string. END is the index of the last glyph in that glyph row
21084 area. X is the current output position assigned to the new glyph
21085 string constructed. HL overrides that face of the glyph; e.g. it
21086 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21087 right-most x-position of the drawing area. */
21088
21089 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21090 do \
21091 { \
21092 int face_id; \
21093 XChar2b *char2b; \
21094 \
21095 face_id = (row)->glyphs[area][START].face_id; \
21096 \
21097 s = (struct glyph_string *) alloca (sizeof *s); \
21098 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21099 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21100 append_glyph_string (&HEAD, &TAIL, s); \
21101 s->x = (X); \
21102 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21103 } \
21104 while (0)
21105
21106
21107 /* Add a glyph string for a composite sequence to the list of strings
21108 between HEAD and TAIL. START is the index of the first glyph in
21109 row area AREA of glyph row ROW that is part of the new glyph
21110 string. END is the index of the last glyph in that glyph row area.
21111 X is the current output position assigned to the new glyph string
21112 constructed. HL overrides that face of the glyph; e.g. it is
21113 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21114 x-position of the drawing area. */
21115
21116 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21117 do { \
21118 int face_id = (row)->glyphs[area][START].face_id; \
21119 struct face *base_face = FACE_FROM_ID (f, face_id); \
21120 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21121 struct composition *cmp = composition_table[cmp_id]; \
21122 XChar2b *char2b; \
21123 struct glyph_string *first_s; \
21124 int n; \
21125 \
21126 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21127 \
21128 /* Make glyph_strings for each glyph sequence that is drawable by \
21129 the same face, and append them to HEAD/TAIL. */ \
21130 for (n = 0; n < cmp->glyph_len;) \
21131 { \
21132 s = (struct glyph_string *) alloca (sizeof *s); \
21133 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21134 append_glyph_string (&(HEAD), &(TAIL), s); \
21135 s->cmp = cmp; \
21136 s->cmp_from = n; \
21137 s->x = (X); \
21138 if (n == 0) \
21139 first_s = s; \
21140 n = fill_composite_glyph_string (s, base_face, overlaps); \
21141 } \
21142 \
21143 ++START; \
21144 s = first_s; \
21145 } while (0)
21146
21147
21148 /* Add a glyph string for a glyph-string sequence to the list of strings
21149 between HEAD and TAIL. */
21150
21151 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21152 do { \
21153 int face_id; \
21154 XChar2b *char2b; \
21155 Lisp_Object gstring; \
21156 \
21157 face_id = (row)->glyphs[area][START].face_id; \
21158 gstring = (composition_gstring_from_id \
21159 ((row)->glyphs[area][START].u.cmp.id)); \
21160 s = (struct glyph_string *) alloca (sizeof *s); \
21161 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21162 * LGSTRING_GLYPH_LEN (gstring)); \
21163 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21164 append_glyph_string (&(HEAD), &(TAIL), s); \
21165 s->x = (X); \
21166 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21167 } while (0)
21168
21169
21170 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21171 of AREA of glyph row ROW on window W between indices START and END.
21172 HL overrides the face for drawing glyph strings, e.g. it is
21173 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21174 x-positions of the drawing area.
21175
21176 This is an ugly monster macro construct because we must use alloca
21177 to allocate glyph strings (because draw_glyphs can be called
21178 asynchronously). */
21179
21180 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21181 do \
21182 { \
21183 HEAD = TAIL = NULL; \
21184 while (START < END) \
21185 { \
21186 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21187 switch (first_glyph->type) \
21188 { \
21189 case CHAR_GLYPH: \
21190 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21191 HL, X, LAST_X); \
21192 break; \
21193 \
21194 case COMPOSITE_GLYPH: \
21195 if (first_glyph->u.cmp.automatic) \
21196 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21197 HL, X, LAST_X); \
21198 else \
21199 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21200 HL, X, LAST_X); \
21201 break; \
21202 \
21203 case STRETCH_GLYPH: \
21204 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21205 HL, X, LAST_X); \
21206 break; \
21207 \
21208 case IMAGE_GLYPH: \
21209 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21210 HL, X, LAST_X); \
21211 break; \
21212 \
21213 default: \
21214 abort (); \
21215 } \
21216 \
21217 if (s) \
21218 { \
21219 set_glyph_string_background_width (s, START, LAST_X); \
21220 (X) += s->width; \
21221 } \
21222 } \
21223 } while (0)
21224
21225
21226 /* Draw glyphs between START and END in AREA of ROW on window W,
21227 starting at x-position X. X is relative to AREA in W. HL is a
21228 face-override with the following meaning:
21229
21230 DRAW_NORMAL_TEXT draw normally
21231 DRAW_CURSOR draw in cursor face
21232 DRAW_MOUSE_FACE draw in mouse face.
21233 DRAW_INVERSE_VIDEO draw in mode line face
21234 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21235 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21236
21237 If OVERLAPS is non-zero, draw only the foreground of characters and
21238 clip to the physical height of ROW. Non-zero value also defines
21239 the overlapping part to be drawn:
21240
21241 OVERLAPS_PRED overlap with preceding rows
21242 OVERLAPS_SUCC overlap with succeeding rows
21243 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21244 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21245
21246 Value is the x-position reached, relative to AREA of W. */
21247
21248 static int
21249 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21250 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21251 enum draw_glyphs_face hl, int overlaps)
21252 {
21253 struct glyph_string *head, *tail;
21254 struct glyph_string *s;
21255 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21256 int i, j, x_reached, last_x, area_left = 0;
21257 struct frame *f = XFRAME (WINDOW_FRAME (w));
21258 DECLARE_HDC (hdc);
21259
21260 ALLOCATE_HDC (hdc, f);
21261
21262 /* Let's rather be paranoid than getting a SEGV. */
21263 end = min (end, row->used[area]);
21264 start = max (0, start);
21265 start = min (end, start);
21266
21267 /* Translate X to frame coordinates. Set last_x to the right
21268 end of the drawing area. */
21269 if (row->full_width_p)
21270 {
21271 /* X is relative to the left edge of W, without scroll bars
21272 or fringes. */
21273 area_left = WINDOW_LEFT_EDGE_X (w);
21274 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21275 }
21276 else
21277 {
21278 area_left = window_box_left (w, area);
21279 last_x = area_left + window_box_width (w, area);
21280 }
21281 x += area_left;
21282
21283 /* Build a doubly-linked list of glyph_string structures between
21284 head and tail from what we have to draw. Note that the macro
21285 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21286 the reason we use a separate variable `i'. */
21287 i = start;
21288 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21289 if (tail)
21290 x_reached = tail->x + tail->background_width;
21291 else
21292 x_reached = x;
21293
21294 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21295 the row, redraw some glyphs in front or following the glyph
21296 strings built above. */
21297 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21298 {
21299 struct glyph_string *h, *t;
21300 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21301 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
21302 int dummy_x = 0;
21303
21304 /* If mouse highlighting is on, we may need to draw adjacent
21305 glyphs using mouse-face highlighting. */
21306 if (area == TEXT_AREA && row->mouse_face_p)
21307 {
21308 struct glyph_row *mouse_beg_row, *mouse_end_row;
21309
21310 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21311 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21312
21313 if (row >= mouse_beg_row && row <= mouse_end_row)
21314 {
21315 check_mouse_face = 1;
21316 mouse_beg_col = (row == mouse_beg_row)
21317 ? dpyinfo->mouse_face_beg_col : 0;
21318 mouse_end_col = (row == mouse_end_row)
21319 ? dpyinfo->mouse_face_end_col
21320 : row->used[TEXT_AREA];
21321 }
21322 }
21323
21324 /* Compute overhangs for all glyph strings. */
21325 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21326 for (s = head; s; s = s->next)
21327 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21328
21329 /* Prepend glyph strings for glyphs in front of the first glyph
21330 string that are overwritten because of the first glyph
21331 string's left overhang. The background of all strings
21332 prepended must be drawn because the first glyph string
21333 draws over it. */
21334 i = left_overwritten (head);
21335 if (i >= 0)
21336 {
21337 enum draw_glyphs_face overlap_hl;
21338
21339 /* If this row contains mouse highlighting, attempt to draw
21340 the overlapped glyphs with the correct highlight. This
21341 code fails if the overlap encompasses more than one glyph
21342 and mouse-highlight spans only some of these glyphs.
21343 However, making it work perfectly involves a lot more
21344 code, and I don't know if the pathological case occurs in
21345 practice, so we'll stick to this for now. --- cyd */
21346 if (check_mouse_face
21347 && mouse_beg_col < start && mouse_end_col > i)
21348 overlap_hl = DRAW_MOUSE_FACE;
21349 else
21350 overlap_hl = DRAW_NORMAL_TEXT;
21351
21352 j = i;
21353 BUILD_GLYPH_STRINGS (j, start, h, t,
21354 overlap_hl, dummy_x, last_x);
21355 start = i;
21356 compute_overhangs_and_x (t, head->x, 1);
21357 prepend_glyph_string_lists (&head, &tail, h, t);
21358 clip_head = head;
21359 }
21360
21361 /* Prepend glyph strings for glyphs in front of the first glyph
21362 string that overwrite that glyph string because of their
21363 right overhang. For these strings, only the foreground must
21364 be drawn, because it draws over the glyph string at `head'.
21365 The background must not be drawn because this would overwrite
21366 right overhangs of preceding glyphs for which no glyph
21367 strings exist. */
21368 i = left_overwriting (head);
21369 if (i >= 0)
21370 {
21371 enum draw_glyphs_face overlap_hl;
21372
21373 if (check_mouse_face
21374 && mouse_beg_col < start && mouse_end_col > i)
21375 overlap_hl = DRAW_MOUSE_FACE;
21376 else
21377 overlap_hl = DRAW_NORMAL_TEXT;
21378
21379 clip_head = head;
21380 BUILD_GLYPH_STRINGS (i, start, h, t,
21381 overlap_hl, dummy_x, last_x);
21382 for (s = h; s; s = s->next)
21383 s->background_filled_p = 1;
21384 compute_overhangs_and_x (t, head->x, 1);
21385 prepend_glyph_string_lists (&head, &tail, h, t);
21386 }
21387
21388 /* Append glyphs strings for glyphs following the last glyph
21389 string tail that are overwritten by tail. The background of
21390 these strings has to be drawn because tail's foreground draws
21391 over it. */
21392 i = right_overwritten (tail);
21393 if (i >= 0)
21394 {
21395 enum draw_glyphs_face overlap_hl;
21396
21397 if (check_mouse_face
21398 && mouse_beg_col < i && mouse_end_col > end)
21399 overlap_hl = DRAW_MOUSE_FACE;
21400 else
21401 overlap_hl = DRAW_NORMAL_TEXT;
21402
21403 BUILD_GLYPH_STRINGS (end, i, h, t,
21404 overlap_hl, x, last_x);
21405 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21406 we don't have `end = i;' here. */
21407 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21408 append_glyph_string_lists (&head, &tail, h, t);
21409 clip_tail = tail;
21410 }
21411
21412 /* Append glyph strings for glyphs following the last glyph
21413 string tail that overwrite tail. The foreground of such
21414 glyphs has to be drawn because it writes into the background
21415 of tail. The background must not be drawn because it could
21416 paint over the foreground of following glyphs. */
21417 i = right_overwriting (tail);
21418 if (i >= 0)
21419 {
21420 enum draw_glyphs_face overlap_hl;
21421 if (check_mouse_face
21422 && mouse_beg_col < i && mouse_end_col > end)
21423 overlap_hl = DRAW_MOUSE_FACE;
21424 else
21425 overlap_hl = DRAW_NORMAL_TEXT;
21426
21427 clip_tail = tail;
21428 i++; /* We must include the Ith glyph. */
21429 BUILD_GLYPH_STRINGS (end, i, h, t,
21430 overlap_hl, x, last_x);
21431 for (s = h; s; s = s->next)
21432 s->background_filled_p = 1;
21433 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21434 append_glyph_string_lists (&head, &tail, h, t);
21435 }
21436 if (clip_head || clip_tail)
21437 for (s = head; s; s = s->next)
21438 {
21439 s->clip_head = clip_head;
21440 s->clip_tail = clip_tail;
21441 }
21442 }
21443
21444 /* Draw all strings. */
21445 for (s = head; s; s = s->next)
21446 FRAME_RIF (f)->draw_glyph_string (s);
21447
21448 #ifndef HAVE_NS
21449 /* When focus a sole frame and move horizontally, this sets on_p to 0
21450 causing a failure to erase prev cursor position. */
21451 if (area == TEXT_AREA
21452 && !row->full_width_p
21453 /* When drawing overlapping rows, only the glyph strings'
21454 foreground is drawn, which doesn't erase a cursor
21455 completely. */
21456 && !overlaps)
21457 {
21458 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21459 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21460 : (tail ? tail->x + tail->background_width : x));
21461 x0 -= area_left;
21462 x1 -= area_left;
21463
21464 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21465 row->y, MATRIX_ROW_BOTTOM_Y (row));
21466 }
21467 #endif
21468
21469 /* Value is the x-position up to which drawn, relative to AREA of W.
21470 This doesn't include parts drawn because of overhangs. */
21471 if (row->full_width_p)
21472 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21473 else
21474 x_reached -= area_left;
21475
21476 RELEASE_HDC (hdc, f);
21477
21478 return x_reached;
21479 }
21480
21481 /* Expand row matrix if too narrow. Don't expand if area
21482 is not present. */
21483
21484 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21485 { \
21486 if (!fonts_changed_p \
21487 && (it->glyph_row->glyphs[area] \
21488 < it->glyph_row->glyphs[area + 1])) \
21489 { \
21490 it->w->ncols_scale_factor++; \
21491 fonts_changed_p = 1; \
21492 } \
21493 }
21494
21495 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21496 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21497
21498 static INLINE void
21499 append_glyph (struct it *it)
21500 {
21501 struct glyph *glyph;
21502 enum glyph_row_area area = it->area;
21503
21504 xassert (it->glyph_row);
21505 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21506
21507 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21508 if (glyph < it->glyph_row->glyphs[area + 1])
21509 {
21510 /* If the glyph row is reversed, we need to prepend the glyph
21511 rather than append it. */
21512 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21513 {
21514 struct glyph *g;
21515
21516 /* Make room for the additional glyph. */
21517 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21518 g[1] = *g;
21519 glyph = it->glyph_row->glyphs[area];
21520 }
21521 glyph->charpos = CHARPOS (it->position);
21522 glyph->object = it->object;
21523 if (it->pixel_width > 0)
21524 {
21525 glyph->pixel_width = it->pixel_width;
21526 glyph->padding_p = 0;
21527 }
21528 else
21529 {
21530 /* Assure at least 1-pixel width. Otherwise, cursor can't
21531 be displayed correctly. */
21532 glyph->pixel_width = 1;
21533 glyph->padding_p = 1;
21534 }
21535 glyph->ascent = it->ascent;
21536 glyph->descent = it->descent;
21537 glyph->voffset = it->voffset;
21538 glyph->type = CHAR_GLYPH;
21539 glyph->avoid_cursor_p = it->avoid_cursor_p;
21540 glyph->multibyte_p = it->multibyte_p;
21541 glyph->left_box_line_p = it->start_of_box_run_p;
21542 glyph->right_box_line_p = it->end_of_box_run_p;
21543 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21544 || it->phys_descent > it->descent);
21545 glyph->glyph_not_available_p = it->glyph_not_available_p;
21546 glyph->face_id = it->face_id;
21547 glyph->u.ch = it->char_to_display;
21548 glyph->slice.img = null_glyph_slice;
21549 glyph->font_type = FONT_TYPE_UNKNOWN;
21550 if (it->bidi_p)
21551 {
21552 glyph->resolved_level = it->bidi_it.resolved_level;
21553 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21554 abort ();
21555 glyph->bidi_type = it->bidi_it.type;
21556 }
21557 else
21558 {
21559 glyph->resolved_level = 0;
21560 glyph->bidi_type = UNKNOWN_BT;
21561 }
21562 ++it->glyph_row->used[area];
21563 }
21564 else
21565 IT_EXPAND_MATRIX_WIDTH (it, area);
21566 }
21567
21568 /* Store one glyph for the composition IT->cmp_it.id in
21569 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21570 non-null. */
21571
21572 static INLINE void
21573 append_composite_glyph (struct it *it)
21574 {
21575 struct glyph *glyph;
21576 enum glyph_row_area area = it->area;
21577
21578 xassert (it->glyph_row);
21579
21580 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21581 if (glyph < it->glyph_row->glyphs[area + 1])
21582 {
21583 /* If the glyph row is reversed, we need to prepend the glyph
21584 rather than append it. */
21585 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
21586 {
21587 struct glyph *g;
21588
21589 /* Make room for the new glyph. */
21590 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
21591 g[1] = *g;
21592 glyph = it->glyph_row->glyphs[it->area];
21593 }
21594 glyph->charpos = it->cmp_it.charpos;
21595 glyph->object = it->object;
21596 glyph->pixel_width = it->pixel_width;
21597 glyph->ascent = it->ascent;
21598 glyph->descent = it->descent;
21599 glyph->voffset = it->voffset;
21600 glyph->type = COMPOSITE_GLYPH;
21601 if (it->cmp_it.ch < 0)
21602 {
21603 glyph->u.cmp.automatic = 0;
21604 glyph->u.cmp.id = it->cmp_it.id;
21605 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
21606 }
21607 else
21608 {
21609 glyph->u.cmp.automatic = 1;
21610 glyph->u.cmp.id = it->cmp_it.id;
21611 glyph->slice.cmp.from = it->cmp_it.from;
21612 glyph->slice.cmp.to = it->cmp_it.to - 1;
21613 }
21614 glyph->avoid_cursor_p = it->avoid_cursor_p;
21615 glyph->multibyte_p = it->multibyte_p;
21616 glyph->left_box_line_p = it->start_of_box_run_p;
21617 glyph->right_box_line_p = it->end_of_box_run_p;
21618 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21619 || it->phys_descent > it->descent);
21620 glyph->padding_p = 0;
21621 glyph->glyph_not_available_p = 0;
21622 glyph->face_id = it->face_id;
21623 glyph->font_type = FONT_TYPE_UNKNOWN;
21624 if (it->bidi_p)
21625 {
21626 glyph->resolved_level = it->bidi_it.resolved_level;
21627 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21628 abort ();
21629 glyph->bidi_type = it->bidi_it.type;
21630 }
21631 ++it->glyph_row->used[area];
21632 }
21633 else
21634 IT_EXPAND_MATRIX_WIDTH (it, area);
21635 }
21636
21637
21638 /* Change IT->ascent and IT->height according to the setting of
21639 IT->voffset. */
21640
21641 static INLINE void
21642 take_vertical_position_into_account (struct it *it)
21643 {
21644 if (it->voffset)
21645 {
21646 if (it->voffset < 0)
21647 /* Increase the ascent so that we can display the text higher
21648 in the line. */
21649 it->ascent -= it->voffset;
21650 else
21651 /* Increase the descent so that we can display the text lower
21652 in the line. */
21653 it->descent += it->voffset;
21654 }
21655 }
21656
21657
21658 /* Produce glyphs/get display metrics for the image IT is loaded with.
21659 See the description of struct display_iterator in dispextern.h for
21660 an overview of struct display_iterator. */
21661
21662 static void
21663 produce_image_glyph (struct it *it)
21664 {
21665 struct image *img;
21666 struct face *face;
21667 int glyph_ascent, crop;
21668 struct glyph_slice slice;
21669
21670 xassert (it->what == IT_IMAGE);
21671
21672 face = FACE_FROM_ID (it->f, it->face_id);
21673 xassert (face);
21674 /* Make sure X resources of the face is loaded. */
21675 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21676
21677 if (it->image_id < 0)
21678 {
21679 /* Fringe bitmap. */
21680 it->ascent = it->phys_ascent = 0;
21681 it->descent = it->phys_descent = 0;
21682 it->pixel_width = 0;
21683 it->nglyphs = 0;
21684 return;
21685 }
21686
21687 img = IMAGE_FROM_ID (it->f, it->image_id);
21688 xassert (img);
21689 /* Make sure X resources of the image is loaded. */
21690 prepare_image_for_display (it->f, img);
21691
21692 slice.x = slice.y = 0;
21693 slice.width = img->width;
21694 slice.height = img->height;
21695
21696 if (INTEGERP (it->slice.x))
21697 slice.x = XINT (it->slice.x);
21698 else if (FLOATP (it->slice.x))
21699 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21700
21701 if (INTEGERP (it->slice.y))
21702 slice.y = XINT (it->slice.y);
21703 else if (FLOATP (it->slice.y))
21704 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21705
21706 if (INTEGERP (it->slice.width))
21707 slice.width = XINT (it->slice.width);
21708 else if (FLOATP (it->slice.width))
21709 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21710
21711 if (INTEGERP (it->slice.height))
21712 slice.height = XINT (it->slice.height);
21713 else if (FLOATP (it->slice.height))
21714 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21715
21716 if (slice.x >= img->width)
21717 slice.x = img->width;
21718 if (slice.y >= img->height)
21719 slice.y = img->height;
21720 if (slice.x + slice.width >= img->width)
21721 slice.width = img->width - slice.x;
21722 if (slice.y + slice.height > img->height)
21723 slice.height = img->height - slice.y;
21724
21725 if (slice.width == 0 || slice.height == 0)
21726 return;
21727
21728 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21729
21730 it->descent = slice.height - glyph_ascent;
21731 if (slice.y == 0)
21732 it->descent += img->vmargin;
21733 if (slice.y + slice.height == img->height)
21734 it->descent += img->vmargin;
21735 it->phys_descent = it->descent;
21736
21737 it->pixel_width = slice.width;
21738 if (slice.x == 0)
21739 it->pixel_width += img->hmargin;
21740 if (slice.x + slice.width == img->width)
21741 it->pixel_width += img->hmargin;
21742
21743 /* It's quite possible for images to have an ascent greater than
21744 their height, so don't get confused in that case. */
21745 if (it->descent < 0)
21746 it->descent = 0;
21747
21748 it->nglyphs = 1;
21749
21750 if (face->box != FACE_NO_BOX)
21751 {
21752 if (face->box_line_width > 0)
21753 {
21754 if (slice.y == 0)
21755 it->ascent += face->box_line_width;
21756 if (slice.y + slice.height == img->height)
21757 it->descent += face->box_line_width;
21758 }
21759
21760 if (it->start_of_box_run_p && slice.x == 0)
21761 it->pixel_width += eabs (face->box_line_width);
21762 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21763 it->pixel_width += eabs (face->box_line_width);
21764 }
21765
21766 take_vertical_position_into_account (it);
21767
21768 /* Automatically crop wide image glyphs at right edge so we can
21769 draw the cursor on same display row. */
21770 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21771 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21772 {
21773 it->pixel_width -= crop;
21774 slice.width -= crop;
21775 }
21776
21777 if (it->glyph_row)
21778 {
21779 struct glyph *glyph;
21780 enum glyph_row_area area = it->area;
21781
21782 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21783 if (glyph < it->glyph_row->glyphs[area + 1])
21784 {
21785 glyph->charpos = CHARPOS (it->position);
21786 glyph->object = it->object;
21787 glyph->pixel_width = it->pixel_width;
21788 glyph->ascent = glyph_ascent;
21789 glyph->descent = it->descent;
21790 glyph->voffset = it->voffset;
21791 glyph->type = IMAGE_GLYPH;
21792 glyph->avoid_cursor_p = it->avoid_cursor_p;
21793 glyph->multibyte_p = it->multibyte_p;
21794 glyph->left_box_line_p = it->start_of_box_run_p;
21795 glyph->right_box_line_p = it->end_of_box_run_p;
21796 glyph->overlaps_vertically_p = 0;
21797 glyph->padding_p = 0;
21798 glyph->glyph_not_available_p = 0;
21799 glyph->face_id = it->face_id;
21800 glyph->u.img_id = img->id;
21801 glyph->slice.img = slice;
21802 glyph->font_type = FONT_TYPE_UNKNOWN;
21803 if (it->bidi_p)
21804 {
21805 glyph->resolved_level = it->bidi_it.resolved_level;
21806 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21807 abort ();
21808 glyph->bidi_type = it->bidi_it.type;
21809 }
21810 ++it->glyph_row->used[area];
21811 }
21812 else
21813 IT_EXPAND_MATRIX_WIDTH (it, area);
21814 }
21815 }
21816
21817
21818 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
21819 of the glyph, WIDTH and HEIGHT are the width and height of the
21820 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
21821
21822 static void
21823 append_stretch_glyph (struct it *it, Lisp_Object object,
21824 int width, int height, int ascent)
21825 {
21826 struct glyph *glyph;
21827 enum glyph_row_area area = it->area;
21828
21829 xassert (ascent >= 0 && ascent <= height);
21830
21831 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21832 if (glyph < it->glyph_row->glyphs[area + 1])
21833 {
21834 /* If the glyph row is reversed, we need to prepend the glyph
21835 rather than append it. */
21836 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21837 {
21838 struct glyph *g;
21839
21840 /* Make room for the additional glyph. */
21841 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21842 g[1] = *g;
21843 glyph = it->glyph_row->glyphs[area];
21844 }
21845 glyph->charpos = CHARPOS (it->position);
21846 glyph->object = object;
21847 glyph->pixel_width = width;
21848 glyph->ascent = ascent;
21849 glyph->descent = height - ascent;
21850 glyph->voffset = it->voffset;
21851 glyph->type = STRETCH_GLYPH;
21852 glyph->avoid_cursor_p = it->avoid_cursor_p;
21853 glyph->multibyte_p = it->multibyte_p;
21854 glyph->left_box_line_p = it->start_of_box_run_p;
21855 glyph->right_box_line_p = it->end_of_box_run_p;
21856 glyph->overlaps_vertically_p = 0;
21857 glyph->padding_p = 0;
21858 glyph->glyph_not_available_p = 0;
21859 glyph->face_id = it->face_id;
21860 glyph->u.stretch.ascent = ascent;
21861 glyph->u.stretch.height = height;
21862 glyph->slice.img = null_glyph_slice;
21863 glyph->font_type = FONT_TYPE_UNKNOWN;
21864 if (it->bidi_p)
21865 {
21866 glyph->resolved_level = it->bidi_it.resolved_level;
21867 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21868 abort ();
21869 glyph->bidi_type = it->bidi_it.type;
21870 }
21871 else
21872 {
21873 glyph->resolved_level = 0;
21874 glyph->bidi_type = UNKNOWN_BT;
21875 }
21876 ++it->glyph_row->used[area];
21877 }
21878 else
21879 IT_EXPAND_MATRIX_WIDTH (it, area);
21880 }
21881
21882
21883 /* Produce a stretch glyph for iterator IT. IT->object is the value
21884 of the glyph property displayed. The value must be a list
21885 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
21886 being recognized:
21887
21888 1. `:width WIDTH' specifies that the space should be WIDTH *
21889 canonical char width wide. WIDTH may be an integer or floating
21890 point number.
21891
21892 2. `:relative-width FACTOR' specifies that the width of the stretch
21893 should be computed from the width of the first character having the
21894 `glyph' property, and should be FACTOR times that width.
21895
21896 3. `:align-to HPOS' specifies that the space should be wide enough
21897 to reach HPOS, a value in canonical character units.
21898
21899 Exactly one of the above pairs must be present.
21900
21901 4. `:height HEIGHT' specifies that the height of the stretch produced
21902 should be HEIGHT, measured in canonical character units.
21903
21904 5. `:relative-height FACTOR' specifies that the height of the
21905 stretch should be FACTOR times the height of the characters having
21906 the glyph property.
21907
21908 Either none or exactly one of 4 or 5 must be present.
21909
21910 6. `:ascent ASCENT' specifies that ASCENT percent of the height
21911 of the stretch should be used for the ascent of the stretch.
21912 ASCENT must be in the range 0 <= ASCENT <= 100. */
21913
21914 static void
21915 produce_stretch_glyph (struct it *it)
21916 {
21917 /* (space :width WIDTH :height HEIGHT ...) */
21918 Lisp_Object prop, plist;
21919 int width = 0, height = 0, align_to = -1;
21920 int zero_width_ok_p = 0, zero_height_ok_p = 0;
21921 int ascent = 0;
21922 double tem;
21923 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21924 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
21925
21926 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21927
21928 /* List should start with `space'. */
21929 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
21930 plist = XCDR (it->object);
21931
21932 /* Compute the width of the stretch. */
21933 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
21934 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
21935 {
21936 /* Absolute width `:width WIDTH' specified and valid. */
21937 zero_width_ok_p = 1;
21938 width = (int)tem;
21939 }
21940 else if (prop = Fplist_get (plist, QCrelative_width),
21941 NUMVAL (prop) > 0)
21942 {
21943 /* Relative width `:relative-width FACTOR' specified and valid.
21944 Compute the width of the characters having the `glyph'
21945 property. */
21946 struct it it2;
21947 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
21948
21949 it2 = *it;
21950 if (it->multibyte_p)
21951 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
21952 else
21953 {
21954 it2.c = it2.char_to_display = *p, it2.len = 1;
21955 if (! ASCII_CHAR_P (it2.c))
21956 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
21957 }
21958
21959 it2.glyph_row = NULL;
21960 it2.what = IT_CHARACTER;
21961 x_produce_glyphs (&it2);
21962 width = NUMVAL (prop) * it2.pixel_width;
21963 }
21964 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
21965 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
21966 {
21967 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
21968 align_to = (align_to < 0
21969 ? 0
21970 : align_to - window_box_left_offset (it->w, TEXT_AREA));
21971 else if (align_to < 0)
21972 align_to = window_box_left_offset (it->w, TEXT_AREA);
21973 width = max (0, (int)tem + align_to - it->current_x);
21974 zero_width_ok_p = 1;
21975 }
21976 else
21977 /* Nothing specified -> width defaults to canonical char width. */
21978 width = FRAME_COLUMN_WIDTH (it->f);
21979
21980 if (width <= 0 && (width < 0 || !zero_width_ok_p))
21981 width = 1;
21982
21983 /* Compute height. */
21984 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
21985 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21986 {
21987 height = (int)tem;
21988 zero_height_ok_p = 1;
21989 }
21990 else if (prop = Fplist_get (plist, QCrelative_height),
21991 NUMVAL (prop) > 0)
21992 height = FONT_HEIGHT (font) * NUMVAL (prop);
21993 else
21994 height = FONT_HEIGHT (font);
21995
21996 if (height <= 0 && (height < 0 || !zero_height_ok_p))
21997 height = 1;
21998
21999 /* Compute percentage of height used for ascent. If
22000 `:ascent ASCENT' is present and valid, use that. Otherwise,
22001 derive the ascent from the font in use. */
22002 if (prop = Fplist_get (plist, QCascent),
22003 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22004 ascent = height * NUMVAL (prop) / 100.0;
22005 else if (!NILP (prop)
22006 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22007 ascent = min (max (0, (int)tem), height);
22008 else
22009 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22010
22011 if (width > 0 && it->line_wrap != TRUNCATE
22012 && it->current_x + width > it->last_visible_x)
22013 width = it->last_visible_x - it->current_x - 1;
22014
22015 if (width > 0 && height > 0 && it->glyph_row)
22016 {
22017 Lisp_Object object = it->stack[it->sp - 1].string;
22018 if (!STRINGP (object))
22019 object = it->w->buffer;
22020 append_stretch_glyph (it, object, width, height, ascent);
22021 }
22022
22023 it->pixel_width = width;
22024 it->ascent = it->phys_ascent = ascent;
22025 it->descent = it->phys_descent = height - it->ascent;
22026 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22027
22028 take_vertical_position_into_account (it);
22029 }
22030
22031 /* Calculate line-height and line-spacing properties.
22032 An integer value specifies explicit pixel value.
22033 A float value specifies relative value to current face height.
22034 A cons (float . face-name) specifies relative value to
22035 height of specified face font.
22036
22037 Returns height in pixels, or nil. */
22038
22039
22040 static Lisp_Object
22041 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22042 int boff, int override)
22043 {
22044 Lisp_Object face_name = Qnil;
22045 int ascent, descent, height;
22046
22047 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22048 return val;
22049
22050 if (CONSP (val))
22051 {
22052 face_name = XCAR (val);
22053 val = XCDR (val);
22054 if (!NUMBERP (val))
22055 val = make_number (1);
22056 if (NILP (face_name))
22057 {
22058 height = it->ascent + it->descent;
22059 goto scale;
22060 }
22061 }
22062
22063 if (NILP (face_name))
22064 {
22065 font = FRAME_FONT (it->f);
22066 boff = FRAME_BASELINE_OFFSET (it->f);
22067 }
22068 else if (EQ (face_name, Qt))
22069 {
22070 override = 0;
22071 }
22072 else
22073 {
22074 int face_id;
22075 struct face *face;
22076
22077 face_id = lookup_named_face (it->f, face_name, 0);
22078 if (face_id < 0)
22079 return make_number (-1);
22080
22081 face = FACE_FROM_ID (it->f, face_id);
22082 font = face->font;
22083 if (font == NULL)
22084 return make_number (-1);
22085 boff = font->baseline_offset;
22086 if (font->vertical_centering)
22087 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22088 }
22089
22090 ascent = FONT_BASE (font) + boff;
22091 descent = FONT_DESCENT (font) - boff;
22092
22093 if (override)
22094 {
22095 it->override_ascent = ascent;
22096 it->override_descent = descent;
22097 it->override_boff = boff;
22098 }
22099
22100 height = ascent + descent;
22101
22102 scale:
22103 if (FLOATP (val))
22104 height = (int)(XFLOAT_DATA (val) * height);
22105 else if (INTEGERP (val))
22106 height *= XINT (val);
22107
22108 return make_number (height);
22109 }
22110
22111
22112 /* RIF:
22113 Produce glyphs/get display metrics for the display element IT is
22114 loaded with. See the description of struct it in dispextern.h
22115 for an overview of struct it. */
22116
22117 void
22118 x_produce_glyphs (struct it *it)
22119 {
22120 int extra_line_spacing = it->extra_line_spacing;
22121
22122 it->glyph_not_available_p = 0;
22123
22124 if (it->what == IT_CHARACTER)
22125 {
22126 XChar2b char2b;
22127 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22128 struct font *font = face->font;
22129 int font_not_found_p = font == NULL;
22130 struct font_metrics *pcm = NULL;
22131 int boff; /* baseline offset */
22132
22133 if (font_not_found_p)
22134 {
22135 /* When no suitable font found, display an empty box based
22136 on the metrics of the font of the default face (or what
22137 remapped). */
22138 struct face *no_font_face
22139 = FACE_FROM_ID (it->f,
22140 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
22141 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
22142 font = no_font_face->font;
22143 boff = font->baseline_offset;
22144 }
22145 else
22146 {
22147 boff = font->baseline_offset;
22148 if (font->vertical_centering)
22149 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22150 }
22151
22152 if (it->char_to_display != '\n' && it->char_to_display != '\t')
22153 {
22154 int stretched_p;
22155
22156 it->nglyphs = 1;
22157
22158 if (it->override_ascent >= 0)
22159 {
22160 it->ascent = it->override_ascent;
22161 it->descent = it->override_descent;
22162 boff = it->override_boff;
22163 }
22164 else
22165 {
22166 it->ascent = FONT_BASE (font) + boff;
22167 it->descent = FONT_DESCENT (font) - boff;
22168 }
22169
22170 if (! font_not_found_p
22171 && get_char_glyph_code (it->char_to_display, font, &char2b))
22172 {
22173 pcm = get_per_char_metric (it->f, font, &char2b);
22174 if (pcm->width == 0
22175 && pcm->rbearing == 0 && pcm->lbearing == 0)
22176 pcm = NULL;
22177 }
22178
22179 if (pcm)
22180 {
22181 it->phys_ascent = pcm->ascent + boff;
22182 it->phys_descent = pcm->descent - boff;
22183 it->pixel_width = pcm->width;
22184 }
22185 else
22186 {
22187 it->glyph_not_available_p = 1;
22188 it->phys_ascent = it->ascent;
22189 it->phys_descent = it->descent;
22190 it->pixel_width = font->space_width;
22191 }
22192
22193 if (it->constrain_row_ascent_descent_p)
22194 {
22195 if (it->descent > it->max_descent)
22196 {
22197 it->ascent += it->descent - it->max_descent;
22198 it->descent = it->max_descent;
22199 }
22200 if (it->ascent > it->max_ascent)
22201 {
22202 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22203 it->ascent = it->max_ascent;
22204 }
22205 it->phys_ascent = min (it->phys_ascent, it->ascent);
22206 it->phys_descent = min (it->phys_descent, it->descent);
22207 extra_line_spacing = 0;
22208 }
22209
22210 /* If this is a space inside a region of text with
22211 `space-width' property, change its width. */
22212 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22213 if (stretched_p)
22214 it->pixel_width *= XFLOATINT (it->space_width);
22215
22216 /* If face has a box, add the box thickness to the character
22217 height. If character has a box line to the left and/or
22218 right, add the box line width to the character's width. */
22219 if (face->box != FACE_NO_BOX)
22220 {
22221 int thick = face->box_line_width;
22222
22223 if (thick > 0)
22224 {
22225 it->ascent += thick;
22226 it->descent += thick;
22227 }
22228 else
22229 thick = -thick;
22230
22231 if (it->start_of_box_run_p)
22232 it->pixel_width += thick;
22233 if (it->end_of_box_run_p)
22234 it->pixel_width += thick;
22235 }
22236
22237 /* If face has an overline, add the height of the overline
22238 (1 pixel) and a 1 pixel margin to the character height. */
22239 if (face->overline_p)
22240 it->ascent += overline_margin;
22241
22242 if (it->constrain_row_ascent_descent_p)
22243 {
22244 if (it->ascent > it->max_ascent)
22245 it->ascent = it->max_ascent;
22246 if (it->descent > it->max_descent)
22247 it->descent = it->max_descent;
22248 }
22249
22250 take_vertical_position_into_account (it);
22251
22252 /* If we have to actually produce glyphs, do it. */
22253 if (it->glyph_row)
22254 {
22255 if (stretched_p)
22256 {
22257 /* Translate a space with a `space-width' property
22258 into a stretch glyph. */
22259 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22260 / FONT_HEIGHT (font));
22261 append_stretch_glyph (it, it->object, it->pixel_width,
22262 it->ascent + it->descent, ascent);
22263 }
22264 else
22265 append_glyph (it);
22266
22267 /* If characters with lbearing or rbearing are displayed
22268 in this line, record that fact in a flag of the
22269 glyph row. This is used to optimize X output code. */
22270 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22271 it->glyph_row->contains_overlapping_glyphs_p = 1;
22272 }
22273 if (! stretched_p && it->pixel_width == 0)
22274 /* We assure that all visible glyphs have at least 1-pixel
22275 width. */
22276 it->pixel_width = 1;
22277 }
22278 else if (it->char_to_display == '\n')
22279 {
22280 /* A newline has no width, but we need the height of the
22281 line. But if previous part of the line sets a height,
22282 don't increase that height */
22283
22284 Lisp_Object height;
22285 Lisp_Object total_height = Qnil;
22286
22287 it->override_ascent = -1;
22288 it->pixel_width = 0;
22289 it->nglyphs = 0;
22290
22291 height = get_it_property (it, Qline_height);
22292 /* Split (line-height total-height) list */
22293 if (CONSP (height)
22294 && CONSP (XCDR (height))
22295 && NILP (XCDR (XCDR (height))))
22296 {
22297 total_height = XCAR (XCDR (height));
22298 height = XCAR (height);
22299 }
22300 height = calc_line_height_property (it, height, font, boff, 1);
22301
22302 if (it->override_ascent >= 0)
22303 {
22304 it->ascent = it->override_ascent;
22305 it->descent = it->override_descent;
22306 boff = it->override_boff;
22307 }
22308 else
22309 {
22310 it->ascent = FONT_BASE (font) + boff;
22311 it->descent = FONT_DESCENT (font) - boff;
22312 }
22313
22314 if (EQ (height, Qt))
22315 {
22316 if (it->descent > it->max_descent)
22317 {
22318 it->ascent += it->descent - it->max_descent;
22319 it->descent = it->max_descent;
22320 }
22321 if (it->ascent > it->max_ascent)
22322 {
22323 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22324 it->ascent = it->max_ascent;
22325 }
22326 it->phys_ascent = min (it->phys_ascent, it->ascent);
22327 it->phys_descent = min (it->phys_descent, it->descent);
22328 it->constrain_row_ascent_descent_p = 1;
22329 extra_line_spacing = 0;
22330 }
22331 else
22332 {
22333 Lisp_Object spacing;
22334
22335 it->phys_ascent = it->ascent;
22336 it->phys_descent = it->descent;
22337
22338 if ((it->max_ascent > 0 || it->max_descent > 0)
22339 && face->box != FACE_NO_BOX
22340 && face->box_line_width > 0)
22341 {
22342 it->ascent += face->box_line_width;
22343 it->descent += face->box_line_width;
22344 }
22345 if (!NILP (height)
22346 && XINT (height) > it->ascent + it->descent)
22347 it->ascent = XINT (height) - it->descent;
22348
22349 if (!NILP (total_height))
22350 spacing = calc_line_height_property (it, total_height, font, boff, 0);
22351 else
22352 {
22353 spacing = get_it_property (it, Qline_spacing);
22354 spacing = calc_line_height_property (it, spacing, font, boff, 0);
22355 }
22356 if (INTEGERP (spacing))
22357 {
22358 extra_line_spacing = XINT (spacing);
22359 if (!NILP (total_height))
22360 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22361 }
22362 }
22363 }
22364 else /* i.e. (it->char_to_display == '\t') */
22365 {
22366 if (font->space_width > 0)
22367 {
22368 int tab_width = it->tab_width * font->space_width;
22369 int x = it->current_x + it->continuation_lines_width;
22370 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22371
22372 /* If the distance from the current position to the next tab
22373 stop is less than a space character width, use the
22374 tab stop after that. */
22375 if (next_tab_x - x < font->space_width)
22376 next_tab_x += tab_width;
22377
22378 it->pixel_width = next_tab_x - x;
22379 it->nglyphs = 1;
22380 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22381 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22382
22383 if (it->glyph_row)
22384 {
22385 append_stretch_glyph (it, it->object, it->pixel_width,
22386 it->ascent + it->descent, it->ascent);
22387 }
22388 }
22389 else
22390 {
22391 it->pixel_width = 0;
22392 it->nglyphs = 1;
22393 }
22394 }
22395 }
22396 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22397 {
22398 /* A static composition.
22399
22400 Note: A composition is represented as one glyph in the
22401 glyph matrix. There are no padding glyphs.
22402
22403 Important note: pixel_width, ascent, and descent are the
22404 values of what is drawn by draw_glyphs (i.e. the values of
22405 the overall glyphs composed). */
22406 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22407 int boff; /* baseline offset */
22408 struct composition *cmp = composition_table[it->cmp_it.id];
22409 int glyph_len = cmp->glyph_len;
22410 struct font *font = face->font;
22411
22412 it->nglyphs = 1;
22413
22414 /* If we have not yet calculated pixel size data of glyphs of
22415 the composition for the current face font, calculate them
22416 now. Theoretically, we have to check all fonts for the
22417 glyphs, but that requires much time and memory space. So,
22418 here we check only the font of the first glyph. This may
22419 lead to incorrect display, but it's very rare, and C-l
22420 (recenter-top-bottom) can correct the display anyway. */
22421 if (! cmp->font || cmp->font != font)
22422 {
22423 /* Ascent and descent of the font of the first character
22424 of this composition (adjusted by baseline offset).
22425 Ascent and descent of overall glyphs should not be less
22426 than these, respectively. */
22427 int font_ascent, font_descent, font_height;
22428 /* Bounding box of the overall glyphs. */
22429 int leftmost, rightmost, lowest, highest;
22430 int lbearing, rbearing;
22431 int i, width, ascent, descent;
22432 int left_padded = 0, right_padded = 0;
22433 int c;
22434 XChar2b char2b;
22435 struct font_metrics *pcm;
22436 int font_not_found_p;
22437 EMACS_INT pos;
22438
22439 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22440 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22441 break;
22442 if (glyph_len < cmp->glyph_len)
22443 right_padded = 1;
22444 for (i = 0; i < glyph_len; i++)
22445 {
22446 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22447 break;
22448 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22449 }
22450 if (i > 0)
22451 left_padded = 1;
22452
22453 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22454 : IT_CHARPOS (*it));
22455 /* If no suitable font is found, use the default font. */
22456 font_not_found_p = font == NULL;
22457 if (font_not_found_p)
22458 {
22459 face = face->ascii_face;
22460 font = face->font;
22461 }
22462 boff = font->baseline_offset;
22463 if (font->vertical_centering)
22464 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22465 font_ascent = FONT_BASE (font) + boff;
22466 font_descent = FONT_DESCENT (font) - boff;
22467 font_height = FONT_HEIGHT (font);
22468
22469 cmp->font = (void *) font;
22470
22471 pcm = NULL;
22472 if (! font_not_found_p)
22473 {
22474 get_char_face_and_encoding (it->f, c, it->face_id,
22475 &char2b, it->multibyte_p, 0);
22476 pcm = get_per_char_metric (it->f, font, &char2b);
22477 }
22478
22479 /* Initialize the bounding box. */
22480 if (pcm)
22481 {
22482 width = pcm->width;
22483 ascent = pcm->ascent;
22484 descent = pcm->descent;
22485 lbearing = pcm->lbearing;
22486 rbearing = pcm->rbearing;
22487 }
22488 else
22489 {
22490 width = font->space_width;
22491 ascent = FONT_BASE (font);
22492 descent = FONT_DESCENT (font);
22493 lbearing = 0;
22494 rbearing = width;
22495 }
22496
22497 rightmost = width;
22498 leftmost = 0;
22499 lowest = - descent + boff;
22500 highest = ascent + boff;
22501
22502 if (! font_not_found_p
22503 && font->default_ascent
22504 && CHAR_TABLE_P (Vuse_default_ascent)
22505 && !NILP (Faref (Vuse_default_ascent,
22506 make_number (it->char_to_display))))
22507 highest = font->default_ascent + boff;
22508
22509 /* Draw the first glyph at the normal position. It may be
22510 shifted to right later if some other glyphs are drawn
22511 at the left. */
22512 cmp->offsets[i * 2] = 0;
22513 cmp->offsets[i * 2 + 1] = boff;
22514 cmp->lbearing = lbearing;
22515 cmp->rbearing = rbearing;
22516
22517 /* Set cmp->offsets for the remaining glyphs. */
22518 for (i++; i < glyph_len; i++)
22519 {
22520 int left, right, btm, top;
22521 int ch = COMPOSITION_GLYPH (cmp, i);
22522 int face_id;
22523 struct face *this_face;
22524 int this_boff;
22525
22526 if (ch == '\t')
22527 ch = ' ';
22528 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22529 this_face = FACE_FROM_ID (it->f, face_id);
22530 font = this_face->font;
22531
22532 if (font == NULL)
22533 pcm = NULL;
22534 else
22535 {
22536 this_boff = font->baseline_offset;
22537 if (font->vertical_centering)
22538 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22539 get_char_face_and_encoding (it->f, ch, face_id,
22540 &char2b, it->multibyte_p, 0);
22541 pcm = get_per_char_metric (it->f, font, &char2b);
22542 }
22543 if (! pcm)
22544 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22545 else
22546 {
22547 width = pcm->width;
22548 ascent = pcm->ascent;
22549 descent = pcm->descent;
22550 lbearing = pcm->lbearing;
22551 rbearing = pcm->rbearing;
22552 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22553 {
22554 /* Relative composition with or without
22555 alternate chars. */
22556 left = (leftmost + rightmost - width) / 2;
22557 btm = - descent + boff;
22558 if (font->relative_compose
22559 && (! CHAR_TABLE_P (Vignore_relative_composition)
22560 || NILP (Faref (Vignore_relative_composition,
22561 make_number (ch)))))
22562 {
22563
22564 if (- descent >= font->relative_compose)
22565 /* One extra pixel between two glyphs. */
22566 btm = highest + 1;
22567 else if (ascent <= 0)
22568 /* One extra pixel between two glyphs. */
22569 btm = lowest - 1 - ascent - descent;
22570 }
22571 }
22572 else
22573 {
22574 /* A composition rule is specified by an integer
22575 value that encodes global and new reference
22576 points (GREF and NREF). GREF and NREF are
22577 specified by numbers as below:
22578
22579 0---1---2 -- ascent
22580 | |
22581 | |
22582 | |
22583 9--10--11 -- center
22584 | |
22585 ---3---4---5--- baseline
22586 | |
22587 6---7---8 -- descent
22588 */
22589 int rule = COMPOSITION_RULE (cmp, i);
22590 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22591
22592 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22593 grefx = gref % 3, nrefx = nref % 3;
22594 grefy = gref / 3, nrefy = nref / 3;
22595 if (xoff)
22596 xoff = font_height * (xoff - 128) / 256;
22597 if (yoff)
22598 yoff = font_height * (yoff - 128) / 256;
22599
22600 left = (leftmost
22601 + grefx * (rightmost - leftmost) / 2
22602 - nrefx * width / 2
22603 + xoff);
22604
22605 btm = ((grefy == 0 ? highest
22606 : grefy == 1 ? 0
22607 : grefy == 2 ? lowest
22608 : (highest + lowest) / 2)
22609 - (nrefy == 0 ? ascent + descent
22610 : nrefy == 1 ? descent - boff
22611 : nrefy == 2 ? 0
22612 : (ascent + descent) / 2)
22613 + yoff);
22614 }
22615
22616 cmp->offsets[i * 2] = left;
22617 cmp->offsets[i * 2 + 1] = btm + descent;
22618
22619 /* Update the bounding box of the overall glyphs. */
22620 if (width > 0)
22621 {
22622 right = left + width;
22623 if (left < leftmost)
22624 leftmost = left;
22625 if (right > rightmost)
22626 rightmost = right;
22627 }
22628 top = btm + descent + ascent;
22629 if (top > highest)
22630 highest = top;
22631 if (btm < lowest)
22632 lowest = btm;
22633
22634 if (cmp->lbearing > left + lbearing)
22635 cmp->lbearing = left + lbearing;
22636 if (cmp->rbearing < left + rbearing)
22637 cmp->rbearing = left + rbearing;
22638 }
22639 }
22640
22641 /* If there are glyphs whose x-offsets are negative,
22642 shift all glyphs to the right and make all x-offsets
22643 non-negative. */
22644 if (leftmost < 0)
22645 {
22646 for (i = 0; i < cmp->glyph_len; i++)
22647 cmp->offsets[i * 2] -= leftmost;
22648 rightmost -= leftmost;
22649 cmp->lbearing -= leftmost;
22650 cmp->rbearing -= leftmost;
22651 }
22652
22653 if (left_padded && cmp->lbearing < 0)
22654 {
22655 for (i = 0; i < cmp->glyph_len; i++)
22656 cmp->offsets[i * 2] -= cmp->lbearing;
22657 rightmost -= cmp->lbearing;
22658 cmp->rbearing -= cmp->lbearing;
22659 cmp->lbearing = 0;
22660 }
22661 if (right_padded && rightmost < cmp->rbearing)
22662 {
22663 rightmost = cmp->rbearing;
22664 }
22665
22666 cmp->pixel_width = rightmost;
22667 cmp->ascent = highest;
22668 cmp->descent = - lowest;
22669 if (cmp->ascent < font_ascent)
22670 cmp->ascent = font_ascent;
22671 if (cmp->descent < font_descent)
22672 cmp->descent = font_descent;
22673 }
22674
22675 if (it->glyph_row
22676 && (cmp->lbearing < 0
22677 || cmp->rbearing > cmp->pixel_width))
22678 it->glyph_row->contains_overlapping_glyphs_p = 1;
22679
22680 it->pixel_width = cmp->pixel_width;
22681 it->ascent = it->phys_ascent = cmp->ascent;
22682 it->descent = it->phys_descent = cmp->descent;
22683 if (face->box != FACE_NO_BOX)
22684 {
22685 int thick = face->box_line_width;
22686
22687 if (thick > 0)
22688 {
22689 it->ascent += thick;
22690 it->descent += thick;
22691 }
22692 else
22693 thick = - thick;
22694
22695 if (it->start_of_box_run_p)
22696 it->pixel_width += thick;
22697 if (it->end_of_box_run_p)
22698 it->pixel_width += thick;
22699 }
22700
22701 /* If face has an overline, add the height of the overline
22702 (1 pixel) and a 1 pixel margin to the character height. */
22703 if (face->overline_p)
22704 it->ascent += overline_margin;
22705
22706 take_vertical_position_into_account (it);
22707 if (it->ascent < 0)
22708 it->ascent = 0;
22709 if (it->descent < 0)
22710 it->descent = 0;
22711
22712 if (it->glyph_row)
22713 append_composite_glyph (it);
22714 }
22715 else if (it->what == IT_COMPOSITION)
22716 {
22717 /* A dynamic (automatic) composition. */
22718 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22719 Lisp_Object gstring;
22720 struct font_metrics metrics;
22721
22722 gstring = composition_gstring_from_id (it->cmp_it.id);
22723 it->pixel_width
22724 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
22725 &metrics);
22726 if (it->glyph_row
22727 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
22728 it->glyph_row->contains_overlapping_glyphs_p = 1;
22729 it->ascent = it->phys_ascent = metrics.ascent;
22730 it->descent = it->phys_descent = metrics.descent;
22731 if (face->box != FACE_NO_BOX)
22732 {
22733 int thick = face->box_line_width;
22734
22735 if (thick > 0)
22736 {
22737 it->ascent += thick;
22738 it->descent += thick;
22739 }
22740 else
22741 thick = - thick;
22742
22743 if (it->start_of_box_run_p)
22744 it->pixel_width += thick;
22745 if (it->end_of_box_run_p)
22746 it->pixel_width += thick;
22747 }
22748 /* If face has an overline, add the height of the overline
22749 (1 pixel) and a 1 pixel margin to the character height. */
22750 if (face->overline_p)
22751 it->ascent += overline_margin;
22752 take_vertical_position_into_account (it);
22753 if (it->ascent < 0)
22754 it->ascent = 0;
22755 if (it->descent < 0)
22756 it->descent = 0;
22757
22758 if (it->glyph_row)
22759 append_composite_glyph (it);
22760 }
22761 else if (it->what == IT_IMAGE)
22762 produce_image_glyph (it);
22763 else if (it->what == IT_STRETCH)
22764 produce_stretch_glyph (it);
22765
22766 /* Accumulate dimensions. Note: can't assume that it->descent > 0
22767 because this isn't true for images with `:ascent 100'. */
22768 xassert (it->ascent >= 0 && it->descent >= 0);
22769 if (it->area == TEXT_AREA)
22770 it->current_x += it->pixel_width;
22771
22772 if (extra_line_spacing > 0)
22773 {
22774 it->descent += extra_line_spacing;
22775 if (extra_line_spacing > it->max_extra_line_spacing)
22776 it->max_extra_line_spacing = extra_line_spacing;
22777 }
22778
22779 it->max_ascent = max (it->max_ascent, it->ascent);
22780 it->max_descent = max (it->max_descent, it->descent);
22781 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
22782 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
22783 }
22784
22785 /* EXPORT for RIF:
22786 Output LEN glyphs starting at START at the nominal cursor position.
22787 Advance the nominal cursor over the text. The global variable
22788 updated_window contains the window being updated, updated_row is
22789 the glyph row being updated, and updated_area is the area of that
22790 row being updated. */
22791
22792 void
22793 x_write_glyphs (struct glyph *start, int len)
22794 {
22795 int x, hpos;
22796
22797 xassert (updated_window && updated_row);
22798 BLOCK_INPUT;
22799
22800 /* Write glyphs. */
22801
22802 hpos = start - updated_row->glyphs[updated_area];
22803 x = draw_glyphs (updated_window, output_cursor.x,
22804 updated_row, updated_area,
22805 hpos, hpos + len,
22806 DRAW_NORMAL_TEXT, 0);
22807
22808 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
22809 if (updated_area == TEXT_AREA
22810 && updated_window->phys_cursor_on_p
22811 && updated_window->phys_cursor.vpos == output_cursor.vpos
22812 && updated_window->phys_cursor.hpos >= hpos
22813 && updated_window->phys_cursor.hpos < hpos + len)
22814 updated_window->phys_cursor_on_p = 0;
22815
22816 UNBLOCK_INPUT;
22817
22818 /* Advance the output cursor. */
22819 output_cursor.hpos += len;
22820 output_cursor.x = x;
22821 }
22822
22823
22824 /* EXPORT for RIF:
22825 Insert LEN glyphs from START at the nominal cursor position. */
22826
22827 void
22828 x_insert_glyphs (struct glyph *start, int len)
22829 {
22830 struct frame *f;
22831 struct window *w;
22832 int line_height, shift_by_width, shifted_region_width;
22833 struct glyph_row *row;
22834 struct glyph *glyph;
22835 int frame_x, frame_y;
22836 EMACS_INT hpos;
22837
22838 xassert (updated_window && updated_row);
22839 BLOCK_INPUT;
22840 w = updated_window;
22841 f = XFRAME (WINDOW_FRAME (w));
22842
22843 /* Get the height of the line we are in. */
22844 row = updated_row;
22845 line_height = row->height;
22846
22847 /* Get the width of the glyphs to insert. */
22848 shift_by_width = 0;
22849 for (glyph = start; glyph < start + len; ++glyph)
22850 shift_by_width += glyph->pixel_width;
22851
22852 /* Get the width of the region to shift right. */
22853 shifted_region_width = (window_box_width (w, updated_area)
22854 - output_cursor.x
22855 - shift_by_width);
22856
22857 /* Shift right. */
22858 frame_x = window_box_left (w, updated_area) + output_cursor.x;
22859 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
22860
22861 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
22862 line_height, shift_by_width);
22863
22864 /* Write the glyphs. */
22865 hpos = start - row->glyphs[updated_area];
22866 draw_glyphs (w, output_cursor.x, row, updated_area,
22867 hpos, hpos + len,
22868 DRAW_NORMAL_TEXT, 0);
22869
22870 /* Advance the output cursor. */
22871 output_cursor.hpos += len;
22872 output_cursor.x += shift_by_width;
22873 UNBLOCK_INPUT;
22874 }
22875
22876
22877 /* EXPORT for RIF:
22878 Erase the current text line from the nominal cursor position
22879 (inclusive) to pixel column TO_X (exclusive). The idea is that
22880 everything from TO_X onward is already erased.
22881
22882 TO_X is a pixel position relative to updated_area of
22883 updated_window. TO_X == -1 means clear to the end of this area. */
22884
22885 void
22886 x_clear_end_of_line (int to_x)
22887 {
22888 struct frame *f;
22889 struct window *w = updated_window;
22890 int max_x, min_y, max_y;
22891 int from_x, from_y, to_y;
22892
22893 xassert (updated_window && updated_row);
22894 f = XFRAME (w->frame);
22895
22896 if (updated_row->full_width_p)
22897 max_x = WINDOW_TOTAL_WIDTH (w);
22898 else
22899 max_x = window_box_width (w, updated_area);
22900 max_y = window_text_bottom_y (w);
22901
22902 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22903 of window. For TO_X > 0, truncate to end of drawing area. */
22904 if (to_x == 0)
22905 return;
22906 else if (to_x < 0)
22907 to_x = max_x;
22908 else
22909 to_x = min (to_x, max_x);
22910
22911 to_y = min (max_y, output_cursor.y + updated_row->height);
22912
22913 /* Notice if the cursor will be cleared by this operation. */
22914 if (!updated_row->full_width_p)
22915 notice_overwritten_cursor (w, updated_area,
22916 output_cursor.x, -1,
22917 updated_row->y,
22918 MATRIX_ROW_BOTTOM_Y (updated_row));
22919
22920 from_x = output_cursor.x;
22921
22922 /* Translate to frame coordinates. */
22923 if (updated_row->full_width_p)
22924 {
22925 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22926 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22927 }
22928 else
22929 {
22930 int area_left = window_box_left (w, updated_area);
22931 from_x += area_left;
22932 to_x += area_left;
22933 }
22934
22935 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22936 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22937 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22938
22939 /* Prevent inadvertently clearing to end of the X window. */
22940 if (to_x > from_x && to_y > from_y)
22941 {
22942 BLOCK_INPUT;
22943 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22944 to_x - from_x, to_y - from_y);
22945 UNBLOCK_INPUT;
22946 }
22947 }
22948
22949 #endif /* HAVE_WINDOW_SYSTEM */
22950
22951
22952 \f
22953 /***********************************************************************
22954 Cursor types
22955 ***********************************************************************/
22956
22957 /* Value is the internal representation of the specified cursor type
22958 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22959 of the bar cursor. */
22960
22961 static enum text_cursor_kinds
22962 get_specified_cursor_type (Lisp_Object arg, int *width)
22963 {
22964 enum text_cursor_kinds type;
22965
22966 if (NILP (arg))
22967 return NO_CURSOR;
22968
22969 if (EQ (arg, Qbox))
22970 return FILLED_BOX_CURSOR;
22971
22972 if (EQ (arg, Qhollow))
22973 return HOLLOW_BOX_CURSOR;
22974
22975 if (EQ (arg, Qbar))
22976 {
22977 *width = 2;
22978 return BAR_CURSOR;
22979 }
22980
22981 if (CONSP (arg)
22982 && EQ (XCAR (arg), Qbar)
22983 && INTEGERP (XCDR (arg))
22984 && XINT (XCDR (arg)) >= 0)
22985 {
22986 *width = XINT (XCDR (arg));
22987 return BAR_CURSOR;
22988 }
22989
22990 if (EQ (arg, Qhbar))
22991 {
22992 *width = 2;
22993 return HBAR_CURSOR;
22994 }
22995
22996 if (CONSP (arg)
22997 && EQ (XCAR (arg), Qhbar)
22998 && INTEGERP (XCDR (arg))
22999 && XINT (XCDR (arg)) >= 0)
23000 {
23001 *width = XINT (XCDR (arg));
23002 return HBAR_CURSOR;
23003 }
23004
23005 /* Treat anything unknown as "hollow box cursor".
23006 It was bad to signal an error; people have trouble fixing
23007 .Xdefaults with Emacs, when it has something bad in it. */
23008 type = HOLLOW_BOX_CURSOR;
23009
23010 return type;
23011 }
23012
23013 /* Set the default cursor types for specified frame. */
23014 void
23015 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23016 {
23017 int width;
23018 Lisp_Object tem;
23019
23020 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23021 FRAME_CURSOR_WIDTH (f) = width;
23022
23023 /* By default, set up the blink-off state depending on the on-state. */
23024
23025 tem = Fassoc (arg, Vblink_cursor_alist);
23026 if (!NILP (tem))
23027 {
23028 FRAME_BLINK_OFF_CURSOR (f)
23029 = get_specified_cursor_type (XCDR (tem), &width);
23030 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23031 }
23032 else
23033 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23034 }
23035
23036
23037 /* Return the cursor we want to be displayed in window W. Return
23038 width of bar/hbar cursor through WIDTH arg. Return with
23039 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23040 (i.e. if the `system caret' should track this cursor).
23041
23042 In a mini-buffer window, we want the cursor only to appear if we
23043 are reading input from this window. For the selected window, we
23044 want the cursor type given by the frame parameter or buffer local
23045 setting of cursor-type. If explicitly marked off, draw no cursor.
23046 In all other cases, we want a hollow box cursor. */
23047
23048 static enum text_cursor_kinds
23049 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23050 int *active_cursor)
23051 {
23052 struct frame *f = XFRAME (w->frame);
23053 struct buffer *b = XBUFFER (w->buffer);
23054 int cursor_type = DEFAULT_CURSOR;
23055 Lisp_Object alt_cursor;
23056 int non_selected = 0;
23057
23058 *active_cursor = 1;
23059
23060 /* Echo area */
23061 if (cursor_in_echo_area
23062 && FRAME_HAS_MINIBUF_P (f)
23063 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23064 {
23065 if (w == XWINDOW (echo_area_window))
23066 {
23067 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
23068 {
23069 *width = FRAME_CURSOR_WIDTH (f);
23070 return FRAME_DESIRED_CURSOR (f);
23071 }
23072 else
23073 return get_specified_cursor_type (b->cursor_type, width);
23074 }
23075
23076 *active_cursor = 0;
23077 non_selected = 1;
23078 }
23079
23080 /* Detect a nonselected window or nonselected frame. */
23081 else if (w != XWINDOW (f->selected_window)
23082 #ifdef HAVE_WINDOW_SYSTEM
23083 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
23084 #endif
23085 )
23086 {
23087 *active_cursor = 0;
23088
23089 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23090 return NO_CURSOR;
23091
23092 non_selected = 1;
23093 }
23094
23095 /* Never display a cursor in a window in which cursor-type is nil. */
23096 if (NILP (b->cursor_type))
23097 return NO_CURSOR;
23098
23099 /* Get the normal cursor type for this window. */
23100 if (EQ (b->cursor_type, Qt))
23101 {
23102 cursor_type = FRAME_DESIRED_CURSOR (f);
23103 *width = FRAME_CURSOR_WIDTH (f);
23104 }
23105 else
23106 cursor_type = get_specified_cursor_type (b->cursor_type, width);
23107
23108 /* Use cursor-in-non-selected-windows instead
23109 for non-selected window or frame. */
23110 if (non_selected)
23111 {
23112 alt_cursor = b->cursor_in_non_selected_windows;
23113 if (!EQ (Qt, alt_cursor))
23114 return get_specified_cursor_type (alt_cursor, width);
23115 /* t means modify the normal cursor type. */
23116 if (cursor_type == FILLED_BOX_CURSOR)
23117 cursor_type = HOLLOW_BOX_CURSOR;
23118 else if (cursor_type == BAR_CURSOR && *width > 1)
23119 --*width;
23120 return cursor_type;
23121 }
23122
23123 /* Use normal cursor if not blinked off. */
23124 if (!w->cursor_off_p)
23125 {
23126 #ifdef HAVE_WINDOW_SYSTEM
23127 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23128 {
23129 if (cursor_type == FILLED_BOX_CURSOR)
23130 {
23131 /* Using a block cursor on large images can be very annoying.
23132 So use a hollow cursor for "large" images.
23133 If image is not transparent (no mask), also use hollow cursor. */
23134 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23135 if (img != NULL && IMAGEP (img->spec))
23136 {
23137 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23138 where N = size of default frame font size.
23139 This should cover most of the "tiny" icons people may use. */
23140 if (!img->mask
23141 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23142 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23143 cursor_type = HOLLOW_BOX_CURSOR;
23144 }
23145 }
23146 else if (cursor_type != NO_CURSOR)
23147 {
23148 /* Display current only supports BOX and HOLLOW cursors for images.
23149 So for now, unconditionally use a HOLLOW cursor when cursor is
23150 not a solid box cursor. */
23151 cursor_type = HOLLOW_BOX_CURSOR;
23152 }
23153 }
23154 #endif
23155 return cursor_type;
23156 }
23157
23158 /* Cursor is blinked off, so determine how to "toggle" it. */
23159
23160 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23161 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
23162 return get_specified_cursor_type (XCDR (alt_cursor), width);
23163
23164 /* Then see if frame has specified a specific blink off cursor type. */
23165 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23166 {
23167 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23168 return FRAME_BLINK_OFF_CURSOR (f);
23169 }
23170
23171 #if 0
23172 /* Some people liked having a permanently visible blinking cursor,
23173 while others had very strong opinions against it. So it was
23174 decided to remove it. KFS 2003-09-03 */
23175
23176 /* Finally perform built-in cursor blinking:
23177 filled box <-> hollow box
23178 wide [h]bar <-> narrow [h]bar
23179 narrow [h]bar <-> no cursor
23180 other type <-> no cursor */
23181
23182 if (cursor_type == FILLED_BOX_CURSOR)
23183 return HOLLOW_BOX_CURSOR;
23184
23185 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23186 {
23187 *width = 1;
23188 return cursor_type;
23189 }
23190 #endif
23191
23192 return NO_CURSOR;
23193 }
23194
23195
23196 #ifdef HAVE_WINDOW_SYSTEM
23197
23198 /* Notice when the text cursor of window W has been completely
23199 overwritten by a drawing operation that outputs glyphs in AREA
23200 starting at X0 and ending at X1 in the line starting at Y0 and
23201 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23202 the rest of the line after X0 has been written. Y coordinates
23203 are window-relative. */
23204
23205 static void
23206 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23207 int x0, int x1, int y0, int y1)
23208 {
23209 int cx0, cx1, cy0, cy1;
23210 struct glyph_row *row;
23211
23212 if (!w->phys_cursor_on_p)
23213 return;
23214 if (area != TEXT_AREA)
23215 return;
23216
23217 if (w->phys_cursor.vpos < 0
23218 || w->phys_cursor.vpos >= w->current_matrix->nrows
23219 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23220 !(row->enabled_p && row->displays_text_p)))
23221 return;
23222
23223 if (row->cursor_in_fringe_p)
23224 {
23225 row->cursor_in_fringe_p = 0;
23226 draw_fringe_bitmap (w, row, row->reversed_p);
23227 w->phys_cursor_on_p = 0;
23228 return;
23229 }
23230
23231 cx0 = w->phys_cursor.x;
23232 cx1 = cx0 + w->phys_cursor_width;
23233 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23234 return;
23235
23236 /* The cursor image will be completely removed from the
23237 screen if the output area intersects the cursor area in
23238 y-direction. When we draw in [y0 y1[, and some part of
23239 the cursor is at y < y0, that part must have been drawn
23240 before. When scrolling, the cursor is erased before
23241 actually scrolling, so we don't come here. When not
23242 scrolling, the rows above the old cursor row must have
23243 changed, and in this case these rows must have written
23244 over the cursor image.
23245
23246 Likewise if part of the cursor is below y1, with the
23247 exception of the cursor being in the first blank row at
23248 the buffer and window end because update_text_area
23249 doesn't draw that row. (Except when it does, but
23250 that's handled in update_text_area.) */
23251
23252 cy0 = w->phys_cursor.y;
23253 cy1 = cy0 + w->phys_cursor_height;
23254 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23255 return;
23256
23257 w->phys_cursor_on_p = 0;
23258 }
23259
23260 #endif /* HAVE_WINDOW_SYSTEM */
23261
23262 \f
23263 /************************************************************************
23264 Mouse Face
23265 ************************************************************************/
23266
23267 #ifdef HAVE_WINDOW_SYSTEM
23268
23269 /* EXPORT for RIF:
23270 Fix the display of area AREA of overlapping row ROW in window W
23271 with respect to the overlapping part OVERLAPS. */
23272
23273 void
23274 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
23275 enum glyph_row_area area, int overlaps)
23276 {
23277 int i, x;
23278
23279 BLOCK_INPUT;
23280
23281 x = 0;
23282 for (i = 0; i < row->used[area];)
23283 {
23284 if (row->glyphs[area][i].overlaps_vertically_p)
23285 {
23286 int start = i, start_x = x;
23287
23288 do
23289 {
23290 x += row->glyphs[area][i].pixel_width;
23291 ++i;
23292 }
23293 while (i < row->used[area]
23294 && row->glyphs[area][i].overlaps_vertically_p);
23295
23296 draw_glyphs (w, start_x, row, area,
23297 start, i,
23298 DRAW_NORMAL_TEXT, overlaps);
23299 }
23300 else
23301 {
23302 x += row->glyphs[area][i].pixel_width;
23303 ++i;
23304 }
23305 }
23306
23307 UNBLOCK_INPUT;
23308 }
23309
23310
23311 /* EXPORT:
23312 Draw the cursor glyph of window W in glyph row ROW. See the
23313 comment of draw_glyphs for the meaning of HL. */
23314
23315 void
23316 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23317 enum draw_glyphs_face hl)
23318 {
23319 /* If cursor hpos is out of bounds, don't draw garbage. This can
23320 happen in mini-buffer windows when switching between echo area
23321 glyphs and mini-buffer. */
23322 if ((row->reversed_p
23323 ? (w->phys_cursor.hpos >= 0)
23324 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23325 {
23326 int on_p = w->phys_cursor_on_p;
23327 int x1;
23328 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23329 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23330 hl, 0);
23331 w->phys_cursor_on_p = on_p;
23332
23333 if (hl == DRAW_CURSOR)
23334 w->phys_cursor_width = x1 - w->phys_cursor.x;
23335 /* When we erase the cursor, and ROW is overlapped by other
23336 rows, make sure that these overlapping parts of other rows
23337 are redrawn. */
23338 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23339 {
23340 w->phys_cursor_width = x1 - w->phys_cursor.x;
23341
23342 if (row > w->current_matrix->rows
23343 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23344 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23345 OVERLAPS_ERASED_CURSOR);
23346
23347 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23348 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23349 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23350 OVERLAPS_ERASED_CURSOR);
23351 }
23352 }
23353 }
23354
23355
23356 /* EXPORT:
23357 Erase the image of a cursor of window W from the screen. */
23358
23359 void
23360 erase_phys_cursor (struct window *w)
23361 {
23362 struct frame *f = XFRAME (w->frame);
23363 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23364 int hpos = w->phys_cursor.hpos;
23365 int vpos = w->phys_cursor.vpos;
23366 int mouse_face_here_p = 0;
23367 struct glyph_matrix *active_glyphs = w->current_matrix;
23368 struct glyph_row *cursor_row;
23369 struct glyph *cursor_glyph;
23370 enum draw_glyphs_face hl;
23371
23372 /* No cursor displayed or row invalidated => nothing to do on the
23373 screen. */
23374 if (w->phys_cursor_type == NO_CURSOR)
23375 goto mark_cursor_off;
23376
23377 /* VPOS >= active_glyphs->nrows means that window has been resized.
23378 Don't bother to erase the cursor. */
23379 if (vpos >= active_glyphs->nrows)
23380 goto mark_cursor_off;
23381
23382 /* If row containing cursor is marked invalid, there is nothing we
23383 can do. */
23384 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23385 if (!cursor_row->enabled_p)
23386 goto mark_cursor_off;
23387
23388 /* If line spacing is > 0, old cursor may only be partially visible in
23389 window after split-window. So adjust visible height. */
23390 cursor_row->visible_height = min (cursor_row->visible_height,
23391 window_text_bottom_y (w) - cursor_row->y);
23392
23393 /* If row is completely invisible, don't attempt to delete a cursor which
23394 isn't there. This can happen if cursor is at top of a window, and
23395 we switch to a buffer with a header line in that window. */
23396 if (cursor_row->visible_height <= 0)
23397 goto mark_cursor_off;
23398
23399 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23400 if (cursor_row->cursor_in_fringe_p)
23401 {
23402 cursor_row->cursor_in_fringe_p = 0;
23403 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23404 goto mark_cursor_off;
23405 }
23406
23407 /* This can happen when the new row is shorter than the old one.
23408 In this case, either draw_glyphs or clear_end_of_line
23409 should have cleared the cursor. Note that we wouldn't be
23410 able to erase the cursor in this case because we don't have a
23411 cursor glyph at hand. */
23412 if ((cursor_row->reversed_p
23413 ? (w->phys_cursor.hpos < 0)
23414 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23415 goto mark_cursor_off;
23416
23417 /* If the cursor is in the mouse face area, redisplay that when
23418 we clear the cursor. */
23419 if (! NILP (dpyinfo->mouse_face_window)
23420 && coords_in_mouse_face_p (w, hpos, vpos)
23421 /* Don't redraw the cursor's spot in mouse face if it is at the
23422 end of a line (on a newline). The cursor appears there, but
23423 mouse highlighting does not. */
23424 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
23425 mouse_face_here_p = 1;
23426
23427 /* Maybe clear the display under the cursor. */
23428 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23429 {
23430 int x, y, left_x;
23431 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23432 int width;
23433
23434 cursor_glyph = get_phys_cursor_glyph (w);
23435 if (cursor_glyph == NULL)
23436 goto mark_cursor_off;
23437
23438 width = cursor_glyph->pixel_width;
23439 left_x = window_box_left_offset (w, TEXT_AREA);
23440 x = w->phys_cursor.x;
23441 if (x < left_x)
23442 width -= left_x - x;
23443 width = min (width, window_box_width (w, TEXT_AREA) - x);
23444 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23445 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23446
23447 if (width > 0)
23448 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23449 }
23450
23451 /* Erase the cursor by redrawing the character underneath it. */
23452 if (mouse_face_here_p)
23453 hl = DRAW_MOUSE_FACE;
23454 else
23455 hl = DRAW_NORMAL_TEXT;
23456 draw_phys_cursor_glyph (w, cursor_row, hl);
23457
23458 mark_cursor_off:
23459 w->phys_cursor_on_p = 0;
23460 w->phys_cursor_type = NO_CURSOR;
23461 }
23462
23463
23464 /* EXPORT:
23465 Display or clear cursor of window W. If ON is zero, clear the
23466 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23467 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23468
23469 void
23470 display_and_set_cursor (struct window *w, int on,
23471 int hpos, int vpos, int x, int y)
23472 {
23473 struct frame *f = XFRAME (w->frame);
23474 int new_cursor_type;
23475 int new_cursor_width;
23476 int active_cursor;
23477 struct glyph_row *glyph_row;
23478 struct glyph *glyph;
23479
23480 /* This is pointless on invisible frames, and dangerous on garbaged
23481 windows and frames; in the latter case, the frame or window may
23482 be in the midst of changing its size, and x and y may be off the
23483 window. */
23484 if (! FRAME_VISIBLE_P (f)
23485 || FRAME_GARBAGED_P (f)
23486 || vpos >= w->current_matrix->nrows
23487 || hpos >= w->current_matrix->matrix_w)
23488 return;
23489
23490 /* If cursor is off and we want it off, return quickly. */
23491 if (!on && !w->phys_cursor_on_p)
23492 return;
23493
23494 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23495 /* If cursor row is not enabled, we don't really know where to
23496 display the cursor. */
23497 if (!glyph_row->enabled_p)
23498 {
23499 w->phys_cursor_on_p = 0;
23500 return;
23501 }
23502
23503 glyph = NULL;
23504 if (!glyph_row->exact_window_width_line_p
23505 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
23506 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23507
23508 xassert (interrupt_input_blocked);
23509
23510 /* Set new_cursor_type to the cursor we want to be displayed. */
23511 new_cursor_type = get_window_cursor_type (w, glyph,
23512 &new_cursor_width, &active_cursor);
23513
23514 /* If cursor is currently being shown and we don't want it to be or
23515 it is in the wrong place, or the cursor type is not what we want,
23516 erase it. */
23517 if (w->phys_cursor_on_p
23518 && (!on
23519 || w->phys_cursor.x != x
23520 || w->phys_cursor.y != y
23521 || new_cursor_type != w->phys_cursor_type
23522 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23523 && new_cursor_width != w->phys_cursor_width)))
23524 erase_phys_cursor (w);
23525
23526 /* Don't check phys_cursor_on_p here because that flag is only set
23527 to zero in some cases where we know that the cursor has been
23528 completely erased, to avoid the extra work of erasing the cursor
23529 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23530 still not be visible, or it has only been partly erased. */
23531 if (on)
23532 {
23533 w->phys_cursor_ascent = glyph_row->ascent;
23534 w->phys_cursor_height = glyph_row->height;
23535
23536 /* Set phys_cursor_.* before x_draw_.* is called because some
23537 of them may need the information. */
23538 w->phys_cursor.x = x;
23539 w->phys_cursor.y = glyph_row->y;
23540 w->phys_cursor.hpos = hpos;
23541 w->phys_cursor.vpos = vpos;
23542 }
23543
23544 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23545 new_cursor_type, new_cursor_width,
23546 on, active_cursor);
23547 }
23548
23549
23550 /* Switch the display of W's cursor on or off, according to the value
23551 of ON. */
23552
23553 void
23554 update_window_cursor (struct window *w, int on)
23555 {
23556 /* Don't update cursor in windows whose frame is in the process
23557 of being deleted. */
23558 if (w->current_matrix)
23559 {
23560 BLOCK_INPUT;
23561 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23562 w->phys_cursor.x, w->phys_cursor.y);
23563 UNBLOCK_INPUT;
23564 }
23565 }
23566
23567
23568 /* Call update_window_cursor with parameter ON_P on all leaf windows
23569 in the window tree rooted at W. */
23570
23571 static void
23572 update_cursor_in_window_tree (struct window *w, int on_p)
23573 {
23574 while (w)
23575 {
23576 if (!NILP (w->hchild))
23577 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23578 else if (!NILP (w->vchild))
23579 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23580 else
23581 update_window_cursor (w, on_p);
23582
23583 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23584 }
23585 }
23586
23587
23588 /* EXPORT:
23589 Display the cursor on window W, or clear it, according to ON_P.
23590 Don't change the cursor's position. */
23591
23592 void
23593 x_update_cursor (struct frame *f, int on_p)
23594 {
23595 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23596 }
23597
23598
23599 /* EXPORT:
23600 Clear the cursor of window W to background color, and mark the
23601 cursor as not shown. This is used when the text where the cursor
23602 is about to be rewritten. */
23603
23604 void
23605 x_clear_cursor (struct window *w)
23606 {
23607 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
23608 update_window_cursor (w, 0);
23609 }
23610
23611
23612 /* EXPORT:
23613 Display the active region described by mouse_face_* according to DRAW. */
23614
23615 void
23616 show_mouse_face (Display_Info *dpyinfo, enum draw_glyphs_face draw)
23617 {
23618 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
23619 struct frame *f = XFRAME (WINDOW_FRAME (w));
23620
23621 if (/* If window is in the process of being destroyed, don't bother
23622 to do anything. */
23623 w->current_matrix != NULL
23624 /* Don't update mouse highlight if hidden */
23625 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
23626 /* Recognize when we are called to operate on rows that don't exist
23627 anymore. This can happen when a window is split. */
23628 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
23629 {
23630 int phys_cursor_on_p = w->phys_cursor_on_p;
23631 struct glyph_row *row, *first, *last;
23632
23633 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
23634 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
23635
23636 for (row = first; row <= last && row->enabled_p; ++row)
23637 {
23638 int start_hpos, end_hpos, start_x;
23639
23640 /* For all but the first row, the highlight starts at column 0. */
23641 if (row == first)
23642 {
23643 /* R2L rows have BEG and END in reversed order, but the
23644 screen drawing geometry is always left to right. So
23645 we need to mirror the beginning and end of the
23646 highlighted area in R2L rows. */
23647 if (!row->reversed_p)
23648 {
23649 start_hpos = dpyinfo->mouse_face_beg_col;
23650 start_x = dpyinfo->mouse_face_beg_x;
23651 }
23652 else if (row == last)
23653 {
23654 start_hpos = dpyinfo->mouse_face_end_col;
23655 start_x = dpyinfo->mouse_face_end_x;
23656 }
23657 else
23658 {
23659 start_hpos = 0;
23660 start_x = 0;
23661 }
23662 }
23663 else if (row->reversed_p && row == last)
23664 {
23665 start_hpos = dpyinfo->mouse_face_end_col;
23666 start_x = dpyinfo->mouse_face_end_x;
23667 }
23668 else
23669 {
23670 start_hpos = 0;
23671 start_x = 0;
23672 }
23673
23674 if (row == last)
23675 {
23676 if (!row->reversed_p)
23677 end_hpos = dpyinfo->mouse_face_end_col;
23678 else if (row == first)
23679 end_hpos = dpyinfo->mouse_face_beg_col;
23680 else
23681 {
23682 end_hpos = row->used[TEXT_AREA];
23683 if (draw == DRAW_NORMAL_TEXT)
23684 row->fill_line_p = 1; /* Clear to end of line */
23685 }
23686 }
23687 else if (row->reversed_p && row == first)
23688 end_hpos = dpyinfo->mouse_face_beg_col;
23689 else
23690 {
23691 end_hpos = row->used[TEXT_AREA];
23692 if (draw == DRAW_NORMAL_TEXT)
23693 row->fill_line_p = 1; /* Clear to end of line */
23694 }
23695
23696 if (end_hpos > start_hpos)
23697 {
23698 draw_glyphs (w, start_x, row, TEXT_AREA,
23699 start_hpos, end_hpos,
23700 draw, 0);
23701
23702 row->mouse_face_p
23703 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
23704 }
23705 }
23706
23707 /* When we've written over the cursor, arrange for it to
23708 be displayed again. */
23709 if (phys_cursor_on_p && !w->phys_cursor_on_p)
23710 {
23711 BLOCK_INPUT;
23712 display_and_set_cursor (w, 1,
23713 w->phys_cursor.hpos, w->phys_cursor.vpos,
23714 w->phys_cursor.x, w->phys_cursor.y);
23715 UNBLOCK_INPUT;
23716 }
23717 }
23718
23719 /* Change the mouse cursor. */
23720 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
23721 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
23722 else if (draw == DRAW_MOUSE_FACE)
23723 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
23724 else
23725 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
23726 }
23727
23728 /* EXPORT:
23729 Clear out the mouse-highlighted active region.
23730 Redraw it un-highlighted first. Value is non-zero if mouse
23731 face was actually drawn unhighlighted. */
23732
23733 int
23734 clear_mouse_face (Display_Info *dpyinfo)
23735 {
23736 int cleared = 0;
23737
23738 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
23739 {
23740 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
23741 cleared = 1;
23742 }
23743
23744 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23745 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23746 dpyinfo->mouse_face_window = Qnil;
23747 dpyinfo->mouse_face_overlay = Qnil;
23748 return cleared;
23749 }
23750
23751 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
23752 within the mouse face on that window. */
23753 static int
23754 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
23755 {
23756 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23757
23758 /* Quickly resolve the easy cases. */
23759 if (!(WINDOWP (dpyinfo->mouse_face_window)
23760 && XWINDOW (dpyinfo->mouse_face_window) == w))
23761 return 0;
23762 if (vpos < dpyinfo->mouse_face_beg_row
23763 || vpos > dpyinfo->mouse_face_end_row)
23764 return 0;
23765 if (vpos > dpyinfo->mouse_face_beg_row
23766 && vpos < dpyinfo->mouse_face_end_row)
23767 return 1;
23768
23769 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
23770 {
23771 if (dpyinfo->mouse_face_beg_row == dpyinfo->mouse_face_end_row)
23772 {
23773 if (dpyinfo->mouse_face_beg_col <= hpos && hpos < dpyinfo->mouse_face_end_col)
23774 return 1;
23775 }
23776 else if ((vpos == dpyinfo->mouse_face_beg_row
23777 && hpos >= dpyinfo->mouse_face_beg_col)
23778 || (vpos == dpyinfo->mouse_face_end_row
23779 && hpos < dpyinfo->mouse_face_end_col))
23780 return 1;
23781 }
23782 else
23783 {
23784 if (dpyinfo->mouse_face_beg_row == dpyinfo->mouse_face_end_row)
23785 {
23786 if (dpyinfo->mouse_face_end_col < hpos && hpos <= dpyinfo->mouse_face_beg_col)
23787 return 1;
23788 }
23789 else if ((vpos == dpyinfo->mouse_face_beg_row
23790 && hpos <= dpyinfo->mouse_face_beg_col)
23791 || (vpos == dpyinfo->mouse_face_end_row
23792 && hpos > dpyinfo->mouse_face_end_col))
23793 return 1;
23794 }
23795 return 0;
23796 }
23797
23798
23799 /* EXPORT:
23800 Non-zero if physical cursor of window W is within mouse face. */
23801
23802 int
23803 cursor_in_mouse_face_p (struct window *w)
23804 {
23805 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
23806 }
23807
23808
23809 \f
23810 /* Find the glyph rows START_ROW and END_ROW of window W that display
23811 characters between buffer positions START_CHARPOS and END_CHARPOS
23812 (excluding END_CHARPOS). This is similar to row_containing_pos,
23813 but is more accurate when bidi reordering makes buffer positions
23814 change non-linearly with glyph rows. */
23815 static void
23816 rows_from_pos_range (struct window *w,
23817 EMACS_INT start_charpos, EMACS_INT end_charpos,
23818 struct glyph_row **start, struct glyph_row **end)
23819 {
23820 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23821 int last_y = window_text_bottom_y (w);
23822 struct glyph_row *row;
23823
23824 *start = NULL;
23825 *end = NULL;
23826
23827 while (!first->enabled_p
23828 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
23829 first++;
23830
23831 /* Find the START row. */
23832 for (row = first;
23833 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
23834 row++)
23835 {
23836 /* A row can potentially be the START row if the range of the
23837 characters it displays intersects the range
23838 [START_CHARPOS..END_CHARPOS). */
23839 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
23840 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
23841 /* See the commentary in row_containing_pos, for the
23842 explanation of the complicated way to check whether
23843 some position is beyond the end of the characters
23844 displayed by a row. */
23845 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
23846 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
23847 && !row->ends_at_zv_p
23848 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
23849 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
23850 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
23851 && !row->ends_at_zv_p
23852 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
23853 {
23854 /* Found a candidate row. Now make sure at least one of the
23855 glyphs it displays has a charpos from the range
23856 [START_CHARPOS..END_CHARPOS).
23857
23858 This is not obvious because bidi reordering could make
23859 buffer positions of a row be 1,2,3,102,101,100, and if we
23860 want to highlight characters in [50..60), we don't want
23861 this row, even though [50..60) does intersect [1..103),
23862 the range of character positions given by the row's start
23863 and end positions. */
23864 struct glyph *g = row->glyphs[TEXT_AREA];
23865 struct glyph *e = g + row->used[TEXT_AREA];
23866
23867 while (g < e)
23868 {
23869 if (BUFFERP (g->object)
23870 && start_charpos <= g->charpos && g->charpos < end_charpos)
23871 *start = row;
23872 g++;
23873 }
23874 if (*start)
23875 break;
23876 }
23877 }
23878
23879 /* Find the END row. */
23880 if (!*start
23881 /* If the last row is partially visible, start looking for END
23882 from that row, instead of starting from FIRST. */
23883 && !(row->enabled_p
23884 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
23885 row = first;
23886 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
23887 {
23888 struct glyph_row *next = row + 1;
23889
23890 if (!next->enabled_p
23891 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
23892 /* The first row >= START whose range of displayed characters
23893 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
23894 is the row END + 1. */
23895 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
23896 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
23897 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
23898 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
23899 && !next->ends_at_zv_p
23900 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
23901 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
23902 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
23903 && !next->ends_at_zv_p
23904 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
23905 {
23906 *end = row;
23907 break;
23908 }
23909 else
23910 {
23911 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
23912 but none of the characters it displays are in the range, it is
23913 also END + 1. */
23914 struct glyph *g = next->glyphs[TEXT_AREA];
23915 struct glyph *e = g + next->used[TEXT_AREA];
23916
23917 while (g < e)
23918 {
23919 if (BUFFERP (g->object)
23920 && start_charpos <= g->charpos && g->charpos < end_charpos)
23921 break;
23922 g++;
23923 }
23924 if (g == e)
23925 {
23926 *end = row;
23927 break;
23928 }
23929 }
23930 }
23931 }
23932
23933 /* This function sets the mouse_face_* elements of DPYINFO, assuming
23934 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
23935 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
23936 for the overlay or run of text properties specifying the mouse
23937 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
23938 before-string and after-string that must also be highlighted.
23939 DISPLAY_STRING, if non-nil, is a display string that may cover some
23940 or all of the highlighted text. */
23941
23942 static void
23943 mouse_face_from_buffer_pos (Lisp_Object window,
23944 Display_Info *dpyinfo,
23945 EMACS_INT mouse_charpos,
23946 EMACS_INT start_charpos,
23947 EMACS_INT end_charpos,
23948 Lisp_Object before_string,
23949 Lisp_Object after_string,
23950 Lisp_Object display_string)
23951 {
23952 struct window *w = XWINDOW (window);
23953 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23954 struct glyph_row *r1, *r2;
23955 struct glyph *glyph, *end;
23956 EMACS_INT ignore, pos;
23957 int x;
23958
23959 xassert (NILP (display_string) || STRINGP (display_string));
23960 xassert (NILP (before_string) || STRINGP (before_string));
23961 xassert (NILP (after_string) || STRINGP (after_string));
23962
23963 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
23964 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
23965 if (r1 == NULL)
23966 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23967 /* If the before-string or display-string contains newlines,
23968 rows_from_pos_range skips to its last row. Move back. */
23969 if (!NILP (before_string) || !NILP (display_string))
23970 {
23971 struct glyph_row *prev;
23972 while ((prev = r1 - 1, prev >= first)
23973 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
23974 && prev->used[TEXT_AREA] > 0)
23975 {
23976 struct glyph *beg = prev->glyphs[TEXT_AREA];
23977 glyph = beg + prev->used[TEXT_AREA];
23978 while (--glyph >= beg && INTEGERP (glyph->object));
23979 if (glyph < beg
23980 || !(EQ (glyph->object, before_string)
23981 || EQ (glyph->object, display_string)))
23982 break;
23983 r1 = prev;
23984 }
23985 }
23986 if (r2 == NULL)
23987 {
23988 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23989 dpyinfo->mouse_face_past_end = 1;
23990 }
23991 else if (!NILP (after_string))
23992 {
23993 /* If the after-string has newlines, advance to its last row. */
23994 struct glyph_row *next;
23995 struct glyph_row *last
23996 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23997
23998 for (next = r2 + 1;
23999 next <= last
24000 && next->used[TEXT_AREA] > 0
24001 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
24002 ++next)
24003 r2 = next;
24004 }
24005 /* The rest of the display engine assumes that mouse_face_beg_row is
24006 either above below mouse_face_end_row or identical to it. But
24007 with bidi-reordered continued lines, the row for START_CHARPOS
24008 could be below the row for END_CHARPOS. If so, swap the rows and
24009 store them in correct order. */
24010 if (r1->y > r2->y)
24011 {
24012 struct glyph_row *tem = r2;
24013
24014 r2 = r1;
24015 r1 = tem;
24016 }
24017
24018 dpyinfo->mouse_face_beg_y = r1->y;
24019 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
24020 dpyinfo->mouse_face_end_y = r2->y;
24021 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
24022
24023 /* For a bidi-reordered row, the positions of BEFORE_STRING,
24024 AFTER_STRING, DISPLAY_STRING, START_CHARPOS, and END_CHARPOS
24025 could be anywhere in the row and in any order. The strategy
24026 below is to find the leftmost and the rightmost glyph that
24027 belongs to either of these 3 strings, or whose position is
24028 between START_CHARPOS and END_CHARPOS, and highlight all the
24029 glyphs between those two. This may cover more than just the text
24030 between START_CHARPOS and END_CHARPOS if the range of characters
24031 strides the bidi level boundary, e.g. if the beginning is in R2L
24032 text while the end is in L2R text or vice versa. */
24033 if (!r1->reversed_p)
24034 {
24035 /* This row is in a left to right paragraph. Scan it left to
24036 right. */
24037 glyph = r1->glyphs[TEXT_AREA];
24038 end = glyph + r1->used[TEXT_AREA];
24039 x = r1->x;
24040
24041 /* Skip truncation glyphs at the start of the glyph row. */
24042 if (r1->displays_text_p)
24043 for (; glyph < end
24044 && INTEGERP (glyph->object)
24045 && glyph->charpos < 0;
24046 ++glyph)
24047 x += glyph->pixel_width;
24048
24049 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24050 or DISPLAY_STRING, and the first glyph from buffer whose
24051 position is between START_CHARPOS and END_CHARPOS. */
24052 for (; glyph < end
24053 && !INTEGERP (glyph->object)
24054 && !EQ (glyph->object, display_string)
24055 && !(BUFFERP (glyph->object)
24056 && (glyph->charpos >= start_charpos
24057 && glyph->charpos < end_charpos));
24058 ++glyph)
24059 {
24060 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24061 are present at buffer positions between START_CHARPOS and
24062 END_CHARPOS, or if they come from an overlay. */
24063 if (EQ (glyph->object, before_string))
24064 {
24065 pos = string_buffer_position (w, before_string,
24066 start_charpos);
24067 /* If pos == 0, it means before_string came from an
24068 overlay, not from a buffer position. */
24069 if (!pos || pos >= start_charpos && pos < end_charpos)
24070 break;
24071 }
24072 else if (EQ (glyph->object, after_string))
24073 {
24074 pos = string_buffer_position (w, after_string, end_charpos);
24075 if (!pos || pos >= start_charpos && pos < end_charpos)
24076 break;
24077 }
24078 x += glyph->pixel_width;
24079 }
24080 dpyinfo->mouse_face_beg_x = x;
24081 dpyinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24082 }
24083 else
24084 {
24085 /* This row is in a right to left paragraph. Scan it right to
24086 left. */
24087 struct glyph *g;
24088
24089 end = r1->glyphs[TEXT_AREA] - 1;
24090 glyph = end + r1->used[TEXT_AREA];
24091
24092 /* Skip truncation glyphs at the start of the glyph row. */
24093 if (r1->displays_text_p)
24094 for (; glyph > end
24095 && INTEGERP (glyph->object)
24096 && glyph->charpos < 0;
24097 --glyph)
24098 ;
24099
24100 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24101 or DISPLAY_STRING, and the first glyph from buffer whose
24102 position is between START_CHARPOS and END_CHARPOS. */
24103 for (; glyph > end
24104 && !INTEGERP (glyph->object)
24105 && !EQ (glyph->object, display_string)
24106 && !(BUFFERP (glyph->object)
24107 && (glyph->charpos >= start_charpos
24108 && glyph->charpos < end_charpos));
24109 --glyph)
24110 {
24111 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24112 are present at buffer positions between START_CHARPOS and
24113 END_CHARPOS, or if they come from an overlay. */
24114 if (EQ (glyph->object, before_string))
24115 {
24116 pos = string_buffer_position (w, before_string, start_charpos);
24117 /* If pos == 0, it means before_string came from an
24118 overlay, not from a buffer position. */
24119 if (!pos || pos >= start_charpos && pos < end_charpos)
24120 break;
24121 }
24122 else if (EQ (glyph->object, after_string))
24123 {
24124 pos = string_buffer_position (w, after_string, end_charpos);
24125 if (!pos || pos >= start_charpos && pos < end_charpos)
24126 break;
24127 }
24128 }
24129
24130 glyph++; /* first glyph to the right of the highlighted area */
24131 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
24132 x += g->pixel_width;
24133 dpyinfo->mouse_face_beg_x = x;
24134 dpyinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24135 }
24136
24137 /* If the highlight ends in a different row, compute GLYPH and END
24138 for the end row. Otherwise, reuse the values computed above for
24139 the row where the highlight begins. */
24140 if (r2 != r1)
24141 {
24142 if (!r2->reversed_p)
24143 {
24144 glyph = r2->glyphs[TEXT_AREA];
24145 end = glyph + r2->used[TEXT_AREA];
24146 x = r2->x;
24147 }
24148 else
24149 {
24150 end = r2->glyphs[TEXT_AREA] - 1;
24151 glyph = end + r2->used[TEXT_AREA];
24152 }
24153 }
24154
24155 if (!r2->reversed_p)
24156 {
24157 /* Skip truncation and continuation glyphs near the end of the
24158 row, and also blanks and stretch glyphs inserted by
24159 extend_face_to_end_of_line. */
24160 while (end > glyph
24161 && INTEGERP ((end - 1)->object)
24162 && (end - 1)->charpos <= 0)
24163 --end;
24164 /* Scan the rest of the glyph row from the end, looking for the
24165 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24166 DISPLAY_STRING, or whose position is between START_CHARPOS
24167 and END_CHARPOS */
24168 for (--end;
24169 end > glyph
24170 && !INTEGERP (end->object)
24171 && !EQ (end->object, display_string)
24172 && !(BUFFERP (end->object)
24173 && (end->charpos >= start_charpos
24174 && end->charpos < end_charpos));
24175 --end)
24176 {
24177 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24178 are present at buffer positions between START_CHARPOS and
24179 END_CHARPOS, or if they come from an overlay. */
24180 if (EQ (end->object, before_string))
24181 {
24182 pos = string_buffer_position (w, before_string, start_charpos);
24183 if (!pos || pos >= start_charpos && pos < end_charpos)
24184 break;
24185 }
24186 else if (EQ (end->object, after_string))
24187 {
24188 pos = string_buffer_position (w, after_string, end_charpos);
24189 if (!pos || pos >= start_charpos && pos < end_charpos)
24190 break;
24191 }
24192 }
24193 /* Find the X coordinate of the last glyph to be highlighted. */
24194 for (; glyph <= end; ++glyph)
24195 x += glyph->pixel_width;
24196
24197 dpyinfo->mouse_face_end_x = x;
24198 dpyinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
24199 }
24200 else
24201 {
24202 /* Skip truncation and continuation glyphs near the end of the
24203 row, and also blanks and stretch glyphs inserted by
24204 extend_face_to_end_of_line. */
24205 x = r2->x;
24206 end++;
24207 while (end < glyph
24208 && INTEGERP (end->object)
24209 && end->charpos <= 0)
24210 {
24211 x += end->pixel_width;
24212 ++end;
24213 }
24214 /* Scan the rest of the glyph row from the end, looking for the
24215 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24216 DISPLAY_STRING, or whose position is between START_CHARPOS
24217 and END_CHARPOS */
24218 for ( ;
24219 end < glyph
24220 && !INTEGERP (end->object)
24221 && !EQ (end->object, display_string)
24222 && !(BUFFERP (end->object)
24223 && (end->charpos >= start_charpos
24224 && end->charpos < end_charpos));
24225 ++end)
24226 {
24227 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24228 are present at buffer positions between START_CHARPOS and
24229 END_CHARPOS, or if they come from an overlay. */
24230 if (EQ (end->object, before_string))
24231 {
24232 pos = string_buffer_position (w, before_string, start_charpos);
24233 if (!pos || pos >= start_charpos && pos < end_charpos)
24234 break;
24235 }
24236 else if (EQ (end->object, after_string))
24237 {
24238 pos = string_buffer_position (w, after_string, end_charpos);
24239 if (!pos || pos >= start_charpos && pos < end_charpos)
24240 break;
24241 }
24242 x += end->pixel_width;
24243 }
24244 dpyinfo->mouse_face_end_x = x;
24245 dpyinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
24246 }
24247
24248 dpyinfo->mouse_face_window = window;
24249 dpyinfo->mouse_face_face_id
24250 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
24251 mouse_charpos + 1,
24252 !dpyinfo->mouse_face_hidden, -1);
24253 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24254 }
24255
24256 /* The following function is not used anymore (replaced with
24257 mouse_face_from_string_pos), but I leave it here for the time
24258 being, in case someone would. */
24259
24260 #if 0 /* not used */
24261
24262 /* Find the position of the glyph for position POS in OBJECT in
24263 window W's current matrix, and return in *X, *Y the pixel
24264 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
24265
24266 RIGHT_P non-zero means return the position of the right edge of the
24267 glyph, RIGHT_P zero means return the left edge position.
24268
24269 If no glyph for POS exists in the matrix, return the position of
24270 the glyph with the next smaller position that is in the matrix, if
24271 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
24272 exists in the matrix, return the position of the glyph with the
24273 next larger position in OBJECT.
24274
24275 Value is non-zero if a glyph was found. */
24276
24277 static int
24278 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
24279 int *hpos, int *vpos, int *x, int *y, int right_p)
24280 {
24281 int yb = window_text_bottom_y (w);
24282 struct glyph_row *r;
24283 struct glyph *best_glyph = NULL;
24284 struct glyph_row *best_row = NULL;
24285 int best_x = 0;
24286
24287 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24288 r->enabled_p && r->y < yb;
24289 ++r)
24290 {
24291 struct glyph *g = r->glyphs[TEXT_AREA];
24292 struct glyph *e = g + r->used[TEXT_AREA];
24293 int gx;
24294
24295 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24296 if (EQ (g->object, object))
24297 {
24298 if (g->charpos == pos)
24299 {
24300 best_glyph = g;
24301 best_x = gx;
24302 best_row = r;
24303 goto found;
24304 }
24305 else if (best_glyph == NULL
24306 || ((eabs (g->charpos - pos)
24307 < eabs (best_glyph->charpos - pos))
24308 && (right_p
24309 ? g->charpos < pos
24310 : g->charpos > pos)))
24311 {
24312 best_glyph = g;
24313 best_x = gx;
24314 best_row = r;
24315 }
24316 }
24317 }
24318
24319 found:
24320
24321 if (best_glyph)
24322 {
24323 *x = best_x;
24324 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
24325
24326 if (right_p)
24327 {
24328 *x += best_glyph->pixel_width;
24329 ++*hpos;
24330 }
24331
24332 *y = best_row->y;
24333 *vpos = best_row - w->current_matrix->rows;
24334 }
24335
24336 return best_glyph != NULL;
24337 }
24338 #endif /* not used */
24339
24340 /* Find the positions of the first and the last glyphs in window W's
24341 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
24342 (assumed to be a string), and return in DPYINFO's mouse_face
24343 members the pixel and column/row coordinates of those glyphs. */
24344
24345 static void
24346 mouse_face_from_string_pos (struct window *w, Display_Info *dpyinfo,
24347 Lisp_Object object,
24348 EMACS_INT startpos, EMACS_INT endpos)
24349 {
24350 int yb = window_text_bottom_y (w);
24351 struct glyph_row *r;
24352 struct glyph *g, *e;
24353 int gx;
24354 int found = 0;
24355
24356 /* Find the glyph row with at least one position in the range
24357 [STARTPOS..ENDPOS], and the first glyph in that row whose
24358 position belongs to that range. */
24359 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24360 r->enabled_p && r->y < yb;
24361 ++r)
24362 {
24363 if (!r->reversed_p)
24364 {
24365 g = r->glyphs[TEXT_AREA];
24366 e = g + r->used[TEXT_AREA];
24367 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24368 if (EQ (g->object, object)
24369 && startpos <= g->charpos && g->charpos <= endpos)
24370 {
24371 dpyinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24372 dpyinfo->mouse_face_beg_y = r->y;
24373 dpyinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24374 dpyinfo->mouse_face_beg_x = gx;
24375 found = 1;
24376 break;
24377 }
24378 }
24379 else
24380 {
24381 struct glyph *g1;
24382
24383 e = r->glyphs[TEXT_AREA];
24384 g = e + r->used[TEXT_AREA];
24385 for ( ; g > e; --g)
24386 if (EQ ((g-1)->object, object)
24387 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
24388 {
24389 dpyinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24390 dpyinfo->mouse_face_beg_y = r->y;
24391 dpyinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24392 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
24393 gx += g1->pixel_width;
24394 dpyinfo->mouse_face_beg_x = gx;
24395 found = 1;
24396 break;
24397 }
24398 }
24399 if (found)
24400 break;
24401 }
24402
24403 if (!found)
24404 return;
24405
24406 /* Starting with the next row, look for the first row which does NOT
24407 include any glyphs whose positions are in the range. */
24408 for (++r; r->enabled_p && r->y < yb; ++r)
24409 {
24410 g = r->glyphs[TEXT_AREA];
24411 e = g + r->used[TEXT_AREA];
24412 found = 0;
24413 for ( ; g < e; ++g)
24414 if (EQ (g->object, object)
24415 && startpos <= g->charpos && g->charpos <= endpos)
24416 {
24417 found = 1;
24418 break;
24419 }
24420 if (!found)
24421 break;
24422 }
24423
24424 /* The highlighted region ends on the previous row. */
24425 r--;
24426
24427 /* Set the end row and its vertical pixel coordinate. */
24428 dpyinfo->mouse_face_end_row = r - w->current_matrix->rows;
24429 dpyinfo->mouse_face_end_y = r->y;
24430
24431 /* Compute and set the end column and the end column's horizontal
24432 pixel coordinate. */
24433 if (!r->reversed_p)
24434 {
24435 g = r->glyphs[TEXT_AREA];
24436 e = g + r->used[TEXT_AREA];
24437 for ( ; e > g; --e)
24438 if (EQ ((e-1)->object, object)
24439 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
24440 break;
24441 dpyinfo->mouse_face_end_col = e - g;
24442
24443 for (gx = r->x; g < e; ++g)
24444 gx += g->pixel_width;
24445 dpyinfo->mouse_face_end_x = gx;
24446 }
24447 else
24448 {
24449 e = r->glyphs[TEXT_AREA];
24450 g = e + r->used[TEXT_AREA];
24451 for (gx = r->x ; e < g; ++e)
24452 {
24453 if (EQ (e->object, object)
24454 && startpos <= e->charpos && e->charpos <= endpos)
24455 break;
24456 gx += e->pixel_width;
24457 }
24458 dpyinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
24459 dpyinfo->mouse_face_end_x = gx;
24460 }
24461 }
24462
24463 /* See if position X, Y is within a hot-spot of an image. */
24464
24465 static int
24466 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
24467 {
24468 if (!CONSP (hot_spot))
24469 return 0;
24470
24471 if (EQ (XCAR (hot_spot), Qrect))
24472 {
24473 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24474 Lisp_Object rect = XCDR (hot_spot);
24475 Lisp_Object tem;
24476 if (!CONSP (rect))
24477 return 0;
24478 if (!CONSP (XCAR (rect)))
24479 return 0;
24480 if (!CONSP (XCDR (rect)))
24481 return 0;
24482 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24483 return 0;
24484 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24485 return 0;
24486 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24487 return 0;
24488 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24489 return 0;
24490 return 1;
24491 }
24492 else if (EQ (XCAR (hot_spot), Qcircle))
24493 {
24494 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24495 Lisp_Object circ = XCDR (hot_spot);
24496 Lisp_Object lr, lx0, ly0;
24497 if (CONSP (circ)
24498 && CONSP (XCAR (circ))
24499 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24500 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24501 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24502 {
24503 double r = XFLOATINT (lr);
24504 double dx = XINT (lx0) - x;
24505 double dy = XINT (ly0) - y;
24506 return (dx * dx + dy * dy <= r * r);
24507 }
24508 }
24509 else if (EQ (XCAR (hot_spot), Qpoly))
24510 {
24511 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24512 if (VECTORP (XCDR (hot_spot)))
24513 {
24514 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24515 Lisp_Object *poly = v->contents;
24516 int n = v->size;
24517 int i;
24518 int inside = 0;
24519 Lisp_Object lx, ly;
24520 int x0, y0;
24521
24522 /* Need an even number of coordinates, and at least 3 edges. */
24523 if (n < 6 || n & 1)
24524 return 0;
24525
24526 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24527 If count is odd, we are inside polygon. Pixels on edges
24528 may or may not be included depending on actual geometry of the
24529 polygon. */
24530 if ((lx = poly[n-2], !INTEGERP (lx))
24531 || (ly = poly[n-1], !INTEGERP (lx)))
24532 return 0;
24533 x0 = XINT (lx), y0 = XINT (ly);
24534 for (i = 0; i < n; i += 2)
24535 {
24536 int x1 = x0, y1 = y0;
24537 if ((lx = poly[i], !INTEGERP (lx))
24538 || (ly = poly[i+1], !INTEGERP (ly)))
24539 return 0;
24540 x0 = XINT (lx), y0 = XINT (ly);
24541
24542 /* Does this segment cross the X line? */
24543 if (x0 >= x)
24544 {
24545 if (x1 >= x)
24546 continue;
24547 }
24548 else if (x1 < x)
24549 continue;
24550 if (y > y0 && y > y1)
24551 continue;
24552 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24553 inside = !inside;
24554 }
24555 return inside;
24556 }
24557 }
24558 return 0;
24559 }
24560
24561 Lisp_Object
24562 find_hot_spot (Lisp_Object map, int x, int y)
24563 {
24564 while (CONSP (map))
24565 {
24566 if (CONSP (XCAR (map))
24567 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
24568 return XCAR (map);
24569 map = XCDR (map);
24570 }
24571
24572 return Qnil;
24573 }
24574
24575 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
24576 3, 3, 0,
24577 doc: /* Lookup in image map MAP coordinates X and Y.
24578 An image map is an alist where each element has the format (AREA ID PLIST).
24579 An AREA is specified as either a rectangle, a circle, or a polygon:
24580 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
24581 pixel coordinates of the upper left and bottom right corners.
24582 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
24583 and the radius of the circle; r may be a float or integer.
24584 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
24585 vector describes one corner in the polygon.
24586 Returns the alist element for the first matching AREA in MAP. */)
24587 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
24588 {
24589 if (NILP (map))
24590 return Qnil;
24591
24592 CHECK_NUMBER (x);
24593 CHECK_NUMBER (y);
24594
24595 return find_hot_spot (map, XINT (x), XINT (y));
24596 }
24597
24598
24599 /* Display frame CURSOR, optionally using shape defined by POINTER. */
24600 static void
24601 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
24602 {
24603 /* Do not change cursor shape while dragging mouse. */
24604 if (!NILP (do_mouse_tracking))
24605 return;
24606
24607 if (!NILP (pointer))
24608 {
24609 if (EQ (pointer, Qarrow))
24610 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24611 else if (EQ (pointer, Qhand))
24612 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
24613 else if (EQ (pointer, Qtext))
24614 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24615 else if (EQ (pointer, intern ("hdrag")))
24616 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24617 #ifdef HAVE_X_WINDOWS
24618 else if (EQ (pointer, intern ("vdrag")))
24619 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
24620 #endif
24621 else if (EQ (pointer, intern ("hourglass")))
24622 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
24623 else if (EQ (pointer, Qmodeline))
24624 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
24625 else
24626 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24627 }
24628
24629 if (cursor != No_Cursor)
24630 FRAME_RIF (f)->define_frame_cursor (f, cursor);
24631 }
24632
24633 /* Take proper action when mouse has moved to the mode or header line
24634 or marginal area AREA of window W, x-position X and y-position Y.
24635 X is relative to the start of the text display area of W, so the
24636 width of bitmap areas and scroll bars must be subtracted to get a
24637 position relative to the start of the mode line. */
24638
24639 static void
24640 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
24641 enum window_part area)
24642 {
24643 struct window *w = XWINDOW (window);
24644 struct frame *f = XFRAME (w->frame);
24645 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24646 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24647 Lisp_Object pointer = Qnil;
24648 int dx, dy, width, height;
24649 EMACS_INT charpos;
24650 Lisp_Object string, object = Qnil;
24651 Lisp_Object pos, help;
24652
24653 Lisp_Object mouse_face;
24654 int original_x_pixel = x;
24655 struct glyph * glyph = NULL, * row_start_glyph = NULL;
24656 struct glyph_row *row;
24657
24658 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
24659 {
24660 int x0;
24661 struct glyph *end;
24662
24663 /* Kludge alert: mode_line_string takes X/Y in pixels, but
24664 returns them in row/column units! */
24665 string = mode_line_string (w, area, &x, &y, &charpos,
24666 &object, &dx, &dy, &width, &height);
24667
24668 row = (area == ON_MODE_LINE
24669 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
24670 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
24671
24672 /* Find the glyph under the mouse pointer. */
24673 if (row->mode_line_p && row->enabled_p)
24674 {
24675 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
24676 end = glyph + row->used[TEXT_AREA];
24677
24678 for (x0 = original_x_pixel;
24679 glyph < end && x0 >= glyph->pixel_width;
24680 ++glyph)
24681 x0 -= glyph->pixel_width;
24682
24683 if (glyph >= end)
24684 glyph = NULL;
24685 }
24686 }
24687 else
24688 {
24689 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
24690 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
24691 returns them in row/column units! */
24692 string = marginal_area_string (w, area, &x, &y, &charpos,
24693 &object, &dx, &dy, &width, &height);
24694 }
24695
24696 help = Qnil;
24697
24698 if (IMAGEP (object))
24699 {
24700 Lisp_Object image_map, hotspot;
24701 if ((image_map = Fplist_get (XCDR (object), QCmap),
24702 !NILP (image_map))
24703 && (hotspot = find_hot_spot (image_map, dx, dy),
24704 CONSP (hotspot))
24705 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24706 {
24707 Lisp_Object area_id, plist;
24708
24709 area_id = XCAR (hotspot);
24710 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24711 If so, we could look for mouse-enter, mouse-leave
24712 properties in PLIST (and do something...). */
24713 hotspot = XCDR (hotspot);
24714 if (CONSP (hotspot)
24715 && (plist = XCAR (hotspot), CONSP (plist)))
24716 {
24717 pointer = Fplist_get (plist, Qpointer);
24718 if (NILP (pointer))
24719 pointer = Qhand;
24720 help = Fplist_get (plist, Qhelp_echo);
24721 if (!NILP (help))
24722 {
24723 help_echo_string = help;
24724 /* Is this correct? ++kfs */
24725 XSETWINDOW (help_echo_window, w);
24726 help_echo_object = w->buffer;
24727 help_echo_pos = charpos;
24728 }
24729 }
24730 }
24731 if (NILP (pointer))
24732 pointer = Fplist_get (XCDR (object), QCpointer);
24733 }
24734
24735 if (STRINGP (string))
24736 {
24737 pos = make_number (charpos);
24738 /* If we're on a string with `help-echo' text property, arrange
24739 for the help to be displayed. This is done by setting the
24740 global variable help_echo_string to the help string. */
24741 if (NILP (help))
24742 {
24743 help = Fget_text_property (pos, Qhelp_echo, string);
24744 if (!NILP (help))
24745 {
24746 help_echo_string = help;
24747 XSETWINDOW (help_echo_window, w);
24748 help_echo_object = string;
24749 help_echo_pos = charpos;
24750 }
24751 }
24752
24753 if (NILP (pointer))
24754 pointer = Fget_text_property (pos, Qpointer, string);
24755
24756 /* Change the mouse pointer according to what is under X/Y. */
24757 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
24758 {
24759 Lisp_Object map;
24760 map = Fget_text_property (pos, Qlocal_map, string);
24761 if (!KEYMAPP (map))
24762 map = Fget_text_property (pos, Qkeymap, string);
24763 if (!KEYMAPP (map))
24764 cursor = dpyinfo->vertical_scroll_bar_cursor;
24765 }
24766
24767 /* Change the mouse face according to what is under X/Y. */
24768 mouse_face = Fget_text_property (pos, Qmouse_face, string);
24769 if (!NILP (mouse_face)
24770 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24771 && glyph)
24772 {
24773 Lisp_Object b, e;
24774
24775 struct glyph * tmp_glyph;
24776
24777 int gpos;
24778 int gseq_length;
24779 int total_pixel_width;
24780 EMACS_INT begpos, endpos, ignore;
24781
24782 int vpos, hpos;
24783
24784 b = Fprevious_single_property_change (make_number (charpos + 1),
24785 Qmouse_face, string, Qnil);
24786 if (NILP (b))
24787 begpos = 0;
24788 else
24789 begpos = XINT (b);
24790
24791 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
24792 if (NILP (e))
24793 endpos = SCHARS (string);
24794 else
24795 endpos = XINT (e);
24796
24797 /* Calculate the glyph position GPOS of GLYPH in the
24798 displayed string, relative to the beginning of the
24799 highlighted part of the string.
24800
24801 Note: GPOS is different from CHARPOS. CHARPOS is the
24802 position of GLYPH in the internal string object. A mode
24803 line string format has structures which are converted to
24804 a flattened string by the Emacs Lisp interpreter. The
24805 internal string is an element of those structures. The
24806 displayed string is the flattened string. */
24807 tmp_glyph = row_start_glyph;
24808 while (tmp_glyph < glyph
24809 && (!(EQ (tmp_glyph->object, glyph->object)
24810 && begpos <= tmp_glyph->charpos
24811 && tmp_glyph->charpos < endpos)))
24812 tmp_glyph++;
24813 gpos = glyph - tmp_glyph;
24814
24815 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
24816 the highlighted part of the displayed string to which
24817 GLYPH belongs. Note: GSEQ_LENGTH is different from
24818 SCHARS (STRING), because the latter returns the length of
24819 the internal string. */
24820 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
24821 tmp_glyph > glyph
24822 && (!(EQ (tmp_glyph->object, glyph->object)
24823 && begpos <= tmp_glyph->charpos
24824 && tmp_glyph->charpos < endpos));
24825 tmp_glyph--)
24826 ;
24827 gseq_length = gpos + (tmp_glyph - glyph) + 1;
24828
24829 /* Calculate the total pixel width of all the glyphs between
24830 the beginning of the highlighted area and GLYPH. */
24831 total_pixel_width = 0;
24832 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
24833 total_pixel_width += tmp_glyph->pixel_width;
24834
24835 /* Pre calculation of re-rendering position. Note: X is in
24836 column units here, after the call to mode_line_string or
24837 marginal_area_string. */
24838 hpos = x - gpos;
24839 vpos = (area == ON_MODE_LINE
24840 ? (w->current_matrix)->nrows - 1
24841 : 0);
24842
24843 /* If GLYPH's position is included in the region that is
24844 already drawn in mouse face, we have nothing to do. */
24845 if ( EQ (window, dpyinfo->mouse_face_window)
24846 && (!row->reversed_p
24847 ? (dpyinfo->mouse_face_beg_col <= hpos
24848 && hpos < dpyinfo->mouse_face_end_col)
24849 /* In R2L rows we swap BEG and END, see below. */
24850 : (dpyinfo->mouse_face_end_col <= hpos
24851 && hpos < dpyinfo->mouse_face_beg_col))
24852 && dpyinfo->mouse_face_beg_row == vpos )
24853 return;
24854
24855 if (clear_mouse_face (dpyinfo))
24856 cursor = No_Cursor;
24857
24858 if (!row->reversed_p)
24859 {
24860 dpyinfo->mouse_face_beg_col = hpos;
24861 dpyinfo->mouse_face_beg_x = original_x_pixel
24862 - (total_pixel_width + dx);
24863 dpyinfo->mouse_face_end_col = hpos + gseq_length;
24864 dpyinfo->mouse_face_end_x = 0;
24865 }
24866 else
24867 {
24868 /* In R2L rows, show_mouse_face expects BEG and END
24869 coordinates to be swapped. */
24870 dpyinfo->mouse_face_end_col = hpos;
24871 dpyinfo->mouse_face_end_x = original_x_pixel
24872 - (total_pixel_width + dx);
24873 dpyinfo->mouse_face_beg_col = hpos + gseq_length;
24874 dpyinfo->mouse_face_beg_x = 0;
24875 }
24876
24877 dpyinfo->mouse_face_beg_row = vpos;
24878 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
24879 dpyinfo->mouse_face_beg_y = 0;
24880 dpyinfo->mouse_face_end_y = 0;
24881 dpyinfo->mouse_face_past_end = 0;
24882 dpyinfo->mouse_face_window = window;
24883
24884 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
24885 charpos,
24886 0, 0, 0,
24887 &ignore,
24888 glyph->face_id,
24889 1);
24890 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24891
24892 if (NILP (pointer))
24893 pointer = Qhand;
24894 }
24895 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24896 clear_mouse_face (dpyinfo);
24897 }
24898 define_frame_cursor1 (f, cursor, pointer);
24899 }
24900
24901
24902 /* EXPORT:
24903 Take proper action when the mouse has moved to position X, Y on
24904 frame F as regards highlighting characters that have mouse-face
24905 properties. Also de-highlighting chars where the mouse was before.
24906 X and Y can be negative or out of range. */
24907
24908 void
24909 note_mouse_highlight (struct frame *f, int x, int y)
24910 {
24911 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24912 enum window_part part;
24913 Lisp_Object window;
24914 struct window *w;
24915 Cursor cursor = No_Cursor;
24916 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
24917 struct buffer *b;
24918
24919 /* When a menu is active, don't highlight because this looks odd. */
24920 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
24921 if (popup_activated ())
24922 return;
24923 #endif
24924
24925 if (NILP (Vmouse_highlight)
24926 || !f->glyphs_initialized_p
24927 || f->pointer_invisible)
24928 return;
24929
24930 dpyinfo->mouse_face_mouse_x = x;
24931 dpyinfo->mouse_face_mouse_y = y;
24932 dpyinfo->mouse_face_mouse_frame = f;
24933
24934 if (dpyinfo->mouse_face_defer)
24935 return;
24936
24937 if (gc_in_progress)
24938 {
24939 dpyinfo->mouse_face_deferred_gc = 1;
24940 return;
24941 }
24942
24943 /* Which window is that in? */
24944 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
24945
24946 /* If we were displaying active text in another window, clear that.
24947 Also clear if we move out of text area in same window. */
24948 if (! EQ (window, dpyinfo->mouse_face_window)
24949 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
24950 && !NILP (dpyinfo->mouse_face_window)))
24951 clear_mouse_face (dpyinfo);
24952
24953 /* Not on a window -> return. */
24954 if (!WINDOWP (window))
24955 return;
24956
24957 /* Reset help_echo_string. It will get recomputed below. */
24958 help_echo_string = Qnil;
24959
24960 /* Convert to window-relative pixel coordinates. */
24961 w = XWINDOW (window);
24962 frame_to_window_pixel_xy (w, &x, &y);
24963
24964 /* Handle tool-bar window differently since it doesn't display a
24965 buffer. */
24966 if (EQ (window, f->tool_bar_window))
24967 {
24968 note_tool_bar_highlight (f, x, y);
24969 return;
24970 }
24971
24972 /* Mouse is on the mode, header line or margin? */
24973 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
24974 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
24975 {
24976 note_mode_line_or_margin_highlight (window, x, y, part);
24977 return;
24978 }
24979
24980 if (part == ON_VERTICAL_BORDER)
24981 {
24982 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24983 help_echo_string = build_string ("drag-mouse-1: resize");
24984 }
24985 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
24986 || part == ON_SCROLL_BAR)
24987 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24988 else
24989 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24990
24991 /* Are we in a window whose display is up to date?
24992 And verify the buffer's text has not changed. */
24993 b = XBUFFER (w->buffer);
24994 if (part == ON_TEXT
24995 && EQ (w->window_end_valid, w->buffer)
24996 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
24997 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
24998 {
24999 int hpos, vpos, i, dx, dy, area;
25000 EMACS_INT pos;
25001 struct glyph *glyph;
25002 Lisp_Object object;
25003 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
25004 Lisp_Object *overlay_vec = NULL;
25005 int noverlays;
25006 struct buffer *obuf;
25007 EMACS_INT obegv, ozv;
25008 int same_region;
25009
25010 /* Find the glyph under X/Y. */
25011 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
25012
25013 /* Look for :pointer property on image. */
25014 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25015 {
25016 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25017 if (img != NULL && IMAGEP (img->spec))
25018 {
25019 Lisp_Object image_map, hotspot;
25020 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
25021 !NILP (image_map))
25022 && (hotspot = find_hot_spot (image_map,
25023 glyph->slice.img.x + dx,
25024 glyph->slice.img.y + dy),
25025 CONSP (hotspot))
25026 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25027 {
25028 Lisp_Object area_id, plist;
25029
25030 area_id = XCAR (hotspot);
25031 /* Could check AREA_ID to see if we enter/leave this hot-spot.
25032 If so, we could look for mouse-enter, mouse-leave
25033 properties in PLIST (and do something...). */
25034 hotspot = XCDR (hotspot);
25035 if (CONSP (hotspot)
25036 && (plist = XCAR (hotspot), CONSP (plist)))
25037 {
25038 pointer = Fplist_get (plist, Qpointer);
25039 if (NILP (pointer))
25040 pointer = Qhand;
25041 help_echo_string = Fplist_get (plist, Qhelp_echo);
25042 if (!NILP (help_echo_string))
25043 {
25044 help_echo_window = window;
25045 help_echo_object = glyph->object;
25046 help_echo_pos = glyph->charpos;
25047 }
25048 }
25049 }
25050 if (NILP (pointer))
25051 pointer = Fplist_get (XCDR (img->spec), QCpointer);
25052 }
25053 }
25054
25055 /* Clear mouse face if X/Y not over text. */
25056 if (glyph == NULL
25057 || area != TEXT_AREA
25058 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
25059 /* R2L rows have a stretch glyph at their front, which
25060 stands for no text, whereas L2R rows have no glyphs at
25061 all beyond the end of text. Treat such stretch glyphs
25062 like we do with NULL glyphs in L2R rows. */
25063 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
25064 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
25065 && glyph->type == STRETCH_GLYPH
25066 && glyph->avoid_cursor_p))
25067 {
25068 if (clear_mouse_face (dpyinfo))
25069 cursor = No_Cursor;
25070 if (NILP (pointer))
25071 {
25072 if (area != TEXT_AREA)
25073 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25074 else
25075 pointer = Vvoid_text_area_pointer;
25076 }
25077 goto set_cursor;
25078 }
25079
25080 pos = glyph->charpos;
25081 object = glyph->object;
25082 if (!STRINGP (object) && !BUFFERP (object))
25083 goto set_cursor;
25084
25085 /* If we get an out-of-range value, return now; avoid an error. */
25086 if (BUFFERP (object) && pos > BUF_Z (b))
25087 goto set_cursor;
25088
25089 /* Make the window's buffer temporarily current for
25090 overlays_at and compute_char_face. */
25091 obuf = current_buffer;
25092 current_buffer = b;
25093 obegv = BEGV;
25094 ozv = ZV;
25095 BEGV = BEG;
25096 ZV = Z;
25097
25098 /* Is this char mouse-active or does it have help-echo? */
25099 position = make_number (pos);
25100
25101 if (BUFFERP (object))
25102 {
25103 /* Put all the overlays we want in a vector in overlay_vec. */
25104 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
25105 /* Sort overlays into increasing priority order. */
25106 noverlays = sort_overlays (overlay_vec, noverlays, w);
25107 }
25108 else
25109 noverlays = 0;
25110
25111 same_region = coords_in_mouse_face_p (w, hpos, vpos);
25112
25113 if (same_region)
25114 cursor = No_Cursor;
25115
25116 /* Check mouse-face highlighting. */
25117 if (! same_region
25118 /* If there exists an overlay with mouse-face overlapping
25119 the one we are currently highlighting, we have to
25120 check if we enter the overlapping overlay, and then
25121 highlight only that. */
25122 || (OVERLAYP (dpyinfo->mouse_face_overlay)
25123 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
25124 {
25125 /* Find the highest priority overlay with a mouse-face. */
25126 overlay = Qnil;
25127 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
25128 {
25129 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
25130 if (!NILP (mouse_face))
25131 overlay = overlay_vec[i];
25132 }
25133
25134 /* If we're highlighting the same overlay as before, there's
25135 no need to do that again. */
25136 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
25137 goto check_help_echo;
25138 dpyinfo->mouse_face_overlay = overlay;
25139
25140 /* Clear the display of the old active region, if any. */
25141 if (clear_mouse_face (dpyinfo))
25142 cursor = No_Cursor;
25143
25144 /* If no overlay applies, get a text property. */
25145 if (NILP (overlay))
25146 mouse_face = Fget_text_property (position, Qmouse_face, object);
25147
25148 /* Next, compute the bounds of the mouse highlighting and
25149 display it. */
25150 if (!NILP (mouse_face) && STRINGP (object))
25151 {
25152 /* The mouse-highlighting comes from a display string
25153 with a mouse-face. */
25154 Lisp_Object b, e;
25155 EMACS_INT ignore;
25156
25157 b = Fprevious_single_property_change
25158 (make_number (pos + 1), Qmouse_face, object, Qnil);
25159 e = Fnext_single_property_change
25160 (position, Qmouse_face, object, Qnil);
25161 if (NILP (b))
25162 b = make_number (0);
25163 if (NILP (e))
25164 e = make_number (SCHARS (object) - 1);
25165 mouse_face_from_string_pos (w, dpyinfo, object,
25166 XINT (b), XINT (e));
25167 dpyinfo->mouse_face_past_end = 0;
25168 dpyinfo->mouse_face_window = window;
25169 dpyinfo->mouse_face_face_id
25170 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
25171 glyph->face_id, 1);
25172 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
25173 cursor = No_Cursor;
25174 }
25175 else
25176 {
25177 /* The mouse-highlighting, if any, comes from an overlay
25178 or text property in the buffer. */
25179 Lisp_Object buffer, display_string;
25180
25181 if (STRINGP (object))
25182 {
25183 /* If we are on a display string with no mouse-face,
25184 check if the text under it has one. */
25185 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
25186 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25187 pos = string_buffer_position (w, object, start);
25188 if (pos > 0)
25189 {
25190 mouse_face = get_char_property_and_overlay
25191 (make_number (pos), Qmouse_face, w->buffer, &overlay);
25192 buffer = w->buffer;
25193 display_string = object;
25194 }
25195 }
25196 else
25197 {
25198 buffer = object;
25199 display_string = Qnil;
25200 }
25201
25202 if (!NILP (mouse_face))
25203 {
25204 Lisp_Object before, after;
25205 Lisp_Object before_string, after_string;
25206 /* To correctly find the limits of mouse highlight
25207 in a bidi-reordered buffer, we must not use the
25208 optimization of limiting the search in
25209 previous-single-property-change and
25210 next-single-property-change, because
25211 rows_from_pos_range needs the real start and end
25212 positions to DTRT in this case. That's because
25213 the first row visible in a window does not
25214 necessarily display the character whose position
25215 is the smallest. */
25216 Lisp_Object lim1 =
25217 NILP (XBUFFER (buffer)->bidi_display_reordering)
25218 ? Fmarker_position (w->start)
25219 : Qnil;
25220 Lisp_Object lim2 =
25221 NILP (XBUFFER (buffer)->bidi_display_reordering)
25222 ? make_number (BUF_Z (XBUFFER (buffer))
25223 - XFASTINT (w->window_end_pos))
25224 : Qnil;
25225
25226 if (NILP (overlay))
25227 {
25228 /* Handle the text property case. */
25229 before = Fprevious_single_property_change
25230 (make_number (pos + 1), Qmouse_face, buffer, lim1);
25231 after = Fnext_single_property_change
25232 (make_number (pos), Qmouse_face, buffer, lim2);
25233 before_string = after_string = Qnil;
25234 }
25235 else
25236 {
25237 /* Handle the overlay case. */
25238 before = Foverlay_start (overlay);
25239 after = Foverlay_end (overlay);
25240 before_string = Foverlay_get (overlay, Qbefore_string);
25241 after_string = Foverlay_get (overlay, Qafter_string);
25242
25243 if (!STRINGP (before_string)) before_string = Qnil;
25244 if (!STRINGP (after_string)) after_string = Qnil;
25245 }
25246
25247 mouse_face_from_buffer_pos (window, dpyinfo, pos,
25248 XFASTINT (before),
25249 XFASTINT (after),
25250 before_string, after_string,
25251 display_string);
25252 cursor = No_Cursor;
25253 }
25254 }
25255 }
25256
25257 check_help_echo:
25258
25259 /* Look for a `help-echo' property. */
25260 if (NILP (help_echo_string)) {
25261 Lisp_Object help, overlay;
25262
25263 /* Check overlays first. */
25264 help = overlay = Qnil;
25265 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
25266 {
25267 overlay = overlay_vec[i];
25268 help = Foverlay_get (overlay, Qhelp_echo);
25269 }
25270
25271 if (!NILP (help))
25272 {
25273 help_echo_string = help;
25274 help_echo_window = window;
25275 help_echo_object = overlay;
25276 help_echo_pos = pos;
25277 }
25278 else
25279 {
25280 Lisp_Object object = glyph->object;
25281 EMACS_INT charpos = glyph->charpos;
25282
25283 /* Try text properties. */
25284 if (STRINGP (object)
25285 && charpos >= 0
25286 && charpos < SCHARS (object))
25287 {
25288 help = Fget_text_property (make_number (charpos),
25289 Qhelp_echo, object);
25290 if (NILP (help))
25291 {
25292 /* If the string itself doesn't specify a help-echo,
25293 see if the buffer text ``under'' it does. */
25294 struct glyph_row *r
25295 = MATRIX_ROW (w->current_matrix, vpos);
25296 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25297 EMACS_INT pos = string_buffer_position (w, object, start);
25298 if (pos > 0)
25299 {
25300 help = Fget_char_property (make_number (pos),
25301 Qhelp_echo, w->buffer);
25302 if (!NILP (help))
25303 {
25304 charpos = pos;
25305 object = w->buffer;
25306 }
25307 }
25308 }
25309 }
25310 else if (BUFFERP (object)
25311 && charpos >= BEGV
25312 && charpos < ZV)
25313 help = Fget_text_property (make_number (charpos), Qhelp_echo,
25314 object);
25315
25316 if (!NILP (help))
25317 {
25318 help_echo_string = help;
25319 help_echo_window = window;
25320 help_echo_object = object;
25321 help_echo_pos = charpos;
25322 }
25323 }
25324 }
25325
25326 /* Look for a `pointer' property. */
25327 if (NILP (pointer))
25328 {
25329 /* Check overlays first. */
25330 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
25331 pointer = Foverlay_get (overlay_vec[i], Qpointer);
25332
25333 if (NILP (pointer))
25334 {
25335 Lisp_Object object = glyph->object;
25336 EMACS_INT charpos = glyph->charpos;
25337
25338 /* Try text properties. */
25339 if (STRINGP (object)
25340 && charpos >= 0
25341 && charpos < SCHARS (object))
25342 {
25343 pointer = Fget_text_property (make_number (charpos),
25344 Qpointer, object);
25345 if (NILP (pointer))
25346 {
25347 /* If the string itself doesn't specify a pointer,
25348 see if the buffer text ``under'' it does. */
25349 struct glyph_row *r
25350 = MATRIX_ROW (w->current_matrix, vpos);
25351 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25352 EMACS_INT pos = string_buffer_position (w, object,
25353 start);
25354 if (pos > 0)
25355 pointer = Fget_char_property (make_number (pos),
25356 Qpointer, w->buffer);
25357 }
25358 }
25359 else if (BUFFERP (object)
25360 && charpos >= BEGV
25361 && charpos < ZV)
25362 pointer = Fget_text_property (make_number (charpos),
25363 Qpointer, object);
25364 }
25365 }
25366
25367 BEGV = obegv;
25368 ZV = ozv;
25369 current_buffer = obuf;
25370 }
25371
25372 set_cursor:
25373
25374 define_frame_cursor1 (f, cursor, pointer);
25375 }
25376
25377
25378 /* EXPORT for RIF:
25379 Clear any mouse-face on window W. This function is part of the
25380 redisplay interface, and is called from try_window_id and similar
25381 functions to ensure the mouse-highlight is off. */
25382
25383 void
25384 x_clear_window_mouse_face (struct window *w)
25385 {
25386 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
25387 Lisp_Object window;
25388
25389 BLOCK_INPUT;
25390 XSETWINDOW (window, w);
25391 if (EQ (window, dpyinfo->mouse_face_window))
25392 clear_mouse_face (dpyinfo);
25393 UNBLOCK_INPUT;
25394 }
25395
25396
25397 /* EXPORT:
25398 Just discard the mouse face information for frame F, if any.
25399 This is used when the size of F is changed. */
25400
25401 void
25402 cancel_mouse_face (struct frame *f)
25403 {
25404 Lisp_Object window;
25405 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
25406
25407 window = dpyinfo->mouse_face_window;
25408 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
25409 {
25410 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
25411 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
25412 dpyinfo->mouse_face_window = Qnil;
25413 }
25414 }
25415
25416
25417 #endif /* HAVE_WINDOW_SYSTEM */
25418
25419 \f
25420 /***********************************************************************
25421 Exposure Events
25422 ***********************************************************************/
25423
25424 #ifdef HAVE_WINDOW_SYSTEM
25425
25426 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
25427 which intersects rectangle R. R is in window-relative coordinates. */
25428
25429 static void
25430 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
25431 enum glyph_row_area area)
25432 {
25433 struct glyph *first = row->glyphs[area];
25434 struct glyph *end = row->glyphs[area] + row->used[area];
25435 struct glyph *last;
25436 int first_x, start_x, x;
25437
25438 if (area == TEXT_AREA && row->fill_line_p)
25439 /* If row extends face to end of line write the whole line. */
25440 draw_glyphs (w, 0, row, area,
25441 0, row->used[area],
25442 DRAW_NORMAL_TEXT, 0);
25443 else
25444 {
25445 /* Set START_X to the window-relative start position for drawing glyphs of
25446 AREA. The first glyph of the text area can be partially visible.
25447 The first glyphs of other areas cannot. */
25448 start_x = window_box_left_offset (w, area);
25449 x = start_x;
25450 if (area == TEXT_AREA)
25451 x += row->x;
25452
25453 /* Find the first glyph that must be redrawn. */
25454 while (first < end
25455 && x + first->pixel_width < r->x)
25456 {
25457 x += first->pixel_width;
25458 ++first;
25459 }
25460
25461 /* Find the last one. */
25462 last = first;
25463 first_x = x;
25464 while (last < end
25465 && x < r->x + r->width)
25466 {
25467 x += last->pixel_width;
25468 ++last;
25469 }
25470
25471 /* Repaint. */
25472 if (last > first)
25473 draw_glyphs (w, first_x - start_x, row, area,
25474 first - row->glyphs[area], last - row->glyphs[area],
25475 DRAW_NORMAL_TEXT, 0);
25476 }
25477 }
25478
25479
25480 /* Redraw the parts of the glyph row ROW on window W intersecting
25481 rectangle R. R is in window-relative coordinates. Value is
25482 non-zero if mouse-face was overwritten. */
25483
25484 static int
25485 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
25486 {
25487 xassert (row->enabled_p);
25488
25489 if (row->mode_line_p || w->pseudo_window_p)
25490 draw_glyphs (w, 0, row, TEXT_AREA,
25491 0, row->used[TEXT_AREA],
25492 DRAW_NORMAL_TEXT, 0);
25493 else
25494 {
25495 if (row->used[LEFT_MARGIN_AREA])
25496 expose_area (w, row, r, LEFT_MARGIN_AREA);
25497 if (row->used[TEXT_AREA])
25498 expose_area (w, row, r, TEXT_AREA);
25499 if (row->used[RIGHT_MARGIN_AREA])
25500 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25501 draw_row_fringe_bitmaps (w, row);
25502 }
25503
25504 return row->mouse_face_p;
25505 }
25506
25507
25508 /* Redraw those parts of glyphs rows during expose event handling that
25509 overlap other rows. Redrawing of an exposed line writes over parts
25510 of lines overlapping that exposed line; this function fixes that.
25511
25512 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25513 row in W's current matrix that is exposed and overlaps other rows.
25514 LAST_OVERLAPPING_ROW is the last such row. */
25515
25516 static void
25517 expose_overlaps (struct window *w,
25518 struct glyph_row *first_overlapping_row,
25519 struct glyph_row *last_overlapping_row,
25520 XRectangle *r)
25521 {
25522 struct glyph_row *row;
25523
25524 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
25525 if (row->overlapping_p)
25526 {
25527 xassert (row->enabled_p && !row->mode_line_p);
25528
25529 row->clip = r;
25530 if (row->used[LEFT_MARGIN_AREA])
25531 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
25532
25533 if (row->used[TEXT_AREA])
25534 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
25535
25536 if (row->used[RIGHT_MARGIN_AREA])
25537 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
25538 row->clip = NULL;
25539 }
25540 }
25541
25542
25543 /* Return non-zero if W's cursor intersects rectangle R. */
25544
25545 static int
25546 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
25547 {
25548 XRectangle cr, result;
25549 struct glyph *cursor_glyph;
25550 struct glyph_row *row;
25551
25552 if (w->phys_cursor.vpos >= 0
25553 && w->phys_cursor.vpos < w->current_matrix->nrows
25554 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
25555 row->enabled_p)
25556 && row->cursor_in_fringe_p)
25557 {
25558 /* Cursor is in the fringe. */
25559 cr.x = window_box_right_offset (w,
25560 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
25561 ? RIGHT_MARGIN_AREA
25562 : TEXT_AREA));
25563 cr.y = row->y;
25564 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
25565 cr.height = row->height;
25566 return x_intersect_rectangles (&cr, r, &result);
25567 }
25568
25569 cursor_glyph = get_phys_cursor_glyph (w);
25570 if (cursor_glyph)
25571 {
25572 /* r is relative to W's box, but w->phys_cursor.x is relative
25573 to left edge of W's TEXT area. Adjust it. */
25574 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
25575 cr.y = w->phys_cursor.y;
25576 cr.width = cursor_glyph->pixel_width;
25577 cr.height = w->phys_cursor_height;
25578 /* ++KFS: W32 version used W32-specific IntersectRect here, but
25579 I assume the effect is the same -- and this is portable. */
25580 return x_intersect_rectangles (&cr, r, &result);
25581 }
25582 /* If we don't understand the format, pretend we're not in the hot-spot. */
25583 return 0;
25584 }
25585
25586
25587 /* EXPORT:
25588 Draw a vertical window border to the right of window W if W doesn't
25589 have vertical scroll bars. */
25590
25591 void
25592 x_draw_vertical_border (struct window *w)
25593 {
25594 struct frame *f = XFRAME (WINDOW_FRAME (w));
25595
25596 /* We could do better, if we knew what type of scroll-bar the adjacent
25597 windows (on either side) have... But we don't :-(
25598 However, I think this works ok. ++KFS 2003-04-25 */
25599
25600 /* Redraw borders between horizontally adjacent windows. Don't
25601 do it for frames with vertical scroll bars because either the
25602 right scroll bar of a window, or the left scroll bar of its
25603 neighbor will suffice as a border. */
25604 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
25605 return;
25606
25607 if (!WINDOW_RIGHTMOST_P (w)
25608 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
25609 {
25610 int x0, x1, y0, y1;
25611
25612 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25613 y1 -= 1;
25614
25615 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25616 x1 -= 1;
25617
25618 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
25619 }
25620 else if (!WINDOW_LEFTMOST_P (w)
25621 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
25622 {
25623 int x0, x1, y0, y1;
25624
25625 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25626 y1 -= 1;
25627
25628 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25629 x0 -= 1;
25630
25631 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
25632 }
25633 }
25634
25635
25636 /* Redraw the part of window W intersection rectangle FR. Pixel
25637 coordinates in FR are frame-relative. Call this function with
25638 input blocked. Value is non-zero if the exposure overwrites
25639 mouse-face. */
25640
25641 static int
25642 expose_window (struct window *w, XRectangle *fr)
25643 {
25644 struct frame *f = XFRAME (w->frame);
25645 XRectangle wr, r;
25646 int mouse_face_overwritten_p = 0;
25647
25648 /* If window is not yet fully initialized, do nothing. This can
25649 happen when toolkit scroll bars are used and a window is split.
25650 Reconfiguring the scroll bar will generate an expose for a newly
25651 created window. */
25652 if (w->current_matrix == NULL)
25653 return 0;
25654
25655 /* When we're currently updating the window, display and current
25656 matrix usually don't agree. Arrange for a thorough display
25657 later. */
25658 if (w == updated_window)
25659 {
25660 SET_FRAME_GARBAGED (f);
25661 return 0;
25662 }
25663
25664 /* Frame-relative pixel rectangle of W. */
25665 wr.x = WINDOW_LEFT_EDGE_X (w);
25666 wr.y = WINDOW_TOP_EDGE_Y (w);
25667 wr.width = WINDOW_TOTAL_WIDTH (w);
25668 wr.height = WINDOW_TOTAL_HEIGHT (w);
25669
25670 if (x_intersect_rectangles (fr, &wr, &r))
25671 {
25672 int yb = window_text_bottom_y (w);
25673 struct glyph_row *row;
25674 int cursor_cleared_p;
25675 struct glyph_row *first_overlapping_row, *last_overlapping_row;
25676
25677 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
25678 r.x, r.y, r.width, r.height));
25679
25680 /* Convert to window coordinates. */
25681 r.x -= WINDOW_LEFT_EDGE_X (w);
25682 r.y -= WINDOW_TOP_EDGE_Y (w);
25683
25684 /* Turn off the cursor. */
25685 if (!w->pseudo_window_p
25686 && phys_cursor_in_rect_p (w, &r))
25687 {
25688 x_clear_cursor (w);
25689 cursor_cleared_p = 1;
25690 }
25691 else
25692 cursor_cleared_p = 0;
25693
25694 /* Update lines intersecting rectangle R. */
25695 first_overlapping_row = last_overlapping_row = NULL;
25696 for (row = w->current_matrix->rows;
25697 row->enabled_p;
25698 ++row)
25699 {
25700 int y0 = row->y;
25701 int y1 = MATRIX_ROW_BOTTOM_Y (row);
25702
25703 if ((y0 >= r.y && y0 < r.y + r.height)
25704 || (y1 > r.y && y1 < r.y + r.height)
25705 || (r.y >= y0 && r.y < y1)
25706 || (r.y + r.height > y0 && r.y + r.height < y1))
25707 {
25708 /* A header line may be overlapping, but there is no need
25709 to fix overlapping areas for them. KFS 2005-02-12 */
25710 if (row->overlapping_p && !row->mode_line_p)
25711 {
25712 if (first_overlapping_row == NULL)
25713 first_overlapping_row = row;
25714 last_overlapping_row = row;
25715 }
25716
25717 row->clip = fr;
25718 if (expose_line (w, row, &r))
25719 mouse_face_overwritten_p = 1;
25720 row->clip = NULL;
25721 }
25722 else if (row->overlapping_p)
25723 {
25724 /* We must redraw a row overlapping the exposed area. */
25725 if (y0 < r.y
25726 ? y0 + row->phys_height > r.y
25727 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
25728 {
25729 if (first_overlapping_row == NULL)
25730 first_overlapping_row = row;
25731 last_overlapping_row = row;
25732 }
25733 }
25734
25735 if (y1 >= yb)
25736 break;
25737 }
25738
25739 /* Display the mode line if there is one. */
25740 if (WINDOW_WANTS_MODELINE_P (w)
25741 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
25742 row->enabled_p)
25743 && row->y < r.y + r.height)
25744 {
25745 if (expose_line (w, row, &r))
25746 mouse_face_overwritten_p = 1;
25747 }
25748
25749 if (!w->pseudo_window_p)
25750 {
25751 /* Fix the display of overlapping rows. */
25752 if (first_overlapping_row)
25753 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
25754 fr);
25755
25756 /* Draw border between windows. */
25757 x_draw_vertical_border (w);
25758
25759 /* Turn the cursor on again. */
25760 if (cursor_cleared_p)
25761 update_window_cursor (w, 1);
25762 }
25763 }
25764
25765 return mouse_face_overwritten_p;
25766 }
25767
25768
25769
25770 /* Redraw (parts) of all windows in the window tree rooted at W that
25771 intersect R. R contains frame pixel coordinates. Value is
25772 non-zero if the exposure overwrites mouse-face. */
25773
25774 static int
25775 expose_window_tree (struct window *w, XRectangle *r)
25776 {
25777 struct frame *f = XFRAME (w->frame);
25778 int mouse_face_overwritten_p = 0;
25779
25780 while (w && !FRAME_GARBAGED_P (f))
25781 {
25782 if (!NILP (w->hchild))
25783 mouse_face_overwritten_p
25784 |= expose_window_tree (XWINDOW (w->hchild), r);
25785 else if (!NILP (w->vchild))
25786 mouse_face_overwritten_p
25787 |= expose_window_tree (XWINDOW (w->vchild), r);
25788 else
25789 mouse_face_overwritten_p |= expose_window (w, r);
25790
25791 w = NILP (w->next) ? NULL : XWINDOW (w->next);
25792 }
25793
25794 return mouse_face_overwritten_p;
25795 }
25796
25797
25798 /* EXPORT:
25799 Redisplay an exposed area of frame F. X and Y are the upper-left
25800 corner of the exposed rectangle. W and H are width and height of
25801 the exposed area. All are pixel values. W or H zero means redraw
25802 the entire frame. */
25803
25804 void
25805 expose_frame (struct frame *f, int x, int y, int w, int h)
25806 {
25807 XRectangle r;
25808 int mouse_face_overwritten_p = 0;
25809
25810 TRACE ((stderr, "expose_frame "));
25811
25812 /* No need to redraw if frame will be redrawn soon. */
25813 if (FRAME_GARBAGED_P (f))
25814 {
25815 TRACE ((stderr, " garbaged\n"));
25816 return;
25817 }
25818
25819 /* If basic faces haven't been realized yet, there is no point in
25820 trying to redraw anything. This can happen when we get an expose
25821 event while Emacs is starting, e.g. by moving another window. */
25822 if (FRAME_FACE_CACHE (f) == NULL
25823 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
25824 {
25825 TRACE ((stderr, " no faces\n"));
25826 return;
25827 }
25828
25829 if (w == 0 || h == 0)
25830 {
25831 r.x = r.y = 0;
25832 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
25833 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
25834 }
25835 else
25836 {
25837 r.x = x;
25838 r.y = y;
25839 r.width = w;
25840 r.height = h;
25841 }
25842
25843 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
25844 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
25845
25846 if (WINDOWP (f->tool_bar_window))
25847 mouse_face_overwritten_p
25848 |= expose_window (XWINDOW (f->tool_bar_window), &r);
25849
25850 #ifdef HAVE_X_WINDOWS
25851 #ifndef MSDOS
25852 #ifndef USE_X_TOOLKIT
25853 if (WINDOWP (f->menu_bar_window))
25854 mouse_face_overwritten_p
25855 |= expose_window (XWINDOW (f->menu_bar_window), &r);
25856 #endif /* not USE_X_TOOLKIT */
25857 #endif
25858 #endif
25859
25860 /* Some window managers support a focus-follows-mouse style with
25861 delayed raising of frames. Imagine a partially obscured frame,
25862 and moving the mouse into partially obscured mouse-face on that
25863 frame. The visible part of the mouse-face will be highlighted,
25864 then the WM raises the obscured frame. With at least one WM, KDE
25865 2.1, Emacs is not getting any event for the raising of the frame
25866 (even tried with SubstructureRedirectMask), only Expose events.
25867 These expose events will draw text normally, i.e. not
25868 highlighted. Which means we must redo the highlight here.
25869 Subsume it under ``we love X''. --gerd 2001-08-15 */
25870 /* Included in Windows version because Windows most likely does not
25871 do the right thing if any third party tool offers
25872 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
25873 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
25874 {
25875 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
25876 if (f == dpyinfo->mouse_face_mouse_frame)
25877 {
25878 int x = dpyinfo->mouse_face_mouse_x;
25879 int y = dpyinfo->mouse_face_mouse_y;
25880 clear_mouse_face (dpyinfo);
25881 note_mouse_highlight (f, x, y);
25882 }
25883 }
25884 }
25885
25886
25887 /* EXPORT:
25888 Determine the intersection of two rectangles R1 and R2. Return
25889 the intersection in *RESULT. Value is non-zero if RESULT is not
25890 empty. */
25891
25892 int
25893 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
25894 {
25895 XRectangle *left, *right;
25896 XRectangle *upper, *lower;
25897 int intersection_p = 0;
25898
25899 /* Rearrange so that R1 is the left-most rectangle. */
25900 if (r1->x < r2->x)
25901 left = r1, right = r2;
25902 else
25903 left = r2, right = r1;
25904
25905 /* X0 of the intersection is right.x0, if this is inside R1,
25906 otherwise there is no intersection. */
25907 if (right->x <= left->x + left->width)
25908 {
25909 result->x = right->x;
25910
25911 /* The right end of the intersection is the minimum of the
25912 the right ends of left and right. */
25913 result->width = (min (left->x + left->width, right->x + right->width)
25914 - result->x);
25915
25916 /* Same game for Y. */
25917 if (r1->y < r2->y)
25918 upper = r1, lower = r2;
25919 else
25920 upper = r2, lower = r1;
25921
25922 /* The upper end of the intersection is lower.y0, if this is inside
25923 of upper. Otherwise, there is no intersection. */
25924 if (lower->y <= upper->y + upper->height)
25925 {
25926 result->y = lower->y;
25927
25928 /* The lower end of the intersection is the minimum of the lower
25929 ends of upper and lower. */
25930 result->height = (min (lower->y + lower->height,
25931 upper->y + upper->height)
25932 - result->y);
25933 intersection_p = 1;
25934 }
25935 }
25936
25937 return intersection_p;
25938 }
25939
25940 #endif /* HAVE_WINDOW_SYSTEM */
25941
25942 \f
25943 /***********************************************************************
25944 Initialization
25945 ***********************************************************************/
25946
25947 void
25948 syms_of_xdisp (void)
25949 {
25950 Vwith_echo_area_save_vector = Qnil;
25951 staticpro (&Vwith_echo_area_save_vector);
25952
25953 Vmessage_stack = Qnil;
25954 staticpro (&Vmessage_stack);
25955
25956 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
25957 staticpro (&Qinhibit_redisplay);
25958
25959 message_dolog_marker1 = Fmake_marker ();
25960 staticpro (&message_dolog_marker1);
25961 message_dolog_marker2 = Fmake_marker ();
25962 staticpro (&message_dolog_marker2);
25963 message_dolog_marker3 = Fmake_marker ();
25964 staticpro (&message_dolog_marker3);
25965
25966 #if GLYPH_DEBUG
25967 defsubr (&Sdump_frame_glyph_matrix);
25968 defsubr (&Sdump_glyph_matrix);
25969 defsubr (&Sdump_glyph_row);
25970 defsubr (&Sdump_tool_bar_row);
25971 defsubr (&Strace_redisplay);
25972 defsubr (&Strace_to_stderr);
25973 #endif
25974 #ifdef HAVE_WINDOW_SYSTEM
25975 defsubr (&Stool_bar_lines_needed);
25976 defsubr (&Slookup_image_map);
25977 #endif
25978 defsubr (&Sformat_mode_line);
25979 defsubr (&Sinvisible_p);
25980 defsubr (&Scurrent_bidi_paragraph_direction);
25981
25982 staticpro (&Qmenu_bar_update_hook);
25983 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
25984
25985 staticpro (&Qoverriding_terminal_local_map);
25986 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
25987
25988 staticpro (&Qoverriding_local_map);
25989 Qoverriding_local_map = intern_c_string ("overriding-local-map");
25990
25991 staticpro (&Qwindow_scroll_functions);
25992 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
25993
25994 staticpro (&Qwindow_text_change_functions);
25995 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
25996
25997 staticpro (&Qredisplay_end_trigger_functions);
25998 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
25999
26000 staticpro (&Qinhibit_point_motion_hooks);
26001 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
26002
26003 Qeval = intern_c_string ("eval");
26004 staticpro (&Qeval);
26005
26006 QCdata = intern_c_string (":data");
26007 staticpro (&QCdata);
26008 Qdisplay = intern_c_string ("display");
26009 staticpro (&Qdisplay);
26010 Qspace_width = intern_c_string ("space-width");
26011 staticpro (&Qspace_width);
26012 Qraise = intern_c_string ("raise");
26013 staticpro (&Qraise);
26014 Qslice = intern_c_string ("slice");
26015 staticpro (&Qslice);
26016 Qspace = intern_c_string ("space");
26017 staticpro (&Qspace);
26018 Qmargin = intern_c_string ("margin");
26019 staticpro (&Qmargin);
26020 Qpointer = intern_c_string ("pointer");
26021 staticpro (&Qpointer);
26022 Qleft_margin = intern_c_string ("left-margin");
26023 staticpro (&Qleft_margin);
26024 Qright_margin = intern_c_string ("right-margin");
26025 staticpro (&Qright_margin);
26026 Qcenter = intern_c_string ("center");
26027 staticpro (&Qcenter);
26028 Qline_height = intern_c_string ("line-height");
26029 staticpro (&Qline_height);
26030 QCalign_to = intern_c_string (":align-to");
26031 staticpro (&QCalign_to);
26032 QCrelative_width = intern_c_string (":relative-width");
26033 staticpro (&QCrelative_width);
26034 QCrelative_height = intern_c_string (":relative-height");
26035 staticpro (&QCrelative_height);
26036 QCeval = intern_c_string (":eval");
26037 staticpro (&QCeval);
26038 QCpropertize = intern_c_string (":propertize");
26039 staticpro (&QCpropertize);
26040 QCfile = intern_c_string (":file");
26041 staticpro (&QCfile);
26042 Qfontified = intern_c_string ("fontified");
26043 staticpro (&Qfontified);
26044 Qfontification_functions = intern_c_string ("fontification-functions");
26045 staticpro (&Qfontification_functions);
26046 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
26047 staticpro (&Qtrailing_whitespace);
26048 Qescape_glyph = intern_c_string ("escape-glyph");
26049 staticpro (&Qescape_glyph);
26050 Qnobreak_space = intern_c_string ("nobreak-space");
26051 staticpro (&Qnobreak_space);
26052 Qimage = intern_c_string ("image");
26053 staticpro (&Qimage);
26054 Qtext = intern_c_string ("text");
26055 staticpro (&Qtext);
26056 Qboth = intern_c_string ("both");
26057 staticpro (&Qboth);
26058 Qboth_horiz = intern_c_string ("both-horiz");
26059 staticpro (&Qboth_horiz);
26060 Qtext_image_horiz = intern_c_string ("text-image-horiz");
26061 staticpro (&Qtext_image_horiz);
26062 QCmap = intern_c_string (":map");
26063 staticpro (&QCmap);
26064 QCpointer = intern_c_string (":pointer");
26065 staticpro (&QCpointer);
26066 Qrect = intern_c_string ("rect");
26067 staticpro (&Qrect);
26068 Qcircle = intern_c_string ("circle");
26069 staticpro (&Qcircle);
26070 Qpoly = intern_c_string ("poly");
26071 staticpro (&Qpoly);
26072 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
26073 staticpro (&Qmessage_truncate_lines);
26074 Qgrow_only = intern_c_string ("grow-only");
26075 staticpro (&Qgrow_only);
26076 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
26077 staticpro (&Qinhibit_menubar_update);
26078 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
26079 staticpro (&Qinhibit_eval_during_redisplay);
26080 Qposition = intern_c_string ("position");
26081 staticpro (&Qposition);
26082 Qbuffer_position = intern_c_string ("buffer-position");
26083 staticpro (&Qbuffer_position);
26084 Qobject = intern_c_string ("object");
26085 staticpro (&Qobject);
26086 Qbar = intern_c_string ("bar");
26087 staticpro (&Qbar);
26088 Qhbar = intern_c_string ("hbar");
26089 staticpro (&Qhbar);
26090 Qbox = intern_c_string ("box");
26091 staticpro (&Qbox);
26092 Qhollow = intern_c_string ("hollow");
26093 staticpro (&Qhollow);
26094 Qhand = intern_c_string ("hand");
26095 staticpro (&Qhand);
26096 Qarrow = intern_c_string ("arrow");
26097 staticpro (&Qarrow);
26098 Qtext = intern_c_string ("text");
26099 staticpro (&Qtext);
26100 Qrisky_local_variable = intern_c_string ("risky-local-variable");
26101 staticpro (&Qrisky_local_variable);
26102 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
26103 staticpro (&Qinhibit_free_realized_faces);
26104
26105 list_of_error = Fcons (Fcons (intern_c_string ("error"),
26106 Fcons (intern_c_string ("void-variable"), Qnil)),
26107 Qnil);
26108 staticpro (&list_of_error);
26109
26110 Qlast_arrow_position = intern_c_string ("last-arrow-position");
26111 staticpro (&Qlast_arrow_position);
26112 Qlast_arrow_string = intern_c_string ("last-arrow-string");
26113 staticpro (&Qlast_arrow_string);
26114
26115 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
26116 staticpro (&Qoverlay_arrow_string);
26117 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
26118 staticpro (&Qoverlay_arrow_bitmap);
26119
26120 echo_buffer[0] = echo_buffer[1] = Qnil;
26121 staticpro (&echo_buffer[0]);
26122 staticpro (&echo_buffer[1]);
26123
26124 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
26125 staticpro (&echo_area_buffer[0]);
26126 staticpro (&echo_area_buffer[1]);
26127
26128 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
26129 staticpro (&Vmessages_buffer_name);
26130
26131 mode_line_proptrans_alist = Qnil;
26132 staticpro (&mode_line_proptrans_alist);
26133 mode_line_string_list = Qnil;
26134 staticpro (&mode_line_string_list);
26135 mode_line_string_face = Qnil;
26136 staticpro (&mode_line_string_face);
26137 mode_line_string_face_prop = Qnil;
26138 staticpro (&mode_line_string_face_prop);
26139 Vmode_line_unwind_vector = Qnil;
26140 staticpro (&Vmode_line_unwind_vector);
26141
26142 help_echo_string = Qnil;
26143 staticpro (&help_echo_string);
26144 help_echo_object = Qnil;
26145 staticpro (&help_echo_object);
26146 help_echo_window = Qnil;
26147 staticpro (&help_echo_window);
26148 previous_help_echo_string = Qnil;
26149 staticpro (&previous_help_echo_string);
26150 help_echo_pos = -1;
26151
26152 Qright_to_left = intern_c_string ("right-to-left");
26153 staticpro (&Qright_to_left);
26154 Qleft_to_right = intern_c_string ("left-to-right");
26155 staticpro (&Qleft_to_right);
26156
26157 #ifdef HAVE_WINDOW_SYSTEM
26158 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
26159 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
26160 For example, if a block cursor is over a tab, it will be drawn as
26161 wide as that tab on the display. */);
26162 x_stretch_cursor_p = 0;
26163 #endif
26164
26165 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
26166 doc: /* *Non-nil means highlight trailing whitespace.
26167 The face used for trailing whitespace is `trailing-whitespace'. */);
26168 Vshow_trailing_whitespace = Qnil;
26169
26170 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
26171 doc: /* *Control highlighting of nobreak space and soft hyphen.
26172 A value of t means highlight the character itself (for nobreak space,
26173 use face `nobreak-space').
26174 A value of nil means no highlighting.
26175 Other values mean display the escape glyph followed by an ordinary
26176 space or ordinary hyphen. */);
26177 Vnobreak_char_display = Qt;
26178
26179 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
26180 doc: /* *The pointer shape to show in void text areas.
26181 A value of nil means to show the text pointer. Other options are `arrow',
26182 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
26183 Vvoid_text_area_pointer = Qarrow;
26184
26185 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
26186 doc: /* Non-nil means don't actually do any redisplay.
26187 This is used for internal purposes. */);
26188 Vinhibit_redisplay = Qnil;
26189
26190 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
26191 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
26192 Vglobal_mode_string = Qnil;
26193
26194 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
26195 doc: /* Marker for where to display an arrow on top of the buffer text.
26196 This must be the beginning of a line in order to work.
26197 See also `overlay-arrow-string'. */);
26198 Voverlay_arrow_position = Qnil;
26199
26200 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
26201 doc: /* String to display as an arrow in non-window frames.
26202 See also `overlay-arrow-position'. */);
26203 Voverlay_arrow_string = make_pure_c_string ("=>");
26204
26205 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
26206 doc: /* List of variables (symbols) which hold markers for overlay arrows.
26207 The symbols on this list are examined during redisplay to determine
26208 where to display overlay arrows. */);
26209 Voverlay_arrow_variable_list
26210 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
26211
26212 DEFVAR_INT ("scroll-step", &scroll_step,
26213 doc: /* *The number of lines to try scrolling a window by when point moves out.
26214 If that fails to bring point back on frame, point is centered instead.
26215 If this is zero, point is always centered after it moves off frame.
26216 If you want scrolling to always be a line at a time, you should set
26217 `scroll-conservatively' to a large value rather than set this to 1. */);
26218
26219 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
26220 doc: /* *Scroll up to this many lines, to bring point back on screen.
26221 If point moves off-screen, redisplay will scroll by up to
26222 `scroll-conservatively' lines in order to bring point just barely
26223 onto the screen again. If that cannot be done, then redisplay
26224 recenters point as usual.
26225
26226 A value of zero means always recenter point if it moves off screen. */);
26227 scroll_conservatively = 0;
26228
26229 DEFVAR_INT ("scroll-margin", &scroll_margin,
26230 doc: /* *Number of lines of margin at the top and bottom of a window.
26231 Recenter the window whenever point gets within this many lines
26232 of the top or bottom of the window. */);
26233 scroll_margin = 0;
26234
26235 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
26236 doc: /* Pixels per inch value for non-window system displays.
26237 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
26238 Vdisplay_pixels_per_inch = make_float (72.0);
26239
26240 #if GLYPH_DEBUG
26241 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
26242 #endif
26243
26244 DEFVAR_LISP ("truncate-partial-width-windows",
26245 &Vtruncate_partial_width_windows,
26246 doc: /* Non-nil means truncate lines in windows narrower than the frame.
26247 For an integer value, truncate lines in each window narrower than the
26248 full frame width, provided the window width is less than that integer;
26249 otherwise, respect the value of `truncate-lines'.
26250
26251 For any other non-nil value, truncate lines in all windows that do
26252 not span the full frame width.
26253
26254 A value of nil means to respect the value of `truncate-lines'.
26255
26256 If `word-wrap' is enabled, you might want to reduce this. */);
26257 Vtruncate_partial_width_windows = make_number (50);
26258
26259 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
26260 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
26261 Any other value means to use the appropriate face, `mode-line',
26262 `header-line', or `menu' respectively. */);
26263 mode_line_inverse_video = 1;
26264
26265 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
26266 doc: /* *Maximum buffer size for which line number should be displayed.
26267 If the buffer is bigger than this, the line number does not appear
26268 in the mode line. A value of nil means no limit. */);
26269 Vline_number_display_limit = Qnil;
26270
26271 DEFVAR_INT ("line-number-display-limit-width",
26272 &line_number_display_limit_width,
26273 doc: /* *Maximum line width (in characters) for line number display.
26274 If the average length of the lines near point is bigger than this, then the
26275 line number may be omitted from the mode line. */);
26276 line_number_display_limit_width = 200;
26277
26278 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
26279 doc: /* *Non-nil means highlight region even in nonselected windows. */);
26280 highlight_nonselected_windows = 0;
26281
26282 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
26283 doc: /* Non-nil if more than one frame is visible on this display.
26284 Minibuffer-only frames don't count, but iconified frames do.
26285 This variable is not guaranteed to be accurate except while processing
26286 `frame-title-format' and `icon-title-format'. */);
26287
26288 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
26289 doc: /* Template for displaying the title bar of visible frames.
26290 \(Assuming the window manager supports this feature.)
26291
26292 This variable has the same structure as `mode-line-format', except that
26293 the %c and %l constructs are ignored. It is used only on frames for
26294 which no explicit name has been set \(see `modify-frame-parameters'). */);
26295
26296 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
26297 doc: /* Template for displaying the title bar of an iconified frame.
26298 \(Assuming the window manager supports this feature.)
26299 This variable has the same structure as `mode-line-format' (which see),
26300 and is used only on frames for which no explicit name has been set
26301 \(see `modify-frame-parameters'). */);
26302 Vicon_title_format
26303 = Vframe_title_format
26304 = pure_cons (intern_c_string ("multiple-frames"),
26305 pure_cons (make_pure_c_string ("%b"),
26306 pure_cons (pure_cons (empty_unibyte_string,
26307 pure_cons (intern_c_string ("invocation-name"),
26308 pure_cons (make_pure_c_string ("@"),
26309 pure_cons (intern_c_string ("system-name"),
26310 Qnil)))),
26311 Qnil)));
26312
26313 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
26314 doc: /* Maximum number of lines to keep in the message log buffer.
26315 If nil, disable message logging. If t, log messages but don't truncate
26316 the buffer when it becomes large. */);
26317 Vmessage_log_max = make_number (100);
26318
26319 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
26320 doc: /* Functions called before redisplay, if window sizes have changed.
26321 The value should be a list of functions that take one argument.
26322 Just before redisplay, for each frame, if any of its windows have changed
26323 size since the last redisplay, or have been split or deleted,
26324 all the functions in the list are called, with the frame as argument. */);
26325 Vwindow_size_change_functions = Qnil;
26326
26327 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
26328 doc: /* List of functions to call before redisplaying a window with scrolling.
26329 Each function is called with two arguments, the window and its new
26330 display-start position. Note that these functions are also called by
26331 `set-window-buffer'. Also note that the value of `window-end' is not
26332 valid when these functions are called. */);
26333 Vwindow_scroll_functions = Qnil;
26334
26335 DEFVAR_LISP ("window-text-change-functions",
26336 &Vwindow_text_change_functions,
26337 doc: /* Functions to call in redisplay when text in the window might change. */);
26338 Vwindow_text_change_functions = Qnil;
26339
26340 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
26341 doc: /* Functions called when redisplay of a window reaches the end trigger.
26342 Each function is called with two arguments, the window and the end trigger value.
26343 See `set-window-redisplay-end-trigger'. */);
26344 Vredisplay_end_trigger_functions = Qnil;
26345
26346 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
26347 doc: /* *Non-nil means autoselect window with mouse pointer.
26348 If nil, do not autoselect windows.
26349 A positive number means delay autoselection by that many seconds: a
26350 window is autoselected only after the mouse has remained in that
26351 window for the duration of the delay.
26352 A negative number has a similar effect, but causes windows to be
26353 autoselected only after the mouse has stopped moving. \(Because of
26354 the way Emacs compares mouse events, you will occasionally wait twice
26355 that time before the window gets selected.\)
26356 Any other value means to autoselect window instantaneously when the
26357 mouse pointer enters it.
26358
26359 Autoselection selects the minibuffer only if it is active, and never
26360 unselects the minibuffer if it is active.
26361
26362 When customizing this variable make sure that the actual value of
26363 `focus-follows-mouse' matches the behavior of your window manager. */);
26364 Vmouse_autoselect_window = Qnil;
26365
26366 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
26367 doc: /* *Non-nil means automatically resize tool-bars.
26368 This dynamically changes the tool-bar's height to the minimum height
26369 that is needed to make all tool-bar items visible.
26370 If value is `grow-only', the tool-bar's height is only increased
26371 automatically; to decrease the tool-bar height, use \\[recenter]. */);
26372 Vauto_resize_tool_bars = Qt;
26373
26374 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
26375 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
26376 auto_raise_tool_bar_buttons_p = 1;
26377
26378 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
26379 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
26380 make_cursor_line_fully_visible_p = 1;
26381
26382 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
26383 doc: /* *Border below tool-bar in pixels.
26384 If an integer, use it as the height of the border.
26385 If it is one of `internal-border-width' or `border-width', use the
26386 value of the corresponding frame parameter.
26387 Otherwise, no border is added below the tool-bar. */);
26388 Vtool_bar_border = Qinternal_border_width;
26389
26390 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
26391 doc: /* *Margin around tool-bar buttons in pixels.
26392 If an integer, use that for both horizontal and vertical margins.
26393 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
26394 HORZ specifying the horizontal margin, and VERT specifying the
26395 vertical margin. */);
26396 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
26397
26398 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
26399 doc: /* *Relief thickness of tool-bar buttons. */);
26400 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
26401
26402 DEFVAR_LISP ("tool-bar-style", &Vtool_bar_style,
26403 doc: /* *Tool bar style to use.
26404 It can be one of
26405 image - show images only
26406 text - show text only
26407 both - show both, text below image
26408 both-horiz - show text to the right of the image
26409 text-image-horiz - show text to the left of the image
26410 any other - use system default or image if no system default. */);
26411 Vtool_bar_style = Qnil;
26412
26413 DEFVAR_INT ("tool-bar-max-label-size", &tool_bar_max_label_size,
26414 doc: /* *Maximum number of characters a label can have to be shown.
26415 The tool bar style must also show labels for this to have any effect, see
26416 `tool-bar-style'. */);
26417 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
26418
26419 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
26420 doc: /* List of functions to call to fontify regions of text.
26421 Each function is called with one argument POS. Functions must
26422 fontify a region starting at POS in the current buffer, and give
26423 fontified regions the property `fontified'. */);
26424 Vfontification_functions = Qnil;
26425 Fmake_variable_buffer_local (Qfontification_functions);
26426
26427 DEFVAR_BOOL ("unibyte-display-via-language-environment",
26428 &unibyte_display_via_language_environment,
26429 doc: /* *Non-nil means display unibyte text according to language environment.
26430 Specifically, this means that raw bytes in the range 160-255 decimal
26431 are displayed by converting them to the equivalent multibyte characters
26432 according to the current language environment. As a result, they are
26433 displayed according to the current fontset.
26434
26435 Note that this variable affects only how these bytes are displayed,
26436 but does not change the fact they are interpreted as raw bytes. */);
26437 unibyte_display_via_language_environment = 0;
26438
26439 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
26440 doc: /* *Maximum height for resizing mini-windows.
26441 If a float, it specifies a fraction of the mini-window frame's height.
26442 If an integer, it specifies a number of lines. */);
26443 Vmax_mini_window_height = make_float (0.25);
26444
26445 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
26446 doc: /* *How to resize mini-windows.
26447 A value of nil means don't automatically resize mini-windows.
26448 A value of t means resize them to fit the text displayed in them.
26449 A value of `grow-only', the default, means let mini-windows grow
26450 only, until their display becomes empty, at which point the windows
26451 go back to their normal size. */);
26452 Vresize_mini_windows = Qgrow_only;
26453
26454 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
26455 doc: /* Alist specifying how to blink the cursor off.
26456 Each element has the form (ON-STATE . OFF-STATE). Whenever the
26457 `cursor-type' frame-parameter or variable equals ON-STATE,
26458 comparing using `equal', Emacs uses OFF-STATE to specify
26459 how to blink it off. ON-STATE and OFF-STATE are values for
26460 the `cursor-type' frame parameter.
26461
26462 If a frame's ON-STATE has no entry in this list,
26463 the frame's other specifications determine how to blink the cursor off. */);
26464 Vblink_cursor_alist = Qnil;
26465
26466 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
26467 doc: /* Allow or disallow automatic horizontal scrolling of windows.
26468 If non-nil, windows are automatically scrolled horizontally to make
26469 point visible. */);
26470 automatic_hscrolling_p = 1;
26471 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
26472 staticpro (&Qauto_hscroll_mode);
26473
26474 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
26475 doc: /* *How many columns away from the window edge point is allowed to get
26476 before automatic hscrolling will horizontally scroll the window. */);
26477 hscroll_margin = 5;
26478
26479 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
26480 doc: /* *How many columns to scroll the window when point gets too close to the edge.
26481 When point is less than `hscroll-margin' columns from the window
26482 edge, automatic hscrolling will scroll the window by the amount of columns
26483 determined by this variable. If its value is a positive integer, scroll that
26484 many columns. If it's a positive floating-point number, it specifies the
26485 fraction of the window's width to scroll. If it's nil or zero, point will be
26486 centered horizontally after the scroll. Any other value, including negative
26487 numbers, are treated as if the value were zero.
26488
26489 Automatic hscrolling always moves point outside the scroll margin, so if
26490 point was more than scroll step columns inside the margin, the window will
26491 scroll more than the value given by the scroll step.
26492
26493 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26494 and `scroll-right' overrides this variable's effect. */);
26495 Vhscroll_step = make_number (0);
26496
26497 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
26498 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26499 Bind this around calls to `message' to let it take effect. */);
26500 message_truncate_lines = 0;
26501
26502 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
26503 doc: /* Normal hook run to update the menu bar definitions.
26504 Redisplay runs this hook before it redisplays the menu bar.
26505 This is used to update submenus such as Buffers,
26506 whose contents depend on various data. */);
26507 Vmenu_bar_update_hook = Qnil;
26508
26509 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
26510 doc: /* Frame for which we are updating a menu.
26511 The enable predicate for a menu binding should check this variable. */);
26512 Vmenu_updating_frame = Qnil;
26513
26514 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
26515 doc: /* Non-nil means don't update menu bars. Internal use only. */);
26516 inhibit_menubar_update = 0;
26517
26518 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
26519 doc: /* Prefix prepended to all continuation lines at display time.
26520 The value may be a string, an image, or a stretch-glyph; it is
26521 interpreted in the same way as the value of a `display' text property.
26522
26523 This variable is overridden by any `wrap-prefix' text or overlay
26524 property.
26525
26526 To add a prefix to non-continuation lines, use `line-prefix'. */);
26527 Vwrap_prefix = Qnil;
26528 staticpro (&Qwrap_prefix);
26529 Qwrap_prefix = intern_c_string ("wrap-prefix");
26530 Fmake_variable_buffer_local (Qwrap_prefix);
26531
26532 DEFVAR_LISP ("line-prefix", &Vline_prefix,
26533 doc: /* Prefix prepended to all non-continuation lines at display time.
26534 The value may be a string, an image, or a stretch-glyph; it is
26535 interpreted in the same way as the value of a `display' text property.
26536
26537 This variable is overridden by any `line-prefix' text or overlay
26538 property.
26539
26540 To add a prefix to continuation lines, use `wrap-prefix'. */);
26541 Vline_prefix = Qnil;
26542 staticpro (&Qline_prefix);
26543 Qline_prefix = intern_c_string ("line-prefix");
26544 Fmake_variable_buffer_local (Qline_prefix);
26545
26546 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
26547 doc: /* Non-nil means don't eval Lisp during redisplay. */);
26548 inhibit_eval_during_redisplay = 0;
26549
26550 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
26551 doc: /* Non-nil means don't free realized faces. Internal use only. */);
26552 inhibit_free_realized_faces = 0;
26553
26554 #if GLYPH_DEBUG
26555 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
26556 doc: /* Inhibit try_window_id display optimization. */);
26557 inhibit_try_window_id = 0;
26558
26559 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
26560 doc: /* Inhibit try_window_reusing display optimization. */);
26561 inhibit_try_window_reusing = 0;
26562
26563 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
26564 doc: /* Inhibit try_cursor_movement display optimization. */);
26565 inhibit_try_cursor_movement = 0;
26566 #endif /* GLYPH_DEBUG */
26567
26568 DEFVAR_INT ("overline-margin", &overline_margin,
26569 doc: /* *Space between overline and text, in pixels.
26570 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
26571 margin to the caracter height. */);
26572 overline_margin = 2;
26573
26574 DEFVAR_INT ("underline-minimum-offset",
26575 &underline_minimum_offset,
26576 doc: /* Minimum distance between baseline and underline.
26577 This can improve legibility of underlined text at small font sizes,
26578 particularly when using variable `x-use-underline-position-properties'
26579 with fonts that specify an UNDERLINE_POSITION relatively close to the
26580 baseline. The default value is 1. */);
26581 underline_minimum_offset = 1;
26582
26583 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
26584 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
26585 This feature only works when on a window system that can change
26586 cursor shapes. */);
26587 display_hourglass_p = 1;
26588
26589 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
26590 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
26591 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
26592
26593 hourglass_atimer = NULL;
26594 hourglass_shown_p = 0;
26595 }
26596
26597
26598 /* Initialize this module when Emacs starts. */
26599
26600 void
26601 init_xdisp (void)
26602 {
26603 Lisp_Object root_window;
26604 struct window *mini_w;
26605
26606 current_header_line_height = current_mode_line_height = -1;
26607
26608 CHARPOS (this_line_start_pos) = 0;
26609
26610 mini_w = XWINDOW (minibuf_window);
26611 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
26612
26613 if (!noninteractive)
26614 {
26615 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
26616 int i;
26617
26618 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
26619 set_window_height (root_window,
26620 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
26621 0);
26622 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
26623 set_window_height (minibuf_window, 1, 0);
26624
26625 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
26626 mini_w->total_cols = make_number (FRAME_COLS (f));
26627
26628 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
26629 scratch_glyph_row.glyphs[TEXT_AREA + 1]
26630 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
26631
26632 /* The default ellipsis glyphs `...'. */
26633 for (i = 0; i < 3; ++i)
26634 default_invis_vector[i] = make_number ('.');
26635 }
26636
26637 {
26638 /* Allocate the buffer for frame titles.
26639 Also used for `format-mode-line'. */
26640 int size = 100;
26641 mode_line_noprop_buf = (char *) xmalloc (size);
26642 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
26643 mode_line_noprop_ptr = mode_line_noprop_buf;
26644 mode_line_target = MODE_LINE_DISPLAY;
26645 }
26646
26647 help_echo_showing_p = 0;
26648 }
26649
26650 /* Since w32 does not support atimers, it defines its own implementation of
26651 the following three functions in w32fns.c. */
26652 #ifndef WINDOWSNT
26653
26654 /* Platform-independent portion of hourglass implementation. */
26655
26656 /* Return non-zero if houglass timer has been started or hourglass is shown. */
26657 int
26658 hourglass_started (void)
26659 {
26660 return hourglass_shown_p || hourglass_atimer != NULL;
26661 }
26662
26663 /* Cancel a currently active hourglass timer, and start a new one. */
26664 void
26665 start_hourglass (void)
26666 {
26667 #if defined (HAVE_WINDOW_SYSTEM)
26668 EMACS_TIME delay;
26669 int secs, usecs = 0;
26670
26671 cancel_hourglass ();
26672
26673 if (INTEGERP (Vhourglass_delay)
26674 && XINT (Vhourglass_delay) > 0)
26675 secs = XFASTINT (Vhourglass_delay);
26676 else if (FLOATP (Vhourglass_delay)
26677 && XFLOAT_DATA (Vhourglass_delay) > 0)
26678 {
26679 Lisp_Object tem;
26680 tem = Ftruncate (Vhourglass_delay, Qnil);
26681 secs = XFASTINT (tem);
26682 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
26683 }
26684 else
26685 secs = DEFAULT_HOURGLASS_DELAY;
26686
26687 EMACS_SET_SECS_USECS (delay, secs, usecs);
26688 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
26689 show_hourglass, NULL);
26690 #endif
26691 }
26692
26693
26694 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
26695 shown. */
26696 void
26697 cancel_hourglass (void)
26698 {
26699 #if defined (HAVE_WINDOW_SYSTEM)
26700 if (hourglass_atimer)
26701 {
26702 cancel_atimer (hourglass_atimer);
26703 hourglass_atimer = NULL;
26704 }
26705
26706 if (hourglass_shown_p)
26707 hide_hourglass ();
26708 #endif
26709 }
26710 #endif /* ! WINDOWSNT */
26711