]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(get_next_display_element): Display codes 8a0 and 8ad
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03,04
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
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. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
202
203 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
206
207 #define INFINITY 10000000
208
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
214
215 extern int interrupt_input;
216 extern int command_loop_level;
217
218 extern Lisp_Object do_mouse_tracking;
219
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
222
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
225
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
231
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
241
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
244
245 /* Pointer shapes */
246 Lisp_Object Qarrow, Qhand, Qtext;
247
248 Lisp_Object Qrisky_local_variable;
249
250 /* Holds the list (error). */
251 Lisp_Object list_of_error;
252
253 /* Functions called to fontify regions of text. */
254
255 Lisp_Object Vfontification_functions;
256 Lisp_Object Qfontification_functions;
257
258 /* Non-zero means automatically select any window when the mouse
259 cursor moves into it. */
260 int mouse_autoselect_window;
261
262 /* Non-zero means draw tool bar buttons raised when the mouse moves
263 over them. */
264
265 int auto_raise_tool_bar_buttons_p;
266
267 /* Non-zero means to reposition window if cursor line is only partially visible. */
268
269 int make_cursor_line_fully_visible_p;
270
271 /* Margin around tool bar buttons in pixels. */
272
273 Lisp_Object Vtool_bar_button_margin;
274
275 /* Thickness of shadow to draw around tool bar buttons. */
276
277 EMACS_INT tool_bar_button_relief;
278
279 /* Non-zero means automatically resize tool-bars so that all tool-bar
280 items are visible, and no blank lines remain. */
281
282 int auto_resize_tool_bars_p;
283
284 /* Non-zero means draw block and hollow cursor as wide as the glyph
285 under it. For example, if a block cursor is over a tab, it will be
286 drawn as wide as that tab on the display. */
287
288 int x_stretch_cursor_p;
289
290 /* Non-nil means don't actually do any redisplay. */
291
292 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
293
294 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
295
296 int inhibit_eval_during_redisplay;
297
298 /* Names of text properties relevant for redisplay. */
299
300 Lisp_Object Qdisplay;
301 extern Lisp_Object Qface, Qinvisible, Qwidth;
302
303 /* Symbols used in text property values. */
304
305 Lisp_Object Vdisplay_pixels_per_inch;
306 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
307 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
308 Lisp_Object Qslice;
309 Lisp_Object Qcenter;
310 Lisp_Object Qmargin, Qpointer;
311 Lisp_Object Qline_height, Qtotal;
312 extern Lisp_Object Qheight;
313 extern Lisp_Object QCwidth, QCheight, QCascent;
314 extern Lisp_Object Qscroll_bar;
315 extern Lisp_Object Qcursor;
316
317 /* Non-nil means highlight trailing whitespace. */
318
319 Lisp_Object Vshow_trailing_whitespace;
320
321 #ifdef HAVE_WINDOW_SYSTEM
322 extern Lisp_Object Voverflow_newline_into_fringe;
323
324 /* Test if overflow newline into fringe. Called with iterator IT
325 at or past right window margin, and with IT->current_x set. */
326
327 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
328 (!NILP (Voverflow_newline_into_fringe) \
329 && FRAME_WINDOW_P (it->f) \
330 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
331 && it->current_x == it->last_visible_x)
332
333 #endif /* HAVE_WINDOW_SYSTEM */
334
335 /* Non-nil means show the text cursor in void text areas
336 i.e. in blank areas after eol and eob. This used to be
337 the default in 21.3. */
338
339 Lisp_Object Vvoid_text_area_pointer;
340
341 /* Name of the face used to highlight trailing whitespace. */
342
343 Lisp_Object Qtrailing_whitespace;
344
345 /* Name and number of the face used to highlight escape glyphs. */
346
347 Lisp_Object Qescape_glyph;
348 int escape_glyph_face;
349
350 /* The symbol `image' which is the car of the lists used to represent
351 images in Lisp. */
352
353 Lisp_Object Qimage;
354
355 /* The image map types. */
356 Lisp_Object QCmap, QCpointer;
357 Lisp_Object Qrect, Qcircle, Qpoly;
358
359 /* Non-zero means print newline to stdout before next mini-buffer
360 message. */
361
362 int noninteractive_need_newline;
363
364 /* Non-zero means print newline to message log before next message. */
365
366 static int message_log_need_newline;
367
368 /* Three markers that message_dolog uses.
369 It could allocate them itself, but that causes trouble
370 in handling memory-full errors. */
371 static Lisp_Object message_dolog_marker1;
372 static Lisp_Object message_dolog_marker2;
373 static Lisp_Object message_dolog_marker3;
374 \f
375 /* The buffer position of the first character appearing entirely or
376 partially on the line of the selected window which contains the
377 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
378 redisplay optimization in redisplay_internal. */
379
380 static struct text_pos this_line_start_pos;
381
382 /* Number of characters past the end of the line above, including the
383 terminating newline. */
384
385 static struct text_pos this_line_end_pos;
386
387 /* The vertical positions and the height of this line. */
388
389 static int this_line_vpos;
390 static int this_line_y;
391 static int this_line_pixel_height;
392
393 /* X position at which this display line starts. Usually zero;
394 negative if first character is partially visible. */
395
396 static int this_line_start_x;
397
398 /* Buffer that this_line_.* variables are referring to. */
399
400 static struct buffer *this_line_buffer;
401
402 /* Nonzero means truncate lines in all windows less wide than the
403 frame. */
404
405 int truncate_partial_width_windows;
406
407 /* A flag to control how to display unibyte 8-bit character. */
408
409 int unibyte_display_via_language_environment;
410
411 /* Nonzero means we have more than one non-mini-buffer-only frame.
412 Not guaranteed to be accurate except while parsing
413 frame-title-format. */
414
415 int multiple_frames;
416
417 Lisp_Object Vglobal_mode_string;
418
419
420 /* List of variables (symbols) which hold markers for overlay arrows.
421 The symbols on this list are examined during redisplay to determine
422 where to display overlay arrows. */
423
424 Lisp_Object Voverlay_arrow_variable_list;
425
426 /* Marker for where to display an arrow on top of the buffer text. */
427
428 Lisp_Object Voverlay_arrow_position;
429
430 /* String to display for the arrow. Only used on terminal frames. */
431
432 Lisp_Object Voverlay_arrow_string;
433
434 /* Values of those variables at last redisplay are stored as
435 properties on `overlay-arrow-position' symbol. However, if
436 Voverlay_arrow_position is a marker, last-arrow-position is its
437 numerical position. */
438
439 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
440
441 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
442 properties on a symbol in overlay-arrow-variable-list. */
443
444 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
445
446 /* Like mode-line-format, but for the title bar on a visible frame. */
447
448 Lisp_Object Vframe_title_format;
449
450 /* Like mode-line-format, but for the title bar on an iconified frame. */
451
452 Lisp_Object Vicon_title_format;
453
454 /* List of functions to call when a window's size changes. These
455 functions get one arg, a frame on which one or more windows' sizes
456 have changed. */
457
458 static Lisp_Object Vwindow_size_change_functions;
459
460 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
461
462 /* Nonzero if overlay arrow has been displayed once in this window. */
463
464 static int overlay_arrow_seen;
465
466 /* Nonzero means highlight the region even in nonselected windows. */
467
468 int highlight_nonselected_windows;
469
470 /* If cursor motion alone moves point off frame, try scrolling this
471 many lines up or down if that will bring it back. */
472
473 static EMACS_INT scroll_step;
474
475 /* Nonzero means scroll just far enough to bring point back on the
476 screen, when appropriate. */
477
478 static EMACS_INT scroll_conservatively;
479
480 /* Recenter the window whenever point gets within this many lines of
481 the top or bottom of the window. This value is translated into a
482 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
483 that there is really a fixed pixel height scroll margin. */
484
485 EMACS_INT scroll_margin;
486
487 /* Number of windows showing the buffer of the selected window (or
488 another buffer with the same base buffer). keyboard.c refers to
489 this. */
490
491 int buffer_shared;
492
493 /* Vector containing glyphs for an ellipsis `...'. */
494
495 static Lisp_Object default_invis_vector[3];
496
497 /* Zero means display the mode-line/header-line/menu-bar in the default face
498 (this slightly odd definition is for compatibility with previous versions
499 of emacs), non-zero means display them using their respective faces.
500
501 This variable is deprecated. */
502
503 int mode_line_inverse_video;
504
505 /* Prompt to display in front of the mini-buffer contents. */
506
507 Lisp_Object minibuf_prompt;
508
509 /* Width of current mini-buffer prompt. Only set after display_line
510 of the line that contains the prompt. */
511
512 int minibuf_prompt_width;
513
514 /* This is the window where the echo area message was displayed. It
515 is always a mini-buffer window, but it may not be the same window
516 currently active as a mini-buffer. */
517
518 Lisp_Object echo_area_window;
519
520 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
521 pushes the current message and the value of
522 message_enable_multibyte on the stack, the function restore_message
523 pops the stack and displays MESSAGE again. */
524
525 Lisp_Object Vmessage_stack;
526
527 /* Nonzero means multibyte characters were enabled when the echo area
528 message was specified. */
529
530 int message_enable_multibyte;
531
532 /* Nonzero if we should redraw the mode lines on the next redisplay. */
533
534 int update_mode_lines;
535
536 /* Nonzero if window sizes or contents have changed since last
537 redisplay that finished. */
538
539 int windows_or_buffers_changed;
540
541 /* Nonzero means a frame's cursor type has been changed. */
542
543 int cursor_type_changed;
544
545 /* Nonzero after display_mode_line if %l was used and it displayed a
546 line number. */
547
548 int line_number_displayed;
549
550 /* Maximum buffer size for which to display line numbers. */
551
552 Lisp_Object Vline_number_display_limit;
553
554 /* Line width to consider when repositioning for line number display. */
555
556 static EMACS_INT line_number_display_limit_width;
557
558 /* Number of lines to keep in the message log buffer. t means
559 infinite. nil means don't log at all. */
560
561 Lisp_Object Vmessage_log_max;
562
563 /* The name of the *Messages* buffer, a string. */
564
565 static Lisp_Object Vmessages_buffer_name;
566
567 /* Current, index 0, and last displayed echo area message. Either
568 buffers from echo_buffers, or nil to indicate no message. */
569
570 Lisp_Object echo_area_buffer[2];
571
572 /* The buffers referenced from echo_area_buffer. */
573
574 static Lisp_Object echo_buffer[2];
575
576 /* A vector saved used in with_area_buffer to reduce consing. */
577
578 static Lisp_Object Vwith_echo_area_save_vector;
579
580 /* Non-zero means display_echo_area should display the last echo area
581 message again. Set by redisplay_preserve_echo_area. */
582
583 static int display_last_displayed_message_p;
584
585 /* Nonzero if echo area is being used by print; zero if being used by
586 message. */
587
588 int message_buf_print;
589
590 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
591
592 Lisp_Object Qinhibit_menubar_update;
593 int inhibit_menubar_update;
594
595 /* Maximum height for resizing mini-windows. Either a float
596 specifying a fraction of the available height, or an integer
597 specifying a number of lines. */
598
599 Lisp_Object Vmax_mini_window_height;
600
601 /* Non-zero means messages should be displayed with truncated
602 lines instead of being continued. */
603
604 int message_truncate_lines;
605 Lisp_Object Qmessage_truncate_lines;
606
607 /* Set to 1 in clear_message to make redisplay_internal aware
608 of an emptied echo area. */
609
610 static int message_cleared_p;
611
612 /* Non-zero means we want a hollow cursor in windows that are not
613 selected. Zero means there's no cursor in such windows. */
614
615 Lisp_Object Vcursor_in_non_selected_windows;
616 Lisp_Object Qcursor_in_non_selected_windows;
617
618 /* How to blink the default frame cursor off. */
619 Lisp_Object Vblink_cursor_alist;
620
621 /* A scratch glyph row with contents used for generating truncation
622 glyphs. Also used in direct_output_for_insert. */
623
624 #define MAX_SCRATCH_GLYPHS 100
625 struct glyph_row scratch_glyph_row;
626 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
627
628 /* Ascent and height of the last line processed by move_it_to. */
629
630 static int last_max_ascent, last_height;
631
632 /* Non-zero if there's a help-echo in the echo area. */
633
634 int help_echo_showing_p;
635
636 /* If >= 0, computed, exact values of mode-line and header-line height
637 to use in the macros CURRENT_MODE_LINE_HEIGHT and
638 CURRENT_HEADER_LINE_HEIGHT. */
639
640 int current_mode_line_height, current_header_line_height;
641
642 /* The maximum distance to look ahead for text properties. Values
643 that are too small let us call compute_char_face and similar
644 functions too often which is expensive. Values that are too large
645 let us call compute_char_face and alike too often because we
646 might not be interested in text properties that far away. */
647
648 #define TEXT_PROP_DISTANCE_LIMIT 100
649
650 #if GLYPH_DEBUG
651
652 /* Variables to turn off display optimizations from Lisp. */
653
654 int inhibit_try_window_id, inhibit_try_window_reusing;
655 int inhibit_try_cursor_movement;
656
657 /* Non-zero means print traces of redisplay if compiled with
658 GLYPH_DEBUG != 0. */
659
660 int trace_redisplay_p;
661
662 #endif /* GLYPH_DEBUG */
663
664 #ifdef DEBUG_TRACE_MOVE
665 /* Non-zero means trace with TRACE_MOVE to stderr. */
666 int trace_move;
667
668 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
669 #else
670 #define TRACE_MOVE(x) (void) 0
671 #endif
672
673 /* Non-zero means automatically scroll windows horizontally to make
674 point visible. */
675
676 int automatic_hscrolling_p;
677
678 /* How close to the margin can point get before the window is scrolled
679 horizontally. */
680 EMACS_INT hscroll_margin;
681
682 /* How much to scroll horizontally when point is inside the above margin. */
683 Lisp_Object Vhscroll_step;
684
685 /* The variable `resize-mini-windows'. If nil, don't resize
686 mini-windows. If t, always resize them to fit the text they
687 display. If `grow-only', let mini-windows grow only until they
688 become empty. */
689
690 Lisp_Object Vresize_mini_windows;
691
692 /* Buffer being redisplayed -- for redisplay_window_error. */
693
694 struct buffer *displayed_buffer;
695
696 /* Value returned from text property handlers (see below). */
697
698 enum prop_handled
699 {
700 HANDLED_NORMALLY,
701 HANDLED_RECOMPUTE_PROPS,
702 HANDLED_OVERLAY_STRING_CONSUMED,
703 HANDLED_RETURN
704 };
705
706 /* A description of text properties that redisplay is interested
707 in. */
708
709 struct props
710 {
711 /* The name of the property. */
712 Lisp_Object *name;
713
714 /* A unique index for the property. */
715 enum prop_idx idx;
716
717 /* A handler function called to set up iterator IT from the property
718 at IT's current position. Value is used to steer handle_stop. */
719 enum prop_handled (*handler) P_ ((struct it *it));
720 };
721
722 static enum prop_handled handle_face_prop P_ ((struct it *));
723 static enum prop_handled handle_invisible_prop P_ ((struct it *));
724 static enum prop_handled handle_display_prop P_ ((struct it *));
725 static enum prop_handled handle_composition_prop P_ ((struct it *));
726 static enum prop_handled handle_overlay_change P_ ((struct it *));
727 static enum prop_handled handle_fontified_prop P_ ((struct it *));
728
729 /* Properties handled by iterators. */
730
731 static struct props it_props[] =
732 {
733 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
734 /* Handle `face' before `display' because some sub-properties of
735 `display' need to know the face. */
736 {&Qface, FACE_PROP_IDX, handle_face_prop},
737 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
738 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
739 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
740 {NULL, 0, NULL}
741 };
742
743 /* Value is the position described by X. If X is a marker, value is
744 the marker_position of X. Otherwise, value is X. */
745
746 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
747
748 /* Enumeration returned by some move_it_.* functions internally. */
749
750 enum move_it_result
751 {
752 /* Not used. Undefined value. */
753 MOVE_UNDEFINED,
754
755 /* Move ended at the requested buffer position or ZV. */
756 MOVE_POS_MATCH_OR_ZV,
757
758 /* Move ended at the requested X pixel position. */
759 MOVE_X_REACHED,
760
761 /* Move within a line ended at the end of a line that must be
762 continued. */
763 MOVE_LINE_CONTINUED,
764
765 /* Move within a line ended at the end of a line that would
766 be displayed truncated. */
767 MOVE_LINE_TRUNCATED,
768
769 /* Move within a line ended at a line end. */
770 MOVE_NEWLINE_OR_CR
771 };
772
773 /* This counter is used to clear the face cache every once in a while
774 in redisplay_internal. It is incremented for each redisplay.
775 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
776 cleared. */
777
778 #define CLEAR_FACE_CACHE_COUNT 500
779 static int clear_face_cache_count;
780
781 /* Record the previous terminal frame we displayed. */
782
783 static struct frame *previous_terminal_frame;
784
785 /* Non-zero while redisplay_internal is in progress. */
786
787 int redisplaying_p;
788
789 /* Non-zero means don't free realized faces. Bound while freeing
790 realized faces is dangerous because glyph matrices might still
791 reference them. */
792
793 int inhibit_free_realized_faces;
794 Lisp_Object Qinhibit_free_realized_faces;
795
796 /* If a string, XTread_socket generates an event to display that string.
797 (The display is done in read_char.) */
798
799 Lisp_Object help_echo_string;
800 Lisp_Object help_echo_window;
801 Lisp_Object help_echo_object;
802 int help_echo_pos;
803
804 /* Temporary variable for XTread_socket. */
805
806 Lisp_Object previous_help_echo_string;
807
808 /* Null glyph slice */
809
810 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
811
812 \f
813 /* Function prototypes. */
814
815 static void setup_for_ellipsis P_ ((struct it *, int));
816 static void mark_window_display_accurate_1 P_ ((struct window *, int));
817 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
818 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
819 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
820 static int redisplay_mode_lines P_ ((Lisp_Object, int));
821 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
822
823 #if 0
824 static int invisible_text_between_p P_ ((struct it *, int, int));
825 #endif
826
827 static int next_element_from_ellipsis P_ ((struct it *));
828 static void pint2str P_ ((char *, int, int));
829 static void pint2hrstr P_ ((char *, int, int));
830 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
831 struct text_pos));
832 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
833 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
834 static void store_frame_title_char P_ ((char));
835 static int store_frame_title P_ ((const unsigned char *, int, int));
836 static void x_consider_frame_title P_ ((Lisp_Object));
837 static void handle_stop P_ ((struct it *));
838 static int tool_bar_lines_needed P_ ((struct frame *));
839 static int single_display_prop_intangible_p P_ ((Lisp_Object));
840 static void ensure_echo_area_buffers P_ ((void));
841 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
842 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
843 static int with_echo_area_buffer P_ ((struct window *, int,
844 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
845 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
846 static void clear_garbaged_frames P_ ((void));
847 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
848 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
849 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
850 static int display_echo_area P_ ((struct window *));
851 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
852 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
853 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
854 static int string_char_and_length P_ ((const unsigned char *, int, int *));
855 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
856 struct text_pos));
857 static int compute_window_start_on_continuation_line P_ ((struct window *));
858 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
859 static void insert_left_trunc_glyphs P_ ((struct it *));
860 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
861 Lisp_Object));
862 static void extend_face_to_end_of_line P_ ((struct it *));
863 static int append_space_for_newline P_ ((struct it *, int));
864 static int make_cursor_line_fully_visible P_ ((struct window *, int));
865 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
866 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
867 static int trailing_whitespace_p P_ ((int));
868 static int message_log_check_duplicate P_ ((int, int, int, int));
869 static void push_it P_ ((struct it *));
870 static void pop_it P_ ((struct it *));
871 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
872 static void select_frame_for_redisplay P_ ((Lisp_Object));
873 static void redisplay_internal P_ ((int));
874 static int echo_area_display P_ ((int));
875 static void redisplay_windows P_ ((Lisp_Object));
876 static void redisplay_window P_ ((Lisp_Object, int));
877 static Lisp_Object redisplay_window_error ();
878 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
879 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
880 static void update_menu_bar P_ ((struct frame *, int));
881 static int try_window_reusing_current_matrix P_ ((struct window *));
882 static int try_window_id P_ ((struct window *));
883 static int display_line P_ ((struct it *));
884 static int display_mode_lines P_ ((struct window *));
885 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
886 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
887 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
888 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
889 static void display_menu_bar P_ ((struct window *));
890 static int display_count_lines P_ ((int, int, int, int, int *));
891 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
892 int, int, struct it *, int, int, int, int));
893 static void compute_line_metrics P_ ((struct it *));
894 static void run_redisplay_end_trigger_hook P_ ((struct it *));
895 static int get_overlay_strings P_ ((struct it *, int));
896 static void next_overlay_string P_ ((struct it *));
897 static void reseat P_ ((struct it *, struct text_pos, int));
898 static void reseat_1 P_ ((struct it *, struct text_pos, int));
899 static void back_to_previous_visible_line_start P_ ((struct it *));
900 void reseat_at_previous_visible_line_start P_ ((struct it *));
901 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
902 static int next_element_from_display_vector P_ ((struct it *));
903 static int next_element_from_string P_ ((struct it *));
904 static int next_element_from_c_string P_ ((struct it *));
905 static int next_element_from_buffer P_ ((struct it *));
906 static int next_element_from_composition P_ ((struct it *));
907 static int next_element_from_image P_ ((struct it *));
908 static int next_element_from_stretch P_ ((struct it *));
909 static void load_overlay_strings P_ ((struct it *, int));
910 static int init_from_display_pos P_ ((struct it *, struct window *,
911 struct display_pos *));
912 static void reseat_to_string P_ ((struct it *, unsigned char *,
913 Lisp_Object, int, int, int, int));
914 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
915 int, int, int));
916 void move_it_vertically_backward P_ ((struct it *, int));
917 static void init_to_row_start P_ ((struct it *, struct window *,
918 struct glyph_row *));
919 static int init_to_row_end P_ ((struct it *, struct window *,
920 struct glyph_row *));
921 static void back_to_previous_line_start P_ ((struct it *));
922 static int forward_to_next_line_start P_ ((struct it *, int *));
923 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
924 Lisp_Object, int));
925 static struct text_pos string_pos P_ ((int, Lisp_Object));
926 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
927 static int number_of_chars P_ ((unsigned char *, int));
928 static void compute_stop_pos P_ ((struct it *));
929 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
930 Lisp_Object));
931 static int face_before_or_after_it_pos P_ ((struct it *, int));
932 static int next_overlay_change P_ ((int));
933 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
934 Lisp_Object, struct text_pos *,
935 int));
936 static int underlying_face_id P_ ((struct it *));
937 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
938 struct window *));
939
940 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
941 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
942
943 #ifdef HAVE_WINDOW_SYSTEM
944
945 static void update_tool_bar P_ ((struct frame *, int));
946 static void build_desired_tool_bar_string P_ ((struct frame *f));
947 static int redisplay_tool_bar P_ ((struct frame *));
948 static void display_tool_bar_line P_ ((struct it *));
949 static void notice_overwritten_cursor P_ ((struct window *,
950 enum glyph_row_area,
951 int, int, int, int));
952
953
954
955 #endif /* HAVE_WINDOW_SYSTEM */
956
957 \f
958 /***********************************************************************
959 Window display dimensions
960 ***********************************************************************/
961
962 /* Return the bottom boundary y-position for text lines in window W.
963 This is the first y position at which a line cannot start.
964 It is relative to the top of the window.
965
966 This is the height of W minus the height of a mode line, if any. */
967
968 INLINE int
969 window_text_bottom_y (w)
970 struct window *w;
971 {
972 int height = WINDOW_TOTAL_HEIGHT (w);
973
974 if (WINDOW_WANTS_MODELINE_P (w))
975 height -= CURRENT_MODE_LINE_HEIGHT (w);
976 return height;
977 }
978
979 /* Return the pixel width of display area AREA of window W. AREA < 0
980 means return the total width of W, not including fringes to
981 the left and right of the window. */
982
983 INLINE int
984 window_box_width (w, area)
985 struct window *w;
986 int area;
987 {
988 int cols = XFASTINT (w->total_cols);
989 int pixels = 0;
990
991 if (!w->pseudo_window_p)
992 {
993 cols -= WINDOW_SCROLL_BAR_COLS (w);
994
995 if (area == TEXT_AREA)
996 {
997 if (INTEGERP (w->left_margin_cols))
998 cols -= XFASTINT (w->left_margin_cols);
999 if (INTEGERP (w->right_margin_cols))
1000 cols -= XFASTINT (w->right_margin_cols);
1001 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1002 }
1003 else if (area == LEFT_MARGIN_AREA)
1004 {
1005 cols = (INTEGERP (w->left_margin_cols)
1006 ? XFASTINT (w->left_margin_cols) : 0);
1007 pixels = 0;
1008 }
1009 else if (area == RIGHT_MARGIN_AREA)
1010 {
1011 cols = (INTEGERP (w->right_margin_cols)
1012 ? XFASTINT (w->right_margin_cols) : 0);
1013 pixels = 0;
1014 }
1015 }
1016
1017 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1018 }
1019
1020
1021 /* Return the pixel height of the display area of window W, not
1022 including mode lines of W, if any. */
1023
1024 INLINE int
1025 window_box_height (w)
1026 struct window *w;
1027 {
1028 struct frame *f = XFRAME (w->frame);
1029 int height = WINDOW_TOTAL_HEIGHT (w);
1030
1031 xassert (height >= 0);
1032
1033 /* Note: the code below that determines the mode-line/header-line
1034 height is essentially the same as that contained in the macro
1035 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1036 the appropriate glyph row has its `mode_line_p' flag set,
1037 and if it doesn't, uses estimate_mode_line_height instead. */
1038
1039 if (WINDOW_WANTS_MODELINE_P (w))
1040 {
1041 struct glyph_row *ml_row
1042 = (w->current_matrix && w->current_matrix->rows
1043 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1044 : 0);
1045 if (ml_row && ml_row->mode_line_p)
1046 height -= ml_row->height;
1047 else
1048 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1049 }
1050
1051 if (WINDOW_WANTS_HEADER_LINE_P (w))
1052 {
1053 struct glyph_row *hl_row
1054 = (w->current_matrix && w->current_matrix->rows
1055 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1056 : 0);
1057 if (hl_row && hl_row->mode_line_p)
1058 height -= hl_row->height;
1059 else
1060 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1061 }
1062
1063 /* With a very small font and a mode-line that's taller than
1064 default, we might end up with a negative height. */
1065 return max (0, height);
1066 }
1067
1068 /* Return the window-relative coordinate of the left edge of display
1069 area AREA of window W. AREA < 0 means return the left edge of the
1070 whole window, to the right of the left fringe of W. */
1071
1072 INLINE int
1073 window_box_left_offset (w, area)
1074 struct window *w;
1075 int area;
1076 {
1077 int x;
1078
1079 if (w->pseudo_window_p)
1080 return 0;
1081
1082 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1083
1084 if (area == TEXT_AREA)
1085 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1086 + window_box_width (w, LEFT_MARGIN_AREA));
1087 else if (area == RIGHT_MARGIN_AREA)
1088 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1089 + window_box_width (w, LEFT_MARGIN_AREA)
1090 + window_box_width (w, TEXT_AREA)
1091 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1092 ? 0
1093 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1094 else if (area == LEFT_MARGIN_AREA
1095 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1096 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1097
1098 return x;
1099 }
1100
1101
1102 /* Return the window-relative coordinate of the right edge of display
1103 area AREA of window W. AREA < 0 means return the left edge of the
1104 whole window, to the left of the right fringe of W. */
1105
1106 INLINE int
1107 window_box_right_offset (w, area)
1108 struct window *w;
1109 int area;
1110 {
1111 return window_box_left_offset (w, area) + window_box_width (w, area);
1112 }
1113
1114 /* Return the frame-relative coordinate of the left edge of display
1115 area AREA of window W. AREA < 0 means return the left edge of the
1116 whole window, to the right of the left fringe of W. */
1117
1118 INLINE int
1119 window_box_left (w, area)
1120 struct window *w;
1121 int area;
1122 {
1123 struct frame *f = XFRAME (w->frame);
1124 int x;
1125
1126 if (w->pseudo_window_p)
1127 return FRAME_INTERNAL_BORDER_WIDTH (f);
1128
1129 x = (WINDOW_LEFT_EDGE_X (w)
1130 + window_box_left_offset (w, area));
1131
1132 return x;
1133 }
1134
1135
1136 /* Return the frame-relative coordinate of the right edge of display
1137 area AREA of window W. AREA < 0 means return the left edge of the
1138 whole window, to the left of the right fringe of W. */
1139
1140 INLINE int
1141 window_box_right (w, area)
1142 struct window *w;
1143 int area;
1144 {
1145 return window_box_left (w, area) + window_box_width (w, area);
1146 }
1147
1148 /* Get the bounding box of the display area AREA of window W, without
1149 mode lines, in frame-relative coordinates. AREA < 0 means the
1150 whole window, not including the left and right fringes of
1151 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1152 coordinates of the upper-left corner of the box. Return in
1153 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1154
1155 INLINE void
1156 window_box (w, area, box_x, box_y, box_width, box_height)
1157 struct window *w;
1158 int area;
1159 int *box_x, *box_y, *box_width, *box_height;
1160 {
1161 if (box_width)
1162 *box_width = window_box_width (w, area);
1163 if (box_height)
1164 *box_height = window_box_height (w);
1165 if (box_x)
1166 *box_x = window_box_left (w, area);
1167 if (box_y)
1168 {
1169 *box_y = WINDOW_TOP_EDGE_Y (w);
1170 if (WINDOW_WANTS_HEADER_LINE_P (w))
1171 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1172 }
1173 }
1174
1175
1176 /* Get the bounding box of the display area AREA of window W, without
1177 mode lines. AREA < 0 means the whole window, not including the
1178 left and right fringe of the window. Return in *TOP_LEFT_X
1179 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1180 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1181 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1182 box. */
1183
1184 INLINE void
1185 window_box_edges (w, area, top_left_x, top_left_y,
1186 bottom_right_x, bottom_right_y)
1187 struct window *w;
1188 int area;
1189 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1190 {
1191 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1192 bottom_right_y);
1193 *bottom_right_x += *top_left_x;
1194 *bottom_right_y += *top_left_y;
1195 }
1196
1197
1198 \f
1199 /***********************************************************************
1200 Utilities
1201 ***********************************************************************/
1202
1203 /* Return the bottom y-position of the line the iterator IT is in.
1204 This can modify IT's settings. */
1205
1206 int
1207 line_bottom_y (it)
1208 struct it *it;
1209 {
1210 int line_height = it->max_ascent + it->max_descent;
1211 int line_top_y = it->current_y;
1212
1213 if (line_height == 0)
1214 {
1215 if (last_height)
1216 line_height = last_height;
1217 else if (IT_CHARPOS (*it) < ZV)
1218 {
1219 move_it_by_lines (it, 1, 1);
1220 line_height = (it->max_ascent || it->max_descent
1221 ? it->max_ascent + it->max_descent
1222 : last_height);
1223 }
1224 else
1225 {
1226 struct glyph_row *row = it->glyph_row;
1227
1228 /* Use the default character height. */
1229 it->glyph_row = NULL;
1230 it->what = IT_CHARACTER;
1231 it->c = ' ';
1232 it->len = 1;
1233 PRODUCE_GLYPHS (it);
1234 line_height = it->ascent + it->descent;
1235 it->glyph_row = row;
1236 }
1237 }
1238
1239 return line_top_y + line_height;
1240 }
1241
1242
1243 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1244 1 if POS is visible and the line containing POS is fully visible.
1245 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1246 and header-lines heights. */
1247
1248 int
1249 pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
1250 struct window *w;
1251 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
1252 {
1253 struct it it;
1254 struct text_pos top;
1255 int visible_p;
1256 struct buffer *old_buffer = NULL;
1257
1258 if (XBUFFER (w->buffer) != current_buffer)
1259 {
1260 old_buffer = current_buffer;
1261 set_buffer_internal_1 (XBUFFER (w->buffer));
1262 }
1263
1264 *fully = visible_p = 0;
1265 SET_TEXT_POS_FROM_MARKER (top, w->start);
1266
1267 /* Compute exact mode line heights, if requested. */
1268 if (exact_mode_line_heights_p)
1269 {
1270 if (WINDOW_WANTS_MODELINE_P (w))
1271 current_mode_line_height
1272 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1273 current_buffer->mode_line_format);
1274
1275 if (WINDOW_WANTS_HEADER_LINE_P (w))
1276 current_header_line_height
1277 = display_mode_line (w, HEADER_LINE_FACE_ID,
1278 current_buffer->header_line_format);
1279 }
1280
1281 start_display (&it, w, top);
1282 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1283 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1284
1285 /* Note that we may overshoot because of invisible text. */
1286 if (IT_CHARPOS (it) >= charpos)
1287 {
1288 int top_y = it.current_y;
1289 int bottom_y = line_bottom_y (&it);
1290 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1291
1292 if (top_y < window_top_y)
1293 visible_p = bottom_y > window_top_y;
1294 else if (top_y < it.last_visible_y)
1295 {
1296 visible_p = 1;
1297 *fully = bottom_y <= it.last_visible_y;
1298 }
1299 if (visible_p && x)
1300 {
1301 *x = it.current_x;
1302 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1303 }
1304 }
1305 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1306 {
1307 struct it it2;
1308
1309 it2 = it;
1310 move_it_by_lines (&it, 1, 0);
1311 if (charpos < IT_CHARPOS (it))
1312 {
1313 visible_p = 1;
1314 if (x)
1315 {
1316 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1317 *x = it2.current_x;
1318 *y = it2.current_y + it2.max_ascent - it2.ascent;
1319 }
1320 }
1321 }
1322
1323 if (old_buffer)
1324 set_buffer_internal_1 (old_buffer);
1325
1326 current_header_line_height = current_mode_line_height = -1;
1327
1328 return visible_p;
1329 }
1330
1331
1332 /* Return the next character from STR which is MAXLEN bytes long.
1333 Return in *LEN the length of the character. This is like
1334 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1335 we find one, we return a `?', but with the length of the invalid
1336 character. */
1337
1338 static INLINE int
1339 string_char_and_length (str, maxlen, len)
1340 const unsigned char *str;
1341 int maxlen, *len;
1342 {
1343 int c;
1344
1345 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1346 if (!CHAR_VALID_P (c, 1))
1347 /* We may not change the length here because other places in Emacs
1348 don't use this function, i.e. they silently accept invalid
1349 characters. */
1350 c = '?';
1351
1352 return c;
1353 }
1354
1355
1356
1357 /* Given a position POS containing a valid character and byte position
1358 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1359
1360 static struct text_pos
1361 string_pos_nchars_ahead (pos, string, nchars)
1362 struct text_pos pos;
1363 Lisp_Object string;
1364 int nchars;
1365 {
1366 xassert (STRINGP (string) && nchars >= 0);
1367
1368 if (STRING_MULTIBYTE (string))
1369 {
1370 int rest = SBYTES (string) - BYTEPOS (pos);
1371 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1372 int len;
1373
1374 while (nchars--)
1375 {
1376 string_char_and_length (p, rest, &len);
1377 p += len, rest -= len;
1378 xassert (rest >= 0);
1379 CHARPOS (pos) += 1;
1380 BYTEPOS (pos) += len;
1381 }
1382 }
1383 else
1384 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1385
1386 return pos;
1387 }
1388
1389
1390 /* Value is the text position, i.e. character and byte position,
1391 for character position CHARPOS in STRING. */
1392
1393 static INLINE struct text_pos
1394 string_pos (charpos, string)
1395 int charpos;
1396 Lisp_Object string;
1397 {
1398 struct text_pos pos;
1399 xassert (STRINGP (string));
1400 xassert (charpos >= 0);
1401 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1402 return pos;
1403 }
1404
1405
1406 /* Value is a text position, i.e. character and byte position, for
1407 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1408 means recognize multibyte characters. */
1409
1410 static struct text_pos
1411 c_string_pos (charpos, s, multibyte_p)
1412 int charpos;
1413 unsigned char *s;
1414 int multibyte_p;
1415 {
1416 struct text_pos pos;
1417
1418 xassert (s != NULL);
1419 xassert (charpos >= 0);
1420
1421 if (multibyte_p)
1422 {
1423 int rest = strlen (s), len;
1424
1425 SET_TEXT_POS (pos, 0, 0);
1426 while (charpos--)
1427 {
1428 string_char_and_length (s, rest, &len);
1429 s += len, rest -= len;
1430 xassert (rest >= 0);
1431 CHARPOS (pos) += 1;
1432 BYTEPOS (pos) += len;
1433 }
1434 }
1435 else
1436 SET_TEXT_POS (pos, charpos, charpos);
1437
1438 return pos;
1439 }
1440
1441
1442 /* Value is the number of characters in C string S. MULTIBYTE_P
1443 non-zero means recognize multibyte characters. */
1444
1445 static int
1446 number_of_chars (s, multibyte_p)
1447 unsigned char *s;
1448 int multibyte_p;
1449 {
1450 int nchars;
1451
1452 if (multibyte_p)
1453 {
1454 int rest = strlen (s), len;
1455 unsigned char *p = (unsigned char *) s;
1456
1457 for (nchars = 0; rest > 0; ++nchars)
1458 {
1459 string_char_and_length (p, rest, &len);
1460 rest -= len, p += len;
1461 }
1462 }
1463 else
1464 nchars = strlen (s);
1465
1466 return nchars;
1467 }
1468
1469
1470 /* Compute byte position NEWPOS->bytepos corresponding to
1471 NEWPOS->charpos. POS is a known position in string STRING.
1472 NEWPOS->charpos must be >= POS.charpos. */
1473
1474 static void
1475 compute_string_pos (newpos, pos, string)
1476 struct text_pos *newpos, pos;
1477 Lisp_Object string;
1478 {
1479 xassert (STRINGP (string));
1480 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1481
1482 if (STRING_MULTIBYTE (string))
1483 *newpos = string_pos_nchars_ahead (pos, string,
1484 CHARPOS (*newpos) - CHARPOS (pos));
1485 else
1486 BYTEPOS (*newpos) = CHARPOS (*newpos);
1487 }
1488
1489 /* EXPORT:
1490 Return an estimation of the pixel height of mode or top lines on
1491 frame F. FACE_ID specifies what line's height to estimate. */
1492
1493 int
1494 estimate_mode_line_height (f, face_id)
1495 struct frame *f;
1496 enum face_id face_id;
1497 {
1498 #ifdef HAVE_WINDOW_SYSTEM
1499 if (FRAME_WINDOW_P (f))
1500 {
1501 int height = FONT_HEIGHT (FRAME_FONT (f));
1502
1503 /* This function is called so early when Emacs starts that the face
1504 cache and mode line face are not yet initialized. */
1505 if (FRAME_FACE_CACHE (f))
1506 {
1507 struct face *face = FACE_FROM_ID (f, face_id);
1508 if (face)
1509 {
1510 if (face->font)
1511 height = FONT_HEIGHT (face->font);
1512 if (face->box_line_width > 0)
1513 height += 2 * face->box_line_width;
1514 }
1515 }
1516
1517 return height;
1518 }
1519 #endif
1520
1521 return 1;
1522 }
1523
1524 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1525 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1526 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1527 not force the value into range. */
1528
1529 void
1530 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1531 FRAME_PTR f;
1532 register int pix_x, pix_y;
1533 int *x, *y;
1534 NativeRectangle *bounds;
1535 int noclip;
1536 {
1537
1538 #ifdef HAVE_WINDOW_SYSTEM
1539 if (FRAME_WINDOW_P (f))
1540 {
1541 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1542 even for negative values. */
1543 if (pix_x < 0)
1544 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1545 if (pix_y < 0)
1546 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1547
1548 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1549 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1550
1551 if (bounds)
1552 STORE_NATIVE_RECT (*bounds,
1553 FRAME_COL_TO_PIXEL_X (f, pix_x),
1554 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1555 FRAME_COLUMN_WIDTH (f) - 1,
1556 FRAME_LINE_HEIGHT (f) - 1);
1557
1558 if (!noclip)
1559 {
1560 if (pix_x < 0)
1561 pix_x = 0;
1562 else if (pix_x > FRAME_TOTAL_COLS (f))
1563 pix_x = FRAME_TOTAL_COLS (f);
1564
1565 if (pix_y < 0)
1566 pix_y = 0;
1567 else if (pix_y > FRAME_LINES (f))
1568 pix_y = FRAME_LINES (f);
1569 }
1570 }
1571 #endif
1572
1573 *x = pix_x;
1574 *y = pix_y;
1575 }
1576
1577
1578 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1579 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1580 can't tell the positions because W's display is not up to date,
1581 return 0. */
1582
1583 int
1584 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1585 struct window *w;
1586 int hpos, vpos;
1587 int *frame_x, *frame_y;
1588 {
1589 #ifdef HAVE_WINDOW_SYSTEM
1590 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1591 {
1592 int success_p;
1593
1594 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1595 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1596
1597 if (display_completed)
1598 {
1599 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1600 struct glyph *glyph = row->glyphs[TEXT_AREA];
1601 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1602
1603 hpos = row->x;
1604 vpos = row->y;
1605 while (glyph < end)
1606 {
1607 hpos += glyph->pixel_width;
1608 ++glyph;
1609 }
1610
1611 /* If first glyph is partially visible, its first visible position is still 0. */
1612 if (hpos < 0)
1613 hpos = 0;
1614
1615 success_p = 1;
1616 }
1617 else
1618 {
1619 hpos = vpos = 0;
1620 success_p = 0;
1621 }
1622
1623 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1624 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1625 return success_p;
1626 }
1627 #endif
1628
1629 *frame_x = hpos;
1630 *frame_y = vpos;
1631 return 1;
1632 }
1633
1634
1635 #ifdef HAVE_WINDOW_SYSTEM
1636
1637 /* Find the glyph under window-relative coordinates X/Y in window W.
1638 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1639 strings. Return in *HPOS and *VPOS the row and column number of
1640 the glyph found. Return in *AREA the glyph area containing X.
1641 Value is a pointer to the glyph found or null if X/Y is not on
1642 text, or we can't tell because W's current matrix is not up to
1643 date. */
1644
1645 static struct glyph *
1646 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1647 struct window *w;
1648 int x, y;
1649 int *hpos, *vpos, *dx, *dy, *area;
1650 {
1651 struct glyph *glyph, *end;
1652 struct glyph_row *row = NULL;
1653 int x0, i;
1654
1655 /* Find row containing Y. Give up if some row is not enabled. */
1656 for (i = 0; i < w->current_matrix->nrows; ++i)
1657 {
1658 row = MATRIX_ROW (w->current_matrix, i);
1659 if (!row->enabled_p)
1660 return NULL;
1661 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1662 break;
1663 }
1664
1665 *vpos = i;
1666 *hpos = 0;
1667
1668 /* Give up if Y is not in the window. */
1669 if (i == w->current_matrix->nrows)
1670 return NULL;
1671
1672 /* Get the glyph area containing X. */
1673 if (w->pseudo_window_p)
1674 {
1675 *area = TEXT_AREA;
1676 x0 = 0;
1677 }
1678 else
1679 {
1680 if (x < window_box_left_offset (w, TEXT_AREA))
1681 {
1682 *area = LEFT_MARGIN_AREA;
1683 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1684 }
1685 else if (x < window_box_right_offset (w, TEXT_AREA))
1686 {
1687 *area = TEXT_AREA;
1688 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1689 }
1690 else
1691 {
1692 *area = RIGHT_MARGIN_AREA;
1693 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1694 }
1695 }
1696
1697 /* Find glyph containing X. */
1698 glyph = row->glyphs[*area];
1699 end = glyph + row->used[*area];
1700 x -= x0;
1701 while (glyph < end && x >= glyph->pixel_width)
1702 {
1703 x -= glyph->pixel_width;
1704 ++glyph;
1705 }
1706
1707 if (glyph == end)
1708 return NULL;
1709
1710 if (dx)
1711 {
1712 *dx = x;
1713 *dy = y - (row->y + row->ascent - glyph->ascent);
1714 }
1715
1716 *hpos = glyph - row->glyphs[*area];
1717 return glyph;
1718 }
1719
1720
1721 /* EXPORT:
1722 Convert frame-relative x/y to coordinates relative to window W.
1723 Takes pseudo-windows into account. */
1724
1725 void
1726 frame_to_window_pixel_xy (w, x, y)
1727 struct window *w;
1728 int *x, *y;
1729 {
1730 if (w->pseudo_window_p)
1731 {
1732 /* A pseudo-window is always full-width, and starts at the
1733 left edge of the frame, plus a frame border. */
1734 struct frame *f = XFRAME (w->frame);
1735 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1736 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1737 }
1738 else
1739 {
1740 *x -= WINDOW_LEFT_EDGE_X (w);
1741 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1742 }
1743 }
1744
1745 /* EXPORT:
1746 Return in *R the clipping rectangle for glyph string S. */
1747
1748 void
1749 get_glyph_string_clip_rect (s, nr)
1750 struct glyph_string *s;
1751 NativeRectangle *nr;
1752 {
1753 XRectangle r;
1754
1755 if (s->row->full_width_p)
1756 {
1757 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1758 r.x = WINDOW_LEFT_EDGE_X (s->w);
1759 r.width = WINDOW_TOTAL_WIDTH (s->w);
1760
1761 /* Unless displaying a mode or menu bar line, which are always
1762 fully visible, clip to the visible part of the row. */
1763 if (s->w->pseudo_window_p)
1764 r.height = s->row->visible_height;
1765 else
1766 r.height = s->height;
1767 }
1768 else
1769 {
1770 /* This is a text line that may be partially visible. */
1771 r.x = window_box_left (s->w, s->area);
1772 r.width = window_box_width (s->w, s->area);
1773 r.height = s->row->visible_height;
1774 }
1775
1776 /* If S draws overlapping rows, it's sufficient to use the top and
1777 bottom of the window for clipping because this glyph string
1778 intentionally draws over other lines. */
1779 if (s->for_overlaps_p)
1780 {
1781 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1782 r.height = window_text_bottom_y (s->w) - r.y;
1783 }
1784 else
1785 {
1786 /* Don't use S->y for clipping because it doesn't take partially
1787 visible lines into account. For example, it can be negative for
1788 partially visible lines at the top of a window. */
1789 if (!s->row->full_width_p
1790 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1791 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1792 else
1793 r.y = max (0, s->row->y);
1794
1795 /* If drawing a tool-bar window, draw it over the internal border
1796 at the top of the window. */
1797 if (WINDOWP (s->f->tool_bar_window)
1798 && s->w == XWINDOW (s->f->tool_bar_window))
1799 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1800 }
1801
1802 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1803
1804 /* If drawing the cursor, don't let glyph draw outside its
1805 advertised boundaries. Cleartype does this under some circumstances. */
1806 if (s->hl == DRAW_CURSOR)
1807 {
1808 struct glyph *glyph = s->first_glyph;
1809 int height;
1810
1811 if (s->x > r.x)
1812 {
1813 r.width -= s->x - r.x;
1814 r.x = s->x;
1815 }
1816 r.width = min (r.width, glyph->pixel_width);
1817
1818 /* Don't draw cursor glyph taller than our actual glyph. */
1819 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1820 if (height < r.height)
1821 {
1822 int max_y = r.y + r.height;
1823 r.y = min (max_y, s->ybase + glyph->descent - height);
1824 r.height = min (max_y - r.y, height);
1825 }
1826 }
1827
1828 #ifdef CONVERT_FROM_XRECT
1829 CONVERT_FROM_XRECT (r, *nr);
1830 #else
1831 *nr = r;
1832 #endif
1833 }
1834
1835 #endif /* HAVE_WINDOW_SYSTEM */
1836
1837 \f
1838 /***********************************************************************
1839 Lisp form evaluation
1840 ***********************************************************************/
1841
1842 /* Error handler for safe_eval and safe_call. */
1843
1844 static Lisp_Object
1845 safe_eval_handler (arg)
1846 Lisp_Object arg;
1847 {
1848 add_to_log ("Error during redisplay: %s", arg, Qnil);
1849 return Qnil;
1850 }
1851
1852
1853 /* Evaluate SEXPR and return the result, or nil if something went
1854 wrong. Prevent redisplay during the evaluation. */
1855
1856 Lisp_Object
1857 safe_eval (sexpr)
1858 Lisp_Object sexpr;
1859 {
1860 Lisp_Object val;
1861
1862 if (inhibit_eval_during_redisplay)
1863 val = Qnil;
1864 else
1865 {
1866 int count = SPECPDL_INDEX ();
1867 struct gcpro gcpro1;
1868
1869 GCPRO1 (sexpr);
1870 specbind (Qinhibit_redisplay, Qt);
1871 /* Use Qt to ensure debugger does not run,
1872 so there is no possibility of wanting to redisplay. */
1873 val = internal_condition_case_1 (Feval, sexpr, Qt,
1874 safe_eval_handler);
1875 UNGCPRO;
1876 val = unbind_to (count, val);
1877 }
1878
1879 return val;
1880 }
1881
1882
1883 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1884 Return the result, or nil if something went wrong. Prevent
1885 redisplay during the evaluation. */
1886
1887 Lisp_Object
1888 safe_call (nargs, args)
1889 int nargs;
1890 Lisp_Object *args;
1891 {
1892 Lisp_Object val;
1893
1894 if (inhibit_eval_during_redisplay)
1895 val = Qnil;
1896 else
1897 {
1898 int count = SPECPDL_INDEX ();
1899 struct gcpro gcpro1;
1900
1901 GCPRO1 (args[0]);
1902 gcpro1.nvars = nargs;
1903 specbind (Qinhibit_redisplay, Qt);
1904 /* Use Qt to ensure debugger does not run,
1905 so there is no possibility of wanting to redisplay. */
1906 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1907 safe_eval_handler);
1908 UNGCPRO;
1909 val = unbind_to (count, val);
1910 }
1911
1912 return val;
1913 }
1914
1915
1916 /* Call function FN with one argument ARG.
1917 Return the result, or nil if something went wrong. */
1918
1919 Lisp_Object
1920 safe_call1 (fn, arg)
1921 Lisp_Object fn, arg;
1922 {
1923 Lisp_Object args[2];
1924 args[0] = fn;
1925 args[1] = arg;
1926 return safe_call (2, args);
1927 }
1928
1929
1930 \f
1931 /***********************************************************************
1932 Debugging
1933 ***********************************************************************/
1934
1935 #if 0
1936
1937 /* Define CHECK_IT to perform sanity checks on iterators.
1938 This is for debugging. It is too slow to do unconditionally. */
1939
1940 static void
1941 check_it (it)
1942 struct it *it;
1943 {
1944 if (it->method == next_element_from_string)
1945 {
1946 xassert (STRINGP (it->string));
1947 xassert (IT_STRING_CHARPOS (*it) >= 0);
1948 }
1949 else
1950 {
1951 xassert (IT_STRING_CHARPOS (*it) < 0);
1952 if (it->method == next_element_from_buffer)
1953 {
1954 /* Check that character and byte positions agree. */
1955 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1956 }
1957 }
1958
1959 if (it->dpvec)
1960 xassert (it->current.dpvec_index >= 0);
1961 else
1962 xassert (it->current.dpvec_index < 0);
1963 }
1964
1965 #define CHECK_IT(IT) check_it ((IT))
1966
1967 #else /* not 0 */
1968
1969 #define CHECK_IT(IT) (void) 0
1970
1971 #endif /* not 0 */
1972
1973
1974 #if GLYPH_DEBUG
1975
1976 /* Check that the window end of window W is what we expect it
1977 to be---the last row in the current matrix displaying text. */
1978
1979 static void
1980 check_window_end (w)
1981 struct window *w;
1982 {
1983 if (!MINI_WINDOW_P (w)
1984 && !NILP (w->window_end_valid))
1985 {
1986 struct glyph_row *row;
1987 xassert ((row = MATRIX_ROW (w->current_matrix,
1988 XFASTINT (w->window_end_vpos)),
1989 !row->enabled_p
1990 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1991 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1992 }
1993 }
1994
1995 #define CHECK_WINDOW_END(W) check_window_end ((W))
1996
1997 #else /* not GLYPH_DEBUG */
1998
1999 #define CHECK_WINDOW_END(W) (void) 0
2000
2001 #endif /* not GLYPH_DEBUG */
2002
2003
2004 \f
2005 /***********************************************************************
2006 Iterator initialization
2007 ***********************************************************************/
2008
2009 /* Initialize IT for displaying current_buffer in window W, starting
2010 at character position CHARPOS. CHARPOS < 0 means that no buffer
2011 position is specified which is useful when the iterator is assigned
2012 a position later. BYTEPOS is the byte position corresponding to
2013 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2014
2015 If ROW is not null, calls to produce_glyphs with IT as parameter
2016 will produce glyphs in that row.
2017
2018 BASE_FACE_ID is the id of a base face to use. It must be one of
2019 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2020 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2021 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2022
2023 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2024 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2025 will be initialized to use the corresponding mode line glyph row of
2026 the desired matrix of W. */
2027
2028 void
2029 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2030 struct it *it;
2031 struct window *w;
2032 int charpos, bytepos;
2033 struct glyph_row *row;
2034 enum face_id base_face_id;
2035 {
2036 int highlight_region_p;
2037
2038 /* Some precondition checks. */
2039 xassert (w != NULL && it != NULL);
2040 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2041 && charpos <= ZV));
2042
2043 /* If face attributes have been changed since the last redisplay,
2044 free realized faces now because they depend on face definitions
2045 that might have changed. Don't free faces while there might be
2046 desired matrices pending which reference these faces. */
2047 if (face_change_count && !inhibit_free_realized_faces)
2048 {
2049 face_change_count = 0;
2050 free_all_realized_faces (Qnil);
2051 }
2052
2053 /* Use one of the mode line rows of W's desired matrix if
2054 appropriate. */
2055 if (row == NULL)
2056 {
2057 if (base_face_id == MODE_LINE_FACE_ID
2058 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2059 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2060 else if (base_face_id == HEADER_LINE_FACE_ID)
2061 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2062 }
2063
2064 /* Clear IT. */
2065 bzero (it, sizeof *it);
2066 it->current.overlay_string_index = -1;
2067 it->current.dpvec_index = -1;
2068 it->base_face_id = base_face_id;
2069 it->string = Qnil;
2070 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2071
2072 /* The window in which we iterate over current_buffer: */
2073 XSETWINDOW (it->window, w);
2074 it->w = w;
2075 it->f = XFRAME (w->frame);
2076
2077 /* Extra space between lines (on window systems only). */
2078 if (base_face_id == DEFAULT_FACE_ID
2079 && FRAME_WINDOW_P (it->f))
2080 {
2081 if (NATNUMP (current_buffer->extra_line_spacing))
2082 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2083 else if (FLOATP (current_buffer->extra_line_spacing))
2084 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2085 * FRAME_LINE_HEIGHT (it->f));
2086 else if (it->f->extra_line_spacing > 0)
2087 it->extra_line_spacing = it->f->extra_line_spacing;
2088 it->max_extra_line_spacing = 0;
2089 }
2090
2091 /* If realized faces have been removed, e.g. because of face
2092 attribute changes of named faces, recompute them. When running
2093 in batch mode, the face cache of Vterminal_frame is null. If
2094 we happen to get called, make a dummy face cache. */
2095 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2096 init_frame_faces (it->f);
2097 if (FRAME_FACE_CACHE (it->f)->used == 0)
2098 recompute_basic_faces (it->f);
2099
2100 /* Current value of the `slice', `space-width', and 'height' properties. */
2101 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2102 it->space_width = Qnil;
2103 it->font_height = Qnil;
2104 it->override_ascent = -1;
2105
2106 /* Are control characters displayed as `^C'? */
2107 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2108
2109 /* -1 means everything between a CR and the following line end
2110 is invisible. >0 means lines indented more than this value are
2111 invisible. */
2112 it->selective = (INTEGERP (current_buffer->selective_display)
2113 ? XFASTINT (current_buffer->selective_display)
2114 : (!NILP (current_buffer->selective_display)
2115 ? -1 : 0));
2116 it->selective_display_ellipsis_p
2117 = !NILP (current_buffer->selective_display_ellipses);
2118
2119 /* Display table to use. */
2120 it->dp = window_display_table (w);
2121
2122 /* Are multibyte characters enabled in current_buffer? */
2123 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2124
2125 /* Non-zero if we should highlight the region. */
2126 highlight_region_p
2127 = (!NILP (Vtransient_mark_mode)
2128 && !NILP (current_buffer->mark_active)
2129 && XMARKER (current_buffer->mark)->buffer != 0);
2130
2131 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2132 start and end of a visible region in window IT->w. Set both to
2133 -1 to indicate no region. */
2134 if (highlight_region_p
2135 /* Maybe highlight only in selected window. */
2136 && (/* Either show region everywhere. */
2137 highlight_nonselected_windows
2138 /* Or show region in the selected window. */
2139 || w == XWINDOW (selected_window)
2140 /* Or show the region if we are in the mini-buffer and W is
2141 the window the mini-buffer refers to. */
2142 || (MINI_WINDOW_P (XWINDOW (selected_window))
2143 && WINDOWP (minibuf_selected_window)
2144 && w == XWINDOW (minibuf_selected_window))))
2145 {
2146 int charpos = marker_position (current_buffer->mark);
2147 it->region_beg_charpos = min (PT, charpos);
2148 it->region_end_charpos = max (PT, charpos);
2149 }
2150 else
2151 it->region_beg_charpos = it->region_end_charpos = -1;
2152
2153 /* Get the position at which the redisplay_end_trigger hook should
2154 be run, if it is to be run at all. */
2155 if (MARKERP (w->redisplay_end_trigger)
2156 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2157 it->redisplay_end_trigger_charpos
2158 = marker_position (w->redisplay_end_trigger);
2159 else if (INTEGERP (w->redisplay_end_trigger))
2160 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2161
2162 /* Correct bogus values of tab_width. */
2163 it->tab_width = XINT (current_buffer->tab_width);
2164 if (it->tab_width <= 0 || it->tab_width > 1000)
2165 it->tab_width = 8;
2166
2167 /* Are lines in the display truncated? */
2168 it->truncate_lines_p
2169 = (base_face_id != DEFAULT_FACE_ID
2170 || XINT (it->w->hscroll)
2171 || (truncate_partial_width_windows
2172 && !WINDOW_FULL_WIDTH_P (it->w))
2173 || !NILP (current_buffer->truncate_lines));
2174
2175 /* Get dimensions of truncation and continuation glyphs. These are
2176 displayed as fringe bitmaps under X, so we don't need them for such
2177 frames. */
2178 if (!FRAME_WINDOW_P (it->f))
2179 {
2180 if (it->truncate_lines_p)
2181 {
2182 /* We will need the truncation glyph. */
2183 xassert (it->glyph_row == NULL);
2184 produce_special_glyphs (it, IT_TRUNCATION);
2185 it->truncation_pixel_width = it->pixel_width;
2186 }
2187 else
2188 {
2189 /* We will need the continuation glyph. */
2190 xassert (it->glyph_row == NULL);
2191 produce_special_glyphs (it, IT_CONTINUATION);
2192 it->continuation_pixel_width = it->pixel_width;
2193 }
2194
2195 /* Reset these values to zero because the produce_special_glyphs
2196 above has changed them. */
2197 it->pixel_width = it->ascent = it->descent = 0;
2198 it->phys_ascent = it->phys_descent = 0;
2199 }
2200
2201 /* Set this after getting the dimensions of truncation and
2202 continuation glyphs, so that we don't produce glyphs when calling
2203 produce_special_glyphs, above. */
2204 it->glyph_row = row;
2205 it->area = TEXT_AREA;
2206
2207 /* Get the dimensions of the display area. The display area
2208 consists of the visible window area plus a horizontally scrolled
2209 part to the left of the window. All x-values are relative to the
2210 start of this total display area. */
2211 if (base_face_id != DEFAULT_FACE_ID)
2212 {
2213 /* Mode lines, menu bar in terminal frames. */
2214 it->first_visible_x = 0;
2215 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2216 }
2217 else
2218 {
2219 it->first_visible_x
2220 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2221 it->last_visible_x = (it->first_visible_x
2222 + window_box_width (w, TEXT_AREA));
2223
2224 /* If we truncate lines, leave room for the truncator glyph(s) at
2225 the right margin. Otherwise, leave room for the continuation
2226 glyph(s). Truncation and continuation glyphs are not inserted
2227 for window-based redisplay. */
2228 if (!FRAME_WINDOW_P (it->f))
2229 {
2230 if (it->truncate_lines_p)
2231 it->last_visible_x -= it->truncation_pixel_width;
2232 else
2233 it->last_visible_x -= it->continuation_pixel_width;
2234 }
2235
2236 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2237 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2238 }
2239
2240 /* Leave room for a border glyph. */
2241 if (!FRAME_WINDOW_P (it->f)
2242 && !WINDOW_RIGHTMOST_P (it->w))
2243 it->last_visible_x -= 1;
2244
2245 it->last_visible_y = window_text_bottom_y (w);
2246
2247 /* For mode lines and alike, arrange for the first glyph having a
2248 left box line if the face specifies a box. */
2249 if (base_face_id != DEFAULT_FACE_ID)
2250 {
2251 struct face *face;
2252
2253 it->face_id = base_face_id;
2254
2255 /* If we have a boxed mode line, make the first character appear
2256 with a left box line. */
2257 face = FACE_FROM_ID (it->f, base_face_id);
2258 if (face->box != FACE_NO_BOX)
2259 it->start_of_box_run_p = 1;
2260 }
2261
2262 /* If a buffer position was specified, set the iterator there,
2263 getting overlays and face properties from that position. */
2264 if (charpos >= BUF_BEG (current_buffer))
2265 {
2266 it->end_charpos = ZV;
2267 it->face_id = -1;
2268 IT_CHARPOS (*it) = charpos;
2269
2270 /* Compute byte position if not specified. */
2271 if (bytepos < charpos)
2272 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2273 else
2274 IT_BYTEPOS (*it) = bytepos;
2275
2276 it->start = it->current;
2277
2278 /* Compute faces etc. */
2279 reseat (it, it->current.pos, 1);
2280 }
2281
2282 CHECK_IT (it);
2283 }
2284
2285
2286 /* Initialize IT for the display of window W with window start POS. */
2287
2288 void
2289 start_display (it, w, pos)
2290 struct it *it;
2291 struct window *w;
2292 struct text_pos pos;
2293 {
2294 struct glyph_row *row;
2295 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2296
2297 row = w->desired_matrix->rows + first_vpos;
2298 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2299 it->first_vpos = first_vpos;
2300
2301 if (!it->truncate_lines_p)
2302 {
2303 int start_at_line_beg_p;
2304 int first_y = it->current_y;
2305
2306 /* If window start is not at a line start, skip forward to POS to
2307 get the correct continuation lines width. */
2308 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2309 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2310 if (!start_at_line_beg_p)
2311 {
2312 int new_x;
2313
2314 reseat_at_previous_visible_line_start (it);
2315 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2316
2317 new_x = it->current_x + it->pixel_width;
2318
2319 /* If lines are continued, this line may end in the middle
2320 of a multi-glyph character (e.g. a control character
2321 displayed as \003, or in the middle of an overlay
2322 string). In this case move_it_to above will not have
2323 taken us to the start of the continuation line but to the
2324 end of the continued line. */
2325 if (it->current_x > 0
2326 && !it->truncate_lines_p /* Lines are continued. */
2327 && (/* And glyph doesn't fit on the line. */
2328 new_x > it->last_visible_x
2329 /* Or it fits exactly and we're on a window
2330 system frame. */
2331 || (new_x == it->last_visible_x
2332 && FRAME_WINDOW_P (it->f))))
2333 {
2334 if (it->current.dpvec_index >= 0
2335 || it->current.overlay_string_index >= 0)
2336 {
2337 set_iterator_to_next (it, 1);
2338 move_it_in_display_line_to (it, -1, -1, 0);
2339 }
2340
2341 it->continuation_lines_width += it->current_x;
2342 }
2343
2344 /* We're starting a new display line, not affected by the
2345 height of the continued line, so clear the appropriate
2346 fields in the iterator structure. */
2347 it->max_ascent = it->max_descent = 0;
2348 it->max_phys_ascent = it->max_phys_descent = 0;
2349
2350 it->current_y = first_y;
2351 it->vpos = 0;
2352 it->current_x = it->hpos = 0;
2353 }
2354 }
2355
2356 #if 0 /* Don't assert the following because start_display is sometimes
2357 called intentionally with a window start that is not at a
2358 line start. Please leave this code in as a comment. */
2359
2360 /* Window start should be on a line start, now. */
2361 xassert (it->continuation_lines_width
2362 || IT_CHARPOS (it) == BEGV
2363 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2364 #endif /* 0 */
2365 }
2366
2367
2368 /* Return 1 if POS is a position in ellipses displayed for invisible
2369 text. W is the window we display, for text property lookup. */
2370
2371 static int
2372 in_ellipses_for_invisible_text_p (pos, w)
2373 struct display_pos *pos;
2374 struct window *w;
2375 {
2376 Lisp_Object prop, window;
2377 int ellipses_p = 0;
2378 int charpos = CHARPOS (pos->pos);
2379
2380 /* If POS specifies a position in a display vector, this might
2381 be for an ellipsis displayed for invisible text. We won't
2382 get the iterator set up for delivering that ellipsis unless
2383 we make sure that it gets aware of the invisible text. */
2384 if (pos->dpvec_index >= 0
2385 && pos->overlay_string_index < 0
2386 && CHARPOS (pos->string_pos) < 0
2387 && charpos > BEGV
2388 && (XSETWINDOW (window, w),
2389 prop = Fget_char_property (make_number (charpos),
2390 Qinvisible, window),
2391 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2392 {
2393 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2394 window);
2395 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2396 }
2397
2398 return ellipses_p;
2399 }
2400
2401
2402 /* Initialize IT for stepping through current_buffer in window W,
2403 starting at position POS that includes overlay string and display
2404 vector/ control character translation position information. Value
2405 is zero if there are overlay strings with newlines at POS. */
2406
2407 static int
2408 init_from_display_pos (it, w, pos)
2409 struct it *it;
2410 struct window *w;
2411 struct display_pos *pos;
2412 {
2413 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2414 int i, overlay_strings_with_newlines = 0;
2415
2416 /* If POS specifies a position in a display vector, this might
2417 be for an ellipsis displayed for invisible text. We won't
2418 get the iterator set up for delivering that ellipsis unless
2419 we make sure that it gets aware of the invisible text. */
2420 if (in_ellipses_for_invisible_text_p (pos, w))
2421 {
2422 --charpos;
2423 bytepos = 0;
2424 }
2425
2426 /* Keep in mind: the call to reseat in init_iterator skips invisible
2427 text, so we might end up at a position different from POS. This
2428 is only a problem when POS is a row start after a newline and an
2429 overlay starts there with an after-string, and the overlay has an
2430 invisible property. Since we don't skip invisible text in
2431 display_line and elsewhere immediately after consuming the
2432 newline before the row start, such a POS will not be in a string,
2433 but the call to init_iterator below will move us to the
2434 after-string. */
2435 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2436
2437 for (i = 0; i < it->n_overlay_strings; ++i)
2438 {
2439 const char *s = SDATA (it->overlay_strings[i]);
2440 const char *e = s + SBYTES (it->overlay_strings[i]);
2441
2442 while (s < e && *s != '\n')
2443 ++s;
2444
2445 if (s < e)
2446 {
2447 overlay_strings_with_newlines = 1;
2448 break;
2449 }
2450 }
2451
2452 /* If position is within an overlay string, set up IT to the right
2453 overlay string. */
2454 if (pos->overlay_string_index >= 0)
2455 {
2456 int relative_index;
2457
2458 /* If the first overlay string happens to have a `display'
2459 property for an image, the iterator will be set up for that
2460 image, and we have to undo that setup first before we can
2461 correct the overlay string index. */
2462 if (it->method == next_element_from_image)
2463 pop_it (it);
2464
2465 /* We already have the first chunk of overlay strings in
2466 IT->overlay_strings. Load more until the one for
2467 pos->overlay_string_index is in IT->overlay_strings. */
2468 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2469 {
2470 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2471 it->current.overlay_string_index = 0;
2472 while (n--)
2473 {
2474 load_overlay_strings (it, 0);
2475 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2476 }
2477 }
2478
2479 it->current.overlay_string_index = pos->overlay_string_index;
2480 relative_index = (it->current.overlay_string_index
2481 % OVERLAY_STRING_CHUNK_SIZE);
2482 it->string = it->overlay_strings[relative_index];
2483 xassert (STRINGP (it->string));
2484 it->current.string_pos = pos->string_pos;
2485 it->method = next_element_from_string;
2486 }
2487
2488 #if 0 /* This is bogus because POS not having an overlay string
2489 position does not mean it's after the string. Example: A
2490 line starting with a before-string and initialization of IT
2491 to the previous row's end position. */
2492 else if (it->current.overlay_string_index >= 0)
2493 {
2494 /* If POS says we're already after an overlay string ending at
2495 POS, make sure to pop the iterator because it will be in
2496 front of that overlay string. When POS is ZV, we've thereby
2497 also ``processed'' overlay strings at ZV. */
2498 while (it->sp)
2499 pop_it (it);
2500 it->current.overlay_string_index = -1;
2501 it->method = next_element_from_buffer;
2502 if (CHARPOS (pos->pos) == ZV)
2503 it->overlay_strings_at_end_processed_p = 1;
2504 }
2505 #endif /* 0 */
2506
2507 if (CHARPOS (pos->string_pos) >= 0)
2508 {
2509 /* Recorded position is not in an overlay string, but in another
2510 string. This can only be a string from a `display' property.
2511 IT should already be filled with that string. */
2512 it->current.string_pos = pos->string_pos;
2513 xassert (STRINGP (it->string));
2514 }
2515
2516 /* Restore position in display vector translations, control
2517 character translations or ellipses. */
2518 if (pos->dpvec_index >= 0)
2519 {
2520 if (it->dpvec == NULL)
2521 get_next_display_element (it);
2522 xassert (it->dpvec && it->current.dpvec_index == 0);
2523 it->current.dpvec_index = pos->dpvec_index;
2524 }
2525
2526 CHECK_IT (it);
2527 return !overlay_strings_with_newlines;
2528 }
2529
2530
2531 /* Initialize IT for stepping through current_buffer in window W
2532 starting at ROW->start. */
2533
2534 static void
2535 init_to_row_start (it, w, row)
2536 struct it *it;
2537 struct window *w;
2538 struct glyph_row *row;
2539 {
2540 init_from_display_pos (it, w, &row->start);
2541 it->start = row->start;
2542 it->continuation_lines_width = row->continuation_lines_width;
2543 CHECK_IT (it);
2544 }
2545
2546
2547 /* Initialize IT for stepping through current_buffer in window W
2548 starting in the line following ROW, i.e. starting at ROW->end.
2549 Value is zero if there are overlay strings with newlines at ROW's
2550 end position. */
2551
2552 static int
2553 init_to_row_end (it, w, row)
2554 struct it *it;
2555 struct window *w;
2556 struct glyph_row *row;
2557 {
2558 int success = 0;
2559
2560 if (init_from_display_pos (it, w, &row->end))
2561 {
2562 if (row->continued_p)
2563 it->continuation_lines_width
2564 = row->continuation_lines_width + row->pixel_width;
2565 CHECK_IT (it);
2566 success = 1;
2567 }
2568
2569 return success;
2570 }
2571
2572
2573
2574 \f
2575 /***********************************************************************
2576 Text properties
2577 ***********************************************************************/
2578
2579 /* Called when IT reaches IT->stop_charpos. Handle text property and
2580 overlay changes. Set IT->stop_charpos to the next position where
2581 to stop. */
2582
2583 static void
2584 handle_stop (it)
2585 struct it *it;
2586 {
2587 enum prop_handled handled;
2588 int handle_overlay_change_p = 1;
2589 struct props *p;
2590
2591 it->dpvec = NULL;
2592 it->current.dpvec_index = -1;
2593
2594 do
2595 {
2596 handled = HANDLED_NORMALLY;
2597
2598 /* Call text property handlers. */
2599 for (p = it_props; p->handler; ++p)
2600 {
2601 handled = p->handler (it);
2602
2603 if (handled == HANDLED_RECOMPUTE_PROPS)
2604 break;
2605 else if (handled == HANDLED_RETURN)
2606 return;
2607 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2608 handle_overlay_change_p = 0;
2609 }
2610
2611 if (handled != HANDLED_RECOMPUTE_PROPS)
2612 {
2613 /* Don't check for overlay strings below when set to deliver
2614 characters from a display vector. */
2615 if (it->method == next_element_from_display_vector)
2616 handle_overlay_change_p = 0;
2617
2618 /* Handle overlay changes. */
2619 if (handle_overlay_change_p)
2620 handled = handle_overlay_change (it);
2621
2622 /* Determine where to stop next. */
2623 if (handled == HANDLED_NORMALLY)
2624 compute_stop_pos (it);
2625 }
2626 }
2627 while (handled == HANDLED_RECOMPUTE_PROPS);
2628 }
2629
2630
2631 /* Compute IT->stop_charpos from text property and overlay change
2632 information for IT's current position. */
2633
2634 static void
2635 compute_stop_pos (it)
2636 struct it *it;
2637 {
2638 register INTERVAL iv, next_iv;
2639 Lisp_Object object, limit, position;
2640
2641 /* If nowhere else, stop at the end. */
2642 it->stop_charpos = it->end_charpos;
2643
2644 if (STRINGP (it->string))
2645 {
2646 /* Strings are usually short, so don't limit the search for
2647 properties. */
2648 object = it->string;
2649 limit = Qnil;
2650 position = make_number (IT_STRING_CHARPOS (*it));
2651 }
2652 else
2653 {
2654 int charpos;
2655
2656 /* If next overlay change is in front of the current stop pos
2657 (which is IT->end_charpos), stop there. Note: value of
2658 next_overlay_change is point-max if no overlay change
2659 follows. */
2660 charpos = next_overlay_change (IT_CHARPOS (*it));
2661 if (charpos < it->stop_charpos)
2662 it->stop_charpos = charpos;
2663
2664 /* If showing the region, we have to stop at the region
2665 start or end because the face might change there. */
2666 if (it->region_beg_charpos > 0)
2667 {
2668 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2669 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2670 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2671 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2672 }
2673
2674 /* Set up variables for computing the stop position from text
2675 property changes. */
2676 XSETBUFFER (object, current_buffer);
2677 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2678 position = make_number (IT_CHARPOS (*it));
2679
2680 }
2681
2682 /* Get the interval containing IT's position. Value is a null
2683 interval if there isn't such an interval. */
2684 iv = validate_interval_range (object, &position, &position, 0);
2685 if (!NULL_INTERVAL_P (iv))
2686 {
2687 Lisp_Object values_here[LAST_PROP_IDX];
2688 struct props *p;
2689
2690 /* Get properties here. */
2691 for (p = it_props; p->handler; ++p)
2692 values_here[p->idx] = textget (iv->plist, *p->name);
2693
2694 /* Look for an interval following iv that has different
2695 properties. */
2696 for (next_iv = next_interval (iv);
2697 (!NULL_INTERVAL_P (next_iv)
2698 && (NILP (limit)
2699 || XFASTINT (limit) > next_iv->position));
2700 next_iv = next_interval (next_iv))
2701 {
2702 for (p = it_props; p->handler; ++p)
2703 {
2704 Lisp_Object new_value;
2705
2706 new_value = textget (next_iv->plist, *p->name);
2707 if (!EQ (values_here[p->idx], new_value))
2708 break;
2709 }
2710
2711 if (p->handler)
2712 break;
2713 }
2714
2715 if (!NULL_INTERVAL_P (next_iv))
2716 {
2717 if (INTEGERP (limit)
2718 && next_iv->position >= XFASTINT (limit))
2719 /* No text property change up to limit. */
2720 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2721 else
2722 /* Text properties change in next_iv. */
2723 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2724 }
2725 }
2726
2727 xassert (STRINGP (it->string)
2728 || (it->stop_charpos >= BEGV
2729 && it->stop_charpos >= IT_CHARPOS (*it)));
2730 }
2731
2732
2733 /* Return the position of the next overlay change after POS in
2734 current_buffer. Value is point-max if no overlay change
2735 follows. This is like `next-overlay-change' but doesn't use
2736 xmalloc. */
2737
2738 static int
2739 next_overlay_change (pos)
2740 int pos;
2741 {
2742 int noverlays;
2743 int endpos;
2744 Lisp_Object *overlays;
2745 int i;
2746
2747 /* Get all overlays at the given position. */
2748 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2749
2750 /* If any of these overlays ends before endpos,
2751 use its ending point instead. */
2752 for (i = 0; i < noverlays; ++i)
2753 {
2754 Lisp_Object oend;
2755 int oendpos;
2756
2757 oend = OVERLAY_END (overlays[i]);
2758 oendpos = OVERLAY_POSITION (oend);
2759 endpos = min (endpos, oendpos);
2760 }
2761
2762 return endpos;
2763 }
2764
2765
2766 \f
2767 /***********************************************************************
2768 Fontification
2769 ***********************************************************************/
2770
2771 /* Handle changes in the `fontified' property of the current buffer by
2772 calling hook functions from Qfontification_functions to fontify
2773 regions of text. */
2774
2775 static enum prop_handled
2776 handle_fontified_prop (it)
2777 struct it *it;
2778 {
2779 Lisp_Object prop, pos;
2780 enum prop_handled handled = HANDLED_NORMALLY;
2781
2782 /* Get the value of the `fontified' property at IT's current buffer
2783 position. (The `fontified' property doesn't have a special
2784 meaning in strings.) If the value is nil, call functions from
2785 Qfontification_functions. */
2786 if (!STRINGP (it->string)
2787 && it->s == NULL
2788 && !NILP (Vfontification_functions)
2789 && !NILP (Vrun_hooks)
2790 && (pos = make_number (IT_CHARPOS (*it)),
2791 prop = Fget_char_property (pos, Qfontified, Qnil),
2792 NILP (prop)))
2793 {
2794 int count = SPECPDL_INDEX ();
2795 Lisp_Object val;
2796
2797 val = Vfontification_functions;
2798 specbind (Qfontification_functions, Qnil);
2799
2800 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2801 safe_call1 (val, pos);
2802 else
2803 {
2804 Lisp_Object globals, fn;
2805 struct gcpro gcpro1, gcpro2;
2806
2807 globals = Qnil;
2808 GCPRO2 (val, globals);
2809
2810 for (; CONSP (val); val = XCDR (val))
2811 {
2812 fn = XCAR (val);
2813
2814 if (EQ (fn, Qt))
2815 {
2816 /* A value of t indicates this hook has a local
2817 binding; it means to run the global binding too.
2818 In a global value, t should not occur. If it
2819 does, we must ignore it to avoid an endless
2820 loop. */
2821 for (globals = Fdefault_value (Qfontification_functions);
2822 CONSP (globals);
2823 globals = XCDR (globals))
2824 {
2825 fn = XCAR (globals);
2826 if (!EQ (fn, Qt))
2827 safe_call1 (fn, pos);
2828 }
2829 }
2830 else
2831 safe_call1 (fn, pos);
2832 }
2833
2834 UNGCPRO;
2835 }
2836
2837 unbind_to (count, Qnil);
2838
2839 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2840 something. This avoids an endless loop if they failed to
2841 fontify the text for which reason ever. */
2842 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2843 handled = HANDLED_RECOMPUTE_PROPS;
2844 }
2845
2846 return handled;
2847 }
2848
2849
2850 \f
2851 /***********************************************************************
2852 Faces
2853 ***********************************************************************/
2854
2855 /* Set up iterator IT from face properties at its current position.
2856 Called from handle_stop. */
2857
2858 static enum prop_handled
2859 handle_face_prop (it)
2860 struct it *it;
2861 {
2862 int new_face_id, next_stop;
2863
2864 if (!STRINGP (it->string))
2865 {
2866 new_face_id
2867 = face_at_buffer_position (it->w,
2868 IT_CHARPOS (*it),
2869 it->region_beg_charpos,
2870 it->region_end_charpos,
2871 &next_stop,
2872 (IT_CHARPOS (*it)
2873 + TEXT_PROP_DISTANCE_LIMIT),
2874 0);
2875
2876 /* Is this a start of a run of characters with box face?
2877 Caveat: this can be called for a freshly initialized
2878 iterator; face_id is -1 in this case. We know that the new
2879 face will not change until limit, i.e. if the new face has a
2880 box, all characters up to limit will have one. But, as
2881 usual, we don't know whether limit is really the end. */
2882 if (new_face_id != it->face_id)
2883 {
2884 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2885
2886 /* If new face has a box but old face has not, this is
2887 the start of a run of characters with box, i.e. it has
2888 a shadow on the left side. The value of face_id of the
2889 iterator will be -1 if this is the initial call that gets
2890 the face. In this case, we have to look in front of IT's
2891 position and see whether there is a face != new_face_id. */
2892 it->start_of_box_run_p
2893 = (new_face->box != FACE_NO_BOX
2894 && (it->face_id >= 0
2895 || IT_CHARPOS (*it) == BEG
2896 || new_face_id != face_before_it_pos (it)));
2897 it->face_box_p = new_face->box != FACE_NO_BOX;
2898 }
2899 }
2900 else
2901 {
2902 int base_face_id, bufpos;
2903
2904 if (it->current.overlay_string_index >= 0)
2905 bufpos = IT_CHARPOS (*it);
2906 else
2907 bufpos = 0;
2908
2909 /* For strings from a buffer, i.e. overlay strings or strings
2910 from a `display' property, use the face at IT's current
2911 buffer position as the base face to merge with, so that
2912 overlay strings appear in the same face as surrounding
2913 text, unless they specify their own faces. */
2914 base_face_id = underlying_face_id (it);
2915
2916 new_face_id = face_at_string_position (it->w,
2917 it->string,
2918 IT_STRING_CHARPOS (*it),
2919 bufpos,
2920 it->region_beg_charpos,
2921 it->region_end_charpos,
2922 &next_stop,
2923 base_face_id, 0);
2924
2925 #if 0 /* This shouldn't be neccessary. Let's check it. */
2926 /* If IT is used to display a mode line we would really like to
2927 use the mode line face instead of the frame's default face. */
2928 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2929 && new_face_id == DEFAULT_FACE_ID)
2930 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2931 #endif
2932
2933 /* Is this a start of a run of characters with box? Caveat:
2934 this can be called for a freshly allocated iterator; face_id
2935 is -1 is this case. We know that the new face will not
2936 change until the next check pos, i.e. if the new face has a
2937 box, all characters up to that position will have a
2938 box. But, as usual, we don't know whether that position
2939 is really the end. */
2940 if (new_face_id != it->face_id)
2941 {
2942 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2943 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2944
2945 /* If new face has a box but old face hasn't, this is the
2946 start of a run of characters with box, i.e. it has a
2947 shadow on the left side. */
2948 it->start_of_box_run_p
2949 = new_face->box && (old_face == NULL || !old_face->box);
2950 it->face_box_p = new_face->box != FACE_NO_BOX;
2951 }
2952 }
2953
2954 it->face_id = new_face_id;
2955 return HANDLED_NORMALLY;
2956 }
2957
2958
2959 /* Return the ID of the face ``underlying'' IT's current position,
2960 which is in a string. If the iterator is associated with a
2961 buffer, return the face at IT's current buffer position.
2962 Otherwise, use the iterator's base_face_id. */
2963
2964 static int
2965 underlying_face_id (it)
2966 struct it *it;
2967 {
2968 int face_id = it->base_face_id, i;
2969
2970 xassert (STRINGP (it->string));
2971
2972 for (i = it->sp - 1; i >= 0; --i)
2973 if (NILP (it->stack[i].string))
2974 face_id = it->stack[i].face_id;
2975
2976 return face_id;
2977 }
2978
2979
2980 /* Compute the face one character before or after the current position
2981 of IT. BEFORE_P non-zero means get the face in front of IT's
2982 position. Value is the id of the face. */
2983
2984 static int
2985 face_before_or_after_it_pos (it, before_p)
2986 struct it *it;
2987 int before_p;
2988 {
2989 int face_id, limit;
2990 int next_check_charpos;
2991 struct text_pos pos;
2992
2993 xassert (it->s == NULL);
2994
2995 if (STRINGP (it->string))
2996 {
2997 int bufpos, base_face_id;
2998
2999 /* No face change past the end of the string (for the case
3000 we are padding with spaces). No face change before the
3001 string start. */
3002 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3003 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3004 return it->face_id;
3005
3006 /* Set pos to the position before or after IT's current position. */
3007 if (before_p)
3008 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3009 else
3010 /* For composition, we must check the character after the
3011 composition. */
3012 pos = (it->what == IT_COMPOSITION
3013 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3014 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3015
3016 if (it->current.overlay_string_index >= 0)
3017 bufpos = IT_CHARPOS (*it);
3018 else
3019 bufpos = 0;
3020
3021 base_face_id = underlying_face_id (it);
3022
3023 /* Get the face for ASCII, or unibyte. */
3024 face_id = face_at_string_position (it->w,
3025 it->string,
3026 CHARPOS (pos),
3027 bufpos,
3028 it->region_beg_charpos,
3029 it->region_end_charpos,
3030 &next_check_charpos,
3031 base_face_id, 0);
3032
3033 /* Correct the face for charsets different from ASCII. Do it
3034 for the multibyte case only. The face returned above is
3035 suitable for unibyte text if IT->string is unibyte. */
3036 if (STRING_MULTIBYTE (it->string))
3037 {
3038 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3039 int rest = SBYTES (it->string) - BYTEPOS (pos);
3040 int c, len;
3041 struct face *face = FACE_FROM_ID (it->f, face_id);
3042
3043 c = string_char_and_length (p, rest, &len);
3044 face_id = FACE_FOR_CHAR (it->f, face, c);
3045 }
3046 }
3047 else
3048 {
3049 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3050 || (IT_CHARPOS (*it) <= BEGV && before_p))
3051 return it->face_id;
3052
3053 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3054 pos = it->current.pos;
3055
3056 if (before_p)
3057 DEC_TEXT_POS (pos, it->multibyte_p);
3058 else
3059 {
3060 if (it->what == IT_COMPOSITION)
3061 /* For composition, we must check the position after the
3062 composition. */
3063 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3064 else
3065 INC_TEXT_POS (pos, it->multibyte_p);
3066 }
3067
3068 /* Determine face for CHARSET_ASCII, or unibyte. */
3069 face_id = face_at_buffer_position (it->w,
3070 CHARPOS (pos),
3071 it->region_beg_charpos,
3072 it->region_end_charpos,
3073 &next_check_charpos,
3074 limit, 0);
3075
3076 /* Correct the face for charsets different from ASCII. Do it
3077 for the multibyte case only. The face returned above is
3078 suitable for unibyte text if current_buffer is unibyte. */
3079 if (it->multibyte_p)
3080 {
3081 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3082 struct face *face = FACE_FROM_ID (it->f, face_id);
3083 face_id = FACE_FOR_CHAR (it->f, face, c);
3084 }
3085 }
3086
3087 return face_id;
3088 }
3089
3090
3091 \f
3092 /***********************************************************************
3093 Invisible text
3094 ***********************************************************************/
3095
3096 /* Set up iterator IT from invisible properties at its current
3097 position. Called from handle_stop. */
3098
3099 static enum prop_handled
3100 handle_invisible_prop (it)
3101 struct it *it;
3102 {
3103 enum prop_handled handled = HANDLED_NORMALLY;
3104
3105 if (STRINGP (it->string))
3106 {
3107 extern Lisp_Object Qinvisible;
3108 Lisp_Object prop, end_charpos, limit, charpos;
3109
3110 /* Get the value of the invisible text property at the
3111 current position. Value will be nil if there is no such
3112 property. */
3113 charpos = make_number (IT_STRING_CHARPOS (*it));
3114 prop = Fget_text_property (charpos, Qinvisible, it->string);
3115
3116 if (!NILP (prop)
3117 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3118 {
3119 handled = HANDLED_RECOMPUTE_PROPS;
3120
3121 /* Get the position at which the next change of the
3122 invisible text property can be found in IT->string.
3123 Value will be nil if the property value is the same for
3124 all the rest of IT->string. */
3125 XSETINT (limit, SCHARS (it->string));
3126 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3127 it->string, limit);
3128
3129 /* Text at current position is invisible. The next
3130 change in the property is at position end_charpos.
3131 Move IT's current position to that position. */
3132 if (INTEGERP (end_charpos)
3133 && XFASTINT (end_charpos) < XFASTINT (limit))
3134 {
3135 struct text_pos old;
3136 old = it->current.string_pos;
3137 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3138 compute_string_pos (&it->current.string_pos, old, it->string);
3139 }
3140 else
3141 {
3142 /* The rest of the string is invisible. If this is an
3143 overlay string, proceed with the next overlay string
3144 or whatever comes and return a character from there. */
3145 if (it->current.overlay_string_index >= 0)
3146 {
3147 next_overlay_string (it);
3148 /* Don't check for overlay strings when we just
3149 finished processing them. */
3150 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3151 }
3152 else
3153 {
3154 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3155 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3156 }
3157 }
3158 }
3159 }
3160 else
3161 {
3162 int invis_p, newpos, next_stop, start_charpos;
3163 Lisp_Object pos, prop, overlay;
3164
3165 /* First of all, is there invisible text at this position? */
3166 start_charpos = IT_CHARPOS (*it);
3167 pos = make_number (IT_CHARPOS (*it));
3168 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3169 &overlay);
3170 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3171
3172 /* If we are on invisible text, skip over it. */
3173 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3174 {
3175 /* Record whether we have to display an ellipsis for the
3176 invisible text. */
3177 int display_ellipsis_p = invis_p == 2;
3178
3179 handled = HANDLED_RECOMPUTE_PROPS;
3180
3181 /* Loop skipping over invisible text. The loop is left at
3182 ZV or with IT on the first char being visible again. */
3183 do
3184 {
3185 /* Try to skip some invisible text. Return value is the
3186 position reached which can be equal to IT's position
3187 if there is nothing invisible here. This skips both
3188 over invisible text properties and overlays with
3189 invisible property. */
3190 newpos = skip_invisible (IT_CHARPOS (*it),
3191 &next_stop, ZV, it->window);
3192
3193 /* If we skipped nothing at all we weren't at invisible
3194 text in the first place. If everything to the end of
3195 the buffer was skipped, end the loop. */
3196 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3197 invis_p = 0;
3198 else
3199 {
3200 /* We skipped some characters but not necessarily
3201 all there are. Check if we ended up on visible
3202 text. Fget_char_property returns the property of
3203 the char before the given position, i.e. if we
3204 get invis_p = 0, this means that the char at
3205 newpos is visible. */
3206 pos = make_number (newpos);
3207 prop = Fget_char_property (pos, Qinvisible, it->window);
3208 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3209 }
3210
3211 /* If we ended up on invisible text, proceed to
3212 skip starting with next_stop. */
3213 if (invis_p)
3214 IT_CHARPOS (*it) = next_stop;
3215 }
3216 while (invis_p);
3217
3218 /* The position newpos is now either ZV or on visible text. */
3219 IT_CHARPOS (*it) = newpos;
3220 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3221
3222 /* If there are before-strings at the start of invisible
3223 text, and the text is invisible because of a text
3224 property, arrange to show before-strings because 20.x did
3225 it that way. (If the text is invisible because of an
3226 overlay property instead of a text property, this is
3227 already handled in the overlay code.) */
3228 if (NILP (overlay)
3229 && get_overlay_strings (it, start_charpos))
3230 {
3231 handled = HANDLED_RECOMPUTE_PROPS;
3232 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3233 }
3234 else if (display_ellipsis_p)
3235 setup_for_ellipsis (it, 0);
3236 }
3237 }
3238
3239 return handled;
3240 }
3241
3242
3243 /* Make iterator IT return `...' next.
3244 Replaces LEN characters from buffer. */
3245
3246 static void
3247 setup_for_ellipsis (it, len)
3248 struct it *it;
3249 int len;
3250 {
3251 /* Use the display table definition for `...'. Invalid glyphs
3252 will be handled by the method returning elements from dpvec. */
3253 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3254 {
3255 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3256 it->dpvec = v->contents;
3257 it->dpend = v->contents + v->size;
3258 }
3259 else
3260 {
3261 /* Default `...'. */
3262 it->dpvec = default_invis_vector;
3263 it->dpend = default_invis_vector + 3;
3264 }
3265
3266 it->dpvec_char_len = len;
3267 it->current.dpvec_index = 0;
3268
3269 /* Remember the current face id in case glyphs specify faces.
3270 IT's face is restored in set_iterator_to_next. */
3271 it->saved_face_id = it->face_id;
3272 it->method = next_element_from_display_vector;
3273 }
3274
3275
3276 \f
3277 /***********************************************************************
3278 'display' property
3279 ***********************************************************************/
3280
3281 /* Set up iterator IT from `display' property at its current position.
3282 Called from handle_stop. */
3283
3284 static enum prop_handled
3285 handle_display_prop (it)
3286 struct it *it;
3287 {
3288 Lisp_Object prop, object;
3289 struct text_pos *position;
3290 int display_replaced_p = 0;
3291
3292 if (STRINGP (it->string))
3293 {
3294 object = it->string;
3295 position = &it->current.string_pos;
3296 }
3297 else
3298 {
3299 object = it->w->buffer;
3300 position = &it->current.pos;
3301 }
3302
3303 /* Reset those iterator values set from display property values. */
3304 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3305 it->space_width = Qnil;
3306 it->font_height = Qnil;
3307 it->voffset = 0;
3308
3309 /* We don't support recursive `display' properties, i.e. string
3310 values that have a string `display' property, that have a string
3311 `display' property etc. */
3312 if (!it->string_from_display_prop_p)
3313 it->area = TEXT_AREA;
3314
3315 prop = Fget_char_property (make_number (position->charpos),
3316 Qdisplay, object);
3317 if (NILP (prop))
3318 return HANDLED_NORMALLY;
3319
3320 if (CONSP (prop)
3321 /* Simple properties. */
3322 && !EQ (XCAR (prop), Qimage)
3323 && !EQ (XCAR (prop), Qspace)
3324 && !EQ (XCAR (prop), Qwhen)
3325 && !EQ (XCAR (prop), Qslice)
3326 && !EQ (XCAR (prop), Qspace_width)
3327 && !EQ (XCAR (prop), Qheight)
3328 && !EQ (XCAR (prop), Qraise)
3329 /* Marginal area specifications. */
3330 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3331 && !EQ (XCAR (prop), Qleft_fringe)
3332 && !EQ (XCAR (prop), Qright_fringe)
3333 && !NILP (XCAR (prop)))
3334 {
3335 for (; CONSP (prop); prop = XCDR (prop))
3336 {
3337 if (handle_single_display_prop (it, XCAR (prop), object,
3338 position, display_replaced_p))
3339 display_replaced_p = 1;
3340 }
3341 }
3342 else if (VECTORP (prop))
3343 {
3344 int i;
3345 for (i = 0; i < ASIZE (prop); ++i)
3346 if (handle_single_display_prop (it, AREF (prop, i), object,
3347 position, display_replaced_p))
3348 display_replaced_p = 1;
3349 }
3350 else
3351 {
3352 if (handle_single_display_prop (it, prop, object, position, 0))
3353 display_replaced_p = 1;
3354 }
3355
3356 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3357 }
3358
3359
3360 /* Value is the position of the end of the `display' property starting
3361 at START_POS in OBJECT. */
3362
3363 static struct text_pos
3364 display_prop_end (it, object, start_pos)
3365 struct it *it;
3366 Lisp_Object object;
3367 struct text_pos start_pos;
3368 {
3369 Lisp_Object end;
3370 struct text_pos end_pos;
3371
3372 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3373 Qdisplay, object, Qnil);
3374 CHARPOS (end_pos) = XFASTINT (end);
3375 if (STRINGP (object))
3376 compute_string_pos (&end_pos, start_pos, it->string);
3377 else
3378 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3379
3380 return end_pos;
3381 }
3382
3383
3384 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3385 is the object in which the `display' property was found. *POSITION
3386 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3387 means that we previously saw a display sub-property which already
3388 replaced text display with something else, for example an image;
3389 ignore such properties after the first one has been processed.
3390
3391 If PROP is a `space' or `image' sub-property, set *POSITION to the
3392 end position of the `display' property.
3393
3394 Value is non-zero if something was found which replaces the display
3395 of buffer or string text. */
3396
3397 static int
3398 handle_single_display_prop (it, prop, object, position,
3399 display_replaced_before_p)
3400 struct it *it;
3401 Lisp_Object prop;
3402 Lisp_Object object;
3403 struct text_pos *position;
3404 int display_replaced_before_p;
3405 {
3406 Lisp_Object value;
3407 int replaces_text_display_p = 0;
3408 Lisp_Object form;
3409
3410 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3411 evaluated. If the result is nil, VALUE is ignored. */
3412 form = Qt;
3413 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3414 {
3415 prop = XCDR (prop);
3416 if (!CONSP (prop))
3417 return 0;
3418 form = XCAR (prop);
3419 prop = XCDR (prop);
3420 }
3421
3422 if (!NILP (form) && !EQ (form, Qt))
3423 {
3424 int count = SPECPDL_INDEX ();
3425 struct gcpro gcpro1;
3426
3427 /* Bind `object' to the object having the `display' property, a
3428 buffer or string. Bind `position' to the position in the
3429 object where the property was found, and `buffer-position'
3430 to the current position in the buffer. */
3431 specbind (Qobject, object);
3432 specbind (Qposition, make_number (CHARPOS (*position)));
3433 specbind (Qbuffer_position,
3434 make_number (STRINGP (object)
3435 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3436 GCPRO1 (form);
3437 form = safe_eval (form);
3438 UNGCPRO;
3439 unbind_to (count, Qnil);
3440 }
3441
3442 if (NILP (form))
3443 return 0;
3444
3445 if (CONSP (prop)
3446 && EQ (XCAR (prop), Qheight)
3447 && CONSP (XCDR (prop)))
3448 {
3449 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3450 return 0;
3451
3452 /* `(height HEIGHT)'. */
3453 it->font_height = XCAR (XCDR (prop));
3454 if (!NILP (it->font_height))
3455 {
3456 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3457 int new_height = -1;
3458
3459 if (CONSP (it->font_height)
3460 && (EQ (XCAR (it->font_height), Qplus)
3461 || EQ (XCAR (it->font_height), Qminus))
3462 && CONSP (XCDR (it->font_height))
3463 && INTEGERP (XCAR (XCDR (it->font_height))))
3464 {
3465 /* `(+ N)' or `(- N)' where N is an integer. */
3466 int steps = XINT (XCAR (XCDR (it->font_height)));
3467 if (EQ (XCAR (it->font_height), Qplus))
3468 steps = - steps;
3469 it->face_id = smaller_face (it->f, it->face_id, steps);
3470 }
3471 else if (FUNCTIONP (it->font_height))
3472 {
3473 /* Call function with current height as argument.
3474 Value is the new height. */
3475 Lisp_Object height;
3476 height = safe_call1 (it->font_height,
3477 face->lface[LFACE_HEIGHT_INDEX]);
3478 if (NUMBERP (height))
3479 new_height = XFLOATINT (height);
3480 }
3481 else if (NUMBERP (it->font_height))
3482 {
3483 /* Value is a multiple of the canonical char height. */
3484 struct face *face;
3485
3486 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3487 new_height = (XFLOATINT (it->font_height)
3488 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3489 }
3490 else
3491 {
3492 /* Evaluate IT->font_height with `height' bound to the
3493 current specified height to get the new height. */
3494 Lisp_Object value;
3495 int count = SPECPDL_INDEX ();
3496
3497 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3498 value = safe_eval (it->font_height);
3499 unbind_to (count, Qnil);
3500
3501 if (NUMBERP (value))
3502 new_height = XFLOATINT (value);
3503 }
3504
3505 if (new_height > 0)
3506 it->face_id = face_with_height (it->f, it->face_id, new_height);
3507 }
3508 }
3509 else if (CONSP (prop)
3510 && EQ (XCAR (prop), Qspace_width)
3511 && CONSP (XCDR (prop)))
3512 {
3513 /* `(space_width WIDTH)'. */
3514 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3515 return 0;
3516
3517 value = XCAR (XCDR (prop));
3518 if (NUMBERP (value) && XFLOATINT (value) > 0)
3519 it->space_width = value;
3520 }
3521 else if (CONSP (prop)
3522 && EQ (XCAR (prop), Qslice))
3523 {
3524 /* `(slice X Y WIDTH HEIGHT)'. */
3525 Lisp_Object tem;
3526
3527 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3528 return 0;
3529
3530 if (tem = XCDR (prop), CONSP (tem))
3531 {
3532 it->slice.x = XCAR (tem);
3533 if (tem = XCDR (tem), CONSP (tem))
3534 {
3535 it->slice.y = XCAR (tem);
3536 if (tem = XCDR (tem), CONSP (tem))
3537 {
3538 it->slice.width = XCAR (tem);
3539 if (tem = XCDR (tem), CONSP (tem))
3540 it->slice.height = XCAR (tem);
3541 }
3542 }
3543 }
3544 }
3545 else if (CONSP (prop)
3546 && EQ (XCAR (prop), Qraise)
3547 && CONSP (XCDR (prop)))
3548 {
3549 /* `(raise FACTOR)'. */
3550 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3551 return 0;
3552
3553 #ifdef HAVE_WINDOW_SYSTEM
3554 value = XCAR (XCDR (prop));
3555 if (NUMBERP (value))
3556 {
3557 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3558 it->voffset = - (XFLOATINT (value)
3559 * (FONT_HEIGHT (face->font)));
3560 }
3561 #endif /* HAVE_WINDOW_SYSTEM */
3562 }
3563 else if (!it->string_from_display_prop_p)
3564 {
3565 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3566 VALUE) or `((margin nil) VALUE)' or VALUE. */
3567 Lisp_Object location, value;
3568 struct text_pos start_pos;
3569 int valid_p;
3570
3571 /* Characters having this form of property are not displayed, so
3572 we have to find the end of the property. */
3573 start_pos = *position;
3574 *position = display_prop_end (it, object, start_pos);
3575 value = Qnil;
3576
3577 /* Let's stop at the new position and assume that all
3578 text properties change there. */
3579 it->stop_charpos = position->charpos;
3580
3581 if (CONSP (prop)
3582 && (EQ (XCAR (prop), Qleft_fringe)
3583 || EQ (XCAR (prop), Qright_fringe))
3584 && CONSP (XCDR (prop)))
3585 {
3586 int face_id = DEFAULT_FACE_ID;
3587 int fringe_bitmap;
3588
3589 /* Save current settings of IT so that we can restore them
3590 when we are finished with the glyph property value. */
3591
3592 /* `(left-fringe BITMAP FACE)'. */
3593 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3594 return 0;
3595
3596 #ifdef HAVE_WINDOW_SYSTEM
3597 value = XCAR (XCDR (prop));
3598 if (!SYMBOLP (value)
3599 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3600 return 0;
3601
3602 if (CONSP (XCDR (XCDR (prop))))
3603 {
3604 Lisp_Object face_name = XCAR (XCDR (XCDR (prop)));
3605 int face_id2 = lookup_named_face (it->f, face_name, 'A', 0);
3606 if (face_id2 >= 0)
3607 face_id = face_id2;
3608 }
3609
3610 push_it (it);
3611
3612 it->area = TEXT_AREA;
3613 it->what = IT_IMAGE;
3614 it->image_id = -1; /* no image */
3615 it->position = start_pos;
3616 it->object = NILP (object) ? it->w->buffer : object;
3617 it->method = next_element_from_image;
3618 it->face_id = face_id;
3619
3620 /* Say that we haven't consumed the characters with
3621 `display' property yet. The call to pop_it in
3622 set_iterator_to_next will clean this up. */
3623 *position = start_pos;
3624
3625 if (EQ (XCAR (prop), Qleft_fringe))
3626 {
3627 it->left_user_fringe_bitmap = fringe_bitmap;
3628 it->left_user_fringe_face_id = face_id;
3629 }
3630 else
3631 {
3632 it->right_user_fringe_bitmap = fringe_bitmap;
3633 it->right_user_fringe_face_id = face_id;
3634 }
3635 #endif /* HAVE_WINDOW_SYSTEM */
3636 return 1;
3637 }
3638
3639 location = Qunbound;
3640 if (CONSP (prop) && CONSP (XCAR (prop)))
3641 {
3642 Lisp_Object tem;
3643
3644 value = XCDR (prop);
3645 if (CONSP (value))
3646 value = XCAR (value);
3647
3648 tem = XCAR (prop);
3649 if (EQ (XCAR (tem), Qmargin)
3650 && (tem = XCDR (tem),
3651 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3652 (NILP (tem)
3653 || EQ (tem, Qleft_margin)
3654 || EQ (tem, Qright_margin))))
3655 location = tem;
3656 }
3657
3658 if (EQ (location, Qunbound))
3659 {
3660 location = Qnil;
3661 value = prop;
3662 }
3663
3664 valid_p = (STRINGP (value)
3665 #ifdef HAVE_WINDOW_SYSTEM
3666 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3667 #endif /* not HAVE_WINDOW_SYSTEM */
3668 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3669
3670 if ((EQ (location, Qleft_margin)
3671 || EQ (location, Qright_margin)
3672 || NILP (location))
3673 && valid_p
3674 && !display_replaced_before_p)
3675 {
3676 replaces_text_display_p = 1;
3677
3678 /* Save current settings of IT so that we can restore them
3679 when we are finished with the glyph property value. */
3680 push_it (it);
3681
3682 if (NILP (location))
3683 it->area = TEXT_AREA;
3684 else if (EQ (location, Qleft_margin))
3685 it->area = LEFT_MARGIN_AREA;
3686 else
3687 it->area = RIGHT_MARGIN_AREA;
3688
3689 if (STRINGP (value))
3690 {
3691 it->string = value;
3692 it->multibyte_p = STRING_MULTIBYTE (it->string);
3693 it->current.overlay_string_index = -1;
3694 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3695 it->end_charpos = it->string_nchars = SCHARS (it->string);
3696 it->method = next_element_from_string;
3697 it->stop_charpos = 0;
3698 it->string_from_display_prop_p = 1;
3699 /* Say that we haven't consumed the characters with
3700 `display' property yet. The call to pop_it in
3701 set_iterator_to_next will clean this up. */
3702 *position = start_pos;
3703 }
3704 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3705 {
3706 it->method = next_element_from_stretch;
3707 it->object = value;
3708 it->current.pos = it->position = start_pos;
3709 }
3710 #ifdef HAVE_WINDOW_SYSTEM
3711 else
3712 {
3713 it->what = IT_IMAGE;
3714 it->image_id = lookup_image (it->f, value);
3715 it->position = start_pos;
3716 it->object = NILP (object) ? it->w->buffer : object;
3717 it->method = next_element_from_image;
3718
3719 /* Say that we haven't consumed the characters with
3720 `display' property yet. The call to pop_it in
3721 set_iterator_to_next will clean this up. */
3722 *position = start_pos;
3723 }
3724 #endif /* HAVE_WINDOW_SYSTEM */
3725 }
3726 else
3727 /* Invalid property or property not supported. Restore
3728 the position to what it was before. */
3729 *position = start_pos;
3730 }
3731
3732 return replaces_text_display_p;
3733 }
3734
3735
3736 /* Check if PROP is a display sub-property value whose text should be
3737 treated as intangible. */
3738
3739 static int
3740 single_display_prop_intangible_p (prop)
3741 Lisp_Object prop;
3742 {
3743 /* Skip over `when FORM'. */
3744 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3745 {
3746 prop = XCDR (prop);
3747 if (!CONSP (prop))
3748 return 0;
3749 prop = XCDR (prop);
3750 }
3751
3752 if (STRINGP (prop))
3753 return 1;
3754
3755 if (!CONSP (prop))
3756 return 0;
3757
3758 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3759 we don't need to treat text as intangible. */
3760 if (EQ (XCAR (prop), Qmargin))
3761 {
3762 prop = XCDR (prop);
3763 if (!CONSP (prop))
3764 return 0;
3765
3766 prop = XCDR (prop);
3767 if (!CONSP (prop)
3768 || EQ (XCAR (prop), Qleft_margin)
3769 || EQ (XCAR (prop), Qright_margin))
3770 return 0;
3771 }
3772
3773 return (CONSP (prop)
3774 && (EQ (XCAR (prop), Qimage)
3775 || EQ (XCAR (prop), Qspace)));
3776 }
3777
3778
3779 /* Check if PROP is a display property value whose text should be
3780 treated as intangible. */
3781
3782 int
3783 display_prop_intangible_p (prop)
3784 Lisp_Object prop;
3785 {
3786 if (CONSP (prop)
3787 && CONSP (XCAR (prop))
3788 && !EQ (Qmargin, XCAR (XCAR (prop))))
3789 {
3790 /* A list of sub-properties. */
3791 while (CONSP (prop))
3792 {
3793 if (single_display_prop_intangible_p (XCAR (prop)))
3794 return 1;
3795 prop = XCDR (prop);
3796 }
3797 }
3798 else if (VECTORP (prop))
3799 {
3800 /* A vector of sub-properties. */
3801 int i;
3802 for (i = 0; i < ASIZE (prop); ++i)
3803 if (single_display_prop_intangible_p (AREF (prop, i)))
3804 return 1;
3805 }
3806 else
3807 return single_display_prop_intangible_p (prop);
3808
3809 return 0;
3810 }
3811
3812
3813 /* Return 1 if PROP is a display sub-property value containing STRING. */
3814
3815 static int
3816 single_display_prop_string_p (prop, string)
3817 Lisp_Object prop, string;
3818 {
3819 if (EQ (string, prop))
3820 return 1;
3821
3822 /* Skip over `when FORM'. */
3823 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3824 {
3825 prop = XCDR (prop);
3826 if (!CONSP (prop))
3827 return 0;
3828 prop = XCDR (prop);
3829 }
3830
3831 if (CONSP (prop))
3832 /* Skip over `margin LOCATION'. */
3833 if (EQ (XCAR (prop), Qmargin))
3834 {
3835 prop = XCDR (prop);
3836 if (!CONSP (prop))
3837 return 0;
3838
3839 prop = XCDR (prop);
3840 if (!CONSP (prop))
3841 return 0;
3842 }
3843
3844 return CONSP (prop) && EQ (XCAR (prop), string);
3845 }
3846
3847
3848 /* Return 1 if STRING appears in the `display' property PROP. */
3849
3850 static int
3851 display_prop_string_p (prop, string)
3852 Lisp_Object prop, string;
3853 {
3854 if (CONSP (prop)
3855 && CONSP (XCAR (prop))
3856 && !EQ (Qmargin, XCAR (XCAR (prop))))
3857 {
3858 /* A list of sub-properties. */
3859 while (CONSP (prop))
3860 {
3861 if (single_display_prop_string_p (XCAR (prop), string))
3862 return 1;
3863 prop = XCDR (prop);
3864 }
3865 }
3866 else if (VECTORP (prop))
3867 {
3868 /* A vector of sub-properties. */
3869 int i;
3870 for (i = 0; i < ASIZE (prop); ++i)
3871 if (single_display_prop_string_p (AREF (prop, i), string))
3872 return 1;
3873 }
3874 else
3875 return single_display_prop_string_p (prop, string);
3876
3877 return 0;
3878 }
3879
3880
3881 /* Determine from which buffer position in W's buffer STRING comes
3882 from. AROUND_CHARPOS is an approximate position where it could
3883 be from. Value is the buffer position or 0 if it couldn't be
3884 determined.
3885
3886 W's buffer must be current.
3887
3888 This function is necessary because we don't record buffer positions
3889 in glyphs generated from strings (to keep struct glyph small).
3890 This function may only use code that doesn't eval because it is
3891 called asynchronously from note_mouse_highlight. */
3892
3893 int
3894 string_buffer_position (w, string, around_charpos)
3895 struct window *w;
3896 Lisp_Object string;
3897 int around_charpos;
3898 {
3899 Lisp_Object limit, prop, pos;
3900 const int MAX_DISTANCE = 1000;
3901 int found = 0;
3902
3903 pos = make_number (around_charpos);
3904 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3905 while (!found && !EQ (pos, limit))
3906 {
3907 prop = Fget_char_property (pos, Qdisplay, Qnil);
3908 if (!NILP (prop) && display_prop_string_p (prop, string))
3909 found = 1;
3910 else
3911 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3912 }
3913
3914 if (!found)
3915 {
3916 pos = make_number (around_charpos);
3917 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3918 while (!found && !EQ (pos, limit))
3919 {
3920 prop = Fget_char_property (pos, Qdisplay, Qnil);
3921 if (!NILP (prop) && display_prop_string_p (prop, string))
3922 found = 1;
3923 else
3924 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3925 limit);
3926 }
3927 }
3928
3929 return found ? XINT (pos) : 0;
3930 }
3931
3932
3933 \f
3934 /***********************************************************************
3935 `composition' property
3936 ***********************************************************************/
3937
3938 /* Set up iterator IT from `composition' property at its current
3939 position. Called from handle_stop. */
3940
3941 static enum prop_handled
3942 handle_composition_prop (it)
3943 struct it *it;
3944 {
3945 Lisp_Object prop, string;
3946 int pos, pos_byte, end;
3947 enum prop_handled handled = HANDLED_NORMALLY;
3948
3949 if (STRINGP (it->string))
3950 {
3951 pos = IT_STRING_CHARPOS (*it);
3952 pos_byte = IT_STRING_BYTEPOS (*it);
3953 string = it->string;
3954 }
3955 else
3956 {
3957 pos = IT_CHARPOS (*it);
3958 pos_byte = IT_BYTEPOS (*it);
3959 string = Qnil;
3960 }
3961
3962 /* If there's a valid composition and point is not inside of the
3963 composition (in the case that the composition is from the current
3964 buffer), draw a glyph composed from the composition components. */
3965 if (find_composition (pos, -1, &pos, &end, &prop, string)
3966 && COMPOSITION_VALID_P (pos, end, prop)
3967 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3968 {
3969 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3970
3971 if (id >= 0)
3972 {
3973 it->method = next_element_from_composition;
3974 it->cmp_id = id;
3975 it->cmp_len = COMPOSITION_LENGTH (prop);
3976 /* For a terminal, draw only the first character of the
3977 components. */
3978 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3979 it->len = (STRINGP (it->string)
3980 ? string_char_to_byte (it->string, end)
3981 : CHAR_TO_BYTE (end)) - pos_byte;
3982 it->stop_charpos = end;
3983 handled = HANDLED_RETURN;
3984 }
3985 }
3986
3987 return handled;
3988 }
3989
3990
3991 \f
3992 /***********************************************************************
3993 Overlay strings
3994 ***********************************************************************/
3995
3996 /* The following structure is used to record overlay strings for
3997 later sorting in load_overlay_strings. */
3998
3999 struct overlay_entry
4000 {
4001 Lisp_Object overlay;
4002 Lisp_Object string;
4003 int priority;
4004 int after_string_p;
4005 };
4006
4007
4008 /* Set up iterator IT from overlay strings at its current position.
4009 Called from handle_stop. */
4010
4011 static enum prop_handled
4012 handle_overlay_change (it)
4013 struct it *it;
4014 {
4015 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4016 return HANDLED_RECOMPUTE_PROPS;
4017 else
4018 return HANDLED_NORMALLY;
4019 }
4020
4021
4022 /* Set up the next overlay string for delivery by IT, if there is an
4023 overlay string to deliver. Called by set_iterator_to_next when the
4024 end of the current overlay string is reached. If there are more
4025 overlay strings to display, IT->string and
4026 IT->current.overlay_string_index are set appropriately here.
4027 Otherwise IT->string is set to nil. */
4028
4029 static void
4030 next_overlay_string (it)
4031 struct it *it;
4032 {
4033 ++it->current.overlay_string_index;
4034 if (it->current.overlay_string_index == it->n_overlay_strings)
4035 {
4036 /* No more overlay strings. Restore IT's settings to what
4037 they were before overlay strings were processed, and
4038 continue to deliver from current_buffer. */
4039 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4040
4041 pop_it (it);
4042 xassert (it->stop_charpos >= BEGV
4043 && it->stop_charpos <= it->end_charpos);
4044 it->string = Qnil;
4045 it->current.overlay_string_index = -1;
4046 SET_TEXT_POS (it->current.string_pos, -1, -1);
4047 it->n_overlay_strings = 0;
4048 it->method = next_element_from_buffer;
4049
4050 /* If we're at the end of the buffer, record that we have
4051 processed the overlay strings there already, so that
4052 next_element_from_buffer doesn't try it again. */
4053 if (IT_CHARPOS (*it) >= it->end_charpos)
4054 it->overlay_strings_at_end_processed_p = 1;
4055
4056 /* If we have to display `...' for invisible text, set
4057 the iterator up for that. */
4058 if (display_ellipsis_p)
4059 setup_for_ellipsis (it, 0);
4060 }
4061 else
4062 {
4063 /* There are more overlay strings to process. If
4064 IT->current.overlay_string_index has advanced to a position
4065 where we must load IT->overlay_strings with more strings, do
4066 it. */
4067 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4068
4069 if (it->current.overlay_string_index && i == 0)
4070 load_overlay_strings (it, 0);
4071
4072 /* Initialize IT to deliver display elements from the overlay
4073 string. */
4074 it->string = it->overlay_strings[i];
4075 it->multibyte_p = STRING_MULTIBYTE (it->string);
4076 SET_TEXT_POS (it->current.string_pos, 0, 0);
4077 it->method = next_element_from_string;
4078 it->stop_charpos = 0;
4079 }
4080
4081 CHECK_IT (it);
4082 }
4083
4084
4085 /* Compare two overlay_entry structures E1 and E2. Used as a
4086 comparison function for qsort in load_overlay_strings. Overlay
4087 strings for the same position are sorted so that
4088
4089 1. All after-strings come in front of before-strings, except
4090 when they come from the same overlay.
4091
4092 2. Within after-strings, strings are sorted so that overlay strings
4093 from overlays with higher priorities come first.
4094
4095 2. Within before-strings, strings are sorted so that overlay
4096 strings from overlays with higher priorities come last.
4097
4098 Value is analogous to strcmp. */
4099
4100
4101 static int
4102 compare_overlay_entries (e1, e2)
4103 void *e1, *e2;
4104 {
4105 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4106 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4107 int result;
4108
4109 if (entry1->after_string_p != entry2->after_string_p)
4110 {
4111 /* Let after-strings appear in front of before-strings if
4112 they come from different overlays. */
4113 if (EQ (entry1->overlay, entry2->overlay))
4114 result = entry1->after_string_p ? 1 : -1;
4115 else
4116 result = entry1->after_string_p ? -1 : 1;
4117 }
4118 else if (entry1->after_string_p)
4119 /* After-strings sorted in order of decreasing priority. */
4120 result = entry2->priority - entry1->priority;
4121 else
4122 /* Before-strings sorted in order of increasing priority. */
4123 result = entry1->priority - entry2->priority;
4124
4125 return result;
4126 }
4127
4128
4129 /* Load the vector IT->overlay_strings with overlay strings from IT's
4130 current buffer position, or from CHARPOS if that is > 0. Set
4131 IT->n_overlays to the total number of overlay strings found.
4132
4133 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4134 a time. On entry into load_overlay_strings,
4135 IT->current.overlay_string_index gives the number of overlay
4136 strings that have already been loaded by previous calls to this
4137 function.
4138
4139 IT->add_overlay_start contains an additional overlay start
4140 position to consider for taking overlay strings from, if non-zero.
4141 This position comes into play when the overlay has an `invisible'
4142 property, and both before and after-strings. When we've skipped to
4143 the end of the overlay, because of its `invisible' property, we
4144 nevertheless want its before-string to appear.
4145 IT->add_overlay_start will contain the overlay start position
4146 in this case.
4147
4148 Overlay strings are sorted so that after-string strings come in
4149 front of before-string strings. Within before and after-strings,
4150 strings are sorted by overlay priority. See also function
4151 compare_overlay_entries. */
4152
4153 static void
4154 load_overlay_strings (it, charpos)
4155 struct it *it;
4156 int charpos;
4157 {
4158 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4159 Lisp_Object overlay, window, str, invisible;
4160 struct Lisp_Overlay *ov;
4161 int start, end;
4162 int size = 20;
4163 int n = 0, i, j, invis_p;
4164 struct overlay_entry *entries
4165 = (struct overlay_entry *) alloca (size * sizeof *entries);
4166
4167 if (charpos <= 0)
4168 charpos = IT_CHARPOS (*it);
4169
4170 /* Append the overlay string STRING of overlay OVERLAY to vector
4171 `entries' which has size `size' and currently contains `n'
4172 elements. AFTER_P non-zero means STRING is an after-string of
4173 OVERLAY. */
4174 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4175 do \
4176 { \
4177 Lisp_Object priority; \
4178 \
4179 if (n == size) \
4180 { \
4181 int new_size = 2 * size; \
4182 struct overlay_entry *old = entries; \
4183 entries = \
4184 (struct overlay_entry *) alloca (new_size \
4185 * sizeof *entries); \
4186 bcopy (old, entries, size * sizeof *entries); \
4187 size = new_size; \
4188 } \
4189 \
4190 entries[n].string = (STRING); \
4191 entries[n].overlay = (OVERLAY); \
4192 priority = Foverlay_get ((OVERLAY), Qpriority); \
4193 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4194 entries[n].after_string_p = (AFTER_P); \
4195 ++n; \
4196 } \
4197 while (0)
4198
4199 /* Process overlay before the overlay center. */
4200 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4201 {
4202 XSETMISC (overlay, ov);
4203 xassert (OVERLAYP (overlay));
4204 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4205 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4206
4207 if (end < charpos)
4208 break;
4209
4210 /* Skip this overlay if it doesn't start or end at IT's current
4211 position. */
4212 if (end != charpos && start != charpos)
4213 continue;
4214
4215 /* Skip this overlay if it doesn't apply to IT->w. */
4216 window = Foverlay_get (overlay, Qwindow);
4217 if (WINDOWP (window) && XWINDOW (window) != it->w)
4218 continue;
4219
4220 /* If the text ``under'' the overlay is invisible, both before-
4221 and after-strings from this overlay are visible; start and
4222 end position are indistinguishable. */
4223 invisible = Foverlay_get (overlay, Qinvisible);
4224 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4225
4226 /* If overlay has a non-empty before-string, record it. */
4227 if ((start == charpos || (end == charpos && invis_p))
4228 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4229 && SCHARS (str))
4230 RECORD_OVERLAY_STRING (overlay, str, 0);
4231
4232 /* If overlay has a non-empty after-string, record it. */
4233 if ((end == charpos || (start == charpos && invis_p))
4234 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4235 && SCHARS (str))
4236 RECORD_OVERLAY_STRING (overlay, str, 1);
4237 }
4238
4239 /* Process overlays after the overlay center. */
4240 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4241 {
4242 XSETMISC (overlay, ov);
4243 xassert (OVERLAYP (overlay));
4244 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4245 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4246
4247 if (start > charpos)
4248 break;
4249
4250 /* Skip this overlay if it doesn't start or end at IT's current
4251 position. */
4252 if (end != charpos && start != charpos)
4253 continue;
4254
4255 /* Skip this overlay if it doesn't apply to IT->w. */
4256 window = Foverlay_get (overlay, Qwindow);
4257 if (WINDOWP (window) && XWINDOW (window) != it->w)
4258 continue;
4259
4260 /* If the text ``under'' the overlay is invisible, it has a zero
4261 dimension, and both before- and after-strings apply. */
4262 invisible = Foverlay_get (overlay, Qinvisible);
4263 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4264
4265 /* If overlay has a non-empty before-string, record it. */
4266 if ((start == charpos || (end == charpos && invis_p))
4267 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4268 && SCHARS (str))
4269 RECORD_OVERLAY_STRING (overlay, str, 0);
4270
4271 /* If overlay has a non-empty after-string, record it. */
4272 if ((end == charpos || (start == charpos && invis_p))
4273 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4274 && SCHARS (str))
4275 RECORD_OVERLAY_STRING (overlay, str, 1);
4276 }
4277
4278 #undef RECORD_OVERLAY_STRING
4279
4280 /* Sort entries. */
4281 if (n > 1)
4282 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4283
4284 /* Record the total number of strings to process. */
4285 it->n_overlay_strings = n;
4286
4287 /* IT->current.overlay_string_index is the number of overlay strings
4288 that have already been consumed by IT. Copy some of the
4289 remaining overlay strings to IT->overlay_strings. */
4290 i = 0;
4291 j = it->current.overlay_string_index;
4292 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4293 it->overlay_strings[i++] = entries[j++].string;
4294
4295 CHECK_IT (it);
4296 }
4297
4298
4299 /* Get the first chunk of overlay strings at IT's current buffer
4300 position, or at CHARPOS if that is > 0. Value is non-zero if at
4301 least one overlay string was found. */
4302
4303 static int
4304 get_overlay_strings (it, charpos)
4305 struct it *it;
4306 int charpos;
4307 {
4308 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4309 process. This fills IT->overlay_strings with strings, and sets
4310 IT->n_overlay_strings to the total number of strings to process.
4311 IT->pos.overlay_string_index has to be set temporarily to zero
4312 because load_overlay_strings needs this; it must be set to -1
4313 when no overlay strings are found because a zero value would
4314 indicate a position in the first overlay string. */
4315 it->current.overlay_string_index = 0;
4316 load_overlay_strings (it, charpos);
4317
4318 /* If we found overlay strings, set up IT to deliver display
4319 elements from the first one. Otherwise set up IT to deliver
4320 from current_buffer. */
4321 if (it->n_overlay_strings)
4322 {
4323 /* Make sure we know settings in current_buffer, so that we can
4324 restore meaningful values when we're done with the overlay
4325 strings. */
4326 compute_stop_pos (it);
4327 xassert (it->face_id >= 0);
4328
4329 /* Save IT's settings. They are restored after all overlay
4330 strings have been processed. */
4331 xassert (it->sp == 0);
4332 push_it (it);
4333
4334 /* Set up IT to deliver display elements from the first overlay
4335 string. */
4336 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4337 it->string = it->overlay_strings[0];
4338 it->stop_charpos = 0;
4339 xassert (STRINGP (it->string));
4340 it->end_charpos = SCHARS (it->string);
4341 it->multibyte_p = STRING_MULTIBYTE (it->string);
4342 it->method = next_element_from_string;
4343 }
4344 else
4345 {
4346 it->string = Qnil;
4347 it->current.overlay_string_index = -1;
4348 it->method = next_element_from_buffer;
4349 }
4350
4351 CHECK_IT (it);
4352
4353 /* Value is non-zero if we found at least one overlay string. */
4354 return STRINGP (it->string);
4355 }
4356
4357
4358 \f
4359 /***********************************************************************
4360 Saving and restoring state
4361 ***********************************************************************/
4362
4363 /* Save current settings of IT on IT->stack. Called, for example,
4364 before setting up IT for an overlay string, to be able to restore
4365 IT's settings to what they were after the overlay string has been
4366 processed. */
4367
4368 static void
4369 push_it (it)
4370 struct it *it;
4371 {
4372 struct iterator_stack_entry *p;
4373
4374 xassert (it->sp < 2);
4375 p = it->stack + it->sp;
4376
4377 p->stop_charpos = it->stop_charpos;
4378 xassert (it->face_id >= 0);
4379 p->face_id = it->face_id;
4380 p->string = it->string;
4381 p->pos = it->current;
4382 p->end_charpos = it->end_charpos;
4383 p->string_nchars = it->string_nchars;
4384 p->area = it->area;
4385 p->multibyte_p = it->multibyte_p;
4386 p->slice = it->slice;
4387 p->space_width = it->space_width;
4388 p->font_height = it->font_height;
4389 p->voffset = it->voffset;
4390 p->string_from_display_prop_p = it->string_from_display_prop_p;
4391 p->display_ellipsis_p = 0;
4392 ++it->sp;
4393 }
4394
4395
4396 /* Restore IT's settings from IT->stack. Called, for example, when no
4397 more overlay strings must be processed, and we return to delivering
4398 display elements from a buffer, or when the end of a string from a
4399 `display' property is reached and we return to delivering display
4400 elements from an overlay string, or from a buffer. */
4401
4402 static void
4403 pop_it (it)
4404 struct it *it;
4405 {
4406 struct iterator_stack_entry *p;
4407
4408 xassert (it->sp > 0);
4409 --it->sp;
4410 p = it->stack + it->sp;
4411 it->stop_charpos = p->stop_charpos;
4412 it->face_id = p->face_id;
4413 it->string = p->string;
4414 it->current = p->pos;
4415 it->end_charpos = p->end_charpos;
4416 it->string_nchars = p->string_nchars;
4417 it->area = p->area;
4418 it->multibyte_p = p->multibyte_p;
4419 it->slice = p->slice;
4420 it->space_width = p->space_width;
4421 it->font_height = p->font_height;
4422 it->voffset = p->voffset;
4423 it->string_from_display_prop_p = p->string_from_display_prop_p;
4424 }
4425
4426
4427 \f
4428 /***********************************************************************
4429 Moving over lines
4430 ***********************************************************************/
4431
4432 /* Set IT's current position to the previous line start. */
4433
4434 static void
4435 back_to_previous_line_start (it)
4436 struct it *it;
4437 {
4438 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4439 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4440 }
4441
4442
4443 /* Move IT to the next line start.
4444
4445 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4446 we skipped over part of the text (as opposed to moving the iterator
4447 continuously over the text). Otherwise, don't change the value
4448 of *SKIPPED_P.
4449
4450 Newlines may come from buffer text, overlay strings, or strings
4451 displayed via the `display' property. That's the reason we can't
4452 simply use find_next_newline_no_quit.
4453
4454 Note that this function may not skip over invisible text that is so
4455 because of text properties and immediately follows a newline. If
4456 it would, function reseat_at_next_visible_line_start, when called
4457 from set_iterator_to_next, would effectively make invisible
4458 characters following a newline part of the wrong glyph row, which
4459 leads to wrong cursor motion. */
4460
4461 static int
4462 forward_to_next_line_start (it, skipped_p)
4463 struct it *it;
4464 int *skipped_p;
4465 {
4466 int old_selective, newline_found_p, n;
4467 const int MAX_NEWLINE_DISTANCE = 500;
4468
4469 /* If already on a newline, just consume it to avoid unintended
4470 skipping over invisible text below. */
4471 if (it->what == IT_CHARACTER
4472 && it->c == '\n'
4473 && CHARPOS (it->position) == IT_CHARPOS (*it))
4474 {
4475 set_iterator_to_next (it, 0);
4476 it->c = 0;
4477 return 1;
4478 }
4479
4480 /* Don't handle selective display in the following. It's (a)
4481 unnecessary because it's done by the caller, and (b) leads to an
4482 infinite recursion because next_element_from_ellipsis indirectly
4483 calls this function. */
4484 old_selective = it->selective;
4485 it->selective = 0;
4486
4487 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4488 from buffer text. */
4489 for (n = newline_found_p = 0;
4490 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4491 n += STRINGP (it->string) ? 0 : 1)
4492 {
4493 if (!get_next_display_element (it))
4494 return 0;
4495 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4496 set_iterator_to_next (it, 0);
4497 }
4498
4499 /* If we didn't find a newline near enough, see if we can use a
4500 short-cut. */
4501 if (!newline_found_p)
4502 {
4503 int start = IT_CHARPOS (*it);
4504 int limit = find_next_newline_no_quit (start, 1);
4505 Lisp_Object pos;
4506
4507 xassert (!STRINGP (it->string));
4508
4509 /* If there isn't any `display' property in sight, and no
4510 overlays, we can just use the position of the newline in
4511 buffer text. */
4512 if (it->stop_charpos >= limit
4513 || ((pos = Fnext_single_property_change (make_number (start),
4514 Qdisplay,
4515 Qnil, make_number (limit)),
4516 NILP (pos))
4517 && next_overlay_change (start) == ZV))
4518 {
4519 IT_CHARPOS (*it) = limit;
4520 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4521 *skipped_p = newline_found_p = 1;
4522 }
4523 else
4524 {
4525 while (get_next_display_element (it)
4526 && !newline_found_p)
4527 {
4528 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4529 set_iterator_to_next (it, 0);
4530 }
4531 }
4532 }
4533
4534 it->selective = old_selective;
4535 return newline_found_p;
4536 }
4537
4538
4539 /* Set IT's current position to the previous visible line start. Skip
4540 invisible text that is so either due to text properties or due to
4541 selective display. Caution: this does not change IT->current_x and
4542 IT->hpos. */
4543
4544 static void
4545 back_to_previous_visible_line_start (it)
4546 struct it *it;
4547 {
4548 int visible_p = 0;
4549
4550 /* Go back one newline if not on BEGV already. */
4551 if (IT_CHARPOS (*it) > BEGV)
4552 back_to_previous_line_start (it);
4553
4554 /* Move over lines that are invisible because of selective display
4555 or text properties. */
4556 while (IT_CHARPOS (*it) > BEGV
4557 && !visible_p)
4558 {
4559 visible_p = 1;
4560
4561 /* If selective > 0, then lines indented more than that values
4562 are invisible. */
4563 if (it->selective > 0
4564 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4565 (double) it->selective)) /* iftc */
4566 visible_p = 0;
4567 else
4568 {
4569 Lisp_Object prop;
4570
4571 /* Check the newline before point for invisibility. */
4572 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4573 Qinvisible, it->window);
4574 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4575 visible_p = 0;
4576 }
4577
4578 if (visible_p)
4579 {
4580 struct it it2 = *it;
4581
4582 if (handle_display_prop (&it2) == HANDLED_RETURN)
4583 visible_p = 0;
4584 }
4585
4586 /* Back one more newline if the current one is invisible. */
4587 if (!visible_p)
4588 back_to_previous_line_start (it);
4589 }
4590
4591 xassert (IT_CHARPOS (*it) >= BEGV);
4592 xassert (IT_CHARPOS (*it) == BEGV
4593 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4594 CHECK_IT (it);
4595 }
4596
4597
4598 /* Reseat iterator IT at the previous visible line start. Skip
4599 invisible text that is so either due to text properties or due to
4600 selective display. At the end, update IT's overlay information,
4601 face information etc. */
4602
4603 void
4604 reseat_at_previous_visible_line_start (it)
4605 struct it *it;
4606 {
4607 back_to_previous_visible_line_start (it);
4608 reseat (it, it->current.pos, 1);
4609 CHECK_IT (it);
4610 }
4611
4612
4613 /* Reseat iterator IT on the next visible line start in the current
4614 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4615 preceding the line start. Skip over invisible text that is so
4616 because of selective display. Compute faces, overlays etc at the
4617 new position. Note that this function does not skip over text that
4618 is invisible because of text properties. */
4619
4620 static void
4621 reseat_at_next_visible_line_start (it, on_newline_p)
4622 struct it *it;
4623 int on_newline_p;
4624 {
4625 int newline_found_p, skipped_p = 0;
4626
4627 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4628
4629 /* Skip over lines that are invisible because they are indented
4630 more than the value of IT->selective. */
4631 if (it->selective > 0)
4632 while (IT_CHARPOS (*it) < ZV
4633 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4634 (double) it->selective)) /* iftc */
4635 {
4636 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4637 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4638 }
4639
4640 /* Position on the newline if that's what's requested. */
4641 if (on_newline_p && newline_found_p)
4642 {
4643 if (STRINGP (it->string))
4644 {
4645 if (IT_STRING_CHARPOS (*it) > 0)
4646 {
4647 --IT_STRING_CHARPOS (*it);
4648 --IT_STRING_BYTEPOS (*it);
4649 }
4650 }
4651 else if (IT_CHARPOS (*it) > BEGV)
4652 {
4653 --IT_CHARPOS (*it);
4654 --IT_BYTEPOS (*it);
4655 reseat (it, it->current.pos, 0);
4656 }
4657 }
4658 else if (skipped_p)
4659 reseat (it, it->current.pos, 0);
4660
4661 CHECK_IT (it);
4662 }
4663
4664
4665 \f
4666 /***********************************************************************
4667 Changing an iterator's position
4668 ***********************************************************************/
4669
4670 /* Change IT's current position to POS in current_buffer. If FORCE_P
4671 is non-zero, always check for text properties at the new position.
4672 Otherwise, text properties are only looked up if POS >=
4673 IT->check_charpos of a property. */
4674
4675 static void
4676 reseat (it, pos, force_p)
4677 struct it *it;
4678 struct text_pos pos;
4679 int force_p;
4680 {
4681 int original_pos = IT_CHARPOS (*it);
4682
4683 reseat_1 (it, pos, 0);
4684
4685 /* Determine where to check text properties. Avoid doing it
4686 where possible because text property lookup is very expensive. */
4687 if (force_p
4688 || CHARPOS (pos) > it->stop_charpos
4689 || CHARPOS (pos) < original_pos)
4690 handle_stop (it);
4691
4692 CHECK_IT (it);
4693 }
4694
4695
4696 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4697 IT->stop_pos to POS, also. */
4698
4699 static void
4700 reseat_1 (it, pos, set_stop_p)
4701 struct it *it;
4702 struct text_pos pos;
4703 int set_stop_p;
4704 {
4705 /* Don't call this function when scanning a C string. */
4706 xassert (it->s == NULL);
4707
4708 /* POS must be a reasonable value. */
4709 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4710
4711 it->current.pos = it->position = pos;
4712 XSETBUFFER (it->object, current_buffer);
4713 it->end_charpos = ZV;
4714 it->dpvec = NULL;
4715 it->current.dpvec_index = -1;
4716 it->current.overlay_string_index = -1;
4717 IT_STRING_CHARPOS (*it) = -1;
4718 IT_STRING_BYTEPOS (*it) = -1;
4719 it->string = Qnil;
4720 it->method = next_element_from_buffer;
4721 /* RMS: I added this to fix a bug in move_it_vertically_backward
4722 where it->area continued to relate to the starting point
4723 for the backward motion. Bug report from
4724 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4725 However, I am not sure whether reseat still does the right thing
4726 in general after this change. */
4727 it->area = TEXT_AREA;
4728 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4729 it->sp = 0;
4730 it->face_before_selective_p = 0;
4731
4732 if (set_stop_p)
4733 it->stop_charpos = CHARPOS (pos);
4734 }
4735
4736
4737 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4738 If S is non-null, it is a C string to iterate over. Otherwise,
4739 STRING gives a Lisp string to iterate over.
4740
4741 If PRECISION > 0, don't return more then PRECISION number of
4742 characters from the string.
4743
4744 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4745 characters have been returned. FIELD_WIDTH < 0 means an infinite
4746 field width.
4747
4748 MULTIBYTE = 0 means disable processing of multibyte characters,
4749 MULTIBYTE > 0 means enable it,
4750 MULTIBYTE < 0 means use IT->multibyte_p.
4751
4752 IT must be initialized via a prior call to init_iterator before
4753 calling this function. */
4754
4755 static void
4756 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4757 struct it *it;
4758 unsigned char *s;
4759 Lisp_Object string;
4760 int charpos;
4761 int precision, field_width, multibyte;
4762 {
4763 /* No region in strings. */
4764 it->region_beg_charpos = it->region_end_charpos = -1;
4765
4766 /* No text property checks performed by default, but see below. */
4767 it->stop_charpos = -1;
4768
4769 /* Set iterator position and end position. */
4770 bzero (&it->current, sizeof it->current);
4771 it->current.overlay_string_index = -1;
4772 it->current.dpvec_index = -1;
4773 xassert (charpos >= 0);
4774
4775 /* If STRING is specified, use its multibyteness, otherwise use the
4776 setting of MULTIBYTE, if specified. */
4777 if (multibyte >= 0)
4778 it->multibyte_p = multibyte > 0;
4779
4780 if (s == NULL)
4781 {
4782 xassert (STRINGP (string));
4783 it->string = string;
4784 it->s = NULL;
4785 it->end_charpos = it->string_nchars = SCHARS (string);
4786 it->method = next_element_from_string;
4787 it->current.string_pos = string_pos (charpos, string);
4788 }
4789 else
4790 {
4791 it->s = s;
4792 it->string = Qnil;
4793
4794 /* Note that we use IT->current.pos, not it->current.string_pos,
4795 for displaying C strings. */
4796 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4797 if (it->multibyte_p)
4798 {
4799 it->current.pos = c_string_pos (charpos, s, 1);
4800 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4801 }
4802 else
4803 {
4804 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4805 it->end_charpos = it->string_nchars = strlen (s);
4806 }
4807
4808 it->method = next_element_from_c_string;
4809 }
4810
4811 /* PRECISION > 0 means don't return more than PRECISION characters
4812 from the string. */
4813 if (precision > 0 && it->end_charpos - charpos > precision)
4814 it->end_charpos = it->string_nchars = charpos + precision;
4815
4816 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4817 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4818 FIELD_WIDTH < 0 means infinite field width. This is useful for
4819 padding with `-' at the end of a mode line. */
4820 if (field_width < 0)
4821 field_width = INFINITY;
4822 if (field_width > it->end_charpos - charpos)
4823 it->end_charpos = charpos + field_width;
4824
4825 /* Use the standard display table for displaying strings. */
4826 if (DISP_TABLE_P (Vstandard_display_table))
4827 it->dp = XCHAR_TABLE (Vstandard_display_table);
4828
4829 it->stop_charpos = charpos;
4830 CHECK_IT (it);
4831 }
4832
4833
4834 \f
4835 /***********************************************************************
4836 Iteration
4837 ***********************************************************************/
4838
4839 /* Load IT's display element fields with information about the next
4840 display element from the current position of IT. Value is zero if
4841 end of buffer (or C string) is reached. */
4842
4843 int
4844 get_next_display_element (it)
4845 struct it *it;
4846 {
4847 /* Non-zero means that we found a display element. Zero means that
4848 we hit the end of what we iterate over. Performance note: the
4849 function pointer `method' used here turns out to be faster than
4850 using a sequence of if-statements. */
4851 int success_p;
4852
4853 get_next:
4854 success_p = (*it->method) (it);
4855
4856 if (it->what == IT_CHARACTER)
4857 {
4858 /* Map via display table or translate control characters.
4859 IT->c, IT->len etc. have been set to the next character by
4860 the function call above. If we have a display table, and it
4861 contains an entry for IT->c, translate it. Don't do this if
4862 IT->c itself comes from a display table, otherwise we could
4863 end up in an infinite recursion. (An alternative could be to
4864 count the recursion depth of this function and signal an
4865 error when a certain maximum depth is reached.) Is it worth
4866 it? */
4867 if (success_p && it->dpvec == NULL)
4868 {
4869 Lisp_Object dv;
4870
4871 if (it->dp
4872 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4873 VECTORP (dv)))
4874 {
4875 struct Lisp_Vector *v = XVECTOR (dv);
4876
4877 /* Return the first character from the display table
4878 entry, if not empty. If empty, don't display the
4879 current character. */
4880 if (v->size)
4881 {
4882 it->dpvec_char_len = it->len;
4883 it->dpvec = v->contents;
4884 it->dpend = v->contents + v->size;
4885 it->current.dpvec_index = 0;
4886 it->saved_face_id = it->face_id;
4887 it->method = next_element_from_display_vector;
4888 }
4889 else
4890 {
4891 set_iterator_to_next (it, 0);
4892 }
4893 goto get_next;
4894 }
4895
4896 /* Translate control characters into `\003' or `^C' form.
4897 Control characters coming from a display table entry are
4898 currently not translated because we use IT->dpvec to hold
4899 the translation. This could easily be changed but I
4900 don't believe that it is worth doing.
4901
4902 If it->multibyte_p is nonzero, eight-bit characters and
4903 non-printable multibyte characters are also translated to
4904 octal form.
4905
4906 If it->multibyte_p is zero, eight-bit characters that
4907 don't have corresponding multibyte char code are also
4908 translated to octal form. */
4909 else if ((it->c < ' '
4910 && (it->area != TEXT_AREA
4911 /* In mode line, treat \n like other crl chars. */
4912 || (it->c != '\n'
4913 && it->glyph_row && it->glyph_row->mode_line_p)
4914 || (it->c != '\n' && it->c != '\t')))
4915 || (it->multibyte_p
4916 ? ((it->c >= 127
4917 && it->len == 1)
4918 || !CHAR_PRINTABLE_P (it->c)
4919 || it->c == 0x8ad
4920 || it->c == 0x8a0)
4921 : (it->c >= 127
4922 && (!unibyte_display_via_language_environment
4923 || it->c == unibyte_char_to_multibyte (it->c)))))
4924 {
4925 /* IT->c is a control character which must be displayed
4926 either as '\003' or as `^C' where the '\\' and '^'
4927 can be defined in the display table. Fill
4928 IT->ctl_chars with glyphs for what we have to
4929 display. Then, set IT->dpvec to these glyphs. */
4930 GLYPH g;
4931 int ctl_len;
4932 int face_id = escape_glyph_face;
4933
4934 /* Find the face id if `escape-glyph' unless we recently did. */
4935 if (face_id < 0)
4936 {
4937 Lisp_Object tem = Fget (Qescape_glyph, Qface);
4938 if (INTEGERP (tem))
4939 face_id = XINT (tem);
4940 else
4941 face_id = 0;
4942 /* If there's overflow, use 0 instead. */
4943 if (FAST_GLYPH_FACE (FAST_MAKE_GLYPH (0, face_id)) != face_id)
4944 face_id = 0;
4945 escape_glyph_face = face_id;
4946 }
4947
4948 if (it->c < 128 && it->ctl_arrow_p)
4949 {
4950 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4951 if (it->dp
4952 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4953 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4954 g = XINT (DISP_CTRL_GLYPH (it->dp));
4955 else
4956 g = FAST_MAKE_GLYPH ('^', face_id);
4957 XSETINT (it->ctl_chars[0], g);
4958
4959 g = FAST_MAKE_GLYPH (it->c ^ 0100, face_id);
4960 XSETINT (it->ctl_chars[1], g);
4961 ctl_len = 2;
4962 }
4963 else if (it->c == 0x8a0 || it->c == 0x8ad)
4964 {
4965 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4966 if (it->dp
4967 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4968 && GLYPH_CHAR_VALID_P (XINT (DISP_ESCAPE_GLYPH (it->dp))))
4969 g = XINT (DISP_ESCAPE_GLYPH (it->dp));
4970 else
4971 g = FAST_MAKE_GLYPH ('\\', face_id);
4972 XSETINT (it->ctl_chars[0], g);
4973
4974 g = FAST_MAKE_GLYPH (it->c == 0x8ad ? '-' : ' ', face_id);
4975 XSETINT (it->ctl_chars[1], g);
4976 ctl_len = 2;
4977 }
4978 else
4979 {
4980 unsigned char str[MAX_MULTIBYTE_LENGTH];
4981 int len;
4982 int i;
4983 GLYPH escape_glyph;
4984
4985 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4986 if (it->dp
4987 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4988 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4989 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4990 else
4991 escape_glyph = FAST_MAKE_GLYPH ('\\', face_id);
4992
4993 if (SINGLE_BYTE_CHAR_P (it->c))
4994 str[0] = it->c, len = 1;
4995 else
4996 {
4997 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4998 if (len < 0)
4999 {
5000 /* It's an invalid character, which
5001 shouldn't happen actually, but due to
5002 bugs it may happen. Let's print the char
5003 as is, there's not much meaningful we can
5004 do with it. */
5005 str[0] = it->c;
5006 str[1] = it->c >> 8;
5007 str[2] = it->c >> 16;
5008 str[3] = it->c >> 24;
5009 len = 4;
5010 }
5011 }
5012
5013 for (i = 0; i < len; i++)
5014 {
5015 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5016 /* Insert three more glyphs into IT->ctl_chars for
5017 the octal display of the character. */
5018 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0',
5019 face_id);
5020 XSETINT (it->ctl_chars[i * 4 + 1], g);
5021 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0',
5022 face_id);
5023 XSETINT (it->ctl_chars[i * 4 + 2], g);
5024 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0',
5025 face_id);
5026 XSETINT (it->ctl_chars[i * 4 + 3], g);
5027 }
5028 ctl_len = len * 4;
5029 }
5030
5031 /* Set up IT->dpvec and return first character from it. */
5032 it->dpvec_char_len = it->len;
5033 it->dpvec = it->ctl_chars;
5034 it->dpend = it->dpvec + ctl_len;
5035 it->current.dpvec_index = 0;
5036 it->saved_face_id = it->face_id;
5037 it->method = next_element_from_display_vector;
5038 goto get_next;
5039 }
5040 }
5041
5042 /* Adjust face id for a multibyte character. There are no
5043 multibyte character in unibyte text. */
5044 if (it->multibyte_p
5045 && success_p
5046 && FRAME_WINDOW_P (it->f))
5047 {
5048 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5049 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5050 }
5051 }
5052
5053 /* Is this character the last one of a run of characters with
5054 box? If yes, set IT->end_of_box_run_p to 1. */
5055 if (it->face_box_p
5056 && it->s == NULL)
5057 {
5058 int face_id;
5059 struct face *face;
5060
5061 it->end_of_box_run_p
5062 = ((face_id = face_after_it_pos (it),
5063 face_id != it->face_id)
5064 && (face = FACE_FROM_ID (it->f, face_id),
5065 face->box == FACE_NO_BOX));
5066 }
5067
5068 /* Value is 0 if end of buffer or string reached. */
5069 return success_p;
5070 }
5071
5072
5073 /* Move IT to the next display element.
5074
5075 RESEAT_P non-zero means if called on a newline in buffer text,
5076 skip to the next visible line start.
5077
5078 Functions get_next_display_element and set_iterator_to_next are
5079 separate because I find this arrangement easier to handle than a
5080 get_next_display_element function that also increments IT's
5081 position. The way it is we can first look at an iterator's current
5082 display element, decide whether it fits on a line, and if it does,
5083 increment the iterator position. The other way around we probably
5084 would either need a flag indicating whether the iterator has to be
5085 incremented the next time, or we would have to implement a
5086 decrement position function which would not be easy to write. */
5087
5088 void
5089 set_iterator_to_next (it, reseat_p)
5090 struct it *it;
5091 int reseat_p;
5092 {
5093 /* Reset flags indicating start and end of a sequence of characters
5094 with box. Reset them at the start of this function because
5095 moving the iterator to a new position might set them. */
5096 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5097
5098 if (it->method == next_element_from_buffer)
5099 {
5100 /* The current display element of IT is a character from
5101 current_buffer. Advance in the buffer, and maybe skip over
5102 invisible lines that are so because of selective display. */
5103 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5104 reseat_at_next_visible_line_start (it, 0);
5105 else
5106 {
5107 xassert (it->len != 0);
5108 IT_BYTEPOS (*it) += it->len;
5109 IT_CHARPOS (*it) += 1;
5110 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5111 }
5112 }
5113 else if (it->method == next_element_from_composition)
5114 {
5115 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5116 if (STRINGP (it->string))
5117 {
5118 IT_STRING_BYTEPOS (*it) += it->len;
5119 IT_STRING_CHARPOS (*it) += it->cmp_len;
5120 it->method = next_element_from_string;
5121 goto consider_string_end;
5122 }
5123 else
5124 {
5125 IT_BYTEPOS (*it) += it->len;
5126 IT_CHARPOS (*it) += it->cmp_len;
5127 it->method = next_element_from_buffer;
5128 }
5129 }
5130 else if (it->method == next_element_from_c_string)
5131 {
5132 /* Current display element of IT is from a C string. */
5133 IT_BYTEPOS (*it) += it->len;
5134 IT_CHARPOS (*it) += 1;
5135 }
5136 else if (it->method == next_element_from_display_vector)
5137 {
5138 /* Current display element of IT is from a display table entry.
5139 Advance in the display table definition. Reset it to null if
5140 end reached, and continue with characters from buffers/
5141 strings. */
5142 ++it->current.dpvec_index;
5143
5144 /* Restore face of the iterator to what they were before the
5145 display vector entry (these entries may contain faces). */
5146 it->face_id = it->saved_face_id;
5147
5148 if (it->dpvec + it->current.dpvec_index == it->dpend)
5149 {
5150 if (it->s)
5151 it->method = next_element_from_c_string;
5152 else if (STRINGP (it->string))
5153 it->method = next_element_from_string;
5154 else
5155 it->method = next_element_from_buffer;
5156
5157 it->dpvec = NULL;
5158 it->current.dpvec_index = -1;
5159
5160 /* Recheck faces after display vector */
5161 it->stop_charpos = 0;
5162
5163 /* Skip over characters which were displayed via IT->dpvec. */
5164 if (it->dpvec_char_len < 0)
5165 reseat_at_next_visible_line_start (it, 1);
5166 else if (it->dpvec_char_len > 0)
5167 {
5168 it->len = it->dpvec_char_len;
5169 set_iterator_to_next (it, reseat_p);
5170 }
5171 }
5172 }
5173 else if (it->method == next_element_from_string)
5174 {
5175 /* Current display element is a character from a Lisp string. */
5176 xassert (it->s == NULL && STRINGP (it->string));
5177 IT_STRING_BYTEPOS (*it) += it->len;
5178 IT_STRING_CHARPOS (*it) += 1;
5179
5180 consider_string_end:
5181
5182 if (it->current.overlay_string_index >= 0)
5183 {
5184 /* IT->string is an overlay string. Advance to the
5185 next, if there is one. */
5186 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5187 next_overlay_string (it);
5188 }
5189 else
5190 {
5191 /* IT->string is not an overlay string. If we reached
5192 its end, and there is something on IT->stack, proceed
5193 with what is on the stack. This can be either another
5194 string, this time an overlay string, or a buffer. */
5195 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5196 && it->sp > 0)
5197 {
5198 pop_it (it);
5199 if (!STRINGP (it->string))
5200 it->method = next_element_from_buffer;
5201 else
5202 goto consider_string_end;
5203 }
5204 }
5205 }
5206 else if (it->method == next_element_from_image
5207 || it->method == next_element_from_stretch)
5208 {
5209 /* The position etc with which we have to proceed are on
5210 the stack. The position may be at the end of a string,
5211 if the `display' property takes up the whole string. */
5212 pop_it (it);
5213 it->image_id = 0;
5214 if (STRINGP (it->string))
5215 {
5216 it->method = next_element_from_string;
5217 goto consider_string_end;
5218 }
5219 else
5220 it->method = next_element_from_buffer;
5221 }
5222 else
5223 /* There are no other methods defined, so this should be a bug. */
5224 abort ();
5225
5226 xassert (it->method != next_element_from_string
5227 || (STRINGP (it->string)
5228 && IT_STRING_CHARPOS (*it) >= 0));
5229 }
5230
5231 /* Load IT's display element fields with information about the next
5232 display element which comes from a display table entry or from the
5233 result of translating a control character to one of the forms `^C'
5234 or `\003'.
5235
5236 IT->dpvec holds the glyphs to return as characters.
5237 IT->saved_face_id holds the face id before the display vector--
5238 it is restored into IT->face_idin set_iterator_to_next. */
5239
5240 static int
5241 next_element_from_display_vector (it)
5242 struct it *it;
5243 {
5244 /* Precondition. */
5245 xassert (it->dpvec && it->current.dpvec_index >= 0);
5246
5247 if (INTEGERP (*it->dpvec)
5248 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5249 {
5250 int lface_id;
5251 GLYPH g;
5252
5253 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5254 it->c = FAST_GLYPH_CHAR (g);
5255 it->len = CHAR_BYTES (it->c);
5256
5257 /* The entry may contain a face id to use. Such a face id is
5258 the id of a Lisp face, not a realized face. A face id of
5259 zero means no face is specified. */
5260 lface_id = FAST_GLYPH_FACE (g);
5261 if (lface_id)
5262 {
5263 /* The function returns -1 if lface_id is invalid. */
5264 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5265 if (face_id >= 0)
5266 it->face_id = face_id;
5267 }
5268 }
5269 else
5270 /* Display table entry is invalid. Return a space. */
5271 it->c = ' ', it->len = 1;
5272
5273 /* Don't change position and object of the iterator here. They are
5274 still the values of the character that had this display table
5275 entry or was translated, and that's what we want. */
5276 it->what = IT_CHARACTER;
5277 return 1;
5278 }
5279
5280
5281 /* Load IT with the next display element from Lisp string IT->string.
5282 IT->current.string_pos is the current position within the string.
5283 If IT->current.overlay_string_index >= 0, the Lisp string is an
5284 overlay string. */
5285
5286 static int
5287 next_element_from_string (it)
5288 struct it *it;
5289 {
5290 struct text_pos position;
5291
5292 xassert (STRINGP (it->string));
5293 xassert (IT_STRING_CHARPOS (*it) >= 0);
5294 position = it->current.string_pos;
5295
5296 /* Time to check for invisible text? */
5297 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5298 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5299 {
5300 handle_stop (it);
5301
5302 /* Since a handler may have changed IT->method, we must
5303 recurse here. */
5304 return get_next_display_element (it);
5305 }
5306
5307 if (it->current.overlay_string_index >= 0)
5308 {
5309 /* Get the next character from an overlay string. In overlay
5310 strings, There is no field width or padding with spaces to
5311 do. */
5312 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5313 {
5314 it->what = IT_EOB;
5315 return 0;
5316 }
5317 else if (STRING_MULTIBYTE (it->string))
5318 {
5319 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5320 const unsigned char *s = (SDATA (it->string)
5321 + IT_STRING_BYTEPOS (*it));
5322 it->c = string_char_and_length (s, remaining, &it->len);
5323 }
5324 else
5325 {
5326 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5327 it->len = 1;
5328 }
5329 }
5330 else
5331 {
5332 /* Get the next character from a Lisp string that is not an
5333 overlay string. Such strings come from the mode line, for
5334 example. We may have to pad with spaces, or truncate the
5335 string. See also next_element_from_c_string. */
5336 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5337 {
5338 it->what = IT_EOB;
5339 return 0;
5340 }
5341 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5342 {
5343 /* Pad with spaces. */
5344 it->c = ' ', it->len = 1;
5345 CHARPOS (position) = BYTEPOS (position) = -1;
5346 }
5347 else if (STRING_MULTIBYTE (it->string))
5348 {
5349 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5350 const unsigned char *s = (SDATA (it->string)
5351 + IT_STRING_BYTEPOS (*it));
5352 it->c = string_char_and_length (s, maxlen, &it->len);
5353 }
5354 else
5355 {
5356 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5357 it->len = 1;
5358 }
5359 }
5360
5361 /* Record what we have and where it came from. Note that we store a
5362 buffer position in IT->position although it could arguably be a
5363 string position. */
5364 it->what = IT_CHARACTER;
5365 it->object = it->string;
5366 it->position = position;
5367 return 1;
5368 }
5369
5370
5371 /* Load IT with next display element from C string IT->s.
5372 IT->string_nchars is the maximum number of characters to return
5373 from the string. IT->end_charpos may be greater than
5374 IT->string_nchars when this function is called, in which case we
5375 may have to return padding spaces. Value is zero if end of string
5376 reached, including padding spaces. */
5377
5378 static int
5379 next_element_from_c_string (it)
5380 struct it *it;
5381 {
5382 int success_p = 1;
5383
5384 xassert (it->s);
5385 it->what = IT_CHARACTER;
5386 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5387 it->object = Qnil;
5388
5389 /* IT's position can be greater IT->string_nchars in case a field
5390 width or precision has been specified when the iterator was
5391 initialized. */
5392 if (IT_CHARPOS (*it) >= it->end_charpos)
5393 {
5394 /* End of the game. */
5395 it->what = IT_EOB;
5396 success_p = 0;
5397 }
5398 else if (IT_CHARPOS (*it) >= it->string_nchars)
5399 {
5400 /* Pad with spaces. */
5401 it->c = ' ', it->len = 1;
5402 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5403 }
5404 else if (it->multibyte_p)
5405 {
5406 /* Implementation note: The calls to strlen apparently aren't a
5407 performance problem because there is no noticeable performance
5408 difference between Emacs running in unibyte or multibyte mode. */
5409 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5410 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5411 maxlen, &it->len);
5412 }
5413 else
5414 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5415
5416 return success_p;
5417 }
5418
5419
5420 /* Set up IT to return characters from an ellipsis, if appropriate.
5421 The definition of the ellipsis glyphs may come from a display table
5422 entry. This function Fills IT with the first glyph from the
5423 ellipsis if an ellipsis is to be displayed. */
5424
5425 static int
5426 next_element_from_ellipsis (it)
5427 struct it *it;
5428 {
5429 if (it->selective_display_ellipsis_p)
5430 setup_for_ellipsis (it, it->len);
5431 else
5432 {
5433 /* The face at the current position may be different from the
5434 face we find after the invisible text. Remember what it
5435 was in IT->saved_face_id, and signal that it's there by
5436 setting face_before_selective_p. */
5437 it->saved_face_id = it->face_id;
5438 it->method = next_element_from_buffer;
5439 reseat_at_next_visible_line_start (it, 1);
5440 it->face_before_selective_p = 1;
5441 }
5442
5443 return get_next_display_element (it);
5444 }
5445
5446
5447 /* Deliver an image display element. The iterator IT is already
5448 filled with image information (done in handle_display_prop). Value
5449 is always 1. */
5450
5451
5452 static int
5453 next_element_from_image (it)
5454 struct it *it;
5455 {
5456 it->what = IT_IMAGE;
5457 return 1;
5458 }
5459
5460
5461 /* Fill iterator IT with next display element from a stretch glyph
5462 property. IT->object is the value of the text property. Value is
5463 always 1. */
5464
5465 static int
5466 next_element_from_stretch (it)
5467 struct it *it;
5468 {
5469 it->what = IT_STRETCH;
5470 return 1;
5471 }
5472
5473
5474 /* Load IT with the next display element from current_buffer. Value
5475 is zero if end of buffer reached. IT->stop_charpos is the next
5476 position at which to stop and check for text properties or buffer
5477 end. */
5478
5479 static int
5480 next_element_from_buffer (it)
5481 struct it *it;
5482 {
5483 int success_p = 1;
5484
5485 /* Check this assumption, otherwise, we would never enter the
5486 if-statement, below. */
5487 xassert (IT_CHARPOS (*it) >= BEGV
5488 && IT_CHARPOS (*it) <= it->stop_charpos);
5489
5490 if (IT_CHARPOS (*it) >= it->stop_charpos)
5491 {
5492 if (IT_CHARPOS (*it) >= it->end_charpos)
5493 {
5494 int overlay_strings_follow_p;
5495
5496 /* End of the game, except when overlay strings follow that
5497 haven't been returned yet. */
5498 if (it->overlay_strings_at_end_processed_p)
5499 overlay_strings_follow_p = 0;
5500 else
5501 {
5502 it->overlay_strings_at_end_processed_p = 1;
5503 overlay_strings_follow_p = get_overlay_strings (it, 0);
5504 }
5505
5506 if (overlay_strings_follow_p)
5507 success_p = get_next_display_element (it);
5508 else
5509 {
5510 it->what = IT_EOB;
5511 it->position = it->current.pos;
5512 success_p = 0;
5513 }
5514 }
5515 else
5516 {
5517 handle_stop (it);
5518 return get_next_display_element (it);
5519 }
5520 }
5521 else
5522 {
5523 /* No face changes, overlays etc. in sight, so just return a
5524 character from current_buffer. */
5525 unsigned char *p;
5526
5527 /* Maybe run the redisplay end trigger hook. Performance note:
5528 This doesn't seem to cost measurable time. */
5529 if (it->redisplay_end_trigger_charpos
5530 && it->glyph_row
5531 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5532 run_redisplay_end_trigger_hook (it);
5533
5534 /* Get the next character, maybe multibyte. */
5535 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5536 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5537 {
5538 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5539 - IT_BYTEPOS (*it));
5540 it->c = string_char_and_length (p, maxlen, &it->len);
5541 }
5542 else
5543 it->c = *p, it->len = 1;
5544
5545 /* Record what we have and where it came from. */
5546 it->what = IT_CHARACTER;;
5547 it->object = it->w->buffer;
5548 it->position = it->current.pos;
5549
5550 /* Normally we return the character found above, except when we
5551 really want to return an ellipsis for selective display. */
5552 if (it->selective)
5553 {
5554 if (it->c == '\n')
5555 {
5556 /* A value of selective > 0 means hide lines indented more
5557 than that number of columns. */
5558 if (it->selective > 0
5559 && IT_CHARPOS (*it) + 1 < ZV
5560 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5561 IT_BYTEPOS (*it) + 1,
5562 (double) it->selective)) /* iftc */
5563 {
5564 success_p = next_element_from_ellipsis (it);
5565 it->dpvec_char_len = -1;
5566 }
5567 }
5568 else if (it->c == '\r' && it->selective == -1)
5569 {
5570 /* A value of selective == -1 means that everything from the
5571 CR to the end of the line is invisible, with maybe an
5572 ellipsis displayed for it. */
5573 success_p = next_element_from_ellipsis (it);
5574 it->dpvec_char_len = -1;
5575 }
5576 }
5577 }
5578
5579 /* Value is zero if end of buffer reached. */
5580 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5581 return success_p;
5582 }
5583
5584
5585 /* Run the redisplay end trigger hook for IT. */
5586
5587 static void
5588 run_redisplay_end_trigger_hook (it)
5589 struct it *it;
5590 {
5591 Lisp_Object args[3];
5592
5593 /* IT->glyph_row should be non-null, i.e. we should be actually
5594 displaying something, or otherwise we should not run the hook. */
5595 xassert (it->glyph_row);
5596
5597 /* Set up hook arguments. */
5598 args[0] = Qredisplay_end_trigger_functions;
5599 args[1] = it->window;
5600 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5601 it->redisplay_end_trigger_charpos = 0;
5602
5603 /* Since we are *trying* to run these functions, don't try to run
5604 them again, even if they get an error. */
5605 it->w->redisplay_end_trigger = Qnil;
5606 Frun_hook_with_args (3, args);
5607
5608 /* Notice if it changed the face of the character we are on. */
5609 handle_face_prop (it);
5610 }
5611
5612
5613 /* Deliver a composition display element. The iterator IT is already
5614 filled with composition information (done in
5615 handle_composition_prop). Value is always 1. */
5616
5617 static int
5618 next_element_from_composition (it)
5619 struct it *it;
5620 {
5621 it->what = IT_COMPOSITION;
5622 it->position = (STRINGP (it->string)
5623 ? it->current.string_pos
5624 : it->current.pos);
5625 return 1;
5626 }
5627
5628
5629 \f
5630 /***********************************************************************
5631 Moving an iterator without producing glyphs
5632 ***********************************************************************/
5633
5634 /* Move iterator IT to a specified buffer or X position within one
5635 line on the display without producing glyphs.
5636
5637 OP should be a bit mask including some or all of these bits:
5638 MOVE_TO_X: Stop on reaching x-position TO_X.
5639 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5640 Regardless of OP's value, stop in reaching the end of the display line.
5641
5642 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5643 This means, in particular, that TO_X includes window's horizontal
5644 scroll amount.
5645
5646 The return value has several possible values that
5647 say what condition caused the scan to stop:
5648
5649 MOVE_POS_MATCH_OR_ZV
5650 - when TO_POS or ZV was reached.
5651
5652 MOVE_X_REACHED
5653 -when TO_X was reached before TO_POS or ZV were reached.
5654
5655 MOVE_LINE_CONTINUED
5656 - when we reached the end of the display area and the line must
5657 be continued.
5658
5659 MOVE_LINE_TRUNCATED
5660 - when we reached the end of the display area and the line is
5661 truncated.
5662
5663 MOVE_NEWLINE_OR_CR
5664 - when we stopped at a line end, i.e. a newline or a CR and selective
5665 display is on. */
5666
5667 static enum move_it_result
5668 move_it_in_display_line_to (it, to_charpos, to_x, op)
5669 struct it *it;
5670 int to_charpos, to_x, op;
5671 {
5672 enum move_it_result result = MOVE_UNDEFINED;
5673 struct glyph_row *saved_glyph_row;
5674
5675 /* Don't produce glyphs in produce_glyphs. */
5676 saved_glyph_row = it->glyph_row;
5677 it->glyph_row = NULL;
5678
5679 #define BUFFER_POS_REACHED_P() \
5680 ((op & MOVE_TO_POS) != 0 \
5681 && BUFFERP (it->object) \
5682 && IT_CHARPOS (*it) >= to_charpos)
5683
5684 while (1)
5685 {
5686 int x, i, ascent = 0, descent = 0;
5687
5688 /* Stop when ZV reached.
5689 We used to stop here when TO_CHARPOS reached as well, but that is
5690 too soon if this glyph does not fit on this line. So we handle it
5691 explicitly below. */
5692 if (!get_next_display_element (it)
5693 || (it->truncate_lines_p
5694 && BUFFER_POS_REACHED_P ()))
5695 {
5696 result = MOVE_POS_MATCH_OR_ZV;
5697 break;
5698 }
5699
5700 /* The call to produce_glyphs will get the metrics of the
5701 display element IT is loaded with. We record in x the
5702 x-position before this display element in case it does not
5703 fit on the line. */
5704 x = it->current_x;
5705
5706 /* Remember the line height so far in case the next element doesn't
5707 fit on the line. */
5708 if (!it->truncate_lines_p)
5709 {
5710 ascent = it->max_ascent;
5711 descent = it->max_descent;
5712 }
5713
5714 PRODUCE_GLYPHS (it);
5715
5716 if (it->area != TEXT_AREA)
5717 {
5718 set_iterator_to_next (it, 1);
5719 continue;
5720 }
5721
5722 /* The number of glyphs we get back in IT->nglyphs will normally
5723 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5724 character on a terminal frame, or (iii) a line end. For the
5725 second case, IT->nglyphs - 1 padding glyphs will be present
5726 (on X frames, there is only one glyph produced for a
5727 composite character.
5728
5729 The behavior implemented below means, for continuation lines,
5730 that as many spaces of a TAB as fit on the current line are
5731 displayed there. For terminal frames, as many glyphs of a
5732 multi-glyph character are displayed in the current line, too.
5733 This is what the old redisplay code did, and we keep it that
5734 way. Under X, the whole shape of a complex character must
5735 fit on the line or it will be completely displayed in the
5736 next line.
5737
5738 Note that both for tabs and padding glyphs, all glyphs have
5739 the same width. */
5740 if (it->nglyphs)
5741 {
5742 /* More than one glyph or glyph doesn't fit on line. All
5743 glyphs have the same width. */
5744 int single_glyph_width = it->pixel_width / it->nglyphs;
5745 int new_x;
5746
5747 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5748 {
5749 new_x = x + single_glyph_width;
5750
5751 /* We want to leave anything reaching TO_X to the caller. */
5752 if ((op & MOVE_TO_X) && new_x > to_x)
5753 {
5754 if (BUFFER_POS_REACHED_P ())
5755 goto buffer_pos_reached;
5756 it->current_x = x;
5757 result = MOVE_X_REACHED;
5758 break;
5759 }
5760 else if (/* Lines are continued. */
5761 !it->truncate_lines_p
5762 && (/* And glyph doesn't fit on the line. */
5763 new_x > it->last_visible_x
5764 /* Or it fits exactly and we're on a window
5765 system frame. */
5766 || (new_x == it->last_visible_x
5767 && FRAME_WINDOW_P (it->f))))
5768 {
5769 if (/* IT->hpos == 0 means the very first glyph
5770 doesn't fit on the line, e.g. a wide image. */
5771 it->hpos == 0
5772 || (new_x == it->last_visible_x
5773 && FRAME_WINDOW_P (it->f)))
5774 {
5775 ++it->hpos;
5776 it->current_x = new_x;
5777 if (i == it->nglyphs - 1)
5778 {
5779 set_iterator_to_next (it, 1);
5780 #ifdef HAVE_WINDOW_SYSTEM
5781 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5782 {
5783 if (!get_next_display_element (it))
5784 {
5785 result = MOVE_POS_MATCH_OR_ZV;
5786 break;
5787 }
5788 if (BUFFER_POS_REACHED_P ())
5789 {
5790 if (ITERATOR_AT_END_OF_LINE_P (it))
5791 result = MOVE_POS_MATCH_OR_ZV;
5792 else
5793 result = MOVE_LINE_CONTINUED;
5794 break;
5795 }
5796 if (ITERATOR_AT_END_OF_LINE_P (it))
5797 {
5798 result = MOVE_NEWLINE_OR_CR;
5799 break;
5800 }
5801 }
5802 #endif /* HAVE_WINDOW_SYSTEM */
5803 }
5804 }
5805 else
5806 {
5807 it->current_x = x;
5808 it->max_ascent = ascent;
5809 it->max_descent = descent;
5810 }
5811
5812 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5813 IT_CHARPOS (*it)));
5814 result = MOVE_LINE_CONTINUED;
5815 break;
5816 }
5817 else if (BUFFER_POS_REACHED_P ())
5818 goto buffer_pos_reached;
5819 else if (new_x > it->first_visible_x)
5820 {
5821 /* Glyph is visible. Increment number of glyphs that
5822 would be displayed. */
5823 ++it->hpos;
5824 }
5825 else
5826 {
5827 /* Glyph is completely off the left margin of the display
5828 area. Nothing to do. */
5829 }
5830 }
5831
5832 if (result != MOVE_UNDEFINED)
5833 break;
5834 }
5835 else if (BUFFER_POS_REACHED_P ())
5836 {
5837 buffer_pos_reached:
5838 it->current_x = x;
5839 it->max_ascent = ascent;
5840 it->max_descent = descent;
5841 result = MOVE_POS_MATCH_OR_ZV;
5842 break;
5843 }
5844 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5845 {
5846 /* Stop when TO_X specified and reached. This check is
5847 necessary here because of lines consisting of a line end,
5848 only. The line end will not produce any glyphs and we
5849 would never get MOVE_X_REACHED. */
5850 xassert (it->nglyphs == 0);
5851 result = MOVE_X_REACHED;
5852 break;
5853 }
5854
5855 /* Is this a line end? If yes, we're done. */
5856 if (ITERATOR_AT_END_OF_LINE_P (it))
5857 {
5858 result = MOVE_NEWLINE_OR_CR;
5859 break;
5860 }
5861
5862 /* The current display element has been consumed. Advance
5863 to the next. */
5864 set_iterator_to_next (it, 1);
5865
5866 /* Stop if lines are truncated and IT's current x-position is
5867 past the right edge of the window now. */
5868 if (it->truncate_lines_p
5869 && it->current_x >= it->last_visible_x)
5870 {
5871 #ifdef HAVE_WINDOW_SYSTEM
5872 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5873 {
5874 if (!get_next_display_element (it)
5875 || BUFFER_POS_REACHED_P ())
5876 {
5877 result = MOVE_POS_MATCH_OR_ZV;
5878 break;
5879 }
5880 if (ITERATOR_AT_END_OF_LINE_P (it))
5881 {
5882 result = MOVE_NEWLINE_OR_CR;
5883 break;
5884 }
5885 }
5886 #endif /* HAVE_WINDOW_SYSTEM */
5887 result = MOVE_LINE_TRUNCATED;
5888 break;
5889 }
5890 }
5891
5892 #undef BUFFER_POS_REACHED_P
5893
5894 /* Restore the iterator settings altered at the beginning of this
5895 function. */
5896 it->glyph_row = saved_glyph_row;
5897 return result;
5898 }
5899
5900
5901 /* Move IT forward until it satisfies one or more of the criteria in
5902 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5903
5904 OP is a bit-mask that specifies where to stop, and in particular,
5905 which of those four position arguments makes a difference. See the
5906 description of enum move_operation_enum.
5907
5908 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5909 screen line, this function will set IT to the next position >
5910 TO_CHARPOS. */
5911
5912 void
5913 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5914 struct it *it;
5915 int to_charpos, to_x, to_y, to_vpos;
5916 int op;
5917 {
5918 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5919 int line_height;
5920 int reached = 0;
5921
5922 for (;;)
5923 {
5924 if (op & MOVE_TO_VPOS)
5925 {
5926 /* If no TO_CHARPOS and no TO_X specified, stop at the
5927 start of the line TO_VPOS. */
5928 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5929 {
5930 if (it->vpos == to_vpos)
5931 {
5932 reached = 1;
5933 break;
5934 }
5935 else
5936 skip = move_it_in_display_line_to (it, -1, -1, 0);
5937 }
5938 else
5939 {
5940 /* TO_VPOS >= 0 means stop at TO_X in the line at
5941 TO_VPOS, or at TO_POS, whichever comes first. */
5942 if (it->vpos == to_vpos)
5943 {
5944 reached = 2;
5945 break;
5946 }
5947
5948 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5949
5950 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5951 {
5952 reached = 3;
5953 break;
5954 }
5955 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5956 {
5957 /* We have reached TO_X but not in the line we want. */
5958 skip = move_it_in_display_line_to (it, to_charpos,
5959 -1, MOVE_TO_POS);
5960 if (skip == MOVE_POS_MATCH_OR_ZV)
5961 {
5962 reached = 4;
5963 break;
5964 }
5965 }
5966 }
5967 }
5968 else if (op & MOVE_TO_Y)
5969 {
5970 struct it it_backup;
5971
5972 /* TO_Y specified means stop at TO_X in the line containing
5973 TO_Y---or at TO_CHARPOS if this is reached first. The
5974 problem is that we can't really tell whether the line
5975 contains TO_Y before we have completely scanned it, and
5976 this may skip past TO_X. What we do is to first scan to
5977 TO_X.
5978
5979 If TO_X is not specified, use a TO_X of zero. The reason
5980 is to make the outcome of this function more predictable.
5981 If we didn't use TO_X == 0, we would stop at the end of
5982 the line which is probably not what a caller would expect
5983 to happen. */
5984 skip = move_it_in_display_line_to (it, to_charpos,
5985 ((op & MOVE_TO_X)
5986 ? to_x : 0),
5987 (MOVE_TO_X
5988 | (op & MOVE_TO_POS)));
5989
5990 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5991 if (skip == MOVE_POS_MATCH_OR_ZV)
5992 {
5993 reached = 5;
5994 break;
5995 }
5996
5997 /* If TO_X was reached, we would like to know whether TO_Y
5998 is in the line. This can only be said if we know the
5999 total line height which requires us to scan the rest of
6000 the line. */
6001 if (skip == MOVE_X_REACHED)
6002 {
6003 it_backup = *it;
6004 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6005 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6006 op & MOVE_TO_POS);
6007 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6008 }
6009
6010 /* Now, decide whether TO_Y is in this line. */
6011 line_height = it->max_ascent + it->max_descent;
6012 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6013
6014 if (to_y >= it->current_y
6015 && to_y < it->current_y + line_height)
6016 {
6017 if (skip == MOVE_X_REACHED)
6018 /* If TO_Y is in this line and TO_X was reached above,
6019 we scanned too far. We have to restore IT's settings
6020 to the ones before skipping. */
6021 *it = it_backup;
6022 reached = 6;
6023 }
6024 else if (skip == MOVE_X_REACHED)
6025 {
6026 skip = skip2;
6027 if (skip == MOVE_POS_MATCH_OR_ZV)
6028 reached = 7;
6029 }
6030
6031 if (reached)
6032 break;
6033 }
6034 else
6035 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6036
6037 switch (skip)
6038 {
6039 case MOVE_POS_MATCH_OR_ZV:
6040 reached = 8;
6041 goto out;
6042
6043 case MOVE_NEWLINE_OR_CR:
6044 set_iterator_to_next (it, 1);
6045 it->continuation_lines_width = 0;
6046 break;
6047
6048 case MOVE_LINE_TRUNCATED:
6049 it->continuation_lines_width = 0;
6050 reseat_at_next_visible_line_start (it, 0);
6051 if ((op & MOVE_TO_POS) != 0
6052 && IT_CHARPOS (*it) > to_charpos)
6053 {
6054 reached = 9;
6055 goto out;
6056 }
6057 break;
6058
6059 case MOVE_LINE_CONTINUED:
6060 it->continuation_lines_width += it->current_x;
6061 break;
6062
6063 default:
6064 abort ();
6065 }
6066
6067 /* Reset/increment for the next run. */
6068 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6069 it->current_x = it->hpos = 0;
6070 it->current_y += it->max_ascent + it->max_descent;
6071 ++it->vpos;
6072 last_height = it->max_ascent + it->max_descent;
6073 last_max_ascent = it->max_ascent;
6074 it->max_ascent = it->max_descent = 0;
6075 }
6076
6077 out:
6078
6079 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6080 }
6081
6082
6083 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6084
6085 If DY > 0, move IT backward at least that many pixels. DY = 0
6086 means move IT backward to the preceding line start or BEGV. This
6087 function may move over more than DY pixels if IT->current_y - DY
6088 ends up in the middle of a line; in this case IT->current_y will be
6089 set to the top of the line moved to. */
6090
6091 void
6092 move_it_vertically_backward (it, dy)
6093 struct it *it;
6094 int dy;
6095 {
6096 int nlines, h;
6097 struct it it2, it3;
6098 int start_pos;
6099
6100 move_further_back:
6101 xassert (dy >= 0);
6102
6103 start_pos = IT_CHARPOS (*it);
6104
6105 /* Estimate how many newlines we must move back. */
6106 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6107
6108 /* Set the iterator's position that many lines back. */
6109 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6110 back_to_previous_visible_line_start (it);
6111
6112 /* Reseat the iterator here. When moving backward, we don't want
6113 reseat to skip forward over invisible text, set up the iterator
6114 to deliver from overlay strings at the new position etc. So,
6115 use reseat_1 here. */
6116 reseat_1 (it, it->current.pos, 1);
6117
6118 /* We are now surely at a line start. */
6119 it->current_x = it->hpos = 0;
6120 it->continuation_lines_width = 0;
6121
6122 /* Move forward and see what y-distance we moved. First move to the
6123 start of the next line so that we get its height. We need this
6124 height to be able to tell whether we reached the specified
6125 y-distance. */
6126 it2 = *it;
6127 it2.max_ascent = it2.max_descent = 0;
6128 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6129 MOVE_TO_POS | MOVE_TO_VPOS);
6130 xassert (IT_CHARPOS (*it) >= BEGV);
6131 it3 = it2;
6132
6133 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6134 xassert (IT_CHARPOS (*it) >= BEGV);
6135 /* H is the actual vertical distance from the position in *IT
6136 and the starting position. */
6137 h = it2.current_y - it->current_y;
6138 /* NLINES is the distance in number of lines. */
6139 nlines = it2.vpos - it->vpos;
6140
6141 /* Correct IT's y and vpos position
6142 so that they are relative to the starting point. */
6143 it->vpos -= nlines;
6144 it->current_y -= h;
6145
6146 if (dy == 0)
6147 {
6148 /* DY == 0 means move to the start of the screen line. The
6149 value of nlines is > 0 if continuation lines were involved. */
6150 if (nlines > 0)
6151 move_it_by_lines (it, nlines, 1);
6152 xassert (IT_CHARPOS (*it) <= start_pos);
6153 }
6154 else
6155 {
6156 /* The y-position we try to reach, relative to *IT.
6157 Note that H has been subtracted in front of the if-statement. */
6158 int target_y = it->current_y + h - dy;
6159 int y0 = it3.current_y;
6160 int y1 = line_bottom_y (&it3);
6161 int line_height = y1 - y0;
6162
6163 /* If we did not reach target_y, try to move further backward if
6164 we can. If we moved too far backward, try to move forward. */
6165 if (target_y < it->current_y
6166 /* This is heuristic. In a window that's 3 lines high, with
6167 a line height of 13 pixels each, recentering with point
6168 on the bottom line will try to move -39/2 = 19 pixels
6169 backward. Try to avoid moving into the first line. */
6170 && it->current_y - target_y > line_height * 2 / 3
6171 && IT_CHARPOS (*it) > BEGV)
6172 {
6173 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6174 target_y - it->current_y));
6175 dy = it->current_y - target_y;
6176 goto move_further_back;
6177 }
6178 else if (target_y >= it->current_y + line_height
6179 && IT_CHARPOS (*it) < ZV)
6180 {
6181 /* Should move forward by at least one line, maybe more.
6182
6183 Note: Calling move_it_by_lines can be expensive on
6184 terminal frames, where compute_motion is used (via
6185 vmotion) to do the job, when there are very long lines
6186 and truncate-lines is nil. That's the reason for
6187 treating terminal frames specially here. */
6188
6189 if (!FRAME_WINDOW_P (it->f))
6190 move_it_vertically (it, target_y - (it->current_y + line_height));
6191 else
6192 {
6193 do
6194 {
6195 move_it_by_lines (it, 1, 1);
6196 }
6197 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6198 }
6199
6200 xassert (IT_CHARPOS (*it) >= BEGV);
6201 }
6202 }
6203 }
6204
6205
6206 /* Move IT by a specified amount of pixel lines DY. DY negative means
6207 move backwards. DY = 0 means move to start of screen line. At the
6208 end, IT will be on the start of a screen line. */
6209
6210 void
6211 move_it_vertically (it, dy)
6212 struct it *it;
6213 int dy;
6214 {
6215 if (dy <= 0)
6216 move_it_vertically_backward (it, -dy);
6217 else
6218 {
6219 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6220 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6221 MOVE_TO_POS | MOVE_TO_Y);
6222 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6223
6224 /* If buffer ends in ZV without a newline, move to the start of
6225 the line to satisfy the post-condition. */
6226 if (IT_CHARPOS (*it) == ZV
6227 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6228 move_it_by_lines (it, 0, 0);
6229 }
6230 }
6231
6232
6233 /* Move iterator IT past the end of the text line it is in. */
6234
6235 void
6236 move_it_past_eol (it)
6237 struct it *it;
6238 {
6239 enum move_it_result rc;
6240
6241 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6242 if (rc == MOVE_NEWLINE_OR_CR)
6243 set_iterator_to_next (it, 0);
6244 }
6245
6246
6247 #if 0 /* Currently not used. */
6248
6249 /* Return non-zero if some text between buffer positions START_CHARPOS
6250 and END_CHARPOS is invisible. IT->window is the window for text
6251 property lookup. */
6252
6253 static int
6254 invisible_text_between_p (it, start_charpos, end_charpos)
6255 struct it *it;
6256 int start_charpos, end_charpos;
6257 {
6258 Lisp_Object prop, limit;
6259 int invisible_found_p;
6260
6261 xassert (it != NULL && start_charpos <= end_charpos);
6262
6263 /* Is text at START invisible? */
6264 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6265 it->window);
6266 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6267 invisible_found_p = 1;
6268 else
6269 {
6270 limit = Fnext_single_char_property_change (make_number (start_charpos),
6271 Qinvisible, Qnil,
6272 make_number (end_charpos));
6273 invisible_found_p = XFASTINT (limit) < end_charpos;
6274 }
6275
6276 return invisible_found_p;
6277 }
6278
6279 #endif /* 0 */
6280
6281
6282 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6283 negative means move up. DVPOS == 0 means move to the start of the
6284 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6285 NEED_Y_P is zero, IT->current_y will be left unchanged.
6286
6287 Further optimization ideas: If we would know that IT->f doesn't use
6288 a face with proportional font, we could be faster for
6289 truncate-lines nil. */
6290
6291 void
6292 move_it_by_lines (it, dvpos, need_y_p)
6293 struct it *it;
6294 int dvpos, need_y_p;
6295 {
6296 struct position pos;
6297
6298 if (!FRAME_WINDOW_P (it->f))
6299 {
6300 struct text_pos textpos;
6301
6302 /* We can use vmotion on frames without proportional fonts. */
6303 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6304 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6305 reseat (it, textpos, 1);
6306 it->vpos += pos.vpos;
6307 it->current_y += pos.vpos;
6308 }
6309 else if (dvpos == 0)
6310 {
6311 /* DVPOS == 0 means move to the start of the screen line. */
6312 move_it_vertically_backward (it, 0);
6313 xassert (it->current_x == 0 && it->hpos == 0);
6314 /* Let next call to line_bottom_y calculate real line height */
6315 last_height = 0;
6316 }
6317 else if (dvpos > 0)
6318 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6319 else
6320 {
6321 struct it it2;
6322 int start_charpos, i;
6323
6324 /* Start at the beginning of the screen line containing IT's
6325 position. */
6326 move_it_vertically_backward (it, 0);
6327
6328 /* Go back -DVPOS visible lines and reseat the iterator there. */
6329 start_charpos = IT_CHARPOS (*it);
6330 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6331 back_to_previous_visible_line_start (it);
6332 reseat (it, it->current.pos, 1);
6333 it->current_x = it->hpos = 0;
6334
6335 /* Above call may have moved too far if continuation lines
6336 are involved. Scan forward and see if it did. */
6337 it2 = *it;
6338 it2.vpos = it2.current_y = 0;
6339 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6340 it->vpos -= it2.vpos;
6341 it->current_y -= it2.current_y;
6342 it->current_x = it->hpos = 0;
6343
6344 /* If we moved too far, move IT some lines forward. */
6345 if (it2.vpos > -dvpos)
6346 {
6347 int delta = it2.vpos + dvpos;
6348 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6349 }
6350 }
6351 }
6352
6353 /* Return 1 if IT points into the middle of a display vector. */
6354
6355 int
6356 in_display_vector_p (it)
6357 struct it *it;
6358 {
6359 return (it->method == next_element_from_display_vector
6360 && it->current.dpvec_index > 0
6361 && it->dpvec + it->current.dpvec_index != it->dpend);
6362 }
6363
6364 \f
6365 /***********************************************************************
6366 Messages
6367 ***********************************************************************/
6368
6369
6370 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6371 to *Messages*. */
6372
6373 void
6374 add_to_log (format, arg1, arg2)
6375 char *format;
6376 Lisp_Object arg1, arg2;
6377 {
6378 Lisp_Object args[3];
6379 Lisp_Object msg, fmt;
6380 char *buffer;
6381 int len;
6382 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6383 USE_SAFE_ALLOCA;
6384
6385 /* Do nothing if called asynchronously. Inserting text into
6386 a buffer may call after-change-functions and alike and
6387 that would means running Lisp asynchronously. */
6388 if (handling_signal)
6389 return;
6390
6391 fmt = msg = Qnil;
6392 GCPRO4 (fmt, msg, arg1, arg2);
6393
6394 args[0] = fmt = build_string (format);
6395 args[1] = arg1;
6396 args[2] = arg2;
6397 msg = Fformat (3, args);
6398
6399 len = SBYTES (msg) + 1;
6400 SAFE_ALLOCA (buffer, char *, len);
6401 bcopy (SDATA (msg), buffer, len);
6402
6403 message_dolog (buffer, len - 1, 1, 0);
6404 SAFE_FREE ();
6405
6406 UNGCPRO;
6407 }
6408
6409
6410 /* Output a newline in the *Messages* buffer if "needs" one. */
6411
6412 void
6413 message_log_maybe_newline ()
6414 {
6415 if (message_log_need_newline)
6416 message_dolog ("", 0, 1, 0);
6417 }
6418
6419
6420 /* Add a string M of length NBYTES to the message log, optionally
6421 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6422 nonzero, means interpret the contents of M as multibyte. This
6423 function calls low-level routines in order to bypass text property
6424 hooks, etc. which might not be safe to run. */
6425
6426 void
6427 message_dolog (m, nbytes, nlflag, multibyte)
6428 const char *m;
6429 int nbytes, nlflag, multibyte;
6430 {
6431 if (!NILP (Vmemory_full))
6432 return;
6433
6434 if (!NILP (Vmessage_log_max))
6435 {
6436 struct buffer *oldbuf;
6437 Lisp_Object oldpoint, oldbegv, oldzv;
6438 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6439 int point_at_end = 0;
6440 int zv_at_end = 0;
6441 Lisp_Object old_deactivate_mark, tem;
6442 struct gcpro gcpro1;
6443
6444 old_deactivate_mark = Vdeactivate_mark;
6445 oldbuf = current_buffer;
6446 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6447 current_buffer->undo_list = Qt;
6448
6449 oldpoint = message_dolog_marker1;
6450 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6451 oldbegv = message_dolog_marker2;
6452 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6453 oldzv = message_dolog_marker3;
6454 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6455 GCPRO1 (old_deactivate_mark);
6456
6457 if (PT == Z)
6458 point_at_end = 1;
6459 if (ZV == Z)
6460 zv_at_end = 1;
6461
6462 BEGV = BEG;
6463 BEGV_BYTE = BEG_BYTE;
6464 ZV = Z;
6465 ZV_BYTE = Z_BYTE;
6466 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6467
6468 /* Insert the string--maybe converting multibyte to single byte
6469 or vice versa, so that all the text fits the buffer. */
6470 if (multibyte
6471 && NILP (current_buffer->enable_multibyte_characters))
6472 {
6473 int i, c, char_bytes;
6474 unsigned char work[1];
6475
6476 /* Convert a multibyte string to single-byte
6477 for the *Message* buffer. */
6478 for (i = 0; i < nbytes; i += char_bytes)
6479 {
6480 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6481 work[0] = (SINGLE_BYTE_CHAR_P (c)
6482 ? c
6483 : multibyte_char_to_unibyte (c, Qnil));
6484 insert_1_both (work, 1, 1, 1, 0, 0);
6485 }
6486 }
6487 else if (! multibyte
6488 && ! NILP (current_buffer->enable_multibyte_characters))
6489 {
6490 int i, c, char_bytes;
6491 unsigned char *msg = (unsigned char *) m;
6492 unsigned char str[MAX_MULTIBYTE_LENGTH];
6493 /* Convert a single-byte string to multibyte
6494 for the *Message* buffer. */
6495 for (i = 0; i < nbytes; i++)
6496 {
6497 c = unibyte_char_to_multibyte (msg[i]);
6498 char_bytes = CHAR_STRING (c, str);
6499 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6500 }
6501 }
6502 else if (nbytes)
6503 insert_1 (m, nbytes, 1, 0, 0);
6504
6505 if (nlflag)
6506 {
6507 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6508 insert_1 ("\n", 1, 1, 0, 0);
6509
6510 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6511 this_bol = PT;
6512 this_bol_byte = PT_BYTE;
6513
6514 /* See if this line duplicates the previous one.
6515 If so, combine duplicates. */
6516 if (this_bol > BEG)
6517 {
6518 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6519 prev_bol = PT;
6520 prev_bol_byte = PT_BYTE;
6521
6522 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6523 this_bol, this_bol_byte);
6524 if (dup)
6525 {
6526 del_range_both (prev_bol, prev_bol_byte,
6527 this_bol, this_bol_byte, 0);
6528 if (dup > 1)
6529 {
6530 char dupstr[40];
6531 int duplen;
6532
6533 /* If you change this format, don't forget to also
6534 change message_log_check_duplicate. */
6535 sprintf (dupstr, " [%d times]", dup);
6536 duplen = strlen (dupstr);
6537 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6538 insert_1 (dupstr, duplen, 1, 0, 1);
6539 }
6540 }
6541 }
6542
6543 /* If we have more than the desired maximum number of lines
6544 in the *Messages* buffer now, delete the oldest ones.
6545 This is safe because we don't have undo in this buffer. */
6546
6547 if (NATNUMP (Vmessage_log_max))
6548 {
6549 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6550 -XFASTINT (Vmessage_log_max) - 1, 0);
6551 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6552 }
6553 }
6554 BEGV = XMARKER (oldbegv)->charpos;
6555 BEGV_BYTE = marker_byte_position (oldbegv);
6556
6557 if (zv_at_end)
6558 {
6559 ZV = Z;
6560 ZV_BYTE = Z_BYTE;
6561 }
6562 else
6563 {
6564 ZV = XMARKER (oldzv)->charpos;
6565 ZV_BYTE = marker_byte_position (oldzv);
6566 }
6567
6568 if (point_at_end)
6569 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6570 else
6571 /* We can't do Fgoto_char (oldpoint) because it will run some
6572 Lisp code. */
6573 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6574 XMARKER (oldpoint)->bytepos);
6575
6576 UNGCPRO;
6577 unchain_marker (XMARKER (oldpoint));
6578 unchain_marker (XMARKER (oldbegv));
6579 unchain_marker (XMARKER (oldzv));
6580
6581 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6582 set_buffer_internal (oldbuf);
6583 if (NILP (tem))
6584 windows_or_buffers_changed = old_windows_or_buffers_changed;
6585 message_log_need_newline = !nlflag;
6586 Vdeactivate_mark = old_deactivate_mark;
6587 }
6588 }
6589
6590
6591 /* We are at the end of the buffer after just having inserted a newline.
6592 (Note: We depend on the fact we won't be crossing the gap.)
6593 Check to see if the most recent message looks a lot like the previous one.
6594 Return 0 if different, 1 if the new one should just replace it, or a
6595 value N > 1 if we should also append " [N times]". */
6596
6597 static int
6598 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6599 int prev_bol, this_bol;
6600 int prev_bol_byte, this_bol_byte;
6601 {
6602 int i;
6603 int len = Z_BYTE - 1 - this_bol_byte;
6604 int seen_dots = 0;
6605 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6606 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6607
6608 for (i = 0; i < len; i++)
6609 {
6610 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6611 seen_dots = 1;
6612 if (p1[i] != p2[i])
6613 return seen_dots;
6614 }
6615 p1 += len;
6616 if (*p1 == '\n')
6617 return 2;
6618 if (*p1++ == ' ' && *p1++ == '[')
6619 {
6620 int n = 0;
6621 while (*p1 >= '0' && *p1 <= '9')
6622 n = n * 10 + *p1++ - '0';
6623 if (strncmp (p1, " times]\n", 8) == 0)
6624 return n+1;
6625 }
6626 return 0;
6627 }
6628
6629
6630 /* Display an echo area message M with a specified length of NBYTES
6631 bytes. The string may include null characters. If M is 0, clear
6632 out any existing message, and let the mini-buffer text show
6633 through.
6634
6635 The buffer M must continue to exist until after the echo area gets
6636 cleared or some other message gets displayed there. This means do
6637 not pass text that is stored in a Lisp string; do not pass text in
6638 a buffer that was alloca'd. */
6639
6640 void
6641 message2 (m, nbytes, multibyte)
6642 const char *m;
6643 int nbytes;
6644 int multibyte;
6645 {
6646 /* First flush out any partial line written with print. */
6647 message_log_maybe_newline ();
6648 if (m)
6649 message_dolog (m, nbytes, 1, multibyte);
6650 message2_nolog (m, nbytes, multibyte);
6651 }
6652
6653
6654 /* The non-logging counterpart of message2. */
6655
6656 void
6657 message2_nolog (m, nbytes, multibyte)
6658 const char *m;
6659 int nbytes, multibyte;
6660 {
6661 struct frame *sf = SELECTED_FRAME ();
6662 message_enable_multibyte = multibyte;
6663
6664 if (noninteractive)
6665 {
6666 if (noninteractive_need_newline)
6667 putc ('\n', stderr);
6668 noninteractive_need_newline = 0;
6669 if (m)
6670 fwrite (m, nbytes, 1, stderr);
6671 if (cursor_in_echo_area == 0)
6672 fprintf (stderr, "\n");
6673 fflush (stderr);
6674 }
6675 /* A null message buffer means that the frame hasn't really been
6676 initialized yet. Error messages get reported properly by
6677 cmd_error, so this must be just an informative message; toss it. */
6678 else if (INTERACTIVE
6679 && sf->glyphs_initialized_p
6680 && FRAME_MESSAGE_BUF (sf))
6681 {
6682 Lisp_Object mini_window;
6683 struct frame *f;
6684
6685 /* Get the frame containing the mini-buffer
6686 that the selected frame is using. */
6687 mini_window = FRAME_MINIBUF_WINDOW (sf);
6688 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6689
6690 FRAME_SAMPLE_VISIBILITY (f);
6691 if (FRAME_VISIBLE_P (sf)
6692 && ! FRAME_VISIBLE_P (f))
6693 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6694
6695 if (m)
6696 {
6697 set_message (m, Qnil, nbytes, multibyte);
6698 if (minibuffer_auto_raise)
6699 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6700 }
6701 else
6702 clear_message (1, 1);
6703
6704 do_pending_window_change (0);
6705 echo_area_display (1);
6706 do_pending_window_change (0);
6707 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6708 (*frame_up_to_date_hook) (f);
6709 }
6710 }
6711
6712
6713 /* Display an echo area message M with a specified length of NBYTES
6714 bytes. The string may include null characters. If M is not a
6715 string, clear out any existing message, and let the mini-buffer
6716 text show through. */
6717
6718 void
6719 message3 (m, nbytes, multibyte)
6720 Lisp_Object m;
6721 int nbytes;
6722 int multibyte;
6723 {
6724 struct gcpro gcpro1;
6725
6726 GCPRO1 (m);
6727 clear_message (1,1);
6728
6729 /* First flush out any partial line written with print. */
6730 message_log_maybe_newline ();
6731 if (STRINGP (m))
6732 message_dolog (SDATA (m), nbytes, 1, multibyte);
6733 message3_nolog (m, nbytes, multibyte);
6734
6735 UNGCPRO;
6736 }
6737
6738
6739 /* The non-logging version of message3. */
6740
6741 void
6742 message3_nolog (m, nbytes, multibyte)
6743 Lisp_Object m;
6744 int nbytes, multibyte;
6745 {
6746 struct frame *sf = SELECTED_FRAME ();
6747 message_enable_multibyte = multibyte;
6748
6749 if (noninteractive)
6750 {
6751 if (noninteractive_need_newline)
6752 putc ('\n', stderr);
6753 noninteractive_need_newline = 0;
6754 if (STRINGP (m))
6755 fwrite (SDATA (m), nbytes, 1, stderr);
6756 if (cursor_in_echo_area == 0)
6757 fprintf (stderr, "\n");
6758 fflush (stderr);
6759 }
6760 /* A null message buffer means that the frame hasn't really been
6761 initialized yet. Error messages get reported properly by
6762 cmd_error, so this must be just an informative message; toss it. */
6763 else if (INTERACTIVE
6764 && sf->glyphs_initialized_p
6765 && FRAME_MESSAGE_BUF (sf))
6766 {
6767 Lisp_Object mini_window;
6768 Lisp_Object frame;
6769 struct frame *f;
6770
6771 /* Get the frame containing the mini-buffer
6772 that the selected frame is using. */
6773 mini_window = FRAME_MINIBUF_WINDOW (sf);
6774 frame = XWINDOW (mini_window)->frame;
6775 f = XFRAME (frame);
6776
6777 FRAME_SAMPLE_VISIBILITY (f);
6778 if (FRAME_VISIBLE_P (sf)
6779 && !FRAME_VISIBLE_P (f))
6780 Fmake_frame_visible (frame);
6781
6782 if (STRINGP (m) && SCHARS (m) > 0)
6783 {
6784 set_message (NULL, m, nbytes, multibyte);
6785 if (minibuffer_auto_raise)
6786 Fraise_frame (frame);
6787 }
6788 else
6789 clear_message (1, 1);
6790
6791 do_pending_window_change (0);
6792 echo_area_display (1);
6793 do_pending_window_change (0);
6794 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6795 (*frame_up_to_date_hook) (f);
6796 }
6797 }
6798
6799
6800 /* Display a null-terminated echo area message M. If M is 0, clear
6801 out any existing message, and let the mini-buffer text show through.
6802
6803 The buffer M must continue to exist until after the echo area gets
6804 cleared or some other message gets displayed there. Do not pass
6805 text that is stored in a Lisp string. Do not pass text in a buffer
6806 that was alloca'd. */
6807
6808 void
6809 message1 (m)
6810 char *m;
6811 {
6812 message2 (m, (m ? strlen (m) : 0), 0);
6813 }
6814
6815
6816 /* The non-logging counterpart of message1. */
6817
6818 void
6819 message1_nolog (m)
6820 char *m;
6821 {
6822 message2_nolog (m, (m ? strlen (m) : 0), 0);
6823 }
6824
6825 /* Display a message M which contains a single %s
6826 which gets replaced with STRING. */
6827
6828 void
6829 message_with_string (m, string, log)
6830 char *m;
6831 Lisp_Object string;
6832 int log;
6833 {
6834 CHECK_STRING (string);
6835
6836 if (noninteractive)
6837 {
6838 if (m)
6839 {
6840 if (noninteractive_need_newline)
6841 putc ('\n', stderr);
6842 noninteractive_need_newline = 0;
6843 fprintf (stderr, m, SDATA (string));
6844 if (cursor_in_echo_area == 0)
6845 fprintf (stderr, "\n");
6846 fflush (stderr);
6847 }
6848 }
6849 else if (INTERACTIVE)
6850 {
6851 /* The frame whose minibuffer we're going to display the message on.
6852 It may be larger than the selected frame, so we need
6853 to use its buffer, not the selected frame's buffer. */
6854 Lisp_Object mini_window;
6855 struct frame *f, *sf = SELECTED_FRAME ();
6856
6857 /* Get the frame containing the minibuffer
6858 that the selected frame is using. */
6859 mini_window = FRAME_MINIBUF_WINDOW (sf);
6860 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6861
6862 /* A null message buffer means that the frame hasn't really been
6863 initialized yet. Error messages get reported properly by
6864 cmd_error, so this must be just an informative message; toss it. */
6865 if (FRAME_MESSAGE_BUF (f))
6866 {
6867 Lisp_Object args[2], message;
6868 struct gcpro gcpro1, gcpro2;
6869
6870 args[0] = build_string (m);
6871 args[1] = message = string;
6872 GCPRO2 (args[0], message);
6873 gcpro1.nvars = 2;
6874
6875 message = Fformat (2, args);
6876
6877 if (log)
6878 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6879 else
6880 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6881
6882 UNGCPRO;
6883
6884 /* Print should start at the beginning of the message
6885 buffer next time. */
6886 message_buf_print = 0;
6887 }
6888 }
6889 }
6890
6891
6892 /* Dump an informative message to the minibuf. If M is 0, clear out
6893 any existing message, and let the mini-buffer text show through. */
6894
6895 /* VARARGS 1 */
6896 void
6897 message (m, a1, a2, a3)
6898 char *m;
6899 EMACS_INT a1, a2, a3;
6900 {
6901 if (noninteractive)
6902 {
6903 if (m)
6904 {
6905 if (noninteractive_need_newline)
6906 putc ('\n', stderr);
6907 noninteractive_need_newline = 0;
6908 fprintf (stderr, m, a1, a2, a3);
6909 if (cursor_in_echo_area == 0)
6910 fprintf (stderr, "\n");
6911 fflush (stderr);
6912 }
6913 }
6914 else if (INTERACTIVE)
6915 {
6916 /* The frame whose mini-buffer we're going to display the message
6917 on. It may be larger than the selected frame, so we need to
6918 use its buffer, not the selected frame's buffer. */
6919 Lisp_Object mini_window;
6920 struct frame *f, *sf = SELECTED_FRAME ();
6921
6922 /* Get the frame containing the mini-buffer
6923 that the selected frame is using. */
6924 mini_window = FRAME_MINIBUF_WINDOW (sf);
6925 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6926
6927 /* A null message buffer means that the frame hasn't really been
6928 initialized yet. Error messages get reported properly by
6929 cmd_error, so this must be just an informative message; toss
6930 it. */
6931 if (FRAME_MESSAGE_BUF (f))
6932 {
6933 if (m)
6934 {
6935 int len;
6936 #ifdef NO_ARG_ARRAY
6937 char *a[3];
6938 a[0] = (char *) a1;
6939 a[1] = (char *) a2;
6940 a[2] = (char *) a3;
6941
6942 len = doprnt (FRAME_MESSAGE_BUF (f),
6943 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6944 #else
6945 len = doprnt (FRAME_MESSAGE_BUF (f),
6946 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6947 (char **) &a1);
6948 #endif /* NO_ARG_ARRAY */
6949
6950 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6951 }
6952 else
6953 message1 (0);
6954
6955 /* Print should start at the beginning of the message
6956 buffer next time. */
6957 message_buf_print = 0;
6958 }
6959 }
6960 }
6961
6962
6963 /* The non-logging version of message. */
6964
6965 void
6966 message_nolog (m, a1, a2, a3)
6967 char *m;
6968 EMACS_INT a1, a2, a3;
6969 {
6970 Lisp_Object old_log_max;
6971 old_log_max = Vmessage_log_max;
6972 Vmessage_log_max = Qnil;
6973 message (m, a1, a2, a3);
6974 Vmessage_log_max = old_log_max;
6975 }
6976
6977
6978 /* Display the current message in the current mini-buffer. This is
6979 only called from error handlers in process.c, and is not time
6980 critical. */
6981
6982 void
6983 update_echo_area ()
6984 {
6985 if (!NILP (echo_area_buffer[0]))
6986 {
6987 Lisp_Object string;
6988 string = Fcurrent_message ();
6989 message3 (string, SBYTES (string),
6990 !NILP (current_buffer->enable_multibyte_characters));
6991 }
6992 }
6993
6994
6995 /* Make sure echo area buffers in `echo_buffers' are live.
6996 If they aren't, make new ones. */
6997
6998 static void
6999 ensure_echo_area_buffers ()
7000 {
7001 int i;
7002
7003 for (i = 0; i < 2; ++i)
7004 if (!BUFFERP (echo_buffer[i])
7005 || NILP (XBUFFER (echo_buffer[i])->name))
7006 {
7007 char name[30];
7008 Lisp_Object old_buffer;
7009 int j;
7010
7011 old_buffer = echo_buffer[i];
7012 sprintf (name, " *Echo Area %d*", i);
7013 echo_buffer[i] = Fget_buffer_create (build_string (name));
7014 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7015
7016 for (j = 0; j < 2; ++j)
7017 if (EQ (old_buffer, echo_area_buffer[j]))
7018 echo_area_buffer[j] = echo_buffer[i];
7019 }
7020 }
7021
7022
7023 /* Call FN with args A1..A4 with either the current or last displayed
7024 echo_area_buffer as current buffer.
7025
7026 WHICH zero means use the current message buffer
7027 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7028 from echo_buffer[] and clear it.
7029
7030 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7031 suitable buffer from echo_buffer[] and clear it.
7032
7033 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7034 that the current message becomes the last displayed one, make
7035 choose a suitable buffer for echo_area_buffer[0], and clear it.
7036
7037 Value is what FN returns. */
7038
7039 static int
7040 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7041 struct window *w;
7042 int which;
7043 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7044 EMACS_INT a1;
7045 Lisp_Object a2;
7046 EMACS_INT a3, a4;
7047 {
7048 Lisp_Object buffer;
7049 int this_one, the_other, clear_buffer_p, rc;
7050 int count = SPECPDL_INDEX ();
7051
7052 /* If buffers aren't live, make new ones. */
7053 ensure_echo_area_buffers ();
7054
7055 clear_buffer_p = 0;
7056
7057 if (which == 0)
7058 this_one = 0, the_other = 1;
7059 else if (which > 0)
7060 this_one = 1, the_other = 0;
7061 else
7062 {
7063 this_one = 0, the_other = 1;
7064 clear_buffer_p = 1;
7065
7066 /* We need a fresh one in case the current echo buffer equals
7067 the one containing the last displayed echo area message. */
7068 if (!NILP (echo_area_buffer[this_one])
7069 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7070 echo_area_buffer[this_one] = Qnil;
7071 }
7072
7073 /* Choose a suitable buffer from echo_buffer[] is we don't
7074 have one. */
7075 if (NILP (echo_area_buffer[this_one]))
7076 {
7077 echo_area_buffer[this_one]
7078 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7079 ? echo_buffer[the_other]
7080 : echo_buffer[this_one]);
7081 clear_buffer_p = 1;
7082 }
7083
7084 buffer = echo_area_buffer[this_one];
7085
7086 /* Don't get confused by reusing the buffer used for echoing
7087 for a different purpose. */
7088 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7089 cancel_echoing ();
7090
7091 record_unwind_protect (unwind_with_echo_area_buffer,
7092 with_echo_area_buffer_unwind_data (w));
7093
7094 /* Make the echo area buffer current. Note that for display
7095 purposes, it is not necessary that the displayed window's buffer
7096 == current_buffer, except for text property lookup. So, let's
7097 only set that buffer temporarily here without doing a full
7098 Fset_window_buffer. We must also change w->pointm, though,
7099 because otherwise an assertions in unshow_buffer fails, and Emacs
7100 aborts. */
7101 set_buffer_internal_1 (XBUFFER (buffer));
7102 if (w)
7103 {
7104 w->buffer = buffer;
7105 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7106 }
7107
7108 current_buffer->undo_list = Qt;
7109 current_buffer->read_only = Qnil;
7110 specbind (Qinhibit_read_only, Qt);
7111 specbind (Qinhibit_modification_hooks, Qt);
7112
7113 if (clear_buffer_p && Z > BEG)
7114 del_range (BEG, Z);
7115
7116 xassert (BEGV >= BEG);
7117 xassert (ZV <= Z && ZV >= BEGV);
7118
7119 rc = fn (a1, a2, a3, a4);
7120
7121 xassert (BEGV >= BEG);
7122 xassert (ZV <= Z && ZV >= BEGV);
7123
7124 unbind_to (count, Qnil);
7125 return rc;
7126 }
7127
7128
7129 /* Save state that should be preserved around the call to the function
7130 FN called in with_echo_area_buffer. */
7131
7132 static Lisp_Object
7133 with_echo_area_buffer_unwind_data (w)
7134 struct window *w;
7135 {
7136 int i = 0;
7137 Lisp_Object vector;
7138
7139 /* Reduce consing by keeping one vector in
7140 Vwith_echo_area_save_vector. */
7141 vector = Vwith_echo_area_save_vector;
7142 Vwith_echo_area_save_vector = Qnil;
7143
7144 if (NILP (vector))
7145 vector = Fmake_vector (make_number (7), Qnil);
7146
7147 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7148 AREF (vector, i) = Vdeactivate_mark, ++i;
7149 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7150
7151 if (w)
7152 {
7153 XSETWINDOW (AREF (vector, i), w); ++i;
7154 AREF (vector, i) = w->buffer; ++i;
7155 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7156 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7157 }
7158 else
7159 {
7160 int end = i + 4;
7161 for (; i < end; ++i)
7162 AREF (vector, i) = Qnil;
7163 }
7164
7165 xassert (i == ASIZE (vector));
7166 return vector;
7167 }
7168
7169
7170 /* Restore global state from VECTOR which was created by
7171 with_echo_area_buffer_unwind_data. */
7172
7173 static Lisp_Object
7174 unwind_with_echo_area_buffer (vector)
7175 Lisp_Object vector;
7176 {
7177 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7178 Vdeactivate_mark = AREF (vector, 1);
7179 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7180
7181 if (WINDOWP (AREF (vector, 3)))
7182 {
7183 struct window *w;
7184 Lisp_Object buffer, charpos, bytepos;
7185
7186 w = XWINDOW (AREF (vector, 3));
7187 buffer = AREF (vector, 4);
7188 charpos = AREF (vector, 5);
7189 bytepos = AREF (vector, 6);
7190
7191 w->buffer = buffer;
7192 set_marker_both (w->pointm, buffer,
7193 XFASTINT (charpos), XFASTINT (bytepos));
7194 }
7195
7196 Vwith_echo_area_save_vector = vector;
7197 return Qnil;
7198 }
7199
7200
7201 /* Set up the echo area for use by print functions. MULTIBYTE_P
7202 non-zero means we will print multibyte. */
7203
7204 void
7205 setup_echo_area_for_printing (multibyte_p)
7206 int multibyte_p;
7207 {
7208 /* If we can't find an echo area any more, exit. */
7209 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7210 Fkill_emacs (Qnil);
7211
7212 ensure_echo_area_buffers ();
7213
7214 if (!message_buf_print)
7215 {
7216 /* A message has been output since the last time we printed.
7217 Choose a fresh echo area buffer. */
7218 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7219 echo_area_buffer[0] = echo_buffer[1];
7220 else
7221 echo_area_buffer[0] = echo_buffer[0];
7222
7223 /* Switch to that buffer and clear it. */
7224 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7225 current_buffer->truncate_lines = Qnil;
7226
7227 if (Z > BEG)
7228 {
7229 int count = SPECPDL_INDEX ();
7230 specbind (Qinhibit_read_only, Qt);
7231 /* Note that undo recording is always disabled. */
7232 del_range (BEG, Z);
7233 unbind_to (count, Qnil);
7234 }
7235 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7236
7237 /* Set up the buffer for the multibyteness we need. */
7238 if (multibyte_p
7239 != !NILP (current_buffer->enable_multibyte_characters))
7240 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7241
7242 /* Raise the frame containing the echo area. */
7243 if (minibuffer_auto_raise)
7244 {
7245 struct frame *sf = SELECTED_FRAME ();
7246 Lisp_Object mini_window;
7247 mini_window = FRAME_MINIBUF_WINDOW (sf);
7248 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7249 }
7250
7251 message_log_maybe_newline ();
7252 message_buf_print = 1;
7253 }
7254 else
7255 {
7256 if (NILP (echo_area_buffer[0]))
7257 {
7258 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7259 echo_area_buffer[0] = echo_buffer[1];
7260 else
7261 echo_area_buffer[0] = echo_buffer[0];
7262 }
7263
7264 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7265 {
7266 /* Someone switched buffers between print requests. */
7267 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7268 current_buffer->truncate_lines = Qnil;
7269 }
7270 }
7271 }
7272
7273
7274 /* Display an echo area message in window W. Value is non-zero if W's
7275 height is changed. If display_last_displayed_message_p is
7276 non-zero, display the message that was last displayed, otherwise
7277 display the current message. */
7278
7279 static int
7280 display_echo_area (w)
7281 struct window *w;
7282 {
7283 int i, no_message_p, window_height_changed_p, count;
7284
7285 /* Temporarily disable garbage collections while displaying the echo
7286 area. This is done because a GC can print a message itself.
7287 That message would modify the echo area buffer's contents while a
7288 redisplay of the buffer is going on, and seriously confuse
7289 redisplay. */
7290 count = inhibit_garbage_collection ();
7291
7292 /* If there is no message, we must call display_echo_area_1
7293 nevertheless because it resizes the window. But we will have to
7294 reset the echo_area_buffer in question to nil at the end because
7295 with_echo_area_buffer will sets it to an empty buffer. */
7296 i = display_last_displayed_message_p ? 1 : 0;
7297 no_message_p = NILP (echo_area_buffer[i]);
7298
7299 window_height_changed_p
7300 = with_echo_area_buffer (w, display_last_displayed_message_p,
7301 display_echo_area_1,
7302 (EMACS_INT) w, Qnil, 0, 0);
7303
7304 if (no_message_p)
7305 echo_area_buffer[i] = Qnil;
7306
7307 unbind_to (count, Qnil);
7308 return window_height_changed_p;
7309 }
7310
7311
7312 /* Helper for display_echo_area. Display the current buffer which
7313 contains the current echo area message in window W, a mini-window,
7314 a pointer to which is passed in A1. A2..A4 are currently not used.
7315 Change the height of W so that all of the message is displayed.
7316 Value is non-zero if height of W was changed. */
7317
7318 static int
7319 display_echo_area_1 (a1, a2, a3, a4)
7320 EMACS_INT a1;
7321 Lisp_Object a2;
7322 EMACS_INT a3, a4;
7323 {
7324 struct window *w = (struct window *) a1;
7325 Lisp_Object window;
7326 struct text_pos start;
7327 int window_height_changed_p = 0;
7328
7329 /* Do this before displaying, so that we have a large enough glyph
7330 matrix for the display. */
7331 window_height_changed_p = resize_mini_window (w, 0);
7332
7333 /* Display. */
7334 clear_glyph_matrix (w->desired_matrix);
7335 XSETWINDOW (window, w);
7336 SET_TEXT_POS (start, BEG, BEG_BYTE);
7337 try_window (window, start);
7338
7339 return window_height_changed_p;
7340 }
7341
7342
7343 /* Resize the echo area window to exactly the size needed for the
7344 currently displayed message, if there is one. If a mini-buffer
7345 is active, don't shrink it. */
7346
7347 void
7348 resize_echo_area_exactly ()
7349 {
7350 if (BUFFERP (echo_area_buffer[0])
7351 && WINDOWP (echo_area_window))
7352 {
7353 struct window *w = XWINDOW (echo_area_window);
7354 int resized_p;
7355 Lisp_Object resize_exactly;
7356
7357 if (minibuf_level == 0)
7358 resize_exactly = Qt;
7359 else
7360 resize_exactly = Qnil;
7361
7362 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7363 (EMACS_INT) w, resize_exactly, 0, 0);
7364 if (resized_p)
7365 {
7366 ++windows_or_buffers_changed;
7367 ++update_mode_lines;
7368 redisplay_internal (0);
7369 }
7370 }
7371 }
7372
7373
7374 /* Callback function for with_echo_area_buffer, when used from
7375 resize_echo_area_exactly. A1 contains a pointer to the window to
7376 resize, EXACTLY non-nil means resize the mini-window exactly to the
7377 size of the text displayed. A3 and A4 are not used. Value is what
7378 resize_mini_window returns. */
7379
7380 static int
7381 resize_mini_window_1 (a1, exactly, a3, a4)
7382 EMACS_INT a1;
7383 Lisp_Object exactly;
7384 EMACS_INT a3, a4;
7385 {
7386 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7387 }
7388
7389
7390 /* Resize mini-window W to fit the size of its contents. EXACT:P
7391 means size the window exactly to the size needed. Otherwise, it's
7392 only enlarged until W's buffer is empty. Value is non-zero if
7393 the window height has been changed. */
7394
7395 int
7396 resize_mini_window (w, exact_p)
7397 struct window *w;
7398 int exact_p;
7399 {
7400 struct frame *f = XFRAME (w->frame);
7401 int window_height_changed_p = 0;
7402
7403 xassert (MINI_WINDOW_P (w));
7404
7405 /* Don't resize windows while redisplaying a window; it would
7406 confuse redisplay functions when the size of the window they are
7407 displaying changes from under them. Such a resizing can happen,
7408 for instance, when which-func prints a long message while
7409 we are running fontification-functions. We're running these
7410 functions with safe_call which binds inhibit-redisplay to t. */
7411 if (!NILP (Vinhibit_redisplay))
7412 return 0;
7413
7414 /* Nil means don't try to resize. */
7415 if (NILP (Vresize_mini_windows)
7416 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7417 return 0;
7418
7419 if (!FRAME_MINIBUF_ONLY_P (f))
7420 {
7421 struct it it;
7422 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7423 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7424 int height, max_height;
7425 int unit = FRAME_LINE_HEIGHT (f);
7426 struct text_pos start;
7427 struct buffer *old_current_buffer = NULL;
7428
7429 if (current_buffer != XBUFFER (w->buffer))
7430 {
7431 old_current_buffer = current_buffer;
7432 set_buffer_internal (XBUFFER (w->buffer));
7433 }
7434
7435 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7436
7437 /* Compute the max. number of lines specified by the user. */
7438 if (FLOATP (Vmax_mini_window_height))
7439 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7440 else if (INTEGERP (Vmax_mini_window_height))
7441 max_height = XINT (Vmax_mini_window_height);
7442 else
7443 max_height = total_height / 4;
7444
7445 /* Correct that max. height if it's bogus. */
7446 max_height = max (1, max_height);
7447 max_height = min (total_height, max_height);
7448
7449 /* Find out the height of the text in the window. */
7450 if (it.truncate_lines_p)
7451 height = 1;
7452 else
7453 {
7454 last_height = 0;
7455 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7456 if (it.max_ascent == 0 && it.max_descent == 0)
7457 height = it.current_y + last_height;
7458 else
7459 height = it.current_y + it.max_ascent + it.max_descent;
7460 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7461 height = (height + unit - 1) / unit;
7462 }
7463
7464 /* Compute a suitable window start. */
7465 if (height > max_height)
7466 {
7467 height = max_height;
7468 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7469 move_it_vertically_backward (&it, (height - 1) * unit);
7470 start = it.current.pos;
7471 }
7472 else
7473 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7474 SET_MARKER_FROM_TEXT_POS (w->start, start);
7475
7476 if (EQ (Vresize_mini_windows, Qgrow_only))
7477 {
7478 /* Let it grow only, until we display an empty message, in which
7479 case the window shrinks again. */
7480 if (height > WINDOW_TOTAL_LINES (w))
7481 {
7482 int old_height = WINDOW_TOTAL_LINES (w);
7483 freeze_window_starts (f, 1);
7484 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7485 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7486 }
7487 else if (height < WINDOW_TOTAL_LINES (w)
7488 && (exact_p || BEGV == ZV))
7489 {
7490 int old_height = WINDOW_TOTAL_LINES (w);
7491 freeze_window_starts (f, 0);
7492 shrink_mini_window (w);
7493 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7494 }
7495 }
7496 else
7497 {
7498 /* Always resize to exact size needed. */
7499 if (height > WINDOW_TOTAL_LINES (w))
7500 {
7501 int old_height = WINDOW_TOTAL_LINES (w);
7502 freeze_window_starts (f, 1);
7503 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7504 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7505 }
7506 else if (height < WINDOW_TOTAL_LINES (w))
7507 {
7508 int old_height = WINDOW_TOTAL_LINES (w);
7509 freeze_window_starts (f, 0);
7510 shrink_mini_window (w);
7511
7512 if (height)
7513 {
7514 freeze_window_starts (f, 1);
7515 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7516 }
7517
7518 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7519 }
7520 }
7521
7522 if (old_current_buffer)
7523 set_buffer_internal (old_current_buffer);
7524 }
7525
7526 return window_height_changed_p;
7527 }
7528
7529
7530 /* Value is the current message, a string, or nil if there is no
7531 current message. */
7532
7533 Lisp_Object
7534 current_message ()
7535 {
7536 Lisp_Object msg;
7537
7538 if (NILP (echo_area_buffer[0]))
7539 msg = Qnil;
7540 else
7541 {
7542 with_echo_area_buffer (0, 0, current_message_1,
7543 (EMACS_INT) &msg, Qnil, 0, 0);
7544 if (NILP (msg))
7545 echo_area_buffer[0] = Qnil;
7546 }
7547
7548 return msg;
7549 }
7550
7551
7552 static int
7553 current_message_1 (a1, a2, a3, a4)
7554 EMACS_INT a1;
7555 Lisp_Object a2;
7556 EMACS_INT a3, a4;
7557 {
7558 Lisp_Object *msg = (Lisp_Object *) a1;
7559
7560 if (Z > BEG)
7561 *msg = make_buffer_string (BEG, Z, 1);
7562 else
7563 *msg = Qnil;
7564 return 0;
7565 }
7566
7567
7568 /* Push the current message on Vmessage_stack for later restauration
7569 by restore_message. Value is non-zero if the current message isn't
7570 empty. This is a relatively infrequent operation, so it's not
7571 worth optimizing. */
7572
7573 int
7574 push_message ()
7575 {
7576 Lisp_Object msg;
7577 msg = current_message ();
7578 Vmessage_stack = Fcons (msg, Vmessage_stack);
7579 return STRINGP (msg);
7580 }
7581
7582
7583 /* Restore message display from the top of Vmessage_stack. */
7584
7585 void
7586 restore_message ()
7587 {
7588 Lisp_Object msg;
7589
7590 xassert (CONSP (Vmessage_stack));
7591 msg = XCAR (Vmessage_stack);
7592 if (STRINGP (msg))
7593 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7594 else
7595 message3_nolog (msg, 0, 0);
7596 }
7597
7598
7599 /* Handler for record_unwind_protect calling pop_message. */
7600
7601 Lisp_Object
7602 pop_message_unwind (dummy)
7603 Lisp_Object dummy;
7604 {
7605 pop_message ();
7606 return Qnil;
7607 }
7608
7609 /* Pop the top-most entry off Vmessage_stack. */
7610
7611 void
7612 pop_message ()
7613 {
7614 xassert (CONSP (Vmessage_stack));
7615 Vmessage_stack = XCDR (Vmessage_stack);
7616 }
7617
7618
7619 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7620 exits. If the stack is not empty, we have a missing pop_message
7621 somewhere. */
7622
7623 void
7624 check_message_stack ()
7625 {
7626 if (!NILP (Vmessage_stack))
7627 abort ();
7628 }
7629
7630
7631 /* Truncate to NCHARS what will be displayed in the echo area the next
7632 time we display it---but don't redisplay it now. */
7633
7634 void
7635 truncate_echo_area (nchars)
7636 int nchars;
7637 {
7638 if (nchars == 0)
7639 echo_area_buffer[0] = Qnil;
7640 /* A null message buffer means that the frame hasn't really been
7641 initialized yet. Error messages get reported properly by
7642 cmd_error, so this must be just an informative message; toss it. */
7643 else if (!noninteractive
7644 && INTERACTIVE
7645 && !NILP (echo_area_buffer[0]))
7646 {
7647 struct frame *sf = SELECTED_FRAME ();
7648 if (FRAME_MESSAGE_BUF (sf))
7649 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7650 }
7651 }
7652
7653
7654 /* Helper function for truncate_echo_area. Truncate the current
7655 message to at most NCHARS characters. */
7656
7657 static int
7658 truncate_message_1 (nchars, a2, a3, a4)
7659 EMACS_INT nchars;
7660 Lisp_Object a2;
7661 EMACS_INT a3, a4;
7662 {
7663 if (BEG + nchars < Z)
7664 del_range (BEG + nchars, Z);
7665 if (Z == BEG)
7666 echo_area_buffer[0] = Qnil;
7667 return 0;
7668 }
7669
7670
7671 /* Set the current message to a substring of S or STRING.
7672
7673 If STRING is a Lisp string, set the message to the first NBYTES
7674 bytes from STRING. NBYTES zero means use the whole string. If
7675 STRING is multibyte, the message will be displayed multibyte.
7676
7677 If S is not null, set the message to the first LEN bytes of S. LEN
7678 zero means use the whole string. MULTIBYTE_P non-zero means S is
7679 multibyte. Display the message multibyte in that case. */
7680
7681 void
7682 set_message (s, string, nbytes, multibyte_p)
7683 const char *s;
7684 Lisp_Object string;
7685 int nbytes, multibyte_p;
7686 {
7687 message_enable_multibyte
7688 = ((s && multibyte_p)
7689 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7690
7691 with_echo_area_buffer (0, -1, set_message_1,
7692 (EMACS_INT) s, string, nbytes, multibyte_p);
7693 message_buf_print = 0;
7694 help_echo_showing_p = 0;
7695 }
7696
7697
7698 /* Helper function for set_message. Arguments have the same meaning
7699 as there, with A1 corresponding to S and A2 corresponding to STRING
7700 This function is called with the echo area buffer being
7701 current. */
7702
7703 static int
7704 set_message_1 (a1, a2, nbytes, multibyte_p)
7705 EMACS_INT a1;
7706 Lisp_Object a2;
7707 EMACS_INT nbytes, multibyte_p;
7708 {
7709 const char *s = (const char *) a1;
7710 Lisp_Object string = a2;
7711
7712 xassert (BEG == Z);
7713
7714 /* Change multibyteness of the echo buffer appropriately. */
7715 if (message_enable_multibyte
7716 != !NILP (current_buffer->enable_multibyte_characters))
7717 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7718
7719 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7720
7721 /* Insert new message at BEG. */
7722 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7723
7724 if (STRINGP (string))
7725 {
7726 int nchars;
7727
7728 if (nbytes == 0)
7729 nbytes = SBYTES (string);
7730 nchars = string_byte_to_char (string, nbytes);
7731
7732 /* This function takes care of single/multibyte conversion. We
7733 just have to ensure that the echo area buffer has the right
7734 setting of enable_multibyte_characters. */
7735 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7736 }
7737 else if (s)
7738 {
7739 if (nbytes == 0)
7740 nbytes = strlen (s);
7741
7742 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7743 {
7744 /* Convert from multi-byte to single-byte. */
7745 int i, c, n;
7746 unsigned char work[1];
7747
7748 /* Convert a multibyte string to single-byte. */
7749 for (i = 0; i < nbytes; i += n)
7750 {
7751 c = string_char_and_length (s + i, nbytes - i, &n);
7752 work[0] = (SINGLE_BYTE_CHAR_P (c)
7753 ? c
7754 : multibyte_char_to_unibyte (c, Qnil));
7755 insert_1_both (work, 1, 1, 1, 0, 0);
7756 }
7757 }
7758 else if (!multibyte_p
7759 && !NILP (current_buffer->enable_multibyte_characters))
7760 {
7761 /* Convert from single-byte to multi-byte. */
7762 int i, c, n;
7763 const unsigned char *msg = (const unsigned char *) s;
7764 unsigned char str[MAX_MULTIBYTE_LENGTH];
7765
7766 /* Convert a single-byte string to multibyte. */
7767 for (i = 0; i < nbytes; i++)
7768 {
7769 c = unibyte_char_to_multibyte (msg[i]);
7770 n = CHAR_STRING (c, str);
7771 insert_1_both (str, 1, n, 1, 0, 0);
7772 }
7773 }
7774 else
7775 insert_1 (s, nbytes, 1, 0, 0);
7776 }
7777
7778 return 0;
7779 }
7780
7781
7782 /* Clear messages. CURRENT_P non-zero means clear the current
7783 message. LAST_DISPLAYED_P non-zero means clear the message
7784 last displayed. */
7785
7786 void
7787 clear_message (current_p, last_displayed_p)
7788 int current_p, last_displayed_p;
7789 {
7790 if (current_p)
7791 {
7792 echo_area_buffer[0] = Qnil;
7793 message_cleared_p = 1;
7794 }
7795
7796 if (last_displayed_p)
7797 echo_area_buffer[1] = Qnil;
7798
7799 message_buf_print = 0;
7800 }
7801
7802 /* Clear garbaged frames.
7803
7804 This function is used where the old redisplay called
7805 redraw_garbaged_frames which in turn called redraw_frame which in
7806 turn called clear_frame. The call to clear_frame was a source of
7807 flickering. I believe a clear_frame is not necessary. It should
7808 suffice in the new redisplay to invalidate all current matrices,
7809 and ensure a complete redisplay of all windows. */
7810
7811 static void
7812 clear_garbaged_frames ()
7813 {
7814 if (frame_garbaged)
7815 {
7816 Lisp_Object tail, frame;
7817 int changed_count = 0;
7818
7819 FOR_EACH_FRAME (tail, frame)
7820 {
7821 struct frame *f = XFRAME (frame);
7822
7823 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7824 {
7825 if (f->resized_p)
7826 {
7827 Fredraw_frame (frame);
7828 f->force_flush_display_p = 1;
7829 }
7830 clear_current_matrices (f);
7831 changed_count++;
7832 f->garbaged = 0;
7833 f->resized_p = 0;
7834 }
7835 }
7836
7837 frame_garbaged = 0;
7838 if (changed_count)
7839 ++windows_or_buffers_changed;
7840 }
7841 }
7842
7843
7844 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7845 is non-zero update selected_frame. Value is non-zero if the
7846 mini-windows height has been changed. */
7847
7848 static int
7849 echo_area_display (update_frame_p)
7850 int update_frame_p;
7851 {
7852 Lisp_Object mini_window;
7853 struct window *w;
7854 struct frame *f;
7855 int window_height_changed_p = 0;
7856 struct frame *sf = SELECTED_FRAME ();
7857
7858 mini_window = FRAME_MINIBUF_WINDOW (sf);
7859 w = XWINDOW (mini_window);
7860 f = XFRAME (WINDOW_FRAME (w));
7861
7862 /* Don't display if frame is invisible or not yet initialized. */
7863 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7864 return 0;
7865
7866 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7867 #ifndef MAC_OS8
7868 #ifdef HAVE_WINDOW_SYSTEM
7869 /* When Emacs starts, selected_frame may be a visible terminal
7870 frame, even if we run under a window system. If we let this
7871 through, a message would be displayed on the terminal. */
7872 if (EQ (selected_frame, Vterminal_frame)
7873 && !NILP (Vwindow_system))
7874 return 0;
7875 #endif /* HAVE_WINDOW_SYSTEM */
7876 #endif
7877
7878 /* Redraw garbaged frames. */
7879 if (frame_garbaged)
7880 clear_garbaged_frames ();
7881
7882 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7883 {
7884 echo_area_window = mini_window;
7885 window_height_changed_p = display_echo_area (w);
7886 w->must_be_updated_p = 1;
7887
7888 /* Update the display, unless called from redisplay_internal.
7889 Also don't update the screen during redisplay itself. The
7890 update will happen at the end of redisplay, and an update
7891 here could cause confusion. */
7892 if (update_frame_p && !redisplaying_p)
7893 {
7894 int n = 0;
7895
7896 /* If the display update has been interrupted by pending
7897 input, update mode lines in the frame. Due to the
7898 pending input, it might have been that redisplay hasn't
7899 been called, so that mode lines above the echo area are
7900 garbaged. This looks odd, so we prevent it here. */
7901 if (!display_completed)
7902 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7903
7904 if (window_height_changed_p
7905 /* Don't do this if Emacs is shutting down. Redisplay
7906 needs to run hooks. */
7907 && !NILP (Vrun_hooks))
7908 {
7909 /* Must update other windows. Likewise as in other
7910 cases, don't let this update be interrupted by
7911 pending input. */
7912 int count = SPECPDL_INDEX ();
7913 specbind (Qredisplay_dont_pause, Qt);
7914 windows_or_buffers_changed = 1;
7915 redisplay_internal (0);
7916 unbind_to (count, Qnil);
7917 }
7918 else if (FRAME_WINDOW_P (f) && n == 0)
7919 {
7920 /* Window configuration is the same as before.
7921 Can do with a display update of the echo area,
7922 unless we displayed some mode lines. */
7923 update_single_window (w, 1);
7924 rif->flush_display (f);
7925 }
7926 else
7927 update_frame (f, 1, 1);
7928
7929 /* If cursor is in the echo area, make sure that the next
7930 redisplay displays the minibuffer, so that the cursor will
7931 be replaced with what the minibuffer wants. */
7932 if (cursor_in_echo_area)
7933 ++windows_or_buffers_changed;
7934 }
7935 }
7936 else if (!EQ (mini_window, selected_window))
7937 windows_or_buffers_changed++;
7938
7939 /* Last displayed message is now the current message. */
7940 echo_area_buffer[1] = echo_area_buffer[0];
7941
7942 /* Prevent redisplay optimization in redisplay_internal by resetting
7943 this_line_start_pos. This is done because the mini-buffer now
7944 displays the message instead of its buffer text. */
7945 if (EQ (mini_window, selected_window))
7946 CHARPOS (this_line_start_pos) = 0;
7947
7948 return window_height_changed_p;
7949 }
7950
7951
7952 \f
7953 /***********************************************************************
7954 Frame Titles
7955 ***********************************************************************/
7956
7957
7958 /* The frame title buffering code is also used by Fformat_mode_line.
7959 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7960
7961 /* A buffer for constructing frame titles in it; allocated from the
7962 heap in init_xdisp and resized as needed in store_frame_title_char. */
7963
7964 static char *frame_title_buf;
7965
7966 /* The buffer's end, and a current output position in it. */
7967
7968 static char *frame_title_buf_end;
7969 static char *frame_title_ptr;
7970
7971
7972 /* Store a single character C for the frame title in frame_title_buf.
7973 Re-allocate frame_title_buf if necessary. */
7974
7975 static void
7976 #ifdef PROTOTYPES
7977 store_frame_title_char (char c)
7978 #else
7979 store_frame_title_char (c)
7980 char c;
7981 #endif
7982 {
7983 /* If output position has reached the end of the allocated buffer,
7984 double the buffer's size. */
7985 if (frame_title_ptr == frame_title_buf_end)
7986 {
7987 int len = frame_title_ptr - frame_title_buf;
7988 int new_size = 2 * len * sizeof *frame_title_buf;
7989 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7990 frame_title_buf_end = frame_title_buf + new_size;
7991 frame_title_ptr = frame_title_buf + len;
7992 }
7993
7994 *frame_title_ptr++ = c;
7995 }
7996
7997
7998 /* Store part of a frame title in frame_title_buf, beginning at
7999 frame_title_ptr. STR is the string to store. Do not copy
8000 characters that yield more columns than PRECISION; PRECISION <= 0
8001 means copy the whole string. Pad with spaces until FIELD_WIDTH
8002 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8003 pad. Called from display_mode_element when it is used to build a
8004 frame title. */
8005
8006 static int
8007 store_frame_title (str, field_width, precision)
8008 const unsigned char *str;
8009 int field_width, precision;
8010 {
8011 int n = 0;
8012 int dummy, nbytes;
8013
8014 /* Copy at most PRECISION chars from STR. */
8015 nbytes = strlen (str);
8016 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8017 while (nbytes--)
8018 store_frame_title_char (*str++);
8019
8020 /* Fill up with spaces until FIELD_WIDTH reached. */
8021 while (field_width > 0
8022 && n < field_width)
8023 {
8024 store_frame_title_char (' ');
8025 ++n;
8026 }
8027
8028 return n;
8029 }
8030
8031 #ifdef HAVE_WINDOW_SYSTEM
8032
8033 /* Set the title of FRAME, if it has changed. The title format is
8034 Vicon_title_format if FRAME is iconified, otherwise it is
8035 frame_title_format. */
8036
8037 static void
8038 x_consider_frame_title (frame)
8039 Lisp_Object frame;
8040 {
8041 struct frame *f = XFRAME (frame);
8042
8043 if (FRAME_WINDOW_P (f)
8044 || FRAME_MINIBUF_ONLY_P (f)
8045 || f->explicit_name)
8046 {
8047 /* Do we have more than one visible frame on this X display? */
8048 Lisp_Object tail;
8049 Lisp_Object fmt;
8050 struct buffer *obuf;
8051 int len;
8052 struct it it;
8053
8054 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8055 {
8056 Lisp_Object other_frame = XCAR (tail);
8057 struct frame *tf = XFRAME (other_frame);
8058
8059 if (tf != f
8060 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8061 && !FRAME_MINIBUF_ONLY_P (tf)
8062 && !EQ (other_frame, tip_frame)
8063 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8064 break;
8065 }
8066
8067 /* Set global variable indicating that multiple frames exist. */
8068 multiple_frames = CONSP (tail);
8069
8070 /* Switch to the buffer of selected window of the frame. Set up
8071 frame_title_ptr so that display_mode_element will output into it;
8072 then display the title. */
8073 obuf = current_buffer;
8074 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8075 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8076 frame_title_ptr = frame_title_buf;
8077 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8078 NULL, DEFAULT_FACE_ID);
8079 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8080 len = frame_title_ptr - frame_title_buf;
8081 frame_title_ptr = NULL;
8082 set_buffer_internal_1 (obuf);
8083
8084 /* Set the title only if it's changed. This avoids consing in
8085 the common case where it hasn't. (If it turns out that we've
8086 already wasted too much time by walking through the list with
8087 display_mode_element, then we might need to optimize at a
8088 higher level than this.) */
8089 if (! STRINGP (f->name)
8090 || SBYTES (f->name) != len
8091 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8092 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8093 }
8094 }
8095
8096 #endif /* not HAVE_WINDOW_SYSTEM */
8097
8098
8099
8100 \f
8101 /***********************************************************************
8102 Menu Bars
8103 ***********************************************************************/
8104
8105
8106 /* Prepare for redisplay by updating menu-bar item lists when
8107 appropriate. This can call eval. */
8108
8109 void
8110 prepare_menu_bars ()
8111 {
8112 int all_windows;
8113 struct gcpro gcpro1, gcpro2;
8114 struct frame *f;
8115 Lisp_Object tooltip_frame;
8116
8117 #ifdef HAVE_WINDOW_SYSTEM
8118 tooltip_frame = tip_frame;
8119 #else
8120 tooltip_frame = Qnil;
8121 #endif
8122
8123 /* Update all frame titles based on their buffer names, etc. We do
8124 this before the menu bars so that the buffer-menu will show the
8125 up-to-date frame titles. */
8126 #ifdef HAVE_WINDOW_SYSTEM
8127 if (windows_or_buffers_changed || update_mode_lines)
8128 {
8129 Lisp_Object tail, frame;
8130
8131 FOR_EACH_FRAME (tail, frame)
8132 {
8133 f = XFRAME (frame);
8134 if (!EQ (frame, tooltip_frame)
8135 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8136 x_consider_frame_title (frame);
8137 }
8138 }
8139 #endif /* HAVE_WINDOW_SYSTEM */
8140
8141 /* Update the menu bar item lists, if appropriate. This has to be
8142 done before any actual redisplay or generation of display lines. */
8143 all_windows = (update_mode_lines
8144 || buffer_shared > 1
8145 || windows_or_buffers_changed);
8146 if (all_windows)
8147 {
8148 Lisp_Object tail, frame;
8149 int count = SPECPDL_INDEX ();
8150
8151 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8152
8153 FOR_EACH_FRAME (tail, frame)
8154 {
8155 f = XFRAME (frame);
8156
8157 /* Ignore tooltip frame. */
8158 if (EQ (frame, tooltip_frame))
8159 continue;
8160
8161 /* If a window on this frame changed size, report that to
8162 the user and clear the size-change flag. */
8163 if (FRAME_WINDOW_SIZES_CHANGED (f))
8164 {
8165 Lisp_Object functions;
8166
8167 /* Clear flag first in case we get an error below. */
8168 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8169 functions = Vwindow_size_change_functions;
8170 GCPRO2 (tail, functions);
8171
8172 while (CONSP (functions))
8173 {
8174 call1 (XCAR (functions), frame);
8175 functions = XCDR (functions);
8176 }
8177 UNGCPRO;
8178 }
8179
8180 GCPRO1 (tail);
8181 update_menu_bar (f, 0);
8182 #ifdef HAVE_WINDOW_SYSTEM
8183 update_tool_bar (f, 0);
8184 #endif
8185 UNGCPRO;
8186 }
8187
8188 unbind_to (count, Qnil);
8189 }
8190 else
8191 {
8192 struct frame *sf = SELECTED_FRAME ();
8193 update_menu_bar (sf, 1);
8194 #ifdef HAVE_WINDOW_SYSTEM
8195 update_tool_bar (sf, 1);
8196 #endif
8197 }
8198
8199 /* Motif needs this. See comment in xmenu.c. Turn it off when
8200 pending_menu_activation is not defined. */
8201 #ifdef USE_X_TOOLKIT
8202 pending_menu_activation = 0;
8203 #endif
8204 }
8205
8206
8207 /* Update the menu bar item list for frame F. This has to be done
8208 before we start to fill in any display lines, because it can call
8209 eval.
8210
8211 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8212
8213 static void
8214 update_menu_bar (f, save_match_data)
8215 struct frame *f;
8216 int save_match_data;
8217 {
8218 Lisp_Object window;
8219 register struct window *w;
8220
8221 /* If called recursively during a menu update, do nothing. This can
8222 happen when, for instance, an activate-menubar-hook causes a
8223 redisplay. */
8224 if (inhibit_menubar_update)
8225 return;
8226
8227 window = FRAME_SELECTED_WINDOW (f);
8228 w = XWINDOW (window);
8229
8230 #if 0 /* The if statement below this if statement used to include the
8231 condition !NILP (w->update_mode_line), rather than using
8232 update_mode_lines directly, and this if statement may have
8233 been added to make that condition work. Now the if
8234 statement below matches its comment, this isn't needed. */
8235 if (update_mode_lines)
8236 w->update_mode_line = Qt;
8237 #endif
8238
8239 if (FRAME_WINDOW_P (f)
8240 ?
8241 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8242 || defined (USE_GTK)
8243 FRAME_EXTERNAL_MENU_BAR (f)
8244 #else
8245 FRAME_MENU_BAR_LINES (f) > 0
8246 #endif
8247 : FRAME_MENU_BAR_LINES (f) > 0)
8248 {
8249 /* If the user has switched buffers or windows, we need to
8250 recompute to reflect the new bindings. But we'll
8251 recompute when update_mode_lines is set too; that means
8252 that people can use force-mode-line-update to request
8253 that the menu bar be recomputed. The adverse effect on
8254 the rest of the redisplay algorithm is about the same as
8255 windows_or_buffers_changed anyway. */
8256 if (windows_or_buffers_changed
8257 /* This used to test w->update_mode_line, but we believe
8258 there is no need to recompute the menu in that case. */
8259 || update_mode_lines
8260 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8261 < BUF_MODIFF (XBUFFER (w->buffer)))
8262 != !NILP (w->last_had_star))
8263 || ((!NILP (Vtransient_mark_mode)
8264 && !NILP (XBUFFER (w->buffer)->mark_active))
8265 != !NILP (w->region_showing)))
8266 {
8267 struct buffer *prev = current_buffer;
8268 int count = SPECPDL_INDEX ();
8269
8270 specbind (Qinhibit_menubar_update, Qt);
8271
8272 set_buffer_internal_1 (XBUFFER (w->buffer));
8273 if (save_match_data)
8274 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8275 if (NILP (Voverriding_local_map_menu_flag))
8276 {
8277 specbind (Qoverriding_terminal_local_map, Qnil);
8278 specbind (Qoverriding_local_map, Qnil);
8279 }
8280
8281 /* Run the Lucid hook. */
8282 safe_run_hooks (Qactivate_menubar_hook);
8283
8284 /* If it has changed current-menubar from previous value,
8285 really recompute the menu-bar from the value. */
8286 if (! NILP (Vlucid_menu_bar_dirty_flag))
8287 call0 (Qrecompute_lucid_menubar);
8288
8289 safe_run_hooks (Qmenu_bar_update_hook);
8290 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8291
8292 /* Redisplay the menu bar in case we changed it. */
8293 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8294 || defined (USE_GTK)
8295 if (FRAME_WINDOW_P (f)
8296 #if defined (MAC_OS)
8297 /* All frames on Mac OS share the same menubar. So only the
8298 selected frame should be allowed to set it. */
8299 && f == SELECTED_FRAME ()
8300 #endif
8301 )
8302 set_frame_menubar (f, 0, 0);
8303 else
8304 /* On a terminal screen, the menu bar is an ordinary screen
8305 line, and this makes it get updated. */
8306 w->update_mode_line = Qt;
8307 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8308 /* In the non-toolkit version, the menu bar is an ordinary screen
8309 line, and this makes it get updated. */
8310 w->update_mode_line = Qt;
8311 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8312
8313 unbind_to (count, Qnil);
8314 set_buffer_internal_1 (prev);
8315 }
8316 }
8317 }
8318
8319
8320 \f
8321 /***********************************************************************
8322 Output Cursor
8323 ***********************************************************************/
8324
8325 #ifdef HAVE_WINDOW_SYSTEM
8326
8327 /* EXPORT:
8328 Nominal cursor position -- where to draw output.
8329 HPOS and VPOS are window relative glyph matrix coordinates.
8330 X and Y are window relative pixel coordinates. */
8331
8332 struct cursor_pos output_cursor;
8333
8334
8335 /* EXPORT:
8336 Set the global variable output_cursor to CURSOR. All cursor
8337 positions are relative to updated_window. */
8338
8339 void
8340 set_output_cursor (cursor)
8341 struct cursor_pos *cursor;
8342 {
8343 output_cursor.hpos = cursor->hpos;
8344 output_cursor.vpos = cursor->vpos;
8345 output_cursor.x = cursor->x;
8346 output_cursor.y = cursor->y;
8347 }
8348
8349
8350 /* EXPORT for RIF:
8351 Set a nominal cursor position.
8352
8353 HPOS and VPOS are column/row positions in a window glyph matrix. X
8354 and Y are window text area relative pixel positions.
8355
8356 If this is done during an update, updated_window will contain the
8357 window that is being updated and the position is the future output
8358 cursor position for that window. If updated_window is null, use
8359 selected_window and display the cursor at the given position. */
8360
8361 void
8362 x_cursor_to (vpos, hpos, y, x)
8363 int vpos, hpos, y, x;
8364 {
8365 struct window *w;
8366
8367 /* If updated_window is not set, work on selected_window. */
8368 if (updated_window)
8369 w = updated_window;
8370 else
8371 w = XWINDOW (selected_window);
8372
8373 /* Set the output cursor. */
8374 output_cursor.hpos = hpos;
8375 output_cursor.vpos = vpos;
8376 output_cursor.x = x;
8377 output_cursor.y = y;
8378
8379 /* If not called as part of an update, really display the cursor.
8380 This will also set the cursor position of W. */
8381 if (updated_window == NULL)
8382 {
8383 BLOCK_INPUT;
8384 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8385 if (rif->flush_display_optional)
8386 rif->flush_display_optional (SELECTED_FRAME ());
8387 UNBLOCK_INPUT;
8388 }
8389 }
8390
8391 #endif /* HAVE_WINDOW_SYSTEM */
8392
8393 \f
8394 /***********************************************************************
8395 Tool-bars
8396 ***********************************************************************/
8397
8398 #ifdef HAVE_WINDOW_SYSTEM
8399
8400 /* Where the mouse was last time we reported a mouse event. */
8401
8402 FRAME_PTR last_mouse_frame;
8403
8404 /* Tool-bar item index of the item on which a mouse button was pressed
8405 or -1. */
8406
8407 int last_tool_bar_item;
8408
8409
8410 /* Update the tool-bar item list for frame F. This has to be done
8411 before we start to fill in any display lines. Called from
8412 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8413 and restore it here. */
8414
8415 static void
8416 update_tool_bar (f, save_match_data)
8417 struct frame *f;
8418 int save_match_data;
8419 {
8420 #ifdef USE_GTK
8421 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8422 #else
8423 int do_update = WINDOWP (f->tool_bar_window)
8424 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8425 #endif
8426
8427 if (do_update)
8428 {
8429 Lisp_Object window;
8430 struct window *w;
8431
8432 window = FRAME_SELECTED_WINDOW (f);
8433 w = XWINDOW (window);
8434
8435 /* If the user has switched buffers or windows, we need to
8436 recompute to reflect the new bindings. But we'll
8437 recompute when update_mode_lines is set too; that means
8438 that people can use force-mode-line-update to request
8439 that the menu bar be recomputed. The adverse effect on
8440 the rest of the redisplay algorithm is about the same as
8441 windows_or_buffers_changed anyway. */
8442 if (windows_or_buffers_changed
8443 || !NILP (w->update_mode_line)
8444 || update_mode_lines
8445 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8446 < BUF_MODIFF (XBUFFER (w->buffer)))
8447 != !NILP (w->last_had_star))
8448 || ((!NILP (Vtransient_mark_mode)
8449 && !NILP (XBUFFER (w->buffer)->mark_active))
8450 != !NILP (w->region_showing)))
8451 {
8452 struct buffer *prev = current_buffer;
8453 int count = SPECPDL_INDEX ();
8454 Lisp_Object new_tool_bar;
8455 int new_n_tool_bar;
8456 struct gcpro gcpro1;
8457
8458 /* Set current_buffer to the buffer of the selected
8459 window of the frame, so that we get the right local
8460 keymaps. */
8461 set_buffer_internal_1 (XBUFFER (w->buffer));
8462
8463 /* Save match data, if we must. */
8464 if (save_match_data)
8465 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8466
8467 /* Make sure that we don't accidentally use bogus keymaps. */
8468 if (NILP (Voverriding_local_map_menu_flag))
8469 {
8470 specbind (Qoverriding_terminal_local_map, Qnil);
8471 specbind (Qoverriding_local_map, Qnil);
8472 }
8473
8474 GCPRO1 (new_tool_bar);
8475
8476 /* Build desired tool-bar items from keymaps. */
8477 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8478 &new_n_tool_bar);
8479
8480 /* Redisplay the tool-bar if we changed it. */
8481 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8482 {
8483 /* Redisplay that happens asynchronously due to an expose event
8484 may access f->tool_bar_items. Make sure we update both
8485 variables within BLOCK_INPUT so no such event interrupts. */
8486 BLOCK_INPUT;
8487 f->tool_bar_items = new_tool_bar;
8488 f->n_tool_bar_items = new_n_tool_bar;
8489 w->update_mode_line = Qt;
8490 UNBLOCK_INPUT;
8491 }
8492
8493 UNGCPRO;
8494
8495 unbind_to (count, Qnil);
8496 set_buffer_internal_1 (prev);
8497 }
8498 }
8499 }
8500
8501
8502 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8503 F's desired tool-bar contents. F->tool_bar_items must have
8504 been set up previously by calling prepare_menu_bars. */
8505
8506 static void
8507 build_desired_tool_bar_string (f)
8508 struct frame *f;
8509 {
8510 int i, size, size_needed;
8511 struct gcpro gcpro1, gcpro2, gcpro3;
8512 Lisp_Object image, plist, props;
8513
8514 image = plist = props = Qnil;
8515 GCPRO3 (image, plist, props);
8516
8517 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8518 Otherwise, make a new string. */
8519
8520 /* The size of the string we might be able to reuse. */
8521 size = (STRINGP (f->desired_tool_bar_string)
8522 ? SCHARS (f->desired_tool_bar_string)
8523 : 0);
8524
8525 /* We need one space in the string for each image. */
8526 size_needed = f->n_tool_bar_items;
8527
8528 /* Reuse f->desired_tool_bar_string, if possible. */
8529 if (size < size_needed || NILP (f->desired_tool_bar_string))
8530 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8531 make_number (' '));
8532 else
8533 {
8534 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8535 Fremove_text_properties (make_number (0), make_number (size),
8536 props, f->desired_tool_bar_string);
8537 }
8538
8539 /* Put a `display' property on the string for the images to display,
8540 put a `menu_item' property on tool-bar items with a value that
8541 is the index of the item in F's tool-bar item vector. */
8542 for (i = 0; i < f->n_tool_bar_items; ++i)
8543 {
8544 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8545
8546 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8547 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8548 int hmargin, vmargin, relief, idx, end;
8549 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8550
8551 /* If image is a vector, choose the image according to the
8552 button state. */
8553 image = PROP (TOOL_BAR_ITEM_IMAGES);
8554 if (VECTORP (image))
8555 {
8556 if (enabled_p)
8557 idx = (selected_p
8558 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8559 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8560 else
8561 idx = (selected_p
8562 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8563 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8564
8565 xassert (ASIZE (image) >= idx);
8566 image = AREF (image, idx);
8567 }
8568 else
8569 idx = -1;
8570
8571 /* Ignore invalid image specifications. */
8572 if (!valid_image_p (image))
8573 continue;
8574
8575 /* Display the tool-bar button pressed, or depressed. */
8576 plist = Fcopy_sequence (XCDR (image));
8577
8578 /* Compute margin and relief to draw. */
8579 relief = (tool_bar_button_relief >= 0
8580 ? tool_bar_button_relief
8581 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8582 hmargin = vmargin = relief;
8583
8584 if (INTEGERP (Vtool_bar_button_margin)
8585 && XINT (Vtool_bar_button_margin) > 0)
8586 {
8587 hmargin += XFASTINT (Vtool_bar_button_margin);
8588 vmargin += XFASTINT (Vtool_bar_button_margin);
8589 }
8590 else if (CONSP (Vtool_bar_button_margin))
8591 {
8592 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8593 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8594 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8595
8596 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8597 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8598 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8599 }
8600
8601 if (auto_raise_tool_bar_buttons_p)
8602 {
8603 /* Add a `:relief' property to the image spec if the item is
8604 selected. */
8605 if (selected_p)
8606 {
8607 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8608 hmargin -= relief;
8609 vmargin -= relief;
8610 }
8611 }
8612 else
8613 {
8614 /* If image is selected, display it pressed, i.e. with a
8615 negative relief. If it's not selected, display it with a
8616 raised relief. */
8617 plist = Fplist_put (plist, QCrelief,
8618 (selected_p
8619 ? make_number (-relief)
8620 : make_number (relief)));
8621 hmargin -= relief;
8622 vmargin -= relief;
8623 }
8624
8625 /* Put a margin around the image. */
8626 if (hmargin || vmargin)
8627 {
8628 if (hmargin == vmargin)
8629 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8630 else
8631 plist = Fplist_put (plist, QCmargin,
8632 Fcons (make_number (hmargin),
8633 make_number (vmargin)));
8634 }
8635
8636 /* If button is not enabled, and we don't have special images
8637 for the disabled state, make the image appear disabled by
8638 applying an appropriate algorithm to it. */
8639 if (!enabled_p && idx < 0)
8640 plist = Fplist_put (plist, QCconversion, Qdisabled);
8641
8642 /* Put a `display' text property on the string for the image to
8643 display. Put a `menu-item' property on the string that gives
8644 the start of this item's properties in the tool-bar items
8645 vector. */
8646 image = Fcons (Qimage, plist);
8647 props = list4 (Qdisplay, image,
8648 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8649
8650 /* Let the last image hide all remaining spaces in the tool bar
8651 string. The string can be longer than needed when we reuse a
8652 previous string. */
8653 if (i + 1 == f->n_tool_bar_items)
8654 end = SCHARS (f->desired_tool_bar_string);
8655 else
8656 end = i + 1;
8657 Fadd_text_properties (make_number (i), make_number (end),
8658 props, f->desired_tool_bar_string);
8659 #undef PROP
8660 }
8661
8662 UNGCPRO;
8663 }
8664
8665
8666 /* Display one line of the tool-bar of frame IT->f. */
8667
8668 static void
8669 display_tool_bar_line (it)
8670 struct it *it;
8671 {
8672 struct glyph_row *row = it->glyph_row;
8673 int max_x = it->last_visible_x;
8674 struct glyph *last;
8675
8676 prepare_desired_row (row);
8677 row->y = it->current_y;
8678
8679 /* Note that this isn't made use of if the face hasn't a box,
8680 so there's no need to check the face here. */
8681 it->start_of_box_run_p = 1;
8682
8683 while (it->current_x < max_x)
8684 {
8685 int x_before, x, n_glyphs_before, i, nglyphs;
8686
8687 /* Get the next display element. */
8688 if (!get_next_display_element (it))
8689 break;
8690
8691 /* Produce glyphs. */
8692 x_before = it->current_x;
8693 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8694 PRODUCE_GLYPHS (it);
8695
8696 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8697 i = 0;
8698 x = x_before;
8699 while (i < nglyphs)
8700 {
8701 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8702
8703 if (x + glyph->pixel_width > max_x)
8704 {
8705 /* Glyph doesn't fit on line. */
8706 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8707 it->current_x = x;
8708 goto out;
8709 }
8710
8711 ++it->hpos;
8712 x += glyph->pixel_width;
8713 ++i;
8714 }
8715
8716 /* Stop at line ends. */
8717 if (ITERATOR_AT_END_OF_LINE_P (it))
8718 break;
8719
8720 set_iterator_to_next (it, 1);
8721 }
8722
8723 out:;
8724
8725 row->displays_text_p = row->used[TEXT_AREA] != 0;
8726 extend_face_to_end_of_line (it);
8727 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8728 last->right_box_line_p = 1;
8729 if (last == row->glyphs[TEXT_AREA])
8730 last->left_box_line_p = 1;
8731 compute_line_metrics (it);
8732
8733 /* If line is empty, make it occupy the rest of the tool-bar. */
8734 if (!row->displays_text_p)
8735 {
8736 row->height = row->phys_height = it->last_visible_y - row->y;
8737 row->ascent = row->phys_ascent = 0;
8738 row->extra_line_spacing = 0;
8739 }
8740
8741 row->full_width_p = 1;
8742 row->continued_p = 0;
8743 row->truncated_on_left_p = 0;
8744 row->truncated_on_right_p = 0;
8745
8746 it->current_x = it->hpos = 0;
8747 it->current_y += row->height;
8748 ++it->vpos;
8749 ++it->glyph_row;
8750 }
8751
8752
8753 /* Value is the number of screen lines needed to make all tool-bar
8754 items of frame F visible. */
8755
8756 static int
8757 tool_bar_lines_needed (f)
8758 struct frame *f;
8759 {
8760 struct window *w = XWINDOW (f->tool_bar_window);
8761 struct it it;
8762
8763 /* Initialize an iterator for iteration over
8764 F->desired_tool_bar_string in the tool-bar window of frame F. */
8765 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8766 it.first_visible_x = 0;
8767 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8768 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8769
8770 while (!ITERATOR_AT_END_P (&it))
8771 {
8772 it.glyph_row = w->desired_matrix->rows;
8773 clear_glyph_row (it.glyph_row);
8774 display_tool_bar_line (&it);
8775 }
8776
8777 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8778 }
8779
8780
8781 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8782 0, 1, 0,
8783 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8784 (frame)
8785 Lisp_Object frame;
8786 {
8787 struct frame *f;
8788 struct window *w;
8789 int nlines = 0;
8790
8791 if (NILP (frame))
8792 frame = selected_frame;
8793 else
8794 CHECK_FRAME (frame);
8795 f = XFRAME (frame);
8796
8797 if (WINDOWP (f->tool_bar_window)
8798 || (w = XWINDOW (f->tool_bar_window),
8799 WINDOW_TOTAL_LINES (w) > 0))
8800 {
8801 update_tool_bar (f, 1);
8802 if (f->n_tool_bar_items)
8803 {
8804 build_desired_tool_bar_string (f);
8805 nlines = tool_bar_lines_needed (f);
8806 }
8807 }
8808
8809 return make_number (nlines);
8810 }
8811
8812
8813 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8814 height should be changed. */
8815
8816 static int
8817 redisplay_tool_bar (f)
8818 struct frame *f;
8819 {
8820 struct window *w;
8821 struct it it;
8822 struct glyph_row *row;
8823 int change_height_p = 0;
8824
8825 #ifdef USE_GTK
8826 if (FRAME_EXTERNAL_TOOL_BAR (f))
8827 update_frame_tool_bar (f);
8828 return 0;
8829 #endif
8830
8831 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8832 do anything. This means you must start with tool-bar-lines
8833 non-zero to get the auto-sizing effect. Or in other words, you
8834 can turn off tool-bars by specifying tool-bar-lines zero. */
8835 if (!WINDOWP (f->tool_bar_window)
8836 || (w = XWINDOW (f->tool_bar_window),
8837 WINDOW_TOTAL_LINES (w) == 0))
8838 return 0;
8839
8840 /* Set up an iterator for the tool-bar window. */
8841 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8842 it.first_visible_x = 0;
8843 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8844 row = it.glyph_row;
8845
8846 /* Build a string that represents the contents of the tool-bar. */
8847 build_desired_tool_bar_string (f);
8848 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8849
8850 /* Display as many lines as needed to display all tool-bar items. */
8851 while (it.current_y < it.last_visible_y)
8852 display_tool_bar_line (&it);
8853
8854 /* It doesn't make much sense to try scrolling in the tool-bar
8855 window, so don't do it. */
8856 w->desired_matrix->no_scrolling_p = 1;
8857 w->must_be_updated_p = 1;
8858
8859 if (auto_resize_tool_bars_p)
8860 {
8861 int nlines;
8862
8863 /* If we couldn't display everything, change the tool-bar's
8864 height. */
8865 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8866 change_height_p = 1;
8867
8868 /* If there are blank lines at the end, except for a partially
8869 visible blank line at the end that is smaller than
8870 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8871 row = it.glyph_row - 1;
8872 if (!row->displays_text_p
8873 && row->height >= FRAME_LINE_HEIGHT (f))
8874 change_height_p = 1;
8875
8876 /* If row displays tool-bar items, but is partially visible,
8877 change the tool-bar's height. */
8878 if (row->displays_text_p
8879 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8880 change_height_p = 1;
8881
8882 /* Resize windows as needed by changing the `tool-bar-lines'
8883 frame parameter. */
8884 if (change_height_p
8885 && (nlines = tool_bar_lines_needed (f),
8886 nlines != WINDOW_TOTAL_LINES (w)))
8887 {
8888 extern Lisp_Object Qtool_bar_lines;
8889 Lisp_Object frame;
8890 int old_height = WINDOW_TOTAL_LINES (w);
8891
8892 XSETFRAME (frame, f);
8893 clear_glyph_matrix (w->desired_matrix);
8894 Fmodify_frame_parameters (frame,
8895 Fcons (Fcons (Qtool_bar_lines,
8896 make_number (nlines)),
8897 Qnil));
8898 if (WINDOW_TOTAL_LINES (w) != old_height)
8899 fonts_changed_p = 1;
8900 }
8901 }
8902
8903 return change_height_p;
8904 }
8905
8906
8907 /* Get information about the tool-bar item which is displayed in GLYPH
8908 on frame F. Return in *PROP_IDX the index where tool-bar item
8909 properties start in F->tool_bar_items. Value is zero if
8910 GLYPH doesn't display a tool-bar item. */
8911
8912 static int
8913 tool_bar_item_info (f, glyph, prop_idx)
8914 struct frame *f;
8915 struct glyph *glyph;
8916 int *prop_idx;
8917 {
8918 Lisp_Object prop;
8919 int success_p;
8920 int charpos;
8921
8922 /* This function can be called asynchronously, which means we must
8923 exclude any possibility that Fget_text_property signals an
8924 error. */
8925 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8926 charpos = max (0, charpos);
8927
8928 /* Get the text property `menu-item' at pos. The value of that
8929 property is the start index of this item's properties in
8930 F->tool_bar_items. */
8931 prop = Fget_text_property (make_number (charpos),
8932 Qmenu_item, f->current_tool_bar_string);
8933 if (INTEGERP (prop))
8934 {
8935 *prop_idx = XINT (prop);
8936 success_p = 1;
8937 }
8938 else
8939 success_p = 0;
8940
8941 return success_p;
8942 }
8943
8944 \f
8945 /* Get information about the tool-bar item at position X/Y on frame F.
8946 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8947 the current matrix of the tool-bar window of F, or NULL if not
8948 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8949 item in F->tool_bar_items. Value is
8950
8951 -1 if X/Y is not on a tool-bar item
8952 0 if X/Y is on the same item that was highlighted before.
8953 1 otherwise. */
8954
8955 static int
8956 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8957 struct frame *f;
8958 int x, y;
8959 struct glyph **glyph;
8960 int *hpos, *vpos, *prop_idx;
8961 {
8962 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8963 struct window *w = XWINDOW (f->tool_bar_window);
8964 int area;
8965
8966 /* Find the glyph under X/Y. */
8967 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8968 if (*glyph == NULL)
8969 return -1;
8970
8971 /* Get the start of this tool-bar item's properties in
8972 f->tool_bar_items. */
8973 if (!tool_bar_item_info (f, *glyph, prop_idx))
8974 return -1;
8975
8976 /* Is mouse on the highlighted item? */
8977 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8978 && *vpos >= dpyinfo->mouse_face_beg_row
8979 && *vpos <= dpyinfo->mouse_face_end_row
8980 && (*vpos > dpyinfo->mouse_face_beg_row
8981 || *hpos >= dpyinfo->mouse_face_beg_col)
8982 && (*vpos < dpyinfo->mouse_face_end_row
8983 || *hpos < dpyinfo->mouse_face_end_col
8984 || dpyinfo->mouse_face_past_end))
8985 return 0;
8986
8987 return 1;
8988 }
8989
8990
8991 /* EXPORT:
8992 Handle mouse button event on the tool-bar of frame F, at
8993 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8994 0 for button release. MODIFIERS is event modifiers for button
8995 release. */
8996
8997 void
8998 handle_tool_bar_click (f, x, y, down_p, modifiers)
8999 struct frame *f;
9000 int x, y, down_p;
9001 unsigned int modifiers;
9002 {
9003 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9004 struct window *w = XWINDOW (f->tool_bar_window);
9005 int hpos, vpos, prop_idx;
9006 struct glyph *glyph;
9007 Lisp_Object enabled_p;
9008
9009 /* If not on the highlighted tool-bar item, return. */
9010 frame_to_window_pixel_xy (w, &x, &y);
9011 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9012 return;
9013
9014 /* If item is disabled, do nothing. */
9015 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9016 if (NILP (enabled_p))
9017 return;
9018
9019 if (down_p)
9020 {
9021 /* Show item in pressed state. */
9022 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9023 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9024 last_tool_bar_item = prop_idx;
9025 }
9026 else
9027 {
9028 Lisp_Object key, frame;
9029 struct input_event event;
9030 EVENT_INIT (event);
9031
9032 /* Show item in released state. */
9033 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9034 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9035
9036 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9037
9038 XSETFRAME (frame, f);
9039 event.kind = TOOL_BAR_EVENT;
9040 event.frame_or_window = frame;
9041 event.arg = frame;
9042 kbd_buffer_store_event (&event);
9043
9044 event.kind = TOOL_BAR_EVENT;
9045 event.frame_or_window = frame;
9046 event.arg = key;
9047 event.modifiers = modifiers;
9048 kbd_buffer_store_event (&event);
9049 last_tool_bar_item = -1;
9050 }
9051 }
9052
9053
9054 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9055 tool-bar window-relative coordinates X/Y. Called from
9056 note_mouse_highlight. */
9057
9058 static void
9059 note_tool_bar_highlight (f, x, y)
9060 struct frame *f;
9061 int x, y;
9062 {
9063 Lisp_Object window = f->tool_bar_window;
9064 struct window *w = XWINDOW (window);
9065 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9066 int hpos, vpos;
9067 struct glyph *glyph;
9068 struct glyph_row *row;
9069 int i;
9070 Lisp_Object enabled_p;
9071 int prop_idx;
9072 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9073 int mouse_down_p, rc;
9074
9075 /* Function note_mouse_highlight is called with negative x(y
9076 values when mouse moves outside of the frame. */
9077 if (x <= 0 || y <= 0)
9078 {
9079 clear_mouse_face (dpyinfo);
9080 return;
9081 }
9082
9083 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9084 if (rc < 0)
9085 {
9086 /* Not on tool-bar item. */
9087 clear_mouse_face (dpyinfo);
9088 return;
9089 }
9090 else if (rc == 0)
9091 /* On same tool-bar item as before. */
9092 goto set_help_echo;
9093
9094 clear_mouse_face (dpyinfo);
9095
9096 /* Mouse is down, but on different tool-bar item? */
9097 mouse_down_p = (dpyinfo->grabbed
9098 && f == last_mouse_frame
9099 && FRAME_LIVE_P (f));
9100 if (mouse_down_p
9101 && last_tool_bar_item != prop_idx)
9102 return;
9103
9104 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9105 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9106
9107 /* If tool-bar item is not enabled, don't highlight it. */
9108 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9109 if (!NILP (enabled_p))
9110 {
9111 /* Compute the x-position of the glyph. In front and past the
9112 image is a space. We include this in the highlighted area. */
9113 row = MATRIX_ROW (w->current_matrix, vpos);
9114 for (i = x = 0; i < hpos; ++i)
9115 x += row->glyphs[TEXT_AREA][i].pixel_width;
9116
9117 /* Record this as the current active region. */
9118 dpyinfo->mouse_face_beg_col = hpos;
9119 dpyinfo->mouse_face_beg_row = vpos;
9120 dpyinfo->mouse_face_beg_x = x;
9121 dpyinfo->mouse_face_beg_y = row->y;
9122 dpyinfo->mouse_face_past_end = 0;
9123
9124 dpyinfo->mouse_face_end_col = hpos + 1;
9125 dpyinfo->mouse_face_end_row = vpos;
9126 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9127 dpyinfo->mouse_face_end_y = row->y;
9128 dpyinfo->mouse_face_window = window;
9129 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9130
9131 /* Display it as active. */
9132 show_mouse_face (dpyinfo, draw);
9133 dpyinfo->mouse_face_image_state = draw;
9134 }
9135
9136 set_help_echo:
9137
9138 /* Set help_echo_string to a help string to display for this tool-bar item.
9139 XTread_socket does the rest. */
9140 help_echo_object = help_echo_window = Qnil;
9141 help_echo_pos = -1;
9142 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9143 if (NILP (help_echo_string))
9144 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9145 }
9146
9147 #endif /* HAVE_WINDOW_SYSTEM */
9148
9149
9150 \f
9151 /************************************************************************
9152 Horizontal scrolling
9153 ************************************************************************/
9154
9155 static int hscroll_window_tree P_ ((Lisp_Object));
9156 static int hscroll_windows P_ ((Lisp_Object));
9157
9158 /* For all leaf windows in the window tree rooted at WINDOW, set their
9159 hscroll value so that PT is (i) visible in the window, and (ii) so
9160 that it is not within a certain margin at the window's left and
9161 right border. Value is non-zero if any window's hscroll has been
9162 changed. */
9163
9164 static int
9165 hscroll_window_tree (window)
9166 Lisp_Object window;
9167 {
9168 int hscrolled_p = 0;
9169 int hscroll_relative_p = FLOATP (Vhscroll_step);
9170 int hscroll_step_abs = 0;
9171 double hscroll_step_rel = 0;
9172
9173 if (hscroll_relative_p)
9174 {
9175 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9176 if (hscroll_step_rel < 0)
9177 {
9178 hscroll_relative_p = 0;
9179 hscroll_step_abs = 0;
9180 }
9181 }
9182 else if (INTEGERP (Vhscroll_step))
9183 {
9184 hscroll_step_abs = XINT (Vhscroll_step);
9185 if (hscroll_step_abs < 0)
9186 hscroll_step_abs = 0;
9187 }
9188 else
9189 hscroll_step_abs = 0;
9190
9191 while (WINDOWP (window))
9192 {
9193 struct window *w = XWINDOW (window);
9194
9195 if (WINDOWP (w->hchild))
9196 hscrolled_p |= hscroll_window_tree (w->hchild);
9197 else if (WINDOWP (w->vchild))
9198 hscrolled_p |= hscroll_window_tree (w->vchild);
9199 else if (w->cursor.vpos >= 0)
9200 {
9201 int h_margin;
9202 int text_area_width;
9203 struct glyph_row *current_cursor_row
9204 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9205 struct glyph_row *desired_cursor_row
9206 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9207 struct glyph_row *cursor_row
9208 = (desired_cursor_row->enabled_p
9209 ? desired_cursor_row
9210 : current_cursor_row);
9211
9212 text_area_width = window_box_width (w, TEXT_AREA);
9213
9214 /* Scroll when cursor is inside this scroll margin. */
9215 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9216
9217 if ((XFASTINT (w->hscroll)
9218 && w->cursor.x <= h_margin)
9219 || (cursor_row->enabled_p
9220 && cursor_row->truncated_on_right_p
9221 && (w->cursor.x >= text_area_width - h_margin)))
9222 {
9223 struct it it;
9224 int hscroll;
9225 struct buffer *saved_current_buffer;
9226 int pt;
9227 int wanted_x;
9228
9229 /* Find point in a display of infinite width. */
9230 saved_current_buffer = current_buffer;
9231 current_buffer = XBUFFER (w->buffer);
9232
9233 if (w == XWINDOW (selected_window))
9234 pt = BUF_PT (current_buffer);
9235 else
9236 {
9237 pt = marker_position (w->pointm);
9238 pt = max (BEGV, pt);
9239 pt = min (ZV, pt);
9240 }
9241
9242 /* Move iterator to pt starting at cursor_row->start in
9243 a line with infinite width. */
9244 init_to_row_start (&it, w, cursor_row);
9245 it.last_visible_x = INFINITY;
9246 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9247 current_buffer = saved_current_buffer;
9248
9249 /* Position cursor in window. */
9250 if (!hscroll_relative_p && hscroll_step_abs == 0)
9251 hscroll = max (0, (it.current_x
9252 - (ITERATOR_AT_END_OF_LINE_P (&it)
9253 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9254 : (text_area_width / 2))))
9255 / FRAME_COLUMN_WIDTH (it.f);
9256 else if (w->cursor.x >= text_area_width - h_margin)
9257 {
9258 if (hscroll_relative_p)
9259 wanted_x = text_area_width * (1 - hscroll_step_rel)
9260 - h_margin;
9261 else
9262 wanted_x = text_area_width
9263 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9264 - h_margin;
9265 hscroll
9266 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9267 }
9268 else
9269 {
9270 if (hscroll_relative_p)
9271 wanted_x = text_area_width * hscroll_step_rel
9272 + h_margin;
9273 else
9274 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9275 + h_margin;
9276 hscroll
9277 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9278 }
9279 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9280
9281 /* Don't call Fset_window_hscroll if value hasn't
9282 changed because it will prevent redisplay
9283 optimizations. */
9284 if (XFASTINT (w->hscroll) != hscroll)
9285 {
9286 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9287 w->hscroll = make_number (hscroll);
9288 hscrolled_p = 1;
9289 }
9290 }
9291 }
9292
9293 window = w->next;
9294 }
9295
9296 /* Value is non-zero if hscroll of any leaf window has been changed. */
9297 return hscrolled_p;
9298 }
9299
9300
9301 /* Set hscroll so that cursor is visible and not inside horizontal
9302 scroll margins for all windows in the tree rooted at WINDOW. See
9303 also hscroll_window_tree above. Value is non-zero if any window's
9304 hscroll has been changed. If it has, desired matrices on the frame
9305 of WINDOW are cleared. */
9306
9307 static int
9308 hscroll_windows (window)
9309 Lisp_Object window;
9310 {
9311 int hscrolled_p;
9312
9313 if (automatic_hscrolling_p)
9314 {
9315 hscrolled_p = hscroll_window_tree (window);
9316 if (hscrolled_p)
9317 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9318 }
9319 else
9320 hscrolled_p = 0;
9321 return hscrolled_p;
9322 }
9323
9324
9325 \f
9326 /************************************************************************
9327 Redisplay
9328 ************************************************************************/
9329
9330 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9331 to a non-zero value. This is sometimes handy to have in a debugger
9332 session. */
9333
9334 #if GLYPH_DEBUG
9335
9336 /* First and last unchanged row for try_window_id. */
9337
9338 int debug_first_unchanged_at_end_vpos;
9339 int debug_last_unchanged_at_beg_vpos;
9340
9341 /* Delta vpos and y. */
9342
9343 int debug_dvpos, debug_dy;
9344
9345 /* Delta in characters and bytes for try_window_id. */
9346
9347 int debug_delta, debug_delta_bytes;
9348
9349 /* Values of window_end_pos and window_end_vpos at the end of
9350 try_window_id. */
9351
9352 EMACS_INT debug_end_pos, debug_end_vpos;
9353
9354 /* Append a string to W->desired_matrix->method. FMT is a printf
9355 format string. A1...A9 are a supplement for a variable-length
9356 argument list. If trace_redisplay_p is non-zero also printf the
9357 resulting string to stderr. */
9358
9359 static void
9360 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9361 struct window *w;
9362 char *fmt;
9363 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9364 {
9365 char buffer[512];
9366 char *method = w->desired_matrix->method;
9367 int len = strlen (method);
9368 int size = sizeof w->desired_matrix->method;
9369 int remaining = size - len - 1;
9370
9371 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9372 if (len && remaining)
9373 {
9374 method[len] = '|';
9375 --remaining, ++len;
9376 }
9377
9378 strncpy (method + len, buffer, remaining);
9379
9380 if (trace_redisplay_p)
9381 fprintf (stderr, "%p (%s): %s\n",
9382 w,
9383 ((BUFFERP (w->buffer)
9384 && STRINGP (XBUFFER (w->buffer)->name))
9385 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9386 : "no buffer"),
9387 buffer);
9388 }
9389
9390 #endif /* GLYPH_DEBUG */
9391
9392
9393 /* Value is non-zero if all changes in window W, which displays
9394 current_buffer, are in the text between START and END. START is a
9395 buffer position, END is given as a distance from Z. Used in
9396 redisplay_internal for display optimization. */
9397
9398 static INLINE int
9399 text_outside_line_unchanged_p (w, start, end)
9400 struct window *w;
9401 int start, end;
9402 {
9403 int unchanged_p = 1;
9404
9405 /* If text or overlays have changed, see where. */
9406 if (XFASTINT (w->last_modified) < MODIFF
9407 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9408 {
9409 /* Gap in the line? */
9410 if (GPT < start || Z - GPT < end)
9411 unchanged_p = 0;
9412
9413 /* Changes start in front of the line, or end after it? */
9414 if (unchanged_p
9415 && (BEG_UNCHANGED < start - 1
9416 || END_UNCHANGED < end))
9417 unchanged_p = 0;
9418
9419 /* If selective display, can't optimize if changes start at the
9420 beginning of the line. */
9421 if (unchanged_p
9422 && INTEGERP (current_buffer->selective_display)
9423 && XINT (current_buffer->selective_display) > 0
9424 && (BEG_UNCHANGED < start || GPT <= start))
9425 unchanged_p = 0;
9426
9427 /* If there are overlays at the start or end of the line, these
9428 may have overlay strings with newlines in them. A change at
9429 START, for instance, may actually concern the display of such
9430 overlay strings as well, and they are displayed on different
9431 lines. So, quickly rule out this case. (For the future, it
9432 might be desirable to implement something more telling than
9433 just BEG/END_UNCHANGED.) */
9434 if (unchanged_p)
9435 {
9436 if (BEG + BEG_UNCHANGED == start
9437 && overlay_touches_p (start))
9438 unchanged_p = 0;
9439 if (END_UNCHANGED == end
9440 && overlay_touches_p (Z - end))
9441 unchanged_p = 0;
9442 }
9443 }
9444
9445 return unchanged_p;
9446 }
9447
9448
9449 /* Do a frame update, taking possible shortcuts into account. This is
9450 the main external entry point for redisplay.
9451
9452 If the last redisplay displayed an echo area message and that message
9453 is no longer requested, we clear the echo area or bring back the
9454 mini-buffer if that is in use. */
9455
9456 void
9457 redisplay ()
9458 {
9459 redisplay_internal (0);
9460 }
9461
9462
9463 static Lisp_Object
9464 overlay_arrow_string_or_property (var, pbitmap)
9465 Lisp_Object var;
9466 int *pbitmap;
9467 {
9468 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9469 Lisp_Object bitmap;
9470
9471 if (pbitmap)
9472 {
9473 *pbitmap = 0;
9474 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9475 *pbitmap = XINT (bitmap);
9476 }
9477
9478 if (!NILP (pstr))
9479 return pstr;
9480 return Voverlay_arrow_string;
9481 }
9482
9483 /* Return 1 if there are any overlay-arrows in current_buffer. */
9484 static int
9485 overlay_arrow_in_current_buffer_p ()
9486 {
9487 Lisp_Object vlist;
9488
9489 for (vlist = Voverlay_arrow_variable_list;
9490 CONSP (vlist);
9491 vlist = XCDR (vlist))
9492 {
9493 Lisp_Object var = XCAR (vlist);
9494 Lisp_Object val;
9495
9496 if (!SYMBOLP (var))
9497 continue;
9498 val = find_symbol_value (var);
9499 if (MARKERP (val)
9500 && current_buffer == XMARKER (val)->buffer)
9501 return 1;
9502 }
9503 return 0;
9504 }
9505
9506
9507 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9508 has changed. */
9509
9510 static int
9511 overlay_arrows_changed_p ()
9512 {
9513 Lisp_Object vlist;
9514
9515 for (vlist = Voverlay_arrow_variable_list;
9516 CONSP (vlist);
9517 vlist = XCDR (vlist))
9518 {
9519 Lisp_Object var = XCAR (vlist);
9520 Lisp_Object val, pstr;
9521
9522 if (!SYMBOLP (var))
9523 continue;
9524 val = find_symbol_value (var);
9525 if (!MARKERP (val))
9526 continue;
9527 if (! EQ (COERCE_MARKER (val),
9528 Fget (var, Qlast_arrow_position))
9529 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9530 EQ (pstr, Fget (var, Qlast_arrow_string))))
9531 return 1;
9532 }
9533 return 0;
9534 }
9535
9536 /* Mark overlay arrows to be updated on next redisplay. */
9537
9538 static void
9539 update_overlay_arrows (up_to_date)
9540 int up_to_date;
9541 {
9542 Lisp_Object vlist;
9543
9544 for (vlist = Voverlay_arrow_variable_list;
9545 CONSP (vlist);
9546 vlist = XCDR (vlist))
9547 {
9548 Lisp_Object var = XCAR (vlist);
9549
9550 if (!SYMBOLP (var))
9551 continue;
9552
9553 if (up_to_date > 0)
9554 {
9555 Lisp_Object val = find_symbol_value (var);
9556 Fput (var, Qlast_arrow_position,
9557 COERCE_MARKER (val));
9558 Fput (var, Qlast_arrow_string,
9559 overlay_arrow_string_or_property (var, 0));
9560 }
9561 else if (up_to_date < 0
9562 || !NILP (Fget (var, Qlast_arrow_position)))
9563 {
9564 Fput (var, Qlast_arrow_position, Qt);
9565 Fput (var, Qlast_arrow_string, Qt);
9566 }
9567 }
9568 }
9569
9570
9571 /* Return overlay arrow string to display at row.
9572 Return t if display as bitmap in left fringe.
9573 Return nil if no overlay arrow. */
9574
9575 static Lisp_Object
9576 overlay_arrow_at_row (it, row, pbitmap)
9577 struct it *it;
9578 struct glyph_row *row;
9579 int *pbitmap;
9580 {
9581 Lisp_Object vlist;
9582
9583 for (vlist = Voverlay_arrow_variable_list;
9584 CONSP (vlist);
9585 vlist = XCDR (vlist))
9586 {
9587 Lisp_Object var = XCAR (vlist);
9588 Lisp_Object val;
9589
9590 if (!SYMBOLP (var))
9591 continue;
9592
9593 val = find_symbol_value (var);
9594
9595 if (MARKERP (val)
9596 && current_buffer == XMARKER (val)->buffer
9597 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9598 {
9599 val = overlay_arrow_string_or_property (var, pbitmap);
9600 if (FRAME_WINDOW_P (it->f)
9601 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9602 return Qt;
9603 if (STRINGP (val))
9604 return val;
9605 break;
9606 }
9607 }
9608
9609 *pbitmap = 0;
9610 return Qnil;
9611 }
9612
9613 /* Return 1 if point moved out of or into a composition. Otherwise
9614 return 0. PREV_BUF and PREV_PT are the last point buffer and
9615 position. BUF and PT are the current point buffer and position. */
9616
9617 int
9618 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9619 struct buffer *prev_buf, *buf;
9620 int prev_pt, pt;
9621 {
9622 int start, end;
9623 Lisp_Object prop;
9624 Lisp_Object buffer;
9625
9626 XSETBUFFER (buffer, buf);
9627 /* Check a composition at the last point if point moved within the
9628 same buffer. */
9629 if (prev_buf == buf)
9630 {
9631 if (prev_pt == pt)
9632 /* Point didn't move. */
9633 return 0;
9634
9635 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9636 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9637 && COMPOSITION_VALID_P (start, end, prop)
9638 && start < prev_pt && end > prev_pt)
9639 /* The last point was within the composition. Return 1 iff
9640 point moved out of the composition. */
9641 return (pt <= start || pt >= end);
9642 }
9643
9644 /* Check a composition at the current point. */
9645 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9646 && find_composition (pt, -1, &start, &end, &prop, buffer)
9647 && COMPOSITION_VALID_P (start, end, prop)
9648 && start < pt && end > pt);
9649 }
9650
9651
9652 /* Reconsider the setting of B->clip_changed which is displayed
9653 in window W. */
9654
9655 static INLINE void
9656 reconsider_clip_changes (w, b)
9657 struct window *w;
9658 struct buffer *b;
9659 {
9660 if (b->clip_changed
9661 && !NILP (w->window_end_valid)
9662 && w->current_matrix->buffer == b
9663 && w->current_matrix->zv == BUF_ZV (b)
9664 && w->current_matrix->begv == BUF_BEGV (b))
9665 b->clip_changed = 0;
9666
9667 /* If display wasn't paused, and W is not a tool bar window, see if
9668 point has been moved into or out of a composition. In that case,
9669 we set b->clip_changed to 1 to force updating the screen. If
9670 b->clip_changed has already been set to 1, we can skip this
9671 check. */
9672 if (!b->clip_changed
9673 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9674 {
9675 int pt;
9676
9677 if (w == XWINDOW (selected_window))
9678 pt = BUF_PT (current_buffer);
9679 else
9680 pt = marker_position (w->pointm);
9681
9682 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9683 || pt != XINT (w->last_point))
9684 && check_point_in_composition (w->current_matrix->buffer,
9685 XINT (w->last_point),
9686 XBUFFER (w->buffer), pt))
9687 b->clip_changed = 1;
9688 }
9689 }
9690 \f
9691
9692 /* Select FRAME to forward the values of frame-local variables into C
9693 variables so that the redisplay routines can access those values
9694 directly. */
9695
9696 static void
9697 select_frame_for_redisplay (frame)
9698 Lisp_Object frame;
9699 {
9700 Lisp_Object tail, sym, val;
9701 Lisp_Object old = selected_frame;
9702
9703 selected_frame = frame;
9704
9705 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9706 if (CONSP (XCAR (tail))
9707 && (sym = XCAR (XCAR (tail)),
9708 SYMBOLP (sym))
9709 && (sym = indirect_variable (sym),
9710 val = SYMBOL_VALUE (sym),
9711 (BUFFER_LOCAL_VALUEP (val)
9712 || SOME_BUFFER_LOCAL_VALUEP (val)))
9713 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9714 Fsymbol_value (sym);
9715
9716 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9717 if (CONSP (XCAR (tail))
9718 && (sym = XCAR (XCAR (tail)),
9719 SYMBOLP (sym))
9720 && (sym = indirect_variable (sym),
9721 val = SYMBOL_VALUE (sym),
9722 (BUFFER_LOCAL_VALUEP (val)
9723 || SOME_BUFFER_LOCAL_VALUEP (val)))
9724 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9725 Fsymbol_value (sym);
9726 }
9727
9728
9729 #define STOP_POLLING \
9730 do { if (! polling_stopped_here) stop_polling (); \
9731 polling_stopped_here = 1; } while (0)
9732
9733 #define RESUME_POLLING \
9734 do { if (polling_stopped_here) start_polling (); \
9735 polling_stopped_here = 0; } while (0)
9736
9737
9738 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9739 response to any user action; therefore, we should preserve the echo
9740 area. (Actually, our caller does that job.) Perhaps in the future
9741 avoid recentering windows if it is not necessary; currently that
9742 causes some problems. */
9743
9744 static void
9745 redisplay_internal (preserve_echo_area)
9746 int preserve_echo_area;
9747 {
9748 struct window *w = XWINDOW (selected_window);
9749 struct frame *f = XFRAME (w->frame);
9750 int pause;
9751 int must_finish = 0;
9752 struct text_pos tlbufpos, tlendpos;
9753 int number_of_visible_frames;
9754 int count;
9755 struct frame *sf = SELECTED_FRAME ();
9756 int polling_stopped_here = 0;
9757
9758 /* Non-zero means redisplay has to consider all windows on all
9759 frames. Zero means, only selected_window is considered. */
9760 int consider_all_windows_p;
9761
9762 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9763
9764 /* No redisplay if running in batch mode or frame is not yet fully
9765 initialized, or redisplay is explicitly turned off by setting
9766 Vinhibit_redisplay. */
9767 if (noninteractive
9768 || !NILP (Vinhibit_redisplay)
9769 || !f->glyphs_initialized_p)
9770 return;
9771
9772 /* The flag redisplay_performed_directly_p is set by
9773 direct_output_for_insert when it already did the whole screen
9774 update necessary. */
9775 if (redisplay_performed_directly_p)
9776 {
9777 redisplay_performed_directly_p = 0;
9778 if (!hscroll_windows (selected_window))
9779 return;
9780 }
9781
9782 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9783 if (popup_activated ())
9784 return;
9785 #endif
9786
9787 /* I don't think this happens but let's be paranoid. */
9788 if (redisplaying_p)
9789 return;
9790
9791 /* Record a function that resets redisplaying_p to its old value
9792 when we leave this function. */
9793 count = SPECPDL_INDEX ();
9794 record_unwind_protect (unwind_redisplay,
9795 Fcons (make_number (redisplaying_p), selected_frame));
9796 ++redisplaying_p;
9797 specbind (Qinhibit_free_realized_faces, Qnil);
9798
9799 retry:
9800 pause = 0;
9801 reconsider_clip_changes (w, current_buffer);
9802
9803 /* If new fonts have been loaded that make a glyph matrix adjustment
9804 necessary, do it. */
9805 if (fonts_changed_p)
9806 {
9807 adjust_glyphs (NULL);
9808 ++windows_or_buffers_changed;
9809 fonts_changed_p = 0;
9810 }
9811
9812 /* If face_change_count is non-zero, init_iterator will free all
9813 realized faces, which includes the faces referenced from current
9814 matrices. So, we can't reuse current matrices in this case. */
9815 if (face_change_count)
9816 ++windows_or_buffers_changed;
9817
9818 if (! FRAME_WINDOW_P (sf)
9819 && previous_terminal_frame != sf)
9820 {
9821 /* Since frames on an ASCII terminal share the same display
9822 area, displaying a different frame means redisplay the whole
9823 thing. */
9824 windows_or_buffers_changed++;
9825 SET_FRAME_GARBAGED (sf);
9826 XSETFRAME (Vterminal_frame, sf);
9827 }
9828 previous_terminal_frame = sf;
9829
9830 /* Set the visible flags for all frames. Do this before checking
9831 for resized or garbaged frames; they want to know if their frames
9832 are visible. See the comment in frame.h for
9833 FRAME_SAMPLE_VISIBILITY. */
9834 {
9835 Lisp_Object tail, frame;
9836
9837 number_of_visible_frames = 0;
9838
9839 FOR_EACH_FRAME (tail, frame)
9840 {
9841 struct frame *f = XFRAME (frame);
9842
9843 FRAME_SAMPLE_VISIBILITY (f);
9844 if (FRAME_VISIBLE_P (f))
9845 ++number_of_visible_frames;
9846 clear_desired_matrices (f);
9847 }
9848 }
9849
9850 /* Notice any pending interrupt request to change frame size. */
9851 do_pending_window_change (1);
9852
9853 /* Clear frames marked as garbaged. */
9854 if (frame_garbaged)
9855 clear_garbaged_frames ();
9856
9857 /* Build menubar and tool-bar items. */
9858 prepare_menu_bars ();
9859
9860 if (windows_or_buffers_changed)
9861 update_mode_lines++;
9862
9863 /* Detect case that we need to write or remove a star in the mode line. */
9864 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9865 {
9866 w->update_mode_line = Qt;
9867 if (buffer_shared > 1)
9868 update_mode_lines++;
9869 }
9870
9871 /* If %c is in the mode line, update it if needed. */
9872 if (!NILP (w->column_number_displayed)
9873 /* This alternative quickly identifies a common case
9874 where no change is needed. */
9875 && !(PT == XFASTINT (w->last_point)
9876 && XFASTINT (w->last_modified) >= MODIFF
9877 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9878 && (XFASTINT (w->column_number_displayed)
9879 != (int) current_column ())) /* iftc */
9880 w->update_mode_line = Qt;
9881
9882 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9883
9884 /* The variable buffer_shared is set in redisplay_window and
9885 indicates that we redisplay a buffer in different windows. See
9886 there. */
9887 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9888 || cursor_type_changed);
9889
9890 /* If specs for an arrow have changed, do thorough redisplay
9891 to ensure we remove any arrow that should no longer exist. */
9892 if (overlay_arrows_changed_p ())
9893 consider_all_windows_p = windows_or_buffers_changed = 1;
9894
9895 /* Normally the message* functions will have already displayed and
9896 updated the echo area, but the frame may have been trashed, or
9897 the update may have been preempted, so display the echo area
9898 again here. Checking message_cleared_p captures the case that
9899 the echo area should be cleared. */
9900 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9901 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9902 || (message_cleared_p
9903 && minibuf_level == 0
9904 /* If the mini-window is currently selected, this means the
9905 echo-area doesn't show through. */
9906 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9907 {
9908 int window_height_changed_p = echo_area_display (0);
9909 must_finish = 1;
9910
9911 /* If we don't display the current message, don't clear the
9912 message_cleared_p flag, because, if we did, we wouldn't clear
9913 the echo area in the next redisplay which doesn't preserve
9914 the echo area. */
9915 if (!display_last_displayed_message_p)
9916 message_cleared_p = 0;
9917
9918 if (fonts_changed_p)
9919 goto retry;
9920 else if (window_height_changed_p)
9921 {
9922 consider_all_windows_p = 1;
9923 ++update_mode_lines;
9924 ++windows_or_buffers_changed;
9925
9926 /* If window configuration was changed, frames may have been
9927 marked garbaged. Clear them or we will experience
9928 surprises wrt scrolling. */
9929 if (frame_garbaged)
9930 clear_garbaged_frames ();
9931 }
9932 }
9933 else if (EQ (selected_window, minibuf_window)
9934 && (current_buffer->clip_changed
9935 || XFASTINT (w->last_modified) < MODIFF
9936 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9937 && resize_mini_window (w, 0))
9938 {
9939 /* Resized active mini-window to fit the size of what it is
9940 showing if its contents might have changed. */
9941 must_finish = 1;
9942 consider_all_windows_p = 1;
9943 ++windows_or_buffers_changed;
9944 ++update_mode_lines;
9945
9946 /* If window configuration was changed, frames may have been
9947 marked garbaged. Clear them or we will experience
9948 surprises wrt scrolling. */
9949 if (frame_garbaged)
9950 clear_garbaged_frames ();
9951 }
9952
9953
9954 /* If showing the region, and mark has changed, we must redisplay
9955 the whole window. The assignment to this_line_start_pos prevents
9956 the optimization directly below this if-statement. */
9957 if (((!NILP (Vtransient_mark_mode)
9958 && !NILP (XBUFFER (w->buffer)->mark_active))
9959 != !NILP (w->region_showing))
9960 || (!NILP (w->region_showing)
9961 && !EQ (w->region_showing,
9962 Fmarker_position (XBUFFER (w->buffer)->mark))))
9963 CHARPOS (this_line_start_pos) = 0;
9964
9965 /* Optimize the case that only the line containing the cursor in the
9966 selected window has changed. Variables starting with this_ are
9967 set in display_line and record information about the line
9968 containing the cursor. */
9969 tlbufpos = this_line_start_pos;
9970 tlendpos = this_line_end_pos;
9971 if (!consider_all_windows_p
9972 && CHARPOS (tlbufpos) > 0
9973 && NILP (w->update_mode_line)
9974 && !current_buffer->clip_changed
9975 && !current_buffer->prevent_redisplay_optimizations_p
9976 && FRAME_VISIBLE_P (XFRAME (w->frame))
9977 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9978 /* Make sure recorded data applies to current buffer, etc. */
9979 && this_line_buffer == current_buffer
9980 && current_buffer == XBUFFER (w->buffer)
9981 && NILP (w->force_start)
9982 && NILP (w->optional_new_start)
9983 /* Point must be on the line that we have info recorded about. */
9984 && PT >= CHARPOS (tlbufpos)
9985 && PT <= Z - CHARPOS (tlendpos)
9986 /* All text outside that line, including its final newline,
9987 must be unchanged */
9988 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9989 CHARPOS (tlendpos)))
9990 {
9991 if (CHARPOS (tlbufpos) > BEGV
9992 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9993 && (CHARPOS (tlbufpos) == ZV
9994 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9995 /* Former continuation line has disappeared by becoming empty */
9996 goto cancel;
9997 else if (XFASTINT (w->last_modified) < MODIFF
9998 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9999 || MINI_WINDOW_P (w))
10000 {
10001 /* We have to handle the case of continuation around a
10002 wide-column character (See the comment in indent.c around
10003 line 885).
10004
10005 For instance, in the following case:
10006
10007 -------- Insert --------
10008 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10009 J_I_ ==> J_I_ `^^' are cursors.
10010 ^^ ^^
10011 -------- --------
10012
10013 As we have to redraw the line above, we should goto cancel. */
10014
10015 struct it it;
10016 int line_height_before = this_line_pixel_height;
10017
10018 /* Note that start_display will handle the case that the
10019 line starting at tlbufpos is a continuation lines. */
10020 start_display (&it, w, tlbufpos);
10021
10022 /* Implementation note: It this still necessary? */
10023 if (it.current_x != this_line_start_x)
10024 goto cancel;
10025
10026 TRACE ((stderr, "trying display optimization 1\n"));
10027 w->cursor.vpos = -1;
10028 overlay_arrow_seen = 0;
10029 it.vpos = this_line_vpos;
10030 it.current_y = this_line_y;
10031 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10032 display_line (&it);
10033
10034 /* If line contains point, is not continued,
10035 and ends at same distance from eob as before, we win */
10036 if (w->cursor.vpos >= 0
10037 /* Line is not continued, otherwise this_line_start_pos
10038 would have been set to 0 in display_line. */
10039 && CHARPOS (this_line_start_pos)
10040 /* Line ends as before. */
10041 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10042 /* Line has same height as before. Otherwise other lines
10043 would have to be shifted up or down. */
10044 && this_line_pixel_height == line_height_before)
10045 {
10046 /* If this is not the window's last line, we must adjust
10047 the charstarts of the lines below. */
10048 if (it.current_y < it.last_visible_y)
10049 {
10050 struct glyph_row *row
10051 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10052 int delta, delta_bytes;
10053
10054 if (Z - CHARPOS (tlendpos) == ZV)
10055 {
10056 /* This line ends at end of (accessible part of)
10057 buffer. There is no newline to count. */
10058 delta = (Z
10059 - CHARPOS (tlendpos)
10060 - MATRIX_ROW_START_CHARPOS (row));
10061 delta_bytes = (Z_BYTE
10062 - BYTEPOS (tlendpos)
10063 - MATRIX_ROW_START_BYTEPOS (row));
10064 }
10065 else
10066 {
10067 /* This line ends in a newline. Must take
10068 account of the newline and the rest of the
10069 text that follows. */
10070 delta = (Z
10071 - CHARPOS (tlendpos)
10072 - MATRIX_ROW_START_CHARPOS (row));
10073 delta_bytes = (Z_BYTE
10074 - BYTEPOS (tlendpos)
10075 - MATRIX_ROW_START_BYTEPOS (row));
10076 }
10077
10078 increment_matrix_positions (w->current_matrix,
10079 this_line_vpos + 1,
10080 w->current_matrix->nrows,
10081 delta, delta_bytes);
10082 }
10083
10084 /* If this row displays text now but previously didn't,
10085 or vice versa, w->window_end_vpos may have to be
10086 adjusted. */
10087 if ((it.glyph_row - 1)->displays_text_p)
10088 {
10089 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10090 XSETINT (w->window_end_vpos, this_line_vpos);
10091 }
10092 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10093 && this_line_vpos > 0)
10094 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10095 w->window_end_valid = Qnil;
10096
10097 /* Update hint: No need to try to scroll in update_window. */
10098 w->desired_matrix->no_scrolling_p = 1;
10099
10100 #if GLYPH_DEBUG
10101 *w->desired_matrix->method = 0;
10102 debug_method_add (w, "optimization 1");
10103 #endif
10104 #ifdef HAVE_WINDOW_SYSTEM
10105 update_window_fringes (w, 0);
10106 #endif
10107 goto update;
10108 }
10109 else
10110 goto cancel;
10111 }
10112 else if (/* Cursor position hasn't changed. */
10113 PT == XFASTINT (w->last_point)
10114 /* Make sure the cursor was last displayed
10115 in this window. Otherwise we have to reposition it. */
10116 && 0 <= w->cursor.vpos
10117 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10118 {
10119 if (!must_finish)
10120 {
10121 do_pending_window_change (1);
10122
10123 /* We used to always goto end_of_redisplay here, but this
10124 isn't enough if we have a blinking cursor. */
10125 if (w->cursor_off_p == w->last_cursor_off_p)
10126 goto end_of_redisplay;
10127 }
10128 goto update;
10129 }
10130 /* If highlighting the region, or if the cursor is in the echo area,
10131 then we can't just move the cursor. */
10132 else if (! (!NILP (Vtransient_mark_mode)
10133 && !NILP (current_buffer->mark_active))
10134 && (EQ (selected_window, current_buffer->last_selected_window)
10135 || highlight_nonselected_windows)
10136 && NILP (w->region_showing)
10137 && NILP (Vshow_trailing_whitespace)
10138 && !cursor_in_echo_area)
10139 {
10140 struct it it;
10141 struct glyph_row *row;
10142
10143 /* Skip from tlbufpos to PT and see where it is. Note that
10144 PT may be in invisible text. If so, we will end at the
10145 next visible position. */
10146 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10147 NULL, DEFAULT_FACE_ID);
10148 it.current_x = this_line_start_x;
10149 it.current_y = this_line_y;
10150 it.vpos = this_line_vpos;
10151
10152 /* The call to move_it_to stops in front of PT, but
10153 moves over before-strings. */
10154 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10155
10156 if (it.vpos == this_line_vpos
10157 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10158 row->enabled_p))
10159 {
10160 xassert (this_line_vpos == it.vpos);
10161 xassert (this_line_y == it.current_y);
10162 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10163 #if GLYPH_DEBUG
10164 *w->desired_matrix->method = 0;
10165 debug_method_add (w, "optimization 3");
10166 #endif
10167 goto update;
10168 }
10169 else
10170 goto cancel;
10171 }
10172
10173 cancel:
10174 /* Text changed drastically or point moved off of line. */
10175 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10176 }
10177
10178 CHARPOS (this_line_start_pos) = 0;
10179 consider_all_windows_p |= buffer_shared > 1;
10180 ++clear_face_cache_count;
10181
10182
10183 /* Build desired matrices, and update the display. If
10184 consider_all_windows_p is non-zero, do it for all windows on all
10185 frames. Otherwise do it for selected_window, only. */
10186
10187 if (consider_all_windows_p)
10188 {
10189 Lisp_Object tail, frame;
10190 int i, n = 0, size = 50;
10191 struct frame **updated
10192 = (struct frame **) alloca (size * sizeof *updated);
10193
10194 /* Clear the face cache eventually. */
10195 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10196 {
10197 clear_face_cache (0);
10198 clear_face_cache_count = 0;
10199 }
10200
10201 /* Recompute # windows showing selected buffer. This will be
10202 incremented each time such a window is displayed. */
10203 buffer_shared = 0;
10204
10205 FOR_EACH_FRAME (tail, frame)
10206 {
10207 struct frame *f = XFRAME (frame);
10208
10209 if (FRAME_WINDOW_P (f) || f == sf)
10210 {
10211 if (! EQ (frame, selected_frame))
10212 /* Select the frame, for the sake of frame-local
10213 variables. */
10214 select_frame_for_redisplay (frame);
10215
10216 #ifdef HAVE_WINDOW_SYSTEM
10217 if (clear_face_cache_count % 50 == 0
10218 && FRAME_WINDOW_P (f))
10219 clear_image_cache (f, 0);
10220 #endif /* HAVE_WINDOW_SYSTEM */
10221
10222 /* Mark all the scroll bars to be removed; we'll redeem
10223 the ones we want when we redisplay their windows. */
10224 if (condemn_scroll_bars_hook)
10225 condemn_scroll_bars_hook (f);
10226
10227 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10228 redisplay_windows (FRAME_ROOT_WINDOW (f));
10229
10230 /* Any scroll bars which redisplay_windows should have
10231 nuked should now go away. */
10232 if (judge_scroll_bars_hook)
10233 judge_scroll_bars_hook (f);
10234
10235 /* If fonts changed, display again. */
10236 /* ??? rms: I suspect it is a mistake to jump all the way
10237 back to retry here. It should just retry this frame. */
10238 if (fonts_changed_p)
10239 goto retry;
10240
10241 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10242 {
10243 /* See if we have to hscroll. */
10244 if (hscroll_windows (f->root_window))
10245 goto retry;
10246
10247 /* Prevent various kinds of signals during display
10248 update. stdio is not robust about handling
10249 signals, which can cause an apparent I/O
10250 error. */
10251 if (interrupt_input)
10252 unrequest_sigio ();
10253 STOP_POLLING;
10254
10255 /* Update the display. */
10256 set_window_update_flags (XWINDOW (f->root_window), 1);
10257 pause |= update_frame (f, 0, 0);
10258 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10259 if (pause)
10260 break;
10261 #endif
10262
10263 if (n == size)
10264 {
10265 int nbytes = size * sizeof *updated;
10266 struct frame **p = (struct frame **) alloca (2 * nbytes);
10267 bcopy (updated, p, nbytes);
10268 size *= 2;
10269 }
10270
10271 updated[n++] = f;
10272 }
10273 }
10274 }
10275
10276 if (!pause)
10277 {
10278 /* Do the mark_window_display_accurate after all windows have
10279 been redisplayed because this call resets flags in buffers
10280 which are needed for proper redisplay. */
10281 for (i = 0; i < n; ++i)
10282 {
10283 struct frame *f = updated[i];
10284 mark_window_display_accurate (f->root_window, 1);
10285 if (frame_up_to_date_hook)
10286 frame_up_to_date_hook (f);
10287 }
10288 }
10289 }
10290 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10291 {
10292 Lisp_Object mini_window;
10293 struct frame *mini_frame;
10294
10295 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10296 /* Use list_of_error, not Qerror, so that
10297 we catch only errors and don't run the debugger. */
10298 internal_condition_case_1 (redisplay_window_1, selected_window,
10299 list_of_error,
10300 redisplay_window_error);
10301
10302 /* Compare desired and current matrices, perform output. */
10303
10304 update:
10305 /* If fonts changed, display again. */
10306 if (fonts_changed_p)
10307 goto retry;
10308
10309 /* Prevent various kinds of signals during display update.
10310 stdio is not robust about handling signals,
10311 which can cause an apparent I/O error. */
10312 if (interrupt_input)
10313 unrequest_sigio ();
10314 STOP_POLLING;
10315
10316 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10317 {
10318 if (hscroll_windows (selected_window))
10319 goto retry;
10320
10321 XWINDOW (selected_window)->must_be_updated_p = 1;
10322 pause = update_frame (sf, 0, 0);
10323 }
10324
10325 /* We may have called echo_area_display at the top of this
10326 function. If the echo area is on another frame, that may
10327 have put text on a frame other than the selected one, so the
10328 above call to update_frame would not have caught it. Catch
10329 it here. */
10330 mini_window = FRAME_MINIBUF_WINDOW (sf);
10331 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10332
10333 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10334 {
10335 XWINDOW (mini_window)->must_be_updated_p = 1;
10336 pause |= update_frame (mini_frame, 0, 0);
10337 if (!pause && hscroll_windows (mini_window))
10338 goto retry;
10339 }
10340 }
10341
10342 /* If display was paused because of pending input, make sure we do a
10343 thorough update the next time. */
10344 if (pause)
10345 {
10346 /* Prevent the optimization at the beginning of
10347 redisplay_internal that tries a single-line update of the
10348 line containing the cursor in the selected window. */
10349 CHARPOS (this_line_start_pos) = 0;
10350
10351 /* Let the overlay arrow be updated the next time. */
10352 update_overlay_arrows (0);
10353
10354 /* If we pause after scrolling, some rows in the current
10355 matrices of some windows are not valid. */
10356 if (!WINDOW_FULL_WIDTH_P (w)
10357 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10358 update_mode_lines = 1;
10359 }
10360 else
10361 {
10362 if (!consider_all_windows_p)
10363 {
10364 /* This has already been done above if
10365 consider_all_windows_p is set. */
10366 mark_window_display_accurate_1 (w, 1);
10367
10368 /* Say overlay arrows are up to date. */
10369 update_overlay_arrows (1);
10370
10371 if (frame_up_to_date_hook != 0)
10372 frame_up_to_date_hook (sf);
10373 }
10374
10375 update_mode_lines = 0;
10376 windows_or_buffers_changed = 0;
10377 cursor_type_changed = 0;
10378 }
10379
10380 /* Start SIGIO interrupts coming again. Having them off during the
10381 code above makes it less likely one will discard output, but not
10382 impossible, since there might be stuff in the system buffer here.
10383 But it is much hairier to try to do anything about that. */
10384 if (interrupt_input)
10385 request_sigio ();
10386 RESUME_POLLING;
10387
10388 /* If a frame has become visible which was not before, redisplay
10389 again, so that we display it. Expose events for such a frame
10390 (which it gets when becoming visible) don't call the parts of
10391 redisplay constructing glyphs, so simply exposing a frame won't
10392 display anything in this case. So, we have to display these
10393 frames here explicitly. */
10394 if (!pause)
10395 {
10396 Lisp_Object tail, frame;
10397 int new_count = 0;
10398
10399 FOR_EACH_FRAME (tail, frame)
10400 {
10401 int this_is_visible = 0;
10402
10403 if (XFRAME (frame)->visible)
10404 this_is_visible = 1;
10405 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10406 if (XFRAME (frame)->visible)
10407 this_is_visible = 1;
10408
10409 if (this_is_visible)
10410 new_count++;
10411 }
10412
10413 if (new_count != number_of_visible_frames)
10414 windows_or_buffers_changed++;
10415 }
10416
10417 /* Change frame size now if a change is pending. */
10418 do_pending_window_change (1);
10419
10420 /* If we just did a pending size change, or have additional
10421 visible frames, redisplay again. */
10422 if (windows_or_buffers_changed && !pause)
10423 goto retry;
10424
10425 end_of_redisplay:
10426 unbind_to (count, Qnil);
10427 RESUME_POLLING;
10428 }
10429
10430
10431 /* Redisplay, but leave alone any recent echo area message unless
10432 another message has been requested in its place.
10433
10434 This is useful in situations where you need to redisplay but no
10435 user action has occurred, making it inappropriate for the message
10436 area to be cleared. See tracking_off and
10437 wait_reading_process_output for examples of these situations.
10438
10439 FROM_WHERE is an integer saying from where this function was
10440 called. This is useful for debugging. */
10441
10442 void
10443 redisplay_preserve_echo_area (from_where)
10444 int from_where;
10445 {
10446 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10447
10448 if (!NILP (echo_area_buffer[1]))
10449 {
10450 /* We have a previously displayed message, but no current
10451 message. Redisplay the previous message. */
10452 display_last_displayed_message_p = 1;
10453 redisplay_internal (1);
10454 display_last_displayed_message_p = 0;
10455 }
10456 else
10457 redisplay_internal (1);
10458
10459 if (rif != NULL && rif->flush_display_optional)
10460 rif->flush_display_optional (NULL);
10461 }
10462
10463
10464 /* Function registered with record_unwind_protect in
10465 redisplay_internal. Reset redisplaying_p to the value it had
10466 before redisplay_internal was called, and clear
10467 prevent_freeing_realized_faces_p. It also selects the previously
10468 selected frame. */
10469
10470 static Lisp_Object
10471 unwind_redisplay (val)
10472 Lisp_Object val;
10473 {
10474 Lisp_Object old_redisplaying_p, old_frame;
10475
10476 old_redisplaying_p = XCAR (val);
10477 redisplaying_p = XFASTINT (old_redisplaying_p);
10478 old_frame = XCDR (val);
10479 if (! EQ (old_frame, selected_frame))
10480 select_frame_for_redisplay (old_frame);
10481 return Qnil;
10482 }
10483
10484
10485 /* Mark the display of window W as accurate or inaccurate. If
10486 ACCURATE_P is non-zero mark display of W as accurate. If
10487 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10488 redisplay_internal is called. */
10489
10490 static void
10491 mark_window_display_accurate_1 (w, accurate_p)
10492 struct window *w;
10493 int accurate_p;
10494 {
10495 if (BUFFERP (w->buffer))
10496 {
10497 struct buffer *b = XBUFFER (w->buffer);
10498
10499 w->last_modified
10500 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10501 w->last_overlay_modified
10502 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10503 w->last_had_star
10504 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10505
10506 if (accurate_p)
10507 {
10508 b->clip_changed = 0;
10509 b->prevent_redisplay_optimizations_p = 0;
10510
10511 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10512 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10513 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10514 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10515
10516 w->current_matrix->buffer = b;
10517 w->current_matrix->begv = BUF_BEGV (b);
10518 w->current_matrix->zv = BUF_ZV (b);
10519
10520 w->last_cursor = w->cursor;
10521 w->last_cursor_off_p = w->cursor_off_p;
10522
10523 if (w == XWINDOW (selected_window))
10524 w->last_point = make_number (BUF_PT (b));
10525 else
10526 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10527 }
10528 }
10529
10530 if (accurate_p)
10531 {
10532 w->window_end_valid = w->buffer;
10533 #if 0 /* This is incorrect with variable-height lines. */
10534 xassert (XINT (w->window_end_vpos)
10535 < (WINDOW_TOTAL_LINES (w)
10536 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10537 #endif
10538 w->update_mode_line = Qnil;
10539 }
10540 }
10541
10542
10543 /* Mark the display of windows in the window tree rooted at WINDOW as
10544 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10545 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10546 be redisplayed the next time redisplay_internal is called. */
10547
10548 void
10549 mark_window_display_accurate (window, accurate_p)
10550 Lisp_Object window;
10551 int accurate_p;
10552 {
10553 struct window *w;
10554
10555 for (; !NILP (window); window = w->next)
10556 {
10557 w = XWINDOW (window);
10558 mark_window_display_accurate_1 (w, accurate_p);
10559
10560 if (!NILP (w->vchild))
10561 mark_window_display_accurate (w->vchild, accurate_p);
10562 if (!NILP (w->hchild))
10563 mark_window_display_accurate (w->hchild, accurate_p);
10564 }
10565
10566 if (accurate_p)
10567 {
10568 update_overlay_arrows (1);
10569 }
10570 else
10571 {
10572 /* Force a thorough redisplay the next time by setting
10573 last_arrow_position and last_arrow_string to t, which is
10574 unequal to any useful value of Voverlay_arrow_... */
10575 update_overlay_arrows (-1);
10576 }
10577 }
10578
10579
10580 /* Return value in display table DP (Lisp_Char_Table *) for character
10581 C. Since a display table doesn't have any parent, we don't have to
10582 follow parent. Do not call this function directly but use the
10583 macro DISP_CHAR_VECTOR. */
10584
10585 Lisp_Object
10586 disp_char_vector (dp, c)
10587 struct Lisp_Char_Table *dp;
10588 int c;
10589 {
10590 int code[4], i;
10591 Lisp_Object val;
10592
10593 if (SINGLE_BYTE_CHAR_P (c))
10594 return (dp->contents[c]);
10595
10596 SPLIT_CHAR (c, code[0], code[1], code[2]);
10597 if (code[1] < 32)
10598 code[1] = -1;
10599 else if (code[2] < 32)
10600 code[2] = -1;
10601
10602 /* Here, the possible range of code[0] (== charset ID) is
10603 128..max_charset. Since the top level char table contains data
10604 for multibyte characters after 256th element, we must increment
10605 code[0] by 128 to get a correct index. */
10606 code[0] += 128;
10607 code[3] = -1; /* anchor */
10608
10609 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10610 {
10611 val = dp->contents[code[i]];
10612 if (!SUB_CHAR_TABLE_P (val))
10613 return (NILP (val) ? dp->defalt : val);
10614 }
10615
10616 /* Here, val is a sub char table. We return the default value of
10617 it. */
10618 return (dp->defalt);
10619 }
10620
10621
10622 \f
10623 /***********************************************************************
10624 Window Redisplay
10625 ***********************************************************************/
10626
10627 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10628
10629 static void
10630 redisplay_windows (window)
10631 Lisp_Object window;
10632 {
10633 while (!NILP (window))
10634 {
10635 struct window *w = XWINDOW (window);
10636
10637 if (!NILP (w->hchild))
10638 redisplay_windows (w->hchild);
10639 else if (!NILP (w->vchild))
10640 redisplay_windows (w->vchild);
10641 else
10642 {
10643 displayed_buffer = XBUFFER (w->buffer);
10644 /* Use list_of_error, not Qerror, so that
10645 we catch only errors and don't run the debugger. */
10646 internal_condition_case_1 (redisplay_window_0, window,
10647 list_of_error,
10648 redisplay_window_error);
10649 }
10650
10651 window = w->next;
10652 }
10653 }
10654
10655 static Lisp_Object
10656 redisplay_window_error ()
10657 {
10658 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10659 return Qnil;
10660 }
10661
10662 static Lisp_Object
10663 redisplay_window_0 (window)
10664 Lisp_Object window;
10665 {
10666 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10667 redisplay_window (window, 0);
10668 return Qnil;
10669 }
10670
10671 static Lisp_Object
10672 redisplay_window_1 (window)
10673 Lisp_Object window;
10674 {
10675 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10676 redisplay_window (window, 1);
10677 return Qnil;
10678 }
10679 \f
10680
10681 /* Increment GLYPH until it reaches END or CONDITION fails while
10682 adding (GLYPH)->pixel_width to X. */
10683
10684 #define SKIP_GLYPHS(glyph, end, x, condition) \
10685 do \
10686 { \
10687 (x) += (glyph)->pixel_width; \
10688 ++(glyph); \
10689 } \
10690 while ((glyph) < (end) && (condition))
10691
10692
10693 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10694 DELTA is the number of bytes by which positions recorded in ROW
10695 differ from current buffer positions. */
10696
10697 void
10698 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10699 struct window *w;
10700 struct glyph_row *row;
10701 struct glyph_matrix *matrix;
10702 int delta, delta_bytes, dy, dvpos;
10703 {
10704 struct glyph *glyph = row->glyphs[TEXT_AREA];
10705 struct glyph *end = glyph + row->used[TEXT_AREA];
10706 struct glyph *cursor = NULL;
10707 /* The first glyph that starts a sequence of glyphs from string. */
10708 struct glyph *string_start;
10709 /* The X coordinate of string_start. */
10710 int string_start_x;
10711 /* The last known character position. */
10712 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10713 /* The last known character position before string_start. */
10714 int string_before_pos;
10715 int x = row->x;
10716 int cursor_x = x;
10717 int cursor_from_overlay_pos = 0;
10718 int pt_old = PT - delta;
10719
10720 /* Skip over glyphs not having an object at the start of the row.
10721 These are special glyphs like truncation marks on terminal
10722 frames. */
10723 if (row->displays_text_p)
10724 while (glyph < end
10725 && INTEGERP (glyph->object)
10726 && glyph->charpos < 0)
10727 {
10728 x += glyph->pixel_width;
10729 ++glyph;
10730 }
10731
10732 string_start = NULL;
10733 while (glyph < end
10734 && !INTEGERP (glyph->object)
10735 && (!BUFFERP (glyph->object)
10736 || (last_pos = glyph->charpos) < pt_old))
10737 {
10738 if (! STRINGP (glyph->object))
10739 {
10740 string_start = NULL;
10741 x += glyph->pixel_width;
10742 ++glyph;
10743 if (cursor_from_overlay_pos
10744 && last_pos > cursor_from_overlay_pos)
10745 {
10746 cursor_from_overlay_pos = 0;
10747 cursor = 0;
10748 }
10749 }
10750 else
10751 {
10752 string_before_pos = last_pos;
10753 string_start = glyph;
10754 string_start_x = x;
10755 /* Skip all glyphs from string. */
10756 do
10757 {
10758 int pos;
10759 if ((cursor == NULL || glyph > cursor)
10760 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10761 Qcursor, (glyph)->object))
10762 && (pos = string_buffer_position (w, glyph->object,
10763 string_before_pos),
10764 (pos == 0 /* From overlay */
10765 || pos == pt_old)))
10766 {
10767 /* Estimate overlay buffer position from the buffer
10768 positions of the glyphs before and after the overlay.
10769 Add 1 to last_pos so that if point corresponds to the
10770 glyph right after the overlay, we still use a 'cursor'
10771 property found in that overlay. */
10772 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10773 cursor = glyph;
10774 cursor_x = x;
10775 }
10776 x += glyph->pixel_width;
10777 ++glyph;
10778 }
10779 while (glyph < end && STRINGP (glyph->object));
10780 }
10781 }
10782
10783 if (cursor != NULL)
10784 {
10785 glyph = cursor;
10786 x = cursor_x;
10787 }
10788 else if (string_start
10789 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10790 {
10791 /* We may have skipped over point because the previous glyphs
10792 are from string. As there's no easy way to know the
10793 character position of the current glyph, find the correct
10794 glyph on point by scanning from string_start again. */
10795 Lisp_Object limit;
10796 Lisp_Object string;
10797 int pos;
10798
10799 limit = make_number (pt_old + 1);
10800 end = glyph;
10801 glyph = string_start;
10802 x = string_start_x;
10803 string = glyph->object;
10804 pos = string_buffer_position (w, string, string_before_pos);
10805 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10806 because we always put cursor after overlay strings. */
10807 while (pos == 0 && glyph < end)
10808 {
10809 string = glyph->object;
10810 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10811 if (glyph < end)
10812 pos = string_buffer_position (w, glyph->object, string_before_pos);
10813 }
10814
10815 while (glyph < end)
10816 {
10817 pos = XINT (Fnext_single_char_property_change
10818 (make_number (pos), Qdisplay, Qnil, limit));
10819 if (pos > pt_old)
10820 break;
10821 /* Skip glyphs from the same string. */
10822 string = glyph->object;
10823 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10824 /* Skip glyphs from an overlay. */
10825 while (glyph < end
10826 && ! string_buffer_position (w, glyph->object, pos))
10827 {
10828 string = glyph->object;
10829 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10830 }
10831 }
10832 }
10833
10834 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10835 w->cursor.x = x;
10836 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10837 w->cursor.y = row->y + dy;
10838
10839 if (w == XWINDOW (selected_window))
10840 {
10841 if (!row->continued_p
10842 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10843 && row->x == 0)
10844 {
10845 this_line_buffer = XBUFFER (w->buffer);
10846
10847 CHARPOS (this_line_start_pos)
10848 = MATRIX_ROW_START_CHARPOS (row) + delta;
10849 BYTEPOS (this_line_start_pos)
10850 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10851
10852 CHARPOS (this_line_end_pos)
10853 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10854 BYTEPOS (this_line_end_pos)
10855 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10856
10857 this_line_y = w->cursor.y;
10858 this_line_pixel_height = row->height;
10859 this_line_vpos = w->cursor.vpos;
10860 this_line_start_x = row->x;
10861 }
10862 else
10863 CHARPOS (this_line_start_pos) = 0;
10864 }
10865 }
10866
10867
10868 /* Run window scroll functions, if any, for WINDOW with new window
10869 start STARTP. Sets the window start of WINDOW to that position.
10870
10871 We assume that the window's buffer is really current. */
10872
10873 static INLINE struct text_pos
10874 run_window_scroll_functions (window, startp)
10875 Lisp_Object window;
10876 struct text_pos startp;
10877 {
10878 struct window *w = XWINDOW (window);
10879 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10880
10881 if (current_buffer != XBUFFER (w->buffer))
10882 abort ();
10883
10884 if (!NILP (Vwindow_scroll_functions))
10885 {
10886 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10887 make_number (CHARPOS (startp)));
10888 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10889 /* In case the hook functions switch buffers. */
10890 if (current_buffer != XBUFFER (w->buffer))
10891 set_buffer_internal_1 (XBUFFER (w->buffer));
10892 }
10893
10894 return startp;
10895 }
10896
10897
10898 /* Make sure the line containing the cursor is fully visible.
10899 A value of 1 means there is nothing to be done.
10900 (Either the line is fully visible, or it cannot be made so,
10901 or we cannot tell.)
10902
10903 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10904 is higher than window.
10905
10906 A value of 0 means the caller should do scrolling
10907 as if point had gone off the screen. */
10908
10909 static int
10910 make_cursor_line_fully_visible (w, force_p)
10911 struct window *w;
10912 int force_p;
10913 {
10914 struct glyph_matrix *matrix;
10915 struct glyph_row *row;
10916 int window_height;
10917
10918 if (!make_cursor_line_fully_visible_p)
10919 return 1;
10920
10921 /* It's not always possible to find the cursor, e.g, when a window
10922 is full of overlay strings. Don't do anything in that case. */
10923 if (w->cursor.vpos < 0)
10924 return 1;
10925
10926 matrix = w->desired_matrix;
10927 row = MATRIX_ROW (matrix, w->cursor.vpos);
10928
10929 /* If the cursor row is not partially visible, there's nothing to do. */
10930 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
10931 return 1;
10932
10933 /* If the row the cursor is in is taller than the window's height,
10934 it's not clear what to do, so do nothing. */
10935 window_height = window_box_height (w);
10936 if (row->height >= window_height)
10937 {
10938 if (!force_p || w->vscroll)
10939 return 1;
10940 }
10941 return 0;
10942
10943 #if 0
10944 /* This code used to try to scroll the window just enough to make
10945 the line visible. It returned 0 to say that the caller should
10946 allocate larger glyph matrices. */
10947
10948 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10949 {
10950 int dy = row->height - row->visible_height;
10951 w->vscroll = 0;
10952 w->cursor.y += dy;
10953 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10954 }
10955 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10956 {
10957 int dy = - (row->height - row->visible_height);
10958 w->vscroll = dy;
10959 w->cursor.y += dy;
10960 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10961 }
10962
10963 /* When we change the cursor y-position of the selected window,
10964 change this_line_y as well so that the display optimization for
10965 the cursor line of the selected window in redisplay_internal uses
10966 the correct y-position. */
10967 if (w == XWINDOW (selected_window))
10968 this_line_y = w->cursor.y;
10969
10970 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10971 redisplay with larger matrices. */
10972 if (matrix->nrows < required_matrix_height (w))
10973 {
10974 fonts_changed_p = 1;
10975 return 0;
10976 }
10977
10978 return 1;
10979 #endif /* 0 */
10980 }
10981
10982
10983 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10984 non-zero means only WINDOW is redisplayed in redisplay_internal.
10985 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10986 in redisplay_window to bring a partially visible line into view in
10987 the case that only the cursor has moved.
10988
10989 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10990 last screen line's vertical height extends past the end of the screen.
10991
10992 Value is
10993
10994 1 if scrolling succeeded
10995
10996 0 if scrolling didn't find point.
10997
10998 -1 if new fonts have been loaded so that we must interrupt
10999 redisplay, adjust glyph matrices, and try again. */
11000
11001 enum
11002 {
11003 SCROLLING_SUCCESS,
11004 SCROLLING_FAILED,
11005 SCROLLING_NEED_LARGER_MATRICES
11006 };
11007
11008 static int
11009 try_scrolling (window, just_this_one_p, scroll_conservatively,
11010 scroll_step, temp_scroll_step, last_line_misfit)
11011 Lisp_Object window;
11012 int just_this_one_p;
11013 EMACS_INT scroll_conservatively, scroll_step;
11014 int temp_scroll_step;
11015 int last_line_misfit;
11016 {
11017 struct window *w = XWINDOW (window);
11018 struct frame *f = XFRAME (w->frame);
11019 struct text_pos scroll_margin_pos;
11020 struct text_pos pos;
11021 struct text_pos startp;
11022 struct it it;
11023 Lisp_Object window_end;
11024 int this_scroll_margin;
11025 int dy = 0;
11026 int scroll_max;
11027 int rc;
11028 int amount_to_scroll = 0;
11029 Lisp_Object aggressive;
11030 int height;
11031 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11032
11033 #if GLYPH_DEBUG
11034 debug_method_add (w, "try_scrolling");
11035 #endif
11036
11037 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11038
11039 /* Compute scroll margin height in pixels. We scroll when point is
11040 within this distance from the top or bottom of the window. */
11041 if (scroll_margin > 0)
11042 {
11043 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11044 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11045 }
11046 else
11047 this_scroll_margin = 0;
11048
11049 /* Force scroll_conservatively to have a reasonable value so it doesn't
11050 cause an overflow while computing how much to scroll. */
11051 if (scroll_conservatively)
11052 scroll_conservatively = min (scroll_conservatively,
11053 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11054
11055 /* Compute how much we should try to scroll maximally to bring point
11056 into view. */
11057 if (scroll_step || scroll_conservatively || temp_scroll_step)
11058 scroll_max = max (scroll_step,
11059 max (scroll_conservatively, temp_scroll_step));
11060 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11061 || NUMBERP (current_buffer->scroll_up_aggressively))
11062 /* We're trying to scroll because of aggressive scrolling
11063 but no scroll_step is set. Choose an arbitrary one. Maybe
11064 there should be a variable for this. */
11065 scroll_max = 10;
11066 else
11067 scroll_max = 0;
11068 scroll_max *= FRAME_LINE_HEIGHT (f);
11069
11070 /* Decide whether we have to scroll down. Start at the window end
11071 and move this_scroll_margin up to find the position of the scroll
11072 margin. */
11073 window_end = Fwindow_end (window, Qt);
11074
11075 too_near_end:
11076
11077 CHARPOS (scroll_margin_pos) = XINT (window_end);
11078 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11079
11080 if (this_scroll_margin || extra_scroll_margin_lines)
11081 {
11082 start_display (&it, w, scroll_margin_pos);
11083 if (this_scroll_margin)
11084 move_it_vertically_backward (&it, this_scroll_margin);
11085 if (extra_scroll_margin_lines)
11086 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11087 scroll_margin_pos = it.current.pos;
11088 }
11089
11090 if (PT >= CHARPOS (scroll_margin_pos))
11091 {
11092 int y0;
11093
11094 /* Point is in the scroll margin at the bottom of the window, or
11095 below. Compute a new window start that makes point visible. */
11096
11097 /* Compute the distance from the scroll margin to PT.
11098 Give up if the distance is greater than scroll_max. */
11099 start_display (&it, w, scroll_margin_pos);
11100 y0 = it.current_y;
11101 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11102 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11103
11104 /* To make point visible, we have to move the window start
11105 down so that the line the cursor is in is visible, which
11106 means we have to add in the height of the cursor line. */
11107 dy = line_bottom_y (&it) - y0;
11108
11109 if (dy > scroll_max)
11110 return SCROLLING_FAILED;
11111
11112 /* Move the window start down. If scrolling conservatively,
11113 move it just enough down to make point visible. If
11114 scroll_step is set, move it down by scroll_step. */
11115 start_display (&it, w, startp);
11116
11117 if (scroll_conservatively)
11118 /* Set AMOUNT_TO_SCROLL to at least one line,
11119 and at most scroll_conservatively lines. */
11120 amount_to_scroll
11121 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11122 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11123 else if (scroll_step || temp_scroll_step)
11124 amount_to_scroll = scroll_max;
11125 else
11126 {
11127 aggressive = current_buffer->scroll_up_aggressively;
11128 height = WINDOW_BOX_TEXT_HEIGHT (w);
11129 if (NUMBERP (aggressive))
11130 {
11131 double float_amount = XFLOATINT (aggressive) * height;
11132 amount_to_scroll = float_amount;
11133 if (amount_to_scroll == 0 && float_amount > 0)
11134 amount_to_scroll = 1;
11135 }
11136 }
11137
11138 if (amount_to_scroll <= 0)
11139 return SCROLLING_FAILED;
11140
11141 /* If moving by amount_to_scroll leaves STARTP unchanged,
11142 move it down one screen line. */
11143
11144 move_it_vertically (&it, amount_to_scroll);
11145 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11146 move_it_by_lines (&it, 1, 1);
11147 startp = it.current.pos;
11148 }
11149 else
11150 {
11151 /* See if point is inside the scroll margin at the top of the
11152 window. */
11153 scroll_margin_pos = startp;
11154 if (this_scroll_margin)
11155 {
11156 start_display (&it, w, startp);
11157 move_it_vertically (&it, this_scroll_margin);
11158 scroll_margin_pos = it.current.pos;
11159 }
11160
11161 if (PT < CHARPOS (scroll_margin_pos))
11162 {
11163 /* Point is in the scroll margin at the top of the window or
11164 above what is displayed in the window. */
11165 int y0;
11166
11167 /* Compute the vertical distance from PT to the scroll
11168 margin position. Give up if distance is greater than
11169 scroll_max. */
11170 SET_TEXT_POS (pos, PT, PT_BYTE);
11171 start_display (&it, w, pos);
11172 y0 = it.current_y;
11173 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11174 it.last_visible_y, -1,
11175 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11176 dy = it.current_y - y0;
11177 if (dy > scroll_max)
11178 return SCROLLING_FAILED;
11179
11180 /* Compute new window start. */
11181 start_display (&it, w, startp);
11182
11183 if (scroll_conservatively)
11184 amount_to_scroll
11185 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11186 else if (scroll_step || temp_scroll_step)
11187 amount_to_scroll = scroll_max;
11188 else
11189 {
11190 aggressive = current_buffer->scroll_down_aggressively;
11191 height = WINDOW_BOX_TEXT_HEIGHT (w);
11192 if (NUMBERP (aggressive))
11193 {
11194 double float_amount = XFLOATINT (aggressive) * height;
11195 amount_to_scroll = float_amount;
11196 if (amount_to_scroll == 0 && float_amount > 0)
11197 amount_to_scroll = 1;
11198 }
11199 }
11200
11201 if (amount_to_scroll <= 0)
11202 return SCROLLING_FAILED;
11203
11204 move_it_vertically_backward (&it, amount_to_scroll);
11205 startp = it.current.pos;
11206 }
11207 }
11208
11209 /* Run window scroll functions. */
11210 startp = run_window_scroll_functions (window, startp);
11211
11212 /* Display the window. Give up if new fonts are loaded, or if point
11213 doesn't appear. */
11214 if (!try_window (window, startp))
11215 rc = SCROLLING_NEED_LARGER_MATRICES;
11216 else if (w->cursor.vpos < 0)
11217 {
11218 clear_glyph_matrix (w->desired_matrix);
11219 rc = SCROLLING_FAILED;
11220 }
11221 else
11222 {
11223 /* Maybe forget recorded base line for line number display. */
11224 if (!just_this_one_p
11225 || current_buffer->clip_changed
11226 || BEG_UNCHANGED < CHARPOS (startp))
11227 w->base_line_number = Qnil;
11228
11229 /* If cursor ends up on a partially visible line,
11230 treat that as being off the bottom of the screen. */
11231 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11232 {
11233 clear_glyph_matrix (w->desired_matrix);
11234 ++extra_scroll_margin_lines;
11235 goto too_near_end;
11236 }
11237 rc = SCROLLING_SUCCESS;
11238 }
11239
11240 return rc;
11241 }
11242
11243
11244 /* Compute a suitable window start for window W if display of W starts
11245 on a continuation line. Value is non-zero if a new window start
11246 was computed.
11247
11248 The new window start will be computed, based on W's width, starting
11249 from the start of the continued line. It is the start of the
11250 screen line with the minimum distance from the old start W->start. */
11251
11252 static int
11253 compute_window_start_on_continuation_line (w)
11254 struct window *w;
11255 {
11256 struct text_pos pos, start_pos;
11257 int window_start_changed_p = 0;
11258
11259 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11260
11261 /* If window start is on a continuation line... Window start may be
11262 < BEGV in case there's invisible text at the start of the
11263 buffer (M-x rmail, for example). */
11264 if (CHARPOS (start_pos) > BEGV
11265 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11266 {
11267 struct it it;
11268 struct glyph_row *row;
11269
11270 /* Handle the case that the window start is out of range. */
11271 if (CHARPOS (start_pos) < BEGV)
11272 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11273 else if (CHARPOS (start_pos) > ZV)
11274 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11275
11276 /* Find the start of the continued line. This should be fast
11277 because scan_buffer is fast (newline cache). */
11278 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11279 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11280 row, DEFAULT_FACE_ID);
11281 reseat_at_previous_visible_line_start (&it);
11282
11283 /* If the line start is "too far" away from the window start,
11284 say it takes too much time to compute a new window start. */
11285 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11286 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11287 {
11288 int min_distance, distance;
11289
11290 /* Move forward by display lines to find the new window
11291 start. If window width was enlarged, the new start can
11292 be expected to be > the old start. If window width was
11293 decreased, the new window start will be < the old start.
11294 So, we're looking for the display line start with the
11295 minimum distance from the old window start. */
11296 pos = it.current.pos;
11297 min_distance = INFINITY;
11298 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11299 distance < min_distance)
11300 {
11301 min_distance = distance;
11302 pos = it.current.pos;
11303 move_it_by_lines (&it, 1, 0);
11304 }
11305
11306 /* Set the window start there. */
11307 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11308 window_start_changed_p = 1;
11309 }
11310 }
11311
11312 return window_start_changed_p;
11313 }
11314
11315
11316 /* Try cursor movement in case text has not changed in window WINDOW,
11317 with window start STARTP. Value is
11318
11319 CURSOR_MOVEMENT_SUCCESS if successful
11320
11321 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11322
11323 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11324 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11325 we want to scroll as if scroll-step were set to 1. See the code.
11326
11327 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11328 which case we have to abort this redisplay, and adjust matrices
11329 first. */
11330
11331 enum
11332 {
11333 CURSOR_MOVEMENT_SUCCESS,
11334 CURSOR_MOVEMENT_CANNOT_BE_USED,
11335 CURSOR_MOVEMENT_MUST_SCROLL,
11336 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11337 };
11338
11339 static int
11340 try_cursor_movement (window, startp, scroll_step)
11341 Lisp_Object window;
11342 struct text_pos startp;
11343 int *scroll_step;
11344 {
11345 struct window *w = XWINDOW (window);
11346 struct frame *f = XFRAME (w->frame);
11347 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11348
11349 #if GLYPH_DEBUG
11350 if (inhibit_try_cursor_movement)
11351 return rc;
11352 #endif
11353
11354 /* Handle case where text has not changed, only point, and it has
11355 not moved off the frame. */
11356 if (/* Point may be in this window. */
11357 PT >= CHARPOS (startp)
11358 /* Selective display hasn't changed. */
11359 && !current_buffer->clip_changed
11360 /* Function force-mode-line-update is used to force a thorough
11361 redisplay. It sets either windows_or_buffers_changed or
11362 update_mode_lines. So don't take a shortcut here for these
11363 cases. */
11364 && !update_mode_lines
11365 && !windows_or_buffers_changed
11366 && !cursor_type_changed
11367 /* Can't use this case if highlighting a region. When a
11368 region exists, cursor movement has to do more than just
11369 set the cursor. */
11370 && !(!NILP (Vtransient_mark_mode)
11371 && !NILP (current_buffer->mark_active))
11372 && NILP (w->region_showing)
11373 && NILP (Vshow_trailing_whitespace)
11374 /* Right after splitting windows, last_point may be nil. */
11375 && INTEGERP (w->last_point)
11376 /* This code is not used for mini-buffer for the sake of the case
11377 of redisplaying to replace an echo area message; since in
11378 that case the mini-buffer contents per se are usually
11379 unchanged. This code is of no real use in the mini-buffer
11380 since the handling of this_line_start_pos, etc., in redisplay
11381 handles the same cases. */
11382 && !EQ (window, minibuf_window)
11383 /* When splitting windows or for new windows, it happens that
11384 redisplay is called with a nil window_end_vpos or one being
11385 larger than the window. This should really be fixed in
11386 window.c. I don't have this on my list, now, so we do
11387 approximately the same as the old redisplay code. --gerd. */
11388 && INTEGERP (w->window_end_vpos)
11389 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11390 && (FRAME_WINDOW_P (f)
11391 || !overlay_arrow_in_current_buffer_p ()))
11392 {
11393 int this_scroll_margin, top_scroll_margin;
11394 struct glyph_row *row = NULL;
11395
11396 #if GLYPH_DEBUG
11397 debug_method_add (w, "cursor movement");
11398 #endif
11399
11400 /* Scroll if point within this distance from the top or bottom
11401 of the window. This is a pixel value. */
11402 this_scroll_margin = max (0, scroll_margin);
11403 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11404 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11405
11406 top_scroll_margin = this_scroll_margin;
11407 if (WINDOW_WANTS_HEADER_LINE_P (w))
11408 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11409
11410 /* Start with the row the cursor was displayed during the last
11411 not paused redisplay. Give up if that row is not valid. */
11412 if (w->last_cursor.vpos < 0
11413 || w->last_cursor.vpos >= w->current_matrix->nrows)
11414 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11415 else
11416 {
11417 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11418 if (row->mode_line_p)
11419 ++row;
11420 if (!row->enabled_p)
11421 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11422 }
11423
11424 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11425 {
11426 int scroll_p = 0;
11427 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11428
11429 if (PT > XFASTINT (w->last_point))
11430 {
11431 /* Point has moved forward. */
11432 while (MATRIX_ROW_END_CHARPOS (row) < PT
11433 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11434 {
11435 xassert (row->enabled_p);
11436 ++row;
11437 }
11438
11439 /* The end position of a row equals the start position
11440 of the next row. If PT is there, we would rather
11441 display it in the next line. */
11442 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11443 && MATRIX_ROW_END_CHARPOS (row) == PT
11444 && !cursor_row_p (w, row))
11445 ++row;
11446
11447 /* If within the scroll margin, scroll. Note that
11448 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11449 the next line would be drawn, and that
11450 this_scroll_margin can be zero. */
11451 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11452 || PT > MATRIX_ROW_END_CHARPOS (row)
11453 /* Line is completely visible last line in window
11454 and PT is to be set in the next line. */
11455 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11456 && PT == MATRIX_ROW_END_CHARPOS (row)
11457 && !row->ends_at_zv_p
11458 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11459 scroll_p = 1;
11460 }
11461 else if (PT < XFASTINT (w->last_point))
11462 {
11463 /* Cursor has to be moved backward. Note that PT >=
11464 CHARPOS (startp) because of the outer if-statement. */
11465 while (!row->mode_line_p
11466 && (MATRIX_ROW_START_CHARPOS (row) > PT
11467 || (MATRIX_ROW_START_CHARPOS (row) == PT
11468 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11469 && (row->y > top_scroll_margin
11470 || CHARPOS (startp) == BEGV))
11471 {
11472 xassert (row->enabled_p);
11473 --row;
11474 }
11475
11476 /* Consider the following case: Window starts at BEGV,
11477 there is invisible, intangible text at BEGV, so that
11478 display starts at some point START > BEGV. It can
11479 happen that we are called with PT somewhere between
11480 BEGV and START. Try to handle that case. */
11481 if (row < w->current_matrix->rows
11482 || row->mode_line_p)
11483 {
11484 row = w->current_matrix->rows;
11485 if (row->mode_line_p)
11486 ++row;
11487 }
11488
11489 /* Due to newlines in overlay strings, we may have to
11490 skip forward over overlay strings. */
11491 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11492 && MATRIX_ROW_END_CHARPOS (row) == PT
11493 && !cursor_row_p (w, row))
11494 ++row;
11495
11496 /* If within the scroll margin, scroll. */
11497 if (row->y < top_scroll_margin
11498 && CHARPOS (startp) != BEGV)
11499 scroll_p = 1;
11500 }
11501
11502 if (PT < MATRIX_ROW_START_CHARPOS (row)
11503 || PT > MATRIX_ROW_END_CHARPOS (row))
11504 {
11505 /* if PT is not in the glyph row, give up. */
11506 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11507 }
11508 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11509 && make_cursor_line_fully_visible_p)
11510 {
11511 if (PT == MATRIX_ROW_END_CHARPOS (row)
11512 && !row->ends_at_zv_p
11513 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11514 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11515 else if (row->height > window_box_height (w))
11516 {
11517 /* If we end up in a partially visible line, let's
11518 make it fully visible, except when it's taller
11519 than the window, in which case we can't do much
11520 about it. */
11521 *scroll_step = 1;
11522 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11523 }
11524 else
11525 {
11526 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11527 if (!make_cursor_line_fully_visible (w, 0))
11528 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11529 else
11530 rc = CURSOR_MOVEMENT_SUCCESS;
11531 }
11532 }
11533 else if (scroll_p)
11534 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11535 else
11536 {
11537 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11538 rc = CURSOR_MOVEMENT_SUCCESS;
11539 }
11540 }
11541 }
11542
11543 return rc;
11544 }
11545
11546 void
11547 set_vertical_scroll_bar (w)
11548 struct window *w;
11549 {
11550 int start, end, whole;
11551
11552 /* Calculate the start and end positions for the current window.
11553 At some point, it would be nice to choose between scrollbars
11554 which reflect the whole buffer size, with special markers
11555 indicating narrowing, and scrollbars which reflect only the
11556 visible region.
11557
11558 Note that mini-buffers sometimes aren't displaying any text. */
11559 if (!MINI_WINDOW_P (w)
11560 || (w == XWINDOW (minibuf_window)
11561 && NILP (echo_area_buffer[0])))
11562 {
11563 struct buffer *buf = XBUFFER (w->buffer);
11564 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11565 start = marker_position (w->start) - BUF_BEGV (buf);
11566 /* I don't think this is guaranteed to be right. For the
11567 moment, we'll pretend it is. */
11568 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11569
11570 if (end < start)
11571 end = start;
11572 if (whole < (end - start))
11573 whole = end - start;
11574 }
11575 else
11576 start = end = whole = 0;
11577
11578 /* Indicate what this scroll bar ought to be displaying now. */
11579 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11580 }
11581
11582
11583 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11584 selected_window is redisplayed.
11585
11586 We can return without actually redisplaying the window if
11587 fonts_changed_p is nonzero. In that case, redisplay_internal will
11588 retry. */
11589
11590 static void
11591 redisplay_window (window, just_this_one_p)
11592 Lisp_Object window;
11593 int just_this_one_p;
11594 {
11595 struct window *w = XWINDOW (window);
11596 struct frame *f = XFRAME (w->frame);
11597 struct buffer *buffer = XBUFFER (w->buffer);
11598 struct buffer *old = current_buffer;
11599 struct text_pos lpoint, opoint, startp;
11600 int update_mode_line;
11601 int tem;
11602 struct it it;
11603 /* Record it now because it's overwritten. */
11604 int current_matrix_up_to_date_p = 0;
11605 int used_current_matrix_p = 0;
11606 /* This is less strict than current_matrix_up_to_date_p.
11607 It indictes that the buffer contents and narrowing are unchanged. */
11608 int buffer_unchanged_p = 0;
11609 int temp_scroll_step = 0;
11610 int count = SPECPDL_INDEX ();
11611 int rc;
11612 int centering_position;
11613 int last_line_misfit = 0;
11614
11615 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11616 opoint = lpoint;
11617
11618 /* W must be a leaf window here. */
11619 xassert (!NILP (w->buffer));
11620 #if GLYPH_DEBUG
11621 *w->desired_matrix->method = 0;
11622 #endif
11623
11624 /* Force this to be looked up again for each redisp of each window. */
11625 escape_glyph_face = -1;
11626
11627 specbind (Qinhibit_point_motion_hooks, Qt);
11628
11629 reconsider_clip_changes (w, buffer);
11630
11631 /* Has the mode line to be updated? */
11632 update_mode_line = (!NILP (w->update_mode_line)
11633 || update_mode_lines
11634 || buffer->clip_changed
11635 || buffer->prevent_redisplay_optimizations_p);
11636
11637 if (MINI_WINDOW_P (w))
11638 {
11639 if (w == XWINDOW (echo_area_window)
11640 && !NILP (echo_area_buffer[0]))
11641 {
11642 if (update_mode_line)
11643 /* We may have to update a tty frame's menu bar or a
11644 tool-bar. Example `M-x C-h C-h C-g'. */
11645 goto finish_menu_bars;
11646 else
11647 /* We've already displayed the echo area glyphs in this window. */
11648 goto finish_scroll_bars;
11649 }
11650 else if ((w != XWINDOW (minibuf_window)
11651 || minibuf_level == 0)
11652 /* When buffer is nonempty, redisplay window normally. */
11653 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11654 /* Quail displays non-mini buffers in minibuffer window.
11655 In that case, redisplay the window normally. */
11656 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11657 {
11658 /* W is a mini-buffer window, but it's not active, so clear
11659 it. */
11660 int yb = window_text_bottom_y (w);
11661 struct glyph_row *row;
11662 int y;
11663
11664 for (y = 0, row = w->desired_matrix->rows;
11665 y < yb;
11666 y += row->height, ++row)
11667 blank_row (w, row, y);
11668 goto finish_scroll_bars;
11669 }
11670
11671 clear_glyph_matrix (w->desired_matrix);
11672 }
11673
11674 /* Otherwise set up data on this window; select its buffer and point
11675 value. */
11676 /* Really select the buffer, for the sake of buffer-local
11677 variables. */
11678 set_buffer_internal_1 (XBUFFER (w->buffer));
11679 SET_TEXT_POS (opoint, PT, PT_BYTE);
11680
11681 current_matrix_up_to_date_p
11682 = (!NILP (w->window_end_valid)
11683 && !current_buffer->clip_changed
11684 && !current_buffer->prevent_redisplay_optimizations_p
11685 && XFASTINT (w->last_modified) >= MODIFF
11686 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11687
11688 buffer_unchanged_p
11689 = (!NILP (w->window_end_valid)
11690 && !current_buffer->clip_changed
11691 && XFASTINT (w->last_modified) >= MODIFF
11692 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11693
11694 /* When windows_or_buffers_changed is non-zero, we can't rely on
11695 the window end being valid, so set it to nil there. */
11696 if (windows_or_buffers_changed)
11697 {
11698 /* If window starts on a continuation line, maybe adjust the
11699 window start in case the window's width changed. */
11700 if (XMARKER (w->start)->buffer == current_buffer)
11701 compute_window_start_on_continuation_line (w);
11702
11703 w->window_end_valid = Qnil;
11704 }
11705
11706 /* Some sanity checks. */
11707 CHECK_WINDOW_END (w);
11708 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11709 abort ();
11710 if (BYTEPOS (opoint) < CHARPOS (opoint))
11711 abort ();
11712
11713 /* If %c is in mode line, update it if needed. */
11714 if (!NILP (w->column_number_displayed)
11715 /* This alternative quickly identifies a common case
11716 where no change is needed. */
11717 && !(PT == XFASTINT (w->last_point)
11718 && XFASTINT (w->last_modified) >= MODIFF
11719 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11720 && (XFASTINT (w->column_number_displayed)
11721 != (int) current_column ())) /* iftc */
11722 update_mode_line = 1;
11723
11724 /* Count number of windows showing the selected buffer. An indirect
11725 buffer counts as its base buffer. */
11726 if (!just_this_one_p)
11727 {
11728 struct buffer *current_base, *window_base;
11729 current_base = current_buffer;
11730 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11731 if (current_base->base_buffer)
11732 current_base = current_base->base_buffer;
11733 if (window_base->base_buffer)
11734 window_base = window_base->base_buffer;
11735 if (current_base == window_base)
11736 buffer_shared++;
11737 }
11738
11739 /* Point refers normally to the selected window. For any other
11740 window, set up appropriate value. */
11741 if (!EQ (window, selected_window))
11742 {
11743 int new_pt = XMARKER (w->pointm)->charpos;
11744 int new_pt_byte = marker_byte_position (w->pointm);
11745 if (new_pt < BEGV)
11746 {
11747 new_pt = BEGV;
11748 new_pt_byte = BEGV_BYTE;
11749 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11750 }
11751 else if (new_pt > (ZV - 1))
11752 {
11753 new_pt = ZV;
11754 new_pt_byte = ZV_BYTE;
11755 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11756 }
11757
11758 /* We don't use SET_PT so that the point-motion hooks don't run. */
11759 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11760 }
11761
11762 /* If any of the character widths specified in the display table
11763 have changed, invalidate the width run cache. It's true that
11764 this may be a bit late to catch such changes, but the rest of
11765 redisplay goes (non-fatally) haywire when the display table is
11766 changed, so why should we worry about doing any better? */
11767 if (current_buffer->width_run_cache)
11768 {
11769 struct Lisp_Char_Table *disptab = buffer_display_table ();
11770
11771 if (! disptab_matches_widthtab (disptab,
11772 XVECTOR (current_buffer->width_table)))
11773 {
11774 invalidate_region_cache (current_buffer,
11775 current_buffer->width_run_cache,
11776 BEG, Z);
11777 recompute_width_table (current_buffer, disptab);
11778 }
11779 }
11780
11781 /* If window-start is screwed up, choose a new one. */
11782 if (XMARKER (w->start)->buffer != current_buffer)
11783 goto recenter;
11784
11785 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11786
11787 /* If someone specified a new starting point but did not insist,
11788 check whether it can be used. */
11789 if (!NILP (w->optional_new_start)
11790 && CHARPOS (startp) >= BEGV
11791 && CHARPOS (startp) <= ZV)
11792 {
11793 w->optional_new_start = Qnil;
11794 start_display (&it, w, startp);
11795 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11796 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11797 if (IT_CHARPOS (it) == PT)
11798 w->force_start = Qt;
11799 /* IT may overshoot PT if text at PT is invisible. */
11800 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11801 w->force_start = Qt;
11802
11803
11804 }
11805
11806 /* Handle case where place to start displaying has been specified,
11807 unless the specified location is outside the accessible range. */
11808 if (!NILP (w->force_start)
11809 || w->frozen_window_start_p)
11810 {
11811 /* We set this later on if we have to adjust point. */
11812 int new_vpos = -1;
11813
11814 w->force_start = Qnil;
11815 w->vscroll = 0;
11816 w->window_end_valid = Qnil;
11817
11818 /* Forget any recorded base line for line number display. */
11819 if (!buffer_unchanged_p)
11820 w->base_line_number = Qnil;
11821
11822 /* Redisplay the mode line. Select the buffer properly for that.
11823 Also, run the hook window-scroll-functions
11824 because we have scrolled. */
11825 /* Note, we do this after clearing force_start because
11826 if there's an error, it is better to forget about force_start
11827 than to get into an infinite loop calling the hook functions
11828 and having them get more errors. */
11829 if (!update_mode_line
11830 || ! NILP (Vwindow_scroll_functions))
11831 {
11832 update_mode_line = 1;
11833 w->update_mode_line = Qt;
11834 startp = run_window_scroll_functions (window, startp);
11835 }
11836
11837 w->last_modified = make_number (0);
11838 w->last_overlay_modified = make_number (0);
11839 if (CHARPOS (startp) < BEGV)
11840 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11841 else if (CHARPOS (startp) > ZV)
11842 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11843
11844 /* Redisplay, then check if cursor has been set during the
11845 redisplay. Give up if new fonts were loaded. */
11846 if (!try_window (window, startp))
11847 {
11848 w->force_start = Qt;
11849 clear_glyph_matrix (w->desired_matrix);
11850 goto need_larger_matrices;
11851 }
11852
11853 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11854 {
11855 /* If point does not appear, try to move point so it does
11856 appear. The desired matrix has been built above, so we
11857 can use it here. */
11858 new_vpos = window_box_height (w) / 2;
11859 }
11860
11861 if (!make_cursor_line_fully_visible (w, 0))
11862 {
11863 /* Point does appear, but on a line partly visible at end of window.
11864 Move it back to a fully-visible line. */
11865 new_vpos = window_box_height (w);
11866 }
11867
11868 /* If we need to move point for either of the above reasons,
11869 now actually do it. */
11870 if (new_vpos >= 0)
11871 {
11872 struct glyph_row *row;
11873
11874 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11875 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11876 ++row;
11877
11878 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11879 MATRIX_ROW_START_BYTEPOS (row));
11880
11881 if (w != XWINDOW (selected_window))
11882 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11883 else if (current_buffer == old)
11884 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11885
11886 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11887
11888 /* If we are highlighting the region, then we just changed
11889 the region, so redisplay to show it. */
11890 if (!NILP (Vtransient_mark_mode)
11891 && !NILP (current_buffer->mark_active))
11892 {
11893 clear_glyph_matrix (w->desired_matrix);
11894 if (!try_window (window, startp))
11895 goto need_larger_matrices;
11896 }
11897 }
11898
11899 #if GLYPH_DEBUG
11900 debug_method_add (w, "forced window start");
11901 #endif
11902 goto done;
11903 }
11904
11905 /* Handle case where text has not changed, only point, and it has
11906 not moved off the frame, and we are not retrying after hscroll.
11907 (current_matrix_up_to_date_p is nonzero when retrying.) */
11908 if (current_matrix_up_to_date_p
11909 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11910 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11911 {
11912 switch (rc)
11913 {
11914 case CURSOR_MOVEMENT_SUCCESS:
11915 used_current_matrix_p = 1;
11916 goto done;
11917
11918 #if 0 /* try_cursor_movement never returns this value. */
11919 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11920 goto need_larger_matrices;
11921 #endif
11922
11923 case CURSOR_MOVEMENT_MUST_SCROLL:
11924 goto try_to_scroll;
11925
11926 default:
11927 abort ();
11928 }
11929 }
11930 /* If current starting point was originally the beginning of a line
11931 but no longer is, find a new starting point. */
11932 else if (!NILP (w->start_at_line_beg)
11933 && !(CHARPOS (startp) <= BEGV
11934 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11935 {
11936 #if GLYPH_DEBUG
11937 debug_method_add (w, "recenter 1");
11938 #endif
11939 goto recenter;
11940 }
11941
11942 /* Try scrolling with try_window_id. Value is > 0 if update has
11943 been done, it is -1 if we know that the same window start will
11944 not work. It is 0 if unsuccessful for some other reason. */
11945 else if ((tem = try_window_id (w)) != 0)
11946 {
11947 #if GLYPH_DEBUG
11948 debug_method_add (w, "try_window_id %d", tem);
11949 #endif
11950
11951 if (fonts_changed_p)
11952 goto need_larger_matrices;
11953 if (tem > 0)
11954 goto done;
11955
11956 /* Otherwise try_window_id has returned -1 which means that we
11957 don't want the alternative below this comment to execute. */
11958 }
11959 else if (CHARPOS (startp) >= BEGV
11960 && CHARPOS (startp) <= ZV
11961 && PT >= CHARPOS (startp)
11962 && (CHARPOS (startp) < ZV
11963 /* Avoid starting at end of buffer. */
11964 || CHARPOS (startp) == BEGV
11965 || (XFASTINT (w->last_modified) >= MODIFF
11966 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11967 {
11968 #if GLYPH_DEBUG
11969 debug_method_add (w, "same window start");
11970 #endif
11971
11972 /* Try to redisplay starting at same place as before.
11973 If point has not moved off frame, accept the results. */
11974 if (!current_matrix_up_to_date_p
11975 /* Don't use try_window_reusing_current_matrix in this case
11976 because a window scroll function can have changed the
11977 buffer. */
11978 || !NILP (Vwindow_scroll_functions)
11979 || MINI_WINDOW_P (w)
11980 || !(used_current_matrix_p
11981 = try_window_reusing_current_matrix (w)))
11982 {
11983 IF_DEBUG (debug_method_add (w, "1"));
11984 try_window (window, startp);
11985 }
11986
11987 if (fonts_changed_p)
11988 goto need_larger_matrices;
11989
11990 if (w->cursor.vpos >= 0)
11991 {
11992 if (!just_this_one_p
11993 || current_buffer->clip_changed
11994 || BEG_UNCHANGED < CHARPOS (startp))
11995 /* Forget any recorded base line for line number display. */
11996 w->base_line_number = Qnil;
11997
11998 if (!make_cursor_line_fully_visible (w, 1))
11999 {
12000 clear_glyph_matrix (w->desired_matrix);
12001 last_line_misfit = 1;
12002 }
12003 /* Drop through and scroll. */
12004 else
12005 goto done;
12006 }
12007 else
12008 clear_glyph_matrix (w->desired_matrix);
12009 }
12010
12011 try_to_scroll:
12012
12013 w->last_modified = make_number (0);
12014 w->last_overlay_modified = make_number (0);
12015
12016 /* Redisplay the mode line. Select the buffer properly for that. */
12017 if (!update_mode_line)
12018 {
12019 update_mode_line = 1;
12020 w->update_mode_line = Qt;
12021 }
12022
12023 /* Try to scroll by specified few lines. */
12024 if ((scroll_conservatively
12025 || scroll_step
12026 || temp_scroll_step
12027 || NUMBERP (current_buffer->scroll_up_aggressively)
12028 || NUMBERP (current_buffer->scroll_down_aggressively))
12029 && !current_buffer->clip_changed
12030 && CHARPOS (startp) >= BEGV
12031 && CHARPOS (startp) <= ZV)
12032 {
12033 /* The function returns -1 if new fonts were loaded, 1 if
12034 successful, 0 if not successful. */
12035 int rc = try_scrolling (window, just_this_one_p,
12036 scroll_conservatively,
12037 scroll_step,
12038 temp_scroll_step, last_line_misfit);
12039 switch (rc)
12040 {
12041 case SCROLLING_SUCCESS:
12042 goto done;
12043
12044 case SCROLLING_NEED_LARGER_MATRICES:
12045 goto need_larger_matrices;
12046
12047 case SCROLLING_FAILED:
12048 break;
12049
12050 default:
12051 abort ();
12052 }
12053 }
12054
12055 /* Finally, just choose place to start which centers point */
12056
12057 recenter:
12058 centering_position = window_box_height (w) / 2;
12059
12060 point_at_top:
12061 /* Jump here with centering_position already set to 0. */
12062
12063 #if GLYPH_DEBUG
12064 debug_method_add (w, "recenter");
12065 #endif
12066
12067 /* w->vscroll = 0; */
12068
12069 /* Forget any previously recorded base line for line number display. */
12070 if (!buffer_unchanged_p)
12071 w->base_line_number = Qnil;
12072
12073 /* Move backward half the height of the window. */
12074 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12075 it.current_y = it.last_visible_y;
12076 move_it_vertically_backward (&it, centering_position);
12077 xassert (IT_CHARPOS (it) >= BEGV);
12078
12079 /* The function move_it_vertically_backward may move over more
12080 than the specified y-distance. If it->w is small, e.g. a
12081 mini-buffer window, we may end up in front of the window's
12082 display area. Start displaying at the start of the line
12083 containing PT in this case. */
12084 if (it.current_y <= 0)
12085 {
12086 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12087 move_it_vertically_backward (&it, 0);
12088 xassert (IT_CHARPOS (it) <= PT);
12089 it.current_y = 0;
12090 }
12091
12092 it.current_x = it.hpos = 0;
12093
12094 /* Set startp here explicitly in case that helps avoid an infinite loop
12095 in case the window-scroll-functions functions get errors. */
12096 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12097
12098 /* Run scroll hooks. */
12099 startp = run_window_scroll_functions (window, it.current.pos);
12100
12101 /* Redisplay the window. */
12102 if (!current_matrix_up_to_date_p
12103 || windows_or_buffers_changed
12104 || cursor_type_changed
12105 /* Don't use try_window_reusing_current_matrix in this case
12106 because it can have changed the buffer. */
12107 || !NILP (Vwindow_scroll_functions)
12108 || !just_this_one_p
12109 || MINI_WINDOW_P (w)
12110 || !(used_current_matrix_p
12111 = try_window_reusing_current_matrix (w)))
12112 try_window (window, startp);
12113
12114 /* If new fonts have been loaded (due to fontsets), give up. We
12115 have to start a new redisplay since we need to re-adjust glyph
12116 matrices. */
12117 if (fonts_changed_p)
12118 goto need_larger_matrices;
12119
12120 /* If cursor did not appear assume that the middle of the window is
12121 in the first line of the window. Do it again with the next line.
12122 (Imagine a window of height 100, displaying two lines of height
12123 60. Moving back 50 from it->last_visible_y will end in the first
12124 line.) */
12125 if (w->cursor.vpos < 0)
12126 {
12127 if (!NILP (w->window_end_valid)
12128 && PT >= Z - XFASTINT (w->window_end_pos))
12129 {
12130 clear_glyph_matrix (w->desired_matrix);
12131 move_it_by_lines (&it, 1, 0);
12132 try_window (window, it.current.pos);
12133 }
12134 else if (PT < IT_CHARPOS (it))
12135 {
12136 clear_glyph_matrix (w->desired_matrix);
12137 move_it_by_lines (&it, -1, 0);
12138 try_window (window, it.current.pos);
12139 }
12140 else
12141 {
12142 /* Not much we can do about it. */
12143 }
12144 }
12145
12146 /* Consider the following case: Window starts at BEGV, there is
12147 invisible, intangible text at BEGV, so that display starts at
12148 some point START > BEGV. It can happen that we are called with
12149 PT somewhere between BEGV and START. Try to handle that case. */
12150 if (w->cursor.vpos < 0)
12151 {
12152 struct glyph_row *row = w->current_matrix->rows;
12153 if (row->mode_line_p)
12154 ++row;
12155 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12156 }
12157
12158 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12159 {
12160 /* If vscroll is enabled, disable it and try again. */
12161 if (w->vscroll)
12162 {
12163 w->vscroll = 0;
12164 clear_glyph_matrix (w->desired_matrix);
12165 goto recenter;
12166 }
12167
12168 /* If centering point failed to make the whole line visible,
12169 put point at the top instead. That has to make the whole line
12170 visible, if it can be done. */
12171 clear_glyph_matrix (w->desired_matrix);
12172 centering_position = 0;
12173 goto point_at_top;
12174 }
12175
12176 done:
12177
12178 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12179 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12180 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12181 ? Qt : Qnil);
12182
12183 /* Display the mode line, if we must. */
12184 if ((update_mode_line
12185 /* If window not full width, must redo its mode line
12186 if (a) the window to its side is being redone and
12187 (b) we do a frame-based redisplay. This is a consequence
12188 of how inverted lines are drawn in frame-based redisplay. */
12189 || (!just_this_one_p
12190 && !FRAME_WINDOW_P (f)
12191 && !WINDOW_FULL_WIDTH_P (w))
12192 /* Line number to display. */
12193 || INTEGERP (w->base_line_pos)
12194 /* Column number is displayed and different from the one displayed. */
12195 || (!NILP (w->column_number_displayed)
12196 && (XFASTINT (w->column_number_displayed)
12197 != (int) current_column ()))) /* iftc */
12198 /* This means that the window has a mode line. */
12199 && (WINDOW_WANTS_MODELINE_P (w)
12200 || WINDOW_WANTS_HEADER_LINE_P (w)))
12201 {
12202 display_mode_lines (w);
12203
12204 /* If mode line height has changed, arrange for a thorough
12205 immediate redisplay using the correct mode line height. */
12206 if (WINDOW_WANTS_MODELINE_P (w)
12207 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12208 {
12209 fonts_changed_p = 1;
12210 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12211 = DESIRED_MODE_LINE_HEIGHT (w);
12212 }
12213
12214 /* If top line height has changed, arrange for a thorough
12215 immediate redisplay using the correct mode line height. */
12216 if (WINDOW_WANTS_HEADER_LINE_P (w)
12217 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12218 {
12219 fonts_changed_p = 1;
12220 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12221 = DESIRED_HEADER_LINE_HEIGHT (w);
12222 }
12223
12224 if (fonts_changed_p)
12225 goto need_larger_matrices;
12226 }
12227
12228 if (!line_number_displayed
12229 && !BUFFERP (w->base_line_pos))
12230 {
12231 w->base_line_pos = Qnil;
12232 w->base_line_number = Qnil;
12233 }
12234
12235 finish_menu_bars:
12236
12237 /* When we reach a frame's selected window, redo the frame's menu bar. */
12238 if (update_mode_line
12239 && EQ (FRAME_SELECTED_WINDOW (f), window))
12240 {
12241 int redisplay_menu_p = 0;
12242 int redisplay_tool_bar_p = 0;
12243
12244 if (FRAME_WINDOW_P (f))
12245 {
12246 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12247 || defined (USE_GTK)
12248 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12249 #else
12250 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12251 #endif
12252 }
12253 else
12254 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12255
12256 if (redisplay_menu_p)
12257 display_menu_bar (w);
12258
12259 #ifdef HAVE_WINDOW_SYSTEM
12260 #ifdef USE_GTK
12261 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12262 #else
12263 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12264 && (FRAME_TOOL_BAR_LINES (f) > 0
12265 || auto_resize_tool_bars_p);
12266
12267 #endif
12268
12269 if (redisplay_tool_bar_p)
12270 redisplay_tool_bar (f);
12271 #endif
12272 }
12273
12274 #ifdef HAVE_WINDOW_SYSTEM
12275 if (FRAME_WINDOW_P (f)
12276 && update_window_fringes (w, 0)
12277 && !just_this_one_p
12278 && (used_current_matrix_p || overlay_arrow_seen)
12279 && !w->pseudo_window_p)
12280 {
12281 update_begin (f);
12282 BLOCK_INPUT;
12283 if (draw_window_fringes (w, 1))
12284 x_draw_vertical_border (w);
12285 UNBLOCK_INPUT;
12286 update_end (f);
12287 }
12288 #endif /* HAVE_WINDOW_SYSTEM */
12289
12290 /* We go to this label, with fonts_changed_p nonzero,
12291 if it is necessary to try again using larger glyph matrices.
12292 We have to redeem the scroll bar even in this case,
12293 because the loop in redisplay_internal expects that. */
12294 need_larger_matrices:
12295 ;
12296 finish_scroll_bars:
12297
12298 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12299 {
12300 /* Set the thumb's position and size. */
12301 set_vertical_scroll_bar (w);
12302
12303 /* Note that we actually used the scroll bar attached to this
12304 window, so it shouldn't be deleted at the end of redisplay. */
12305 redeem_scroll_bar_hook (w);
12306 }
12307
12308 /* Restore current_buffer and value of point in it. */
12309 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12310 set_buffer_internal_1 (old);
12311 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12312
12313 unbind_to (count, Qnil);
12314 }
12315
12316
12317 /* Build the complete desired matrix of WINDOW with a window start
12318 buffer position POS. Value is non-zero if successful. It is zero
12319 if fonts were loaded during redisplay which makes re-adjusting
12320 glyph matrices necessary. */
12321
12322 int
12323 try_window (window, pos)
12324 Lisp_Object window;
12325 struct text_pos pos;
12326 {
12327 struct window *w = XWINDOW (window);
12328 struct it it;
12329 struct glyph_row *last_text_row = NULL;
12330
12331 /* Make POS the new window start. */
12332 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12333
12334 /* Mark cursor position as unknown. No overlay arrow seen. */
12335 w->cursor.vpos = -1;
12336 overlay_arrow_seen = 0;
12337
12338 /* Initialize iterator and info to start at POS. */
12339 start_display (&it, w, pos);
12340
12341 /* Display all lines of W. */
12342 while (it.current_y < it.last_visible_y)
12343 {
12344 if (display_line (&it))
12345 last_text_row = it.glyph_row - 1;
12346 if (fonts_changed_p)
12347 return 0;
12348 }
12349
12350 /* If bottom moved off end of frame, change mode line percentage. */
12351 if (XFASTINT (w->window_end_pos) <= 0
12352 && Z != IT_CHARPOS (it))
12353 w->update_mode_line = Qt;
12354
12355 /* Set window_end_pos to the offset of the last character displayed
12356 on the window from the end of current_buffer. Set
12357 window_end_vpos to its row number. */
12358 if (last_text_row)
12359 {
12360 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12361 w->window_end_bytepos
12362 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12363 w->window_end_pos
12364 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12365 w->window_end_vpos
12366 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12367 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12368 ->displays_text_p);
12369 }
12370 else
12371 {
12372 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12373 w->window_end_pos = make_number (Z - ZV);
12374 w->window_end_vpos = make_number (0);
12375 }
12376
12377 /* But that is not valid info until redisplay finishes. */
12378 w->window_end_valid = Qnil;
12379 return 1;
12380 }
12381
12382
12383 \f
12384 /************************************************************************
12385 Window redisplay reusing current matrix when buffer has not changed
12386 ************************************************************************/
12387
12388 /* Try redisplay of window W showing an unchanged buffer with a
12389 different window start than the last time it was displayed by
12390 reusing its current matrix. Value is non-zero if successful.
12391 W->start is the new window start. */
12392
12393 static int
12394 try_window_reusing_current_matrix (w)
12395 struct window *w;
12396 {
12397 struct frame *f = XFRAME (w->frame);
12398 struct glyph_row *row, *bottom_row;
12399 struct it it;
12400 struct run run;
12401 struct text_pos start, new_start;
12402 int nrows_scrolled, i;
12403 struct glyph_row *last_text_row;
12404 struct glyph_row *last_reused_text_row;
12405 struct glyph_row *start_row;
12406 int start_vpos, min_y, max_y;
12407
12408 #if GLYPH_DEBUG
12409 if (inhibit_try_window_reusing)
12410 return 0;
12411 #endif
12412
12413 if (/* This function doesn't handle terminal frames. */
12414 !FRAME_WINDOW_P (f)
12415 /* Don't try to reuse the display if windows have been split
12416 or such. */
12417 || windows_or_buffers_changed
12418 || cursor_type_changed)
12419 return 0;
12420
12421 /* Can't do this if region may have changed. */
12422 if ((!NILP (Vtransient_mark_mode)
12423 && !NILP (current_buffer->mark_active))
12424 || !NILP (w->region_showing)
12425 || !NILP (Vshow_trailing_whitespace))
12426 return 0;
12427
12428 /* If top-line visibility has changed, give up. */
12429 if (WINDOW_WANTS_HEADER_LINE_P (w)
12430 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12431 return 0;
12432
12433 /* Give up if old or new display is scrolled vertically. We could
12434 make this function handle this, but right now it doesn't. */
12435 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12436 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12437 return 0;
12438
12439 /* The variable new_start now holds the new window start. The old
12440 start `start' can be determined from the current matrix. */
12441 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12442 start = start_row->start.pos;
12443 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12444
12445 /* Clear the desired matrix for the display below. */
12446 clear_glyph_matrix (w->desired_matrix);
12447
12448 if (CHARPOS (new_start) <= CHARPOS (start))
12449 {
12450 int first_row_y;
12451
12452 /* Don't use this method if the display starts with an ellipsis
12453 displayed for invisible text. It's not easy to handle that case
12454 below, and it's certainly not worth the effort since this is
12455 not a frequent case. */
12456 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12457 return 0;
12458
12459 IF_DEBUG (debug_method_add (w, "twu1"));
12460
12461 /* Display up to a row that can be reused. The variable
12462 last_text_row is set to the last row displayed that displays
12463 text. Note that it.vpos == 0 if or if not there is a
12464 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12465 start_display (&it, w, new_start);
12466 first_row_y = it.current_y;
12467 w->cursor.vpos = -1;
12468 last_text_row = last_reused_text_row = NULL;
12469
12470 while (it.current_y < it.last_visible_y
12471 && !fonts_changed_p)
12472 {
12473 /* If we have reached into the characters in the START row,
12474 that means the line boundaries have changed. So we
12475 can't start copying with the row START. Maybe it will
12476 work to start copying with the following row. */
12477 while (IT_CHARPOS (it) > CHARPOS (start))
12478 {
12479 /* Advance to the next row as the "start". */
12480 start_row++;
12481 start = start_row->start.pos;
12482 /* If there are no more rows to try, or just one, give up. */
12483 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12484 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12485 || CHARPOS (start) == ZV)
12486 {
12487 clear_glyph_matrix (w->desired_matrix);
12488 return 0;
12489 }
12490
12491 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12492 }
12493 /* If we have reached alignment,
12494 we can copy the rest of the rows. */
12495 if (IT_CHARPOS (it) == CHARPOS (start))
12496 break;
12497
12498 if (display_line (&it))
12499 last_text_row = it.glyph_row - 1;
12500 }
12501
12502 /* A value of current_y < last_visible_y means that we stopped
12503 at the previous window start, which in turn means that we
12504 have at least one reusable row. */
12505 if (it.current_y < it.last_visible_y)
12506 {
12507 /* IT.vpos always starts from 0; it counts text lines. */
12508 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12509
12510 /* Find PT if not already found in the lines displayed. */
12511 if (w->cursor.vpos < 0)
12512 {
12513 int dy = it.current_y - start_row->y;
12514
12515 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12516 row = row_containing_pos (w, PT, row, NULL, dy);
12517 if (row)
12518 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12519 dy, nrows_scrolled);
12520 else
12521 {
12522 clear_glyph_matrix (w->desired_matrix);
12523 return 0;
12524 }
12525 }
12526
12527 /* Scroll the display. Do it before the current matrix is
12528 changed. The problem here is that update has not yet
12529 run, i.e. part of the current matrix is not up to date.
12530 scroll_run_hook will clear the cursor, and use the
12531 current matrix to get the height of the row the cursor is
12532 in. */
12533 run.current_y = start_row->y;
12534 run.desired_y = it.current_y;
12535 run.height = it.last_visible_y - it.current_y;
12536
12537 if (run.height > 0 && run.current_y != run.desired_y)
12538 {
12539 update_begin (f);
12540 rif->update_window_begin_hook (w);
12541 rif->clear_window_mouse_face (w);
12542 rif->scroll_run_hook (w, &run);
12543 rif->update_window_end_hook (w, 0, 0);
12544 update_end (f);
12545 }
12546
12547 /* Shift current matrix down by nrows_scrolled lines. */
12548 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12549 rotate_matrix (w->current_matrix,
12550 start_vpos,
12551 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12552 nrows_scrolled);
12553
12554 /* Disable lines that must be updated. */
12555 for (i = 0; i < it.vpos; ++i)
12556 (start_row + i)->enabled_p = 0;
12557
12558 /* Re-compute Y positions. */
12559 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12560 max_y = it.last_visible_y;
12561 for (row = start_row + nrows_scrolled;
12562 row < bottom_row;
12563 ++row)
12564 {
12565 row->y = it.current_y;
12566 row->visible_height = row->height;
12567
12568 if (row->y < min_y)
12569 row->visible_height -= min_y - row->y;
12570 if (row->y + row->height > max_y)
12571 row->visible_height -= row->y + row->height - max_y;
12572 row->redraw_fringe_bitmaps_p = 1;
12573
12574 it.current_y += row->height;
12575
12576 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12577 last_reused_text_row = row;
12578 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12579 break;
12580 }
12581
12582 /* Disable lines in the current matrix which are now
12583 below the window. */
12584 for (++row; row < bottom_row; ++row)
12585 row->enabled_p = 0;
12586 }
12587
12588 /* Update window_end_pos etc.; last_reused_text_row is the last
12589 reused row from the current matrix containing text, if any.
12590 The value of last_text_row is the last displayed line
12591 containing text. */
12592 if (last_reused_text_row)
12593 {
12594 w->window_end_bytepos
12595 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12596 w->window_end_pos
12597 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12598 w->window_end_vpos
12599 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12600 w->current_matrix));
12601 }
12602 else if (last_text_row)
12603 {
12604 w->window_end_bytepos
12605 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12606 w->window_end_pos
12607 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12608 w->window_end_vpos
12609 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12610 }
12611 else
12612 {
12613 /* This window must be completely empty. */
12614 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12615 w->window_end_pos = make_number (Z - ZV);
12616 w->window_end_vpos = make_number (0);
12617 }
12618 w->window_end_valid = Qnil;
12619
12620 /* Update hint: don't try scrolling again in update_window. */
12621 w->desired_matrix->no_scrolling_p = 1;
12622
12623 #if GLYPH_DEBUG
12624 debug_method_add (w, "try_window_reusing_current_matrix 1");
12625 #endif
12626 return 1;
12627 }
12628 else if (CHARPOS (new_start) > CHARPOS (start))
12629 {
12630 struct glyph_row *pt_row, *row;
12631 struct glyph_row *first_reusable_row;
12632 struct glyph_row *first_row_to_display;
12633 int dy;
12634 int yb = window_text_bottom_y (w);
12635
12636 /* Find the row starting at new_start, if there is one. Don't
12637 reuse a partially visible line at the end. */
12638 first_reusable_row = start_row;
12639 while (first_reusable_row->enabled_p
12640 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12641 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12642 < CHARPOS (new_start)))
12643 ++first_reusable_row;
12644
12645 /* Give up if there is no row to reuse. */
12646 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12647 || !first_reusable_row->enabled_p
12648 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12649 != CHARPOS (new_start)))
12650 return 0;
12651
12652 /* We can reuse fully visible rows beginning with
12653 first_reusable_row to the end of the window. Set
12654 first_row_to_display to the first row that cannot be reused.
12655 Set pt_row to the row containing point, if there is any. */
12656 pt_row = NULL;
12657 for (first_row_to_display = first_reusable_row;
12658 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12659 ++first_row_to_display)
12660 {
12661 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12662 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12663 pt_row = first_row_to_display;
12664 }
12665
12666 /* Start displaying at the start of first_row_to_display. */
12667 xassert (first_row_to_display->y < yb);
12668 init_to_row_start (&it, w, first_row_to_display);
12669
12670 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12671 - start_vpos);
12672 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12673 - nrows_scrolled);
12674 it.current_y = (first_row_to_display->y - first_reusable_row->y
12675 + WINDOW_HEADER_LINE_HEIGHT (w));
12676
12677 /* Display lines beginning with first_row_to_display in the
12678 desired matrix. Set last_text_row to the last row displayed
12679 that displays text. */
12680 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12681 if (pt_row == NULL)
12682 w->cursor.vpos = -1;
12683 last_text_row = NULL;
12684 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12685 if (display_line (&it))
12686 last_text_row = it.glyph_row - 1;
12687
12688 /* Give up If point isn't in a row displayed or reused. */
12689 if (w->cursor.vpos < 0)
12690 {
12691 clear_glyph_matrix (w->desired_matrix);
12692 return 0;
12693 }
12694
12695 /* If point is in a reused row, adjust y and vpos of the cursor
12696 position. */
12697 if (pt_row)
12698 {
12699 w->cursor.vpos -= nrows_scrolled;
12700 w->cursor.y -= first_reusable_row->y - start_row->y;
12701 }
12702
12703 /* Scroll the display. */
12704 run.current_y = first_reusable_row->y;
12705 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12706 run.height = it.last_visible_y - run.current_y;
12707 dy = run.current_y - run.desired_y;
12708
12709 if (run.height)
12710 {
12711 update_begin (f);
12712 rif->update_window_begin_hook (w);
12713 rif->clear_window_mouse_face (w);
12714 rif->scroll_run_hook (w, &run);
12715 rif->update_window_end_hook (w, 0, 0);
12716 update_end (f);
12717 }
12718
12719 /* Adjust Y positions of reused rows. */
12720 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12721 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12722 max_y = it.last_visible_y;
12723 for (row = first_reusable_row; row < first_row_to_display; ++row)
12724 {
12725 row->y -= dy;
12726 row->visible_height = row->height;
12727 if (row->y < min_y)
12728 row->visible_height -= min_y - row->y;
12729 if (row->y + row->height > max_y)
12730 row->visible_height -= row->y + row->height - max_y;
12731 row->redraw_fringe_bitmaps_p = 1;
12732 }
12733
12734 /* Scroll the current matrix. */
12735 xassert (nrows_scrolled > 0);
12736 rotate_matrix (w->current_matrix,
12737 start_vpos,
12738 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12739 -nrows_scrolled);
12740
12741 /* Disable rows not reused. */
12742 for (row -= nrows_scrolled; row < bottom_row; ++row)
12743 row->enabled_p = 0;
12744
12745 /* Point may have moved to a different line, so we cannot assume that
12746 the previous cursor position is valid; locate the correct row. */
12747 if (pt_row)
12748 {
12749 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12750 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12751 row++)
12752 {
12753 w->cursor.vpos++;
12754 w->cursor.y = row->y;
12755 }
12756 if (row < bottom_row)
12757 {
12758 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12759 while (glyph->charpos < PT)
12760 {
12761 w->cursor.hpos++;
12762 w->cursor.x += glyph->pixel_width;
12763 glyph++;
12764 }
12765 }
12766 }
12767
12768 /* Adjust window end. A null value of last_text_row means that
12769 the window end is in reused rows which in turn means that
12770 only its vpos can have changed. */
12771 if (last_text_row)
12772 {
12773 w->window_end_bytepos
12774 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12775 w->window_end_pos
12776 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12777 w->window_end_vpos
12778 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12779 }
12780 else
12781 {
12782 w->window_end_vpos
12783 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12784 }
12785
12786 w->window_end_valid = Qnil;
12787 w->desired_matrix->no_scrolling_p = 1;
12788
12789 #if GLYPH_DEBUG
12790 debug_method_add (w, "try_window_reusing_current_matrix 2");
12791 #endif
12792 return 1;
12793 }
12794
12795 return 0;
12796 }
12797
12798
12799 \f
12800 /************************************************************************
12801 Window redisplay reusing current matrix when buffer has changed
12802 ************************************************************************/
12803
12804 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12805 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12806 int *, int *));
12807 static struct glyph_row *
12808 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12809 struct glyph_row *));
12810
12811
12812 /* Return the last row in MATRIX displaying text. If row START is
12813 non-null, start searching with that row. IT gives the dimensions
12814 of the display. Value is null if matrix is empty; otherwise it is
12815 a pointer to the row found. */
12816
12817 static struct glyph_row *
12818 find_last_row_displaying_text (matrix, it, start)
12819 struct glyph_matrix *matrix;
12820 struct it *it;
12821 struct glyph_row *start;
12822 {
12823 struct glyph_row *row, *row_found;
12824
12825 /* Set row_found to the last row in IT->w's current matrix
12826 displaying text. The loop looks funny but think of partially
12827 visible lines. */
12828 row_found = NULL;
12829 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12830 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12831 {
12832 xassert (row->enabled_p);
12833 row_found = row;
12834 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12835 break;
12836 ++row;
12837 }
12838
12839 return row_found;
12840 }
12841
12842
12843 /* Return the last row in the current matrix of W that is not affected
12844 by changes at the start of current_buffer that occurred since W's
12845 current matrix was built. Value is null if no such row exists.
12846
12847 BEG_UNCHANGED us the number of characters unchanged at the start of
12848 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12849 first changed character in current_buffer. Characters at positions <
12850 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12851 when the current matrix was built. */
12852
12853 static struct glyph_row *
12854 find_last_unchanged_at_beg_row (w)
12855 struct window *w;
12856 {
12857 int first_changed_pos = BEG + BEG_UNCHANGED;
12858 struct glyph_row *row;
12859 struct glyph_row *row_found = NULL;
12860 int yb = window_text_bottom_y (w);
12861
12862 /* Find the last row displaying unchanged text. */
12863 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12864 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12865 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12866 {
12867 if (/* If row ends before first_changed_pos, it is unchanged,
12868 except in some case. */
12869 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12870 /* When row ends in ZV and we write at ZV it is not
12871 unchanged. */
12872 && !row->ends_at_zv_p
12873 /* When first_changed_pos is the end of a continued line,
12874 row is not unchanged because it may be no longer
12875 continued. */
12876 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12877 && (row->continued_p
12878 || row->exact_window_width_line_p)))
12879 row_found = row;
12880
12881 /* Stop if last visible row. */
12882 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12883 break;
12884
12885 ++row;
12886 }
12887
12888 return row_found;
12889 }
12890
12891
12892 /* Find the first glyph row in the current matrix of W that is not
12893 affected by changes at the end of current_buffer since the
12894 time W's current matrix was built.
12895
12896 Return in *DELTA the number of chars by which buffer positions in
12897 unchanged text at the end of current_buffer must be adjusted.
12898
12899 Return in *DELTA_BYTES the corresponding number of bytes.
12900
12901 Value is null if no such row exists, i.e. all rows are affected by
12902 changes. */
12903
12904 static struct glyph_row *
12905 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12906 struct window *w;
12907 int *delta, *delta_bytes;
12908 {
12909 struct glyph_row *row;
12910 struct glyph_row *row_found = NULL;
12911
12912 *delta = *delta_bytes = 0;
12913
12914 /* Display must not have been paused, otherwise the current matrix
12915 is not up to date. */
12916 if (NILP (w->window_end_valid))
12917 abort ();
12918
12919 /* A value of window_end_pos >= END_UNCHANGED means that the window
12920 end is in the range of changed text. If so, there is no
12921 unchanged row at the end of W's current matrix. */
12922 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12923 return NULL;
12924
12925 /* Set row to the last row in W's current matrix displaying text. */
12926 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12927
12928 /* If matrix is entirely empty, no unchanged row exists. */
12929 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12930 {
12931 /* The value of row is the last glyph row in the matrix having a
12932 meaningful buffer position in it. The end position of row
12933 corresponds to window_end_pos. This allows us to translate
12934 buffer positions in the current matrix to current buffer
12935 positions for characters not in changed text. */
12936 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12937 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12938 int last_unchanged_pos, last_unchanged_pos_old;
12939 struct glyph_row *first_text_row
12940 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12941
12942 *delta = Z - Z_old;
12943 *delta_bytes = Z_BYTE - Z_BYTE_old;
12944
12945 /* Set last_unchanged_pos to the buffer position of the last
12946 character in the buffer that has not been changed. Z is the
12947 index + 1 of the last character in current_buffer, i.e. by
12948 subtracting END_UNCHANGED we get the index of the last
12949 unchanged character, and we have to add BEG to get its buffer
12950 position. */
12951 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12952 last_unchanged_pos_old = last_unchanged_pos - *delta;
12953
12954 /* Search backward from ROW for a row displaying a line that
12955 starts at a minimum position >= last_unchanged_pos_old. */
12956 for (; row > first_text_row; --row)
12957 {
12958 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12959 abort ();
12960
12961 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12962 row_found = row;
12963 }
12964 }
12965
12966 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12967 abort ();
12968
12969 return row_found;
12970 }
12971
12972
12973 /* Make sure that glyph rows in the current matrix of window W
12974 reference the same glyph memory as corresponding rows in the
12975 frame's frame matrix. This function is called after scrolling W's
12976 current matrix on a terminal frame in try_window_id and
12977 try_window_reusing_current_matrix. */
12978
12979 static void
12980 sync_frame_with_window_matrix_rows (w)
12981 struct window *w;
12982 {
12983 struct frame *f = XFRAME (w->frame);
12984 struct glyph_row *window_row, *window_row_end, *frame_row;
12985
12986 /* Preconditions: W must be a leaf window and full-width. Its frame
12987 must have a frame matrix. */
12988 xassert (NILP (w->hchild) && NILP (w->vchild));
12989 xassert (WINDOW_FULL_WIDTH_P (w));
12990 xassert (!FRAME_WINDOW_P (f));
12991
12992 /* If W is a full-width window, glyph pointers in W's current matrix
12993 have, by definition, to be the same as glyph pointers in the
12994 corresponding frame matrix. Note that frame matrices have no
12995 marginal areas (see build_frame_matrix). */
12996 window_row = w->current_matrix->rows;
12997 window_row_end = window_row + w->current_matrix->nrows;
12998 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12999 while (window_row < window_row_end)
13000 {
13001 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13002 struct glyph *end = window_row->glyphs[LAST_AREA];
13003
13004 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13005 frame_row->glyphs[TEXT_AREA] = start;
13006 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13007 frame_row->glyphs[LAST_AREA] = end;
13008
13009 /* Disable frame rows whose corresponding window rows have
13010 been disabled in try_window_id. */
13011 if (!window_row->enabled_p)
13012 frame_row->enabled_p = 0;
13013
13014 ++window_row, ++frame_row;
13015 }
13016 }
13017
13018
13019 /* Find the glyph row in window W containing CHARPOS. Consider all
13020 rows between START and END (not inclusive). END null means search
13021 all rows to the end of the display area of W. Value is the row
13022 containing CHARPOS or null. */
13023
13024 struct glyph_row *
13025 row_containing_pos (w, charpos, start, end, dy)
13026 struct window *w;
13027 int charpos;
13028 struct glyph_row *start, *end;
13029 int dy;
13030 {
13031 struct glyph_row *row = start;
13032 int last_y;
13033
13034 /* If we happen to start on a header-line, skip that. */
13035 if (row->mode_line_p)
13036 ++row;
13037
13038 if ((end && row >= end) || !row->enabled_p)
13039 return NULL;
13040
13041 last_y = window_text_bottom_y (w) - dy;
13042
13043 while (1)
13044 {
13045 /* Give up if we have gone too far. */
13046 if (end && row >= end)
13047 return NULL;
13048 /* This formerly returned if they were equal.
13049 I think that both quantities are of a "last plus one" type;
13050 if so, when they are equal, the row is within the screen. -- rms. */
13051 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13052 return NULL;
13053
13054 /* If it is in this row, return this row. */
13055 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13056 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13057 /* The end position of a row equals the start
13058 position of the next row. If CHARPOS is there, we
13059 would rather display it in the next line, except
13060 when this line ends in ZV. */
13061 && !row->ends_at_zv_p
13062 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13063 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13064 return row;
13065 ++row;
13066 }
13067 }
13068
13069
13070 /* Try to redisplay window W by reusing its existing display. W's
13071 current matrix must be up to date when this function is called,
13072 i.e. window_end_valid must not be nil.
13073
13074 Value is
13075
13076 1 if display has been updated
13077 0 if otherwise unsuccessful
13078 -1 if redisplay with same window start is known not to succeed
13079
13080 The following steps are performed:
13081
13082 1. Find the last row in the current matrix of W that is not
13083 affected by changes at the start of current_buffer. If no such row
13084 is found, give up.
13085
13086 2. Find the first row in W's current matrix that is not affected by
13087 changes at the end of current_buffer. Maybe there is no such row.
13088
13089 3. Display lines beginning with the row + 1 found in step 1 to the
13090 row found in step 2 or, if step 2 didn't find a row, to the end of
13091 the window.
13092
13093 4. If cursor is not known to appear on the window, give up.
13094
13095 5. If display stopped at the row found in step 2, scroll the
13096 display and current matrix as needed.
13097
13098 6. Maybe display some lines at the end of W, if we must. This can
13099 happen under various circumstances, like a partially visible line
13100 becoming fully visible, or because newly displayed lines are displayed
13101 in smaller font sizes.
13102
13103 7. Update W's window end information. */
13104
13105 static int
13106 try_window_id (w)
13107 struct window *w;
13108 {
13109 struct frame *f = XFRAME (w->frame);
13110 struct glyph_matrix *current_matrix = w->current_matrix;
13111 struct glyph_matrix *desired_matrix = w->desired_matrix;
13112 struct glyph_row *last_unchanged_at_beg_row;
13113 struct glyph_row *first_unchanged_at_end_row;
13114 struct glyph_row *row;
13115 struct glyph_row *bottom_row;
13116 int bottom_vpos;
13117 struct it it;
13118 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13119 struct text_pos start_pos;
13120 struct run run;
13121 int first_unchanged_at_end_vpos = 0;
13122 struct glyph_row *last_text_row, *last_text_row_at_end;
13123 struct text_pos start;
13124 int first_changed_charpos, last_changed_charpos;
13125
13126 #if GLYPH_DEBUG
13127 if (inhibit_try_window_id)
13128 return 0;
13129 #endif
13130
13131 /* This is handy for debugging. */
13132 #if 0
13133 #define GIVE_UP(X) \
13134 do { \
13135 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13136 return 0; \
13137 } while (0)
13138 #else
13139 #define GIVE_UP(X) return 0
13140 #endif
13141
13142 SET_TEXT_POS_FROM_MARKER (start, w->start);
13143
13144 /* Don't use this for mini-windows because these can show
13145 messages and mini-buffers, and we don't handle that here. */
13146 if (MINI_WINDOW_P (w))
13147 GIVE_UP (1);
13148
13149 /* This flag is used to prevent redisplay optimizations. */
13150 if (windows_or_buffers_changed || cursor_type_changed)
13151 GIVE_UP (2);
13152
13153 /* Verify that narrowing has not changed.
13154 Also verify that we were not told to prevent redisplay optimizations.
13155 It would be nice to further
13156 reduce the number of cases where this prevents try_window_id. */
13157 if (current_buffer->clip_changed
13158 || current_buffer->prevent_redisplay_optimizations_p)
13159 GIVE_UP (3);
13160
13161 /* Window must either use window-based redisplay or be full width. */
13162 if (!FRAME_WINDOW_P (f)
13163 && (!line_ins_del_ok
13164 || !WINDOW_FULL_WIDTH_P (w)))
13165 GIVE_UP (4);
13166
13167 /* Give up if point is not known NOT to appear in W. */
13168 if (PT < CHARPOS (start))
13169 GIVE_UP (5);
13170
13171 /* Another way to prevent redisplay optimizations. */
13172 if (XFASTINT (w->last_modified) == 0)
13173 GIVE_UP (6);
13174
13175 /* Verify that window is not hscrolled. */
13176 if (XFASTINT (w->hscroll) != 0)
13177 GIVE_UP (7);
13178
13179 /* Verify that display wasn't paused. */
13180 if (NILP (w->window_end_valid))
13181 GIVE_UP (8);
13182
13183 /* Can't use this if highlighting a region because a cursor movement
13184 will do more than just set the cursor. */
13185 if (!NILP (Vtransient_mark_mode)
13186 && !NILP (current_buffer->mark_active))
13187 GIVE_UP (9);
13188
13189 /* Likewise if highlighting trailing whitespace. */
13190 if (!NILP (Vshow_trailing_whitespace))
13191 GIVE_UP (11);
13192
13193 /* Likewise if showing a region. */
13194 if (!NILP (w->region_showing))
13195 GIVE_UP (10);
13196
13197 /* Can use this if overlay arrow position and or string have changed. */
13198 if (overlay_arrows_changed_p ())
13199 GIVE_UP (12);
13200
13201
13202 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13203 only if buffer has really changed. The reason is that the gap is
13204 initially at Z for freshly visited files. The code below would
13205 set end_unchanged to 0 in that case. */
13206 if (MODIFF > SAVE_MODIFF
13207 /* This seems to happen sometimes after saving a buffer. */
13208 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13209 {
13210 if (GPT - BEG < BEG_UNCHANGED)
13211 BEG_UNCHANGED = GPT - BEG;
13212 if (Z - GPT < END_UNCHANGED)
13213 END_UNCHANGED = Z - GPT;
13214 }
13215
13216 /* The position of the first and last character that has been changed. */
13217 first_changed_charpos = BEG + BEG_UNCHANGED;
13218 last_changed_charpos = Z - END_UNCHANGED;
13219
13220 /* If window starts after a line end, and the last change is in
13221 front of that newline, then changes don't affect the display.
13222 This case happens with stealth-fontification. Note that although
13223 the display is unchanged, glyph positions in the matrix have to
13224 be adjusted, of course. */
13225 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13226 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13227 && ((last_changed_charpos < CHARPOS (start)
13228 && CHARPOS (start) == BEGV)
13229 || (last_changed_charpos < CHARPOS (start) - 1
13230 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13231 {
13232 int Z_old, delta, Z_BYTE_old, delta_bytes;
13233 struct glyph_row *r0;
13234
13235 /* Compute how many chars/bytes have been added to or removed
13236 from the buffer. */
13237 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13238 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13239 delta = Z - Z_old;
13240 delta_bytes = Z_BYTE - Z_BYTE_old;
13241
13242 /* Give up if PT is not in the window. Note that it already has
13243 been checked at the start of try_window_id that PT is not in
13244 front of the window start. */
13245 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13246 GIVE_UP (13);
13247
13248 /* If window start is unchanged, we can reuse the whole matrix
13249 as is, after adjusting glyph positions. No need to compute
13250 the window end again, since its offset from Z hasn't changed. */
13251 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13252 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13253 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13254 /* PT must not be in a partially visible line. */
13255 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13256 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13257 {
13258 /* Adjust positions in the glyph matrix. */
13259 if (delta || delta_bytes)
13260 {
13261 struct glyph_row *r1
13262 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13263 increment_matrix_positions (w->current_matrix,
13264 MATRIX_ROW_VPOS (r0, current_matrix),
13265 MATRIX_ROW_VPOS (r1, current_matrix),
13266 delta, delta_bytes);
13267 }
13268
13269 /* Set the cursor. */
13270 row = row_containing_pos (w, PT, r0, NULL, 0);
13271 if (row)
13272 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13273 else
13274 abort ();
13275 return 1;
13276 }
13277 }
13278
13279 /* Handle the case that changes are all below what is displayed in
13280 the window, and that PT is in the window. This shortcut cannot
13281 be taken if ZV is visible in the window, and text has been added
13282 there that is visible in the window. */
13283 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13284 /* ZV is not visible in the window, or there are no
13285 changes at ZV, actually. */
13286 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13287 || first_changed_charpos == last_changed_charpos))
13288 {
13289 struct glyph_row *r0;
13290
13291 /* Give up if PT is not in the window. Note that it already has
13292 been checked at the start of try_window_id that PT is not in
13293 front of the window start. */
13294 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13295 GIVE_UP (14);
13296
13297 /* If window start is unchanged, we can reuse the whole matrix
13298 as is, without changing glyph positions since no text has
13299 been added/removed in front of the window end. */
13300 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13301 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13302 /* PT must not be in a partially visible line. */
13303 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13304 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13305 {
13306 /* We have to compute the window end anew since text
13307 can have been added/removed after it. */
13308 w->window_end_pos
13309 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13310 w->window_end_bytepos
13311 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13312
13313 /* Set the cursor. */
13314 row = row_containing_pos (w, PT, r0, NULL, 0);
13315 if (row)
13316 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13317 else
13318 abort ();
13319 return 2;
13320 }
13321 }
13322
13323 /* Give up if window start is in the changed area.
13324
13325 The condition used to read
13326
13327 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13328
13329 but why that was tested escapes me at the moment. */
13330 if (CHARPOS (start) >= first_changed_charpos
13331 && CHARPOS (start) <= last_changed_charpos)
13332 GIVE_UP (15);
13333
13334 /* Check that window start agrees with the start of the first glyph
13335 row in its current matrix. Check this after we know the window
13336 start is not in changed text, otherwise positions would not be
13337 comparable. */
13338 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13339 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13340 GIVE_UP (16);
13341
13342 /* Give up if the window ends in strings. Overlay strings
13343 at the end are difficult to handle, so don't try. */
13344 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13345 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13346 GIVE_UP (20);
13347
13348 /* Compute the position at which we have to start displaying new
13349 lines. Some of the lines at the top of the window might be
13350 reusable because they are not displaying changed text. Find the
13351 last row in W's current matrix not affected by changes at the
13352 start of current_buffer. Value is null if changes start in the
13353 first line of window. */
13354 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13355 if (last_unchanged_at_beg_row)
13356 {
13357 /* Avoid starting to display in the moddle of a character, a TAB
13358 for instance. This is easier than to set up the iterator
13359 exactly, and it's not a frequent case, so the additional
13360 effort wouldn't really pay off. */
13361 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13362 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13363 && last_unchanged_at_beg_row > w->current_matrix->rows)
13364 --last_unchanged_at_beg_row;
13365
13366 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13367 GIVE_UP (17);
13368
13369 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13370 GIVE_UP (18);
13371 start_pos = it.current.pos;
13372
13373 /* Start displaying new lines in the desired matrix at the same
13374 vpos we would use in the current matrix, i.e. below
13375 last_unchanged_at_beg_row. */
13376 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13377 current_matrix);
13378 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13379 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13380
13381 xassert (it.hpos == 0 && it.current_x == 0);
13382 }
13383 else
13384 {
13385 /* There are no reusable lines at the start of the window.
13386 Start displaying in the first text line. */
13387 start_display (&it, w, start);
13388 it.vpos = it.first_vpos;
13389 start_pos = it.current.pos;
13390 }
13391
13392 /* Find the first row that is not affected by changes at the end of
13393 the buffer. Value will be null if there is no unchanged row, in
13394 which case we must redisplay to the end of the window. delta
13395 will be set to the value by which buffer positions beginning with
13396 first_unchanged_at_end_row have to be adjusted due to text
13397 changes. */
13398 first_unchanged_at_end_row
13399 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13400 IF_DEBUG (debug_delta = delta);
13401 IF_DEBUG (debug_delta_bytes = delta_bytes);
13402
13403 /* Set stop_pos to the buffer position up to which we will have to
13404 display new lines. If first_unchanged_at_end_row != NULL, this
13405 is the buffer position of the start of the line displayed in that
13406 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13407 that we don't stop at a buffer position. */
13408 stop_pos = 0;
13409 if (first_unchanged_at_end_row)
13410 {
13411 xassert (last_unchanged_at_beg_row == NULL
13412 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13413
13414 /* If this is a continuation line, move forward to the next one
13415 that isn't. Changes in lines above affect this line.
13416 Caution: this may move first_unchanged_at_end_row to a row
13417 not displaying text. */
13418 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13419 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13420 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13421 < it.last_visible_y))
13422 ++first_unchanged_at_end_row;
13423
13424 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13425 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13426 >= it.last_visible_y))
13427 first_unchanged_at_end_row = NULL;
13428 else
13429 {
13430 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13431 + delta);
13432 first_unchanged_at_end_vpos
13433 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13434 xassert (stop_pos >= Z - END_UNCHANGED);
13435 }
13436 }
13437 else if (last_unchanged_at_beg_row == NULL)
13438 GIVE_UP (19);
13439
13440
13441 #if GLYPH_DEBUG
13442
13443 /* Either there is no unchanged row at the end, or the one we have
13444 now displays text. This is a necessary condition for the window
13445 end pos calculation at the end of this function. */
13446 xassert (first_unchanged_at_end_row == NULL
13447 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13448
13449 debug_last_unchanged_at_beg_vpos
13450 = (last_unchanged_at_beg_row
13451 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13452 : -1);
13453 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13454
13455 #endif /* GLYPH_DEBUG != 0 */
13456
13457
13458 /* Display new lines. Set last_text_row to the last new line
13459 displayed which has text on it, i.e. might end up as being the
13460 line where the window_end_vpos is. */
13461 w->cursor.vpos = -1;
13462 last_text_row = NULL;
13463 overlay_arrow_seen = 0;
13464 while (it.current_y < it.last_visible_y
13465 && !fonts_changed_p
13466 && (first_unchanged_at_end_row == NULL
13467 || IT_CHARPOS (it) < stop_pos))
13468 {
13469 if (display_line (&it))
13470 last_text_row = it.glyph_row - 1;
13471 }
13472
13473 if (fonts_changed_p)
13474 return -1;
13475
13476
13477 /* Compute differences in buffer positions, y-positions etc. for
13478 lines reused at the bottom of the window. Compute what we can
13479 scroll. */
13480 if (first_unchanged_at_end_row
13481 /* No lines reused because we displayed everything up to the
13482 bottom of the window. */
13483 && it.current_y < it.last_visible_y)
13484 {
13485 dvpos = (it.vpos
13486 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13487 current_matrix));
13488 dy = it.current_y - first_unchanged_at_end_row->y;
13489 run.current_y = first_unchanged_at_end_row->y;
13490 run.desired_y = run.current_y + dy;
13491 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13492 }
13493 else
13494 {
13495 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13496 first_unchanged_at_end_row = NULL;
13497 }
13498 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13499
13500
13501 /* Find the cursor if not already found. We have to decide whether
13502 PT will appear on this window (it sometimes doesn't, but this is
13503 not a very frequent case.) This decision has to be made before
13504 the current matrix is altered. A value of cursor.vpos < 0 means
13505 that PT is either in one of the lines beginning at
13506 first_unchanged_at_end_row or below the window. Don't care for
13507 lines that might be displayed later at the window end; as
13508 mentioned, this is not a frequent case. */
13509 if (w->cursor.vpos < 0)
13510 {
13511 /* Cursor in unchanged rows at the top? */
13512 if (PT < CHARPOS (start_pos)
13513 && last_unchanged_at_beg_row)
13514 {
13515 row = row_containing_pos (w, PT,
13516 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13517 last_unchanged_at_beg_row + 1, 0);
13518 if (row)
13519 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13520 }
13521
13522 /* Start from first_unchanged_at_end_row looking for PT. */
13523 else if (first_unchanged_at_end_row)
13524 {
13525 row = row_containing_pos (w, PT - delta,
13526 first_unchanged_at_end_row, NULL, 0);
13527 if (row)
13528 set_cursor_from_row (w, row, w->current_matrix, delta,
13529 delta_bytes, dy, dvpos);
13530 }
13531
13532 /* Give up if cursor was not found. */
13533 if (w->cursor.vpos < 0)
13534 {
13535 clear_glyph_matrix (w->desired_matrix);
13536 return -1;
13537 }
13538 }
13539
13540 /* Don't let the cursor end in the scroll margins. */
13541 {
13542 int this_scroll_margin, cursor_height;
13543
13544 this_scroll_margin = max (0, scroll_margin);
13545 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13546 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13547 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13548
13549 if ((w->cursor.y < this_scroll_margin
13550 && CHARPOS (start) > BEGV)
13551 /* Old redisplay didn't take scroll margin into account at the bottom,
13552 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13553 || (w->cursor.y + (make_cursor_line_fully_visible_p
13554 ? cursor_height + this_scroll_margin
13555 : 1)) > it.last_visible_y)
13556 {
13557 w->cursor.vpos = -1;
13558 clear_glyph_matrix (w->desired_matrix);
13559 return -1;
13560 }
13561 }
13562
13563 /* Scroll the display. Do it before changing the current matrix so
13564 that xterm.c doesn't get confused about where the cursor glyph is
13565 found. */
13566 if (dy && run.height)
13567 {
13568 update_begin (f);
13569
13570 if (FRAME_WINDOW_P (f))
13571 {
13572 rif->update_window_begin_hook (w);
13573 rif->clear_window_mouse_face (w);
13574 rif->scroll_run_hook (w, &run);
13575 rif->update_window_end_hook (w, 0, 0);
13576 }
13577 else
13578 {
13579 /* Terminal frame. In this case, dvpos gives the number of
13580 lines to scroll by; dvpos < 0 means scroll up. */
13581 int first_unchanged_at_end_vpos
13582 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13583 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13584 int end = (WINDOW_TOP_EDGE_LINE (w)
13585 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13586 + window_internal_height (w));
13587
13588 /* Perform the operation on the screen. */
13589 if (dvpos > 0)
13590 {
13591 /* Scroll last_unchanged_at_beg_row to the end of the
13592 window down dvpos lines. */
13593 set_terminal_window (end);
13594
13595 /* On dumb terminals delete dvpos lines at the end
13596 before inserting dvpos empty lines. */
13597 if (!scroll_region_ok)
13598 ins_del_lines (end - dvpos, -dvpos);
13599
13600 /* Insert dvpos empty lines in front of
13601 last_unchanged_at_beg_row. */
13602 ins_del_lines (from, dvpos);
13603 }
13604 else if (dvpos < 0)
13605 {
13606 /* Scroll up last_unchanged_at_beg_vpos to the end of
13607 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13608 set_terminal_window (end);
13609
13610 /* Delete dvpos lines in front of
13611 last_unchanged_at_beg_vpos. ins_del_lines will set
13612 the cursor to the given vpos and emit |dvpos| delete
13613 line sequences. */
13614 ins_del_lines (from + dvpos, dvpos);
13615
13616 /* On a dumb terminal insert dvpos empty lines at the
13617 end. */
13618 if (!scroll_region_ok)
13619 ins_del_lines (end + dvpos, -dvpos);
13620 }
13621
13622 set_terminal_window (0);
13623 }
13624
13625 update_end (f);
13626 }
13627
13628 /* Shift reused rows of the current matrix to the right position.
13629 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13630 text. */
13631 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13632 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13633 if (dvpos < 0)
13634 {
13635 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13636 bottom_vpos, dvpos);
13637 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13638 bottom_vpos, 0);
13639 }
13640 else if (dvpos > 0)
13641 {
13642 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13643 bottom_vpos, dvpos);
13644 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13645 first_unchanged_at_end_vpos + dvpos, 0);
13646 }
13647
13648 /* For frame-based redisplay, make sure that current frame and window
13649 matrix are in sync with respect to glyph memory. */
13650 if (!FRAME_WINDOW_P (f))
13651 sync_frame_with_window_matrix_rows (w);
13652
13653 /* Adjust buffer positions in reused rows. */
13654 if (delta)
13655 increment_matrix_positions (current_matrix,
13656 first_unchanged_at_end_vpos + dvpos,
13657 bottom_vpos, delta, delta_bytes);
13658
13659 /* Adjust Y positions. */
13660 if (dy)
13661 shift_glyph_matrix (w, current_matrix,
13662 first_unchanged_at_end_vpos + dvpos,
13663 bottom_vpos, dy);
13664
13665 if (first_unchanged_at_end_row)
13666 first_unchanged_at_end_row += dvpos;
13667
13668 /* If scrolling up, there may be some lines to display at the end of
13669 the window. */
13670 last_text_row_at_end = NULL;
13671 if (dy < 0)
13672 {
13673 /* Scrolling up can leave for example a partially visible line
13674 at the end of the window to be redisplayed. */
13675 /* Set last_row to the glyph row in the current matrix where the
13676 window end line is found. It has been moved up or down in
13677 the matrix by dvpos. */
13678 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13679 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13680
13681 /* If last_row is the window end line, it should display text. */
13682 xassert (last_row->displays_text_p);
13683
13684 /* If window end line was partially visible before, begin
13685 displaying at that line. Otherwise begin displaying with the
13686 line following it. */
13687 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13688 {
13689 init_to_row_start (&it, w, last_row);
13690 it.vpos = last_vpos;
13691 it.current_y = last_row->y;
13692 }
13693 else
13694 {
13695 init_to_row_end (&it, w, last_row);
13696 it.vpos = 1 + last_vpos;
13697 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13698 ++last_row;
13699 }
13700
13701 /* We may start in a continuation line. If so, we have to
13702 get the right continuation_lines_width and current_x. */
13703 it.continuation_lines_width = last_row->continuation_lines_width;
13704 it.hpos = it.current_x = 0;
13705
13706 /* Display the rest of the lines at the window end. */
13707 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13708 while (it.current_y < it.last_visible_y
13709 && !fonts_changed_p)
13710 {
13711 /* Is it always sure that the display agrees with lines in
13712 the current matrix? I don't think so, so we mark rows
13713 displayed invalid in the current matrix by setting their
13714 enabled_p flag to zero. */
13715 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13716 if (display_line (&it))
13717 last_text_row_at_end = it.glyph_row - 1;
13718 }
13719 }
13720
13721 /* Update window_end_pos and window_end_vpos. */
13722 if (first_unchanged_at_end_row
13723 && first_unchanged_at_end_row->y < it.last_visible_y
13724 && !last_text_row_at_end)
13725 {
13726 /* Window end line if one of the preserved rows from the current
13727 matrix. Set row to the last row displaying text in current
13728 matrix starting at first_unchanged_at_end_row, after
13729 scrolling. */
13730 xassert (first_unchanged_at_end_row->displays_text_p);
13731 row = find_last_row_displaying_text (w->current_matrix, &it,
13732 first_unchanged_at_end_row);
13733 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13734
13735 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13736 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13737 w->window_end_vpos
13738 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13739 xassert (w->window_end_bytepos >= 0);
13740 IF_DEBUG (debug_method_add (w, "A"));
13741 }
13742 else if (last_text_row_at_end)
13743 {
13744 w->window_end_pos
13745 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13746 w->window_end_bytepos
13747 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13748 w->window_end_vpos
13749 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13750 xassert (w->window_end_bytepos >= 0);
13751 IF_DEBUG (debug_method_add (w, "B"));
13752 }
13753 else if (last_text_row)
13754 {
13755 /* We have displayed either to the end of the window or at the
13756 end of the window, i.e. the last row with text is to be found
13757 in the desired matrix. */
13758 w->window_end_pos
13759 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13760 w->window_end_bytepos
13761 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13762 w->window_end_vpos
13763 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13764 xassert (w->window_end_bytepos >= 0);
13765 }
13766 else if (first_unchanged_at_end_row == NULL
13767 && last_text_row == NULL
13768 && last_text_row_at_end == NULL)
13769 {
13770 /* Displayed to end of window, but no line containing text was
13771 displayed. Lines were deleted at the end of the window. */
13772 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13773 int vpos = XFASTINT (w->window_end_vpos);
13774 struct glyph_row *current_row = current_matrix->rows + vpos;
13775 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13776
13777 for (row = NULL;
13778 row == NULL && vpos >= first_vpos;
13779 --vpos, --current_row, --desired_row)
13780 {
13781 if (desired_row->enabled_p)
13782 {
13783 if (desired_row->displays_text_p)
13784 row = desired_row;
13785 }
13786 else if (current_row->displays_text_p)
13787 row = current_row;
13788 }
13789
13790 xassert (row != NULL);
13791 w->window_end_vpos = make_number (vpos + 1);
13792 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13793 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13794 xassert (w->window_end_bytepos >= 0);
13795 IF_DEBUG (debug_method_add (w, "C"));
13796 }
13797 else
13798 abort ();
13799
13800 #if 0 /* This leads to problems, for instance when the cursor is
13801 at ZV, and the cursor line displays no text. */
13802 /* Disable rows below what's displayed in the window. This makes
13803 debugging easier. */
13804 enable_glyph_matrix_rows (current_matrix,
13805 XFASTINT (w->window_end_vpos) + 1,
13806 bottom_vpos, 0);
13807 #endif
13808
13809 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13810 debug_end_vpos = XFASTINT (w->window_end_vpos));
13811
13812 /* Record that display has not been completed. */
13813 w->window_end_valid = Qnil;
13814 w->desired_matrix->no_scrolling_p = 1;
13815 return 3;
13816
13817 #undef GIVE_UP
13818 }
13819
13820
13821 \f
13822 /***********************************************************************
13823 More debugging support
13824 ***********************************************************************/
13825
13826 #if GLYPH_DEBUG
13827
13828 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13829 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13830 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13831
13832
13833 /* Dump the contents of glyph matrix MATRIX on stderr.
13834
13835 GLYPHS 0 means don't show glyph contents.
13836 GLYPHS 1 means show glyphs in short form
13837 GLYPHS > 1 means show glyphs in long form. */
13838
13839 void
13840 dump_glyph_matrix (matrix, glyphs)
13841 struct glyph_matrix *matrix;
13842 int glyphs;
13843 {
13844 int i;
13845 for (i = 0; i < matrix->nrows; ++i)
13846 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13847 }
13848
13849
13850 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13851 the glyph row and area where the glyph comes from. */
13852
13853 void
13854 dump_glyph (row, glyph, area)
13855 struct glyph_row *row;
13856 struct glyph *glyph;
13857 int area;
13858 {
13859 if (glyph->type == CHAR_GLYPH)
13860 {
13861 fprintf (stderr,
13862 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13863 glyph - row->glyphs[TEXT_AREA],
13864 'C',
13865 glyph->charpos,
13866 (BUFFERP (glyph->object)
13867 ? 'B'
13868 : (STRINGP (glyph->object)
13869 ? 'S'
13870 : '-')),
13871 glyph->pixel_width,
13872 glyph->u.ch,
13873 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13874 ? glyph->u.ch
13875 : '.'),
13876 glyph->face_id,
13877 glyph->left_box_line_p,
13878 glyph->right_box_line_p);
13879 }
13880 else if (glyph->type == STRETCH_GLYPH)
13881 {
13882 fprintf (stderr,
13883 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13884 glyph - row->glyphs[TEXT_AREA],
13885 'S',
13886 glyph->charpos,
13887 (BUFFERP (glyph->object)
13888 ? 'B'
13889 : (STRINGP (glyph->object)
13890 ? 'S'
13891 : '-')),
13892 glyph->pixel_width,
13893 0,
13894 '.',
13895 glyph->face_id,
13896 glyph->left_box_line_p,
13897 glyph->right_box_line_p);
13898 }
13899 else if (glyph->type == IMAGE_GLYPH)
13900 {
13901 fprintf (stderr,
13902 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13903 glyph - row->glyphs[TEXT_AREA],
13904 'I',
13905 glyph->charpos,
13906 (BUFFERP (glyph->object)
13907 ? 'B'
13908 : (STRINGP (glyph->object)
13909 ? 'S'
13910 : '-')),
13911 glyph->pixel_width,
13912 glyph->u.img_id,
13913 '.',
13914 glyph->face_id,
13915 glyph->left_box_line_p,
13916 glyph->right_box_line_p);
13917 }
13918 }
13919
13920
13921 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13922 GLYPHS 0 means don't show glyph contents.
13923 GLYPHS 1 means show glyphs in short form
13924 GLYPHS > 1 means show glyphs in long form. */
13925
13926 void
13927 dump_glyph_row (row, vpos, glyphs)
13928 struct glyph_row *row;
13929 int vpos, glyphs;
13930 {
13931 if (glyphs != 1)
13932 {
13933 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13934 fprintf (stderr, "=======================================================================\n");
13935
13936 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13937 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13938 vpos,
13939 MATRIX_ROW_START_CHARPOS (row),
13940 MATRIX_ROW_END_CHARPOS (row),
13941 row->used[TEXT_AREA],
13942 row->contains_overlapping_glyphs_p,
13943 row->enabled_p,
13944 row->truncated_on_left_p,
13945 row->truncated_on_right_p,
13946 row->overlay_arrow_p,
13947 row->continued_p,
13948 MATRIX_ROW_CONTINUATION_LINE_P (row),
13949 row->displays_text_p,
13950 row->ends_at_zv_p,
13951 row->fill_line_p,
13952 row->ends_in_middle_of_char_p,
13953 row->starts_in_middle_of_char_p,
13954 row->mouse_face_p,
13955 row->x,
13956 row->y,
13957 row->pixel_width,
13958 row->height,
13959 row->visible_height,
13960 row->ascent,
13961 row->phys_ascent);
13962 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13963 row->end.overlay_string_index,
13964 row->continuation_lines_width);
13965 fprintf (stderr, "%9d %5d\n",
13966 CHARPOS (row->start.string_pos),
13967 CHARPOS (row->end.string_pos));
13968 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13969 row->end.dpvec_index);
13970 }
13971
13972 if (glyphs > 1)
13973 {
13974 int area;
13975
13976 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13977 {
13978 struct glyph *glyph = row->glyphs[area];
13979 struct glyph *glyph_end = glyph + row->used[area];
13980
13981 /* Glyph for a line end in text. */
13982 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13983 ++glyph_end;
13984
13985 if (glyph < glyph_end)
13986 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13987
13988 for (; glyph < glyph_end; ++glyph)
13989 dump_glyph (row, glyph, area);
13990 }
13991 }
13992 else if (glyphs == 1)
13993 {
13994 int area;
13995
13996 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13997 {
13998 char *s = (char *) alloca (row->used[area] + 1);
13999 int i;
14000
14001 for (i = 0; i < row->used[area]; ++i)
14002 {
14003 struct glyph *glyph = row->glyphs[area] + i;
14004 if (glyph->type == CHAR_GLYPH
14005 && glyph->u.ch < 0x80
14006 && glyph->u.ch >= ' ')
14007 s[i] = glyph->u.ch;
14008 else
14009 s[i] = '.';
14010 }
14011
14012 s[i] = '\0';
14013 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14014 }
14015 }
14016 }
14017
14018
14019 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14020 Sdump_glyph_matrix, 0, 1, "p",
14021 doc: /* Dump the current matrix of the selected window to stderr.
14022 Shows contents of glyph row structures. With non-nil
14023 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14024 glyphs in short form, otherwise show glyphs in long form. */)
14025 (glyphs)
14026 Lisp_Object glyphs;
14027 {
14028 struct window *w = XWINDOW (selected_window);
14029 struct buffer *buffer = XBUFFER (w->buffer);
14030
14031 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14032 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14033 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14034 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14035 fprintf (stderr, "=============================================\n");
14036 dump_glyph_matrix (w->current_matrix,
14037 NILP (glyphs) ? 0 : XINT (glyphs));
14038 return Qnil;
14039 }
14040
14041
14042 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14043 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14044 ()
14045 {
14046 struct frame *f = XFRAME (selected_frame);
14047 dump_glyph_matrix (f->current_matrix, 1);
14048 return Qnil;
14049 }
14050
14051
14052 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14053 doc: /* Dump glyph row ROW to stderr.
14054 GLYPH 0 means don't dump glyphs.
14055 GLYPH 1 means dump glyphs in short form.
14056 GLYPH > 1 or omitted means dump glyphs in long form. */)
14057 (row, glyphs)
14058 Lisp_Object row, glyphs;
14059 {
14060 struct glyph_matrix *matrix;
14061 int vpos;
14062
14063 CHECK_NUMBER (row);
14064 matrix = XWINDOW (selected_window)->current_matrix;
14065 vpos = XINT (row);
14066 if (vpos >= 0 && vpos < matrix->nrows)
14067 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14068 vpos,
14069 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14070 return Qnil;
14071 }
14072
14073
14074 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14075 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14076 GLYPH 0 means don't dump glyphs.
14077 GLYPH 1 means dump glyphs in short form.
14078 GLYPH > 1 or omitted means dump glyphs in long form. */)
14079 (row, glyphs)
14080 Lisp_Object row, glyphs;
14081 {
14082 struct frame *sf = SELECTED_FRAME ();
14083 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14084 int vpos;
14085
14086 CHECK_NUMBER (row);
14087 vpos = XINT (row);
14088 if (vpos >= 0 && vpos < m->nrows)
14089 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14090 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14091 return Qnil;
14092 }
14093
14094
14095 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14096 doc: /* Toggle tracing of redisplay.
14097 With ARG, turn tracing on if and only if ARG is positive. */)
14098 (arg)
14099 Lisp_Object arg;
14100 {
14101 if (NILP (arg))
14102 trace_redisplay_p = !trace_redisplay_p;
14103 else
14104 {
14105 arg = Fprefix_numeric_value (arg);
14106 trace_redisplay_p = XINT (arg) > 0;
14107 }
14108
14109 return Qnil;
14110 }
14111
14112
14113 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14114 doc: /* Like `format', but print result to stderr.
14115 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14116 (nargs, args)
14117 int nargs;
14118 Lisp_Object *args;
14119 {
14120 Lisp_Object s = Fformat (nargs, args);
14121 fprintf (stderr, "%s", SDATA (s));
14122 return Qnil;
14123 }
14124
14125 #endif /* GLYPH_DEBUG */
14126
14127
14128 \f
14129 /***********************************************************************
14130 Building Desired Matrix Rows
14131 ***********************************************************************/
14132
14133 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14134 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14135
14136 static struct glyph_row *
14137 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14138 struct window *w;
14139 Lisp_Object overlay_arrow_string;
14140 {
14141 struct frame *f = XFRAME (WINDOW_FRAME (w));
14142 struct buffer *buffer = XBUFFER (w->buffer);
14143 struct buffer *old = current_buffer;
14144 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14145 int arrow_len = SCHARS (overlay_arrow_string);
14146 const unsigned char *arrow_end = arrow_string + arrow_len;
14147 const unsigned char *p;
14148 struct it it;
14149 int multibyte_p;
14150 int n_glyphs_before;
14151
14152 set_buffer_temp (buffer);
14153 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14154 it.glyph_row->used[TEXT_AREA] = 0;
14155 SET_TEXT_POS (it.position, 0, 0);
14156
14157 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14158 p = arrow_string;
14159 while (p < arrow_end)
14160 {
14161 Lisp_Object face, ilisp;
14162
14163 /* Get the next character. */
14164 if (multibyte_p)
14165 it.c = string_char_and_length (p, arrow_len, &it.len);
14166 else
14167 it.c = *p, it.len = 1;
14168 p += it.len;
14169
14170 /* Get its face. */
14171 ilisp = make_number (p - arrow_string);
14172 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14173 it.face_id = compute_char_face (f, it.c, face);
14174
14175 /* Compute its width, get its glyphs. */
14176 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14177 SET_TEXT_POS (it.position, -1, -1);
14178 PRODUCE_GLYPHS (&it);
14179
14180 /* If this character doesn't fit any more in the line, we have
14181 to remove some glyphs. */
14182 if (it.current_x > it.last_visible_x)
14183 {
14184 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14185 break;
14186 }
14187 }
14188
14189 set_buffer_temp (old);
14190 return it.glyph_row;
14191 }
14192
14193
14194 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14195 glyphs are only inserted for terminal frames since we can't really
14196 win with truncation glyphs when partially visible glyphs are
14197 involved. Which glyphs to insert is determined by
14198 produce_special_glyphs. */
14199
14200 static void
14201 insert_left_trunc_glyphs (it)
14202 struct it *it;
14203 {
14204 struct it truncate_it;
14205 struct glyph *from, *end, *to, *toend;
14206
14207 xassert (!FRAME_WINDOW_P (it->f));
14208
14209 /* Get the truncation glyphs. */
14210 truncate_it = *it;
14211 truncate_it.current_x = 0;
14212 truncate_it.face_id = DEFAULT_FACE_ID;
14213 truncate_it.glyph_row = &scratch_glyph_row;
14214 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14215 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14216 truncate_it.object = make_number (0);
14217 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14218
14219 /* Overwrite glyphs from IT with truncation glyphs. */
14220 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14221 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14222 to = it->glyph_row->glyphs[TEXT_AREA];
14223 toend = to + it->glyph_row->used[TEXT_AREA];
14224
14225 while (from < end)
14226 *to++ = *from++;
14227
14228 /* There may be padding glyphs left over. Overwrite them too. */
14229 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14230 {
14231 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14232 while (from < end)
14233 *to++ = *from++;
14234 }
14235
14236 if (to > toend)
14237 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14238 }
14239
14240
14241 /* Compute the pixel height and width of IT->glyph_row.
14242
14243 Most of the time, ascent and height of a display line will be equal
14244 to the max_ascent and max_height values of the display iterator
14245 structure. This is not the case if
14246
14247 1. We hit ZV without displaying anything. In this case, max_ascent
14248 and max_height will be zero.
14249
14250 2. We have some glyphs that don't contribute to the line height.
14251 (The glyph row flag contributes_to_line_height_p is for future
14252 pixmap extensions).
14253
14254 The first case is easily covered by using default values because in
14255 these cases, the line height does not really matter, except that it
14256 must not be zero. */
14257
14258 static void
14259 compute_line_metrics (it)
14260 struct it *it;
14261 {
14262 struct glyph_row *row = it->glyph_row;
14263 int area, i;
14264
14265 if (FRAME_WINDOW_P (it->f))
14266 {
14267 int i, min_y, max_y;
14268
14269 /* The line may consist of one space only, that was added to
14270 place the cursor on it. If so, the row's height hasn't been
14271 computed yet. */
14272 if (row->height == 0)
14273 {
14274 if (it->max_ascent + it->max_descent == 0)
14275 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14276 row->ascent = it->max_ascent;
14277 row->height = it->max_ascent + it->max_descent;
14278 row->phys_ascent = it->max_phys_ascent;
14279 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14280 row->extra_line_spacing = it->max_extra_line_spacing;
14281 }
14282
14283 /* Compute the width of this line. */
14284 row->pixel_width = row->x;
14285 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14286 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14287
14288 xassert (row->pixel_width >= 0);
14289 xassert (row->ascent >= 0 && row->height > 0);
14290
14291 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14292 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14293
14294 /* If first line's physical ascent is larger than its logical
14295 ascent, use the physical ascent, and make the row taller.
14296 This makes accented characters fully visible. */
14297 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14298 && row->phys_ascent > row->ascent)
14299 {
14300 row->height += row->phys_ascent - row->ascent;
14301 row->ascent = row->phys_ascent;
14302 }
14303
14304 /* Compute how much of the line is visible. */
14305 row->visible_height = row->height;
14306
14307 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14308 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14309
14310 if (row->y < min_y)
14311 row->visible_height -= min_y - row->y;
14312 if (row->y + row->height > max_y)
14313 row->visible_height -= row->y + row->height - max_y;
14314 }
14315 else
14316 {
14317 row->pixel_width = row->used[TEXT_AREA];
14318 if (row->continued_p)
14319 row->pixel_width -= it->continuation_pixel_width;
14320 else if (row->truncated_on_right_p)
14321 row->pixel_width -= it->truncation_pixel_width;
14322 row->ascent = row->phys_ascent = 0;
14323 row->height = row->phys_height = row->visible_height = 1;
14324 row->extra_line_spacing = 0;
14325 }
14326
14327 /* Compute a hash code for this row. */
14328 row->hash = 0;
14329 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14330 for (i = 0; i < row->used[area]; ++i)
14331 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14332 + row->glyphs[area][i].u.val
14333 + row->glyphs[area][i].face_id
14334 + row->glyphs[area][i].padding_p
14335 + (row->glyphs[area][i].type << 2));
14336
14337 it->max_ascent = it->max_descent = 0;
14338 it->max_phys_ascent = it->max_phys_descent = 0;
14339 }
14340
14341
14342 /* Append one space to the glyph row of iterator IT if doing a
14343 window-based redisplay. The space has the same face as
14344 IT->face_id. Value is non-zero if a space was added.
14345
14346 This function is called to make sure that there is always one glyph
14347 at the end of a glyph row that the cursor can be set on under
14348 window-systems. (If there weren't such a glyph we would not know
14349 how wide and tall a box cursor should be displayed).
14350
14351 At the same time this space let's a nicely handle clearing to the
14352 end of the line if the row ends in italic text. */
14353
14354 static int
14355 append_space_for_newline (it, default_face_p)
14356 struct it *it;
14357 int default_face_p;
14358 {
14359 if (FRAME_WINDOW_P (it->f))
14360 {
14361 int n = it->glyph_row->used[TEXT_AREA];
14362
14363 if (it->glyph_row->glyphs[TEXT_AREA] + n
14364 < it->glyph_row->glyphs[1 + TEXT_AREA])
14365 {
14366 /* Save some values that must not be changed.
14367 Must save IT->c and IT->len because otherwise
14368 ITERATOR_AT_END_P wouldn't work anymore after
14369 append_space_for_newline has been called. */
14370 enum display_element_type saved_what = it->what;
14371 int saved_c = it->c, saved_len = it->len;
14372 int saved_x = it->current_x;
14373 int saved_face_id = it->face_id;
14374 struct text_pos saved_pos;
14375 Lisp_Object saved_object;
14376 struct face *face;
14377
14378 saved_object = it->object;
14379 saved_pos = it->position;
14380
14381 it->what = IT_CHARACTER;
14382 bzero (&it->position, sizeof it->position);
14383 it->object = make_number (0);
14384 it->c = ' ';
14385 it->len = 1;
14386
14387 if (default_face_p)
14388 it->face_id = DEFAULT_FACE_ID;
14389 else if (it->face_before_selective_p)
14390 it->face_id = it->saved_face_id;
14391 face = FACE_FROM_ID (it->f, it->face_id);
14392 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14393
14394 PRODUCE_GLYPHS (it);
14395
14396 it->override_ascent = -1;
14397 it->constrain_row_ascent_descent_p = 0;
14398 it->current_x = saved_x;
14399 it->object = saved_object;
14400 it->position = saved_pos;
14401 it->what = saved_what;
14402 it->face_id = saved_face_id;
14403 it->len = saved_len;
14404 it->c = saved_c;
14405 return 1;
14406 }
14407 }
14408
14409 return 0;
14410 }
14411
14412
14413 /* Extend the face of the last glyph in the text area of IT->glyph_row
14414 to the end of the display line. Called from display_line.
14415 If the glyph row is empty, add a space glyph to it so that we
14416 know the face to draw. Set the glyph row flag fill_line_p. */
14417
14418 static void
14419 extend_face_to_end_of_line (it)
14420 struct it *it;
14421 {
14422 struct face *face;
14423 struct frame *f = it->f;
14424
14425 /* If line is already filled, do nothing. */
14426 if (it->current_x >= it->last_visible_x)
14427 return;
14428
14429 /* Face extension extends the background and box of IT->face_id
14430 to the end of the line. If the background equals the background
14431 of the frame, we don't have to do anything. */
14432 if (it->face_before_selective_p)
14433 face = FACE_FROM_ID (it->f, it->saved_face_id);
14434 else
14435 face = FACE_FROM_ID (f, it->face_id);
14436
14437 if (FRAME_WINDOW_P (f)
14438 && face->box == FACE_NO_BOX
14439 && face->background == FRAME_BACKGROUND_PIXEL (f)
14440 && !face->stipple)
14441 return;
14442
14443 /* Set the glyph row flag indicating that the face of the last glyph
14444 in the text area has to be drawn to the end of the text area. */
14445 it->glyph_row->fill_line_p = 1;
14446
14447 /* If current character of IT is not ASCII, make sure we have the
14448 ASCII face. This will be automatically undone the next time
14449 get_next_display_element returns a multibyte character. Note
14450 that the character will always be single byte in unibyte text. */
14451 if (!SINGLE_BYTE_CHAR_P (it->c))
14452 {
14453 it->face_id = FACE_FOR_CHAR (f, face, 0);
14454 }
14455
14456 if (FRAME_WINDOW_P (f))
14457 {
14458 /* If the row is empty, add a space with the current face of IT,
14459 so that we know which face to draw. */
14460 if (it->glyph_row->used[TEXT_AREA] == 0)
14461 {
14462 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14463 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14464 it->glyph_row->used[TEXT_AREA] = 1;
14465 }
14466 }
14467 else
14468 {
14469 /* Save some values that must not be changed. */
14470 int saved_x = it->current_x;
14471 struct text_pos saved_pos;
14472 Lisp_Object saved_object;
14473 enum display_element_type saved_what = it->what;
14474 int saved_face_id = it->face_id;
14475
14476 saved_object = it->object;
14477 saved_pos = it->position;
14478
14479 it->what = IT_CHARACTER;
14480 bzero (&it->position, sizeof it->position);
14481 it->object = make_number (0);
14482 it->c = ' ';
14483 it->len = 1;
14484 it->face_id = face->id;
14485
14486 PRODUCE_GLYPHS (it);
14487
14488 while (it->current_x <= it->last_visible_x)
14489 PRODUCE_GLYPHS (it);
14490
14491 /* Don't count these blanks really. It would let us insert a left
14492 truncation glyph below and make us set the cursor on them, maybe. */
14493 it->current_x = saved_x;
14494 it->object = saved_object;
14495 it->position = saved_pos;
14496 it->what = saved_what;
14497 it->face_id = saved_face_id;
14498 }
14499 }
14500
14501
14502 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14503 trailing whitespace. */
14504
14505 static int
14506 trailing_whitespace_p (charpos)
14507 int charpos;
14508 {
14509 int bytepos = CHAR_TO_BYTE (charpos);
14510 int c = 0;
14511
14512 while (bytepos < ZV_BYTE
14513 && (c = FETCH_CHAR (bytepos),
14514 c == ' ' || c == '\t'))
14515 ++bytepos;
14516
14517 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14518 {
14519 if (bytepos != PT_BYTE)
14520 return 1;
14521 }
14522 return 0;
14523 }
14524
14525
14526 /* Highlight trailing whitespace, if any, in ROW. */
14527
14528 void
14529 highlight_trailing_whitespace (f, row)
14530 struct frame *f;
14531 struct glyph_row *row;
14532 {
14533 int used = row->used[TEXT_AREA];
14534
14535 if (used)
14536 {
14537 struct glyph *start = row->glyphs[TEXT_AREA];
14538 struct glyph *glyph = start + used - 1;
14539
14540 /* Skip over glyphs inserted to display the cursor at the
14541 end of a line, for extending the face of the last glyph
14542 to the end of the line on terminals, and for truncation
14543 and continuation glyphs. */
14544 while (glyph >= start
14545 && glyph->type == CHAR_GLYPH
14546 && INTEGERP (glyph->object))
14547 --glyph;
14548
14549 /* If last glyph is a space or stretch, and it's trailing
14550 whitespace, set the face of all trailing whitespace glyphs in
14551 IT->glyph_row to `trailing-whitespace'. */
14552 if (glyph >= start
14553 && BUFFERP (glyph->object)
14554 && (glyph->type == STRETCH_GLYPH
14555 || (glyph->type == CHAR_GLYPH
14556 && glyph->u.ch == ' '))
14557 && trailing_whitespace_p (glyph->charpos))
14558 {
14559 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14560 if (face_id < 0)
14561 return;
14562
14563 while (glyph >= start
14564 && BUFFERP (glyph->object)
14565 && (glyph->type == STRETCH_GLYPH
14566 || (glyph->type == CHAR_GLYPH
14567 && glyph->u.ch == ' ')))
14568 (glyph--)->face_id = face_id;
14569 }
14570 }
14571 }
14572
14573
14574 /* Value is non-zero if glyph row ROW in window W should be
14575 used to hold the cursor. */
14576
14577 static int
14578 cursor_row_p (w, row)
14579 struct window *w;
14580 struct glyph_row *row;
14581 {
14582 int cursor_row_p = 1;
14583
14584 if (PT == MATRIX_ROW_END_CHARPOS (row))
14585 {
14586 /* If the row ends with a newline from a string, we don't want
14587 the cursor there (if the row is continued it doesn't end in a
14588 newline). */
14589 if (CHARPOS (row->end.string_pos) >= 0
14590 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14591 cursor_row_p = row->continued_p;
14592
14593 /* If the row ends at ZV, display the cursor at the end of that
14594 row instead of at the start of the row below. */
14595 else if (row->ends_at_zv_p)
14596 cursor_row_p = 1;
14597 else
14598 cursor_row_p = 0;
14599 }
14600
14601 return cursor_row_p;
14602 }
14603
14604
14605 /* Construct the glyph row IT->glyph_row in the desired matrix of
14606 IT->w from text at the current position of IT. See dispextern.h
14607 for an overview of struct it. Value is non-zero if
14608 IT->glyph_row displays text, as opposed to a line displaying ZV
14609 only. */
14610
14611 static int
14612 display_line (it)
14613 struct it *it;
14614 {
14615 struct glyph_row *row = it->glyph_row;
14616 int overlay_arrow_bitmap;
14617 Lisp_Object overlay_arrow_string;
14618
14619 /* We always start displaying at hpos zero even if hscrolled. */
14620 xassert (it->hpos == 0 && it->current_x == 0);
14621
14622 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14623 >= it->w->desired_matrix->nrows)
14624 {
14625 it->w->nrows_scale_factor++;
14626 fonts_changed_p = 1;
14627 return 0;
14628 }
14629
14630 /* Is IT->w showing the region? */
14631 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14632
14633 /* Clear the result glyph row and enable it. */
14634 prepare_desired_row (row);
14635
14636 row->y = it->current_y;
14637 row->start = it->start;
14638 row->continuation_lines_width = it->continuation_lines_width;
14639 row->displays_text_p = 1;
14640 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14641 it->starts_in_middle_of_char_p = 0;
14642
14643 /* Arrange the overlays nicely for our purposes. Usually, we call
14644 display_line on only one line at a time, in which case this
14645 can't really hurt too much, or we call it on lines which appear
14646 one after another in the buffer, in which case all calls to
14647 recenter_overlay_lists but the first will be pretty cheap. */
14648 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14649
14650 /* Move over display elements that are not visible because we are
14651 hscrolled. This may stop at an x-position < IT->first_visible_x
14652 if the first glyph is partially visible or if we hit a line end. */
14653 if (it->current_x < it->first_visible_x)
14654 {
14655 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14656 MOVE_TO_POS | MOVE_TO_X);
14657 }
14658
14659 /* Get the initial row height. This is either the height of the
14660 text hscrolled, if there is any, or zero. */
14661 row->ascent = it->max_ascent;
14662 row->height = it->max_ascent + it->max_descent;
14663 row->phys_ascent = it->max_phys_ascent;
14664 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14665 row->extra_line_spacing = it->max_extra_line_spacing;
14666
14667 /* Loop generating characters. The loop is left with IT on the next
14668 character to display. */
14669 while (1)
14670 {
14671 int n_glyphs_before, hpos_before, x_before;
14672 int x, i, nglyphs;
14673 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14674
14675 /* Retrieve the next thing to display. Value is zero if end of
14676 buffer reached. */
14677 if (!get_next_display_element (it))
14678 {
14679 /* Maybe add a space at the end of this line that is used to
14680 display the cursor there under X. Set the charpos of the
14681 first glyph of blank lines not corresponding to any text
14682 to -1. */
14683 #ifdef HAVE_WINDOW_SYSTEM
14684 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14685 row->exact_window_width_line_p = 1;
14686 else
14687 #endif /* HAVE_WINDOW_SYSTEM */
14688 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14689 || row->used[TEXT_AREA] == 0)
14690 {
14691 row->glyphs[TEXT_AREA]->charpos = -1;
14692 row->displays_text_p = 0;
14693
14694 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14695 && (!MINI_WINDOW_P (it->w)
14696 || (minibuf_level && EQ (it->window, minibuf_window))))
14697 row->indicate_empty_line_p = 1;
14698 }
14699
14700 it->continuation_lines_width = 0;
14701 row->ends_at_zv_p = 1;
14702 break;
14703 }
14704
14705 /* Now, get the metrics of what we want to display. This also
14706 generates glyphs in `row' (which is IT->glyph_row). */
14707 n_glyphs_before = row->used[TEXT_AREA];
14708 x = it->current_x;
14709
14710 /* Remember the line height so far in case the next element doesn't
14711 fit on the line. */
14712 if (!it->truncate_lines_p)
14713 {
14714 ascent = it->max_ascent;
14715 descent = it->max_descent;
14716 phys_ascent = it->max_phys_ascent;
14717 phys_descent = it->max_phys_descent;
14718 }
14719
14720 PRODUCE_GLYPHS (it);
14721
14722 /* If this display element was in marginal areas, continue with
14723 the next one. */
14724 if (it->area != TEXT_AREA)
14725 {
14726 row->ascent = max (row->ascent, it->max_ascent);
14727 row->height = max (row->height, it->max_ascent + it->max_descent);
14728 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14729 row->phys_height = max (row->phys_height,
14730 it->max_phys_ascent + it->max_phys_descent);
14731 row->extra_line_spacing = max (row->extra_line_spacing,
14732 it->max_extra_line_spacing);
14733 set_iterator_to_next (it, 1);
14734 continue;
14735 }
14736
14737 /* Does the display element fit on the line? If we truncate
14738 lines, we should draw past the right edge of the window. If
14739 we don't truncate, we want to stop so that we can display the
14740 continuation glyph before the right margin. If lines are
14741 continued, there are two possible strategies for characters
14742 resulting in more than 1 glyph (e.g. tabs): Display as many
14743 glyphs as possible in this line and leave the rest for the
14744 continuation line, or display the whole element in the next
14745 line. Original redisplay did the former, so we do it also. */
14746 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14747 hpos_before = it->hpos;
14748 x_before = x;
14749
14750 if (/* Not a newline. */
14751 nglyphs > 0
14752 /* Glyphs produced fit entirely in the line. */
14753 && it->current_x < it->last_visible_x)
14754 {
14755 it->hpos += nglyphs;
14756 row->ascent = max (row->ascent, it->max_ascent);
14757 row->height = max (row->height, it->max_ascent + it->max_descent);
14758 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14759 row->phys_height = max (row->phys_height,
14760 it->max_phys_ascent + it->max_phys_descent);
14761 row->extra_line_spacing = max (row->extra_line_spacing,
14762 it->max_extra_line_spacing);
14763 if (it->current_x - it->pixel_width < it->first_visible_x)
14764 row->x = x - it->first_visible_x;
14765 }
14766 else
14767 {
14768 int new_x;
14769 struct glyph *glyph;
14770
14771 for (i = 0; i < nglyphs; ++i, x = new_x)
14772 {
14773 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14774 new_x = x + glyph->pixel_width;
14775
14776 if (/* Lines are continued. */
14777 !it->truncate_lines_p
14778 && (/* Glyph doesn't fit on the line. */
14779 new_x > it->last_visible_x
14780 /* Or it fits exactly on a window system frame. */
14781 || (new_x == it->last_visible_x
14782 && FRAME_WINDOW_P (it->f))))
14783 {
14784 /* End of a continued line. */
14785
14786 if (it->hpos == 0
14787 || (new_x == it->last_visible_x
14788 && FRAME_WINDOW_P (it->f)))
14789 {
14790 /* Current glyph is the only one on the line or
14791 fits exactly on the line. We must continue
14792 the line because we can't draw the cursor
14793 after the glyph. */
14794 row->continued_p = 1;
14795 it->current_x = new_x;
14796 it->continuation_lines_width += new_x;
14797 ++it->hpos;
14798 if (i == nglyphs - 1)
14799 {
14800 set_iterator_to_next (it, 1);
14801 #ifdef HAVE_WINDOW_SYSTEM
14802 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14803 {
14804 if (!get_next_display_element (it))
14805 {
14806 row->exact_window_width_line_p = 1;
14807 it->continuation_lines_width = 0;
14808 row->continued_p = 0;
14809 row->ends_at_zv_p = 1;
14810 }
14811 else if (ITERATOR_AT_END_OF_LINE_P (it))
14812 {
14813 row->continued_p = 0;
14814 row->exact_window_width_line_p = 1;
14815 }
14816 }
14817 #endif /* HAVE_WINDOW_SYSTEM */
14818 }
14819 }
14820 else if (CHAR_GLYPH_PADDING_P (*glyph)
14821 && !FRAME_WINDOW_P (it->f))
14822 {
14823 /* A padding glyph that doesn't fit on this line.
14824 This means the whole character doesn't fit
14825 on the line. */
14826 row->used[TEXT_AREA] = n_glyphs_before;
14827
14828 /* Fill the rest of the row with continuation
14829 glyphs like in 20.x. */
14830 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14831 < row->glyphs[1 + TEXT_AREA])
14832 produce_special_glyphs (it, IT_CONTINUATION);
14833
14834 row->continued_p = 1;
14835 it->current_x = x_before;
14836 it->continuation_lines_width += x_before;
14837
14838 /* Restore the height to what it was before the
14839 element not fitting on the line. */
14840 it->max_ascent = ascent;
14841 it->max_descent = descent;
14842 it->max_phys_ascent = phys_ascent;
14843 it->max_phys_descent = phys_descent;
14844 }
14845 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14846 {
14847 /* A TAB that extends past the right edge of the
14848 window. This produces a single glyph on
14849 window system frames. We leave the glyph in
14850 this row and let it fill the row, but don't
14851 consume the TAB. */
14852 it->continuation_lines_width += it->last_visible_x;
14853 row->ends_in_middle_of_char_p = 1;
14854 row->continued_p = 1;
14855 glyph->pixel_width = it->last_visible_x - x;
14856 it->starts_in_middle_of_char_p = 1;
14857 }
14858 else
14859 {
14860 /* Something other than a TAB that draws past
14861 the right edge of the window. Restore
14862 positions to values before the element. */
14863 row->used[TEXT_AREA] = n_glyphs_before + i;
14864
14865 /* Display continuation glyphs. */
14866 if (!FRAME_WINDOW_P (it->f))
14867 produce_special_glyphs (it, IT_CONTINUATION);
14868 row->continued_p = 1;
14869
14870 it->continuation_lines_width += x;
14871
14872 if (nglyphs > 1 && i > 0)
14873 {
14874 row->ends_in_middle_of_char_p = 1;
14875 it->starts_in_middle_of_char_p = 1;
14876 }
14877
14878 /* Restore the height to what it was before the
14879 element not fitting on the line. */
14880 it->max_ascent = ascent;
14881 it->max_descent = descent;
14882 it->max_phys_ascent = phys_ascent;
14883 it->max_phys_descent = phys_descent;
14884 }
14885
14886 break;
14887 }
14888 else if (new_x > it->first_visible_x)
14889 {
14890 /* Increment number of glyphs actually displayed. */
14891 ++it->hpos;
14892
14893 if (x < it->first_visible_x)
14894 /* Glyph is partially visible, i.e. row starts at
14895 negative X position. */
14896 row->x = x - it->first_visible_x;
14897 }
14898 else
14899 {
14900 /* Glyph is completely off the left margin of the
14901 window. This should not happen because of the
14902 move_it_in_display_line at the start of this
14903 function, unless the text display area of the
14904 window is empty. */
14905 xassert (it->first_visible_x <= it->last_visible_x);
14906 }
14907 }
14908
14909 row->ascent = max (row->ascent, it->max_ascent);
14910 row->height = max (row->height, it->max_ascent + it->max_descent);
14911 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14912 row->phys_height = max (row->phys_height,
14913 it->max_phys_ascent + it->max_phys_descent);
14914 row->extra_line_spacing = max (row->extra_line_spacing,
14915 it->max_extra_line_spacing);
14916
14917 /* End of this display line if row is continued. */
14918 if (row->continued_p || row->ends_at_zv_p)
14919 break;
14920 }
14921
14922 at_end_of_line:
14923 /* Is this a line end? If yes, we're also done, after making
14924 sure that a non-default face is extended up to the right
14925 margin of the window. */
14926 if (ITERATOR_AT_END_OF_LINE_P (it))
14927 {
14928 int used_before = row->used[TEXT_AREA];
14929
14930 row->ends_in_newline_from_string_p = STRINGP (it->object);
14931
14932 #ifdef HAVE_WINDOW_SYSTEM
14933 /* Add a space at the end of the line that is used to
14934 display the cursor there. */
14935 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14936 append_space_for_newline (it, 0);
14937 #endif /* HAVE_WINDOW_SYSTEM */
14938
14939 /* Extend the face to the end of the line. */
14940 extend_face_to_end_of_line (it);
14941
14942 /* Make sure we have the position. */
14943 if (used_before == 0)
14944 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14945
14946 /* Consume the line end. This skips over invisible lines. */
14947 set_iterator_to_next (it, 1);
14948 it->continuation_lines_width = 0;
14949 break;
14950 }
14951
14952 /* Proceed with next display element. Note that this skips
14953 over lines invisible because of selective display. */
14954 set_iterator_to_next (it, 1);
14955
14956 /* If we truncate lines, we are done when the last displayed
14957 glyphs reach past the right margin of the window. */
14958 if (it->truncate_lines_p
14959 && (FRAME_WINDOW_P (it->f)
14960 ? (it->current_x >= it->last_visible_x)
14961 : (it->current_x > it->last_visible_x)))
14962 {
14963 /* Maybe add truncation glyphs. */
14964 if (!FRAME_WINDOW_P (it->f))
14965 {
14966 int i, n;
14967
14968 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14969 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14970 break;
14971
14972 for (n = row->used[TEXT_AREA]; i < n; ++i)
14973 {
14974 row->used[TEXT_AREA] = i;
14975 produce_special_glyphs (it, IT_TRUNCATION);
14976 }
14977 }
14978 #ifdef HAVE_WINDOW_SYSTEM
14979 else
14980 {
14981 /* Don't truncate if we can overflow newline into fringe. */
14982 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14983 {
14984 if (!get_next_display_element (it))
14985 {
14986 it->continuation_lines_width = 0;
14987 row->ends_at_zv_p = 1;
14988 row->exact_window_width_line_p = 1;
14989 break;
14990 }
14991 if (ITERATOR_AT_END_OF_LINE_P (it))
14992 {
14993 row->exact_window_width_line_p = 1;
14994 goto at_end_of_line;
14995 }
14996 }
14997 }
14998 #endif /* HAVE_WINDOW_SYSTEM */
14999
15000 row->truncated_on_right_p = 1;
15001 it->continuation_lines_width = 0;
15002 reseat_at_next_visible_line_start (it, 0);
15003 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15004 it->hpos = hpos_before;
15005 it->current_x = x_before;
15006 break;
15007 }
15008 }
15009
15010 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15011 at the left window margin. */
15012 if (it->first_visible_x
15013 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15014 {
15015 if (!FRAME_WINDOW_P (it->f))
15016 insert_left_trunc_glyphs (it);
15017 row->truncated_on_left_p = 1;
15018 }
15019
15020 /* If the start of this line is the overlay arrow-position, then
15021 mark this glyph row as the one containing the overlay arrow.
15022 This is clearly a mess with variable size fonts. It would be
15023 better to let it be displayed like cursors under X. */
15024 if (! overlay_arrow_seen
15025 && (overlay_arrow_string
15026 = overlay_arrow_at_row (it, row, &overlay_arrow_bitmap),
15027 !NILP (overlay_arrow_string)))
15028 {
15029 /* Overlay arrow in window redisplay is a fringe bitmap. */
15030 if (STRINGP (overlay_arrow_string))
15031 {
15032 struct glyph_row *arrow_row
15033 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15034 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15035 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15036 struct glyph *p = row->glyphs[TEXT_AREA];
15037 struct glyph *p2, *end;
15038
15039 /* Copy the arrow glyphs. */
15040 while (glyph < arrow_end)
15041 *p++ = *glyph++;
15042
15043 /* Throw away padding glyphs. */
15044 p2 = p;
15045 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15046 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15047 ++p2;
15048 if (p2 > p)
15049 {
15050 while (p2 < end)
15051 *p++ = *p2++;
15052 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15053 }
15054 }
15055 else
15056 {
15057 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
15058 row->overlay_arrow_p = 1;
15059 }
15060 overlay_arrow_seen = 1;
15061 }
15062
15063 /* Compute pixel dimensions of this line. */
15064 compute_line_metrics (it);
15065
15066 /* Remember the position at which this line ends. */
15067 row->end = it->current;
15068
15069 /* Save fringe bitmaps in this row. */
15070 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15071 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15072 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15073 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15074
15075 it->left_user_fringe_bitmap = 0;
15076 it->left_user_fringe_face_id = 0;
15077 it->right_user_fringe_bitmap = 0;
15078 it->right_user_fringe_face_id = 0;
15079
15080 /* Maybe set the cursor. */
15081 if (it->w->cursor.vpos < 0
15082 && PT >= MATRIX_ROW_START_CHARPOS (row)
15083 && PT <= MATRIX_ROW_END_CHARPOS (row)
15084 && cursor_row_p (it->w, row))
15085 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15086
15087 /* Highlight trailing whitespace. */
15088 if (!NILP (Vshow_trailing_whitespace))
15089 highlight_trailing_whitespace (it->f, it->glyph_row);
15090
15091 /* Prepare for the next line. This line starts horizontally at (X
15092 HPOS) = (0 0). Vertical positions are incremented. As a
15093 convenience for the caller, IT->glyph_row is set to the next
15094 row to be used. */
15095 it->current_x = it->hpos = 0;
15096 it->current_y += row->height;
15097 ++it->vpos;
15098 ++it->glyph_row;
15099 it->start = it->current;
15100 return row->displays_text_p;
15101 }
15102
15103
15104 \f
15105 /***********************************************************************
15106 Menu Bar
15107 ***********************************************************************/
15108
15109 /* Redisplay the menu bar in the frame for window W.
15110
15111 The menu bar of X frames that don't have X toolkit support is
15112 displayed in a special window W->frame->menu_bar_window.
15113
15114 The menu bar of terminal frames is treated specially as far as
15115 glyph matrices are concerned. Menu bar lines are not part of
15116 windows, so the update is done directly on the frame matrix rows
15117 for the menu bar. */
15118
15119 static void
15120 display_menu_bar (w)
15121 struct window *w;
15122 {
15123 struct frame *f = XFRAME (WINDOW_FRAME (w));
15124 struct it it;
15125 Lisp_Object items;
15126 int i;
15127
15128 /* Don't do all this for graphical frames. */
15129 #ifdef HAVE_NTGUI
15130 if (!NILP (Vwindow_system))
15131 return;
15132 #endif
15133 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15134 if (FRAME_X_P (f))
15135 return;
15136 #endif
15137 #ifdef MAC_OS
15138 if (FRAME_MAC_P (f))
15139 return;
15140 #endif
15141
15142 #ifdef USE_X_TOOLKIT
15143 xassert (!FRAME_WINDOW_P (f));
15144 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15145 it.first_visible_x = 0;
15146 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15147 #else /* not USE_X_TOOLKIT */
15148 if (FRAME_WINDOW_P (f))
15149 {
15150 /* Menu bar lines are displayed in the desired matrix of the
15151 dummy window menu_bar_window. */
15152 struct window *menu_w;
15153 xassert (WINDOWP (f->menu_bar_window));
15154 menu_w = XWINDOW (f->menu_bar_window);
15155 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15156 MENU_FACE_ID);
15157 it.first_visible_x = 0;
15158 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15159 }
15160 else
15161 {
15162 /* This is a TTY frame, i.e. character hpos/vpos are used as
15163 pixel x/y. */
15164 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15165 MENU_FACE_ID);
15166 it.first_visible_x = 0;
15167 it.last_visible_x = FRAME_COLS (f);
15168 }
15169 #endif /* not USE_X_TOOLKIT */
15170
15171 if (! mode_line_inverse_video)
15172 /* Force the menu-bar to be displayed in the default face. */
15173 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15174
15175 /* Clear all rows of the menu bar. */
15176 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15177 {
15178 struct glyph_row *row = it.glyph_row + i;
15179 clear_glyph_row (row);
15180 row->enabled_p = 1;
15181 row->full_width_p = 1;
15182 }
15183
15184 /* Display all items of the menu bar. */
15185 items = FRAME_MENU_BAR_ITEMS (it.f);
15186 for (i = 0; i < XVECTOR (items)->size; i += 4)
15187 {
15188 Lisp_Object string;
15189
15190 /* Stop at nil string. */
15191 string = AREF (items, i + 1);
15192 if (NILP (string))
15193 break;
15194
15195 /* Remember where item was displayed. */
15196 AREF (items, i + 3) = make_number (it.hpos);
15197
15198 /* Display the item, pad with one space. */
15199 if (it.current_x < it.last_visible_x)
15200 display_string (NULL, string, Qnil, 0, 0, &it,
15201 SCHARS (string) + 1, 0, 0, -1);
15202 }
15203
15204 /* Fill out the line with spaces. */
15205 if (it.current_x < it.last_visible_x)
15206 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15207
15208 /* Compute the total height of the lines. */
15209 compute_line_metrics (&it);
15210 }
15211
15212
15213 \f
15214 /***********************************************************************
15215 Mode Line
15216 ***********************************************************************/
15217
15218 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15219 FORCE is non-zero, redisplay mode lines unconditionally.
15220 Otherwise, redisplay only mode lines that are garbaged. Value is
15221 the number of windows whose mode lines were redisplayed. */
15222
15223 static int
15224 redisplay_mode_lines (window, force)
15225 Lisp_Object window;
15226 int force;
15227 {
15228 int nwindows = 0;
15229
15230 while (!NILP (window))
15231 {
15232 struct window *w = XWINDOW (window);
15233
15234 if (WINDOWP (w->hchild))
15235 nwindows += redisplay_mode_lines (w->hchild, force);
15236 else if (WINDOWP (w->vchild))
15237 nwindows += redisplay_mode_lines (w->vchild, force);
15238 else if (force
15239 || FRAME_GARBAGED_P (XFRAME (w->frame))
15240 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15241 {
15242 struct text_pos lpoint;
15243 struct buffer *old = current_buffer;
15244
15245 /* Set the window's buffer for the mode line display. */
15246 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15247 set_buffer_internal_1 (XBUFFER (w->buffer));
15248
15249 /* Point refers normally to the selected window. For any
15250 other window, set up appropriate value. */
15251 if (!EQ (window, selected_window))
15252 {
15253 struct text_pos pt;
15254
15255 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15256 if (CHARPOS (pt) < BEGV)
15257 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15258 else if (CHARPOS (pt) > (ZV - 1))
15259 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15260 else
15261 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15262 }
15263
15264 /* Display mode lines. */
15265 clear_glyph_matrix (w->desired_matrix);
15266 if (display_mode_lines (w))
15267 {
15268 ++nwindows;
15269 w->must_be_updated_p = 1;
15270 }
15271
15272 /* Restore old settings. */
15273 set_buffer_internal_1 (old);
15274 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15275 }
15276
15277 window = w->next;
15278 }
15279
15280 return nwindows;
15281 }
15282
15283
15284 /* Display the mode and/or top line of window W. Value is the number
15285 of mode lines displayed. */
15286
15287 static int
15288 display_mode_lines (w)
15289 struct window *w;
15290 {
15291 Lisp_Object old_selected_window, old_selected_frame;
15292 int n = 0;
15293
15294 old_selected_frame = selected_frame;
15295 selected_frame = w->frame;
15296 old_selected_window = selected_window;
15297 XSETWINDOW (selected_window, w);
15298
15299 /* These will be set while the mode line specs are processed. */
15300 line_number_displayed = 0;
15301 w->column_number_displayed = Qnil;
15302
15303 if (WINDOW_WANTS_MODELINE_P (w))
15304 {
15305 struct window *sel_w = XWINDOW (old_selected_window);
15306
15307 /* Select mode line face based on the real selected window. */
15308 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15309 current_buffer->mode_line_format);
15310 ++n;
15311 }
15312
15313 if (WINDOW_WANTS_HEADER_LINE_P (w))
15314 {
15315 display_mode_line (w, HEADER_LINE_FACE_ID,
15316 current_buffer->header_line_format);
15317 ++n;
15318 }
15319
15320 selected_frame = old_selected_frame;
15321 selected_window = old_selected_window;
15322 return n;
15323 }
15324
15325
15326 /* Display mode or top line of window W. FACE_ID specifies which line
15327 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15328 FORMAT is the mode line format to display. Value is the pixel
15329 height of the mode line displayed. */
15330
15331 static int
15332 display_mode_line (w, face_id, format)
15333 struct window *w;
15334 enum face_id face_id;
15335 Lisp_Object format;
15336 {
15337 struct it it;
15338 struct face *face;
15339
15340 init_iterator (&it, w, -1, -1, NULL, face_id);
15341 prepare_desired_row (it.glyph_row);
15342
15343 it.glyph_row->mode_line_p = 1;
15344
15345 if (! mode_line_inverse_video)
15346 /* Force the mode-line to be displayed in the default face. */
15347 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15348
15349 /* Temporarily make frame's keyboard the current kboard so that
15350 kboard-local variables in the mode_line_format will get the right
15351 values. */
15352 push_frame_kboard (it.f);
15353 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15354 pop_frame_kboard ();
15355
15356 /* Fill up with spaces. */
15357 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15358
15359 compute_line_metrics (&it);
15360 it.glyph_row->full_width_p = 1;
15361 it.glyph_row->continued_p = 0;
15362 it.glyph_row->truncated_on_left_p = 0;
15363 it.glyph_row->truncated_on_right_p = 0;
15364
15365 /* Make a 3D mode-line have a shadow at its right end. */
15366 face = FACE_FROM_ID (it.f, face_id);
15367 extend_face_to_end_of_line (&it);
15368 if (face->box != FACE_NO_BOX)
15369 {
15370 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15371 + it.glyph_row->used[TEXT_AREA] - 1);
15372 last->right_box_line_p = 1;
15373 }
15374
15375 return it.glyph_row->height;
15376 }
15377
15378 /* Alist that caches the results of :propertize.
15379 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15380 Lisp_Object mode_line_proptrans_alist;
15381
15382 /* List of strings making up the mode-line. */
15383 Lisp_Object mode_line_string_list;
15384
15385 /* Base face property when building propertized mode line string. */
15386 static Lisp_Object mode_line_string_face;
15387 static Lisp_Object mode_line_string_face_prop;
15388
15389
15390 /* Contribute ELT to the mode line for window IT->w. How it
15391 translates into text depends on its data type.
15392
15393 IT describes the display environment in which we display, as usual.
15394
15395 DEPTH is the depth in recursion. It is used to prevent
15396 infinite recursion here.
15397
15398 FIELD_WIDTH is the number of characters the display of ELT should
15399 occupy in the mode line, and PRECISION is the maximum number of
15400 characters to display from ELT's representation. See
15401 display_string for details.
15402
15403 Returns the hpos of the end of the text generated by ELT.
15404
15405 PROPS is a property list to add to any string we encounter.
15406
15407 If RISKY is nonzero, remove (disregard) any properties in any string
15408 we encounter, and ignore :eval and :propertize.
15409
15410 If the global variable `frame_title_ptr' is non-NULL, then the output
15411 is passed to `store_frame_title' instead of `display_string'. */
15412
15413 static int
15414 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15415 struct it *it;
15416 int depth;
15417 int field_width, precision;
15418 Lisp_Object elt, props;
15419 int risky;
15420 {
15421 int n = 0, field, prec;
15422 int literal = 0;
15423
15424 tail_recurse:
15425 if (depth > 100)
15426 elt = build_string ("*too-deep*");
15427
15428 depth++;
15429
15430 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15431 {
15432 case Lisp_String:
15433 {
15434 /* A string: output it and check for %-constructs within it. */
15435 unsigned char c;
15436 const unsigned char *this, *lisp_string;
15437
15438 if (!NILP (props) || risky)
15439 {
15440 Lisp_Object oprops, aelt;
15441 oprops = Ftext_properties_at (make_number (0), elt);
15442
15443 /* If the starting string's properties are not what
15444 we want, translate the string. Also, if the string
15445 is risky, do that anyway. */
15446
15447 if (NILP (Fequal (props, oprops)) || risky)
15448 {
15449 /* If the starting string has properties,
15450 merge the specified ones onto the existing ones. */
15451 if (! NILP (oprops) && !risky)
15452 {
15453 Lisp_Object tem;
15454
15455 oprops = Fcopy_sequence (oprops);
15456 tem = props;
15457 while (CONSP (tem))
15458 {
15459 oprops = Fplist_put (oprops, XCAR (tem),
15460 XCAR (XCDR (tem)));
15461 tem = XCDR (XCDR (tem));
15462 }
15463 props = oprops;
15464 }
15465
15466 aelt = Fassoc (elt, mode_line_proptrans_alist);
15467 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15468 {
15469 mode_line_proptrans_alist
15470 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15471 elt = XCAR (aelt);
15472 }
15473 else
15474 {
15475 Lisp_Object tem;
15476
15477 elt = Fcopy_sequence (elt);
15478 Fset_text_properties (make_number (0), Flength (elt),
15479 props, elt);
15480 /* Add this item to mode_line_proptrans_alist. */
15481 mode_line_proptrans_alist
15482 = Fcons (Fcons (elt, props),
15483 mode_line_proptrans_alist);
15484 /* Truncate mode_line_proptrans_alist
15485 to at most 50 elements. */
15486 tem = Fnthcdr (make_number (50),
15487 mode_line_proptrans_alist);
15488 if (! NILP (tem))
15489 XSETCDR (tem, Qnil);
15490 }
15491 }
15492 }
15493
15494 this = SDATA (elt);
15495 lisp_string = this;
15496
15497 if (literal)
15498 {
15499 prec = precision - n;
15500 if (frame_title_ptr)
15501 n += store_frame_title (SDATA (elt), -1, prec);
15502 else if (!NILP (mode_line_string_list))
15503 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15504 else
15505 n += display_string (NULL, elt, Qnil, 0, 0, it,
15506 0, prec, 0, STRING_MULTIBYTE (elt));
15507
15508 break;
15509 }
15510
15511 while ((precision <= 0 || n < precision)
15512 && *this
15513 && (frame_title_ptr
15514 || !NILP (mode_line_string_list)
15515 || it->current_x < it->last_visible_x))
15516 {
15517 const unsigned char *last = this;
15518
15519 /* Advance to end of string or next format specifier. */
15520 while ((c = *this++) != '\0' && c != '%')
15521 ;
15522
15523 if (this - 1 != last)
15524 {
15525 int nchars, nbytes;
15526
15527 /* Output to end of string or up to '%'. Field width
15528 is length of string. Don't output more than
15529 PRECISION allows us. */
15530 --this;
15531
15532 prec = c_string_width (last, this - last, precision - n,
15533 &nchars, &nbytes);
15534
15535 if (frame_title_ptr)
15536 n += store_frame_title (last, 0, prec);
15537 else if (!NILP (mode_line_string_list))
15538 {
15539 int bytepos = last - lisp_string;
15540 int charpos = string_byte_to_char (elt, bytepos);
15541 int endpos = (precision <= 0
15542 ? string_byte_to_char (elt,
15543 this - lisp_string)
15544 : charpos + nchars);
15545
15546 n += store_mode_line_string (NULL,
15547 Fsubstring (elt, make_number (charpos),
15548 make_number (endpos)),
15549 0, 0, 0, Qnil);
15550 }
15551 else
15552 {
15553 int bytepos = last - lisp_string;
15554 int charpos = string_byte_to_char (elt, bytepos);
15555 n += display_string (NULL, elt, Qnil, 0, charpos,
15556 it, 0, prec, 0,
15557 STRING_MULTIBYTE (elt));
15558 }
15559 }
15560 else /* c == '%' */
15561 {
15562 const unsigned char *percent_position = this;
15563
15564 /* Get the specified minimum width. Zero means
15565 don't pad. */
15566 field = 0;
15567 while ((c = *this++) >= '0' && c <= '9')
15568 field = field * 10 + c - '0';
15569
15570 /* Don't pad beyond the total padding allowed. */
15571 if (field_width - n > 0 && field > field_width - n)
15572 field = field_width - n;
15573
15574 /* Note that either PRECISION <= 0 or N < PRECISION. */
15575 prec = precision - n;
15576
15577 if (c == 'M')
15578 n += display_mode_element (it, depth, field, prec,
15579 Vglobal_mode_string, props,
15580 risky);
15581 else if (c != 0)
15582 {
15583 int multibyte;
15584 int bytepos, charpos;
15585 unsigned char *spec;
15586
15587 bytepos = percent_position - lisp_string;
15588 charpos = (STRING_MULTIBYTE (elt)
15589 ? string_byte_to_char (elt, bytepos)
15590 : bytepos);
15591
15592 spec
15593 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15594
15595 if (frame_title_ptr)
15596 n += store_frame_title (spec, field, prec);
15597 else if (!NILP (mode_line_string_list))
15598 {
15599 int len = strlen (spec);
15600 Lisp_Object tem = make_string (spec, len);
15601 props = Ftext_properties_at (make_number (charpos), elt);
15602 /* Should only keep face property in props */
15603 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15604 }
15605 else
15606 {
15607 int nglyphs_before, nwritten;
15608
15609 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15610 nwritten = display_string (spec, Qnil, elt,
15611 charpos, 0, it,
15612 field, prec, 0,
15613 multibyte);
15614
15615 /* Assign to the glyphs written above the
15616 string where the `%x' came from, position
15617 of the `%'. */
15618 if (nwritten > 0)
15619 {
15620 struct glyph *glyph
15621 = (it->glyph_row->glyphs[TEXT_AREA]
15622 + nglyphs_before);
15623 int i;
15624
15625 for (i = 0; i < nwritten; ++i)
15626 {
15627 glyph[i].object = elt;
15628 glyph[i].charpos = charpos;
15629 }
15630
15631 n += nwritten;
15632 }
15633 }
15634 }
15635 else /* c == 0 */
15636 break;
15637 }
15638 }
15639 }
15640 break;
15641
15642 case Lisp_Symbol:
15643 /* A symbol: process the value of the symbol recursively
15644 as if it appeared here directly. Avoid error if symbol void.
15645 Special case: if value of symbol is a string, output the string
15646 literally. */
15647 {
15648 register Lisp_Object tem;
15649
15650 /* If the variable is not marked as risky to set
15651 then its contents are risky to use. */
15652 if (NILP (Fget (elt, Qrisky_local_variable)))
15653 risky = 1;
15654
15655 tem = Fboundp (elt);
15656 if (!NILP (tem))
15657 {
15658 tem = Fsymbol_value (elt);
15659 /* If value is a string, output that string literally:
15660 don't check for % within it. */
15661 if (STRINGP (tem))
15662 literal = 1;
15663
15664 if (!EQ (tem, elt))
15665 {
15666 /* Give up right away for nil or t. */
15667 elt = tem;
15668 goto tail_recurse;
15669 }
15670 }
15671 }
15672 break;
15673
15674 case Lisp_Cons:
15675 {
15676 register Lisp_Object car, tem;
15677
15678 /* A cons cell: five distinct cases.
15679 If first element is :eval or :propertize, do something special.
15680 If first element is a string or a cons, process all the elements
15681 and effectively concatenate them.
15682 If first element is a negative number, truncate displaying cdr to
15683 at most that many characters. If positive, pad (with spaces)
15684 to at least that many characters.
15685 If first element is a symbol, process the cadr or caddr recursively
15686 according to whether the symbol's value is non-nil or nil. */
15687 car = XCAR (elt);
15688 if (EQ (car, QCeval))
15689 {
15690 /* An element of the form (:eval FORM) means evaluate FORM
15691 and use the result as mode line elements. */
15692
15693 if (risky)
15694 break;
15695
15696 if (CONSP (XCDR (elt)))
15697 {
15698 Lisp_Object spec;
15699 spec = safe_eval (XCAR (XCDR (elt)));
15700 n += display_mode_element (it, depth, field_width - n,
15701 precision - n, spec, props,
15702 risky);
15703 }
15704 }
15705 else if (EQ (car, QCpropertize))
15706 {
15707 /* An element of the form (:propertize ELT PROPS...)
15708 means display ELT but applying properties PROPS. */
15709
15710 if (risky)
15711 break;
15712
15713 if (CONSP (XCDR (elt)))
15714 n += display_mode_element (it, depth, field_width - n,
15715 precision - n, XCAR (XCDR (elt)),
15716 XCDR (XCDR (elt)), risky);
15717 }
15718 else if (SYMBOLP (car))
15719 {
15720 tem = Fboundp (car);
15721 elt = XCDR (elt);
15722 if (!CONSP (elt))
15723 goto invalid;
15724 /* elt is now the cdr, and we know it is a cons cell.
15725 Use its car if CAR has a non-nil value. */
15726 if (!NILP (tem))
15727 {
15728 tem = Fsymbol_value (car);
15729 if (!NILP (tem))
15730 {
15731 elt = XCAR (elt);
15732 goto tail_recurse;
15733 }
15734 }
15735 /* Symbol's value is nil (or symbol is unbound)
15736 Get the cddr of the original list
15737 and if possible find the caddr and use that. */
15738 elt = XCDR (elt);
15739 if (NILP (elt))
15740 break;
15741 else if (!CONSP (elt))
15742 goto invalid;
15743 elt = XCAR (elt);
15744 goto tail_recurse;
15745 }
15746 else if (INTEGERP (car))
15747 {
15748 register int lim = XINT (car);
15749 elt = XCDR (elt);
15750 if (lim < 0)
15751 {
15752 /* Negative int means reduce maximum width. */
15753 if (precision <= 0)
15754 precision = -lim;
15755 else
15756 precision = min (precision, -lim);
15757 }
15758 else if (lim > 0)
15759 {
15760 /* Padding specified. Don't let it be more than
15761 current maximum. */
15762 if (precision > 0)
15763 lim = min (precision, lim);
15764
15765 /* If that's more padding than already wanted, queue it.
15766 But don't reduce padding already specified even if
15767 that is beyond the current truncation point. */
15768 field_width = max (lim, field_width);
15769 }
15770 goto tail_recurse;
15771 }
15772 else if (STRINGP (car) || CONSP (car))
15773 {
15774 register int limit = 50;
15775 /* Limit is to protect against circular lists. */
15776 while (CONSP (elt)
15777 && --limit > 0
15778 && (precision <= 0 || n < precision))
15779 {
15780 n += display_mode_element (it, depth, field_width - n,
15781 precision - n, XCAR (elt),
15782 props, risky);
15783 elt = XCDR (elt);
15784 }
15785 }
15786 }
15787 break;
15788
15789 default:
15790 invalid:
15791 elt = build_string ("*invalid*");
15792 goto tail_recurse;
15793 }
15794
15795 /* Pad to FIELD_WIDTH. */
15796 if (field_width > 0 && n < field_width)
15797 {
15798 if (frame_title_ptr)
15799 n += store_frame_title ("", field_width - n, 0);
15800 else if (!NILP (mode_line_string_list))
15801 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15802 else
15803 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15804 0, 0, 0);
15805 }
15806
15807 return n;
15808 }
15809
15810 /* Store a mode-line string element in mode_line_string_list.
15811
15812 If STRING is non-null, display that C string. Otherwise, the Lisp
15813 string LISP_STRING is displayed.
15814
15815 FIELD_WIDTH is the minimum number of output glyphs to produce.
15816 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15817 with spaces. FIELD_WIDTH <= 0 means don't pad.
15818
15819 PRECISION is the maximum number of characters to output from
15820 STRING. PRECISION <= 0 means don't truncate the string.
15821
15822 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15823 properties to the string.
15824
15825 PROPS are the properties to add to the string.
15826 The mode_line_string_face face property is always added to the string.
15827 */
15828
15829 static int
15830 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15831 char *string;
15832 Lisp_Object lisp_string;
15833 int copy_string;
15834 int field_width;
15835 int precision;
15836 Lisp_Object props;
15837 {
15838 int len;
15839 int n = 0;
15840
15841 if (string != NULL)
15842 {
15843 len = strlen (string);
15844 if (precision > 0 && len > precision)
15845 len = precision;
15846 lisp_string = make_string (string, len);
15847 if (NILP (props))
15848 props = mode_line_string_face_prop;
15849 else if (!NILP (mode_line_string_face))
15850 {
15851 Lisp_Object face = Fsafe_plist_get (props, Qface);
15852 props = Fcopy_sequence (props);
15853 if (NILP (face))
15854 face = mode_line_string_face;
15855 else
15856 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15857 props = Fplist_put (props, Qface, face);
15858 }
15859 Fadd_text_properties (make_number (0), make_number (len),
15860 props, lisp_string);
15861 }
15862 else
15863 {
15864 len = XFASTINT (Flength (lisp_string));
15865 if (precision > 0 && len > precision)
15866 {
15867 len = precision;
15868 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15869 precision = -1;
15870 }
15871 if (!NILP (mode_line_string_face))
15872 {
15873 Lisp_Object face;
15874 if (NILP (props))
15875 props = Ftext_properties_at (make_number (0), lisp_string);
15876 face = Fsafe_plist_get (props, Qface);
15877 if (NILP (face))
15878 face = mode_line_string_face;
15879 else
15880 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15881 props = Fcons (Qface, Fcons (face, Qnil));
15882 if (copy_string)
15883 lisp_string = Fcopy_sequence (lisp_string);
15884 }
15885 if (!NILP (props))
15886 Fadd_text_properties (make_number (0), make_number (len),
15887 props, lisp_string);
15888 }
15889
15890 if (len > 0)
15891 {
15892 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15893 n += len;
15894 }
15895
15896 if (field_width > len)
15897 {
15898 field_width -= len;
15899 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15900 if (!NILP (props))
15901 Fadd_text_properties (make_number (0), make_number (field_width),
15902 props, lisp_string);
15903 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15904 n += field_width;
15905 }
15906
15907 return n;
15908 }
15909
15910
15911 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15912 0, 4, 0,
15913 doc: /* Return the mode-line of selected window as a string.
15914 First optional arg FORMAT specifies a different format string (see
15915 `mode-line-format' for details) to use. If FORMAT is t, return
15916 the buffer's header-line. Second optional arg WINDOW specifies a
15917 different window to use as the context for the formatting.
15918 If third optional arg NO-PROPS is non-nil, string is not propertized.
15919 Fourth optional arg BUFFER specifies which buffer to use. */)
15920 (format, window, no_props, buffer)
15921 Lisp_Object format, window, no_props, buffer;
15922 {
15923 struct it it;
15924 int len;
15925 struct window *w;
15926 struct buffer *old_buffer = NULL;
15927 enum face_id face_id = DEFAULT_FACE_ID;
15928
15929 if (NILP (window))
15930 window = selected_window;
15931 CHECK_WINDOW (window);
15932 w = XWINDOW (window);
15933
15934 if (NILP (buffer))
15935 buffer = w->buffer;
15936
15937 CHECK_BUFFER (buffer);
15938
15939 if (XBUFFER (buffer) != current_buffer)
15940 {
15941 old_buffer = current_buffer;
15942 set_buffer_internal_1 (XBUFFER (buffer));
15943 }
15944
15945 if (NILP (format) || EQ (format, Qt))
15946 {
15947 face_id = (NILP (format)
15948 ? CURRENT_MODE_LINE_FACE_ID (w)
15949 : HEADER_LINE_FACE_ID);
15950 format = (NILP (format)
15951 ? current_buffer->mode_line_format
15952 : current_buffer->header_line_format);
15953 }
15954
15955 init_iterator (&it, w, -1, -1, NULL, face_id);
15956
15957 if (NILP (no_props))
15958 {
15959 mode_line_string_face
15960 = (face_id == MODE_LINE_FACE_ID ? Qmode_line
15961 : face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive
15962 : face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15963
15964 mode_line_string_face_prop
15965 = (NILP (mode_line_string_face) ? Qnil
15966 : Fcons (Qface, Fcons (mode_line_string_face, Qnil)));
15967
15968 /* We need a dummy last element in mode_line_string_list to
15969 indicate we are building the propertized mode-line string.
15970 Using mode_line_string_face_prop here GC protects it. */
15971 mode_line_string_list
15972 = Fcons (mode_line_string_face_prop, Qnil);
15973 frame_title_ptr = NULL;
15974 }
15975 else
15976 {
15977 mode_line_string_face_prop = Qnil;
15978 mode_line_string_list = Qnil;
15979 frame_title_ptr = frame_title_buf;
15980 }
15981
15982 push_frame_kboard (it.f);
15983 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15984 pop_frame_kboard ();
15985
15986 if (old_buffer)
15987 set_buffer_internal_1 (old_buffer);
15988
15989 if (NILP (no_props))
15990 {
15991 Lisp_Object str;
15992 mode_line_string_list = Fnreverse (mode_line_string_list);
15993 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15994 make_string ("", 0));
15995 mode_line_string_face_prop = Qnil;
15996 mode_line_string_list = Qnil;
15997 return str;
15998 }
15999
16000 len = frame_title_ptr - frame_title_buf;
16001 if (len > 0 && frame_title_ptr[-1] == '-')
16002 {
16003 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16004 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16005 ;
16006 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16007 if (len > frame_title_ptr - frame_title_buf)
16008 len = frame_title_ptr - frame_title_buf;
16009 }
16010
16011 frame_title_ptr = NULL;
16012 return make_string (frame_title_buf, len);
16013 }
16014
16015 /* Write a null-terminated, right justified decimal representation of
16016 the positive integer D to BUF using a minimal field width WIDTH. */
16017
16018 static void
16019 pint2str (buf, width, d)
16020 register char *buf;
16021 register int width;
16022 register int d;
16023 {
16024 register char *p = buf;
16025
16026 if (d <= 0)
16027 *p++ = '0';
16028 else
16029 {
16030 while (d > 0)
16031 {
16032 *p++ = d % 10 + '0';
16033 d /= 10;
16034 }
16035 }
16036
16037 for (width -= (int) (p - buf); width > 0; --width)
16038 *p++ = ' ';
16039 *p-- = '\0';
16040 while (p > buf)
16041 {
16042 d = *buf;
16043 *buf++ = *p;
16044 *p-- = d;
16045 }
16046 }
16047
16048 /* Write a null-terminated, right justified decimal and "human
16049 readable" representation of the nonnegative integer D to BUF using
16050 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16051
16052 static const char power_letter[] =
16053 {
16054 0, /* not used */
16055 'k', /* kilo */
16056 'M', /* mega */
16057 'G', /* giga */
16058 'T', /* tera */
16059 'P', /* peta */
16060 'E', /* exa */
16061 'Z', /* zetta */
16062 'Y' /* yotta */
16063 };
16064
16065 static void
16066 pint2hrstr (buf, width, d)
16067 char *buf;
16068 int width;
16069 int d;
16070 {
16071 /* We aim to represent the nonnegative integer D as
16072 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16073 int quotient = d;
16074 int remainder = 0;
16075 /* -1 means: do not use TENTHS. */
16076 int tenths = -1;
16077 int exponent = 0;
16078
16079 /* Length of QUOTIENT.TENTHS as a string. */
16080 int length;
16081
16082 char * psuffix;
16083 char * p;
16084
16085 if (1000 <= quotient)
16086 {
16087 /* Scale to the appropriate EXPONENT. */
16088 do
16089 {
16090 remainder = quotient % 1000;
16091 quotient /= 1000;
16092 exponent++;
16093 }
16094 while (1000 <= quotient);
16095
16096 /* Round to nearest and decide whether to use TENTHS or not. */
16097 if (quotient <= 9)
16098 {
16099 tenths = remainder / 100;
16100 if (50 <= remainder % 100)
16101 {
16102 if (tenths < 9)
16103 tenths++;
16104 else
16105 {
16106 quotient++;
16107 if (quotient == 10)
16108 tenths = -1;
16109 else
16110 tenths = 0;
16111 }
16112 }
16113 }
16114 else
16115 if (500 <= remainder)
16116 {
16117 if (quotient < 999)
16118 quotient++;
16119 else
16120 {
16121 quotient = 1;
16122 exponent++;
16123 tenths = 0;
16124 }
16125 }
16126 }
16127
16128 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16129 if (tenths == -1 && quotient <= 99)
16130 if (quotient <= 9)
16131 length = 1;
16132 else
16133 length = 2;
16134 else
16135 length = 3;
16136 p = psuffix = buf + max (width, length);
16137
16138 /* Print EXPONENT. */
16139 if (exponent)
16140 *psuffix++ = power_letter[exponent];
16141 *psuffix = '\0';
16142
16143 /* Print TENTHS. */
16144 if (tenths >= 0)
16145 {
16146 *--p = '0' + tenths;
16147 *--p = '.';
16148 }
16149
16150 /* Print QUOTIENT. */
16151 do
16152 {
16153 int digit = quotient % 10;
16154 *--p = '0' + digit;
16155 }
16156 while ((quotient /= 10) != 0);
16157
16158 /* Print leading spaces. */
16159 while (buf < p)
16160 *--p = ' ';
16161 }
16162
16163 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16164 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16165 type of CODING_SYSTEM. Return updated pointer into BUF. */
16166
16167 static unsigned char invalid_eol_type[] = "(*invalid*)";
16168
16169 static char *
16170 decode_mode_spec_coding (coding_system, buf, eol_flag)
16171 Lisp_Object coding_system;
16172 register char *buf;
16173 int eol_flag;
16174 {
16175 Lisp_Object val;
16176 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16177 const unsigned char *eol_str;
16178 int eol_str_len;
16179 /* The EOL conversion we are using. */
16180 Lisp_Object eoltype;
16181
16182 val = Fget (coding_system, Qcoding_system);
16183 eoltype = Qnil;
16184
16185 if (!VECTORP (val)) /* Not yet decided. */
16186 {
16187 if (multibyte)
16188 *buf++ = '-';
16189 if (eol_flag)
16190 eoltype = eol_mnemonic_undecided;
16191 /* Don't mention EOL conversion if it isn't decided. */
16192 }
16193 else
16194 {
16195 Lisp_Object eolvalue;
16196
16197 eolvalue = Fget (coding_system, Qeol_type);
16198
16199 if (multibyte)
16200 *buf++ = XFASTINT (AREF (val, 1));
16201
16202 if (eol_flag)
16203 {
16204 /* The EOL conversion that is normal on this system. */
16205
16206 if (NILP (eolvalue)) /* Not yet decided. */
16207 eoltype = eol_mnemonic_undecided;
16208 else if (VECTORP (eolvalue)) /* Not yet decided. */
16209 eoltype = eol_mnemonic_undecided;
16210 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16211 eoltype = (XFASTINT (eolvalue) == 0
16212 ? eol_mnemonic_unix
16213 : (XFASTINT (eolvalue) == 1
16214 ? eol_mnemonic_dos : eol_mnemonic_mac));
16215 }
16216 }
16217
16218 if (eol_flag)
16219 {
16220 /* Mention the EOL conversion if it is not the usual one. */
16221 if (STRINGP (eoltype))
16222 {
16223 eol_str = SDATA (eoltype);
16224 eol_str_len = SBYTES (eoltype);
16225 }
16226 else if (INTEGERP (eoltype)
16227 && CHAR_VALID_P (XINT (eoltype), 0))
16228 {
16229 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16230 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16231 eol_str = tmp;
16232 }
16233 else
16234 {
16235 eol_str = invalid_eol_type;
16236 eol_str_len = sizeof (invalid_eol_type) - 1;
16237 }
16238 bcopy (eol_str, buf, eol_str_len);
16239 buf += eol_str_len;
16240 }
16241
16242 return buf;
16243 }
16244
16245 /* Return a string for the output of a mode line %-spec for window W,
16246 generated by character C. PRECISION >= 0 means don't return a
16247 string longer than that value. FIELD_WIDTH > 0 means pad the
16248 string returned with spaces to that value. Return 1 in *MULTIBYTE
16249 if the result is multibyte text.
16250
16251 Note we operate on the current buffer for most purposes,
16252 the exception being w->base_line_pos. */
16253
16254 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16255
16256 static char *
16257 decode_mode_spec (w, c, field_width, precision, multibyte)
16258 struct window *w;
16259 register int c;
16260 int field_width, precision;
16261 int *multibyte;
16262 {
16263 Lisp_Object obj;
16264 struct frame *f = XFRAME (WINDOW_FRAME (w));
16265 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16266 struct buffer *b = current_buffer;
16267
16268 obj = Qnil;
16269 *multibyte = 0;
16270
16271 switch (c)
16272 {
16273 case '*':
16274 if (!NILP (b->read_only))
16275 return "%";
16276 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16277 return "*";
16278 return "-";
16279
16280 case '+':
16281 /* This differs from %* only for a modified read-only buffer. */
16282 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16283 return "*";
16284 if (!NILP (b->read_only))
16285 return "%";
16286 return "-";
16287
16288 case '&':
16289 /* This differs from %* in ignoring read-only-ness. */
16290 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16291 return "*";
16292 return "-";
16293
16294 case '%':
16295 return "%";
16296
16297 case '[':
16298 {
16299 int i;
16300 char *p;
16301
16302 if (command_loop_level > 5)
16303 return "[[[... ";
16304 p = decode_mode_spec_buf;
16305 for (i = 0; i < command_loop_level; i++)
16306 *p++ = '[';
16307 *p = 0;
16308 return decode_mode_spec_buf;
16309 }
16310
16311 case ']':
16312 {
16313 int i;
16314 char *p;
16315
16316 if (command_loop_level > 5)
16317 return " ...]]]";
16318 p = decode_mode_spec_buf;
16319 for (i = 0; i < command_loop_level; i++)
16320 *p++ = ']';
16321 *p = 0;
16322 return decode_mode_spec_buf;
16323 }
16324
16325 case '-':
16326 {
16327 register int i;
16328
16329 /* Let lots_of_dashes be a string of infinite length. */
16330 if (!NILP (mode_line_string_list))
16331 return "--";
16332 if (field_width <= 0
16333 || field_width > sizeof (lots_of_dashes))
16334 {
16335 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16336 decode_mode_spec_buf[i] = '-';
16337 decode_mode_spec_buf[i] = '\0';
16338 return decode_mode_spec_buf;
16339 }
16340 else
16341 return lots_of_dashes;
16342 }
16343
16344 case 'b':
16345 obj = b->name;
16346 break;
16347
16348 case 'c':
16349 {
16350 int col = (int) current_column (); /* iftc */
16351 w->column_number_displayed = make_number (col);
16352 pint2str (decode_mode_spec_buf, field_width, col);
16353 return decode_mode_spec_buf;
16354 }
16355
16356 case 'F':
16357 /* %F displays the frame name. */
16358 if (!NILP (f->title))
16359 return (char *) SDATA (f->title);
16360 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16361 return (char *) SDATA (f->name);
16362 return "Emacs";
16363
16364 case 'f':
16365 obj = b->filename;
16366 break;
16367
16368 case 'i':
16369 {
16370 int size = ZV - BEGV;
16371 pint2str (decode_mode_spec_buf, field_width, size);
16372 return decode_mode_spec_buf;
16373 }
16374
16375 case 'I':
16376 {
16377 int size = ZV - BEGV;
16378 pint2hrstr (decode_mode_spec_buf, field_width, size);
16379 return decode_mode_spec_buf;
16380 }
16381
16382 case 'l':
16383 {
16384 int startpos = XMARKER (w->start)->charpos;
16385 int startpos_byte = marker_byte_position (w->start);
16386 int line, linepos, linepos_byte, topline;
16387 int nlines, junk;
16388 int height = WINDOW_TOTAL_LINES (w);
16389
16390 /* If we decided that this buffer isn't suitable for line numbers,
16391 don't forget that too fast. */
16392 if (EQ (w->base_line_pos, w->buffer))
16393 goto no_value;
16394 /* But do forget it, if the window shows a different buffer now. */
16395 else if (BUFFERP (w->base_line_pos))
16396 w->base_line_pos = Qnil;
16397
16398 /* If the buffer is very big, don't waste time. */
16399 if (INTEGERP (Vline_number_display_limit)
16400 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16401 {
16402 w->base_line_pos = Qnil;
16403 w->base_line_number = Qnil;
16404 goto no_value;
16405 }
16406
16407 if (!NILP (w->base_line_number)
16408 && !NILP (w->base_line_pos)
16409 && XFASTINT (w->base_line_pos) <= startpos)
16410 {
16411 line = XFASTINT (w->base_line_number);
16412 linepos = XFASTINT (w->base_line_pos);
16413 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16414 }
16415 else
16416 {
16417 line = 1;
16418 linepos = BUF_BEGV (b);
16419 linepos_byte = BUF_BEGV_BYTE (b);
16420 }
16421
16422 /* Count lines from base line to window start position. */
16423 nlines = display_count_lines (linepos, linepos_byte,
16424 startpos_byte,
16425 startpos, &junk);
16426
16427 topline = nlines + line;
16428
16429 /* Determine a new base line, if the old one is too close
16430 or too far away, or if we did not have one.
16431 "Too close" means it's plausible a scroll-down would
16432 go back past it. */
16433 if (startpos == BUF_BEGV (b))
16434 {
16435 w->base_line_number = make_number (topline);
16436 w->base_line_pos = make_number (BUF_BEGV (b));
16437 }
16438 else if (nlines < height + 25 || nlines > height * 3 + 50
16439 || linepos == BUF_BEGV (b))
16440 {
16441 int limit = BUF_BEGV (b);
16442 int limit_byte = BUF_BEGV_BYTE (b);
16443 int position;
16444 int distance = (height * 2 + 30) * line_number_display_limit_width;
16445
16446 if (startpos - distance > limit)
16447 {
16448 limit = startpos - distance;
16449 limit_byte = CHAR_TO_BYTE (limit);
16450 }
16451
16452 nlines = display_count_lines (startpos, startpos_byte,
16453 limit_byte,
16454 - (height * 2 + 30),
16455 &position);
16456 /* If we couldn't find the lines we wanted within
16457 line_number_display_limit_width chars per line,
16458 give up on line numbers for this window. */
16459 if (position == limit_byte && limit == startpos - distance)
16460 {
16461 w->base_line_pos = w->buffer;
16462 w->base_line_number = Qnil;
16463 goto no_value;
16464 }
16465
16466 w->base_line_number = make_number (topline - nlines);
16467 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16468 }
16469
16470 /* Now count lines from the start pos to point. */
16471 nlines = display_count_lines (startpos, startpos_byte,
16472 PT_BYTE, PT, &junk);
16473
16474 /* Record that we did display the line number. */
16475 line_number_displayed = 1;
16476
16477 /* Make the string to show. */
16478 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16479 return decode_mode_spec_buf;
16480 no_value:
16481 {
16482 char* p = decode_mode_spec_buf;
16483 int pad = field_width - 2;
16484 while (pad-- > 0)
16485 *p++ = ' ';
16486 *p++ = '?';
16487 *p++ = '?';
16488 *p = '\0';
16489 return decode_mode_spec_buf;
16490 }
16491 }
16492 break;
16493
16494 case 'm':
16495 obj = b->mode_name;
16496 break;
16497
16498 case 'n':
16499 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16500 return " Narrow";
16501 break;
16502
16503 case 'p':
16504 {
16505 int pos = marker_position (w->start);
16506 int total = BUF_ZV (b) - BUF_BEGV (b);
16507
16508 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16509 {
16510 if (pos <= BUF_BEGV (b))
16511 return "All";
16512 else
16513 return "Bottom";
16514 }
16515 else if (pos <= BUF_BEGV (b))
16516 return "Top";
16517 else
16518 {
16519 if (total > 1000000)
16520 /* Do it differently for a large value, to avoid overflow. */
16521 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16522 else
16523 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16524 /* We can't normally display a 3-digit number,
16525 so get us a 2-digit number that is close. */
16526 if (total == 100)
16527 total = 99;
16528 sprintf (decode_mode_spec_buf, "%2d%%", total);
16529 return decode_mode_spec_buf;
16530 }
16531 }
16532
16533 /* Display percentage of size above the bottom of the screen. */
16534 case 'P':
16535 {
16536 int toppos = marker_position (w->start);
16537 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16538 int total = BUF_ZV (b) - BUF_BEGV (b);
16539
16540 if (botpos >= BUF_ZV (b))
16541 {
16542 if (toppos <= BUF_BEGV (b))
16543 return "All";
16544 else
16545 return "Bottom";
16546 }
16547 else
16548 {
16549 if (total > 1000000)
16550 /* Do it differently for a large value, to avoid overflow. */
16551 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16552 else
16553 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16554 /* We can't normally display a 3-digit number,
16555 so get us a 2-digit number that is close. */
16556 if (total == 100)
16557 total = 99;
16558 if (toppos <= BUF_BEGV (b))
16559 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16560 else
16561 sprintf (decode_mode_spec_buf, "%2d%%", total);
16562 return decode_mode_spec_buf;
16563 }
16564 }
16565
16566 case 's':
16567 /* status of process */
16568 obj = Fget_buffer_process (Fcurrent_buffer ());
16569 if (NILP (obj))
16570 return "no process";
16571 #ifdef subprocesses
16572 obj = Fsymbol_name (Fprocess_status (obj));
16573 #endif
16574 break;
16575
16576 case 't': /* indicate TEXT or BINARY */
16577 #ifdef MODE_LINE_BINARY_TEXT
16578 return MODE_LINE_BINARY_TEXT (b);
16579 #else
16580 return "T";
16581 #endif
16582
16583 case 'z':
16584 /* coding-system (not including end-of-line format) */
16585 case 'Z':
16586 /* coding-system (including end-of-line type) */
16587 {
16588 int eol_flag = (c == 'Z');
16589 char *p = decode_mode_spec_buf;
16590
16591 if (! FRAME_WINDOW_P (f))
16592 {
16593 /* No need to mention EOL here--the terminal never needs
16594 to do EOL conversion. */
16595 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16596 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16597 }
16598 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16599 p, eol_flag);
16600
16601 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16602 #ifdef subprocesses
16603 obj = Fget_buffer_process (Fcurrent_buffer ());
16604 if (PROCESSP (obj))
16605 {
16606 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16607 p, eol_flag);
16608 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16609 p, eol_flag);
16610 }
16611 #endif /* subprocesses */
16612 #endif /* 0 */
16613 *p = 0;
16614 return decode_mode_spec_buf;
16615 }
16616 }
16617
16618 if (STRINGP (obj))
16619 {
16620 *multibyte = STRING_MULTIBYTE (obj);
16621 return (char *) SDATA (obj);
16622 }
16623 else
16624 return "";
16625 }
16626
16627
16628 /* Count up to COUNT lines starting from START / START_BYTE.
16629 But don't go beyond LIMIT_BYTE.
16630 Return the number of lines thus found (always nonnegative).
16631
16632 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16633
16634 static int
16635 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16636 int start, start_byte, limit_byte, count;
16637 int *byte_pos_ptr;
16638 {
16639 register unsigned char *cursor;
16640 unsigned char *base;
16641
16642 register int ceiling;
16643 register unsigned char *ceiling_addr;
16644 int orig_count = count;
16645
16646 /* If we are not in selective display mode,
16647 check only for newlines. */
16648 int selective_display = (!NILP (current_buffer->selective_display)
16649 && !INTEGERP (current_buffer->selective_display));
16650
16651 if (count > 0)
16652 {
16653 while (start_byte < limit_byte)
16654 {
16655 ceiling = BUFFER_CEILING_OF (start_byte);
16656 ceiling = min (limit_byte - 1, ceiling);
16657 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16658 base = (cursor = BYTE_POS_ADDR (start_byte));
16659 while (1)
16660 {
16661 if (selective_display)
16662 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16663 ;
16664 else
16665 while (*cursor != '\n' && ++cursor != ceiling_addr)
16666 ;
16667
16668 if (cursor != ceiling_addr)
16669 {
16670 if (--count == 0)
16671 {
16672 start_byte += cursor - base + 1;
16673 *byte_pos_ptr = start_byte;
16674 return orig_count;
16675 }
16676 else
16677 if (++cursor == ceiling_addr)
16678 break;
16679 }
16680 else
16681 break;
16682 }
16683 start_byte += cursor - base;
16684 }
16685 }
16686 else
16687 {
16688 while (start_byte > limit_byte)
16689 {
16690 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16691 ceiling = max (limit_byte, ceiling);
16692 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16693 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16694 while (1)
16695 {
16696 if (selective_display)
16697 while (--cursor != ceiling_addr
16698 && *cursor != '\n' && *cursor != 015)
16699 ;
16700 else
16701 while (--cursor != ceiling_addr && *cursor != '\n')
16702 ;
16703
16704 if (cursor != ceiling_addr)
16705 {
16706 if (++count == 0)
16707 {
16708 start_byte += cursor - base + 1;
16709 *byte_pos_ptr = start_byte;
16710 /* When scanning backwards, we should
16711 not count the newline posterior to which we stop. */
16712 return - orig_count - 1;
16713 }
16714 }
16715 else
16716 break;
16717 }
16718 /* Here we add 1 to compensate for the last decrement
16719 of CURSOR, which took it past the valid range. */
16720 start_byte += cursor - base + 1;
16721 }
16722 }
16723
16724 *byte_pos_ptr = limit_byte;
16725
16726 if (count < 0)
16727 return - orig_count + count;
16728 return orig_count - count;
16729
16730 }
16731
16732
16733 \f
16734 /***********************************************************************
16735 Displaying strings
16736 ***********************************************************************/
16737
16738 /* Display a NUL-terminated string, starting with index START.
16739
16740 If STRING is non-null, display that C string. Otherwise, the Lisp
16741 string LISP_STRING is displayed.
16742
16743 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16744 FACE_STRING. Display STRING or LISP_STRING with the face at
16745 FACE_STRING_POS in FACE_STRING:
16746
16747 Display the string in the environment given by IT, but use the
16748 standard display table, temporarily.
16749
16750 FIELD_WIDTH is the minimum number of output glyphs to produce.
16751 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16752 with spaces. If STRING has more characters, more than FIELD_WIDTH
16753 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16754
16755 PRECISION is the maximum number of characters to output from
16756 STRING. PRECISION < 0 means don't truncate the string.
16757
16758 This is roughly equivalent to printf format specifiers:
16759
16760 FIELD_WIDTH PRECISION PRINTF
16761 ----------------------------------------
16762 -1 -1 %s
16763 -1 10 %.10s
16764 10 -1 %10s
16765 20 10 %20.10s
16766
16767 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16768 display them, and < 0 means obey the current buffer's value of
16769 enable_multibyte_characters.
16770
16771 Value is the number of glyphs produced. */
16772
16773 static int
16774 display_string (string, lisp_string, face_string, face_string_pos,
16775 start, it, field_width, precision, max_x, multibyte)
16776 unsigned char *string;
16777 Lisp_Object lisp_string;
16778 Lisp_Object face_string;
16779 int face_string_pos;
16780 int start;
16781 struct it *it;
16782 int field_width, precision, max_x;
16783 int multibyte;
16784 {
16785 int hpos_at_start = it->hpos;
16786 int saved_face_id = it->face_id;
16787 struct glyph_row *row = it->glyph_row;
16788
16789 /* Initialize the iterator IT for iteration over STRING beginning
16790 with index START. */
16791 reseat_to_string (it, string, lisp_string, start,
16792 precision, field_width, multibyte);
16793
16794 /* If displaying STRING, set up the face of the iterator
16795 from LISP_STRING, if that's given. */
16796 if (STRINGP (face_string))
16797 {
16798 int endptr;
16799 struct face *face;
16800
16801 it->face_id
16802 = face_at_string_position (it->w, face_string, face_string_pos,
16803 0, it->region_beg_charpos,
16804 it->region_end_charpos,
16805 &endptr, it->base_face_id, 0);
16806 face = FACE_FROM_ID (it->f, it->face_id);
16807 it->face_box_p = face->box != FACE_NO_BOX;
16808 }
16809
16810 /* Set max_x to the maximum allowed X position. Don't let it go
16811 beyond the right edge of the window. */
16812 if (max_x <= 0)
16813 max_x = it->last_visible_x;
16814 else
16815 max_x = min (max_x, it->last_visible_x);
16816
16817 /* Skip over display elements that are not visible. because IT->w is
16818 hscrolled. */
16819 if (it->current_x < it->first_visible_x)
16820 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16821 MOVE_TO_POS | MOVE_TO_X);
16822
16823 row->ascent = it->max_ascent;
16824 row->height = it->max_ascent + it->max_descent;
16825 row->phys_ascent = it->max_phys_ascent;
16826 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16827 row->extra_line_spacing = it->max_extra_line_spacing;
16828
16829 /* This condition is for the case that we are called with current_x
16830 past last_visible_x. */
16831 while (it->current_x < max_x)
16832 {
16833 int x_before, x, n_glyphs_before, i, nglyphs;
16834
16835 /* Get the next display element. */
16836 if (!get_next_display_element (it))
16837 break;
16838
16839 /* Produce glyphs. */
16840 x_before = it->current_x;
16841 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16842 PRODUCE_GLYPHS (it);
16843
16844 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16845 i = 0;
16846 x = x_before;
16847 while (i < nglyphs)
16848 {
16849 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16850
16851 if (!it->truncate_lines_p
16852 && x + glyph->pixel_width > max_x)
16853 {
16854 /* End of continued line or max_x reached. */
16855 if (CHAR_GLYPH_PADDING_P (*glyph))
16856 {
16857 /* A wide character is unbreakable. */
16858 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16859 it->current_x = x_before;
16860 }
16861 else
16862 {
16863 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16864 it->current_x = x;
16865 }
16866 break;
16867 }
16868 else if (x + glyph->pixel_width > it->first_visible_x)
16869 {
16870 /* Glyph is at least partially visible. */
16871 ++it->hpos;
16872 if (x < it->first_visible_x)
16873 it->glyph_row->x = x - it->first_visible_x;
16874 }
16875 else
16876 {
16877 /* Glyph is off the left margin of the display area.
16878 Should not happen. */
16879 abort ();
16880 }
16881
16882 row->ascent = max (row->ascent, it->max_ascent);
16883 row->height = max (row->height, it->max_ascent + it->max_descent);
16884 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16885 row->phys_height = max (row->phys_height,
16886 it->max_phys_ascent + it->max_phys_descent);
16887 row->extra_line_spacing = max (row->extra_line_spacing,
16888 it->max_extra_line_spacing);
16889 x += glyph->pixel_width;
16890 ++i;
16891 }
16892
16893 /* Stop if max_x reached. */
16894 if (i < nglyphs)
16895 break;
16896
16897 /* Stop at line ends. */
16898 if (ITERATOR_AT_END_OF_LINE_P (it))
16899 {
16900 it->continuation_lines_width = 0;
16901 break;
16902 }
16903
16904 set_iterator_to_next (it, 1);
16905
16906 /* Stop if truncating at the right edge. */
16907 if (it->truncate_lines_p
16908 && it->current_x >= it->last_visible_x)
16909 {
16910 /* Add truncation mark, but don't do it if the line is
16911 truncated at a padding space. */
16912 if (IT_CHARPOS (*it) < it->string_nchars)
16913 {
16914 if (!FRAME_WINDOW_P (it->f))
16915 {
16916 int i, n;
16917
16918 if (it->current_x > it->last_visible_x)
16919 {
16920 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16921 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16922 break;
16923 for (n = row->used[TEXT_AREA]; i < n; ++i)
16924 {
16925 row->used[TEXT_AREA] = i;
16926 produce_special_glyphs (it, IT_TRUNCATION);
16927 }
16928 }
16929 produce_special_glyphs (it, IT_TRUNCATION);
16930 }
16931 it->glyph_row->truncated_on_right_p = 1;
16932 }
16933 break;
16934 }
16935 }
16936
16937 /* Maybe insert a truncation at the left. */
16938 if (it->first_visible_x
16939 && IT_CHARPOS (*it) > 0)
16940 {
16941 if (!FRAME_WINDOW_P (it->f))
16942 insert_left_trunc_glyphs (it);
16943 it->glyph_row->truncated_on_left_p = 1;
16944 }
16945
16946 it->face_id = saved_face_id;
16947
16948 /* Value is number of columns displayed. */
16949 return it->hpos - hpos_at_start;
16950 }
16951
16952
16953 \f
16954 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16955 appears as an element of LIST or as the car of an element of LIST.
16956 If PROPVAL is a list, compare each element against LIST in that
16957 way, and return 1/2 if any element of PROPVAL is found in LIST.
16958 Otherwise return 0. This function cannot quit.
16959 The return value is 2 if the text is invisible but with an ellipsis
16960 and 1 if it's invisible and without an ellipsis. */
16961
16962 int
16963 invisible_p (propval, list)
16964 register Lisp_Object propval;
16965 Lisp_Object list;
16966 {
16967 register Lisp_Object tail, proptail;
16968
16969 for (tail = list; CONSP (tail); tail = XCDR (tail))
16970 {
16971 register Lisp_Object tem;
16972 tem = XCAR (tail);
16973 if (EQ (propval, tem))
16974 return 1;
16975 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16976 return NILP (XCDR (tem)) ? 1 : 2;
16977 }
16978
16979 if (CONSP (propval))
16980 {
16981 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16982 {
16983 Lisp_Object propelt;
16984 propelt = XCAR (proptail);
16985 for (tail = list; CONSP (tail); tail = XCDR (tail))
16986 {
16987 register Lisp_Object tem;
16988 tem = XCAR (tail);
16989 if (EQ (propelt, tem))
16990 return 1;
16991 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16992 return NILP (XCDR (tem)) ? 1 : 2;
16993 }
16994 }
16995 }
16996
16997 return 0;
16998 }
16999
17000 /* Calculate a width or height in pixels from a specification using
17001 the following elements:
17002
17003 SPEC ::=
17004 NUM - a (fractional) multiple of the default font width/height
17005 (NUM) - specifies exactly NUM pixels
17006 UNIT - a fixed number of pixels, see below.
17007 ELEMENT - size of a display element in pixels, see below.
17008 (NUM . SPEC) - equals NUM * SPEC
17009 (+ SPEC SPEC ...) - add pixel values
17010 (- SPEC SPEC ...) - subtract pixel values
17011 (- SPEC) - negate pixel value
17012
17013 NUM ::=
17014 INT or FLOAT - a number constant
17015 SYMBOL - use symbol's (buffer local) variable binding.
17016
17017 UNIT ::=
17018 in - pixels per inch *)
17019 mm - pixels per 1/1000 meter *)
17020 cm - pixels per 1/100 meter *)
17021 width - width of current font in pixels.
17022 height - height of current font in pixels.
17023
17024 *) using the ratio(s) defined in display-pixels-per-inch.
17025
17026 ELEMENT ::=
17027
17028 left-fringe - left fringe width in pixels
17029 right-fringe - right fringe width in pixels
17030
17031 left-margin - left margin width in pixels
17032 right-margin - right margin width in pixels
17033
17034 scroll-bar - scroll-bar area width in pixels
17035
17036 Examples:
17037
17038 Pixels corresponding to 5 inches:
17039 (5 . in)
17040
17041 Total width of non-text areas on left side of window (if scroll-bar is on left):
17042 '(space :width (+ left-fringe left-margin scroll-bar))
17043
17044 Align to first text column (in header line):
17045 '(space :align-to 0)
17046
17047 Align to middle of text area minus half the width of variable `my-image'
17048 containing a loaded image:
17049 '(space :align-to (0.5 . (- text my-image)))
17050
17051 Width of left margin minus width of 1 character in the default font:
17052 '(space :width (- left-margin 1))
17053
17054 Width of left margin minus width of 2 characters in the current font:
17055 '(space :width (- left-margin (2 . width)))
17056
17057 Center 1 character over left-margin (in header line):
17058 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17059
17060 Different ways to express width of left fringe plus left margin minus one pixel:
17061 '(space :width (- (+ left-fringe left-margin) (1)))
17062 '(space :width (+ left-fringe left-margin (- (1))))
17063 '(space :width (+ left-fringe left-margin (-1)))
17064
17065 */
17066
17067 #define NUMVAL(X) \
17068 ((INTEGERP (X) || FLOATP (X)) \
17069 ? XFLOATINT (X) \
17070 : - 1)
17071
17072 int
17073 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17074 double *res;
17075 struct it *it;
17076 Lisp_Object prop;
17077 void *font;
17078 int width_p, *align_to;
17079 {
17080 double pixels;
17081
17082 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17083 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17084
17085 if (NILP (prop))
17086 return OK_PIXELS (0);
17087
17088 if (SYMBOLP (prop))
17089 {
17090 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17091 {
17092 char *unit = SDATA (SYMBOL_NAME (prop));
17093
17094 if (unit[0] == 'i' && unit[1] == 'n')
17095 pixels = 1.0;
17096 else if (unit[0] == 'm' && unit[1] == 'm')
17097 pixels = 25.4;
17098 else if (unit[0] == 'c' && unit[1] == 'm')
17099 pixels = 2.54;
17100 else
17101 pixels = 0;
17102 if (pixels > 0)
17103 {
17104 double ppi;
17105 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17106 || (CONSP (Vdisplay_pixels_per_inch)
17107 && (ppi = (width_p
17108 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17109 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17110 ppi > 0)))
17111 return OK_PIXELS (ppi / pixels);
17112
17113 return 0;
17114 }
17115 }
17116
17117 #ifdef HAVE_WINDOW_SYSTEM
17118 if (EQ (prop, Qheight))
17119 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17120 if (EQ (prop, Qwidth))
17121 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17122 #else
17123 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17124 return OK_PIXELS (1);
17125 #endif
17126
17127 if (EQ (prop, Qtext))
17128 return OK_PIXELS (width_p
17129 ? window_box_width (it->w, TEXT_AREA)
17130 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17131
17132 if (align_to && *align_to < 0)
17133 {
17134 *res = 0;
17135 if (EQ (prop, Qleft))
17136 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17137 if (EQ (prop, Qright))
17138 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17139 if (EQ (prop, Qcenter))
17140 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17141 + window_box_width (it->w, TEXT_AREA) / 2);
17142 if (EQ (prop, Qleft_fringe))
17143 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17144 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17145 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17146 if (EQ (prop, Qright_fringe))
17147 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17148 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17149 : window_box_right_offset (it->w, TEXT_AREA));
17150 if (EQ (prop, Qleft_margin))
17151 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17152 if (EQ (prop, Qright_margin))
17153 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17154 if (EQ (prop, Qscroll_bar))
17155 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17156 ? 0
17157 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17158 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17159 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17160 : 0)));
17161 }
17162 else
17163 {
17164 if (EQ (prop, Qleft_fringe))
17165 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17166 if (EQ (prop, Qright_fringe))
17167 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17168 if (EQ (prop, Qleft_margin))
17169 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17170 if (EQ (prop, Qright_margin))
17171 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17172 if (EQ (prop, Qscroll_bar))
17173 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17174 }
17175
17176 prop = Fbuffer_local_value (prop, it->w->buffer);
17177 }
17178
17179 if (INTEGERP (prop) || FLOATP (prop))
17180 {
17181 int base_unit = (width_p
17182 ? FRAME_COLUMN_WIDTH (it->f)
17183 : FRAME_LINE_HEIGHT (it->f));
17184 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17185 }
17186
17187 if (CONSP (prop))
17188 {
17189 Lisp_Object car = XCAR (prop);
17190 Lisp_Object cdr = XCDR (prop);
17191
17192 if (SYMBOLP (car))
17193 {
17194 #ifdef HAVE_WINDOW_SYSTEM
17195 if (valid_image_p (prop))
17196 {
17197 int id = lookup_image (it->f, prop);
17198 struct image *img = IMAGE_FROM_ID (it->f, id);
17199
17200 return OK_PIXELS (width_p ? img->width : img->height);
17201 }
17202 #endif
17203 if (EQ (car, Qplus) || EQ (car, Qminus))
17204 {
17205 int first = 1;
17206 double px;
17207
17208 pixels = 0;
17209 while (CONSP (cdr))
17210 {
17211 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17212 font, width_p, align_to))
17213 return 0;
17214 if (first)
17215 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17216 else
17217 pixels += px;
17218 cdr = XCDR (cdr);
17219 }
17220 if (EQ (car, Qminus))
17221 pixels = -pixels;
17222 return OK_PIXELS (pixels);
17223 }
17224
17225 car = Fbuffer_local_value (car, it->w->buffer);
17226 }
17227
17228 if (INTEGERP (car) || FLOATP (car))
17229 {
17230 double fact;
17231 pixels = XFLOATINT (car);
17232 if (NILP (cdr))
17233 return OK_PIXELS (pixels);
17234 if (calc_pixel_width_or_height (&fact, it, cdr,
17235 font, width_p, align_to))
17236 return OK_PIXELS (pixels * fact);
17237 return 0;
17238 }
17239
17240 return 0;
17241 }
17242
17243 return 0;
17244 }
17245
17246 \f
17247 /***********************************************************************
17248 Glyph Display
17249 ***********************************************************************/
17250
17251 #ifdef HAVE_WINDOW_SYSTEM
17252
17253 #if GLYPH_DEBUG
17254
17255 void
17256 dump_glyph_string (s)
17257 struct glyph_string *s;
17258 {
17259 fprintf (stderr, "glyph string\n");
17260 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17261 s->x, s->y, s->width, s->height);
17262 fprintf (stderr, " ybase = %d\n", s->ybase);
17263 fprintf (stderr, " hl = %d\n", s->hl);
17264 fprintf (stderr, " left overhang = %d, right = %d\n",
17265 s->left_overhang, s->right_overhang);
17266 fprintf (stderr, " nchars = %d\n", s->nchars);
17267 fprintf (stderr, " extends to end of line = %d\n",
17268 s->extends_to_end_of_line_p);
17269 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17270 fprintf (stderr, " bg width = %d\n", s->background_width);
17271 }
17272
17273 #endif /* GLYPH_DEBUG */
17274
17275 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17276 of XChar2b structures for S; it can't be allocated in
17277 init_glyph_string because it must be allocated via `alloca'. W
17278 is the window on which S is drawn. ROW and AREA are the glyph row
17279 and area within the row from which S is constructed. START is the
17280 index of the first glyph structure covered by S. HL is a
17281 face-override for drawing S. */
17282
17283 #ifdef HAVE_NTGUI
17284 #define OPTIONAL_HDC(hdc) hdc,
17285 #define DECLARE_HDC(hdc) HDC hdc;
17286 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17287 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17288 #endif
17289
17290 #ifndef OPTIONAL_HDC
17291 #define OPTIONAL_HDC(hdc)
17292 #define DECLARE_HDC(hdc)
17293 #define ALLOCATE_HDC(hdc, f)
17294 #define RELEASE_HDC(hdc, f)
17295 #endif
17296
17297 static void
17298 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17299 struct glyph_string *s;
17300 DECLARE_HDC (hdc)
17301 XChar2b *char2b;
17302 struct window *w;
17303 struct glyph_row *row;
17304 enum glyph_row_area area;
17305 int start;
17306 enum draw_glyphs_face hl;
17307 {
17308 bzero (s, sizeof *s);
17309 s->w = w;
17310 s->f = XFRAME (w->frame);
17311 #ifdef HAVE_NTGUI
17312 s->hdc = hdc;
17313 #endif
17314 s->display = FRAME_X_DISPLAY (s->f);
17315 s->window = FRAME_X_WINDOW (s->f);
17316 s->char2b = char2b;
17317 s->hl = hl;
17318 s->row = row;
17319 s->area = area;
17320 s->first_glyph = row->glyphs[area] + start;
17321 s->height = row->height;
17322 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17323
17324 /* Display the internal border below the tool-bar window. */
17325 if (WINDOWP (s->f->tool_bar_window)
17326 && s->w == XWINDOW (s->f->tool_bar_window))
17327 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17328
17329 s->ybase = s->y + row->ascent;
17330 }
17331
17332
17333 /* Append the list of glyph strings with head H and tail T to the list
17334 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17335
17336 static INLINE void
17337 append_glyph_string_lists (head, tail, h, t)
17338 struct glyph_string **head, **tail;
17339 struct glyph_string *h, *t;
17340 {
17341 if (h)
17342 {
17343 if (*head)
17344 (*tail)->next = h;
17345 else
17346 *head = h;
17347 h->prev = *tail;
17348 *tail = t;
17349 }
17350 }
17351
17352
17353 /* Prepend the list of glyph strings with head H and tail T to the
17354 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17355 result. */
17356
17357 static INLINE void
17358 prepend_glyph_string_lists (head, tail, h, t)
17359 struct glyph_string **head, **tail;
17360 struct glyph_string *h, *t;
17361 {
17362 if (h)
17363 {
17364 if (*head)
17365 (*head)->prev = t;
17366 else
17367 *tail = t;
17368 t->next = *head;
17369 *head = h;
17370 }
17371 }
17372
17373
17374 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17375 Set *HEAD and *TAIL to the resulting list. */
17376
17377 static INLINE void
17378 append_glyph_string (head, tail, s)
17379 struct glyph_string **head, **tail;
17380 struct glyph_string *s;
17381 {
17382 s->next = s->prev = NULL;
17383 append_glyph_string_lists (head, tail, s, s);
17384 }
17385
17386
17387 /* Get face and two-byte form of character glyph GLYPH on frame F.
17388 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17389 a pointer to a realized face that is ready for display. */
17390
17391 static INLINE struct face *
17392 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17393 struct frame *f;
17394 struct glyph *glyph;
17395 XChar2b *char2b;
17396 int *two_byte_p;
17397 {
17398 struct face *face;
17399
17400 xassert (glyph->type == CHAR_GLYPH);
17401 face = FACE_FROM_ID (f, glyph->face_id);
17402
17403 if (two_byte_p)
17404 *two_byte_p = 0;
17405
17406 if (!glyph->multibyte_p)
17407 {
17408 /* Unibyte case. We don't have to encode, but we have to make
17409 sure to use a face suitable for unibyte. */
17410 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17411 }
17412 else if (glyph->u.ch < 128
17413 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17414 {
17415 /* Case of ASCII in a face known to fit ASCII. */
17416 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17417 }
17418 else
17419 {
17420 int c1, c2, charset;
17421
17422 /* Split characters into bytes. If c2 is -1 afterwards, C is
17423 really a one-byte character so that byte1 is zero. */
17424 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17425 if (c2 > 0)
17426 STORE_XCHAR2B (char2b, c1, c2);
17427 else
17428 STORE_XCHAR2B (char2b, 0, c1);
17429
17430 /* Maybe encode the character in *CHAR2B. */
17431 if (charset != CHARSET_ASCII)
17432 {
17433 struct font_info *font_info
17434 = FONT_INFO_FROM_ID (f, face->font_info_id);
17435 if (font_info)
17436 glyph->font_type
17437 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17438 }
17439 }
17440
17441 /* Make sure X resources of the face are allocated. */
17442 xassert (face != NULL);
17443 PREPARE_FACE_FOR_DISPLAY (f, face);
17444 return face;
17445 }
17446
17447
17448 /* Fill glyph string S with composition components specified by S->cmp.
17449
17450 FACES is an array of faces for all components of this composition.
17451 S->gidx is the index of the first component for S.
17452 OVERLAPS_P non-zero means S should draw the foreground only, and
17453 use its physical height for clipping.
17454
17455 Value is the index of a component not in S. */
17456
17457 static int
17458 fill_composite_glyph_string (s, faces, overlaps_p)
17459 struct glyph_string *s;
17460 struct face **faces;
17461 int overlaps_p;
17462 {
17463 int i;
17464
17465 xassert (s);
17466
17467 s->for_overlaps_p = overlaps_p;
17468
17469 s->face = faces[s->gidx];
17470 s->font = s->face->font;
17471 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17472
17473 /* For all glyphs of this composition, starting at the offset
17474 S->gidx, until we reach the end of the definition or encounter a
17475 glyph that requires the different face, add it to S. */
17476 ++s->nchars;
17477 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17478 ++s->nchars;
17479
17480 /* All glyph strings for the same composition has the same width,
17481 i.e. the width set for the first component of the composition. */
17482
17483 s->width = s->first_glyph->pixel_width;
17484
17485 /* If the specified font could not be loaded, use the frame's
17486 default font, but record the fact that we couldn't load it in
17487 the glyph string so that we can draw rectangles for the
17488 characters of the glyph string. */
17489 if (s->font == NULL)
17490 {
17491 s->font_not_found_p = 1;
17492 s->font = FRAME_FONT (s->f);
17493 }
17494
17495 /* Adjust base line for subscript/superscript text. */
17496 s->ybase += s->first_glyph->voffset;
17497
17498 xassert (s->face && s->face->gc);
17499
17500 /* This glyph string must always be drawn with 16-bit functions. */
17501 s->two_byte_p = 1;
17502
17503 return s->gidx + s->nchars;
17504 }
17505
17506
17507 /* Fill glyph string S from a sequence of character glyphs.
17508
17509 FACE_ID is the face id of the string. START is the index of the
17510 first glyph to consider, END is the index of the last + 1.
17511 OVERLAPS_P non-zero means S should draw the foreground only, and
17512 use its physical height for clipping.
17513
17514 Value is the index of the first glyph not in S. */
17515
17516 static int
17517 fill_glyph_string (s, face_id, start, end, overlaps_p)
17518 struct glyph_string *s;
17519 int face_id;
17520 int start, end, overlaps_p;
17521 {
17522 struct glyph *glyph, *last;
17523 int voffset;
17524 int glyph_not_available_p;
17525
17526 xassert (s->f == XFRAME (s->w->frame));
17527 xassert (s->nchars == 0);
17528 xassert (start >= 0 && end > start);
17529
17530 s->for_overlaps_p = overlaps_p,
17531 glyph = s->row->glyphs[s->area] + start;
17532 last = s->row->glyphs[s->area] + end;
17533 voffset = glyph->voffset;
17534
17535 glyph_not_available_p = glyph->glyph_not_available_p;
17536
17537 while (glyph < last
17538 && glyph->type == CHAR_GLYPH
17539 && glyph->voffset == voffset
17540 /* Same face id implies same font, nowadays. */
17541 && glyph->face_id == face_id
17542 && glyph->glyph_not_available_p == glyph_not_available_p)
17543 {
17544 int two_byte_p;
17545
17546 s->face = get_glyph_face_and_encoding (s->f, glyph,
17547 s->char2b + s->nchars,
17548 &two_byte_p);
17549 s->two_byte_p = two_byte_p;
17550 ++s->nchars;
17551 xassert (s->nchars <= end - start);
17552 s->width += glyph->pixel_width;
17553 ++glyph;
17554 }
17555
17556 s->font = s->face->font;
17557 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17558
17559 /* If the specified font could not be loaded, use the frame's font,
17560 but record the fact that we couldn't load it in
17561 S->font_not_found_p so that we can draw rectangles for the
17562 characters of the glyph string. */
17563 if (s->font == NULL || glyph_not_available_p)
17564 {
17565 s->font_not_found_p = 1;
17566 s->font = FRAME_FONT (s->f);
17567 }
17568
17569 /* Adjust base line for subscript/superscript text. */
17570 s->ybase += voffset;
17571
17572 xassert (s->face && s->face->gc);
17573 return glyph - s->row->glyphs[s->area];
17574 }
17575
17576
17577 /* Fill glyph string S from image glyph S->first_glyph. */
17578
17579 static void
17580 fill_image_glyph_string (s)
17581 struct glyph_string *s;
17582 {
17583 xassert (s->first_glyph->type == IMAGE_GLYPH);
17584 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17585 xassert (s->img);
17586 s->slice = s->first_glyph->slice;
17587 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17588 s->font = s->face->font;
17589 s->width = s->first_glyph->pixel_width;
17590
17591 /* Adjust base line for subscript/superscript text. */
17592 s->ybase += s->first_glyph->voffset;
17593 }
17594
17595
17596 /* Fill glyph string S from a sequence of stretch glyphs.
17597
17598 ROW is the glyph row in which the glyphs are found, AREA is the
17599 area within the row. START is the index of the first glyph to
17600 consider, END is the index of the last + 1.
17601
17602 Value is the index of the first glyph not in S. */
17603
17604 static int
17605 fill_stretch_glyph_string (s, row, area, start, end)
17606 struct glyph_string *s;
17607 struct glyph_row *row;
17608 enum glyph_row_area area;
17609 int start, end;
17610 {
17611 struct glyph *glyph, *last;
17612 int voffset, face_id;
17613
17614 xassert (s->first_glyph->type == STRETCH_GLYPH);
17615
17616 glyph = s->row->glyphs[s->area] + start;
17617 last = s->row->glyphs[s->area] + end;
17618 face_id = glyph->face_id;
17619 s->face = FACE_FROM_ID (s->f, face_id);
17620 s->font = s->face->font;
17621 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17622 s->width = glyph->pixel_width;
17623 voffset = glyph->voffset;
17624
17625 for (++glyph;
17626 (glyph < last
17627 && glyph->type == STRETCH_GLYPH
17628 && glyph->voffset == voffset
17629 && glyph->face_id == face_id);
17630 ++glyph)
17631 s->width += glyph->pixel_width;
17632
17633 /* Adjust base line for subscript/superscript text. */
17634 s->ybase += voffset;
17635
17636 /* The case that face->gc == 0 is handled when drawing the glyph
17637 string by calling PREPARE_FACE_FOR_DISPLAY. */
17638 xassert (s->face);
17639 return glyph - s->row->glyphs[s->area];
17640 }
17641
17642
17643 /* EXPORT for RIF:
17644 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17645 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17646 assumed to be zero. */
17647
17648 void
17649 x_get_glyph_overhangs (glyph, f, left, right)
17650 struct glyph *glyph;
17651 struct frame *f;
17652 int *left, *right;
17653 {
17654 *left = *right = 0;
17655
17656 if (glyph->type == CHAR_GLYPH)
17657 {
17658 XFontStruct *font;
17659 struct face *face;
17660 struct font_info *font_info;
17661 XChar2b char2b;
17662 XCharStruct *pcm;
17663
17664 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17665 font = face->font;
17666 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17667 if (font /* ++KFS: Should this be font_info ? */
17668 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17669 {
17670 if (pcm->rbearing > pcm->width)
17671 *right = pcm->rbearing - pcm->width;
17672 if (pcm->lbearing < 0)
17673 *left = -pcm->lbearing;
17674 }
17675 }
17676 }
17677
17678
17679 /* Return the index of the first glyph preceding glyph string S that
17680 is overwritten by S because of S's left overhang. Value is -1
17681 if no glyphs are overwritten. */
17682
17683 static int
17684 left_overwritten (s)
17685 struct glyph_string *s;
17686 {
17687 int k;
17688
17689 if (s->left_overhang)
17690 {
17691 int x = 0, i;
17692 struct glyph *glyphs = s->row->glyphs[s->area];
17693 int first = s->first_glyph - glyphs;
17694
17695 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17696 x -= glyphs[i].pixel_width;
17697
17698 k = i + 1;
17699 }
17700 else
17701 k = -1;
17702
17703 return k;
17704 }
17705
17706
17707 /* Return the index of the first glyph preceding glyph string S that
17708 is overwriting S because of its right overhang. Value is -1 if no
17709 glyph in front of S overwrites S. */
17710
17711 static int
17712 left_overwriting (s)
17713 struct glyph_string *s;
17714 {
17715 int i, k, x;
17716 struct glyph *glyphs = s->row->glyphs[s->area];
17717 int first = s->first_glyph - glyphs;
17718
17719 k = -1;
17720 x = 0;
17721 for (i = first - 1; i >= 0; --i)
17722 {
17723 int left, right;
17724 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17725 if (x + right > 0)
17726 k = i;
17727 x -= glyphs[i].pixel_width;
17728 }
17729
17730 return k;
17731 }
17732
17733
17734 /* Return the index of the last glyph following glyph string S that is
17735 not overwritten by S because of S's right overhang. Value is -1 if
17736 no such glyph is found. */
17737
17738 static int
17739 right_overwritten (s)
17740 struct glyph_string *s;
17741 {
17742 int k = -1;
17743
17744 if (s->right_overhang)
17745 {
17746 int x = 0, i;
17747 struct glyph *glyphs = s->row->glyphs[s->area];
17748 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17749 int end = s->row->used[s->area];
17750
17751 for (i = first; i < end && s->right_overhang > x; ++i)
17752 x += glyphs[i].pixel_width;
17753
17754 k = i;
17755 }
17756
17757 return k;
17758 }
17759
17760
17761 /* Return the index of the last glyph following glyph string S that
17762 overwrites S because of its left overhang. Value is negative
17763 if no such glyph is found. */
17764
17765 static int
17766 right_overwriting (s)
17767 struct glyph_string *s;
17768 {
17769 int i, k, x;
17770 int end = s->row->used[s->area];
17771 struct glyph *glyphs = s->row->glyphs[s->area];
17772 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17773
17774 k = -1;
17775 x = 0;
17776 for (i = first; i < end; ++i)
17777 {
17778 int left, right;
17779 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17780 if (x - left < 0)
17781 k = i;
17782 x += glyphs[i].pixel_width;
17783 }
17784
17785 return k;
17786 }
17787
17788
17789 /* Get face and two-byte form of character C in face FACE_ID on frame
17790 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17791 means we want to display multibyte text. DISPLAY_P non-zero means
17792 make sure that X resources for the face returned are allocated.
17793 Value is a pointer to a realized face that is ready for display if
17794 DISPLAY_P is non-zero. */
17795
17796 static INLINE struct face *
17797 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17798 struct frame *f;
17799 int c, face_id;
17800 XChar2b *char2b;
17801 int multibyte_p, display_p;
17802 {
17803 struct face *face = FACE_FROM_ID (f, face_id);
17804
17805 if (!multibyte_p)
17806 {
17807 /* Unibyte case. We don't have to encode, but we have to make
17808 sure to use a face suitable for unibyte. */
17809 STORE_XCHAR2B (char2b, 0, c);
17810 face_id = FACE_FOR_CHAR (f, face, c);
17811 face = FACE_FROM_ID (f, face_id);
17812 }
17813 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17814 {
17815 /* Case of ASCII in a face known to fit ASCII. */
17816 STORE_XCHAR2B (char2b, 0, c);
17817 }
17818 else
17819 {
17820 int c1, c2, charset;
17821
17822 /* Split characters into bytes. If c2 is -1 afterwards, C is
17823 really a one-byte character so that byte1 is zero. */
17824 SPLIT_CHAR (c, charset, c1, c2);
17825 if (c2 > 0)
17826 STORE_XCHAR2B (char2b, c1, c2);
17827 else
17828 STORE_XCHAR2B (char2b, 0, c1);
17829
17830 /* Maybe encode the character in *CHAR2B. */
17831 if (face->font != NULL)
17832 {
17833 struct font_info *font_info
17834 = FONT_INFO_FROM_ID (f, face->font_info_id);
17835 if (font_info)
17836 rif->encode_char (c, char2b, font_info, 0);
17837 }
17838 }
17839
17840 /* Make sure X resources of the face are allocated. */
17841 #ifdef HAVE_X_WINDOWS
17842 if (display_p)
17843 #endif
17844 {
17845 xassert (face != NULL);
17846 PREPARE_FACE_FOR_DISPLAY (f, face);
17847 }
17848
17849 return face;
17850 }
17851
17852
17853 /* Set background width of glyph string S. START is the index of the
17854 first glyph following S. LAST_X is the right-most x-position + 1
17855 in the drawing area. */
17856
17857 static INLINE void
17858 set_glyph_string_background_width (s, start, last_x)
17859 struct glyph_string *s;
17860 int start;
17861 int last_x;
17862 {
17863 /* If the face of this glyph string has to be drawn to the end of
17864 the drawing area, set S->extends_to_end_of_line_p. */
17865 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17866
17867 if (start == s->row->used[s->area]
17868 && s->area == TEXT_AREA
17869 && ((s->hl == DRAW_NORMAL_TEXT
17870 && (s->row->fill_line_p
17871 || s->face->background != default_face->background
17872 || s->face->stipple != default_face->stipple
17873 || s->row->mouse_face_p))
17874 || s->hl == DRAW_MOUSE_FACE
17875 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17876 && s->row->fill_line_p)))
17877 s->extends_to_end_of_line_p = 1;
17878
17879 /* If S extends its face to the end of the line, set its
17880 background_width to the distance to the right edge of the drawing
17881 area. */
17882 if (s->extends_to_end_of_line_p)
17883 s->background_width = last_x - s->x + 1;
17884 else
17885 s->background_width = s->width;
17886 }
17887
17888
17889 /* Compute overhangs and x-positions for glyph string S and its
17890 predecessors, or successors. X is the starting x-position for S.
17891 BACKWARD_P non-zero means process predecessors. */
17892
17893 static void
17894 compute_overhangs_and_x (s, x, backward_p)
17895 struct glyph_string *s;
17896 int x;
17897 int backward_p;
17898 {
17899 if (backward_p)
17900 {
17901 while (s)
17902 {
17903 if (rif->compute_glyph_string_overhangs)
17904 rif->compute_glyph_string_overhangs (s);
17905 x -= s->width;
17906 s->x = x;
17907 s = s->prev;
17908 }
17909 }
17910 else
17911 {
17912 while (s)
17913 {
17914 if (rif->compute_glyph_string_overhangs)
17915 rif->compute_glyph_string_overhangs (s);
17916 s->x = x;
17917 x += s->width;
17918 s = s->next;
17919 }
17920 }
17921 }
17922
17923
17924
17925 /* The following macros are only called from draw_glyphs below.
17926 They reference the following parameters of that function directly:
17927 `w', `row', `area', and `overlap_p'
17928 as well as the following local variables:
17929 `s', `f', and `hdc' (in W32) */
17930
17931 #ifdef HAVE_NTGUI
17932 /* On W32, silently add local `hdc' variable to argument list of
17933 init_glyph_string. */
17934 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17935 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17936 #else
17937 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17938 init_glyph_string (s, char2b, w, row, area, start, hl)
17939 #endif
17940
17941 /* Add a glyph string for a stretch glyph to the list of strings
17942 between HEAD and TAIL. START is the index of the stretch glyph in
17943 row area AREA of glyph row ROW. END is the index of the last glyph
17944 in that glyph row area. X is the current output position assigned
17945 to the new glyph string constructed. HL overrides that face of the
17946 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17947 is the right-most x-position of the drawing area. */
17948
17949 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17950 and below -- keep them on one line. */
17951 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17952 do \
17953 { \
17954 s = (struct glyph_string *) alloca (sizeof *s); \
17955 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17956 START = fill_stretch_glyph_string (s, row, area, START, END); \
17957 append_glyph_string (&HEAD, &TAIL, s); \
17958 s->x = (X); \
17959 } \
17960 while (0)
17961
17962
17963 /* Add a glyph string for an image glyph to the list of strings
17964 between HEAD and TAIL. START is the index of the image glyph in
17965 row area AREA of glyph row ROW. END is the index of the last glyph
17966 in that glyph row area. X is the current output position assigned
17967 to the new glyph string constructed. HL overrides that face of the
17968 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17969 is the right-most x-position of the drawing area. */
17970
17971 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17972 do \
17973 { \
17974 s = (struct glyph_string *) alloca (sizeof *s); \
17975 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17976 fill_image_glyph_string (s); \
17977 append_glyph_string (&HEAD, &TAIL, s); \
17978 ++START; \
17979 s->x = (X); \
17980 } \
17981 while (0)
17982
17983
17984 /* Add a glyph string for a sequence of character glyphs to the list
17985 of strings between HEAD and TAIL. START is the index of the first
17986 glyph in row area AREA of glyph row ROW that is part of the new
17987 glyph string. END is the index of the last glyph in that glyph row
17988 area. X is the current output position assigned to the new glyph
17989 string constructed. HL overrides that face of the glyph; e.g. it
17990 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17991 right-most x-position of the drawing area. */
17992
17993 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17994 do \
17995 { \
17996 int c, face_id; \
17997 XChar2b *char2b; \
17998 \
17999 c = (row)->glyphs[area][START].u.ch; \
18000 face_id = (row)->glyphs[area][START].face_id; \
18001 \
18002 s = (struct glyph_string *) alloca (sizeof *s); \
18003 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18004 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18005 append_glyph_string (&HEAD, &TAIL, s); \
18006 s->x = (X); \
18007 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18008 } \
18009 while (0)
18010
18011
18012 /* Add a glyph string for a composite sequence to the list of strings
18013 between HEAD and TAIL. START is the index of the first glyph in
18014 row area AREA of glyph row ROW that is part of the new glyph
18015 string. END is the index of the last glyph in that glyph row area.
18016 X is the current output position assigned to the new glyph string
18017 constructed. HL overrides that face of the glyph; e.g. it is
18018 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18019 x-position of the drawing area. */
18020
18021 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18022 do { \
18023 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18024 int face_id = (row)->glyphs[area][START].face_id; \
18025 struct face *base_face = FACE_FROM_ID (f, face_id); \
18026 struct composition *cmp = composition_table[cmp_id]; \
18027 int glyph_len = cmp->glyph_len; \
18028 XChar2b *char2b; \
18029 struct face **faces; \
18030 struct glyph_string *first_s = NULL; \
18031 int n; \
18032 \
18033 base_face = base_face->ascii_face; \
18034 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18035 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18036 /* At first, fill in `char2b' and `faces'. */ \
18037 for (n = 0; n < glyph_len; n++) \
18038 { \
18039 int c = COMPOSITION_GLYPH (cmp, n); \
18040 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18041 faces[n] = FACE_FROM_ID (f, this_face_id); \
18042 get_char_face_and_encoding (f, c, this_face_id, \
18043 char2b + n, 1, 1); \
18044 } \
18045 \
18046 /* Make glyph_strings for each glyph sequence that is drawable by \
18047 the same face, and append them to HEAD/TAIL. */ \
18048 for (n = 0; n < cmp->glyph_len;) \
18049 { \
18050 s = (struct glyph_string *) alloca (sizeof *s); \
18051 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18052 append_glyph_string (&(HEAD), &(TAIL), s); \
18053 s->cmp = cmp; \
18054 s->gidx = n; \
18055 s->x = (X); \
18056 \
18057 if (n == 0) \
18058 first_s = s; \
18059 \
18060 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18061 } \
18062 \
18063 ++START; \
18064 s = first_s; \
18065 } while (0)
18066
18067
18068 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18069 of AREA of glyph row ROW on window W between indices START and END.
18070 HL overrides the face for drawing glyph strings, e.g. it is
18071 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18072 x-positions of the drawing area.
18073
18074 This is an ugly monster macro construct because we must use alloca
18075 to allocate glyph strings (because draw_glyphs can be called
18076 asynchronously). */
18077
18078 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18079 do \
18080 { \
18081 HEAD = TAIL = NULL; \
18082 while (START < END) \
18083 { \
18084 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18085 switch (first_glyph->type) \
18086 { \
18087 case CHAR_GLYPH: \
18088 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18089 HL, X, LAST_X); \
18090 break; \
18091 \
18092 case COMPOSITE_GLYPH: \
18093 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18094 HL, X, LAST_X); \
18095 break; \
18096 \
18097 case STRETCH_GLYPH: \
18098 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18099 HL, X, LAST_X); \
18100 break; \
18101 \
18102 case IMAGE_GLYPH: \
18103 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18104 HL, X, LAST_X); \
18105 break; \
18106 \
18107 default: \
18108 abort (); \
18109 } \
18110 \
18111 set_glyph_string_background_width (s, START, LAST_X); \
18112 (X) += s->width; \
18113 } \
18114 } \
18115 while (0)
18116
18117
18118 /* Draw glyphs between START and END in AREA of ROW on window W,
18119 starting at x-position X. X is relative to AREA in W. HL is a
18120 face-override with the following meaning:
18121
18122 DRAW_NORMAL_TEXT draw normally
18123 DRAW_CURSOR draw in cursor face
18124 DRAW_MOUSE_FACE draw in mouse face.
18125 DRAW_INVERSE_VIDEO draw in mode line face
18126 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18127 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18128
18129 If OVERLAPS_P is non-zero, draw only the foreground of characters
18130 and clip to the physical height of ROW.
18131
18132 Value is the x-position reached, relative to AREA of W. */
18133
18134 static int
18135 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18136 struct window *w;
18137 int x;
18138 struct glyph_row *row;
18139 enum glyph_row_area area;
18140 int start, end;
18141 enum draw_glyphs_face hl;
18142 int overlaps_p;
18143 {
18144 struct glyph_string *head, *tail;
18145 struct glyph_string *s;
18146 int last_x, area_width;
18147 int x_reached;
18148 int i, j;
18149 struct frame *f = XFRAME (WINDOW_FRAME (w));
18150 DECLARE_HDC (hdc);
18151
18152 ALLOCATE_HDC (hdc, f);
18153
18154 /* Let's rather be paranoid than getting a SEGV. */
18155 end = min (end, row->used[area]);
18156 start = max (0, start);
18157 start = min (end, start);
18158
18159 /* Translate X to frame coordinates. Set last_x to the right
18160 end of the drawing area. */
18161 if (row->full_width_p)
18162 {
18163 /* X is relative to the left edge of W, without scroll bars
18164 or fringes. */
18165 x += WINDOW_LEFT_EDGE_X (w);
18166 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18167 }
18168 else
18169 {
18170 int area_left = window_box_left (w, area);
18171 x += area_left;
18172 area_width = window_box_width (w, area);
18173 last_x = area_left + area_width;
18174 }
18175
18176 /* Build a doubly-linked list of glyph_string structures between
18177 head and tail from what we have to draw. Note that the macro
18178 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18179 the reason we use a separate variable `i'. */
18180 i = start;
18181 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18182 if (tail)
18183 x_reached = tail->x + tail->background_width;
18184 else
18185 x_reached = x;
18186
18187 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18188 the row, redraw some glyphs in front or following the glyph
18189 strings built above. */
18190 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18191 {
18192 int dummy_x = 0;
18193 struct glyph_string *h, *t;
18194
18195 /* Compute overhangs for all glyph strings. */
18196 if (rif->compute_glyph_string_overhangs)
18197 for (s = head; s; s = s->next)
18198 rif->compute_glyph_string_overhangs (s);
18199
18200 /* Prepend glyph strings for glyphs in front of the first glyph
18201 string that are overwritten because of the first glyph
18202 string's left overhang. The background of all strings
18203 prepended must be drawn because the first glyph string
18204 draws over it. */
18205 i = left_overwritten (head);
18206 if (i >= 0)
18207 {
18208 j = i;
18209 BUILD_GLYPH_STRINGS (j, start, h, t,
18210 DRAW_NORMAL_TEXT, dummy_x, last_x);
18211 start = i;
18212 compute_overhangs_and_x (t, head->x, 1);
18213 prepend_glyph_string_lists (&head, &tail, h, t);
18214 }
18215
18216 /* Prepend glyph strings for glyphs in front of the first glyph
18217 string that overwrite that glyph string because of their
18218 right overhang. For these strings, only the foreground must
18219 be drawn, because it draws over the glyph string at `head'.
18220 The background must not be drawn because this would overwrite
18221 right overhangs of preceding glyphs for which no glyph
18222 strings exist. */
18223 i = left_overwriting (head);
18224 if (i >= 0)
18225 {
18226 BUILD_GLYPH_STRINGS (i, start, h, t,
18227 DRAW_NORMAL_TEXT, dummy_x, last_x);
18228 for (s = h; s; s = s->next)
18229 s->background_filled_p = 1;
18230 compute_overhangs_and_x (t, head->x, 1);
18231 prepend_glyph_string_lists (&head, &tail, h, t);
18232 }
18233
18234 /* Append glyphs strings for glyphs following the last glyph
18235 string tail that are overwritten by tail. The background of
18236 these strings has to be drawn because tail's foreground draws
18237 over it. */
18238 i = right_overwritten (tail);
18239 if (i >= 0)
18240 {
18241 BUILD_GLYPH_STRINGS (end, i, h, t,
18242 DRAW_NORMAL_TEXT, x, last_x);
18243 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18244 append_glyph_string_lists (&head, &tail, h, t);
18245 }
18246
18247 /* Append glyph strings for glyphs following the last glyph
18248 string tail that overwrite tail. The foreground of such
18249 glyphs has to be drawn because it writes into the background
18250 of tail. The background must not be drawn because it could
18251 paint over the foreground of following glyphs. */
18252 i = right_overwriting (tail);
18253 if (i >= 0)
18254 {
18255 BUILD_GLYPH_STRINGS (end, i, h, t,
18256 DRAW_NORMAL_TEXT, x, last_x);
18257 for (s = h; s; s = s->next)
18258 s->background_filled_p = 1;
18259 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18260 append_glyph_string_lists (&head, &tail, h, t);
18261 }
18262 }
18263
18264 /* Draw all strings. */
18265 for (s = head; s; s = s->next)
18266 rif->draw_glyph_string (s);
18267
18268 if (area == TEXT_AREA
18269 && !row->full_width_p
18270 /* When drawing overlapping rows, only the glyph strings'
18271 foreground is drawn, which doesn't erase a cursor
18272 completely. */
18273 && !overlaps_p)
18274 {
18275 int x0 = head ? head->x : x;
18276 int x1 = tail ? tail->x + tail->background_width : x;
18277
18278 int text_left = window_box_left (w, TEXT_AREA);
18279 x0 -= text_left;
18280 x1 -= text_left;
18281
18282 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18283 row->y, MATRIX_ROW_BOTTOM_Y (row));
18284 }
18285
18286 /* Value is the x-position up to which drawn, relative to AREA of W.
18287 This doesn't include parts drawn because of overhangs. */
18288 if (row->full_width_p)
18289 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18290 else
18291 x_reached -= window_box_left (w, area);
18292
18293 RELEASE_HDC (hdc, f);
18294
18295 return x_reached;
18296 }
18297
18298 /* Expand row matrix if too narrow. Don't expand if area
18299 is not present. */
18300
18301 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18302 { \
18303 if (!fonts_changed_p \
18304 && (it->glyph_row->glyphs[area] \
18305 < it->glyph_row->glyphs[area + 1])) \
18306 { \
18307 it->w->ncols_scale_factor++; \
18308 fonts_changed_p = 1; \
18309 } \
18310 }
18311
18312 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18313 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18314
18315 static INLINE void
18316 append_glyph (it)
18317 struct it *it;
18318 {
18319 struct glyph *glyph;
18320 enum glyph_row_area area = it->area;
18321
18322 xassert (it->glyph_row);
18323 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18324
18325 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18326 if (glyph < it->glyph_row->glyphs[area + 1])
18327 {
18328 glyph->charpos = CHARPOS (it->position);
18329 glyph->object = it->object;
18330 glyph->pixel_width = it->pixel_width;
18331 glyph->ascent = it->ascent;
18332 glyph->descent = it->descent;
18333 glyph->voffset = it->voffset;
18334 glyph->type = CHAR_GLYPH;
18335 glyph->multibyte_p = it->multibyte_p;
18336 glyph->left_box_line_p = it->start_of_box_run_p;
18337 glyph->right_box_line_p = it->end_of_box_run_p;
18338 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18339 || it->phys_descent > it->descent);
18340 glyph->padding_p = 0;
18341 glyph->glyph_not_available_p = it->glyph_not_available_p;
18342 glyph->face_id = it->face_id;
18343 glyph->u.ch = it->char_to_display;
18344 glyph->slice = null_glyph_slice;
18345 glyph->font_type = FONT_TYPE_UNKNOWN;
18346 ++it->glyph_row->used[area];
18347 }
18348 else
18349 IT_EXPAND_MATRIX_WIDTH (it, area);
18350 }
18351
18352 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18353 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18354
18355 static INLINE void
18356 append_composite_glyph (it)
18357 struct it *it;
18358 {
18359 struct glyph *glyph;
18360 enum glyph_row_area area = it->area;
18361
18362 xassert (it->glyph_row);
18363
18364 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18365 if (glyph < it->glyph_row->glyphs[area + 1])
18366 {
18367 glyph->charpos = CHARPOS (it->position);
18368 glyph->object = it->object;
18369 glyph->pixel_width = it->pixel_width;
18370 glyph->ascent = it->ascent;
18371 glyph->descent = it->descent;
18372 glyph->voffset = it->voffset;
18373 glyph->type = COMPOSITE_GLYPH;
18374 glyph->multibyte_p = it->multibyte_p;
18375 glyph->left_box_line_p = it->start_of_box_run_p;
18376 glyph->right_box_line_p = it->end_of_box_run_p;
18377 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18378 || it->phys_descent > it->descent);
18379 glyph->padding_p = 0;
18380 glyph->glyph_not_available_p = 0;
18381 glyph->face_id = it->face_id;
18382 glyph->u.cmp_id = it->cmp_id;
18383 glyph->slice = null_glyph_slice;
18384 glyph->font_type = FONT_TYPE_UNKNOWN;
18385 ++it->glyph_row->used[area];
18386 }
18387 else
18388 IT_EXPAND_MATRIX_WIDTH (it, area);
18389 }
18390
18391
18392 /* Change IT->ascent and IT->height according to the setting of
18393 IT->voffset. */
18394
18395 static INLINE void
18396 take_vertical_position_into_account (it)
18397 struct it *it;
18398 {
18399 if (it->voffset)
18400 {
18401 if (it->voffset < 0)
18402 /* Increase the ascent so that we can display the text higher
18403 in the line. */
18404 it->ascent -= it->voffset;
18405 else
18406 /* Increase the descent so that we can display the text lower
18407 in the line. */
18408 it->descent += it->voffset;
18409 }
18410 }
18411
18412
18413 /* Produce glyphs/get display metrics for the image IT is loaded with.
18414 See the description of struct display_iterator in dispextern.h for
18415 an overview of struct display_iterator. */
18416
18417 static void
18418 produce_image_glyph (it)
18419 struct it *it;
18420 {
18421 struct image *img;
18422 struct face *face;
18423 int glyph_ascent;
18424 struct glyph_slice slice;
18425
18426 xassert (it->what == IT_IMAGE);
18427
18428 face = FACE_FROM_ID (it->f, it->face_id);
18429 xassert (face);
18430 /* Make sure X resources of the face is loaded. */
18431 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18432
18433 if (it->image_id < 0)
18434 {
18435 /* Fringe bitmap. */
18436 it->ascent = it->phys_ascent = 0;
18437 it->descent = it->phys_descent = 0;
18438 it->pixel_width = 0;
18439 it->nglyphs = 0;
18440 return;
18441 }
18442
18443 img = IMAGE_FROM_ID (it->f, it->image_id);
18444 xassert (img);
18445 /* Make sure X resources of the image is loaded. */
18446 prepare_image_for_display (it->f, img);
18447
18448 slice.x = slice.y = 0;
18449 slice.width = img->width;
18450 slice.height = img->height;
18451
18452 if (INTEGERP (it->slice.x))
18453 slice.x = XINT (it->slice.x);
18454 else if (FLOATP (it->slice.x))
18455 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18456
18457 if (INTEGERP (it->slice.y))
18458 slice.y = XINT (it->slice.y);
18459 else if (FLOATP (it->slice.y))
18460 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18461
18462 if (INTEGERP (it->slice.width))
18463 slice.width = XINT (it->slice.width);
18464 else if (FLOATP (it->slice.width))
18465 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18466
18467 if (INTEGERP (it->slice.height))
18468 slice.height = XINT (it->slice.height);
18469 else if (FLOATP (it->slice.height))
18470 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18471
18472 if (slice.x >= img->width)
18473 slice.x = img->width;
18474 if (slice.y >= img->height)
18475 slice.y = img->height;
18476 if (slice.x + slice.width >= img->width)
18477 slice.width = img->width - slice.x;
18478 if (slice.y + slice.height > img->height)
18479 slice.height = img->height - slice.y;
18480
18481 if (slice.width == 0 || slice.height == 0)
18482 return;
18483
18484 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18485
18486 it->descent = slice.height - glyph_ascent;
18487 if (slice.y == 0)
18488 it->descent += img->vmargin;
18489 if (slice.y + slice.height == img->height)
18490 it->descent += img->vmargin;
18491 it->phys_descent = it->descent;
18492
18493 it->pixel_width = slice.width;
18494 if (slice.x == 0)
18495 it->pixel_width += img->hmargin;
18496 if (slice.x + slice.width == img->width)
18497 it->pixel_width += img->hmargin;
18498
18499 /* It's quite possible for images to have an ascent greater than
18500 their height, so don't get confused in that case. */
18501 if (it->descent < 0)
18502 it->descent = 0;
18503
18504 #if 0 /* this breaks image tiling */
18505 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18506 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18507 if (face_ascent > it->ascent)
18508 it->ascent = it->phys_ascent = face_ascent;
18509 #endif
18510
18511 it->nglyphs = 1;
18512
18513 if (face->box != FACE_NO_BOX)
18514 {
18515 if (face->box_line_width > 0)
18516 {
18517 if (slice.y == 0)
18518 it->ascent += face->box_line_width;
18519 if (slice.y + slice.height == img->height)
18520 it->descent += face->box_line_width;
18521 }
18522
18523 if (it->start_of_box_run_p && slice.x == 0)
18524 it->pixel_width += abs (face->box_line_width);
18525 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18526 it->pixel_width += abs (face->box_line_width);
18527 }
18528
18529 take_vertical_position_into_account (it);
18530
18531 if (it->glyph_row)
18532 {
18533 struct glyph *glyph;
18534 enum glyph_row_area area = it->area;
18535
18536 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18537 if (glyph < it->glyph_row->glyphs[area + 1])
18538 {
18539 glyph->charpos = CHARPOS (it->position);
18540 glyph->object = it->object;
18541 glyph->pixel_width = it->pixel_width;
18542 glyph->ascent = glyph_ascent;
18543 glyph->descent = it->descent;
18544 glyph->voffset = it->voffset;
18545 glyph->type = IMAGE_GLYPH;
18546 glyph->multibyte_p = it->multibyte_p;
18547 glyph->left_box_line_p = it->start_of_box_run_p;
18548 glyph->right_box_line_p = it->end_of_box_run_p;
18549 glyph->overlaps_vertically_p = 0;
18550 glyph->padding_p = 0;
18551 glyph->glyph_not_available_p = 0;
18552 glyph->face_id = it->face_id;
18553 glyph->u.img_id = img->id;
18554 glyph->slice = slice;
18555 glyph->font_type = FONT_TYPE_UNKNOWN;
18556 ++it->glyph_row->used[area];
18557 }
18558 else
18559 IT_EXPAND_MATRIX_WIDTH (it, area);
18560 }
18561 }
18562
18563
18564 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18565 of the glyph, WIDTH and HEIGHT are the width and height of the
18566 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18567
18568 static void
18569 append_stretch_glyph (it, object, width, height, ascent)
18570 struct it *it;
18571 Lisp_Object object;
18572 int width, height;
18573 int ascent;
18574 {
18575 struct glyph *glyph;
18576 enum glyph_row_area area = it->area;
18577
18578 xassert (ascent >= 0 && ascent <= height);
18579
18580 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18581 if (glyph < it->glyph_row->glyphs[area + 1])
18582 {
18583 glyph->charpos = CHARPOS (it->position);
18584 glyph->object = object;
18585 glyph->pixel_width = width;
18586 glyph->ascent = ascent;
18587 glyph->descent = height - ascent;
18588 glyph->voffset = it->voffset;
18589 glyph->type = STRETCH_GLYPH;
18590 glyph->multibyte_p = it->multibyte_p;
18591 glyph->left_box_line_p = it->start_of_box_run_p;
18592 glyph->right_box_line_p = it->end_of_box_run_p;
18593 glyph->overlaps_vertically_p = 0;
18594 glyph->padding_p = 0;
18595 glyph->glyph_not_available_p = 0;
18596 glyph->face_id = it->face_id;
18597 glyph->u.stretch.ascent = ascent;
18598 glyph->u.stretch.height = height;
18599 glyph->slice = null_glyph_slice;
18600 glyph->font_type = FONT_TYPE_UNKNOWN;
18601 ++it->glyph_row->used[area];
18602 }
18603 else
18604 IT_EXPAND_MATRIX_WIDTH (it, area);
18605 }
18606
18607
18608 /* Produce a stretch glyph for iterator IT. IT->object is the value
18609 of the glyph property displayed. The value must be a list
18610 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18611 being recognized:
18612
18613 1. `:width WIDTH' specifies that the space should be WIDTH *
18614 canonical char width wide. WIDTH may be an integer or floating
18615 point number.
18616
18617 2. `:relative-width FACTOR' specifies that the width of the stretch
18618 should be computed from the width of the first character having the
18619 `glyph' property, and should be FACTOR times that width.
18620
18621 3. `:align-to HPOS' specifies that the space should be wide enough
18622 to reach HPOS, a value in canonical character units.
18623
18624 Exactly one of the above pairs must be present.
18625
18626 4. `:height HEIGHT' specifies that the height of the stretch produced
18627 should be HEIGHT, measured in canonical character units.
18628
18629 5. `:relative-height FACTOR' specifies that the height of the
18630 stretch should be FACTOR times the height of the characters having
18631 the glyph property.
18632
18633 Either none or exactly one of 4 or 5 must be present.
18634
18635 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18636 of the stretch should be used for the ascent of the stretch.
18637 ASCENT must be in the range 0 <= ASCENT <= 100. */
18638
18639 static void
18640 produce_stretch_glyph (it)
18641 struct it *it;
18642 {
18643 /* (space :width WIDTH :height HEIGHT ...) */
18644 Lisp_Object prop, plist;
18645 int width = 0, height = 0, align_to = -1;
18646 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18647 int ascent = 0;
18648 double tem;
18649 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18650 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18651
18652 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18653
18654 /* List should start with `space'. */
18655 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18656 plist = XCDR (it->object);
18657
18658 /* Compute the width of the stretch. */
18659 if ((prop = Fsafe_plist_get (plist, QCwidth), !NILP (prop))
18660 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18661 {
18662 /* Absolute width `:width WIDTH' specified and valid. */
18663 zero_width_ok_p = 1;
18664 width = (int)tem;
18665 }
18666 else if (prop = Fsafe_plist_get (plist, QCrelative_width),
18667 NUMVAL (prop) > 0)
18668 {
18669 /* Relative width `:relative-width FACTOR' specified and valid.
18670 Compute the width of the characters having the `glyph'
18671 property. */
18672 struct it it2;
18673 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18674
18675 it2 = *it;
18676 if (it->multibyte_p)
18677 {
18678 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18679 - IT_BYTEPOS (*it));
18680 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18681 }
18682 else
18683 it2.c = *p, it2.len = 1;
18684
18685 it2.glyph_row = NULL;
18686 it2.what = IT_CHARACTER;
18687 x_produce_glyphs (&it2);
18688 width = NUMVAL (prop) * it2.pixel_width;
18689 }
18690 else if ((prop = Fsafe_plist_get (plist, QCalign_to), !NILP (prop))
18691 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18692 {
18693 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18694 align_to = (align_to < 0
18695 ? 0
18696 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18697 else if (align_to < 0)
18698 align_to = window_box_left_offset (it->w, TEXT_AREA);
18699 width = max (0, (int)tem + align_to - it->current_x);
18700 zero_width_ok_p = 1;
18701 }
18702 else
18703 /* Nothing specified -> width defaults to canonical char width. */
18704 width = FRAME_COLUMN_WIDTH (it->f);
18705
18706 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18707 width = 1;
18708
18709 /* Compute height. */
18710 if ((prop = Fsafe_plist_get (plist, QCheight), !NILP (prop))
18711 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18712 {
18713 height = (int)tem;
18714 zero_height_ok_p = 1;
18715 }
18716 else if (prop = Fsafe_plist_get (plist, QCrelative_height),
18717 NUMVAL (prop) > 0)
18718 height = FONT_HEIGHT (font) * NUMVAL (prop);
18719 else
18720 height = FONT_HEIGHT (font);
18721
18722 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18723 height = 1;
18724
18725 /* Compute percentage of height used for ascent. If
18726 `:ascent ASCENT' is present and valid, use that. Otherwise,
18727 derive the ascent from the font in use. */
18728 if (prop = Fsafe_plist_get (plist, QCascent),
18729 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18730 ascent = height * NUMVAL (prop) / 100.0;
18731 else if (!NILP (prop)
18732 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18733 ascent = min (max (0, (int)tem), height);
18734 else
18735 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18736
18737 if (width > 0 && height > 0 && it->glyph_row)
18738 {
18739 Lisp_Object object = it->stack[it->sp - 1].string;
18740 if (!STRINGP (object))
18741 object = it->w->buffer;
18742 append_stretch_glyph (it, object, width, height, ascent);
18743 }
18744
18745 it->pixel_width = width;
18746 it->ascent = it->phys_ascent = ascent;
18747 it->descent = it->phys_descent = height - it->ascent;
18748 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18749
18750 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18751 {
18752 if (face->box_line_width > 0)
18753 {
18754 it->ascent += face->box_line_width;
18755 it->descent += face->box_line_width;
18756 }
18757
18758 if (it->start_of_box_run_p)
18759 it->pixel_width += abs (face->box_line_width);
18760 if (it->end_of_box_run_p)
18761 it->pixel_width += abs (face->box_line_width);
18762 }
18763
18764 take_vertical_position_into_account (it);
18765 }
18766
18767 /* Calculate line-height and line-spacing properties.
18768 An integer value specifies explicit pixel value.
18769 A float value specifies relative value to current face height.
18770 A cons (float . face-name) specifies relative value to
18771 height of specified face font.
18772
18773 Returns height in pixels, or nil. */
18774
18775 static Lisp_Object
18776 calc_line_height_property (it, prop, font, boff, total)
18777 struct it *it;
18778 Lisp_Object prop;
18779 XFontStruct *font;
18780 int boff, *total;
18781 {
18782 Lisp_Object position, val;
18783 Lisp_Object face_name = Qnil;
18784 int ascent, descent, height, override;
18785
18786 if (STRINGP (it->object))
18787 position = make_number (IT_STRING_CHARPOS (*it));
18788 else if (BUFFERP (it->object))
18789 position = make_number (IT_CHARPOS (*it));
18790 else
18791 return Qnil;
18792
18793 val = Fget_char_property (position, prop, it->object);
18794
18795 if (NILP (val))
18796 return val;
18797
18798 if (total && CONSP (val) && EQ (XCAR (val), Qtotal))
18799 {
18800 *total = 1;
18801 val = XCDR (val);
18802 }
18803
18804 if (INTEGERP (val))
18805 return val;
18806
18807 if (CONSP (val))
18808 {
18809 face_name = XCDR (val);
18810 val = XCAR (val);
18811 }
18812 else if (SYMBOLP (val))
18813 {
18814 face_name = val;
18815 val = Qnil;
18816 }
18817
18818 override = EQ (prop, Qline_height);
18819
18820 if (NILP (face_name))
18821 {
18822 font = FRAME_FONT (it->f);
18823 boff = FRAME_BASELINE_OFFSET (it->f);
18824 }
18825 else if (EQ (face_name, Qt))
18826 {
18827 override = 0;
18828 }
18829 else
18830 {
18831 int face_id;
18832 struct face *face;
18833 struct font_info *font_info;
18834
18835 face_id = lookup_named_face (it->f, face_name, ' ', 0);
18836 if (face_id < 0)
18837 return make_number (-1);
18838
18839 face = FACE_FROM_ID (it->f, face_id);
18840 font = face->font;
18841 if (font == NULL)
18842 return make_number (-1);
18843
18844 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18845 boff = font_info->baseline_offset;
18846 if (font_info->vertical_centering)
18847 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18848 }
18849
18850 ascent = FONT_BASE (font) + boff;
18851 descent = FONT_DESCENT (font) - boff;
18852
18853 if (override)
18854 {
18855 it->override_ascent = ascent;
18856 it->override_descent = descent;
18857 it->override_boff = boff;
18858 }
18859
18860 height = ascent + descent;
18861 if (FLOATP (val))
18862 height = (int)(XFLOAT_DATA (val) * height);
18863 else if (INTEGERP (val))
18864 height *= XINT (val);
18865
18866 return make_number (height);
18867 }
18868
18869
18870 /* RIF:
18871 Produce glyphs/get display metrics for the display element IT is
18872 loaded with. See the description of struct display_iterator in
18873 dispextern.h for an overview of struct display_iterator. */
18874
18875 void
18876 x_produce_glyphs (it)
18877 struct it *it;
18878 {
18879 int extra_line_spacing = it->extra_line_spacing;
18880
18881 it->glyph_not_available_p = 0;
18882
18883 if (it->what == IT_CHARACTER)
18884 {
18885 XChar2b char2b;
18886 XFontStruct *font;
18887 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18888 XCharStruct *pcm;
18889 int font_not_found_p;
18890 struct font_info *font_info;
18891 int boff; /* baseline offset */
18892 /* We may change it->multibyte_p upon unibyte<->multibyte
18893 conversion. So, save the current value now and restore it
18894 later.
18895
18896 Note: It seems that we don't have to record multibyte_p in
18897 struct glyph because the character code itself tells if or
18898 not the character is multibyte. Thus, in the future, we must
18899 consider eliminating the field `multibyte_p' in the struct
18900 glyph. */
18901 int saved_multibyte_p = it->multibyte_p;
18902
18903 /* Maybe translate single-byte characters to multibyte, or the
18904 other way. */
18905 it->char_to_display = it->c;
18906 if (!ASCII_BYTE_P (it->c))
18907 {
18908 if (unibyte_display_via_language_environment
18909 && SINGLE_BYTE_CHAR_P (it->c)
18910 && (it->c >= 0240
18911 || !NILP (Vnonascii_translation_table)))
18912 {
18913 it->char_to_display = unibyte_char_to_multibyte (it->c);
18914 it->multibyte_p = 1;
18915 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18916 face = FACE_FROM_ID (it->f, it->face_id);
18917 }
18918 else if (!SINGLE_BYTE_CHAR_P (it->c)
18919 && !it->multibyte_p)
18920 {
18921 it->multibyte_p = 1;
18922 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18923 face = FACE_FROM_ID (it->f, it->face_id);
18924 }
18925 }
18926
18927 /* Get font to use. Encode IT->char_to_display. */
18928 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18929 &char2b, it->multibyte_p, 0);
18930 font = face->font;
18931
18932 /* When no suitable font found, use the default font. */
18933 font_not_found_p = font == NULL;
18934 if (font_not_found_p)
18935 {
18936 font = FRAME_FONT (it->f);
18937 boff = FRAME_BASELINE_OFFSET (it->f);
18938 font_info = NULL;
18939 }
18940 else
18941 {
18942 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18943 boff = font_info->baseline_offset;
18944 if (font_info->vertical_centering)
18945 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18946 }
18947
18948 if (it->char_to_display >= ' '
18949 && (!it->multibyte_p || it->char_to_display < 128))
18950 {
18951 /* Either unibyte or ASCII. */
18952 int stretched_p;
18953
18954 it->nglyphs = 1;
18955
18956 pcm = rif->per_char_metric (font, &char2b,
18957 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18958
18959 if (it->override_ascent >= 0)
18960 {
18961 it->ascent = it->override_ascent;
18962 it->descent = it->override_descent;
18963 boff = it->override_boff;
18964 }
18965 else
18966 {
18967 it->ascent = FONT_BASE (font) + boff;
18968 it->descent = FONT_DESCENT (font) - boff;
18969 }
18970
18971 if (pcm)
18972 {
18973 it->phys_ascent = pcm->ascent + boff;
18974 it->phys_descent = pcm->descent - boff;
18975 it->pixel_width = pcm->width;
18976 }
18977 else
18978 {
18979 it->glyph_not_available_p = 1;
18980 it->phys_ascent = it->ascent;
18981 it->phys_descent = it->descent;
18982 it->pixel_width = FONT_WIDTH (font);
18983 }
18984
18985 if (it->constrain_row_ascent_descent_p)
18986 {
18987 if (it->descent > it->max_descent)
18988 {
18989 it->ascent += it->descent - it->max_descent;
18990 it->descent = it->max_descent;
18991 }
18992 if (it->ascent > it->max_ascent)
18993 {
18994 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
18995 it->ascent = it->max_ascent;
18996 }
18997 it->phys_ascent = min (it->phys_ascent, it->ascent);
18998 it->phys_descent = min (it->phys_descent, it->descent);
18999 extra_line_spacing = 0;
19000 }
19001
19002 /* If this is a space inside a region of text with
19003 `space-width' property, change its width. */
19004 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19005 if (stretched_p)
19006 it->pixel_width *= XFLOATINT (it->space_width);
19007
19008 /* If face has a box, add the box thickness to the character
19009 height. If character has a box line to the left and/or
19010 right, add the box line width to the character's width. */
19011 if (face->box != FACE_NO_BOX)
19012 {
19013 int thick = face->box_line_width;
19014
19015 if (thick > 0)
19016 {
19017 it->ascent += thick;
19018 it->descent += thick;
19019 }
19020 else
19021 thick = -thick;
19022
19023 if (it->start_of_box_run_p)
19024 it->pixel_width += thick;
19025 if (it->end_of_box_run_p)
19026 it->pixel_width += thick;
19027 }
19028
19029 /* If face has an overline, add the height of the overline
19030 (1 pixel) and a 1 pixel margin to the character height. */
19031 if (face->overline_p)
19032 it->ascent += 2;
19033
19034 if (it->constrain_row_ascent_descent_p)
19035 {
19036 if (it->ascent > it->max_ascent)
19037 it->ascent = it->max_ascent;
19038 if (it->descent > it->max_descent)
19039 it->descent = it->max_descent;
19040 }
19041
19042 take_vertical_position_into_account (it);
19043
19044 /* If we have to actually produce glyphs, do it. */
19045 if (it->glyph_row)
19046 {
19047 if (stretched_p)
19048 {
19049 /* Translate a space with a `space-width' property
19050 into a stretch glyph. */
19051 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19052 / FONT_HEIGHT (font));
19053 append_stretch_glyph (it, it->object, it->pixel_width,
19054 it->ascent + it->descent, ascent);
19055 }
19056 else
19057 append_glyph (it);
19058
19059 /* If characters with lbearing or rbearing are displayed
19060 in this line, record that fact in a flag of the
19061 glyph row. This is used to optimize X output code. */
19062 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19063 it->glyph_row->contains_overlapping_glyphs_p = 1;
19064 }
19065 }
19066 else if (it->char_to_display == '\n')
19067 {
19068 /* A newline has no width but we need the height of the line.
19069 But if previous part of the line set a height, don't
19070 increase that height */
19071
19072 Lisp_Object height;
19073
19074 it->override_ascent = -1;
19075 it->pixel_width = 0;
19076 it->nglyphs = 0;
19077
19078 height = calc_line_height_property(it, Qline_height, font, boff, 0);
19079
19080 if (it->override_ascent >= 0)
19081 {
19082 it->ascent = it->override_ascent;
19083 it->descent = it->override_descent;
19084 boff = it->override_boff;
19085 }
19086 else
19087 {
19088 it->ascent = FONT_BASE (font) + boff;
19089 it->descent = FONT_DESCENT (font) - boff;
19090 }
19091
19092 if (EQ (height, make_number(0)))
19093 {
19094 if (it->descent > it->max_descent)
19095 {
19096 it->ascent += it->descent - it->max_descent;
19097 it->descent = it->max_descent;
19098 }
19099 if (it->ascent > it->max_ascent)
19100 {
19101 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19102 it->ascent = it->max_ascent;
19103 }
19104 it->phys_ascent = min (it->phys_ascent, it->ascent);
19105 it->phys_descent = min (it->phys_descent, it->descent);
19106 it->constrain_row_ascent_descent_p = 1;
19107 extra_line_spacing = 0;
19108 }
19109 else
19110 {
19111 Lisp_Object spacing;
19112 int total = 0;
19113
19114 it->phys_ascent = it->ascent;
19115 it->phys_descent = it->descent;
19116
19117 if ((it->max_ascent > 0 || it->max_descent > 0)
19118 && face->box != FACE_NO_BOX
19119 && face->box_line_width > 0)
19120 {
19121 it->ascent += face->box_line_width;
19122 it->descent += face->box_line_width;
19123 }
19124 if (!NILP (height)
19125 && XINT (height) > it->ascent + it->descent)
19126 it->ascent = XINT (height) - it->descent;
19127
19128 spacing = calc_line_height_property(it, Qline_spacing, font, boff, &total);
19129 if (INTEGERP (spacing))
19130 {
19131 extra_line_spacing = XINT (spacing);
19132 if (total)
19133 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19134 }
19135 }
19136 }
19137 else if (it->char_to_display == '\t')
19138 {
19139 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
19140 int x = it->current_x + it->continuation_lines_width;
19141 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19142
19143 /* If the distance from the current position to the next tab
19144 stop is less than a canonical character width, use the
19145 tab stop after that. */
19146 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
19147 next_tab_x += tab_width;
19148
19149 it->pixel_width = next_tab_x - x;
19150 it->nglyphs = 1;
19151 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19152 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19153
19154 if (it->glyph_row)
19155 {
19156 append_stretch_glyph (it, it->object, it->pixel_width,
19157 it->ascent + it->descent, it->ascent);
19158 }
19159 }
19160 else
19161 {
19162 /* A multi-byte character. Assume that the display width of the
19163 character is the width of the character multiplied by the
19164 width of the font. */
19165
19166 /* If we found a font, this font should give us the right
19167 metrics. If we didn't find a font, use the frame's
19168 default font and calculate the width of the character
19169 from the charset width; this is what old redisplay code
19170 did. */
19171
19172 pcm = rif->per_char_metric (font, &char2b,
19173 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19174
19175 if (font_not_found_p || !pcm)
19176 {
19177 int charset = CHAR_CHARSET (it->char_to_display);
19178
19179 it->glyph_not_available_p = 1;
19180 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19181 * CHARSET_WIDTH (charset));
19182 it->phys_ascent = FONT_BASE (font) + boff;
19183 it->phys_descent = FONT_DESCENT (font) - boff;
19184 }
19185 else
19186 {
19187 it->pixel_width = pcm->width;
19188 it->phys_ascent = pcm->ascent + boff;
19189 it->phys_descent = pcm->descent - boff;
19190 if (it->glyph_row
19191 && (pcm->lbearing < 0
19192 || pcm->rbearing > pcm->width))
19193 it->glyph_row->contains_overlapping_glyphs_p = 1;
19194 }
19195 it->nglyphs = 1;
19196 it->ascent = FONT_BASE (font) + boff;
19197 it->descent = FONT_DESCENT (font) - boff;
19198 if (face->box != FACE_NO_BOX)
19199 {
19200 int thick = face->box_line_width;
19201
19202 if (thick > 0)
19203 {
19204 it->ascent += thick;
19205 it->descent += thick;
19206 }
19207 else
19208 thick = - thick;
19209
19210 if (it->start_of_box_run_p)
19211 it->pixel_width += thick;
19212 if (it->end_of_box_run_p)
19213 it->pixel_width += thick;
19214 }
19215
19216 /* If face has an overline, add the height of the overline
19217 (1 pixel) and a 1 pixel margin to the character height. */
19218 if (face->overline_p)
19219 it->ascent += 2;
19220
19221 take_vertical_position_into_account (it);
19222
19223 if (it->glyph_row)
19224 append_glyph (it);
19225 }
19226 it->multibyte_p = saved_multibyte_p;
19227 }
19228 else if (it->what == IT_COMPOSITION)
19229 {
19230 /* Note: A composition is represented as one glyph in the
19231 glyph matrix. There are no padding glyphs. */
19232 XChar2b char2b;
19233 XFontStruct *font;
19234 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19235 XCharStruct *pcm;
19236 int font_not_found_p;
19237 struct font_info *font_info;
19238 int boff; /* baseline offset */
19239 struct composition *cmp = composition_table[it->cmp_id];
19240
19241 /* Maybe translate single-byte characters to multibyte. */
19242 it->char_to_display = it->c;
19243 if (unibyte_display_via_language_environment
19244 && SINGLE_BYTE_CHAR_P (it->c)
19245 && (it->c >= 0240
19246 || (it->c >= 0200
19247 && !NILP (Vnonascii_translation_table))))
19248 {
19249 it->char_to_display = unibyte_char_to_multibyte (it->c);
19250 }
19251
19252 /* Get face and font to use. Encode IT->char_to_display. */
19253 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19254 face = FACE_FROM_ID (it->f, it->face_id);
19255 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19256 &char2b, it->multibyte_p, 0);
19257 font = face->font;
19258
19259 /* When no suitable font found, use the default font. */
19260 font_not_found_p = font == NULL;
19261 if (font_not_found_p)
19262 {
19263 font = FRAME_FONT (it->f);
19264 boff = FRAME_BASELINE_OFFSET (it->f);
19265 font_info = NULL;
19266 }
19267 else
19268 {
19269 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19270 boff = font_info->baseline_offset;
19271 if (font_info->vertical_centering)
19272 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19273 }
19274
19275 /* There are no padding glyphs, so there is only one glyph to
19276 produce for the composition. Important is that pixel_width,
19277 ascent and descent are the values of what is drawn by
19278 draw_glyphs (i.e. the values of the overall glyphs composed). */
19279 it->nglyphs = 1;
19280
19281 /* If we have not yet calculated pixel size data of glyphs of
19282 the composition for the current face font, calculate them
19283 now. Theoretically, we have to check all fonts for the
19284 glyphs, but that requires much time and memory space. So,
19285 here we check only the font of the first glyph. This leads
19286 to incorrect display very rarely, and C-l (recenter) can
19287 correct the display anyway. */
19288 if (cmp->font != (void *) font)
19289 {
19290 /* Ascent and descent of the font of the first character of
19291 this composition (adjusted by baseline offset). Ascent
19292 and descent of overall glyphs should not be less than
19293 them respectively. */
19294 int font_ascent = FONT_BASE (font) + boff;
19295 int font_descent = FONT_DESCENT (font) - boff;
19296 /* Bounding box of the overall glyphs. */
19297 int leftmost, rightmost, lowest, highest;
19298 int i, width, ascent, descent;
19299
19300 cmp->font = (void *) font;
19301
19302 /* Initialize the bounding box. */
19303 if (font_info
19304 && (pcm = rif->per_char_metric (font, &char2b,
19305 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19306 {
19307 width = pcm->width;
19308 ascent = pcm->ascent;
19309 descent = pcm->descent;
19310 }
19311 else
19312 {
19313 width = FONT_WIDTH (font);
19314 ascent = FONT_BASE (font);
19315 descent = FONT_DESCENT (font);
19316 }
19317
19318 rightmost = width;
19319 lowest = - descent + boff;
19320 highest = ascent + boff;
19321 leftmost = 0;
19322
19323 if (font_info
19324 && font_info->default_ascent
19325 && CHAR_TABLE_P (Vuse_default_ascent)
19326 && !NILP (Faref (Vuse_default_ascent,
19327 make_number (it->char_to_display))))
19328 highest = font_info->default_ascent + boff;
19329
19330 /* Draw the first glyph at the normal position. It may be
19331 shifted to right later if some other glyphs are drawn at
19332 the left. */
19333 cmp->offsets[0] = 0;
19334 cmp->offsets[1] = boff;
19335
19336 /* Set cmp->offsets for the remaining glyphs. */
19337 for (i = 1; i < cmp->glyph_len; i++)
19338 {
19339 int left, right, btm, top;
19340 int ch = COMPOSITION_GLYPH (cmp, i);
19341 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19342
19343 face = FACE_FROM_ID (it->f, face_id);
19344 get_char_face_and_encoding (it->f, ch, face->id,
19345 &char2b, it->multibyte_p, 0);
19346 font = face->font;
19347 if (font == NULL)
19348 {
19349 font = FRAME_FONT (it->f);
19350 boff = FRAME_BASELINE_OFFSET (it->f);
19351 font_info = NULL;
19352 }
19353 else
19354 {
19355 font_info
19356 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19357 boff = font_info->baseline_offset;
19358 if (font_info->vertical_centering)
19359 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19360 }
19361
19362 if (font_info
19363 && (pcm = rif->per_char_metric (font, &char2b,
19364 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19365 {
19366 width = pcm->width;
19367 ascent = pcm->ascent;
19368 descent = pcm->descent;
19369 }
19370 else
19371 {
19372 width = FONT_WIDTH (font);
19373 ascent = 1;
19374 descent = 0;
19375 }
19376
19377 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19378 {
19379 /* Relative composition with or without
19380 alternate chars. */
19381 left = (leftmost + rightmost - width) / 2;
19382 btm = - descent + boff;
19383 if (font_info && font_info->relative_compose
19384 && (! CHAR_TABLE_P (Vignore_relative_composition)
19385 || NILP (Faref (Vignore_relative_composition,
19386 make_number (ch)))))
19387 {
19388
19389 if (- descent >= font_info->relative_compose)
19390 /* One extra pixel between two glyphs. */
19391 btm = highest + 1;
19392 else if (ascent <= 0)
19393 /* One extra pixel between two glyphs. */
19394 btm = lowest - 1 - ascent - descent;
19395 }
19396 }
19397 else
19398 {
19399 /* A composition rule is specified by an integer
19400 value that encodes global and new reference
19401 points (GREF and NREF). GREF and NREF are
19402 specified by numbers as below:
19403
19404 0---1---2 -- ascent
19405 | |
19406 | |
19407 | |
19408 9--10--11 -- center
19409 | |
19410 ---3---4---5--- baseline
19411 | |
19412 6---7---8 -- descent
19413 */
19414 int rule = COMPOSITION_RULE (cmp, i);
19415 int gref, nref, grefx, grefy, nrefx, nrefy;
19416
19417 COMPOSITION_DECODE_RULE (rule, gref, nref);
19418 grefx = gref % 3, nrefx = nref % 3;
19419 grefy = gref / 3, nrefy = nref / 3;
19420
19421 left = (leftmost
19422 + grefx * (rightmost - leftmost) / 2
19423 - nrefx * width / 2);
19424 btm = ((grefy == 0 ? highest
19425 : grefy == 1 ? 0
19426 : grefy == 2 ? lowest
19427 : (highest + lowest) / 2)
19428 - (nrefy == 0 ? ascent + descent
19429 : nrefy == 1 ? descent - boff
19430 : nrefy == 2 ? 0
19431 : (ascent + descent) / 2));
19432 }
19433
19434 cmp->offsets[i * 2] = left;
19435 cmp->offsets[i * 2 + 1] = btm + descent;
19436
19437 /* Update the bounding box of the overall glyphs. */
19438 right = left + width;
19439 top = btm + descent + ascent;
19440 if (left < leftmost)
19441 leftmost = left;
19442 if (right > rightmost)
19443 rightmost = right;
19444 if (top > highest)
19445 highest = top;
19446 if (btm < lowest)
19447 lowest = btm;
19448 }
19449
19450 /* If there are glyphs whose x-offsets are negative,
19451 shift all glyphs to the right and make all x-offsets
19452 non-negative. */
19453 if (leftmost < 0)
19454 {
19455 for (i = 0; i < cmp->glyph_len; i++)
19456 cmp->offsets[i * 2] -= leftmost;
19457 rightmost -= leftmost;
19458 }
19459
19460 cmp->pixel_width = rightmost;
19461 cmp->ascent = highest;
19462 cmp->descent = - lowest;
19463 if (cmp->ascent < font_ascent)
19464 cmp->ascent = font_ascent;
19465 if (cmp->descent < font_descent)
19466 cmp->descent = font_descent;
19467 }
19468
19469 it->pixel_width = cmp->pixel_width;
19470 it->ascent = it->phys_ascent = cmp->ascent;
19471 it->descent = it->phys_descent = cmp->descent;
19472
19473 if (face->box != FACE_NO_BOX)
19474 {
19475 int thick = face->box_line_width;
19476
19477 if (thick > 0)
19478 {
19479 it->ascent += thick;
19480 it->descent += thick;
19481 }
19482 else
19483 thick = - thick;
19484
19485 if (it->start_of_box_run_p)
19486 it->pixel_width += thick;
19487 if (it->end_of_box_run_p)
19488 it->pixel_width += thick;
19489 }
19490
19491 /* If face has an overline, add the height of the overline
19492 (1 pixel) and a 1 pixel margin to the character height. */
19493 if (face->overline_p)
19494 it->ascent += 2;
19495
19496 take_vertical_position_into_account (it);
19497
19498 if (it->glyph_row)
19499 append_composite_glyph (it);
19500 }
19501 else if (it->what == IT_IMAGE)
19502 produce_image_glyph (it);
19503 else if (it->what == IT_STRETCH)
19504 produce_stretch_glyph (it);
19505
19506 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19507 because this isn't true for images with `:ascent 100'. */
19508 xassert (it->ascent >= 0 && it->descent >= 0);
19509 if (it->area == TEXT_AREA)
19510 it->current_x += it->pixel_width;
19511
19512 if (extra_line_spacing > 0)
19513 {
19514 it->descent += extra_line_spacing;
19515 if (extra_line_spacing > it->max_extra_line_spacing)
19516 it->max_extra_line_spacing = extra_line_spacing;
19517 }
19518
19519 it->max_ascent = max (it->max_ascent, it->ascent);
19520 it->max_descent = max (it->max_descent, it->descent);
19521 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19522 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19523 }
19524
19525 /* EXPORT for RIF:
19526 Output LEN glyphs starting at START at the nominal cursor position.
19527 Advance the nominal cursor over the text. The global variable
19528 updated_window contains the window being updated, updated_row is
19529 the glyph row being updated, and updated_area is the area of that
19530 row being updated. */
19531
19532 void
19533 x_write_glyphs (start, len)
19534 struct glyph *start;
19535 int len;
19536 {
19537 int x, hpos;
19538
19539 xassert (updated_window && updated_row);
19540 BLOCK_INPUT;
19541
19542 /* Write glyphs. */
19543
19544 hpos = start - updated_row->glyphs[updated_area];
19545 x = draw_glyphs (updated_window, output_cursor.x,
19546 updated_row, updated_area,
19547 hpos, hpos + len,
19548 DRAW_NORMAL_TEXT, 0);
19549
19550 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19551 if (updated_area == TEXT_AREA
19552 && updated_window->phys_cursor_on_p
19553 && updated_window->phys_cursor.vpos == output_cursor.vpos
19554 && updated_window->phys_cursor.hpos >= hpos
19555 && updated_window->phys_cursor.hpos < hpos + len)
19556 updated_window->phys_cursor_on_p = 0;
19557
19558 UNBLOCK_INPUT;
19559
19560 /* Advance the output cursor. */
19561 output_cursor.hpos += len;
19562 output_cursor.x = x;
19563 }
19564
19565
19566 /* EXPORT for RIF:
19567 Insert LEN glyphs from START at the nominal cursor position. */
19568
19569 void
19570 x_insert_glyphs (start, len)
19571 struct glyph *start;
19572 int len;
19573 {
19574 struct frame *f;
19575 struct window *w;
19576 int line_height, shift_by_width, shifted_region_width;
19577 struct glyph_row *row;
19578 struct glyph *glyph;
19579 int frame_x, frame_y, hpos;
19580
19581 xassert (updated_window && updated_row);
19582 BLOCK_INPUT;
19583 w = updated_window;
19584 f = XFRAME (WINDOW_FRAME (w));
19585
19586 /* Get the height of the line we are in. */
19587 row = updated_row;
19588 line_height = row->height;
19589
19590 /* Get the width of the glyphs to insert. */
19591 shift_by_width = 0;
19592 for (glyph = start; glyph < start + len; ++glyph)
19593 shift_by_width += glyph->pixel_width;
19594
19595 /* Get the width of the region to shift right. */
19596 shifted_region_width = (window_box_width (w, updated_area)
19597 - output_cursor.x
19598 - shift_by_width);
19599
19600 /* Shift right. */
19601 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19602 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19603
19604 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19605 line_height, shift_by_width);
19606
19607 /* Write the glyphs. */
19608 hpos = start - row->glyphs[updated_area];
19609 draw_glyphs (w, output_cursor.x, row, updated_area,
19610 hpos, hpos + len,
19611 DRAW_NORMAL_TEXT, 0);
19612
19613 /* Advance the output cursor. */
19614 output_cursor.hpos += len;
19615 output_cursor.x += shift_by_width;
19616 UNBLOCK_INPUT;
19617 }
19618
19619
19620 /* EXPORT for RIF:
19621 Erase the current text line from the nominal cursor position
19622 (inclusive) to pixel column TO_X (exclusive). The idea is that
19623 everything from TO_X onward is already erased.
19624
19625 TO_X is a pixel position relative to updated_area of
19626 updated_window. TO_X == -1 means clear to the end of this area. */
19627
19628 void
19629 x_clear_end_of_line (to_x)
19630 int to_x;
19631 {
19632 struct frame *f;
19633 struct window *w = updated_window;
19634 int max_x, min_y, max_y;
19635 int from_x, from_y, to_y;
19636
19637 xassert (updated_window && updated_row);
19638 f = XFRAME (w->frame);
19639
19640 if (updated_row->full_width_p)
19641 max_x = WINDOW_TOTAL_WIDTH (w);
19642 else
19643 max_x = window_box_width (w, updated_area);
19644 max_y = window_text_bottom_y (w);
19645
19646 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19647 of window. For TO_X > 0, truncate to end of drawing area. */
19648 if (to_x == 0)
19649 return;
19650 else if (to_x < 0)
19651 to_x = max_x;
19652 else
19653 to_x = min (to_x, max_x);
19654
19655 to_y = min (max_y, output_cursor.y + updated_row->height);
19656
19657 /* Notice if the cursor will be cleared by this operation. */
19658 if (!updated_row->full_width_p)
19659 notice_overwritten_cursor (w, updated_area,
19660 output_cursor.x, -1,
19661 updated_row->y,
19662 MATRIX_ROW_BOTTOM_Y (updated_row));
19663
19664 from_x = output_cursor.x;
19665
19666 /* Translate to frame coordinates. */
19667 if (updated_row->full_width_p)
19668 {
19669 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19670 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19671 }
19672 else
19673 {
19674 int area_left = window_box_left (w, updated_area);
19675 from_x += area_left;
19676 to_x += area_left;
19677 }
19678
19679 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19680 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19681 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19682
19683 /* Prevent inadvertently clearing to end of the X window. */
19684 if (to_x > from_x && to_y > from_y)
19685 {
19686 BLOCK_INPUT;
19687 rif->clear_frame_area (f, from_x, from_y,
19688 to_x - from_x, to_y - from_y);
19689 UNBLOCK_INPUT;
19690 }
19691 }
19692
19693 #endif /* HAVE_WINDOW_SYSTEM */
19694
19695
19696 \f
19697 /***********************************************************************
19698 Cursor types
19699 ***********************************************************************/
19700
19701 /* Value is the internal representation of the specified cursor type
19702 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19703 of the bar cursor. */
19704
19705 static enum text_cursor_kinds
19706 get_specified_cursor_type (arg, width)
19707 Lisp_Object arg;
19708 int *width;
19709 {
19710 enum text_cursor_kinds type;
19711
19712 if (NILP (arg))
19713 return NO_CURSOR;
19714
19715 if (EQ (arg, Qbox))
19716 return FILLED_BOX_CURSOR;
19717
19718 if (EQ (arg, Qhollow))
19719 return HOLLOW_BOX_CURSOR;
19720
19721 if (EQ (arg, Qbar))
19722 {
19723 *width = 2;
19724 return BAR_CURSOR;
19725 }
19726
19727 if (CONSP (arg)
19728 && EQ (XCAR (arg), Qbar)
19729 && INTEGERP (XCDR (arg))
19730 && XINT (XCDR (arg)) >= 0)
19731 {
19732 *width = XINT (XCDR (arg));
19733 return BAR_CURSOR;
19734 }
19735
19736 if (EQ (arg, Qhbar))
19737 {
19738 *width = 2;
19739 return HBAR_CURSOR;
19740 }
19741
19742 if (CONSP (arg)
19743 && EQ (XCAR (arg), Qhbar)
19744 && INTEGERP (XCDR (arg))
19745 && XINT (XCDR (arg)) >= 0)
19746 {
19747 *width = XINT (XCDR (arg));
19748 return HBAR_CURSOR;
19749 }
19750
19751 /* Treat anything unknown as "hollow box cursor".
19752 It was bad to signal an error; people have trouble fixing
19753 .Xdefaults with Emacs, when it has something bad in it. */
19754 type = HOLLOW_BOX_CURSOR;
19755
19756 return type;
19757 }
19758
19759 /* Set the default cursor types for specified frame. */
19760 void
19761 set_frame_cursor_types (f, arg)
19762 struct frame *f;
19763 Lisp_Object arg;
19764 {
19765 int width;
19766 Lisp_Object tem;
19767
19768 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19769 FRAME_CURSOR_WIDTH (f) = width;
19770
19771 /* By default, set up the blink-off state depending on the on-state. */
19772
19773 tem = Fassoc (arg, Vblink_cursor_alist);
19774 if (!NILP (tem))
19775 {
19776 FRAME_BLINK_OFF_CURSOR (f)
19777 = get_specified_cursor_type (XCDR (tem), &width);
19778 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19779 }
19780 else
19781 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19782 }
19783
19784
19785 /* Return the cursor we want to be displayed in window W. Return
19786 width of bar/hbar cursor through WIDTH arg. Return with
19787 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19788 (i.e. if the `system caret' should track this cursor).
19789
19790 In a mini-buffer window, we want the cursor only to appear if we
19791 are reading input from this window. For the selected window, we
19792 want the cursor type given by the frame parameter or buffer local
19793 setting of cursor-type. If explicitly marked off, draw no cursor.
19794 In all other cases, we want a hollow box cursor. */
19795
19796 static enum text_cursor_kinds
19797 get_window_cursor_type (w, glyph, width, active_cursor)
19798 struct window *w;
19799 struct glyph *glyph;
19800 int *width;
19801 int *active_cursor;
19802 {
19803 struct frame *f = XFRAME (w->frame);
19804 struct buffer *b = XBUFFER (w->buffer);
19805 int cursor_type = DEFAULT_CURSOR;
19806 Lisp_Object alt_cursor;
19807 int non_selected = 0;
19808
19809 *active_cursor = 1;
19810
19811 /* Echo area */
19812 if (cursor_in_echo_area
19813 && FRAME_HAS_MINIBUF_P (f)
19814 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19815 {
19816 if (w == XWINDOW (echo_area_window))
19817 {
19818 *width = FRAME_CURSOR_WIDTH (f);
19819 return FRAME_DESIRED_CURSOR (f);
19820 }
19821
19822 *active_cursor = 0;
19823 non_selected = 1;
19824 }
19825
19826 /* Nonselected window or nonselected frame. */
19827 else if (w != XWINDOW (f->selected_window)
19828 #ifdef HAVE_WINDOW_SYSTEM
19829 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19830 #endif
19831 )
19832 {
19833 *active_cursor = 0;
19834
19835 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19836 return NO_CURSOR;
19837
19838 non_selected = 1;
19839 }
19840
19841 /* Never display a cursor in a window in which cursor-type is nil. */
19842 if (NILP (b->cursor_type))
19843 return NO_CURSOR;
19844
19845 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19846 if (non_selected)
19847 {
19848 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19849 return get_specified_cursor_type (alt_cursor, width);
19850 }
19851
19852 /* Get the normal cursor type for this window. */
19853 if (EQ (b->cursor_type, Qt))
19854 {
19855 cursor_type = FRAME_DESIRED_CURSOR (f);
19856 *width = FRAME_CURSOR_WIDTH (f);
19857 }
19858 else
19859 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19860
19861 /* Use normal cursor if not blinked off. */
19862 if (!w->cursor_off_p)
19863 {
19864 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19865 if (cursor_type == FILLED_BOX_CURSOR)
19866 cursor_type = HOLLOW_BOX_CURSOR;
19867 }
19868 return cursor_type;
19869 }
19870
19871 /* Cursor is blinked off, so determine how to "toggle" it. */
19872
19873 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19874 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19875 return get_specified_cursor_type (XCDR (alt_cursor), width);
19876
19877 /* Then see if frame has specified a specific blink off cursor type. */
19878 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19879 {
19880 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19881 return FRAME_BLINK_OFF_CURSOR (f);
19882 }
19883
19884 #if 0
19885 /* Some people liked having a permanently visible blinking cursor,
19886 while others had very strong opinions against it. So it was
19887 decided to remove it. KFS 2003-09-03 */
19888
19889 /* Finally perform built-in cursor blinking:
19890 filled box <-> hollow box
19891 wide [h]bar <-> narrow [h]bar
19892 narrow [h]bar <-> no cursor
19893 other type <-> no cursor */
19894
19895 if (cursor_type == FILLED_BOX_CURSOR)
19896 return HOLLOW_BOX_CURSOR;
19897
19898 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19899 {
19900 *width = 1;
19901 return cursor_type;
19902 }
19903 #endif
19904
19905 return NO_CURSOR;
19906 }
19907
19908
19909 #ifdef HAVE_WINDOW_SYSTEM
19910
19911 /* Notice when the text cursor of window W has been completely
19912 overwritten by a drawing operation that outputs glyphs in AREA
19913 starting at X0 and ending at X1 in the line starting at Y0 and
19914 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19915 the rest of the line after X0 has been written. Y coordinates
19916 are window-relative. */
19917
19918 static void
19919 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19920 struct window *w;
19921 enum glyph_row_area area;
19922 int x0, y0, x1, y1;
19923 {
19924 int cx0, cx1, cy0, cy1;
19925 struct glyph_row *row;
19926
19927 if (!w->phys_cursor_on_p)
19928 return;
19929 if (area != TEXT_AREA)
19930 return;
19931
19932 row = w->current_matrix->rows + w->phys_cursor.vpos;
19933 if (!row->displays_text_p)
19934 return;
19935
19936 if (row->cursor_in_fringe_p)
19937 {
19938 row->cursor_in_fringe_p = 0;
19939 draw_fringe_bitmap (w, row, 0);
19940 w->phys_cursor_on_p = 0;
19941 return;
19942 }
19943
19944 cx0 = w->phys_cursor.x;
19945 cx1 = cx0 + w->phys_cursor_width;
19946 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
19947 return;
19948
19949 /* The cursor image will be completely removed from the
19950 screen if the output area intersects the cursor area in
19951 y-direction. When we draw in [y0 y1[, and some part of
19952 the cursor is at y < y0, that part must have been drawn
19953 before. When scrolling, the cursor is erased before
19954 actually scrolling, so we don't come here. When not
19955 scrolling, the rows above the old cursor row must have
19956 changed, and in this case these rows must have written
19957 over the cursor image.
19958
19959 Likewise if part of the cursor is below y1, with the
19960 exception of the cursor being in the first blank row at
19961 the buffer and window end because update_text_area
19962 doesn't draw that row. (Except when it does, but
19963 that's handled in update_text_area.) */
19964
19965 cy0 = w->phys_cursor.y;
19966 cy1 = cy0 + w->phys_cursor_height;
19967 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
19968 return;
19969
19970 w->phys_cursor_on_p = 0;
19971 }
19972
19973 #endif /* HAVE_WINDOW_SYSTEM */
19974
19975 \f
19976 /************************************************************************
19977 Mouse Face
19978 ************************************************************************/
19979
19980 #ifdef HAVE_WINDOW_SYSTEM
19981
19982 /* EXPORT for RIF:
19983 Fix the display of area AREA of overlapping row ROW in window W. */
19984
19985 void
19986 x_fix_overlapping_area (w, row, area)
19987 struct window *w;
19988 struct glyph_row *row;
19989 enum glyph_row_area area;
19990 {
19991 int i, x;
19992
19993 BLOCK_INPUT;
19994
19995 x = 0;
19996 for (i = 0; i < row->used[area];)
19997 {
19998 if (row->glyphs[area][i].overlaps_vertically_p)
19999 {
20000 int start = i, start_x = x;
20001
20002 do
20003 {
20004 x += row->glyphs[area][i].pixel_width;
20005 ++i;
20006 }
20007 while (i < row->used[area]
20008 && row->glyphs[area][i].overlaps_vertically_p);
20009
20010 draw_glyphs (w, start_x, row, area,
20011 start, i,
20012 DRAW_NORMAL_TEXT, 1);
20013 }
20014 else
20015 {
20016 x += row->glyphs[area][i].pixel_width;
20017 ++i;
20018 }
20019 }
20020
20021 UNBLOCK_INPUT;
20022 }
20023
20024
20025 /* EXPORT:
20026 Draw the cursor glyph of window W in glyph row ROW. See the
20027 comment of draw_glyphs for the meaning of HL. */
20028
20029 void
20030 draw_phys_cursor_glyph (w, row, hl)
20031 struct window *w;
20032 struct glyph_row *row;
20033 enum draw_glyphs_face hl;
20034 {
20035 /* If cursor hpos is out of bounds, don't draw garbage. This can
20036 happen in mini-buffer windows when switching between echo area
20037 glyphs and mini-buffer. */
20038 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20039 {
20040 int on_p = w->phys_cursor_on_p;
20041 int x1;
20042 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20043 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20044 hl, 0);
20045 w->phys_cursor_on_p = on_p;
20046
20047 if (hl == DRAW_CURSOR)
20048 w->phys_cursor_width = x1 - w->phys_cursor.x;
20049 /* When we erase the cursor, and ROW is overlapped by other
20050 rows, make sure that these overlapping parts of other rows
20051 are redrawn. */
20052 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20053 {
20054 if (row > w->current_matrix->rows
20055 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20056 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20057
20058 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20059 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20060 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20061 }
20062 }
20063 }
20064
20065
20066 /* EXPORT:
20067 Erase the image of a cursor of window W from the screen. */
20068
20069 void
20070 erase_phys_cursor (w)
20071 struct window *w;
20072 {
20073 struct frame *f = XFRAME (w->frame);
20074 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20075 int hpos = w->phys_cursor.hpos;
20076 int vpos = w->phys_cursor.vpos;
20077 int mouse_face_here_p = 0;
20078 struct glyph_matrix *active_glyphs = w->current_matrix;
20079 struct glyph_row *cursor_row;
20080 struct glyph *cursor_glyph;
20081 enum draw_glyphs_face hl;
20082
20083 /* No cursor displayed or row invalidated => nothing to do on the
20084 screen. */
20085 if (w->phys_cursor_type == NO_CURSOR)
20086 goto mark_cursor_off;
20087
20088 /* VPOS >= active_glyphs->nrows means that window has been resized.
20089 Don't bother to erase the cursor. */
20090 if (vpos >= active_glyphs->nrows)
20091 goto mark_cursor_off;
20092
20093 /* If row containing cursor is marked invalid, there is nothing we
20094 can do. */
20095 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20096 if (!cursor_row->enabled_p)
20097 goto mark_cursor_off;
20098
20099 /* If line spacing is > 0, old cursor may only be partially visible in
20100 window after split-window. So adjust visible height. */
20101 cursor_row->visible_height = min (cursor_row->visible_height,
20102 window_text_bottom_y (w) - cursor_row->y);
20103
20104 /* If row is completely invisible, don't attempt to delete a cursor which
20105 isn't there. This can happen if cursor is at top of a window, and
20106 we switch to a buffer with a header line in that window. */
20107 if (cursor_row->visible_height <= 0)
20108 goto mark_cursor_off;
20109
20110 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20111 if (cursor_row->cursor_in_fringe_p)
20112 {
20113 cursor_row->cursor_in_fringe_p = 0;
20114 draw_fringe_bitmap (w, cursor_row, 0);
20115 goto mark_cursor_off;
20116 }
20117
20118 /* This can happen when the new row is shorter than the old one.
20119 In this case, either draw_glyphs or clear_end_of_line
20120 should have cleared the cursor. Note that we wouldn't be
20121 able to erase the cursor in this case because we don't have a
20122 cursor glyph at hand. */
20123 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20124 goto mark_cursor_off;
20125
20126 /* If the cursor is in the mouse face area, redisplay that when
20127 we clear the cursor. */
20128 if (! NILP (dpyinfo->mouse_face_window)
20129 && w == XWINDOW (dpyinfo->mouse_face_window)
20130 && (vpos > dpyinfo->mouse_face_beg_row
20131 || (vpos == dpyinfo->mouse_face_beg_row
20132 && hpos >= dpyinfo->mouse_face_beg_col))
20133 && (vpos < dpyinfo->mouse_face_end_row
20134 || (vpos == dpyinfo->mouse_face_end_row
20135 && hpos < dpyinfo->mouse_face_end_col))
20136 /* Don't redraw the cursor's spot in mouse face if it is at the
20137 end of a line (on a newline). The cursor appears there, but
20138 mouse highlighting does not. */
20139 && cursor_row->used[TEXT_AREA] > hpos)
20140 mouse_face_here_p = 1;
20141
20142 /* Maybe clear the display under the cursor. */
20143 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20144 {
20145 int x, y;
20146 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20147 int width;
20148
20149 cursor_glyph = get_phys_cursor_glyph (w);
20150 if (cursor_glyph == NULL)
20151 goto mark_cursor_off;
20152
20153 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20154 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20155 width = min (cursor_glyph->pixel_width,
20156 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20157
20158 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20159 }
20160
20161 /* Erase the cursor by redrawing the character underneath it. */
20162 if (mouse_face_here_p)
20163 hl = DRAW_MOUSE_FACE;
20164 else
20165 hl = DRAW_NORMAL_TEXT;
20166 draw_phys_cursor_glyph (w, cursor_row, hl);
20167
20168 mark_cursor_off:
20169 w->phys_cursor_on_p = 0;
20170 w->phys_cursor_type = NO_CURSOR;
20171 }
20172
20173
20174 /* EXPORT:
20175 Display or clear cursor of window W. If ON is zero, clear the
20176 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20177 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20178
20179 void
20180 display_and_set_cursor (w, on, hpos, vpos, x, y)
20181 struct window *w;
20182 int on, hpos, vpos, x, y;
20183 {
20184 struct frame *f = XFRAME (w->frame);
20185 int new_cursor_type;
20186 int new_cursor_width;
20187 int active_cursor;
20188 struct glyph_row *glyph_row;
20189 struct glyph *glyph;
20190
20191 /* This is pointless on invisible frames, and dangerous on garbaged
20192 windows and frames; in the latter case, the frame or window may
20193 be in the midst of changing its size, and x and y may be off the
20194 window. */
20195 if (! FRAME_VISIBLE_P (f)
20196 || FRAME_GARBAGED_P (f)
20197 || vpos >= w->current_matrix->nrows
20198 || hpos >= w->current_matrix->matrix_w)
20199 return;
20200
20201 /* If cursor is off and we want it off, return quickly. */
20202 if (!on && !w->phys_cursor_on_p)
20203 return;
20204
20205 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20206 /* If cursor row is not enabled, we don't really know where to
20207 display the cursor. */
20208 if (!glyph_row->enabled_p)
20209 {
20210 w->phys_cursor_on_p = 0;
20211 return;
20212 }
20213
20214 glyph = NULL;
20215 if (!glyph_row->exact_window_width_line_p
20216 || hpos < glyph_row->used[TEXT_AREA])
20217 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20218
20219 xassert (interrupt_input_blocked);
20220
20221 /* Set new_cursor_type to the cursor we want to be displayed. */
20222 new_cursor_type = get_window_cursor_type (w, glyph,
20223 &new_cursor_width, &active_cursor);
20224
20225 /* If cursor is currently being shown and we don't want it to be or
20226 it is in the wrong place, or the cursor type is not what we want,
20227 erase it. */
20228 if (w->phys_cursor_on_p
20229 && (!on
20230 || w->phys_cursor.x != x
20231 || w->phys_cursor.y != y
20232 || new_cursor_type != w->phys_cursor_type
20233 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20234 && new_cursor_width != w->phys_cursor_width)))
20235 erase_phys_cursor (w);
20236
20237 /* Don't check phys_cursor_on_p here because that flag is only set
20238 to zero in some cases where we know that the cursor has been
20239 completely erased, to avoid the extra work of erasing the cursor
20240 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20241 still not be visible, or it has only been partly erased. */
20242 if (on)
20243 {
20244 w->phys_cursor_ascent = glyph_row->ascent;
20245 w->phys_cursor_height = glyph_row->height;
20246
20247 /* Set phys_cursor_.* before x_draw_.* is called because some
20248 of them may need the information. */
20249 w->phys_cursor.x = x;
20250 w->phys_cursor.y = glyph_row->y;
20251 w->phys_cursor.hpos = hpos;
20252 w->phys_cursor.vpos = vpos;
20253 }
20254
20255 rif->draw_window_cursor (w, glyph_row, x, y,
20256 new_cursor_type, new_cursor_width,
20257 on, active_cursor);
20258 }
20259
20260
20261 /* Switch the display of W's cursor on or off, according to the value
20262 of ON. */
20263
20264 static void
20265 update_window_cursor (w, on)
20266 struct window *w;
20267 int on;
20268 {
20269 /* Don't update cursor in windows whose frame is in the process
20270 of being deleted. */
20271 if (w->current_matrix)
20272 {
20273 BLOCK_INPUT;
20274 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20275 w->phys_cursor.x, w->phys_cursor.y);
20276 UNBLOCK_INPUT;
20277 }
20278 }
20279
20280
20281 /* Call update_window_cursor with parameter ON_P on all leaf windows
20282 in the window tree rooted at W. */
20283
20284 static void
20285 update_cursor_in_window_tree (w, on_p)
20286 struct window *w;
20287 int on_p;
20288 {
20289 while (w)
20290 {
20291 if (!NILP (w->hchild))
20292 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20293 else if (!NILP (w->vchild))
20294 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20295 else
20296 update_window_cursor (w, on_p);
20297
20298 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20299 }
20300 }
20301
20302
20303 /* EXPORT:
20304 Display the cursor on window W, or clear it, according to ON_P.
20305 Don't change the cursor's position. */
20306
20307 void
20308 x_update_cursor (f, on_p)
20309 struct frame *f;
20310 int on_p;
20311 {
20312 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20313 }
20314
20315
20316 /* EXPORT:
20317 Clear the cursor of window W to background color, and mark the
20318 cursor as not shown. This is used when the text where the cursor
20319 is is about to be rewritten. */
20320
20321 void
20322 x_clear_cursor (w)
20323 struct window *w;
20324 {
20325 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20326 update_window_cursor (w, 0);
20327 }
20328
20329
20330 /* EXPORT:
20331 Display the active region described by mouse_face_* according to DRAW. */
20332
20333 void
20334 show_mouse_face (dpyinfo, draw)
20335 Display_Info *dpyinfo;
20336 enum draw_glyphs_face draw;
20337 {
20338 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20339 struct frame *f = XFRAME (WINDOW_FRAME (w));
20340
20341 if (/* If window is in the process of being destroyed, don't bother
20342 to do anything. */
20343 w->current_matrix != NULL
20344 /* Don't update mouse highlight if hidden */
20345 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20346 /* Recognize when we are called to operate on rows that don't exist
20347 anymore. This can happen when a window is split. */
20348 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20349 {
20350 int phys_cursor_on_p = w->phys_cursor_on_p;
20351 struct glyph_row *row, *first, *last;
20352
20353 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20354 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20355
20356 for (row = first; row <= last && row->enabled_p; ++row)
20357 {
20358 int start_hpos, end_hpos, start_x;
20359
20360 /* For all but the first row, the highlight starts at column 0. */
20361 if (row == first)
20362 {
20363 start_hpos = dpyinfo->mouse_face_beg_col;
20364 start_x = dpyinfo->mouse_face_beg_x;
20365 }
20366 else
20367 {
20368 start_hpos = 0;
20369 start_x = 0;
20370 }
20371
20372 if (row == last)
20373 end_hpos = dpyinfo->mouse_face_end_col;
20374 else
20375 end_hpos = row->used[TEXT_AREA];
20376
20377 if (end_hpos > start_hpos)
20378 {
20379 draw_glyphs (w, start_x, row, TEXT_AREA,
20380 start_hpos, end_hpos,
20381 draw, 0);
20382
20383 row->mouse_face_p
20384 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20385 }
20386 }
20387
20388 /* When we've written over the cursor, arrange for it to
20389 be displayed again. */
20390 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20391 {
20392 BLOCK_INPUT;
20393 display_and_set_cursor (w, 1,
20394 w->phys_cursor.hpos, w->phys_cursor.vpos,
20395 w->phys_cursor.x, w->phys_cursor.y);
20396 UNBLOCK_INPUT;
20397 }
20398 }
20399
20400 /* Change the mouse cursor. */
20401 if (draw == DRAW_NORMAL_TEXT)
20402 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20403 else if (draw == DRAW_MOUSE_FACE)
20404 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20405 else
20406 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20407 }
20408
20409 /* EXPORT:
20410 Clear out the mouse-highlighted active region.
20411 Redraw it un-highlighted first. Value is non-zero if mouse
20412 face was actually drawn unhighlighted. */
20413
20414 int
20415 clear_mouse_face (dpyinfo)
20416 Display_Info *dpyinfo;
20417 {
20418 int cleared = 0;
20419
20420 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20421 {
20422 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20423 cleared = 1;
20424 }
20425
20426 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20427 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20428 dpyinfo->mouse_face_window = Qnil;
20429 dpyinfo->mouse_face_overlay = Qnil;
20430 return cleared;
20431 }
20432
20433
20434 /* EXPORT:
20435 Non-zero if physical cursor of window W is within mouse face. */
20436
20437 int
20438 cursor_in_mouse_face_p (w)
20439 struct window *w;
20440 {
20441 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20442 int in_mouse_face = 0;
20443
20444 if (WINDOWP (dpyinfo->mouse_face_window)
20445 && XWINDOW (dpyinfo->mouse_face_window) == w)
20446 {
20447 int hpos = w->phys_cursor.hpos;
20448 int vpos = w->phys_cursor.vpos;
20449
20450 if (vpos >= dpyinfo->mouse_face_beg_row
20451 && vpos <= dpyinfo->mouse_face_end_row
20452 && (vpos > dpyinfo->mouse_face_beg_row
20453 || hpos >= dpyinfo->mouse_face_beg_col)
20454 && (vpos < dpyinfo->mouse_face_end_row
20455 || hpos < dpyinfo->mouse_face_end_col
20456 || dpyinfo->mouse_face_past_end))
20457 in_mouse_face = 1;
20458 }
20459
20460 return in_mouse_face;
20461 }
20462
20463
20464
20465 \f
20466 /* Find the glyph matrix position of buffer position CHARPOS in window
20467 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20468 current glyphs must be up to date. If CHARPOS is above window
20469 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20470 of last line in W. In the row containing CHARPOS, stop before glyphs
20471 having STOP as object. */
20472
20473 #if 1 /* This is a version of fast_find_position that's more correct
20474 in the presence of hscrolling, for example. I didn't install
20475 it right away because the problem fixed is minor, it failed
20476 in 20.x as well, and I think it's too risky to install
20477 so near the release of 21.1. 2001-09-25 gerd. */
20478
20479 static int
20480 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20481 struct window *w;
20482 int charpos;
20483 int *hpos, *vpos, *x, *y;
20484 Lisp_Object stop;
20485 {
20486 struct glyph_row *row, *first;
20487 struct glyph *glyph, *end;
20488 int past_end = 0;
20489
20490 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20491 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20492 {
20493 *x = first->x;
20494 *y = first->y;
20495 *hpos = 0;
20496 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20497 return 1;
20498 }
20499
20500 row = row_containing_pos (w, charpos, first, NULL, 0);
20501 if (row == NULL)
20502 {
20503 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20504 past_end = 1;
20505 }
20506
20507 *x = row->x;
20508 *y = row->y;
20509 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20510
20511 glyph = row->glyphs[TEXT_AREA];
20512 end = glyph + row->used[TEXT_AREA];
20513
20514 /* Skip over glyphs not having an object at the start of the row.
20515 These are special glyphs like truncation marks on terminal
20516 frames. */
20517 if (row->displays_text_p)
20518 while (glyph < end
20519 && INTEGERP (glyph->object)
20520 && !EQ (stop, glyph->object)
20521 && glyph->charpos < 0)
20522 {
20523 *x += glyph->pixel_width;
20524 ++glyph;
20525 }
20526
20527 while (glyph < end
20528 && !INTEGERP (glyph->object)
20529 && !EQ (stop, glyph->object)
20530 && (!BUFFERP (glyph->object)
20531 || glyph->charpos < charpos))
20532 {
20533 *x += glyph->pixel_width;
20534 ++glyph;
20535 }
20536
20537 *hpos = glyph - row->glyphs[TEXT_AREA];
20538 return !past_end;
20539 }
20540
20541 #else /* not 1 */
20542
20543 static int
20544 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20545 struct window *w;
20546 int pos;
20547 int *hpos, *vpos, *x, *y;
20548 Lisp_Object stop;
20549 {
20550 int i;
20551 int lastcol;
20552 int maybe_next_line_p = 0;
20553 int line_start_position;
20554 int yb = window_text_bottom_y (w);
20555 struct glyph_row *row, *best_row;
20556 int row_vpos, best_row_vpos;
20557 int current_x;
20558
20559 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20560 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20561
20562 while (row->y < yb)
20563 {
20564 if (row->used[TEXT_AREA])
20565 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20566 else
20567 line_start_position = 0;
20568
20569 if (line_start_position > pos)
20570 break;
20571 /* If the position sought is the end of the buffer,
20572 don't include the blank lines at the bottom of the window. */
20573 else if (line_start_position == pos
20574 && pos == BUF_ZV (XBUFFER (w->buffer)))
20575 {
20576 maybe_next_line_p = 1;
20577 break;
20578 }
20579 else if (line_start_position > 0)
20580 {
20581 best_row = row;
20582 best_row_vpos = row_vpos;
20583 }
20584
20585 if (row->y + row->height >= yb)
20586 break;
20587
20588 ++row;
20589 ++row_vpos;
20590 }
20591
20592 /* Find the right column within BEST_ROW. */
20593 lastcol = 0;
20594 current_x = best_row->x;
20595 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20596 {
20597 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20598 int charpos = glyph->charpos;
20599
20600 if (BUFFERP (glyph->object))
20601 {
20602 if (charpos == pos)
20603 {
20604 *hpos = i;
20605 *vpos = best_row_vpos;
20606 *x = current_x;
20607 *y = best_row->y;
20608 return 1;
20609 }
20610 else if (charpos > pos)
20611 break;
20612 }
20613 else if (EQ (glyph->object, stop))
20614 break;
20615
20616 if (charpos > 0)
20617 lastcol = i;
20618 current_x += glyph->pixel_width;
20619 }
20620
20621 /* If we're looking for the end of the buffer,
20622 and we didn't find it in the line we scanned,
20623 use the start of the following line. */
20624 if (maybe_next_line_p)
20625 {
20626 ++best_row;
20627 ++best_row_vpos;
20628 lastcol = 0;
20629 current_x = best_row->x;
20630 }
20631
20632 *vpos = best_row_vpos;
20633 *hpos = lastcol + 1;
20634 *x = current_x;
20635 *y = best_row->y;
20636 return 0;
20637 }
20638
20639 #endif /* not 1 */
20640
20641
20642 /* Find the position of the glyph for position POS in OBJECT in
20643 window W's current matrix, and return in *X, *Y the pixel
20644 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20645
20646 RIGHT_P non-zero means return the position of the right edge of the
20647 glyph, RIGHT_P zero means return the left edge position.
20648
20649 If no glyph for POS exists in the matrix, return the position of
20650 the glyph with the next smaller position that is in the matrix, if
20651 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20652 exists in the matrix, return the position of the glyph with the
20653 next larger position in OBJECT.
20654
20655 Value is non-zero if a glyph was found. */
20656
20657 static int
20658 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20659 struct window *w;
20660 int pos;
20661 Lisp_Object object;
20662 int *hpos, *vpos, *x, *y;
20663 int right_p;
20664 {
20665 int yb = window_text_bottom_y (w);
20666 struct glyph_row *r;
20667 struct glyph *best_glyph = NULL;
20668 struct glyph_row *best_row = NULL;
20669 int best_x = 0;
20670
20671 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20672 r->enabled_p && r->y < yb;
20673 ++r)
20674 {
20675 struct glyph *g = r->glyphs[TEXT_AREA];
20676 struct glyph *e = g + r->used[TEXT_AREA];
20677 int gx;
20678
20679 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20680 if (EQ (g->object, object))
20681 {
20682 if (g->charpos == pos)
20683 {
20684 best_glyph = g;
20685 best_x = gx;
20686 best_row = r;
20687 goto found;
20688 }
20689 else if (best_glyph == NULL
20690 || ((abs (g->charpos - pos)
20691 < abs (best_glyph->charpos - pos))
20692 && (right_p
20693 ? g->charpos < pos
20694 : g->charpos > pos)))
20695 {
20696 best_glyph = g;
20697 best_x = gx;
20698 best_row = r;
20699 }
20700 }
20701 }
20702
20703 found:
20704
20705 if (best_glyph)
20706 {
20707 *x = best_x;
20708 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20709
20710 if (right_p)
20711 {
20712 *x += best_glyph->pixel_width;
20713 ++*hpos;
20714 }
20715
20716 *y = best_row->y;
20717 *vpos = best_row - w->current_matrix->rows;
20718 }
20719
20720 return best_glyph != NULL;
20721 }
20722
20723
20724 /* See if position X, Y is within a hot-spot of an image. */
20725
20726 static int
20727 on_hot_spot_p (hot_spot, x, y)
20728 Lisp_Object hot_spot;
20729 int x, y;
20730 {
20731 if (!CONSP (hot_spot))
20732 return 0;
20733
20734 if (EQ (XCAR (hot_spot), Qrect))
20735 {
20736 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20737 Lisp_Object rect = XCDR (hot_spot);
20738 Lisp_Object tem;
20739 if (!CONSP (rect))
20740 return 0;
20741 if (!CONSP (XCAR (rect)))
20742 return 0;
20743 if (!CONSP (XCDR (rect)))
20744 return 0;
20745 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20746 return 0;
20747 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20748 return 0;
20749 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20750 return 0;
20751 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20752 return 0;
20753 return 1;
20754 }
20755 else if (EQ (XCAR (hot_spot), Qcircle))
20756 {
20757 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20758 Lisp_Object circ = XCDR (hot_spot);
20759 Lisp_Object lr, lx0, ly0;
20760 if (CONSP (circ)
20761 && CONSP (XCAR (circ))
20762 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20763 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20764 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20765 {
20766 double r = XFLOATINT (lr);
20767 double dx = XINT (lx0) - x;
20768 double dy = XINT (ly0) - y;
20769 return (dx * dx + dy * dy <= r * r);
20770 }
20771 }
20772 else if (EQ (XCAR (hot_spot), Qpoly))
20773 {
20774 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20775 if (VECTORP (XCDR (hot_spot)))
20776 {
20777 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20778 Lisp_Object *poly = v->contents;
20779 int n = v->size;
20780 int i;
20781 int inside = 0;
20782 Lisp_Object lx, ly;
20783 int x0, y0;
20784
20785 /* Need an even number of coordinates, and at least 3 edges. */
20786 if (n < 6 || n & 1)
20787 return 0;
20788
20789 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20790 If count is odd, we are inside polygon. Pixels on edges
20791 may or may not be included depending on actual geometry of the
20792 polygon. */
20793 if ((lx = poly[n-2], !INTEGERP (lx))
20794 || (ly = poly[n-1], !INTEGERP (lx)))
20795 return 0;
20796 x0 = XINT (lx), y0 = XINT (ly);
20797 for (i = 0; i < n; i += 2)
20798 {
20799 int x1 = x0, y1 = y0;
20800 if ((lx = poly[i], !INTEGERP (lx))
20801 || (ly = poly[i+1], !INTEGERP (ly)))
20802 return 0;
20803 x0 = XINT (lx), y0 = XINT (ly);
20804
20805 /* Does this segment cross the X line? */
20806 if (x0 >= x)
20807 {
20808 if (x1 >= x)
20809 continue;
20810 }
20811 else if (x1 < x)
20812 continue;
20813 if (y > y0 && y > y1)
20814 continue;
20815 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20816 inside = !inside;
20817 }
20818 return inside;
20819 }
20820 }
20821 /* If we don't understand the format, pretend we're not in the hot-spot. */
20822 return 0;
20823 }
20824
20825 Lisp_Object
20826 find_hot_spot (map, x, y)
20827 Lisp_Object map;
20828 int x, y;
20829 {
20830 while (CONSP (map))
20831 {
20832 if (CONSP (XCAR (map))
20833 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20834 return XCAR (map);
20835 map = XCDR (map);
20836 }
20837
20838 return Qnil;
20839 }
20840
20841 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20842 3, 3, 0,
20843 doc: /* Lookup in image map MAP coordinates X and Y.
20844 An image map is an alist where each element has the format (AREA ID PLIST).
20845 An AREA is specified as either a rectangle, a circle, or a polygon:
20846 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20847 pixel coordinates of the upper left and bottom right corners.
20848 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20849 and the radius of the circle; r may be a float or integer.
20850 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20851 vector describes one corner in the polygon.
20852 Returns the alist element for the first matching AREA in MAP. */)
20853 (map, x, y)
20854 Lisp_Object map;
20855 Lisp_Object x, y;
20856 {
20857 if (NILP (map))
20858 return Qnil;
20859
20860 CHECK_NUMBER (x);
20861 CHECK_NUMBER (y);
20862
20863 return find_hot_spot (map, XINT (x), XINT (y));
20864 }
20865
20866
20867 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20868 static void
20869 define_frame_cursor1 (f, cursor, pointer)
20870 struct frame *f;
20871 Cursor cursor;
20872 Lisp_Object pointer;
20873 {
20874 /* Do not change cursor shape while dragging mouse. */
20875 if (!NILP (do_mouse_tracking))
20876 return;
20877
20878 if (!NILP (pointer))
20879 {
20880 if (EQ (pointer, Qarrow))
20881 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20882 else if (EQ (pointer, Qhand))
20883 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20884 else if (EQ (pointer, Qtext))
20885 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20886 else if (EQ (pointer, intern ("hdrag")))
20887 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20888 #ifdef HAVE_X_WINDOWS
20889 else if (EQ (pointer, intern ("vdrag")))
20890 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20891 #endif
20892 else if (EQ (pointer, intern ("hourglass")))
20893 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20894 else if (EQ (pointer, Qmodeline))
20895 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20896 else
20897 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20898 }
20899
20900 if (cursor != No_Cursor)
20901 rif->define_frame_cursor (f, cursor);
20902 }
20903
20904 /* Take proper action when mouse has moved to the mode or header line
20905 or marginal area AREA of window W, x-position X and y-position Y.
20906 X is relative to the start of the text display area of W, so the
20907 width of bitmap areas and scroll bars must be subtracted to get a
20908 position relative to the start of the mode line. */
20909
20910 static void
20911 note_mode_line_or_margin_highlight (w, x, y, area)
20912 struct window *w;
20913 int x, y;
20914 enum window_part area;
20915 {
20916 struct frame *f = XFRAME (w->frame);
20917 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20918 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20919 Lisp_Object pointer = Qnil;
20920 int charpos, dx, dy, width, height;
20921 Lisp_Object string, object = Qnil;
20922 Lisp_Object pos, help;
20923
20924 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20925 string = mode_line_string (w, area, &x, &y, &charpos,
20926 &object, &dx, &dy, &width, &height);
20927 else
20928 {
20929 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20930 string = marginal_area_string (w, area, &x, &y, &charpos,
20931 &object, &dx, &dy, &width, &height);
20932 }
20933
20934 help = Qnil;
20935
20936 if (IMAGEP (object))
20937 {
20938 Lisp_Object image_map, hotspot;
20939 if ((image_map = Fsafe_plist_get (XCDR (object), QCmap),
20940 !NILP (image_map))
20941 && (hotspot = find_hot_spot (image_map, dx, dy),
20942 CONSP (hotspot))
20943 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20944 {
20945 Lisp_Object area_id, plist;
20946
20947 area_id = XCAR (hotspot);
20948 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20949 If so, we could look for mouse-enter, mouse-leave
20950 properties in PLIST (and do something...). */
20951 hotspot = XCDR (hotspot);
20952 if (CONSP (hotspot)
20953 && (plist = XCAR (hotspot), CONSP (plist)))
20954 {
20955 pointer = Fsafe_plist_get (plist, Qpointer);
20956 if (NILP (pointer))
20957 pointer = Qhand;
20958 help = Fsafe_plist_get (plist, Qhelp_echo);
20959 if (!NILP (help))
20960 {
20961 help_echo_string = help;
20962 /* Is this correct? ++kfs */
20963 XSETWINDOW (help_echo_window, w);
20964 help_echo_object = w->buffer;
20965 help_echo_pos = charpos;
20966 }
20967 }
20968 if (NILP (pointer))
20969 pointer = Fsafe_plist_get (XCDR (object), QCpointer);
20970 }
20971 }
20972
20973 if (STRINGP (string))
20974 {
20975 pos = make_number (charpos);
20976 /* If we're on a string with `help-echo' text property, arrange
20977 for the help to be displayed. This is done by setting the
20978 global variable help_echo_string to the help string. */
20979 if (NILP (help))
20980 {
20981 help = Fget_text_property (pos, Qhelp_echo, string);
20982 if (!NILP (help))
20983 {
20984 help_echo_string = help;
20985 XSETWINDOW (help_echo_window, w);
20986 help_echo_object = string;
20987 help_echo_pos = charpos;
20988 }
20989 }
20990
20991 if (NILP (pointer))
20992 pointer = Fget_text_property (pos, Qpointer, string);
20993
20994 /* Change the mouse pointer according to what is under X/Y. */
20995 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
20996 {
20997 Lisp_Object map;
20998 map = Fget_text_property (pos, Qlocal_map, string);
20999 if (!KEYMAPP (map))
21000 map = Fget_text_property (pos, Qkeymap, string);
21001 if (!KEYMAPP (map))
21002 cursor = dpyinfo->vertical_scroll_bar_cursor;
21003 }
21004 }
21005
21006 define_frame_cursor1 (f, cursor, pointer);
21007 }
21008
21009
21010 /* EXPORT:
21011 Take proper action when the mouse has moved to position X, Y on
21012 frame F as regards highlighting characters that have mouse-face
21013 properties. Also de-highlighting chars where the mouse was before.
21014 X and Y can be negative or out of range. */
21015
21016 void
21017 note_mouse_highlight (f, x, y)
21018 struct frame *f;
21019 int x, y;
21020 {
21021 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21022 enum window_part part;
21023 Lisp_Object window;
21024 struct window *w;
21025 Cursor cursor = No_Cursor;
21026 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21027 struct buffer *b;
21028
21029 /* When a menu is active, don't highlight because this looks odd. */
21030 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21031 if (popup_activated ())
21032 return;
21033 #endif
21034
21035 if (NILP (Vmouse_highlight)
21036 || !f->glyphs_initialized_p)
21037 return;
21038
21039 dpyinfo->mouse_face_mouse_x = x;
21040 dpyinfo->mouse_face_mouse_y = y;
21041 dpyinfo->mouse_face_mouse_frame = f;
21042
21043 if (dpyinfo->mouse_face_defer)
21044 return;
21045
21046 if (gc_in_progress)
21047 {
21048 dpyinfo->mouse_face_deferred_gc = 1;
21049 return;
21050 }
21051
21052 /* Which window is that in? */
21053 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21054
21055 /* If we were displaying active text in another window, clear that.
21056 Also clear if we move out of text area in same window. */
21057 if (! EQ (window, dpyinfo->mouse_face_window)
21058 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21059 clear_mouse_face (dpyinfo);
21060
21061 /* Not on a window -> return. */
21062 if (!WINDOWP (window))
21063 return;
21064
21065 /* Reset help_echo_string. It will get recomputed below. */
21066 help_echo_string = Qnil;
21067
21068 /* Convert to window-relative pixel coordinates. */
21069 w = XWINDOW (window);
21070 frame_to_window_pixel_xy (w, &x, &y);
21071
21072 /* Handle tool-bar window differently since it doesn't display a
21073 buffer. */
21074 if (EQ (window, f->tool_bar_window))
21075 {
21076 note_tool_bar_highlight (f, x, y);
21077 return;
21078 }
21079
21080 /* Mouse is on the mode, header line or margin? */
21081 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21082 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21083 {
21084 note_mode_line_or_margin_highlight (w, x, y, part);
21085 return;
21086 }
21087
21088 if (part == ON_VERTICAL_BORDER)
21089 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21090 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21091 || part == ON_SCROLL_BAR)
21092 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21093 else
21094 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21095
21096 /* Are we in a window whose display is up to date?
21097 And verify the buffer's text has not changed. */
21098 b = XBUFFER (w->buffer);
21099 if (part == ON_TEXT
21100 && EQ (w->window_end_valid, w->buffer)
21101 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21102 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21103 {
21104 int hpos, vpos, pos, i, dx, dy, area;
21105 struct glyph *glyph;
21106 Lisp_Object object;
21107 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21108 Lisp_Object *overlay_vec = NULL;
21109 int noverlays;
21110 struct buffer *obuf;
21111 int obegv, ozv, same_region;
21112
21113 /* Find the glyph under X/Y. */
21114 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21115
21116 /* Look for :pointer property on image. */
21117 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21118 {
21119 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21120 if (img != NULL && IMAGEP (img->spec))
21121 {
21122 Lisp_Object image_map, hotspot;
21123 if ((image_map = Fsafe_plist_get (XCDR (img->spec), QCmap),
21124 !NILP (image_map))
21125 && (hotspot = find_hot_spot (image_map,
21126 glyph->slice.x + dx,
21127 glyph->slice.y + dy),
21128 CONSP (hotspot))
21129 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21130 {
21131 Lisp_Object area_id, plist;
21132
21133 area_id = XCAR (hotspot);
21134 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21135 If so, we could look for mouse-enter, mouse-leave
21136 properties in PLIST (and do something...). */
21137 hotspot = XCDR (hotspot);
21138 if (CONSP (hotspot)
21139 && (plist = XCAR (hotspot), CONSP (plist)))
21140 {
21141 pointer = Fsafe_plist_get (plist, Qpointer);
21142 if (NILP (pointer))
21143 pointer = Qhand;
21144 help_echo_string = Fsafe_plist_get (plist, Qhelp_echo);
21145 if (!NILP (help_echo_string))
21146 {
21147 help_echo_window = window;
21148 help_echo_object = glyph->object;
21149 help_echo_pos = glyph->charpos;
21150 }
21151 }
21152 }
21153 if (NILP (pointer))
21154 pointer = Fsafe_plist_get (XCDR (img->spec), QCpointer);
21155 }
21156 }
21157
21158 /* Clear mouse face if X/Y not over text. */
21159 if (glyph == NULL
21160 || area != TEXT_AREA
21161 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21162 {
21163 if (clear_mouse_face (dpyinfo))
21164 cursor = No_Cursor;
21165 if (NILP (pointer))
21166 {
21167 if (area != TEXT_AREA)
21168 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21169 else
21170 pointer = Vvoid_text_area_pointer;
21171 }
21172 goto set_cursor;
21173 }
21174
21175 pos = glyph->charpos;
21176 object = glyph->object;
21177 if (!STRINGP (object) && !BUFFERP (object))
21178 goto set_cursor;
21179
21180 /* If we get an out-of-range value, return now; avoid an error. */
21181 if (BUFFERP (object) && pos > BUF_Z (b))
21182 goto set_cursor;
21183
21184 /* Make the window's buffer temporarily current for
21185 overlays_at and compute_char_face. */
21186 obuf = current_buffer;
21187 current_buffer = b;
21188 obegv = BEGV;
21189 ozv = ZV;
21190 BEGV = BEG;
21191 ZV = Z;
21192
21193 /* Is this char mouse-active or does it have help-echo? */
21194 position = make_number (pos);
21195
21196 if (BUFFERP (object))
21197 {
21198 /* Put all the overlays we want in a vector in overlay_vec. */
21199 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21200 /* Sort overlays into increasing priority order. */
21201 noverlays = sort_overlays (overlay_vec, noverlays, w);
21202 }
21203 else
21204 noverlays = 0;
21205
21206 same_region = (EQ (window, dpyinfo->mouse_face_window)
21207 && vpos >= dpyinfo->mouse_face_beg_row
21208 && vpos <= dpyinfo->mouse_face_end_row
21209 && (vpos > dpyinfo->mouse_face_beg_row
21210 || hpos >= dpyinfo->mouse_face_beg_col)
21211 && (vpos < dpyinfo->mouse_face_end_row
21212 || hpos < dpyinfo->mouse_face_end_col
21213 || dpyinfo->mouse_face_past_end));
21214
21215 if (same_region)
21216 cursor = No_Cursor;
21217
21218 /* Check mouse-face highlighting. */
21219 if (! same_region
21220 /* If there exists an overlay with mouse-face overlapping
21221 the one we are currently highlighting, we have to
21222 check if we enter the overlapping overlay, and then
21223 highlight only that. */
21224 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21225 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21226 {
21227 /* Find the highest priority overlay that has a mouse-face
21228 property. */
21229 overlay = Qnil;
21230 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21231 {
21232 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21233 if (!NILP (mouse_face))
21234 overlay = overlay_vec[i];
21235 }
21236
21237 /* If we're actually highlighting the same overlay as
21238 before, there's no need to do that again. */
21239 if (!NILP (overlay)
21240 && EQ (overlay, dpyinfo->mouse_face_overlay))
21241 goto check_help_echo;
21242
21243 dpyinfo->mouse_face_overlay = overlay;
21244
21245 /* Clear the display of the old active region, if any. */
21246 if (clear_mouse_face (dpyinfo))
21247 cursor = No_Cursor;
21248
21249 /* If no overlay applies, get a text property. */
21250 if (NILP (overlay))
21251 mouse_face = Fget_text_property (position, Qmouse_face, object);
21252
21253 /* Handle the overlay case. */
21254 if (!NILP (overlay))
21255 {
21256 /* Find the range of text around this char that
21257 should be active. */
21258 Lisp_Object before, after;
21259 int ignore;
21260
21261 before = Foverlay_start (overlay);
21262 after = Foverlay_end (overlay);
21263 /* Record this as the current active region. */
21264 fast_find_position (w, XFASTINT (before),
21265 &dpyinfo->mouse_face_beg_col,
21266 &dpyinfo->mouse_face_beg_row,
21267 &dpyinfo->mouse_face_beg_x,
21268 &dpyinfo->mouse_face_beg_y, Qnil);
21269
21270 dpyinfo->mouse_face_past_end
21271 = !fast_find_position (w, XFASTINT (after),
21272 &dpyinfo->mouse_face_end_col,
21273 &dpyinfo->mouse_face_end_row,
21274 &dpyinfo->mouse_face_end_x,
21275 &dpyinfo->mouse_face_end_y, Qnil);
21276 dpyinfo->mouse_face_window = window;
21277
21278 dpyinfo->mouse_face_face_id
21279 = face_at_buffer_position (w, pos, 0, 0,
21280 &ignore, pos + 1,
21281 !dpyinfo->mouse_face_hidden);
21282
21283 /* Display it as active. */
21284 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21285 cursor = No_Cursor;
21286 }
21287 /* Handle the text property case. */
21288 else if (!NILP (mouse_face) && BUFFERP (object))
21289 {
21290 /* Find the range of text around this char that
21291 should be active. */
21292 Lisp_Object before, after, beginning, end;
21293 int ignore;
21294
21295 beginning = Fmarker_position (w->start);
21296 end = make_number (BUF_Z (XBUFFER (object))
21297 - XFASTINT (w->window_end_pos));
21298 before
21299 = Fprevious_single_property_change (make_number (pos + 1),
21300 Qmouse_face,
21301 object, beginning);
21302 after
21303 = Fnext_single_property_change (position, Qmouse_face,
21304 object, end);
21305
21306 /* Record this as the current active region. */
21307 fast_find_position (w, XFASTINT (before),
21308 &dpyinfo->mouse_face_beg_col,
21309 &dpyinfo->mouse_face_beg_row,
21310 &dpyinfo->mouse_face_beg_x,
21311 &dpyinfo->mouse_face_beg_y, Qnil);
21312 dpyinfo->mouse_face_past_end
21313 = !fast_find_position (w, XFASTINT (after),
21314 &dpyinfo->mouse_face_end_col,
21315 &dpyinfo->mouse_face_end_row,
21316 &dpyinfo->mouse_face_end_x,
21317 &dpyinfo->mouse_face_end_y, Qnil);
21318 dpyinfo->mouse_face_window = window;
21319
21320 if (BUFFERP (object))
21321 dpyinfo->mouse_face_face_id
21322 = face_at_buffer_position (w, pos, 0, 0,
21323 &ignore, pos + 1,
21324 !dpyinfo->mouse_face_hidden);
21325
21326 /* Display it as active. */
21327 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21328 cursor = No_Cursor;
21329 }
21330 else if (!NILP (mouse_face) && STRINGP (object))
21331 {
21332 Lisp_Object b, e;
21333 int ignore;
21334
21335 b = Fprevious_single_property_change (make_number (pos + 1),
21336 Qmouse_face,
21337 object, Qnil);
21338 e = Fnext_single_property_change (position, Qmouse_face,
21339 object, Qnil);
21340 if (NILP (b))
21341 b = make_number (0);
21342 if (NILP (e))
21343 e = make_number (SCHARS (object) - 1);
21344 fast_find_string_pos (w, XINT (b), object,
21345 &dpyinfo->mouse_face_beg_col,
21346 &dpyinfo->mouse_face_beg_row,
21347 &dpyinfo->mouse_face_beg_x,
21348 &dpyinfo->mouse_face_beg_y, 0);
21349 fast_find_string_pos (w, XINT (e), object,
21350 &dpyinfo->mouse_face_end_col,
21351 &dpyinfo->mouse_face_end_row,
21352 &dpyinfo->mouse_face_end_x,
21353 &dpyinfo->mouse_face_end_y, 1);
21354 dpyinfo->mouse_face_past_end = 0;
21355 dpyinfo->mouse_face_window = window;
21356 dpyinfo->mouse_face_face_id
21357 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21358 glyph->face_id, 1);
21359 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21360 cursor = No_Cursor;
21361 }
21362 else if (STRINGP (object) && NILP (mouse_face))
21363 {
21364 /* A string which doesn't have mouse-face, but
21365 the text ``under'' it might have. */
21366 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21367 int start = MATRIX_ROW_START_CHARPOS (r);
21368
21369 pos = string_buffer_position (w, object, start);
21370 if (pos > 0)
21371 mouse_face = get_char_property_and_overlay (make_number (pos),
21372 Qmouse_face,
21373 w->buffer,
21374 &overlay);
21375 if (!NILP (mouse_face) && !NILP (overlay))
21376 {
21377 Lisp_Object before = Foverlay_start (overlay);
21378 Lisp_Object after = Foverlay_end (overlay);
21379 int ignore;
21380
21381 /* Note that we might not be able to find position
21382 BEFORE in the glyph matrix if the overlay is
21383 entirely covered by a `display' property. In
21384 this case, we overshoot. So let's stop in
21385 the glyph matrix before glyphs for OBJECT. */
21386 fast_find_position (w, XFASTINT (before),
21387 &dpyinfo->mouse_face_beg_col,
21388 &dpyinfo->mouse_face_beg_row,
21389 &dpyinfo->mouse_face_beg_x,
21390 &dpyinfo->mouse_face_beg_y,
21391 object);
21392
21393 dpyinfo->mouse_face_past_end
21394 = !fast_find_position (w, XFASTINT (after),
21395 &dpyinfo->mouse_face_end_col,
21396 &dpyinfo->mouse_face_end_row,
21397 &dpyinfo->mouse_face_end_x,
21398 &dpyinfo->mouse_face_end_y,
21399 Qnil);
21400 dpyinfo->mouse_face_window = window;
21401 dpyinfo->mouse_face_face_id
21402 = face_at_buffer_position (w, pos, 0, 0,
21403 &ignore, pos + 1,
21404 !dpyinfo->mouse_face_hidden);
21405
21406 /* Display it as active. */
21407 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21408 cursor = No_Cursor;
21409 }
21410 }
21411 }
21412
21413 check_help_echo:
21414
21415 /* Look for a `help-echo' property. */
21416 if (NILP (help_echo_string)) {
21417 Lisp_Object help, overlay;
21418
21419 /* Check overlays first. */
21420 help = overlay = Qnil;
21421 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21422 {
21423 overlay = overlay_vec[i];
21424 help = Foverlay_get (overlay, Qhelp_echo);
21425 }
21426
21427 if (!NILP (help))
21428 {
21429 help_echo_string = help;
21430 help_echo_window = window;
21431 help_echo_object = overlay;
21432 help_echo_pos = pos;
21433 }
21434 else
21435 {
21436 Lisp_Object object = glyph->object;
21437 int charpos = glyph->charpos;
21438
21439 /* Try text properties. */
21440 if (STRINGP (object)
21441 && charpos >= 0
21442 && charpos < SCHARS (object))
21443 {
21444 help = Fget_text_property (make_number (charpos),
21445 Qhelp_echo, object);
21446 if (NILP (help))
21447 {
21448 /* If the string itself doesn't specify a help-echo,
21449 see if the buffer text ``under'' it does. */
21450 struct glyph_row *r
21451 = MATRIX_ROW (w->current_matrix, vpos);
21452 int start = MATRIX_ROW_START_CHARPOS (r);
21453 int pos = string_buffer_position (w, object, start);
21454 if (pos > 0)
21455 {
21456 help = Fget_char_property (make_number (pos),
21457 Qhelp_echo, w->buffer);
21458 if (!NILP (help))
21459 {
21460 charpos = pos;
21461 object = w->buffer;
21462 }
21463 }
21464 }
21465 }
21466 else if (BUFFERP (object)
21467 && charpos >= BEGV
21468 && charpos < ZV)
21469 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21470 object);
21471
21472 if (!NILP (help))
21473 {
21474 help_echo_string = help;
21475 help_echo_window = window;
21476 help_echo_object = object;
21477 help_echo_pos = charpos;
21478 }
21479 }
21480 }
21481
21482 /* Look for a `pointer' property. */
21483 if (NILP (pointer))
21484 {
21485 /* Check overlays first. */
21486 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21487 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21488
21489 if (NILP (pointer))
21490 {
21491 Lisp_Object object = glyph->object;
21492 int charpos = glyph->charpos;
21493
21494 /* Try text properties. */
21495 if (STRINGP (object)
21496 && charpos >= 0
21497 && charpos < SCHARS (object))
21498 {
21499 pointer = Fget_text_property (make_number (charpos),
21500 Qpointer, object);
21501 if (NILP (pointer))
21502 {
21503 /* If the string itself doesn't specify a pointer,
21504 see if the buffer text ``under'' it does. */
21505 struct glyph_row *r
21506 = MATRIX_ROW (w->current_matrix, vpos);
21507 int start = MATRIX_ROW_START_CHARPOS (r);
21508 int pos = string_buffer_position (w, object, start);
21509 if (pos > 0)
21510 pointer = Fget_char_property (make_number (pos),
21511 Qpointer, w->buffer);
21512 }
21513 }
21514 else if (BUFFERP (object)
21515 && charpos >= BEGV
21516 && charpos < ZV)
21517 pointer = Fget_text_property (make_number (charpos),
21518 Qpointer, object);
21519 }
21520 }
21521
21522 BEGV = obegv;
21523 ZV = ozv;
21524 current_buffer = obuf;
21525 }
21526
21527 set_cursor:
21528
21529 define_frame_cursor1 (f, cursor, pointer);
21530 }
21531
21532
21533 /* EXPORT for RIF:
21534 Clear any mouse-face on window W. This function is part of the
21535 redisplay interface, and is called from try_window_id and similar
21536 functions to ensure the mouse-highlight is off. */
21537
21538 void
21539 x_clear_window_mouse_face (w)
21540 struct window *w;
21541 {
21542 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21543 Lisp_Object window;
21544
21545 BLOCK_INPUT;
21546 XSETWINDOW (window, w);
21547 if (EQ (window, dpyinfo->mouse_face_window))
21548 clear_mouse_face (dpyinfo);
21549 UNBLOCK_INPUT;
21550 }
21551
21552
21553 /* EXPORT:
21554 Just discard the mouse face information for frame F, if any.
21555 This is used when the size of F is changed. */
21556
21557 void
21558 cancel_mouse_face (f)
21559 struct frame *f;
21560 {
21561 Lisp_Object window;
21562 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21563
21564 window = dpyinfo->mouse_face_window;
21565 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21566 {
21567 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21568 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21569 dpyinfo->mouse_face_window = Qnil;
21570 }
21571 }
21572
21573
21574 #endif /* HAVE_WINDOW_SYSTEM */
21575
21576 \f
21577 /***********************************************************************
21578 Exposure Events
21579 ***********************************************************************/
21580
21581 #ifdef HAVE_WINDOW_SYSTEM
21582
21583 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21584 which intersects rectangle R. R is in window-relative coordinates. */
21585
21586 static void
21587 expose_area (w, row, r, area)
21588 struct window *w;
21589 struct glyph_row *row;
21590 XRectangle *r;
21591 enum glyph_row_area area;
21592 {
21593 struct glyph *first = row->glyphs[area];
21594 struct glyph *end = row->glyphs[area] + row->used[area];
21595 struct glyph *last;
21596 int first_x, start_x, x;
21597
21598 if (area == TEXT_AREA && row->fill_line_p)
21599 /* If row extends face to end of line write the whole line. */
21600 draw_glyphs (w, 0, row, area,
21601 0, row->used[area],
21602 DRAW_NORMAL_TEXT, 0);
21603 else
21604 {
21605 /* Set START_X to the window-relative start position for drawing glyphs of
21606 AREA. The first glyph of the text area can be partially visible.
21607 The first glyphs of other areas cannot. */
21608 start_x = window_box_left_offset (w, area);
21609 x = start_x;
21610 if (area == TEXT_AREA)
21611 x += row->x;
21612
21613 /* Find the first glyph that must be redrawn. */
21614 while (first < end
21615 && x + first->pixel_width < r->x)
21616 {
21617 x += first->pixel_width;
21618 ++first;
21619 }
21620
21621 /* Find the last one. */
21622 last = first;
21623 first_x = x;
21624 while (last < end
21625 && x < r->x + r->width)
21626 {
21627 x += last->pixel_width;
21628 ++last;
21629 }
21630
21631 /* Repaint. */
21632 if (last > first)
21633 draw_glyphs (w, first_x - start_x, row, area,
21634 first - row->glyphs[area], last - row->glyphs[area],
21635 DRAW_NORMAL_TEXT, 0);
21636 }
21637 }
21638
21639
21640 /* Redraw the parts of the glyph row ROW on window W intersecting
21641 rectangle R. R is in window-relative coordinates. Value is
21642 non-zero if mouse-face was overwritten. */
21643
21644 static int
21645 expose_line (w, row, r)
21646 struct window *w;
21647 struct glyph_row *row;
21648 XRectangle *r;
21649 {
21650 xassert (row->enabled_p);
21651
21652 if (row->mode_line_p || w->pseudo_window_p)
21653 draw_glyphs (w, 0, row, TEXT_AREA,
21654 0, row->used[TEXT_AREA],
21655 DRAW_NORMAL_TEXT, 0);
21656 else
21657 {
21658 if (row->used[LEFT_MARGIN_AREA])
21659 expose_area (w, row, r, LEFT_MARGIN_AREA);
21660 if (row->used[TEXT_AREA])
21661 expose_area (w, row, r, TEXT_AREA);
21662 if (row->used[RIGHT_MARGIN_AREA])
21663 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21664 draw_row_fringe_bitmaps (w, row);
21665 }
21666
21667 return row->mouse_face_p;
21668 }
21669
21670
21671 /* Redraw those parts of glyphs rows during expose event handling that
21672 overlap other rows. Redrawing of an exposed line writes over parts
21673 of lines overlapping that exposed line; this function fixes that.
21674
21675 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21676 row in W's current matrix that is exposed and overlaps other rows.
21677 LAST_OVERLAPPING_ROW is the last such row. */
21678
21679 static void
21680 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21681 struct window *w;
21682 struct glyph_row *first_overlapping_row;
21683 struct glyph_row *last_overlapping_row;
21684 {
21685 struct glyph_row *row;
21686
21687 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21688 if (row->overlapping_p)
21689 {
21690 xassert (row->enabled_p && !row->mode_line_p);
21691
21692 if (row->used[LEFT_MARGIN_AREA])
21693 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21694
21695 if (row->used[TEXT_AREA])
21696 x_fix_overlapping_area (w, row, TEXT_AREA);
21697
21698 if (row->used[RIGHT_MARGIN_AREA])
21699 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21700 }
21701 }
21702
21703
21704 /* Return non-zero if W's cursor intersects rectangle R. */
21705
21706 static int
21707 phys_cursor_in_rect_p (w, r)
21708 struct window *w;
21709 XRectangle *r;
21710 {
21711 XRectangle cr, result;
21712 struct glyph *cursor_glyph;
21713
21714 cursor_glyph = get_phys_cursor_glyph (w);
21715 if (cursor_glyph)
21716 {
21717 /* r is relative to W's box, but w->phys_cursor.x is relative
21718 to left edge of W's TEXT area. Adjust it. */
21719 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21720 cr.y = w->phys_cursor.y;
21721 cr.width = cursor_glyph->pixel_width;
21722 cr.height = w->phys_cursor_height;
21723 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21724 I assume the effect is the same -- and this is portable. */
21725 return x_intersect_rectangles (&cr, r, &result);
21726 }
21727 else
21728 return 0;
21729 }
21730
21731
21732 /* EXPORT:
21733 Draw a vertical window border to the right of window W if W doesn't
21734 have vertical scroll bars. */
21735
21736 void
21737 x_draw_vertical_border (w)
21738 struct window *w;
21739 {
21740 /* We could do better, if we knew what type of scroll-bar the adjacent
21741 windows (on either side) have... But we don't :-(
21742 However, I think this works ok. ++KFS 2003-04-25 */
21743
21744 /* Redraw borders between horizontally adjacent windows. Don't
21745 do it for frames with vertical scroll bars because either the
21746 right scroll bar of a window, or the left scroll bar of its
21747 neighbor will suffice as a border. */
21748 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
21749 return;
21750
21751 if (!WINDOW_RIGHTMOST_P (w)
21752 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21753 {
21754 int x0, x1, y0, y1;
21755
21756 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21757 y1 -= 1;
21758
21759 rif->draw_vertical_window_border (w, x1, y0, y1);
21760 }
21761 else if (!WINDOW_LEFTMOST_P (w)
21762 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21763 {
21764 int x0, x1, y0, y1;
21765
21766 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21767 y1 -= 1;
21768
21769 rif->draw_vertical_window_border (w, x0, y0, y1);
21770 }
21771 }
21772
21773
21774 /* Redraw the part of window W intersection rectangle FR. Pixel
21775 coordinates in FR are frame-relative. Call this function with
21776 input blocked. Value is non-zero if the exposure overwrites
21777 mouse-face. */
21778
21779 static int
21780 expose_window (w, fr)
21781 struct window *w;
21782 XRectangle *fr;
21783 {
21784 struct frame *f = XFRAME (w->frame);
21785 XRectangle wr, r;
21786 int mouse_face_overwritten_p = 0;
21787
21788 /* If window is not yet fully initialized, do nothing. This can
21789 happen when toolkit scroll bars are used and a window is split.
21790 Reconfiguring the scroll bar will generate an expose for a newly
21791 created window. */
21792 if (w->current_matrix == NULL)
21793 return 0;
21794
21795 /* When we're currently updating the window, display and current
21796 matrix usually don't agree. Arrange for a thorough display
21797 later. */
21798 if (w == updated_window)
21799 {
21800 SET_FRAME_GARBAGED (f);
21801 return 0;
21802 }
21803
21804 /* Frame-relative pixel rectangle of W. */
21805 wr.x = WINDOW_LEFT_EDGE_X (w);
21806 wr.y = WINDOW_TOP_EDGE_Y (w);
21807 wr.width = WINDOW_TOTAL_WIDTH (w);
21808 wr.height = WINDOW_TOTAL_HEIGHT (w);
21809
21810 if (x_intersect_rectangles (fr, &wr, &r))
21811 {
21812 int yb = window_text_bottom_y (w);
21813 struct glyph_row *row;
21814 int cursor_cleared_p;
21815 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21816
21817 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21818 r.x, r.y, r.width, r.height));
21819
21820 /* Convert to window coordinates. */
21821 r.x -= WINDOW_LEFT_EDGE_X (w);
21822 r.y -= WINDOW_TOP_EDGE_Y (w);
21823
21824 /* Turn off the cursor. */
21825 if (!w->pseudo_window_p
21826 && phys_cursor_in_rect_p (w, &r))
21827 {
21828 x_clear_cursor (w);
21829 cursor_cleared_p = 1;
21830 }
21831 else
21832 cursor_cleared_p = 0;
21833
21834 /* Update lines intersecting rectangle R. */
21835 first_overlapping_row = last_overlapping_row = NULL;
21836 for (row = w->current_matrix->rows;
21837 row->enabled_p;
21838 ++row)
21839 {
21840 int y0 = row->y;
21841 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21842
21843 if ((y0 >= r.y && y0 < r.y + r.height)
21844 || (y1 > r.y && y1 < r.y + r.height)
21845 || (r.y >= y0 && r.y < y1)
21846 || (r.y + r.height > y0 && r.y + r.height < y1))
21847 {
21848 if (row->overlapping_p)
21849 {
21850 if (first_overlapping_row == NULL)
21851 first_overlapping_row = row;
21852 last_overlapping_row = row;
21853 }
21854
21855 if (expose_line (w, row, &r))
21856 mouse_face_overwritten_p = 1;
21857 }
21858
21859 if (y1 >= yb)
21860 break;
21861 }
21862
21863 /* Display the mode line if there is one. */
21864 if (WINDOW_WANTS_MODELINE_P (w)
21865 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21866 row->enabled_p)
21867 && row->y < r.y + r.height)
21868 {
21869 if (expose_line (w, row, &r))
21870 mouse_face_overwritten_p = 1;
21871 }
21872
21873 if (!w->pseudo_window_p)
21874 {
21875 /* Fix the display of overlapping rows. */
21876 if (first_overlapping_row)
21877 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21878
21879 /* Draw border between windows. */
21880 x_draw_vertical_border (w);
21881
21882 /* Turn the cursor on again. */
21883 if (cursor_cleared_p)
21884 update_window_cursor (w, 1);
21885 }
21886 }
21887
21888 #ifdef HAVE_CARBON
21889 /* Display scroll bar for this window. */
21890 if (!NILP (w->vertical_scroll_bar))
21891 {
21892 /* ++KFS:
21893 If this doesn't work here (maybe some header files are missing),
21894 make a function in macterm.c and call it to do the job! */
21895 ControlHandle ch
21896 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21897
21898 Draw1Control (ch);
21899 }
21900 #endif
21901
21902 return mouse_face_overwritten_p;
21903 }
21904
21905
21906
21907 /* Redraw (parts) of all windows in the window tree rooted at W that
21908 intersect R. R contains frame pixel coordinates. Value is
21909 non-zero if the exposure overwrites mouse-face. */
21910
21911 static int
21912 expose_window_tree (w, r)
21913 struct window *w;
21914 XRectangle *r;
21915 {
21916 struct frame *f = XFRAME (w->frame);
21917 int mouse_face_overwritten_p = 0;
21918
21919 while (w && !FRAME_GARBAGED_P (f))
21920 {
21921 if (!NILP (w->hchild))
21922 mouse_face_overwritten_p
21923 |= expose_window_tree (XWINDOW (w->hchild), r);
21924 else if (!NILP (w->vchild))
21925 mouse_face_overwritten_p
21926 |= expose_window_tree (XWINDOW (w->vchild), r);
21927 else
21928 mouse_face_overwritten_p |= expose_window (w, r);
21929
21930 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21931 }
21932
21933 return mouse_face_overwritten_p;
21934 }
21935
21936
21937 /* EXPORT:
21938 Redisplay an exposed area of frame F. X and Y are the upper-left
21939 corner of the exposed rectangle. W and H are width and height of
21940 the exposed area. All are pixel values. W or H zero means redraw
21941 the entire frame. */
21942
21943 void
21944 expose_frame (f, x, y, w, h)
21945 struct frame *f;
21946 int x, y, w, h;
21947 {
21948 XRectangle r;
21949 int mouse_face_overwritten_p = 0;
21950
21951 TRACE ((stderr, "expose_frame "));
21952
21953 /* No need to redraw if frame will be redrawn soon. */
21954 if (FRAME_GARBAGED_P (f))
21955 {
21956 TRACE ((stderr, " garbaged\n"));
21957 return;
21958 }
21959
21960 #ifdef HAVE_CARBON
21961 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21962 or deactivated here, for unknown reasons, activated scroll bars
21963 are shown in deactivated frames in some instances. */
21964 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21965 activate_scroll_bars (f);
21966 else
21967 deactivate_scroll_bars (f);
21968 #endif
21969
21970 /* If basic faces haven't been realized yet, there is no point in
21971 trying to redraw anything. This can happen when we get an expose
21972 event while Emacs is starting, e.g. by moving another window. */
21973 if (FRAME_FACE_CACHE (f) == NULL
21974 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21975 {
21976 TRACE ((stderr, " no faces\n"));
21977 return;
21978 }
21979
21980 if (w == 0 || h == 0)
21981 {
21982 r.x = r.y = 0;
21983 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21984 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21985 }
21986 else
21987 {
21988 r.x = x;
21989 r.y = y;
21990 r.width = w;
21991 r.height = h;
21992 }
21993
21994 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21995 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21996
21997 if (WINDOWP (f->tool_bar_window))
21998 mouse_face_overwritten_p
21999 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22000
22001 #ifdef HAVE_X_WINDOWS
22002 #ifndef MSDOS
22003 #ifndef USE_X_TOOLKIT
22004 if (WINDOWP (f->menu_bar_window))
22005 mouse_face_overwritten_p
22006 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22007 #endif /* not USE_X_TOOLKIT */
22008 #endif
22009 #endif
22010
22011 /* Some window managers support a focus-follows-mouse style with
22012 delayed raising of frames. Imagine a partially obscured frame,
22013 and moving the mouse into partially obscured mouse-face on that
22014 frame. The visible part of the mouse-face will be highlighted,
22015 then the WM raises the obscured frame. With at least one WM, KDE
22016 2.1, Emacs is not getting any event for the raising of the frame
22017 (even tried with SubstructureRedirectMask), only Expose events.
22018 These expose events will draw text normally, i.e. not
22019 highlighted. Which means we must redo the highlight here.
22020 Subsume it under ``we love X''. --gerd 2001-08-15 */
22021 /* Included in Windows version because Windows most likely does not
22022 do the right thing if any third party tool offers
22023 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22024 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22025 {
22026 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22027 if (f == dpyinfo->mouse_face_mouse_frame)
22028 {
22029 int x = dpyinfo->mouse_face_mouse_x;
22030 int y = dpyinfo->mouse_face_mouse_y;
22031 clear_mouse_face (dpyinfo);
22032 note_mouse_highlight (f, x, y);
22033 }
22034 }
22035 }
22036
22037
22038 /* EXPORT:
22039 Determine the intersection of two rectangles R1 and R2. Return
22040 the intersection in *RESULT. Value is non-zero if RESULT is not
22041 empty. */
22042
22043 int
22044 x_intersect_rectangles (r1, r2, result)
22045 XRectangle *r1, *r2, *result;
22046 {
22047 XRectangle *left, *right;
22048 XRectangle *upper, *lower;
22049 int intersection_p = 0;
22050
22051 /* Rearrange so that R1 is the left-most rectangle. */
22052 if (r1->x < r2->x)
22053 left = r1, right = r2;
22054 else
22055 left = r2, right = r1;
22056
22057 /* X0 of the intersection is right.x0, if this is inside R1,
22058 otherwise there is no intersection. */
22059 if (right->x <= left->x + left->width)
22060 {
22061 result->x = right->x;
22062
22063 /* The right end of the intersection is the minimum of the
22064 the right ends of left and right. */
22065 result->width = (min (left->x + left->width, right->x + right->width)
22066 - result->x);
22067
22068 /* Same game for Y. */
22069 if (r1->y < r2->y)
22070 upper = r1, lower = r2;
22071 else
22072 upper = r2, lower = r1;
22073
22074 /* The upper end of the intersection is lower.y0, if this is inside
22075 of upper. Otherwise, there is no intersection. */
22076 if (lower->y <= upper->y + upper->height)
22077 {
22078 result->y = lower->y;
22079
22080 /* The lower end of the intersection is the minimum of the lower
22081 ends of upper and lower. */
22082 result->height = (min (lower->y + lower->height,
22083 upper->y + upper->height)
22084 - result->y);
22085 intersection_p = 1;
22086 }
22087 }
22088
22089 return intersection_p;
22090 }
22091
22092 #endif /* HAVE_WINDOW_SYSTEM */
22093
22094 \f
22095 /***********************************************************************
22096 Initialization
22097 ***********************************************************************/
22098
22099 void
22100 syms_of_xdisp ()
22101 {
22102 Vwith_echo_area_save_vector = Qnil;
22103 staticpro (&Vwith_echo_area_save_vector);
22104
22105 Vmessage_stack = Qnil;
22106 staticpro (&Vmessage_stack);
22107
22108 Qinhibit_redisplay = intern ("inhibit-redisplay");
22109 staticpro (&Qinhibit_redisplay);
22110
22111 message_dolog_marker1 = Fmake_marker ();
22112 staticpro (&message_dolog_marker1);
22113 message_dolog_marker2 = Fmake_marker ();
22114 staticpro (&message_dolog_marker2);
22115 message_dolog_marker3 = Fmake_marker ();
22116 staticpro (&message_dolog_marker3);
22117
22118 #if GLYPH_DEBUG
22119 defsubr (&Sdump_frame_glyph_matrix);
22120 defsubr (&Sdump_glyph_matrix);
22121 defsubr (&Sdump_glyph_row);
22122 defsubr (&Sdump_tool_bar_row);
22123 defsubr (&Strace_redisplay);
22124 defsubr (&Strace_to_stderr);
22125 #endif
22126 #ifdef HAVE_WINDOW_SYSTEM
22127 defsubr (&Stool_bar_lines_needed);
22128 defsubr (&Slookup_image_map);
22129 #endif
22130 defsubr (&Sformat_mode_line);
22131
22132 staticpro (&Qmenu_bar_update_hook);
22133 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22134
22135 staticpro (&Qoverriding_terminal_local_map);
22136 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22137
22138 staticpro (&Qoverriding_local_map);
22139 Qoverriding_local_map = intern ("overriding-local-map");
22140
22141 staticpro (&Qwindow_scroll_functions);
22142 Qwindow_scroll_functions = intern ("window-scroll-functions");
22143
22144 staticpro (&Qredisplay_end_trigger_functions);
22145 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22146
22147 staticpro (&Qinhibit_point_motion_hooks);
22148 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22149
22150 QCdata = intern (":data");
22151 staticpro (&QCdata);
22152 Qdisplay = intern ("display");
22153 staticpro (&Qdisplay);
22154 Qspace_width = intern ("space-width");
22155 staticpro (&Qspace_width);
22156 Qraise = intern ("raise");
22157 staticpro (&Qraise);
22158 Qslice = intern ("slice");
22159 staticpro (&Qslice);
22160 Qspace = intern ("space");
22161 staticpro (&Qspace);
22162 Qmargin = intern ("margin");
22163 staticpro (&Qmargin);
22164 Qpointer = intern ("pointer");
22165 staticpro (&Qpointer);
22166 Qleft_margin = intern ("left-margin");
22167 staticpro (&Qleft_margin);
22168 Qright_margin = intern ("right-margin");
22169 staticpro (&Qright_margin);
22170 Qcenter = intern ("center");
22171 staticpro (&Qcenter);
22172 Qline_height = intern ("line-height");
22173 staticpro (&Qline_height);
22174 Qtotal = intern ("total");
22175 staticpro (&Qtotal);
22176 QCalign_to = intern (":align-to");
22177 staticpro (&QCalign_to);
22178 QCrelative_width = intern (":relative-width");
22179 staticpro (&QCrelative_width);
22180 QCrelative_height = intern (":relative-height");
22181 staticpro (&QCrelative_height);
22182 QCeval = intern (":eval");
22183 staticpro (&QCeval);
22184 QCpropertize = intern (":propertize");
22185 staticpro (&QCpropertize);
22186 QCfile = intern (":file");
22187 staticpro (&QCfile);
22188 Qfontified = intern ("fontified");
22189 staticpro (&Qfontified);
22190 Qfontification_functions = intern ("fontification-functions");
22191 staticpro (&Qfontification_functions);
22192 Qtrailing_whitespace = intern ("trailing-whitespace");
22193 staticpro (&Qtrailing_whitespace);
22194 Qescape_glyph = intern ("escape-glyph");
22195 staticpro (&Qescape_glyph);
22196 Qimage = intern ("image");
22197 staticpro (&Qimage);
22198 QCmap = intern (":map");
22199 staticpro (&QCmap);
22200 QCpointer = intern (":pointer");
22201 staticpro (&QCpointer);
22202 Qrect = intern ("rect");
22203 staticpro (&Qrect);
22204 Qcircle = intern ("circle");
22205 staticpro (&Qcircle);
22206 Qpoly = intern ("poly");
22207 staticpro (&Qpoly);
22208 Qmessage_truncate_lines = intern ("message-truncate-lines");
22209 staticpro (&Qmessage_truncate_lines);
22210 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22211 staticpro (&Qcursor_in_non_selected_windows);
22212 Qgrow_only = intern ("grow-only");
22213 staticpro (&Qgrow_only);
22214 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22215 staticpro (&Qinhibit_menubar_update);
22216 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22217 staticpro (&Qinhibit_eval_during_redisplay);
22218 Qposition = intern ("position");
22219 staticpro (&Qposition);
22220 Qbuffer_position = intern ("buffer-position");
22221 staticpro (&Qbuffer_position);
22222 Qobject = intern ("object");
22223 staticpro (&Qobject);
22224 Qbar = intern ("bar");
22225 staticpro (&Qbar);
22226 Qhbar = intern ("hbar");
22227 staticpro (&Qhbar);
22228 Qbox = intern ("box");
22229 staticpro (&Qbox);
22230 Qhollow = intern ("hollow");
22231 staticpro (&Qhollow);
22232 Qhand = intern ("hand");
22233 staticpro (&Qhand);
22234 Qarrow = intern ("arrow");
22235 staticpro (&Qarrow);
22236 Qtext = intern ("text");
22237 staticpro (&Qtext);
22238 Qrisky_local_variable = intern ("risky-local-variable");
22239 staticpro (&Qrisky_local_variable);
22240 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22241 staticpro (&Qinhibit_free_realized_faces);
22242
22243 list_of_error = Fcons (Fcons (intern ("error"),
22244 Fcons (intern ("void-variable"), Qnil)),
22245 Qnil);
22246 staticpro (&list_of_error);
22247
22248 Qlast_arrow_position = intern ("last-arrow-position");
22249 staticpro (&Qlast_arrow_position);
22250 Qlast_arrow_string = intern ("last-arrow-string");
22251 staticpro (&Qlast_arrow_string);
22252
22253 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22254 staticpro (&Qoverlay_arrow_string);
22255 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22256 staticpro (&Qoverlay_arrow_bitmap);
22257
22258 echo_buffer[0] = echo_buffer[1] = Qnil;
22259 staticpro (&echo_buffer[0]);
22260 staticpro (&echo_buffer[1]);
22261
22262 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22263 staticpro (&echo_area_buffer[0]);
22264 staticpro (&echo_area_buffer[1]);
22265
22266 Vmessages_buffer_name = build_string ("*Messages*");
22267 staticpro (&Vmessages_buffer_name);
22268
22269 mode_line_proptrans_alist = Qnil;
22270 staticpro (&mode_line_proptrans_alist);
22271
22272 mode_line_string_list = Qnil;
22273 staticpro (&mode_line_string_list);
22274
22275 help_echo_string = Qnil;
22276 staticpro (&help_echo_string);
22277 help_echo_object = Qnil;
22278 staticpro (&help_echo_object);
22279 help_echo_window = Qnil;
22280 staticpro (&help_echo_window);
22281 previous_help_echo_string = Qnil;
22282 staticpro (&previous_help_echo_string);
22283 help_echo_pos = -1;
22284
22285 #ifdef HAVE_WINDOW_SYSTEM
22286 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22287 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22288 For example, if a block cursor is over a tab, it will be drawn as
22289 wide as that tab on the display. */);
22290 x_stretch_cursor_p = 0;
22291 #endif
22292
22293 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22294 doc: /* *Non-nil means highlight trailing whitespace.
22295 The face used for trailing whitespace is `trailing-whitespace'. */);
22296 Vshow_trailing_whitespace = Qnil;
22297
22298 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22299 doc: /* *The pointer shape to show in void text areas.
22300 Nil means to show the text pointer. Other options are `arrow', `text',
22301 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22302 Vvoid_text_area_pointer = Qarrow;
22303
22304 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22305 doc: /* Non-nil means don't actually do any redisplay.
22306 This is used for internal purposes. */);
22307 Vinhibit_redisplay = Qnil;
22308
22309 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22310 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22311 Vglobal_mode_string = Qnil;
22312
22313 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22314 doc: /* Marker for where to display an arrow on top of the buffer text.
22315 This must be the beginning of a line in order to work.
22316 See also `overlay-arrow-string'. */);
22317 Voverlay_arrow_position = Qnil;
22318
22319 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22320 doc: /* String to display as an arrow in non-window frames.
22321 See also `overlay-arrow-position'. */);
22322 Voverlay_arrow_string = Qnil;
22323
22324 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22325 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22326 The symbols on this list are examined during redisplay to determine
22327 where to display overlay arrows. */);
22328 Voverlay_arrow_variable_list
22329 = Fcons (intern ("overlay-arrow-position"), Qnil);
22330
22331 DEFVAR_INT ("scroll-step", &scroll_step,
22332 doc: /* *The number of lines to try scrolling a window by when point moves out.
22333 If that fails to bring point back on frame, point is centered instead.
22334 If this is zero, point is always centered after it moves off frame.
22335 If you want scrolling to always be a line at a time, you should set
22336 `scroll-conservatively' to a large value rather than set this to 1. */);
22337
22338 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22339 doc: /* *Scroll up to this many lines, to bring point back on screen.
22340 A value of zero means to scroll the text to center point vertically
22341 in the window. */);
22342 scroll_conservatively = 0;
22343
22344 DEFVAR_INT ("scroll-margin", &scroll_margin,
22345 doc: /* *Number of lines of margin at the top and bottom of a window.
22346 Recenter the window whenever point gets within this many lines
22347 of the top or bottom of the window. */);
22348 scroll_margin = 0;
22349
22350 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22351 doc: /* Pixels per inch on current display.
22352 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22353 Vdisplay_pixels_per_inch = make_float (72.0);
22354
22355 #if GLYPH_DEBUG
22356 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22357 #endif
22358
22359 DEFVAR_BOOL ("truncate-partial-width-windows",
22360 &truncate_partial_width_windows,
22361 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22362 truncate_partial_width_windows = 1;
22363
22364 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22365 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22366 Any other value means to use the appropriate face, `mode-line',
22367 `header-line', or `menu' respectively. */);
22368 mode_line_inverse_video = 1;
22369
22370 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22371 doc: /* *Maximum buffer size for which line number should be displayed.
22372 If the buffer is bigger than this, the line number does not appear
22373 in the mode line. A value of nil means no limit. */);
22374 Vline_number_display_limit = Qnil;
22375
22376 DEFVAR_INT ("line-number-display-limit-width",
22377 &line_number_display_limit_width,
22378 doc: /* *Maximum line width (in characters) for line number display.
22379 If the average length of the lines near point is bigger than this, then the
22380 line number may be omitted from the mode line. */);
22381 line_number_display_limit_width = 200;
22382
22383 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22384 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22385 highlight_nonselected_windows = 0;
22386
22387 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22388 doc: /* Non-nil if more than one frame is visible on this display.
22389 Minibuffer-only frames don't count, but iconified frames do.
22390 This variable is not guaranteed to be accurate except while processing
22391 `frame-title-format' and `icon-title-format'. */);
22392
22393 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22394 doc: /* Template for displaying the title bar of visible frames.
22395 \(Assuming the window manager supports this feature.)
22396 This variable has the same structure as `mode-line-format' (which see),
22397 and is used only on frames for which no explicit name has been set
22398 \(see `modify-frame-parameters'). */);
22399
22400 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22401 doc: /* Template for displaying the title bar of an iconified frame.
22402 \(Assuming the window manager supports this feature.)
22403 This variable has the same structure as `mode-line-format' (which see),
22404 and is used only on frames for which no explicit name has been set
22405 \(see `modify-frame-parameters'). */);
22406 Vicon_title_format
22407 = Vframe_title_format
22408 = Fcons (intern ("multiple-frames"),
22409 Fcons (build_string ("%b"),
22410 Fcons (Fcons (empty_string,
22411 Fcons (intern ("invocation-name"),
22412 Fcons (build_string ("@"),
22413 Fcons (intern ("system-name"),
22414 Qnil)))),
22415 Qnil)));
22416
22417 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22418 doc: /* Maximum number of lines to keep in the message log buffer.
22419 If nil, disable message logging. If t, log messages but don't truncate
22420 the buffer when it becomes large. */);
22421 Vmessage_log_max = make_number (50);
22422
22423 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22424 doc: /* Functions called before redisplay, if window sizes have changed.
22425 The value should be a list of functions that take one argument.
22426 Just before redisplay, for each frame, if any of its windows have changed
22427 size since the last redisplay, or have been split or deleted,
22428 all the functions in the list are called, with the frame as argument. */);
22429 Vwindow_size_change_functions = Qnil;
22430
22431 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22432 doc: /* List of functions to call before redisplaying a window with scrolling.
22433 Each function is called with two arguments, the window
22434 and its new display-start position. Note that the value of `window-end'
22435 is not valid when these functions are called. */);
22436 Vwindow_scroll_functions = Qnil;
22437
22438 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22439 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22440 mouse_autoselect_window = 0;
22441
22442 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22443 doc: /* *Non-nil means automatically resize tool-bars.
22444 This increases a tool-bar's height if not all tool-bar items are visible.
22445 It decreases a tool-bar's height when it would display blank lines
22446 otherwise. */);
22447 auto_resize_tool_bars_p = 1;
22448
22449 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22450 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22451 auto_raise_tool_bar_buttons_p = 1;
22452
22453 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22454 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22455 make_cursor_line_fully_visible_p = 1;
22456
22457 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22458 doc: /* *Margin around tool-bar buttons in pixels.
22459 If an integer, use that for both horizontal and vertical margins.
22460 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22461 HORZ specifying the horizontal margin, and VERT specifying the
22462 vertical margin. */);
22463 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22464
22465 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22466 doc: /* *Relief thickness of tool-bar buttons. */);
22467 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22468
22469 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22470 doc: /* List of functions to call to fontify regions of text.
22471 Each function is called with one argument POS. Functions must
22472 fontify a region starting at POS in the current buffer, and give
22473 fontified regions the property `fontified'. */);
22474 Vfontification_functions = Qnil;
22475 Fmake_variable_buffer_local (Qfontification_functions);
22476
22477 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22478 &unibyte_display_via_language_environment,
22479 doc: /* *Non-nil means display unibyte text according to language environment.
22480 Specifically this means that unibyte non-ASCII characters
22481 are displayed by converting them to the equivalent multibyte characters
22482 according to the current language environment. As a result, they are
22483 displayed according to the current fontset. */);
22484 unibyte_display_via_language_environment = 0;
22485
22486 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22487 doc: /* *Maximum height for resizing mini-windows.
22488 If a float, it specifies a fraction of the mini-window frame's height.
22489 If an integer, it specifies a number of lines. */);
22490 Vmax_mini_window_height = make_float (0.25);
22491
22492 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22493 doc: /* *How to resize mini-windows.
22494 A value of nil means don't automatically resize mini-windows.
22495 A value of t means resize them to fit the text displayed in them.
22496 A value of `grow-only', the default, means let mini-windows grow
22497 only, until their display becomes empty, at which point the windows
22498 go back to their normal size. */);
22499 Vresize_mini_windows = Qgrow_only;
22500
22501 DEFVAR_LISP ("cursor-in-non-selected-windows",
22502 &Vcursor_in_non_selected_windows,
22503 doc: /* *Cursor type to display in non-selected windows.
22504 t means to use hollow box cursor. See `cursor-type' for other values. */);
22505 Vcursor_in_non_selected_windows = Qt;
22506
22507 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22508 doc: /* Alist specifying how to blink the cursor off.
22509 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22510 `cursor-type' frame-parameter or variable equals ON-STATE,
22511 comparing using `equal', Emacs uses OFF-STATE to specify
22512 how to blink it off. */);
22513 Vblink_cursor_alist = Qnil;
22514
22515 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22516 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22517 automatic_hscrolling_p = 1;
22518
22519 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22520 doc: /* *How many columns away from the window edge point is allowed to get
22521 before automatic hscrolling will horizontally scroll the window. */);
22522 hscroll_margin = 5;
22523
22524 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22525 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22526 When point is less than `automatic-hscroll-margin' columns from the window
22527 edge, automatic hscrolling will scroll the window by the amount of columns
22528 determined by this variable. If its value is a positive integer, scroll that
22529 many columns. If it's a positive floating-point number, it specifies the
22530 fraction of the window's width to scroll. If it's nil or zero, point will be
22531 centered horizontally after the scroll. Any other value, including negative
22532 numbers, are treated as if the value were zero.
22533
22534 Automatic hscrolling always moves point outside the scroll margin, so if
22535 point was more than scroll step columns inside the margin, the window will
22536 scroll more than the value given by the scroll step.
22537
22538 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22539 and `scroll-right' overrides this variable's effect. */);
22540 Vhscroll_step = make_number (0);
22541
22542 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22543 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22544 Bind this around calls to `message' to let it take effect. */);
22545 message_truncate_lines = 0;
22546
22547 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22548 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22549 Can be used to update submenus whose contents should vary. */);
22550 Vmenu_bar_update_hook = Qnil;
22551
22552 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22553 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22554 inhibit_menubar_update = 0;
22555
22556 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22557 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22558 inhibit_eval_during_redisplay = 0;
22559
22560 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22561 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22562 inhibit_free_realized_faces = 0;
22563
22564 #if GLYPH_DEBUG
22565 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22566 doc: /* Inhibit try_window_id display optimization. */);
22567 inhibit_try_window_id = 0;
22568
22569 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22570 doc: /* Inhibit try_window_reusing display optimization. */);
22571 inhibit_try_window_reusing = 0;
22572
22573 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22574 doc: /* Inhibit try_cursor_movement display optimization. */);
22575 inhibit_try_cursor_movement = 0;
22576 #endif /* GLYPH_DEBUG */
22577 }
22578
22579
22580 /* Initialize this module when Emacs starts. */
22581
22582 void
22583 init_xdisp ()
22584 {
22585 Lisp_Object root_window;
22586 struct window *mini_w;
22587
22588 current_header_line_height = current_mode_line_height = -1;
22589
22590 CHARPOS (this_line_start_pos) = 0;
22591
22592 mini_w = XWINDOW (minibuf_window);
22593 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22594
22595 if (!noninteractive)
22596 {
22597 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22598 int i;
22599
22600 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22601 set_window_height (root_window,
22602 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22603 0);
22604 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22605 set_window_height (minibuf_window, 1, 0);
22606
22607 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22608 mini_w->total_cols = make_number (FRAME_COLS (f));
22609
22610 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22611 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22612 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22613
22614 /* The default ellipsis glyphs `...'. */
22615 for (i = 0; i < 3; ++i)
22616 default_invis_vector[i] = make_number ('.');
22617 }
22618
22619 {
22620 /* Allocate the buffer for frame titles.
22621 Also used for `format-mode-line'. */
22622 int size = 100;
22623 frame_title_buf = (char *) xmalloc (size);
22624 frame_title_buf_end = frame_title_buf + size;
22625 frame_title_ptr = NULL;
22626 }
22627
22628 help_echo_showing_p = 0;
22629 }
22630
22631
22632 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22633 (do not change this comment) */