Implemented multiple tty support.
[bpt/emacs.git] / src / xdisp.c
CommitLineData
a2889657 1/* Display generation from window structure and buffer text.
1a89be1e 2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03
5f5c8ee5 3 Free Software Foundation, Inc.
a2889657
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
b1d1124b 9the Free Software Foundation; either version 2, or (at your option)
a2889657
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
a2889657 21
5f5c8ee5
GM
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,
48400103 36 let's say almost---see the description of direct update
4b41cebb 37 operations, below.)
5f5c8ee5
GM
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)
2311178e 50 direct_output_for_insert,
5f5c8ee5
GM
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
82a7ab23 55 +--------------+ redisplay +----------------+
5f5c8ee5
GM
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
959bc044 67 What does redisplay do? Obviously, it has to figure out somehow what
5f5c8ee5
GM
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
48400103 91 You will find a lot of redisplay optimizations when you start
5f5c8ee5
GM
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
2311178e 109
5f5c8ee5
GM
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
959bc044 123 interface functions taking an iterator structure (struct it)
5f5c8ee5
GM
124 argument.
125
48400103 126 Iteration over things to be displayed is then simple. It is
1178ddde
GM
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
5f5c8ee5
GM
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. */
a2889657 168
18160b98 169#include <config.h>
a2889657 170#include <stdio.h>
7ee72033 171
a2889657 172#include "lisp.h"
546a4f00 173#include "keyboard.h"
44fa5b1e 174#include "frame.h"
a2889657 175#include "window.h"
28d440ab 176#include "systty.h" /* For emacs_tty in termchar.h */
a2889657
JB
177#include "termchar.h"
178#include "dispextern.h"
179#include "buffer.h"
1c9241f5 180#include "charset.h"
a2889657
JB
181#include "indent.h"
182#include "commands.h"
fa3c6b4d 183#include "keymap.h"
a2889657
JB
184#include "macros.h"
185#include "disptab.h"
30c566e4 186#include "termhooks.h"
b0a0fbda 187#include "intervals.h"
1c9241f5
KH
188#include "coding.h"
189#include "process.h"
dfcf069d 190#include "region-cache.h"
0ef75e87 191#include "fontset.h"
f9b3d256 192#include "blockinput.h"
dfcf069d 193
6d55d620 194#ifdef HAVE_X_WINDOWS
dfcf069d
AS
195#include "xterm.h"
196#endif
a75dfea0
AI
197#ifdef WINDOWSNT
198#include "w32term.h"
199#endif
e0f712ba 200#ifdef MAC_OS
1a578e9b 201#include "macterm.h"
365fa1b3
AC
202
203Cursor No_Cursor;
1a578e9b 204#endif
a2889657 205
1dabd0cf
KS
206#ifndef FRAME_X_OUTPUT
207#define FRAME_X_OUTPUT(f) ((f)->output_data.x)
208#endif
209
5f5c8ee5
GM
210#define INFINITY 10000000
211
488dd4c4
JD
212#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
213 || defined (USE_GTK)
2e621225 214extern void set_frame_menubar P_ ((struct frame *f, int, int));
cd6dfed6 215extern int pending_menu_activation;
76412d64
RS
216#endif
217
a2889657
JB
218extern int interrupt_input;
219extern int command_loop_level;
220
b6436d4e 221extern int minibuffer_auto_raise;
b2b5455d 222extern Lisp_Object Vminibuffer_list;
b6436d4e 223
c4628384 224extern Lisp_Object Qface;
fec8f23e 225extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
c4628384 226
399164b4
KH
227extern Lisp_Object Voverriding_local_map;
228extern Lisp_Object Voverriding_local_map_menu_flag;
5f5c8ee5 229extern Lisp_Object Qmenu_item;
0b063c27 230extern Lisp_Object Qwhen;
fa3c6b4d 231extern Lisp_Object Qhelp_echo;
399164b4 232
d46fb96a 233Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
75c43375 234Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
e0bfbde6 235Lisp_Object Qredisplay_end_trigger_functions;
2e54982e 236Lisp_Object Qinhibit_point_motion_hooks;
0b063c27 237Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
5f5c8ee5 238Lisp_Object Qfontified;
6422c1d7 239Lisp_Object Qgrow_only;
869fb12c 240Lisp_Object Qinhibit_eval_during_redisplay;
b384d6f8 241Lisp_Object Qbuffer_position, Qposition, Qobject;
5f5c8ee5 242
cfe03a41
KS
243/* Cursor shapes */
244Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245
c53a1624
RS
246Lisp_Object Qrisky_local_variable;
247
7033d6df
RS
248/* Holds the list (error). */
249Lisp_Object list_of_error;
250
5f5c8ee5
GM
251/* Functions called to fontify regions of text. */
252
253Lisp_Object Vfontification_functions;
254Lisp_Object Qfontification_functions;
255
fa3c6b4d
KS
256/* Non-zero means automatically select any window when the mouse
257 cursor moves into it. */
258int mouse_autoselect_window;
259
e037b9ec 260/* Non-zero means draw tool bar buttons raised when the mouse moves
5f5c8ee5
GM
261 over them. */
262
e037b9ec 263int auto_raise_tool_bar_buttons_p;
5f5c8ee5 264
e037b9ec 265/* Margin around tool bar buttons in pixels. */
5f5c8ee5 266
35a41507 267Lisp_Object Vtool_bar_button_margin;
5f5c8ee5 268
e037b9ec 269/* Thickness of shadow to draw around tool bar buttons. */
5f5c8ee5 270
31ade731 271EMACS_INT tool_bar_button_relief;
5f5c8ee5 272
e037b9ec 273/* Non-zero means automatically resize tool-bars so that all tool-bar
5f5c8ee5
GM
274 items are visible, and no blank lines remain. */
275
e037b9ec 276int auto_resize_tool_bars_p;
399164b4 277
fa3c6b4d
KS
278/* Non-zero means draw block and hollow cursor as wide as the glyph
279 under it. For example, if a block cursor is over a tab, it will be
280 drawn as wide as that tab on the display. */
281
282int x_stretch_cursor_p;
283
735c094c
KH
284/* Non-nil means don't actually do any redisplay. */
285
286Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
287
30a3f61c
GM
288/* Non-zero means Lisp evaluation during redisplay is inhibited. */
289
869fb12c 290int inhibit_eval_during_redisplay;
30a3f61c 291
5f5c8ee5
GM
292/* Names of text properties relevant for redisplay. */
293
a7e27ef7 294Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
4b41cebb 295extern Lisp_Object Qface, Qinvisible, Qwidth;
5f5c8ee5
GM
296
297/* Symbols used in text property values. */
298
f65536fa 299Lisp_Object Vdisplay_pixels_per_inch;
5f5c8ee5 300Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
a7e27ef7 301Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
f3751a65 302Lisp_Object Qmargin;
a7e27ef7 303extern Lisp_Object Qheight;
133c764e 304extern Lisp_Object QCwidth, QCheight, QCascent;
f65536fa 305extern Lisp_Object Qscroll_bar;
5f5c8ee5 306
8f897821 307/* Non-nil means highlight trailing whitespace. */
5f5c8ee5 308
8f897821 309Lisp_Object Vshow_trailing_whitespace;
5f5c8ee5 310
f65536fa
KS
311/* Non-nil means show the text cursor in void text areas
312 i.e. in blank areas after eol and eob. This used to be
313 the default in 21.3. */
314
315Lisp_Object Vshow_text_cursor_in_void;
316
5f5c8ee5
GM
317/* Name of the face used to highlight trailing whitespace. */
318
319Lisp_Object Qtrailing_whitespace;
320
321/* The symbol `image' which is the car of the lists used to represent
322 images in Lisp. */
323
324Lisp_Object Qimage;
325
326/* Non-zero means print newline to stdout before next mini-buffer
327 message. */
a2889657
JB
328
329int noninteractive_need_newline;
330
5f5c8ee5 331/* Non-zero means print newline to message log before next message. */
f88eb0b6 332
3c6595e0 333static int message_log_need_newline;
f88eb0b6 334
b14bc55e
RS
335/* Three markers that message_dolog uses.
336 It could allocate them itself, but that causes trouble
337 in handling memory-full errors. */
338static Lisp_Object message_dolog_marker1;
339static Lisp_Object message_dolog_marker2;
340static Lisp_Object message_dolog_marker3;
5f5c8ee5
GM
341\f
342/* The buffer position of the first character appearing entirely or
343 partially on the line of the selected window which contains the
344 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
345 redisplay optimization in redisplay_internal. */
a2889657 346
5f5c8ee5 347static struct text_pos this_line_start_pos;
a2889657 348
5f5c8ee5
GM
349/* Number of characters past the end of the line above, including the
350 terminating newline. */
351
352static struct text_pos this_line_end_pos;
353
354/* The vertical positions and the height of this line. */
a2889657 355
a2889657 356static int this_line_vpos;
5f5c8ee5
GM
357static int this_line_y;
358static int this_line_pixel_height;
359
360/* X position at which this display line starts. Usually zero;
361 negative if first character is partially visible. */
362
363static int this_line_start_x;
a2889657 364
5f5c8ee5 365/* Buffer that this_line_.* variables are referring to. */
a2889657 366
a2889657
JB
367static struct buffer *this_line_buffer;
368
5f5c8ee5
GM
369/* Nonzero means truncate lines in all windows less wide than the
370 frame. */
a2889657 371
a2889657
JB
372int truncate_partial_width_windows;
373
7bbe686f 374/* A flag to control how to display unibyte 8-bit character. */
5f5c8ee5 375
7bbe686f 376int unibyte_display_via_language_environment;
2311178e 377
5f5c8ee5
GM
378/* Nonzero means we have more than one non-mini-buffer-only frame.
379 Not guaranteed to be accurate except while parsing
380 frame-title-format. */
7bbe686f 381
d39b6696
KH
382int multiple_frames;
383
a2889657
JB
384Lisp_Object Vglobal_mode_string;
385
386/* Marker for where to display an arrow on top of the buffer text. */
5f5c8ee5 387
a2889657
JB
388Lisp_Object Voverlay_arrow_position;
389
5f5c8ee5
GM
390/* String to display for the arrow. Only used on terminal frames. */
391
a2889657
JB
392Lisp_Object Voverlay_arrow_string;
393
5f5c8ee5
GM
394/* Values of those variables at last redisplay. However, if
395 Voverlay_arrow_position is a marker, last_arrow_position is its
396 numerical position. */
397
d45de95b
RS
398static Lisp_Object last_arrow_position, last_arrow_string;
399
5f5c8ee5
GM
400/* Like mode-line-format, but for the title bar on a visible frame. */
401
d39b6696
KH
402Lisp_Object Vframe_title_format;
403
5f5c8ee5
GM
404/* Like mode-line-format, but for the title bar on an iconified frame. */
405
d39b6696
KH
406Lisp_Object Vicon_title_format;
407
08b610e4
RS
408/* List of functions to call when a window's size changes. These
409 functions get one arg, a frame on which one or more windows' sizes
410 have changed. */
5f5c8ee5 411
08b610e4
RS
412static Lisp_Object Vwindow_size_change_functions;
413
0bca8940 414Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
cf074754 415
a2889657 416/* Nonzero if overlay arrow has been displayed once in this window. */
a2889657 417
5f5c8ee5 418static int overlay_arrow_seen;
ca26e1c8 419
fba9ce76 420/* Nonzero means highlight the region even in nonselected windows. */
fba9ce76 421
5f5c8ee5
GM
422int highlight_nonselected_windows;
423
424/* If cursor motion alone moves point off frame, try scrolling this
425 many lines up or down if that will bring it back. */
426
31ade731 427static EMACS_INT scroll_step;
a2889657 428
4b41cebb 429/* Nonzero means scroll just far enough to bring point back on the
5f5c8ee5
GM
430 screen, when appropriate. */
431
31ade731 432static EMACS_INT scroll_conservatively;
0789adb2 433
5f5c8ee5
GM
434/* Recenter the window whenever point gets within this many lines of
435 the top or bottom of the window. This value is translated into a
da8b7f4f
KS
436 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
437 that there is really a fixed pixel height scroll margin. */
5f5c8ee5 438
31ade731 439EMACS_INT scroll_margin;
9afd2168 440
5f5c8ee5
GM
441/* Number of windows showing the buffer of the selected window (or
442 another buffer with the same base buffer). keyboard.c refers to
443 this. */
a2889657 444
a2889657
JB
445int buffer_shared;
446
5f5c8ee5 447/* Vector containing glyphs for an ellipsis `...'. */
a2889657 448
5f5c8ee5 449static Lisp_Object default_invis_vector[3];
a2889657 450
1862a24e
MB
451/* Zero means display the mode-line/header-line/menu-bar in the default face
452 (this slightly odd definition is for compatibility with previous versions
453 of emacs), non-zero means display them using their respective faces.
454
455 This variable is deprecated. */
a2889657 456
a2889657
JB
457int mode_line_inverse_video;
458
5f5c8ee5
GM
459/* Prompt to display in front of the mini-buffer contents. */
460
8c5b6a0a 461Lisp_Object minibuf_prompt;
a2889657 462
5f5c8ee5
GM
463/* Width of current mini-buffer prompt. Only set after display_line
464 of the line that contains the prompt. */
465
a2889657 466int minibuf_prompt_width;
5f5c8ee5 467
5f5c8ee5
GM
468/* This is the window where the echo area message was displayed. It
469 is always a mini-buffer window, but it may not be the same window
470 currently active as a mini-buffer. */
471
73af359d
RS
472Lisp_Object echo_area_window;
473
c6e89d6c
GM
474/* List of pairs (MESSAGE . MULTIBYTE). The function save_message
475 pushes the current message and the value of
476 message_enable_multibyte on the stack, the function restore_message
477 pops the stack and displays MESSAGE again. */
478
479Lisp_Object Vmessage_stack;
480
a3788d53
RS
481/* Nonzero means multibyte characters were enabled when the echo area
482 message was specified. */
5f5c8ee5 483
a3788d53
RS
484int message_enable_multibyte;
485
4b41cebb 486/* Nonzero if we should redraw the mode lines on the next redisplay. */
5f5c8ee5 487
a2889657
JB
488int update_mode_lines;
489
5f5c8ee5 490/* Nonzero if window sizes or contents have changed since last
4b41cebb 491 redisplay that finished. */
5f5c8ee5 492
a2889657
JB
493int windows_or_buffers_changed;
494
5fb96e96
RS
495/* Nonzero means a frame's cursor type has been changed. */
496
497int cursor_type_changed;
498
5f5c8ee5
GM
499/* Nonzero after display_mode_line if %l was used and it displayed a
500 line number. */
501
aa6d10fa
RS
502int line_number_displayed;
503
504/* Maximum buffer size for which to display line numbers. */
5f5c8ee5 505
090703f4 506Lisp_Object Vline_number_display_limit;
5992c4f7 507
4b41cebb 508/* Line width to consider when repositioning for line number display. */
5d121aec 509
31ade731 510static EMACS_INT line_number_display_limit_width;
5d121aec 511
5f5c8ee5
GM
512/* Number of lines to keep in the message log buffer. t means
513 infinite. nil means don't log at all. */
514
5992c4f7 515Lisp_Object Vmessage_log_max;
d45de95b 516
6a94510a
GM
517/* The name of the *Messages* buffer, a string. */
518
519static Lisp_Object Vmessages_buffer_name;
520
c6e89d6c
GM
521/* Current, index 0, and last displayed echo area message. Either
522 buffers from echo_buffers, or nil to indicate no message. */
523
524Lisp_Object echo_area_buffer[2];
525
526/* The buffers referenced from echo_area_buffer. */
527
528static Lisp_Object echo_buffer[2];
529
530/* A vector saved used in with_area_buffer to reduce consing. */
531
532static Lisp_Object Vwith_echo_area_save_vector;
533
534/* Non-zero means display_echo_area should display the last echo area
535 message again. Set by redisplay_preserve_echo_area. */
536
537static int display_last_displayed_message_p;
538
539/* Nonzero if echo area is being used by print; zero if being used by
540 message. */
541
542int message_buf_print;
543
e1477f43
GM
544/* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
545
546Lisp_Object Qinhibit_menubar_update;
547int inhibit_menubar_update;
548
9142dd5b
GM
549/* Maximum height for resizing mini-windows. Either a float
550 specifying a fraction of the available height, or an integer
551 specifying a number of lines. */
c6e89d6c 552
ad4f174e
GM
553Lisp_Object Vmax_mini_window_height;
554
555/* Non-zero means messages should be displayed with truncated
556 lines instead of being continued. */
557
558int message_truncate_lines;
559Lisp_Object Qmessage_truncate_lines;
c6e89d6c 560
6e019995
GM
561/* Set to 1 in clear_message to make redisplay_internal aware
562 of an emptied echo area. */
563
564static int message_cleared_p;
565
d6d26ed3
GM
566/* Non-zero means we want a hollow cursor in windows that are not
567 selected. Zero means there's no cursor in such windows. */
568
cfe03a41 569Lisp_Object Vcursor_in_non_selected_windows;
af79bccb 570Lisp_Object Qcursor_in_non_selected_windows;
d6d26ed3 571
cfe03a41
KS
572/* How to blink the default frame cursor off. */
573Lisp_Object Vblink_cursor_alist;
574
5f5c8ee5
GM
575/* A scratch glyph row with contents used for generating truncation
576 glyphs. Also used in direct_output_for_insert. */
12adba34 577
5f5c8ee5
GM
578#define MAX_SCRATCH_GLYPHS 100
579struct glyph_row scratch_glyph_row;
580static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 581
5f5c8ee5
GM
582/* Ascent and height of the last line processed by move_it_to. */
583
584static int last_max_ascent, last_height;
585
21fdfb65
GM
586/* Non-zero if there's a help-echo in the echo area. */
587
588int help_echo_showing_p;
589
04612a64
GM
590/* If >= 0, computed, exact values of mode-line and header-line height
591 to use in the macros CURRENT_MODE_LINE_HEIGHT and
592 CURRENT_HEADER_LINE_HEIGHT. */
593
594int current_mode_line_height, current_header_line_height;
595
5f5c8ee5 596/* The maximum distance to look ahead for text properties. Values
2311178e 597 that are too small let us call compute_char_face and similar
5f5c8ee5
GM
598 functions too often which is expensive. Values that are too large
599 let us call compute_char_face and alike too often because we
600 might not be interested in text properties that far away. */
601
602#define TEXT_PROP_DISTANCE_LIMIT 100
603
47589c8c
GM
604#if GLYPH_DEBUG
605
76cb5e06
GM
606/* Variables to turn off display optimizations from Lisp. */
607
608int inhibit_try_window_id, inhibit_try_window_reusing;
609int inhibit_try_cursor_movement;
610
5f5c8ee5
GM
611/* Non-zero means print traces of redisplay if compiled with
612 GLYPH_DEBUG != 0. */
613
5f5c8ee5 614int trace_redisplay_p;
47589c8c 615
546a4f00 616#endif /* GLYPH_DEBUG */
47589c8c 617
546a4f00
AI
618#ifdef DEBUG_TRACE_MOVE
619/* Non-zero means trace with TRACE_MOVE to stderr. */
47589c8c
GM
620int trace_move;
621
47589c8c
GM
622#define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
623#else
91c3f500 624#define TRACE_MOVE(x) (void) 0
5f5c8ee5 625#endif
2311178e 626
d475bcb8
GM
627/* Non-zero means automatically scroll windows horizontally to make
628 point visible. */
629
630int automatic_hscrolling_p;
631
1df7e8f0
EZ
632/* How close to the margin can point get before the window is scrolled
633 horizontally. */
5d335845 634EMACS_INT hscroll_margin;
1df7e8f0
EZ
635
636/* How much to scroll horizontally when point is inside the above margin. */
e76d28d5 637Lisp_Object Vhscroll_step;
1df7e8f0 638
e00daaa0
GM
639/* A list of symbols, one for each supported image type. */
640
641Lisp_Object Vimage_types;
642
6422c1d7 643/* The variable `resize-mini-windows'. If nil, don't resize
67526daf 644 mini-windows. If t, always resize them to fit the text they
6422c1d7
GM
645 display. If `grow-only', let mini-windows grow only until they
646 become empty. */
647
648Lisp_Object Vresize_mini_windows;
649
82a7ab23
RS
650/* Buffer being redisplayed -- for redisplay_window_error. */
651
652struct buffer *displayed_buffer;
653
5f5c8ee5
GM
654/* Value returned from text property handlers (see below). */
655
656enum prop_handled
3c6595e0 657{
5f5c8ee5
GM
658 HANDLED_NORMALLY,
659 HANDLED_RECOMPUTE_PROPS,
660 HANDLED_OVERLAY_STRING_CONSUMED,
661 HANDLED_RETURN
662};
3c6595e0 663
5f5c8ee5
GM
664/* A description of text properties that redisplay is interested
665 in. */
3c6595e0 666
5f5c8ee5
GM
667struct props
668{
669 /* The name of the property. */
670 Lisp_Object *name;
90adcf20 671
5f5c8ee5
GM
672 /* A unique index for the property. */
673 enum prop_idx idx;
674
675 /* A handler function called to set up iterator IT from the property
676 at IT's current position. Value is used to steer handle_stop. */
677 enum prop_handled (*handler) P_ ((struct it *it));
678};
679
680static enum prop_handled handle_face_prop P_ ((struct it *));
681static enum prop_handled handle_invisible_prop P_ ((struct it *));
682static enum prop_handled handle_display_prop P_ ((struct it *));
260a86a0 683static enum prop_handled handle_composition_prop P_ ((struct it *));
5f5c8ee5
GM
684static enum prop_handled handle_overlay_change P_ ((struct it *));
685static enum prop_handled handle_fontified_prop P_ ((struct it *));
686
687/* Properties handled by iterators. */
688
689static struct props it_props[] =
5992c4f7 690{
5f5c8ee5
GM
691 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
692 /* Handle `face' before `display' because some sub-properties of
693 `display' need to know the face. */
694 {&Qface, FACE_PROP_IDX, handle_face_prop},
695 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
696 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
260a86a0 697 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
5f5c8ee5
GM
698 {NULL, 0, NULL}
699};
5992c4f7 700
5f5c8ee5
GM
701/* Value is the position described by X. If X is a marker, value is
702 the marker_position of X. Otherwise, value is X. */
12adba34 703
5f5c8ee5 704#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 705
5f5c8ee5 706/* Enumeration returned by some move_it_.* functions internally. */
12adba34 707
5f5c8ee5
GM
708enum move_it_result
709{
710 /* Not used. Undefined value. */
711 MOVE_UNDEFINED,
bab29e15 712
5f5c8ee5
GM
713 /* Move ended at the requested buffer position or ZV. */
714 MOVE_POS_MATCH_OR_ZV,
bab29e15 715
5f5c8ee5
GM
716 /* Move ended at the requested X pixel position. */
717 MOVE_X_REACHED,
12adba34 718
5f5c8ee5
GM
719 /* Move within a line ended at the end of a line that must be
720 continued. */
721 MOVE_LINE_CONTINUED,
2311178e 722
5f5c8ee5
GM
723 /* Move within a line ended at the end of a line that would
724 be displayed truncated. */
725 MOVE_LINE_TRUNCATED,
ff6c30e5 726
5f5c8ee5
GM
727 /* Move within a line ended at a line end. */
728 MOVE_NEWLINE_OR_CR
729};
12adba34 730
1987b083
RS
731/* This counter is used to clear the face cache every once in a while
732 in redisplay_internal. It is incremented for each redisplay.
733 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
734 cleared. */
735
736#define CLEAR_FACE_CACHE_COUNT 500
737static int clear_face_cache_count;
738
739/* Record the previous terminal frame we displayed. */
740
741static struct frame *previous_terminal_frame;
742
743/* Non-zero while redisplay_internal is in progress. */
744
745int redisplaying_p;
746
26683087
RS
747/* Non-zero means don't free realized faces. Bound while freeing
748 realized faces is dangerous because glyph matrices might still
749 reference them. */
1987b083 750
26683087
RS
751int inhibit_free_realized_faces;
752Lisp_Object Qinhibit_free_realized_faces;
ff6c30e5 753
fa3c6b4d
KS
754/* If a string, XTread_socket generates an event to display that string.
755 (The display is done in read_char.) */
756
757Lisp_Object help_echo_string;
758Lisp_Object help_echo_window;
759Lisp_Object help_echo_object;
760int help_echo_pos;
761
762/* Temporary variable for XTread_socket. */
763
764Lisp_Object previous_help_echo_string;
765
766
5f5c8ee5
GM
767\f
768/* Function prototypes. */
769
5a08cbaf 770static void setup_for_ellipsis P_ ((struct it *));
43c09969 771static void mark_window_display_accurate_1 P_ ((struct window *, int));
74bd6d65
GM
772static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
773static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
cafafe0b 774static int cursor_row_p P_ ((struct window *, struct glyph_row *));
715e84c9 775static int redisplay_mode_lines P_ ((Lisp_Object, int));
2e621225 776static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
c0a53abb
PJ
777
778#if 0
2e621225 779static int invisible_text_between_p P_ ((struct it *, int, int));
c0a53abb
PJ
780#endif
781
2e621225
GM
782static int next_element_from_ellipsis P_ ((struct it *));
783static void pint2str P_ ((char *, int, int));
525b8b09 784static void pint2hrstr P_ ((char *, int, int));
2e621225
GM
785static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
786 struct text_pos));
787static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
788static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
789static void store_frame_title_char P_ ((char));
50f80c2f 790static int store_frame_title P_ ((const unsigned char *, int, int));
2e621225
GM
791static void x_consider_frame_title P_ ((Lisp_Object));
792static void handle_stop P_ ((struct it *));
793static int tool_bar_lines_needed P_ ((struct frame *));
06568bbf 794static int single_display_prop_intangible_p P_ ((Lisp_Object));
5bcfeb49 795static void ensure_echo_area_buffers P_ ((void));
c6e89d6c
GM
796static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
797static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
23a96c77 798static int with_echo_area_buffer P_ ((struct window *, int,
23dd2d97
KR
799 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
800 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 801static void clear_garbaged_frames P_ ((void));
23dd2d97
KR
802static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
803static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
804static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 805static int display_echo_area P_ ((struct window *));
23dd2d97
KR
806static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
807static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
28514cd9 808static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
50f80c2f 809static int string_char_and_length P_ ((const unsigned char *, int, int *));
5f5c8ee5
GM
810static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
811 struct text_pos));
812static int compute_window_start_on_continuation_line P_ ((struct window *));
116d6f5c 813static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
5f5c8ee5
GM
814static void insert_left_trunc_glyphs P_ ((struct it *));
815static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
816static void extend_face_to_end_of_line P_ ((struct it *));
80c6cb1f 817static int append_space P_ ((struct it *, int));
cb617e7c 818static int make_cursor_line_fully_visible P_ ((struct window *));
03b0a4b4 819static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
47589c8c 820static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
5f5c8ee5
GM
821static int trailing_whitespace_p P_ ((int));
822static int message_log_check_duplicate P_ ((int, int, int, int));
5f5c8ee5
GM
823static void push_it P_ ((struct it *));
824static void pop_it P_ ((struct it *));
825static void sync_frame_with_window_matrix_rows P_ ((struct window *));
dd429b03 826static void select_frame_for_redisplay P_ ((Lisp_Object));
5f5c8ee5 827static void redisplay_internal P_ ((int));
c6e89d6c 828static int echo_area_display P_ ((int));
5f5c8ee5
GM
829static void redisplay_windows P_ ((Lisp_Object));
830static void redisplay_window P_ ((Lisp_Object, int));
82a7ab23
RS
831static Lisp_Object redisplay_window_error ();
832static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
833static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
5f5c8ee5
GM
834static void update_menu_bar P_ ((struct frame *, int));
835static int try_window_reusing_current_matrix P_ ((struct window *));
836static int try_window_id P_ ((struct window *));
837static int display_line P_ ((struct it *));
715e84c9 838static int display_mode_lines P_ ((struct window *));
04612a64 839static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
c53a1624 840static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
fec8f23e 841static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
72f62cb5 842static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
5f5c8ee5
GM
843static void display_menu_bar P_ ((struct window *));
844static int display_count_lines P_ ((int, int, int, int, int *));
845static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
846 int, int, struct it *, int, int, int, int));
847static void compute_line_metrics P_ ((struct it *));
848static void run_redisplay_end_trigger_hook P_ ((struct it *));
5a08cbaf 849static int get_overlay_strings P_ ((struct it *, int));
5f5c8ee5 850static void next_overlay_string P_ ((struct it *));
5f5c8ee5
GM
851static void reseat P_ ((struct it *, struct text_pos, int));
852static void reseat_1 P_ ((struct it *, struct text_pos, int));
853static void back_to_previous_visible_line_start P_ ((struct it *));
854static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 855static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
856static int next_element_from_display_vector P_ ((struct it *));
857static int next_element_from_string P_ ((struct it *));
858static int next_element_from_c_string P_ ((struct it *));
859static int next_element_from_buffer P_ ((struct it *));
260a86a0 860static int next_element_from_composition P_ ((struct it *));
5f5c8ee5
GM
861static int next_element_from_image P_ ((struct it *));
862static int next_element_from_stretch P_ ((struct it *));
5a08cbaf 863static void load_overlay_strings P_ ((struct it *, int));
47d57b22
GM
864static int init_from_display_pos P_ ((struct it *, struct window *,
865 struct display_pos *));
5f5c8ee5
GM
866static void reseat_to_string P_ ((struct it *, unsigned char *,
867 Lisp_Object, int, int, int, int));
5f5c8ee5
GM
868static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
869 int, int, int));
870void move_it_vertically_backward P_ ((struct it *, int));
871static void init_to_row_start P_ ((struct it *, struct window *,
872 struct glyph_row *));
47d57b22
GM
873static int init_to_row_end P_ ((struct it *, struct window *,
874 struct glyph_row *));
5f5c8ee5 875static void back_to_previous_line_start P_ ((struct it *));
cafafe0b 876static int forward_to_next_line_start P_ ((struct it *, int *));
5f5c8ee5
GM
877static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
878 Lisp_Object, int));
879static struct text_pos string_pos P_ ((int, Lisp_Object));
880static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
881static int number_of_chars P_ ((unsigned char *, int));
882static void compute_stop_pos P_ ((struct it *));
883static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
884 Lisp_Object));
885static int face_before_or_after_it_pos P_ ((struct it *, int));
886static int next_overlay_change P_ ((int));
887static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
a61b7058
GM
888 Lisp_Object, struct text_pos *,
889 int));
06a12811 890static int underlying_face_id P_ ((struct it *));
4bde0ebb
GM
891static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
892 struct window *));
5f5c8ee5
GM
893
894#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
895#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 896
5f5c8ee5 897#ifdef HAVE_WINDOW_SYSTEM
12adba34 898
e037b9ec
GM
899static void update_tool_bar P_ ((struct frame *, int));
900static void build_desired_tool_bar_string P_ ((struct frame *f));
901static int redisplay_tool_bar P_ ((struct frame *));
902static void display_tool_bar_line P_ ((struct it *));
fa3c6b4d
KS
903static void notice_overwritten_cursor P_ ((struct window *,
904 enum glyph_row_area,
905 int, int, int, int));
906
907
12adba34 908
5f5c8ee5 909#endif /* HAVE_WINDOW_SYSTEM */
12adba34 910
5f5c8ee5
GM
911\f
912/***********************************************************************
913 Window display dimensions
914 ***********************************************************************/
12adba34 915
40a301b3
RS
916/* Return the bottom boundary y-position for text lines in window W.
917 This is the first y position at which a line cannot start.
918 It is relative to the top of the window.
919
920 This is the height of W minus the height of a mode line, if any. */
5f5c8ee5
GM
921
922INLINE int
923window_text_bottom_y (w)
924 struct window *w;
925{
da8b7f4f 926 int height = WINDOW_TOTAL_HEIGHT (w);
1a578e9b 927
5f5c8ee5
GM
928 if (WINDOW_WANTS_MODELINE_P (w))
929 height -= CURRENT_MODE_LINE_HEIGHT (w);
930 return height;
f88eb0b6
KH
931}
932
5f5c8ee5 933/* Return the pixel width of display area AREA of window W. AREA < 0
b46952ae 934 means return the total width of W, not including fringes to
5f5c8ee5 935 the left and right of the window. */
ff6c30e5 936
5f5c8ee5
GM
937INLINE int
938window_box_width (w, area)
939 struct window *w;
940 int area;
941{
da8b7f4f
KS
942 int cols = XFASTINT (w->total_cols);
943 int pixels = 0;
2311178e 944
5f5c8ee5 945 if (!w->pseudo_window_p)
ff6c30e5 946 {
da8b7f4f 947 cols -= WINDOW_SCROLL_BAR_COLS (w);
2311178e 948
5f5c8ee5
GM
949 if (area == TEXT_AREA)
950 {
da8b7f4f
KS
951 if (INTEGERP (w->left_margin_cols))
952 cols -= XFASTINT (w->left_margin_cols);
953 if (INTEGERP (w->right_margin_cols))
954 cols -= XFASTINT (w->right_margin_cols);
955 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
5f5c8ee5
GM
956 }
957 else if (area == LEFT_MARGIN_AREA)
da8b7f4f
KS
958 {
959 cols = (INTEGERP (w->left_margin_cols)
960 ? XFASTINT (w->left_margin_cols) : 0);
961 pixels = 0;
962 }
5f5c8ee5 963 else if (area == RIGHT_MARGIN_AREA)
da8b7f4f
KS
964 {
965 cols = (INTEGERP (w->right_margin_cols)
966 ? XFASTINT (w->right_margin_cols) : 0);
967 pixels = 0;
968 }
ff6c30e5 969 }
5f5c8ee5 970
da8b7f4f 971 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
ff6c30e5 972}
1adc55de 973
1adc55de 974
5f5c8ee5 975/* Return the pixel height of the display area of window W, not
4b41cebb 976 including mode lines of W, if any. */
f88eb0b6 977
5f5c8ee5
GM
978INLINE int
979window_box_height (w)
980 struct window *w;
f88eb0b6 981{
5f5c8ee5 982 struct frame *f = XFRAME (w->frame);
da8b7f4f 983 int height = WINDOW_TOTAL_HEIGHT (w);
7ecd4937
GM
984
985 xassert (height >= 0);
2311178e 986
d9c9e99c
MB
987 /* Note: the code below that determines the mode-line/header-line
988 height is essentially the same as that contained in the macro
989 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
990 the appropriate glyph row has its `mode_line_p' flag set,
991 and if it doesn't, uses estimate_mode_line_height instead. */
992
5f5c8ee5 993 if (WINDOW_WANTS_MODELINE_P (w))
97dff879
MB
994 {
995 struct glyph_row *ml_row
996 = (w->current_matrix && w->current_matrix->rows
997 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
998 : 0);
999 if (ml_row && ml_row->mode_line_p)
1000 height -= ml_row->height;
1001 else
96d2320f 1002 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
97dff879 1003 }
5f5c8ee5 1004
045dee35 1005 if (WINDOW_WANTS_HEADER_LINE_P (w))
97dff879
MB
1006 {
1007 struct glyph_row *hl_row
1008 = (w->current_matrix && w->current_matrix->rows
1009 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1010 : 0);
1011 if (hl_row && hl_row->mode_line_p)
1012 height -= hl_row->height;
1013 else
1014 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1015 }
5f5c8ee5 1016
7c75be36
PJ
1017 /* With a very small font and a mode-line that's taller than
1018 default, we might end up with a negative height. */
1019 return max (0, height);
5992c4f7
KH
1020}
1021
da8b7f4f
KS
1022/* Return the window-relative coordinate of the left edge of display
1023 area AREA of window W. AREA < 0 means return the left edge of the
1024 whole window, to the right of the left fringe of W. */
1025
1026INLINE int
1027window_box_left_offset (w, area)
1028 struct window *w;
1029 int area;
1030{
1031 int x;
1032
1033 if (w->pseudo_window_p)
1034 return 0;
1035
1036 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1037
1038 if (area == TEXT_AREA)
1039 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1040 + window_box_width (w, LEFT_MARGIN_AREA));
1041 else if (area == RIGHT_MARGIN_AREA)
1042 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1043 + window_box_width (w, LEFT_MARGIN_AREA)
1044 + window_box_width (w, TEXT_AREA)
1045 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1046 ? 0
1047 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1048 else if (area == LEFT_MARGIN_AREA
1049 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1050 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1051
1052 return x;
1053}
1054
1055
1056/* Return the window-relative coordinate of the right edge of display
1057 area AREA of window W. AREA < 0 means return the left edge of the
1058 whole window, to the left of the right fringe of W. */
1059
1060INLINE int
1061window_box_right_offset (w, area)
1062 struct window *w;
1063 int area;
1064{
1065 return window_box_left_offset (w, area) + window_box_width (w, area);
1066}
5992c4f7 1067
5f5c8ee5
GM
1068/* Return the frame-relative coordinate of the left edge of display
1069 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 1070 whole window, to the right of the left fringe of W. */
5992c4f7 1071
5f5c8ee5
GM
1072INLINE int
1073window_box_left (w, area)
1074 struct window *w;
1075 int area;
90adcf20 1076{
5f5c8ee5 1077 struct frame *f = XFRAME (w->frame);
da8b7f4f 1078 int x;
a3788d53 1079
da8b7f4f
KS
1080 if (w->pseudo_window_p)
1081 return FRAME_INTERNAL_BORDER_WIDTH (f);
2311178e 1082
da8b7f4f
KS
1083 x = (WINDOW_LEFT_EDGE_X (w)
1084 + window_box_left_offset (w, area));
73af359d 1085
5f5c8ee5 1086 return x;
2311178e 1087}
90adcf20 1088
b6436d4e 1089
5f5c8ee5
GM
1090/* Return the frame-relative coordinate of the right edge of display
1091 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 1092 whole window, to the left of the right fringe of W. */
ded34426 1093
5f5c8ee5
GM
1094INLINE int
1095window_box_right (w, area)
1096 struct window *w;
1097 int area;
1098{
1099 return window_box_left (w, area) + window_box_width (w, area);
2311178e
TTN
1100}
1101
5f5c8ee5
GM
1102/* Get the bounding box of the display area AREA of window W, without
1103 mode lines, in frame-relative coordinates. AREA < 0 means the
b46952ae 1104 whole window, not including the left and right fringes of
5f5c8ee5
GM
1105 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1106 coordinates of the upper-left corner of the box. Return in
1107 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1108
1109INLINE void
1110window_box (w, area, box_x, box_y, box_width, box_height)
1111 struct window *w;
1112 int area;
1113 int *box_x, *box_y, *box_width, *box_height;
1114{
da8b7f4f
KS
1115 if (box_width)
1116 *box_width = window_box_width (w, area);
1117 if (box_height)
1118 *box_height = window_box_height (w);
1119 if (box_x)
1120 *box_x = window_box_left (w, area);
1121 if (box_y)
1122 {
1123 *box_y = WINDOW_TOP_EDGE_Y (w);
1124 if (WINDOW_WANTS_HEADER_LINE_P (w))
1125 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1126 }
ded34426 1127}
1adc55de 1128
1adc55de 1129
5f5c8ee5 1130/* Get the bounding box of the display area AREA of window W, without
b46952ae
KS
1131 mode lines. AREA < 0 means the whole window, not including the
1132 left and right fringe of the window. Return in *TOP_LEFT_X
5f5c8ee5
GM
1133 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1134 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1135 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1136 box. */
ded34426 1137
5f5c8ee5
GM
1138INLINE void
1139window_box_edges (w, area, top_left_x, top_left_y,
1140 bottom_right_x, bottom_right_y)
1141 struct window *w;
1142 int area;
1143 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 1144{
5f5c8ee5
GM
1145 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1146 bottom_right_y);
1147 *bottom_right_x += *top_left_x;
1148 *bottom_right_y += *top_left_y;
48ae5f0a
KH
1149}
1150
5f5c8ee5
GM
1151
1152\f
1153/***********************************************************************
1154 Utilities
1155 ***********************************************************************/
1156
8b6ea97f
GM
1157/* Return the bottom y-position of the line the iterator IT is in.
1158 This can modify IT's settings. */
1159
1160int
1161line_bottom_y (it)
1162 struct it *it;
1163{
1164 int line_height = it->max_ascent + it->max_descent;
1165 int line_top_y = it->current_y;
2311178e 1166
8b6ea97f
GM
1167 if (line_height == 0)
1168 {
1169 if (last_height)
1170 line_height = last_height;
1171 else if (IT_CHARPOS (*it) < ZV)
1172 {
1173 move_it_by_lines (it, 1, 1);
1174 line_height = (it->max_ascent || it->max_descent
1175 ? it->max_ascent + it->max_descent
1176 : last_height);
1177 }
1178 else
1179 {
1180 struct glyph_row *row = it->glyph_row;
2311178e 1181
8b6ea97f
GM
1182 /* Use the default character height. */
1183 it->glyph_row = NULL;
1184 it->what = IT_CHARACTER;
1185 it->c = ' ';
1186 it->len = 1;
1187 PRODUCE_GLYPHS (it);
1188 line_height = it->ascent + it->descent;
1189 it->glyph_row = row;
1190 }
1191 }
1192
1193 return line_top_y + line_height;
1194}
1195
1196
0db95684
GM
1197/* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1198 1 if POS is visible and the line containing POS is fully visible.
1199 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1200 and header-lines heights. */
3a641a69
GM
1201
1202int
04612a64 1203pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
3a641a69 1204 struct window *w;
04612a64 1205 int charpos, *fully, exact_mode_line_heights_p;
3a641a69
GM
1206{
1207 struct it it;
1208 struct text_pos top;
fcab1954
GM
1209 int visible_p;
1210 struct buffer *old_buffer = NULL;
1211
1212 if (XBUFFER (w->buffer) != current_buffer)
1213 {
1214 old_buffer = current_buffer;
1215 set_buffer_internal_1 (XBUFFER (w->buffer));
1216 }
3a641a69
GM
1217
1218 *fully = visible_p = 0;
1219 SET_TEXT_POS_FROM_MARKER (top, w->start);
2311178e 1220
04612a64
GM
1221 /* Compute exact mode line heights, if requested. */
1222 if (exact_mode_line_heights_p)
1223 {
1224 if (WINDOW_WANTS_MODELINE_P (w))
1225 current_mode_line_height
96d2320f 1226 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
04612a64 1227 current_buffer->mode_line_format);
2311178e 1228
04612a64
GM
1229 if (WINDOW_WANTS_HEADER_LINE_P (w))
1230 current_header_line_height
1231 = display_mode_line (w, HEADER_LINE_FACE_ID,
1232 current_buffer->header_line_format);
1233 }
3a641a69 1234
04612a64 1235 start_display (&it, w, top);
3a641a69
GM
1236 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1237 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
a13be207
GM
1238
1239 /* Note that we may overshoot because of invisible text. */
1240 if (IT_CHARPOS (it) >= charpos)
3a641a69 1241 {
8b6ea97f
GM
1242 int top_y = it.current_y;
1243 int bottom_y = line_bottom_y (&it);
da8b7f4f 1244 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
2311178e 1245
8b6ea97f
GM
1246 if (top_y < window_top_y)
1247 visible_p = bottom_y > window_top_y;
1248 else if (top_y < it.last_visible_y)
fcab1954
GM
1249 {
1250 visible_p = 1;
8b6ea97f 1251 *fully = bottom_y <= it.last_visible_y;
fcab1954 1252 }
3a641a69
GM
1253 }
1254 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1255 {
1256 move_it_by_lines (&it, 1, 0);
1257 if (charpos < IT_CHARPOS (it))
1258 {
1259 visible_p = 1;
1260 *fully = 0;
1261 }
1262 }
fcab1954
GM
1263
1264 if (old_buffer)
1265 set_buffer_internal_1 (old_buffer);
04612a64
GM
1266
1267 current_header_line_height = current_mode_line_height = -1;
3a641a69
GM
1268 return visible_p;
1269}
1270
1271
4fdb80f2
GM
1272/* Return the next character from STR which is MAXLEN bytes long.
1273 Return in *LEN the length of the character. This is like
1274 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
9ab8560d 1275 we find one, we return a `?', but with the length of the invalid
4fdb80f2
GM
1276 character. */
1277
1278static INLINE int
7a5b8a93 1279string_char_and_length (str, maxlen, len)
50f80c2f 1280 const unsigned char *str;
7a5b8a93 1281 int maxlen, *len;
4fdb80f2
GM
1282{
1283 int c;
1284
1285 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1286 if (!CHAR_VALID_P (c, 1))
1287 /* We may not change the length here because other places in Emacs
9ab8560d 1288 don't use this function, i.e. they silently accept invalid
4fdb80f2
GM
1289 characters. */
1290 c = '?';
1291
1292 return c;
1293}
1294
1295
1296
5f5c8ee5
GM
1297/* Given a position POS containing a valid character and byte position
1298 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1299
1300static struct text_pos
1301string_pos_nchars_ahead (pos, string, nchars)
1302 struct text_pos pos;
1303 Lisp_Object string;
1304 int nchars;
0b1005ef 1305{
5f5c8ee5
GM
1306 xassert (STRINGP (string) && nchars >= 0);
1307
1308 if (STRING_MULTIBYTE (string))
1309 {
2051c264 1310 int rest = SBYTES (string) - BYTEPOS (pos);
50f80c2f 1311 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
5f5c8ee5
GM
1312 int len;
1313
1314 while (nchars--)
1315 {
4fdb80f2 1316 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
1317 p += len, rest -= len;
1318 xassert (rest >= 0);
1319 CHARPOS (pos) += 1;
1320 BYTEPOS (pos) += len;
1321 }
1322 }
1323 else
1324 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1325
1326 return pos;
0a9dc68b
RS
1327}
1328
0a9dc68b 1329
5f5c8ee5
GM
1330/* Value is the text position, i.e. character and byte position,
1331 for character position CHARPOS in STRING. */
1332
1333static INLINE struct text_pos
1334string_pos (charpos, string)
1335 int charpos;
0a9dc68b 1336 Lisp_Object string;
0a9dc68b 1337{
5f5c8ee5
GM
1338 struct text_pos pos;
1339 xassert (STRINGP (string));
1340 xassert (charpos >= 0);
1341 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1342 return pos;
1343}
1344
1345
1346/* Value is a text position, i.e. character and byte position, for
1347 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1348 means recognize multibyte characters. */
1349
1350static struct text_pos
1351c_string_pos (charpos, s, multibyte_p)
1352 int charpos;
1353 unsigned char *s;
1354 int multibyte_p;
1355{
1356 struct text_pos pos;
1357
1358 xassert (s != NULL);
1359 xassert (charpos >= 0);
1360
1361 if (multibyte_p)
0a9dc68b 1362 {
5f5c8ee5
GM
1363 int rest = strlen (s), len;
1364
1365 SET_TEXT_POS (pos, 0, 0);
1366 while (charpos--)
0a9dc68b 1367 {
4fdb80f2 1368 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
1369 s += len, rest -= len;
1370 xassert (rest >= 0);
1371 CHARPOS (pos) += 1;
1372 BYTEPOS (pos) += len;
0a9dc68b
RS
1373 }
1374 }
5f5c8ee5
GM
1375 else
1376 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 1377
5f5c8ee5
GM
1378 return pos;
1379}
0a9dc68b 1380
0a9dc68b 1381
5f5c8ee5
GM
1382/* Value is the number of characters in C string S. MULTIBYTE_P
1383 non-zero means recognize multibyte characters. */
0a9dc68b 1384
5f5c8ee5
GM
1385static int
1386number_of_chars (s, multibyte_p)
1387 unsigned char *s;
1388 int multibyte_p;
1389{
1390 int nchars;
2311178e 1391
5f5c8ee5
GM
1392 if (multibyte_p)
1393 {
1394 int rest = strlen (s), len;
1395 unsigned char *p = (unsigned char *) s;
0a9dc68b 1396
5f5c8ee5
GM
1397 for (nchars = 0; rest > 0; ++nchars)
1398 {
4fdb80f2 1399 string_char_and_length (p, rest, &len);
5f5c8ee5 1400 rest -= len, p += len;
0a9dc68b
RS
1401 }
1402 }
5f5c8ee5
GM
1403 else
1404 nchars = strlen (s);
1405
1406 return nchars;
0b1005ef
KH
1407}
1408
2311178e 1409
5f5c8ee5
GM
1410/* Compute byte position NEWPOS->bytepos corresponding to
1411 NEWPOS->charpos. POS is a known position in string STRING.
1412 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1413
5f5c8ee5
GM
1414static void
1415compute_string_pos (newpos, pos, string)
1416 struct text_pos *newpos, pos;
1417 Lisp_Object string;
76412d64 1418{
5f5c8ee5
GM
1419 xassert (STRINGP (string));
1420 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
2311178e 1421
5f5c8ee5 1422 if (STRING_MULTIBYTE (string))
6fc556fd
KR
1423 *newpos = string_pos_nchars_ahead (pos, string,
1424 CHARPOS (*newpos) - CHARPOS (pos));
5f5c8ee5
GM
1425 else
1426 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1427}
1428
fa3c6b4d
KS
1429/* EXPORT:
1430 Return an estimation of the pixel height of mode or top lines on
1431 frame F. FACE_ID specifies what line's height to estimate. */
1432
1433int
1434estimate_mode_line_height (f, face_id)
1435 struct frame *f;
1436 enum face_id face_id;
1437{
1438#ifdef HAVE_WINDOW_SYSTEM
1439 if (FRAME_WINDOW_P (f))
1440 {
1441 int height = FONT_HEIGHT (FRAME_FONT (f));
1442
1443 /* This function is called so early when Emacs starts that the face
1444 cache and mode line face are not yet initialized. */
1445 if (FRAME_FACE_CACHE (f))
1446 {
1447 struct face *face = FACE_FROM_ID (f, face_id);
1448 if (face)
1449 {
1450 if (face->font)
1451 height = FONT_HEIGHT (face->font);
1452 if (face->box_line_width > 0)
1453 height += 2 * face->box_line_width;
1454 }
1455 }
1456
1457 return height;
1458 }
1459#endif
1460
1461 return 1;
1462}
1463
e080d3eb
KS
1464/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1465 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1466 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1467 not force the value into range. */
1468
1469void
1470pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1471 FRAME_PTR f;
1472 register int pix_x, pix_y;
1473 int *x, *y;
1474 NativeRectangle *bounds;
1475 int noclip;
1476{
1477
1478#ifdef HAVE_WINDOW_SYSTEM
1479 if (FRAME_WINDOW_P (f))
1480 {
da8b7f4f 1481 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
e080d3eb
KS
1482 even for negative values. */
1483 if (pix_x < 0)
da8b7f4f 1484 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
e080d3eb 1485 if (pix_y < 0)
da8b7f4f 1486 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
e080d3eb 1487
da8b7f4f
KS
1488 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1489 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
e080d3eb
KS
1490
1491 if (bounds)
1492 STORE_NATIVE_RECT (*bounds,
da8b7f4f
KS
1493 FRAME_COL_TO_PIXEL_X (f, pix_x),
1494 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1495 FRAME_COLUMN_WIDTH (f) - 1,
1496 FRAME_LINE_HEIGHT (f) - 1);
e080d3eb
KS
1497
1498 if (!noclip)
1499 {
1500 if (pix_x < 0)
1501 pix_x = 0;
da8b7f4f
KS
1502 else if (pix_x > FRAME_TOTAL_COLS (f))
1503 pix_x = FRAME_TOTAL_COLS (f);
e080d3eb
KS
1504
1505 if (pix_y < 0)
1506 pix_y = 0;
da8b7f4f
KS
1507 else if (pix_y > FRAME_LINES (f))
1508 pix_y = FRAME_LINES (f);
e080d3eb
KS
1509 }
1510 }
1511#endif
1512
1513 *x = pix_x;
1514 *y = pix_y;
1515}
1516
1517
1518/* Given HPOS/VPOS in the current matrix of W, return corresponding
1519 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1520 can't tell the positions because W's display is not up to date,
1521 return 0. */
1522
1523int
1524glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1525 struct window *w;
1526 int hpos, vpos;
1527 int *frame_x, *frame_y;
1528{
1529#ifdef HAVE_WINDOW_SYSTEM
1530 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1531 {
1532 int success_p;
1533
1534 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1535 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1536
1537 if (display_completed)
1538 {
1539 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1540 struct glyph *glyph = row->glyphs[TEXT_AREA];
1541 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1542
1543 hpos = row->x;
1544 vpos = row->y;
1545 while (glyph < end)
1546 {
1547 hpos += glyph->pixel_width;
1548 ++glyph;
1549 }
1550
f65536fa
KS
1551 /* If first glyph is partially visible, its first visible position is still 0. */
1552 if (hpos < 0)
1553 hpos = 0;
1554
e080d3eb
KS
1555 success_p = 1;
1556 }
1557 else
1558 {
1559 hpos = vpos = 0;
1560 success_p = 0;
1561 }
1562
1563 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1564 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1565 return success_p;
1566 }
1567#endif
1568
1569 *frame_x = hpos;
1570 *frame_y = vpos;
1571 return 1;
1572}
1573
1574
fa3c6b4d
KS
1575#ifdef HAVE_WINDOW_SYSTEM
1576
1577/* Find the glyph under window-relative coordinates X/Y in window W.
1578 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1579 strings. Return in *HPOS and *VPOS the row and column number of
1580 the glyph found. Return in *AREA the glyph area containing X.
1581 Value is a pointer to the glyph found or null if X/Y is not on
1582 text, or we can't tell because W's current matrix is not up to
1583 date. */
1584
1585static struct glyph *
1586x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
1587 struct window *w;
1588 int x, y;
1589 int *hpos, *vpos, *area;
1590 int buffer_only_p;
1591{
1592 struct glyph *glyph, *end;
1593 struct glyph_row *row = NULL;
da8b7f4f 1594 int x0, i;
fa3c6b4d
KS
1595
1596 /* Find row containing Y. Give up if some row is not enabled. */
1597 for (i = 0; i < w->current_matrix->nrows; ++i)
1598 {
1599 row = MATRIX_ROW (w->current_matrix, i);
1600 if (!row->enabled_p)
1601 return NULL;
1602 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1603 break;
1604 }
1605
1606 *vpos = i;
1607 *hpos = 0;
1608
1609 /* Give up if Y is not in the window. */
1610 if (i == w->current_matrix->nrows)
1611 return NULL;
1612
1613 /* Get the glyph area containing X. */
1614 if (w->pseudo_window_p)
1615 {
1616 *area = TEXT_AREA;
1617 x0 = 0;
1618 }
1619 else
1620 {
da8b7f4f 1621 if (x < window_box_left_offset (w, TEXT_AREA))
fa3c6b4d
KS
1622 {
1623 *area = LEFT_MARGIN_AREA;
da8b7f4f 1624 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
fa3c6b4d 1625 }
da8b7f4f 1626 else if (x < window_box_right_offset (w, TEXT_AREA))
fa3c6b4d
KS
1627 {
1628 *area = TEXT_AREA;
f65536fa 1629 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
fa3c6b4d
KS
1630 }
1631 else
1632 {
1633 *area = RIGHT_MARGIN_AREA;
da8b7f4f 1634 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
fa3c6b4d
KS
1635 }
1636 }
1637
1638 /* Find glyph containing X. */
1639 glyph = row->glyphs[*area];
1640 end = glyph + row->used[*area];
1641 while (glyph < end)
1642 {
1643 if (x < x0 + glyph->pixel_width)
1644 {
1645 if (w->pseudo_window_p)
1646 break;
1647 else if (!buffer_only_p || BUFFERP (glyph->object))
1648 break;
1649 }
1650
1651 x0 += glyph->pixel_width;
1652 ++glyph;
1653 }
1654
1655 if (glyph == end)
1656 return NULL;
1657
1658 *hpos = glyph - row->glyphs[*area];
1659 return glyph;
1660}
1661
1662
1663/* EXPORT:
1664 Convert frame-relative x/y to coordinates relative to window W.
1665 Takes pseudo-windows into account. */
1666
1667void
1668frame_to_window_pixel_xy (w, x, y)
1669 struct window *w;
1670 int *x, *y;
1671{
1672 if (w->pseudo_window_p)
1673 {
1674 /* A pseudo-window is always full-width, and starts at the
1675 left edge of the frame, plus a frame border. */
1676 struct frame *f = XFRAME (w->frame);
da8b7f4f 1677 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
fa3c6b4d
KS
1678 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1679 }
1680 else
1681 {
da8b7f4f 1682 *x -= WINDOW_LEFT_EDGE_X (w);
fa3c6b4d
KS
1683 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1684 }
1685}
1686
1687/* EXPORT:
1688 Return in *R the clipping rectangle for glyph string S. */
1689
1690void
1691get_glyph_string_clip_rect (s, nr)
1692 struct glyph_string *s;
1693 NativeRectangle *nr;
1694{
1695 XRectangle r;
1696
1697 if (s->row->full_width_p)
1698 {
da8b7f4f
KS
1699 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1700 r.x = WINDOW_LEFT_EDGE_X (s->w);
1701 r.width = WINDOW_TOTAL_WIDTH (s->w);
fa3c6b4d
KS
1702
1703 /* Unless displaying a mode or menu bar line, which are always
1704 fully visible, clip to the visible part of the row. */
1705 if (s->w->pseudo_window_p)
1706 r.height = s->row->visible_height;
1707 else
1708 r.height = s->height;
1709 }
1710 else
1711 {
1712 /* This is a text line that may be partially visible. */
da8b7f4f 1713 r.x = window_box_left (s->w, s->area);
fa3c6b4d
KS
1714 r.width = window_box_width (s->w, s->area);
1715 r.height = s->row->visible_height;
1716 }
1717
1718 /* If S draws overlapping rows, it's sufficient to use the top and
1719 bottom of the window for clipping because this glyph string
1720 intentionally draws over other lines. */
1721 if (s->for_overlaps_p)
1722 {
da8b7f4f 1723 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
fa3c6b4d
KS
1724 r.height = window_text_bottom_y (s->w) - r.y;
1725 }
1726 else
1727 {
1728 /* Don't use S->y for clipping because it doesn't take partially
1729 visible lines into account. For example, it can be negative for
1730 partially visible lines at the top of a window. */
1731 if (!s->row->full_width_p
1732 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
da8b7f4f 1733 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
fa3c6b4d
KS
1734 else
1735 r.y = max (0, s->row->y);
1736
1737 /* If drawing a tool-bar window, draw it over the internal border
1738 at the top of the window. */
1739 if (s->w == XWINDOW (s->f->tool_bar_window))
b4ebbb12 1740 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
fa3c6b4d
KS
1741 }
1742
1743 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1744
1745#ifdef HAVE_NTGUI
1746 /* ++KFS: From W32 port, but it looks ok for all platforms to me. */
1747 /* If drawing the cursor, don't let glyph draw outside its
1748 advertised boundaries. Cleartype does this under some circumstances. */
1749 if (s->hl == DRAW_CURSOR)
1750 {
1751 if (s->x > r.x)
1752 {
1753 r.width -= s->x - r.x;
1754 r.x = s->x;
1755 }
1756 r.width = min (r.width, s->first_glyph->pixel_width);
1757 }
1758#endif
1759
1760#ifdef CONVERT_FROM_XRECT
1761 CONVERT_FROM_XRECT (r, *nr);
1762#else
1763 *nr = r;
fd4c9408 1764#endif
fa3c6b4d
KS
1765}
1766
1767#endif /* HAVE_WINDOW_SYSTEM */
9c74a0dd 1768
5f5c8ee5
GM
1769\f
1770/***********************************************************************
1771 Lisp form evaluation
1772 ***********************************************************************/
1773
116d6f5c 1774/* Error handler for safe_eval and safe_call. */
5f5c8ee5
GM
1775
1776static Lisp_Object
116d6f5c 1777safe_eval_handler (arg)
5f5c8ee5
GM
1778 Lisp_Object arg;
1779{
0dbf9fd2 1780 add_to_log ("Error during redisplay: %s", arg, Qnil);
5f5c8ee5
GM
1781 return Qnil;
1782}
1783
1784
1785/* Evaluate SEXPR and return the result, or nil if something went
2913a9c0 1786 wrong. Prevent redisplay during the evaluation. */
5f5c8ee5 1787
71e5b1b8 1788Lisp_Object
116d6f5c 1789safe_eval (sexpr)
5f5c8ee5
GM
1790 Lisp_Object sexpr;
1791{
5f5c8ee5 1792 Lisp_Object val;
2311178e 1793
30a3f61c
GM
1794 if (inhibit_eval_during_redisplay)
1795 val = Qnil;
1796 else
1797 {
331379bf 1798 int count = SPECPDL_INDEX ();
30a3f61c 1799 struct gcpro gcpro1;
0d8b31c0 1800
30a3f61c
GM
1801 GCPRO1 (sexpr);
1802 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
1803 /* Use Qt to ensure debugger does not run,
1804 so there is no possibility of wanting to redisplay. */
1805 val = internal_condition_case_1 (Feval, sexpr, Qt,
30a3f61c
GM
1806 safe_eval_handler);
1807 UNGCPRO;
1808 val = unbind_to (count, val);
1809 }
2311178e 1810
30a3f61c 1811 return val;
0d8b31c0
GM
1812}
1813
1814
1815/* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2913a9c0
GM
1816 Return the result, or nil if something went wrong. Prevent
1817 redisplay during the evaluation. */
0d8b31c0
GM
1818
1819Lisp_Object
116d6f5c 1820safe_call (nargs, args)
0d8b31c0
GM
1821 int nargs;
1822 Lisp_Object *args;
1823{
0d8b31c0 1824 Lisp_Object val;
2311178e 1825
30a3f61c
GM
1826 if (inhibit_eval_during_redisplay)
1827 val = Qnil;
1828 else
1829 {
331379bf 1830 int count = SPECPDL_INDEX ();
30a3f61c 1831 struct gcpro gcpro1;
0d8b31c0 1832
30a3f61c
GM
1833 GCPRO1 (args[0]);
1834 gcpro1.nvars = nargs;
1835 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
1836 /* Use Qt to ensure debugger does not run,
1837 so there is no possibility of wanting to redisplay. */
1838 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
30a3f61c
GM
1839 safe_eval_handler);
1840 UNGCPRO;
1841 val = unbind_to (count, val);
1842 }
1843
1844 return val;
5f5c8ee5
GM
1845}
1846
1847
116d6f5c
GM
1848/* Call function FN with one argument ARG.
1849 Return the result, or nil if something went wrong. */
1850
1851Lisp_Object
1852safe_call1 (fn, arg)
1853 Lisp_Object fn, arg;
1854{
1855 Lisp_Object args[2];
1856 args[0] = fn;
1857 args[1] = arg;
1858 return safe_call (2, args);
1859}
1860
1861
5f5c8ee5
GM
1862\f
1863/***********************************************************************
1864 Debugging
1865 ***********************************************************************/
1866
1867#if 0
1868
1869/* Define CHECK_IT to perform sanity checks on iterators.
1870 This is for debugging. It is too slow to do unconditionally. */
1871
1872static void
1873check_it (it)
1874 struct it *it;
1875{
1876 if (it->method == next_element_from_string)
a2889657 1877 {
5f5c8ee5
GM
1878 xassert (STRINGP (it->string));
1879 xassert (IT_STRING_CHARPOS (*it) >= 0);
1880 }
1881 else if (it->method == next_element_from_buffer)
1882 {
1883 /* Check that character and byte positions agree. */
1884 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1885 }
73af359d 1886
5f5c8ee5
GM
1887 if (it->dpvec)
1888 xassert (it->current.dpvec_index >= 0);
1889 else
1890 xassert (it->current.dpvec_index < 0);
1891}
1f40cad2 1892
5f5c8ee5
GM
1893#define CHECK_IT(IT) check_it ((IT))
1894
1895#else /* not 0 */
1896
1897#define CHECK_IT(IT) (void) 0
1898
1899#endif /* not 0 */
1900
1901
1902#if GLYPH_DEBUG
1903
1904/* Check that the window end of window W is what we expect it
1905 to be---the last row in the current matrix displaying text. */
1906
1907static void
1908check_window_end (w)
1909 struct window *w;
1910{
1911 if (!MINI_WINDOW_P (w)
1912 && !NILP (w->window_end_valid))
1913 {
1914 struct glyph_row *row;
1915 xassert ((row = MATRIX_ROW (w->current_matrix,
1916 XFASTINT (w->window_end_vpos)),
1917 !row->enabled_p
1918 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1919 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1920 }
1921}
1922
1923#define CHECK_WINDOW_END(W) check_window_end ((W))
1924
1925#else /* not GLYPH_DEBUG */
1926
1927#define CHECK_WINDOW_END(W) (void) 0
1928
1929#endif /* not GLYPH_DEBUG */
1930
1931
1932\f
1933/***********************************************************************
1934 Iterator initialization
1935 ***********************************************************************/
1936
1937/* Initialize IT for displaying current_buffer in window W, starting
1938 at character position CHARPOS. CHARPOS < 0 means that no buffer
1939 position is specified which is useful when the iterator is assigned
1940 a position later. BYTEPOS is the byte position corresponding to
3ebf0ea9 1941 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
5f5c8ee5
GM
1942
1943 If ROW is not null, calls to produce_glyphs with IT as parameter
1944 will produce glyphs in that row.
1945
1946 BASE_FACE_ID is the id of a base face to use. It must be one of
96d2320f
KS
1947 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1948 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1949 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2311178e 1950
96d2320f
KS
1951 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1952 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1953 will be initialized to use the corresponding mode line glyph row of
1954 the desired matrix of W. */
5f5c8ee5
GM
1955
1956void
1957init_iterator (it, w, charpos, bytepos, row, base_face_id)
1958 struct it *it;
1959 struct window *w;
1960 int charpos, bytepos;
1961 struct glyph_row *row;
1962 enum face_id base_face_id;
1963{
1964 int highlight_region_p;
5f5c8ee5
GM
1965
1966 /* Some precondition checks. */
1967 xassert (w != NULL && it != NULL);
3b6b6db7
SM
1968 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1969 && charpos <= ZV));
5f5c8ee5
GM
1970
1971 /* If face attributes have been changed since the last redisplay,
1972 free realized faces now because they depend on face definitions
7d0393cf 1973 that might have changed. Don't free faces while there might be
1987b083 1974 desired matrices pending which reference these faces. */
26683087 1975 if (face_change_count && !inhibit_free_realized_faces)
5f5c8ee5
GM
1976 {
1977 face_change_count = 0;
1978 free_all_realized_faces (Qnil);
1979 }
1980
1981 /* Use one of the mode line rows of W's desired matrix if
1982 appropriate. */
1983 if (row == NULL)
1984 {
96d2320f
KS
1985 if (base_face_id == MODE_LINE_FACE_ID
1986 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
5f5c8ee5 1987 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
1988 else if (base_face_id == HEADER_LINE_FACE_ID)
1989 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5 1990 }
2311178e 1991
5f5c8ee5
GM
1992 /* Clear IT. */
1993 bzero (it, sizeof *it);
1994 it->current.overlay_string_index = -1;
1995 it->current.dpvec_index = -1;
5f5c8ee5
GM
1996 it->base_face_id = base_face_id;
1997
1998 /* The window in which we iterate over current_buffer: */
1999 XSETWINDOW (it->window, w);
2000 it->w = w;
2001 it->f = XFRAME (w->frame);
2002
d475bcb8
GM
2003 /* Extra space between lines (on window systems only). */
2004 if (base_face_id == DEFAULT_FACE_ID
2005 && FRAME_WINDOW_P (it->f))
2006 {
2007 if (NATNUMP (current_buffer->extra_line_spacing))
2008 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2009 else if (it->f->extra_line_spacing > 0)
2010 it->extra_line_spacing = it->f->extra_line_spacing;
2011 }
2012
5f5c8ee5 2013 /* If realized faces have been removed, e.g. because of face
62be9979
GM
2014 attribute changes of named faces, recompute them. When running
2015 in batch mode, the face cache of Vterminal_frame is null. If
2016 we happen to get called, make a dummy face cache. */
a24d4cb2 2017 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
62be9979 2018 init_frame_faces (it->f);
5f5c8ee5
GM
2019 if (FRAME_FACE_CACHE (it->f)->used == 0)
2020 recompute_basic_faces (it->f);
2021
5f5c8ee5
GM
2022 /* Current value of the `space-width', and 'height' properties. */
2023 it->space_width = Qnil;
2024 it->font_height = Qnil;
2311178e 2025
5f5c8ee5
GM
2026 /* Are control characters displayed as `^C'? */
2027 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2028
2029 /* -1 means everything between a CR and the following line end
2030 is invisible. >0 means lines indented more than this value are
2031 invisible. */
2032 it->selective = (INTEGERP (current_buffer->selective_display)
2033 ? XFASTINT (current_buffer->selective_display)
2311178e 2034 : (!NILP (current_buffer->selective_display)
5f5c8ee5
GM
2035 ? -1 : 0));
2036 it->selective_display_ellipsis_p
2037 = !NILP (current_buffer->selective_display_ellipses);
2038
2039 /* Display table to use. */
2040 it->dp = window_display_table (w);
2041
2042 /* Are multibyte characters enabled in current_buffer? */
2043 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2044
2045 /* Non-zero if we should highlight the region. */
2046 highlight_region_p
3ebf0ea9 2047 = (!NILP (Vtransient_mark_mode)
5f5c8ee5
GM
2048 && !NILP (current_buffer->mark_active)
2049 && XMARKER (current_buffer->mark)->buffer != 0);
2050
2051 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2052 start and end of a visible region in window IT->w. Set both to
2053 -1 to indicate no region. */
2054 if (highlight_region_p
2055 /* Maybe highlight only in selected window. */
2311178e 2056 && (/* Either show region everywhere. */
5f5c8ee5
GM
2057 highlight_nonselected_windows
2058 /* Or show region in the selected window. */
2059 || w == XWINDOW (selected_window)
2060 /* Or show the region if we are in the mini-buffer and W is
2061 the window the mini-buffer refers to. */
2062 || (MINI_WINDOW_P (XWINDOW (selected_window))
5705966b
KS
2063 && WINDOWP (minibuf_selected_window)
2064 && w == XWINDOW (minibuf_selected_window))))
5f5c8ee5
GM
2065 {
2066 int charpos = marker_position (current_buffer->mark);
2067 it->region_beg_charpos = min (PT, charpos);
2068 it->region_end_charpos = max (PT, charpos);
2069 }
2070 else
2071 it->region_beg_charpos = it->region_end_charpos = -1;
2072
2073 /* Get the position at which the redisplay_end_trigger hook should
2074 be run, if it is to be run at all. */
2075 if (MARKERP (w->redisplay_end_trigger)
2076 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2077 it->redisplay_end_trigger_charpos
2078 = marker_position (w->redisplay_end_trigger);
2079 else if (INTEGERP (w->redisplay_end_trigger))
2080 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2081
2082 /* Correct bogus values of tab_width. */
2083 it->tab_width = XINT (current_buffer->tab_width);
2084 if (it->tab_width <= 0 || it->tab_width > 1000)
2085 it->tab_width = 8;
2086
2087 /* Are lines in the display truncated? */
2088 it->truncate_lines_p
2089 = (base_face_id != DEFAULT_FACE_ID
2090 || XINT (it->w->hscroll)
2091 || (truncate_partial_width_windows
2092 && !WINDOW_FULL_WIDTH_P (it->w))
2093 || !NILP (current_buffer->truncate_lines));
2094
2095 /* Get dimensions of truncation and continuation glyphs. These are
b46952ae 2096 displayed as fringe bitmaps under X, so we don't need them for such
5f5c8ee5
GM
2097 frames. */
2098 if (!FRAME_WINDOW_P (it->f))
2099 {
2100 if (it->truncate_lines_p)
2101 {
2102 /* We will need the truncation glyph. */
2103 xassert (it->glyph_row == NULL);
2104 produce_special_glyphs (it, IT_TRUNCATION);
2105 it->truncation_pixel_width = it->pixel_width;
2106 }
2107 else
2108 {
2109 /* We will need the continuation glyph. */
2110 xassert (it->glyph_row == NULL);
2111 produce_special_glyphs (it, IT_CONTINUATION);
2112 it->continuation_pixel_width = it->pixel_width;
2113 }
2114
153c2160 2115 /* Reset these values to zero because the produce_special_glyphs
5f5c8ee5
GM
2116 above has changed them. */
2117 it->pixel_width = it->ascent = it->descent = 0;
312246d1 2118 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
2119 }
2120
2121 /* Set this after getting the dimensions of truncation and
2122 continuation glyphs, so that we don't produce glyphs when calling
2123 produce_special_glyphs, above. */
2124 it->glyph_row = row;
2125 it->area = TEXT_AREA;
2126
2127 /* Get the dimensions of the display area. The display area
2128 consists of the visible window area plus a horizontally scrolled
2129 part to the left of the window. All x-values are relative to the
2130 start of this total display area. */
2131 if (base_face_id != DEFAULT_FACE_ID)
2132 {
2133 /* Mode lines, menu bar in terminal frames. */
2134 it->first_visible_x = 0;
da8b7f4f 2135 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
5f5c8ee5
GM
2136 }
2137 else
2138 {
2139 it->first_visible_x
da8b7f4f 2140 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
5f5c8ee5
GM
2141 it->last_visible_x = (it->first_visible_x
2142 + window_box_width (w, TEXT_AREA));
2143
2144 /* If we truncate lines, leave room for the truncator glyph(s) at
2145 the right margin. Otherwise, leave room for the continuation
2146 glyph(s). Truncation and continuation glyphs are not inserted
2147 for window-based redisplay. */
2148 if (!FRAME_WINDOW_P (it->f))
2149 {
2150 if (it->truncate_lines_p)
2151 it->last_visible_x -= it->truncation_pixel_width;
2152 else
2153 it->last_visible_x -= it->continuation_pixel_width;
2154 }
2155
045dee35 2156 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
da8b7f4f 2157 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
2158 }
2159
2160 /* Leave room for a border glyph. */
2161 if (!FRAME_WINDOW_P (it->f)
2162 && !WINDOW_RIGHTMOST_P (it->w))
2163 it->last_visible_x -= 1;
2164
2165 it->last_visible_y = window_text_bottom_y (w);
2166
2167 /* For mode lines and alike, arrange for the first glyph having a
2168 left box line if the face specifies a box. */
2169 if (base_face_id != DEFAULT_FACE_ID)
2170 {
2171 struct face *face;
2311178e 2172
5f5c8ee5
GM
2173 it->face_id = base_face_id;
2174
2175 /* If we have a boxed mode line, make the first character appear
2176 with a left box line. */
2177 face = FACE_FROM_ID (it->f, base_face_id);
2178 if (face->box != FACE_NO_BOX)
2179 it->start_of_box_run_p = 1;
2180 }
2181
2182 /* If a buffer position was specified, set the iterator there,
2183 getting overlays and face properties from that position. */
3ebf0ea9 2184 if (charpos >= BUF_BEG (current_buffer))
5f5c8ee5
GM
2185 {
2186 it->end_charpos = ZV;
2187 it->face_id = -1;
2188 IT_CHARPOS (*it) = charpos;
2311178e 2189
5f5c8ee5 2190 /* Compute byte position if not specified. */
3ebf0ea9 2191 if (bytepos < charpos)
5f5c8ee5
GM
2192 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2193 else
2194 IT_BYTEPOS (*it) = bytepos;
2195
29b3ea63
KS
2196 it->start = it->current;
2197
5f5c8ee5
GM
2198 /* Compute faces etc. */
2199 reseat (it, it->current.pos, 1);
2200 }
2311178e 2201
5f5c8ee5
GM
2202 CHECK_IT (it);
2203}
2204
2205
2206/* Initialize IT for the display of window W with window start POS. */
2207
2208void
2209start_display (it, w, pos)
2210 struct it *it;
2211 struct window *w;
2212 struct text_pos pos;
2213{
5f5c8ee5 2214 struct glyph_row *row;
045dee35 2215 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
2216
2217 row = w->desired_matrix->rows + first_vpos;
2218 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3ccb8bcf 2219 it->first_vpos = first_vpos;
17fdcfc8
GM
2220
2221 if (!it->truncate_lines_p)
5f5c8ee5 2222 {
17fdcfc8
GM
2223 int start_at_line_beg_p;
2224 int first_y = it->current_y;
2311178e 2225
17fdcfc8
GM
2226 /* If window start is not at a line start, skip forward to POS to
2227 get the correct continuation lines width. */
2228 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2229 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2230 if (!start_at_line_beg_p)
5f5c8ee5 2231 {
0e47bbf7
RS
2232 int new_x;
2233
17fdcfc8
GM
2234 reseat_at_previous_visible_line_start (it);
2235 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2236
0e47bbf7
RS
2237 new_x = it->current_x + it->pixel_width;
2238
17fdcfc8
GM
2239 /* If lines are continued, this line may end in the middle
2240 of a multi-glyph character (e.g. a control character
2241 displayed as \003, or in the middle of an overlay
2242 string). In this case move_it_to above will not have
2243 taken us to the start of the continuation line but to the
2244 end of the continued line. */
0e47bbf7
RS
2245 if (it->current_x > 0
2246 && !it->truncate_lines_p /* Lines are continued. */
2247 && (/* And glyph doesn't fit on the line. */
2248 new_x > it->last_visible_x
2249 /* Or it fits exactly and we're on a window
2250 system frame. */
2251 || (new_x == it->last_visible_x
2252 && FRAME_WINDOW_P (it->f))))
5f5c8ee5 2253 {
b28cb6ed
GM
2254 if (it->current.dpvec_index >= 0
2255 || it->current.overlay_string_index >= 0)
2256 {
cafafe0b 2257 set_iterator_to_next (it, 1);
b28cb6ed
GM
2258 move_it_in_display_line_to (it, -1, -1, 0);
2259 }
2311178e 2260
b28cb6ed 2261 it->continuation_lines_width += it->current_x;
5f5c8ee5 2262 }
b28cb6ed
GM
2263
2264 /* We're starting a new display line, not affected by the
2265 height of the continued line, so clear the appropriate
2266 fields in the iterator structure. */
2267 it->max_ascent = it->max_descent = 0;
2268 it->max_phys_ascent = it->max_phys_descent = 0;
2311178e 2269
17fdcfc8
GM
2270 it->current_y = first_y;
2271 it->vpos = 0;
2272 it->current_x = it->hpos = 0;
2273 }
5f5c8ee5
GM
2274 }
2275
2276#if 0 /* Don't assert the following because start_display is sometimes
2277 called intentionally with a window start that is not at a
2278 line start. Please leave this code in as a comment. */
2311178e 2279
5f5c8ee5
GM
2280 /* Window start should be on a line start, now. */
2281 xassert (it->continuation_lines_width
2282 || IT_CHARPOS (it) == BEGV
2283 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2284#endif /* 0 */
2285}
2286
2287
4bde0ebb
GM
2288/* Return 1 if POS is a position in ellipses displayed for invisible
2289 text. W is the window we display, for text property lookup. */
5f5c8ee5 2290
4bde0ebb
GM
2291static int
2292in_ellipses_for_invisible_text_p (pos, w)
5f5c8ee5 2293 struct display_pos *pos;
4bde0ebb 2294 struct window *w;
5f5c8ee5 2295{
ac90c44f 2296 Lisp_Object prop, window;
4bde0ebb
GM
2297 int ellipses_p = 0;
2298 int charpos = CHARPOS (pos->pos);
2311178e 2299
ac90c44f
GM
2300 /* If POS specifies a position in a display vector, this might
2301 be for an ellipsis displayed for invisible text. We won't
2302 get the iterator set up for delivering that ellipsis unless
2303 we make sure that it gets aware of the invisible text. */
2304 if (pos->dpvec_index >= 0
2305 && pos->overlay_string_index < 0
2306 && CHARPOS (pos->string_pos) < 0
2307 && charpos > BEGV
2308 && (XSETWINDOW (window, w),
2309 prop = Fget_char_property (make_number (charpos),
2310 Qinvisible, window),
20fbd925 2311 !TEXT_PROP_MEANS_INVISIBLE (prop)))
ac90c44f
GM
2312 {
2313 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2314 window);
8580a4e3 2315 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
4bde0ebb
GM
2316 }
2317
2318 return ellipses_p;
2319}
2320
2321
2322/* Initialize IT for stepping through current_buffer in window W,
2323 starting at position POS that includes overlay string and display
47d57b22
GM
2324 vector/ control character translation position information. Value
2325 is zero if there are overlay strings with newlines at POS. */
4bde0ebb 2326
47d57b22 2327static int
4bde0ebb
GM
2328init_from_display_pos (it, w, pos)
2329 struct it *it;
2330 struct window *w;
2331 struct display_pos *pos;
2332{
2333 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
47d57b22 2334 int i, overlay_strings_with_newlines = 0;
2311178e 2335
4bde0ebb
GM
2336 /* If POS specifies a position in a display vector, this might
2337 be for an ellipsis displayed for invisible text. We won't
2338 get the iterator set up for delivering that ellipsis unless
2339 we make sure that it gets aware of the invisible text. */
2340 if (in_ellipses_for_invisible_text_p (pos, w))
2341 {
2342 --charpos;
2343 bytepos = 0;
ac90c44f 2344 }
2311178e 2345
5f5c8ee5
GM
2346 /* Keep in mind: the call to reseat in init_iterator skips invisible
2347 text, so we might end up at a position different from POS. This
2348 is only a problem when POS is a row start after a newline and an
2349 overlay starts there with an after-string, and the overlay has an
2350 invisible property. Since we don't skip invisible text in
2351 display_line and elsewhere immediately after consuming the
2352 newline before the row start, such a POS will not be in a string,
2353 but the call to init_iterator below will move us to the
2354 after-string. */
ac90c44f 2355 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
5f5c8ee5 2356
47d57b22 2357 for (i = 0; i < it->n_overlay_strings; ++i)
1e1e5daf 2358 {
50f80c2f
KR
2359 const char *s = SDATA (it->overlay_strings[i]);
2360 const char *e = s + SBYTES (it->overlay_strings[i]);
2311178e 2361
1e1e5daf
GM
2362 while (s < e && *s != '\n')
2363 ++s;
2364
2365 if (s < e)
2366 {
2367 overlay_strings_with_newlines = 1;
2368 break;
2369 }
2370 }
47d57b22 2371
75c5350a
GM
2372 /* If position is within an overlay string, set up IT to the right
2373 overlay string. */
5f5c8ee5
GM
2374 if (pos->overlay_string_index >= 0)
2375 {
2376 int relative_index;
75c5350a
GM
2377
2378 /* If the first overlay string happens to have a `display'
2379 property for an image, the iterator will be set up for that
2380 image, and we have to undo that setup first before we can
2381 correct the overlay string index. */
2382 if (it->method == next_element_from_image)
2383 pop_it (it);
2311178e 2384
5f5c8ee5
GM
2385 /* We already have the first chunk of overlay strings in
2386 IT->overlay_strings. Load more until the one for
2387 pos->overlay_string_index is in IT->overlay_strings. */
2388 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2389 {
2390 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2391 it->current.overlay_string_index = 0;
2392 while (n--)
2393 {
5a08cbaf 2394 load_overlay_strings (it, 0);
5f5c8ee5
GM
2395 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2396 }
2397 }
2311178e 2398
5f5c8ee5
GM
2399 it->current.overlay_string_index = pos->overlay_string_index;
2400 relative_index = (it->current.overlay_string_index
2401 % OVERLAY_STRING_CHUNK_SIZE);
2402 it->string = it->overlay_strings[relative_index];
cafafe0b 2403 xassert (STRINGP (it->string));
5f5c8ee5
GM
2404 it->current.string_pos = pos->string_pos;
2405 it->method = next_element_from_string;
2406 }
2311178e 2407
1e1e5daf
GM
2408#if 0 /* This is bogus because POS not having an overlay string
2409 position does not mean it's after the string. Example: A
2410 line starting with a before-string and initialization of IT
2411 to the previous row's end position. */
2be8f184
GM
2412 else if (it->current.overlay_string_index >= 0)
2413 {
2414 /* If POS says we're already after an overlay string ending at
2415 POS, make sure to pop the iterator because it will be in
2416 front of that overlay string. When POS is ZV, we've thereby
2417 also ``processed'' overlay strings at ZV. */
aeb2b8fc
GM
2418 while (it->sp)
2419 pop_it (it);
2be8f184
GM
2420 it->current.overlay_string_index = -1;
2421 it->method = next_element_from_buffer;
2422 if (CHARPOS (pos->pos) == ZV)
2423 it->overlay_strings_at_end_processed_p = 1;
2424 }
1e1e5daf 2425#endif /* 0 */
2311178e 2426
2be8f184 2427 if (CHARPOS (pos->string_pos) >= 0)
5f5c8ee5
GM
2428 {
2429 /* Recorded position is not in an overlay string, but in another
2430 string. This can only be a string from a `display' property.
2431 IT should already be filled with that string. */
2432 it->current.string_pos = pos->string_pos;
2433 xassert (STRINGP (it->string));
2434 }
2435
ac90c44f
GM
2436 /* Restore position in display vector translations, control
2437 character translations or ellipses. */
5f5c8ee5
GM
2438 if (pos->dpvec_index >= 0)
2439 {
ac90c44f
GM
2440 if (it->dpvec == NULL)
2441 get_next_display_element (it);
5f5c8ee5
GM
2442 xassert (it->dpvec && it->current.dpvec_index == 0);
2443 it->current.dpvec_index = pos->dpvec_index;
2444 }
2311178e 2445
5f5c8ee5 2446 CHECK_IT (it);
47d57b22 2447 return !overlay_strings_with_newlines;
5f5c8ee5
GM
2448}
2449
2450
2451/* Initialize IT for stepping through current_buffer in window W
2452 starting at ROW->start. */
2453
2454static void
2455init_to_row_start (it, w, row)
2456 struct it *it;
2457 struct window *w;
2458 struct glyph_row *row;
2459{
2460 init_from_display_pos (it, w, &row->start);
29b3ea63 2461 it->start = row->start;
5f5c8ee5
GM
2462 it->continuation_lines_width = row->continuation_lines_width;
2463 CHECK_IT (it);
2464}
2465
2311178e 2466
5f5c8ee5 2467/* Initialize IT for stepping through current_buffer in window W
47d57b22
GM
2468 starting in the line following ROW, i.e. starting at ROW->end.
2469 Value is zero if there are overlay strings with newlines at ROW's
2470 end position. */
5f5c8ee5 2471
47d57b22 2472static int
5f5c8ee5
GM
2473init_to_row_end (it, w, row)
2474 struct it *it;
2475 struct window *w;
2476 struct glyph_row *row;
2477{
47d57b22 2478 int success = 0;
2311178e 2479
47d57b22
GM
2480 if (init_from_display_pos (it, w, &row->end))
2481 {
2482 if (row->continued_p)
2483 it->continuation_lines_width
2484 = row->continuation_lines_width + row->pixel_width;
2485 CHECK_IT (it);
2486 success = 1;
2487 }
2311178e 2488
47d57b22 2489 return success;
5f5c8ee5
GM
2490}
2491
2492
2493
2494\f
2495/***********************************************************************
2496 Text properties
2497 ***********************************************************************/
2498
2499/* Called when IT reaches IT->stop_charpos. Handle text property and
2500 overlay changes. Set IT->stop_charpos to the next position where
2501 to stop. */
2502
2503static void
2504handle_stop (it)
2505 struct it *it;
2506{
2507 enum prop_handled handled;
2508 int handle_overlay_change_p = 1;
2509 struct props *p;
2510
2511 it->dpvec = NULL;
2512 it->current.dpvec_index = -1;
2513
2514 do
2515 {
2516 handled = HANDLED_NORMALLY;
2311178e 2517
5f5c8ee5
GM
2518 /* Call text property handlers. */
2519 for (p = it_props; p->handler; ++p)
2520 {
2521 handled = p->handler (it);
2970b9be 2522
5f5c8ee5
GM
2523 if (handled == HANDLED_RECOMPUTE_PROPS)
2524 break;
2525 else if (handled == HANDLED_RETURN)
2526 return;
2527 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2528 handle_overlay_change_p = 0;
2529 }
2530
2531 if (handled != HANDLED_RECOMPUTE_PROPS)
2532 {
2533 /* Don't check for overlay strings below when set to deliver
2534 characters from a display vector. */
2535 if (it->method == next_element_from_display_vector)
2536 handle_overlay_change_p = 0;
2537
2538 /* Handle overlay changes. */
2539 if (handle_overlay_change_p)
2540 handled = handle_overlay_change (it);
2311178e 2541
5f5c8ee5
GM
2542 /* Determine where to stop next. */
2543 if (handled == HANDLED_NORMALLY)
2544 compute_stop_pos (it);
2545 }
2546 }
2547 while (handled == HANDLED_RECOMPUTE_PROPS);
2548}
2549
2550
2551/* Compute IT->stop_charpos from text property and overlay change
2552 information for IT's current position. */
2553
2554static void
2555compute_stop_pos (it)
2556 struct it *it;
2557{
2558 register INTERVAL iv, next_iv;
2559 Lisp_Object object, limit, position;
2560
2561 /* If nowhere else, stop at the end. */
2562 it->stop_charpos = it->end_charpos;
2311178e 2563
5f5c8ee5
GM
2564 if (STRINGP (it->string))
2565 {
2566 /* Strings are usually short, so don't limit the search for
2567 properties. */
2568 object = it->string;
2569 limit = Qnil;
ac90c44f 2570 position = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
2571 }
2572 else
2573 {
2574 int charpos;
2575
2576 /* If next overlay change is in front of the current stop pos
2577 (which is IT->end_charpos), stop there. Note: value of
2578 next_overlay_change is point-max if no overlay change
2579 follows. */
2580 charpos = next_overlay_change (IT_CHARPOS (*it));
2581 if (charpos < it->stop_charpos)
2582 it->stop_charpos = charpos;
2583
2584 /* If showing the region, we have to stop at the region
2585 start or end because the face might change there. */
2586 if (it->region_beg_charpos > 0)
2587 {
2588 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2589 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2590 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2591 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2592 }
2311178e 2593
5f5c8ee5
GM
2594 /* Set up variables for computing the stop position from text
2595 property changes. */
2596 XSETBUFFER (object, current_buffer);
ac90c44f
GM
2597 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2598 position = make_number (IT_CHARPOS (*it));
5f5c8ee5
GM
2599
2600 }
2601
2602 /* Get the interval containing IT's position. Value is a null
2603 interval if there isn't such an interval. */
2604 iv = validate_interval_range (object, &position, &position, 0);
2605 if (!NULL_INTERVAL_P (iv))
2606 {
2607 Lisp_Object values_here[LAST_PROP_IDX];
2608 struct props *p;
2609
2610 /* Get properties here. */
2611 for (p = it_props; p->handler; ++p)
2612 values_here[p->idx] = textget (iv->plist, *p->name);
2613
2614 /* Look for an interval following iv that has different
2615 properties. */
2616 for (next_iv = next_interval (iv);
2617 (!NULL_INTERVAL_P (next_iv)
2618 && (NILP (limit)
2619 || XFASTINT (limit) > next_iv->position));
2620 next_iv = next_interval (next_iv))
2621 {
2622 for (p = it_props; p->handler; ++p)
2623 {
2624 Lisp_Object new_value;
2625
2626 new_value = textget (next_iv->plist, *p->name);
2627 if (!EQ (values_here[p->idx], new_value))
2628 break;
2629 }
2311178e 2630
5f5c8ee5
GM
2631 if (p->handler)
2632 break;
2633 }
2634
2635 if (!NULL_INTERVAL_P (next_iv))
2636 {
2637 if (INTEGERP (limit)
2638 && next_iv->position >= XFASTINT (limit))
2639 /* No text property change up to limit. */
2640 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2641 else
2642 /* Text properties change in next_iv. */
2643 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2644 }
2645 }
2646
2647 xassert (STRINGP (it->string)
2648 || (it->stop_charpos >= BEGV
2649 && it->stop_charpos >= IT_CHARPOS (*it)));
2650}
2651
2652
2653/* Return the position of the next overlay change after POS in
2654 current_buffer. Value is point-max if no overlay change
2655 follows. This is like `next-overlay-change' but doesn't use
2656 xmalloc. */
2657
2658static int
2659next_overlay_change (pos)
2660 int pos;
2661{
2662 int noverlays;
2663 int endpos;
2664 Lisp_Object *overlays;
2665 int len;
2666 int i;
2667
2668 /* Get all overlays at the given position. */
2669 len = 10;
2670 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2671 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2672 if (noverlays > len)
2673 {
2674 len = noverlays;
2675 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2676 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2677 }
2678
2679 /* If any of these overlays ends before endpos,
2680 use its ending point instead. */
2681 for (i = 0; i < noverlays; ++i)
2682 {
2683 Lisp_Object oend;
2684 int oendpos;
2685
2686 oend = OVERLAY_END (overlays[i]);
2687 oendpos = OVERLAY_POSITION (oend);
2688 endpos = min (endpos, oendpos);
2689 }
2690
2691 return endpos;
2692}
2693
2694
2695\f
2696/***********************************************************************
2697 Fontification
2698 ***********************************************************************/
2699
2700/* Handle changes in the `fontified' property of the current buffer by
2701 calling hook functions from Qfontification_functions to fontify
2702 regions of text. */
2703
2704static enum prop_handled
2705handle_fontified_prop (it)
2706 struct it *it;
2707{
2708 Lisp_Object prop, pos;
2709 enum prop_handled handled = HANDLED_NORMALLY;
2710
2711 /* Get the value of the `fontified' property at IT's current buffer
2712 position. (The `fontified' property doesn't have a special
2713 meaning in strings.) If the value is nil, call functions from
2714 Qfontification_functions. */
2715 if (!STRINGP (it->string)
2716 && it->s == NULL
2717 && !NILP (Vfontification_functions)
085536c2 2718 && !NILP (Vrun_hooks)
5f5c8ee5
GM
2719 && (pos = make_number (IT_CHARPOS (*it)),
2720 prop = Fget_char_property (pos, Qfontified, Qnil),
2721 NILP (prop)))
2722 {
331379bf 2723 int count = SPECPDL_INDEX ();
085536c2
GM
2724 Lisp_Object val;
2725
2726 val = Vfontification_functions;
2727 specbind (Qfontification_functions, Qnil);
2311178e 2728
085536c2 2729 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
116d6f5c 2730 safe_call1 (val, pos);
085536c2
GM
2731 else
2732 {
2733 Lisp_Object globals, fn;
2734 struct gcpro gcpro1, gcpro2;
5f5c8ee5 2735
085536c2
GM
2736 globals = Qnil;
2737 GCPRO2 (val, globals);
2311178e 2738
085536c2
GM
2739 for (; CONSP (val); val = XCDR (val))
2740 {
2741 fn = XCAR (val);
2311178e 2742
085536c2
GM
2743 if (EQ (fn, Qt))
2744 {
2745 /* A value of t indicates this hook has a local
2746 binding; it means to run the global binding too.
2747 In a global value, t should not occur. If it
2748 does, we must ignore it to avoid an endless
2749 loop. */
2750 for (globals = Fdefault_value (Qfontification_functions);
2751 CONSP (globals);
2752 globals = XCDR (globals))
2753 {
2754 fn = XCAR (globals);
2755 if (!EQ (fn, Qt))
116d6f5c 2756 safe_call1 (fn, pos);
085536c2
GM
2757 }
2758 }
2759 else
116d6f5c 2760 safe_call1 (fn, pos);
085536c2
GM
2761 }
2762
2763 UNGCPRO;
2764 }
2765
2766 unbind_to (count, Qnil);
5f5c8ee5
GM
2767
2768 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2769 something. This avoids an endless loop if they failed to
2770 fontify the text for which reason ever. */
2771 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2772 handled = HANDLED_RECOMPUTE_PROPS;
2773 }
2774
2775 return handled;
2776}
2777
2778
2779\f
2780/***********************************************************************
2781 Faces
2782 ***********************************************************************/
2783
2784/* Set up iterator IT from face properties at its current position.
2785 Called from handle_stop. */
2786
2787static enum prop_handled
2788handle_face_prop (it)
2789 struct it *it;
2790{
2791 int new_face_id, next_stop;
2311178e 2792
5f5c8ee5
GM
2793 if (!STRINGP (it->string))
2794 {
2795 new_face_id
2796 = face_at_buffer_position (it->w,
2797 IT_CHARPOS (*it),
2798 it->region_beg_charpos,
2799 it->region_end_charpos,
2800 &next_stop,
2801 (IT_CHARPOS (*it)
2802 + TEXT_PROP_DISTANCE_LIMIT),
2803 0);
2311178e 2804
5f5c8ee5
GM
2805 /* Is this a start of a run of characters with box face?
2806 Caveat: this can be called for a freshly initialized
4b41cebb 2807 iterator; face_id is -1 in this case. We know that the new
5f5c8ee5
GM
2808 face will not change until limit, i.e. if the new face has a
2809 box, all characters up to limit will have one. But, as
2810 usual, we don't know whether limit is really the end. */
2811 if (new_face_id != it->face_id)
2812 {
2813 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2311178e 2814
5f5c8ee5
GM
2815 /* If new face has a box but old face has not, this is
2816 the start of a run of characters with box, i.e. it has
2817 a shadow on the left side. The value of face_id of the
2818 iterator will be -1 if this is the initial call that gets
2819 the face. In this case, we have to look in front of IT's
2820 position and see whether there is a face != new_face_id. */
2821 it->start_of_box_run_p
2822 = (new_face->box != FACE_NO_BOX
2823 && (it->face_id >= 0
2824 || IT_CHARPOS (*it) == BEG
2825 || new_face_id != face_before_it_pos (it)));
2826 it->face_box_p = new_face->box != FACE_NO_BOX;
2827 }
2828 }
2829 else
2830 {
06a12811
GM
2831 int base_face_id, bufpos;
2832
2833 if (it->current.overlay_string_index >= 0)
2834 bufpos = IT_CHARPOS (*it);
2835 else
2836 bufpos = 0;
2311178e 2837
06a12811
GM
2838 /* For strings from a buffer, i.e. overlay strings or strings
2839 from a `display' property, use the face at IT's current
2840 buffer position as the base face to merge with, so that
2841 overlay strings appear in the same face as surrounding
2842 text, unless they specify their own faces. */
2843 base_face_id = underlying_face_id (it);
2311178e 2844
06a12811
GM
2845 new_face_id = face_at_string_position (it->w,
2846 it->string,
2847 IT_STRING_CHARPOS (*it),
2848 bufpos,
2849 it->region_beg_charpos,
2850 it->region_end_charpos,
2851 &next_stop,
5de7c6f2 2852 base_face_id, 0);
2311178e 2853
5f5c8ee5
GM
2854#if 0 /* This shouldn't be neccessary. Let's check it. */
2855 /* If IT is used to display a mode line we would really like to
2856 use the mode line face instead of the frame's default face. */
2857 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2858 && new_face_id == DEFAULT_FACE_ID)
96d2320f 2859 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
5f5c8ee5 2860#endif
2311178e 2861
5f5c8ee5
GM
2862 /* Is this a start of a run of characters with box? Caveat:
2863 this can be called for a freshly allocated iterator; face_id
2864 is -1 is this case. We know that the new face will not
2865 change until the next check pos, i.e. if the new face has a
2866 box, all characters up to that position will have a
2867 box. But, as usual, we don't know whether that position
2868 is really the end. */
2869 if (new_face_id != it->face_id)
2870 {
2871 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2872 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2311178e 2873
5f5c8ee5
GM
2874 /* If new face has a box but old face hasn't, this is the
2875 start of a run of characters with box, i.e. it has a
2876 shadow on the left side. */
2877 it->start_of_box_run_p
2878 = new_face->box && (old_face == NULL || !old_face->box);
2879 it->face_box_p = new_face->box != FACE_NO_BOX;
2880 }
2881 }
2311178e 2882
5f5c8ee5 2883 it->face_id = new_face_id;
5f5c8ee5
GM
2884 return HANDLED_NORMALLY;
2885}
2886
2887
06a12811
GM
2888/* Return the ID of the face ``underlying'' IT's current position,
2889 which is in a string. If the iterator is associated with a
2890 buffer, return the face at IT's current buffer position.
2891 Otherwise, use the iterator's base_face_id. */
2892
2893static int
2894underlying_face_id (it)
2895 struct it *it;
2896{
2897 int face_id = it->base_face_id, i;
2898
2899 xassert (STRINGP (it->string));
2900
2901 for (i = it->sp - 1; i >= 0; --i)
2902 if (NILP (it->stack[i].string))
2903 face_id = it->stack[i].face_id;
2904
2905 return face_id;
2906}
2907
2908
5f5c8ee5
GM
2909/* Compute the face one character before or after the current position
2910 of IT. BEFORE_P non-zero means get the face in front of IT's
2911 position. Value is the id of the face. */
2912
2913static int
2914face_before_or_after_it_pos (it, before_p)
2915 struct it *it;
2916 int before_p;
2917{
2918 int face_id, limit;
2919 int next_check_charpos;
2920 struct text_pos pos;
2921
2922 xassert (it->s == NULL);
2311178e 2923
5f5c8ee5
GM
2924 if (STRINGP (it->string))
2925 {
06a12811 2926 int bufpos, base_face_id;
2311178e 2927
5f5c8ee5
GM
2928 /* No face change past the end of the string (for the case
2929 we are padding with spaces). No face change before the
2930 string start. */
2051c264 2931 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
5f5c8ee5
GM
2932 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2933 return it->face_id;
2934
2935 /* Set pos to the position before or after IT's current position. */
2936 if (before_p)
2937 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2938 else
260a86a0
KH
2939 /* For composition, we must check the character after the
2940 composition. */
2941 pos = (it->what == IT_COMPOSITION
2942 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2943 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
5f5c8ee5 2944
06a12811
GM
2945 if (it->current.overlay_string_index >= 0)
2946 bufpos = IT_CHARPOS (*it);
2947 else
2948 bufpos = 0;
2949
2950 base_face_id = underlying_face_id (it);
2951
5f5c8ee5 2952 /* Get the face for ASCII, or unibyte. */
06a12811
GM
2953 face_id = face_at_string_position (it->w,
2954 it->string,
2955 CHARPOS (pos),
2956 bufpos,
2957 it->region_beg_charpos,
2958 it->region_end_charpos,
2959 &next_check_charpos,
5de7c6f2 2960 base_face_id, 0);
5f5c8ee5
GM
2961
2962 /* Correct the face for charsets different from ASCII. Do it
2963 for the multibyte case only. The face returned above is
2964 suitable for unibyte text if IT->string is unibyte. */
2965 if (STRING_MULTIBYTE (it->string))
2966 {
50f80c2f 2967 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2051c264 2968 int rest = SBYTES (it->string) - BYTEPOS (pos);
980806b6
KH
2969 int c, len;
2970 struct face *face = FACE_FROM_ID (it->f, face_id);
2311178e 2971
4fdb80f2 2972 c = string_char_and_length (p, rest, &len);
980806b6 2973 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2974 }
2975 }
2976 else
2977 {
70851746
GM
2978 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2979 || (IT_CHARPOS (*it) <= BEGV && before_p))
2980 return it->face_id;
2311178e 2981
5f5c8ee5
GM
2982 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2983 pos = it->current.pos;
2311178e 2984
5f5c8ee5 2985 if (before_p)
c1005d06 2986 DEC_TEXT_POS (pos, it->multibyte_p);
5f5c8ee5 2987 else
260a86a0
KH
2988 {
2989 if (it->what == IT_COMPOSITION)
2990 /* For composition, we must check the position after the
2991 composition. */
2992 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2993 else
c1005d06 2994 INC_TEXT_POS (pos, it->multibyte_p);
260a86a0 2995 }
2311178e 2996
5f5c8ee5
GM
2997 /* Determine face for CHARSET_ASCII, or unibyte. */
2998 face_id = face_at_buffer_position (it->w,
2999 CHARPOS (pos),
3000 it->region_beg_charpos,
3001 it->region_end_charpos,
3002 &next_check_charpos,
3003 limit, 0);
3004
3005 /* Correct the face for charsets different from ASCII. Do it
3006 for the multibyte case only. The face returned above is
3007 suitable for unibyte text if current_buffer is unibyte. */
3008 if (it->multibyte_p)
3009 {
1d1b6e6a 3010 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
980806b6
KH
3011 struct face *face = FACE_FROM_ID (it->f, face_id);
3012 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
3013 }
3014 }
2311178e 3015
5f5c8ee5
GM
3016 return face_id;
3017}
3018
3019
3020\f
3021/***********************************************************************
3022 Invisible text
3023 ***********************************************************************/
3024
3025/* Set up iterator IT from invisible properties at its current
3026 position. Called from handle_stop. */
3027
3028static enum prop_handled
3029handle_invisible_prop (it)
3030 struct it *it;
3031{
3032 enum prop_handled handled = HANDLED_NORMALLY;
3033
3034 if (STRINGP (it->string))
3035 {
3036 extern Lisp_Object Qinvisible;
3037 Lisp_Object prop, end_charpos, limit, charpos;
3038
3039 /* Get the value of the invisible text property at the
3040 current position. Value will be nil if there is no such
3041 property. */
ac90c44f 3042 charpos = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
3043 prop = Fget_text_property (charpos, Qinvisible, it->string);
3044
eadc0bf8
GM
3045 if (!NILP (prop)
3046 && IT_STRING_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
3047 {
3048 handled = HANDLED_RECOMPUTE_PROPS;
2311178e 3049
5f5c8ee5
GM
3050 /* Get the position at which the next change of the
3051 invisible text property can be found in IT->string.
3052 Value will be nil if the property value is the same for
3053 all the rest of IT->string. */
2051c264 3054 XSETINT (limit, SCHARS (it->string));
5f5c8ee5 3055 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2051c264 3056 it->string, limit);
2311178e 3057
5f5c8ee5
GM
3058 /* Text at current position is invisible. The next
3059 change in the property is at position end_charpos.
3060 Move IT's current position to that position. */
3061 if (INTEGERP (end_charpos)
3062 && XFASTINT (end_charpos) < XFASTINT (limit))
3063 {
3064 struct text_pos old;
3065 old = it->current.string_pos;
3066 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3067 compute_string_pos (&it->current.string_pos, old, it->string);
3068 }
3069 else
3070 {
3071 /* The rest of the string is invisible. If this is an
3072 overlay string, proceed with the next overlay string
3073 or whatever comes and return a character from there. */
3074 if (it->current.overlay_string_index >= 0)
3075 {
3076 next_overlay_string (it);
3077 /* Don't check for overlay strings when we just
3078 finished processing them. */
3079 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3080 }
3081 else
3082 {
2051c264
GM
3083 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3084 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
5f5c8ee5
GM
3085 }
3086 }
3087 }
3088 }
3089 else
3090 {
8580a4e3 3091 int invis_p, newpos, next_stop, start_charpos;
5a08cbaf 3092 Lisp_Object pos, prop, overlay;
5f5c8ee5
GM
3093
3094 /* First of all, is there invisible text at this position? */
5a08cbaf 3095 start_charpos = IT_CHARPOS (*it);
ac90c44f 3096 pos = make_number (IT_CHARPOS (*it));
5a08cbaf
GM
3097 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3098 &overlay);
8580a4e3
SM
3099 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3100
5f5c8ee5 3101 /* If we are on invisible text, skip over it. */
8580a4e3 3102 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
3103 {
3104 /* Record whether we have to display an ellipsis for the
3105 invisible text. */
8580a4e3 3106 int display_ellipsis_p = invis_p == 2;
5f5c8ee5
GM
3107
3108 handled = HANDLED_RECOMPUTE_PROPS;
2311178e 3109
5f5c8ee5
GM
3110 /* Loop skipping over invisible text. The loop is left at
3111 ZV or with IT on the first char being visible again. */
3112 do
3113 {
3114 /* Try to skip some invisible text. Return value is the
3115 position reached which can be equal to IT's position
3116 if there is nothing invisible here. This skips both
3117 over invisible text properties and overlays with
3118 invisible property. */
3119 newpos = skip_invisible (IT_CHARPOS (*it),
3120 &next_stop, ZV, it->window);
3121
3122 /* If we skipped nothing at all we weren't at invisible
3123 text in the first place. If everything to the end of
3124 the buffer was skipped, end the loop. */
3125 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
8580a4e3 3126 invis_p = 0;
5f5c8ee5
GM
3127 else
3128 {
3129 /* We skipped some characters but not necessarily
3130 all there are. Check if we ended up on visible
3131 text. Fget_char_property returns the property of
3132 the char before the given position, i.e. if we
8580a4e3 3133 get invis_p = 0, this means that the char at
5f5c8ee5 3134 newpos is visible. */
ac90c44f 3135 pos = make_number (newpos);
5f5c8ee5 3136 prop = Fget_char_property (pos, Qinvisible, it->window);
8580a4e3 3137 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
5f5c8ee5 3138 }
2311178e 3139
5f5c8ee5
GM
3140 /* If we ended up on invisible text, proceed to
3141 skip starting with next_stop. */
8580a4e3 3142 if (invis_p)
5f5c8ee5
GM
3143 IT_CHARPOS (*it) = next_stop;
3144 }
8580a4e3 3145 while (invis_p);
5a08cbaf 3146
5f5c8ee5
GM
3147 /* The position newpos is now either ZV or on visible text. */
3148 IT_CHARPOS (*it) = newpos;
3149 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2311178e 3150
5a08cbaf
GM
3151 /* If there are before-strings at the start of invisible
3152 text, and the text is invisible because of a text
3153 property, arrange to show before-strings because 20.x did
3154 it that way. (If the text is invisible because of an
3155 overlay property instead of a text property, this is
3156 already handled in the overlay code.) */
3157 if (NILP (overlay)
3158 && get_overlay_strings (it, start_charpos))
5f5c8ee5 3159 {
5a08cbaf
GM
3160 handled = HANDLED_RECOMPUTE_PROPS;
3161 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
5f5c8ee5 3162 }
5a08cbaf
GM
3163 else if (display_ellipsis_p)
3164 setup_for_ellipsis (it);
5f5c8ee5
GM
3165 }
3166 }
3167
3168 return handled;
3169}
3170
3171
5a08cbaf
GM
3172/* Make iterator IT return `...' next. */
3173
3174static void
3175setup_for_ellipsis (it)
3176 struct it *it;
3177{
2311178e 3178 if (it->dp
5a08cbaf
GM
3179 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3180 {
3181 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3182 it->dpvec = v->contents;
3183 it->dpend = v->contents + v->size;
3184 }
2311178e 3185 else
5a08cbaf
GM
3186 {
3187 /* Default `...'. */
3188 it->dpvec = default_invis_vector;
3189 it->dpend = default_invis_vector + 3;
3190 }
2311178e 3191
5a08cbaf
GM
3192 /* The ellipsis display does not replace the display of the
3193 character at the new position. Indicate this by setting
3194 IT->dpvec_char_len to zero. */
3195 it->dpvec_char_len = 0;
2311178e 3196
5a08cbaf
GM
3197 it->current.dpvec_index = 0;
3198 it->method = next_element_from_display_vector;
3199}
3200
3201
5f5c8ee5
GM
3202\f
3203/***********************************************************************
3204 'display' property
3205 ***********************************************************************/
3206
3207/* Set up iterator IT from `display' property at its current position.
3208 Called from handle_stop. */
3209
3210static enum prop_handled
3211handle_display_prop (it)
3212 struct it *it;
3213{
3214 Lisp_Object prop, object;
3215 struct text_pos *position;
a61b7058 3216 int display_replaced_p = 0;
5f5c8ee5
GM
3217
3218 if (STRINGP (it->string))
3219 {
3220 object = it->string;
3221 position = &it->current.string_pos;
3222 }
3223 else
3224 {
221dd3e7 3225 object = it->w->buffer;
5f5c8ee5
GM
3226 position = &it->current.pos;
3227 }
3228
3229 /* Reset those iterator values set from display property values. */
3230 it->font_height = Qnil;
3231 it->space_width = Qnil;
3232 it->voffset = 0;
3233
3234 /* We don't support recursive `display' properties, i.e. string
3235 values that have a string `display' property, that have a string
3236 `display' property etc. */
3237 if (!it->string_from_display_prop_p)
3238 it->area = TEXT_AREA;
3239
3240 prop = Fget_char_property (make_number (position->charpos),
3241 Qdisplay, object);
3242 if (NILP (prop))
3243 return HANDLED_NORMALLY;
3244
f3751a65 3245 if (CONSP (prop)
12700f40
GM
3246 /* Simple properties. */
3247 && !EQ (XCAR (prop), Qimage)
3248 && !EQ (XCAR (prop), Qspace)
3249 && !EQ (XCAR (prop), Qwhen)
3250 && !EQ (XCAR (prop), Qspace_width)
3251 && !EQ (XCAR (prop), Qheight)
3252 && !EQ (XCAR (prop), Qraise)
3253 /* Marginal area specifications. */
3254 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3255 && !NILP (XCAR (prop)))
5f5c8ee5 3256 {
a61b7058 3257 for (; CONSP (prop); prop = XCDR (prop))
5f5c8ee5 3258 {
a61b7058
GM
3259 if (handle_single_display_prop (it, XCAR (prop), object,
3260 position, display_replaced_p))
3261 display_replaced_p = 1;
5f5c8ee5
GM
3262 }
3263 }
3264 else if (VECTORP (prop))
3265 {
3266 int i;
a61b7058
GM
3267 for (i = 0; i < ASIZE (prop); ++i)
3268 if (handle_single_display_prop (it, AREF (prop, i), object,
3269 position, display_replaced_p))
3270 display_replaced_p = 1;
5f5c8ee5
GM
3271 }
3272 else
3273 {
a61b7058
GM
3274 if (handle_single_display_prop (it, prop, object, position, 0))
3275 display_replaced_p = 1;
5f5c8ee5
GM
3276 }
3277
a61b7058 3278 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
5f5c8ee5
GM
3279}
3280
3281
6c577098 3282/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
3283 at START_POS in OBJECT. */
3284
3285static struct text_pos
3286display_prop_end (it, object, start_pos)
3287 struct it *it;
3288 Lisp_Object object;
3289 struct text_pos start_pos;
3290{
3291 Lisp_Object end;
3292 struct text_pos end_pos;
5f5c8ee5 3293
016b5642
MB
3294 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3295 Qdisplay, object, Qnil);
6c577098
GM
3296 CHARPOS (end_pos) = XFASTINT (end);
3297 if (STRINGP (object))
5f5c8ee5
GM
3298 compute_string_pos (&end_pos, start_pos, it->string);
3299 else
6c577098 3300 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
3301
3302 return end_pos;
3303}
3304
3305
3306/* Set up IT from a single `display' sub-property value PROP. OBJECT
3307 is the object in which the `display' property was found. *POSITION
a61b7058
GM
3308 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3309 means that we previously saw a display sub-property which already
3310 replaced text display with something else, for example an image;
3311 ignore such properties after the first one has been processed.
5f5c8ee5
GM
3312
3313 If PROP is a `space' or `image' sub-property, set *POSITION to the
3314 end position of the `display' property.
3315
4b41cebb 3316 Value is non-zero if something was found which replaces the display
a61b7058 3317 of buffer or string text. */
5f5c8ee5
GM
3318
3319static int
a61b7058
GM
3320handle_single_display_prop (it, prop, object, position,
3321 display_replaced_before_p)
5f5c8ee5
GM
3322 struct it *it;
3323 Lisp_Object prop;
3324 Lisp_Object object;
3325 struct text_pos *position;
a61b7058 3326 int display_replaced_before_p;
5f5c8ee5
GM
3327{
3328 Lisp_Object value;
a61b7058 3329 int replaces_text_display_p = 0;
5f5c8ee5
GM
3330 Lisp_Object form;
3331
d3acf96b 3332 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
4b41cebb 3333 evaluated. If the result is nil, VALUE is ignored. */
5f5c8ee5 3334 form = Qt;
d3acf96b 3335 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5f5c8ee5
GM
3336 {
3337 prop = XCDR (prop);
3338 if (!CONSP (prop))
3339 return 0;
3340 form = XCAR (prop);
3341 prop = XCDR (prop);
5f5c8ee5
GM
3342 }
3343
3344 if (!NILP (form) && !EQ (form, Qt))
3345 {
331379bf 3346 int count = SPECPDL_INDEX ();
5f5c8ee5 3347 struct gcpro gcpro1;
5f5c8ee5 3348
b384d6f8
GM
3349 /* Bind `object' to the object having the `display' property, a
3350 buffer or string. Bind `position' to the position in the
3351 object where the property was found, and `buffer-position'
3352 to the current position in the buffer. */
3353 specbind (Qobject, object);
31ac723b
SM
3354 specbind (Qposition, make_number (CHARPOS (*position)));
3355 specbind (Qbuffer_position,
3356 make_number (STRINGP (object)
3357 ? IT_CHARPOS (*it) : CHARPOS (*position)));
b384d6f8 3358 GCPRO1 (form);
116d6f5c 3359 form = safe_eval (form);
b384d6f8
GM
3360 UNGCPRO;
3361 unbind_to (count, Qnil);
5f5c8ee5 3362 }
2311178e 3363
5f5c8ee5
GM
3364 if (NILP (form))
3365 return 0;
3366
3367 if (CONSP (prop)
3368 && EQ (XCAR (prop), Qheight)
3369 && CONSP (XCDR (prop)))
3370 {
e4093f38 3371 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
5f5c8ee5 3372 return 0;
2311178e 3373
5f5c8ee5
GM
3374 /* `(height HEIGHT)'. */
3375 it->font_height = XCAR (XCDR (prop));
3376 if (!NILP (it->font_height))
3377 {
3378 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3379 int new_height = -1;
3380
3381 if (CONSP (it->font_height)
3382 && (EQ (XCAR (it->font_height), Qplus)
3383 || EQ (XCAR (it->font_height), Qminus))
3384 && CONSP (XCDR (it->font_height))
3385 && INTEGERP (XCAR (XCDR (it->font_height))))
3386 {
3387 /* `(+ N)' or `(- N)' where N is an integer. */
3388 int steps = XINT (XCAR (XCDR (it->font_height)));
3389 if (EQ (XCAR (it->font_height), Qplus))
3390 steps = - steps;
3391 it->face_id = smaller_face (it->f, it->face_id, steps);
3392 }
0d8b31c0 3393 else if (FUNCTIONP (it->font_height))
5f5c8ee5
GM
3394 {
3395 /* Call function with current height as argument.
3396 Value is the new height. */
116d6f5c
GM
3397 Lisp_Object height;
3398 height = safe_call1 (it->font_height,
3399 face->lface[LFACE_HEIGHT_INDEX]);
5f5c8ee5
GM
3400 if (NUMBERP (height))
3401 new_height = XFLOATINT (height);
5f5c8ee5
GM
3402 }
3403 else if (NUMBERP (it->font_height))
3404 {
3405 /* Value is a multiple of the canonical char height. */
3406 struct face *face;
2311178e 3407
5f5c8ee5
GM
3408 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3409 new_height = (XFLOATINT (it->font_height)
3410 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3411 }
3412 else
3413 {
3414 /* Evaluate IT->font_height with `height' bound to the
3415 current specified height to get the new height. */
3416 Lisp_Object value;
331379bf 3417 int count = SPECPDL_INDEX ();
2311178e 3418
5f5c8ee5 3419 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
116d6f5c 3420 value = safe_eval (it->font_height);
5f5c8ee5 3421 unbind_to (count, Qnil);
2311178e 3422
5f5c8ee5
GM
3423 if (NUMBERP (value))
3424 new_height = XFLOATINT (value);
3425 }
2311178e 3426
5f5c8ee5
GM
3427 if (new_height > 0)
3428 it->face_id = face_with_height (it->f, it->face_id, new_height);
3429 }
3430 }
3431 else if (CONSP (prop)
3432 && EQ (XCAR (prop), Qspace_width)
3433 && CONSP (XCDR (prop)))
3434 {
3435 /* `(space_width WIDTH)'. */
e4093f38 3436 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 3437 return 0;
2311178e 3438
5f5c8ee5
GM
3439 value = XCAR (XCDR (prop));
3440 if (NUMBERP (value) && XFLOATINT (value) > 0)
3441 it->space_width = value;
3442 }
3443 else if (CONSP (prop)
3444 && EQ (XCAR (prop), Qraise)
3445 && CONSP (XCDR (prop)))
3446 {
5f5c8ee5 3447 /* `(raise FACTOR)'. */
e4093f38 3448 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 3449 return 0;
2311178e 3450
fb3842a8 3451#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
3452 value = XCAR (XCDR (prop));
3453 if (NUMBERP (value))
3454 {
3455 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3456 it->voffset = - (XFLOATINT (value)
1ab3e082 3457 * (FONT_HEIGHT (face->font)));
5f5c8ee5
GM
3458 }
3459#endif /* HAVE_WINDOW_SYSTEM */
3460 }
3461 else if (!it->string_from_display_prop_p)
3462 {
f3751a65 3463 /* `((margin left-margin) VALUE)' or `((margin right-margin)
4b41cebb 3464 VALUE) or `((margin nil) VALUE)' or VALUE. */
5f5c8ee5
GM
3465 Lisp_Object location, value;
3466 struct text_pos start_pos;
3467 int valid_p;
3468
3469 /* Characters having this form of property are not displayed, so
3470 we have to find the end of the property. */
5f5c8ee5
GM
3471 start_pos = *position;
3472 *position = display_prop_end (it, object, start_pos);
15e26c76 3473 value = Qnil;
5f5c8ee5
GM
3474
3475 /* Let's stop at the new position and assume that all
3476 text properties change there. */
3477 it->stop_charpos = position->charpos;
3478
f3751a65
GM
3479 location = Qunbound;
3480 if (CONSP (prop) && CONSP (XCAR (prop)))
5f5c8ee5 3481 {
f3751a65 3482 Lisp_Object tem;
2311178e 3483
5f5c8ee5 3484 value = XCDR (prop);
f3751a65
GM
3485 if (CONSP (value))
3486 value = XCAR (value);
3487
3488 tem = XCAR (prop);
3489 if (EQ (XCAR (tem), Qmargin)
3490 && (tem = XCDR (tem),
3491 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3492 (NILP (tem)
3493 || EQ (tem, Qleft_margin)
3494 || EQ (tem, Qright_margin))))
3495 location = tem;
5f5c8ee5 3496 }
f3751a65
GM
3497
3498 if (EQ (location, Qunbound))
5f5c8ee5
GM
3499 {
3500 location = Qnil;
3501 value = prop;
3502 }
3503
3504#ifdef HAVE_WINDOW_SYSTEM
fb3842a8 3505 if (FRAME_TERMCAP_P (it->f))
5f5c8ee5
GM
3506 valid_p = STRINGP (value);
3507 else
3508 valid_p = (STRINGP (value)
3509 || (CONSP (value) && EQ (XCAR (value), Qspace))
3510 || valid_image_p (value));
3511#else /* not HAVE_WINDOW_SYSTEM */
3512 valid_p = STRINGP (value);
3513#endif /* not HAVE_WINDOW_SYSTEM */
2311178e 3514
5f5c8ee5
GM
3515 if ((EQ (location, Qleft_margin)
3516 || EQ (location, Qright_margin)
3517 || NILP (location))
a61b7058
GM
3518 && valid_p
3519 && !display_replaced_before_p)
5f5c8ee5 3520 {
a61b7058 3521 replaces_text_display_p = 1;
2311178e 3522
5f5c8ee5
GM
3523 /* Save current settings of IT so that we can restore them
3524 when we are finished with the glyph property value. */
3525 push_it (it);
2311178e 3526
5f5c8ee5
GM
3527 if (NILP (location))
3528 it->area = TEXT_AREA;
3529 else if (EQ (location, Qleft_margin))
3530 it->area = LEFT_MARGIN_AREA;
3531 else
3532 it->area = RIGHT_MARGIN_AREA;
2311178e 3533
5f5c8ee5
GM
3534 if (STRINGP (value))
3535 {
3536 it->string = value;
3537 it->multibyte_p = STRING_MULTIBYTE (it->string);
3538 it->current.overlay_string_index = -1;
3539 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2051c264 3540 it->end_charpos = it->string_nchars = SCHARS (it->string);
5f5c8ee5
GM
3541 it->method = next_element_from_string;
3542 it->stop_charpos = 0;
3543 it->string_from_display_prop_p = 1;
e187cf71
GM
3544 /* Say that we haven't consumed the characters with
3545 `display' property yet. The call to pop_it in
3546 set_iterator_to_next will clean this up. */
3547 *position = start_pos;
5f5c8ee5
GM
3548 }
3549 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3550 {
3551 it->method = next_element_from_stretch;
3552 it->object = value;
3553 it->current.pos = it->position = start_pos;
3554 }
3555#ifdef HAVE_WINDOW_SYSTEM
3556 else
3557 {
3558 it->what = IT_IMAGE;
3559 it->image_id = lookup_image (it->f, value);
3560 it->position = start_pos;
3561 it->object = NILP (object) ? it->w->buffer : object;
3562 it->method = next_element_from_image;
2311178e 3563
02513cdd 3564 /* Say that we haven't consumed the characters with
5f5c8ee5
GM
3565 `display' property yet. The call to pop_it in
3566 set_iterator_to_next will clean this up. */
3567 *position = start_pos;
3568 }
3569#endif /* HAVE_WINDOW_SYSTEM */
3570 }
a21a3943
GM
3571 else
3572 /* Invalid property or property not supported. Restore
3573 the position to what it was before. */
3574 *position = start_pos;
5f5c8ee5
GM
3575 }
3576
a61b7058 3577 return replaces_text_display_p;
5f5c8ee5
GM
3578}
3579
3580
06568bbf
GM
3581/* Check if PROP is a display sub-property value whose text should be
3582 treated as intangible. */
3583
3584static int
3585single_display_prop_intangible_p (prop)
3586 Lisp_Object prop;
3587{
3588 /* Skip over `when FORM'. */
3589 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3590 {
3591 prop = XCDR (prop);
3592 if (!CONSP (prop))
3593 return 0;
3594 prop = XCDR (prop);
3595 }
3596
959bc044
SM
3597 if (STRINGP (prop))
3598 return 1;
3599
06568bbf
GM
3600 if (!CONSP (prop))
3601 return 0;
3602
3603 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3604 we don't need to treat text as intangible. */
3605 if (EQ (XCAR (prop), Qmargin))
3606 {
3607 prop = XCDR (prop);
3608 if (!CONSP (prop))
3609 return 0;
3610
3611 prop = XCDR (prop);
3612 if (!CONSP (prop)
3613 || EQ (XCAR (prop), Qleft_margin)
3614 || EQ (XCAR (prop), Qright_margin))
3615 return 0;
3616 }
2311178e 3617
6cd83a9a
SM
3618 return (CONSP (prop)
3619 && (EQ (XCAR (prop), Qimage)
3620 || EQ (XCAR (prop), Qspace)));
06568bbf
GM
3621}
3622
3623
3624/* Check if PROP is a display property value whose text should be
3625 treated as intangible. */
3626
3627int
3628display_prop_intangible_p (prop)
3629 Lisp_Object prop;
3630{
3631 if (CONSP (prop)
3632 && CONSP (XCAR (prop))
3633 && !EQ (Qmargin, XCAR (XCAR (prop))))
3634 {
3635 /* A list of sub-properties. */
3636 while (CONSP (prop))
3637 {
3638 if (single_display_prop_intangible_p (XCAR (prop)))
3639 return 1;
3640 prop = XCDR (prop);
3641 }
3642 }
3643 else if (VECTORP (prop))
3644 {
3645 /* A vector of sub-properties. */
3646 int i;
a61b7058
GM
3647 for (i = 0; i < ASIZE (prop); ++i)
3648 if (single_display_prop_intangible_p (AREF (prop, i)))
06568bbf
GM
3649 return 1;
3650 }
3651 else
3652 return single_display_prop_intangible_p (prop);
3653
3654 return 0;
3655}
3656
74bd6d65
GM
3657
3658/* Return 1 if PROP is a display sub-property value containing STRING. */
3659
3660static int
3661single_display_prop_string_p (prop, string)
3662 Lisp_Object prop, string;
3663{
74bd6d65
GM
3664 if (EQ (string, prop))
3665 return 1;
2311178e 3666
74bd6d65
GM
3667 /* Skip over `when FORM'. */
3668 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3669 {
3670 prop = XCDR (prop);
3671 if (!CONSP (prop))
3672 return 0;
3673 prop = XCDR (prop);
3674 }
3675
3676 if (CONSP (prop))
3677 /* Skip over `margin LOCATION'. */
3678 if (EQ (XCAR (prop), Qmargin))
3679 {
3680 prop = XCDR (prop);
3681 if (!CONSP (prop))
3682 return 0;
3683
3684 prop = XCDR (prop);
3685 if (!CONSP (prop))
3686 return 0;
3687 }
2311178e 3688
74bd6d65
GM
3689 return CONSP (prop) && EQ (XCAR (prop), string);
3690}
3691
3692
3693/* Return 1 if STRING appears in the `display' property PROP. */
3694
3695static int
3696display_prop_string_p (prop, string)
3697 Lisp_Object prop, string;
3698{
74bd6d65
GM
3699 if (CONSP (prop)
3700 && CONSP (XCAR (prop))
3701 && !EQ (Qmargin, XCAR (XCAR (prop))))
3702 {
3703 /* A list of sub-properties. */
3704 while (CONSP (prop))
3705 {
3706 if (single_display_prop_string_p (XCAR (prop), string))
3707 return 1;
3708 prop = XCDR (prop);
3709 }
3710 }
3711 else if (VECTORP (prop))
3712 {
3713 /* A vector of sub-properties. */
3714 int i;
3715 for (i = 0; i < ASIZE (prop); ++i)
3716 if (single_display_prop_string_p (AREF (prop, i), string))
3717 return 1;
3718 }
3719 else
3720 return single_display_prop_string_p (prop, string);
3721
3722 return 0;
3723}
3724
3725
3726/* Determine from which buffer position in W's buffer STRING comes
3727 from. AROUND_CHARPOS is an approximate position where it could
3728 be from. Value is the buffer position or 0 if it couldn't be
3729 determined.
3730
3731 W's buffer must be current.
3732
3733 This function is necessary because we don't record buffer positions
3734 in glyphs generated from strings (to keep struct glyph small).
3735 This function may only use code that doesn't eval because it is
3736 called asynchronously from note_mouse_highlight. */
3737
3738int
3739string_buffer_position (w, string, around_charpos)
3740 struct window *w;
3741 Lisp_Object string;
3742 int around_charpos;
3743{
74bd6d65
GM
3744 Lisp_Object limit, prop, pos;
3745 const int MAX_DISTANCE = 1000;
3746 int found = 0;
3747
d8731202 3748 pos = make_number (around_charpos);
74bd6d65
GM
3749 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3750 while (!found && !EQ (pos, limit))
3751 {
3752 prop = Fget_char_property (pos, Qdisplay, Qnil);
3753 if (!NILP (prop) && display_prop_string_p (prop, string))
3754 found = 1;
3755 else
134d9283 3756 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
74bd6d65
GM
3757 }
3758
3759 if (!found)
3760 {
d8731202 3761 pos = make_number (around_charpos);
74bd6d65
GM
3762 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3763 while (!found && !EQ (pos, limit))
3764 {
3765 prop = Fget_char_property (pos, Qdisplay, Qnil);
3766 if (!NILP (prop) && display_prop_string_p (prop, string))
3767 found = 1;
3768 else
134d9283
GM
3769 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3770 limit);
74bd6d65
GM
3771 }
3772 }
3773
3774 return found ? XINT (pos) : 0;
3775}
3776
3777
5f5c8ee5 3778\f
260a86a0
KH
3779/***********************************************************************
3780 `composition' property
3781 ***********************************************************************/
3782
3783/* Set up iterator IT from `composition' property at its current
3784 position. Called from handle_stop. */
3785
3786static enum prop_handled
3787handle_composition_prop (it)
3788 struct it *it;
3789{
3790 Lisp_Object prop, string;
3791 int pos, pos_byte, end;
3792 enum prop_handled handled = HANDLED_NORMALLY;
3793
3794 if (STRINGP (it->string))
3795 {
3796 pos = IT_STRING_CHARPOS (*it);
3797 pos_byte = IT_STRING_BYTEPOS (*it);
3798 string = it->string;
3799 }
3800 else
3801 {
3802 pos = IT_CHARPOS (*it);
3803 pos_byte = IT_BYTEPOS (*it);
3804 string = Qnil;
3805 }
3806
3807 /* If there's a valid composition and point is not inside of the
3808 composition (in the case that the composition is from the current
3809 buffer), draw a glyph composed from the composition components. */
3810 if (find_composition (pos, -1, &pos, &end, &prop, string)
3811 && COMPOSITION_VALID_P (pos, end, prop)
3812 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3813 {
3814 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3815
3816 if (id >= 0)
3817 {
3818 it->method = next_element_from_composition;
3819 it->cmp_id = id;
3820 it->cmp_len = COMPOSITION_LENGTH (prop);
3821 /* For a terminal, draw only the first character of the
3822 components. */
3823 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3824 it->len = (STRINGP (it->string)
3825 ? string_char_to_byte (it->string, end)
3826 : CHAR_TO_BYTE (end)) - pos_byte;
3827 it->stop_charpos = end;
3828 handled = HANDLED_RETURN;
3829 }
3830 }
3831
3832 return handled;
3833}
3834
3835
3836\f
5f5c8ee5
GM
3837/***********************************************************************
3838 Overlay strings
3839 ***********************************************************************/
3840
3841/* The following structure is used to record overlay strings for
3842 later sorting in load_overlay_strings. */
3843
3844struct overlay_entry
3845{
2970b9be 3846 Lisp_Object overlay;
5f5c8ee5
GM
3847 Lisp_Object string;
3848 int priority;
3849 int after_string_p;
3850};
3851
3852
3853/* Set up iterator IT from overlay strings at its current position.
3854 Called from handle_stop. */
3855
3856static enum prop_handled
3857handle_overlay_change (it)
3858 struct it *it;
3859{
5a08cbaf 3860 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
2970b9be 3861 return HANDLED_RECOMPUTE_PROPS;
5f5c8ee5 3862 else
2970b9be 3863 return HANDLED_NORMALLY;
5f5c8ee5
GM
3864}
3865
3866
3867/* Set up the next overlay string for delivery by IT, if there is an
3868 overlay string to deliver. Called by set_iterator_to_next when the
3869 end of the current overlay string is reached. If there are more
3870 overlay strings to display, IT->string and
3871 IT->current.overlay_string_index are set appropriately here.
3872 Otherwise IT->string is set to nil. */
2311178e 3873
5f5c8ee5
GM
3874static void
3875next_overlay_string (it)
3876 struct it *it;
3877{
3878 ++it->current.overlay_string_index;
3879 if (it->current.overlay_string_index == it->n_overlay_strings)
3880 {
3881 /* No more overlay strings. Restore IT's settings to what
3882 they were before overlay strings were processed, and
3883 continue to deliver from current_buffer. */
5a08cbaf 3884 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
2311178e 3885
5f5c8ee5
GM
3886 pop_it (it);
3887 xassert (it->stop_charpos >= BEGV
3888 && it->stop_charpos <= it->end_charpos);
3889 it->string = Qnil;
3890 it->current.overlay_string_index = -1;
3891 SET_TEXT_POS (it->current.string_pos, -1, -1);
3892 it->n_overlay_strings = 0;
3893 it->method = next_element_from_buffer;
2970b9be
GM
3894
3895 /* If we're at the end of the buffer, record that we have
3896 processed the overlay strings there already, so that
3897 next_element_from_buffer doesn't try it again. */
3898 if (IT_CHARPOS (*it) >= it->end_charpos)
3899 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf
GM
3900
3901 /* If we have to display `...' for invisible text, set
3902 the iterator up for that. */
3903 if (display_ellipsis_p)
3904 setup_for_ellipsis (it);
5f5c8ee5
GM
3905 }
3906 else
3907 {
3908 /* There are more overlay strings to process. If
3909 IT->current.overlay_string_index has advanced to a position
3910 where we must load IT->overlay_strings with more strings, do
3911 it. */
3912 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2311178e 3913
5f5c8ee5 3914 if (it->current.overlay_string_index && i == 0)
5a08cbaf 3915 load_overlay_strings (it, 0);
5f5c8ee5
GM
3916
3917 /* Initialize IT to deliver display elements from the overlay
3918 string. */
3919 it->string = it->overlay_strings[i];
3920 it->multibyte_p = STRING_MULTIBYTE (it->string);
3921 SET_TEXT_POS (it->current.string_pos, 0, 0);
3922 it->method = next_element_from_string;
3923 it->stop_charpos = 0;
3924 }
2311178e 3925
5f5c8ee5
GM
3926 CHECK_IT (it);
3927}
3928
3929
3930/* Compare two overlay_entry structures E1 and E2. Used as a
3931 comparison function for qsort in load_overlay_strings. Overlay
3932 strings for the same position are sorted so that
3933
2970b9be
GM
3934 1. All after-strings come in front of before-strings, except
3935 when they come from the same overlay.
2311178e 3936
5f5c8ee5
GM
3937 2. Within after-strings, strings are sorted so that overlay strings
3938 from overlays with higher priorities come first.
3939
3940 2. Within before-strings, strings are sorted so that overlay
3941 strings from overlays with higher priorities come last.
3942
3943 Value is analogous to strcmp. */
3944
2311178e 3945
5f5c8ee5
GM
3946static int
3947compare_overlay_entries (e1, e2)
3948 void *e1, *e2;
3949{
3950 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3951 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3952 int result;
3953
3954 if (entry1->after_string_p != entry2->after_string_p)
2970b9be
GM
3955 {
3956 /* Let after-strings appear in front of before-strings if
3957 they come from different overlays. */
3958 if (EQ (entry1->overlay, entry2->overlay))
3959 result = entry1->after_string_p ? 1 : -1;
3960 else
3961 result = entry1->after_string_p ? -1 : 1;
3962 }
5f5c8ee5
GM
3963 else if (entry1->after_string_p)
3964 /* After-strings sorted in order of decreasing priority. */
3965 result = entry2->priority - entry1->priority;
3966 else
3967 /* Before-strings sorted in order of increasing priority. */
3968 result = entry1->priority - entry2->priority;
3969
3970 return result;
3971}
3972
3973
3974/* Load the vector IT->overlay_strings with overlay strings from IT's
5a08cbaf
GM
3975 current buffer position, or from CHARPOS if that is > 0. Set
3976 IT->n_overlays to the total number of overlay strings found.
5f5c8ee5
GM
3977
3978 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3979 a time. On entry into load_overlay_strings,
3980 IT->current.overlay_string_index gives the number of overlay
3981 strings that have already been loaded by previous calls to this
3982 function.
3983
2970b9be
GM
3984 IT->add_overlay_start contains an additional overlay start
3985 position to consider for taking overlay strings from, if non-zero.
3986 This position comes into play when the overlay has an `invisible'
3987 property, and both before and after-strings. When we've skipped to
3988 the end of the overlay, because of its `invisible' property, we
3989 nevertheless want its before-string to appear.
3990 IT->add_overlay_start will contain the overlay start position
3991 in this case.
3992
5f5c8ee5
GM
3993 Overlay strings are sorted so that after-string strings come in
3994 front of before-string strings. Within before and after-strings,
3995 strings are sorted by overlay priority. See also function
3996 compare_overlay_entries. */
2311178e 3997
5f5c8ee5 3998static void
5a08cbaf 3999load_overlay_strings (it, charpos)
5f5c8ee5 4000 struct it *it;
5a08cbaf 4001 int charpos;
5f5c8ee5
GM
4002{
4003 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
b7253a3e
SM
4004 Lisp_Object overlay, window, str, invisible;
4005 struct Lisp_Overlay *ov;
5f5c8ee5
GM
4006 int start, end;
4007 int size = 20;
cafafe0b 4008 int n = 0, i, j, invis_p;
5f5c8ee5
GM
4009 struct overlay_entry *entries
4010 = (struct overlay_entry *) alloca (size * sizeof *entries);
5a08cbaf
GM
4011
4012 if (charpos <= 0)
4013 charpos = IT_CHARPOS (*it);
5f5c8ee5
GM
4014
4015 /* Append the overlay string STRING of overlay OVERLAY to vector
4016 `entries' which has size `size' and currently contains `n'
4017 elements. AFTER_P non-zero means STRING is an after-string of
4018 OVERLAY. */
4019#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4020 do \
4021 { \
4022 Lisp_Object priority; \
4023 \
4024 if (n == size) \
4025 { \
4026 int new_size = 2 * size; \
4027 struct overlay_entry *old = entries; \
4028 entries = \
4029 (struct overlay_entry *) alloca (new_size \
4030 * sizeof *entries); \
4031 bcopy (old, entries, size * sizeof *entries); \
4032 size = new_size; \
4033 } \
4034 \
4035 entries[n].string = (STRING); \
2970b9be 4036 entries[n].overlay = (OVERLAY); \
5f5c8ee5 4037 priority = Foverlay_get ((OVERLAY), Qpriority); \
2970b9be 4038 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5f5c8ee5
GM
4039 entries[n].after_string_p = (AFTER_P); \
4040 ++n; \
4041 } \
4042 while (0)
4043
4044 /* Process overlay before the overlay center. */
b7253a3e 4045 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5f5c8ee5 4046 {
b7253a3e 4047 XSETMISC (overlay, ov);
5f5c8ee5
GM
4048 xassert (OVERLAYP (overlay));
4049 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4050 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2311178e 4051
cafafe0b 4052 if (end < charpos)
5f5c8ee5
GM
4053 break;
4054
4055 /* Skip this overlay if it doesn't start or end at IT's current
4056 position. */
cafafe0b 4057 if (end != charpos && start != charpos)
5f5c8ee5 4058 continue;
2311178e 4059
5f5c8ee5
GM
4060 /* Skip this overlay if it doesn't apply to IT->w. */
4061 window = Foverlay_get (overlay, Qwindow);
4062 if (WINDOWP (window) && XWINDOW (window) != it->w)
4063 continue;
4064
cafafe0b
GM
4065 /* If the text ``under'' the overlay is invisible, both before-
4066 and after-strings from this overlay are visible; start and
4067 end position are indistinguishable. */
4068 invisible = Foverlay_get (overlay, Qinvisible);
4069 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4070
5f5c8ee5 4071 /* If overlay has a non-empty before-string, record it. */
cafafe0b 4072 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 4073 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 4074 && SCHARS (str))
5f5c8ee5 4075 RECORD_OVERLAY_STRING (overlay, str, 0);
2311178e 4076
5f5c8ee5 4077 /* If overlay has a non-empty after-string, record it. */
cafafe0b 4078 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 4079 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 4080 && SCHARS (str))
5f5c8ee5
GM
4081 RECORD_OVERLAY_STRING (overlay, str, 1);
4082 }
2311178e 4083
5f5c8ee5 4084 /* Process overlays after the overlay center. */
b7253a3e 4085 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5f5c8ee5 4086 {
b7253a3e 4087 XSETMISC (overlay, ov);
5f5c8ee5
GM
4088 xassert (OVERLAYP (overlay));
4089 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4090 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4091
cafafe0b 4092 if (start > charpos)
5f5c8ee5 4093 break;
2311178e 4094
5f5c8ee5
GM
4095 /* Skip this overlay if it doesn't start or end at IT's current
4096 position. */
cafafe0b 4097 if (end != charpos && start != charpos)
5f5c8ee5
GM
4098 continue;
4099
4100 /* Skip this overlay if it doesn't apply to IT->w. */
4101 window = Foverlay_get (overlay, Qwindow);
4102 if (WINDOWP (window) && XWINDOW (window) != it->w)
4103 continue;
2311178e 4104
cafafe0b
GM
4105 /* If the text ``under'' the overlay is invisible, it has a zero
4106 dimension, and both before- and after-strings apply. */
4107 invisible = Foverlay_get (overlay, Qinvisible);
4108 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4109
5f5c8ee5 4110 /* If overlay has a non-empty before-string, record it. */
cafafe0b 4111 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 4112 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 4113 && SCHARS (str))
5f5c8ee5 4114 RECORD_OVERLAY_STRING (overlay, str, 0);
2311178e 4115
5f5c8ee5 4116 /* If overlay has a non-empty after-string, record it. */
cafafe0b 4117 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 4118 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 4119 && SCHARS (str))
5f5c8ee5
GM
4120 RECORD_OVERLAY_STRING (overlay, str, 1);
4121 }
4122
4123#undef RECORD_OVERLAY_STRING
2311178e 4124
5f5c8ee5 4125 /* Sort entries. */
cafafe0b 4126 if (n > 1)
2970b9be 4127 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5f5c8ee5
GM
4128
4129 /* Record the total number of strings to process. */
4130 it->n_overlay_strings = n;
4131
4132 /* IT->current.overlay_string_index is the number of overlay strings
4133 that have already been consumed by IT. Copy some of the
4134 remaining overlay strings to IT->overlay_strings. */
4135 i = 0;
4136 j = it->current.overlay_string_index;
4137 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4138 it->overlay_strings[i++] = entries[j++].string;
2970b9be 4139
5f5c8ee5
GM
4140 CHECK_IT (it);
4141}
4142
4143
4144/* Get the first chunk of overlay strings at IT's current buffer
5a08cbaf
GM
4145 position, or at CHARPOS if that is > 0. Value is non-zero if at
4146 least one overlay string was found. */
5f5c8ee5
GM
4147
4148static int
5a08cbaf 4149get_overlay_strings (it, charpos)
5f5c8ee5 4150 struct it *it;
5a08cbaf 4151 int charpos;
5f5c8ee5
GM
4152{
4153 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4154 process. This fills IT->overlay_strings with strings, and sets
4155 IT->n_overlay_strings to the total number of strings to process.
4156 IT->pos.overlay_string_index has to be set temporarily to zero
4157 because load_overlay_strings needs this; it must be set to -1
4158 when no overlay strings are found because a zero value would
4159 indicate a position in the first overlay string. */
4160 it->current.overlay_string_index = 0;
5a08cbaf 4161 load_overlay_strings (it, charpos);
5f5c8ee5
GM
4162
4163 /* If we found overlay strings, set up IT to deliver display
4164 elements from the first one. Otherwise set up IT to deliver
4165 from current_buffer. */
4166 if (it->n_overlay_strings)
4167 {
4168 /* Make sure we know settings in current_buffer, so that we can
4169 restore meaningful values when we're done with the overlay
4170 strings. */
4171 compute_stop_pos (it);
4172 xassert (it->face_id >= 0);
2311178e 4173
5f5c8ee5
GM
4174 /* Save IT's settings. They are restored after all overlay
4175 strings have been processed. */
4176 xassert (it->sp == 0);
4177 push_it (it);
4178
4179 /* Set up IT to deliver display elements from the first overlay
4180 string. */
4181 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5f5c8ee5 4182 it->string = it->overlay_strings[0];
b2046df8 4183 it->stop_charpos = 0;
5f5c8ee5 4184 xassert (STRINGP (it->string));
2051c264 4185 it->end_charpos = SCHARS (it->string);
d5db4077 4186 it->multibyte_p = STRING_MULTIBYTE (it->string);
5f5c8ee5
GM
4187 it->method = next_element_from_string;
4188 }
4189 else
4190 {
4191 it->string = Qnil;
4192 it->current.overlay_string_index = -1;
4193 it->method = next_element_from_buffer;
4194 }
4195
4196 CHECK_IT (it);
4197
4198 /* Value is non-zero if we found at least one overlay string. */
4199 return STRINGP (it->string);
4200}
4201
4202
4203\f
4204/***********************************************************************
4205 Saving and restoring state
4206 ***********************************************************************/
4207
4208/* Save current settings of IT on IT->stack. Called, for example,
4209 before setting up IT for an overlay string, to be able to restore
4210 IT's settings to what they were after the overlay string has been
4211 processed. */
4212
4213static void
4214push_it (it)
4215 struct it *it;
4216{
4217 struct iterator_stack_entry *p;
2311178e 4218
5f5c8ee5
GM
4219 xassert (it->sp < 2);
4220 p = it->stack + it->sp;
4221
4222 p->stop_charpos = it->stop_charpos;
4223 xassert (it->face_id >= 0);
4224 p->face_id = it->face_id;
4225 p->string = it->string;
4226 p->pos = it->current;
4227 p->end_charpos = it->end_charpos;
4228 p->string_nchars = it->string_nchars;
4229 p->area = it->area;
4230 p->multibyte_p = it->multibyte_p;
4231 p->space_width = it->space_width;
4232 p->font_height = it->font_height;
4233 p->voffset = it->voffset;
4234 p->string_from_display_prop_p = it->string_from_display_prop_p;
5a08cbaf 4235 p->display_ellipsis_p = 0;
5f5c8ee5
GM
4236 ++it->sp;
4237}
4238
4239
4240/* Restore IT's settings from IT->stack. Called, for example, when no
4241 more overlay strings must be processed, and we return to delivering
4242 display elements from a buffer, or when the end of a string from a
4243 `display' property is reached and we return to delivering display
4244 elements from an overlay string, or from a buffer. */
4245
4246static void
4247pop_it (it)
4248 struct it *it;
4249{
4250 struct iterator_stack_entry *p;
2311178e 4251
5f5c8ee5
GM
4252 xassert (it->sp > 0);
4253 --it->sp;
4254 p = it->stack + it->sp;
4255 it->stop_charpos = p->stop_charpos;
4256 it->face_id = p->face_id;
4257 it->string = p->string;
4258 it->current = p->pos;
4259 it->end_charpos = p->end_charpos;
4260 it->string_nchars = p->string_nchars;
4261 it->area = p->area;
4262 it->multibyte_p = p->multibyte_p;
4263 it->space_width = p->space_width;
4264 it->font_height = p->font_height;
4265 it->voffset = p->voffset;
4266 it->string_from_display_prop_p = p->string_from_display_prop_p;
4267}
4268
4269
4270\f
4271/***********************************************************************
4272 Moving over lines
4273 ***********************************************************************/
4274
4275/* Set IT's current position to the previous line start. */
4276
4277static void
4278back_to_previous_line_start (it)
4279 struct it *it;
4280{
4281 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4282 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4283}
4284
4285
cafafe0b 4286/* Move IT to the next line start.
2311178e 4287
cafafe0b
GM
4288 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4289 we skipped over part of the text (as opposed to moving the iterator
4290 continuously over the text). Otherwise, don't change the value
4291 of *SKIPPED_P.
2311178e 4292
cafafe0b
GM
4293 Newlines may come from buffer text, overlay strings, or strings
4294 displayed via the `display' property. That's the reason we can't
0fd37545
GM
4295 simply use find_next_newline_no_quit.
4296
4297 Note that this function may not skip over invisible text that is so
4298 because of text properties and immediately follows a newline. If
4299 it would, function reseat_at_next_visible_line_start, when called
4300 from set_iterator_to_next, would effectively make invisible
4301 characters following a newline part of the wrong glyph row, which
4302 leads to wrong cursor motion. */
5f5c8ee5 4303
cafafe0b
GM
4304static int
4305forward_to_next_line_start (it, skipped_p)
5f5c8ee5 4306 struct it *it;
cafafe0b 4307 int *skipped_p;
5f5c8ee5 4308{
54918e2b 4309 int old_selective, newline_found_p, n;
cafafe0b
GM
4310 const int MAX_NEWLINE_DISTANCE = 500;
4311
0fd37545
GM
4312 /* If already on a newline, just consume it to avoid unintended
4313 skipping over invisible text below. */
51695746
GM
4314 if (it->what == IT_CHARACTER
4315 && it->c == '\n'
4316 && CHARPOS (it->position) == IT_CHARPOS (*it))
0fd37545
GM
4317 {
4318 set_iterator_to_next (it, 0);
a77dc1ec 4319 it->c = 0;
0fd37545
GM
4320 return 1;
4321 }
4322
54918e2b 4323 /* Don't handle selective display in the following. It's (a)
0fd37545
GM
4324 unnecessary because it's done by the caller, and (b) leads to an
4325 infinite recursion because next_element_from_ellipsis indirectly
4326 calls this function. */
54918e2b
GM
4327 old_selective = it->selective;
4328 it->selective = 0;
4329
cafafe0b
GM
4330 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4331 from buffer text. */
3aec8722
GM
4332 for (n = newline_found_p = 0;
4333 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4334 n += STRINGP (it->string) ? 0 : 1)
cafafe0b 4335 {
8692ca92 4336 if (!get_next_display_element (it))
d43be70c 4337 return 0;
d02f1cb8 4338 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
cafafe0b 4339 set_iterator_to_next (it, 0);
cafafe0b
GM
4340 }
4341
4342 /* If we didn't find a newline near enough, see if we can use a
4343 short-cut. */
db0bb807 4344 if (!newline_found_p)
cafafe0b
GM
4345 {
4346 int start = IT_CHARPOS (*it);
4347 int limit = find_next_newline_no_quit (start, 1);
4348 Lisp_Object pos;
4349
4350 xassert (!STRINGP (it->string));
4351
4352 /* If there isn't any `display' property in sight, and no
4353 overlays, we can just use the position of the newline in
4354 buffer text. */
4355 if (it->stop_charpos >= limit
4356 || ((pos = Fnext_single_property_change (make_number (start),
4357 Qdisplay,
4358 Qnil, make_number (limit)),
4359 NILP (pos))
4360 && next_overlay_change (start) == ZV))
4361 {
4362 IT_CHARPOS (*it) = limit;
4363 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4364 *skipped_p = newline_found_p = 1;
4365 }
4366 else
4367 {
4368 while (get_next_display_element (it)
4369 && !newline_found_p)
4370 {
4371 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4372 set_iterator_to_next (it, 0);
4373 }
4374 }
4375 }
4376
54918e2b 4377 it->selective = old_selective;
cafafe0b 4378 return newline_found_p;
5f5c8ee5
GM
4379}
4380
4381
4382/* Set IT's current position to the previous visible line start. Skip
4383 invisible text that is so either due to text properties or due to
4384 selective display. Caution: this does not change IT->current_x and
4385 IT->hpos. */
4386
4387static void
4388back_to_previous_visible_line_start (it)
4389 struct it *it;
4390{
4391 int visible_p = 0;
4392
4393 /* Go back one newline if not on BEGV already. */
4394 if (IT_CHARPOS (*it) > BEGV)
4395 back_to_previous_line_start (it);
4396
4397 /* Move over lines that are invisible because of selective display
4398 or text properties. */
4399 while (IT_CHARPOS (*it) > BEGV
4400 && !visible_p)
4401 {
4402 visible_p = 1;
4403
4404 /* If selective > 0, then lines indented more than that values
4405 are invisible. */
4406 if (it->selective > 0
4407 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
a67e162b 4408 (double) it->selective)) /* iftc */
5f5c8ee5 4409 visible_p = 0;
2311178e 4410 else
5f5c8ee5
GM
4411 {
4412 Lisp_Object prop;
4413
6fc556fd
KR
4414 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4415 Qinvisible, it->window);
5f5c8ee5
GM
4416 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4417 visible_p = 0;
4418 }
5f5c8ee5
GM
4419
4420 /* Back one more newline if the current one is invisible. */
4421 if (!visible_p)
4422 back_to_previous_line_start (it);
4423 }
4424
4425 xassert (IT_CHARPOS (*it) >= BEGV);
4426 xassert (IT_CHARPOS (*it) == BEGV
4427 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4428 CHECK_IT (it);
4429}
4430
4431
4432/* Reseat iterator IT at the previous visible line start. Skip
4433 invisible text that is so either due to text properties or due to
4434 selective display. At the end, update IT's overlay information,
4435 face information etc. */
4436
4437static void
4438reseat_at_previous_visible_line_start (it)
4439 struct it *it;
4440{
4441 back_to_previous_visible_line_start (it);
4442 reseat (it, it->current.pos, 1);
4443 CHECK_IT (it);
4444}
4445
4446
4447/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
4448 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4449 preceding the line start. Skip over invisible text that is so
4450 because of selective display. Compute faces, overlays etc at the
4451 new position. Note that this function does not skip over text that
4452 is invisible because of text properties. */
5f5c8ee5
GM
4453
4454static void
312246d1 4455reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 4456 struct it *it;
312246d1 4457 int on_newline_p;
5f5c8ee5 4458{
cafafe0b
GM
4459 int newline_found_p, skipped_p = 0;
4460
4461 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4462
4463 /* Skip over lines that are invisible because they are indented
4464 more than the value of IT->selective. */
4465 if (it->selective > 0)
4466 while (IT_CHARPOS (*it) < ZV
a77dc1ec 4467 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
a67e162b 4468 (double) it->selective)) /* iftc */
a77dc1ec
GM
4469 {
4470 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4471 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4472 }
cafafe0b
GM
4473
4474 /* Position on the newline if that's what's requested. */
4475 if (on_newline_p && newline_found_p)
5f5c8ee5 4476 {
cafafe0b 4477 if (STRINGP (it->string))
5f5c8ee5 4478 {
cafafe0b
GM
4479 if (IT_STRING_CHARPOS (*it) > 0)
4480 {
4481 --IT_STRING_CHARPOS (*it);
4482 --IT_STRING_BYTEPOS (*it);
4483 }
5f5c8ee5 4484 }
cafafe0b 4485 else if (IT_CHARPOS (*it) > BEGV)
312246d1
GM
4486 {
4487 --IT_CHARPOS (*it);
cafafe0b
GM
4488 --IT_BYTEPOS (*it);
4489 reseat (it, it->current.pos, 0);
312246d1 4490 }
5f5c8ee5 4491 }
cafafe0b
GM
4492 else if (skipped_p)
4493 reseat (it, it->current.pos, 0);
2311178e 4494
5f5c8ee5
GM
4495 CHECK_IT (it);
4496}
4497
4498
4499\f
4500/***********************************************************************
4501 Changing an iterator's position
4502***********************************************************************/
4503
4504/* Change IT's current position to POS in current_buffer. If FORCE_P
4505 is non-zero, always check for text properties at the new position.
4506 Otherwise, text properties are only looked up if POS >=
4507 IT->check_charpos of a property. */
4508
4509static void
4510reseat (it, pos, force_p)
4511 struct it *it;
4512 struct text_pos pos;
4513 int force_p;
4514{
4515 int original_pos = IT_CHARPOS (*it);
4516
4517 reseat_1 (it, pos, 0);
4518
4519 /* Determine where to check text properties. Avoid doing it
4520 where possible because text property lookup is very expensive. */
4521 if (force_p
4522 || CHARPOS (pos) > it->stop_charpos
4523 || CHARPOS (pos) < original_pos)
4524 handle_stop (it);
4525
4526 CHECK_IT (it);
4527}
4528
4529
4530/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4531 IT->stop_pos to POS, also. */
4532
4533static void
4534reseat_1 (it, pos, set_stop_p)
4535 struct it *it;
4536 struct text_pos pos;
4537 int set_stop_p;
4538{
4539 /* Don't call this function when scanning a C string. */
4540 xassert (it->s == NULL);
4541
4542 /* POS must be a reasonable value. */
4543 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4544
4545 it->current.pos = it->position = pos;
4546 XSETBUFFER (it->object, current_buffer);
78c663d8 4547 it->end_charpos = ZV;
5f5c8ee5
GM
4548 it->dpvec = NULL;
4549 it->current.dpvec_index = -1;
4550 it->current.overlay_string_index = -1;
4551 IT_STRING_CHARPOS (*it) = -1;
4552 IT_STRING_BYTEPOS (*it) = -1;
4553 it->string = Qnil;
4554 it->method = next_element_from_buffer;
5905025c
RS
4555 /* RMS: I added this to fix a bug in move_it_vertically_backward
4556 where it->area continued to relate to the starting point
4557 for the backward motion. Bug report from
4558 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4559 However, I am not sure whether reseat still does the right thing
4560 in general after this change. */
4561 it->area = TEXT_AREA;
06fd3792 4562 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5f5c8ee5 4563 it->sp = 0;
4aad61f8 4564 it->face_before_selective_p = 0;
5f5c8ee5
GM
4565
4566 if (set_stop_p)
4567 it->stop_charpos = CHARPOS (pos);
4568}
4569
4570
4571/* Set up IT for displaying a string, starting at CHARPOS in window W.
4572 If S is non-null, it is a C string to iterate over. Otherwise,
4573 STRING gives a Lisp string to iterate over.
2311178e 4574
5f5c8ee5
GM
4575 If PRECISION > 0, don't return more then PRECISION number of
4576 characters from the string.
4577
4578 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4579 characters have been returned. FIELD_WIDTH < 0 means an infinite
4580 field width.
4581
4582 MULTIBYTE = 0 means disable processing of multibyte characters,
4583 MULTIBYTE > 0 means enable it,
4584 MULTIBYTE < 0 means use IT->multibyte_p.
4585
4586 IT must be initialized via a prior call to init_iterator before
4587 calling this function. */
4588
4589static void
4590reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4591 struct it *it;
4592 unsigned char *s;
4593 Lisp_Object string;
4594 int charpos;
4595 int precision, field_width, multibyte;
4596{
4597 /* No region in strings. */
4598 it->region_beg_charpos = it->region_end_charpos = -1;
4599
4600 /* No text property checks performed by default, but see below. */
4601 it->stop_charpos = -1;
4602
4603 /* Set iterator position and end position. */
4604 bzero (&it->current, sizeof it->current);
4605 it->current.overlay_string_index = -1;
4606 it->current.dpvec_index = -1;
5f5c8ee5 4607 xassert (charpos >= 0);
2311178e 4608
e719f5ae
GM
4609 /* If STRING is specified, use its multibyteness, otherwise use the
4610 setting of MULTIBYTE, if specified. */
cabf45da 4611 if (multibyte >= 0)
5f5c8ee5 4612 it->multibyte_p = multibyte > 0;
2311178e 4613
5f5c8ee5
GM
4614 if (s == NULL)
4615 {
4616 xassert (STRINGP (string));
4617 it->string = string;
4618 it->s = NULL;
2051c264 4619 it->end_charpos = it->string_nchars = SCHARS (string);
5f5c8ee5
GM
4620 it->method = next_element_from_string;
4621 it->current.string_pos = string_pos (charpos, string);
4622 }
4623 else
4624 {
4625 it->s = s;
4626 it->string = Qnil;
4627
4628 /* Note that we use IT->current.pos, not it->current.string_pos,
4629 for displaying C strings. */
4630 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4631 if (it->multibyte_p)
4632 {
4633 it->current.pos = c_string_pos (charpos, s, 1);
4634 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4635 }
4636 else
4637 {
4638 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4639 it->end_charpos = it->string_nchars = strlen (s);
4640 }
2311178e 4641
5f5c8ee5
GM
4642 it->method = next_element_from_c_string;
4643 }
4644
4645 /* PRECISION > 0 means don't return more than PRECISION characters
4646 from the string. */
4647 if (precision > 0 && it->end_charpos - charpos > precision)
4648 it->end_charpos = it->string_nchars = charpos + precision;
4649
4650 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4651 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4652 FIELD_WIDTH < 0 means infinite field width. This is useful for
4653 padding with `-' at the end of a mode line. */
4654 if (field_width < 0)
4655 field_width = INFINITY;
4656 if (field_width > it->end_charpos - charpos)
4657 it->end_charpos = charpos + field_width;
4658
4659 /* Use the standard display table for displaying strings. */
4660 if (DISP_TABLE_P (Vstandard_display_table))
4661 it->dp = XCHAR_TABLE (Vstandard_display_table);
4662
4663 it->stop_charpos = charpos;
4664 CHECK_IT (it);
4665}
4666
4667
4668\f
4669/***********************************************************************
4670 Iteration
4671 ***********************************************************************/
4672
4673/* Load IT's display element fields with information about the next
4674 display element from the current position of IT. Value is zero if
4675 end of buffer (or C string) is reached. */
4676
4677int
4678get_next_display_element (it)
4679 struct it *it;
4680{
7d0393cf 4681 /* Non-zero means that we found a display element. Zero means that
5f5c8ee5
GM
4682 we hit the end of what we iterate over. Performance note: the
4683 function pointer `method' used here turns out to be faster than
4684 using a sequence of if-statements. */
4685 int success_p = (*it->method) (it);
5f5c8ee5
GM
4686
4687 if (it->what == IT_CHARACTER)
4688 {
4689 /* Map via display table or translate control characters.
4690 IT->c, IT->len etc. have been set to the next character by
4691 the function call above. If we have a display table, and it
4692 contains an entry for IT->c, translate it. Don't do this if
4693 IT->c itself comes from a display table, otherwise we could
4694 end up in an infinite recursion. (An alternative could be to
4695 count the recursion depth of this function and signal an
4696 error when a certain maximum depth is reached.) Is it worth
4697 it? */
4698 if (success_p && it->dpvec == NULL)
4699 {
4700 Lisp_Object dv;
4701
4702 if (it->dp
4703 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4704 VECTORP (dv)))
4705 {
4706 struct Lisp_Vector *v = XVECTOR (dv);
4707
4708 /* Return the first character from the display table
4709 entry, if not empty. If empty, don't display the
4710 current character. */
4711 if (v->size)
4712 {
4713 it->dpvec_char_len = it->len;
4714 it->dpvec = v->contents;
4715 it->dpend = v->contents + v->size;
4716 it->current.dpvec_index = 0;
4717 it->method = next_element_from_display_vector;
7eee47cc
GM
4718 success_p = get_next_display_element (it);
4719 }
4720 else
4721 {
4722 set_iterator_to_next (it, 0);
4723 success_p = get_next_display_element (it);
5f5c8ee5 4724 }
5f5c8ee5
GM
4725 }
4726
4727 /* Translate control characters into `\003' or `^C' form.
4728 Control characters coming from a display table entry are
4729 currently not translated because we use IT->dpvec to hold
4730 the translation. This could easily be changed but I
197516c2
KH
4731 don't believe that it is worth doing.
4732
fa831cf8
KH
4733 If it->multibyte_p is nonzero, eight-bit characters and
4734 non-printable multibyte characters are also translated to
4735 octal form.
4736
4737 If it->multibyte_p is zero, eight-bit characters that
4738 don't have corresponding multibyte char code are also
4739 translated to octal form. */
ba197fe6 4740 else if ((it->c < ' '
5f5c8ee5 4741 && (it->area != TEXT_AREA
c6e89d6c 4742 || (it->c != '\n' && it->c != '\t')))
fa831cf8
KH
4743 || (it->multibyte_p
4744 ? ((it->c >= 127
4745 && it->len == 1)
4746 || !CHAR_PRINTABLE_P (it->c))
ba197fe6 4747 : (it->c >= 127
fa831cf8 4748 && it->c == unibyte_char_to_multibyte (it->c))))
5f5c8ee5
GM
4749 {
4750 /* IT->c is a control character which must be displayed
4751 either as '\003' or as `^C' where the '\\' and '^'
4752 can be defined in the display table. Fill
4753 IT->ctl_chars with glyphs for what we have to
4754 display. Then, set IT->dpvec to these glyphs. */
4755 GLYPH g;
4756
54c85a23 4757 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
4758 {
4759 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4760 if (it->dp
4761 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4762 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4763 g = XINT (DISP_CTRL_GLYPH (it->dp));
4764 else
4765 g = FAST_MAKE_GLYPH ('^', 0);
4766 XSETINT (it->ctl_chars[0], g);
4767
4768 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4769 XSETINT (it->ctl_chars[1], g);
4770
4771 /* Set up IT->dpvec and return first character from it. */
4772 it->dpvec_char_len = it->len;
4773 it->dpvec = it->ctl_chars;
4774 it->dpend = it->dpvec + 2;
4775 it->current.dpvec_index = 0;
4776 it->method = next_element_from_display_vector;
4777 get_next_display_element (it);
4778 }
4779 else
4780 {
260a86a0 4781 unsigned char str[MAX_MULTIBYTE_LENGTH];
c5924f47 4782 int len;
197516c2
KH
4783 int i;
4784 GLYPH escape_glyph;
4785
5f5c8ee5
GM
4786 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4787 if (it->dp
4788 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4789 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
197516c2 4790 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5f5c8ee5 4791 else
197516c2
KH
4792 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4793
c5924f47
KH
4794 if (SINGLE_BYTE_CHAR_P (it->c))
4795 str[0] = it->c, len = 1;
4796 else
ea9dd091
GM
4797 {
4798 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4799 if (len < 0)
4800 {
4801 /* It's an invalid character, which
4802 shouldn't happen actually, but due to
4803 bugs it may happen. Let's print the char
4804 as is, there's not much meaningful we can
4805 do with it. */
4806 str[0] = it->c;
4807 str[1] = it->c >> 8;
4808 str[2] = it->c >> 16;
4809 str[3] = it->c >> 24;
4810 len = 4;
4811 }
4812 }
c5924f47 4813
197516c2
KH
4814 for (i = 0; i < len; i++)
4815 {
4816 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4817 /* Insert three more glyphs into IT->ctl_chars for
4818 the octal display of the character. */
2311178e 4819 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
197516c2 4820 XSETINT (it->ctl_chars[i * 4 + 1], g);
2311178e 4821 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
197516c2 4822 XSETINT (it->ctl_chars[i * 4 + 2], g);
2311178e 4823 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
197516c2
KH
4824 XSETINT (it->ctl_chars[i * 4 + 3], g);
4825 }
5f5c8ee5
GM
4826
4827 /* Set up IT->dpvec and return the first character
4828 from it. */
4829 it->dpvec_char_len = it->len;
4830 it->dpvec = it->ctl_chars;
197516c2 4831 it->dpend = it->dpvec + len * 4;
5f5c8ee5
GM
4832 it->current.dpvec_index = 0;
4833 it->method = next_element_from_display_vector;
4834 get_next_display_element (it);
4835 }
4836 }
4837 }
4838
980806b6
KH
4839 /* Adjust face id for a multibyte character. There are no
4840 multibyte character in unibyte text. */
5f5c8ee5
GM
4841 if (it->multibyte_p
4842 && success_p
980806b6 4843 && FRAME_WINDOW_P (it->f))
5f5c8ee5 4844 {
980806b6
KH
4845 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4846 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5f5c8ee5
GM
4847 }
4848 }
4849
4850 /* Is this character the last one of a run of characters with
4851 box? If yes, set IT->end_of_box_run_p to 1. */
4852 if (it->face_box_p
4853 && it->s == NULL)
4854 {
4855 int face_id;
4856 struct face *face;
4857
4858 it->end_of_box_run_p
4859 = ((face_id = face_after_it_pos (it),
4860 face_id != it->face_id)
4861 && (face = FACE_FROM_ID (it->f, face_id),
4862 face->box == FACE_NO_BOX));
4863 }
4864
4865 /* Value is 0 if end of buffer or string reached. */
4866 return success_p;
4867}
4868
4869
4870/* Move IT to the next display element.
4871
cafafe0b
GM
4872 RESEAT_P non-zero means if called on a newline in buffer text,
4873 skip to the next visible line start.
4874
5f5c8ee5
GM
4875 Functions get_next_display_element and set_iterator_to_next are
4876 separate because I find this arrangement easier to handle than a
4877 get_next_display_element function that also increments IT's
4878 position. The way it is we can first look at an iterator's current
4879 display element, decide whether it fits on a line, and if it does,
4880 increment the iterator position. The other way around we probably
4881 would either need a flag indicating whether the iterator has to be
4882 incremented the next time, or we would have to implement a
4883 decrement position function which would not be easy to write. */
4884
4885void
cafafe0b 4886set_iterator_to_next (it, reseat_p)
5f5c8ee5 4887 struct it *it;
cafafe0b 4888 int reseat_p;
5f5c8ee5 4889{
483de32b
GM
4890 /* Reset flags indicating start and end of a sequence of characters
4891 with box. Reset them at the start of this function because
4892 moving the iterator to a new position might set them. */
4893 it->start_of_box_run_p = it->end_of_box_run_p = 0;
2311178e 4894
5f5c8ee5
GM
4895 if (it->method == next_element_from_buffer)
4896 {
4897 /* The current display element of IT is a character from
4898 current_buffer. Advance in the buffer, and maybe skip over
4899 invisible lines that are so because of selective display. */
cafafe0b 4900 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
312246d1 4901 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4902 else
4903 {
4904 xassert (it->len != 0);
4905 IT_BYTEPOS (*it) += it->len;
4906 IT_CHARPOS (*it) += 1;
4907 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4908 }
4909 }
260a86a0
KH
4910 else if (it->method == next_element_from_composition)
4911 {
4912 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4913 if (STRINGP (it->string))
4914 {
4915 IT_STRING_BYTEPOS (*it) += it->len;
4916 IT_STRING_CHARPOS (*it) += it->cmp_len;
4917 it->method = next_element_from_string;
4918 goto consider_string_end;
4919 }
4920 else
4921 {
4922 IT_BYTEPOS (*it) += it->len;
4923 IT_CHARPOS (*it) += it->cmp_len;
4924 it->method = next_element_from_buffer;
4925 }
4926 }
5f5c8ee5
GM
4927 else if (it->method == next_element_from_c_string)
4928 {
4929 /* Current display element of IT is from a C string. */
4930 IT_BYTEPOS (*it) += it->len;
4931 IT_CHARPOS (*it) += 1;
4932 }
4933 else if (it->method == next_element_from_display_vector)
4934 {
4935 /* Current display element of IT is from a display table entry.
4936 Advance in the display table definition. Reset it to null if
4937 end reached, and continue with characters from buffers/
4938 strings. */
4939 ++it->current.dpvec_index;
286bcbc9 4940
980806b6
KH
4941 /* Restore face of the iterator to what they were before the
4942 display vector entry (these entries may contain faces). */
5f5c8ee5 4943 it->face_id = it->saved_face_id;
2311178e 4944
5f5c8ee5
GM
4945 if (it->dpvec + it->current.dpvec_index == it->dpend)
4946 {
4947 if (it->s)
4948 it->method = next_element_from_c_string;
4949 else if (STRINGP (it->string))
4950 it->method = next_element_from_string;
4951 else
4952 it->method = next_element_from_buffer;
4953
4954 it->dpvec = NULL;
4955 it->current.dpvec_index = -1;
4956
312246d1
GM
4957 /* Skip over characters which were displayed via IT->dpvec. */
4958 if (it->dpvec_char_len < 0)
4959 reseat_at_next_visible_line_start (it, 1);
4960 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
4961 {
4962 it->len = it->dpvec_char_len;
cafafe0b 4963 set_iterator_to_next (it, reseat_p);
5f5c8ee5
GM
4964 }
4965 }
4966 }
4967 else if (it->method == next_element_from_string)
4968 {
4969 /* Current display element is a character from a Lisp string. */
4970 xassert (it->s == NULL && STRINGP (it->string));
4971 IT_STRING_BYTEPOS (*it) += it->len;
4972 IT_STRING_CHARPOS (*it) += 1;
2311178e 4973
5f5c8ee5
GM
4974 consider_string_end:
4975
4976 if (it->current.overlay_string_index >= 0)
4977 {
4978 /* IT->string is an overlay string. Advance to the
4979 next, if there is one. */
2051c264 4980 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
4981 next_overlay_string (it);
4982 }
4983 else
4984 {
4985 /* IT->string is not an overlay string. If we reached
4986 its end, and there is something on IT->stack, proceed
4987 with what is on the stack. This can be either another
4988 string, this time an overlay string, or a buffer. */
2051c264 4989 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5f5c8ee5
GM
4990 && it->sp > 0)
4991 {
4992 pop_it (it);
4993 if (!STRINGP (it->string))
4994 it->method = next_element_from_buffer;
b2046df8
GM
4995 else
4996 goto consider_string_end;
5f5c8ee5
GM
4997 }
4998 }
4999 }
5000 else if (it->method == next_element_from_image
5001 || it->method == next_element_from_stretch)
5002 {
5003 /* The position etc with which we have to proceed are on
5004 the stack. The position may be at the end of a string,
5005 if the `display' property takes up the whole string. */
5006 pop_it (it);
5007 it->image_id = 0;
5008 if (STRINGP (it->string))
5009 {
5010 it->method = next_element_from_string;
5011 goto consider_string_end;
5012 }
5013 else
5014 it->method = next_element_from_buffer;
5015 }
5016 else
5017 /* There are no other methods defined, so this should be a bug. */
5018 abort ();
5019
5f5c8ee5
GM
5020 xassert (it->method != next_element_from_string
5021 || (STRINGP (it->string)
5022 && IT_STRING_CHARPOS (*it) >= 0));
5023}
5024
5025
5026/* Load IT's display element fields with information about the next
5027 display element which comes from a display table entry or from the
5028 result of translating a control character to one of the forms `^C'
5029 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5030
5031static int
5032next_element_from_display_vector (it)
5033 struct it *it;
5034{
5035 /* Precondition. */
5036 xassert (it->dpvec && it->current.dpvec_index >= 0);
5037
5038 /* Remember the current face id in case glyphs specify faces.
5039 IT's face is restored in set_iterator_to_next. */
5040 it->saved_face_id = it->face_id;
2311178e 5041
5f5c8ee5
GM
5042 if (INTEGERP (*it->dpvec)
5043 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5044 {
5045 int lface_id;
5046 GLYPH g;
5047
5048 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5049 it->c = FAST_GLYPH_CHAR (g);
c5924f47 5050 it->len = CHAR_BYTES (it->c);
5f5c8ee5
GM
5051
5052 /* The entry may contain a face id to use. Such a face id is
5053 the id of a Lisp face, not a realized face. A face id of
969065c3 5054 zero means no face is specified. */
5f5c8ee5
GM
5055 lface_id = FAST_GLYPH_FACE (g);
5056 if (lface_id)
5057 {
969065c3 5058 /* The function returns -1 if lface_id is invalid. */
5f5c8ee5
GM
5059 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5060 if (face_id >= 0)
969065c3 5061 it->face_id = face_id;
5f5c8ee5
GM
5062 }
5063 }
5064 else
5065 /* Display table entry is invalid. Return a space. */
5066 it->c = ' ', it->len = 1;
5067
5068 /* Don't change position and object of the iterator here. They are
5069 still the values of the character that had this display table
5070 entry or was translated, and that's what we want. */
5071 it->what = IT_CHARACTER;
5072 return 1;
5073}
5074
5075
5076/* Load IT with the next display element from Lisp string IT->string.
5077 IT->current.string_pos is the current position within the string.
5078 If IT->current.overlay_string_index >= 0, the Lisp string is an
5079 overlay string. */
5080
5081static int
5082next_element_from_string (it)
5083 struct it *it;
5084{
5085 struct text_pos position;
5086
5087 xassert (STRINGP (it->string));
5088 xassert (IT_STRING_CHARPOS (*it) >= 0);
5089 position = it->current.string_pos;
5090
5091 /* Time to check for invisible text? */
5092 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5093 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5094 {
5095 handle_stop (it);
5096
5097 /* Since a handler may have changed IT->method, we must
5098 recurse here. */
5099 return get_next_display_element (it);
5100 }
5101
5102 if (it->current.overlay_string_index >= 0)
5103 {
5104 /* Get the next character from an overlay string. In overlay
5105 strings, There is no field width or padding with spaces to
5106 do. */
2051c264 5107 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
5108 {
5109 it->what = IT_EOB;
5110 return 0;
5111 }
5112 else if (STRING_MULTIBYTE (it->string))
5113 {
2051c264 5114 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
50f80c2f
KR
5115 const unsigned char *s = (SDATA (it->string)
5116 + IT_STRING_BYTEPOS (*it));
4fdb80f2 5117 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
5118 }
5119 else
5120 {
2051c264 5121 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
5122 it->len = 1;
5123 }
5124 }
5125 else
5126 {
5127 /* Get the next character from a Lisp string that is not an
5128 overlay string. Such strings come from the mode line, for
5129 example. We may have to pad with spaces, or truncate the
5130 string. See also next_element_from_c_string. */
5131 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5132 {
5133 it->what = IT_EOB;
5134 return 0;
5135 }
5136 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5137 {
5138 /* Pad with spaces. */
5139 it->c = ' ', it->len = 1;
5140 CHARPOS (position) = BYTEPOS (position) = -1;
5141 }
5142 else if (STRING_MULTIBYTE (it->string))
5143 {
2051c264 5144 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
50f80c2f
KR
5145 const unsigned char *s = (SDATA (it->string)
5146 + IT_STRING_BYTEPOS (*it));
4fdb80f2 5147 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
5148 }
5149 else
5150 {
2051c264 5151 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
5152 it->len = 1;
5153 }
5154 }
5155
5156 /* Record what we have and where it came from. Note that we store a
5157 buffer position in IT->position although it could arguably be a
5158 string position. */
5159 it->what = IT_CHARACTER;
5160 it->object = it->string;
5161 it->position = position;
5162 return 1;
5163}
5164
5165
5166/* Load IT with next display element from C string IT->s.
5167 IT->string_nchars is the maximum number of characters to return
5168 from the string. IT->end_charpos may be greater than
5169 IT->string_nchars when this function is called, in which case we
5170 may have to return padding spaces. Value is zero if end of string
5171 reached, including padding spaces. */
5172
5173static int
5174next_element_from_c_string (it)
5175 struct it *it;
5176{
5177 int success_p = 1;
2311178e 5178
5f5c8ee5
GM
5179 xassert (it->s);
5180 it->what = IT_CHARACTER;
5181 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5182 it->object = Qnil;
2311178e 5183
5f5c8ee5
GM
5184 /* IT's position can be greater IT->string_nchars in case a field
5185 width or precision has been specified when the iterator was
5186 initialized. */
5187 if (IT_CHARPOS (*it) >= it->end_charpos)
5188 {
5189 /* End of the game. */
5190 it->what = IT_EOB;
5191 success_p = 0;
5192 }
5193 else if (IT_CHARPOS (*it) >= it->string_nchars)
5194 {
5195 /* Pad with spaces. */
5196 it->c = ' ', it->len = 1;
5197 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5198 }
5199 else if (it->multibyte_p)
5200 {
5201 /* Implementation note: The calls to strlen apparently aren't a
5202 performance problem because there is no noticeable performance
5203 difference between Emacs running in unibyte or multibyte mode. */
5204 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
5205 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5206 maxlen, &it->len);
5f5c8ee5
GM
5207 }
5208 else
5209 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
2311178e 5210
5f5c8ee5
GM
5211 return success_p;
5212}
5213
5214
5215/* Set up IT to return characters from an ellipsis, if appropriate.
5216 The definition of the ellipsis glyphs may come from a display table
5217 entry. This function Fills IT with the first glyph from the
5218 ellipsis if an ellipsis is to be displayed. */
5219
13f19968 5220static int
5f5c8ee5
GM
5221next_element_from_ellipsis (it)
5222 struct it *it;
5223{
13f19968 5224 if (it->selective_display_ellipsis_p)
5f5c8ee5 5225 {
13f19968
GM
5226 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5227 {
5228 /* Use the display table definition for `...'. Invalid glyphs
5229 will be handled by the method returning elements from dpvec. */
5230 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5231 it->dpvec_char_len = it->len;
5232 it->dpvec = v->contents;
5233 it->dpend = v->contents + v->size;
5234 it->current.dpvec_index = 0;
5235 it->method = next_element_from_display_vector;
5236 }
5237 else
5238 {
5239 /* Use default `...' which is stored in default_invis_vector. */
5240 it->dpvec_char_len = it->len;
5241 it->dpvec = default_invis_vector;
5242 it->dpend = default_invis_vector + 3;
5243 it->current.dpvec_index = 0;
5244 it->method = next_element_from_display_vector;
5245 }
5f5c8ee5 5246 }
13f19968 5247 else
54918e2b 5248 {
4aad61f8
GM
5249 /* The face at the current position may be different from the
5250 face we find after the invisible text. Remember what it
5251 was in IT->saved_face_id, and signal that it's there by
5252 setting face_before_selective_p. */
5253 it->saved_face_id = it->face_id;
54918e2b
GM
5254 it->method = next_element_from_buffer;
5255 reseat_at_next_visible_line_start (it, 1);
4aad61f8 5256 it->face_before_selective_p = 1;
54918e2b 5257 }
2311178e 5258
13f19968 5259 return get_next_display_element (it);
5f5c8ee5
GM
5260}
5261
5262
5263/* Deliver an image display element. The iterator IT is already
5264 filled with image information (done in handle_display_prop). Value
5265 is always 1. */
2311178e 5266
5f5c8ee5
GM
5267
5268static int
5269next_element_from_image (it)
5270 struct it *it;
5271{
5272 it->what = IT_IMAGE;
5273 return 1;
5274}
5275
5276
5277/* Fill iterator IT with next display element from a stretch glyph
5278 property. IT->object is the value of the text property. Value is
5279 always 1. */
5280
5281static int
5282next_element_from_stretch (it)
5283 struct it *it;
5284{
5285 it->what = IT_STRETCH;
5286 return 1;
5287}
5288
5289
5290/* Load IT with the next display element from current_buffer. Value
5291 is zero if end of buffer reached. IT->stop_charpos is the next
5292 position at which to stop and check for text properties or buffer
5293 end. */
5294
5295static int
5296next_element_from_buffer (it)
5297 struct it *it;
5298{
5299 int success_p = 1;
5300
5301 /* Check this assumption, otherwise, we would never enter the
5302 if-statement, below. */
5303 xassert (IT_CHARPOS (*it) >= BEGV
5304 && IT_CHARPOS (*it) <= it->stop_charpos);
5305
5306 if (IT_CHARPOS (*it) >= it->stop_charpos)
5307 {
5308 if (IT_CHARPOS (*it) >= it->end_charpos)
5309 {
5310 int overlay_strings_follow_p;
2311178e 5311
5f5c8ee5
GM
5312 /* End of the game, except when overlay strings follow that
5313 haven't been returned yet. */
5314 if (it->overlay_strings_at_end_processed_p)
5315 overlay_strings_follow_p = 0;
5316 else
5317 {
5318 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf 5319 overlay_strings_follow_p = get_overlay_strings (it, 0);
5f5c8ee5
GM
5320 }
5321
5322 if (overlay_strings_follow_p)
5323 success_p = get_next_display_element (it);
5324 else
5325 {
5326 it->what = IT_EOB;
5327 it->position = it->current.pos;
5328 success_p = 0;
5329 }
5330 }
5331 else
5332 {
5333 handle_stop (it);
5334 return get_next_display_element (it);
5335 }
5336 }
5337 else
5338 {
5339 /* No face changes, overlays etc. in sight, so just return a
5340 character from current_buffer. */
5341 unsigned char *p;
5342
5343 /* Maybe run the redisplay end trigger hook. Performance note:
5344 This doesn't seem to cost measurable time. */
5345 if (it->redisplay_end_trigger_charpos
5346 && it->glyph_row
5347 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5348 run_redisplay_end_trigger_hook (it);
5349
5350 /* Get the next character, maybe multibyte. */
5351 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
260a86a0 5352 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5f5c8ee5
GM
5353 {
5354 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5355 - IT_BYTEPOS (*it));
4fdb80f2 5356 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
5357 }
5358 else
5359 it->c = *p, it->len = 1;
5360
5361 /* Record what we have and where it came from. */
5362 it->what = IT_CHARACTER;;
5363 it->object = it->w->buffer;
5364 it->position = it->current.pos;
5365
5366 /* Normally we return the character found above, except when we
5367 really want to return an ellipsis for selective display. */
5368 if (it->selective)
5369 {
5370 if (it->c == '\n')
5371 {
5372 /* A value of selective > 0 means hide lines indented more
5373 than that number of columns. */
5374 if (it->selective > 0
5375 && IT_CHARPOS (*it) + 1 < ZV
5376 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5377 IT_BYTEPOS (*it) + 1,
a67e162b 5378 (double) it->selective)) /* iftc */
312246d1 5379 {
13f19968 5380 success_p = next_element_from_ellipsis (it);
312246d1
GM
5381 it->dpvec_char_len = -1;
5382 }
5f5c8ee5
GM
5383 }
5384 else if (it->c == '\r' && it->selective == -1)
5385 {
5386 /* A value of selective == -1 means that everything from the
5387 CR to the end of the line is invisible, with maybe an
5388 ellipsis displayed for it. */
13f19968 5389 success_p = next_element_from_ellipsis (it);
312246d1 5390 it->dpvec_char_len = -1;
5f5c8ee5
GM
5391 }
5392 }
5393 }
5394
5395 /* Value is zero if end of buffer reached. */
c880678e 5396 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5f5c8ee5
GM
5397 return success_p;
5398}
5399
2311178e 5400
5f5c8ee5
GM
5401/* Run the redisplay end trigger hook for IT. */
5402
5403static void
5404run_redisplay_end_trigger_hook (it)
5405 struct it *it;
5406{
5407 Lisp_Object args[3];
5408
5409 /* IT->glyph_row should be non-null, i.e. we should be actually
5410 displaying something, or otherwise we should not run the hook. */
5411 xassert (it->glyph_row);
5412
5413 /* Set up hook arguments. */
5414 args[0] = Qredisplay_end_trigger_functions;
5415 args[1] = it->window;
5416 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5417 it->redisplay_end_trigger_charpos = 0;
5418
5419 /* Since we are *trying* to run these functions, don't try to run
5420 them again, even if they get an error. */
5421 it->w->redisplay_end_trigger = Qnil;
5422 Frun_hook_with_args (3, args);
2311178e 5423
5f5c8ee5
GM
5424 /* Notice if it changed the face of the character we are on. */
5425 handle_face_prop (it);
5426}
5427
5428
260a86a0
KH
5429/* Deliver a composition display element. The iterator IT is already
5430 filled with composition information (done in
5431 handle_composition_prop). Value is always 1. */
5432
5433static int
5434next_element_from_composition (it)
5435 struct it *it;
5436{
5437 it->what = IT_COMPOSITION;
5438 it->position = (STRINGP (it->string)
5439 ? it->current.string_pos
5440 : it->current.pos);
5441 return 1;
5442}
5443
5444
5f5c8ee5
GM
5445\f
5446/***********************************************************************
5447 Moving an iterator without producing glyphs
5448 ***********************************************************************/
5449
5450/* Move iterator IT to a specified buffer or X position within one
5451 line on the display without producing glyphs.
5452
c53a1624
RS
5453 OP should be a bit mask including some or all of these bits:
5454 MOVE_TO_X: Stop on reaching x-position TO_X.
5455 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5456 Regardless of OP's value, stop in reaching the end of the display line.
5f5c8ee5 5457
c53a1624
RS
5458 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5459 This means, in particular, that TO_X includes window's horizontal
5460 scroll amount.
5f5c8ee5 5461
c53a1624
RS
5462 The return value has several possible values that
5463 say what condition caused the scan to stop:
5f5c8ee5
GM
5464
5465 MOVE_POS_MATCH_OR_ZV
5466 - when TO_POS or ZV was reached.
2311178e 5467
5f5c8ee5
GM
5468 MOVE_X_REACHED
5469 -when TO_X was reached before TO_POS or ZV were reached.
2311178e 5470
5f5c8ee5
GM
5471 MOVE_LINE_CONTINUED
5472 - when we reached the end of the display area and the line must
5473 be continued.
2311178e 5474
5f5c8ee5
GM
5475 MOVE_LINE_TRUNCATED
5476 - when we reached the end of the display area and the line is
5477 truncated.
5478
5479 MOVE_NEWLINE_OR_CR
5480 - when we stopped at a line end, i.e. a newline or a CR and selective
5481 display is on. */
5482
701552dd 5483static enum move_it_result
5f5c8ee5
GM
5484move_it_in_display_line_to (it, to_charpos, to_x, op)
5485 struct it *it;
5486 int to_charpos, to_x, op;
5487{
5488 enum move_it_result result = MOVE_UNDEFINED;
5489 struct glyph_row *saved_glyph_row;
5490
5491 /* Don't produce glyphs in produce_glyphs. */
5492 saved_glyph_row = it->glyph_row;
5493 it->glyph_row = NULL;
5494
5f5c8ee5
GM
5495 while (1)
5496 {
ae26e27d 5497 int x, i, ascent = 0, descent = 0;
2311178e 5498
5f5c8ee5
GM
5499 /* Stop when ZV or TO_CHARPOS reached. */
5500 if (!get_next_display_element (it)
5501 || ((op & MOVE_TO_POS) != 0
5502 && BUFFERP (it->object)
5503 && IT_CHARPOS (*it) >= to_charpos))
5504 {
5505 result = MOVE_POS_MATCH_OR_ZV;
5506 break;
5507 }
2311178e 5508
5f5c8ee5
GM
5509 /* The call to produce_glyphs will get the metrics of the
5510 display element IT is loaded with. We record in x the
5511 x-position before this display element in case it does not
5512 fit on the line. */
5513 x = it->current_x;
2311178e 5514
47589c8c
GM
5515 /* Remember the line height so far in case the next element doesn't
5516 fit on the line. */
5517 if (!it->truncate_lines_p)
5518 {
5519 ascent = it->max_ascent;
5520 descent = it->max_descent;
5521 }
2311178e 5522
5f5c8ee5
GM
5523 PRODUCE_GLYPHS (it);
5524
5525 if (it->area != TEXT_AREA)
5526 {
cafafe0b 5527 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5528 continue;
5529 }
5530
5531 /* The number of glyphs we get back in IT->nglyphs will normally
5532 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5533 character on a terminal frame, or (iii) a line end. For the
5534 second case, IT->nglyphs - 1 padding glyphs will be present
5535 (on X frames, there is only one glyph produced for a
5536 composite character.
5537
5538 The behavior implemented below means, for continuation lines,
5539 that as many spaces of a TAB as fit on the current line are
5540 displayed there. For terminal frames, as many glyphs of a
5541 multi-glyph character are displayed in the current line, too.
5542 This is what the old redisplay code did, and we keep it that
5543 way. Under X, the whole shape of a complex character must
5544 fit on the line or it will be completely displayed in the
5545 next line.
5546
5547 Note that both for tabs and padding glyphs, all glyphs have
5548 the same width. */
5549 if (it->nglyphs)
5550 {
5551 /* More than one glyph or glyph doesn't fit on line. All
5552 glyphs have the same width. */
5553 int single_glyph_width = it->pixel_width / it->nglyphs;
5554 int new_x;
2311178e 5555
5f5c8ee5
GM
5556 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5557 {
5558 new_x = x + single_glyph_width;
5559
5560 /* We want to leave anything reaching TO_X to the caller. */
5561 if ((op & MOVE_TO_X) && new_x > to_x)
5562 {
5563 it->current_x = x;
5564 result = MOVE_X_REACHED;
5565 break;
5566 }
5567 else if (/* Lines are continued. */
5568 !it->truncate_lines_p
5569 && (/* And glyph doesn't fit on the line. */
5570 new_x > it->last_visible_x
5571 /* Or it fits exactly and we're on a window
5572 system frame. */
5573 || (new_x == it->last_visible_x
5574 && FRAME_WINDOW_P (it->f))))
5575 {
5576 if (/* IT->hpos == 0 means the very first glyph
5577 doesn't fit on the line, e.g. a wide image. */
5578 it->hpos == 0
5579 || (new_x == it->last_visible_x
5580 && FRAME_WINDOW_P (it->f)))
5581 {
5582 ++it->hpos;
5583 it->current_x = new_x;
5584 if (i == it->nglyphs - 1)
cafafe0b 5585 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5586 }
5587 else
47589c8c
GM
5588 {
5589 it->current_x = x;
5590 it->max_ascent = ascent;
5591 it->max_descent = descent;
5592 }
2311178e 5593
47589c8c
GM
5594 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5595 IT_CHARPOS (*it)));
5f5c8ee5
GM
5596 result = MOVE_LINE_CONTINUED;
5597 break;
5598 }
5599 else if (new_x > it->first_visible_x)
5600 {
5601 /* Glyph is visible. Increment number of glyphs that
5602 would be displayed. */
5603 ++it->hpos;
5604 }
5605 else
5606 {
2311178e 5607 /* Glyph is completely off the left margin of the display
5f5c8ee5
GM
5608 area. Nothing to do. */
5609 }
5610 }
5611
5612 if (result != MOVE_UNDEFINED)
5613 break;
5614 }
5615 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5616 {
5617 /* Stop when TO_X specified and reached. This check is
5618 necessary here because of lines consisting of a line end,
5619 only. The line end will not produce any glyphs and we
5620 would never get MOVE_X_REACHED. */
5621 xassert (it->nglyphs == 0);
5622 result = MOVE_X_REACHED;
5623 break;
5624 }
2311178e 5625
5f5c8ee5
GM
5626 /* Is this a line end? If yes, we're done. */
5627 if (ITERATOR_AT_END_OF_LINE_P (it))
5628 {
5629 result = MOVE_NEWLINE_OR_CR;
5630 break;
5631 }
2311178e 5632
5f5c8ee5
GM
5633 /* The current display element has been consumed. Advance
5634 to the next. */
cafafe0b 5635 set_iterator_to_next (it, 1);
2311178e 5636
5f5c8ee5
GM
5637 /* Stop if lines are truncated and IT's current x-position is
5638 past the right edge of the window now. */
5639 if (it->truncate_lines_p
5640 && it->current_x >= it->last_visible_x)
5641 {
5642 result = MOVE_LINE_TRUNCATED;
5643 break;
5644 }
5645 }
5646
5647 /* Restore the iterator settings altered at the beginning of this
5648 function. */
5649 it->glyph_row = saved_glyph_row;
5650 return result;
5651}
5652
5653
9b2bba76
RS
5654/* Move IT forward until it satisfies one or more of the criteria in
5655 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5656
5657 OP is a bit-mask that specifies where to stop, and in particular,
5658 which of those four position arguments makes a difference. See the
5659 description of enum move_operation_enum.
2311178e 5660
5f5c8ee5
GM
5661 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5662 screen line, this function will set IT to the next position >
5663 TO_CHARPOS. */
5664
5665void
5666move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5667 struct it *it;
5668 int to_charpos, to_x, to_y, to_vpos;
5669 int op;
5670{
5671 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5672 int line_height;
47589c8c 5673 int reached = 0;
5f5c8ee5 5674
47589c8c 5675 for (;;)
5f5c8ee5
GM
5676 {
5677 if (op & MOVE_TO_VPOS)
5678 {
5679 /* If no TO_CHARPOS and no TO_X specified, stop at the
5680 start of the line TO_VPOS. */
5681 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5682 {
5683 if (it->vpos == to_vpos)
47589c8c
GM
5684 {
5685 reached = 1;
5686 break;
5687 }
5688 else
5689 skip = move_it_in_display_line_to (it, -1, -1, 0);
5f5c8ee5
GM
5690 }
5691 else
5692 {
5693 /* TO_VPOS >= 0 means stop at TO_X in the line at
5694 TO_VPOS, or at TO_POS, whichever comes first. */
47589c8c
GM
5695 if (it->vpos == to_vpos)
5696 {
5697 reached = 2;
5698 break;
5699 }
2311178e 5700
5f5c8ee5
GM
5701 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5702
5703 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
47589c8c
GM
5704 {
5705 reached = 3;
5706 break;
5707 }
5f5c8ee5
GM
5708 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5709 {
5710 /* We have reached TO_X but not in the line we want. */
5711 skip = move_it_in_display_line_to (it, to_charpos,
5712 -1, MOVE_TO_POS);
5713 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5714 {
5715 reached = 4;
5716 break;
5717 }
5f5c8ee5
GM
5718 }
5719 }
5720 }
5721 else if (op & MOVE_TO_Y)
5722 {
5723 struct it it_backup;
2311178e 5724
5f5c8ee5
GM
5725 /* TO_Y specified means stop at TO_X in the line containing
5726 TO_Y---or at TO_CHARPOS if this is reached first. The
5727 problem is that we can't really tell whether the line
5728 contains TO_Y before we have completely scanned it, and
5729 this may skip past TO_X. What we do is to first scan to
5730 TO_X.
5731
5732 If TO_X is not specified, use a TO_X of zero. The reason
5733 is to make the outcome of this function more predictable.
5734 If we didn't use TO_X == 0, we would stop at the end of
5735 the line which is probably not what a caller would expect
5736 to happen. */
5737 skip = move_it_in_display_line_to (it, to_charpos,
5738 ((op & MOVE_TO_X)
5739 ? to_x : 0),
5740 (MOVE_TO_X
5741 | (op & MOVE_TO_POS)));
5742
5743 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5744 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5745 {
5746 reached = 5;
5747 break;
5748 }
2311178e 5749
5f5c8ee5
GM
5750 /* If TO_X was reached, we would like to know whether TO_Y
5751 is in the line. This can only be said if we know the
5752 total line height which requires us to scan the rest of
5753 the line. */
5f5c8ee5
GM
5754 if (skip == MOVE_X_REACHED)
5755 {
5756 it_backup = *it;
47589c8c 5757 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5758 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5759 op & MOVE_TO_POS);
47589c8c 5760 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5761 }
5762
5763 /* Now, decide whether TO_Y is in this line. */
5764 line_height = it->max_ascent + it->max_descent;
47589c8c 5765 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
2311178e 5766
5f5c8ee5
GM
5767 if (to_y >= it->current_y
5768 && to_y < it->current_y + line_height)
5769 {
5770 if (skip == MOVE_X_REACHED)
5771 /* If TO_Y is in this line and TO_X was reached above,
5772 we scanned too far. We have to restore IT's settings
5773 to the ones before skipping. */
5774 *it = it_backup;
47589c8c 5775 reached = 6;
5f5c8ee5
GM
5776 }
5777 else if (skip == MOVE_X_REACHED)
5778 {
5779 skip = skip2;
5780 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c 5781 reached = 7;
5f5c8ee5
GM
5782 }
5783
47589c8c 5784 if (reached)
5f5c8ee5
GM
5785 break;
5786 }
5787 else
5788 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5789
5790 switch (skip)
5791 {
5792 case MOVE_POS_MATCH_OR_ZV:
47589c8c
GM
5793 reached = 8;
5794 goto out;
5f5c8ee5
GM
5795
5796 case MOVE_NEWLINE_OR_CR:
cafafe0b 5797 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5798 it->continuation_lines_width = 0;
5799 break;
5800
5801 case MOVE_LINE_TRUNCATED:
5802 it->continuation_lines_width = 0;
312246d1 5803 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
5804 if ((op & MOVE_TO_POS) != 0
5805 && IT_CHARPOS (*it) > to_charpos)
47589c8c
GM
5806 {
5807 reached = 9;
5808 goto out;
5809 }
5f5c8ee5
GM
5810 break;
5811
5812 case MOVE_LINE_CONTINUED:
5813 it->continuation_lines_width += it->current_x;
5814 break;
5815
5816 default:
5817 abort ();
5818 }
5819
5820 /* Reset/increment for the next run. */
5821 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5822 it->current_x = it->hpos = 0;
5823 it->current_y += it->max_ascent + it->max_descent;
5824 ++it->vpos;
5825 last_height = it->max_ascent + it->max_descent;
5826 last_max_ascent = it->max_ascent;
5827 it->max_ascent = it->max_descent = 0;
5828 }
2311178e 5829
47589c8c
GM
5830 out:
5831
5832 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5f5c8ee5
GM
5833}
5834
5835
5836/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5837
5838 If DY > 0, move IT backward at least that many pixels. DY = 0
5839 means move IT backward to the preceding line start or BEGV. This
5840 function may move over more than DY pixels if IT->current_y - DY
5841 ends up in the middle of a line; in this case IT->current_y will be
5842 set to the top of the line moved to. */
5843
5844void
5845move_it_vertically_backward (it, dy)
5846 struct it *it;
5847 int dy;
5848{
79ddf6f7
GM
5849 int nlines, h;
5850 struct it it2, it3;
5f5c8ee5 5851 int start_pos = IT_CHARPOS (*it);
2311178e 5852
5f5c8ee5
GM
5853 xassert (dy >= 0);
5854
5855 /* Estimate how many newlines we must move back. */
da8b7f4f 5856 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
5f5c8ee5
GM
5857
5858 /* Set the iterator's position that many lines back. */
5859 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5860 back_to_previous_visible_line_start (it);
5861
5862 /* Reseat the iterator here. When moving backward, we don't want
5863 reseat to skip forward over invisible text, set up the iterator
5864 to deliver from overlay strings at the new position etc. So,
5865 use reseat_1 here. */
5866 reseat_1 (it, it->current.pos, 1);
5867
5868 /* We are now surely at a line start. */
5869 it->current_x = it->hpos = 0;
0e47bbf7 5870 it->continuation_lines_width = 0;
5f5c8ee5
GM
5871
5872 /* Move forward and see what y-distance we moved. First move to the
5873 start of the next line so that we get its height. We need this
5874 height to be able to tell whether we reached the specified
5875 y-distance. */
5876 it2 = *it;
5877 it2.max_ascent = it2.max_descent = 0;
5878 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5879 MOVE_TO_POS | MOVE_TO_VPOS);
5880 xassert (IT_CHARPOS (*it) >= BEGV);
79ddf6f7 5881 it3 = it2;
2311178e 5882
5f5c8ee5
GM
5883 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5884 xassert (IT_CHARPOS (*it) >= BEGV);
ccbb9ed2
RS
5885 /* H is the actual vertical distance from the position in *IT
5886 and the starting position. */
5f5c8ee5 5887 h = it2.current_y - it->current_y;
ccbb9ed2 5888 /* NLINES is the distance in number of lines. */
5f5c8ee5
GM
5889 nlines = it2.vpos - it->vpos;
5890
ccbb9ed2
RS
5891 /* Correct IT's y and vpos position
5892 so that they are relative to the starting point. */
5f5c8ee5
GM
5893 it->vpos -= nlines;
5894 it->current_y -= h;
2311178e 5895
5f5c8ee5
GM
5896 if (dy == 0)
5897 {
5898 /* DY == 0 means move to the start of the screen line. The
5899 value of nlines is > 0 if continuation lines were involved. */
5900 if (nlines > 0)
5901 move_it_by_lines (it, nlines, 1);
5902 xassert (IT_CHARPOS (*it) <= start_pos);
5903 }
ccbb9ed2 5904 else
5f5c8ee5 5905 {
ccbb9ed2
RS
5906 /* The y-position we try to reach, relative to *IT.
5907 Note that H has been subtracted in front of the if-statement. */
5f5c8ee5 5908 int target_y = it->current_y + h - dy;
79ddf6f7
GM
5909 int y0 = it3.current_y;
5910 int y1 = line_bottom_y (&it3);
5911 int line_height = y1 - y0;
f7ccfc8c 5912
5f5c8ee5
GM
5913 /* If we did not reach target_y, try to move further backward if
5914 we can. If we moved too far backward, try to move forward. */
5915 if (target_y < it->current_y
79ddf6f7
GM
5916 /* This is heuristic. In a window that's 3 lines high, with
5917 a line height of 13 pixels each, recentering with point
5918 on the bottom line will try to move -39/2 = 19 pixels
5919 backward. Try to avoid moving into the first line. */
798dbe1f 5920 && it->current_y - target_y > line_height / 3 * 2
5f5c8ee5
GM
5921 && IT_CHARPOS (*it) > BEGV)
5922 {
f7ccfc8c
GM
5923 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5924 target_y - it->current_y));
5f5c8ee5
GM
5925 move_it_vertically (it, target_y - it->current_y);
5926 xassert (IT_CHARPOS (*it) >= BEGV);
5927 }
5928 else if (target_y >= it->current_y + line_height
5929 && IT_CHARPOS (*it) < ZV)
5930 {
55591976 5931 /* Should move forward by at least one line, maybe more.
2311178e 5932
55591976
GM
5933 Note: Calling move_it_by_lines can be expensive on
5934 terminal frames, where compute_motion is used (via
5935 vmotion) to do the job, when there are very long lines
5936 and truncate-lines is nil. That's the reason for
5937 treating terminal frames specially here. */
2311178e 5938
55591976
GM
5939 if (!FRAME_WINDOW_P (it->f))
5940 move_it_vertically (it, target_y - (it->current_y + line_height));
5941 else
f7ccfc8c 5942 {
55591976
GM
5943 do
5944 {
5945 move_it_by_lines (it, 1, 1);
5946 }
5947 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
f7ccfc8c 5948 }
f7ccfc8c 5949
5f5c8ee5
GM
5950 xassert (IT_CHARPOS (*it) >= BEGV);
5951 }
5952 }
5953}
5954
5955
5956/* Move IT by a specified amount of pixel lines DY. DY negative means
5957 move backwards. DY = 0 means move to start of screen line. At the
5958 end, IT will be on the start of a screen line. */
5959
2311178e 5960void
5f5c8ee5
GM
5961move_it_vertically (it, dy)
5962 struct it *it;
5963 int dy;
5964{
5965 if (dy <= 0)
5966 move_it_vertically_backward (it, -dy);
5967 else if (dy > 0)
5968 {
47589c8c 5969 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5f5c8ee5
GM
5970 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5971 MOVE_TO_POS | MOVE_TO_Y);
47589c8c 5972 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5973
5974 /* If buffer ends in ZV without a newline, move to the start of
5975 the line to satisfy the post-condition. */
5976 if (IT_CHARPOS (*it) == ZV
5977 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5978 move_it_by_lines (it, 0, 0);
5979 }
5980}
5981
5982
47fc2c10
GM
5983/* Move iterator IT past the end of the text line it is in. */
5984
5985void
5986move_it_past_eol (it)
5987 struct it *it;
5988{
5989 enum move_it_result rc;
2311178e 5990
47fc2c10
GM
5991 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5992 if (rc == MOVE_NEWLINE_OR_CR)
5993 set_iterator_to_next (it, 0);
5994}
5995
5996
2c79b732
GM
5997#if 0 /* Currently not used. */
5998
5f5c8ee5
GM
5999/* Return non-zero if some text between buffer positions START_CHARPOS
6000 and END_CHARPOS is invisible. IT->window is the window for text
6001 property lookup. */
6002
6003static int
6004invisible_text_between_p (it, start_charpos, end_charpos)
6005 struct it *it;
6006 int start_charpos, end_charpos;
6007{
5f5c8ee5
GM
6008 Lisp_Object prop, limit;
6009 int invisible_found_p;
2311178e 6010
5f5c8ee5
GM
6011 xassert (it != NULL && start_charpos <= end_charpos);
6012
6013 /* Is text at START invisible? */
6014 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6015 it->window);
6016 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6017 invisible_found_p = 1;
6018 else
6019 {
016b5642
MB
6020 limit = Fnext_single_char_property_change (make_number (start_charpos),
6021 Qinvisible, Qnil,
6022 make_number (end_charpos));
5f5c8ee5
GM
6023 invisible_found_p = XFASTINT (limit) < end_charpos;
6024 }
6025
6026 return invisible_found_p;
5f5c8ee5
GM
6027}
6028
2c79b732
GM
6029#endif /* 0 */
6030
5f5c8ee5
GM
6031
6032/* Move IT by a specified number DVPOS of screen lines down. DVPOS
6033 negative means move up. DVPOS == 0 means move to the start of the
6034 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6035 NEED_Y_P is zero, IT->current_y will be left unchanged.
6036
6037 Further optimization ideas: If we would know that IT->f doesn't use
6038 a face with proportional font, we could be faster for
6039 truncate-lines nil. */
6040
6041void
6042move_it_by_lines (it, dvpos, need_y_p)
6043 struct it *it;
6044 int dvpos, need_y_p;
6045{
6046 struct position pos;
2311178e 6047
5f5c8ee5
GM
6048 if (!FRAME_WINDOW_P (it->f))
6049 {
6050 struct text_pos textpos;
2311178e 6051
5f5c8ee5
GM
6052 /* We can use vmotion on frames without proportional fonts. */
6053 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6054 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6055 reseat (it, textpos, 1);
6056 it->vpos += pos.vpos;
6057 it->current_y += pos.vpos;
6058 }
6059 else if (dvpos == 0)
6060 {
6061 /* DVPOS == 0 means move to the start of the screen line. */
6062 move_it_vertically_backward (it, 0);
6063 xassert (it->current_x == 0 && it->hpos == 0);
6064 }
6065 else if (dvpos > 0)
2c79b732 6066 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5f5c8ee5
GM
6067 else
6068 {
6069 struct it it2;
6070 int start_charpos, i;
2311178e 6071
e8660d73
GM
6072 /* Start at the beginning of the screen line containing IT's
6073 position. */
6074 move_it_vertically_backward (it, 0);
2311178e 6075
5f5c8ee5
GM
6076 /* Go back -DVPOS visible lines and reseat the iterator there. */
6077 start_charpos = IT_CHARPOS (*it);
6078 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6079 back_to_previous_visible_line_start (it);
6080 reseat (it, it->current.pos, 1);
6081 it->current_x = it->hpos = 0;
6082
6083 /* Above call may have moved too far if continuation lines
6084 are involved. Scan forward and see if it did. */
6085 it2 = *it;
6086 it2.vpos = it2.current_y = 0;
6087 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6088 it->vpos -= it2.vpos;
6089 it->current_y -= it2.current_y;
6090 it->current_x = it->hpos = 0;
6091
6092 /* If we moved too far, move IT some lines forward. */
6093 if (it2.vpos > -dvpos)
6094 {
6095 int delta = it2.vpos + dvpos;
6096 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6097 }
6098 }
6099}
6100
3824b1a6
AS
6101/* Return 1 if IT points into the middle of a display vector. */
6102
6103int
6104in_display_vector_p (it)
6105 struct it *it;
6106{
6107 return (it->method == next_element_from_display_vector
6108 && it->current.dpvec_index > 0
6109 && it->dpvec + it->current.dpvec_index != it->dpend);
6110}
5f5c8ee5
GM
6111
6112\f
6113/***********************************************************************
6114 Messages
6115 ***********************************************************************/
6116
6117
937248bc
GM
6118/* Add a message with format string FORMAT and arguments ARG1 and ARG2
6119 to *Messages*. */
6120
6121void
6122add_to_log (format, arg1, arg2)
6123 char *format;
6124 Lisp_Object arg1, arg2;
6125{
6126 Lisp_Object args[3];
6127 Lisp_Object msg, fmt;
6128 char *buffer;
6129 int len;
6130 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6131
ae794295
GM
6132 /* Do nothing if called asynchronously. Inserting text into
6133 a buffer may call after-change-functions and alike and
6134 that would means running Lisp asynchronously. */
6135 if (handling_signal)
6136 return;
6137
937248bc
GM
6138 fmt = msg = Qnil;
6139 GCPRO4 (fmt, msg, arg1, arg2);
2311178e 6140
937248bc
GM
6141 args[0] = fmt = build_string (format);
6142 args[1] = arg1;
6143 args[2] = arg2;
6fc556fd 6144 msg = Fformat (3, args);
937248bc 6145
2051c264 6146 len = SBYTES (msg) + 1;
937248bc 6147 buffer = (char *) alloca (len);
2051c264 6148 bcopy (SDATA (msg), buffer, len);
2311178e 6149
796184bc 6150 message_dolog (buffer, len - 1, 1, 0);
937248bc
GM
6151 UNGCPRO;
6152}
6153
6154
5f5c8ee5
GM
6155/* Output a newline in the *Messages* buffer if "needs" one. */
6156
6157void
6158message_log_maybe_newline ()
6159{
6160 if (message_log_need_newline)
6161 message_dolog ("", 0, 1, 0);
6162}
6163
6164
1e313f28 6165/* Add a string M of length NBYTES to the message log, optionally
5f5c8ee5
GM
6166 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6167 nonzero, means interpret the contents of M as multibyte. This
6168 function calls low-level routines in order to bypass text property
6169 hooks, etc. which might not be safe to run. */
6170
6171void
1e313f28 6172message_dolog (m, nbytes, nlflag, multibyte)
50f80c2f 6173 const char *m;
1e313f28 6174 int nbytes, nlflag, multibyte;
5f5c8ee5 6175{
a67e162b
RS
6176 if (!NILP (Vmemory_full))
6177 return;
6178
5f5c8ee5
GM
6179 if (!NILP (Vmessage_log_max))
6180 {
6181 struct buffer *oldbuf;
6182 Lisp_Object oldpoint, oldbegv, oldzv;
6183 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6184 int point_at_end = 0;
6185 int zv_at_end = 0;
6186 Lisp_Object old_deactivate_mark, tem;
6052529b 6187 struct gcpro gcpro1;
5f5c8ee5
GM
6188
6189 old_deactivate_mark = Vdeactivate_mark;
6190 oldbuf = current_buffer;
6a94510a 6191 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5f5c8ee5
GM
6192 current_buffer->undo_list = Qt;
6193
b14bc55e
RS
6194 oldpoint = message_dolog_marker1;
6195 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6196 oldbegv = message_dolog_marker2;
6197 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6198 oldzv = message_dolog_marker3;
6199 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6200 GCPRO1 (old_deactivate_mark);
5f5c8ee5
GM
6201
6202 if (PT == Z)
6203 point_at_end = 1;
6204 if (ZV == Z)
6205 zv_at_end = 1;
6206
6207 BEGV = BEG;
6208 BEGV_BYTE = BEG_BYTE;
6209 ZV = Z;
6210 ZV_BYTE = Z_BYTE;
6211 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6212
6213 /* Insert the string--maybe converting multibyte to single byte
6214 or vice versa, so that all the text fits the buffer. */
6215 if (multibyte
6216 && NILP (current_buffer->enable_multibyte_characters))
6217 {
1e313f28 6218 int i, c, char_bytes;
5f5c8ee5 6219 unsigned char work[1];
2311178e 6220
5f5c8ee5
GM
6221 /* Convert a multibyte string to single-byte
6222 for the *Message* buffer. */
6e57ec5e 6223 for (i = 0; i < nbytes; i += char_bytes)
5f5c8ee5 6224 {
1e313f28 6225 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5f5c8ee5
GM
6226 work[0] = (SINGLE_BYTE_CHAR_P (c)
6227 ? c
6228 : multibyte_char_to_unibyte (c, Qnil));
6229 insert_1_both (work, 1, 1, 1, 0, 0);
6230 }
6231 }
6232 else if (! multibyte
6233 && ! NILP (current_buffer->enable_multibyte_characters))
6234 {
1e313f28 6235 int i, c, char_bytes;
5f5c8ee5 6236 unsigned char *msg = (unsigned char *) m;
260a86a0 6237 unsigned char str[MAX_MULTIBYTE_LENGTH];
5f5c8ee5
GM
6238 /* Convert a single-byte string to multibyte
6239 for the *Message* buffer. */
1e313f28 6240 for (i = 0; i < nbytes; i++)
5f5c8ee5
GM
6241 {
6242 c = unibyte_char_to_multibyte (msg[i]);
1e313f28
GM
6243 char_bytes = CHAR_STRING (c, str);
6244 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5f5c8ee5
GM
6245 }
6246 }
1e313f28
GM
6247 else if (nbytes)
6248 insert_1 (m, nbytes, 1, 0, 0);
5f5c8ee5
GM
6249
6250 if (nlflag)
6251 {
6252 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6253 insert_1 ("\n", 1, 1, 0, 0);
6254
6255 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6256 this_bol = PT;
6257 this_bol_byte = PT_BYTE;
6258
b14bc55e
RS
6259 /* See if this line duplicates the previous one.
6260 If so, combine duplicates. */
5f5c8ee5
GM
6261 if (this_bol > BEG)
6262 {
6263 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6264 prev_bol = PT;
6265 prev_bol_byte = PT_BYTE;
6266
6267 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6268 this_bol, this_bol_byte);
6269 if (dup)
6270 {
6271 del_range_both (prev_bol, prev_bol_byte,
6272 this_bol, this_bol_byte, 0);
6273 if (dup > 1)
6274 {
6275 char dupstr[40];
6276 int duplen;
6277
6278 /* If you change this format, don't forget to also
6279 change message_log_check_duplicate. */
6280 sprintf (dupstr, " [%d times]", dup);
6281 duplen = strlen (dupstr);
6282 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6283 insert_1 (dupstr, duplen, 1, 0, 1);
6284 }
6285 }
6286 }
6287
b14bc55e
RS
6288 /* If we have more than the desired maximum number of lines
6289 in the *Messages* buffer now, delete the oldest ones.
6290 This is safe because we don't have undo in this buffer. */
6291
5f5c8ee5
GM
6292 if (NATNUMP (Vmessage_log_max))
6293 {
6294 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6295 -XFASTINT (Vmessage_log_max) - 1, 0);
6296 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6297 }
6298 }
6299 BEGV = XMARKER (oldbegv)->charpos;
6300 BEGV_BYTE = marker_byte_position (oldbegv);
6301
6302 if (zv_at_end)
6303 {
6304 ZV = Z;
6305 ZV_BYTE = Z_BYTE;
6306 }
6307 else
6308 {
6309 ZV = XMARKER (oldzv)->charpos;
6310 ZV_BYTE = marker_byte_position (oldzv);
6311 }
6312
6313 if (point_at_end)
6314 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6315 else
6316 /* We can't do Fgoto_char (oldpoint) because it will run some
6317 Lisp code. */
6318 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6319 XMARKER (oldpoint)->bytepos);
6320
6321 UNGCPRO;
cfea0546
SM
6322 unchain_marker (XMARKER (oldpoint));
6323 unchain_marker (XMARKER (oldbegv));
6324 unchain_marker (XMARKER (oldzv));
5f5c8ee5
GM
6325
6326 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6327 set_buffer_internal (oldbuf);
6328 if (NILP (tem))
6329 windows_or_buffers_changed = old_windows_or_buffers_changed;
6330 message_log_need_newline = !nlflag;
6331 Vdeactivate_mark = old_deactivate_mark;
6332 }
6333}
6334
6335
6336/* We are at the end of the buffer after just having inserted a newline.
6337 (Note: We depend on the fact we won't be crossing the gap.)
6338 Check to see if the most recent message looks a lot like the previous one.
6339 Return 0 if different, 1 if the new one should just replace it, or a
6340 value N > 1 if we should also append " [N times]". */
6341
6342static int
6343message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6344 int prev_bol, this_bol;
6345 int prev_bol_byte, this_bol_byte;
6346{
6347 int i;
6348 int len = Z_BYTE - 1 - this_bol_byte;
6349 int seen_dots = 0;
6350 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6351 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6352
6353 for (i = 0; i < len; i++)
6354 {
509633e3 6355 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5f5c8ee5
GM
6356 seen_dots = 1;
6357 if (p1[i] != p2[i])
6358 return seen_dots;
6359 }
6360 p1 += len;
6361 if (*p1 == '\n')
6362 return 2;
6363 if (*p1++ == ' ' && *p1++ == '[')
6364 {
6365 int n = 0;
6366 while (*p1 >= '0' && *p1 <= '9')
6367 n = n * 10 + *p1++ - '0';
6368 if (strncmp (p1, " times]\n", 8) == 0)
6369 return n+1;
6370 }
6371 return 0;
6372}
6373
6374
1e313f28
GM
6375/* Display an echo area message M with a specified length of NBYTES
6376 bytes. The string may include null characters. If M is 0, clear
6377 out any existing message, and let the mini-buffer text show
6378 through.
5f5c8ee5
GM
6379
6380 The buffer M must continue to exist until after the echo area gets
6381 cleared or some other message gets displayed there. This means do
6382 not pass text that is stored in a Lisp string; do not pass text in
6383 a buffer that was alloca'd. */
6384
6385void
1e313f28 6386message2 (m, nbytes, multibyte)
50f80c2f 6387 const char *m;
1e313f28 6388 int nbytes;
5f5c8ee5
GM
6389 int multibyte;
6390{
6391 /* First flush out any partial line written with print. */
6392 message_log_maybe_newline ();
6393 if (m)
1e313f28
GM
6394 message_dolog (m, nbytes, 1, multibyte);
6395 message2_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
6396}
6397
6398
6399/* The non-logging counterpart of message2. */
6400
6401void
1e313f28 6402message2_nolog (m, nbytes, multibyte)
50f80c2f 6403 const char *m;
e3383b6f 6404 int nbytes, multibyte;
5f5c8ee5 6405{
886bd6f2 6406 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6407 message_enable_multibyte = multibyte;
6408
6409 if (noninteractive)
6410 {
6411 if (noninteractive_need_newline)
6412 putc ('\n', stderr);
6413 noninteractive_need_newline = 0;
6414 if (m)
1e313f28 6415 fwrite (m, nbytes, 1, stderr);
5f5c8ee5
GM
6416 if (cursor_in_echo_area == 0)
6417 fprintf (stderr, "\n");
6418 fflush (stderr);
6419 }
6420 /* A null message buffer means that the frame hasn't really been
6421 initialized yet. Error messages get reported properly by
6422 cmd_error, so this must be just an informative message; toss it. */
2311178e 6423 else if (INTERACTIVE
886bd6f2
GM
6424 && sf->glyphs_initialized_p
6425 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
6426 {
6427 Lisp_Object mini_window;
6428 struct frame *f;
6429
6430 /* Get the frame containing the mini-buffer
6431 that the selected frame is using. */
886bd6f2 6432 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6433 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6434
6435 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 6436 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
6437 && ! FRAME_VISIBLE_P (f))
6438 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6439
6440 if (m)
6441 {
1e313f28 6442 set_message (m, Qnil, nbytes, multibyte);
5f5c8ee5
GM
6443 if (minibuffer_auto_raise)
6444 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6445 }
6446 else
c6e89d6c 6447 clear_message (1, 1);
5f5c8ee5 6448
c6e89d6c 6449 do_pending_window_change (0);
5f5c8ee5 6450 echo_area_display (1);
c6e89d6c 6451 do_pending_window_change (0);
5f5c8ee5
GM
6452 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6453 (*frame_up_to_date_hook) (f);
6454 }
6455}
6456
6457
c6e89d6c
GM
6458/* Display an echo area message M with a specified length of NBYTES
6459 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
6460 string, clear out any existing message, and let the mini-buffer
6461 text show through. */
6462
6463void
c6e89d6c 6464message3 (m, nbytes, multibyte)
5f5c8ee5 6465 Lisp_Object m;
c6e89d6c 6466 int nbytes;
5f5c8ee5
GM
6467 int multibyte;
6468{
6469 struct gcpro gcpro1;
6470
6471 GCPRO1 (m);
2311178e 6472
5f5c8ee5
GM
6473 /* First flush out any partial line written with print. */
6474 message_log_maybe_newline ();
6475 if (STRINGP (m))
2051c264 6476 message_dolog (SDATA (m), nbytes, 1, multibyte);
c6e89d6c 6477 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
6478
6479 UNGCPRO;
6480}
6481
6482
6483/* The non-logging version of message3. */
6484
6485void
c6e89d6c 6486message3_nolog (m, nbytes, multibyte)
5f5c8ee5 6487 Lisp_Object m;
c6e89d6c 6488 int nbytes, multibyte;
5f5c8ee5 6489{
886bd6f2 6490 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6491 message_enable_multibyte = multibyte;
6492
6493 if (noninteractive)
6494 {
6495 if (noninteractive_need_newline)
6496 putc ('\n', stderr);
6497 noninteractive_need_newline = 0;
6498 if (STRINGP (m))
2051c264 6499 fwrite (SDATA (m), nbytes, 1, stderr);
5f5c8ee5
GM
6500 if (cursor_in_echo_area == 0)
6501 fprintf (stderr, "\n");
6502 fflush (stderr);
6503 }
6504 /* A null message buffer means that the frame hasn't really been
6505 initialized yet. Error messages get reported properly by
6506 cmd_error, so this must be just an informative message; toss it. */
2311178e 6507 else if (INTERACTIVE
886bd6f2
GM
6508 && sf->glyphs_initialized_p
6509 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
6510 {
6511 Lisp_Object mini_window;
c6e89d6c 6512 Lisp_Object frame;
5f5c8ee5
GM
6513 struct frame *f;
6514
6515 /* Get the frame containing the mini-buffer
6516 that the selected frame is using. */
886bd6f2 6517 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6518 frame = XWINDOW (mini_window)->frame;
6519 f = XFRAME (frame);
5f5c8ee5
GM
6520
6521 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 6522 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
6523 && !FRAME_VISIBLE_P (f))
6524 Fmake_frame_visible (frame);
5f5c8ee5 6525
2051c264 6526 if (STRINGP (m) && SCHARS (m) > 0)
5f5c8ee5 6527 {
c6e89d6c 6528 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
6529 if (minibuffer_auto_raise)
6530 Fraise_frame (frame);
5f5c8ee5
GM
6531 }
6532 else
c6e89d6c 6533 clear_message (1, 1);
5f5c8ee5 6534
c6e89d6c 6535 do_pending_window_change (0);
5f5c8ee5 6536 echo_area_display (1);
c6e89d6c 6537 do_pending_window_change (0);
5f5c8ee5
GM
6538 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6539 (*frame_up_to_date_hook) (f);
6540 }
6541}
6542
6543
6544/* Display a null-terminated echo area message M. If M is 0, clear
6545 out any existing message, and let the mini-buffer text show through.
6546
6547 The buffer M must continue to exist until after the echo area gets
6548 cleared or some other message gets displayed there. Do not pass
6549 text that is stored in a Lisp string. Do not pass text in a buffer
6550 that was alloca'd. */
6551
6552void
6553message1 (m)
6554 char *m;
6555{
6556 message2 (m, (m ? strlen (m) : 0), 0);
6557}
6558
6559
6560/* The non-logging counterpart of message1. */
6561
6562void
6563message1_nolog (m)
6564 char *m;
6565{
6566 message2_nolog (m, (m ? strlen (m) : 0), 0);
6567}
6568
6569/* Display a message M which contains a single %s
6570 which gets replaced with STRING. */
6571
6572void
6573message_with_string (m, string, log)
6574 char *m;
6575 Lisp_Object string;
6576 int log;
6577{
5b6d51b6
RS
6578 CHECK_STRING (string);
6579
5f5c8ee5
GM
6580 if (noninteractive)
6581 {
6582 if (m)
6583 {
6584 if (noninteractive_need_newline)
6585 putc ('\n', stderr);
6586 noninteractive_need_newline = 0;
2051c264 6587 fprintf (stderr, m, SDATA (string));
5f5c8ee5
GM
6588 if (cursor_in_echo_area == 0)
6589 fprintf (stderr, "\n");
6590 fflush (stderr);
6591 }
6592 }
6593 else if (INTERACTIVE)
6594 {
6595 /* The frame whose minibuffer we're going to display the message on.
6596 It may be larger than the selected frame, so we need
6597 to use its buffer, not the selected frame's buffer. */
6598 Lisp_Object mini_window;
886bd6f2 6599 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6600
6601 /* Get the frame containing the minibuffer
6602 that the selected frame is using. */
886bd6f2 6603 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6604 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6605
6606 /* A null message buffer means that the frame hasn't really been
6607 initialized yet. Error messages get reported properly by
6608 cmd_error, so this must be just an informative message; toss it. */
6609 if (FRAME_MESSAGE_BUF (f))
6610 {
eb484132
GM
6611 Lisp_Object args[2], message;
6612 struct gcpro gcpro1, gcpro2;
5f5c8ee5 6613
eb484132
GM
6614 args[0] = build_string (m);
6615 args[1] = message = string;
78e17433 6616 GCPRO2 (args[0], message);
eb484132 6617 gcpro1.nvars = 2;
2311178e 6618
eb484132 6619 message = Fformat (2, args);
5f5c8ee5
GM
6620
6621 if (log)
d5db4077 6622 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
5f5c8ee5 6623 else
d5db4077 6624 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
eb484132
GM
6625
6626 UNGCPRO;
5f5c8ee5
GM
6627
6628 /* Print should start at the beginning of the message
6629 buffer next time. */
6630 message_buf_print = 0;
6631 }
6632 }
6633}
6634
6635
5f5c8ee5
GM
6636/* Dump an informative message to the minibuf. If M is 0, clear out
6637 any existing message, and let the mini-buffer text show through. */
6638
6639/* VARARGS 1 */
6640void
6641message (m, a1, a2, a3)
6642 char *m;
6643 EMACS_INT a1, a2, a3;
6644{
6645 if (noninteractive)
6646 {
6647 if (m)
6648 {
6649 if (noninteractive_need_newline)
6650 putc ('\n', stderr);
6651 noninteractive_need_newline = 0;
6652 fprintf (stderr, m, a1, a2, a3);
6653 if (cursor_in_echo_area == 0)
6654 fprintf (stderr, "\n");
6655 fflush (stderr);
6656 }
6657 }
6658 else if (INTERACTIVE)
6659 {
6660 /* The frame whose mini-buffer we're going to display the message
6661 on. It may be larger than the selected frame, so we need to
6662 use its buffer, not the selected frame's buffer. */
6663 Lisp_Object mini_window;
886bd6f2 6664 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6665
6666 /* Get the frame containing the mini-buffer
6667 that the selected frame is using. */
886bd6f2 6668 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6669 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6670
6671 /* A null message buffer means that the frame hasn't really been
6672 initialized yet. Error messages get reported properly by
6673 cmd_error, so this must be just an informative message; toss
6674 it. */
6675 if (FRAME_MESSAGE_BUF (f))
6676 {
6677 if (m)
6678 {
6679 int len;
6680#ifdef NO_ARG_ARRAY
6681 char *a[3];
6682 a[0] = (char *) a1;
6683 a[1] = (char *) a2;
6684 a[2] = (char *) a3;
6685
6686 len = doprnt (FRAME_MESSAGE_BUF (f),
6687 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6688#else
6689 len = doprnt (FRAME_MESSAGE_BUF (f),
6690 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6691 (char **) &a1);
6692#endif /* NO_ARG_ARRAY */
6693
6694 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6695 }
6696 else
6697 message1 (0);
6698
6699 /* Print should start at the beginning of the message
6700 buffer next time. */
6701 message_buf_print = 0;
6702 }
6703 }
6704}
6705
6706
6707/* The non-logging version of message. */
6708
6709void
6710message_nolog (m, a1, a2, a3)
6711 char *m;
6712 EMACS_INT a1, a2, a3;
6713{
6714 Lisp_Object old_log_max;
6715 old_log_max = Vmessage_log_max;
6716 Vmessage_log_max = Qnil;
6717 message (m, a1, a2, a3);
6718 Vmessage_log_max = old_log_max;
6719}
6720
6721
c6e89d6c
GM
6722/* Display the current message in the current mini-buffer. This is
6723 only called from error handlers in process.c, and is not time
6724 critical. */
5f5c8ee5
GM
6725
6726void
6727update_echo_area ()
6728{
c6e89d6c
GM
6729 if (!NILP (echo_area_buffer[0]))
6730 {
6731 Lisp_Object string;
6732 string = Fcurrent_message ();
2311178e 6733 message3 (string, SBYTES (string),
c6e89d6c
GM
6734 !NILP (current_buffer->enable_multibyte_characters));
6735 }
6736}
6737
6738
a67e162b
RS
6739/* Make sure echo area buffers in `echo_buffers' are live.
6740 If they aren't, make new ones. */
5bcfeb49
GM
6741
6742static void
6743ensure_echo_area_buffers ()
6744{
6745 int i;
6746
6747 for (i = 0; i < 2; ++i)
6748 if (!BUFFERP (echo_buffer[i])
6749 || NILP (XBUFFER (echo_buffer[i])->name))
6750 {
6751 char name[30];
ff3d9573
GM
6752 Lisp_Object old_buffer;
6753 int j;
6754
6755 old_buffer = echo_buffer[i];
5bcfeb49
GM
6756 sprintf (name, " *Echo Area %d*", i);
6757 echo_buffer[i] = Fget_buffer_create (build_string (name));
ad4f174e 6758 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
ff3d9573
GM
6759
6760 for (j = 0; j < 2; ++j)
6761 if (EQ (old_buffer, echo_area_buffer[j]))
6762 echo_area_buffer[j] = echo_buffer[i];
5bcfeb49
GM
6763 }
6764}
6765
6766
23a96c77 6767/* Call FN with args A1..A4 with either the current or last displayed
c6e89d6c
GM
6768 echo_area_buffer as current buffer.
6769
6770 WHICH zero means use the current message buffer
6771 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6772 from echo_buffer[] and clear it.
6773
6774 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6775 suitable buffer from echo_buffer[] and clear it.
6776
6777 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6778 that the current message becomes the last displayed one, make
6779 choose a suitable buffer for echo_area_buffer[0], and clear it.
6780
4b41cebb 6781 Value is what FN returns. */
c6e89d6c
GM
6782
6783static int
23a96c77 6784with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
c6e89d6c
GM
6785 struct window *w;
6786 int which;
23dd2d97
KR
6787 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6788 EMACS_INT a1;
6789 Lisp_Object a2;
6790 EMACS_INT a3, a4;
c6e89d6c
GM
6791{
6792 Lisp_Object buffer;
15e26c76 6793 int this_one, the_other, clear_buffer_p, rc;
331379bf 6794 int count = SPECPDL_INDEX ();
c6e89d6c 6795
9583b2bb 6796 /* If buffers aren't live, make new ones. */
5bcfeb49 6797 ensure_echo_area_buffers ();
c6e89d6c
GM
6798
6799 clear_buffer_p = 0;
2311178e 6800
c6e89d6c
GM
6801 if (which == 0)
6802 this_one = 0, the_other = 1;
6803 else if (which > 0)
6804 this_one = 1, the_other = 0;
5f5c8ee5 6805 else
c6e89d6c
GM
6806 {
6807 this_one = 0, the_other = 1;
6808 clear_buffer_p = 1;
2311178e 6809
c6e89d6c
GM
6810 /* We need a fresh one in case the current echo buffer equals
6811 the one containing the last displayed echo area message. */
6812 if (!NILP (echo_area_buffer[this_one])
6813 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6814 echo_area_buffer[this_one] = Qnil;
c6e89d6c
GM
6815 }
6816
6817 /* Choose a suitable buffer from echo_buffer[] is we don't
6818 have one. */
6819 if (NILP (echo_area_buffer[this_one]))
6820 {
6821 echo_area_buffer[this_one]
6822 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6823 ? echo_buffer[the_other]
6824 : echo_buffer[this_one]);
6825 clear_buffer_p = 1;
6826 }
6827
6828 buffer = echo_area_buffer[this_one];
6829
1013f4e3
GM
6830 /* Don't get confused by reusing the buffer used for echoing
6831 for a different purpose. */
032906b1 6832 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
1013f4e3
GM
6833 cancel_echoing ();
6834
c6e89d6c
GM
6835 record_unwind_protect (unwind_with_echo_area_buffer,
6836 with_echo_area_buffer_unwind_data (w));
6837
6838 /* Make the echo area buffer current. Note that for display
6839 purposes, it is not necessary that the displayed window's buffer
6840 == current_buffer, except for text property lookup. So, let's
6841 only set that buffer temporarily here without doing a full
6842 Fset_window_buffer. We must also change w->pointm, though,
6843 because otherwise an assertions in unshow_buffer fails, and Emacs
6844 aborts. */
9142dd5b 6845 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
6846 if (w)
6847 {
6848 w->buffer = buffer;
6849 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6850 }
ad4f174e 6851
c6e89d6c
GM
6852 current_buffer->undo_list = Qt;
6853 current_buffer->read_only = Qnil;
bbbf6d06 6854 specbind (Qinhibit_read_only, Qt);
0b04fa5f 6855 specbind (Qinhibit_modification_hooks, Qt);
c6e89d6c
GM
6856
6857 if (clear_buffer_p && Z > BEG)
6858 del_range (BEG, Z);
6859
6860 xassert (BEGV >= BEG);
6861 xassert (ZV <= Z && ZV >= BEGV);
6862
23a96c77 6863 rc = fn (a1, a2, a3, a4);
c6e89d6c
GM
6864
6865 xassert (BEGV >= BEG);
6866 xassert (ZV <= Z && ZV >= BEGV);
6867
6868 unbind_to (count, Qnil);
6869 return rc;
5f5c8ee5
GM
6870}
6871
6872
c6e89d6c
GM
6873/* Save state that should be preserved around the call to the function
6874 FN called in with_echo_area_buffer. */
5f5c8ee5 6875
c6e89d6c
GM
6876static Lisp_Object
6877with_echo_area_buffer_unwind_data (w)
6878 struct window *w;
5f5c8ee5 6879{
c6e89d6c
GM
6880 int i = 0;
6881 Lisp_Object vector;
5f5c8ee5 6882
c6e89d6c
GM
6883 /* Reduce consing by keeping one vector in
6884 Vwith_echo_area_save_vector. */
6885 vector = Vwith_echo_area_save_vector;
6886 Vwith_echo_area_save_vector = Qnil;
2311178e 6887
c6e89d6c 6888 if (NILP (vector))
9142dd5b 6889 vector = Fmake_vector (make_number (7), Qnil);
2311178e 6890
a61b7058
GM
6891 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6892 AREF (vector, i) = Vdeactivate_mark, ++i;
6893 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
2311178e 6894
c6e89d6c
GM
6895 if (w)
6896 {
a61b7058
GM
6897 XSETWINDOW (AREF (vector, i), w); ++i;
6898 AREF (vector, i) = w->buffer; ++i;
6899 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6900 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
c6e89d6c
GM
6901 }
6902 else
6903 {
6904 int end = i + 4;
a61b7058
GM
6905 for (; i < end; ++i)
6906 AREF (vector, i) = Qnil;
c6e89d6c 6907 }
5f5c8ee5 6908
a61b7058 6909 xassert (i == ASIZE (vector));
c6e89d6c
GM
6910 return vector;
6911}
5f5c8ee5 6912
5f5c8ee5 6913
c6e89d6c
GM
6914/* Restore global state from VECTOR which was created by
6915 with_echo_area_buffer_unwind_data. */
6916
6917static Lisp_Object
6918unwind_with_echo_area_buffer (vector)
6919 Lisp_Object vector;
6920{
bbbf6d06
GM
6921 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6922 Vdeactivate_mark = AREF (vector, 1);
6923 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
c6e89d6c 6924
bbbf6d06 6925 if (WINDOWP (AREF (vector, 3)))
c6e89d6c
GM
6926 {
6927 struct window *w;
6928 Lisp_Object buffer, charpos, bytepos;
2311178e 6929
bbbf6d06
GM
6930 w = XWINDOW (AREF (vector, 3));
6931 buffer = AREF (vector, 4);
6932 charpos = AREF (vector, 5);
6933 bytepos = AREF (vector, 6);
2311178e 6934
c6e89d6c
GM
6935 w->buffer = buffer;
6936 set_marker_both (w->pointm, buffer,
6937 XFASTINT (charpos), XFASTINT (bytepos));
6938 }
6939
6940 Vwith_echo_area_save_vector = vector;
6941 return Qnil;
6942}
6943
6944
6945/* Set up the echo area for use by print functions. MULTIBYTE_P
6946 non-zero means we will print multibyte. */
6947
6948void
6949setup_echo_area_for_printing (multibyte_p)
6950 int multibyte_p;
6951{
03b0a4b4
RS
6952 /* If we can't find an echo area any more, exit. */
6953 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6954 Fkill_emacs (Qnil);
6955
5bcfeb49
GM
6956 ensure_echo_area_buffers ();
6957
c6e89d6c
GM
6958 if (!message_buf_print)
6959 {
6960 /* A message has been output since the last time we printed.
6961 Choose a fresh echo area buffer. */
6962 if (EQ (echo_area_buffer[1], echo_buffer[0]))
2311178e 6963 echo_area_buffer[0] = echo_buffer[1];
c6e89d6c
GM
6964 else
6965 echo_area_buffer[0] = echo_buffer[0];
6966
6967 /* Switch to that buffer and clear it. */
6968 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
ab2c5f0a 6969 current_buffer->truncate_lines = Qnil;
2311178e 6970
c6e89d6c 6971 if (Z > BEG)
bbbf6d06 6972 {
331379bf 6973 int count = SPECPDL_INDEX ();
bbbf6d06 6974 specbind (Qinhibit_read_only, Qt);
a67e162b 6975 /* Note that undo recording is always disabled. */
bbbf6d06
GM
6976 del_range (BEG, Z);
6977 unbind_to (count, Qnil);
6978 }
c6e89d6c
GM
6979 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6980
6981 /* Set up the buffer for the multibyteness we need. */
6982 if (multibyte_p
6983 != !NILP (current_buffer->enable_multibyte_characters))
6984 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6985
6986 /* Raise the frame containing the echo area. */
6987 if (minibuffer_auto_raise)
6988 {
886bd6f2 6989 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 6990 Lisp_Object mini_window;
886bd6f2 6991 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6992 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6993 }
6994
8a4e3c0c 6995 message_log_maybe_newline ();
c6e89d6c
GM
6996 message_buf_print = 1;
6997 }
fa77249f
GM
6998 else
6999 {
7000 if (NILP (echo_area_buffer[0]))
7001 {
7002 if (EQ (echo_area_buffer[1], echo_buffer[0]))
2311178e 7003 echo_area_buffer[0] = echo_buffer[1];
fa77249f
GM
7004 else
7005 echo_area_buffer[0] = echo_buffer[0];
7006 }
2311178e 7007
fa77249f 7008 if (current_buffer != XBUFFER (echo_area_buffer[0]))
ab2c5f0a
GM
7009 {
7010 /* Someone switched buffers between print requests. */
7011 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7012 current_buffer->truncate_lines = Qnil;
7013 }
fa77249f 7014 }
c6e89d6c
GM
7015}
7016
7017
dd2eb166
GM
7018/* Display an echo area message in window W. Value is non-zero if W's
7019 height is changed. If display_last_displayed_message_p is
7020 non-zero, display the message that was last displayed, otherwise
7021 display the current message. */
c6e89d6c
GM
7022
7023static int
7024display_echo_area (w)
7025 struct window *w;
7026{
25edb08f
GM
7027 int i, no_message_p, window_height_changed_p, count;
7028
7029 /* Temporarily disable garbage collections while displaying the echo
7030 area. This is done because a GC can print a message itself.
7031 That message would modify the echo area buffer's contents while a
7032 redisplay of the buffer is going on, and seriously confuse
7033 redisplay. */
7034 count = inhibit_garbage_collection ();
dd2eb166
GM
7035
7036 /* If there is no message, we must call display_echo_area_1
7037 nevertheless because it resizes the window. But we will have to
7038 reset the echo_area_buffer in question to nil at the end because
7039 with_echo_area_buffer will sets it to an empty buffer. */
7040 i = display_last_displayed_message_p ? 1 : 0;
7041 no_message_p = NILP (echo_area_buffer[i]);
2311178e 7042
dd2eb166
GM
7043 window_height_changed_p
7044 = with_echo_area_buffer (w, display_last_displayed_message_p,
23a96c77 7045 display_echo_area_1,
23dd2d97 7046 (EMACS_INT) w, Qnil, 0, 0);
dd2eb166
GM
7047
7048 if (no_message_p)
7049 echo_area_buffer[i] = Qnil;
25edb08f
GM
7050
7051 unbind_to (count, Qnil);
dd2eb166 7052 return window_height_changed_p;
c6e89d6c
GM
7053}
7054
7055
7056/* Helper for display_echo_area. Display the current buffer which
23a96c77
GM
7057 contains the current echo area message in window W, a mini-window,
7058 a pointer to which is passed in A1. A2..A4 are currently not used.
c6e89d6c
GM
7059 Change the height of W so that all of the message is displayed.
7060 Value is non-zero if height of W was changed. */
7061
7062static int
23a96c77 7063display_echo_area_1 (a1, a2, a3, a4)
23dd2d97
KR
7064 EMACS_INT a1;
7065 Lisp_Object a2;
7066 EMACS_INT a3, a4;
c6e89d6c 7067{
23a96c77 7068 struct window *w = (struct window *) a1;
c6e89d6c 7069 Lisp_Object window;
c6e89d6c
GM
7070 struct text_pos start;
7071 int window_height_changed_p = 0;
7072
7073 /* Do this before displaying, so that we have a large enough glyph
7074 matrix for the display. */
92a90e89 7075 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c
GM
7076
7077 /* Display. */
7078 clear_glyph_matrix (w->desired_matrix);
7079 XSETWINDOW (window, w);
7080 SET_TEXT_POS (start, BEG, BEG_BYTE);
7081 try_window (window, start);
7082
c6e89d6c
GM
7083 return window_height_changed_p;
7084}
7085
7086
92a90e89 7087/* Resize the echo area window to exactly the size needed for the
6d004fea
GM
7088 currently displayed message, if there is one. If a mini-buffer
7089 is active, don't shrink it. */
92a90e89
GM
7090
7091void
308a74d8 7092resize_echo_area_exactly ()
92a90e89
GM
7093{
7094 if (BUFFERP (echo_area_buffer[0])
7095 && WINDOWP (echo_area_window))
7096 {
7097 struct window *w = XWINDOW (echo_area_window);
7098 int resized_p;
6d004fea
GM
7099 Lisp_Object resize_exactly;
7100
7101 if (minibuf_level == 0)
7102 resize_exactly = Qt;
7103 else
7104 resize_exactly = Qnil;
2311178e 7105
23a96c77 7106 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6d004fea 7107 (EMACS_INT) w, resize_exactly, 0, 0);
92a90e89
GM
7108 if (resized_p)
7109 {
7110 ++windows_or_buffers_changed;
7111 ++update_mode_lines;
7112 redisplay_internal (0);
7113 }
7114 }
7115}
7116
7117
23a96c77 7118/* Callback function for with_echo_area_buffer, when used from
308a74d8 7119 resize_echo_area_exactly. A1 contains a pointer to the window to
6d004fea
GM
7120 resize, EXACTLY non-nil means resize the mini-window exactly to the
7121 size of the text displayed. A3 and A4 are not used. Value is what
7122 resize_mini_window returns. */
23a96c77
GM
7123
7124static int
6d004fea 7125resize_mini_window_1 (a1, exactly, a3, a4)
23dd2d97 7126 EMACS_INT a1;
6d004fea 7127 Lisp_Object exactly;
23dd2d97 7128 EMACS_INT a3, a4;
23a96c77 7129{
6d004fea 7130 return resize_mini_window ((struct window *) a1, !NILP (exactly));
23a96c77
GM
7131}
7132
7133
92a90e89
GM
7134/* Resize mini-window W to fit the size of its contents. EXACT:P
7135 means size the window exactly to the size needed. Otherwise, it's
7136 only enlarged until W's buffer is empty. Value is non-zero if
4b41cebb 7137 the window height has been changed. */
c6e89d6c 7138
9472f927 7139int
92a90e89 7140resize_mini_window (w, exact_p)
c6e89d6c 7141 struct window *w;
92a90e89 7142 int exact_p;
c6e89d6c
GM
7143{
7144 struct frame *f = XFRAME (w->frame);
7145 int window_height_changed_p = 0;
7146
7147 xassert (MINI_WINDOW_P (w));
97cafc0f 7148
2913a9c0
GM
7149 /* Don't resize windows while redisplaying a window; it would
7150 confuse redisplay functions when the size of the window they are
7151 displaying changes from under them. Such a resizing can happen,
7152 for instance, when which-func prints a long message while
7153 we are running fontification-functions. We're running these
7154 functions with safe_call which binds inhibit-redisplay to t. */
7155 if (!NILP (Vinhibit_redisplay))
64c5be50 7156 return 0;
2311178e 7157
97cafc0f 7158 /* Nil means don't try to resize. */
6422c1d7 7159 if (NILP (Vresize_mini_windows)
fa3c6b4d 7160 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
97cafc0f 7161 return 0;
2311178e 7162
c6e89d6c
GM
7163 if (!FRAME_MINIBUF_ONLY_P (f))
7164 {
7165 struct it it;
dd2eb166 7166 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
da8b7f4f 7167 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
dd2eb166 7168 int height, max_height;
da8b7f4f 7169 int unit = FRAME_LINE_HEIGHT (f);
dd2eb166 7170 struct text_pos start;
1bfdbe43
GM
7171 struct buffer *old_current_buffer = NULL;
7172
7173 if (current_buffer != XBUFFER (w->buffer))
7174 {
7175 old_current_buffer = current_buffer;
7176 set_buffer_internal (XBUFFER (w->buffer));
7177 }
9142dd5b 7178
c6e89d6c 7179 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 7180
dd2eb166
GM
7181 /* Compute the max. number of lines specified by the user. */
7182 if (FLOATP (Vmax_mini_window_height))
da8b7f4f 7183 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
dd2eb166
GM
7184 else if (INTEGERP (Vmax_mini_window_height))
7185 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
7186 else
7187 max_height = total_height / 4;
2311178e 7188
4b41cebb 7189 /* Correct that max. height if it's bogus. */
dd2eb166
GM
7190 max_height = max (1, max_height);
7191 max_height = min (total_height, max_height);
2311178e 7192
dd2eb166 7193 /* Find out the height of the text in the window. */
ad4f174e
GM
7194 if (it.truncate_lines_p)
7195 height = 1;
55b064bd 7196 else
ad4f174e
GM
7197 {
7198 last_height = 0;
7199 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7200 if (it.max_ascent == 0 && it.max_descent == 0)
7201 height = it.current_y + last_height;
7202 else
7203 height = it.current_y + it.max_ascent + it.max_descent;
3c4b7685 7204 height -= it.extra_line_spacing;
ad4f174e
GM
7205 height = (height + unit - 1) / unit;
7206 }
2311178e 7207
dd2eb166
GM
7208 /* Compute a suitable window start. */
7209 if (height > max_height)
7210 {
7211 height = max_height;
7212 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7213 move_it_vertically_backward (&it, (height - 1) * unit);
7214 start = it.current.pos;
7215 }
7216 else
7217 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7218 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 7219
6422c1d7 7220 if (EQ (Vresize_mini_windows, Qgrow_only))
dd2eb166 7221 {
6422c1d7
GM
7222 /* Let it grow only, until we display an empty message, in which
7223 case the window shrinks again. */
da8b7f4f 7224 if (height > WINDOW_TOTAL_LINES (w))
6422c1d7 7225 {
da8b7f4f 7226 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7 7227 freeze_window_starts (f, 1);
da8b7f4f
KS
7228 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7229 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7230 }
da8b7f4f 7231 else if (height < WINDOW_TOTAL_LINES (w)
6422c1d7
GM
7232 && (exact_p || BEGV == ZV))
7233 {
da8b7f4f 7234 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7
GM
7235 freeze_window_starts (f, 0);
7236 shrink_mini_window (w);
da8b7f4f 7237 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7238 }
da448723 7239 }
2311178e 7240 else
da448723 7241 {
6422c1d7 7242 /* Always resize to exact size needed. */
da8b7f4f 7243 if (height > WINDOW_TOTAL_LINES (w))
6422c1d7 7244 {
da8b7f4f 7245 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7 7246 freeze_window_starts (f, 1);
da8b7f4f
KS
7247 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7248 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7249 }
da8b7f4f 7250 else if (height < WINDOW_TOTAL_LINES (w))
6422c1d7 7251 {
da8b7f4f 7252 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7
GM
7253 freeze_window_starts (f, 0);
7254 shrink_mini_window (w);
7255
7256 if (height)
7257 {
7258 freeze_window_starts (f, 1);
da8b7f4f 7259 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
6422c1d7 7260 }
2311178e 7261
da8b7f4f 7262 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7263 }
9142dd5b 7264 }
1bfdbe43
GM
7265
7266 if (old_current_buffer)
7267 set_buffer_internal (old_current_buffer);
c6e89d6c
GM
7268 }
7269
7270 return window_height_changed_p;
7271}
7272
7273
7274/* Value is the current message, a string, or nil if there is no
7275 current message. */
7276
7277Lisp_Object
7278current_message ()
7279{
7280 Lisp_Object msg;
7281
7282 if (NILP (echo_area_buffer[0]))
7283 msg = Qnil;
7284 else
7285 {
23a96c77 7286 with_echo_area_buffer (0, 0, current_message_1,
23dd2d97 7287 (EMACS_INT) &msg, Qnil, 0, 0);
c6e89d6c
GM
7288 if (NILP (msg))
7289 echo_area_buffer[0] = Qnil;
7290 }
2311178e 7291
c6e89d6c
GM
7292 return msg;
7293}
7294
7295
7296static int
23a96c77 7297current_message_1 (a1, a2, a3, a4)
23dd2d97
KR
7298 EMACS_INT a1;
7299 Lisp_Object a2;
7300 EMACS_INT a3, a4;
c6e89d6c 7301{
23a96c77 7302 Lisp_Object *msg = (Lisp_Object *) a1;
2311178e 7303
c6e89d6c
GM
7304 if (Z > BEG)
7305 *msg = make_buffer_string (BEG, Z, 1);
7306 else
7307 *msg = Qnil;
7308 return 0;
7309}
7310
7311
7312/* Push the current message on Vmessage_stack for later restauration
7313 by restore_message. Value is non-zero if the current message isn't
7314 empty. This is a relatively infrequent operation, so it's not
7315 worth optimizing. */
7316
7317int
7318push_message ()
7319{
7320 Lisp_Object msg;
7321 msg = current_message ();
7322 Vmessage_stack = Fcons (msg, Vmessage_stack);
7323 return STRINGP (msg);
7324}
7325
7326
7327/* Restore message display from the top of Vmessage_stack. */
7328
7329void
7330restore_message ()
7331{
7332 Lisp_Object msg;
2311178e 7333
c6e89d6c
GM
7334 xassert (CONSP (Vmessage_stack));
7335 msg = XCAR (Vmessage_stack);
7336 if (STRINGP (msg))
d5db4077 7337 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
c6e89d6c
GM
7338 else
7339 message3_nolog (msg, 0, 0);
7340}
7341
7342
37d66095
RS
7343/* Handler for record_unwind_protect calling pop_message. */
7344
7345Lisp_Object
7346pop_message_unwind (dummy)
7347 Lisp_Object dummy;
7348{
7349 pop_message ();
7350 return Qnil;
7351}
7352
c6e89d6c
GM
7353/* Pop the top-most entry off Vmessage_stack. */
7354
7355void
7356pop_message ()
7357{
7358 xassert (CONSP (Vmessage_stack));
7359 Vmessage_stack = XCDR (Vmessage_stack);
7360}
7361
7362
7363/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7364 exits. If the stack is not empty, we have a missing pop_message
7365 somewhere. */
7366
7367void
7368check_message_stack ()
7369{
7370 if (!NILP (Vmessage_stack))
7371 abort ();
7372}
7373
7374
7375/* Truncate to NCHARS what will be displayed in the echo area the next
7376 time we display it---but don't redisplay it now. */
7377
7378void
7379truncate_echo_area (nchars)
7380 int nchars;
7381{
7382 if (nchars == 0)
7383 echo_area_buffer[0] = Qnil;
7384 /* A null message buffer means that the frame hasn't really been
7385 initialized yet. Error messages get reported properly by
7386 cmd_error, so this must be just an informative message; toss it. */
7387 else if (!noninteractive
7388 && INTERACTIVE
c6e89d6c 7389 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
7390 {
7391 struct frame *sf = SELECTED_FRAME ();
7392 if (FRAME_MESSAGE_BUF (sf))
23dd2d97 7393 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
886bd6f2 7394 }
c6e89d6c
GM
7395}
7396
7397
7398/* Helper function for truncate_echo_area. Truncate the current
7399 message to at most NCHARS characters. */
7400
7401static int
23a96c77 7402truncate_message_1 (nchars, a2, a3, a4)
23dd2d97
KR
7403 EMACS_INT nchars;
7404 Lisp_Object a2;
7405 EMACS_INT a3, a4;
c6e89d6c
GM
7406{
7407 if (BEG + nchars < Z)
7408 del_range (BEG + nchars, Z);
7409 if (Z == BEG)
7410 echo_area_buffer[0] = Qnil;
7411 return 0;
7412}
7413
7414
7415/* Set the current message to a substring of S or STRING.
7416
7417 If STRING is a Lisp string, set the message to the first NBYTES
7418 bytes from STRING. NBYTES zero means use the whole string. If
7419 STRING is multibyte, the message will be displayed multibyte.
7420
7421 If S is not null, set the message to the first LEN bytes of S. LEN
7422 zero means use the whole string. MULTIBYTE_P non-zero means S is
7423 multibyte. Display the message multibyte in that case. */
7424
7425void
7426set_message (s, string, nbytes, multibyte_p)
50f80c2f 7427 const char *s;
c6e89d6c 7428 Lisp_Object string;
e3383b6f 7429 int nbytes, multibyte_p;
c6e89d6c
GM
7430{
7431 message_enable_multibyte
7432 = ((s && multibyte_p)
7433 || (STRINGP (string) && STRING_MULTIBYTE (string)));
2311178e 7434
23a96c77
GM
7435 with_echo_area_buffer (0, -1, set_message_1,
7436 (EMACS_INT) s, string, nbytes, multibyte_p);
c6e89d6c 7437 message_buf_print = 0;
21fdfb65 7438 help_echo_showing_p = 0;
c6e89d6c
GM
7439}
7440
7441
7442/* Helper function for set_message. Arguments have the same meaning
23a96c77
GM
7443 as there, with A1 corresponding to S and A2 corresponding to STRING
7444 This function is called with the echo area buffer being
c6e89d6c
GM
7445 current. */
7446
7447static int
23a96c77 7448set_message_1 (a1, a2, nbytes, multibyte_p)
23dd2d97
KR
7449 EMACS_INT a1;
7450 Lisp_Object a2;
7451 EMACS_INT nbytes, multibyte_p;
c6e89d6c 7452{
50f80c2f 7453 const char *s = (const char *) a1;
23dd2d97 7454 Lisp_Object string = a2;
2311178e 7455
c6e89d6c 7456 xassert (BEG == Z);
2311178e 7457
c6e89d6c
GM
7458 /* Change multibyteness of the echo buffer appropriately. */
7459 if (message_enable_multibyte
7460 != !NILP (current_buffer->enable_multibyte_characters))
7461 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7462
ad4f174e 7463 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
2311178e 7464
c6e89d6c
GM
7465 /* Insert new message at BEG. */
7466 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7467
7468 if (STRINGP (string))
7469 {
7470 int nchars;
2311178e 7471
c6e89d6c 7472 if (nbytes == 0)
2051c264 7473 nbytes = SBYTES (string);
c6e89d6c 7474 nchars = string_byte_to_char (string, nbytes);
2311178e 7475
c6e89d6c
GM
7476 /* This function takes care of single/multibyte conversion. We
7477 just have to ensure that the echo area buffer has the right
7478 setting of enable_multibyte_characters. */
7479 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7480 }
7481 else if (s)
7482 {
7483 if (nbytes == 0)
7484 nbytes = strlen (s);
2311178e 7485
c6e89d6c
GM
7486 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7487 {
7488 /* Convert from multi-byte to single-byte. */
7489 int i, c, n;
7490 unsigned char work[1];
2311178e 7491
c6e89d6c
GM
7492 /* Convert a multibyte string to single-byte. */
7493 for (i = 0; i < nbytes; i += n)
7494 {
7495 c = string_char_and_length (s + i, nbytes - i, &n);
7496 work[0] = (SINGLE_BYTE_CHAR_P (c)
7497 ? c
7498 : multibyte_char_to_unibyte (c, Qnil));
7499 insert_1_both (work, 1, 1, 1, 0, 0);
7500 }
7501 }
7502 else if (!multibyte_p
7503 && !NILP (current_buffer->enable_multibyte_characters))
7504 {
7505 /* Convert from single-byte to multi-byte. */
7506 int i, c, n;
50f80c2f 7507 const unsigned char *msg = (const unsigned char *) s;
260a86a0 7508 unsigned char str[MAX_MULTIBYTE_LENGTH];
2311178e 7509
c6e89d6c
GM
7510 /* Convert a single-byte string to multibyte. */
7511 for (i = 0; i < nbytes; i++)
7512 {
7513 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
7514 n = CHAR_STRING (c, str);
7515 insert_1_both (str, 1, n, 1, 0, 0);
c6e89d6c
GM
7516 }
7517 }
7518 else
7519 insert_1 (s, nbytes, 1, 0, 0);
7520 }
7521
7522 return 0;
7523}
7524
7525
7526/* Clear messages. CURRENT_P non-zero means clear the current
7527 message. LAST_DISPLAYED_P non-zero means clear the message
7528 last displayed. */
7529
7530void
7531clear_message (current_p, last_displayed_p)
7532 int current_p, last_displayed_p;
7533{
7534 if (current_p)
6e019995
GM
7535 {
7536 echo_area_buffer[0] = Qnil;
7537 message_cleared_p = 1;
7538 }
2311178e 7539
c6e89d6c
GM
7540 if (last_displayed_p)
7541 echo_area_buffer[1] = Qnil;
2311178e 7542
c6e89d6c
GM
7543 message_buf_print = 0;
7544}
7545
7546/* Clear garbaged frames.
7547
7548 This function is used where the old redisplay called
7549 redraw_garbaged_frames which in turn called redraw_frame which in
7550 turn called clear_frame. The call to clear_frame was a source of
7551 flickering. I believe a clear_frame is not necessary. It should
7552 suffice in the new redisplay to invalidate all current matrices,
7553 and ensure a complete redisplay of all windows. */
7554
7555static void
7556clear_garbaged_frames ()
7557{
5f5c8ee5
GM
7558 if (frame_garbaged)
7559 {
5f5c8ee5 7560 Lisp_Object tail, frame;
5fb96e96 7561 int changed_count = 0;
2311178e 7562
5f5c8ee5
GM
7563 FOR_EACH_FRAME (tail, frame)
7564 {
7565 struct frame *f = XFRAME (frame);
2311178e 7566
5f5c8ee5
GM
7567 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7568 {
6bb95882 7569 if (f->resized_p)
573e57f1 7570 Fredraw_frame (frame);
5f5c8ee5 7571 clear_current_matrices (f);
5fb96e96 7572 changed_count++;
5f5c8ee5 7573 f->garbaged = 0;
6bb95882 7574 f->resized_p = 0;
5f5c8ee5
GM
7575 }
7576 }
7577
7578 frame_garbaged = 0;
5fb96e96
RS
7579 if (changed_count)
7580 ++windows_or_buffers_changed;
5f5c8ee5 7581 }
c6e89d6c 7582}
5f5c8ee5 7583
5f5c8ee5 7584
886bd6f2
GM
7585/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7586 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 7587 mini-windows height has been changed. */
5f5c8ee5 7588
c6e89d6c
GM
7589static int
7590echo_area_display (update_frame_p)
7591 int update_frame_p;
7592{
7593 Lisp_Object mini_window;
7594 struct window *w;
7595 struct frame *f;
7596 int window_height_changed_p = 0;
886bd6f2 7597 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 7598
886bd6f2 7599 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
7600 w = XWINDOW (mini_window);
7601 f = XFRAME (WINDOW_FRAME (w));
7602
7603 /* Don't display if frame is invisible or not yet initialized. */
7604 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7605 return 0;
5f5c8ee5 7606
1a578e9b 7607/* The terminal frame is used as the first Emacs frame on the Mac OS. */
e0f712ba 7608#ifndef MAC_OS8
1ab3e082 7609#ifdef HAVE_WINDOW_SYSTEM
c6e89d6c
GM
7610 /* When Emacs starts, selected_frame may be a visible terminal
7611 frame, even if we run under a window system. If we let this
7612 through, a message would be displayed on the terminal. */
2311178e 7613 if (EQ (selected_frame, Vterminal_frame)
622e3754 7614 && !NILP (Vwindow_system))
c6e89d6c 7615 return 0;
1ab3e082 7616#endif /* HAVE_WINDOW_SYSTEM */
1a578e9b 7617#endif
c6e89d6c
GM
7618
7619 /* Redraw garbaged frames. */
7620 if (frame_garbaged)
7621 clear_garbaged_frames ();
7622
7623 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7624 {
7625 echo_area_window = mini_window;
7626 window_height_changed_p = display_echo_area (w);
5f5c8ee5 7627 w->must_be_updated_p = 1;
c59c668a 7628
d4358b37
GM
7629 /* Update the display, unless called from redisplay_internal.
7630 Also don't update the screen during redisplay itself. The
7631 update will happen at the end of redisplay, and an update
7632 here could cause confusion. */
7633 if (update_frame_p && !redisplaying_p)
5f5c8ee5 7634 {
715e84c9 7635 int n = 0;
edc68111 7636
715e84c9
GM
7637 /* If the display update has been interrupted by pending
7638 input, update mode lines in the frame. Due to the
7639 pending input, it might have been that redisplay hasn't
7640 been called, so that mode lines above the echo area are
7641 garbaged. This looks odd, so we prevent it here. */
7642 if (!display_completed)
7643 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
2311178e 7644
8af1308e
GM
7645 if (window_height_changed_p
7646 /* Don't do this if Emacs is shutting down. Redisplay
7647 needs to run hooks. */
7648 && !NILP (Vrun_hooks))
c59c668a 7649 {
c06017fb
GM
7650 /* Must update other windows. Likewise as in other
7651 cases, don't let this update be interrupted by
7652 pending input. */
331379bf 7653 int count = SPECPDL_INDEX ();
c06017fb 7654 specbind (Qredisplay_dont_pause, Qt);
edc68111 7655 windows_or_buffers_changed = 1;
c59c668a 7656 redisplay_internal (0);
c06017fb 7657 unbind_to (count, Qnil);
c59c668a 7658 }
715e84c9 7659 else if (FRAME_WINDOW_P (f) && n == 0)
5f5c8ee5 7660 {
edc68111 7661 /* Window configuration is the same as before.
715e84c9
GM
7662 Can do with a display update of the echo area,
7663 unless we displayed some mode lines. */
5f5c8ee5
GM
7664 update_single_window (w, 1);
7665 rif->flush_display (f);
7666 }
7667 else
7668 update_frame (f, 1, 1);
31b6671b
GM
7669
7670 /* If cursor is in the echo area, make sure that the next
7671 redisplay displays the minibuffer, so that the cursor will
7672 be replaced with what the minibuffer wants. */
7673 if (cursor_in_echo_area)
7674 ++windows_or_buffers_changed;
5f5c8ee5
GM
7675 }
7676 }
7677 else if (!EQ (mini_window, selected_window))
7678 windows_or_buffers_changed++;
c59c668a
GM
7679
7680 /* Last displayed message is now the current message. */
dd2eb166 7681 echo_area_buffer[1] = echo_area_buffer[0];
2311178e 7682
5f5c8ee5
GM
7683 /* Prevent redisplay optimization in redisplay_internal by resetting
7684 this_line_start_pos. This is done because the mini-buffer now
7685 displays the message instead of its buffer text. */
7686 if (EQ (mini_window, selected_window))
7687 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
7688
7689 return window_height_changed_p;
5f5c8ee5
GM
7690}
7691
7692
7693\f
7694/***********************************************************************
7695 Frame Titles
7696 ***********************************************************************/
7697
7698
8143e6ab
KS
7699/* The frame title buffering code is also used by Fformat_mode_line.
7700 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
5f5c8ee5
GM
7701
7702/* A buffer for constructing frame titles in it; allocated from the
7703 heap in init_xdisp and resized as needed in store_frame_title_char. */
7704
7705static char *frame_title_buf;
7706
7707/* The buffer's end, and a current output position in it. */
7708
7709static char *frame_title_buf_end;
7710static char *frame_title_ptr;
7711
7712
7713/* Store a single character C for the frame title in frame_title_buf.
7714 Re-allocate frame_title_buf if necessary. */
7715
7716static void
5d15cc8f
DL
7717#ifdef PROTOTYPES
7718store_frame_title_char (char c)
7719#else
5f5c8ee5
GM
7720store_frame_title_char (c)
7721 char c;
5d15cc8f 7722#endif
5f5c8ee5
GM
7723{
7724 /* If output position has reached the end of the allocated buffer,
7725 double the buffer's size. */
7726 if (frame_title_ptr == frame_title_buf_end)
7727 {
7728 int len = frame_title_ptr - frame_title_buf;
7729 int new_size = 2 * len * sizeof *frame_title_buf;
7730 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7731 frame_title_buf_end = frame_title_buf + new_size;
7732 frame_title_ptr = frame_title_buf + len;
7733 }
7734
7735 *frame_title_ptr++ = c;
7736}
7737
7738
7739/* Store part of a frame title in frame_title_buf, beginning at
d26b89b8
KH
7740 frame_title_ptr. STR is the string to store. Do not copy
7741 characters that yield more columns than PRECISION; PRECISION <= 0
7742 means copy the whole string. Pad with spaces until FIELD_WIDTH
7743 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7744 pad. Called from display_mode_element when it is used to build a
7745 frame title. */
5f5c8ee5
GM
7746
7747static int
7748store_frame_title (str, field_width, precision)
50f80c2f 7749 const unsigned char *str;
5f5c8ee5
GM
7750 int field_width, precision;
7751{
7752 int n = 0;
3b552d56 7753 int dummy, nbytes;
5f5c8ee5
GM
7754
7755 /* Copy at most PRECISION chars from STR. */
d26b89b8
KH
7756 nbytes = strlen (str);
7757 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7758 while (nbytes--)
7759 store_frame_title_char (*str++);
5f5c8ee5
GM
7760
7761 /* Fill up with spaces until FIELD_WIDTH reached. */
7762 while (field_width > 0
7763 && n < field_width)
7764 {
7765 store_frame_title_char (' ');
7766 ++n;
7767 }
7768
7769 return n;
7770}
7771
8143e6ab 7772#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
7773
7774/* Set the title of FRAME, if it has changed. The title format is
7775 Vicon_title_format if FRAME is iconified, otherwise it is
7776 frame_title_format. */
7777
7778static void
7779x_consider_frame_title (frame)
7780 Lisp_Object frame;
7781{
7782 struct frame *f = XFRAME (frame);
7783
7784 if (FRAME_WINDOW_P (f)
7785 || FRAME_MINIBUF_ONLY_P (f)
7786 || f->explicit_name)
7787 {
7788 /* Do we have more than one visible frame on this X display? */
7789 Lisp_Object tail;
7790 Lisp_Object fmt;
7791 struct buffer *obuf;
7792 int len;
7793 struct it it;
7794
9472f927 7795 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 7796 {
74779f52
JR
7797 Lisp_Object other_frame = XCAR (tail);
7798 struct frame *tf = XFRAME (other_frame);
5f5c8ee5 7799
2311178e 7800 if (tf != f
5f5c8ee5
GM
7801 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7802 && !FRAME_MINIBUF_ONLY_P (tf)
74779f52 7803 && !EQ (other_frame, tip_frame)
5f5c8ee5
GM
7804 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7805 break;
7806 }
7807
7808 /* Set global variable indicating that multiple frames exist. */
7809 multiple_frames = CONSP (tail);
7810
7811 /* Switch to the buffer of selected window of the frame. Set up
7812 frame_title_ptr so that display_mode_element will output into it;
7813 then display the title. */
7814 obuf = current_buffer;
bb336f8d 7815 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
5f5c8ee5
GM
7816 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7817 frame_title_ptr = frame_title_buf;
7818 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7819 NULL, DEFAULT_FACE_ID);
c53a1624 7820 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
d26b89b8 7821 len = frame_title_ptr - frame_title_buf;
5f5c8ee5 7822 frame_title_ptr = NULL;
bb336f8d 7823 set_buffer_internal_1 (obuf);
5f5c8ee5
GM
7824
7825 /* Set the title only if it's changed. This avoids consing in
7826 the common case where it hasn't. (If it turns out that we've
7827 already wasted too much time by walking through the list with
7828 display_mode_element, then we might need to optimize at a
7829 higher level than this.) */
2311178e 7830 if (! STRINGP (f->name)
2051c264
GM
7831 || SBYTES (f->name) != len
7832 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
5f5c8ee5
GM
7833 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7834 }
7835}
7836
5f5c8ee5
GM
7837#endif /* not HAVE_WINDOW_SYSTEM */
7838
7839
7840
7841\f
7842/***********************************************************************
7843 Menu Bars
7844 ***********************************************************************/
7845
7846
7847/* Prepare for redisplay by updating menu-bar item lists when
7848 appropriate. This can call eval. */
7849
7850void
7851prepare_menu_bars ()
7852{
7853 int all_windows;
7854 struct gcpro gcpro1, gcpro2;
7855 struct frame *f;
6b5c4794 7856 Lisp_Object tooltip_frame;
5f5c8ee5 7857
86ffe5cd 7858#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
7859 tooltip_frame = tip_frame;
7860#else
6b5c4794 7861 tooltip_frame = Qnil;
5f5c8ee5
GM
7862#endif
7863
7864 /* Update all frame titles based on their buffer names, etc. We do
7865 this before the menu bars so that the buffer-menu will show the
7866 up-to-date frame titles. */
7867#ifdef HAVE_WINDOW_SYSTEM
7868 if (windows_or_buffers_changed || update_mode_lines)
7869 {
7870 Lisp_Object tail, frame;
7871
7872 FOR_EACH_FRAME (tail, frame)
7873 {
7874 f = XFRAME (frame);
6b5c4794 7875 if (!EQ (frame, tooltip_frame)
5f5c8ee5
GM
7876 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7877 x_consider_frame_title (frame);
7878 }
7879 }
7880#endif /* HAVE_WINDOW_SYSTEM */
7881
7882 /* Update the menu bar item lists, if appropriate. This has to be
7883 done before any actual redisplay or generation of display lines. */
2311178e 7884 all_windows = (update_mode_lines
5f5c8ee5
GM
7885 || buffer_shared > 1
7886 || windows_or_buffers_changed);
7887 if (all_windows)
7888 {
7889 Lisp_Object tail, frame;
331379bf 7890 int count = SPECPDL_INDEX ();
5f5c8ee5
GM
7891
7892 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7893
7894 FOR_EACH_FRAME (tail, frame)
7895 {
7896 f = XFRAME (frame);
7897
7898 /* Ignore tooltip frame. */
6b5c4794 7899 if (EQ (frame, tooltip_frame))
5f5c8ee5 7900 continue;
2311178e 7901
5f5c8ee5
GM
7902 /* If a window on this frame changed size, report that to
7903 the user and clear the size-change flag. */
7904 if (FRAME_WINDOW_SIZES_CHANGED (f))
7905 {
7906 Lisp_Object functions;
2311178e 7907
5f5c8ee5
GM
7908 /* Clear flag first in case we get an error below. */
7909 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7910 functions = Vwindow_size_change_functions;
7911 GCPRO2 (tail, functions);
2311178e 7912
5f5c8ee5
GM
7913 while (CONSP (functions))
7914 {
7915 call1 (XCAR (functions), frame);
7916 functions = XCDR (functions);
7917 }
7918 UNGCPRO;
7919 }
2311178e 7920
5f5c8ee5
GM
7921 GCPRO1 (tail);
7922 update_menu_bar (f, 0);
7923#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 7924 update_tool_bar (f, 0);
5f5c8ee5
GM
7925#endif
7926 UNGCPRO;
7927 }
7928
7929 unbind_to (count, Qnil);
7930 }
7931 else
7932 {
886bd6f2
GM
7933 struct frame *sf = SELECTED_FRAME ();
7934 update_menu_bar (sf, 1);
5f5c8ee5 7935#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 7936 update_tool_bar (sf, 1);
5f5c8ee5
GM
7937#endif
7938 }
7939
7940 /* Motif needs this. See comment in xmenu.c. Turn it off when
7941 pending_menu_activation is not defined. */
7942#ifdef USE_X_TOOLKIT
7943 pending_menu_activation = 0;
7944#endif
7945}
7946
7947
7948/* Update the menu bar item list for frame F. This has to be done
7949 before we start to fill in any display lines, because it can call
7950 eval.
7951
7952 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7953
7954static void
7955update_menu_bar (f, save_match_data)
7956 struct frame *f;
7957 int save_match_data;
7958{
7959 Lisp_Object window;
7960 register struct window *w;
7961
e1477f43
GM
7962 /* If called recursively during a menu update, do nothing. This can
7963 happen when, for instance, an activate-menubar-hook causes a
7964 redisplay. */
7965 if (inhibit_menubar_update)
7966 return;
7967
5f5c8ee5
GM
7968 window = FRAME_SELECTED_WINDOW (f);
7969 w = XWINDOW (window);
2311178e 7970
84f2e615 7971#if 0 /* The if statement below this if statement used to include the
a5f08374
RS
7972 condition !NILP (w->update_mode_line), rather than using
7973 update_mode_lines directly, and this if statement may have
7974 been added to make that condition work. Now the if
2311178e 7975 statement below matches its comment, this isn't needed. */
5f5c8ee5
GM
7976 if (update_mode_lines)
7977 w->update_mode_line = Qt;
a5f08374 7978#endif
5f5c8ee5
GM
7979
7980 if (FRAME_WINDOW_P (f)
7981 ?
488dd4c4
JD
7982#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7983 || defined (USE_GTK)
2311178e 7984 FRAME_EXTERNAL_MENU_BAR (f)
5f5c8ee5
GM
7985#else
7986 FRAME_MENU_BAR_LINES (f) > 0
7987#endif
7988 : FRAME_MENU_BAR_LINES (f) > 0)
7989 {
7990 /* If the user has switched buffers or windows, we need to
7991 recompute to reflect the new bindings. But we'll
7992 recompute when update_mode_lines is set too; that means
7993 that people can use force-mode-line-update to request
7994 that the menu bar be recomputed. The adverse effect on
7995 the rest of the redisplay algorithm is about the same as
7996 windows_or_buffers_changed anyway. */
7997 if (windows_or_buffers_changed
a5f08374
RS
7998 /* This used to test w->update_mode_line, but we believe
7999 there is no need to recompute the menu in that case. */
8000 || update_mode_lines
5f5c8ee5
GM
8001 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8002 < BUF_MODIFF (XBUFFER (w->buffer)))
8003 != !NILP (w->last_had_star))
8004 || ((!NILP (Vtransient_mark_mode)
8005 && !NILP (XBUFFER (w->buffer)->mark_active))
8006 != !NILP (w->region_showing)))
8007 {
8008 struct buffer *prev = current_buffer;
331379bf 8009 int count = SPECPDL_INDEX ();
5f5c8ee5 8010
e1477f43
GM
8011 specbind (Qinhibit_menubar_update, Qt);
8012
5f5c8ee5
GM
8013 set_buffer_internal_1 (XBUFFER (w->buffer));
8014 if (save_match_data)
8015 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8016 if (NILP (Voverriding_local_map_menu_flag))
8017 {
8018 specbind (Qoverriding_terminal_local_map, Qnil);
8019 specbind (Qoverriding_local_map, Qnil);
8020 }
8021
8022 /* Run the Lucid hook. */
65048e97 8023 safe_run_hooks (Qactivate_menubar_hook);
2311178e 8024
5f5c8ee5
GM
8025 /* If it has changed current-menubar from previous value,
8026 really recompute the menu-bar from the value. */
8027 if (! NILP (Vlucid_menu_bar_dirty_flag))
8028 call0 (Qrecompute_lucid_menubar);
2311178e 8029
5f5c8ee5
GM
8030 safe_run_hooks (Qmenu_bar_update_hook);
8031 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2311178e 8032
5f5c8ee5 8033 /* Redisplay the menu bar in case we changed it. */
488dd4c4
JD
8034#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8035 || defined (USE_GTK)
1a578e9b 8036 if (FRAME_WINDOW_P (f)
e0f712ba 8037#if defined (MAC_OS)
1a578e9b
AC
8038 /* All frames on Mac OS share the same menubar. So only the
8039 selected frame should be allowed to set it. */
8040 && f == SELECTED_FRAME ()
8041#endif
8042 )
5f5c8ee5
GM
8043 set_frame_menubar (f, 0, 0);
8044 else
8045 /* On a terminal screen, the menu bar is an ordinary screen
8046 line, and this makes it get updated. */
8047 w->update_mode_line = Qt;
488dd4c4 8048#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
5f5c8ee5
GM
8049 /* In the non-toolkit version, the menu bar is an ordinary screen
8050 line, and this makes it get updated. */
8051 w->update_mode_line = Qt;
488dd4c4 8052#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
5f5c8ee5
GM
8053
8054 unbind_to (count, Qnil);
8055 set_buffer_internal_1 (prev);
8056 }
8057 }
8058}
8059
8060
8061\f
fa3c6b4d
KS
8062/***********************************************************************
8063 Output Cursor
8064 ***********************************************************************/
8065
1dabd0cf
KS
8066#ifdef HAVE_WINDOW_SYSTEM
8067
fa3c6b4d
KS
8068/* EXPORT:
8069 Nominal cursor position -- where to draw output.
8070 HPOS and VPOS are window relative glyph matrix coordinates.
8071 X and Y are window relative pixel coordinates. */
8072
8073struct cursor_pos output_cursor;
8074
8075
8076/* EXPORT:
8077 Set the global variable output_cursor to CURSOR. All cursor
8078 positions are relative to updated_window. */
8079
8080void
8081set_output_cursor (cursor)
8082 struct cursor_pos *cursor;
8083{
8084 output_cursor.hpos = cursor->hpos;
8085 output_cursor.vpos = cursor->vpos;
8086 output_cursor.x = cursor->x;
8087 output_cursor.y = cursor->y;
8088}
8089
8090
8091/* EXPORT for RIF:
8092 Set a nominal cursor position.
8093
8094 HPOS and VPOS are column/row positions in a window glyph matrix. X
8095 and Y are window text area relative pixel positions.
8096
8097 If this is done during an update, updated_window will contain the
8098 window that is being updated and the position is the future output
8099 cursor position for that window. If updated_window is null, use
8100 selected_window and display the cursor at the given position. */
8101
8102void
8103x_cursor_to (vpos, hpos, y, x)
8104 int vpos, hpos, y, x;
8105{
8106 struct window *w;
8107
8108 /* If updated_window is not set, work on selected_window. */
8109 if (updated_window)
8110 w = updated_window;
8111 else
8112 w = XWINDOW (selected_window);
8113
8114 /* Set the output cursor. */
8115 output_cursor.hpos = hpos;
8116 output_cursor.vpos = vpos;
8117 output_cursor.x = x;
8118 output_cursor.y = y;
8119
8120 /* If not called as part of an update, really display the cursor.
8121 This will also set the cursor position of W. */
8122 if (updated_window == NULL)
8123 {
8124 BLOCK_INPUT;
8125 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8126 if (rif->flush_display_optional)
8127 rif->flush_display_optional (SELECTED_FRAME ());
8128 UNBLOCK_INPUT;
8129 }
8130}
8131
1dabd0cf 8132#endif /* HAVE_WINDOW_SYSTEM */
fa3c6b4d
KS
8133
8134\f
5f5c8ee5 8135/***********************************************************************
e037b9ec 8136 Tool-bars
5f5c8ee5
GM
8137 ***********************************************************************/
8138
8139#ifdef HAVE_WINDOW_SYSTEM
8140
fa3c6b4d
KS
8141/* Where the mouse was last time we reported a mouse event. */
8142
8143FRAME_PTR last_mouse_frame;
8144
8145/* Tool-bar item index of the item on which a mouse button was pressed
8146 or -1. */
8147
8148int last_tool_bar_item;
8149
8150
e037b9ec 8151/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
8152 before we start to fill in any display lines. Called from
8153 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8154 and restore it here. */
8155
8156static void
e037b9ec 8157update_tool_bar (f, save_match_data)
5f5c8ee5
GM
8158 struct frame *f;
8159 int save_match_data;
8160{
488dd4c4 8161#ifdef USE_GTK
810f2256 8162 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
488dd4c4
JD
8163#else
8164 int do_update = WINDOWP (f->tool_bar_window)
da8b7f4f 8165 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
488dd4c4
JD
8166#endif
8167
8168 if (do_update)
5f5c8ee5
GM
8169 {
8170 Lisp_Object window;
8171 struct window *w;
8172
8173 window = FRAME_SELECTED_WINDOW (f);
8174 w = XWINDOW (window);
2311178e 8175
5f5c8ee5
GM
8176 /* If the user has switched buffers or windows, we need to
8177 recompute to reflect the new bindings. But we'll
8178 recompute when update_mode_lines is set too; that means
8179 that people can use force-mode-line-update to request
8180 that the menu bar be recomputed. The adverse effect on
8181 the rest of the redisplay algorithm is about the same as
8182 windows_or_buffers_changed anyway. */
8183 if (windows_or_buffers_changed
8184 || !NILP (w->update_mode_line)
84f2e615 8185 || update_mode_lines
5f5c8ee5
GM
8186 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8187 < BUF_MODIFF (XBUFFER (w->buffer)))
8188 != !NILP (w->last_had_star))
8189 || ((!NILP (Vtransient_mark_mode)
8190 && !NILP (XBUFFER (w->buffer)->mark_active))
8191 != !NILP (w->region_showing)))
8192 {
8193 struct buffer *prev = current_buffer;
331379bf 8194 int count = SPECPDL_INDEX ();
84f2e615
RS
8195 Lisp_Object old_tool_bar;
8196 struct gcpro gcpro1;
a2889657 8197
5f5c8ee5
GM
8198 /* Set current_buffer to the buffer of the selected
8199 window of the frame, so that we get the right local
8200 keymaps. */
8201 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 8202
5f5c8ee5
GM
8203 /* Save match data, if we must. */
8204 if (save_match_data)
8205 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8206
8207 /* Make sure that we don't accidentally use bogus keymaps. */
8208 if (NILP (Voverriding_local_map_menu_flag))
8209 {
8210 specbind (Qoverriding_terminal_local_map, Qnil);
8211 specbind (Qoverriding_local_map, Qnil);
1f40cad2 8212 }
1f40cad2 8213
84f2e615
RS
8214 old_tool_bar = f->tool_bar_items;
8215 GCPRO1 (old_tool_bar);
8216
e037b9ec 8217 /* Build desired tool-bar items from keymaps. */
e3b2c21f 8218 BLOCK_INPUT;
7464726e
GM
8219 f->tool_bar_items
8220 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
e3b2c21f 8221 UNBLOCK_INPUT;
2311178e 8222
84f2e615
RS
8223 /* Redisplay the tool-bar if we changed it. */
8224 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8225 w->update_mode_line = Qt;
1e802fb5
SM
8226
8227 UNGCPRO;
5f5c8ee5
GM
8228
8229 unbind_to (count, Qnil);
8230 set_buffer_internal_1 (prev);
81d478f3 8231 }
a2889657
JB
8232 }
8233}
8234
6c4429a5 8235
e037b9ec 8236/* Set F->desired_tool_bar_string to a Lisp string representing frame
7464726e 8237 F's desired tool-bar contents. F->tool_bar_items must have
5f5c8ee5
GM
8238 been set up previously by calling prepare_menu_bars. */
8239
a2889657 8240static void
e037b9ec 8241build_desired_tool_bar_string (f)
5f5c8ee5 8242 struct frame *f;
a2889657 8243{
a23887b9 8244 int i, size, size_needed;
5f5c8ee5
GM
8245 struct gcpro gcpro1, gcpro2, gcpro3;
8246 Lisp_Object image, plist, props;
a2889657 8247
5f5c8ee5
GM
8248 image = plist = props = Qnil;
8249 GCPRO3 (image, plist, props);
a2889657 8250
e037b9ec 8251 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5 8252 Otherwise, make a new string. */
2311178e 8253
5f5c8ee5 8254 /* The size of the string we might be able to reuse. */
e037b9ec 8255 size = (STRINGP (f->desired_tool_bar_string)
2051c264 8256 ? SCHARS (f->desired_tool_bar_string)
5f5c8ee5
GM
8257 : 0);
8258
b94c0d9c 8259 /* We need one space in the string for each image. */
a23887b9 8260 size_needed = f->n_tool_bar_items;
2311178e 8261
b94c0d9c 8262 /* Reuse f->desired_tool_bar_string, if possible. */
cb2ddc53 8263 if (size < size_needed || NILP (f->desired_tool_bar_string))
6fc556fd
KR
8264 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8265 make_number (' '));
5f5c8ee5
GM
8266 else
8267 {
8268 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8269 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 8270 props, f->desired_tool_bar_string);
5f5c8ee5 8271 }
a2889657 8272
5f5c8ee5 8273 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
8274 put a `menu_item' property on tool-bar items with a value that
8275 is the index of the item in F's tool-bar item vector. */
a23887b9 8276 for (i = 0; i < f->n_tool_bar_items; ++i)
a2889657 8277 {
7464726e 8278#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
5f5c8ee5 8279
e037b9ec
GM
8280 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8281 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
35a41507 8282 int hmargin, vmargin, relief, idx, end;
ecd01a0e 8283 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
5f5c8ee5
GM
8284
8285 /* If image is a vector, choose the image according to the
8286 button state. */
e037b9ec 8287 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
8288 if (VECTORP (image))
8289 {
5f5c8ee5
GM
8290 if (enabled_p)
8291 idx = (selected_p
e037b9ec
GM
8292 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8293 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
8294 else
8295 idx = (selected_p
e037b9ec
GM
8296 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8297 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
2311178e 8298
37e4e482
GM
8299 xassert (ASIZE (image) >= idx);
8300 image = AREF (image, idx);
5f5c8ee5 8301 }
37e4e482
GM
8302 else
8303 idx = -1;
5f5c8ee5
GM
8304
8305 /* Ignore invalid image specifications. */
8306 if (!valid_image_p (image))
8307 continue;
8308
e037b9ec 8309 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
8310 plist = Fcopy_sequence (XCDR (image));
8311
8312 /* Compute margin and relief to draw. */
9f864bf9 8313 relief = (tool_bar_button_relief >= 0
c3d76173
GM
8314 ? tool_bar_button_relief
8315 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
35a41507
GM
8316 hmargin = vmargin = relief;
8317
8318 if (INTEGERP (Vtool_bar_button_margin)
8319 && XINT (Vtool_bar_button_margin) > 0)
8320 {
8321 hmargin += XFASTINT (Vtool_bar_button_margin);
8322 vmargin += XFASTINT (Vtool_bar_button_margin);
8323 }
8324 else if (CONSP (Vtool_bar_button_margin))
8325 {
8326 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8327 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8328 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
2311178e 8329
35a41507
GM
8330 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8331 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8332 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8333 }
2311178e 8334
e037b9ec 8335 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
8336 {
8337 /* Add a `:relief' property to the image spec if the item is
8338 selected. */
8339 if (selected_p)
8340 {
8341 plist = Fplist_put (plist, QCrelief, make_number (-relief));
35a41507
GM
8342 hmargin -= relief;
8343 vmargin -= relief;
5f5c8ee5
GM
8344 }
8345 }
8346 else
8347 {
8348 /* If image is selected, display it pressed, i.e. with a
8349 negative relief. If it's not selected, display it with a
8350 raised relief. */
8351 plist = Fplist_put (plist, QCrelief,
8352 (selected_p
8353 ? make_number (-relief)
8354 : make_number (relief)));
35a41507
GM
8355 hmargin -= relief;
8356 vmargin -= relief;
5f5c8ee5
GM
8357 }
8358
8359 /* Put a margin around the image. */
35a41507
GM
8360 if (hmargin || vmargin)
8361 {
8362 if (hmargin == vmargin)
8363 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8364 else
8365 plist = Fplist_put (plist, QCmargin,
8366 Fcons (make_number (hmargin),
8367 make_number (vmargin)));
8368 }
2311178e 8369
37e4e482
GM
8370 /* If button is not enabled, and we don't have special images
8371 for the disabled state, make the image appear disabled by
5f5c8ee5 8372 applying an appropriate algorithm to it. */
37e4e482 8373 if (!enabled_p && idx < 0)
ecd01a0e 8374 plist = Fplist_put (plist, QCconversion, Qdisabled);
2311178e 8375
5f5c8ee5
GM
8376 /* Put a `display' text property on the string for the image to
8377 display. Put a `menu-item' property on the string that gives
e037b9ec 8378 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
8379 vector. */
8380 image = Fcons (Qimage, plist);
8381 props = list4 (Qdisplay, image,
a23887b9
GM
8382 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8383
8384 /* Let the last image hide all remaining spaces in the tool bar
8385 string. The string can be longer than needed when we reuse a
8386 previous string. */
8387 if (i + 1 == f->n_tool_bar_items)
2051c264 8388 end = SCHARS (f->desired_tool_bar_string);
a23887b9
GM
8389 else
8390 end = i + 1;
8391 Fadd_text_properties (make_number (i), make_number (end),
e037b9ec 8392 props, f->desired_tool_bar_string);
5f5c8ee5 8393#undef PROP
a2889657
JB
8394 }
8395
5f5c8ee5
GM
8396 UNGCPRO;
8397}
8398
8399
e037b9ec 8400/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
8401
8402static void
e037b9ec 8403display_tool_bar_line (it)
5f5c8ee5
GM
8404 struct it *it;
8405{
8406 struct glyph_row *row = it->glyph_row;
8407 int max_x = it->last_visible_x;
8408 struct glyph *last;
2311178e 8409
5f5c8ee5
GM
8410 prepare_desired_row (row);
8411 row->y = it->current_y;
90aa2856
GM
8412
8413 /* Note that this isn't made use of if the face hasn't a box,
8414 so there's no need to check the face here. */
8415 it->start_of_box_run_p = 1;
2311178e 8416
5f5c8ee5 8417 while (it->current_x < max_x)
a2889657 8418 {
5f5c8ee5 8419 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 8420
5f5c8ee5
GM
8421 /* Get the next display element. */
8422 if (!get_next_display_element (it))
8423 break;
73af359d 8424
5f5c8ee5
GM
8425 /* Produce glyphs. */
8426 x_before = it->current_x;
8427 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8428 PRODUCE_GLYPHS (it);
daa37602 8429
5f5c8ee5
GM
8430 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8431 i = 0;
8432 x = x_before;
8433 while (i < nglyphs)
8434 {
8435 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
2311178e 8436
5f5c8ee5
GM
8437 if (x + glyph->pixel_width > max_x)
8438 {
8439 /* Glyph doesn't fit on line. */
8440 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8441 it->current_x = x;
8442 goto out;
8443 }
daa37602 8444
5f5c8ee5
GM
8445 ++it->hpos;
8446 x += glyph->pixel_width;
8447 ++i;
8448 }
8449
8450 /* Stop at line ends. */
8451 if (ITERATOR_AT_END_OF_LINE_P (it))
8452 break;
8453
cafafe0b 8454 set_iterator_to_next (it, 1);
a2889657 8455 }
a2889657 8456
5f5c8ee5 8457 out:;
a2889657 8458
5f5c8ee5
GM
8459 row->displays_text_p = row->used[TEXT_AREA] != 0;
8460 extend_face_to_end_of_line (it);
8461 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8462 last->right_box_line_p = 1;
90aa2856
GM
8463 if (last == row->glyphs[TEXT_AREA])
8464 last->left_box_line_p = 1;
5f5c8ee5 8465 compute_line_metrics (it);
2311178e 8466
e037b9ec 8467 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
8468 if (!row->displays_text_p)
8469 {
312246d1
GM
8470 row->height = row->phys_height = it->last_visible_y - row->y;
8471 row->ascent = row->phys_ascent = 0;
5f5c8ee5 8472 }
2311178e 8473
5f5c8ee5
GM
8474 row->full_width_p = 1;
8475 row->continued_p = 0;
8476 row->truncated_on_left_p = 0;
8477 row->truncated_on_right_p = 0;
8478
8479 it->current_x = it->hpos = 0;
8480 it->current_y += row->height;
8481 ++it->vpos;
8482 ++it->glyph_row;
a2889657 8483}
96a410bc 8484
5f5c8ee5 8485
e037b9ec 8486/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 8487 items of frame F visible. */
96a410bc 8488
d39b6696 8489static int
e037b9ec 8490tool_bar_lines_needed (f)
5f5c8ee5 8491 struct frame *f;
d39b6696 8492{
e037b9ec 8493 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5 8494 struct it it;
2311178e 8495
e037b9ec
GM
8496 /* Initialize an iterator for iteration over
8497 F->desired_tool_bar_string in the tool-bar window of frame F. */
8498 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5 8499 it.first_visible_x = 0;
da8b7f4f 8500 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
e037b9ec 8501 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
8502
8503 while (!ITERATOR_AT_END_P (&it))
8504 {
8505 it.glyph_row = w->desired_matrix->rows;
8506 clear_glyph_row (it.glyph_row);
e037b9ec 8507 display_tool_bar_line (&it);
5f5c8ee5
GM
8508 }
8509
da8b7f4f 8510 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
d39b6696 8511}
96a410bc 8512
5f5c8ee5 8513
57c28064
GM
8514DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8515 0, 1, 0,
7ee72033
MB
8516 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8517 (frame)
57c28064
GM
8518 Lisp_Object frame;
8519{
8520 struct frame *f;
8521 struct window *w;
8522 int nlines = 0;
8523
8524 if (NILP (frame))
8525 frame = selected_frame;
8526 else
b7826503 8527 CHECK_FRAME (frame);
57c28064 8528 f = XFRAME (frame);
2311178e 8529
57c28064
GM
8530 if (WINDOWP (f->tool_bar_window)
8531 || (w = XWINDOW (f->tool_bar_window),
da8b7f4f 8532 WINDOW_TOTAL_LINES (w) > 0))
57c28064
GM
8533 {
8534 update_tool_bar (f, 1);
8535 if (f->n_tool_bar_items)
8536 {
8537 build_desired_tool_bar_string (f);
8538 nlines = tool_bar_lines_needed (f);
8539 }
8540 }
8541
8542 return make_number (nlines);
8543}
8544
8545
e037b9ec 8546/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
8547 height should be changed. */
8548
8549static int
e037b9ec 8550redisplay_tool_bar (f)
5f5c8ee5 8551 struct frame *f;
96a410bc 8552{
5f5c8ee5
GM
8553 struct window *w;
8554 struct it it;
8555 struct glyph_row *row;
8556 int change_height_p = 0;
177c0ea7 8557
488dd4c4 8558#ifdef USE_GTK
810f2256 8559 if (FRAME_EXTERNAL_TOOL_BAR (f))
488dd4c4
JD
8560 update_frame_tool_bar (f);
8561 return 0;
8562#endif
2311178e 8563
e037b9ec
GM
8564 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8565 do anything. This means you must start with tool-bar-lines
5f5c8ee5 8566 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
8567 can turn off tool-bars by specifying tool-bar-lines zero. */
8568 if (!WINDOWP (f->tool_bar_window)
8569 || (w = XWINDOW (f->tool_bar_window),
da8b7f4f 8570 WINDOW_TOTAL_LINES (w) == 0))
5f5c8ee5 8571 return 0;
96a410bc 8572
e037b9ec
GM
8573 /* Set up an iterator for the tool-bar window. */
8574 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5 8575 it.first_visible_x = 0;
da8b7f4f 8576 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
5f5c8ee5 8577 row = it.glyph_row;
3450d04c 8578
e037b9ec
GM
8579 /* Build a string that represents the contents of the tool-bar. */
8580 build_desired_tool_bar_string (f);
8581 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 8582
e037b9ec 8583 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 8584 while (it.current_y < it.last_visible_y)
e037b9ec 8585 display_tool_bar_line (&it);
3450d04c 8586
e037b9ec 8587 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
8588 window, so don't do it. */
8589 w->desired_matrix->no_scrolling_p = 1;
8590 w->must_be_updated_p = 1;
3450d04c 8591
e037b9ec 8592 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
8593 {
8594 int nlines;
6e33e6f0
GM
8595
8596 /* If we couldn't display everything, change the tool-bar's
8597 height. */
8598 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8599 change_height_p = 1;
2311178e 8600
5f5c8ee5
GM
8601 /* If there are blank lines at the end, except for a partially
8602 visible blank line at the end that is smaller than
da8b7f4f 8603 FRAME_LINE_HEIGHT, change the tool-bar's height. */
5f5c8ee5
GM
8604 row = it.glyph_row - 1;
8605 if (!row->displays_text_p
da8b7f4f 8606 && row->height >= FRAME_LINE_HEIGHT (f))
5f5c8ee5
GM
8607 change_height_p = 1;
8608
e037b9ec
GM
8609 /* If row displays tool-bar items, but is partially visible,
8610 change the tool-bar's height. */
5f5c8ee5
GM
8611 if (row->displays_text_p
8612 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8613 change_height_p = 1;
8614
e037b9ec 8615 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
8616 frame parameter. */
8617 if (change_height_p
e037b9ec 8618 && (nlines = tool_bar_lines_needed (f),
da8b7f4f 8619 nlines != WINDOW_TOTAL_LINES (w)))
5f5c8ee5 8620 {
e037b9ec 8621 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5 8622 Lisp_Object frame;
da8b7f4f 8623 int old_height = WINDOW_TOTAL_LINES (w);
2311178e 8624
5f5c8ee5
GM
8625 XSETFRAME (frame, f);
8626 clear_glyph_matrix (w->desired_matrix);
8627 Fmodify_frame_parameters (frame,
e037b9ec 8628 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
8629 make_number (nlines)),
8630 Qnil));
da8b7f4f 8631 if (WINDOW_TOTAL_LINES (w) != old_height)
e85ee976 8632 fonts_changed_p = 1;
5f5c8ee5
GM
8633 }
8634 }
3450d04c 8635
5f5c8ee5 8636 return change_height_p;
96a410bc 8637}
90adcf20 8638
5f5c8ee5 8639
e037b9ec
GM
8640/* Get information about the tool-bar item which is displayed in GLYPH
8641 on frame F. Return in *PROP_IDX the index where tool-bar item
7464726e 8642 properties start in F->tool_bar_items. Value is zero if
e037b9ec 8643 GLYPH doesn't display a tool-bar item. */
5f5c8ee5 8644
fa3c6b4d 8645static int
e037b9ec 8646tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
8647 struct frame *f;
8648 struct glyph *glyph;
8649 int *prop_idx;
90adcf20 8650{
5f5c8ee5
GM
8651 Lisp_Object prop;
8652 int success_p;
be676094
GM
8653 int charpos;
8654
8655 /* This function can be called asynchronously, which means we must
8656 exclude any possibility that Fget_text_property signals an
8657 error. */
2051c264 8658 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
be676094 8659 charpos = max (0, charpos);
2311178e 8660
5f5c8ee5
GM
8661 /* Get the text property `menu-item' at pos. The value of that
8662 property is the start index of this item's properties in
7464726e 8663 F->tool_bar_items. */
be676094 8664 prop = Fget_text_property (make_number (charpos),
e037b9ec 8665 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
8666 if (INTEGERP (prop))
8667 {
8668 *prop_idx = XINT (prop);
8669 success_p = 1;
8670 }
8671 else
8672 success_p = 0;
90adcf20 8673
5f5c8ee5
GM
8674 return success_p;
8675}
2311178e 8676
fa3c6b4d
KS
8677\f
8678/* Get information about the tool-bar item at position X/Y on frame F.
8679 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8680 the current matrix of the tool-bar window of F, or NULL if not
8681 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8682 item in F->tool_bar_items. Value is
8683
8684 -1 if X/Y is not on a tool-bar item
8685 0 if X/Y is on the same item that was highlighted before.
8686 1 otherwise. */
8687
8688static int
8689get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8690 struct frame *f;
8691 int x, y;
8692 struct glyph **glyph;
8693 int *hpos, *vpos, *prop_idx;
8694{
8695 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8696 struct window *w = XWINDOW (f->tool_bar_window);
8697 int area;
8698
8699 /* Find the glyph under X/Y. */
8700 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
8701 if (*glyph == NULL)
8702 return -1;
8703
8704 /* Get the start of this tool-bar item's properties in
8705 f->tool_bar_items. */
8706 if (!tool_bar_item_info (f, *glyph, prop_idx))
8707 return -1;
8708
8709 /* Is mouse on the highlighted item? */
8710 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8711 && *vpos >= dpyinfo->mouse_face_beg_row
8712 && *vpos <= dpyinfo->mouse_face_end_row
8713 && (*vpos > dpyinfo->mouse_face_beg_row
8714 || *hpos >= dpyinfo->mouse_face_beg_col)
8715 && (*vpos < dpyinfo->mouse_face_end_row
8716 || *hpos < dpyinfo->mouse_face_end_col
8717 || dpyinfo->mouse_face_past_end))
8718 return 0;
8719
8720 return 1;
8721}
8722
8723
8724/* EXPORT:
8725 Handle mouse button event on the tool-bar of frame F, at
8726 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8727 0 for button release. MODIFIERS is event modifiers for button
8728 release. */
8729
8730void
8731handle_tool_bar_click (f, x, y, down_p, modifiers)
8732 struct frame *f;
8733 int x, y, down_p;
8734 unsigned int modifiers;
8735{
8736 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8737 struct window *w = XWINDOW (f->tool_bar_window);
8738 int hpos, vpos, prop_idx;
8739 struct glyph *glyph;
8740 Lisp_Object enabled_p;
8741
8742 /* If not on the highlighted tool-bar item, return. */
8743 frame_to_window_pixel_xy (w, &x, &y);
8744 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8745 return;
8746
8747 /* If item is disabled, do nothing. */
8748 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8749 if (NILP (enabled_p))
8750 return;
8751
8752 if (down_p)
8753 {
8754 /* Show item in pressed state. */
8755 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8756 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8757 last_tool_bar_item = prop_idx;
8758 }
8759 else
8760 {
8761 Lisp_Object key, frame;
8762 struct input_event event;
d86705ec 8763 EVENT_INIT (event);
fa3c6b4d
KS
8764
8765 /* Show item in released state. */
8766 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8767 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8768
8769 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8770
8771 XSETFRAME (frame, f);
8772 event.kind = TOOL_BAR_EVENT;
8773 event.frame_or_window = frame;
8774 event.arg = frame;
8775 kbd_buffer_store_event (&event);
8776
8777 event.kind = TOOL_BAR_EVENT;
8778 event.frame_or_window = frame;
8779 event.arg = key;
8780 event.modifiers = modifiers;
8781 kbd_buffer_store_event (&event);
8782 last_tool_bar_item = -1;
8783 }
8784}
8785
8786
8787/* Possibly highlight a tool-bar item on frame F when mouse moves to
8788 tool-bar window-relative coordinates X/Y. Called from
8789 note_mouse_highlight. */
8790
8791static void
8792note_tool_bar_highlight (f, x, y)
8793 struct frame *f;
8794 int x, y;
8795{
8796 Lisp_Object window = f->tool_bar_window;
8797 struct window *w = XWINDOW (window);
8798 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8799 int hpos, vpos;
8800 struct glyph *glyph;
8801 struct glyph_row *row;
8802 int i;
8803 Lisp_Object enabled_p;
8804 int prop_idx;
8805 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8806 int mouse_down_p, rc;
8807
8808 /* Function note_mouse_highlight is called with negative x(y
8809 values when mouse moves outside of the frame. */
8810 if (x <= 0 || y <= 0)
8811 {
8812 clear_mouse_face (dpyinfo);
8813 return;
8814 }
8815
8816 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
8817 if (rc < 0)
8818 {
8819 /* Not on tool-bar item. */
8820 clear_mouse_face (dpyinfo);
8821 return;
8822 }
8823 else if (rc == 0)
8824 /* On same tool-bar item as before. */
8825 goto set_help_echo;
8826
8827 clear_mouse_face (dpyinfo);
8828
8829 /* Mouse is down, but on different tool-bar item? */
8830 mouse_down_p = (dpyinfo->grabbed
8831 && f == last_mouse_frame
8832 && FRAME_LIVE_P (f));
8833 if (mouse_down_p
8834 && last_tool_bar_item != prop_idx)
8835 return;
8836
8837 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
8838 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
8839
8840 /* If tool-bar item is not enabled, don't highlight it. */
8841 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8842 if (!NILP (enabled_p))
8843 {
8844 /* Compute the x-position of the glyph. In front and past the
da8b7f4f 8845 image is a space. We include this in the highlighted area. */
fa3c6b4d
KS
8846 row = MATRIX_ROW (w->current_matrix, vpos);
8847 for (i = x = 0; i < hpos; ++i)
8848 x += row->glyphs[TEXT_AREA][i].pixel_width;
8849
8850 /* Record this as the current active region. */
8851 dpyinfo->mouse_face_beg_col = hpos;
8852 dpyinfo->mouse_face_beg_row = vpos;
8853 dpyinfo->mouse_face_beg_x = x;
8854 dpyinfo->mouse_face_beg_y = row->y;
8855 dpyinfo->mouse_face_past_end = 0;
8856
8857 dpyinfo->mouse_face_end_col = hpos + 1;
8858 dpyinfo->mouse_face_end_row = vpos;
8859 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
8860 dpyinfo->mouse_face_end_y = row->y;
8861 dpyinfo->mouse_face_window = window;
8862 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
8863
8864 /* Display it as active. */
8865 show_mouse_face (dpyinfo, draw);
8866 dpyinfo->mouse_face_image_state = draw;
8867 }
8868
8869 set_help_echo:
8870
8871 /* Set help_echo_string to a help string to display for this tool-bar item.
8872 XTread_socket does the rest. */
8873 help_echo_object = help_echo_window = Qnil;
8874 help_echo_pos = -1;
8875 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
8876 if (NILP (help_echo_string))
8877 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
8878}
8879
5f5c8ee5 8880#endif /* HAVE_WINDOW_SYSTEM */
ffbbc941
KS
8881
8882
8883\f
8884/***********************************************************************
8885 Fringes
8886 ***********************************************************************/
8887
8888#ifdef HAVE_WINDOW_SYSTEM
8889
8890/* An arrow like this: `<-'. */
8891static unsigned char left_bits[] = {
8892 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
8893
8894/* Right truncation arrow bitmap `->'. */
8895static unsigned char right_bits[] = {
8896 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
8897
8898/* Marker for continued lines. */
8899static unsigned char continued_bits[] = {
8900 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
8901
8902/* Marker for continuation lines. */
8903static unsigned char continuation_bits[] = {
8904 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
8905
8906/* Overlay arrow bitmap. A triangular arrow. */
8907static unsigned char ov_bits[] = {
8908 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
8909
8910/* Bitmap drawn to indicate lines not displaying text if
8911 `indicate-empty-lines' is non-nil. */
8912static unsigned char zv_bits[] = {
8913 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8914 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8915 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8916 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8917 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8918 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8919 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8920 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
8921
8922struct fringe_bitmap fringe_bitmaps[MAX_FRINGE_BITMAPS] =
8923{
8924 { 0, 0, 0, NULL /* NO_FRINGE_BITMAP */ },
8925 { 8, sizeof (left_bits), 0, left_bits },
8926 { 8, sizeof (right_bits), 0, right_bits },
8927 { 8, sizeof (continued_bits), 0, continued_bits },
8928 { 8, sizeof (continuation_bits), 0, continuation_bits },
8929 { 8, sizeof (ov_bits), 0, ov_bits },
8930 { 8, sizeof (zv_bits), 3, zv_bits }
8931};
8932
8933
8934/* Draw the bitmap WHICH in one of the left or right fringes of
8935 window W. ROW is the glyph row for which to display the bitmap; it
8936 determines the vertical position at which the bitmap has to be
8937 drawn. */
8938
8939static void
8940draw_fringe_bitmap (w, row, which, left_p)
8941 struct window *w;
8942 struct glyph_row *row;
8943 enum fringe_bitmap_type which;
8944 int left_p;
8945{
8946 struct frame *f = XFRAME (WINDOW_FRAME (w));
8947 struct draw_fringe_bitmap_params p;
8948
8949 /* Convert row to frame coordinates. */
8950 p.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8951
8952 p.which = which;
8953 p.wd = fringe_bitmaps[which].width;
8954
8955 p.h = fringe_bitmaps[which].height;
8956 p.dh = (fringe_bitmaps[which].period
8957 ? (p.y % fringe_bitmaps[which].period)
8958 : 0);
8959 p.h -= p.dh;
8960 /* Clip bitmap if too high. */
8961 if (p.h > row->height)
8962 p.h = row->height;
8963
8964 p.face = FACE_FROM_ID (f, FRINGE_FACE_ID);
8965 PREPARE_FACE_FOR_DISPLAY (f, p.face);
8966
8967 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
8968 the fringe. */
8969 p.bx = -1;
8970 if (left_p)
8971 {
da8b7f4f
KS
8972 int wd = WINDOW_LEFT_FRINGE_WIDTH (w);
8973 int x = window_box_left (w, (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
8974 ? LEFT_MARGIN_AREA
8975 : TEXT_AREA));
8976 if (p.wd > wd)
8977 p.wd = wd;
8978 p.x = x - p.wd - (wd - p.wd) / 2;
8979
8980 if (p.wd < wd || row->height > p.h)
ffbbc941
KS
8981 {
8982 /* If W has a vertical border to its left, don't draw over it. */
da8b7f4f
KS
8983 wd -= ((!WINDOW_LEFTMOST_P (w)
8984 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
8985 ? 1 : 0);
8986 p.bx = x - wd;
8987 p.nx = wd;
ffbbc941
KS
8988 }
8989 }
8990 else
8991 {
da8b7f4f
KS
8992 int x = window_box_right (w,
8993 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
8994 ? RIGHT_MARGIN_AREA
8995 : TEXT_AREA));
8996 int wd = WINDOW_RIGHT_FRINGE_WIDTH (w);
8997 if (p.wd > wd)
8998 p.wd = wd;
8999 p.x = x + (wd - p.wd) / 2;
ffbbc941
KS
9000 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
9001 the fringe. */
da8b7f4f 9002 if (p.wd < wd || row->height > p.h)
ffbbc941 9003 {
da8b7f4f
KS
9004 p.bx = x;
9005 p.nx = wd;
ffbbc941
KS
9006 }
9007 }
9008
9009 if (p.bx >= 0)
9010 {
da8b7f4f 9011 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
ffbbc941
KS
9012
9013 p.by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, row->y));
9014 p.ny = row->visible_height;
9015 }
9016
9017 /* Adjust y to the offset in the row to start drawing the bitmap. */
9018 p.y += (row->height - p.h) / 2;
9019
9020 rif->draw_fringe_bitmap (w, row, &p);
9021}
9022
9023/* Draw fringe bitmaps for glyph row ROW on window W. Call this
9024 function with input blocked. */
9025
9026void
9027draw_row_fringe_bitmaps (w, row)
9028 struct window *w;
9029 struct glyph_row *row;
9030{
ffbbc941
KS
9031 enum fringe_bitmap_type bitmap;
9032
9033 xassert (interrupt_input_blocked);
9034
9035 /* If row is completely invisible, because of vscrolling, we
9036 don't have to draw anything. */
9037 if (row->visible_height <= 0)
9038 return;
9039
da8b7f4f 9040 if (WINDOW_LEFT_FRINGE_WIDTH (w) != 0)
ffbbc941
KS
9041 {
9042 /* Decide which bitmap to draw in the left fringe. */
9043 if (row->overlay_arrow_p)
9044 bitmap = OVERLAY_ARROW_BITMAP;
9045 else if (row->truncated_on_left_p)
9046 bitmap = LEFT_TRUNCATION_BITMAP;
9047 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
9048 bitmap = CONTINUATION_LINE_BITMAP;
9049 else if (row->indicate_empty_line_p)
9050 bitmap = ZV_LINE_BITMAP;
9051 else
9052 bitmap = NO_FRINGE_BITMAP;
9053
9054 draw_fringe_bitmap (w, row, bitmap, 1);
9055 }
9056
da8b7f4f 9057 if (WINDOW_RIGHT_FRINGE_WIDTH (w) != 0)
ffbbc941
KS
9058 {
9059 /* Decide which bitmap to draw in the right fringe. */
9060 if (row->truncated_on_right_p)
9061 bitmap = RIGHT_TRUNCATION_BITMAP;
9062 else if (row->continued_p)
9063 bitmap = CONTINUED_LINE_BITMAP;
da8b7f4f 9064 else if (row->indicate_empty_line_p && WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
ffbbc941
KS
9065 bitmap = ZV_LINE_BITMAP;
9066 else
9067 bitmap = NO_FRINGE_BITMAP;
9068
9069 draw_fringe_bitmap (w, row, bitmap, 0);
9070 }
9071}
9072
9073
564946b6
KS
9074/* Compute actual fringe widths for frame F.
9075
9076 If REDRAW is 1, redraw F if the fringe settings was actually
9077 modified and F is visible.
9078
9079 Since the combined left and right fringe must occupy an integral
9080 number of columns, we may need to add some pixels to each fringe.
9081 Typically, we add an equal amount (+/- 1 pixel) to each fringe,
9082 but a negative width value is taken literally (after negating it).
9083
9084 We never make the fringes narrower than specified. It is planned
9085 to make fringe bitmaps customizable and expandable, and at that
9086 time, the user will typically specify the minimum number of pixels
9087 needed for his bitmaps, so we shouldn't select anything less than
9088 what is specified.
9089*/
ffbbc941
KS
9090
9091void
9092compute_fringe_widths (f, redraw)
9093 struct frame *f;
9094 int redraw;
9095{
da8b7f4f
KS
9096 int o_left = FRAME_LEFT_FRINGE_WIDTH (f);
9097 int o_right = FRAME_RIGHT_FRINGE_WIDTH (f);
9098 int o_cols = FRAME_FRINGE_COLS (f);
ffbbc941
KS
9099
9100 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
9101 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
9102 int left_fringe_width, right_fringe_width;
9103
9104 if (!NILP (left_fringe))
9105 left_fringe = Fcdr (left_fringe);
9106 if (!NILP (right_fringe))
9107 right_fringe = Fcdr (right_fringe);
9108
9109 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
9110 XINT (left_fringe));
9111 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
9112 XINT (right_fringe));
9113
9114 if (left_fringe_width || right_fringe_width)
9115 {
9116 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
9117 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
9118 int conf_wid = left_wid + right_wid;
da8b7f4f 9119 int font_wid = FRAME_COLUMN_WIDTH (f);
ffbbc941
KS
9120 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
9121 int real_wid = cols * font_wid;
9122 if (left_wid && right_wid)
9123 {
9124 if (left_fringe_width < 0)
9125 {
9126 /* Left fringe width is fixed, adjust right fringe if necessary */
da8b7f4f
KS
9127 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid;
9128 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid - left_wid;
ffbbc941
KS
9129 }
9130 else if (right_fringe_width < 0)
9131 {
9132 /* Right fringe width is fixed, adjust left fringe if necessary */
da8b7f4f
KS
9133 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid - right_wid;
9134 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid;
ffbbc941
KS
9135 }
9136 else
9137 {
9138 /* Adjust both fringes with an equal amount.
9139 Note that we are doing integer arithmetic here, so don't
9140 lose a pixel if the total width is an odd number. */
9141 int fill = real_wid - conf_wid;
da8b7f4f
KS
9142 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid + fill/2;
9143 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid + fill - fill/2;
ffbbc941
KS
9144 }
9145 }
9146 else if (left_fringe_width)
9147 {
da8b7f4f
KS
9148 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid;
9149 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
ffbbc941
KS
9150 }
9151 else
9152 {
da8b7f4f
KS
9153 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9154 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid;
ffbbc941 9155 }
da8b7f4f 9156 FRAME_FRINGE_COLS (f) = cols;
ffbbc941
KS
9157 }
9158 else
9159 {
da8b7f4f
KS
9160 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9161 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9162 FRAME_FRINGE_COLS (f) = 0;
ffbbc941
KS
9163 }
9164
9165 if (redraw && FRAME_VISIBLE_P (f))
da8b7f4f
KS
9166 if (o_left != FRAME_LEFT_FRINGE_WIDTH (f) ||
9167 o_right != FRAME_RIGHT_FRINGE_WIDTH (f) ||
9168 o_cols != FRAME_FRINGE_COLS (f))
ffbbc941
KS
9169 redraw_frame (f);
9170}
9171
9172#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 9173
feb0c42f 9174
5f5c8ee5
GM
9175\f
9176/************************************************************************
9177 Horizontal scrolling
9178 ************************************************************************/
feb0c42f 9179
5f5c8ee5
GM
9180static int hscroll_window_tree P_ ((Lisp_Object));
9181static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 9182
5f5c8ee5
GM
9183/* For all leaf windows in the window tree rooted at WINDOW, set their
9184 hscroll value so that PT is (i) visible in the window, and (ii) so
9185 that it is not within a certain margin at the window's left and
9186 right border. Value is non-zero if any window's hscroll has been
9187 changed. */
9188
9189static int
9190hscroll_window_tree (window)
9191 Lisp_Object window;
9192{
9193 int hscrolled_p = 0;
e76d28d5 9194 int hscroll_relative_p = FLOATP (Vhscroll_step);
1df7e8f0
EZ
9195 int hscroll_step_abs = 0;
9196 double hscroll_step_rel = 0;
9197
9198 if (hscroll_relative_p)
9199 {
e76d28d5 9200 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
1df7e8f0
EZ
9201 if (hscroll_step_rel < 0)
9202 {
9203 hscroll_relative_p = 0;
9204 hscroll_step_abs = 0;
9205 }
9206 }
e76d28d5 9207 else if (INTEGERP (Vhscroll_step))
1df7e8f0 9208 {
e76d28d5 9209 hscroll_step_abs = XINT (Vhscroll_step);
1df7e8f0
EZ
9210 if (hscroll_step_abs < 0)
9211 hscroll_step_abs = 0;
9212 }
9213 else
9214 hscroll_step_abs = 0;
9215
5f5c8ee5 9216 while (WINDOWP (window))
90adcf20 9217 {
5f5c8ee5 9218 struct window *w = XWINDOW (window);
1df7e8f0 9219
5f5c8ee5
GM
9220 if (WINDOWP (w->hchild))
9221 hscrolled_p |= hscroll_window_tree (w->hchild);
9222 else if (WINDOWP (w->vchild))
9223 hscrolled_p |= hscroll_window_tree (w->vchild);
9224 else if (w->cursor.vpos >= 0)
9225 {
da8b7f4f
KS
9226 int h_margin;
9227 int text_area_width;
92a90e89
GM
9228 struct glyph_row *current_cursor_row
9229 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9230 struct glyph_row *desired_cursor_row
9231 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9232 struct glyph_row *cursor_row
9233 = (desired_cursor_row->enabled_p
9234 ? desired_cursor_row
9235 : current_cursor_row);
a2725ab2 9236
da8b7f4f 9237 text_area_width = window_box_width (w, TEXT_AREA);
90adcf20 9238
5f5c8ee5 9239 /* Scroll when cursor is inside this scroll margin. */
da8b7f4f 9240 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
1df7e8f0 9241
5f5c8ee5 9242 if ((XFASTINT (w->hscroll)
e76d28d5 9243 && w->cursor.x <= h_margin)
92a90e89
GM
9244 || (cursor_row->enabled_p
9245 && cursor_row->truncated_on_right_p
e76d28d5 9246 && (w->cursor.x >= text_area_width - h_margin)))
08b610e4 9247 {
5f5c8ee5
GM
9248 struct it it;
9249 int hscroll;
9250 struct buffer *saved_current_buffer;
9251 int pt;
1df7e8f0 9252 int wanted_x;
5f5c8ee5
GM
9253
9254 /* Find point in a display of infinite width. */
9255 saved_current_buffer = current_buffer;
9256 current_buffer = XBUFFER (w->buffer);
1df7e8f0 9257
5f5c8ee5
GM
9258 if (w == XWINDOW (selected_window))
9259 pt = BUF_PT (current_buffer);
9260 else
08b610e4 9261 {
5f5c8ee5
GM
9262 pt = marker_position (w->pointm);
9263 pt = max (BEGV, pt);
9264 pt = min (ZV, pt);
9265 }
9266
9267 /* Move iterator to pt starting at cursor_row->start in
9268 a line with infinite width. */
9269 init_to_row_start (&it, w, cursor_row);
9270 it.last_visible_x = INFINITY;
9271 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9272 current_buffer = saved_current_buffer;
9273
1df7e8f0
EZ
9274 /* Position cursor in window. */
9275 if (!hscroll_relative_p && hscroll_step_abs == 0)
9276 hscroll = max (0, it.current_x - text_area_width / 2)
da8b7f4f 9277 / FRAME_COLUMN_WIDTH (it.f);
e76d28d5 9278 else if (w->cursor.x >= text_area_width - h_margin)
1df7e8f0
EZ
9279 {
9280 if (hscroll_relative_p)
9281 wanted_x = text_area_width * (1 - hscroll_step_rel)
e76d28d5 9282 - h_margin;
1df7e8f0
EZ
9283 else
9284 wanted_x = text_area_width
da8b7f4f 9285 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
e76d28d5 9286 - h_margin;
1df7e8f0 9287 hscroll
da8b7f4f 9288 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
1df7e8f0
EZ
9289 }
9290 else
9291 {
9292 if (hscroll_relative_p)
9293 wanted_x = text_area_width * hscroll_step_rel
e76d28d5 9294 + h_margin;
1df7e8f0 9295 else
da8b7f4f 9296 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
e76d28d5 9297 + h_margin;
1df7e8f0 9298 hscroll
da8b7f4f 9299 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
1df7e8f0 9300 }
3172f70b 9301 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
5f5c8ee5
GM
9302
9303 /* Don't call Fset_window_hscroll if value hasn't
9304 changed because it will prevent redisplay
9305 optimizations. */
9306 if (XFASTINT (w->hscroll) != hscroll)
9307 {
3172f70b
GM
9308 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9309 w->hscroll = make_number (hscroll);
5f5c8ee5 9310 hscrolled_p = 1;
08b610e4 9311 }
08b610e4 9312 }
08b610e4 9313 }
a2725ab2 9314
5f5c8ee5 9315 window = w->next;
90adcf20 9316 }
cd6dfed6 9317
5f5c8ee5
GM
9318 /* Value is non-zero if hscroll of any leaf window has been changed. */
9319 return hscrolled_p;
9320}
9321
9322
9323/* Set hscroll so that cursor is visible and not inside horizontal
9324 scroll margins for all windows in the tree rooted at WINDOW. See
9325 also hscroll_window_tree above. Value is non-zero if any window's
9326 hscroll has been changed. If it has, desired matrices on the frame
9327 of WINDOW are cleared. */
9328
9329static int
9330hscroll_windows (window)
9331 Lisp_Object window;
9332{
d475bcb8 9333 int hscrolled_p;
2311178e 9334
d475bcb8
GM
9335 if (automatic_hscrolling_p)
9336 {
9337 hscrolled_p = hscroll_window_tree (window);
9338 if (hscrolled_p)
9339 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9340 }
9341 else
9342 hscrolled_p = 0;
5f5c8ee5 9343 return hscrolled_p;
90adcf20 9344}
5f5c8ee5
GM
9345
9346
90adcf20 9347\f
5f5c8ee5
GM
9348/************************************************************************
9349 Redisplay
9350 ************************************************************************/
9351
9352/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9353 to a non-zero value. This is sometimes handy to have in a debugger
9354 session. */
9355
9356#if GLYPH_DEBUG
a2889657 9357
5f5c8ee5
GM
9358/* First and last unchanged row for try_window_id. */
9359
9360int debug_first_unchanged_at_end_vpos;
9361int debug_last_unchanged_at_beg_vpos;
9362
9363/* Delta vpos and y. */
9364
9365int debug_dvpos, debug_dy;
9366
9367/* Delta in characters and bytes for try_window_id. */
9368
9369int debug_delta, debug_delta_bytes;
9370
9371/* Values of window_end_pos and window_end_vpos at the end of
9372 try_window_id. */
9373
31ade731 9374EMACS_INT debug_end_pos, debug_end_vpos;
5f5c8ee5
GM
9375
9376/* Append a string to W->desired_matrix->method. FMT is a printf
9377 format string. A1...A9 are a supplement for a variable-length
9378 argument list. If trace_redisplay_p is non-zero also printf the
9379 resulting string to stderr. */
9380
9381static void
9382debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9383 struct window *w;
9384 char *fmt;
9385 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9386{
9387 char buffer[512];
9388 char *method = w->desired_matrix->method;
9389 int len = strlen (method);
9390 int size = sizeof w->desired_matrix->method;
9391 int remaining = size - len - 1;
9392
9393 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9394 if (len && remaining)
9395 {
9396 method[len] = '|';
9397 --remaining, ++len;
9398 }
2311178e 9399
5f5c8ee5
GM
9400 strncpy (method + len, buffer, remaining);
9401
9402 if (trace_redisplay_p)
9403 fprintf (stderr, "%p (%s): %s\n",
9404 w,
9405 ((BUFFERP (w->buffer)
9406 && STRINGP (XBUFFER (w->buffer)->name))
2051c264 9407 ? (char *) SDATA (XBUFFER (w->buffer)->name)
5f5c8ee5
GM
9408 : "no buffer"),
9409 buffer);
9410}
a2889657 9411
5f5c8ee5 9412#endif /* GLYPH_DEBUG */
90adcf20 9413
a2889657 9414
5f5c8ee5
GM
9415/* Value is non-zero if all changes in window W, which displays
9416 current_buffer, are in the text between START and END. START is a
9417 buffer position, END is given as a distance from Z. Used in
9418 redisplay_internal for display optimization. */
9419
9420static INLINE int
9421text_outside_line_unchanged_p (w, start, end)
9422 struct window *w;
9423 int start, end;
9424{
9425 int unchanged_p = 1;
2311178e 9426
5f5c8ee5
GM
9427 /* If text or overlays have changed, see where. */
9428 if (XFASTINT (w->last_modified) < MODIFF
9429 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9430 {
9431 /* Gap in the line? */
9432 if (GPT < start || Z - GPT < end)
9433 unchanged_p = 0;
9434
9435 /* Changes start in front of the line, or end after it? */
9436 if (unchanged_p
9142dd5b
GM
9437 && (BEG_UNCHANGED < start - 1
9438 || END_UNCHANGED < end))
5f5c8ee5 9439 unchanged_p = 0;
2311178e 9440
5f5c8ee5
GM
9441 /* If selective display, can't optimize if changes start at the
9442 beginning of the line. */
9443 if (unchanged_p
9444 && INTEGERP (current_buffer->selective_display)
9445 && XINT (current_buffer->selective_display) > 0
9142dd5b 9446 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5 9447 unchanged_p = 0;
47d57b22
GM
9448
9449 /* If there are overlays at the start or end of the line, these
9450 may have overlay strings with newlines in them. A change at
9451 START, for instance, may actually concern the display of such
9452 overlay strings as well, and they are displayed on different
9453 lines. So, quickly rule out this case. (For the future, it
9454 might be desirable to implement something more telling than
9455 just BEG/END_UNCHANGED.) */
9456 if (unchanged_p)
9457 {
9458 if (BEG + BEG_UNCHANGED == start
9459 && overlay_touches_p (start))
9460 unchanged_p = 0;
9461 if (END_UNCHANGED == end
9462 && overlay_touches_p (Z - end))
9463 unchanged_p = 0;
9464 }
5f5c8ee5
GM
9465 }
9466
9467 return unchanged_p;
9468}
9469
9470
9471/* Do a frame update, taking possible shortcuts into account. This is
9472 the main external entry point for redisplay.
9473
9474 If the last redisplay displayed an echo area message and that message
9475 is no longer requested, we clear the echo area or bring back the
9476 mini-buffer if that is in use. */
20de20dc 9477
a2889657
JB
9478void
9479redisplay ()
e9874cee
RS
9480{
9481 redisplay_internal (0);
9482}
9483
47d57b22 9484
260a86a0
KH
9485/* Return 1 if point moved out of or into a composition. Otherwise
9486 return 0. PREV_BUF and PREV_PT are the last point buffer and
9487 position. BUF and PT are the current point buffer and position. */
9488
9489int
9490check_point_in_composition (prev_buf, prev_pt, buf, pt)
9491 struct buffer *prev_buf, *buf;
9492 int prev_pt, pt;
9493{
9494 int start, end;
9495 Lisp_Object prop;
9496 Lisp_Object buffer;
9497
9498 XSETBUFFER (buffer, buf);
9499 /* Check a composition at the last point if point moved within the
9500 same buffer. */
9501 if (prev_buf == buf)
9502 {
9503 if (prev_pt == pt)
9504 /* Point didn't move. */
9505 return 0;
2311178e 9506
260a86a0
KH
9507 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9508 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9509 && COMPOSITION_VALID_P (start, end, prop)
9510 && start < prev_pt && end > prev_pt)
9511 /* The last point was within the composition. Return 1 iff
9512 point moved out of the composition. */
9513 return (pt <= start || pt >= end);
9514 }
9515
9516 /* Check a composition at the current point. */
9517 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9518 && find_composition (pt, -1, &start, &end, &prop, buffer)
9519 && COMPOSITION_VALID_P (start, end, prop)
9520 && start < pt && end > pt);
9521}
5f5c8ee5 9522
47d57b22 9523
9142dd5b
GM
9524/* Reconsider the setting of B->clip_changed which is displayed
9525 in window W. */
9526
9527static INLINE void
9528reconsider_clip_changes (w, b)
9529 struct window *w;
9530 struct buffer *b;
9531{
c62c1bb5 9532 if (b->clip_changed
9142dd5b
GM
9533 && !NILP (w->window_end_valid)
9534 && w->current_matrix->buffer == b
9535 && w->current_matrix->zv == BUF_ZV (b)
9536 && w->current_matrix->begv == BUF_BEGV (b))
9537 b->clip_changed = 0;
260a86a0
KH
9538
9539 /* If display wasn't paused, and W is not a tool bar window, see if
9540 point has been moved into or out of a composition. In that case,
9541 we set b->clip_changed to 1 to force updating the screen. If
9542 b->clip_changed has already been set to 1, we can skip this
9543 check. */
9544 if (!b->clip_changed
9545 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9546 {
9547 int pt;
9548
9549 if (w == XWINDOW (selected_window))
9550 pt = BUF_PT (current_buffer);
9551 else
9552 pt = marker_position (w->pointm);
9553
9554 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
6fc556fd 9555 || pt != XINT (w->last_point))
260a86a0 9556 && check_point_in_composition (w->current_matrix->buffer,
6fc556fd 9557 XINT (w->last_point),
260a86a0
KH
9558 XBUFFER (w->buffer), pt))
9559 b->clip_changed = 1;
9560 }
9142dd5b 9561}
aac2d8b2 9562\f
dd429b03
KH
9563
9564/* Select FRAME to forward the values of frame-local variables into C
9565 variables so that the redisplay routines can access those values
9566 directly. */
9567
9568static void
9569select_frame_for_redisplay (frame)
9570 Lisp_Object frame;
9571{
9572 Lisp_Object tail, sym, val;
9573 Lisp_Object old = selected_frame;
9574
9575 selected_frame = frame;
9576
9577 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9578 if (CONSP (XCAR (tail))
9579 && (sym = XCAR (XCAR (tail)),
9580 SYMBOLP (sym))
9581 && (sym = indirect_variable (sym),
9582 val = SYMBOL_VALUE (sym),
9583 (BUFFER_LOCAL_VALUEP (val)
9584 || SOME_BUFFER_LOCAL_VALUEP (val)))
9585 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9586 Fsymbol_value (sym);
9587
9588 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9589 if (CONSP (XCAR (tail))
9590 && (sym = XCAR (XCAR (tail)),
9591 SYMBOLP (sym))
9592 && (sym = indirect_variable (sym),
9593 val = SYMBOL_VALUE (sym),
9594 (BUFFER_LOCAL_VALUEP (val)
9595 || SOME_BUFFER_LOCAL_VALUEP (val)))
9596 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9597 Fsymbol_value (sym);
9598}
9599
9600
aac2d8b2
RS
9601#define STOP_POLLING \
9602do { if (! polling_stopped_here) stop_polling (); \
9603 polling_stopped_here = 1; } while (0)
9604
9605#define RESUME_POLLING \
9606do { if (polling_stopped_here) start_polling (); \
9607 polling_stopped_here = 0; } while (0)
9142dd5b
GM
9608
9609
5f5c8ee5
GM
9610/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9611 response to any user action; therefore, we should preserve the echo
9612 area. (Actually, our caller does that job.) Perhaps in the future
9613 avoid recentering windows if it is not necessary; currently that
9614 causes some problems. */
e9874cee
RS
9615
9616static void
9617redisplay_internal (preserve_echo_area)
9618 int preserve_echo_area;
a2889657 9619{
5f5c8ee5
GM
9620 struct window *w = XWINDOW (selected_window);
9621 struct frame *f = XFRAME (w->frame);
9622 int pause;
a2889657 9623 int must_finish = 0;
5f5c8ee5 9624 struct text_pos tlbufpos, tlendpos;
89819bdd 9625 int number_of_visible_frames;
28514cd9 9626 int count;
886bd6f2 9627 struct frame *sf = SELECTED_FRAME ();
aac2d8b2 9628 int polling_stopped_here = 0;
a2889657 9629
5f5c8ee5
GM
9630 /* Non-zero means redisplay has to consider all windows on all
9631 frames. Zero means, only selected_window is considered. */
9632 int consider_all_windows_p;
2311178e 9633
5f5c8ee5
GM
9634 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9635
9636 /* No redisplay if running in batch mode or frame is not yet fully
9637 initialized, or redisplay is explicitly turned off by setting
9638 Vinhibit_redisplay. */
9639 if (noninteractive
9640 || !NILP (Vinhibit_redisplay)
9641 || !f->glyphs_initialized_p)
a2889657
JB
9642 return;
9643
5f5c8ee5
GM
9644 /* The flag redisplay_performed_directly_p is set by
9645 direct_output_for_insert when it already did the whole screen
9646 update necessary. */
9647 if (redisplay_performed_directly_p)
9648 {
9649 redisplay_performed_directly_p = 0;
9650 if (!hscroll_windows (selected_window))
9651 return;
9652 }
9653
488dd4c4 9654#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15f0cf78
RS
9655 if (popup_activated ())
9656 return;
9657#endif
9658
28514cd9 9659 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 9660 if (redisplaying_p)
735c094c
KH
9661 return;
9662
28514cd9
GM
9663 /* Record a function that resets redisplaying_p to its old value
9664 when we leave this function. */
331379bf 9665 count = SPECPDL_INDEX ();
dd429b03
KH
9666 record_unwind_protect (unwind_redisplay,
9667 Fcons (make_number (redisplaying_p), selected_frame));
28514cd9 9668 ++redisplaying_p;
26683087 9669 specbind (Qinhibit_free_realized_faces, Qnil);
2311178e 9670
8b32d885 9671 retry:
bd9d0f3f 9672 pause = 0;
9142dd5b
GM
9673 reconsider_clip_changes (w, current_buffer);
9674
5f5c8ee5
GM
9675 /* If new fonts have been loaded that make a glyph matrix adjustment
9676 necessary, do it. */
9677 if (fonts_changed_p)
9678 {
9679 adjust_glyphs (NULL);
9680 ++windows_or_buffers_changed;
9681 fonts_changed_p = 0;
9682 }
9683
6961e0c1
GM
9684 /* If face_change_count is non-zero, init_iterator will free all
9685 realized faces, which includes the faces referenced from current
9686 matrices. So, we can't reuse current matrices in this case. */
9687 if (face_change_count)
9688 ++windows_or_buffers_changed;
9689
886bd6f2
GM
9690 if (! FRAME_WINDOW_P (sf)
9691 && previous_terminal_frame != sf)
20de20dc 9692 {
5f5c8ee5
GM
9693 /* Since frames on an ASCII terminal share the same display
9694 area, displaying a different frame means redisplay the whole
9695 thing. */
20de20dc 9696 windows_or_buffers_changed++;
886bd6f2
GM
9697 SET_FRAME_GARBAGED (sf);
9698 XSETFRAME (Vterminal_frame, sf);
20de20dc 9699 }
886bd6f2 9700 previous_terminal_frame = sf;
20de20dc 9701
5f5c8ee5
GM
9702 /* Set the visible flags for all frames. Do this before checking
9703 for resized or garbaged frames; they want to know if their frames
9704 are visible. See the comment in frame.h for
9705 FRAME_SAMPLE_VISIBILITY. */
d724d989 9706 {
35f56f96 9707 Lisp_Object tail, frame;
d724d989 9708
89819bdd
RS
9709 number_of_visible_frames = 0;
9710
35f56f96 9711 FOR_EACH_FRAME (tail, frame)
f82aff7c 9712 {
5f5c8ee5 9713 struct frame *f = XFRAME (frame);
2311178e 9714
5f5c8ee5
GM
9715 FRAME_SAMPLE_VISIBILITY (f);
9716 if (FRAME_VISIBLE_P (f))
9717 ++number_of_visible_frames;
9718 clear_desired_matrices (f);
f82aff7c 9719 }
d724d989
JB
9720 }
9721
44fa5b1e 9722 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 9723 do_pending_window_change (1);
a2889657 9724
5f5c8ee5 9725 /* Clear frames marked as garbaged. */
44fa5b1e 9726 if (frame_garbaged)
c6e89d6c 9727 clear_garbaged_frames ();
a2889657 9728
e037b9ec 9729 /* Build menubar and tool-bar items. */
f82aff7c
RS
9730 prepare_menu_bars ();
9731
28995e67 9732 if (windows_or_buffers_changed)
a2889657
JB
9733 update_mode_lines++;
9734
538f13d4
RS
9735 /* Detect case that we need to write or remove a star in the mode line. */
9736 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
9737 {
9738 w->update_mode_line = Qt;
9739 if (buffer_shared > 1)
9740 update_mode_lines++;
9741 }
9742
5f5c8ee5 9743 /* If %c is in the mode line, update it if needed. */
28995e67
RS
9744 if (!NILP (w->column_number_displayed)
9745 /* This alternative quickly identifies a common case
9746 where no change is needed. */
9747 && !(PT == XFASTINT (w->last_point)
8850a573
RS
9748 && XFASTINT (w->last_modified) >= MODIFF
9749 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
2311178e
TTN
9750 && (XFASTINT (w->column_number_displayed)
9751 != (int) current_column ())) /* iftc */
9752 w->update_mode_line = Qt;
28995e67 9753
44fa5b1e 9754 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 9755
5f5c8ee5
GM
9756 /* The variable buffer_shared is set in redisplay_window and
9757 indicates that we redisplay a buffer in different windows. See
9758 there. */
5fb96e96
RS
9759 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9760 || cursor_type_changed);
a2889657
JB
9761
9762 /* If specs for an arrow have changed, do thorough redisplay
9763 to ensure we remove any arrow that should no longer exist. */
d45de95b 9764 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 9765 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 9766 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 9767
90adcf20
RS
9768 /* Normally the message* functions will have already displayed and
9769 updated the echo area, but the frame may have been trashed, or
9770 the update may have been preempted, so display the echo area
6e019995 9771 again here. Checking message_cleared_p captures the case that
c6e89d6c 9772 the echo area should be cleared. */
96d5e9a0
GM
9773 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9774 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
3fa69ee7
GM
9775 || (message_cleared_p
9776 && minibuf_level == 0
9777 /* If the mini-window is currently selected, this means the
9778 echo-area doesn't show through. */
9779 && !MINI_WINDOW_P (XWINDOW (selected_window))))
90adcf20 9780 {
c6e89d6c 9781 int window_height_changed_p = echo_area_display (0);
90adcf20 9782 must_finish = 1;
6e019995
GM
9783
9784 /* If we don't display the current message, don't clear the
9785 message_cleared_p flag, because, if we did, we wouldn't clear
9786 the echo area in the next redisplay which doesn't preserve
9787 the echo area. */
9788 if (!display_last_displayed_message_p)
9789 message_cleared_p = 0;
2311178e 9790
c6e89d6c
GM
9791 if (fonts_changed_p)
9792 goto retry;
9793 else if (window_height_changed_p)
9794 {
9795 consider_all_windows_p = 1;
9796 ++update_mode_lines;
9797 ++windows_or_buffers_changed;
2311178e 9798
9142dd5b
GM
9799 /* If window configuration was changed, frames may have been
9800 marked garbaged. Clear them or we will experience
9801 surprises wrt scrolling. */
9802 if (frame_garbaged)
9803 clear_garbaged_frames ();
c6e89d6c 9804 }
90adcf20 9805 }
97a36635 9806 else if (EQ (selected_window, minibuf_window)
dd2eb166
GM
9807 && (current_buffer->clip_changed
9808 || XFASTINT (w->last_modified) < MODIFF
9809 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 9810 && resize_mini_window (w, 0))
c6e89d6c
GM
9811 {
9812 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
9813 showing if its contents might have changed. */
9814 must_finish = 1;
9815 consider_all_windows_p = 1;
c6e89d6c 9816 ++windows_or_buffers_changed;
dd2eb166 9817 ++update_mode_lines;
2311178e 9818
9142dd5b
GM
9819 /* If window configuration was changed, frames may have been
9820 marked garbaged. Clear them or we will experience
9821 surprises wrt scrolling. */
9822 if (frame_garbaged)
9823 clear_garbaged_frames ();
c6e89d6c 9824 }
2311178e 9825
90adcf20 9826
5f5c8ee5
GM
9827 /* If showing the region, and mark has changed, we must redisplay
9828 the whole window. The assignment to this_line_start_pos prevents
9829 the optimization directly below this if-statement. */
bd66d1ba
RS
9830 if (((!NILP (Vtransient_mark_mode)
9831 && !NILP (XBUFFER (w->buffer)->mark_active))
9832 != !NILP (w->region_showing))
82d04750
JB
9833 || (!NILP (w->region_showing)
9834 && !EQ (w->region_showing,
9835 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
9836 CHARPOS (this_line_start_pos) = 0;
9837
9838 /* Optimize the case that only the line containing the cursor in the
9839 selected window has changed. Variables starting with this_ are
9840 set in display_line and record information about the line
9841 containing the cursor. */
9842 tlbufpos = this_line_start_pos;
9843 tlendpos = this_line_end_pos;
9844 if (!consider_all_windows_p
9845 && CHARPOS (tlbufpos) > 0
9846 && NILP (w->update_mode_line)
73af359d 9847 && !current_buffer->clip_changed
c62c1bb5 9848 && !current_buffer->prevent_redisplay_optimizations_p
44fa5b1e 9849 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 9850 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 9851 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
9852 && this_line_buffer == current_buffer
9853 && current_buffer == XBUFFER (w->buffer)
265a9e55 9854 && NILP (w->force_start)
acda20e1 9855 && NILP (w->optional_new_start)
5f5c8ee5
GM
9856 /* Point must be on the line that we have info recorded about. */
9857 && PT >= CHARPOS (tlbufpos)
9858 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
9859 /* All text outside that line, including its final newline,
9860 must be unchanged */
5f5c8ee5
GM
9861 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9862 CHARPOS (tlendpos)))
9863 {
9864 if (CHARPOS (tlbufpos) > BEGV
9865 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9866 && (CHARPOS (tlbufpos) == ZV
9867 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
9868 /* Former continuation line has disappeared by becoming empty */
9869 goto cancel;
9870 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 9871 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
9872 || MINI_WINDOW_P (w))
9873 {
1c9241f5
KH
9874 /* We have to handle the case of continuation around a
9875 wide-column character (See the comment in indent.c around
9876 line 885).
9877
9878 For instance, in the following case:
9879
9880 -------- Insert --------
9881 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9882 J_I_ ==> J_I_ `^^' are cursors.
9883 ^^ ^^
9884 -------- --------
9885
9886 As we have to redraw the line above, we should goto cancel. */
9887
5f5c8ee5
GM
9888 struct it it;
9889 int line_height_before = this_line_pixel_height;
9890
9891 /* Note that start_display will handle the case that the
9892 line starting at tlbufpos is a continuation lines. */
9893 start_display (&it, w, tlbufpos);
9894
9895 /* Implementation note: It this still necessary? */
9896 if (it.current_x != this_line_start_x)
1c9241f5
KH
9897 goto cancel;
9898
5f5c8ee5
GM
9899 TRACE ((stderr, "trying display optimization 1\n"));
9900 w->cursor.vpos = -1;
a2889657 9901 overlay_arrow_seen = 0;
5f5c8ee5
GM
9902 it.vpos = this_line_vpos;
9903 it.current_y = this_line_y;
9904 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9905 display_line (&it);
9906
a2889657 9907 /* If line contains point, is not continued,
5f5c8ee5 9908 and ends at same distance from eob as before, we win */
2311178e 9909 if (w->cursor.vpos >= 0
5f5c8ee5
GM
9910 /* Line is not continued, otherwise this_line_start_pos
9911 would have been set to 0 in display_line. */
9912 && CHARPOS (this_line_start_pos)
9913 /* Line ends as before. */
9914 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9915 /* Line has same height as before. Otherwise other lines
9916 would have to be shifted up or down. */
9917 && this_line_pixel_height == line_height_before)
a2889657 9918 {
5f5c8ee5
GM
9919 /* If this is not the window's last line, we must adjust
9920 the charstarts of the lines below. */
9921 if (it.current_y < it.last_visible_y)
9922 {
9923 struct glyph_row *row
9924 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9925 int delta, delta_bytes;
2311178e 9926
5f5c8ee5
GM
9927 if (Z - CHARPOS (tlendpos) == ZV)
9928 {
9929 /* This line ends at end of (accessible part of)
9930 buffer. There is no newline to count. */
9931 delta = (Z
9932 - CHARPOS (tlendpos)
9933 - MATRIX_ROW_START_CHARPOS (row));
9934 delta_bytes = (Z_BYTE
9935 - BYTEPOS (tlendpos)
9936 - MATRIX_ROW_START_BYTEPOS (row));
9937 }
9938 else
9939 {
9940 /* This line ends in a newline. Must take
9941 account of the newline and the rest of the
9942 text that follows. */
9943 delta = (Z
9944 - CHARPOS (tlendpos)
9945 - MATRIX_ROW_START_CHARPOS (row));
9946 delta_bytes = (Z_BYTE
9947 - BYTEPOS (tlendpos)
9948 - MATRIX_ROW_START_BYTEPOS (row));
9949 }
2311178e 9950
f2d86d7a
GM
9951 increment_matrix_positions (w->current_matrix,
9952 this_line_vpos + 1,
9953 w->current_matrix->nrows,
9954 delta, delta_bytes);
85bcef6c 9955 }
46db8486 9956
5f5c8ee5
GM
9957 /* If this row displays text now but previously didn't,
9958 or vice versa, w->window_end_vpos may have to be
9959 adjusted. */
9960 if ((it.glyph_row - 1)->displays_text_p)
9961 {
9962 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9963 XSETINT (w->window_end_vpos, this_line_vpos);
9964 }
9965 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9966 && this_line_vpos > 0)
9967 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9968 w->window_end_valid = Qnil;
2311178e 9969
5f5c8ee5
GM
9970 /* Update hint: No need to try to scroll in update_window. */
9971 w->desired_matrix->no_scrolling_p = 1;
9972
9973#if GLYPH_DEBUG
9974 *w->desired_matrix->method = 0;
9975 debug_method_add (w, "optimization 1");
9976#endif
a2889657
JB
9977 goto update;
9978 }
9979 else
9980 goto cancel;
9981 }
5f5c8ee5
GM
9982 else if (/* Cursor position hasn't changed. */
9983 PT == XFASTINT (w->last_point)
b6f0fe04
RS
9984 /* Make sure the cursor was last displayed
9985 in this window. Otherwise we have to reposition it. */
5f5c8ee5 9986 && 0 <= w->cursor.vpos
da8b7f4f 9987 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
a2889657
JB
9988 {
9989 if (!must_finish)
9990 {
c6e89d6c 9991 do_pending_window_change (1);
5f5c8ee5 9992
2311178e 9993 /* We used to always goto end_of_redisplay here, but this
5f5c8ee5
GM
9994 isn't enough if we have a blinking cursor. */
9995 if (w->cursor_off_p == w->last_cursor_off_p)
9996 goto end_of_redisplay;
a2889657
JB
9997 }
9998 goto update;
9999 }
8b51f1e3
KH
10000 /* If highlighting the region, or if the cursor is in the echo area,
10001 then we can't just move the cursor. */
bd66d1ba
RS
10002 else if (! (!NILP (Vtransient_mark_mode)
10003 && !NILP (current_buffer->mark_active))
97a36635 10004 && (EQ (selected_window, current_buffer->last_selected_window)
293a54ce 10005 || highlight_nonselected_windows)
8b51f1e3 10006 && NILP (w->region_showing)
8f897821 10007 && NILP (Vshow_trailing_whitespace)
8b51f1e3 10008 && !cursor_in_echo_area)
a2889657 10009 {
5f5c8ee5
GM
10010 struct it it;
10011 struct glyph_row *row;
10012
10013 /* Skip from tlbufpos to PT and see where it is. Note that
10014 PT may be in invisible text. If so, we will end at the
10015 next visible position. */
10016 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10017 NULL, DEFAULT_FACE_ID);
10018 it.current_x = this_line_start_x;
10019 it.current_y = this_line_y;
10020 it.vpos = this_line_vpos;
2311178e 10021
5f5c8ee5
GM
10022 /* The call to move_it_to stops in front of PT, but
10023 moves over before-strings. */
10024 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10025
10026 if (it.vpos == this_line_vpos
10027 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10028 row->enabled_p))
a2889657 10029 {
5f5c8ee5
GM
10030 xassert (this_line_vpos == it.vpos);
10031 xassert (this_line_y == it.current_y);
10032 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
ade0bee1
GM
10033#if GLYPH_DEBUG
10034 *w->desired_matrix->method = 0;
10035 debug_method_add (w, "optimization 3");
10036#endif
a2889657
JB
10037 goto update;
10038 }
10039 else
10040 goto cancel;
10041 }
5f5c8ee5 10042
a2889657 10043 cancel:
5f5c8ee5
GM
10044 /* Text changed drastically or point moved off of line. */
10045 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
10046 }
10047
5f5c8ee5
GM
10048 CHARPOS (this_line_start_pos) = 0;
10049 consider_all_windows_p |= buffer_shared > 1;
10050 ++clear_face_cache_count;
a2889657 10051
2311178e 10052
bd9d0f3f
GM
10053 /* Build desired matrices, and update the display. If
10054 consider_all_windows_p is non-zero, do it for all windows on all
10055 frames. Otherwise do it for selected_window, only. */
463f6b91 10056
5f5c8ee5 10057 if (consider_all_windows_p)
a2889657 10058 {
35f56f96 10059 Lisp_Object tail, frame;
0528abe1
GM
10060 int i, n = 0, size = 50;
10061 struct frame **updated
10062 = (struct frame **) alloca (size * sizeof *updated);
a2889657 10063
5f5c8ee5
GM
10064 /* Clear the face cache eventually. */
10065 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 10066 {
5f5c8ee5 10067 clear_face_cache (0);
463f6b91
RS
10068 clear_face_cache_count = 0;
10069 }
31b24551 10070
5f5c8ee5
GM
10071 /* Recompute # windows showing selected buffer. This will be
10072 incremented each time such a window is displayed. */
a2889657
JB
10073 buffer_shared = 0;
10074
35f56f96 10075 FOR_EACH_FRAME (tail, frame)
30c566e4 10076 {
5f5c8ee5 10077 struct frame *f = XFRAME (frame);
2311178e 10078
886bd6f2 10079 if (FRAME_WINDOW_P (f) || f == sf)
9769686d 10080 {
dd429b03
KH
10081 if (! EQ (frame, selected_frame))
10082 /* Select the frame, for the sake of frame-local
10083 variables. */
10084 select_frame_for_redisplay (frame);
10085
ae02e06a 10086#ifdef HAVE_WINDOW_SYSTEM
bb336f8d
RS
10087 if (clear_face_cache_count % 50 == 0
10088 && FRAME_WINDOW_P (f))
10089 clear_image_cache (f, 0);
ae02e06a 10090#endif /* HAVE_WINDOW_SYSTEM */
bb336f8d 10091
5f5c8ee5
GM
10092 /* Mark all the scroll bars to be removed; we'll redeem
10093 the ones we want when we redisplay their windows. */
9769686d 10094 if (condemn_scroll_bars_hook)
504454e8 10095 condemn_scroll_bars_hook (f);
30c566e4 10096
f21ef775 10097 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 10098 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 10099
5f5c8ee5
GM
10100 /* Any scroll bars which redisplay_windows should have
10101 nuked should now go away. */
9769686d 10102 if (judge_scroll_bars_hook)
504454e8 10103 judge_scroll_bars_hook (f);
bd9d0f3f
GM
10104
10105 /* If fonts changed, display again. */
b60c9653
RS
10106 /* ??? rms: I suspect it is a mistake to jump all the way
10107 back to retry here. It should just retry this frame. */
bd9d0f3f
GM
10108 if (fonts_changed_p)
10109 goto retry;
2311178e 10110
bd9d0f3f
GM
10111 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10112 {
10113 /* See if we have to hscroll. */
10114 if (hscroll_windows (f->root_window))
10115 goto retry;
10116
10117 /* Prevent various kinds of signals during display
10118 update. stdio is not robust about handling
10119 signals, which can cause an apparent I/O
10120 error. */
10121 if (interrupt_input)
10122 unrequest_sigio ();
aac2d8b2 10123 STOP_POLLING;
bd9d0f3f
GM
10124
10125 /* Update the display. */
10126 set_window_update_flags (XWINDOW (f->root_window), 1);
10127 pause |= update_frame (f, 0, 0);
ccbb9ed2 10128#if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
bd9d0f3f
GM
10129 if (pause)
10130 break;
ccbb9ed2 10131#endif
bd9d0f3f 10132
0528abe1
GM
10133 if (n == size)
10134 {
10135 int nbytes = size * sizeof *updated;
10136 struct frame **p = (struct frame **) alloca (2 * nbytes);
10137 bcopy (updated, p, nbytes);
10138 size *= 2;
10139 }
2311178e 10140
0528abe1 10141 updated[n++] = f;
bd9d0f3f 10142 }
9769686d 10143 }
30c566e4 10144 }
0528abe1 10145
6f68b035
GM
10146 if (!pause)
10147 {
10148 /* Do the mark_window_display_accurate after all windows have
10149 been redisplayed because this call resets flags in buffers
10150 which are needed for proper redisplay. */
10151 for (i = 0; i < n; ++i)
10152 {
10153 struct frame *f = updated[i];
10154 mark_window_display_accurate (f->root_window, 1);
10155 if (frame_up_to_date_hook)
10156 frame_up_to_date_hook (f);
10157 }
0528abe1 10158 }
a2889657 10159 }
bd9d0f3f
GM
10160 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10161 {
10162 Lisp_Object mini_window;
10163 struct frame *mini_frame;
5f5c8ee5 10164
82a7ab23 10165 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
7033d6df
RS
10166 /* Use list_of_error, not Qerror, so that
10167 we catch only errors and don't run the debugger. */
10168 internal_condition_case_1 (redisplay_window_1, selected_window,
10169 list_of_error,
82a7ab23 10170 redisplay_window_error);
2311178e 10171
bd9d0f3f 10172 /* Compare desired and current matrices, perform output. */
7d0393cf 10173
26683087 10174 update:
bd9d0f3f
GM
10175 /* If fonts changed, display again. */
10176 if (fonts_changed_p)
92a90e89 10177 goto retry;
a2889657 10178
bd9d0f3f
GM
10179 /* Prevent various kinds of signals during display update.
10180 stdio is not robust about handling signals,
10181 which can cause an apparent I/O error. */
10182 if (interrupt_input)
10183 unrequest_sigio ();
aac2d8b2 10184 STOP_POLLING;
1af9f229 10185
bd9d0f3f 10186 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
5f5c8ee5 10187 {
92a90e89
GM
10188 if (hscroll_windows (selected_window))
10189 goto retry;
2311178e 10190
5f5c8ee5 10191 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 10192 pause = update_frame (sf, 0, 0);
5f5c8ee5 10193 }
d724d989 10194
8de2d90b 10195 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
10196 function. If the echo area is on another frame, that may
10197 have put text on a frame other than the selected one, so the
10198 above call to update_frame would not have caught it. Catch
8de2d90b 10199 it here. */
bd9d0f3f
GM
10200 mini_window = FRAME_MINIBUF_WINDOW (sf);
10201 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
2311178e 10202
bd9d0f3f
GM
10203 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10204 {
10205 XWINDOW (mini_window)->must_be_updated_p = 1;
10206 pause |= update_frame (mini_frame, 0, 0);
10207 if (!pause && hscroll_windows (mini_window))
10208 goto retry;
10209 }
6e8290aa 10210 }
a2889657 10211
5f5c8ee5
GM
10212 /* If display was paused because of pending input, make sure we do a
10213 thorough update the next time. */
a2889657
JB
10214 if (pause)
10215 {
5f5c8ee5
GM
10216 /* Prevent the optimization at the beginning of
10217 redisplay_internal that tries a single-line update of the
10218 line containing the cursor in the selected window. */
10219 CHARPOS (this_line_start_pos) = 0;
10220
10221 /* Let the overlay arrow be updated the next time. */
265a9e55 10222 if (!NILP (last_arrow_position))
a2889657
JB
10223 {
10224 last_arrow_position = Qt;
10225 last_arrow_string = Qt;
10226 }
2311178e 10227
5f5c8ee5
GM
10228 /* If we pause after scrolling, some rows in the current
10229 matrices of some windows are not valid. */
10230 if (!WINDOW_FULL_WIDTH_P (w)
10231 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
10232 update_mode_lines = 1;
10233 }
43c09969 10234 else
a2889657 10235 {
43c09969 10236 if (!consider_all_windows_p)
a2889657 10237 {
43c09969
GM
10238 /* This has already been done above if
10239 consider_all_windows_p is set. */
10240 mark_window_display_accurate_1 (w, 1);
2311178e 10241
927c5b3b
GM
10242 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10243 last_arrow_string = Voverlay_arrow_string;
2311178e 10244
efc63ef0 10245 if (frame_up_to_date_hook != 0)
43c09969 10246 frame_up_to_date_hook (sf);
a2889657 10247 }
15e26c76 10248
a2889657
JB
10249 update_mode_lines = 0;
10250 windows_or_buffers_changed = 0;
5fb96e96 10251 cursor_type_changed = 0;
a2889657
JB
10252 }
10253
5f5c8ee5
GM
10254 /* Start SIGIO interrupts coming again. Having them off during the
10255 code above makes it less likely one will discard output, but not
10256 impossible, since there might be stuff in the system buffer here.
a2889657 10257 But it is much hairier to try to do anything about that. */
a2889657
JB
10258 if (interrupt_input)
10259 request_sigio ();
aac2d8b2 10260 RESUME_POLLING;
a2889657 10261
5f5c8ee5
GM
10262 /* If a frame has become visible which was not before, redisplay
10263 again, so that we display it. Expose events for such a frame
10264 (which it gets when becoming visible) don't call the parts of
10265 redisplay constructing glyphs, so simply exposing a frame won't
10266 display anything in this case. So, we have to display these
10267 frames here explicitly. */
11c52c4f
RS
10268 if (!pause)
10269 {
10270 Lisp_Object tail, frame;
10271 int new_count = 0;
10272
10273 FOR_EACH_FRAME (tail, frame)
10274 {
10275 int this_is_visible = 0;
8e83f802
RS
10276
10277 if (XFRAME (frame)->visible)
10278 this_is_visible = 1;
10279 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10280 if (XFRAME (frame)->visible)
10281 this_is_visible = 1;
11c52c4f
RS
10282
10283 if (this_is_visible)
10284 new_count++;
10285 }
10286
89819bdd 10287 if (new_count != number_of_visible_frames)
11c52c4f
RS
10288 windows_or_buffers_changed++;
10289 }
10290
44fa5b1e 10291 /* Change frame size now if a change is pending. */
c6e89d6c 10292 do_pending_window_change (1);
d8e242fd 10293
8b32d885
RS
10294 /* If we just did a pending size change, or have additional
10295 visible frames, redisplay again. */
3c8c72e0 10296 if (windows_or_buffers_changed && !pause)
8b32d885 10297 goto retry;
5f5c8ee5 10298
1987b083 10299 end_of_redisplay:
28514cd9 10300 unbind_to (count, Qnil);
aac2d8b2 10301 RESUME_POLLING;
a2889657
JB
10302}
10303
5f5c8ee5
GM
10304
10305/* Redisplay, but leave alone any recent echo area message unless
10306 another message has been requested in its place.
a2889657
JB
10307
10308 This is useful in situations where you need to redisplay but no
10309 user action has occurred, making it inappropriate for the message
10310 area to be cleared. See tracking_off and
9bf76936
GM
10311 wait_reading_process_input for examples of these situations.
10312
10313 FROM_WHERE is an integer saying from where this function was
10314 called. This is useful for debugging. */
a2889657 10315
8991bb31 10316void
9bf76936
GM
10317redisplay_preserve_echo_area (from_where)
10318 int from_where;
a2889657 10319{
9bf76936
GM
10320 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10321
c6e89d6c 10322 if (!NILP (echo_area_buffer[1]))
a2889657 10323 {
c6e89d6c
GM
10324 /* We have a previously displayed message, but no current
10325 message. Redisplay the previous message. */
10326 display_last_displayed_message_p = 1;
e9874cee 10327 redisplay_internal (1);
c6e89d6c 10328 display_last_displayed_message_p = 0;
a2889657
JB
10329 }
10330 else
e9874cee 10331 redisplay_internal (1);
a2889657
JB
10332}
10333
5f5c8ee5 10334
28514cd9 10335/* Function registered with record_unwind_protect in
acfca545
RS
10336 redisplay_internal. Reset redisplaying_p to the value it had
10337 before redisplay_internal was called, and clear
dd429b03
KH
10338 prevent_freeing_realized_faces_p. It also selects the previously
10339 selected frame. */
28514cd9
GM
10340
10341static Lisp_Object
dd429b03
KH
10342unwind_redisplay (val)
10343 Lisp_Object val;
28514cd9 10344{
dd429b03
KH
10345 Lisp_Object old_redisplaying_p, old_frame;
10346
10347 old_redisplaying_p = XCAR (val);
28514cd9 10348 redisplaying_p = XFASTINT (old_redisplaying_p);
dd429b03
KH
10349 old_frame = XCDR (val);
10350 if (! EQ (old_frame, selected_frame))
10351 select_frame_for_redisplay (old_frame);
c6e89d6c 10352 return Qnil;
28514cd9
GM
10353}
10354
10355
43c09969
GM
10356/* Mark the display of window W as accurate or inaccurate. If
10357 ACCURATE_P is non-zero mark display of W as accurate. If
10358 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10359 redisplay_internal is called. */
5f5c8ee5 10360
43c09969
GM
10361static void
10362mark_window_display_accurate_1 (w, accurate_p)
10363 struct window *w;
5f5c8ee5 10364 int accurate_p;
a2889657 10365{
43c09969 10366 if (BUFFERP (w->buffer))
a2889657 10367 {
43c09969 10368 struct buffer *b = XBUFFER (w->buffer);
2311178e 10369
43c09969
GM
10370 w->last_modified
10371 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10372 w->last_overlay_modified
10373 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10374 w->last_had_star
10375 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
a2889657 10376
43c09969 10377 if (accurate_p)
bd66d1ba 10378 {
43c09969
GM
10379 b->clip_changed = 0;
10380 b->prevent_redisplay_optimizations_p = 0;
10381
10382 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10383 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10384 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10385 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
2311178e 10386
43c09969
GM
10387 w->current_matrix->buffer = b;
10388 w->current_matrix->begv = BUF_BEGV (b);
10389 w->current_matrix->zv = BUF_ZV (b);
2311178e 10390
43c09969
GM
10391 w->last_cursor = w->cursor;
10392 w->last_cursor_off_p = w->cursor_off_p;
2311178e 10393
43c09969
GM
10394 if (w == XWINDOW (selected_window))
10395 w->last_point = make_number (BUF_PT (b));
10396 else
10397 w->last_point = make_number (XMARKER (w->pointm)->charpos);
bd66d1ba 10398 }
43c09969 10399 }
bd66d1ba 10400
43c09969
GM
10401 if (accurate_p)
10402 {
d2f84654 10403 w->window_end_valid = w->buffer;
99332eb6 10404#if 0 /* This is incorrect with variable-height lines. */
2913a9c0 10405 xassert (XINT (w->window_end_vpos)
da8b7f4f 10406 < (WINDOW_TOTAL_LINES (w)
2913a9c0 10407 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
99332eb6 10408#endif
a2889657 10409 w->update_mode_line = Qnil;
43c09969
GM
10410 }
10411}
10412
10413
10414/* Mark the display of windows in the window tree rooted at WINDOW as
10415 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10416 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10417 be redisplayed the next time redisplay_internal is called. */
10418
10419void
10420mark_window_display_accurate (window, accurate_p)
10421 Lisp_Object window;
10422 int accurate_p;
10423{
10424 struct window *w;
2311178e 10425
43c09969
GM
10426 for (; !NILP (window); window = w->next)
10427 {
10428 w = XWINDOW (window);
10429 mark_window_display_accurate_1 (w, accurate_p);
a2889657 10430
265a9e55 10431 if (!NILP (w->vchild))
5f5c8ee5 10432 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 10433 if (!NILP (w->hchild))
5f5c8ee5 10434 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
10435 }
10436
5f5c8ee5 10437 if (accurate_p)
a2889657 10438 {
d45de95b 10439 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
10440 last_arrow_string = Voverlay_arrow_string;
10441 }
10442 else
10443 {
5f5c8ee5
GM
10444 /* Force a thorough redisplay the next time by setting
10445 last_arrow_position and last_arrow_string to t, which is
4b41cebb 10446 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
10447 last_arrow_position = Qt;
10448 last_arrow_string = Qt;
10449 }
10450}
5f5c8ee5
GM
10451
10452
10453/* Return value in display table DP (Lisp_Char_Table *) for character
10454 C. Since a display table doesn't have any parent, we don't have to
10455 follow parent. Do not call this function directly but use the
10456 macro DISP_CHAR_VECTOR. */
10457
10458Lisp_Object
10459disp_char_vector (dp, c)
10460 struct Lisp_Char_Table *dp;
10461 int c;
10462{
10463 int code[4], i;
10464 Lisp_Object val;
10465
10466 if (SINGLE_BYTE_CHAR_P (c))
10467 return (dp->contents[c]);
2311178e 10468
c5924f47 10469 SPLIT_CHAR (c, code[0], code[1], code[2]);
260a86a0
KH
10470 if (code[1] < 32)
10471 code[1] = -1;
10472 else if (code[2] < 32)
10473 code[2] = -1;
2311178e 10474
5f5c8ee5
GM
10475 /* Here, the possible range of code[0] (== charset ID) is
10476 128..max_charset. Since the top level char table contains data
10477 for multibyte characters after 256th element, we must increment
10478 code[0] by 128 to get a correct index. */
10479 code[0] += 128;
10480 code[3] = -1; /* anchor */
10481
10482 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10483 {
10484 val = dp->contents[code[i]];
10485 if (!SUB_CHAR_TABLE_P (val))
10486 return (NILP (val) ? dp->defalt : val);
10487 }
2311178e 10488
5f5c8ee5
GM
10489 /* Here, val is a sub char table. We return the default value of
10490 it. */
10491 return (dp->defalt);
10492}
10493
10494
a2889657 10495\f
5f5c8ee5
GM
10496/***********************************************************************
10497 Window Redisplay
10498 ***********************************************************************/
a2725ab2 10499
5f5c8ee5 10500/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
10501
10502static void
5f5c8ee5
GM
10503redisplay_windows (window)
10504 Lisp_Object window;
90adcf20 10505{
5f5c8ee5
GM
10506 while (!NILP (window))
10507 {
10508 struct window *w = XWINDOW (window);
2311178e 10509
5f5c8ee5
GM
10510 if (!NILP (w->hchild))
10511 redisplay_windows (w->hchild);
10512 else if (!NILP (w->vchild))
10513 redisplay_windows (w->vchild);
10514 else
82a7ab23
RS
10515 {
10516 displayed_buffer = XBUFFER (w->buffer);
7033d6df
RS
10517 /* Use list_of_error, not Qerror, so that
10518 we catch only errors and don't run the debugger. */
2311178e 10519 internal_condition_case_1 (redisplay_window_0, window,
7033d6df 10520 list_of_error,
82a7ab23
RS
10521 redisplay_window_error);
10522 }
a2725ab2 10523
5f5c8ee5
GM
10524 window = w->next;
10525 }
10526}
10527
82a7ab23
RS
10528static Lisp_Object
10529redisplay_window_error ()
10530{
10531 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10532 return Qnil;
10533}
10534
10535static Lisp_Object
10536redisplay_window_0 (window)
10537 Lisp_Object window;
10538{
10539 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10540 redisplay_window (window, 0);
10541 return Qnil;
10542}
5f5c8ee5 10543
82a7ab23
RS
10544static Lisp_Object
10545redisplay_window_1 (window)
10546 Lisp_Object window;
10547{
10548 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10549 redisplay_window (window, 1);
10550 return Qnil;
10551}
10552\f
bc6371a6
KH
10553
10554/* Increment GLYPH until it reaches END or CONDITION fails while
10555 adding (GLYPH)->pixel_width to X. */
10556
10557#define SKIP_GLYPHS(glyph, end, x, condition) \
10558 do \
10559 { \
10560 (x) += (glyph)->pixel_width; \
10561 ++(glyph); \
10562 } \
10563 while ((glyph) < (end) && (condition))
10564
10565
5f5c8ee5
GM
10566/* Set cursor position of W. PT is assumed to be displayed in ROW.
10567 DELTA is the number of bytes by which positions recorded in ROW
10568 differ from current buffer positions. */
10569
10570void
10571set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10572 struct window *w;
10573 struct glyph_row *row;
10574 struct glyph_matrix *matrix;
10575 int delta, delta_bytes, dy, dvpos;
10576{
10577 struct glyph *glyph = row->glyphs[TEXT_AREA];
10578 struct glyph *end = glyph + row->used[TEXT_AREA];
ae73dc1c
KH
10579 /* The first glyph that starts a sequence of glyphs from string. */
10580 struct glyph *string_start;
10581 /* The X coordinate of string_start. */
10582 int string_start_x;
10583 /* The last known character position. */
10584 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10585 /* The last known character position before string_start. */
10586 int string_before_pos;
5f5c8ee5
GM
10587 int x = row->x;
10588 int pt_old = PT - delta;
10589
10590 /* Skip over glyphs not having an object at the start of the row.
10591 These are special glyphs like truncation marks on terminal
10592 frames. */
10593 if (row->displays_text_p)
10594 while (glyph < end
6fc556fd 10595 && INTEGERP (glyph->object)
5f5c8ee5
GM
10596 && glyph->charpos < 0)
10597 {
10598 x += glyph->pixel_width;
10599 ++glyph;
10600 }
10601
ae73dc1c 10602 string_start = NULL;
5f5c8ee5 10603 while (glyph < end
6fc556fd 10604 && !INTEGERP (glyph->object)
5f5c8ee5 10605 && (!BUFFERP (glyph->object)
ae73dc1c 10606 || (last_pos = glyph->charpos) < pt_old))
5f5c8ee5 10607 {
ae73dc1c
KH
10608 if (! STRINGP (glyph->object))
10609 {
10610 string_start = NULL;
10611 x += glyph->pixel_width;
10612 ++glyph;
10613 }
10614 else
10615 {
10616 string_before_pos = last_pos;
10617 string_start = glyph;
10618 string_start_x = x;
10619 /* Skip all glyphs from string. */
bc6371a6 10620 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
ae73dc1c
KH
10621 }
10622 }
10623
10624 if (string_start
10625 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10626 {
10627 /* We may have skipped over point because the previous glyphs
10628 are from string. As there's no easy way to know the
10629 character position of the current glyph, find the correct
10630 glyph on point by scanning from string_start again. */
bc6371a6
KH
10631 Lisp_Object limit;
10632 Lisp_Object string;
10633 int pos;
ae73dc1c 10634
bc6371a6
KH
10635 limit = make_number (pt_old + 1);
10636 end = glyph;
ae73dc1c
KH
10637 glyph = string_start;
10638 x = string_start_x;
bc6371a6
KH
10639 string = glyph->object;
10640 pos = string_buffer_position (w, string, string_before_pos);
10641 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10642 because we always put cursor after overlay strings. */
10643 while (pos == 0 && glyph < end)
ae73dc1c 10644 {
bc6371a6
KH
10645 string = glyph->object;
10646 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10647 if (glyph < end)
10648 pos = string_buffer_position (w, glyph->object, string_before_pos);
10649 }
10650
10651 while (glyph < end)
10652 {
10653 pos = XINT (Fnext_single_char_property_change
10654 (make_number (pos), Qdisplay, Qnil, limit));
10655 if (pos > pt_old)
10656 break;
ae73dc1c 10657 /* Skip glyphs from the same string. */
bc6371a6
KH
10658 string = glyph->object;
10659 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10660 /* Skip glyphs from an overlay. */
10661 while (glyph < end
10662 && ! string_buffer_position (w, glyph->object, pos))
ae73dc1c 10663 {
bc6371a6
KH
10664 string = glyph->object;
10665 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
ae73dc1c 10666 }
ae73dc1c 10667 }
5f5c8ee5
GM
10668 }
10669
10670 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10671 w->cursor.x = x;
10672 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10673 w->cursor.y = row->y + dy;
10674
10675 if (w == XWINDOW (selected_window))
10676 {
10677 if (!row->continued_p
10678 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10679 && row->x == 0)
10680 {
10681 this_line_buffer = XBUFFER (w->buffer);
2311178e 10682
5f5c8ee5
GM
10683 CHARPOS (this_line_start_pos)
10684 = MATRIX_ROW_START_CHARPOS (row) + delta;
10685 BYTEPOS (this_line_start_pos)
10686 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
2311178e 10687
5f5c8ee5
GM
10688 CHARPOS (this_line_end_pos)
10689 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10690 BYTEPOS (this_line_end_pos)
10691 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
2311178e 10692
5f5c8ee5
GM
10693 this_line_y = w->cursor.y;
10694 this_line_pixel_height = row->height;
10695 this_line_vpos = w->cursor.vpos;
10696 this_line_start_x = row->x;
10697 }
10698 else
10699 CHARPOS (this_line_start_pos) = 0;
10700 }
10701}
10702
10703
10704/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
10705 start STARTP. Sets the window start of WINDOW to that position.
10706
10707 We assume that the window's buffer is really current. */
5f5c8ee5
GM
10708
10709static INLINE struct text_pos
10710run_window_scroll_functions (window, startp)
10711 Lisp_Object window;
10712 struct text_pos startp;
10713{
10714 struct window *w = XWINDOW (window);
10715 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
10716
10717 if (current_buffer != XBUFFER (w->buffer))
10718 abort ();
10719
5f5c8ee5
GM
10720 if (!NILP (Vwindow_scroll_functions))
10721 {
2311178e 10722 run_hook_with_args_2 (Qwindow_scroll_functions, window,
5f5c8ee5
GM
10723 make_number (CHARPOS (startp)));
10724 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
10725 /* In case the hook functions switch buffers. */
10726 if (current_buffer != XBUFFER (w->buffer))
10727 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 10728 }
90adcf20 10729
5f5c8ee5
GM
10730 return startp;
10731}
10732
10733
ddf6b9a3
RS
10734/* Make sure the line containing the cursor is fully visible.
10735 A value of 1 means there is nothing to be done.
10736 (Either the line is fully visible, or it cannot be made so,
10737 or we cannot tell.)
10738 A value of 0 means the caller should do scrolling
10739 as if point had gone off the screen. */
5f5c8ee5 10740
cb617e7c 10741static int
5f5c8ee5
GM
10742make_cursor_line_fully_visible (w)
10743 struct window *w;
10744{
10745 struct glyph_matrix *matrix;
10746 struct glyph_row *row;
478d746b 10747 int window_height;
2311178e 10748
5f5c8ee5
GM
10749 /* It's not always possible to find the cursor, e.g, when a window
10750 is full of overlay strings. Don't do anything in that case. */
10751 if (w->cursor.vpos < 0)
cb617e7c 10752 return 1;
2311178e 10753
5f5c8ee5
GM
10754 matrix = w->desired_matrix;
10755 row = MATRIX_ROW (matrix, w->cursor.vpos);
10756
acda20e1 10757 /* If the cursor row is not partially visible, there's nothing to do. */
b28cb6ed 10758 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
cb617e7c 10759 return 1;
b28cb6ed
GM
10760
10761 /* If the row the cursor is in is taller than the window's height,
10762 it's not clear what to do, so do nothing. */
10763 window_height = window_box_height (w);
10764 if (row->height >= window_height)
cb617e7c 10765 return 1;
b28cb6ed 10766
ddf6b9a3
RS
10767 return 0;
10768
10769#if 0
10770 /* This code used to try to scroll the window just enough to make
10771 the line visible. It returned 0 to say that the caller should
10772 allocate larger glyph matrices. */
10773
b28cb6ed 10774 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
5f5c8ee5
GM
10775 {
10776 int dy = row->height - row->visible_height;
10777 w->vscroll = 0;
10778 w->cursor.y += dy;
10779 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10780 }
b28cb6ed 10781 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
5f5c8ee5
GM
10782 {
10783 int dy = - (row->height - row->visible_height);
10784 w->vscroll = dy;
10785 w->cursor.y += dy;
10786 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10787 }
2311178e 10788
5f5c8ee5
GM
10789 /* When we change the cursor y-position of the selected window,
10790 change this_line_y as well so that the display optimization for
10791 the cursor line of the selected window in redisplay_internal uses
10792 the correct y-position. */
10793 if (w == XWINDOW (selected_window))
10794 this_line_y = w->cursor.y;
cb617e7c
GM
10795
10796 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10797 redisplay with larger matrices. */
10798 if (matrix->nrows < required_matrix_height (w))
10799 {
10800 fonts_changed_p = 1;
10801 return 0;
10802 }
10803
10804 return 1;
ddf6b9a3 10805#endif /* 0 */
5f5c8ee5
GM
10806}
10807
10808
10809/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10810 non-zero means only WINDOW is redisplayed in redisplay_internal.
10811 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10812 in redisplay_window to bring a partially visible line into view in
10813 the case that only the cursor has moved.
10814
03b0a4b4
RS
10815 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10816 last screen line's vertical height extends past the end of the screen.
10817
5f5c8ee5
GM
10818 Value is
10819
10820 1 if scrolling succeeded
2311178e 10821
5f5c8ee5 10822 0 if scrolling didn't find point.
2311178e 10823
5f5c8ee5
GM
10824 -1 if new fonts have been loaded so that we must interrupt
10825 redisplay, adjust glyph matrices, and try again. */
10826
cb617e7c
GM
10827enum
10828{
10829 SCROLLING_SUCCESS,
10830 SCROLLING_FAILED,
10831 SCROLLING_NEED_LARGER_MATRICES
10832};
10833
5f5c8ee5
GM
10834static int
10835try_scrolling (window, just_this_one_p, scroll_conservatively,
03b0a4b4 10836 scroll_step, temp_scroll_step, last_line_misfit)
5f5c8ee5
GM
10837 Lisp_Object window;
10838 int just_this_one_p;
31ade731 10839 EMACS_INT scroll_conservatively, scroll_step;
5f5c8ee5 10840 int temp_scroll_step;
03b0a4b4 10841 int last_line_misfit;
5f5c8ee5
GM
10842{
10843 struct window *w = XWINDOW (window);
10844 struct frame *f = XFRAME (w->frame);
10845 struct text_pos scroll_margin_pos;
10846 struct text_pos pos;
10847 struct text_pos startp;
10848 struct it it;
10849 Lisp_Object window_end;
10850 int this_scroll_margin;
10851 int dy = 0;
10852 int scroll_max;
b8a63ccb 10853 int rc;
5f5c8ee5
GM
10854 int amount_to_scroll = 0;
10855 Lisp_Object aggressive;
10856 int height;
03b0a4b4 10857 int end_scroll_margin;
5f5c8ee5
GM
10858
10859#if GLYPH_DEBUG
10860 debug_method_add (w, "try_scrolling");
78614721 10861#endif
5f5c8ee5
GM
10862
10863 SET_TEXT_POS_FROM_MARKER (startp, w->start);
2311178e 10864
5f5c8ee5
GM
10865 /* Compute scroll margin height in pixels. We scroll when point is
10866 within this distance from the top or bottom of the window. */
10867 if (scroll_margin > 0)
90adcf20 10868 {
da8b7f4f
KS
10869 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10870 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
5f5c8ee5
GM
10871 }
10872 else
10873 this_scroll_margin = 0;
10874
10875 /* Compute how much we should try to scroll maximally to bring point
10876 into view. */
0894e696
SM
10877 if (scroll_step || scroll_conservatively || temp_scroll_step)
10878 scroll_max = max (scroll_step,
10879 max (scroll_conservatively, temp_scroll_step));
5f5c8ee5
GM
10880 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10881 || NUMBERP (current_buffer->scroll_up_aggressively))
10882 /* We're trying to scroll because of aggressive scrolling
10883 but no scroll_step is set. Choose an arbitrary one. Maybe
10884 there should be a variable for this. */
10885 scroll_max = 10;
10886 else
10887 scroll_max = 0;
da8b7f4f 10888 scroll_max *= FRAME_LINE_HEIGHT (f);
5f5c8ee5
GM
10889
10890 /* Decide whether we have to scroll down. Start at the window end
10891 and move this_scroll_margin up to find the position of the scroll
10892 margin. */
10893 window_end = Fwindow_end (window, Qt);
03b0a4b4
RS
10894
10895 too_near_end:
10896
5f5c8ee5
GM
10897 CHARPOS (scroll_margin_pos) = XINT (window_end);
10898 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
03b0a4b4
RS
10899
10900 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
10901 if (end_scroll_margin)
5f5c8ee5
GM
10902 {
10903 start_display (&it, w, scroll_margin_pos);
03b0a4b4 10904 move_it_vertically (&it, - end_scroll_margin);
5f5c8ee5
GM
10905 scroll_margin_pos = it.current.pos;
10906 }
10907
10908 if (PT >= CHARPOS (scroll_margin_pos))
10909 {
10910 int y0;
2311178e 10911
5f5c8ee5
GM
10912 /* Point is in the scroll margin at the bottom of the window, or
10913 below. Compute a new window start that makes point visible. */
47589c8c 10914
5f5c8ee5
GM
10915 /* Compute the distance from the scroll margin to PT.
10916 Give up if the distance is greater than scroll_max. */
10917 start_display (&it, w, scroll_margin_pos);
10918 y0 = it.current_y;
10919 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10920 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
539e92ad
GM
10921
10922 /* To make point visible, we have to move the window start
10923 down so that the line the cursor is in is visible, which
10924 means we have to add in the height of the cursor line. */
10925 dy = line_bottom_y (&it) - y0;
2311178e 10926
5f5c8ee5 10927 if (dy > scroll_max)
cb617e7c 10928 return SCROLLING_FAILED;
2311178e 10929
5f5c8ee5
GM
10930 /* Move the window start down. If scrolling conservatively,
10931 move it just enough down to make point visible. If
10932 scroll_step is set, move it down by scroll_step. */
10933 start_display (&it, w, startp);
10934
10935 if (scroll_conservatively)
03b0a4b4
RS
10936 /* Set AMOUNT_TO_SCROLL to at least one line,
10937 and at most scroll_conservatively lines. */
e89aaabd 10938 amount_to_scroll
da8b7f4f
KS
10939 = min (max (dy, FRAME_LINE_HEIGHT (f)),
10940 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
5f5c8ee5
GM
10941 else if (scroll_step || temp_scroll_step)
10942 amount_to_scroll = scroll_max;
10943 else
90adcf20 10944 {
46226c1d 10945 aggressive = current_buffer->scroll_up_aggressively;
da8b7f4f 10946 height = WINDOW_BOX_TEXT_HEIGHT (w);
5f5c8ee5
GM
10947 if (NUMBERP (aggressive))
10948 amount_to_scroll = XFLOATINT (aggressive) * height;
10949 }
a2725ab2 10950
5f5c8ee5 10951 if (amount_to_scroll <= 0)
cb617e7c 10952 return SCROLLING_FAILED;
a2725ab2 10953
03b0a4b4
RS
10954 /* If moving by amount_to_scroll leaves STARTP unchanged,
10955 move it down one screen line. */
10956
5f5c8ee5 10957 move_it_vertically (&it, amount_to_scroll);
03b0a4b4
RS
10958 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10959 move_it_by_lines (&it, 1, 1);
5f5c8ee5
GM
10960 startp = it.current.pos;
10961 }
10962 else
10963 {
10964 /* See if point is inside the scroll margin at the top of the
10965 window. */
10966 scroll_margin_pos = startp;
10967 if (this_scroll_margin)
10968 {
10969 start_display (&it, w, startp);
10970 move_it_vertically (&it, this_scroll_margin);
10971 scroll_margin_pos = it.current.pos;
10972 }
10973
10974 if (PT < CHARPOS (scroll_margin_pos))
10975 {
10976 /* Point is in the scroll margin at the top of the window or
10977 above what is displayed in the window. */
10978 int y0;
2311178e 10979
5f5c8ee5
GM
10980 /* Compute the vertical distance from PT to the scroll
10981 margin position. Give up if distance is greater than
10982 scroll_max. */
10983 SET_TEXT_POS (pos, PT, PT_BYTE);
10984 start_display (&it, w, pos);
10985 y0 = it.current_y;
10986 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
10987 it.last_visible_y, -1,
10988 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10989 dy = it.current_y - y0;
10990 if (dy > scroll_max)
cb617e7c 10991 return SCROLLING_FAILED;
2311178e 10992
5f5c8ee5
GM
10993 /* Compute new window start. */
10994 start_display (&it, w, startp);
2311178e 10995
5f5c8ee5 10996 if (scroll_conservatively)
0894e696 10997 amount_to_scroll =
da8b7f4f 10998 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
10999 else if (scroll_step || temp_scroll_step)
11000 amount_to_scroll = scroll_max;
538f13d4 11001 else
5f5c8ee5 11002 {
46226c1d 11003 aggressive = current_buffer->scroll_down_aggressively;
da8b7f4f 11004 height = WINDOW_BOX_TEXT_HEIGHT (w);
5f5c8ee5
GM
11005 if (NUMBERP (aggressive))
11006 amount_to_scroll = XFLOATINT (aggressive) * height;
11007 }
a2725ab2 11008
5f5c8ee5 11009 if (amount_to_scroll <= 0)
cb617e7c 11010 return SCROLLING_FAILED;
2311178e 11011
5f5c8ee5
GM
11012 move_it_vertically (&it, - amount_to_scroll);
11013 startp = it.current.pos;
90adcf20
RS
11014 }
11015 }
a2889657 11016
5f5c8ee5
GM
11017 /* Run window scroll functions. */
11018 startp = run_window_scroll_functions (window, startp);
90adcf20 11019
5f5c8ee5
GM
11020 /* Display the window. Give up if new fonts are loaded, or if point
11021 doesn't appear. */
11022 if (!try_window (window, startp))
cb617e7c 11023 rc = SCROLLING_NEED_LARGER_MATRICES;
5f5c8ee5
GM
11024 else if (w->cursor.vpos < 0)
11025 {
11026 clear_glyph_matrix (w->desired_matrix);
cb617e7c 11027 rc = SCROLLING_FAILED;
5f5c8ee5
GM
11028 }
11029 else
11030 {
11031 /* Maybe forget recorded base line for line number display. */
2311178e 11032 if (!just_this_one_p
5f5c8ee5 11033 || current_buffer->clip_changed
9142dd5b 11034 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5 11035 w->base_line_number = Qnil;
2311178e 11036
ddf6b9a3
RS
11037 /* If cursor ends up on a partially visible line,
11038 treat that as being off the bottom of the screen. */
11039 if (! make_cursor_line_fully_visible (w))
ef3c2c73
RS
11040 {
11041 clear_glyph_matrix (w->desired_matrix);
03b0a4b4 11042 last_line_misfit = 1;
ef3c2c73
RS
11043 goto too_near_end;
11044 }
ddf6b9a3 11045 rc = SCROLLING_SUCCESS;
5f5c8ee5
GM
11046 }
11047
11048 return rc;
a2889657
JB
11049}
11050
5f5c8ee5
GM
11051
11052/* Compute a suitable window start for window W if display of W starts
11053 on a continuation line. Value is non-zero if a new window start
11054 was computed.
11055
11056 The new window start will be computed, based on W's width, starting
11057 from the start of the continued line. It is the start of the
11058 screen line with the minimum distance from the old start W->start. */
11059
11060static int
11061compute_window_start_on_continuation_line (w)
11062 struct window *w;
1f1ff51d 11063{
5f5c8ee5
GM
11064 struct text_pos pos, start_pos;
11065 int window_start_changed_p = 0;
1f1ff51d 11066
5f5c8ee5 11067 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 11068
5f5c8ee5 11069 /* If window start is on a continuation line... Window start may be
2311178e 11070 < BEGV in case there's invisible text at the start of the
5f5c8ee5
GM
11071 buffer (M-x rmail, for example). */
11072 if (CHARPOS (start_pos) > BEGV
11073 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 11074 {
5f5c8ee5
GM
11075 struct it it;
11076 struct glyph_row *row;
f3751a65
GM
11077
11078 /* Handle the case that the window start is out of range. */
11079 if (CHARPOS (start_pos) < BEGV)
11080 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11081 else if (CHARPOS (start_pos) > ZV)
11082 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
2311178e 11083
5f5c8ee5
GM
11084 /* Find the start of the continued line. This should be fast
11085 because scan_buffer is fast (newline cache). */
045dee35 11086 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
11087 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11088 row, DEFAULT_FACE_ID);
11089 reseat_at_previous_visible_line_start (&it);
11090
11091 /* If the line start is "too far" away from the window start,
11092 say it takes too much time to compute a new window start. */
11093 if (CHARPOS (start_pos) - IT_CHARPOS (it)
da8b7f4f 11094 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
5f5c8ee5
GM
11095 {
11096 int min_distance, distance;
2311178e 11097
5f5c8ee5
GM
11098 /* Move forward by display lines to find the new window
11099 start. If window width was enlarged, the new start can
11100 be expected to be > the old start. If window width was
11101 decreased, the new window start will be < the old start.
11102 So, we're looking for the display line start with the
11103 minimum distance from the old window start. */
11104 pos = it.current.pos;
11105 min_distance = INFINITY;
11106 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11107 distance < min_distance)
11108 {
11109 min_distance = distance;
11110 pos = it.current.pos;
11111 move_it_by_lines (&it, 1, 0);
11112 }
2311178e 11113
5f5c8ee5
GM
11114 /* Set the window start there. */
11115 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11116 window_start_changed_p = 1;
11117 }
1f1ff51d 11118 }
2311178e 11119
5f5c8ee5 11120 return window_start_changed_p;
1f1ff51d
KH
11121}
11122
5f5c8ee5 11123
1dd5768b 11124/* Try cursor movement in case text has not changed in window WINDOW,
47589c8c
GM
11125 with window start STARTP. Value is
11126
cb617e7c 11127 CURSOR_MOVEMENT_SUCCESS if successful
2311178e 11128
cb617e7c
GM
11129 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11130
11131 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11132 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11133 we want to scroll as if scroll-step were set to 1. See the code.
11134
11135 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11136 which case we have to abort this redisplay, and adjust matrices
11137 first. */
11138
2311178e 11139enum
cb617e7c
GM
11140{
11141 CURSOR_MOVEMENT_SUCCESS,
11142 CURSOR_MOVEMENT_CANNOT_BE_USED,
11143 CURSOR_MOVEMENT_MUST_SCROLL,
11144 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11145};
47589c8c
GM
11146
11147static int
11148try_cursor_movement (window, startp, scroll_step)
11149 Lisp_Object window;
11150 struct text_pos startp;
11151 int *scroll_step;
11152{
11153 struct window *w = XWINDOW (window);
11154 struct frame *f = XFRAME (w->frame);
cb617e7c 11155 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
2311178e 11156
69d1f7c9 11157#if GLYPH_DEBUG
76cb5e06
GM
11158 if (inhibit_try_cursor_movement)
11159 return rc;
11160#endif
11161
47589c8c
GM
11162 /* Handle case where text has not changed, only point, and it has
11163 not moved off the frame. */
11164 if (/* Point may be in this window. */
11165 PT >= CHARPOS (startp)
47589c8c
GM
11166 /* Selective display hasn't changed. */
11167 && !current_buffer->clip_changed
4db87380
GM
11168 /* Function force-mode-line-update is used to force a thorough
11169 redisplay. It sets either windows_or_buffers_changed or
11170 update_mode_lines. So don't take a shortcut here for these
11171 cases. */
11172 && !update_mode_lines
11173 && !windows_or_buffers_changed
5fb96e96 11174 && !cursor_type_changed
2311178e 11175 /* Can't use this case if highlighting a region. When a
47589c8c
GM
11176 region exists, cursor movement has to do more than just
11177 set the cursor. */
11178 && !(!NILP (Vtransient_mark_mode)
11179 && !NILP (current_buffer->mark_active))
11180 && NILP (w->region_showing)
11181 && NILP (Vshow_trailing_whitespace)
11182 /* Right after splitting windows, last_point may be nil. */
11183 && INTEGERP (w->last_point)
11184 /* This code is not used for mini-buffer for the sake of the case
11185 of redisplaying to replace an echo area message; since in
11186 that case the mini-buffer contents per se are usually
11187 unchanged. This code is of no real use in the mini-buffer
11188 since the handling of this_line_start_pos, etc., in redisplay
11189 handles the same cases. */
11190 && !EQ (window, minibuf_window)
11191 /* When splitting windows or for new windows, it happens that
11192 redisplay is called with a nil window_end_vpos or one being
11193 larger than the window. This should really be fixed in
11194 window.c. I don't have this on my list, now, so we do
11195 approximately the same as the old redisplay code. --gerd. */
11196 && INTEGERP (w->window_end_vpos)
11197 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11198 && (FRAME_WINDOW_P (f)
11199 || !MARKERP (Voverlay_arrow_position)
11200 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
11201 {
11202 int this_scroll_margin;
8ee5b6a3 11203 struct glyph_row *row = NULL;
47589c8c
GM
11204
11205#if GLYPH_DEBUG
11206 debug_method_add (w, "cursor movement");
11207#endif
11208
11209 /* Scroll if point within this distance from the top or bottom
11210 of the window. This is a pixel value. */
11211 this_scroll_margin = max (0, scroll_margin);
da8b7f4f
KS
11212 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11213 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
47589c8c
GM
11214
11215 /* Start with the row the cursor was displayed during the last
11216 not paused redisplay. Give up if that row is not valid. */
bd9d0f3f
GM
11217 if (w->last_cursor.vpos < 0
11218 || w->last_cursor.vpos >= w->current_matrix->nrows)
cb617e7c 11219 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
11220 else
11221 {
11222 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11223 if (row->mode_line_p)
11224 ++row;
11225 if (!row->enabled_p)
cb617e7c 11226 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
11227 }
11228
cb617e7c 11229 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
47589c8c
GM
11230 {
11231 int scroll_p = 0;
68c5d1db 11232 int last_y = window_text_bottom_y (w) - this_scroll_margin;
2311178e 11233
47589c8c
GM
11234 if (PT > XFASTINT (w->last_point))
11235 {
11236 /* Point has moved forward. */
47589c8c
GM
11237 while (MATRIX_ROW_END_CHARPOS (row) < PT
11238 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11239 {
11240 xassert (row->enabled_p);
11241 ++row;
11242 }
11243
11244 /* The end position of a row equals the start position
11245 of the next row. If PT is there, we would rather
cafafe0b
GM
11246 display it in the next line. */
11247 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11248 && MATRIX_ROW_END_CHARPOS (row) == PT
11249 && !cursor_row_p (w, row))
11250 ++row;
47589c8c
GM
11251
11252 /* If within the scroll margin, scroll. Note that
11253 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11254 the next line would be drawn, and that
11255 this_scroll_margin can be zero. */
11256 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11257 || PT > MATRIX_ROW_END_CHARPOS (row)
11258 /* Line is completely visible last line in window
11259 and PT is to be set in the next line. */
11260 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11261 && PT == MATRIX_ROW_END_CHARPOS (row)
11262 && !row->ends_at_zv_p
11263 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11264 scroll_p = 1;
11265 }
11266 else if (PT < XFASTINT (w->last_point))
11267 {
11268 /* Cursor has to be moved backward. Note that PT >=
11269 CHARPOS (startp) because of the outer
11270 if-statement. */
11271 while (!row->mode_line_p
11272 && (MATRIX_ROW_START_CHARPOS (row) > PT
11273 || (MATRIX_ROW_START_CHARPOS (row) == PT
11274 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11275 && (row->y > this_scroll_margin
11276 || CHARPOS (startp) == BEGV))
11277 {
11278 xassert (row->enabled_p);
11279 --row;
11280 }
11281
11282 /* Consider the following case: Window starts at BEGV,
11283 there is invisible, intangible text at BEGV, so that
11284 display starts at some point START > BEGV. It can
11285 happen that we are called with PT somewhere between
11286 BEGV and START. Try to handle that case. */
11287 if (row < w->current_matrix->rows
11288 || row->mode_line_p)
11289 {
11290 row = w->current_matrix->rows;
11291 if (row->mode_line_p)
11292 ++row;
11293 }
11294
11295 /* Due to newlines in overlay strings, we may have to
11296 skip forward over overlay strings. */
68c5d1db
GM
11297 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11298 && MATRIX_ROW_END_CHARPOS (row) == PT
11299 && !cursor_row_p (w, row))
47589c8c 11300 ++row;
2311178e 11301
47589c8c
GM
11302 /* If within the scroll margin, scroll. */
11303 if (row->y < this_scroll_margin
11304 && CHARPOS (startp) != BEGV)
11305 scroll_p = 1;
11306 }
11307
11308 if (PT < MATRIX_ROW_START_CHARPOS (row)
11309 || PT > MATRIX_ROW_END_CHARPOS (row))
11310 {
11311 /* if PT is not in the glyph row, give up. */
cb617e7c 11312 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c 11313 }
440fc135 11314 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
47589c8c 11315 {
8de4aaf8
GM
11316 if (PT == MATRIX_ROW_END_CHARPOS (row)
11317 && !row->ends_at_zv_p
11318 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
cb617e7c 11319 rc = CURSOR_MOVEMENT_MUST_SCROLL;
3f7e3031 11320 else if (row->height > window_box_height (w))
440fc135 11321 {
8de4aaf8
GM
11322 /* If we end up in a partially visible line, let's
11323 make it fully visible, except when it's taller
11324 than the window, in which case we can't do much
11325 about it. */
440fc135 11326 *scroll_step = 1;
cb617e7c 11327 rc = CURSOR_MOVEMENT_MUST_SCROLL;
440fc135
GM
11328 }
11329 else
11330 {
11331 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
cb617e7c 11332 if (!make_cursor_line_fully_visible (w))
ddf6b9a3 11333 rc = CURSOR_MOVEMENT_MUST_SCROLL;
cb617e7c
GM
11334 else
11335 rc = CURSOR_MOVEMENT_SUCCESS;
440fc135 11336 }
47589c8c
GM
11337 }
11338 else if (scroll_p)
cb617e7c 11339 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
11340 else
11341 {
11342 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
cb617e7c 11343 rc = CURSOR_MOVEMENT_SUCCESS;
47589c8c
GM
11344 }
11345 }
11346 }
11347
11348 return rc;
11349}
11350
9a08d928
SM
11351void
11352set_vertical_scroll_bar (w)
11353 struct window *w;
11354{
11355 int start, end, whole;
11356
11357 /* Calculate the start and end positions for the current window.
11358 At some point, it would be nice to choose between scrollbars
11359 which reflect the whole buffer size, with special markers
11360 indicating narrowing, and scrollbars which reflect only the
11361 visible region.
11362
11363 Note that mini-buffers sometimes aren't displaying any text. */
11364 if (!MINI_WINDOW_P (w)
11365 || (w == XWINDOW (minibuf_window)
11366 && NILP (echo_area_buffer[0])))
11367 {
11368 struct buffer *buf = XBUFFER (w->buffer);
11369 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11370 start = marker_position (w->start) - BUF_BEGV (buf);
11371 /* I don't think this is guaranteed to be right. For the
11372 moment, we'll pretend it is. */
11373 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11374
11375 if (end < start)
11376 end = start;
11377 if (whole < (end - start))
11378 whole = end - start;
11379 }
11380 else
11381 start = end = whole = 0;
11382
11383 /* Indicate what this scroll bar ought to be displaying now. */
11384 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11385}
47589c8c 11386
5f5c8ee5 11387/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
b60c9653
RS
11388 selected_window is redisplayed.
11389
11390 We can return without actually redisplaying the window if
11391 fonts_changed_p is nonzero. In that case, redisplay_internal will
11392 retry. */
90adcf20 11393
a2889657 11394static void
5f5c8ee5 11395redisplay_window (window, just_this_one_p)
a2889657 11396 Lisp_Object window;
5f5c8ee5 11397 int just_this_one_p;
a2889657 11398{
5f5c8ee5
GM
11399 struct window *w = XWINDOW (window);
11400 struct frame *f = XFRAME (w->frame);
11401 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 11402 struct buffer *old = current_buffer;
5f5c8ee5 11403 struct text_pos lpoint, opoint, startp;
e481f960 11404 int update_mode_line;
5f5c8ee5
GM
11405 int tem;
11406 struct it it;
11407 /* Record it now because it's overwritten. */
11408 int current_matrix_up_to_date_p = 0;
c62c1bb5
RS
11409 /* This is less strict than current_matrix_up_to_date_p.
11410 It indictes that the buffer contents and narrowing are unchanged. */
11411 int buffer_unchanged_p = 0;
5f5c8ee5 11412 int temp_scroll_step = 0;
331379bf 11413 int count = SPECPDL_INDEX ();
47589c8c 11414 int rc;
ddf6b9a3 11415 int centering_position;
03b0a4b4 11416 int last_line_misfit = 0;
a2889657 11417
5f5c8ee5
GM
11418 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11419 opoint = lpoint;
a2889657 11420
5f5c8ee5
GM
11421 /* W must be a leaf window here. */
11422 xassert (!NILP (w->buffer));
11423#if GLYPH_DEBUG
11424 *w->desired_matrix->method = 0;
11425#endif
2e54982e
RS
11426
11427 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
11428
11429 reconsider_clip_changes (w, buffer);
2311178e
TTN
11430
11431 /* Has the mode line to be updated? */
5f5c8ee5
GM
11432 update_mode_line = (!NILP (w->update_mode_line)
11433 || update_mode_lines
c62c1bb5
RS
11434 || buffer->clip_changed
11435 || buffer->prevent_redisplay_optimizations_p);
8de2d90b
JB
11436
11437 if (MINI_WINDOW_P (w))
11438 {
5f5c8ee5 11439 if (w == XWINDOW (echo_area_window)
c6e89d6c 11440 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
11441 {
11442 if (update_mode_line)
11443 /* We may have to update a tty frame's menu bar or a
e037b9ec 11444 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
11445 goto finish_menu_bars;
11446 else
11447 /* We've already displayed the echo area glyphs in this window. */
11448 goto finish_scroll_bars;
11449 }
b2b5455d
RS
11450 else if ((w != XWINDOW (minibuf_window)
11451 || minibuf_level == 0)
c0bcce6f
JPW
11452 /* When buffer is nonempty, redisplay window normally. */
11453 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
3354fdcf
RS
11454 /* Quail displays non-mini buffers in minibuffer window.
11455 In that case, redisplay the window normally. */
b2b5455d 11456 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
8de2d90b 11457 {
3354fdcf
RS
11458 /* W is a mini-buffer window, but it's not active, so clear
11459 it. */
5f5c8ee5
GM
11460 int yb = window_text_bottom_y (w);
11461 struct glyph_row *row;
11462 int y;
11463
11464 for (y = 0, row = w->desired_matrix->rows;
11465 y < yb;
11466 y += row->height, ++row)
11467 blank_row (w, row, y);
88f22aff 11468 goto finish_scroll_bars;
8de2d90b 11469 }
c095a1dd
GM
11470
11471 clear_glyph_matrix (w->desired_matrix);
8de2d90b 11472 }
a2889657 11473
5f5c8ee5
GM
11474 /* Otherwise set up data on this window; select its buffer and point
11475 value. */
6a93695f
GM
11476 /* Really select the buffer, for the sake of buffer-local
11477 variables. */
11478 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5
GM
11479 SET_TEXT_POS (opoint, PT, PT_BYTE);
11480
11481 current_matrix_up_to_date_p
11482 = (!NILP (w->window_end_valid)
11483 && !current_buffer->clip_changed
c62c1bb5 11484 && !current_buffer->prevent_redisplay_optimizations_p
5f5c8ee5
GM
11485 && XFASTINT (w->last_modified) >= MODIFF
11486 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 11487
c62c1bb5
RS
11488 buffer_unchanged_p
11489 = (!NILP (w->window_end_valid)
11490 && !current_buffer->clip_changed
3f1258d0 11491 && XFASTINT (w->last_modified) >= MODIFF
c62c1bb5
RS
11492 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11493
5f5c8ee5
GM
11494 /* When windows_or_buffers_changed is non-zero, we can't rely on
11495 the window end being valid, so set it to nil there. */
11496 if (windows_or_buffers_changed)
11497 {
11498 /* If window starts on a continuation line, maybe adjust the
11499 window start in case the window's width changed. */
11500 if (XMARKER (w->start)->buffer == current_buffer)
11501 compute_window_start_on_continuation_line (w);
2311178e 11502
5f5c8ee5
GM
11503 w->window_end_valid = Qnil;
11504 }
12adba34 11505
5f5c8ee5
GM
11506 /* Some sanity checks. */
11507 CHECK_WINDOW_END (w);
11508 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 11509 abort ();
5f5c8ee5 11510 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 11511 abort ();
a2889657 11512
28995e67
RS
11513 /* If %c is in mode line, update it if needed. */
11514 if (!NILP (w->column_number_displayed)
11515 /* This alternative quickly identifies a common case
11516 where no change is needed. */
11517 && !(PT == XFASTINT (w->last_point)
8850a573
RS
11518 && XFASTINT (w->last_modified) >= MODIFF
11519 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
2311178e
TTN
11520 && (XFASTINT (w->column_number_displayed)
11521 != (int) current_column ())) /* iftc */
11522 update_mode_line = 1;
28995e67 11523
5f5c8ee5
GM
11524 /* Count number of windows showing the selected buffer. An indirect
11525 buffer counts as its base buffer. */
11526 if (!just_this_one_p)
42640f83
RS
11527 {
11528 struct buffer *current_base, *window_base;
11529 current_base = current_buffer;
11530 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11531 if (current_base->base_buffer)
11532 current_base = current_base->base_buffer;
11533 if (window_base->base_buffer)
11534 window_base = window_base->base_buffer;
11535 if (current_base == window_base)
11536 buffer_shared++;
11537 }
a2889657 11538
5f5c8ee5
GM
11539 /* Point refers normally to the selected window. For any other
11540 window, set up appropriate value. */
a2889657
JB
11541 if (!EQ (window, selected_window))
11542 {
12adba34
RS
11543 int new_pt = XMARKER (w->pointm)->charpos;
11544 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 11545 if (new_pt < BEGV)
a2889657 11546 {
f67a0f51 11547 new_pt = BEGV;
12adba34
RS
11548 new_pt_byte = BEGV_BYTE;
11549 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 11550 }
f67a0f51 11551 else if (new_pt > (ZV - 1))
a2889657 11552 {
f67a0f51 11553 new_pt = ZV;
12adba34
RS
11554 new_pt_byte = ZV_BYTE;
11555 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 11556 }
2311178e 11557
f67a0f51 11558 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 11559 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
11560 }
11561
f4faa47c 11562 /* If any of the character widths specified in the display table
5f5c8ee5
GM
11563 have changed, invalidate the width run cache. It's true that
11564 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
11565 redisplay goes (non-fatally) haywire when the display table is
11566 changed, so why should we worry about doing any better? */
11567 if (current_buffer->width_run_cache)
11568 {
f908610f 11569 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
11570
11571 if (! disptab_matches_widthtab (disptab,
11572 XVECTOR (current_buffer->width_table)))
11573 {
11574 invalidate_region_cache (current_buffer,
11575 current_buffer->width_run_cache,
11576 BEG, Z);
11577 recompute_width_table (current_buffer, disptab);
11578 }
11579 }
11580
a2889657 11581 /* If window-start is screwed up, choose a new one. */
a2889657
JB
11582 if (XMARKER (w->start)->buffer != current_buffer)
11583 goto recenter;
11584
5f5c8ee5 11585 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 11586
cf0df6ab
RS
11587 /* If someone specified a new starting point but did not insist,
11588 check whether it can be used. */
cfad01b4
GM
11589 if (!NILP (w->optional_new_start)
11590 && CHARPOS (startp) >= BEGV
11591 && CHARPOS (startp) <= ZV)
cf0df6ab
RS
11592 {
11593 w->optional_new_start = Qnil;
5f5c8ee5
GM
11594 start_display (&it, w, startp);
11595 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11596 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11597 if (IT_CHARPOS (it) == PT)
cf0df6ab 11598 w->force_start = Qt;
29b3ea63
KS
11599 /* IT may overshoot PT if text at PT is invisible. */
11600 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11601 w->force_start = Qt;
11602
11603
cf0df6ab
RS
11604 }
11605
8de2d90b 11606 /* Handle case where place to start displaying has been specified,
aa6d10fa 11607 unless the specified location is outside the accessible range. */
9472f927
GM
11608 if (!NILP (w->force_start)
11609 || w->frozen_window_start_p)
a2889657 11610 {
b60c9653
RS
11611 /* We set this later on if we have to adjust point. */
11612 int new_vpos = -1;
11613
e63574d7 11614 w->force_start = Qnil;
5f5c8ee5 11615 w->vscroll = 0;
b5174a51 11616 w->window_end_valid = Qnil;
5f5c8ee5
GM
11617
11618 /* Forget any recorded base line for line number display. */
c62c1bb5 11619 if (!buffer_unchanged_p)
5f5c8ee5
GM
11620 w->base_line_number = Qnil;
11621
75c43375
RS
11622 /* Redisplay the mode line. Select the buffer properly for that.
11623 Also, run the hook window-scroll-functions
11624 because we have scrolled. */
e63574d7
RS
11625 /* Note, we do this after clearing force_start because
11626 if there's an error, it is better to forget about force_start
11627 than to get into an infinite loop calling the hook functions
11628 and having them get more errors. */
75c43375
RS
11629 if (!update_mode_line
11630 || ! NILP (Vwindow_scroll_functions))
e481f960 11631 {
e481f960
RS
11632 update_mode_line = 1;
11633 w->update_mode_line = Qt;
5f5c8ee5 11634 startp = run_window_scroll_functions (window, startp);
e481f960 11635 }
2311178e 11636
ac90c44f
GM
11637 w->last_modified = make_number (0);
11638 w->last_overlay_modified = make_number (0);
5f5c8ee5
GM
11639 if (CHARPOS (startp) < BEGV)
11640 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11641 else if (CHARPOS (startp) > ZV)
11642 SET_TEXT_POS (startp, ZV, ZV_BYTE);
2311178e
TTN
11643
11644 /* Redisplay, then check if cursor has been set during the
5f5c8ee5
GM
11645 redisplay. Give up if new fonts were loaded. */
11646 if (!try_window (window, startp))
11647 {
11648 w->force_start = Qt;
11649 clear_glyph_matrix (w->desired_matrix);
b60c9653 11650 goto need_larger_matrices;
5f5c8ee5
GM
11651 }
11652
9472f927 11653 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5 11654 {
b28cb6ed
GM
11655 /* If point does not appear, try to move point so it does
11656 appear. The desired matrix has been built above, so we
11657 can use it here. */
b60c9653
RS
11658 new_vpos = window_box_height (w) / 2;
11659 }
11660
11661 if (!make_cursor_line_fully_visible (w))
11662 {
11663 /* Point does appear, but on a line partly visible at end of window.
11664 Move it back to a fully-visible line. */
11665 new_vpos = window_box_height (w);
11666 }
11667
11668 /* If we need to move point for either of the above reasons,
11669 now actually do it. */
11670 if (new_vpos >= 0)
11671 {
b28cb6ed
GM
11672 struct glyph_row *row;
11673
b28cb6ed 11674 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
b60c9653 11675 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
5f5c8ee5
GM
11676 ++row;
11677
11678 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11679 MATRIX_ROW_START_BYTEPOS (row));
11680
90adcf20 11681 if (w != XWINDOW (selected_window))
12adba34 11682 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
11683 else if (current_buffer == old)
11684 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11685
11686 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
2311178e 11687
5f5c8ee5
GM
11688 /* If we are highlighting the region, then we just changed
11689 the region, so redisplay to show it. */
df0b5ea1
RS
11690 if (!NILP (Vtransient_mark_mode)
11691 && !NILP (current_buffer->mark_active))
6f27fa9b 11692 {
5f5c8ee5
GM
11693 clear_glyph_matrix (w->desired_matrix);
11694 if (!try_window (window, startp))
cb617e7c 11695 goto need_larger_matrices;
6f27fa9b 11696 }
a2889657 11697 }
5f5c8ee5 11698
5f5c8ee5
GM
11699#if GLYPH_DEBUG
11700 debug_method_add (w, "forced window start");
11701#endif
a2889657
JB
11702 goto done;
11703 }
11704
5f5c8ee5 11705 /* Handle case where text has not changed, only point, and it has
3f029ea0
RS
11706 not moved off the frame, and we are not retrying after hscroll.
11707 (current_matrix_up_to_date_p is nonzero when retrying.) */
11708 if (current_matrix_up_to_date_p
47589c8c 11709 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
cb617e7c 11710 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
a2889657 11711 {
cb617e7c
GM
11712 switch (rc)
11713 {
11714 case CURSOR_MOVEMENT_SUCCESS:
11715 goto done;
2311178e 11716
b60c9653 11717#if 0 /* try_cursor_movement never returns this value. */
cb617e7c
GM
11718 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11719 goto need_larger_matrices;
b60c9653 11720#endif
2311178e 11721
cb617e7c
GM
11722 case CURSOR_MOVEMENT_MUST_SCROLL:
11723 goto try_to_scroll;
2311178e 11724
cb617e7c
GM
11725 default:
11726 abort ();
11727 }
a2889657
JB
11728 }
11729 /* If current starting point was originally the beginning of a line
11730 but no longer is, find a new starting point. */
265a9e55 11731 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
11732 && !(CHARPOS (startp) <= BEGV
11733 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 11734 {
5f5c8ee5
GM
11735#if GLYPH_DEBUG
11736 debug_method_add (w, "recenter 1");
11737#endif
a2889657
JB
11738 goto recenter;
11739 }
2311178e 11740
27d16f05
GM
11741 /* Try scrolling with try_window_id. Value is > 0 if update has
11742 been done, it is -1 if we know that the same window start will
11743 not work. It is 0 if unsuccessful for some other reason. */
11744 else if ((tem = try_window_id (w)) != 0)
a2889657 11745 {
5f5c8ee5 11746#if GLYPH_DEBUG
ef121659 11747 debug_method_add (w, "try_window_id %d", tem);
5f5c8ee5
GM
11748#endif
11749
11750 if (fonts_changed_p)
cb617e7c 11751 goto need_larger_matrices;
a2889657
JB
11752 if (tem > 0)
11753 goto done;
ef121659 11754
5f5c8ee5
GM
11755 /* Otherwise try_window_id has returned -1 which means that we
11756 don't want the alternative below this comment to execute. */
a2889657 11757 }
5f5c8ee5
GM
11758 else if (CHARPOS (startp) >= BEGV
11759 && CHARPOS (startp) <= ZV
11760 && PT >= CHARPOS (startp)
11761 && (CHARPOS (startp) < ZV
e9874cee 11762 /* Avoid starting at end of buffer. */
5f5c8ee5 11763 || CHARPOS (startp) == BEGV
8850a573
RS
11764 || (XFASTINT (w->last_modified) >= MODIFF
11765 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 11766 {
5f5c8ee5
GM
11767#if GLYPH_DEBUG
11768 debug_method_add (w, "same window start");
11769#endif
2311178e 11770
5f5c8ee5
GM
11771 /* Try to redisplay starting at same place as before.
11772 If point has not moved off frame, accept the results. */
11773 if (!current_matrix_up_to_date_p
11774 /* Don't use try_window_reusing_current_matrix in this case
15e26c76
GM
11775 because a window scroll function can have changed the
11776 buffer. */
5f5c8ee5
GM
11777 || !NILP (Vwindow_scroll_functions)
11778 || MINI_WINDOW_P (w)
11779 || !try_window_reusing_current_matrix (w))
11780 {
11781 IF_DEBUG (debug_method_add (w, "1"));
11782 try_window (window, startp);
11783 }
11784
11785 if (fonts_changed_p)
cb617e7c 11786 goto need_larger_matrices;
2311178e 11787
5f5c8ee5 11788 if (w->cursor.vpos >= 0)
aa6d10fa 11789 {
2311178e 11790 if (!just_this_one_p
5f5c8ee5 11791 || current_buffer->clip_changed
9142dd5b 11792 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
11793 /* Forget any recorded base line for line number display. */
11794 w->base_line_number = Qnil;
2311178e 11795
cb617e7c 11796 if (!make_cursor_line_fully_visible (w))
03b0a4b4
RS
11797 {
11798 clear_glyph_matrix (w->desired_matrix);
11799 last_line_misfit = 1;
11800 }
ddf6b9a3 11801 /* Drop through and scroll. */
ef3c2c73
RS
11802 else
11803 goto done;
aa6d10fa 11804 }
a2889657 11805 else
5f5c8ee5 11806 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
11807 }
11808
5f5c8ee5
GM
11809 try_to_scroll:
11810
ac90c44f
GM
11811 w->last_modified = make_number (0);
11812 w->last_overlay_modified = make_number (0);
5f5c8ee5 11813
e481f960
RS
11814 /* Redisplay the mode line. Select the buffer properly for that. */
11815 if (!update_mode_line)
11816 {
e481f960
RS
11817 update_mode_line = 1;
11818 w->update_mode_line = Qt;
11819 }
a2889657 11820
5f5c8ee5
GM
11821 /* Try to scroll by specified few lines. */
11822 if ((scroll_conservatively
11823 || scroll_step
11824 || temp_scroll_step
11825 || NUMBERP (current_buffer->scroll_up_aggressively)
11826 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 11827 && !current_buffer->clip_changed
2311178e 11828 && CHARPOS (startp) >= BEGV
5f5c8ee5 11829 && CHARPOS (startp) <= ZV)
0789adb2 11830 {
5f5c8ee5
GM
11831 /* The function returns -1 if new fonts were loaded, 1 if
11832 successful, 0 if not successful. */
11833 int rc = try_scrolling (window, just_this_one_p,
11834 scroll_conservatively,
11835 scroll_step,
03b0a4b4 11836 temp_scroll_step, last_line_misfit);
cb617e7c
GM
11837 switch (rc)
11838 {
11839 case SCROLLING_SUCCESS:
11840 goto done;
2311178e 11841
cb617e7c
GM
11842 case SCROLLING_NEED_LARGER_MATRICES:
11843 goto need_larger_matrices;
2311178e 11844
cb617e7c
GM
11845 case SCROLLING_FAILED:
11846 break;
2311178e 11847
cb617e7c
GM
11848 default:
11849 abort ();
11850 }
5f5c8ee5 11851 }
f9c8af06 11852
5f5c8ee5 11853 /* Finally, just choose place to start which centers point */
5936754e 11854
5f5c8ee5 11855 recenter:
ddf6b9a3
RS
11856 centering_position = window_box_height (w) / 2;
11857
11858 point_at_top:
11859 /* Jump here with centering_position already set to 0. */
44173109 11860
5f5c8ee5
GM
11861#if GLYPH_DEBUG
11862 debug_method_add (w, "recenter");
11863#endif
0789adb2 11864
5f5c8ee5 11865 /* w->vscroll = 0; */
0789adb2 11866
5f5c8ee5 11867 /* Forget any previously recorded base line for line number display. */
c62c1bb5 11868 if (!buffer_unchanged_p)
5f5c8ee5
GM
11869 w->base_line_number = Qnil;
11870
11871 /* Move backward half the height of the window. */
11872 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11873 it.current_y = it.last_visible_y;
ddf6b9a3 11874 move_it_vertically_backward (&it, centering_position);
5f5c8ee5
GM
11875 xassert (IT_CHARPOS (it) >= BEGV);
11876
11877 /* The function move_it_vertically_backward may move over more
11878 than the specified y-distance. If it->w is small, e.g. a
11879 mini-buffer window, we may end up in front of the window's
11880 display area. Start displaying at the start of the line
11881 containing PT in this case. */
11882 if (it.current_y <= 0)
11883 {
11884 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11885 move_it_vertically (&it, 0);
11886 xassert (IT_CHARPOS (it) <= PT);
11887 it.current_y = 0;
0789adb2
RS
11888 }
11889
5f5c8ee5 11890 it.current_x = it.hpos = 0;
2311178e 11891
5f5c8ee5
GM
11892 /* Set startp here explicitly in case that helps avoid an infinite loop
11893 in case the window-scroll-functions functions get errors. */
11894 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11895
11896 /* Run scroll hooks. */
11897 startp = run_window_scroll_functions (window, it.current.pos);
11898
11899 /* Redisplay the window. */
11900 if (!current_matrix_up_to_date_p
11901 || windows_or_buffers_changed
5fb96e96 11902 || cursor_type_changed
5f5c8ee5
GM
11903 /* Don't use try_window_reusing_current_matrix in this case
11904 because it can have changed the buffer. */
11905 || !NILP (Vwindow_scroll_functions)
11906 || !just_this_one_p
11907 || MINI_WINDOW_P (w)
11908 || !try_window_reusing_current_matrix (w))
11909 try_window (window, startp);
11910
11911 /* If new fonts have been loaded (due to fontsets), give up. We
11912 have to start a new redisplay since we need to re-adjust glyph
11913 matrices. */
11914 if (fonts_changed_p)
cb617e7c 11915 goto need_larger_matrices;
5f5c8ee5
GM
11916
11917 /* If cursor did not appear assume that the middle of the window is
11918 in the first line of the window. Do it again with the next line.
11919 (Imagine a window of height 100, displaying two lines of height
11920 60. Moving back 50 from it->last_visible_y will end in the first
11921 line.) */
11922 if (w->cursor.vpos < 0)
a2889657 11923 {
5f5c8ee5
GM
11924 if (!NILP (w->window_end_valid)
11925 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 11926 {
5f5c8ee5
GM
11927 clear_glyph_matrix (w->desired_matrix);
11928 move_it_by_lines (&it, 1, 0);
11929 try_window (window, it.current.pos);
a2889657 11930 }
5f5c8ee5 11931 else if (PT < IT_CHARPOS (it))
a2889657 11932 {
5f5c8ee5
GM
11933 clear_glyph_matrix (w->desired_matrix);
11934 move_it_by_lines (&it, -1, 0);
11935 try_window (window, it.current.pos);
11936 }
11937 else
11938 {
11939 /* Not much we can do about it. */
a2889657 11940 }
a2889657 11941 }
010494d0 11942
5f5c8ee5
GM
11943 /* Consider the following case: Window starts at BEGV, there is
11944 invisible, intangible text at BEGV, so that display starts at
11945 some point START > BEGV. It can happen that we are called with
11946 PT somewhere between BEGV and START. Try to handle that case. */
11947 if (w->cursor.vpos < 0)
835766b6 11948 {
5f5c8ee5
GM
11949 struct glyph_row *row = w->current_matrix->rows;
11950 if (row->mode_line_p)
11951 ++row;
11952 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 11953 }
2311178e 11954
cb617e7c 11955 if (!make_cursor_line_fully_visible (w))
ddf6b9a3 11956 {
932357b4
KS
11957 /* If vscroll is enabled, disable it and try again. */
11958 if (w->vscroll)
11959 {
11960 w->vscroll = 0;
11961 clear_glyph_matrix (w->desired_matrix);
11962 goto recenter;
11963 }
2a6d0874 11964
ddf6b9a3
RS
11965 /* If centering point failed to make the whole line visible,
11966 put point at the top instead. That has to make the whole line
11967 visible, if it can be done. */
11968 centering_position = 0;
11969 goto point_at_top;
11970 }
b5174a51 11971
74d481ac
GM
11972 done:
11973
5f5c8ee5
GM
11974 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11975 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11976 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11977 ? Qt : Qnil);
a2889657 11978
5f5c8ee5 11979 /* Display the mode line, if we must. */
e481f960 11980 if ((update_mode_line
aa6d10fa 11981 /* If window not full width, must redo its mode line
2311178e 11982 if (a) the window to its side is being redone and
5f5c8ee5
GM
11983 (b) we do a frame-based redisplay. This is a consequence
11984 of how inverted lines are drawn in frame-based redisplay. */
2311178e 11985 || (!just_this_one_p
5f5c8ee5
GM
11986 && !FRAME_WINDOW_P (f)
11987 && !WINDOW_FULL_WIDTH_P (w))
11988 /* Line number to display. */
155ef550 11989 || INTEGERP (w->base_line_pos)
5f5c8ee5 11990 /* Column number is displayed and different from the one displayed. */
155ef550 11991 || (!NILP (w->column_number_displayed)
2311178e
TTN
11992 && (XFASTINT (w->column_number_displayed)
11993 != (int) current_column ()))) /* iftc */
5f5c8ee5
GM
11994 /* This means that the window has a mode line. */
11995 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 11996 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 11997 {
5f5c8ee5
GM
11998 display_mode_lines (w);
11999
12000 /* If mode line height has changed, arrange for a thorough
12001 immediate redisplay using the correct mode line height. */
12002 if (WINDOW_WANTS_MODELINE_P (w)
12003 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 12004 {
5f5c8ee5
GM
12005 fonts_changed_p = 1;
12006 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12007 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 12008 }
2311178e 12009
5f5c8ee5
GM
12010 /* If top line height has changed, arrange for a thorough
12011 immediate redisplay using the correct mode line height. */
045dee35
GM
12012 if (WINDOW_WANTS_HEADER_LINE_P (w)
12013 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
12014 {
12015 fonts_changed_p = 1;
045dee35
GM
12016 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12017 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
12018 }
12019
12020 if (fonts_changed_p)
cb617e7c 12021 goto need_larger_matrices;
5ba50c51 12022 }
5f5c8ee5
GM
12023
12024 if (!line_number_displayed
12025 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
12026 {
12027 w->base_line_pos = Qnil;
12028 w->base_line_number = Qnil;
12029 }
a2889657 12030
5f5c8ee5 12031 finish_menu_bars:
2311178e 12032
7ce2c095 12033 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 12034 if (update_mode_line
5f5c8ee5
GM
12035 && EQ (FRAME_SELECTED_WINDOW (f), window))
12036 {
12037 int redisplay_menu_p = 0;
488dd4c4 12038 int redisplay_tool_bar_p = 0;
5f5c8ee5
GM
12039
12040 if (FRAME_WINDOW_P (f))
12041 {
488dd4c4
JD
12042#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12043 || defined (USE_GTK)
5f5c8ee5 12044 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 12045#else
5f5c8ee5 12046 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 12047#endif
5f5c8ee5
GM
12048 }
12049 else
12050 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12051
12052 if (redisplay_menu_p)
12053 display_menu_bar (w);
12054
12055#ifdef HAVE_WINDOW_SYSTEM
488dd4c4
JD
12056#ifdef USE_GTK
12057 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12058#else
12059 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12060 && (FRAME_TOOL_BAR_LINES (f) > 0
12061 || auto_resize_tool_bars_p);
177c0ea7 12062
488dd4c4
JD
12063#endif
12064
12065 if (redisplay_tool_bar_p)
12066 redisplay_tool_bar (f);
5f5c8ee5
GM
12067#endif
12068 }
7ce2c095 12069
b60c9653
RS
12070 /* We go to this label, with fonts_changed_p nonzero,
12071 if it is necessary to try again using larger glyph matrices.
12072 We have to redeem the scroll bar even in this case,
12073 because the loop in redisplay_internal expects that. */
cb617e7c
GM
12074 need_larger_matrices:
12075 ;
88f22aff 12076 finish_scroll_bars:
5f5c8ee5 12077
da8b7f4f 12078 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
30c566e4 12079 {
9a08d928
SM
12080 /* Set the thumb's position and size. */
12081 set_vertical_scroll_bar (w);
30c566e4 12082
5f5c8ee5
GM
12083 /* Note that we actually used the scroll bar attached to this
12084 window, so it shouldn't be deleted at the end of redisplay. */
504454e8 12085 redeem_scroll_bar_hook (w);
30c566e4 12086 }
b1d1124b 12087
5f5c8ee5
GM
12088 /* Restore current_buffer and value of point in it. */
12089 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
6a93695f 12090 set_buffer_internal_1 (old);
5f5c8ee5 12091 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
12092
12093 unbind_to (count, Qnil);
a2889657 12094}
a2889657 12095
5f5c8ee5
GM
12096
12097/* Build the complete desired matrix of WINDOW with a window start
12098 buffer position POS. Value is non-zero if successful. It is zero
12099 if fonts were loaded during redisplay which makes re-adjusting
12100 glyph matrices necessary. */
12101
12102int
a2889657
JB
12103try_window (window, pos)
12104 Lisp_Object window;
5f5c8ee5
GM
12105 struct text_pos pos;
12106{
12107 struct window *w = XWINDOW (window);
12108 struct it it;
12109 struct glyph_row *last_text_row = NULL;
9cbab4ff 12110
5f5c8ee5
GM
12111 /* Make POS the new window start. */
12112 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 12113
5f5c8ee5
GM
12114 /* Mark cursor position as unknown. No overlay arrow seen. */
12115 w->cursor.vpos = -1;
a2889657 12116 overlay_arrow_seen = 0;
642eefc6 12117
5f5c8ee5
GM
12118 /* Initialize iterator and info to start at POS. */
12119 start_display (&it, w, pos);
a2889657 12120
5f5c8ee5
GM
12121 /* Display all lines of W. */
12122 while (it.current_y < it.last_visible_y)
12123 {
12124 if (display_line (&it))
12125 last_text_row = it.glyph_row - 1;
12126 if (fonts_changed_p)
12127 return 0;
12128 }
a2889657 12129
5f5c8ee5
GM
12130 /* If bottom moved off end of frame, change mode line percentage. */
12131 if (XFASTINT (w->window_end_pos) <= 0
12132 && Z != IT_CHARPOS (it))
a2889657
JB
12133 w->update_mode_line = Qt;
12134
5f5c8ee5
GM
12135 /* Set window_end_pos to the offset of the last character displayed
12136 on the window from the end of current_buffer. Set
12137 window_end_vpos to its row number. */
12138 if (last_text_row)
12139 {
12140 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12141 w->window_end_bytepos
12142 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12143 w->window_end_pos
12144 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12145 w->window_end_vpos
12146 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
12147 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12148 ->displays_text_p);
12149 }
12150 else
12151 {
86bf3faa
RS
12152 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12153 w->window_end_pos = make_number (Z - ZV);
12154 w->window_end_vpos = make_number (0);
5f5c8ee5 12155 }
2311178e 12156
a2889657
JB
12157 /* But that is not valid info until redisplay finishes. */
12158 w->window_end_valid = Qnil;
5f5c8ee5 12159 return 1;
a2889657 12160}
5f5c8ee5
GM
12161
12162
a2889657 12163\f
5f5c8ee5
GM
12164/************************************************************************
12165 Window redisplay reusing current matrix when buffer has not changed
12166 ************************************************************************/
12167
12168/* Try redisplay of window W showing an unchanged buffer with a
12169 different window start than the last time it was displayed by
12170 reusing its current matrix. Value is non-zero if successful.
12171 W->start is the new window start. */
a2889657
JB
12172
12173static int
5f5c8ee5
GM
12174try_window_reusing_current_matrix (w)
12175 struct window *w;
a2889657 12176{
5f5c8ee5
GM
12177 struct frame *f = XFRAME (w->frame);
12178 struct glyph_row *row, *bottom_row;
12179 struct it it;
12180 struct run run;
12181 struct text_pos start, new_start;
12182 int nrows_scrolled, i;
12183 struct glyph_row *last_text_row;
12184 struct glyph_row *last_reused_text_row;
12185 struct glyph_row *start_row;
12186 int start_vpos, min_y, max_y;
75c5350a 12187
69d1f7c9 12188#if GLYPH_DEBUG
76cb5e06
GM
12189 if (inhibit_try_window_reusing)
12190 return 0;
12191#endif
12192
d18354b6
GM
12193 if (/* This function doesn't handle terminal frames. */
12194 !FRAME_WINDOW_P (f)
12195 /* Don't try to reuse the display if windows have been split
12196 or such. */
5fb96e96
RS
12197 || windows_or_buffers_changed
12198 || cursor_type_changed)
5f5c8ee5 12199 return 0;
a2889657 12200
5f5c8ee5
GM
12201 /* Can't do this if region may have changed. */
12202 if ((!NILP (Vtransient_mark_mode)
12203 && !NILP (current_buffer->mark_active))
8f897821
GM
12204 || !NILP (w->region_showing)
12205 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 12206 return 0;
a2889657 12207
5f5c8ee5 12208 /* If top-line visibility has changed, give up. */
045dee35
GM
12209 if (WINDOW_WANTS_HEADER_LINE_P (w)
12210 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
12211 return 0;
12212
12213 /* Give up if old or new display is scrolled vertically. We could
12214 make this function handle this, but right now it doesn't. */
12215 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12216 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12217 return 0;
12218
12219 /* The variable new_start now holds the new window start. The old
12220 start `start' can be determined from the current matrix. */
12221 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12222 start = start_row->start.pos;
12223 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 12224
5f5c8ee5
GM
12225 /* Clear the desired matrix for the display below. */
12226 clear_glyph_matrix (w->desired_matrix);
2311178e 12227
5f5c8ee5
GM
12228 if (CHARPOS (new_start) <= CHARPOS (start))
12229 {
12230 int first_row_y;
2311178e 12231
607ec83c
GM
12232 /* Don't use this method if the display starts with an ellipsis
12233 displayed for invisible text. It's not easy to handle that case
12234 below, and it's certainly not worth the effort since this is
12235 not a frequent case. */
12236 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12237 return 0;
12238
5f5c8ee5 12239 IF_DEBUG (debug_method_add (w, "twu1"));
2311178e 12240
5f5c8ee5
GM
12241 /* Display up to a row that can be reused. The variable
12242 last_text_row is set to the last row displayed that displays
b48f74cb
GM
12243 text. Note that it.vpos == 0 if or if not there is a
12244 header-line; it's not the same as the MATRIX_ROW_VPOS! */
5f5c8ee5
GM
12245 start_display (&it, w, new_start);
12246 first_row_y = it.current_y;
12247 w->cursor.vpos = -1;
12248 last_text_row = last_reused_text_row = NULL;
2311178e 12249
5f5c8ee5
GM
12250 while (it.current_y < it.last_visible_y
12251 && IT_CHARPOS (it) < CHARPOS (start)
12252 && !fonts_changed_p)
12253 if (display_line (&it))
12254 last_text_row = it.glyph_row - 1;
12255
12256 /* A value of current_y < last_visible_y means that we stopped
12257 at the previous window start, which in turn means that we
12258 have at least one reusable row. */
12259 if (it.current_y < it.last_visible_y)
a2889657 12260 {
b48f74cb 12261 /* IT.vpos always starts from 0; it counts text lines. */
5f5c8ee5 12262 nrows_scrolled = it.vpos;
2311178e 12263
5f5c8ee5
GM
12264 /* Find PT if not already found in the lines displayed. */
12265 if (w->cursor.vpos < 0)
a2889657 12266 {
5f5c8ee5 12267 int dy = it.current_y - first_row_y;
2311178e 12268
5f5c8ee5 12269 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
0df56f4d
GM
12270 row = row_containing_pos (w, PT, row, NULL, dy);
12271 if (row)
12272 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12273 dy, nrows_scrolled);
12274 else
5f5c8ee5
GM
12275 {
12276 clear_glyph_matrix (w->desired_matrix);
12277 return 0;
12278 }
a2889657 12279 }
5f5c8ee5
GM
12280
12281 /* Scroll the display. Do it before the current matrix is
12282 changed. The problem here is that update has not yet
12283 run, i.e. part of the current matrix is not up to date.
12284 scroll_run_hook will clear the cursor, and use the
12285 current matrix to get the height of the row the cursor is
12286 in. */
12287 run.current_y = first_row_y;
12288 run.desired_y = it.current_y;
12289 run.height = it.last_visible_y - it.current_y;
b48f74cb
GM
12290
12291 if (run.height > 0 && run.current_y != run.desired_y)
a2889657 12292 {
5f5c8ee5
GM
12293 update_begin (f);
12294 rif->update_window_begin_hook (w);
fa3c6b4d 12295 rif->clear_window_mouse_face (w);
5f5c8ee5 12296 rif->scroll_run_hook (w, &run);
64d1e7d3 12297 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5 12298 update_end (f);
a2889657 12299 }
5f5c8ee5
GM
12300
12301 /* Shift current matrix down by nrows_scrolled lines. */
12302 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12303 rotate_matrix (w->current_matrix,
12304 start_vpos,
12305 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12306 nrows_scrolled);
2311178e 12307
9c8b8382 12308 /* Disable lines that must be updated. */
5f5c8ee5 12309 for (i = 0; i < it.vpos; ++i)
b48f74cb 12310 (start_row + i)->enabled_p = 0;
9c8b8382 12311
5f5c8ee5 12312 /* Re-compute Y positions. */
da8b7f4f 12313 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
5f5c8ee5 12314 max_y = it.last_visible_y;
b48f74cb
GM
12315 for (row = start_row + nrows_scrolled;
12316 row < bottom_row;
12317 ++row)
d2f84654 12318 {
5f5c8ee5 12319 row->y = it.current_y;
75c5350a 12320 row->visible_height = row->height;
5f5c8ee5
GM
12321
12322 if (row->y < min_y)
75c5350a
GM
12323 row->visible_height -= min_y - row->y;
12324 if (row->y + row->height > max_y)
12325 row->visible_height -= row->y + row->height - max_y;
2311178e 12326
5f5c8ee5 12327 it.current_y += row->height;
5f5c8ee5
GM
12328
12329 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12330 last_reused_text_row = row;
12331 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12332 break;
d2f84654 12333 }
2311178e 12334
9c8b8382
GM
12335 /* Disable lines in the current matrix which are now
12336 below the window. */
bafb434c 12337 for (++row; row < bottom_row; ++row)
9c8b8382 12338 row->enabled_p = 0;
a2889657 12339 }
5f5c8ee5
GM
12340
12341 /* Update window_end_pos etc.; last_reused_text_row is the last
12342 reused row from the current matrix containing text, if any.
12343 The value of last_text_row is the last displayed line
12344 containing text. */
12345 if (last_reused_text_row)
a2889657 12346 {
5f5c8ee5
GM
12347 w->window_end_bytepos
12348 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
ac90c44f
GM
12349 w->window_end_pos
12350 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12351 w->window_end_vpos
12352 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12353 w->current_matrix));
a2889657 12354 }
5f5c8ee5
GM
12355 else if (last_text_row)
12356 {
12357 w->window_end_bytepos
12358 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12359 w->window_end_pos
12360 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12361 w->window_end_vpos
12362 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
12363 }
12364 else
12365 {
12366 /* This window must be completely empty. */
86bf3faa
RS
12367 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12368 w->window_end_pos = make_number (Z - ZV);
12369 w->window_end_vpos = make_number (0);
5f5c8ee5
GM
12370 }
12371 w->window_end_valid = Qnil;
a2889657 12372
5f5c8ee5
GM
12373 /* Update hint: don't try scrolling again in update_window. */
12374 w->desired_matrix->no_scrolling_p = 1;
2311178e 12375
5f5c8ee5
GM
12376#if GLYPH_DEBUG
12377 debug_method_add (w, "try_window_reusing_current_matrix 1");
12378#endif
12379 return 1;
a2889657 12380 }
5f5c8ee5
GM
12381 else if (CHARPOS (new_start) > CHARPOS (start))
12382 {
12383 struct glyph_row *pt_row, *row;
12384 struct glyph_row *first_reusable_row;
12385 struct glyph_row *first_row_to_display;
12386 int dy;
12387 int yb = window_text_bottom_y (w);
12388
5f5c8ee5
GM
12389 /* Find the row starting at new_start, if there is one. Don't
12390 reuse a partially visible line at the end. */
b48f74cb 12391 first_reusable_row = start_row;
5f5c8ee5
GM
12392 while (first_reusable_row->enabled_p
12393 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12394 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12395 < CHARPOS (new_start)))
12396 ++first_reusable_row;
12397
12398 /* Give up if there is no row to reuse. */
12399 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
12400 || !first_reusable_row->enabled_p
12401 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12402 != CHARPOS (new_start)))
5f5c8ee5
GM
12403 return 0;
12404
5f5c8ee5
GM
12405 /* We can reuse fully visible rows beginning with
12406 first_reusable_row to the end of the window. Set
12407 first_row_to_display to the first row that cannot be reused.
12408 Set pt_row to the row containing point, if there is any. */
5f5c8ee5 12409 pt_row = NULL;
ac90c44f
GM
12410 for (first_row_to_display = first_reusable_row;
12411 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12412 ++first_row_to_display)
5f5c8ee5 12413 {
4bde0ebb
GM
12414 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12415 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
5f5c8ee5 12416 pt_row = first_row_to_display;
5f5c8ee5 12417 }
a2889657 12418
5f5c8ee5
GM
12419 /* Start displaying at the start of first_row_to_display. */
12420 xassert (first_row_to_display->y < yb);
12421 init_to_row_start (&it, w, first_row_to_display);
607ec83c 12422
b48f74cb
GM
12423 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12424 - start_vpos);
5f5c8ee5
GM
12425 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12426 - nrows_scrolled);
b48f74cb 12427 it.current_y = (first_row_to_display->y - first_reusable_row->y
da8b7f4f 12428 + WINDOW_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
12429
12430 /* Display lines beginning with first_row_to_display in the
12431 desired matrix. Set last_text_row to the last row displayed
12432 that displays text. */
12433 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12434 if (pt_row == NULL)
12435 w->cursor.vpos = -1;
12436 last_text_row = NULL;
12437 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12438 if (display_line (&it))
12439 last_text_row = it.glyph_row - 1;
12440
12441 /* Give up If point isn't in a row displayed or reused. */
12442 if (w->cursor.vpos < 0)
12443 {
12444 clear_glyph_matrix (w->desired_matrix);
12445 return 0;
12446 }
12adba34 12447
5f5c8ee5
GM
12448 /* If point is in a reused row, adjust y and vpos of the cursor
12449 position. */
12450 if (pt_row)
12451 {
12452 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12453 w->current_matrix);
12454 w->cursor.y -= first_reusable_row->y;
a2889657
JB
12455 }
12456
5f5c8ee5
GM
12457 /* Scroll the display. */
12458 run.current_y = first_reusable_row->y;
da8b7f4f 12459 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
5f5c8ee5 12460 run.height = it.last_visible_y - run.current_y;
4da61803 12461 dy = run.current_y - run.desired_y;
2311178e 12462
5f5c8ee5
GM
12463 if (run.height)
12464 {
12465 struct frame *f = XFRAME (WINDOW_FRAME (w));
12466 update_begin (f);
12467 rif->update_window_begin_hook (w);
fa3c6b4d 12468 rif->clear_window_mouse_face (w);
5f5c8ee5 12469 rif->scroll_run_hook (w, &run);
64d1e7d3 12470 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
12471 update_end (f);
12472 }
a2889657 12473
5f5c8ee5
GM
12474 /* Adjust Y positions of reused rows. */
12475 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
da8b7f4f 12476 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
5f5c8ee5 12477 max_y = it.last_visible_y;
b48f74cb 12478 for (row = first_reusable_row; row < first_row_to_display; ++row)
5f5c8ee5
GM
12479 {
12480 row->y -= dy;
75c5350a 12481 row->visible_height = row->height;
5f5c8ee5 12482 if (row->y < min_y)
75c5350a
GM
12483 row->visible_height -= min_y - row->y;
12484 if (row->y + row->height > max_y)
12485 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5 12486 }
a2889657 12487
5f5c8ee5
GM
12488 /* Scroll the current matrix. */
12489 xassert (nrows_scrolled > 0);
12490 rotate_matrix (w->current_matrix,
12491 start_vpos,
12492 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12493 -nrows_scrolled);
12494
ac90c44f
GM
12495 /* Disable rows not reused. */
12496 for (row -= nrows_scrolled; row < bottom_row; ++row)
12497 row->enabled_p = 0;
12498
5f5c8ee5
GM
12499 /* Adjust window end. A null value of last_text_row means that
12500 the window end is in reused rows which in turn means that
12501 only its vpos can have changed. */
12502 if (last_text_row)
12503 {
12504 w->window_end_bytepos
12505 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12506 w->window_end_pos
12507 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12508 w->window_end_vpos
12509 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
12510 }
12511 else
a2889657 12512 {
ac90c44f
GM
12513 w->window_end_vpos
12514 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 12515 }
2311178e 12516
5f5c8ee5
GM
12517 w->window_end_valid = Qnil;
12518 w->desired_matrix->no_scrolling_p = 1;
12519
12520#if GLYPH_DEBUG
12521 debug_method_add (w, "try_window_reusing_current_matrix 2");
12522#endif
12523 return 1;
a2889657 12524 }
2311178e 12525
5f5c8ee5
GM
12526 return 0;
12527}
a2889657 12528
a2889657 12529
5f5c8ee5
GM
12530\f
12531/************************************************************************
12532 Window redisplay reusing current matrix when buffer has changed
12533 ************************************************************************/
12534
1ec185cb
GM
12535static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12536static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
5f5c8ee5
GM
12537 int *, int *));
12538static struct glyph_row *
12539find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12540 struct glyph_row *));
12541
12542
12543/* Return the last row in MATRIX displaying text. If row START is
12544 non-null, start searching with that row. IT gives the dimensions
12545 of the display. Value is null if matrix is empty; otherwise it is
12546 a pointer to the row found. */
12547
12548static struct glyph_row *
12549find_last_row_displaying_text (matrix, it, start)
12550 struct glyph_matrix *matrix;
12551 struct it *it;
12552 struct glyph_row *start;
12553{
12554 struct glyph_row *row, *row_found;
12555
12556 /* Set row_found to the last row in IT->w's current matrix
12557 displaying text. The loop looks funny but think of partially
12558 visible lines. */
12559 row_found = NULL;
12560 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12561 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12562 {
12563 xassert (row->enabled_p);
12564 row_found = row;
12565 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12566 break;
12567 ++row;
a2889657 12568 }
2311178e 12569
5f5c8ee5
GM
12570 return row_found;
12571}
12572
a2889657 12573
5f5c8ee5 12574/* Return the last row in the current matrix of W that is not affected
d43fbe9d
GM
12575 by changes at the start of current_buffer that occurred since W's
12576 current matrix was built. Value is null if no such row exists.
a2889657 12577
d43fbe9d
GM
12578 BEG_UNCHANGED us the number of characters unchanged at the start of
12579 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12580 first changed character in current_buffer. Characters at positions <
12581 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12582 when the current matrix was built. */
5f5c8ee5
GM
12583
12584static struct glyph_row *
1ec185cb 12585find_last_unchanged_at_beg_row (w)
5f5c8ee5
GM
12586 struct window *w;
12587{
9142dd5b 12588 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
12589 struct glyph_row *row;
12590 struct glyph_row *row_found = NULL;
12591 int yb = window_text_bottom_y (w);
12592
12593 /* Find the last row displaying unchanged text. */
12594 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12595 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12596 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 12597 {
5f5c8ee5
GM
12598 if (/* If row ends before first_changed_pos, it is unchanged,
12599 except in some case. */
12600 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12601 /* When row ends in ZV and we write at ZV it is not
12602 unchanged. */
12603 && !row->ends_at_zv_p
12604 /* When first_changed_pos is the end of a continued line,
12605 row is not unchanged because it may be no longer
12606 continued. */
12607 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12608 && row->continued_p))
12609 row_found = row;
12610
12611 /* Stop if last visible row. */
12612 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12613 break;
2311178e 12614
5f5c8ee5 12615 ++row;
a2889657
JB
12616 }
12617
5f5c8ee5 12618 return row_found;
a2889657 12619}
5f5c8ee5
GM
12620
12621
12622/* Find the first glyph row in the current matrix of W that is not
2311178e 12623 affected by changes at the end of current_buffer since the
d43fbe9d
GM
12624 time W's current matrix was built.
12625
12626 Return in *DELTA the number of chars by which buffer positions in
12627 unchanged text at the end of current_buffer must be adjusted.
2311178e 12628
d43fbe9d
GM
12629 Return in *DELTA_BYTES the corresponding number of bytes.
12630
12631 Value is null if no such row exists, i.e. all rows are affected by
12632 changes. */
2311178e 12633
5f5c8ee5 12634static struct glyph_row *
1ec185cb 12635find_first_unchanged_at_end_row (w, delta, delta_bytes)
5f5c8ee5
GM
12636 struct window *w;
12637 int *delta, *delta_bytes;
a2889657 12638{
5f5c8ee5
GM
12639 struct glyph_row *row;
12640 struct glyph_row *row_found = NULL;
c581d710 12641
5f5c8ee5 12642 *delta = *delta_bytes = 0;
b2a76982 12643
1ec185cb
GM
12644 /* Display must not have been paused, otherwise the current matrix
12645 is not up to date. */
12646 if (NILP (w->window_end_valid))
12647 abort ();
2311178e 12648
1ec185cb 12649 /* A value of window_end_pos >= END_UNCHANGED means that the window
5f5c8ee5
GM
12650 end is in the range of changed text. If so, there is no
12651 unchanged row at the end of W's current matrix. */
9142dd5b 12652 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
12653 return NULL;
12654
12655 /* Set row to the last row in W's current matrix displaying text. */
12656 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
2311178e
TTN
12657
12658 /* If matrix is entirely empty, no unchanged row exists. */
5f5c8ee5
GM
12659 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12660 {
12661 /* The value of row is the last glyph row in the matrix having a
12662 meaningful buffer position in it. The end position of row
12663 corresponds to window_end_pos. This allows us to translate
12664 buffer positions in the current matrix to current buffer
12665 positions for characters not in changed text. */
12666 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12667 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12668 int last_unchanged_pos, last_unchanged_pos_old;
12669 struct glyph_row *first_text_row
12670 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12671
12672 *delta = Z - Z_old;
12673 *delta_bytes = Z_BYTE - Z_BYTE_old;
12674
12675 /* Set last_unchanged_pos to the buffer position of the last
12676 character in the buffer that has not been changed. Z is the
d43fbe9d
GM
12677 index + 1 of the last character in current_buffer, i.e. by
12678 subtracting END_UNCHANGED we get the index of the last
5f5c8ee5
GM
12679 unchanged character, and we have to add BEG to get its buffer
12680 position. */
9142dd5b 12681 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5 12682 last_unchanged_pos_old = last_unchanged_pos - *delta;
2311178e 12683
5f5c8ee5
GM
12684 /* Search backward from ROW for a row displaying a line that
12685 starts at a minimum position >= last_unchanged_pos_old. */
1ec185cb 12686 for (; row > first_text_row; --row)
5f5c8ee5 12687 {
1ec185cb
GM
12688 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12689 abort ();
2311178e 12690
5f5c8ee5
GM
12691 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12692 row_found = row;
5f5c8ee5
GM
12693 }
12694 }
12695
1ec185cb
GM
12696 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12697 abort ();
2311178e 12698
5f5c8ee5 12699 return row_found;
c581d710
RS
12700}
12701
c581d710 12702
5f5c8ee5
GM
12703/* Make sure that glyph rows in the current matrix of window W
12704 reference the same glyph memory as corresponding rows in the
12705 frame's frame matrix. This function is called after scrolling W's
12706 current matrix on a terminal frame in try_window_id and
12707 try_window_reusing_current_matrix. */
12708
12709static void
12710sync_frame_with_window_matrix_rows (w)
12711 struct window *w;
c581d710 12712{
5f5c8ee5
GM
12713 struct frame *f = XFRAME (w->frame);
12714 struct glyph_row *window_row, *window_row_end, *frame_row;
12715
12716 /* Preconditions: W must be a leaf window and full-width. Its frame
12717 must have a frame matrix. */
12718 xassert (NILP (w->hchild) && NILP (w->vchild));
12719 xassert (WINDOW_FULL_WIDTH_P (w));
12720 xassert (!FRAME_WINDOW_P (f));
12721
12722 /* If W is a full-width window, glyph pointers in W's current matrix
12723 have, by definition, to be the same as glyph pointers in the
7d4cc828
GM
12724 corresponding frame matrix. Note that frame matrices have no
12725 marginal areas (see build_frame_matrix). */
5f5c8ee5
GM
12726 window_row = w->current_matrix->rows;
12727 window_row_end = window_row + w->current_matrix->nrows;
da8b7f4f 12728 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
5f5c8ee5 12729 while (window_row < window_row_end)
659a218f 12730 {
7d4cc828
GM
12731 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12732 struct glyph *end = window_row->glyphs[LAST_AREA];
12733
12734 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12735 frame_row->glyphs[TEXT_AREA] = start;
12736 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12737 frame_row->glyphs[LAST_AREA] = end;
f002db93
GM
12738
12739 /* Disable frame rows whose corresponding window rows have
12740 been disabled in try_window_id. */
12741 if (!window_row->enabled_p)
12742 frame_row->enabled_p = 0;
2311178e 12743
5f5c8ee5 12744 ++window_row, ++frame_row;
659a218f 12745 }
a2889657 12746}
5f5c8ee5
GM
12747
12748
e037b9ec
GM
12749/* Find the glyph row in window W containing CHARPOS. Consider all
12750 rows between START and END (not inclusive). END null means search
12751 all rows to the end of the display area of W. Value is the row
12752 containing CHARPOS or null. */
12753
0bef35bc 12754struct glyph_row *
0df56f4d 12755row_containing_pos (w, charpos, start, end, dy)
e037b9ec
GM
12756 struct window *w;
12757 int charpos;
12758 struct glyph_row *start, *end;
0df56f4d 12759 int dy;
e037b9ec
GM
12760{
12761 struct glyph_row *row = start;
12762 int last_y;
12763
12764 /* If we happen to start on a header-line, skip that. */
12765 if (row->mode_line_p)
12766 ++row;
2311178e 12767
e037b9ec
GM
12768 if ((end && row >= end) || !row->enabled_p)
12769 return NULL;
2311178e 12770
0df56f4d 12771 last_y = window_text_bottom_y (w) - dy;
2311178e 12772
5cce13dd
RS
12773 while (1)
12774 {
12775 /* Give up if we have gone too far. */
12776 if (end && row >= end)
12777 return NULL;
40a301b3
RS
12778 /* This formerly returned if they were equal.
12779 I think that both quantities are of a "last plus one" type;
12780 if so, when they are equal, the row is within the screen. -- rms. */
12781 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
5cce13dd
RS
12782 return NULL;
12783
12784 /* If it is in this row, return this row. */
12785 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
e037b9ec 12786 || (MATRIX_ROW_END_CHARPOS (row) == charpos
0df56f4d
GM
12787 /* The end position of a row equals the start
12788 position of the next row. If CHARPOS is there, we
12789 would rather display it in the next line, except
12790 when this line ends in ZV. */
12791 && !row->ends_at_zv_p
5cce13dd
RS
12792 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12793 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12794 return row;
12795 ++row;
12796 }
e037b9ec
GM
12797}
12798
12799
5f5c8ee5
GM
12800/* Try to redisplay window W by reusing its existing display. W's
12801 current matrix must be up to date when this function is called,
12802 i.e. window_end_valid must not be nil.
12803
12804 Value is
12805
12806 1 if display has been updated
12807 0 if otherwise unsuccessful
12808 -1 if redisplay with same window start is known not to succeed
12809
12810 The following steps are performed:
12811
12812 1. Find the last row in the current matrix of W that is not
12813 affected by changes at the start of current_buffer. If no such row
12814 is found, give up.
12815
12816 2. Find the first row in W's current matrix that is not affected by
12817 changes at the end of current_buffer. Maybe there is no such row.
12818
12819 3. Display lines beginning with the row + 1 found in step 1 to the
12820 row found in step 2 or, if step 2 didn't find a row, to the end of
12821 the window.
12822
12823 4. If cursor is not known to appear on the window, give up.
12824
12825 5. If display stopped at the row found in step 2, scroll the
12826 display and current matrix as needed.
12827
12828 6. Maybe display some lines at the end of W, if we must. This can
12829 happen under various circumstances, like a partially visible line
12830 becoming fully visible, or because newly displayed lines are displayed
12831 in smaller font sizes.
12832
12833 7. Update W's window end information. */
12834
12adba34 12835static int
5f5c8ee5 12836try_window_id (w)
12adba34 12837 struct window *w;
12adba34 12838{
5f5c8ee5
GM
12839 struct frame *f = XFRAME (w->frame);
12840 struct glyph_matrix *current_matrix = w->current_matrix;
12841 struct glyph_matrix *desired_matrix = w->desired_matrix;
12842 struct glyph_row *last_unchanged_at_beg_row;
12843 struct glyph_row *first_unchanged_at_end_row;
12844 struct glyph_row *row;
12845 struct glyph_row *bottom_row;
12846 int bottom_vpos;
12847 struct it it;
12848 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12849 struct text_pos start_pos;
12850 struct run run;
12851 int first_unchanged_at_end_vpos = 0;
12852 struct glyph_row *last_text_row, *last_text_row_at_end;
12853 struct text_pos start;
27d16f05 12854 int first_changed_charpos, last_changed_charpos;
5f5c8ee5 12855
69d1f7c9 12856#if GLYPH_DEBUG
76cb5e06
GM
12857 if (inhibit_try_window_id)
12858 return 0;
12859#endif
12860
27d16f05
GM
12861 /* This is handy for debugging. */
12862#if 0
12863#define GIVE_UP(X) \
12864 do { \
12865 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12866 return 0; \
12867 } while (0)
12868#else
62397849 12869#define GIVE_UP(X) return 0
27d16f05 12870#endif
2311178e 12871
5f5c8ee5
GM
12872 SET_TEXT_POS_FROM_MARKER (start, w->start);
12873
27d16f05
GM
12874 /* Don't use this for mini-windows because these can show
12875 messages and mini-buffers, and we don't handle that here. */
12876 if (MINI_WINDOW_P (w))
12877 GIVE_UP (1);
2311178e 12878
27d16f05 12879 /* This flag is used to prevent redisplay optimizations. */
5fb96e96 12880 if (windows_or_buffers_changed || cursor_type_changed)
27d16f05 12881 GIVE_UP (2);
2311178e 12882
c62c1bb5
RS
12883 /* Verify that narrowing has not changed.
12884 Also verify that we were not told to prevent redisplay optimizations.
12885 It would be nice to further
27d16f05 12886 reduce the number of cases where this prevents try_window_id. */
c62c1bb5
RS
12887 if (current_buffer->clip_changed
12888 || current_buffer->prevent_redisplay_optimizations_p)
27d16f05
GM
12889 GIVE_UP (3);
12890
12891 /* Window must either use window-based redisplay or be full width. */
12892 if (!FRAME_WINDOW_P (f)
28d440ab 12893 && (!TTY_LINE_INS_DEL_OK (CURTTY ())
27d16f05
GM
12894 || !WINDOW_FULL_WIDTH_P (w)))
12895 GIVE_UP (4);
5f5c8ee5 12896
f5376658 12897 /* Give up if point is not known NOT to appear in W. */
27d16f05
GM
12898 if (PT < CHARPOS (start))
12899 GIVE_UP (5);
12900
12901 /* Another way to prevent redisplay optimizations. */
12902 if (XFASTINT (w->last_modified) == 0)
12903 GIVE_UP (6);
2311178e 12904
f5376658 12905 /* Verify that window is not hscrolled. */
27d16f05
GM
12906 if (XFASTINT (w->hscroll) != 0)
12907 GIVE_UP (7);
2311178e 12908
f5376658 12909 /* Verify that display wasn't paused. */
27d16f05
GM
12910 if (NILP (w->window_end_valid))
12911 GIVE_UP (8);
2311178e 12912
27d16f05
GM
12913 /* Can't use this if highlighting a region because a cursor movement
12914 will do more than just set the cursor. */
12915 if (!NILP (Vtransient_mark_mode)
12916 && !NILP (current_buffer->mark_active))
12917 GIVE_UP (9);
12918
12919 /* Likewise if highlighting trailing whitespace. */
12920 if (!NILP (Vshow_trailing_whitespace))
12921 GIVE_UP (11);
2311178e 12922
27d16f05
GM
12923 /* Likewise if showing a region. */
12924 if (!NILP (w->region_showing))
12925 GIVE_UP (10);
2311178e 12926
27d16f05
GM
12927 /* Can use this if overlay arrow position and or string have changed. */
12928 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
12929 || !EQ (last_arrow_string, Voverlay_arrow_string))
12930 GIVE_UP (12);
12931
2311178e 12932
5f5c8ee5
GM
12933 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12934 only if buffer has really changed. The reason is that the gap is
12935 initially at Z for freshly visited files. The code below would
12936 set end_unchanged to 0 in that case. */
28ee91c0
GM
12937 if (MODIFF > SAVE_MODIFF
12938 /* This seems to happen sometimes after saving a buffer. */
12939 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
5f5c8ee5 12940 {
9142dd5b
GM
12941 if (GPT - BEG < BEG_UNCHANGED)
12942 BEG_UNCHANGED = GPT - BEG;
12943 if (Z - GPT < END_UNCHANGED)
12944 END_UNCHANGED = Z - GPT;
5f5c8ee5 12945 }
bf9249e3 12946
27d16f05
GM
12947 /* The position of the first and last character that has been changed. */
12948 first_changed_charpos = BEG + BEG_UNCHANGED;
12949 last_changed_charpos = Z - END_UNCHANGED;
12950
5f5c8ee5
GM
12951 /* If window starts after a line end, and the last change is in
12952 front of that newline, then changes don't affect the display.
f2d86d7a
GM
12953 This case happens with stealth-fontification. Note that although
12954 the display is unchanged, glyph positions in the matrix have to
12955 be adjusted, of course. */
5f5c8ee5 12956 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27d16f05 12957 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
e60f4527 12958 && ((last_changed_charpos < CHARPOS (start)
27d16f05 12959 && CHARPOS (start) == BEGV)
e60f4527 12960 || (last_changed_charpos < CHARPOS (start) - 1
27d16f05
GM
12961 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12962 {
12963 int Z_old, delta, Z_BYTE_old, delta_bytes;
12964 struct glyph_row *r0;
12965
12966 /* Compute how many chars/bytes have been added to or removed
12967 from the buffer. */
12968 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12969 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12970 delta = Z - Z_old;
12971 delta_bytes = Z_BYTE - Z_BYTE_old;
2311178e 12972
27d16f05
GM
12973 /* Give up if PT is not in the window. Note that it already has
12974 been checked at the start of try_window_id that PT is not in
12975 front of the window start. */
12976 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
12977 GIVE_UP (13);
12978
12979 /* If window start is unchanged, we can reuse the whole matrix
12980 as is, after adjusting glyph positions. No need to compute
12981 the window end again, since its offset from Z hasn't changed. */
12982 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12983 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
2d031b89
AS
12984 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
12985 /* PT must not be in a partially visible line. */
12986 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
12987 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
27d16f05
GM
12988 {
12989 /* Adjust positions in the glyph matrix. */
12990 if (delta || delta_bytes)
12991 {
12992 struct glyph_row *r1
12993 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12994 increment_matrix_positions (w->current_matrix,
12995 MATRIX_ROW_VPOS (r0, current_matrix),
12996 MATRIX_ROW_VPOS (r1, current_matrix),
12997 delta, delta_bytes);
12998 }
2311178e 12999
27d16f05 13000 /* Set the cursor. */
0df56f4d 13001 row = row_containing_pos (w, PT, r0, NULL, 0);
59adf8e8
KS
13002 if (row)
13003 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
40a301b3
RS
13004 else
13005 abort ();
27d16f05
GM
13006 return 1;
13007 }
5f5c8ee5
GM
13008 }
13009
27d16f05 13010 /* Handle the case that changes are all below what is displayed in
33ea6c4d 13011 the window, and that PT is in the window. This shortcut cannot
2b9c25e0
GM
13012 be taken if ZV is visible in the window, and text has been added
13013 there that is visible in the window. */
13014 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
33ea6c4d
GM
13015 /* ZV is not visible in the window, or there are no
13016 changes at ZV, actually. */
13017 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13018 || first_changed_charpos == last_changed_charpos))
5f5c8ee5 13019 {
27d16f05 13020 struct glyph_row *r0;
ef121659 13021
27d16f05
GM
13022 /* Give up if PT is not in the window. Note that it already has
13023 been checked at the start of try_window_id that PT is not in
13024 front of the window start. */
13025 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13026 GIVE_UP (14);
13027
13028 /* If window start is unchanged, we can reuse the whole matrix
13029 as is, without changing glyph positions since no text has
13030 been added/removed in front of the window end. */
13031 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
2d031b89
AS
13032 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13033 /* PT must not be in a partially visible line. */
13034 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13035 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
27d16f05
GM
13036 {
13037 /* We have to compute the window end anew since text
13038 can have been added/removed after it. */
13039 w->window_end_pos
13040 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13041 w->window_end_bytepos
13042 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13043
13044 /* Set the cursor. */
0df56f4d 13045 row = row_containing_pos (w, PT, r0, NULL, 0);
59adf8e8
KS
13046 if (row)
13047 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
40a301b3
RS
13048 else
13049 abort ();
27d16f05
GM
13050 return 2;
13051 }
5f5c8ee5
GM
13052 }
13053
2b9c25e0 13054 /* Give up if window start is in the changed area.
2311178e 13055
2b9c25e0
GM
13056 The condition used to read
13057
13058 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13059
13060 but why that was tested escapes me at the moment. */
13061 if (CHARPOS (start) >= first_changed_charpos
27d16f05
GM
13062 && CHARPOS (start) <= last_changed_charpos)
13063 GIVE_UP (15);
2311178e 13064
f5376658
RS
13065 /* Check that window start agrees with the start of the first glyph
13066 row in its current matrix. Check this after we know the window
13067 start is not in changed text, otherwise positions would not be
13068 comparable. */
27d16f05 13069 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
5f5c8ee5 13070 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
27d16f05 13071 GIVE_UP (16);
5f5c8ee5 13072
23e8bd86
GM
13073 /* Give up if the window ends in strings. Overlay strings
13074 at the end are difficult to handle, so don't try. */
13075 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13076 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13077 GIVE_UP (20);
13078
5f5c8ee5
GM
13079 /* Compute the position at which we have to start displaying new
13080 lines. Some of the lines at the top of the window might be
13081 reusable because they are not displaying changed text. Find the
13082 last row in W's current matrix not affected by changes at the
13083 start of current_buffer. Value is null if changes start in the
13084 first line of window. */
1ec185cb 13085 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
5f5c8ee5
GM
13086 if (last_unchanged_at_beg_row)
13087 {
67f1cf4c
GM
13088 /* Avoid starting to display in the moddle of a character, a TAB
13089 for instance. This is easier than to set up the iterator
13090 exactly, and it's not a frequent case, so the additional
13091 effort wouldn't really pay off. */
e74fb0f7
GM
13092 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13093 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
67f1cf4c
GM
13094 && last_unchanged_at_beg_row > w->current_matrix->rows)
13095 --last_unchanged_at_beg_row;
13096
13097 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
27d16f05 13098 GIVE_UP (17);
47d57b22
GM
13099
13100 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13101 GIVE_UP (18);
5f5c8ee5 13102 start_pos = it.current.pos;
2311178e 13103
5f5c8ee5
GM
13104 /* Start displaying new lines in the desired matrix at the same
13105 vpos we would use in the current matrix, i.e. below
13106 last_unchanged_at_beg_row. */
13107 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13108 current_matrix);
13109 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13110 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13111
13112 xassert (it.hpos == 0 && it.current_x == 0);
13113 }
13114 else
13115 {
13116 /* There are no reusable lines at the start of the window.
3ccb8bcf 13117 Start displaying in the first text line. */
5f5c8ee5 13118 start_display (&it, w, start);
3ccb8bcf 13119 it.vpos = it.first_vpos;
5f5c8ee5
GM
13120 start_pos = it.current.pos;
13121 }
13122
5f5c8ee5
GM
13123 /* Find the first row that is not affected by changes at the end of
13124 the buffer. Value will be null if there is no unchanged row, in
13125 which case we must redisplay to the end of the window. delta
13126 will be set to the value by which buffer positions beginning with
13127 first_unchanged_at_end_row have to be adjusted due to text
13128 changes. */
13129 first_unchanged_at_end_row
1ec185cb 13130 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
5f5c8ee5
GM
13131 IF_DEBUG (debug_delta = delta);
13132 IF_DEBUG (debug_delta_bytes = delta_bytes);
2311178e 13133
5f5c8ee5
GM
13134 /* Set stop_pos to the buffer position up to which we will have to
13135 display new lines. If first_unchanged_at_end_row != NULL, this
13136 is the buffer position of the start of the line displayed in that
13137 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13138 that we don't stop at a buffer position. */
13139 stop_pos = 0;
13140 if (first_unchanged_at_end_row)
13141 {
13142 xassert (last_unchanged_at_beg_row == NULL
13143 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
2311178e 13144
5f5c8ee5
GM
13145 /* If this is a continuation line, move forward to the next one
13146 that isn't. Changes in lines above affect this line.
13147 Caution: this may move first_unchanged_at_end_row to a row
13148 not displaying text. */
13149 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13150 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13151 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13152 < it.last_visible_y))
13153 ++first_unchanged_at_end_row;
13154
13155 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13156 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13157 >= it.last_visible_y))
13158 first_unchanged_at_end_row = NULL;
13159 else
13160 {
13161 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13162 + delta);
13163 first_unchanged_at_end_vpos
13164 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 13165 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
13166 }
13167 }
13168 else if (last_unchanged_at_beg_row == NULL)
23e8bd86 13169 GIVE_UP (19);
5f5c8ee5
GM
13170
13171
13172#if GLYPH_DEBUG
2311178e 13173
5f5c8ee5
GM
13174 /* Either there is no unchanged row at the end, or the one we have
13175 now displays text. This is a necessary condition for the window
13176 end pos calculation at the end of this function. */
13177 xassert (first_unchanged_at_end_row == NULL
13178 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
2311178e 13179
5f5c8ee5
GM
13180 debug_last_unchanged_at_beg_vpos
13181 = (last_unchanged_at_beg_row
13182 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13183 : -1);
13184 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
2311178e 13185
5f5c8ee5
GM
13186#endif /* GLYPH_DEBUG != 0 */
13187
2311178e 13188
5f5c8ee5
GM
13189 /* Display new lines. Set last_text_row to the last new line
13190 displayed which has text on it, i.e. might end up as being the
13191 line where the window_end_vpos is. */
13192 w->cursor.vpos = -1;
13193 last_text_row = NULL;
13194 overlay_arrow_seen = 0;
13195 while (it.current_y < it.last_visible_y
13196 && !fonts_changed_p
13197 && (first_unchanged_at_end_row == NULL
13198 || IT_CHARPOS (it) < stop_pos))
13199 {
13200 if (display_line (&it))
13201 last_text_row = it.glyph_row - 1;
13202 }
13203
13204 if (fonts_changed_p)
13205 return -1;
13206
13207
13208 /* Compute differences in buffer positions, y-positions etc. for
13209 lines reused at the bottom of the window. Compute what we can
13210 scroll. */
ca42b2e8
GM
13211 if (first_unchanged_at_end_row
13212 /* No lines reused because we displayed everything up to the
13213 bottom of the window. */
13214 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
13215 {
13216 dvpos = (it.vpos
13217 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13218 current_matrix));
13219 dy = it.current_y - first_unchanged_at_end_row->y;
13220 run.current_y = first_unchanged_at_end_row->y;
13221 run.desired_y = run.current_y + dy;
13222 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13223 }
13224 else
ca42b2e8
GM
13225 {
13226 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13227 first_unchanged_at_end_row = NULL;
13228 }
5f5c8ee5
GM
13229 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13230
8f8ba186 13231
5f5c8ee5
GM
13232 /* Find the cursor if not already found. We have to decide whether
13233 PT will appear on this window (it sometimes doesn't, but this is
13234 not a very frequent case.) This decision has to be made before
13235 the current matrix is altered. A value of cursor.vpos < 0 means
13236 that PT is either in one of the lines beginning at
13237 first_unchanged_at_end_row or below the window. Don't care for
13238 lines that might be displayed later at the window end; as
13239 mentioned, this is not a frequent case. */
13240 if (w->cursor.vpos < 0)
13241 {
5f5c8ee5
GM
13242 /* Cursor in unchanged rows at the top? */
13243 if (PT < CHARPOS (start_pos)
13244 && last_unchanged_at_beg_row)
13245 {
e037b9ec
GM
13246 row = row_containing_pos (w, PT,
13247 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
0df56f4d 13248 last_unchanged_at_beg_row + 1, 0);
bfe0ee88
GM
13249 if (row)
13250 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
13251 }
13252
13253 /* Start from first_unchanged_at_end_row looking for PT. */
13254 else if (first_unchanged_at_end_row)
13255 {
e037b9ec 13256 row = row_containing_pos (w, PT - delta,
0df56f4d 13257 first_unchanged_at_end_row, NULL, 0);
e037b9ec 13258 if (row)
468155d7
GM
13259 set_cursor_from_row (w, row, w->current_matrix, delta,
13260 delta_bytes, dy, dvpos);
5f5c8ee5
GM
13261 }
13262
13263 /* Give up if cursor was not found. */
13264 if (w->cursor.vpos < 0)
13265 {
13266 clear_glyph_matrix (w->desired_matrix);
13267 return -1;
13268 }
13269 }
2311178e 13270
5f5c8ee5
GM
13271 /* Don't let the cursor end in the scroll margins. */
13272 {
13273 int this_scroll_margin, cursor_height;
2311178e 13274
5f5c8ee5 13275 this_scroll_margin = max (0, scroll_margin);
da8b7f4f
KS
13276 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13277 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
5f5c8ee5 13278 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
2311178e 13279
5f5c8ee5
GM
13280 if ((w->cursor.y < this_scroll_margin
13281 && CHARPOS (start) > BEGV)
13282 /* Don't take scroll margin into account at the bottom because
13283 old redisplay didn't do it either. */
13284 || w->cursor.y + cursor_height > it.last_visible_y)
13285 {
13286 w->cursor.vpos = -1;
13287 clear_glyph_matrix (w->desired_matrix);
13288 return -1;
13289 }
13290 }
13291
13292 /* Scroll the display. Do it before changing the current matrix so
13293 that xterm.c doesn't get confused about where the cursor glyph is
13294 found. */
fa77249f 13295 if (dy && run.height)
5f5c8ee5
GM
13296 {
13297 update_begin (f);
2311178e 13298
5f5c8ee5
GM
13299 if (FRAME_WINDOW_P (f))
13300 {
13301 rif->update_window_begin_hook (w);
fa3c6b4d 13302 rif->clear_window_mouse_face (w);
5f5c8ee5 13303 rif->scroll_run_hook (w, &run);
64d1e7d3 13304 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
13305 }
13306 else
13307 {
13308 /* Terminal frame. In this case, dvpos gives the number of
13309 lines to scroll by; dvpos < 0 means scroll up. */
13310 int first_unchanged_at_end_vpos
13311 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
da8b7f4f
KS
13312 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13313 int end = (WINDOW_TOP_EDGE_LINE (w)
522ed7fb
GM
13314 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13315 + window_internal_height (w));
2311178e 13316
5f5c8ee5
GM
13317 /* Perform the operation on the screen. */
13318 if (dvpos > 0)
13319 {
13320 /* Scroll last_unchanged_at_beg_row to the end of the
13321 window down dvpos lines. */
13322 set_terminal_window (end);
13323
13324 /* On dumb terminals delete dvpos lines at the end
13325 before inserting dvpos empty lines. */
28d440ab 13326 if (!TTY_SCROLL_REGION_OK (CURTTY ()))
5f5c8ee5
GM
13327 ins_del_lines (end - dvpos, -dvpos);
13328
13329 /* Insert dvpos empty lines in front of
13330 last_unchanged_at_beg_row. */
13331 ins_del_lines (from, dvpos);
13332 }
13333 else if (dvpos < 0)
13334 {
13335 /* Scroll up last_unchanged_at_beg_vpos to the end of
13336 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13337 set_terminal_window (end);
13338
13339 /* Delete dvpos lines in front of
13340 last_unchanged_at_beg_vpos. ins_del_lines will set
13341 the cursor to the given vpos and emit |dvpos| delete
13342 line sequences. */
13343 ins_del_lines (from + dvpos, dvpos);
13344
13345 /* On a dumb terminal insert dvpos empty lines at the
13346 end. */
28d440ab 13347 if (!TTY_SCROLL_REGION_OK (CURTTY ()))
5f5c8ee5
GM
13348 ins_del_lines (end + dvpos, -dvpos);
13349 }
2311178e 13350
5f5c8ee5
GM
13351 set_terminal_window (0);
13352 }
2311178e 13353
5f5c8ee5
GM
13354 update_end (f);
13355 }
13356
ca42b2e8
GM
13357 /* Shift reused rows of the current matrix to the right position.
13358 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13359 text. */
13360 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13361 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
13362 if (dvpos < 0)
13363 {
13364 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13365 bottom_vpos, dvpos);
13366 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13367 bottom_vpos, 0);
13368 }
13369 else if (dvpos > 0)
13370 {
13371 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13372 bottom_vpos, dvpos);
13373 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13374 first_unchanged_at_end_vpos + dvpos, 0);
13375 }
13376
13377 /* For frame-based redisplay, make sure that current frame and window
13378 matrix are in sync with respect to glyph memory. */
13379 if (!FRAME_WINDOW_P (f))
13380 sync_frame_with_window_matrix_rows (w);
13381
13382 /* Adjust buffer positions in reused rows. */
13383 if (delta)
f2d86d7a
GM
13384 increment_matrix_positions (current_matrix,
13385 first_unchanged_at_end_vpos + dvpos,
13386 bottom_vpos, delta, delta_bytes);
5f5c8ee5
GM
13387
13388 /* Adjust Y positions. */
13389 if (dy)
13390 shift_glyph_matrix (w, current_matrix,
13391 first_unchanged_at_end_vpos + dvpos,
13392 bottom_vpos, dy);
13393
13394 if (first_unchanged_at_end_row)
13395 first_unchanged_at_end_row += dvpos;
13396
13397 /* If scrolling up, there may be some lines to display at the end of
13398 the window. */
13399 last_text_row_at_end = NULL;
13400 if (dy < 0)
13401 {
9c6ba6d1
GM
13402 /* Scrolling up can leave for example a partially visible line
13403 at the end of the window to be redisplayed. */
5f5c8ee5
GM
13404 /* Set last_row to the glyph row in the current matrix where the
13405 window end line is found. It has been moved up or down in
13406 the matrix by dvpos. */
13407 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13408 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13409
13410 /* If last_row is the window end line, it should display text. */
13411 xassert (last_row->displays_text_p);
13412
13413 /* If window end line was partially visible before, begin
13414 displaying at that line. Otherwise begin displaying with the
13415 line following it. */
13416 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13417 {
13418 init_to_row_start (&it, w, last_row);
13419 it.vpos = last_vpos;
13420 it.current_y = last_row->y;
13421 }
13422 else
13423 {
13424 init_to_row_end (&it, w, last_row);
13425 it.vpos = 1 + last_vpos;
13426 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13427 ++last_row;
13428 }
12adba34 13429
23e8bd86
GM
13430 /* We may start in a continuation line. If so, we have to
13431 get the right continuation_lines_width and current_x. */
13432 it.continuation_lines_width = last_row->continuation_lines_width;
13433 it.hpos = it.current_x = 0;
2311178e 13434
23e8bd86
GM
13435 /* Display the rest of the lines at the window end. */
13436 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13437 while (it.current_y < it.last_visible_y
13438 && !fonts_changed_p)
5f5c8ee5 13439 {
23e8bd86
GM
13440 /* Is it always sure that the display agrees with lines in
13441 the current matrix? I don't think so, so we mark rows
13442 displayed invalid in the current matrix by setting their
13443 enabled_p flag to zero. */
13444 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13445 if (display_line (&it))
13446 last_text_row_at_end = it.glyph_row - 1;
5f5c8ee5
GM
13447 }
13448 }
12adba34 13449
5f5c8ee5
GM
13450 /* Update window_end_pos and window_end_vpos. */
13451 if (first_unchanged_at_end_row
13452 && first_unchanged_at_end_row->y < it.last_visible_y
13453 && !last_text_row_at_end)
13454 {
13455 /* Window end line if one of the preserved rows from the current
13456 matrix. Set row to the last row displaying text in current
13457 matrix starting at first_unchanged_at_end_row, after
13458 scrolling. */
13459 xassert (first_unchanged_at_end_row->displays_text_p);
13460 row = find_last_row_displaying_text (w->current_matrix, &it,
13461 first_unchanged_at_end_row);
13462 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
2311178e 13463
ac90c44f 13464 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
5f5c8ee5 13465 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
ac90c44f
GM
13466 w->window_end_vpos
13467 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
23fca891 13468 xassert (w->window_end_bytepos >= 0);
0416532f 13469 IF_DEBUG (debug_method_add (w, "A"));
5f5c8ee5
GM
13470 }
13471 else if (last_text_row_at_end)
13472 {
ac90c44f
GM
13473 w->window_end_pos
13474 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
5f5c8ee5
GM
13475 w->window_end_bytepos
13476 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
ac90c44f
GM
13477 w->window_end_vpos
13478 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
23fca891 13479 xassert (w->window_end_bytepos >= 0);
0416532f 13480 IF_DEBUG (debug_method_add (w, "B"));
5f5c8ee5
GM
13481 }
13482 else if (last_text_row)
13483 {
13484 /* We have displayed either to the end of the window or at the
13485 end of the window, i.e. the last row with text is to be found
13486 in the desired matrix. */
ac90c44f
GM
13487 w->window_end_pos
13488 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
5f5c8ee5
GM
13489 w->window_end_bytepos
13490 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
13491 w->window_end_vpos
13492 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
23fca891 13493 xassert (w->window_end_bytepos >= 0);
5f5c8ee5
GM
13494 }
13495 else if (first_unchanged_at_end_row == NULL
13496 && last_text_row == NULL
13497 && last_text_row_at_end == NULL)
13498 {
13499 /* Displayed to end of window, but no line containing text was
13500 displayed. Lines were deleted at the end of the window. */
0416532f
GM
13501 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13502 int vpos = XFASTINT (w->window_end_vpos);
13503 struct glyph_row *current_row = current_matrix->rows + vpos;
13504 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13505
13506 for (row = NULL;
13507 row == NULL && vpos >= first_vpos;
13508 --vpos, --current_row, --desired_row)
13509 {
13510 if (desired_row->enabled_p)
13511 {
13512 if (desired_row->displays_text_p)
13513 row = desired_row;
13514 }
13515 else if (current_row->displays_text_p)
13516 row = current_row;
13517 }
12adba34 13518
0416532f 13519 xassert (row != NULL);
d88a79d4 13520 w->window_end_vpos = make_number (vpos + 1);
8c56a983
GM
13521 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13522 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
23fca891 13523 xassert (w->window_end_bytepos >= 0);
0416532f 13524 IF_DEBUG (debug_method_add (w, "C"));
5f5c8ee5
GM
13525 }
13526 else
13527 abort ();
ac90c44f 13528
8cdb267e
GM
13529#if 0 /* This leads to problems, for instance when the cursor is
13530 at ZV, and the cursor line displays no text. */
ac90c44f
GM
13531 /* Disable rows below what's displayed in the window. This makes
13532 debugging easier. */
13533 enable_glyph_matrix_rows (current_matrix,
13534 XFASTINT (w->window_end_vpos) + 1,
13535 bottom_vpos, 0);
8cdb267e 13536#endif
23e8bd86 13537
5f5c8ee5
GM
13538 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13539 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 13540
5f5c8ee5
GM
13541 /* Record that display has not been completed. */
13542 w->window_end_valid = Qnil;
13543 w->desired_matrix->no_scrolling_p = 1;
ef121659 13544 return 3;
27d16f05
GM
13545
13546#undef GIVE_UP
12adba34 13547}
0f9c0ff0 13548
a2889657 13549
5f5c8ee5
GM
13550\f
13551/***********************************************************************
13552 More debugging support
13553 ***********************************************************************/
a2889657 13554
5f5c8ee5 13555#if GLYPH_DEBUG
a2889657 13556
eaaa65b0 13557void dump_glyph_row P_ ((struct glyph_row *, int, int));
9ab436b9 13558void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
e187cf71 13559void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
1c9241f5 13560
31b24551 13561
9ab436b9 13562/* Dump the contents of glyph matrix MATRIX on stderr.
ca26e1c8 13563
9ab436b9
GM
13564 GLYPHS 0 means don't show glyph contents.
13565 GLYPHS 1 means show glyphs in short form
13566 GLYPHS > 1 means show glyphs in long form. */
13567
13568void
13569dump_glyph_matrix (matrix, glyphs)
5f5c8ee5 13570 struct glyph_matrix *matrix;
9ab436b9 13571 int glyphs;
5f5c8ee5 13572{
efc63ef0 13573 int i;
5f5c8ee5 13574 for (i = 0; i < matrix->nrows; ++i)
eaaa65b0 13575 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
5f5c8ee5 13576}
31b24551 13577
68a37fa8 13578
e187cf71
GM
13579/* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13580 the glyph row and area where the glyph comes from. */
13581
13582void
13583dump_glyph (row, glyph, area)
13584 struct glyph_row *row;
13585 struct glyph *glyph;
13586 int area;
13587{
13588 if (glyph->type == CHAR_GLYPH)
13589 {
13590 fprintf (stderr,
13591 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13592 glyph - row->glyphs[TEXT_AREA],
13593 'C',
13594 glyph->charpos,
13595 (BUFFERP (glyph->object)
13596 ? 'B'
13597 : (STRINGP (glyph->object)
13598 ? 'S'
13599 : '-')),
13600 glyph->pixel_width,
13601 glyph->u.ch,
13602 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13603 ? glyph->u.ch
13604 : '.'),
13605 glyph->face_id,
13606 glyph->left_box_line_p,
13607 glyph->right_box_line_p);
13608 }
13609 else if (glyph->type == STRETCH_GLYPH)
13610 {
13611 fprintf (stderr,
13612 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13613 glyph - row->glyphs[TEXT_AREA],
13614 'S',
13615 glyph->charpos,
13616 (BUFFERP (glyph->object)
13617 ? 'B'
13618 : (STRINGP (glyph->object)
13619 ? 'S'
13620 : '-')),
13621 glyph->pixel_width,
13622 0,
13623 '.',
13624 glyph->face_id,
13625 glyph->left_box_line_p,
13626 glyph->right_box_line_p);
13627 }
13628 else if (glyph->type == IMAGE_GLYPH)
13629 {
13630 fprintf (stderr,
13631 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13632 glyph - row->glyphs[TEXT_AREA],
13633 'I',
13634 glyph->charpos,
13635 (BUFFERP (glyph->object)
13636 ? 'B'
13637 : (STRINGP (glyph->object)
13638 ? 'S'
13639 : '-')),
13640 glyph->pixel_width,
13641 glyph->u.img_id,
13642 '.',
13643 glyph->face_id,
13644 glyph->left_box_line_p,
13645 glyph->right_box_line_p);
13646 }
13647}
13648
13649
5f5c8ee5 13650/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
9ab436b9
GM
13651 GLYPHS 0 means don't show glyph contents.
13652 GLYPHS 1 means show glyphs in short form
13653 GLYPHS > 1 means show glyphs in long form. */
a2889657 13654
5f5c8ee5 13655void
eaaa65b0
GM
13656dump_glyph_row (row, vpos, glyphs)
13657 struct glyph_row *row;
9ab436b9 13658 int vpos, glyphs;
5f5c8ee5 13659{
9ab436b9
GM
13660 if (glyphs != 1)
13661 {
b94c0d9c 13662 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
9ab436b9 13663 fprintf (stderr, "=======================================================================\n");
2311178e 13664
0ad38729 13665 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
33ea6c4d 13666%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
eaaa65b0 13667 vpos,
9ab436b9
GM
13668 MATRIX_ROW_START_CHARPOS (row),
13669 MATRIX_ROW_END_CHARPOS (row),
13670 row->used[TEXT_AREA],
13671 row->contains_overlapping_glyphs_p,
13672 row->enabled_p,
9ab436b9
GM
13673 row->truncated_on_left_p,
13674 row->truncated_on_right_p,
13675 row->overlay_arrow_p,
13676 row->continued_p,
13677 MATRIX_ROW_CONTINUATION_LINE_P (row),
13678 row->displays_text_p,
13679 row->ends_at_zv_p,
13680 row->fill_line_p,
13681 row->ends_in_middle_of_char_p,
13682 row->starts_in_middle_of_char_p,
b94c0d9c 13683 row->mouse_face_p,
9ab436b9
GM
13684 row->x,
13685 row->y,
13686 row->pixel_width,
13687 row->height,
13688 row->visible_height,
13689 row->ascent,
13690 row->phys_ascent);
33ea6c4d
GM
13691 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13692 row->end.overlay_string_index,
13693 row->continuation_lines_width);
9ab436b9
GM
13694 fprintf (stderr, "%9d %5d\n",
13695 CHARPOS (row->start.string_pos),
13696 CHARPOS (row->end.string_pos));
13697 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13698 row->end.dpvec_index);
13699 }
2311178e 13700
9ab436b9 13701 if (glyphs > 1)
bd66d1ba 13702 {
e187cf71
GM
13703 int area;
13704
13705 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13706 {
091f8878
GM
13707 struct glyph *glyph = row->glyphs[area];
13708 struct glyph *glyph_end = glyph + row->used[area];
2311178e 13709
e187cf71 13710 /* Glyph for a line end in text. */
091f8878 13711 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
e187cf71 13712 ++glyph_end;
2311178e 13713
e187cf71
GM
13714 if (glyph < glyph_end)
13715 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
2311178e 13716
e187cf71
GM
13717 for (; glyph < glyph_end; ++glyph)
13718 dump_glyph (row, glyph, area);
f7b4b63a 13719 }
f4faa47c 13720 }
9ab436b9
GM
13721 else if (glyphs == 1)
13722 {
e187cf71 13723 int area;
9ab436b9 13724
e187cf71 13725 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
9ab436b9 13726 {
e187cf71
GM
13727 char *s = (char *) alloca (row->used[area] + 1);
13728 int i;
13729
13730 for (i = 0; i < row->used[area]; ++i)
13731 {
13732 struct glyph *glyph = row->glyphs[area] + i;
13733 if (glyph->type == CHAR_GLYPH
13734 && glyph->u.ch < 0x80
13735 && glyph->u.ch >= ' ')
13736 s[i] = glyph->u.ch;
13737 else
13738 s[i] = '.';
13739 }
2311178e 13740
e187cf71
GM
13741 s[i] = '\0';
13742 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13743 }
9ab436b9 13744 }
5f5c8ee5 13745}
f4faa47c 13746
a2889657 13747
5f5c8ee5
GM
13748DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13749 Sdump_glyph_matrix, 0, 1, "p",
7ee72033 13750 doc: /* Dump the current matrix of the selected window to stderr.
228299fa
GM
13751Shows contents of glyph row structures. With non-nil
13752parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
7ee72033
MB
13753glyphs in short form, otherwise show glyphs in long form. */)
13754 (glyphs)
9ab436b9 13755 Lisp_Object glyphs;
5f5c8ee5
GM
13756{
13757 struct window *w = XWINDOW (selected_window);
13758 struct buffer *buffer = XBUFFER (w->buffer);
13759
13760 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13761 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13762 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13763 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13764 fprintf (stderr, "=============================================\n");
9ab436b9
GM
13765 dump_glyph_matrix (w->current_matrix,
13766 NILP (glyphs) ? 0 : XINT (glyphs));
5f5c8ee5
GM
13767 return Qnil;
13768}
1c2250c2 13769
1fca3fae 13770
7d4cc828
GM
13771DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13772 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13773 ()
13774{
13775 struct frame *f = XFRAME (selected_frame);
13776 dump_glyph_matrix (f->current_matrix, 1);
13777 return Qnil;
13778}
13779
13780
e9abc296 13781DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
7ee72033 13782 doc: /* Dump glyph row ROW to stderr.
228299fa
GM
13783GLYPH 0 means don't dump glyphs.
13784GLYPH 1 means dump glyphs in short form.
7ee72033
MB
13785GLYPH > 1 or omitted means dump glyphs in long form. */)
13786 (row, glyphs)
e9abc296 13787 Lisp_Object row, glyphs;
5f5c8ee5 13788{
eaaa65b0
GM
13789 struct glyph_matrix *matrix;
13790 int vpos;
2311178e 13791
b7826503 13792 CHECK_NUMBER (row);
eaaa65b0
GM
13793 matrix = XWINDOW (selected_window)->current_matrix;
13794 vpos = XINT (row);
13795 if (vpos >= 0 && vpos < matrix->nrows)
13796 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13797 vpos,
13798 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
13799 return Qnil;
13800}
1fca3fae 13801
67481ae5 13802
b94c0d9c 13803DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
c2552f79 13804 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
228299fa
GM
13805GLYPH 0 means don't dump glyphs.
13806GLYPH 1 means dump glyphs in short form.
7ee72033
MB
13807GLYPH > 1 or omitted means dump glyphs in long form. */)
13808 (row, glyphs)
b94c0d9c 13809 Lisp_Object row, glyphs;
5f5c8ee5 13810{
886bd6f2 13811 struct frame *sf = SELECTED_FRAME ();
eaaa65b0
GM
13812 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13813 int vpos;
2311178e 13814
b7826503 13815 CHECK_NUMBER (row);
eaaa65b0
GM
13816 vpos = XINT (row);
13817 if (vpos >= 0 && vpos < m->nrows)
13818 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13819 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
13820 return Qnil;
13821}
ca26e1c8 13822
0f9c0ff0 13823
62397849 13824DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
7ee72033
MB
13825 doc: /* Toggle tracing of redisplay.
13826With ARG, turn tracing on if and only if ARG is positive. */)
13827 (arg)
62397849 13828 Lisp_Object arg;
5f5c8ee5 13829{
62397849
GM
13830 if (NILP (arg))
13831 trace_redisplay_p = !trace_redisplay_p;
13832 else
13833 {
13834 arg = Fprefix_numeric_value (arg);
13835 trace_redisplay_p = XINT (arg) > 0;
13836 }
2311178e 13837
5f5c8ee5
GM
13838 return Qnil;
13839}
bf9249e3
GM
13840
13841
f4a374a1 13842DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
e9757d41
SM
13843 doc: /* Like `format', but print result to stderr.
13844usage: (trace-to-stderr STRING &rest OBJECTS) */)
7ee72033 13845 (nargs, args)
f4a374a1
GM
13846 int nargs;
13847 Lisp_Object *args;
bf9249e3 13848{
f4a374a1 13849 Lisp_Object s = Fformat (nargs, args);
2051c264 13850 fprintf (stderr, "%s", SDATA (s));
bf9249e3
GM
13851 return Qnil;
13852}
2311178e 13853
5f5c8ee5 13854#endif /* GLYPH_DEBUG */
ca26e1c8 13855
ca26e1c8 13856
5f5c8ee5
GM
13857\f
13858/***********************************************************************
13859 Building Desired Matrix Rows
13860 ***********************************************************************/
a2889657 13861
5f5c8ee5
GM
13862/* Return a temporary glyph row holding the glyphs of an overlay
13863 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 13864
5f5c8ee5
GM
13865static struct glyph_row *
13866get_overlay_arrow_glyph_row (w)
13867 struct window *w;
13868{
13869 struct frame *f = XFRAME (WINDOW_FRAME (w));
13870 struct buffer *buffer = XBUFFER (w->buffer);
13871 struct buffer *old = current_buffer;
50f80c2f 13872 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
2051c264 13873 int arrow_len = SCHARS (Voverlay_arrow_string);
50f80c2f
KR
13874 const unsigned char *arrow_end = arrow_string + arrow_len;
13875 const unsigned char *p;
5f5c8ee5
GM
13876 struct it it;
13877 int multibyte_p;
13878 int n_glyphs_before;
13879
13880 set_buffer_temp (buffer);
13881 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13882 it.glyph_row->used[TEXT_AREA] = 0;
13883 SET_TEXT_POS (it.position, 0, 0);
13884
13885 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13886 p = arrow_string;
13887 while (p < arrow_end)
13888 {
13889 Lisp_Object face, ilisp;
2311178e 13890
5f5c8ee5
GM
13891 /* Get the next character. */
13892 if (multibyte_p)
4fdb80f2 13893 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
13894 else
13895 it.c = *p, it.len = 1;
13896 p += it.len;
2311178e 13897
5f5c8ee5 13898 /* Get its face. */
ac90c44f 13899 ilisp = make_number (p - arrow_string);
5f5c8ee5
GM
13900 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
13901 it.face_id = compute_char_face (f, it.c, face);
13902
13903 /* Compute its width, get its glyphs. */
13904 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 13905 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
13906 PRODUCE_GLYPHS (&it);
13907
13908 /* If this character doesn't fit any more in the line, we have
13909 to remove some glyphs. */
13910 if (it.current_x > it.last_visible_x)
13911 {
13912 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13913 break;
13914 }
13915 }
2311178e 13916
5f5c8ee5
GM
13917 set_buffer_temp (old);
13918 return it.glyph_row;
13919}
ca26e1c8 13920
b0a0fbda 13921
5f5c8ee5
GM
13922/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13923 glyphs are only inserted for terminal frames since we can't really
13924 win with truncation glyphs when partially visible glyphs are
13925 involved. Which glyphs to insert is determined by
13926 produce_special_glyphs. */
67481ae5 13927
5f5c8ee5
GM
13928static void
13929insert_left_trunc_glyphs (it)
13930 struct it *it;
13931{
13932 struct it truncate_it;
13933 struct glyph *from, *end, *to, *toend;
13934
13935 xassert (!FRAME_WINDOW_P (it->f));
13936
13937 /* Get the truncation glyphs. */
13938 truncate_it = *it;
5f5c8ee5
GM
13939 truncate_it.current_x = 0;
13940 truncate_it.face_id = DEFAULT_FACE_ID;
13941 truncate_it.glyph_row = &scratch_glyph_row;
13942 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13943 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
6fc556fd 13944 truncate_it.object = make_number (0);
5f5c8ee5 13945 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
2311178e 13946
5f5c8ee5
GM
13947 /* Overwrite glyphs from IT with truncation glyphs. */
13948 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13949 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13950 to = it->glyph_row->glyphs[TEXT_AREA];
13951 toend = to + it->glyph_row->used[TEXT_AREA];
13952
13953 while (from < end)
13954 *to++ = *from++;
13955
37be86f2
KH
13956 /* There may be padding glyphs left over. Overwrite them too. */
13957 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13958 {
13959 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13960 while (from < end)
13961 *to++ = *from++;
13962 }
5f5c8ee5 13963
37be86f2
KH
13964 if (to > toend)
13965 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
5f5c8ee5 13966}
e0bfbde6 13967
e0bfbde6 13968
5f5c8ee5 13969/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 13970
5f5c8ee5
GM
13971 Most of the time, ascent and height of a display line will be equal
13972 to the max_ascent and max_height values of the display iterator
13973 structure. This is not the case if
67481ae5 13974
5f5c8ee5
GM
13975 1. We hit ZV without displaying anything. In this case, max_ascent
13976 and max_height will be zero.
1c9241f5 13977
5f5c8ee5
GM
13978 2. We have some glyphs that don't contribute to the line height.
13979 (The glyph row flag contributes_to_line_height_p is for future
13980 pixmap extensions).
f6fd109b 13981
5f5c8ee5
GM
13982 The first case is easily covered by using default values because in
13983 these cases, the line height does not really matter, except that it
13984 must not be zero. */
67481ae5 13985
5f5c8ee5
GM
13986static void
13987compute_line_metrics (it)
13988 struct it *it;
13989{
13990 struct glyph_row *row = it->glyph_row;
13991 int area, i;
1c2250c2 13992
5f5c8ee5
GM
13993 if (FRAME_WINDOW_P (it->f))
13994 {
75c5350a 13995 int i, min_y, max_y;
1c2250c2 13996
5f5c8ee5
GM
13997 /* The line may consist of one space only, that was added to
13998 place the cursor on it. If so, the row's height hasn't been
13999 computed yet. */
14000 if (row->height == 0)
14001 {
14002 if (it->max_ascent + it->max_descent == 0)
da8b7f4f 14003 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
5f5c8ee5
GM
14004 row->ascent = it->max_ascent;
14005 row->height = it->max_ascent + it->max_descent;
312246d1
GM
14006 row->phys_ascent = it->max_phys_ascent;
14007 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5 14008 }
2311178e 14009
5f5c8ee5
GM
14010 /* Compute the width of this line. */
14011 row->pixel_width = row->x;
14012 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14013 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14014
14015 xassert (row->pixel_width >= 0);
14016 xassert (row->ascent >= 0 && row->height > 0);
14017
312246d1
GM
14018 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14019 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14020
14021 /* If first line's physical ascent is larger than its logical
14022 ascent, use the physical ascent, and make the row taller.
14023 This makes accented characters fully visible. */
b28cb6ed 14024 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
312246d1
GM
14025 && row->phys_ascent > row->ascent)
14026 {
14027 row->height += row->phys_ascent - row->ascent;
14028 row->ascent = row->phys_ascent;
14029 }
14030
5f5c8ee5
GM
14031 /* Compute how much of the line is visible. */
14032 row->visible_height = row->height;
2311178e 14033
da8b7f4f
KS
14034 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14035 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
75c5350a
GM
14036
14037 if (row->y < min_y)
14038 row->visible_height -= min_y - row->y;
14039 if (row->y + row->height > max_y)
14040 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5
GM
14041 }
14042 else
14043 {
14044 row->pixel_width = row->used[TEXT_AREA];
33ea6c4d
GM
14045 if (row->continued_p)
14046 row->pixel_width -= it->continuation_pixel_width;
14047 else if (row->truncated_on_right_p)
14048 row->pixel_width -= it->truncation_pixel_width;
312246d1
GM
14049 row->ascent = row->phys_ascent = 0;
14050 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 14051 }
67481ae5 14052
5f5c8ee5
GM
14053 /* Compute a hash code for this row. */
14054 row->hash = 0;
14055 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14056 for (i = 0; i < row->used[area]; ++i)
14057 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14058 + row->glyphs[area][i].u.val
43d120d8
KH
14059 + row->glyphs[area][i].face_id
14060 + row->glyphs[area][i].padding_p
5f5c8ee5 14061 + (row->glyphs[area][i].type << 2));
a2889657 14062
5f5c8ee5 14063 it->max_ascent = it->max_descent = 0;
312246d1 14064 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 14065}
12adba34 14066
ca26e1c8 14067
5f5c8ee5
GM
14068/* Append one space to the glyph row of iterator IT if doing a
14069 window-based redisplay. DEFAULT_FACE_P non-zero means let the
14070 space have the default face, otherwise let it have the same face as
80c6cb1f 14071 IT->face_id. Value is non-zero if a space was added.
c6e89d6c
GM
14072
14073 This function is called to make sure that there is always one glyph
14074 at the end of a glyph row that the cursor can be set on under
14075 window-systems. (If there weren't such a glyph we would not know
14076 how wide and tall a box cursor should be displayed).
14077
14078 At the same time this space let's a nicely handle clearing to the
14079 end of the line if the row ends in italic text. */
ca26e1c8 14080
80c6cb1f 14081static int
5f5c8ee5
GM
14082append_space (it, default_face_p)
14083 struct it *it;
14084 int default_face_p;
14085{
14086 if (FRAME_WINDOW_P (it->f))
14087 {
14088 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 14089
5f5c8ee5
GM
14090 if (it->glyph_row->glyphs[TEXT_AREA] + n
14091 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 14092 {
cafafe0b
GM
14093 /* Save some values that must not be changed.
14094 Must save IT->c and IT->len because otherwise
14095 ITERATOR_AT_END_P wouldn't work anymore after
14096 append_space has been called. */
478d746b 14097 enum display_element_type saved_what = it->what;
cafafe0b
GM
14098 int saved_c = it->c, saved_len = it->len;
14099 int saved_x = it->current_x;
5f5c8ee5 14100 int saved_face_id = it->face_id;
cafafe0b 14101 struct text_pos saved_pos;
5f5c8ee5 14102 Lisp_Object saved_object;
980806b6 14103 struct face *face;
5f5c8ee5
GM
14104
14105 saved_object = it->object;
14106 saved_pos = it->position;
2311178e 14107
5f5c8ee5
GM
14108 it->what = IT_CHARACTER;
14109 bzero (&it->position, sizeof it->position);
6fc556fd 14110 it->object = make_number (0);
5f5c8ee5
GM
14111 it->c = ' ';
14112 it->len = 1;
5f5c8ee5
GM
14113
14114 if (default_face_p)
14115 it->face_id = DEFAULT_FACE_ID;
4aad61f8
GM
14116 else if (it->face_before_selective_p)
14117 it->face_id = it->saved_face_id;
980806b6
KH
14118 face = FACE_FROM_ID (it->f, it->face_id);
14119 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
1842fc1a 14120
5f5c8ee5 14121 PRODUCE_GLYPHS (it);
2311178e 14122
5f5c8ee5
GM
14123 it->current_x = saved_x;
14124 it->object = saved_object;
14125 it->position = saved_pos;
14126 it->what = saved_what;
14127 it->face_id = saved_face_id;
cafafe0b
GM
14128 it->len = saved_len;
14129 it->c = saved_c;
80c6cb1f 14130 return 1;
5f5c8ee5
GM
14131 }
14132 }
80c6cb1f
GM
14133
14134 return 0;
5f5c8ee5 14135}
12adba34 14136
1842fc1a 14137
5f5c8ee5
GM
14138/* Extend the face of the last glyph in the text area of IT->glyph_row
14139 to the end of the display line. Called from display_line.
14140 If the glyph row is empty, add a space glyph to it so that we
14141 know the face to draw. Set the glyph row flag fill_line_p. */
2311178e 14142
5f5c8ee5
GM
14143static void
14144extend_face_to_end_of_line (it)
14145 struct it *it;
14146{
14147 struct face *face;
14148 struct frame *f = it->f;
1842fc1a 14149
5f5c8ee5
GM
14150 /* If line is already filled, do nothing. */
14151 if (it->current_x >= it->last_visible_x)
14152 return;
2311178e 14153
5f5c8ee5
GM
14154 /* Face extension extends the background and box of IT->face_id
14155 to the end of the line. If the background equals the background
4aad61f8
GM
14156 of the frame, we don't have to do anything. */
14157 if (it->face_before_selective_p)
14158 face = FACE_FROM_ID (it->f, it->saved_face_id);
14159 else
14160 face = FACE_FROM_ID (f, it->face_id);
2311178e 14161
5f5c8ee5
GM
14162 if (FRAME_WINDOW_P (f)
14163 && face->box == FACE_NO_BOX
14164 && face->background == FRAME_BACKGROUND_PIXEL (f)
14165 && !face->stipple)
14166 return;
1842fc1a 14167
5f5c8ee5
GM
14168 /* Set the glyph row flag indicating that the face of the last glyph
14169 in the text area has to be drawn to the end of the text area. */
14170 it->glyph_row->fill_line_p = 1;
545e04f6 14171
980806b6
KH
14172 /* If current character of IT is not ASCII, make sure we have the
14173 ASCII face. This will be automatically undone the next time
14174 get_next_display_element returns a multibyte character. Note
14175 that the character will always be single byte in unibyte text. */
14176 if (!SINGLE_BYTE_CHAR_P (it->c))
5f5c8ee5 14177 {
980806b6 14178 it->face_id = FACE_FOR_CHAR (f, face, 0);
5f5c8ee5 14179 }
545e04f6 14180
5f5c8ee5
GM
14181 if (FRAME_WINDOW_P (f))
14182 {
14183 /* If the row is empty, add a space with the current face of IT,
14184 so that we know which face to draw. */
14185 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 14186 {
5f5c8ee5 14187 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
43d120d8 14188 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
5f5c8ee5 14189 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 14190 }
5f5c8ee5
GM
14191 }
14192 else
14193 {
14194 /* Save some values that must not be changed. */
14195 int saved_x = it->current_x;
14196 struct text_pos saved_pos;
14197 Lisp_Object saved_object;
478d746b 14198 enum display_element_type saved_what = it->what;
4aad61f8 14199 int saved_face_id = it->face_id;
5f5c8ee5
GM
14200
14201 saved_object = it->object;
14202 saved_pos = it->position;
2311178e 14203
5f5c8ee5
GM
14204 it->what = IT_CHARACTER;
14205 bzero (&it->position, sizeof it->position);
6fc556fd 14206 it->object = make_number (0);
5f5c8ee5
GM
14207 it->c = ' ';
14208 it->len = 1;
4aad61f8 14209 it->face_id = face->id;
2311178e 14210
5f5c8ee5 14211 PRODUCE_GLYPHS (it);
2311178e 14212
5f5c8ee5
GM
14213 while (it->current_x <= it->last_visible_x)
14214 PRODUCE_GLYPHS (it);
2311178e 14215
5f5c8ee5
GM
14216 /* Don't count these blanks really. It would let us insert a left
14217 truncation glyph below and make us set the cursor on them, maybe. */
14218 it->current_x = saved_x;
14219 it->object = saved_object;
14220 it->position = saved_pos;
14221 it->what = saved_what;
4aad61f8 14222 it->face_id = saved_face_id;
5f5c8ee5
GM
14223 }
14224}
12adba34 14225
545e04f6 14226
5f5c8ee5
GM
14227/* Value is non-zero if text starting at CHARPOS in current_buffer is
14228 trailing whitespace. */
1c9241f5 14229
5f5c8ee5
GM
14230static int
14231trailing_whitespace_p (charpos)
14232 int charpos;
14233{
14234 int bytepos = CHAR_TO_BYTE (charpos);
14235 int c = 0;
7bbe686f 14236
5f5c8ee5
GM
14237 while (bytepos < ZV_BYTE
14238 && (c = FETCH_CHAR (bytepos),
14239 c == ' ' || c == '\t'))
14240 ++bytepos;
0d09d1e6 14241
8f897821
GM
14242 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14243 {
14244 if (bytepos != PT_BYTE)
14245 return 1;
14246 }
14247 return 0;
5f5c8ee5 14248}
31b24551 14249
545e04f6 14250
5f5c8ee5 14251/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 14252
5f5c8ee5
GM
14253void
14254highlight_trailing_whitespace (f, row)
14255 struct frame *f;
14256 struct glyph_row *row;
14257{
14258 int used = row->used[TEXT_AREA];
2311178e 14259
5f5c8ee5
GM
14260 if (used)
14261 {
14262 struct glyph *start = row->glyphs[TEXT_AREA];
14263 struct glyph *glyph = start + used - 1;
14264
ade0bee1
GM
14265 /* Skip over glyphs inserted to display the cursor at the
14266 end of a line, for extending the face of the last glyph
14267 to the end of the line on terminals, and for truncation
14268 and continuation glyphs. */
65008712
GM
14269 while (glyph >= start
14270 && glyph->type == CHAR_GLYPH
65008712 14271 && INTEGERP (glyph->object))
5f5c8ee5
GM
14272 --glyph;
14273
14274 /* If last glyph is a space or stretch, and it's trailing
14275 whitespace, set the face of all trailing whitespace glyphs in
14276 IT->glyph_row to `trailing-whitespace'. */
14277 if (glyph >= start
14278 && BUFFERP (glyph->object)
14279 && (glyph->type == STRETCH_GLYPH
14280 || (glyph->type == CHAR_GLYPH
43d120d8 14281 && glyph->u.ch == ' '))
5f5c8ee5 14282 && trailing_whitespace_p (glyph->charpos))
545e04f6 14283 {
980806b6 14284 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
2311178e 14285
5f5c8ee5
GM
14286 while (glyph >= start
14287 && BUFFERP (glyph->object)
14288 && (glyph->type == STRETCH_GLYPH
14289 || (glyph->type == CHAR_GLYPH
43d120d8
KH
14290 && glyph->u.ch == ' ')))
14291 (glyph--)->face_id = face_id;
545e04f6 14292 }
a2889657 14293 }
5f5c8ee5 14294}
a2889657 14295
5fcbb24d 14296
cafafe0b 14297/* Value is non-zero if glyph row ROW in window W should be
0fd37545 14298 used to hold the cursor. */
cafafe0b
GM
14299
14300static int
14301cursor_row_p (w, row)
14302 struct window *w;
14303 struct glyph_row *row;
14304{
9a038881 14305 int cursor_row_p = 1;
2311178e 14306
cafafe0b
GM
14307 if (PT == MATRIX_ROW_END_CHARPOS (row))
14308 {
9a038881
GM
14309 /* If the row ends with a newline from a string, we don't want
14310 the cursor there (if the row is continued it doesn't end in a
14311 newline). */
14312 if (CHARPOS (row->end.string_pos) >= 0
14313 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14314 cursor_row_p = row->continued_p;
14315
14316 /* If the row ends at ZV, display the cursor at the end of that
14317 row instead of at the start of the row below. */
14318 else if (row->ends_at_zv_p)
14319 cursor_row_p = 1;
14320 else
14321 cursor_row_p = 0;
cafafe0b
GM
14322 }
14323
9a038881 14324 return cursor_row_p;
cafafe0b
GM
14325}
14326
14327
5f5c8ee5
GM
14328/* Construct the glyph row IT->glyph_row in the desired matrix of
14329 IT->w from text at the current position of IT. See dispextern.h
14330 for an overview of struct it. Value is non-zero if
14331 IT->glyph_row displays text, as opposed to a line displaying ZV
14332 only. */
2311178e 14333
5f5c8ee5
GM
14334static int
14335display_line (it)
14336 struct it *it;
14337{
14338 struct glyph_row *row = it->glyph_row;
14339
14340 /* We always start displaying at hpos zero even if hscrolled. */
14341 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 14342
5f5c8ee5
GM
14343 /* We must not display in a row that's not a text row. */
14344 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14345 < it->w->desired_matrix->nrows);
12adba34 14346
5f5c8ee5
GM
14347 /* Is IT->w showing the region? */
14348 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 14349
5f5c8ee5
GM
14350 /* Clear the result glyph row and enable it. */
14351 prepare_desired_row (row);
12adba34 14352
5f5c8ee5 14353 row->y = it->current_y;
29b3ea63 14354 row->start = it->start;
5f5c8ee5
GM
14355 row->continuation_lines_width = it->continuation_lines_width;
14356 row->displays_text_p = 1;
91004049
GM
14357 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14358 it->starts_in_middle_of_char_p = 0;
5f5c8ee5
GM
14359
14360 /* Arrange the overlays nicely for our purposes. Usually, we call
14361 display_line on only one line at a time, in which case this
14362 can't really hurt too much, or we call it on lines which appear
14363 one after another in the buffer, in which case all calls to
14364 recenter_overlay_lists but the first will be pretty cheap. */
14365 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14366
5f5c8ee5
GM
14367 /* Move over display elements that are not visible because we are
14368 hscrolled. This may stop at an x-position < IT->first_visible_x
14369 if the first glyph is partially visible or if we hit a line end. */
14370 if (it->current_x < it->first_visible_x)
14371 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14372 MOVE_TO_POS | MOVE_TO_X);
14373
14374 /* Get the initial row height. This is either the height of the
14375 text hscrolled, if there is any, or zero. */
14376 row->ascent = it->max_ascent;
14377 row->height = it->max_ascent + it->max_descent;
312246d1
GM
14378 row->phys_ascent = it->max_phys_ascent;
14379 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
14380
14381 /* Loop generating characters. The loop is left with IT on the next
14382 character to display. */
14383 while (1)
14384 {
14385 int n_glyphs_before, hpos_before, x_before;
14386 int x, i, nglyphs;
ae26e27d 14387 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
b28cb6ed 14388
5f5c8ee5
GM
14389 /* Retrieve the next thing to display. Value is zero if end of
14390 buffer reached. */
14391 if (!get_next_display_element (it))
14392 {
14393 /* Maybe add a space at the end of this line that is used to
1b335865
GM
14394 display the cursor there under X. Set the charpos of the
14395 first glyph of blank lines not corresponding to any text
14396 to -1. */
14397 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
14398 || row->used[TEXT_AREA] == 0)
a2889657 14399 {
5f5c8ee5
GM
14400 row->glyphs[TEXT_AREA]->charpos = -1;
14401 row->displays_text_p = 0;
14402
2ad551af 14403 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
e6b70fd8
GM
14404 && (!MINI_WINDOW_P (it->w)
14405 || (minibuf_level && EQ (it->window, minibuf_window))))
5f5c8ee5 14406 row->indicate_empty_line_p = 1;
a2889657 14407 }
2311178e 14408
5f5c8ee5
GM
14409 it->continuation_lines_width = 0;
14410 row->ends_at_zv_p = 1;
14411 break;
a2889657 14412 }
a2889657 14413
5f5c8ee5
GM
14414 /* Now, get the metrics of what we want to display. This also
14415 generates glyphs in `row' (which is IT->glyph_row). */
14416 n_glyphs_before = row->used[TEXT_AREA];
14417 x = it->current_x;
e6819faf
GM
14418
14419 /* Remember the line height so far in case the next element doesn't
14420 fit on the line. */
14421 if (!it->truncate_lines_p)
14422 {
14423 ascent = it->max_ascent;
14424 descent = it->max_descent;
14425 phys_ascent = it->max_phys_ascent;
14426 phys_descent = it->max_phys_descent;
14427 }
2311178e 14428
5f5c8ee5 14429 PRODUCE_GLYPHS (it);
a2889657 14430
5f5c8ee5
GM
14431 /* If this display element was in marginal areas, continue with
14432 the next one. */
14433 if (it->area != TEXT_AREA)
a2889657 14434 {
5f5c8ee5
GM
14435 row->ascent = max (row->ascent, it->max_ascent);
14436 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14437 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14438 row->phys_height = max (row->phys_height,
14439 it->max_phys_ascent + it->max_phys_descent);
cafafe0b 14440 set_iterator_to_next (it, 1);
5f5c8ee5
GM
14441 continue;
14442 }
5936754e 14443
5f5c8ee5
GM
14444 /* Does the display element fit on the line? If we truncate
14445 lines, we should draw past the right edge of the window. If
14446 we don't truncate, we want to stop so that we can display the
14447 continuation glyph before the right margin. If lines are
14448 continued, there are two possible strategies for characters
14449 resulting in more than 1 glyph (e.g. tabs): Display as many
14450 glyphs as possible in this line and leave the rest for the
14451 continuation line, or display the whole element in the next
14452 line. Original redisplay did the former, so we do it also. */
14453 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14454 hpos_before = it->hpos;
14455 x_before = x;
4dcd74e6 14456
4b41cebb 14457 if (/* Not a newline. */
4dcd74e6 14458 nglyphs > 0
202c1b5b 14459 /* Glyphs produced fit entirely in the line. */
4dcd74e6
GM
14460 && it->current_x < it->last_visible_x)
14461 {
37be86f2 14462 it->hpos += nglyphs;
5f5c8ee5
GM
14463 row->ascent = max (row->ascent, it->max_ascent);
14464 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14465 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14466 row->phys_height = max (row->phys_height,
14467 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
14468 if (it->current_x - it->pixel_width < it->first_visible_x)
14469 row->x = x - it->first_visible_x;
14470 }
14471 else
14472 {
14473 int new_x;
14474 struct glyph *glyph;
2311178e 14475
5f5c8ee5 14476 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 14477 {
5f5c8ee5
GM
14478 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14479 new_x = x + glyph->pixel_width;
14480
14481 if (/* Lines are continued. */
14482 !it->truncate_lines_p
14483 && (/* Glyph doesn't fit on the line. */
14484 new_x > it->last_visible_x
14485 /* Or it fits exactly on a window system frame. */
14486 || (new_x == it->last_visible_x
14487 && FRAME_WINDOW_P (it->f))))
a2889657 14488 {
5f5c8ee5 14489 /* End of a continued line. */
2311178e 14490
5f5c8ee5
GM
14491 if (it->hpos == 0
14492 || (new_x == it->last_visible_x
14493 && FRAME_WINDOW_P (it->f)))
14494 {
e6819faf
GM
14495 /* Current glyph is the only one on the line or
14496 fits exactly on the line. We must continue
14497 the line because we can't draw the cursor
14498 after the glyph. */
5f5c8ee5
GM
14499 row->continued_p = 1;
14500 it->current_x = new_x;
14501 it->continuation_lines_width += new_x;
14502 ++it->hpos;
14503 if (i == nglyphs - 1)
cafafe0b 14504 set_iterator_to_next (it, 1);
5f5c8ee5 14505 }
3b3c4bf0
GM
14506 else if (CHAR_GLYPH_PADDING_P (*glyph)
14507 && !FRAME_WINDOW_P (it->f))
14508 {
14509 /* A padding glyph that doesn't fit on this line.
14510 This means the whole character doesn't fit
14511 on the line. */
14512 row->used[TEXT_AREA] = n_glyphs_before;
2311178e 14513
3b3c4bf0
GM
14514 /* Fill the rest of the row with continuation
14515 glyphs like in 20.x. */
14516 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14517 < row->glyphs[1 + TEXT_AREA])
14518 produce_special_glyphs (it, IT_CONTINUATION);
2311178e 14519
3b3c4bf0
GM
14520 row->continued_p = 1;
14521 it->current_x = x_before;
14522 it->continuation_lines_width += x_before;
2311178e 14523
3b3c4bf0
GM
14524 /* Restore the height to what it was before the
14525 element not fitting on the line. */
14526 it->max_ascent = ascent;
14527 it->max_descent = descent;
14528 it->max_phys_ascent = phys_ascent;
14529 it->max_phys_descent = phys_descent;
14530 }
0df56f4d
GM
14531 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14532 {
14533 /* A TAB that extends past the right edge of the
14534 window. This produces a single glyph on
14535 window system frames. We leave the glyph in
14536 this row and let it fill the row, but don't
14537 consume the TAB. */
14538 it->continuation_lines_width += it->last_visible_x;
14539 row->ends_in_middle_of_char_p = 1;
14540 row->continued_p = 1;
14541 glyph->pixel_width = it->last_visible_x - x;
14542 it->starts_in_middle_of_char_p = 1;
14543 }
5f5c8ee5 14544 else
5936754e 14545 {
0df56f4d
GM
14546 /* Something other than a TAB that draws past
14547 the right edge of the window. Restore
14548 positions to values before the element. */
5f5c8ee5 14549 row->used[TEXT_AREA] = n_glyphs_before + i;
2311178e 14550
5f5c8ee5
GM
14551 /* Display continuation glyphs. */
14552 if (!FRAME_WINDOW_P (it->f))
14553 produce_special_glyphs (it, IT_CONTINUATION);
14554 row->continued_p = 1;
653c329b 14555
0df56f4d 14556 it->continuation_lines_width += x;
2311178e 14557
91004049
GM
14558 if (nglyphs > 1 && i > 0)
14559 {
14560 row->ends_in_middle_of_char_p = 1;
14561 it->starts_in_middle_of_char_p = 1;
14562 }
2311178e 14563
e6819faf
GM
14564 /* Restore the height to what it was before the
14565 element not fitting on the line. */
14566 it->max_ascent = ascent;
14567 it->max_descent = descent;
14568 it->max_phys_ascent = phys_ascent;
14569 it->max_phys_descent = phys_descent;
5936754e 14570 }
e6819faf 14571
5f5c8ee5
GM
14572 break;
14573 }
14574 else if (new_x > it->first_visible_x)
14575 {
14576 /* Increment number of glyphs actually displayed. */
14577 ++it->hpos;
2311178e 14578
5f5c8ee5
GM
14579 if (x < it->first_visible_x)
14580 /* Glyph is partially visible, i.e. row starts at
14581 negative X position. */
14582 row->x = x - it->first_visible_x;
14583 }
14584 else
14585 {
14586 /* Glyph is completely off the left margin of the
14587 window. This should not happen because of the
72b8c434
RS
14588 move_it_in_display_line at the start of this
14589 function, unless the text display area of the
14590 window is empty. */
14591 xassert (it->first_visible_x <= it->last_visible_x);
a2889657 14592 }
a2889657 14593 }
2311178e 14594
5f5c8ee5
GM
14595 row->ascent = max (row->ascent, it->max_ascent);
14596 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14597 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14598 row->phys_height = max (row->phys_height,
14599 it->max_phys_ascent + it->max_phys_descent);
2311178e 14600
5f5c8ee5
GM
14601 /* End of this display line if row is continued. */
14602 if (row->continued_p)
14603 break;
a2889657 14604 }
a2889657 14605
5f5c8ee5
GM
14606 /* Is this a line end? If yes, we're also done, after making
14607 sure that a non-default face is extended up to the right
14608 margin of the window. */
14609 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 14610 {
5f5c8ee5
GM
14611 int used_before = row->used[TEXT_AREA];
14612
e74fb0f7
GM
14613 row->ends_in_newline_from_string_p = STRINGP (it->object);
14614
5f5c8ee5
GM
14615 /* Add a space at the end of the line that is used to
14616 display the cursor there. */
14617 append_space (it, 0);
2311178e 14618
5f5c8ee5
GM
14619 /* Extend the face to the end of the line. */
14620 extend_face_to_end_of_line (it);
14621
14622 /* Make sure we have the position. */
14623 if (used_before == 0)
14624 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
2311178e 14625
5f5c8ee5 14626 /* Consume the line end. This skips over invisible lines. */
cafafe0b 14627 set_iterator_to_next (it, 1);
5f5c8ee5
GM
14628 it->continuation_lines_width = 0;
14629 break;
1c9241f5 14630 }
a2889657 14631
2311178e 14632 /* Proceed with next display element. Note that this skips
5f5c8ee5 14633 over lines invisible because of selective display. */
cafafe0b 14634 set_iterator_to_next (it, 1);
b1d1124b 14635
5f5c8ee5
GM
14636 /* If we truncate lines, we are done when the last displayed
14637 glyphs reach past the right margin of the window. */
14638 if (it->truncate_lines_p
14639 && (FRAME_WINDOW_P (it->f)
14640 ? (it->current_x >= it->last_visible_x)
14641 : (it->current_x > it->last_visible_x)))
75d13c64 14642 {
5f5c8ee5
GM
14643 /* Maybe add truncation glyphs. */
14644 if (!FRAME_WINDOW_P (it->f))
14645 {
a98b5ed9 14646 int i, n;
2311178e 14647
a98b5ed9
GM
14648 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14649 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14650 break;
14651
14652 for (n = row->used[TEXT_AREA]; i < n; ++i)
14653 {
14654 row->used[TEXT_AREA] = i;
14655 produce_special_glyphs (it, IT_TRUNCATION);
14656 }
5f5c8ee5 14657 }
2311178e 14658
5f5c8ee5
GM
14659 row->truncated_on_right_p = 1;
14660 it->continuation_lines_width = 0;
312246d1 14661 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
14662 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14663 it->hpos = hpos_before;
14664 it->current_x = x_before;
14665 break;
75d13c64 14666 }
a2889657 14667 }
a2889657 14668
5f5c8ee5
GM
14669 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14670 at the left window margin. */
14671 if (it->first_visible_x
14672 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14673 {
14674 if (!FRAME_WINDOW_P (it->f))
14675 insert_left_trunc_glyphs (it);
14676 row->truncated_on_left_p = 1;
14677 }
a2889657 14678
5f5c8ee5
GM
14679 /* If the start of this line is the overlay arrow-position, then
14680 mark this glyph row as the one containing the overlay arrow.
14681 This is clearly a mess with variable size fonts. It would be
14682 better to let it be displayed like cursors under X. */
e24c997d 14683 if (MARKERP (Voverlay_arrow_position)
a2889657 14684 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
14685 && (MATRIX_ROW_START_CHARPOS (row)
14686 == marker_position (Voverlay_arrow_position))
e24c997d 14687 && STRINGP (Voverlay_arrow_string)
a2889657
JB
14688 && ! overlay_arrow_seen)
14689 {
b46952ae 14690 /* Overlay arrow in window redisplay is a fringe bitmap. */
5f5c8ee5 14691 if (!FRAME_WINDOW_P (it->f))
c4628384 14692 {
5f5c8ee5
GM
14693 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
14694 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14695 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14696 struct glyph *p = row->glyphs[TEXT_AREA];
14697 struct glyph *p2, *end;
14698
14699 /* Copy the arrow glyphs. */
14700 while (glyph < arrow_end)
14701 *p++ = *glyph++;
14702
14703 /* Throw away padding glyphs. */
14704 p2 = p;
14705 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14706 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14707 ++p2;
14708 if (p2 > p)
212e4f87 14709 {
5f5c8ee5
GM
14710 while (p2 < end)
14711 *p++ = *p2++;
14712 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 14713 }
c4628384 14714 }
2311178e 14715
a2889657 14716 overlay_arrow_seen = 1;
5f5c8ee5 14717 row->overlay_arrow_p = 1;
a2889657
JB
14718 }
14719
5f5c8ee5
GM
14720 /* Compute pixel dimensions of this line. */
14721 compute_line_metrics (it);
14722
14723 /* Remember the position at which this line ends. */
14724 row->end = it->current;
14725
173cbca8 14726 /* Maybe set the cursor. */
5f5c8ee5
GM
14727 if (it->w->cursor.vpos < 0
14728 && PT >= MATRIX_ROW_START_CHARPOS (row)
cafafe0b
GM
14729 && PT <= MATRIX_ROW_END_CHARPOS (row)
14730 && cursor_row_p (it->w, row))
14731 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
14732
14733 /* Highlight trailing whitespace. */
8f897821 14734 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
14735 highlight_trailing_whitespace (it->f, it->glyph_row);
14736
14737 /* Prepare for the next line. This line starts horizontally at (X
14738 HPOS) = (0 0). Vertical positions are incremented. As a
14739 convenience for the caller, IT->glyph_row is set to the next
14740 row to be used. */
14741 it->current_x = it->hpos = 0;
14742 it->current_y += row->height;
14743 ++it->vpos;
14744 ++it->glyph_row;
29b3ea63 14745 it->start = it->current;
5f5c8ee5 14746 return row->displays_text_p;
a2889657 14747}
5f5c8ee5
GM
14748
14749
a2889657 14750\f
5f5c8ee5
GM
14751/***********************************************************************
14752 Menu Bar
14753 ***********************************************************************/
14754
14755/* Redisplay the menu bar in the frame for window W.
14756
14757 The menu bar of X frames that don't have X toolkit support is
14758 displayed in a special window W->frame->menu_bar_window.
2311178e 14759
5f5c8ee5
GM
14760 The menu bar of terminal frames is treated specially as far as
14761 glyph matrices are concerned. Menu bar lines are not part of
14762 windows, so the update is done directly on the frame matrix rows
14763 for the menu bar. */
7ce2c095
RS
14764
14765static void
14766display_menu_bar (w)
14767 struct window *w;
14768{
5f5c8ee5
GM
14769 struct frame *f = XFRAME (WINDOW_FRAME (w));
14770 struct it it;
14771 Lisp_Object items;
8351baf2 14772 int i;
7ce2c095 14773
5f5c8ee5 14774 /* Don't do all this for graphical frames. */
dc937613 14775#ifdef HAVE_NTGUI
d129c4c2
KH
14776 if (!NILP (Vwindow_system))
14777 return;
dc937613 14778#endif
488dd4c4 14779#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
d3413a53 14780 if (FRAME_X_P (f))
7ce2c095 14781 return;
5f5c8ee5 14782#endif
e0f712ba 14783#ifdef MAC_OS
1a578e9b
AC
14784 if (FRAME_MAC_P (f))
14785 return;
14786#endif
5f5c8ee5
GM
14787
14788#ifdef USE_X_TOOLKIT
14789 xassert (!FRAME_WINDOW_P (f));
52377a47 14790 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5 14791 it.first_visible_x = 0;
da8b7f4f 14792 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
5f5c8ee5
GM
14793#else /* not USE_X_TOOLKIT */
14794 if (FRAME_WINDOW_P (f))
14795 {
14796 /* Menu bar lines are displayed in the desired matrix of the
14797 dummy window menu_bar_window. */
14798 struct window *menu_w;
14799 xassert (WINDOWP (f->menu_bar_window));
14800 menu_w = XWINDOW (f->menu_bar_window);
14801 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 14802 MENU_FACE_ID);
5f5c8ee5 14803 it.first_visible_x = 0;
da8b7f4f 14804 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
5f5c8ee5
GM
14805 }
14806 else
14807 {
14808 /* This is a TTY frame, i.e. character hpos/vpos are used as
14809 pixel x/y. */
14810 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 14811 MENU_FACE_ID);
5f5c8ee5 14812 it.first_visible_x = 0;
da8b7f4f 14813 it.last_visible_x = FRAME_COLS (f);
5f5c8ee5
GM
14814 }
14815#endif /* not USE_X_TOOLKIT */
14816
1862a24e
MB
14817 if (! mode_line_inverse_video)
14818 /* Force the menu-bar to be displayed in the default face. */
14819 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14820
5f5c8ee5
GM
14821 /* Clear all rows of the menu bar. */
14822 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14823 {
14824 struct glyph_row *row = it.glyph_row + i;
14825 clear_glyph_row (row);
14826 row->enabled_p = 1;
14827 row->full_width_p = 1;
14828 }
7ce2c095 14829
5f5c8ee5
GM
14830 /* Display all items of the menu bar. */
14831 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 14832 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 14833 {
5f5c8ee5
GM
14834 Lisp_Object string;
14835
14836 /* Stop at nil string. */
a61b7058 14837 string = AREF (items, i + 1);
8351baf2
RS
14838 if (NILP (string))
14839 break;
2d66ad19 14840
5f5c8ee5 14841 /* Remember where item was displayed. */
a61b7058 14842 AREF (items, i + 3) = make_number (it.hpos);
7ce2c095 14843
5f5c8ee5
GM
14844 /* Display the item, pad with one space. */
14845 if (it.current_x < it.last_visible_x)
14846 display_string (NULL, string, Qnil, 0, 0, &it,
2051c264 14847 SCHARS (string) + 1, 0, 0, -1);
7ce2c095
RS
14848 }
14849
2d66ad19 14850 /* Fill out the line with spaces. */
5f5c8ee5
GM
14851 if (it.current_x < it.last_visible_x)
14852 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 14853
5f5c8ee5
GM
14854 /* Compute the total height of the lines. */
14855 compute_line_metrics (&it);
7ce2c095 14856}
5f5c8ee5
GM
14857
14858
7ce2c095 14859\f
5f5c8ee5
GM
14860/***********************************************************************
14861 Mode Line
14862 ***********************************************************************/
14863
715e84c9
GM
14864/* Redisplay mode lines in the window tree whose root is WINDOW. If
14865 FORCE is non-zero, redisplay mode lines unconditionally.
14866 Otherwise, redisplay only mode lines that are garbaged. Value is
14867 the number of windows whose mode lines were redisplayed. */
a2889657 14868
715e84c9
GM
14869static int
14870redisplay_mode_lines (window, force)
14871 Lisp_Object window;
14872 int force;
14873{
14874 int nwindows = 0;
2311178e 14875
715e84c9
GM
14876 while (!NILP (window))
14877 {
14878 struct window *w = XWINDOW (window);
2311178e 14879
715e84c9
GM
14880 if (WINDOWP (w->hchild))
14881 nwindows += redisplay_mode_lines (w->hchild, force);
14882 else if (WINDOWP (w->vchild))
14883 nwindows += redisplay_mode_lines (w->vchild, force);
14884 else if (force
14885 || FRAME_GARBAGED_P (XFRAME (w->frame))
14886 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14887 {
715e84c9
GM
14888 struct text_pos lpoint;
14889 struct buffer *old = current_buffer;
14890
14891 /* Set the window's buffer for the mode line display. */
14892 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14893 set_buffer_internal_1 (XBUFFER (w->buffer));
2311178e 14894
715e84c9
GM
14895 /* Point refers normally to the selected window. For any
14896 other window, set up appropriate value. */
14897 if (!EQ (window, selected_window))
14898 {
14899 struct text_pos pt;
2311178e 14900
715e84c9
GM
14901 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
14902 if (CHARPOS (pt) < BEGV)
14903 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14904 else if (CHARPOS (pt) > (ZV - 1))
14905 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
14906 else
14907 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
14908 }
14909
715e84c9
GM
14910 /* Display mode lines. */
14911 clear_glyph_matrix (w->desired_matrix);
14912 if (display_mode_lines (w))
14913 {
14914 ++nwindows;
14915 w->must_be_updated_p = 1;
14916 }
14917
14918 /* Restore old settings. */
715e84c9
GM
14919 set_buffer_internal_1 (old);
14920 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14921 }
14922
14923 window = w->next;
14924 }
14925
14926 return nwindows;
14927}
14928
14929
14930/* Display the mode and/or top line of window W. Value is the number
14931 of mode lines displayed. */
14932
14933static int
5f5c8ee5 14934display_mode_lines (w)
a2889657
JB
14935 struct window *w;
14936{
edd1e654 14937 Lisp_Object old_selected_window, old_selected_frame;
715e84c9 14938 int n = 0;
edd1e654
GM
14939
14940 old_selected_frame = selected_frame;
14941 selected_frame = w->frame;
14942 old_selected_window = selected_window;
14943 XSETWINDOW (selected_window, w);
2311178e 14944
5f5c8ee5 14945 /* These will be set while the mode line specs are processed. */
aa6d10fa 14946 line_number_displayed = 0;
155ef550 14947 w->column_number_displayed = Qnil;
aa6d10fa 14948
5f5c8ee5 14949 if (WINDOW_WANTS_MODELINE_P (w))
715e84c9 14950 {
c96e83bf 14951 struct window *sel_w = XWINDOW (old_selected_window);
f87c0a98 14952
96d2320f 14953 /* Select mode line face based on the real selected window. */
c96e83bf 14954 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
715e84c9
GM
14955 current_buffer->mode_line_format);
14956 ++n;
14957 }
2311178e 14958
045dee35 14959 if (WINDOW_WANTS_HEADER_LINE_P (w))
715e84c9
GM
14960 {
14961 display_mode_line (w, HEADER_LINE_FACE_ID,
14962 current_buffer->header_line_format);
14963 ++n;
14964 }
14965
edd1e654
GM
14966 selected_frame = old_selected_frame;
14967 selected_window = old_selected_window;
715e84c9 14968 return n;
5f5c8ee5 14969}
03b294dc 14970
03b294dc 14971
5f5c8ee5 14972/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 14973 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
04612a64
GM
14974 FORMAT is the mode line format to display. Value is the pixel
14975 height of the mode line displayed. */
03b294dc 14976
04612a64 14977static int
5f5c8ee5
GM
14978display_mode_line (w, face_id, format)
14979 struct window *w;
14980 enum face_id face_id;
14981 Lisp_Object format;
14982{
14983 struct it it;
14984 struct face *face;
03b294dc 14985
5f5c8ee5
GM
14986 init_iterator (&it, w, -1, -1, NULL, face_id);
14987 prepare_desired_row (it.glyph_row);
14988
1862a24e
MB
14989 if (! mode_line_inverse_video)
14990 /* Force the mode-line to be displayed in the default face. */
14991 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14992
5f5c8ee5
GM
14993 /* Temporarily make frame's keyboard the current kboard so that
14994 kboard-local variables in the mode_line_format will get the right
14995 values. */
14996 push_frame_kboard (it.f);
c53a1624 14997 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
5f5c8ee5 14998 pop_frame_kboard ();
a2889657 14999
5f5c8ee5
GM
15000 /* Fill up with spaces. */
15001 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
2311178e 15002
5f5c8ee5
GM
15003 compute_line_metrics (&it);
15004 it.glyph_row->full_width_p = 1;
15005 it.glyph_row->mode_line_p = 1;
5f5c8ee5
GM
15006 it.glyph_row->continued_p = 0;
15007 it.glyph_row->truncated_on_left_p = 0;
15008 it.glyph_row->truncated_on_right_p = 0;
15009
15010 /* Make a 3D mode-line have a shadow at its right end. */
15011 face = FACE_FROM_ID (it.f, face_id);
15012 extend_face_to_end_of_line (&it);
15013 if (face->box != FACE_NO_BOX)
d7eb09a0 15014 {
5f5c8ee5
GM
15015 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15016 + it.glyph_row->used[TEXT_AREA] - 1);
15017 last->right_box_line_p = 1;
d7eb09a0 15018 }
04612a64
GM
15019
15020 return it.glyph_row->height;
a2889657
JB
15021}
15022
0fcf414f
RS
15023/* Alist that caches the results of :propertize.
15024 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15025Lisp_Object mode_line_proptrans_alist;
a2889657 15026
fec8f23e
KS
15027/* List of strings making up the mode-line. */
15028Lisp_Object mode_line_string_list;
15029
15030/* Base face property when building propertized mode line string. */
15031static Lisp_Object mode_line_string_face;
15032static Lisp_Object mode_line_string_face_prop;
15033
15034
5f5c8ee5
GM
15035/* Contribute ELT to the mode line for window IT->w. How it
15036 translates into text depends on its data type.
a2889657 15037
5f5c8ee5 15038 IT describes the display environment in which we display, as usual.
a2889657
JB
15039
15040 DEPTH is the depth in recursion. It is used to prevent
15041 infinite recursion here.
15042
5f5c8ee5
GM
15043 FIELD_WIDTH is the number of characters the display of ELT should
15044 occupy in the mode line, and PRECISION is the maximum number of
15045 characters to display from ELT's representation. See
b8523839 15046 display_string for details.
a2889657 15047
c53a1624
RS
15048 Returns the hpos of the end of the text generated by ELT.
15049
15050 PROPS is a property list to add to any string we encounter.
15051
7d0393cf 15052 If RISKY is nonzero, remove (disregard) any properties in any string
93da8435
SM
15053 we encounter, and ignore :eval and :propertize.
15054
15055 If the global variable `frame_title_ptr' is non-NULL, then the output
15056 is passed to `store_frame_title' instead of `display_string'. */
a2889657
JB
15057
15058static int
c53a1624 15059display_mode_element (it, depth, field_width, precision, elt, props, risky)
5f5c8ee5 15060 struct it *it;
a2889657 15061 int depth;
5f5c8ee5 15062 int field_width, precision;
0fcf414f 15063 Lisp_Object elt, props;
c53a1624 15064 int risky;
a2889657 15065{
5f5c8ee5 15066 int n = 0, field, prec;
0fcf414f 15067 int literal = 0;
5f5c8ee5 15068
a2889657 15069 tail_recurse:
1a89be1e
SM
15070 if (depth > 100)
15071 elt = build_string ("*too-deep*");
a2889657
JB
15072
15073 depth++;
15074
0220c518 15075 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
15076 {
15077 case Lisp_String:
15078 {
15079 /* A string: output it and check for %-constructs within it. */
5f5c8ee5 15080 unsigned char c;
50f80c2f 15081 const unsigned char *this, *lisp_string;
5f5c8ee5 15082
c53a1624 15083 if (!NILP (props) || risky)
0fcf414f
RS
15084 {
15085 Lisp_Object oprops, aelt;
15086 oprops = Ftext_properties_at (make_number (0), elt);
c53a1624
RS
15087
15088 if (NILP (Fequal (props, oprops)) || risky)
0fcf414f 15089 {
ae02e06a
RS
15090 /* If the starting string has properties,
15091 merge the specified ones onto the existing ones. */
c53a1624 15092 if (! NILP (oprops) && !risky)
ae02e06a
RS
15093 {
15094 Lisp_Object tem;
15095
15096 oprops = Fcopy_sequence (oprops);
15097 tem = props;
15098 while (CONSP (tem))
15099 {
15100 oprops = Fplist_put (oprops, XCAR (tem),
15101 XCAR (XCDR (tem)));
15102 tem = XCDR (XCDR (tem));
15103 }
15104 props = oprops;
15105 }
15106
0fcf414f
RS
15107 aelt = Fassoc (elt, mode_line_proptrans_alist);
15108 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
adb63af1
RS
15109 {
15110 mode_line_proptrans_alist
15111 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15112 elt = XCAR (aelt);
15113 }
0fcf414f
RS
15114 else
15115 {
adb63af1
RS
15116 Lisp_Object tem;
15117
0fcf414f 15118 elt = Fcopy_sequence (elt);
dc3b2c8b
SM
15119 Fset_text_properties (make_number (0), Flength (elt),
15120 props, elt);
adb63af1 15121 /* Add this item to mode_line_proptrans_alist. */
0fcf414f
RS
15122 mode_line_proptrans_alist
15123 = Fcons (Fcons (elt, props),
15124 mode_line_proptrans_alist);
adb63af1
RS
15125 /* Truncate mode_line_proptrans_alist
15126 to at most 50 elements. */
15127 tem = Fnthcdr (make_number (50),
15128 mode_line_proptrans_alist);
15129 if (! NILP (tem))
15130 XSETCDR (tem, Qnil);
0fcf414f
RS
15131 }
15132 }
15133 }
15134
2051c264 15135 this = SDATA (elt);
ae02e06a
RS
15136 lisp_string = this;
15137
0fcf414f
RS
15138 if (literal)
15139 {
15140 prec = precision - n;
15141 if (frame_title_ptr)
2051c264 15142 n += store_frame_title (SDATA (elt), -1, prec);
fec8f23e
KS
15143 else if (!NILP (mode_line_string_list))
15144 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
0fcf414f
RS
15145 else
15146 n += display_string (NULL, elt, Qnil, 0, 0, it,
15147 0, prec, 0, STRING_MULTIBYTE (elt));
15148
15149 break;
15150 }
15151
5f5c8ee5
GM
15152 while ((precision <= 0 || n < precision)
15153 && *this
15154 && (frame_title_ptr
fec8f23e 15155 || !NILP (mode_line_string_list)
5f5c8ee5 15156 || it->current_x < it->last_visible_x))
a2889657 15157 {
50f80c2f 15158 const unsigned char *last = this;
5f5c8ee5
GM
15159
15160 /* Advance to end of string or next format specifier. */
a2889657
JB
15161 while ((c = *this++) != '\0' && c != '%')
15162 ;
2311178e 15163
a2889657
JB
15164 if (this - 1 != last)
15165 {
5f5c8ee5
GM
15166 /* Output to end of string or up to '%'. Field width
15167 is length of string. Don't output more than
15168 PRECISION allows us. */
d26b89b8 15169 --this;
eaaa65b0
GM
15170
15171 prec = chars_in_text (last, this - last);
5f5c8ee5
GM
15172 if (precision > 0 && prec > precision - n)
15173 prec = precision - n;
2311178e 15174
d39b6696 15175 if (frame_title_ptr)
d26b89b8 15176 n += store_frame_title (last, 0, prec);
fec8f23e
KS
15177 else if (!NILP (mode_line_string_list))
15178 {
15179 int bytepos = last - lisp_string;
15180 int charpos = string_byte_to_char (elt, bytepos);
15181 n += store_mode_line_string (NULL,
15182 Fsubstring (elt, make_number (charpos),
15183 make_number (charpos + prec)),
15184 0, 0, 0, Qnil);
15185 }
d39b6696 15186 else
eaaa65b0
GM
15187 {
15188 int bytepos = last - lisp_string;
15189 int charpos = string_byte_to_char (elt, bytepos);
15190 n += display_string (NULL, elt, Qnil, 0, charpos,
ca77144d
GM
15191 it, 0, prec, 0,
15192 STRING_MULTIBYTE (elt));
eaaa65b0 15193 }
a2889657
JB
15194 }
15195 else /* c == '%' */
15196 {
50f80c2f 15197 const unsigned char *percent_position = this;
2311178e 15198
5f5c8ee5
GM
15199 /* Get the specified minimum width. Zero means
15200 don't pad. */
15201 field = 0;
a2889657 15202 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 15203 field = field * 10 + c - '0';
a2889657 15204
5f5c8ee5
GM
15205 /* Don't pad beyond the total padding allowed. */
15206 if (field_width - n > 0 && field > field_width - n)
15207 field = field_width - n;
a2889657 15208
5f5c8ee5
GM
15209 /* Note that either PRECISION <= 0 or N < PRECISION. */
15210 prec = precision - n;
2311178e 15211
a2889657 15212 if (c == 'M')
5f5c8ee5 15213 n += display_mode_element (it, depth, field, prec,
c53a1624
RS
15214 Vglobal_mode_string, props,
15215 risky);
a2889657 15216 else if (c != 0)
d39b6696 15217 {
72f62cb5 15218 int multibyte;
ae02e06a
RS
15219 int bytepos, charpos;
15220 unsigned char *spec;
2311178e 15221
ae02e06a
RS
15222 bytepos = percent_position - lisp_string;
15223 charpos = (STRING_MULTIBYTE (elt)
15224 ? string_byte_to_char (elt, bytepos)
15225 : bytepos);
15226
15227 spec
72f62cb5
GM
15228 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15229
d39b6696 15230 if (frame_title_ptr)
5f5c8ee5 15231 n += store_frame_title (spec, field, prec);
fec8f23e
KS
15232 else if (!NILP (mode_line_string_list))
15233 {
15234 int len = strlen (spec);
15235 Lisp_Object tem = make_string (spec, len);
15236 props = Ftext_properties_at (make_number (charpos), elt);
15237 /* Should only keep face property in props */
15238 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15239 }
d39b6696 15240 else
5f5c8ee5 15241 {
ae02e06a 15242 int nglyphs_before, nwritten;
2311178e 15243
72f62cb5 15244 nglyphs_before = it->glyph_row->used[TEXT_AREA];
72f62cb5
GM
15245 nwritten = display_string (spec, Qnil, elt,
15246 charpos, 0, it,
15247 field, prec, 0,
15248 multibyte);
5f5c8ee5
GM
15249
15250 /* Assign to the glyphs written above the
15251 string where the `%x' came from, position
15252 of the `%'. */
15253 if (nwritten > 0)
15254 {
15255 struct glyph *glyph
15256 = (it->glyph_row->glyphs[TEXT_AREA]
15257 + nglyphs_before);
15258 int i;
15259
15260 for (i = 0; i < nwritten; ++i)
15261 {
15262 glyph[i].object = elt;
15263 glyph[i].charpos = charpos;
15264 }
2311178e 15265
5f5c8ee5
GM
15266 n += nwritten;
15267 }
15268 }
d39b6696 15269 }
b8523839
AS
15270 else /* c == 0 */
15271 break;
a2889657
JB
15272 }
15273 }
15274 }
15275 break;
15276
15277 case Lisp_Symbol:
15278 /* A symbol: process the value of the symbol recursively
15279 as if it appeared here directly. Avoid error if symbol void.
15280 Special case: if value of symbol is a string, output the string
15281 literally. */
15282 {
15283 register Lisp_Object tem;
c53a1624
RS
15284
15285 /* If the variable is not marked as risky to set
15286 then its contents are risky to use. */
15287 if (NILP (Fget (elt, Qrisky_local_variable)))
15288 risky = 1;
15289
a2889657 15290 tem = Fboundp (elt);
265a9e55 15291 if (!NILP (tem))
a2889657
JB
15292 {
15293 tem = Fsymbol_value (elt);
15294 /* If value is a string, output that string literally:
15295 don't check for % within it. */
e24c997d 15296 if (STRINGP (tem))
0fcf414f
RS
15297 literal = 1;
15298
15299 if (!EQ (tem, elt))
5f5c8ee5
GM
15300 {
15301 /* Give up right away for nil or t. */
15302 elt = tem;
15303 goto tail_recurse;
15304 }
a2889657
JB
15305 }
15306 }
15307 break;
15308
15309 case Lisp_Cons:
15310 {
15311 register Lisp_Object car, tem;
15312
0fcf414f
RS
15313 /* A cons cell: five distinct cases.
15314 If first element is :eval or :propertize, do something special.
a2889657
JB
15315 If first element is a string or a cons, process all the elements
15316 and effectively concatenate them.
15317 If first element is a negative number, truncate displaying cdr to
15318 at most that many characters. If positive, pad (with spaces)
15319 to at least that many characters.
15320 If first element is a symbol, process the cadr or caddr recursively
15321 according to whether the symbol's value is non-nil or nil. */
9472f927 15322 car = XCAR (elt);
0fcf414f 15323 if (EQ (car, QCeval))
5f5c8ee5
GM
15324 {
15325 /* An element of the form (:eval FORM) means evaluate FORM
15326 and use the result as mode line elements. */
0fcf414f 15327
c53a1624
RS
15328 if (risky)
15329 break;
15330
0fcf414f
RS
15331 if (CONSP (XCDR (elt)))
15332 {
15333 Lisp_Object spec;
15334 spec = safe_eval (XCAR (XCDR (elt)));
15335 n += display_mode_element (it, depth, field_width - n,
c53a1624
RS
15336 precision - n, spec, props,
15337 risky);
0fcf414f
RS
15338 }
15339 }
15340 else if (EQ (car, QCpropertize))
15341 {
c53a1624
RS
15342 /* An element of the form (:propertize ELT PROPS...)
15343 means display ELT but applying properties PROPS. */
15344
15345 if (risky)
15346 break;
15347
0fcf414f 15348 if (CONSP (XCDR (elt)))
c53a1624
RS
15349 n += display_mode_element (it, depth, field_width - n,
15350 precision - n, XCAR (XCDR (elt)),
15351 XCDR (XCDR (elt)), risky);
5f5c8ee5
GM
15352 }
15353 else if (SYMBOLP (car))
a2889657
JB
15354 {
15355 tem = Fboundp (car);
9472f927 15356 elt = XCDR (elt);
e24c997d 15357 if (!CONSP (elt))
a2889657
JB
15358 goto invalid;
15359 /* elt is now the cdr, and we know it is a cons cell.
15360 Use its car if CAR has a non-nil value. */
265a9e55 15361 if (!NILP (tem))
a2889657
JB
15362 {
15363 tem = Fsymbol_value (car);
265a9e55 15364 if (!NILP (tem))
9472f927
GM
15365 {
15366 elt = XCAR (elt);
15367 goto tail_recurse;
15368 }
a2889657
JB
15369 }
15370 /* Symbol's value is nil (or symbol is unbound)
15371 Get the cddr of the original list
15372 and if possible find the caddr and use that. */
9472f927 15373 elt = XCDR (elt);
265a9e55 15374 if (NILP (elt))
a2889657 15375 break;
e24c997d 15376 else if (!CONSP (elt))
a2889657 15377 goto invalid;
9472f927 15378 elt = XCAR (elt);
a2889657
JB
15379 goto tail_recurse;
15380 }
e24c997d 15381 else if (INTEGERP (car))
a2889657
JB
15382 {
15383 register int lim = XINT (car);
9472f927 15384 elt = XCDR (elt);
a2889657 15385 if (lim < 0)
5f5c8ee5
GM
15386 {
15387 /* Negative int means reduce maximum width. */
15388 if (precision <= 0)
15389 precision = -lim;
15390 else
15391 precision = min (precision, -lim);
15392 }
a2889657
JB
15393 else if (lim > 0)
15394 {
15395 /* Padding specified. Don't let it be more than
15396 current maximum. */
5f5c8ee5
GM
15397 if (precision > 0)
15398 lim = min (precision, lim);
15399
a2889657
JB
15400 /* If that's more padding than already wanted, queue it.
15401 But don't reduce padding already specified even if
15402 that is beyond the current truncation point. */
5f5c8ee5 15403 field_width = max (lim, field_width);
a2889657
JB
15404 }
15405 goto tail_recurse;
15406 }
e24c997d 15407 else if (STRINGP (car) || CONSP (car))
a2889657
JB
15408 {
15409 register int limit = 50;
5f5c8ee5
GM
15410 /* Limit is to protect against circular lists. */
15411 while (CONSP (elt)
15412 && --limit > 0
15413 && (precision <= 0 || n < precision))
a2889657 15414 {
5f5c8ee5 15415 n += display_mode_element (it, depth, field_width - n,
c53a1624
RS
15416 precision - n, XCAR (elt),
15417 props, risky);
9472f927 15418 elt = XCDR (elt);
a2889657
JB
15419 }
15420 }
15421 }
15422 break;
15423
15424 default:
15425 invalid:
1a89be1e
SM
15426 elt = build_string ("*invalid*");
15427 goto tail_recurse;
a2889657
JB
15428 }
15429
5f5c8ee5
GM
15430 /* Pad to FIELD_WIDTH. */
15431 if (field_width > 0 && n < field_width)
15432 {
15433 if (frame_title_ptr)
15434 n += store_frame_title ("", field_width - n, 0);
fec8f23e
KS
15435 else if (!NILP (mode_line_string_list))
15436 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
5f5c8ee5
GM
15437 else
15438 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15439 0, 0, 0);
15440 }
2311178e 15441
5f5c8ee5 15442 return n;
a2889657 15443}
5f5c8ee5 15444
fec8f23e
KS
15445/* Store a mode-line string element in mode_line_string_list.
15446
15447 If STRING is non-null, display that C string. Otherwise, the Lisp
15448 string LISP_STRING is displayed.
15449
15450 FIELD_WIDTH is the minimum number of output glyphs to produce.
15451 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15452 with spaces. FIELD_WIDTH <= 0 means don't pad.
15453
15454 PRECISION is the maximum number of characters to output from
15455 STRING. PRECISION <= 0 means don't truncate the string.
15456
15457 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15458 properties to the string.
15459
7d0393cf 15460 PROPS are the properties to add to the string.
fec8f23e
KS
15461 The mode_line_string_face face property is always added to the string.
15462 */
15463
15464static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15465 char *string;
15466 Lisp_Object lisp_string;
15467 int copy_string;
15468 int field_width;
15469 int precision;
15470 Lisp_Object props;
15471{
15472 int len;
15473 int n = 0;
15474
15475 if (string != NULL)
15476 {
15477 len = strlen (string);
15478 if (precision > 0 && len > precision)
15479 len = precision;
15480 lisp_string = make_string (string, len);
15481 if (NILP (props))
15482 props = mode_line_string_face_prop;
15483 else if (!NILP (mode_line_string_face))
15484 {
15485 Lisp_Object face = Fplist_get (props, Qface);
15486 props = Fcopy_sequence (props);
15487 if (NILP (face))
15488 face = mode_line_string_face;
15489 else
15490 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15491 props = Fplist_put (props, Qface, face);
15492 }
15493 Fadd_text_properties (make_number (0), make_number (len),
15494 props, lisp_string);
15495 }
7d0393cf 15496 else
fec8f23e 15497 {
c8224325 15498 len = XFASTINT (Flength (lisp_string));
fec8f23e
KS
15499 if (precision > 0 && len > precision)
15500 {
15501 len = precision;
15502 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15503 precision = -1;
15504 }
15505 if (!NILP (mode_line_string_face))
15506 {
15507 Lisp_Object face;
15508 if (NILP (props))
15509 props = Ftext_properties_at (make_number (0), lisp_string);
15510 face = Fplist_get (props, Qface);
15511 if (NILP (face))
15512 face = mode_line_string_face;
15513 else
15514 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15515 props = Fcons (Qface, Fcons (face, Qnil));
15516 if (copy_string)
15517 lisp_string = Fcopy_sequence (lisp_string);
15518 }
15519 if (!NILP (props))
15520 Fadd_text_properties (make_number (0), make_number (len),
15521 props, lisp_string);
15522 }
15523
15524 if (len > 0)
15525 {
15526 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
7d0393cf 15527 n += len;
fec8f23e
KS
15528 }
15529
15530 if (field_width > len)
15531 {
15532 field_width -= len;
15533 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15534 if (!NILP (props))
15535 Fadd_text_properties (make_number (0), make_number (field_width),
15536 props, lisp_string);
15537 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
7d0393cf 15538 n += field_width;
fec8f23e
KS
15539 }
15540
15541 return n;
15542}
15543
5f5c8ee5 15544
8143e6ab 15545DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
fec8f23e 15546 0, 3, 0,
8143e6ab
KS
15547 doc: /* Return the mode-line of selected window as a string.
15548First optional arg FORMAT specifies a different format string (see
8b22c65a 15549`mode-line-format' for details) to use. If FORMAT is t, return
8143e6ab 15550the buffer's header-line. Second optional arg WINDOW specifies a
fec8f23e
KS
15551different window to use as the context for the formatting.
15552If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15553 (format, window, no_props)
15554 Lisp_Object format, window, no_props;
8143e6ab
KS
15555{
15556 struct it it;
8143e6ab
KS
15557 int len;
15558 struct window *w;
15559 struct buffer *old_buffer = NULL;
fec8f23e 15560 enum face_id face_id = DEFAULT_FACE_ID;
8143e6ab
KS
15561
15562 if (NILP (window))
15563 window = selected_window;
15564 CHECK_WINDOW (window);
15565 w = XWINDOW (window);
15566 CHECK_BUFFER (w->buffer);
15567
15568 if (XBUFFER (w->buffer) != current_buffer)
15569 {
15570 old_buffer = current_buffer;
15571 set_buffer_internal_1 (XBUFFER (w->buffer));
15572 }
15573
15574 if (NILP (format) || EQ (format, Qt))
fec8f23e
KS
15575 {
15576 face_id = NILP (format)
15577 ? CURRENT_MODE_LINE_FACE_ID (w) :
15578 HEADER_LINE_FACE_ID;
15579 format = NILP (format)
15580 ? current_buffer->mode_line_format
15581 : current_buffer->header_line_format;
15582 }
15583
15584 init_iterator (&it, w, -1, -1, NULL, face_id);
8143e6ab 15585
fec8f23e
KS
15586 if (NILP (no_props))
15587 {
15588 mode_line_string_face =
15589 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15590 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15591 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
8143e6ab 15592
fec8f23e
KS
15593 mode_line_string_face_prop =
15594 NILP (mode_line_string_face) ? Qnil :
15595 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15596
15597 /* We need a dummy last element in mode_line_string_list to
15598 indicate we are building the propertized mode-line string.
15599 Using mode_line_string_face_prop here GC protects it. */
7d0393cf 15600 mode_line_string_list =
fec8f23e
KS
15601 Fcons (mode_line_string_face_prop, Qnil);
15602 frame_title_ptr = NULL;
15603 }
15604 else
15605 {
15606 mode_line_string_face_prop = Qnil;
15607 mode_line_string_list = Qnil;
15608 frame_title_ptr = frame_title_buf;
15609 }
8143e6ab
KS
15610
15611 push_frame_kboard (it.f);
15612 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15613 pop_frame_kboard ();
15614
15615 if (old_buffer)
15616 set_buffer_internal_1 (old_buffer);
15617
fec8f23e
KS
15618 if (NILP (no_props))
15619 {
15620 Lisp_Object str;
15621 mode_line_string_list = Fnreverse (mode_line_string_list);
15622 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15623 make_string ("", 0));
15624 mode_line_string_face_prop = Qnil;
15625 mode_line_string_list = Qnil;
15626 return str;
15627 }
15628
8143e6ab
KS
15629 len = frame_title_ptr - frame_title_buf;
15630 if (len > 0 && frame_title_ptr[-1] == '-')
15631 {
15632 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15633 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15634 ;
15635 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15636 if (len > frame_title_ptr - frame_title_buf)
15637 len = frame_title_ptr - frame_title_buf;
15638 }
15639
15640 frame_title_ptr = NULL;
15641 return make_string (frame_title_buf, len);
15642}
15643
766525bc
RS
15644/* Write a null-terminated, right justified decimal representation of
15645 the positive integer D to BUF using a minimal field width WIDTH. */
15646
15647static void
15648pint2str (buf, width, d)
15649 register char *buf;
15650 register int width;
15651 register int d;
15652{
15653 register char *p = buf;
2311178e 15654
766525bc 15655 if (d <= 0)
5f5c8ee5 15656 *p++ = '0';
766525bc 15657 else
5f5c8ee5 15658 {
766525bc 15659 while (d > 0)
5f5c8ee5 15660 {
766525bc
RS
15661 *p++ = d % 10 + '0';
15662 d /= 10;
5f5c8ee5
GM
15663 }
15664 }
2311178e 15665
5f5c8ee5
GM
15666 for (width -= (int) (p - buf); width > 0; --width)
15667 *p++ = ' ';
766525bc
RS
15668 *p-- = '\0';
15669 while (p > buf)
5f5c8ee5 15670 {
766525bc
RS
15671 d = *buf;
15672 *buf++ = *p;
15673 *p-- = d;
5f5c8ee5 15674 }
766525bc
RS
15675}
15676
525b8b09
LK
15677/* Write a null-terminated, right justified decimal and "human
15678 readable" representation of the nonnegative integer D to BUF using
15679 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15680
15681static const char power_letter[] =
15682 {
15683 0, /* not used */
15684 'k', /* kilo */
15685 'M', /* mega */
15686 'G', /* giga */
15687 'T', /* tera */
15688 'P', /* peta */
15689 'E', /* exa */
15690 'Z', /* zetta */
15691 'Y' /* yotta */
15692 };
15693
15694static void
15695pint2hrstr (buf, width, d)
15696 char *buf;
15697 int width;
15698 int d;
15699{
15700 /* We aim to represent the nonnegative integer D as
15701 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15702 int quotient = d;
15703 int remainder = 0;
15704 /* -1 means: do not use TENTHS. */
15705 int tenths = -1;
15706 int exponent = 0;
15707
15708 /* Length of QUOTIENT.TENTHS as a string. */
15709 int length;
15710
15711 char * psuffix;
15712 char * p;
15713
15714 if (1000 <= quotient)
15715 {
15716 /* Scale to the appropriate EXPONENT. */
15717 do
15718 {
15719 remainder = quotient % 1000;
15720 quotient /= 1000;
15721 exponent++;
15722 }
15723 while (1000 <= quotient);
15724
15725 /* Round to nearest and decide whether to use TENTHS or not. */
15726 if (quotient <= 9)
15727 {
15728 tenths = remainder / 100;
15729 if (50 <= remainder % 100)
15730 if (tenths < 9)
15731 tenths++;
15732 else
15733 {
15734 quotient++;
15735 if (quotient == 10)
15736 tenths = -1;
15737 else
15738 tenths = 0;
15739 }
15740 }
15741 else
15742 if (500 <= remainder)
15743 if (quotient < 999)
15744 quotient++;
15745 else
15746 {
15747 quotient = 1;
15748 exponent++;
15749 tenths = 0;
15750 }
15751 }
15752
15753 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
15754 if (tenths == -1 && quotient <= 99)
15755 if (quotient <= 9)
15756 length = 1;
15757 else
15758 length = 2;
15759 else
15760 length = 3;
15761 p = psuffix = buf + max (width, length);
15762
15763 /* Print EXPONENT. */
15764 if (exponent)
15765 *psuffix++ = power_letter[exponent];
15766 *psuffix = '\0';
15767
15768 /* Print TENTHS. */
15769 if (tenths >= 0)
15770 {
15771 *--p = '0' + tenths;
15772 *--p = '.';
15773 }
15774
15775 /* Print QUOTIENT. */
15776 do
15777 {
15778 int digit = quotient % 10;
15779 *--p = '0' + digit;
15780 }
15781 while ((quotient /= 10) != 0);
15782
15783 /* Print leading spaces. */
15784 while (buf < p)
15785 *--p = ' ';
15786}
15787
5f5c8ee5 15788/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
15789 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15790 type of CODING_SYSTEM. Return updated pointer into BUF. */
15791
6693a99a 15792static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 15793
1c9241f5
KH
15794static char *
15795decode_mode_spec_coding (coding_system, buf, eol_flag)
15796 Lisp_Object coding_system;
15797 register char *buf;
15798 int eol_flag;
15799{
1e1078d6 15800 Lisp_Object val;
916848d8 15801 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
50f80c2f 15802 const unsigned char *eol_str;
302f2b38
EZ
15803 int eol_str_len;
15804 /* The EOL conversion we are using. */
15805 Lisp_Object eoltype;
1e1078d6 15806
4a09dee0 15807 val = Fget (coding_system, Qcoding_system);
085536c2 15808 eoltype = Qnil;
1c9241f5 15809
4a09dee0 15810 if (!VECTORP (val)) /* Not yet decided. */
1c9241f5 15811 {
916848d8
RS
15812 if (multibyte)
15813 *buf++ = '-';
21e989e3 15814 if (eol_flag)
302f2b38 15815 eoltype = eol_mnemonic_undecided;
1e1078d6 15816 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
15817 }
15818 else
15819 {
1e1078d6
RS
15820 Lisp_Object eolvalue;
15821
15822 eolvalue = Fget (coding_system, Qeol_type);
15823
916848d8 15824 if (multibyte)
a61b7058 15825 *buf++ = XFASTINT (AREF (val, 1));
916848d8 15826
1c9241f5
KH
15827 if (eol_flag)
15828 {
1e1078d6
RS
15829 /* The EOL conversion that is normal on this system. */
15830
15831 if (NILP (eolvalue)) /* Not yet decided. */
15832 eoltype = eol_mnemonic_undecided;
15833 else if (VECTORP (eolvalue)) /* Not yet decided. */
15834 eoltype = eol_mnemonic_undecided;
15835 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15836 eoltype = (XFASTINT (eolvalue) == 0
15837 ? eol_mnemonic_unix
15838 : (XFASTINT (eolvalue) == 1
15839 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
15840 }
15841 }
2311178e 15842
302f2b38
EZ
15843 if (eol_flag)
15844 {
15845 /* Mention the EOL conversion if it is not the usual one. */
15846 if (STRINGP (eoltype))
15847 {
2051c264
GM
15848 eol_str = SDATA (eoltype);
15849 eol_str_len = SBYTES (eoltype);
302f2b38 15850 }
f30b3499
KH
15851 else if (INTEGERP (eoltype)
15852 && CHAR_VALID_P (XINT (eoltype), 0))
15853 {
50f80c2f
KR
15854 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
15855 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
15856 eol_str = tmp;
f30b3499 15857 }
302f2b38
EZ
15858 else
15859 {
15860 eol_str = invalid_eol_type;
15861 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 15862 }
f30b3499 15863 bcopy (eol_str, buf, eol_str_len);
302f2b38 15864 buf += eol_str_len;
1c9241f5 15865 }
302f2b38 15866
1c9241f5
KH
15867 return buf;
15868}
15869
a2889657 15870/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
15871 generated by character C. PRECISION >= 0 means don't return a
15872 string longer than that value. FIELD_WIDTH > 0 means pad the
72f62cb5
GM
15873 string returned with spaces to that value. Return 1 in *MULTIBYTE
15874 if the result is multibyte text. */
a2889657 15875
11e82b76
JB
15876static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
15877
a2889657 15878static char *
72f62cb5 15879decode_mode_spec (w, c, field_width, precision, multibyte)
a2889657 15880 struct window *w;
68c45bf0 15881 register int c;
5f5c8ee5 15882 int field_width, precision;
72f62cb5 15883 int *multibyte;
a2889657 15884{
0b67772d 15885 Lisp_Object obj;
5f5c8ee5
GM
15886 struct frame *f = XFRAME (WINDOW_FRAME (w));
15887 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 15888 struct buffer *b = XBUFFER (w->buffer);
a2889657 15889
0b67772d 15890 obj = Qnil;
72f62cb5 15891 *multibyte = 0;
a2889657
JB
15892
15893 switch (c)
15894 {
1af9f229
RS
15895 case '*':
15896 if (!NILP (b->read_only))
15897 return "%";
15898 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15899 return "*";
15900 return "-";
15901
15902 case '+':
15903 /* This differs from %* only for a modified read-only buffer. */
15904 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15905 return "*";
15906 if (!NILP (b->read_only))
15907 return "%";
15908 return "-";
15909
15910 case '&':
15911 /* This differs from %* in ignoring read-only-ness. */
15912 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15913 return "*";
15914 return "-";
15915
15916 case '%':
15917 return "%";
15918
2311178e 15919 case '[':
1af9f229
RS
15920 {
15921 int i;
15922 char *p;
15923
15924 if (command_loop_level > 5)
15925 return "[[[... ";
15926 p = decode_mode_spec_buf;
15927 for (i = 0; i < command_loop_level; i++)
15928 *p++ = '[';
15929 *p = 0;
15930 return decode_mode_spec_buf;
15931 }
15932
2311178e 15933 case ']':
1af9f229
RS
15934 {
15935 int i;
15936 char *p;
15937
15938 if (command_loop_level > 5)
15939 return " ...]]]";
15940 p = decode_mode_spec_buf;
15941 for (i = 0; i < command_loop_level; i++)
15942 *p++ = ']';
15943 *p = 0;
15944 return decode_mode_spec_buf;
15945 }
15946
15947 case '-':
15948 {
1af9f229 15949 register int i;
5f5c8ee5
GM
15950
15951 /* Let lots_of_dashes be a string of infinite length. */
fec8f23e
KS
15952 if (!NILP (mode_line_string_list))
15953 return "--";
5f5c8ee5
GM
15954 if (field_width <= 0
15955 || field_width > sizeof (lots_of_dashes))
1af9f229 15956 {
5f5c8ee5
GM
15957 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
15958 decode_mode_spec_buf[i] = '-';
15959 decode_mode_spec_buf[i] = '\0';
15960 return decode_mode_spec_buf;
1af9f229 15961 }
5f5c8ee5
GM
15962 else
15963 return lots_of_dashes;
1af9f229
RS
15964 }
15965
2311178e 15966 case 'b':
d39b6696 15967 obj = b->name;
a2889657
JB
15968 break;
15969
1af9f229
RS
15970 case 'c':
15971 {
2311178e 15972 int col = (int) current_column (); /* iftc */
ac90c44f 15973 w->column_number_displayed = make_number (col);
5f5c8ee5 15974 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
15975 return decode_mode_spec_buf;
15976 }
15977
15978 case 'F':
15979 /* %F displays the frame name. */
5f5c8ee5 15980 if (!NILP (f->title))
2051c264 15981 return (char *) SDATA (f->title);
fd8ff63d 15982 if (f->explicit_name || ! FRAME_WINDOW_P (f))
2051c264 15983 return (char *) SDATA (f->name);
9c6da96f 15984 return "Emacs";
1af9f229 15985
2311178e 15986 case 'f':
d39b6696 15987 obj = b->filename;
a2889657
JB
15988 break;
15989
525b8b09
LK
15990 case 'i':
15991 {
15992 int size = ZV - BEGV;
15993 pint2str (decode_mode_spec_buf, field_width, size);
15994 return decode_mode_spec_buf;
15995 }
15996
15997 case 'I':
15998 {
15999 int size = ZV - BEGV;
16000 pint2hrstr (decode_mode_spec_buf, field_width, size);
16001 return decode_mode_spec_buf;
16002 }
16003
aa6d10fa
RS
16004 case 'l':
16005 {
12adba34
RS
16006 int startpos = XMARKER (w->start)->charpos;
16007 int startpos_byte = marker_byte_position (w->start);
16008 int line, linepos, linepos_byte, topline;
aa6d10fa 16009 int nlines, junk;
da8b7f4f 16010 int height = WINDOW_TOTAL_LINES (w);
aa6d10fa 16011
2311178e 16012 /* If we decided that this buffer isn't suitable for line numbers,
aa6d10fa
RS
16013 don't forget that too fast. */
16014 if (EQ (w->base_line_pos, w->buffer))
766525bc 16015 goto no_value;
5300fd39
RS
16016 /* But do forget it, if the window shows a different buffer now. */
16017 else if (BUFFERP (w->base_line_pos))
16018 w->base_line_pos = Qnil;
aa6d10fa
RS
16019
16020 /* If the buffer is very big, don't waste time. */
37d034d3 16021 if (INTEGERP (Vline_number_display_limit)
090703f4 16022 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
aa6d10fa
RS
16023 {
16024 w->base_line_pos = Qnil;
16025 w->base_line_number = Qnil;
766525bc 16026 goto no_value;
aa6d10fa
RS
16027 }
16028
16029 if (!NILP (w->base_line_number)
16030 && !NILP (w->base_line_pos)
12adba34 16031 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
16032 {
16033 line = XFASTINT (w->base_line_number);
16034 linepos = XFASTINT (w->base_line_pos);
12adba34 16035 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
16036 }
16037 else
16038 {
16039 line = 1;
d39b6696 16040 linepos = BUF_BEGV (b);
12adba34 16041 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
16042 }
16043
16044 /* Count lines from base line to window start position. */
12adba34
RS
16045 nlines = display_count_lines (linepos, linepos_byte,
16046 startpos_byte,
16047 startpos, &junk);
aa6d10fa
RS
16048
16049 topline = nlines + line;
16050
16051 /* Determine a new base line, if the old one is too close
16052 or too far away, or if we did not have one.
16053 "Too close" means it's plausible a scroll-down would
16054 go back past it. */
d39b6696 16055 if (startpos == BUF_BEGV (b))
aa6d10fa 16056 {
ac90c44f
GM
16057 w->base_line_number = make_number (topline);
16058 w->base_line_pos = make_number (BUF_BEGV (b));
aa6d10fa
RS
16059 }
16060 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 16061 || linepos == BUF_BEGV (b))
aa6d10fa 16062 {
d39b6696 16063 int limit = BUF_BEGV (b);
12adba34 16064 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 16065 int position;
5d121aec 16066 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
16067
16068 if (startpos - distance > limit)
12adba34
RS
16069 {
16070 limit = startpos - distance;
16071 limit_byte = CHAR_TO_BYTE (limit);
16072 }
aa6d10fa 16073
12adba34
RS
16074 nlines = display_count_lines (startpos, startpos_byte,
16075 limit_byte,
16076 - (height * 2 + 30),
aa6d10fa 16077 &position);
2311178e 16078 /* If we couldn't find the lines we wanted within
5d121aec 16079 line_number_display_limit_width chars per line,
aa6d10fa 16080 give up on line numbers for this window. */
12adba34 16081 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
16082 {
16083 w->base_line_pos = w->buffer;
16084 w->base_line_number = Qnil;
766525bc 16085 goto no_value;
aa6d10fa
RS
16086 }
16087
ac90c44f
GM
16088 w->base_line_number = make_number (topline - nlines);
16089 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
aa6d10fa
RS
16090 }
16091
16092 /* Now count lines from the start pos to point. */
12adba34
RS
16093 nlines = display_count_lines (startpos, startpos_byte,
16094 PT_BYTE, PT, &junk);
aa6d10fa
RS
16095
16096 /* Record that we did display the line number. */
16097 line_number_displayed = 1;
16098
16099 /* Make the string to show. */
5f5c8ee5 16100 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 16101 return decode_mode_spec_buf;
766525bc
RS
16102 no_value:
16103 {
16104 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
16105 int pad = field_width - 2;
16106 while (pad-- > 0)
16107 *p++ = ' ';
16108 *p++ = '?';
b357b9d4
KR
16109 *p++ = '?';
16110 *p = '\0';
766525bc
RS
16111 return decode_mode_spec_buf;
16112 }
aa6d10fa
RS
16113 }
16114 break;
16115
2311178e 16116 case 'm':
d39b6696 16117 obj = b->mode_name;
a2889657
JB
16118 break;
16119
16120 case 'n':
d39b6696 16121 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
16122 return " Narrow";
16123 break;
16124
a2889657
JB
16125 case 'p':
16126 {
16127 int pos = marker_position (w->start);
d39b6696 16128 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 16129
d39b6696 16130 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 16131 {
d39b6696 16132 if (pos <= BUF_BEGV (b))
a2889657
JB
16133 return "All";
16134 else
16135 return "Bottom";
16136 }
d39b6696 16137 else if (pos <= BUF_BEGV (b))
a2889657
JB
16138 return "Top";
16139 else
16140 {
3c7d31b9
RS
16141 if (total > 1000000)
16142 /* Do it differently for a large value, to avoid overflow. */
16143 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16144 else
16145 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
16146 /* We can't normally display a 3-digit number,
16147 so get us a 2-digit number that is close. */
16148 if (total == 100)
16149 total = 99;
16150 sprintf (decode_mode_spec_buf, "%2d%%", total);
16151 return decode_mode_spec_buf;
16152 }
16153 }
16154
8ffcb79f
RS
16155 /* Display percentage of size above the bottom of the screen. */
16156 case 'P':
16157 {
16158 int toppos = marker_position (w->start);
d39b6696
KH
16159 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16160 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 16161
d39b6696 16162 if (botpos >= BUF_ZV (b))
8ffcb79f 16163 {
d39b6696 16164 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
16165 return "All";
16166 else
16167 return "Bottom";
16168 }
16169 else
16170 {
3c7d31b9
RS
16171 if (total > 1000000)
16172 /* Do it differently for a large value, to avoid overflow. */
16173 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16174 else
16175 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
16176 /* We can't normally display a 3-digit number,
16177 so get us a 2-digit number that is close. */
16178 if (total == 100)
16179 total = 99;
d39b6696 16180 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
16181 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16182 else
16183 sprintf (decode_mode_spec_buf, "%2d%%", total);
16184 return decode_mode_spec_buf;
16185 }
16186 }
16187
1af9f229
RS
16188 case 's':
16189 /* status of process */
16190 obj = Fget_buffer_process (w->buffer);
16191 if (NILP (obj))
16192 return "no process";
16193#ifdef subprocesses
16194 obj = Fsymbol_name (Fprocess_status (obj));
16195#endif
16196 break;
d39b6696 16197
1af9f229
RS
16198 case 't': /* indicate TEXT or BINARY */
16199#ifdef MODE_LINE_BINARY_TEXT
16200 return MODE_LINE_BINARY_TEXT (b);
16201#else
16202 return "T";
16203#endif
1c9241f5
KH
16204
16205 case 'z':
16206 /* coding-system (not including end-of-line format) */
16207 case 'Z':
16208 /* coding-system (including end-of-line type) */
16209 {
16210 int eol_flag = (c == 'Z');
539b4d41 16211 char *p = decode_mode_spec_buf;
1c9241f5 16212
d30e754b 16213 if (! FRAME_WINDOW_P (f))
1c9241f5 16214 {
11c52c4f
RS
16215 /* No need to mention EOL here--the terminal never needs
16216 to do EOL conversion. */
16217 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16218 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 16219 }
f13c925f 16220 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 16221 p, eol_flag);
f13c925f 16222
11c52c4f 16223#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
16224#ifdef subprocesses
16225 obj = Fget_buffer_process (Fcurrent_buffer ());
16226 if (PROCESSP (obj))
16227 {
16228 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16229 p, eol_flag);
16230 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16231 p, eol_flag);
16232 }
16233#endif /* subprocesses */
11c52c4f 16234#endif /* 0 */
1c9241f5
KH
16235 *p = 0;
16236 return decode_mode_spec_buf;
16237 }
a2889657 16238 }
d39b6696 16239
e24c997d 16240 if (STRINGP (obj))
72f62cb5
GM
16241 {
16242 *multibyte = STRING_MULTIBYTE (obj);
2051c264 16243 return (char *) SDATA (obj);
72f62cb5 16244 }
a2889657
JB
16245 else
16246 return "";
16247}
5f5c8ee5
GM
16248
16249
12adba34
RS
16250/* Count up to COUNT lines starting from START / START_BYTE.
16251 But don't go beyond LIMIT_BYTE.
16252 Return the number of lines thus found (always nonnegative).
59b49f63 16253
12adba34 16254 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
16255
16256static int
12adba34
RS
16257display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16258 int start, start_byte, limit_byte, count;
16259 int *byte_pos_ptr;
59b49f63 16260{
59b49f63
RS
16261 register unsigned char *cursor;
16262 unsigned char *base;
16263
16264 register int ceiling;
16265 register unsigned char *ceiling_addr;
12adba34 16266 int orig_count = count;
59b49f63
RS
16267
16268 /* If we are not in selective display mode,
16269 check only for newlines. */
12adba34
RS
16270 int selective_display = (!NILP (current_buffer->selective_display)
16271 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
16272
16273 if (count > 0)
12adba34
RS
16274 {
16275 while (start_byte < limit_byte)
16276 {
16277 ceiling = BUFFER_CEILING_OF (start_byte);
16278 ceiling = min (limit_byte - 1, ceiling);
16279 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16280 base = (cursor = BYTE_POS_ADDR (start_byte));
16281 while (1)
16282 {
16283 if (selective_display)
16284 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16285 ;
16286 else
16287 while (*cursor != '\n' && ++cursor != ceiling_addr)
16288 ;
16289
16290 if (cursor != ceiling_addr)
16291 {
16292 if (--count == 0)
16293 {
16294 start_byte += cursor - base + 1;
16295 *byte_pos_ptr = start_byte;
16296 return orig_count;
16297 }
16298 else
16299 if (++cursor == ceiling_addr)
16300 break;
16301 }
16302 else
16303 break;
16304 }
16305 start_byte += cursor - base;
16306 }
16307 }
59b49f63
RS
16308 else
16309 {
12adba34
RS
16310 while (start_byte > limit_byte)
16311 {
16312 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16313 ceiling = max (limit_byte, ceiling);
16314 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16315 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
16316 while (1)
16317 {
12adba34
RS
16318 if (selective_display)
16319 while (--cursor != ceiling_addr
16320 && *cursor != '\n' && *cursor != 015)
16321 ;
16322 else
16323 while (--cursor != ceiling_addr && *cursor != '\n')
16324 ;
16325
59b49f63
RS
16326 if (cursor != ceiling_addr)
16327 {
16328 if (++count == 0)
16329 {
12adba34
RS
16330 start_byte += cursor - base + 1;
16331 *byte_pos_ptr = start_byte;
16332 /* When scanning backwards, we should
16333 not count the newline posterior to which we stop. */
16334 return - orig_count - 1;
59b49f63
RS
16335 }
16336 }
16337 else
16338 break;
16339 }
12adba34
RS
16340 /* Here we add 1 to compensate for the last decrement
16341 of CURSOR, which took it past the valid range. */
16342 start_byte += cursor - base + 1;
59b49f63
RS
16343 }
16344 }
16345
12adba34 16346 *byte_pos_ptr = limit_byte;
aa6d10fa 16347
12adba34
RS
16348 if (count < 0)
16349 return - orig_count + count;
16350 return orig_count - count;
aa6d10fa 16351
12adba34 16352}
a2889657 16353
a2889657 16354
5f5c8ee5
GM
16355\f
16356/***********************************************************************
16357 Displaying strings
16358 ***********************************************************************/
278feba9 16359
5f5c8ee5 16360/* Display a NUL-terminated string, starting with index START.
a3788d53 16361
5f5c8ee5
GM
16362 If STRING is non-null, display that C string. Otherwise, the Lisp
16363 string LISP_STRING is displayed.
a2889657 16364
5f5c8ee5
GM
16365 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16366 FACE_STRING. Display STRING or LISP_STRING with the face at
16367 FACE_STRING_POS in FACE_STRING:
a2889657 16368
5f5c8ee5
GM
16369 Display the string in the environment given by IT, but use the
16370 standard display table, temporarily.
a3788d53 16371
5f5c8ee5
GM
16372 FIELD_WIDTH is the minimum number of output glyphs to produce.
16373 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16374 with spaces. If STRING has more characters, more than FIELD_WIDTH
16375 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
2311178e 16376
5f5c8ee5
GM
16377 PRECISION is the maximum number of characters to output from
16378 STRING. PRECISION < 0 means don't truncate the string.
a2889657 16379
5f5c8ee5 16380 This is roughly equivalent to printf format specifiers:
a2889657 16381
5f5c8ee5
GM
16382 FIELD_WIDTH PRECISION PRINTF
16383 ----------------------------------------
16384 -1 -1 %s
16385 -1 10 %.10s
16386 10 -1 %10s
16387 20 10 %20.10s
a2889657 16388
5f5c8ee5
GM
16389 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16390 display them, and < 0 means obey the current buffer's value of
16391 enable_multibyte_characters.
278feba9 16392
5f5c8ee5 16393 Value is the number of glyphs produced. */
b1d1124b 16394
5f5c8ee5
GM
16395static int
16396display_string (string, lisp_string, face_string, face_string_pos,
16397 start, it, field_width, precision, max_x, multibyte)
16398 unsigned char *string;
16399 Lisp_Object lisp_string;
68c45bf0
PE
16400 Lisp_Object face_string;
16401 int face_string_pos;
5f5c8ee5
GM
16402 int start;
16403 struct it *it;
16404 int field_width, precision, max_x;
16405 int multibyte;
16406{
16407 int hpos_at_start = it->hpos;
16408 int saved_face_id = it->face_id;
16409 struct glyph_row *row = it->glyph_row;
16410
16411 /* Initialize the iterator IT for iteration over STRING beginning
e719f5ae 16412 with index START. */
5f5c8ee5
GM
16413 reseat_to_string (it, string, lisp_string, start,
16414 precision, field_width, multibyte);
16415
16416 /* If displaying STRING, set up the face of the iterator
16417 from LISP_STRING, if that's given. */
16418 if (STRINGP (face_string))
16419 {
16420 int endptr;
16421 struct face *face;
2311178e 16422
5f5c8ee5
GM
16423 it->face_id
16424 = face_at_string_position (it->w, face_string, face_string_pos,
16425 0, it->region_beg_charpos,
16426 it->region_end_charpos,
5de7c6f2 16427 &endptr, it->base_face_id, 0);
5f5c8ee5
GM
16428 face = FACE_FROM_ID (it->f, it->face_id);
16429 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 16430 }
a2889657 16431
5f5c8ee5
GM
16432 /* Set max_x to the maximum allowed X position. Don't let it go
16433 beyond the right edge of the window. */
16434 if (max_x <= 0)
16435 max_x = it->last_visible_x;
16436 else
16437 max_x = min (max_x, it->last_visible_x);
efc63ef0 16438
5f5c8ee5
GM
16439 /* Skip over display elements that are not visible. because IT->w is
16440 hscrolled. */
16441 if (it->current_x < it->first_visible_x)
16442 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16443 MOVE_TO_POS | MOVE_TO_X);
a2889657 16444
5f5c8ee5
GM
16445 row->ascent = it->max_ascent;
16446 row->height = it->max_ascent + it->max_descent;
312246d1
GM
16447 row->phys_ascent = it->max_phys_ascent;
16448 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 16449
5f5c8ee5
GM
16450 /* This condition is for the case that we are called with current_x
16451 past last_visible_x. */
16452 while (it->current_x < max_x)
a2889657 16453 {
5f5c8ee5 16454 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 16455
5f5c8ee5
GM
16456 /* Get the next display element. */
16457 if (!get_next_display_element (it))
90adcf20 16458 break;
1c9241f5 16459
5f5c8ee5
GM
16460 /* Produce glyphs. */
16461 x_before = it->current_x;
16462 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16463 PRODUCE_GLYPHS (it);
90adcf20 16464
5f5c8ee5
GM
16465 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16466 i = 0;
16467 x = x_before;
16468 while (i < nglyphs)
a2889657 16469 {
5f5c8ee5 16470 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
2311178e 16471
5f5c8ee5
GM
16472 if (!it->truncate_lines_p
16473 && x + glyph->pixel_width > max_x)
16474 {
16475 /* End of continued line or max_x reached. */
37be86f2
KH
16476 if (CHAR_GLYPH_PADDING_P (*glyph))
16477 {
16478 /* A wide character is unbreakable. */
16479 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16480 it->current_x = x_before;
16481 }
16482 else
16483 {
16484 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16485 it->current_x = x;
16486 }
5f5c8ee5
GM
16487 break;
16488 }
16489 else if (x + glyph->pixel_width > it->first_visible_x)
16490 {
16491 /* Glyph is at least partially visible. */
16492 ++it->hpos;
16493 if (x < it->first_visible_x)
16494 it->glyph_row->x = x - it->first_visible_x;
16495 }
16496 else
a2889657 16497 {
5f5c8ee5
GM
16498 /* Glyph is off the left margin of the display area.
16499 Should not happen. */
16500 abort ();
a2889657 16501 }
5f5c8ee5
GM
16502
16503 row->ascent = max (row->ascent, it->max_ascent);
16504 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
16505 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16506 row->phys_height = max (row->phys_height,
16507 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
16508 x += glyph->pixel_width;
16509 ++i;
a2889657 16510 }
5f5c8ee5
GM
16511
16512 /* Stop if max_x reached. */
16513 if (i < nglyphs)
16514 break;
16515
16516 /* Stop at line ends. */
16517 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 16518 {
5f5c8ee5
GM
16519 it->continuation_lines_width = 0;
16520 break;
a2889657 16521 }
1c9241f5 16522
cafafe0b 16523 set_iterator_to_next (it, 1);
a688bb24 16524
5f5c8ee5
GM
16525 /* Stop if truncating at the right edge. */
16526 if (it->truncate_lines_p
16527 && it->current_x >= it->last_visible_x)
16528 {
16529 /* Add truncation mark, but don't do it if the line is
16530 truncated at a padding space. */
16531 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 16532 {
5f5c8ee5 16533 if (!FRAME_WINDOW_P (it->f))
37be86f2
KH
16534 {
16535 int i, n;
16536
9299cb15 16537 if (it->current_x > it->last_visible_x)
37be86f2 16538 {
9299cb15
KH
16539 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16540 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16541 break;
16542 for (n = row->used[TEXT_AREA]; i < n; ++i)
16543 {
16544 row->used[TEXT_AREA] = i;
16545 produce_special_glyphs (it, IT_TRUNCATION);
16546 }
37be86f2 16547 }
9299cb15 16548 produce_special_glyphs (it, IT_TRUNCATION);
37be86f2 16549 }
5f5c8ee5 16550 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 16551 }
5f5c8ee5 16552 break;
1c9241f5 16553 }
a2889657
JB
16554 }
16555
5f5c8ee5
GM
16556 /* Maybe insert a truncation at the left. */
16557 if (it->first_visible_x
16558 && IT_CHARPOS (*it) > 0)
a2889657 16559 {
5f5c8ee5
GM
16560 if (!FRAME_WINDOW_P (it->f))
16561 insert_left_trunc_glyphs (it);
16562 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
16563 }
16564
5f5c8ee5 16565 it->face_id = saved_face_id;
2311178e 16566
5f5c8ee5
GM
16567 /* Value is number of columns displayed. */
16568 return it->hpos - hpos_at_start;
16569}
a2889657 16570
a2889657 16571
a2889657 16572\f
3b6b6db7 16573/* This is like a combination of memq and assq. Return 1/2 if PROPVAL
5f5c8ee5
GM
16574 appears as an element of LIST or as the car of an element of LIST.
16575 If PROPVAL is a list, compare each element against LIST in that
3b6b6db7
SM
16576 way, and return 1/2 if any element of PROPVAL is found in LIST.
16577 Otherwise return 0. This function cannot quit.
16578 The return value is 2 if the text is invisible but with an ellipsis
16579 and 1 if it's invisible and without an ellipsis. */
642eefc6
RS
16580
16581int
16582invisible_p (propval, list)
16583 register Lisp_Object propval;
16584 Lisp_Object list;
16585{
3b6b6db7 16586 register Lisp_Object tail, proptail;
2311178e 16587
3b6b6db7
SM
16588 for (tail = list; CONSP (tail); tail = XCDR (tail))
16589 {
16590 register Lisp_Object tem;
16591 tem = XCAR (tail);
16592 if (EQ (propval, tem))
16593 return 1;
16594 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16595 return NILP (XCDR (tem)) ? 1 : 2;
16596 }
2311178e 16597
3b6b6db7
SM
16598 if (CONSP (propval))
16599 {
16600 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16601 {
16602 Lisp_Object propelt;
16603 propelt = XCAR (proptail);
16604 for (tail = list; CONSP (tail); tail = XCDR (tail))
16605 {
16606 register Lisp_Object tem;
16607 tem = XCAR (tail);
16608 if (EQ (propelt, tem))
16609 return 1;
16610 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16611 return NILP (XCDR (tem)) ? 1 : 2;
16612 }
16613 }
16614 }
2311178e 16615
3b6b6db7 16616 return 0;
642eefc6 16617}
5f5c8ee5 16618
642eefc6 16619\f
133c764e
KS
16620/***********************************************************************
16621 Glyph Display
16622 ***********************************************************************/
16623
79fa9e0f
KS
16624#ifdef HAVE_WINDOW_SYSTEM
16625
133c764e
KS
16626#if GLYPH_DEBUG
16627
16628void
16629dump_glyph_string (s)
16630 struct glyph_string *s;
16631{
16632 fprintf (stderr, "glyph string\n");
16633 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
16634 s->x, s->y, s->width, s->height);
16635 fprintf (stderr, " ybase = %d\n", s->ybase);
16636 fprintf (stderr, " hl = %d\n", s->hl);
16637 fprintf (stderr, " left overhang = %d, right = %d\n",
16638 s->left_overhang, s->right_overhang);
16639 fprintf (stderr, " nchars = %d\n", s->nchars);
16640 fprintf (stderr, " extends to end of line = %d\n",
16641 s->extends_to_end_of_line_p);
16642 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
16643 fprintf (stderr, " bg width = %d\n", s->background_width);
16644}
16645
16646#endif /* GLYPH_DEBUG */
16647
16648/* Initialize glyph string S. CHAR2B is a suitably allocated vector
16649 of XChar2b structures for S; it can't be allocated in
16650 init_glyph_string because it must be allocated via `alloca'. W
16651 is the window on which S is drawn. ROW and AREA are the glyph row
16652 and area within the row from which S is constructed. START is the
16653 index of the first glyph structure covered by S. HL is a
16654 face-override for drawing S. */
16655
16656#ifdef HAVE_NTGUI
16657#define OPTIONAL_HDC(hdc) hdc,
16658#define DECLARE_HDC(hdc) HDC hdc;
16659#define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
16660#define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
16661#endif
16662
16663#ifndef OPTIONAL_HDC
16664#define OPTIONAL_HDC(hdc)
16665#define DECLARE_HDC(hdc)
16666#define ALLOCATE_HDC(hdc, f)
16667#define RELEASE_HDC(hdc, f)
16668#endif
16669
16670static void
16671init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
16672 struct glyph_string *s;
16673 DECLARE_HDC (hdc)
16674 XChar2b *char2b;
16675 struct window *w;
16676 struct glyph_row *row;
16677 enum glyph_row_area area;
16678 int start;
16679 enum draw_glyphs_face hl;
16680{
16681 bzero (s, sizeof *s);
16682 s->w = w;
16683 s->f = XFRAME (w->frame);
16684#ifdef HAVE_NTGUI
16685 s->hdc = hdc;
16686#endif
16687 s->display = FRAME_X_DISPLAY (s->f);
16688 s->window = FRAME_X_WINDOW (s->f);
16689 s->char2b = char2b;
16690 s->hl = hl;
16691 s->row = row;
16692 s->area = area;
16693 s->first_glyph = row->glyphs[area] + start;
16694 s->height = row->height;
16695 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
16696
16697 /* Display the internal border below the tool-bar window. */
16698 if (s->w == XWINDOW (s->f->tool_bar_window))
16699 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
16700
16701 s->ybase = s->y + row->ascent;
16702}
16703
16704
16705/* Append the list of glyph strings with head H and tail T to the list
16706 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
16707
16708static INLINE void
16709append_glyph_string_lists (head, tail, h, t)
16710 struct glyph_string **head, **tail;
16711 struct glyph_string *h, *t;
16712{
16713 if (h)
16714 {
16715 if (*head)
16716 (*tail)->next = h;
16717 else
16718 *head = h;
16719 h->prev = *tail;
16720 *tail = t;
16721 }
16722}
16723
16724
16725/* Prepend the list of glyph strings with head H and tail T to the
16726 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
16727 result. */
16728
16729static INLINE void
16730prepend_glyph_string_lists (head, tail, h, t)
16731 struct glyph_string **head, **tail;
16732 struct glyph_string *h, *t;
16733{
16734 if (h)
16735 {
16736 if (*head)
16737 (*head)->prev = t;
16738 else
16739 *tail = t;
16740 t->next = *head;
16741 *head = h;
16742 }
16743}
16744
16745
16746/* Append glyph string S to the list with head *HEAD and tail *TAIL.
16747 Set *HEAD and *TAIL to the resulting list. */
16748
16749static INLINE void
16750append_glyph_string (head, tail, s)
16751 struct glyph_string **head, **tail;
16752 struct glyph_string *s;
16753{
16754 s->next = s->prev = NULL;
16755 append_glyph_string_lists (head, tail, s, s);
16756}
16757
16758
16759/* Get face and two-byte form of character glyph GLYPH on frame F.
16760 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
16761 a pointer to a realized face that is ready for display. */
16762
16763static INLINE struct face *
16764get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
16765 struct frame *f;
16766 struct glyph *glyph;
16767 XChar2b *char2b;
16768 int *two_byte_p;
16769{
16770 struct face *face;
16771
16772 xassert (glyph->type == CHAR_GLYPH);
16773 face = FACE_FROM_ID (f, glyph->face_id);
16774
16775 if (two_byte_p)
16776 *two_byte_p = 0;
16777
16778 if (!glyph->multibyte_p)
16779 {
16780 /* Unibyte case. We don't have to encode, but we have to make
16781 sure to use a face suitable for unibyte. */
16782 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16783 }
16784 else if (glyph->u.ch < 128
16785 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
16786 {
16787 /* Case of ASCII in a face known to fit ASCII. */
16788 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16789 }
16790 else
16791 {
16792 int c1, c2, charset;
16793
16794 /* Split characters into bytes. If c2 is -1 afterwards, C is
16795 really a one-byte character so that byte1 is zero. */
16796 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
16797 if (c2 > 0)
16798 STORE_XCHAR2B (char2b, c1, c2);
16799 else
16800 STORE_XCHAR2B (char2b, 0, c1);
16801
16802 /* Maybe encode the character in *CHAR2B. */
16803 if (charset != CHARSET_ASCII)
16804 {
16805 struct font_info *font_info
16806 = FONT_INFO_FROM_ID (f, face->font_info_id);
16807 if (font_info)
16808 glyph->font_type
16809 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
16810 }
16811 }
16812
16813 /* Make sure X resources of the face are allocated. */
16814 xassert (face != NULL);
16815 PREPARE_FACE_FOR_DISPLAY (f, face);
16816 return face;
16817}
16818
16819
16820/* Fill glyph string S with composition components specified by S->cmp.
16821
16822 FACES is an array of faces for all components of this composition.
16823 S->gidx is the index of the first component for S.
16824 OVERLAPS_P non-zero means S should draw the foreground only, and
16825 use its physical height for clipping.
16826
16827 Value is the index of a component not in S. */
16828
16829static int
16830fill_composite_glyph_string (s, faces, overlaps_p)
16831 struct glyph_string *s;
16832 struct face **faces;
16833 int overlaps_p;
16834{
16835 int i;
16836
16837 xassert (s);
16838
16839 s->for_overlaps_p = overlaps_p;
16840
16841 s->face = faces[s->gidx];
16842 s->font = s->face->font;
16843 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16844
16845 /* For all glyphs of this composition, starting at the offset
16846 S->gidx, until we reach the end of the definition or encounter a
16847 glyph that requires the different face, add it to S. */
16848 ++s->nchars;
16849 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
16850 ++s->nchars;
16851
16852 /* All glyph strings for the same composition has the same width,
16853 i.e. the width set for the first component of the composition. */
16854
16855 s->width = s->first_glyph->pixel_width;
16856
16857 /* If the specified font could not be loaded, use the frame's
16858 default font, but record the fact that we couldn't load it in
16859 the glyph string so that we can draw rectangles for the
16860 characters of the glyph string. */
16861 if (s->font == NULL)
16862 {
16863 s->font_not_found_p = 1;
16864 s->font = FRAME_FONT (s->f);
16865 }
16866
16867 /* Adjust base line for subscript/superscript text. */
16868 s->ybase += s->first_glyph->voffset;
16869
16870 xassert (s->face && s->face->gc);
16871
16872 /* This glyph string must always be drawn with 16-bit functions. */
16873 s->two_byte_p = 1;
16874
16875 return s->gidx + s->nchars;
16876}
16877
16878
16879/* Fill glyph string S from a sequence of character glyphs.
16880
16881 FACE_ID is the face id of the string. START is the index of the
16882 first glyph to consider, END is the index of the last + 1.
16883 OVERLAPS_P non-zero means S should draw the foreground only, and
16884 use its physical height for clipping.
16885
16886 Value is the index of the first glyph not in S. */
16887
16888static int
16889fill_glyph_string (s, face_id, start, end, overlaps_p)
16890 struct glyph_string *s;
16891 int face_id;
16892 int start, end, overlaps_p;
16893{
16894 struct glyph *glyph, *last;
16895 int voffset;
16896 int glyph_not_available_p;
16897
16898 xassert (s->f == XFRAME (s->w->frame));
16899 xassert (s->nchars == 0);
16900 xassert (start >= 0 && end > start);
16901
16902 s->for_overlaps_p = overlaps_p,
16903 glyph = s->row->glyphs[s->area] + start;
16904 last = s->row->glyphs[s->area] + end;
16905 voffset = glyph->voffset;
16906
16907 glyph_not_available_p = glyph->glyph_not_available_p;
16908
16909 while (glyph < last
16910 && glyph->type == CHAR_GLYPH
16911 && glyph->voffset == voffset
16912 /* Same face id implies same font, nowadays. */
16913 && glyph->face_id == face_id
16914 && glyph->glyph_not_available_p == glyph_not_available_p)
16915 {
16916 int two_byte_p;
16917
16918 s->face = get_glyph_face_and_encoding (s->f, glyph,
16919 s->char2b + s->nchars,
16920 &two_byte_p);
16921 s->two_byte_p = two_byte_p;
16922 ++s->nchars;
16923 xassert (s->nchars <= end - start);
16924 s->width += glyph->pixel_width;
16925 ++glyph;
16926 }
16927
16928 s->font = s->face->font;
16929 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16930
16931 /* If the specified font could not be loaded, use the frame's font,
16932 but record the fact that we couldn't load it in
16933 S->font_not_found_p so that we can draw rectangles for the
16934 characters of the glyph string. */
16935 if (s->font == NULL || glyph_not_available_p)
16936 {
16937 s->font_not_found_p = 1;
16938 s->font = FRAME_FONT (s->f);
16939 }
16940
16941 /* Adjust base line for subscript/superscript text. */
16942 s->ybase += voffset;
16943
16944 xassert (s->face && s->face->gc);
16945 return glyph - s->row->glyphs[s->area];
16946}
16947
16948
16949/* Fill glyph string S from image glyph S->first_glyph. */
16950
16951static void
16952fill_image_glyph_string (s)
16953 struct glyph_string *s;
16954{
16955 xassert (s->first_glyph->type == IMAGE_GLYPH);
16956 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
16957 xassert (s->img);
16958 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
16959 s->font = s->face->font;
16960 s->width = s->first_glyph->pixel_width;
16961
16962 /* Adjust base line for subscript/superscript text. */
16963 s->ybase += s->first_glyph->voffset;
16964}
16965
16966
16967/* Fill glyph string S from a sequence of stretch glyphs.
16968
16969 ROW is the glyph row in which the glyphs are found, AREA is the
16970 area within the row. START is the index of the first glyph to
16971 consider, END is the index of the last + 1.
16972
16973 Value is the index of the first glyph not in S. */
16974
16975static int
16976fill_stretch_glyph_string (s, row, area, start, end)
16977 struct glyph_string *s;
16978 struct glyph_row *row;
16979 enum glyph_row_area area;
16980 int start, end;
16981{
16982 struct glyph *glyph, *last;
16983 int voffset, face_id;
16984
16985 xassert (s->first_glyph->type == STRETCH_GLYPH);
16986
16987 glyph = s->row->glyphs[s->area] + start;
16988 last = s->row->glyphs[s->area] + end;
16989 face_id = glyph->face_id;
16990 s->face = FACE_FROM_ID (s->f, face_id);
16991 s->font = s->face->font;
16992 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16993 s->width = glyph->pixel_width;
16994 voffset = glyph->voffset;
16995
16996 for (++glyph;
16997 (glyph < last
16998 && glyph->type == STRETCH_GLYPH
16999 && glyph->voffset == voffset
17000 && glyph->face_id == face_id);
17001 ++glyph)
17002 s->width += glyph->pixel_width;
17003
17004 /* Adjust base line for subscript/superscript text. */
17005 s->ybase += voffset;
17006
17007 /* The case that face->gc == 0 is handled when drawing the glyph
17008 string by calling PREPARE_FACE_FOR_DISPLAY. */
17009 xassert (s->face);
17010 return glyph - s->row->glyphs[s->area];
17011}
17012
17013
17014/* EXPORT for RIF:
17015 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17016 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17017 assumed to be zero. */
17018
17019void
17020x_get_glyph_overhangs (glyph, f, left, right)
17021 struct glyph *glyph;
17022 struct frame *f;
17023 int *left, *right;
17024{
17025 *left = *right = 0;
17026
17027 if (glyph->type == CHAR_GLYPH)
17028 {
17029 XFontStruct *font;
17030 struct face *face;
17031 struct font_info *font_info;
17032 XChar2b char2b;
17033 XCharStruct *pcm;
17034
17035 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17036 font = face->font;
17037 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17038 if (font /* ++KFS: Should this be font_info ? */
17039 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17040 {
17041 if (pcm->rbearing > pcm->width)
17042 *right = pcm->rbearing - pcm->width;
17043 if (pcm->lbearing < 0)
17044 *left = -pcm->lbearing;
17045 }
17046 }
17047}
17048
17049
17050/* Return the index of the first glyph preceding glyph string S that
17051 is overwritten by S because of S's left overhang. Value is -1
17052 if no glyphs are overwritten. */
17053
17054static int
17055left_overwritten (s)
17056 struct glyph_string *s;
17057{
17058 int k;
17059
17060 if (s->left_overhang)
17061 {
17062 int x = 0, i;
17063 struct glyph *glyphs = s->row->glyphs[s->area];
17064 int first = s->first_glyph - glyphs;
17065
17066 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17067 x -= glyphs[i].pixel_width;
17068
17069 k = i + 1;
17070 }
17071 else
17072 k = -1;
17073
17074 return k;
17075}
17076
17077
17078/* Return the index of the first glyph preceding glyph string S that
17079 is overwriting S because of its right overhang. Value is -1 if no
17080 glyph in front of S overwrites S. */
17081
17082static int
17083left_overwriting (s)
17084 struct glyph_string *s;
17085{
17086 int i, k, x;
17087 struct glyph *glyphs = s->row->glyphs[s->area];
17088 int first = s->first_glyph - glyphs;
17089
17090 k = -1;
17091 x = 0;
17092 for (i = first - 1; i >= 0; --i)
17093 {
17094 int left, right;
17095 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17096 if (x + right > 0)
17097 k = i;
17098 x -= glyphs[i].pixel_width;
17099 }
17100
17101 return k;
17102}
17103
17104
17105/* Return the index of the last glyph following glyph string S that is
17106 not overwritten by S because of S's right overhang. Value is -1 if
17107 no such glyph is found. */
17108
17109static int
17110right_overwritten (s)
17111 struct glyph_string *s;
17112{
17113 int k = -1;
17114
17115 if (s->right_overhang)
17116 {
17117 int x = 0, i;
17118 struct glyph *glyphs = s->row->glyphs[s->area];
17119 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17120 int end = s->row->used[s->area];
17121
17122 for (i = first; i < end && s->right_overhang > x; ++i)
17123 x += glyphs[i].pixel_width;
17124
17125 k = i;
17126 }
17127
17128 return k;
17129}
17130
17131
17132/* Return the index of the last glyph following glyph string S that
17133 overwrites S because of its left overhang. Value is negative
17134 if no such glyph is found. */
17135
17136static int
17137right_overwriting (s)
17138 struct glyph_string *s;
17139{
17140 int i, k, x;
17141 int end = s->row->used[s->area];
17142 struct glyph *glyphs = s->row->glyphs[s->area];
17143 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17144
17145 k = -1;
17146 x = 0;
17147 for (i = first; i < end; ++i)
17148 {
17149 int left, right;
17150 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17151 if (x - left < 0)
17152 k = i;
17153 x += glyphs[i].pixel_width;
17154 }
17155
17156 return k;
17157}
17158
17159
17160/* Get face and two-byte form of character C in face FACE_ID on frame
17161 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17162 means we want to display multibyte text. DISPLAY_P non-zero means
17163 make sure that X resources for the face returned are allocated.
17164 Value is a pointer to a realized face that is ready for display if
17165 DISPLAY_P is non-zero. */
17166
17167static INLINE struct face *
17168get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17169 struct frame *f;
17170 int c, face_id;
17171 XChar2b *char2b;
17172 int multibyte_p, display_p;
17173{
17174 struct face *face = FACE_FROM_ID (f, face_id);
17175
17176 if (!multibyte_p)
17177 {
17178 /* Unibyte case. We don't have to encode, but we have to make
17179 sure to use a face suitable for unibyte. */
17180 STORE_XCHAR2B (char2b, 0, c);
17181 face_id = FACE_FOR_CHAR (f, face, c);
17182 face = FACE_FROM_ID (f, face_id);
17183 }
17184 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17185 {
17186 /* Case of ASCII in a face known to fit ASCII. */
17187 STORE_XCHAR2B (char2b, 0, c);
17188 }
17189 else
17190 {
17191 int c1, c2, charset;
17192
17193 /* Split characters into bytes. If c2 is -1 afterwards, C is
17194 really a one-byte character so that byte1 is zero. */
17195 SPLIT_CHAR (c, charset, c1, c2);
17196 if (c2 > 0)
17197 STORE_XCHAR2B (char2b, c1, c2);
17198 else
17199 STORE_XCHAR2B (char2b, 0, c1);
17200
17201 /* Maybe encode the character in *CHAR2B. */
17202 if (face->font != NULL)
17203 {
17204 struct font_info *font_info
17205 = FONT_INFO_FROM_ID (f, face->font_info_id);
17206 if (font_info)
17207 rif->encode_char (c, char2b, font_info, 0);
17208 }
17209 }
17210
17211 /* Make sure X resources of the face are allocated. */
17212#ifdef HAVE_X_WINDOWS
17213 if (display_p)
17214#endif
17215 {
17216 xassert (face != NULL);
17217 PREPARE_FACE_FOR_DISPLAY (f, face);
17218 }
17219
17220 return face;
17221}
17222
17223
17224/* Set background width of glyph string S. START is the index of the
17225 first glyph following S. LAST_X is the right-most x-position + 1
17226 in the drawing area. */
17227
17228static INLINE void
17229set_glyph_string_background_width (s, start, last_x)
17230 struct glyph_string *s;
17231 int start;
17232 int last_x;
17233{
17234 /* If the face of this glyph string has to be drawn to the end of
17235 the drawing area, set S->extends_to_end_of_line_p. */
17236 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17237
17238 if (start == s->row->used[s->area]
17239 && s->area == TEXT_AREA
17240 && ((s->hl == DRAW_NORMAL_TEXT
17241 && (s->row->fill_line_p
17242 || s->face->background != default_face->background
17243 || s->face->stipple != default_face->stipple
17244 || s->row->mouse_face_p))
17245 || s->hl == DRAW_MOUSE_FACE
17246 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17247 && s->row->fill_line_p)))
17248 s->extends_to_end_of_line_p = 1;
17249
17250 /* If S extends its face to the end of the line, set its
17251 background_width to the distance to the right edge of the drawing
17252 area. */
17253 if (s->extends_to_end_of_line_p)
17254 s->background_width = last_x - s->x + 1;
17255 else
17256 s->background_width = s->width;
17257}
17258
17259
17260/* Compute overhangs and x-positions for glyph string S and its
17261 predecessors, or successors. X is the starting x-position for S.
17262 BACKWARD_P non-zero means process predecessors. */
17263
17264static void
17265compute_overhangs_and_x (s, x, backward_p)
17266 struct glyph_string *s;
17267 int x;
17268 int backward_p;
17269{
17270 if (backward_p)
17271 {
17272 while (s)
17273 {
17274 if (rif->compute_glyph_string_overhangs)
17275 rif->compute_glyph_string_overhangs (s);
17276 x -= s->width;
17277 s->x = x;
17278 s = s->prev;
17279 }
17280 }
17281 else
17282 {
17283 while (s)
17284 {
17285 if (rif->compute_glyph_string_overhangs)
17286 rif->compute_glyph_string_overhangs (s);
17287 s->x = x;
17288 x += s->width;
17289 s = s->next;
17290 }
17291 }
17292}
17293
17294
17295
fa3c6b4d 17296/* The following macros are only called from draw_glyphs below.
133c764e
KS
17297 They reference the following parameters of that function directly:
17298 `w', `row', `area', and `overlap_p'
17299 as well as the following local variables:
17300 `s', `f', and `hdc' (in W32) */
17301
17302#ifdef HAVE_NTGUI
17303/* On W32, silently add local `hdc' variable to argument list of
17304 init_glyph_string. */
17305#define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17306 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17307#else
17308#define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17309 init_glyph_string (s, char2b, w, row, area, start, hl)
17310#endif
17311
17312/* Add a glyph string for a stretch glyph to the list of strings
17313 between HEAD and TAIL. START is the index of the stretch glyph in
17314 row area AREA of glyph row ROW. END is the index of the last glyph
17315 in that glyph row area. X is the current output position assigned
17316 to the new glyph string constructed. HL overrides that face of the
17317 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17318 is the right-most x-position of the drawing area. */
17319
17320/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17321 and below -- keep them on one line. */
17322#define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17323 do \
17324 { \
17325 s = (struct glyph_string *) alloca (sizeof *s); \
17326 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17327 START = fill_stretch_glyph_string (s, row, area, START, END); \
17328 append_glyph_string (&HEAD, &TAIL, s); \
17329 s->x = (X); \
17330 } \
17331 while (0)
17332
17333
17334/* Add a glyph string for an image glyph to the list of strings
17335 between HEAD and TAIL. START is the index of the image glyph in
17336 row area AREA of glyph row ROW. END is the index of the last glyph
17337 in that glyph row area. X is the current output position assigned
17338 to the new glyph string constructed. HL overrides that face of the
17339 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17340 is the right-most x-position of the drawing area. */
17341
17342#define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17343 do \
17344 { \
17345 s = (struct glyph_string *) alloca (sizeof *s); \
17346 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17347 fill_image_glyph_string (s); \
17348 append_glyph_string (&HEAD, &TAIL, s); \
17349 ++START; \
17350 s->x = (X); \
17351 } \
17352 while (0)
17353
17354
17355/* Add a glyph string for a sequence of character glyphs to the list
17356 of strings between HEAD and TAIL. START is the index of the first
17357 glyph in row area AREA of glyph row ROW that is part of the new
17358 glyph string. END is the index of the last glyph in that glyph row
17359 area. X is the current output position assigned to the new glyph
17360 string constructed. HL overrides that face of the glyph; e.g. it
17361 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17362 right-most x-position of the drawing area. */
17363
17364#define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17365 do \
17366 { \
17367 int c, face_id; \
17368 XChar2b *char2b; \
17369 \
17370 c = (row)->glyphs[area][START].u.ch; \
17371 face_id = (row)->glyphs[area][START].face_id; \
17372 \
17373 s = (struct glyph_string *) alloca (sizeof *s); \
17374 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17375 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17376 append_glyph_string (&HEAD, &TAIL, s); \
17377 s->x = (X); \
17378 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17379 } \
17380 while (0)
17381
17382
17383/* Add a glyph string for a composite sequence to the list of strings
17384 between HEAD and TAIL. START is the index of the first glyph in
17385 row area AREA of glyph row ROW that is part of the new glyph
17386 string. END is the index of the last glyph in that glyph row area.
17387 X is the current output position assigned to the new glyph string
17388 constructed. HL overrides that face of the glyph; e.g. it is
17389 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17390 x-position of the drawing area. */
17391
17392#define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17393 do { \
17394 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17395 int face_id = (row)->glyphs[area][START].face_id; \
17396 struct face *base_face = FACE_FROM_ID (f, face_id); \
17397 struct composition *cmp = composition_table[cmp_id]; \
17398 int glyph_len = cmp->glyph_len; \
17399 XChar2b *char2b; \
17400 struct face **faces; \
17401 struct glyph_string *first_s = NULL; \
17402 int n; \
17403 \
17404 base_face = base_face->ascii_face; \
17405 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17406 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17407 /* At first, fill in `char2b' and `faces'. */ \
17408 for (n = 0; n < glyph_len; n++) \
17409 { \
17410 int c = COMPOSITION_GLYPH (cmp, n); \
17411 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17412 faces[n] = FACE_FROM_ID (f, this_face_id); \
17413 get_char_face_and_encoding (f, c, this_face_id, \
17414 char2b + n, 1, 1); \
17415 } \
17416 \
17417 /* Make glyph_strings for each glyph sequence that is drawable by \
17418 the same face, and append them to HEAD/TAIL. */ \
17419 for (n = 0; n < cmp->glyph_len;) \
17420 { \
17421 s = (struct glyph_string *) alloca (sizeof *s); \
17422 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17423 append_glyph_string (&(HEAD), &(TAIL), s); \
17424 s->cmp = cmp; \
17425 s->gidx = n; \
17426 s->x = (X); \
17427 \
17428 if (n == 0) \
17429 first_s = s; \
17430 \
17431 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17432 } \
17433 \
17434 ++START; \
17435 s = first_s; \
17436 } while (0)
17437
17438
17439/* Build a list of glyph strings between HEAD and TAIL for the glyphs
17440 of AREA of glyph row ROW on window W between indices START and END.
17441 HL overrides the face for drawing glyph strings, e.g. it is
17442 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17443 x-positions of the drawing area.
17444
17445 This is an ugly monster macro construct because we must use alloca
fa3c6b4d 17446 to allocate glyph strings (because draw_glyphs can be called
133c764e
KS
17447 asynchronously). */
17448
17449#define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17450 do \
17451 { \
17452 HEAD = TAIL = NULL; \
17453 while (START < END) \
17454 { \
17455 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17456 switch (first_glyph->type) \
17457 { \
17458 case CHAR_GLYPH: \
17459 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17460 HL, X, LAST_X); \
17461 break; \
17462 \
17463 case COMPOSITE_GLYPH: \
17464 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17465 HL, X, LAST_X); \
17466 break; \
17467 \
17468 case STRETCH_GLYPH: \
17469 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17470 HL, X, LAST_X); \
17471 break; \
17472 \
17473 case IMAGE_GLYPH: \
17474 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17475 HL, X, LAST_X); \
17476 break; \
17477 \
17478 default: \
17479 abort (); \
17480 } \
17481 \
17482 set_glyph_string_background_width (s, START, LAST_X); \
17483 (X) += s->width; \
17484 } \
17485 } \
17486 while (0)
17487
17488
17489/* Draw glyphs between START and END in AREA of ROW on window W,
17490 starting at x-position X. X is relative to AREA in W. HL is a
17491 face-override with the following meaning:
17492
17493 DRAW_NORMAL_TEXT draw normally
17494 DRAW_CURSOR draw in cursor face
17495 DRAW_MOUSE_FACE draw in mouse face.
17496 DRAW_INVERSE_VIDEO draw in mode line face
17497 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17498 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17499
17500 If OVERLAPS_P is non-zero, draw only the foreground of characters
17501 and clip to the physical height of ROW.
17502
17503 Value is the x-position reached, relative to AREA of W. */
17504
fa3c6b4d
KS
17505static int
17506draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
133c764e
KS
17507 struct window *w;
17508 int x;
17509 struct glyph_row *row;
17510 enum glyph_row_area area;
17511 int start, end;
17512 enum draw_glyphs_face hl;
17513 int overlaps_p;
17514{
17515 struct glyph_string *head, *tail;
17516 struct glyph_string *s;
17517 int last_x, area_width;
17518 int x_reached;
17519 int i, j;
17520 struct frame *f = XFRAME (WINDOW_FRAME (w));
17521 DECLARE_HDC (hdc);
17522
17523 ALLOCATE_HDC (hdc, f);
17524
17525 /* Let's rather be paranoid than getting a SEGV. */
17526 end = min (end, row->used[area]);
17527 start = max (0, start);
17528 start = min (end, start);
17529
17530 /* Translate X to frame coordinates. Set last_x to the right
17531 end of the drawing area. */
17532 if (row->full_width_p)
17533 {
17534 /* X is relative to the left edge of W, without scroll bars
17535 or fringes. */
da8b7f4f
KS
17536 x += WINDOW_LEFT_EDGE_X (w);
17537 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
133c764e
KS
17538 }
17539 else
17540 {
da8b7f4f
KS
17541 int area_left = window_box_left (w, area);
17542 x += area_left;
133c764e 17543 area_width = window_box_width (w, area);
da8b7f4f 17544 last_x = area_left + area_width;
133c764e
KS
17545 }
17546
17547 /* Build a doubly-linked list of glyph_string structures between
17548 head and tail from what we have to draw. Note that the macro
17549 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17550 the reason we use a separate variable `i'. */
17551 i = start;
17552 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17553 if (tail)
17554 x_reached = tail->x + tail->background_width;
17555 else
17556 x_reached = x;
17557
17558 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17559 the row, redraw some glyphs in front or following the glyph
17560 strings built above. */
17561 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17562 {
17563 int dummy_x = 0;
17564 struct glyph_string *h, *t;
17565
17566 /* Compute overhangs for all glyph strings. */
17567 if (rif->compute_glyph_string_overhangs)
17568 for (s = head; s; s = s->next)
17569 rif->compute_glyph_string_overhangs (s);
17570
17571 /* Prepend glyph strings for glyphs in front of the first glyph
17572 string that are overwritten because of the first glyph
17573 string's left overhang. The background of all strings
17574 prepended must be drawn because the first glyph string
17575 draws over it. */
17576 i = left_overwritten (head);
17577 if (i >= 0)
17578 {
17579 j = i;
17580 BUILD_GLYPH_STRINGS (j, start, h, t,
17581 DRAW_NORMAL_TEXT, dummy_x, last_x);
17582 start = i;
17583 compute_overhangs_and_x (t, head->x, 1);
17584 prepend_glyph_string_lists (&head, &tail, h, t);
17585 }
17586
17587 /* Prepend glyph strings for glyphs in front of the first glyph
17588 string that overwrite that glyph string because of their
17589 right overhang. For these strings, only the foreground must
17590 be drawn, because it draws over the glyph string at `head'.
17591 The background must not be drawn because this would overwrite
17592 right overhangs of preceding glyphs for which no glyph
17593 strings exist. */
17594 i = left_overwriting (head);
17595 if (i >= 0)
17596 {
17597 BUILD_GLYPH_STRINGS (i, start, h, t,
17598 DRAW_NORMAL_TEXT, dummy_x, last_x);
17599 for (s = h; s; s = s->next)
17600 s->background_filled_p = 1;
17601 compute_overhangs_and_x (t, head->x, 1);
17602 prepend_glyph_string_lists (&head, &tail, h, t);
17603 }
17604
17605 /* Append glyphs strings for glyphs following the last glyph
17606 string tail that are overwritten by tail. The background of
17607 these strings has to be drawn because tail's foreground draws
17608 over it. */
17609 i = right_overwritten (tail);
17610 if (i >= 0)
17611 {
17612 BUILD_GLYPH_STRINGS (end, i, h, t,
17613 DRAW_NORMAL_TEXT, x, last_x);
17614 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17615 append_glyph_string_lists (&head, &tail, h, t);
17616 }
17617
17618 /* Append glyph strings for glyphs following the last glyph
17619 string tail that overwrite tail. The foreground of such
17620 glyphs has to be drawn because it writes into the background
17621 of tail. The background must not be drawn because it could
17622 paint over the foreground of following glyphs. */
17623 i = right_overwriting (tail);
17624 if (i >= 0)
17625 {
17626 BUILD_GLYPH_STRINGS (end, i, h, t,
17627 DRAW_NORMAL_TEXT, x, last_x);
17628 for (s = h; s; s = s->next)
17629 s->background_filled_p = 1;
17630 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17631 append_glyph_string_lists (&head, &tail, h, t);
17632 }
17633 }
17634
17635 /* Draw all strings. */
17636 for (s = head; s; s = s->next)
17637 rif->draw_glyph_string (s);
17638
17639 if (area == TEXT_AREA
17640 && !row->full_width_p
17641 /* When drawing overlapping rows, only the glyph strings'
17642 foreground is drawn, which doesn't erase a cursor
17643 completely. */
17644 && !overlaps_p)
17645 {
17646 int x0 = head ? head->x : x;
17647 int x1 = tail ? tail->x + tail->background_width : x;
17648
da8b7f4f
KS
17649 int text_left = window_box_left (w, TEXT_AREA);
17650 x0 -= text_left;
17651 x1 -= text_left;
133c764e 17652
da8b7f4f 17653 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
133c764e
KS
17654 row->y, MATRIX_ROW_BOTTOM_Y (row));
17655 }
17656
17657 /* Value is the x-position up to which drawn, relative to AREA of W.
17658 This doesn't include parts drawn because of overhangs. */
da8b7f4f
KS
17659 if (row->full_width_p)
17660 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
17661 else
17662 x_reached -= window_box_left (w, area);
133c764e
KS
17663
17664 RELEASE_HDC (hdc, f);
17665
17666 return x_reached;
17667}
17668
17669
17670/* Store one glyph for IT->char_to_display in IT->glyph_row.
17671 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17672
17673static INLINE void
17674append_glyph (it)
17675 struct it *it;
17676{
17677 struct glyph *glyph;
17678 enum glyph_row_area area = it->area;
17679
17680 xassert (it->glyph_row);
17681 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
17682
17683 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17684 if (glyph < it->glyph_row->glyphs[area + 1])
17685 {
17686 glyph->charpos = CHARPOS (it->position);
17687 glyph->object = it->object;
17688 glyph->pixel_width = it->pixel_width;
17689 glyph->voffset = it->voffset;
17690 glyph->type = CHAR_GLYPH;
17691 glyph->multibyte_p = it->multibyte_p;
17692 glyph->left_box_line_p = it->start_of_box_run_p;
17693 glyph->right_box_line_p = it->end_of_box_run_p;
17694 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17695 || it->phys_descent > it->descent);
17696 glyph->padding_p = 0;
17697 glyph->glyph_not_available_p = it->glyph_not_available_p;
17698 glyph->face_id = it->face_id;
17699 glyph->u.ch = it->char_to_display;
17700 glyph->font_type = FONT_TYPE_UNKNOWN;
17701 ++it->glyph_row->used[area];
17702 }
17703}
17704
17705/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
17706 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17707
17708static INLINE void
17709append_composite_glyph (it)
17710 struct it *it;
17711{
17712 struct glyph *glyph;
17713 enum glyph_row_area area = it->area;
17714
17715 xassert (it->glyph_row);
17716
17717 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17718 if (glyph < it->glyph_row->glyphs[area + 1])
17719 {
17720 glyph->charpos = CHARPOS (it->position);
17721 glyph->object = it->object;
17722 glyph->pixel_width = it->pixel_width;
17723 glyph->voffset = it->voffset;
17724 glyph->type = COMPOSITE_GLYPH;
17725 glyph->multibyte_p = it->multibyte_p;
17726 glyph->left_box_line_p = it->start_of_box_run_p;
17727 glyph->right_box_line_p = it->end_of_box_run_p;
17728 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17729 || it->phys_descent > it->descent);
17730 glyph->padding_p = 0;
17731 glyph->glyph_not_available_p = 0;
17732 glyph->face_id = it->face_id;
17733 glyph->u.cmp_id = it->cmp_id;
17734 glyph->font_type = FONT_TYPE_UNKNOWN;
17735 ++it->glyph_row->used[area];
17736 }
17737}
17738
17739
17740/* Change IT->ascent and IT->height according to the setting of
17741 IT->voffset. */
17742
17743static INLINE void
17744take_vertical_position_into_account (it)
17745 struct it *it;
17746{
17747 if (it->voffset)
17748 {
17749 if (it->voffset < 0)
17750 /* Increase the ascent so that we can display the text higher
17751 in the line. */
17752 it->ascent += abs (it->voffset);
17753 else
17754 /* Increase the descent so that we can display the text lower
17755 in the line. */
17756 it->descent += it->voffset;
17757 }
17758}
17759
17760
17761/* Produce glyphs/get display metrics for the image IT is loaded with.
17762 See the description of struct display_iterator in dispextern.h for
17763 an overview of struct display_iterator. */
17764
17765static void
17766produce_image_glyph (it)
17767 struct it *it;
17768{
17769 struct image *img;
17770 struct face *face;
17771
17772 xassert (it->what == IT_IMAGE);
17773
17774 face = FACE_FROM_ID (it->f, it->face_id);
17775 img = IMAGE_FROM_ID (it->f, it->image_id);
17776 xassert (img);
17777
17778 /* Make sure X resources of the face and image are loaded. */
17779 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17780 prepare_image_for_display (it->f, img);
17781
17782 it->ascent = it->phys_ascent = image_ascent (img, face);
17783 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
17784 it->pixel_width = img->width + 2 * img->hmargin;
17785
17786 it->nglyphs = 1;
17787
17788 if (face->box != FACE_NO_BOX)
17789 {
17790 if (face->box_line_width > 0)
17791 {
17792 it->ascent += face->box_line_width;
17793 it->descent += face->box_line_width;
17794 }
17795
17796 if (it->start_of_box_run_p)
17797 it->pixel_width += abs (face->box_line_width);
17798 if (it->end_of_box_run_p)
17799 it->pixel_width += abs (face->box_line_width);
17800 }
17801
17802 take_vertical_position_into_account (it);
17803
17804 if (it->glyph_row)
17805 {
17806 struct glyph *glyph;
17807 enum glyph_row_area area = it->area;
17808
17809 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17810 if (glyph < it->glyph_row->glyphs[area + 1])
17811 {
17812 glyph->charpos = CHARPOS (it->position);
17813 glyph->object = it->object;
17814 glyph->pixel_width = it->pixel_width;
17815 glyph->voffset = it->voffset;
17816 glyph->type = IMAGE_GLYPH;
17817 glyph->multibyte_p = it->multibyte_p;
17818 glyph->left_box_line_p = it->start_of_box_run_p;
17819 glyph->right_box_line_p = it->end_of_box_run_p;
17820 glyph->overlaps_vertically_p = 0;
17821 glyph->padding_p = 0;
17822 glyph->glyph_not_available_p = 0;
17823 glyph->face_id = it->face_id;
17824 glyph->u.img_id = img->id;
17825 glyph->font_type = FONT_TYPE_UNKNOWN;
17826 ++it->glyph_row->used[area];
17827 }
17828 }
17829}
17830
17831
17832/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
17833 of the glyph, WIDTH and HEIGHT are the width and height of the
f65536fa 17834 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
133c764e
KS
17835
17836static void
17837append_stretch_glyph (it, object, width, height, ascent)
17838 struct it *it;
17839 Lisp_Object object;
17840 int width, height;
f65536fa 17841 int ascent;
133c764e
KS
17842{
17843 struct glyph *glyph;
17844 enum glyph_row_area area = it->area;
17845
f65536fa 17846 xassert (ascent >= 0 && ascent <= height);
133c764e
KS
17847
17848 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17849 if (glyph < it->glyph_row->glyphs[area + 1])
17850 {
17851 glyph->charpos = CHARPOS (it->position);
17852 glyph->object = object;
17853 glyph->pixel_width = width;
17854 glyph->voffset = it->voffset;
17855 glyph->type = STRETCH_GLYPH;
17856 glyph->multibyte_p = it->multibyte_p;
17857 glyph->left_box_line_p = it->start_of_box_run_p;
17858 glyph->right_box_line_p = it->end_of_box_run_p;
17859 glyph->overlaps_vertically_p = 0;
17860 glyph->padding_p = 0;
17861 glyph->glyph_not_available_p = 0;
17862 glyph->face_id = it->face_id;
f65536fa 17863 glyph->u.stretch.ascent = ascent;
133c764e
KS
17864 glyph->u.stretch.height = height;
17865 glyph->font_type = FONT_TYPE_UNKNOWN;
17866 ++it->glyph_row->used[area];
17867 }
17868}
17869
17870
f65536fa
KS
17871/* Calculate a width or height in pixels from a specification using
17872 the following elements:
17873
17874 SPEC ::=
17875 NUM - a (fractional) multiple of the default font width/height
17876 (NUM) - specifies exactly NUM pixels
17877 UNIT - a fixed number of pixels, see below.
17878 ELEMENT - size of a display element in pixels, see below.
17879 (NUM . SPEC) - equals NUM * SPEC
17880 (+ SPEC SPEC ...) - add pixel values
17881 (- SPEC SPEC ...) - subtract pixel values
17882 (- SPEC) - negate pixel value
17883
17884 NUM ::=
17885 INT or FLOAT - a number constant
17886 SYMBOL - use symbol's (buffer local) variable binding.
17887
17888 UNIT ::=
17889 in - pixels per inch *)
17890 mm - pixels per 1/1000 meter *)
17891 cm - pixels per 1/100 meter *)
17892 width - width of current font in pixels.
17893 height - height of current font in pixels.
17894
17895 *) using the ratio(s) defined in display-pixels-per-inch.
17896
17897 ELEMENT ::=
17898
17899 left-fringe - left fringe width in pixels
17900 (left-fringe . nil) - left fringe width if inside margins, else 0
17901 (left-fringe . t) - left fringe width if outside margins, else 0
17902
17903 right-fringe - right fringe width in pixels
17904 (right-fringe . nil) - right fringe width if inside margins, else 0
17905 (right-fringe . t) - right fringe width if outside margins, else 0
17906
17907 left-margin - left margin width in pixels
17908 right-margin - right margin width in pixels
17909
17910 scroll-bar - scroll-bar area width in pixels
17911 (scroll-bar . left) - scroll-bar width if on left, else 0
17912 (scroll-bar . right) - scroll-bar width if on right, else 0
17913
17914 Examples:
17915
17916 Pixels corresponding to 5 inches:
17917 (5 . in)
17918
17919 Total width of non-text areas on left side of window:
17920 (+ left-fringe left-margin (scroll-bar . left))
17921
17922 Total width of fringes if inside display margins:
17923 (+ (left-fringe) (right-fringe))
17924
17925 Width of left margin minus width of 1 character in the default font:
17926 (- left-margin 1)
17927
17928 Width of left margin minus width of 2 characters in the current font:
17929 (- left-margin (2 . width))
17930
17931 Width of left fringe plus left margin minus one pixel:
17932 (- (+ left-fringe left-margin) (1))
17933 (+ left-fringe left-margin (- (1)))
17934 (+ left-fringe left-margin (-1))
17935
17936*/
17937
17938#define NUMVAL(X) \
17939 ((INTEGERP (X) || FLOATP (X)) \
17940 ? XFLOATINT (X) \
17941 : - 1)
17942
17943static int
17944calc_pixel_width_or_height (res, it, prop, font, width_p)
17945 double *res;
17946 struct it *it;
17947 Lisp_Object prop;
17948 XFontStruct *font;
17949 int width_p;
17950{
17951 double pixels;
17952
17953#define OK_PIXELS(val) ((*res = (val)), 1)
17954
17955 if (SYMBOLP (prop))
17956 {
17957 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17958 {
17959 char *unit = SDATA (SYMBOL_NAME (prop));
17960
17961 if (unit[0] == 'i' && unit[1] == 'n')
17962 pixels = 1.0;
17963 else if (unit[0] == 'm' && unit[1] == 'm')
17964 pixels = 25.4;
17965 else if (unit[0] == 'c' && unit[1] == 'm')
17966 pixels = 2.54;
17967 else
17968 pixels = 0;
17969 if (pixels > 0)
17970 {
17971 double ppi;
17972 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17973 || (CONSP (Vdisplay_pixels_per_inch)
17974 && (ppi = (width_p
17975 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17976 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17977 ppi > 0)))
17978 return OK_PIXELS (ppi / pixels);
17979
17980 return 0;
17981 }
17982 }
17983
17984 if (EQ (prop, Qheight))
17985 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
17986 if (EQ (prop, Qwidth))
17987 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
17988 if (EQ (prop, Qleft_fringe))
17989 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17990 if (EQ (prop, Qright_fringe))
17991 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17992 if (EQ (prop, Qleft_margin))
17993 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17994 if (EQ (prop, Qright_margin))
17995 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17996 if (EQ (prop, Qscroll_bar))
17997 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17998
17999 prop = Fbuffer_local_value (prop, it->w->buffer);
18000 }
18001
18002 if (INTEGERP (prop) || FLOATP (prop))
18003 {
18004 int base_unit = (width_p
18005 ? FRAME_COLUMN_WIDTH (it->f)
18006 : FRAME_LINE_HEIGHT (it->f));
18007 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18008 }
18009
18010 if (CONSP (prop))
18011 {
18012 Lisp_Object car = XCAR (prop);
18013 Lisp_Object cdr = XCDR (prop);
18014
18015 if (SYMBOLP (car))
18016 {
18017 if (EQ (car, Qplus) || EQ (car, Qminus))
18018 {
18019 int first = 1;
18020 double px;
18021
18022 pixels = 0;
18023 while (CONSP (cdr))
18024 {
18025 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr), font, width_p))
18026 return 0;
18027 if (first)
18028 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18029 else
18030 pixels += px;
18031 cdr = XCDR (cdr);
18032 }
18033 if (EQ (car, Qminus))
18034 pixels = -pixels;
18035 return OK_PIXELS (pixels);
18036 }
18037
18038 if (EQ (car, Qleft_fringe))
18039 return OK_PIXELS ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18040 == !NILP (cdr))
18041 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
18042 : 0);
18043 if (EQ (car, Qright_fringe))
18044 return OK_PIXELS ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18045 == !NILP (cdr))
18046 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18047 : 0);
18048 if (EQ (car, Qscroll_bar))
18049 return OK_PIXELS ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18050 == EQ (cdr, Qleft))
18051 ? WINDOW_SCROLL_BAR_AREA_WIDTH (it->w)
18052 : 0);
18053
18054 car = Fbuffer_local_value (car, it->w->buffer);
18055 }
18056
18057 if (INTEGERP (car) || FLOATP (car))
18058 {
18059 double fact;
18060 pixels = XFLOATINT (car);
18061 if (NILP (cdr))
18062 return OK_PIXELS (pixels);
18063 if (calc_pixel_width_or_height (&fact, it, cdr, font, width_p))
18064 return OK_PIXELS (pixels * fact);
18065 return 0;
18066 }
18067
18068 return 0;
18069 }
18070
18071 return 0;
18072}
18073
133c764e
KS
18074/* Produce a stretch glyph for iterator IT. IT->object is the value
18075 of the glyph property displayed. The value must be a list
18076 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18077 being recognized:
18078
18079 1. `:width WIDTH' specifies that the space should be WIDTH *
18080 canonical char width wide. WIDTH may be an integer or floating
18081 point number.
18082
18083 2. `:relative-width FACTOR' specifies that the width of the stretch
18084 should be computed from the width of the first character having the
18085 `glyph' property, and should be FACTOR times that width.
18086
18087 3. `:align-to HPOS' specifies that the space should be wide enough
18088 to reach HPOS, a value in canonical character units.
18089
18090 Exactly one of the above pairs must be present.
18091
18092 4. `:height HEIGHT' specifies that the height of the stretch produced
18093 should be HEIGHT, measured in canonical character units.
18094
18095 5. `:relative-height FACTOR' specifies that the height of the
18096 stretch should be FACTOR times the height of the characters having
18097 the glyph property.
18098
18099 Either none or exactly one of 4 or 5 must be present.
18100
18101 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18102 of the stretch should be used for the ascent of the stretch.
18103 ASCENT must be in the range 0 <= ASCENT <= 100. */
18104
133c764e
KS
18105static void
18106produce_stretch_glyph (it)
18107 struct it *it;
18108{
f65536fa 18109 /* (space :width WIDTH :height HEIGHT ...) */
133c764e
KS
18110 Lisp_Object prop, plist;
18111 int width = 0, height = 0;
f65536fa
KS
18112 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18113 int ascent = 0;
18114 double tem;
133c764e
KS
18115 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18116 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18117
18118 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18119
18120 /* List should start with `space'. */
18121 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18122 plist = XCDR (it->object);
18123
18124 /* Compute the width of the stretch. */
f65536fa
KS
18125 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18126 && calc_pixel_width_or_height (&tem, it, prop, font, 1))
18127 {
18128 /* Absolute width `:width WIDTH' specified and valid. */
18129 zero_width_ok_p = 1;
18130 width = (int)tem;
18131 }
133c764e
KS
18132 else if (prop = Fplist_get (plist, QCrelative_width),
18133 NUMVAL (prop) > 0)
18134 {
18135 /* Relative width `:relative-width FACTOR' specified and valid.
18136 Compute the width of the characters having the `glyph'
18137 property. */
18138 struct it it2;
18139 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18140
18141 it2 = *it;
18142 if (it->multibyte_p)
18143 {
18144 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18145 - IT_BYTEPOS (*it));
18146 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18147 }
18148 else
18149 it2.c = *p, it2.len = 1;
18150
18151 it2.glyph_row = NULL;
18152 it2.what = IT_CHARACTER;
18153 x_produce_glyphs (&it2);
18154 width = NUMVAL (prop) * it2.pixel_width;
18155 }
f65536fa
KS
18156 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18157 && calc_pixel_width_or_height (&tem, it, prop, font, 1))
18158 {
18159 width = max (0, (int)tem - it->current_x);
18160 zero_width_ok_p = 1;
18161 }
133c764e
KS
18162 else
18163 /* Nothing specified -> width defaults to canonical char width. */
da8b7f4f 18164 width = FRAME_COLUMN_WIDTH (it->f);
133c764e 18165
f65536fa
KS
18166 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18167 width = 1;
18168
133c764e 18169 /* Compute height. */
f65536fa
KS
18170 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18171 && calc_pixel_width_or_height (&tem, it, prop, font, 0))
18172 {
18173 height = (int)tem;
18174 zero_height_ok_p = 1;
18175 }
133c764e
KS
18176 else if (prop = Fplist_get (plist, QCrelative_height),
18177 NUMVAL (prop) > 0)
18178 height = FONT_HEIGHT (font) * NUMVAL (prop);
18179 else
18180 height = FONT_HEIGHT (font);
18181
f65536fa
KS
18182 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18183 height = 1;
18184
133c764e
KS
18185 /* Compute percentage of height used for ascent. If
18186 `:ascent ASCENT' is present and valid, use that. Otherwise,
18187 derive the ascent from the font in use. */
18188 if (prop = Fplist_get (plist, QCascent),
18189 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
f65536fa
KS
18190 ascent = height * NUMVAL (prop) / 100.0;
18191 else if (!NILP (prop)
18192 && calc_pixel_width_or_height (&tem, it, prop, font, 0))
18193 ascent = min (max (0, (int)tem), height);
133c764e 18194 else
f65536fa 18195 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
133c764e 18196
f65536fa 18197 if (width > 0 && height > 0 && it->glyph_row)
133c764e
KS
18198 {
18199 Lisp_Object object = it->stack[it->sp - 1].string;
18200 if (!STRINGP (object))
18201 object = it->w->buffer;
18202 append_stretch_glyph (it, object, width, height, ascent);
18203 }
18204
18205 it->pixel_width = width;
f65536fa 18206 it->ascent = it->phys_ascent = ascent;
133c764e 18207 it->descent = it->phys_descent = height - it->ascent;
f65536fa 18208 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
133c764e 18209
f65536fa 18210 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
133c764e
KS
18211 {
18212 if (face->box_line_width > 0)
18213 {
18214 it->ascent += face->box_line_width;
18215 it->descent += face->box_line_width;
18216 }
18217
18218 if (it->start_of_box_run_p)
18219 it->pixel_width += abs (face->box_line_width);
18220 if (it->end_of_box_run_p)
18221 it->pixel_width += abs (face->box_line_width);
18222 }
18223
18224 take_vertical_position_into_account (it);
18225}
18226
18227/* RIF:
18228 Produce glyphs/get display metrics for the display element IT is
18229 loaded with. See the description of struct display_iterator in
18230 dispextern.h for an overview of struct display_iterator. */
18231
18232void
18233x_produce_glyphs (it)
18234 struct it *it;
18235{
18236 it->glyph_not_available_p = 0;
18237
18238 if (it->what == IT_CHARACTER)
18239 {
18240 XChar2b char2b;
18241 XFontStruct *font;
18242 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18243 XCharStruct *pcm;
18244 int font_not_found_p;
18245 struct font_info *font_info;
18246 int boff; /* baseline offset */
18247 /* We may change it->multibyte_p upon unibyte<->multibyte
18248 conversion. So, save the current value now and restore it
18249 later.
18250
18251 Note: It seems that we don't have to record multibyte_p in
18252 struct glyph because the character code itself tells if or
18253 not the character is multibyte. Thus, in the future, we must
18254 consider eliminating the field `multibyte_p' in the struct
18255 glyph. */
18256 int saved_multibyte_p = it->multibyte_p;
18257
18258 /* Maybe translate single-byte characters to multibyte, or the
18259 other way. */
18260 it->char_to_display = it->c;
18261 if (!ASCII_BYTE_P (it->c))
18262 {
18263 if (unibyte_display_via_language_environment
18264 && SINGLE_BYTE_CHAR_P (it->c)
18265 && (it->c >= 0240
18266 || !NILP (Vnonascii_translation_table)))
18267 {
18268 it->char_to_display = unibyte_char_to_multibyte (it->c);
18269 it->multibyte_p = 1;
18270 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18271 face = FACE_FROM_ID (it->f, it->face_id);
18272 }
18273 else if (!SINGLE_BYTE_CHAR_P (it->c)
18274 && !it->multibyte_p)
18275 {
18276 it->multibyte_p = 1;
18277 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18278 face = FACE_FROM_ID (it->f, it->face_id);
18279 }
18280 }
18281
18282 /* Get font to use. Encode IT->char_to_display. */
18283 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18284 &char2b, it->multibyte_p, 0);
18285 font = face->font;
18286
18287 /* When no suitable font found, use the default font. */
18288 font_not_found_p = font == NULL;
18289 if (font_not_found_p)
18290 {
18291 font = FRAME_FONT (it->f);
18292 boff = FRAME_BASELINE_OFFSET (it->f);
18293 font_info = NULL;
18294 }
18295 else
18296 {
18297 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18298 boff = font_info->baseline_offset;
18299 if (font_info->vertical_centering)
18300 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18301 }
18302
18303 if (it->char_to_display >= ' '
18304 && (!it->multibyte_p || it->char_to_display < 128))
18305 {
18306 /* Either unibyte or ASCII. */
18307 int stretched_p;
18308
18309 it->nglyphs = 1;
18310
18311 pcm = rif->per_char_metric (font, &char2b,
18312 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18313 it->ascent = FONT_BASE (font) + boff;
18314 it->descent = FONT_DESCENT (font) - boff;
18315
18316 if (pcm)
18317 {
18318 it->phys_ascent = pcm->ascent + boff;
18319 it->phys_descent = pcm->descent - boff;
18320 it->pixel_width = pcm->width;
18321 }
18322 else
18323 {
18324 it->glyph_not_available_p = 1;
18325 it->phys_ascent = FONT_BASE (font) + boff;
18326 it->phys_descent = FONT_DESCENT (font) - boff;
18327 it->pixel_width = FONT_WIDTH (font);
18328 }
18329
18330 /* If this is a space inside a region of text with
18331 `space-width' property, change its width. */
18332 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18333 if (stretched_p)
18334 it->pixel_width *= XFLOATINT (it->space_width);
18335
18336 /* If face has a box, add the box thickness to the character
18337 height. If character has a box line to the left and/or
18338 right, add the box line width to the character's width. */
18339 if (face->box != FACE_NO_BOX)
18340 {
18341 int thick = face->box_line_width;
18342
18343 if (thick > 0)
18344 {
18345 it->ascent += thick;
18346 it->descent += thick;
18347 }
18348 else
18349 thick = -thick;
18350
18351 if (it->start_of_box_run_p)
18352 it->pixel_width += thick;
18353 if (it->end_of_box_run_p)
18354 it->pixel_width += thick;
18355 }
18356
18357 /* If face has an overline, add the height of the overline
18358 (1 pixel) and a 1 pixel margin to the character height. */
18359 if (face->overline_p)
18360 it->ascent += 2;
18361
18362 take_vertical_position_into_account (it);
18363
18364 /* If we have to actually produce glyphs, do it. */
18365 if (it->glyph_row)
18366 {
18367 if (stretched_p)
18368 {
18369 /* Translate a space with a `space-width' property
18370 into a stretch glyph. */
f65536fa
KS
18371 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18372 / FONT_HEIGHT (font));
133c764e
KS
18373 append_stretch_glyph (it, it->object, it->pixel_width,
18374 it->ascent + it->descent, ascent);
18375 }
18376 else
18377 append_glyph (it);
18378
18379 /* If characters with lbearing or rbearing are displayed
18380 in this line, record that fact in a flag of the
18381 glyph row. This is used to optimize X output code. */
18382 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18383 it->glyph_row->contains_overlapping_glyphs_p = 1;
18384 }
18385 }
18386 else if (it->char_to_display == '\n')
18387 {
18388 /* A newline has no width but we need the height of the line. */
18389 it->pixel_width = 0;
18390 it->nglyphs = 0;
18391 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18392 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18393
18394 if (face->box != FACE_NO_BOX
18395 && face->box_line_width > 0)
18396 {
18397 it->ascent += face->box_line_width;
18398 it->descent += face->box_line_width;
18399 }
18400 }
18401 else if (it->char_to_display == '\t')
18402 {
da8b7f4f 18403 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
133c764e
KS
18404 int x = it->current_x + it->continuation_lines_width;
18405 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18406
18407 /* If the distance from the current position to the next tab
18408 stop is less than a canonical character width, use the
18409 tab stop after that. */
da8b7f4f 18410 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
133c764e
KS
18411 next_tab_x += tab_width;
18412
18413 it->pixel_width = next_tab_x - x;
18414 it->nglyphs = 1;
18415 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18416 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18417
18418 if (it->glyph_row)
18419 {
133c764e 18420 append_stretch_glyph (it, it->object, it->pixel_width,
f65536fa 18421 it->ascent + it->descent, it->ascent);
133c764e
KS
18422 }
18423 }
18424 else
18425 {
18426 /* A multi-byte character. Assume that the display width of the
18427 character is the width of the character multiplied by the
18428 width of the font. */
18429
18430 /* If we found a font, this font should give us the right
18431 metrics. If we didn't find a font, use the frame's
18432 default font and calculate the width of the character
18433 from the charset width; this is what old redisplay code
18434 did. */
18435
18436 pcm = rif->per_char_metric (font, &char2b,
18437 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18438
18439 if (font_not_found_p || !pcm)
18440 {
18441 int charset = CHAR_CHARSET (it->char_to_display);
18442
18443 it->glyph_not_available_p = 1;
da8b7f4f 18444 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
133c764e
KS
18445 * CHARSET_WIDTH (charset));
18446 it->phys_ascent = FONT_BASE (font) + boff;
18447 it->phys_descent = FONT_DESCENT (font) - boff;
18448 }
18449 else
18450 {
18451 it->pixel_width = pcm->width;
18452 it->phys_ascent = pcm->ascent + boff;
18453 it->phys_descent = pcm->descent - boff;
18454 if (it->glyph_row
18455 && (pcm->lbearing < 0
18456 || pcm->rbearing > pcm->width))
18457 it->glyph_row->contains_overlapping_glyphs_p = 1;
18458 }
18459 it->nglyphs = 1;
18460 it->ascent = FONT_BASE (font) + boff;
18461 it->descent = FONT_DESCENT (font) - boff;
18462 if (face->box != FACE_NO_BOX)
18463 {
18464 int thick = face->box_line_width;
18465
18466 if (thick > 0)
18467 {
18468 it->ascent += thick;
18469 it->descent += thick;
18470 }
18471 else
18472 thick = - thick;
18473
18474 if (it->start_of_box_run_p)
18475 it->pixel_width += thick;
18476 if (it->end_of_box_run_p)
18477 it->pixel_width += thick;
18478 }
18479
18480 /* If face has an overline, add the height of the overline
18481 (1 pixel) and a 1 pixel margin to the character height. */
18482 if (face->overline_p)
18483 it->ascent += 2;
18484
18485 take_vertical_position_into_account (it);
18486
18487 if (it->glyph_row)
18488 append_glyph (it);
18489 }
18490 it->multibyte_p = saved_multibyte_p;
18491 }
18492 else if (it->what == IT_COMPOSITION)
18493 {
18494 /* Note: A composition is represented as one glyph in the
18495 glyph matrix. There are no padding glyphs. */
18496 XChar2b char2b;
18497 XFontStruct *font;
18498 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18499 XCharStruct *pcm;
18500 int font_not_found_p;
18501 struct font_info *font_info;
18502 int boff; /* baseline offset */
18503 struct composition *cmp = composition_table[it->cmp_id];
18504
18505 /* Maybe translate single-byte characters to multibyte. */
18506 it->char_to_display = it->c;
18507 if (unibyte_display_via_language_environment
18508 && SINGLE_BYTE_CHAR_P (it->c)
18509 && (it->c >= 0240
18510 || (it->c >= 0200
18511 && !NILP (Vnonascii_translation_table))))
18512 {
18513 it->char_to_display = unibyte_char_to_multibyte (it->c);
18514 }
18515
18516 /* Get face and font to use. Encode IT->char_to_display. */
18517 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18518 face = FACE_FROM_ID (it->f, it->face_id);
18519 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18520 &char2b, it->multibyte_p, 0);
18521 font = face->font;
18522
18523 /* When no suitable font found, use the default font. */
18524 font_not_found_p = font == NULL;
18525 if (font_not_found_p)
18526 {
18527 font = FRAME_FONT (it->f);
18528 boff = FRAME_BASELINE_OFFSET (it->f);
18529 font_info = NULL;
18530 }
18531 else
18532 {
18533 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18534 boff = font_info->baseline_offset;
18535 if (font_info->vertical_centering)
18536 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18537 }
18538
18539 /* There are no padding glyphs, so there is only one glyph to
18540 produce for the composition. Important is that pixel_width,
18541 ascent and descent are the values of what is drawn by
18542 draw_glyphs (i.e. the values of the overall glyphs composed). */
18543 it->nglyphs = 1;
18544
18545 /* If we have not yet calculated pixel size data of glyphs of
18546 the composition for the current face font, calculate them
18547 now. Theoretically, we have to check all fonts for the
18548 glyphs, but that requires much time and memory space. So,
18549 here we check only the font of the first glyph. This leads
18550 to incorrect display very rarely, and C-l (recenter) can
18551 correct the display anyway. */
18552 if (cmp->font != (void *) font)
18553 {
18554 /* Ascent and descent of the font of the first character of
18555 this composition (adjusted by baseline offset). Ascent
18556 and descent of overall glyphs should not be less than
18557 them respectively. */
18558 int font_ascent = FONT_BASE (font) + boff;
18559 int font_descent = FONT_DESCENT (font) - boff;
18560 /* Bounding box of the overall glyphs. */
18561 int leftmost, rightmost, lowest, highest;
18562 int i, width, ascent, descent;
18563
18564 cmp->font = (void *) font;
18565
18566 /* Initialize the bounding box. */
18567 if (font_info
18568 && (pcm = rif->per_char_metric (font, &char2b,
18569 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
18570 {
18571 width = pcm->width;
18572 ascent = pcm->ascent;
18573 descent = pcm->descent;
18574 }
18575 else
18576 {
18577 width = FONT_WIDTH (font);
18578 ascent = FONT_BASE (font);
18579 descent = FONT_DESCENT (font);
18580 }
18581
18582 rightmost = width;
18583 lowest = - descent + boff;
18584 highest = ascent + boff;
18585 leftmost = 0;
18586
18587 if (font_info
18588 && font_info->default_ascent
18589 && CHAR_TABLE_P (Vuse_default_ascent)
18590 && !NILP (Faref (Vuse_default_ascent,
18591 make_number (it->char_to_display))))
18592 highest = font_info->default_ascent + boff;
18593
18594 /* Draw the first glyph at the normal position. It may be
18595 shifted to right later if some other glyphs are drawn at
18596 the left. */
18597 cmp->offsets[0] = 0;
18598 cmp->offsets[1] = boff;
18599
18600 /* Set cmp->offsets for the remaining glyphs. */
18601 for (i = 1; i < cmp->glyph_len; i++)
18602 {
18603 int left, right, btm, top;
18604 int ch = COMPOSITION_GLYPH (cmp, i);
18605 int face_id = FACE_FOR_CHAR (it->f, face, ch);
18606
18607 face = FACE_FROM_ID (it->f, face_id);
18608 get_char_face_and_encoding (it->f, ch, face->id,
18609 &char2b, it->multibyte_p, 0);
18610 font = face->font;
18611 if (font == NULL)
18612 {
18613 font = FRAME_FONT (it->f);
2a6d0874 18614 boff = FRAME_BASELINE_OFFSET (it->f);
133c764e
KS
18615 font_info = NULL;
18616 }
18617 else
18618 {
18619 font_info
18620 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18621 boff = font_info->baseline_offset;
18622 if (font_info->vertical_centering)
18623 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18624 }
18625
18626 if (font_info
2a6d0874 18627 && (pcm = rif->per_char_metric (font, &char2b,
133c764e
KS
18628 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
18629 {
18630 width = pcm->width;
18631 ascent = pcm->ascent;
18632 descent = pcm->descent;
18633 }
18634 else
18635 {
18636 width = FONT_WIDTH (font);
18637 ascent = 1;
18638 descent = 0;
18639 }
18640
18641 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
18642 {
18643 /* Relative composition with or without
18644 alternate chars. */
18645 left = (leftmost + rightmost - width) / 2;
18646 btm = - descent + boff;
18647 if (font_info && font_info->relative_compose
18648 && (! CHAR_TABLE_P (Vignore_relative_composition)
18649 || NILP (Faref (Vignore_relative_composition,
18650 make_number (ch)))))
18651 {
18652
18653 if (- descent >= font_info->relative_compose)
18654 /* One extra pixel between two glyphs. */
18655 btm = highest + 1;
18656 else if (ascent <= 0)
18657 /* One extra pixel between two glyphs. */
18658 btm = lowest - 1 - ascent - descent;
18659 }
18660 }
18661 else
18662 {
18663 /* A composition rule is specified by an integer
18664 value that encodes global and new reference
18665 points (GREF and NREF). GREF and NREF are
18666 specified by numbers as below:
18667
18668 0---1---2 -- ascent
18669 | |
18670 | |
18671 | |
18672 9--10--11 -- center
18673 | |
18674 ---3---4---5--- baseline
18675 | |
18676 6---7---8 -- descent
18677 */
18678 int rule = COMPOSITION_RULE (cmp, i);
18679 int gref, nref, grefx, grefy, nrefx, nrefy;
18680
18681 COMPOSITION_DECODE_RULE (rule, gref, nref);
18682 grefx = gref % 3, nrefx = nref % 3;
18683 grefy = gref / 3, nrefy = nref / 3;
18684
18685 left = (leftmost
18686 + grefx * (rightmost - leftmost) / 2
18687 - nrefx * width / 2);
18688 btm = ((grefy == 0 ? highest
18689 : grefy == 1 ? 0
18690 : grefy == 2 ? lowest
18691 : (highest + lowest) / 2)
18692 - (nrefy == 0 ? ascent + descent
18693 : nrefy == 1 ? descent - boff
18694 : nrefy == 2 ? 0
18695 : (ascent + descent) / 2));
18696 }
18697
18698 cmp->offsets[i * 2] = left;
18699 cmp->offsets[i * 2 + 1] = btm + descent;
18700
18701 /* Update the bounding box of the overall glyphs. */
18702 right = left + width;
18703 top = btm + descent + ascent;
18704 if (left < leftmost)
18705 leftmost = left;
18706 if (right > rightmost)
18707 rightmost = right;
18708 if (top > highest)
18709 highest = top;
18710 if (btm < lowest)
18711 lowest = btm;
18712 }
18713
18714 /* If there are glyphs whose x-offsets are negative,
18715 shift all glyphs to the right and make all x-offsets
18716 non-negative. */
18717 if (leftmost < 0)
18718 {
18719 for (i = 0; i < cmp->glyph_len; i++)
18720 cmp->offsets[i * 2] -= leftmost;
18721 rightmost -= leftmost;
18722 }
18723
18724 cmp->pixel_width = rightmost;
18725 cmp->ascent = highest;
18726 cmp->descent = - lowest;
18727 if (cmp->ascent < font_ascent)
18728 cmp->ascent = font_ascent;
18729 if (cmp->descent < font_descent)
18730 cmp->descent = font_descent;
18731 }
18732
18733 it->pixel_width = cmp->pixel_width;
18734 it->ascent = it->phys_ascent = cmp->ascent;
18735 it->descent = it->phys_descent = cmp->descent;
18736
18737 if (face->box != FACE_NO_BOX)
18738 {
18739 int thick = face->box_line_width;
18740
18741 if (thick > 0)
18742 {
18743 it->ascent += thick;
18744 it->descent += thick;
18745 }
18746 else
18747 thick = - thick;
18748
18749 if (it->start_of_box_run_p)
18750 it->pixel_width += thick;
18751 if (it->end_of_box_run_p)
18752 it->pixel_width += thick;
18753 }
18754
18755 /* If face has an overline, add the height of the overline
18756 (1 pixel) and a 1 pixel margin to the character height. */
18757 if (face->overline_p)
18758 it->ascent += 2;
18759
18760 take_vertical_position_into_account (it);
18761
18762 if (it->glyph_row)
18763 append_composite_glyph (it);
18764 }
18765 else if (it->what == IT_IMAGE)
18766 produce_image_glyph (it);
18767 else if (it->what == IT_STRETCH)
18768 produce_stretch_glyph (it);
18769
18770 /* Accumulate dimensions. Note: can't assume that it->descent > 0
18771 because this isn't true for images with `:ascent 100'. */
18772 xassert (it->ascent >= 0 && it->descent >= 0);
18773 if (it->area == TEXT_AREA)
18774 it->current_x += it->pixel_width;
18775
18776 it->descent += it->extra_line_spacing;
18777
18778 it->max_ascent = max (it->max_ascent, it->ascent);
18779 it->max_descent = max (it->max_descent, it->descent);
18780 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
18781 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
18782}
18783
fa3c6b4d
KS
18784/* EXPORT for RIF:
18785 Output LEN glyphs starting at START at the nominal cursor position.
18786 Advance the nominal cursor over the text. The global variable
18787 updated_window contains the window being updated, updated_row is
18788 the glyph row being updated, and updated_area is the area of that
18789 row being updated. */
133c764e 18790
fa3c6b4d
KS
18791void
18792x_write_glyphs (start, len)
18793 struct glyph *start;
18794 int len;
18795{
18796 int x, hpos;
133c764e 18797
fa3c6b4d
KS
18798 xassert (updated_window && updated_row);
18799 BLOCK_INPUT;
133c764e 18800
fa3c6b4d 18801 /* Write glyphs. */
cfe03a41 18802
fa3c6b4d
KS
18803 hpos = start - updated_row->glyphs[updated_area];
18804 x = draw_glyphs (updated_window, output_cursor.x,
18805 updated_row, updated_area,
18806 hpos, hpos + len,
18807 DRAW_NORMAL_TEXT, 0);
cfe03a41 18808
fa3c6b4d
KS
18809 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
18810 if (updated_area == TEXT_AREA
18811 && updated_window->phys_cursor_on_p
18812 && updated_window->phys_cursor.vpos == output_cursor.vpos
18813 && updated_window->phys_cursor.hpos >= hpos
18814 && updated_window->phys_cursor.hpos < hpos + len)
18815 updated_window->phys_cursor_on_p = 0;
cfe03a41 18816
fa3c6b4d 18817 UNBLOCK_INPUT;
cfe03a41 18818
fa3c6b4d
KS
18819 /* Advance the output cursor. */
18820 output_cursor.hpos += len;
18821 output_cursor.x = x;
18822}
cfe03a41 18823
cfe03a41 18824
fa3c6b4d
KS
18825/* EXPORT for RIF:
18826 Insert LEN glyphs from START at the nominal cursor position. */
18827
18828void
18829x_insert_glyphs (start, len)
18830 struct glyph *start;
18831 int len;
18832{
18833 struct frame *f;
18834 struct window *w;
18835 int line_height, shift_by_width, shifted_region_width;
18836 struct glyph_row *row;
18837 struct glyph *glyph;
18838 int frame_x, frame_y, hpos;
18839
18840 xassert (updated_window && updated_row);
18841 BLOCK_INPUT;
18842 w = updated_window;
18843 f = XFRAME (WINDOW_FRAME (w));
18844
18845 /* Get the height of the line we are in. */
18846 row = updated_row;
18847 line_height = row->height;
18848
18849 /* Get the width of the glyphs to insert. */
18850 shift_by_width = 0;
18851 for (glyph = start; glyph < start + len; ++glyph)
18852 shift_by_width += glyph->pixel_width;
18853
18854 /* Get the width of the region to shift right. */
18855 shifted_region_width = (window_box_width (w, updated_area)
18856 - output_cursor.x
18857 - shift_by_width);
18858
18859 /* Shift right. */
18860 frame_x = window_box_left (w, updated_area) + output_cursor.x;
18861 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
18862
7b7b454e
AS
18863 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
18864 line_height, shift_by_width);
fa3c6b4d
KS
18865
18866 /* Write the glyphs. */
18867 hpos = start - row->glyphs[updated_area];
18868 draw_glyphs (w, output_cursor.x, row, updated_area,
18869 hpos, hpos + len,
18870 DRAW_NORMAL_TEXT, 0);
18871
18872 /* Advance the output cursor. */
18873 output_cursor.hpos += len;
18874 output_cursor.x += shift_by_width;
18875 UNBLOCK_INPUT;
18876}
18877
18878
18879/* EXPORT for RIF:
18880 Erase the current text line from the nominal cursor position
18881 (inclusive) to pixel column TO_X (exclusive). The idea is that
18882 everything from TO_X onward is already erased.
18883
18884 TO_X is a pixel position relative to updated_area of
18885 updated_window. TO_X == -1 means clear to the end of this area. */
18886
18887void
18888x_clear_end_of_line (to_x)
18889 int to_x;
18890{
18891 struct frame *f;
18892 struct window *w = updated_window;
18893 int max_x, min_y, max_y;
18894 int from_x, from_y, to_y;
18895
18896 xassert (updated_window && updated_row);
18897 f = XFRAME (w->frame);
18898
18899 if (updated_row->full_width_p)
da8b7f4f 18900 max_x = WINDOW_TOTAL_WIDTH (w);
fa3c6b4d
KS
18901 else
18902 max_x = window_box_width (w, updated_area);
18903 max_y = window_text_bottom_y (w);
18904
18905 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
18906 of window. For TO_X > 0, truncate to end of drawing area. */
18907 if (to_x == 0)
18908 return;
18909 else if (to_x < 0)
18910 to_x = max_x;
18911 else
18912 to_x = min (to_x, max_x);
18913
18914 to_y = min (max_y, output_cursor.y + updated_row->height);
18915
18916 /* Notice if the cursor will be cleared by this operation. */
18917 if (!updated_row->full_width_p)
18918 notice_overwritten_cursor (w, updated_area,
18919 output_cursor.x, -1,
18920 updated_row->y,
18921 MATRIX_ROW_BOTTOM_Y (updated_row));
18922
18923 from_x = output_cursor.x;
18924
18925 /* Translate to frame coordinates. */
18926 if (updated_row->full_width_p)
18927 {
18928 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
18929 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
18930 }
18931 else
18932 {
da8b7f4f
KS
18933 int area_left = window_box_left (w, updated_area);
18934 from_x += area_left;
18935 to_x += area_left;
fa3c6b4d
KS
18936 }
18937
da8b7f4f 18938 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
fa3c6b4d
KS
18939 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
18940 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
18941
18942 /* Prevent inadvertently clearing to end of the X window. */
18943 if (to_x > from_x && to_y > from_y)
18944 {
18945 BLOCK_INPUT;
18946 rif->clear_frame_area (f, from_x, from_y,
18947 to_x - from_x, to_y - from_y);
18948 UNBLOCK_INPUT;
18949 }
18950}
18951
18952#endif /* HAVE_WINDOW_SYSTEM */
18953
18954
18955\f
18956/***********************************************************************
18957 Cursor types
18958 ***********************************************************************/
18959
18960/* Value is the internal representation of the specified cursor type
18961 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
18962 of the bar cursor. */
18963
f65536fa 18964static enum text_cursor_kinds
fa3c6b4d
KS
18965get_specified_cursor_type (arg, width)
18966 Lisp_Object arg;
18967 int *width;
18968{
18969 enum text_cursor_kinds type;
18970
18971 if (NILP (arg))
18972 return NO_CURSOR;
18973
18974 if (EQ (arg, Qbox))
18975 return FILLED_BOX_CURSOR;
18976
18977 if (EQ (arg, Qhollow))
18978 return HOLLOW_BOX_CURSOR;
18979
18980 if (EQ (arg, Qbar))
18981 {
18982 *width = 2;
18983 return BAR_CURSOR;
18984 }
18985
18986 if (CONSP (arg)
18987 && EQ (XCAR (arg), Qbar)
18988 && INTEGERP (XCDR (arg))
18989 && XINT (XCDR (arg)) >= 0)
18990 {
18991 *width = XINT (XCDR (arg));
18992 return BAR_CURSOR;
18993 }
18994
18995 if (EQ (arg, Qhbar))
18996 {
18997 *width = 2;
18998 return HBAR_CURSOR;
18999 }
19000
19001 if (CONSP (arg)
19002 && EQ (XCAR (arg), Qhbar)
19003 && INTEGERP (XCDR (arg))
19004 && XINT (XCDR (arg)) >= 0)
19005 {
19006 *width = XINT (XCDR (arg));
19007 return HBAR_CURSOR;
19008 }
19009
19010 /* Treat anything unknown as "hollow box cursor".
19011 It was bad to signal an error; people have trouble fixing
19012 .Xdefaults with Emacs, when it has something bad in it. */
19013 type = HOLLOW_BOX_CURSOR;
19014
19015 return type;
19016}
19017
19018/* Set the default cursor types for specified frame. */
19019void
19020set_frame_cursor_types (f, arg)
19021 struct frame *f;
19022 Lisp_Object arg;
19023{
19024 int width;
19025 Lisp_Object tem;
19026
19027 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19028 FRAME_CURSOR_WIDTH (f) = width;
19029
19030 /* By default, set up the blink-off state depending on the on-state. */
19031
19032 tem = Fassoc (arg, Vblink_cursor_alist);
19033 if (!NILP (tem))
19034 {
19035 FRAME_BLINK_OFF_CURSOR (f)
19036 = get_specified_cursor_type (XCDR (tem), &width);
19037 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19038 }
19039 else
19040 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19041}
19042
19043
19044/* Return the cursor we want to be displayed in window W. Return
19045 width of bar/hbar cursor through WIDTH arg. Return with
19046 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19047 (i.e. if the `system caret' should track this cursor).
19048
19049 In a mini-buffer window, we want the cursor only to appear if we
19050 are reading input from this window. For the selected window, we
19051 want the cursor type given by the frame parameter or buffer local
19052 setting of cursor-type. If explicitly marked off, draw no cursor.
19053 In all other cases, we want a hollow box cursor. */
19054
f65536fa
KS
19055static enum text_cursor_kinds
19056get_window_cursor_type (w, glyph, width, active_cursor)
fa3c6b4d 19057 struct window *w;
f65536fa 19058 struct glyph *glyph;
fa3c6b4d
KS
19059 int *width;
19060 int *active_cursor;
19061{
19062 struct frame *f = XFRAME (w->frame);
19063 struct buffer *b = XBUFFER (w->buffer);
19064 int cursor_type = DEFAULT_CURSOR;
19065 Lisp_Object alt_cursor;
19066 int non_selected = 0;
19067
19068 *active_cursor = 1;
19069
19070 /* Echo area */
19071 if (cursor_in_echo_area
19072 && FRAME_HAS_MINIBUF_P (f)
19073 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19074 {
19075 if (w == XWINDOW (echo_area_window))
19076 {
19077 *width = FRAME_CURSOR_WIDTH (f);
19078 return FRAME_DESIRED_CURSOR (f);
19079 }
19080
19081 *active_cursor = 0;
19082 non_selected = 1;
19083 }
19084
19085 /* Nonselected window or nonselected frame. */
19086 else if (w != XWINDOW (f->selected_window)
19087#ifdef HAVE_WINDOW_SYSTEM
19088 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19089#endif
19090 )
19091 {
19092 *active_cursor = 0;
19093
19094 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19095 return NO_CURSOR;
19096
19097 non_selected = 1;
19098 }
19099
19100 /* Never display a cursor in a window in which cursor-type is nil. */
19101 if (NILP (b->cursor_type))
19102 return NO_CURSOR;
19103
19104 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19105 if (non_selected)
19106 {
19107 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19108 return get_specified_cursor_type (alt_cursor, width);
19109 }
19110
19111 /* Get the normal cursor type for this window. */
19112 if (EQ (b->cursor_type, Qt))
19113 {
19114 cursor_type = FRAME_DESIRED_CURSOR (f);
19115 *width = FRAME_CURSOR_WIDTH (f);
19116 }
19117 else
19118 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19119
19120 /* Use normal cursor if not blinked off. */
19121 if (!w->cursor_off_p)
f65536fa
KS
19122 {
19123 if (glyph->type == IMAGE_GLYPH) {
19124 if (cursor_type == FILLED_BOX_CURSOR)
19125 cursor_type = HOLLOW_BOX_CURSOR;
19126 }
19127 return cursor_type;
19128 }
fa3c6b4d
KS
19129
19130 /* Cursor is blinked off, so determine how to "toggle" it. */
19131
19132 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19133 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19134 return get_specified_cursor_type (XCDR (alt_cursor), width);
19135
19136 /* Then see if frame has specified a specific blink off cursor type. */
19137 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19138 {
19139 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19140 return FRAME_BLINK_OFF_CURSOR (f);
19141 }
19142
a57dd8b1
KS
19143#if 0
19144 /* Some people liked having a permanently visible blinking cursor,
19145 while others had very strong opinions against it. So it was
19146 decided to remove it. KFS 2003-09-03 */
19147
fa3c6b4d
KS
19148 /* Finally perform built-in cursor blinking:
19149 filled box <-> hollow box
19150 wide [h]bar <-> narrow [h]bar
19151 narrow [h]bar <-> no cursor
19152 other type <-> no cursor */
19153
19154 if (cursor_type == FILLED_BOX_CURSOR)
19155 return HOLLOW_BOX_CURSOR;
19156
19157 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19158 {
19159 *width = 1;
19160 return cursor_type;
19161 }
a57dd8b1 19162#endif
fa3c6b4d
KS
19163
19164 return NO_CURSOR;
19165}
19166
19167
19168#ifdef HAVE_WINDOW_SYSTEM
19169
19170/* Notice when the text cursor of window W has been completely
19171 overwritten by a drawing operation that outputs glyphs in AREA
19172 starting at X0 and ending at X1 in the line starting at Y0 and
19173 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19174 the rest of the line after X0 has been written. Y coordinates
19175 are window-relative. */
19176
19177static void
19178notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19179 struct window *w;
19180 enum glyph_row_area area;
19181 int x0, y0, x1, y1;
19182{
fa3c6b4d
KS
19183 if (area == TEXT_AREA && w->phys_cursor_on_p)
19184 {
19185 int cx0 = w->phys_cursor.x;
19186 int cx1 = cx0 + w->phys_cursor_width;
19187 int cy0 = w->phys_cursor.y;
19188 int cy1 = cy0 + w->phys_cursor_height;
19189
19190 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
19191 {
19192 /* The cursor image will be completely removed from the
19193 screen if the output area intersects the cursor area in
19194 y-direction. When we draw in [y0 y1[, and some part of
19195 the cursor is at y < y0, that part must have been drawn
19196 before. When scrolling, the cursor is erased before
19197 actually scrolling, so we don't come here. When not
19198 scrolling, the rows above the old cursor row must have
19199 changed, and in this case these rows must have written
19200 over the cursor image.
19201
19202 Likewise if part of the cursor is below y1, with the
19203 exception of the cursor being in the first blank row at
19204 the buffer and window end because update_text_area
19205 doesn't draw that row. (Except when it does, but
19206 that's handled in update_text_area.) */
19207
19208 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
19209 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
19210 w->phys_cursor_on_p = 0;
19211 }
19212 }
fa3c6b4d
KS
19213}
19214
19215#endif /* HAVE_WINDOW_SYSTEM */
19216
19217\f
19218/************************************************************************
19219 Mouse Face
19220 ************************************************************************/
19221
19222#ifdef HAVE_WINDOW_SYSTEM
19223
19224/* EXPORT for RIF:
19225 Fix the display of area AREA of overlapping row ROW in window W. */
19226
19227void
19228x_fix_overlapping_area (w, row, area)
19229 struct window *w;
19230 struct glyph_row *row;
19231 enum glyph_row_area area;
19232{
19233 int i, x;
19234
19235 BLOCK_INPUT;
19236
57dd22f4 19237 x = 0;
fa3c6b4d
KS
19238 for (i = 0; i < row->used[area];)
19239 {
19240 if (row->glyphs[area][i].overlaps_vertically_p)
19241 {
19242 int start = i, start_x = x;
19243
19244 do
19245 {
19246 x += row->glyphs[area][i].pixel_width;
19247 ++i;
19248 }
19249 while (i < row->used[area]
19250 && row->glyphs[area][i].overlaps_vertically_p);
19251
19252 draw_glyphs (w, start_x, row, area,
19253 start, i,
19254 DRAW_NORMAL_TEXT, 1);
19255 }
19256 else
19257 {
19258 x += row->glyphs[area][i].pixel_width;
19259 ++i;
19260 }
19261 }
19262
19263 UNBLOCK_INPUT;
19264}
19265
19266
19267/* EXPORT:
19268 Draw the cursor glyph of window W in glyph row ROW. See the
19269 comment of draw_glyphs for the meaning of HL. */
19270
19271void
19272draw_phys_cursor_glyph (w, row, hl)
19273 struct window *w;
19274 struct glyph_row *row;
19275 enum draw_glyphs_face hl;
19276{
19277 /* If cursor hpos is out of bounds, don't draw garbage. This can
19278 happen in mini-buffer windows when switching between echo area
19279 glyphs and mini-buffer. */
19280 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19281 {
19282 int on_p = w->phys_cursor_on_p;
19283 int x1;
19284 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19285 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19286 hl, 0);
19287 w->phys_cursor_on_p = on_p;
19288
fa3c6b4d
KS
19289 if (hl == DRAW_CURSOR)
19290 w->phys_cursor_width = x1 - w->phys_cursor.x;
fa3c6b4d
KS
19291 /* When we erase the cursor, and ROW is overlapped by other
19292 rows, make sure that these overlapping parts of other rows
19293 are redrawn. */
365fa1b3 19294 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
fa3c6b4d
KS
19295 {
19296 if (row > w->current_matrix->rows
19297 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19298 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19299
19300 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19301 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19302 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19303 }
19304 }
19305}
19306
19307
19308/* EXPORT:
19309 Erase the image of a cursor of window W from the screen. */
19310
19311void
19312erase_phys_cursor (w)
19313 struct window *w;
19314{
19315 struct frame *f = XFRAME (w->frame);
19316 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19317 int hpos = w->phys_cursor.hpos;
19318 int vpos = w->phys_cursor.vpos;
19319 int mouse_face_here_p = 0;
19320 struct glyph_matrix *active_glyphs = w->current_matrix;
19321 struct glyph_row *cursor_row;
19322 struct glyph *cursor_glyph;
19323 enum draw_glyphs_face hl;
19324
19325 /* No cursor displayed or row invalidated => nothing to do on the
19326 screen. */
19327 if (w->phys_cursor_type == NO_CURSOR)
19328 goto mark_cursor_off;
19329
19330 /* VPOS >= active_glyphs->nrows means that window has been resized.
19331 Don't bother to erase the cursor. */
19332 if (vpos >= active_glyphs->nrows)
19333 goto mark_cursor_off;
19334
19335 /* If row containing cursor is marked invalid, there is nothing we
19336 can do. */
19337 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19338 if (!cursor_row->enabled_p)
19339 goto mark_cursor_off;
19340
19341 /* If row is completely invisible, don't attempt to delete a cursor which
19342 isn't there. This can happen if cursor is at top of a window, and
19343 we switch to a buffer with a header line in that window. */
19344 if (cursor_row->visible_height <= 0)
19345 goto mark_cursor_off;
19346
19347 /* This can happen when the new row is shorter than the old one.
19348 In this case, either draw_glyphs or clear_end_of_line
19349 should have cleared the cursor. Note that we wouldn't be
19350 able to erase the cursor in this case because we don't have a
19351 cursor glyph at hand. */
19352 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19353 goto mark_cursor_off;
19354
19355 /* If the cursor is in the mouse face area, redisplay that when
19356 we clear the cursor. */
19357 if (! NILP (dpyinfo->mouse_face_window)
19358 && w == XWINDOW (dpyinfo->mouse_face_window)
19359 && (vpos > dpyinfo->mouse_face_beg_row
19360 || (vpos == dpyinfo->mouse_face_beg_row
19361 && hpos >= dpyinfo->mouse_face_beg_col))
19362 && (vpos < dpyinfo->mouse_face_end_row
19363 || (vpos == dpyinfo->mouse_face_end_row
19364 && hpos < dpyinfo->mouse_face_end_col))
19365 /* Don't redraw the cursor's spot in mouse face if it is at the
19366 end of a line (on a newline). The cursor appears there, but
19367 mouse highlighting does not. */
19368 && cursor_row->used[TEXT_AREA] > hpos)
19369 mouse_face_here_p = 1;
19370
19371 /* Maybe clear the display under the cursor. */
19372 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19373 {
19374 int x, y;
da8b7f4f 19375 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
fa3c6b4d
KS
19376
19377 cursor_glyph = get_phys_cursor_glyph (w);
19378 if (cursor_glyph == NULL)
19379 goto mark_cursor_off;
19380
19381 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19382 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19383
19384 rif->clear_frame_area (f, x, y,
19385 cursor_glyph->pixel_width, cursor_row->visible_height);
19386 }
19387
19388 /* Erase the cursor by redrawing the character underneath it. */
19389 if (mouse_face_here_p)
19390 hl = DRAW_MOUSE_FACE;
19391 else
19392 hl = DRAW_NORMAL_TEXT;
19393 draw_phys_cursor_glyph (w, cursor_row, hl);
19394
19395 mark_cursor_off:
19396 w->phys_cursor_on_p = 0;
19397 w->phys_cursor_type = NO_CURSOR;
19398}
19399
19400
19401/* EXPORT:
19402 Display or clear cursor of window W. If ON is zero, clear the
19403 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19404 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19405
19406void
19407display_and_set_cursor (w, on, hpos, vpos, x, y)
19408 struct window *w;
19409 int on, hpos, vpos, x, y;
19410{
19411 struct frame *f = XFRAME (w->frame);
19412 int new_cursor_type;
19413 int new_cursor_width;
19414 int active_cursor;
19415 struct glyph_matrix *current_glyphs;
19416 struct glyph_row *glyph_row;
19417 struct glyph *glyph;
19418
19419 /* This is pointless on invisible frames, and dangerous on garbaged
19420 windows and frames; in the latter case, the frame or window may
19421 be in the midst of changing its size, and x and y may be off the
19422 window. */
19423 if (! FRAME_VISIBLE_P (f)
19424 || FRAME_GARBAGED_P (f)
19425 || vpos >= w->current_matrix->nrows
19426 || hpos >= w->current_matrix->matrix_w)
19427 return;
19428
19429 /* If cursor is off and we want it off, return quickly. */
19430 if (!on && !w->phys_cursor_on_p)
19431 return;
19432
19433 current_glyphs = w->current_matrix;
19434 glyph_row = MATRIX_ROW (current_glyphs, vpos);
19435 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
19436
19437 /* If cursor row is not enabled, we don't really know where to
19438 display the cursor. */
19439 if (!glyph_row->enabled_p)
19440 {
19441 w->phys_cursor_on_p = 0;
19442 return;
19443 }
19444
19445 xassert (interrupt_input_blocked);
19446
19447 /* Set new_cursor_type to the cursor we want to be displayed. */
f65536fa
KS
19448 new_cursor_type = get_window_cursor_type (w, glyph,
19449 &new_cursor_width, &active_cursor);
fa3c6b4d
KS
19450
19451 /* If cursor is currently being shown and we don't want it to be or
19452 it is in the wrong place, or the cursor type is not what we want,
19453 erase it. */
19454 if (w->phys_cursor_on_p
19455 && (!on
19456 || w->phys_cursor.x != x
19457 || w->phys_cursor.y != y
19458 || new_cursor_type != w->phys_cursor_type
19459 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19460 && new_cursor_width != w->phys_cursor_width)))
19461 erase_phys_cursor (w);
19462
19463 /* Don't check phys_cursor_on_p here because that flag is only set
19464 to zero in some cases where we know that the cursor has been
19465 completely erased, to avoid the extra work of erasing the cursor
19466 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19467 still not be visible, or it has only been partly erased. */
19468 if (on)
19469 {
19470 w->phys_cursor_ascent = glyph_row->ascent;
19471 w->phys_cursor_height = glyph_row->height;
19472
19473 /* Set phys_cursor_.* before x_draw_.* is called because some
19474 of them may need the information. */
19475 w->phys_cursor.x = x;
19476 w->phys_cursor.y = glyph_row->y;
19477 w->phys_cursor.hpos = hpos;
19478 w->phys_cursor.vpos = vpos;
19479 }
19480
b4ebbb12
KS
19481 rif->draw_window_cursor (w, glyph_row, x, y,
19482 new_cursor_type, new_cursor_width,
19483 on, active_cursor);
fa3c6b4d
KS
19484}
19485
19486
19487/* Switch the display of W's cursor on or off, according to the value
19488 of ON. */
19489
19490static void
19491update_window_cursor (w, on)
19492 struct window *w;
19493 int on;
19494{
19495 /* Don't update cursor in windows whose frame is in the process
19496 of being deleted. */
19497 if (w->current_matrix)
19498 {
19499 BLOCK_INPUT;
19500 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
19501 w->phys_cursor.x, w->phys_cursor.y);
19502 UNBLOCK_INPUT;
19503 }
19504}
19505
19506
19507/* Call update_window_cursor with parameter ON_P on all leaf windows
19508 in the window tree rooted at W. */
19509
19510static void
19511update_cursor_in_window_tree (w, on_p)
19512 struct window *w;
19513 int on_p;
19514{
19515 while (w)
19516 {
19517 if (!NILP (w->hchild))
19518 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
19519 else if (!NILP (w->vchild))
19520 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
19521 else
19522 update_window_cursor (w, on_p);
19523
19524 w = NILP (w->next) ? 0 : XWINDOW (w->next);
19525 }
19526}
19527
19528
19529/* EXPORT:
19530 Display the cursor on window W, or clear it, according to ON_P.
19531 Don't change the cursor's position. */
19532
19533void
19534x_update_cursor (f, on_p)
19535 struct frame *f;
19536 int on_p;
19537{
19538 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
19539}
19540
19541
19542/* EXPORT:
19543 Clear the cursor of window W to background color, and mark the
19544 cursor as not shown. This is used when the text where the cursor
19545 is is about to be rewritten. */
19546
19547void
19548x_clear_cursor (w)
19549 struct window *w;
19550{
19551 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
19552 update_window_cursor (w, 0);
19553}
19554
19555
19556/* EXPORT:
19557 Display the active region described by mouse_face_* according to DRAW. */
19558
19559void
19560show_mouse_face (dpyinfo, draw)
19561 Display_Info *dpyinfo;
19562 enum draw_glyphs_face draw;
19563{
19564 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
19565 struct frame *f = XFRAME (WINDOW_FRAME (w));
19566
19567 if (/* If window is in the process of being destroyed, don't bother
19568 to do anything. */
19569 w->current_matrix != NULL
19570 /* Don't update mouse highlight if hidden */
19571 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
19572 /* Recognize when we are called to operate on rows that don't exist
19573 anymore. This can happen when a window is split. */
19574 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
19575 {
19576 int phys_cursor_on_p = w->phys_cursor_on_p;
19577 struct glyph_row *row, *first, *last;
19578
19579 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19580 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19581
19582 for (row = first; row <= last && row->enabled_p; ++row)
19583 {
19584 int start_hpos, end_hpos, start_x;
19585
19586 /* For all but the first row, the highlight starts at column 0. */
19587 if (row == first)
19588 {
19589 start_hpos = dpyinfo->mouse_face_beg_col;
19590 start_x = dpyinfo->mouse_face_beg_x;
19591 }
19592 else
19593 {
19594 start_hpos = 0;
19595 start_x = 0;
19596 }
19597
19598 if (row == last)
19599 end_hpos = dpyinfo->mouse_face_end_col;
19600 else
19601 end_hpos = row->used[TEXT_AREA];
19602
19603 if (end_hpos > start_hpos)
19604 {
19605 draw_glyphs (w, start_x, row, TEXT_AREA,
19606 start_hpos, end_hpos,
19607 draw, 0);
19608
19609 row->mouse_face_p
19610 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
19611 }
19612 }
19613
19614 /* When we've written over the cursor, arrange for it to
19615 be displayed again. */
19616 if (phys_cursor_on_p && !w->phys_cursor_on_p)
19617 {
19618 BLOCK_INPUT;
19619 display_and_set_cursor (w, 1,
19620 w->phys_cursor.hpos, w->phys_cursor.vpos,
19621 w->phys_cursor.x, w->phys_cursor.y);
19622 UNBLOCK_INPUT;
19623 }
19624 }
19625
19626 /* Change the mouse cursor. */
19627 if (draw == DRAW_NORMAL_TEXT)
19628 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
19629 else if (draw == DRAW_MOUSE_FACE)
19630 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
19631 else
19632 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
19633}
19634
19635/* EXPORT:
19636 Clear out the mouse-highlighted active region.
19637 Redraw it un-highlighted first. Value is non-zero if mouse
19638 face was actually drawn unhighlighted. */
19639
19640int
19641clear_mouse_face (dpyinfo)
19642 Display_Info *dpyinfo;
19643{
19644 int cleared = 0;
19645
19646 if (!NILP (dpyinfo->mouse_face_window))
19647 {
19648 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
19649 cleared = 1;
19650 }
19651
19652 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
19653 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
19654 dpyinfo->mouse_face_window = Qnil;
19655 dpyinfo->mouse_face_overlay = Qnil;
19656 return cleared;
19657}
19658
19659
19660/* EXPORT:
19661 Non-zero if physical cursor of window W is within mouse face. */
19662
19663int
19664cursor_in_mouse_face_p (w)
19665 struct window *w;
19666{
19667 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
19668 int in_mouse_face = 0;
19669
19670 if (WINDOWP (dpyinfo->mouse_face_window)
19671 && XWINDOW (dpyinfo->mouse_face_window) == w)
19672 {
19673 int hpos = w->phys_cursor.hpos;
19674 int vpos = w->phys_cursor.vpos;
19675
19676 if (vpos >= dpyinfo->mouse_face_beg_row
19677 && vpos <= dpyinfo->mouse_face_end_row
19678 && (vpos > dpyinfo->mouse_face_beg_row
19679 || hpos >= dpyinfo->mouse_face_beg_col)
19680 && (vpos < dpyinfo->mouse_face_end_row
19681 || hpos < dpyinfo->mouse_face_end_col
19682 || dpyinfo->mouse_face_past_end))
19683 in_mouse_face = 1;
19684 }
19685
19686 return in_mouse_face;
19687}
19688
19689
19690
19691\f
19692/* Find the glyph matrix position of buffer position CHARPOS in window
19693 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
19694 current glyphs must be up to date. If CHARPOS is above window
19695 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
19696 of last line in W. In the row containing CHARPOS, stop before glyphs
19697 having STOP as object. */
19698
9c3521e0 19699#if 1 /* This is a version of fast_find_position that's more correct
fa3c6b4d
KS
19700 in the presence of hscrolling, for example. I didn't install
19701 it right away because the problem fixed is minor, it failed
19702 in 20.x as well, and I think it's too risky to install
19703 so near the release of 21.1. 2001-09-25 gerd. */
19704
19705static int
19706fast_find_position (w, charpos, hpos, vpos, x, y, stop)
19707 struct window *w;
19708 int charpos;
19709 int *hpos, *vpos, *x, *y;
19710 Lisp_Object stop;
19711{
19712 struct glyph_row *row, *first;
19713 struct glyph *glyph, *end;
b19a5b64 19714 int past_end = 0;
fa3c6b4d
KS
19715
19716 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19717 row = row_containing_pos (w, charpos, first, NULL, 0);
19718 if (row == NULL)
19719 {
19720 if (charpos < MATRIX_ROW_START_CHARPOS (first))
19721 {
19722 *x = *y = *hpos = *vpos = 0;
19723 return 0;
19724 }
19725 else
19726 {
19727 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
19728 past_end = 1;
19729 }
19730 }
19731
19732 *x = row->x;
19733 *y = row->y;
19734 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19735
19736 glyph = row->glyphs[TEXT_AREA];
19737 end = glyph + row->used[TEXT_AREA];
19738
19739 /* Skip over glyphs not having an object at the start of the row.
19740 These are special glyphs like truncation marks on terminal
19741 frames. */
19742 if (row->displays_text_p)
19743 while (glyph < end
19744 && INTEGERP (glyph->object)
19745 && !EQ (stop, glyph->object)
19746 && glyph->charpos < 0)
19747 {
19748 *x += glyph->pixel_width;
19749 ++glyph;
19750 }
19751
19752 while (glyph < end
19753 && !INTEGERP (glyph->object)
19754 && !EQ (stop, glyph->object)
19755 && (!BUFFERP (glyph->object)
19756 || glyph->charpos < charpos))
19757 {
19758 *x += glyph->pixel_width;
19759 ++glyph;
19760 }
19761
19762 *hpos = glyph - row->glyphs[TEXT_AREA];
19763 return past_end;
19764}
19765
9c3521e0 19766#else /* not 1 */
fa3c6b4d
KS
19767
19768static int
19769fast_find_position (w, pos, hpos, vpos, x, y, stop)
19770 struct window *w;
19771 int pos;
19772 int *hpos, *vpos, *x, *y;
19773 Lisp_Object stop;
19774{
19775 int i;
19776 int lastcol;
19777 int maybe_next_line_p = 0;
19778 int line_start_position;
19779 int yb = window_text_bottom_y (w);
19780 struct glyph_row *row, *best_row;
19781 int row_vpos, best_row_vpos;
19782 int current_x;
19783
19784 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19785 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19786
19787 while (row->y < yb)
19788 {
19789 if (row->used[TEXT_AREA])
19790 line_start_position = row->glyphs[TEXT_AREA]->charpos;
19791 else
19792 line_start_position = 0;
19793
19794 if (line_start_position > pos)
19795 break;
19796 /* If the position sought is the end of the buffer,
19797 don't include the blank lines at the bottom of the window. */
19798 else if (line_start_position == pos
19799 && pos == BUF_ZV (XBUFFER (w->buffer)))
19800 {
19801 maybe_next_line_p = 1;
19802 break;
19803 }
19804 else if (line_start_position > 0)
19805 {
19806 best_row = row;
19807 best_row_vpos = row_vpos;
19808 }
19809
19810 if (row->y + row->height >= yb)
19811 break;
19812
19813 ++row;
19814 ++row_vpos;
19815 }
19816
19817 /* Find the right column within BEST_ROW. */
19818 lastcol = 0;
19819 current_x = best_row->x;
19820 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
19821 {
19822 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
19823 int charpos = glyph->charpos;
19824
19825 if (BUFFERP (glyph->object))
19826 {
19827 if (charpos == pos)
19828 {
19829 *hpos = i;
19830 *vpos = best_row_vpos;
19831 *x = current_x;
19832 *y = best_row->y;
19833 return 1;
19834 }
19835 else if (charpos > pos)
19836 break;
19837 }
19838 else if (EQ (glyph->object, stop))
19839 break;
19840
19841 if (charpos > 0)
19842 lastcol = i;
19843 current_x += glyph->pixel_width;
19844 }
19845
19846 /* If we're looking for the end of the buffer,
19847 and we didn't find it in the line we scanned,
19848 use the start of the following line. */
19849 if (maybe_next_line_p)
19850 {
19851 ++best_row;
19852 ++best_row_vpos;
19853 lastcol = 0;
19854 current_x = best_row->x;
19855 }
19856
19857 *vpos = best_row_vpos;
19858 *hpos = lastcol + 1;
19859 *x = current_x;
19860 *y = best_row->y;
19861 return 0;
19862}
19863
9c3521e0 19864#endif /* not 1 */
fa3c6b4d
KS
19865
19866
19867/* Find the position of the glyph for position POS in OBJECT in
19868 window W's current matrix, and return in *X, *Y the pixel
19869 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
19870
19871 RIGHT_P non-zero means return the position of the right edge of the
19872 glyph, RIGHT_P zero means return the left edge position.
19873
19874 If no glyph for POS exists in the matrix, return the position of
19875 the glyph with the next smaller position that is in the matrix, if
19876 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
19877 exists in the matrix, return the position of the glyph with the
19878 next larger position in OBJECT.
19879
19880 Value is non-zero if a glyph was found. */
19881
19882static int
19883fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
19884 struct window *w;
19885 int pos;
19886 Lisp_Object object;
19887 int *hpos, *vpos, *x, *y;
19888 int right_p;
19889{
19890 int yb = window_text_bottom_y (w);
19891 struct glyph_row *r;
19892 struct glyph *best_glyph = NULL;
19893 struct glyph_row *best_row = NULL;
19894 int best_x = 0;
19895
19896 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19897 r->enabled_p && r->y < yb;
19898 ++r)
19899 {
19900 struct glyph *g = r->glyphs[TEXT_AREA];
19901 struct glyph *e = g + r->used[TEXT_AREA];
19902 int gx;
19903
19904 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
19905 if (EQ (g->object, object))
19906 {
19907 if (g->charpos == pos)
19908 {
19909 best_glyph = g;
19910 best_x = gx;
19911 best_row = r;
19912 goto found;
19913 }
19914 else if (best_glyph == NULL
19915 || ((abs (g->charpos - pos)
19916 < abs (best_glyph->charpos - pos))
19917 && (right_p
19918 ? g->charpos < pos
19919 : g->charpos > pos)))
19920 {
19921 best_glyph = g;
19922 best_x = gx;
19923 best_row = r;
19924 }
19925 }
19926 }
19927
19928 found:
19929
19930 if (best_glyph)
19931 {
19932 *x = best_x;
19933 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
19934
19935 if (right_p)
19936 {
19937 *x += best_glyph->pixel_width;
19938 ++*hpos;
19939 }
19940
19941 *y = best_row->y;
19942 *vpos = best_row - w->current_matrix->rows;
19943 }
19944
19945 return best_glyph != NULL;
19946}
19947
19948
fa3c6b4d
KS
19949/* Take proper action when mouse has moved to the mode or header line
19950 or marginal area AREA of window W, x-position X and y-position Y.
19951 X is relative to the start of the text display area of W, so the
19952 width of bitmap areas and scroll bars must be subtracted to get a
19953 position relative to the start of the mode line. */
19954
19955static void
19956note_mode_line_or_margin_highlight (w, x, y, area)
19957 struct window *w;
19958 int x, y;
19959 enum window_part area;
19960{
19961 struct frame *f = XFRAME (w->frame);
19962 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
f65536fa 19963 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
fa3c6b4d
KS
19964 int charpos;
19965 Lisp_Object string, help, map, pos;
19966
19967 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
f65536fa 19968 string = mode_line_string (w, &x, &y, 0, 0, area, &charpos);
fa3c6b4d 19969 else
f65536fa 19970 string = marginal_area_string (w, &x, &y, 0, 0, area, &charpos);
fa3c6b4d
KS
19971
19972 if (STRINGP (string))
19973 {
19974 pos = make_number (charpos);
19975
19976 /* If we're on a string with `help-echo' text property, arrange
19977 for the help to be displayed. This is done by setting the
19978 global variable help_echo_string to the help string. */
19979 help = Fget_text_property (pos, Qhelp_echo, string);
19980 if (!NILP (help))
19981 {
19982 help_echo_string = help;
19983 XSETWINDOW (help_echo_window, w);
19984 help_echo_object = string;
19985 help_echo_pos = charpos;
19986 }
19987
19988 /* Change the mouse pointer according to what is under X/Y. */
f65536fa
KS
19989 if (area == ON_MODE_LINE)
19990 {
19991 map = Fget_text_property (pos, Qlocal_map, string);
19992 if (!KEYMAPP (map))
19993 map = Fget_text_property (pos, Qkeymap, string);
19994 if (!KEYMAPP (map))
19995 cursor = dpyinfo->vertical_scroll_bar_cursor;
19996 }
fa3c6b4d
KS
19997 }
19998
19999 rif->define_frame_cursor (f, cursor);
20000}
20001
fa3c6b4d
KS
20002
20003/* EXPORT:
20004 Take proper action when the mouse has moved to position X, Y on
20005 frame F as regards highlighting characters that have mouse-face
20006 properties. Also de-highlighting chars where the mouse was before.
20007 X and Y can be negative or out of range. */
20008
20009void
20010note_mouse_highlight (f, x, y)
20011 struct frame *f;
20012 int x, y;
20013{
20014 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20015 enum window_part part;
20016 Lisp_Object window;
20017 struct window *w;
20018 Cursor cursor = No_Cursor;
20019 struct buffer *b;
20020
20021 /* When a menu is active, don't highlight because this looks odd. */
20022#if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20023 if (popup_activated ())
20024 return;
20025#endif
20026
20027 if (NILP (Vmouse_highlight)
20028 || !f->glyphs_initialized_p)
20029 return;
20030
20031 dpyinfo->mouse_face_mouse_x = x;
20032 dpyinfo->mouse_face_mouse_y = y;
20033 dpyinfo->mouse_face_mouse_frame = f;
20034
20035 if (dpyinfo->mouse_face_defer)
20036 return;
20037
20038 if (gc_in_progress)
20039 {
20040 dpyinfo->mouse_face_deferred_gc = 1;
20041 return;
20042 }
20043
20044 /* Which window is that in? */
da8b7f4f 20045 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
fa3c6b4d
KS
20046
20047 /* If we were displaying active text in another window, clear that. */
20048 if (! EQ (window, dpyinfo->mouse_face_window))
20049 clear_mouse_face (dpyinfo);
20050
20051 /* Not on a window -> return. */
20052 if (!WINDOWP (window))
20053 return;
20054
20055 /* Reset help_echo_string. It will get recomputed below. */
20056 /* ++KFS: X version didn't do this, but it looks harmless. */
20057 help_echo_string = Qnil;
20058
20059 /* Convert to window-relative pixel coordinates. */
20060 w = XWINDOW (window);
20061 frame_to_window_pixel_xy (w, &x, &y);
20062
20063 /* Handle tool-bar window differently since it doesn't display a
20064 buffer. */
20065 if (EQ (window, f->tool_bar_window))
20066 {
20067 note_tool_bar_highlight (f, x, y);
20068 return;
20069 }
20070
fa3c6b4d
KS
20071 /* Mouse is on the mode, header line or margin? */
20072 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20073 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20074 {
20075 note_mode_line_or_margin_highlight (w, x, y, part);
20076 return;
20077 }
fa3c6b4d
KS
20078
20079 if (part == ON_VERTICAL_BORDER)
20080 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
f65536fa
KS
20081 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
20082 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
fa3c6b4d
KS
20083 else
20084 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20085
20086 /* Are we in a window whose display is up to date?
20087 And verify the buffer's text has not changed. */
20088 b = XBUFFER (w->buffer);
20089 if (part == ON_TEXT
20090 && EQ (w->window_end_valid, w->buffer)
20091 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20092 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20093 {
20094 int hpos, vpos, pos, i, area;
20095 struct glyph *glyph;
20096 Lisp_Object object;
20097 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20098 Lisp_Object *overlay_vec = NULL;
20099 int len, noverlays;
20100 struct buffer *obuf;
20101 int obegv, ozv, same_region;
20102
20103 /* Find the glyph under X/Y. */
20104 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
20105
20106 /* Clear mouse face if X/Y not over text. */
20107 if (glyph == NULL
20108 || area != TEXT_AREA
20109 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
20110 {
fa3c6b4d
KS
20111 if (clear_mouse_face (dpyinfo))
20112 cursor = No_Cursor;
f65536fa
KS
20113 if (NILP (Vshow_text_cursor_in_void))
20114 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
fa3c6b4d
KS
20115 goto set_cursor;
20116 }
20117
20118 pos = glyph->charpos;
20119 object = glyph->object;
20120 if (!STRINGP (object) && !BUFFERP (object))
20121 goto set_cursor;
20122
20123 /* If we get an out-of-range value, return now; avoid an error. */
20124 if (BUFFERP (object) && pos > BUF_Z (b))
20125 goto set_cursor;
20126
f65536fa
KS
20127 if (glyph->type == IMAGE_GLYPH)
20128 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20129
fa3c6b4d
KS
20130 /* Make the window's buffer temporarily current for
20131 overlays_at and compute_char_face. */
20132 obuf = current_buffer;
20133 current_buffer = b;
20134 obegv = BEGV;
20135 ozv = ZV;
20136 BEGV = BEG;
20137 ZV = Z;
20138
20139 /* Is this char mouse-active or does it have help-echo? */
20140 position = make_number (pos);
20141
20142 if (BUFFERP (object))
20143 {
20144 /* Put all the overlays we want in a vector in overlay_vec.
20145 Store the length in len. If there are more than 10, make
20146 enough space for all, and try again. */
20147 len = 10;
20148 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20149 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
20150 if (noverlays > len)
20151 {
20152 len = noverlays;
20153 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20154 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
20155 }
20156
20157 /* Sort overlays into increasing priority order. */
20158 noverlays = sort_overlays (overlay_vec, noverlays, w);
20159 }
20160 else
20161 noverlays = 0;
20162
20163 same_region = (EQ (window, dpyinfo->mouse_face_window)
20164 && vpos >= dpyinfo->mouse_face_beg_row
20165 && vpos <= dpyinfo->mouse_face_end_row
20166 && (vpos > dpyinfo->mouse_face_beg_row
20167 || hpos >= dpyinfo->mouse_face_beg_col)
20168 && (vpos < dpyinfo->mouse_face_end_row
20169 || hpos < dpyinfo->mouse_face_end_col
20170 || dpyinfo->mouse_face_past_end));
20171
20172 if (same_region)
20173 cursor = No_Cursor;
20174
20175 /* Check mouse-face highlighting. */
20176 if (! same_region
20177 /* If there exists an overlay with mouse-face overlapping
20178 the one we are currently highlighting, we have to
20179 check if we enter the overlapping overlay, and then
20180 highlight only that. */
20181 || (OVERLAYP (dpyinfo->mouse_face_overlay)
20182 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
20183 {
20184 /* Find the highest priority overlay that has a mouse-face
20185 property. */
20186 overlay = Qnil;
20187 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
20188 {
20189 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
20190 if (!NILP (mouse_face))
20191 overlay = overlay_vec[i];
20192 }
20193
20194 /* If we're actually highlighting the same overlay as
20195 before, there's no need to do that again. */
20196 if (!NILP (overlay)
20197 && EQ (overlay, dpyinfo->mouse_face_overlay))
20198 goto check_help_echo;
20199
20200 dpyinfo->mouse_face_overlay = overlay;
20201
20202 /* Clear the display of the old active region, if any. */
20203 if (clear_mouse_face (dpyinfo))
20204 cursor = No_Cursor;
20205
20206 /* If no overlay applies, get a text property. */
20207 if (NILP (overlay))
20208 mouse_face = Fget_text_property (position, Qmouse_face, object);
20209
20210 /* Handle the overlay case. */
20211 if (!NILP (overlay))
20212 {
20213 /* Find the range of text around this char that
20214 should be active. */
20215 Lisp_Object before, after;
20216 int ignore;
20217
20218 before = Foverlay_start (overlay);
20219 after = Foverlay_end (overlay);
20220 /* Record this as the current active region. */
20221 fast_find_position (w, XFASTINT (before),
20222 &dpyinfo->mouse_face_beg_col,
20223 &dpyinfo->mouse_face_beg_row,
20224 &dpyinfo->mouse_face_beg_x,
20225 &dpyinfo->mouse_face_beg_y, Qnil);
20226
20227 dpyinfo->mouse_face_past_end
20228 = !fast_find_position (w, XFASTINT (after),
20229 &dpyinfo->mouse_face_end_col,
20230 &dpyinfo->mouse_face_end_row,
20231 &dpyinfo->mouse_face_end_x,
20232 &dpyinfo->mouse_face_end_y, Qnil);
20233 dpyinfo->mouse_face_window = window;
20234
20235 dpyinfo->mouse_face_face_id
20236 = face_at_buffer_position (w, pos, 0, 0,
20237 &ignore, pos + 1,
20238 !dpyinfo->mouse_face_hidden);
20239
20240 /* Display it as active. */
20241 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20242 cursor = No_Cursor;
20243 }
20244 /* Handle the text property case. */
20245 else if (!NILP (mouse_face) && BUFFERP (object))
20246 {
20247 /* Find the range of text around this char that
20248 should be active. */
20249 Lisp_Object before, after, beginning, end;
20250 int ignore;
20251
20252 beginning = Fmarker_position (w->start);
20253 end = make_number (BUF_Z (XBUFFER (object))
20254 - XFASTINT (w->window_end_pos));
20255 before
20256 = Fprevious_single_property_change (make_number (pos + 1),
20257 Qmouse_face,
20258 object, beginning);
20259 after
20260 = Fnext_single_property_change (position, Qmouse_face,
20261 object, end);
20262
20263 /* Record this as the current active region. */
20264 fast_find_position (w, XFASTINT (before),
20265 &dpyinfo->mouse_face_beg_col,
20266 &dpyinfo->mouse_face_beg_row,
20267 &dpyinfo->mouse_face_beg_x,
20268 &dpyinfo->mouse_face_beg_y, Qnil);
20269 dpyinfo->mouse_face_past_end
20270 = !fast_find_position (w, XFASTINT (after),
20271 &dpyinfo->mouse_face_end_col,
20272 &dpyinfo->mouse_face_end_row,
20273 &dpyinfo->mouse_face_end_x,
20274 &dpyinfo->mouse_face_end_y, Qnil);
20275 dpyinfo->mouse_face_window = window;
20276
20277 if (BUFFERP (object))
20278 dpyinfo->mouse_face_face_id
20279 = face_at_buffer_position (w, pos, 0, 0,
20280 &ignore, pos + 1,
20281 !dpyinfo->mouse_face_hidden);
20282
20283 /* Display it as active. */
20284 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20285 cursor = No_Cursor;
20286 }
20287 else if (!NILP (mouse_face) && STRINGP (object))
20288 {
20289 Lisp_Object b, e;
20290 int ignore;
20291
20292 b = Fprevious_single_property_change (make_number (pos + 1),
20293 Qmouse_face,
20294 object, Qnil);
20295 e = Fnext_single_property_change (position, Qmouse_face,
20296 object, Qnil);
20297 if (NILP (b))
20298 b = make_number (0);
20299 if (NILP (e))
20300 e = make_number (SCHARS (object) - 1);
20301 fast_find_string_pos (w, XINT (b), object,
20302 &dpyinfo->mouse_face_beg_col,
20303 &dpyinfo->mouse_face_beg_row,
20304 &dpyinfo->mouse_face_beg_x,
20305 &dpyinfo->mouse_face_beg_y, 0);
20306 fast_find_string_pos (w, XINT (e), object,
20307 &dpyinfo->mouse_face_end_col,
20308 &dpyinfo->mouse_face_end_row,
20309 &dpyinfo->mouse_face_end_x,
20310 &dpyinfo->mouse_face_end_y, 1);
20311 dpyinfo->mouse_face_past_end = 0;
20312 dpyinfo->mouse_face_window = window;
20313 dpyinfo->mouse_face_face_id
20314 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
20315 glyph->face_id, 1);
20316 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20317 cursor = No_Cursor;
20318 }
20319 else if (STRINGP (object) && NILP (mouse_face))
20320 {
20321 /* A string which doesn't have mouse-face, but
20322 the text ``under'' it might have. */
20323 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
20324 int start = MATRIX_ROW_START_CHARPOS (r);
20325
20326 pos = string_buffer_position (w, object, start);
20327 if (pos > 0)
20328 mouse_face = get_char_property_and_overlay (make_number (pos),
20329 Qmouse_face,
20330 w->buffer,
20331 &overlay);
20332 if (!NILP (mouse_face) && !NILP (overlay))
20333 {
20334 Lisp_Object before = Foverlay_start (overlay);
20335 Lisp_Object after = Foverlay_end (overlay);
20336 int ignore;
20337
20338 /* Note that we might not be able to find position
20339 BEFORE in the glyph matrix if the overlay is
20340 entirely covered by a `display' property. In
20341 this case, we overshoot. So let's stop in
20342 the glyph matrix before glyphs for OBJECT. */
20343 fast_find_position (w, XFASTINT (before),
20344 &dpyinfo->mouse_face_beg_col,
20345 &dpyinfo->mouse_face_beg_row,
20346 &dpyinfo->mouse_face_beg_x,
20347 &dpyinfo->mouse_face_beg_y,
20348 object);
20349
20350 dpyinfo->mouse_face_past_end
20351 = !fast_find_position (w, XFASTINT (after),
20352 &dpyinfo->mouse_face_end_col,
20353 &dpyinfo->mouse_face_end_row,
20354 &dpyinfo->mouse_face_end_x,
20355 &dpyinfo->mouse_face_end_y,
20356 Qnil);
20357 dpyinfo->mouse_face_window = window;
20358 dpyinfo->mouse_face_face_id
20359 = face_at_buffer_position (w, pos, 0, 0,
20360 &ignore, pos + 1,
20361 !dpyinfo->mouse_face_hidden);
20362
20363 /* Display it as active. */
20364 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20365 cursor = No_Cursor;
20366 }
20367 }
20368 }
20369
20370 check_help_echo:
20371
20372 /* Look for a `help-echo' property. */
20373 {
20374 Lisp_Object help, overlay;
20375
20376 /* Check overlays first. */
20377 help = overlay = Qnil;
20378 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
20379 {
20380 overlay = overlay_vec[i];
20381 help = Foverlay_get (overlay, Qhelp_echo);
20382 }
20383
20384 if (!NILP (help))
20385 {
20386 help_echo_string = help;
20387 help_echo_window = window;
20388 help_echo_object = overlay;
20389 help_echo_pos = pos;
20390 }
20391 else
20392 {
20393 Lisp_Object object = glyph->object;
20394 int charpos = glyph->charpos;
20395
20396 /* Try text properties. */
20397 if (STRINGP (object)
20398 && charpos >= 0
20399 && charpos < SCHARS (object))
20400 {
20401 help = Fget_text_property (make_number (charpos),
20402 Qhelp_echo, object);
20403 if (NILP (help))
20404 {
20405 /* If the string itself doesn't specify a help-echo,
20406 see if the buffer text ``under'' it does. */
20407 struct glyph_row *r
20408 = MATRIX_ROW (w->current_matrix, vpos);
20409 int start = MATRIX_ROW_START_CHARPOS (r);
20410 int pos = string_buffer_position (w, object, start);
20411 if (pos > 0)
20412 {
20413 help = Fget_char_property (make_number (pos),
20414 Qhelp_echo, w->buffer);
20415 if (!NILP (help))
20416 {
20417 charpos = pos;
20418 object = w->buffer;
20419 }
20420 }
20421 }
20422 }
20423 else if (BUFFERP (object)
20424 && charpos >= BEGV
20425 && charpos < ZV)
20426 help = Fget_text_property (make_number (charpos), Qhelp_echo,
20427 object);
20428
20429 if (!NILP (help))
20430 {
20431 help_echo_string = help;
20432 help_echo_window = window;
20433 help_echo_object = object;
20434 help_echo_pos = charpos;
20435 }
20436 }
20437 }
20438
20439 BEGV = obegv;
20440 ZV = ozv;
20441 current_buffer = obuf;
20442 }
20443
20444 set_cursor:
20445
365fa1b3 20446#ifndef HAVE_CARBON
fa3c6b4d 20447 if (cursor != No_Cursor)
365fa1b3
AC
20448#else
20449 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
20450#endif
fa3c6b4d
KS
20451 rif->define_frame_cursor (f, cursor);
20452}
20453
20454
20455/* EXPORT for RIF:
20456 Clear any mouse-face on window W. This function is part of the
20457 redisplay interface, and is called from try_window_id and similar
20458 functions to ensure the mouse-highlight is off. */
20459
20460void
20461x_clear_window_mouse_face (w)
20462 struct window *w;
20463{
20464 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20465 Lisp_Object window;
20466
20467 BLOCK_INPUT;
20468 XSETWINDOW (window, w);
20469 if (EQ (window, dpyinfo->mouse_face_window))
20470 clear_mouse_face (dpyinfo);
20471 UNBLOCK_INPUT;
20472}
20473
20474
20475/* EXPORT:
20476 Just discard the mouse face information for frame F, if any.
20477 This is used when the size of F is changed. */
20478
20479void
20480cancel_mouse_face (f)
20481 struct frame *f;
20482{
20483 Lisp_Object window;
20484 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20485
20486 window = dpyinfo->mouse_face_window;
20487 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
20488 {
20489 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20490 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20491 dpyinfo->mouse_face_window = Qnil;
20492 }
20493}
20494
20495
20496#endif /* HAVE_WINDOW_SYSTEM */
20497
20498\f
20499/***********************************************************************
20500 Exposure Events
20501 ***********************************************************************/
20502
20503#ifdef HAVE_WINDOW_SYSTEM
20504
20505/* Redraw the part of glyph row area AREA of glyph row ROW on window W
20506 which intersects rectangle R. R is in window-relative coordinates. */
20507
20508static void
20509expose_area (w, row, r, area)
20510 struct window *w;
20511 struct glyph_row *row;
20512 XRectangle *r;
20513 enum glyph_row_area area;
20514{
20515 struct glyph *first = row->glyphs[area];
20516 struct glyph *end = row->glyphs[area] + row->used[area];
20517 struct glyph *last;
20518 int first_x, start_x, x;
20519
20520 if (area == TEXT_AREA && row->fill_line_p)
20521 /* If row extends face to end of line write the whole line. */
20522 draw_glyphs (w, 0, row, area,
20523 0, row->used[area],
20524 DRAW_NORMAL_TEXT, 0);
20525 else
20526 {
20527 /* Set START_X to the window-relative start position for drawing glyphs of
20528 AREA. The first glyph of the text area can be partially visible.
20529 The first glyphs of other areas cannot. */
da8b7f4f 20530 start_x = window_box_left_offset (w, area);
fa3c6b4d 20531 x = start_x;
29b3ea63
KS
20532 if (area == TEXT_AREA)
20533 x += row->x;
fa3c6b4d
KS
20534
20535 /* Find the first glyph that must be redrawn. */
20536 while (first < end
20537 && x + first->pixel_width < r->x)
20538 {
20539 x += first->pixel_width;
20540 ++first;
20541 }
20542
20543 /* Find the last one. */
20544 last = first;
20545 first_x = x;
20546 while (last < end
20547 && x < r->x + r->width)
20548 {
20549 x += last->pixel_width;
20550 ++last;
20551 }
20552
20553 /* Repaint. */
20554 if (last > first)
20555 draw_glyphs (w, first_x - start_x, row, area,
20556 first - row->glyphs[area], last - row->glyphs[area],
20557 DRAW_NORMAL_TEXT, 0);
20558 }
20559}
20560
20561
20562/* Redraw the parts of the glyph row ROW on window W intersecting
20563 rectangle R. R is in window-relative coordinates. Value is
20564 non-zero if mouse-face was overwritten. */
20565
20566static int
20567expose_line (w, row, r)
20568 struct window *w;
20569 struct glyph_row *row;
20570 XRectangle *r;
20571{
20572 xassert (row->enabled_p);
20573
20574 if (row->mode_line_p || w->pseudo_window_p)
20575 draw_glyphs (w, 0, row, TEXT_AREA,
20576 0, row->used[TEXT_AREA],
20577 DRAW_NORMAL_TEXT, 0);
20578 else
20579 {
20580 if (row->used[LEFT_MARGIN_AREA])
20581 expose_area (w, row, r, LEFT_MARGIN_AREA);
20582 if (row->used[TEXT_AREA])
20583 expose_area (w, row, r, TEXT_AREA);
20584 if (row->used[RIGHT_MARGIN_AREA])
20585 expose_area (w, row, r, RIGHT_MARGIN_AREA);
20586 draw_row_fringe_bitmaps (w, row);
cfe03a41
KS
20587 }
20588
fa3c6b4d
KS
20589 return row->mouse_face_p;
20590}
20591
20592
20593/* Redraw those parts of glyphs rows during expose event handling that
20594 overlap other rows. Redrawing of an exposed line writes over parts
20595 of lines overlapping that exposed line; this function fixes that.
20596
20597 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
20598 row in W's current matrix that is exposed and overlaps other rows.
20599 LAST_OVERLAPPING_ROW is the last such row. */
20600
20601static void
20602expose_overlaps (w, first_overlapping_row, last_overlapping_row)
20603 struct window *w;
20604 struct glyph_row *first_overlapping_row;
20605 struct glyph_row *last_overlapping_row;
20606{
20607 struct glyph_row *row;
20608
20609 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
20610 if (row->overlapping_p)
20611 {
20612 xassert (row->enabled_p && !row->mode_line_p);
20613
20614 if (row->used[LEFT_MARGIN_AREA])
20615 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
cfe03a41 20616
fa3c6b4d
KS
20617 if (row->used[TEXT_AREA])
20618 x_fix_overlapping_area (w, row, TEXT_AREA);
20619
20620 if (row->used[RIGHT_MARGIN_AREA])
20621 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
20622 }
20623}
20624
20625
20626/* Return non-zero if W's cursor intersects rectangle R. */
20627
20628static int
20629phys_cursor_in_rect_p (w, r)
20630 struct window *w;
20631 XRectangle *r;
20632{
20633 XRectangle cr, result;
20634 struct glyph *cursor_glyph;
20635
20636 cursor_glyph = get_phys_cursor_glyph (w);
20637 if (cursor_glyph)
cfe03a41 20638 {
aecf50d2
KS
20639 /* r is relative to W's box, but w->phys_cursor.x is relative
20640 to left edge of W's TEXT area. Adjust it. */
20641 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
fa3c6b4d
KS
20642 cr.y = w->phys_cursor.y;
20643 cr.width = cursor_glyph->pixel_width;
20644 cr.height = w->phys_cursor_height;
20645 /* ++KFS: W32 version used W32-specific IntersectRect here, but
20646 I assume the effect is the same -- and this is portable. */
20647 return x_intersect_rectangles (&cr, r, &result);
cfe03a41 20648 }
fa3c6b4d
KS
20649 else
20650 return 0;
20651}
cfe03a41 20652
cfe03a41 20653
fa3c6b4d
KS
20654/* EXPORT:
20655 Draw a vertical window border to the right of window W if W doesn't
20656 have vertical scroll bars. */
cfe03a41 20657
cfe03a41 20658void
fa3c6b4d
KS
20659x_draw_vertical_border (w)
20660 struct window *w;
cfe03a41 20661{
da8b7f4f
KS
20662 /* We could do better, if we knew what type of scroll-bar the adjacent
20663 windows (on either side) have... But we don't :-(
20664 However, I think this works ok. ++KFS 2003-04-25 */
20665
fa3c6b4d
KS
20666 /* Redraw borders between horizontally adjacent windows. Don't
20667 do it for frames with vertical scroll bars because either the
20668 right scroll bar of a window, or the left scroll bar of its
20669 neighbor will suffice as a border. */
20670 if (!WINDOW_RIGHTMOST_P (w)
da8b7f4f 20671 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
fa3c6b4d
KS
20672 {
20673 int x0, x1, y0, y1;
cfe03a41 20674
fa3c6b4d 20675 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
fa3c6b4d 20676 y1 -= 1;
cfe03a41 20677
fa3c6b4d 20678 rif->draw_vertical_window_border (w, x1, y0, y1);
cfe03a41 20679 }
da8b7f4f
KS
20680 else if (!WINDOW_LEFTMOST_P (w)
20681 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
20682 {
20683 int x0, x1, y0, y1;
20684
20685 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
20686 y1 -= 1;
20687
20688 rif->draw_vertical_window_border (w, x0, y0, y1);
20689 }
cfe03a41
KS
20690}
20691
20692
fa3c6b4d
KS
20693/* Redraw the part of window W intersection rectangle FR. Pixel
20694 coordinates in FR are frame-relative. Call this function with
20695 input blocked. Value is non-zero if the exposure overwrites
20696 mouse-face. */
cfe03a41 20697
fa3c6b4d
KS
20698static int
20699expose_window (w, fr)
cfe03a41 20700 struct window *w;
fa3c6b4d 20701 XRectangle *fr;
cfe03a41
KS
20702{
20703 struct frame *f = XFRAME (w->frame);
fa3c6b4d
KS
20704 XRectangle wr, r;
20705 int mouse_face_overwritten_p = 0;
20706
20707 /* If window is not yet fully initialized, do nothing. This can
20708 happen when toolkit scroll bars are used and a window is split.
20709 Reconfiguring the scroll bar will generate an expose for a newly
20710 created window. */
20711 if (w->current_matrix == NULL)
20712 return 0;
cfe03a41 20713
fa3c6b4d
KS
20714 /* When we're currently updating the window, display and current
20715 matrix usually don't agree. Arrange for a thorough display
20716 later. */
20717 if (w == updated_window)
20718 {
20719 SET_FRAME_GARBAGED (f);
20720 return 0;
20721 }
e9c99027 20722
fa3c6b4d 20723 /* Frame-relative pixel rectangle of W. */
da8b7f4f
KS
20724 wr.x = WINDOW_LEFT_EDGE_X (w);
20725 wr.y = WINDOW_TOP_EDGE_Y (w);
20726 wr.width = WINDOW_TOTAL_WIDTH (w);
20727 wr.height = WINDOW_TOTAL_HEIGHT (w);
fa3c6b4d
KS
20728
20729 if (x_intersect_rectangles (fr, &wr, &r))
cfe03a41 20730 {
fa3c6b4d
KS
20731 int yb = window_text_bottom_y (w);
20732 struct glyph_row *row;
20733 int cursor_cleared_p;
20734 struct glyph_row *first_overlapping_row, *last_overlapping_row;
20735
20736 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
20737 r.x, r.y, r.width, r.height));
20738
20739 /* Convert to window coordinates. */
cc8549df
KS
20740 r.x -= WINDOW_LEFT_EDGE_X (w);
20741 r.y -= WINDOW_TOP_EDGE_Y (w);
fa3c6b4d
KS
20742
20743 /* Turn off the cursor. */
20744 if (!w->pseudo_window_p
20745 && phys_cursor_in_rect_p (w, &r))
cfe03a41 20746 {
fa3c6b4d
KS
20747 x_clear_cursor (w);
20748 cursor_cleared_p = 1;
cfe03a41 20749 }
fa3c6b4d
KS
20750 else
20751 cursor_cleared_p = 0;
cfe03a41 20752
fa3c6b4d
KS
20753 /* Update lines intersecting rectangle R. */
20754 first_overlapping_row = last_overlapping_row = NULL;
20755 for (row = w->current_matrix->rows;
20756 row->enabled_p;
20757 ++row)
20758 {
20759 int y0 = row->y;
20760 int y1 = MATRIX_ROW_BOTTOM_Y (row);
cfe03a41 20761
fa3c6b4d
KS
20762 if ((y0 >= r.y && y0 < r.y + r.height)
20763 || (y1 > r.y && y1 < r.y + r.height)
20764 || (r.y >= y0 && r.y < y1)
20765 || (r.y + r.height > y0 && r.y + r.height < y1))
20766 {
20767 if (row->overlapping_p)
20768 {
20769 if (first_overlapping_row == NULL)
20770 first_overlapping_row = row;
20771 last_overlapping_row = row;
20772 }
e9c99027 20773
fa3c6b4d
KS
20774 if (expose_line (w, row, &r))
20775 mouse_face_overwritten_p = 1;
20776 }
cfe03a41 20777
fa3c6b4d
KS
20778 if (y1 >= yb)
20779 break;
20780 }
cfe03a41 20781
fa3c6b4d
KS
20782 /* Display the mode line if there is one. */
20783 if (WINDOW_WANTS_MODELINE_P (w)
20784 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
20785 row->enabled_p)
20786 && row->y < r.y + r.height)
20787 {
20788 if (expose_line (w, row, &r))
20789 mouse_face_overwritten_p = 1;
20790 }
cfe03a41 20791
fa3c6b4d
KS
20792 if (!w->pseudo_window_p)
20793 {
20794 /* Fix the display of overlapping rows. */
20795 if (first_overlapping_row)
20796 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
20797
20798 /* Draw border between windows. */
20799 x_draw_vertical_border (w);
20800
20801 /* Turn the cursor on again. */
20802 if (cursor_cleared_p)
20803 update_window_cursor (w, 1);
20804 }
20805 }
20806
20807#ifdef HAVE_CARBON
20808 /* Display scroll bar for this window. */
20809 if (!NILP (w->vertical_scroll_bar))
cfe03a41 20810 {
fa3c6b4d
KS
20811 /* ++KFS:
20812 If this doesn't work here (maybe some header files are missing),
20813 make a function in macterm.c and call it to do the job! */
20814 ControlHandle ch
20815 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
20816
20817 Draw1Control (ch);
cfe03a41 20818 }
fa3c6b4d 20819#endif
cfe03a41 20820
fa3c6b4d
KS
20821 return mouse_face_overwritten_p;
20822}
20823
20824
20825
20826/* Redraw (parts) of all windows in the window tree rooted at W that
20827 intersect R. R contains frame pixel coordinates. Value is
20828 non-zero if the exposure overwrites mouse-face. */
20829
20830static int
20831expose_window_tree (w, r)
20832 struct window *w;
20833 XRectangle *r;
20834{
20835 struct frame *f = XFRAME (w->frame);
20836 int mouse_face_overwritten_p = 0;
20837
20838 while (w && !FRAME_GARBAGED_P (f))
cfe03a41 20839 {
fa3c6b4d
KS
20840 if (!NILP (w->hchild))
20841 mouse_face_overwritten_p
20842 |= expose_window_tree (XWINDOW (w->hchild), r);
20843 else if (!NILP (w->vchild))
20844 mouse_face_overwritten_p
20845 |= expose_window_tree (XWINDOW (w->vchild), r);
20846 else
20847 mouse_face_overwritten_p |= expose_window (w, r);
20848
20849 w = NILP (w->next) ? NULL : XWINDOW (w->next);
cfe03a41 20850 }
cfe03a41 20851
fa3c6b4d
KS
20852 return mouse_face_overwritten_p;
20853}
cfe03a41 20854
cfe03a41 20855
fa3c6b4d
KS
20856/* EXPORT:
20857 Redisplay an exposed area of frame F. X and Y are the upper-left
20858 corner of the exposed rectangle. W and H are width and height of
20859 the exposed area. All are pixel values. W or H zero means redraw
20860 the entire frame. */
cfe03a41 20861
fa3c6b4d
KS
20862void
20863expose_frame (f, x, y, w, h)
20864 struct frame *f;
20865 int x, y, w, h;
20866{
20867 XRectangle r;
20868 int mouse_face_overwritten_p = 0;
20869
20870 TRACE ((stderr, "expose_frame "));
20871
20872 /* No need to redraw if frame will be redrawn soon. */
20873 if (FRAME_GARBAGED_P (f))
cfe03a41 20874 {
fa3c6b4d
KS
20875 TRACE ((stderr, " garbaged\n"));
20876 return;
cfe03a41 20877 }
7d0393cf 20878
fa3c6b4d
KS
20879#ifdef HAVE_CARBON
20880 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
20881 or deactivated here, for unknown reasons, activated scroll bars
20882 are shown in deactivated frames in some instances. */
20883 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
20884 activate_scroll_bars (f);
20885 else
20886 deactivate_scroll_bars (f);
20887#endif
cfe03a41 20888
fa3c6b4d
KS
20889 /* If basic faces haven't been realized yet, there is no point in
20890 trying to redraw anything. This can happen when we get an expose
20891 event while Emacs is starting, e.g. by moving another window. */
20892 if (FRAME_FACE_CACHE (f) == NULL
20893 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
20894 {
20895 TRACE ((stderr, " no faces\n"));
20896 return;
20897 }
cfe03a41 20898
fa3c6b4d 20899 if (w == 0 || h == 0)
cfe03a41 20900 {
fa3c6b4d 20901 r.x = r.y = 0;
da8b7f4f
KS
20902 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
20903 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
fa3c6b4d
KS
20904 }
20905 else
20906 {
20907 r.x = x;
20908 r.y = y;
20909 r.width = w;
20910 r.height = h;
cfe03a41
KS
20911 }
20912
fa3c6b4d
KS
20913 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
20914 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
20915
20916 if (WINDOWP (f->tool_bar_window))
20917 mouse_face_overwritten_p
20918 |= expose_window (XWINDOW (f->tool_bar_window), &r);
20919
20920#ifdef HAVE_X_WINDOWS
20921#ifndef MSDOS
20922#ifndef USE_X_TOOLKIT
20923 if (WINDOWP (f->menu_bar_window))
20924 mouse_face_overwritten_p
20925 |= expose_window (XWINDOW (f->menu_bar_window), &r);
20926#endif /* not USE_X_TOOLKIT */
20927#endif
20928#endif
20929
20930 /* Some window managers support a focus-follows-mouse style with
20931 delayed raising of frames. Imagine a partially obscured frame,
20932 and moving the mouse into partially obscured mouse-face on that
20933 frame. The visible part of the mouse-face will be highlighted,
20934 then the WM raises the obscured frame. With at least one WM, KDE
20935 2.1, Emacs is not getting any event for the raising of the frame
20936 (even tried with SubstructureRedirectMask), only Expose events.
20937 These expose events will draw text normally, i.e. not
20938 highlighted. Which means we must redo the highlight here.
20939 Subsume it under ``we love X''. --gerd 2001-08-15 */
20940 /* Included in Windows version because Windows most likely does not
20941 do the right thing if any third party tool offers
20942 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
20943 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
20944 {
20945 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20946 if (f == dpyinfo->mouse_face_mouse_frame)
20947 {
20948 int x = dpyinfo->mouse_face_mouse_x;
20949 int y = dpyinfo->mouse_face_mouse_y;
20950 clear_mouse_face (dpyinfo);
20951 note_mouse_highlight (f, x, y);
20952 }
20953 }
cfe03a41
KS
20954}
20955
133c764e 20956
fa3c6b4d
KS
20957/* EXPORT:
20958 Determine the intersection of two rectangles R1 and R2. Return
20959 the intersection in *RESULT. Value is non-zero if RESULT is not
20960 empty. */
133c764e 20961
fa3c6b4d
KS
20962int
20963x_intersect_rectangles (r1, r2, result)
20964 XRectangle *r1, *r2, *result;
133c764e 20965{
fa3c6b4d
KS
20966 XRectangle *left, *right;
20967 XRectangle *upper, *lower;
20968 int intersection_p = 0;
20969
20970 /* Rearrange so that R1 is the left-most rectangle. */
20971 if (r1->x < r2->x)
20972 left = r1, right = r2;
20973 else
20974 left = r2, right = r1;
20975
20976 /* X0 of the intersection is right.x0, if this is inside R1,
20977 otherwise there is no intersection. */
20978 if (right->x <= left->x + left->width)
133c764e 20979 {
fa3c6b4d 20980 result->x = right->x;
133c764e 20981
fa3c6b4d
KS
20982 /* The right end of the intersection is the minimum of the
20983 the right ends of left and right. */
20984 result->width = (min (left->x + left->width, right->x + right->width)
20985 - result->x);
133c764e 20986
fa3c6b4d
KS
20987 /* Same game for Y. */
20988 if (r1->y < r2->y)
20989 upper = r1, lower = r2;
20990 else
20991 upper = r2, lower = r1;
133c764e 20992
fa3c6b4d
KS
20993 /* The upper end of the intersection is lower.y0, if this is inside
20994 of upper. Otherwise, there is no intersection. */
20995 if (lower->y <= upper->y + upper->height)
20996 {
20997 result->y = lower->y;
20998
20999 /* The lower end of the intersection is the minimum of the lower
21000 ends of upper and lower. */
21001 result->height = (min (lower->y + lower->height,
21002 upper->y + upper->height)
21003 - result->y);
21004 intersection_p = 1;
133c764e
KS
21005 }
21006 }
fa3c6b4d
KS
21007
21008 return intersection_p;
133c764e
KS
21009}
21010
fa3c6b4d
KS
21011#endif /* HAVE_WINDOW_SYSTEM */
21012
cfe03a41 21013\f
5f5c8ee5
GM
21014/***********************************************************************
21015 Initialization
21016 ***********************************************************************/
21017
a2889657
JB
21018void
21019syms_of_xdisp ()
21020{
c6e89d6c
GM
21021 Vwith_echo_area_save_vector = Qnil;
21022 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 21023
c6e89d6c
GM
21024 Vmessage_stack = Qnil;
21025 staticpro (&Vmessage_stack);
2311178e 21026
735c094c 21027 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 21028 staticpro (&Qinhibit_redisplay);
735c094c 21029
b14bc55e
RS
21030 message_dolog_marker1 = Fmake_marker ();
21031 staticpro (&message_dolog_marker1);
21032 message_dolog_marker2 = Fmake_marker ();
21033 staticpro (&message_dolog_marker2);
21034 message_dolog_marker3 = Fmake_marker ();
21035 staticpro (&message_dolog_marker3);
21036
5f5c8ee5 21037#if GLYPH_DEBUG
7d4cc828 21038 defsubr (&Sdump_frame_glyph_matrix);
5f5c8ee5
GM
21039 defsubr (&Sdump_glyph_matrix);
21040 defsubr (&Sdump_glyph_row);
e037b9ec 21041 defsubr (&Sdump_tool_bar_row);
62397849 21042 defsubr (&Strace_redisplay);
bf9249e3 21043 defsubr (&Strace_to_stderr);
5f5c8ee5 21044#endif
99a5de87 21045#ifdef HAVE_WINDOW_SYSTEM
57c28064 21046 defsubr (&Stool_bar_lines_needed);
99a5de87 21047#endif
8143e6ab 21048 defsubr (&Sformat_mode_line);
5f5c8ee5 21049
cf074754
RS
21050 staticpro (&Qmenu_bar_update_hook);
21051 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
21052
d46fb96a 21053 staticpro (&Qoverriding_terminal_local_map);
7079aefa 21054 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 21055
399164b4
KH
21056 staticpro (&Qoverriding_local_map);
21057 Qoverriding_local_map = intern ("overriding-local-map");
21058
75c43375
RS
21059 staticpro (&Qwindow_scroll_functions);
21060 Qwindow_scroll_functions = intern ("window-scroll-functions");
21061
e0bfbde6
RS
21062 staticpro (&Qredisplay_end_trigger_functions);
21063 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
2311178e 21064
2e54982e
RS
21065 staticpro (&Qinhibit_point_motion_hooks);
21066 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
21067
9499d71b
GM
21068 QCdata = intern (":data");
21069 staticpro (&QCdata);
5f5c8ee5 21070 Qdisplay = intern ("display");
f3751a65 21071 staticpro (&Qdisplay);
5f5c8ee5
GM
21072 Qspace_width = intern ("space-width");
21073 staticpro (&Qspace_width);
5f5c8ee5
GM
21074 Qraise = intern ("raise");
21075 staticpro (&Qraise);
21076 Qspace = intern ("space");
21077 staticpro (&Qspace);
f3751a65
GM
21078 Qmargin = intern ("margin");
21079 staticpro (&Qmargin);
5f5c8ee5 21080 Qleft_margin = intern ("left-margin");
f3751a65 21081 staticpro (&Qleft_margin);
5f5c8ee5 21082 Qright_margin = intern ("right-margin");
f3751a65 21083 staticpro (&Qright_margin);
5f5c8ee5
GM
21084 Qalign_to = intern ("align-to");
21085 staticpro (&Qalign_to);
21086 QCalign_to = intern (":align-to");
21087 staticpro (&QCalign_to);
5f5c8ee5
GM
21088 Qrelative_width = intern ("relative-width");
21089 staticpro (&Qrelative_width);
21090 QCrelative_width = intern (":relative-width");
21091 staticpro (&QCrelative_width);
21092 QCrelative_height = intern (":relative-height");
21093 staticpro (&QCrelative_height);
21094 QCeval = intern (":eval");
21095 staticpro (&QCeval);
0fcf414f
RS
21096 QCpropertize = intern (":propertize");
21097 staticpro (&QCpropertize);
886bd6f2
GM
21098 QCfile = intern (":file");
21099 staticpro (&QCfile);
5f5c8ee5
GM
21100 Qfontified = intern ("fontified");
21101 staticpro (&Qfontified);
21102 Qfontification_functions = intern ("fontification-functions");
21103 staticpro (&Qfontification_functions);
5f5c8ee5
GM
21104 Qtrailing_whitespace = intern ("trailing-whitespace");
21105 staticpro (&Qtrailing_whitespace);
21106 Qimage = intern ("image");
21107 staticpro (&Qimage);
ad4f174e
GM
21108 Qmessage_truncate_lines = intern ("message-truncate-lines");
21109 staticpro (&Qmessage_truncate_lines);
af79bccb
RS
21110 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
21111 staticpro (&Qcursor_in_non_selected_windows);
6422c1d7
GM
21112 Qgrow_only = intern ("grow-only");
21113 staticpro (&Qgrow_only);
e1477f43
GM
21114 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
21115 staticpro (&Qinhibit_menubar_update);
30a3f61c
GM
21116 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
21117 staticpro (&Qinhibit_eval_during_redisplay);
b384d6f8
GM
21118 Qposition = intern ("position");
21119 staticpro (&Qposition);
21120 Qbuffer_position = intern ("buffer-position");
21121 staticpro (&Qbuffer_position);
21122 Qobject = intern ("object");
21123 staticpro (&Qobject);
cfe03a41
KS
21124 Qbar = intern ("bar");
21125 staticpro (&Qbar);
21126 Qhbar = intern ("hbar");
21127 staticpro (&Qhbar);
21128 Qbox = intern ("box");
21129 staticpro (&Qbox);
21130 Qhollow = intern ("hollow");
21131 staticpro (&Qhollow);
c53a1624
RS
21132 Qrisky_local_variable = intern ("risky-local-variable");
21133 staticpro (&Qrisky_local_variable);
26683087
RS
21134 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
21135 staticpro (&Qinhibit_free_realized_faces);
5f5c8ee5 21136
7033d6df
RS
21137 list_of_error = Fcons (intern ("error"), Qnil);
21138 staticpro (&list_of_error);
21139
a2889657
JB
21140 last_arrow_position = Qnil;
21141 last_arrow_string = Qnil;
f3751a65
GM
21142 staticpro (&last_arrow_position);
21143 staticpro (&last_arrow_string);
2311178e 21144
c6e89d6c
GM
21145 echo_buffer[0] = echo_buffer[1] = Qnil;
21146 staticpro (&echo_buffer[0]);
21147 staticpro (&echo_buffer[1]);
21148
21149 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
21150 staticpro (&echo_area_buffer[0]);
21151 staticpro (&echo_area_buffer[1]);
a2889657 21152
6a94510a
GM
21153 Vmessages_buffer_name = build_string ("*Messages*");
21154 staticpro (&Vmessages_buffer_name);
0fcf414f
RS
21155
21156 mode_line_proptrans_alist = Qnil;
21157 staticpro (&mode_line_proptrans_alist);
2311178e 21158
fec8f23e
KS
21159 mode_line_string_list = Qnil;
21160 staticpro (&mode_line_string_list);
21161
fa3c6b4d
KS
21162 help_echo_string = Qnil;
21163 staticpro (&help_echo_string);
21164 help_echo_object = Qnil;
21165 staticpro (&help_echo_object);
21166 help_echo_window = Qnil;
21167 staticpro (&help_echo_window);
21168 previous_help_echo_string = Qnil;
21169 staticpro (&previous_help_echo_string);
21170 help_echo_pos = -1;
21171
21172#ifdef HAVE_WINDOW_SYSTEM
21173 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
21174 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
21175For example, if a block cursor is over a tab, it will be drawn as
21176wide as that tab on the display. */);
21177 x_stretch_cursor_p = 0;
21178#endif
21179
7ee72033
MB
21180 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
21181 doc: /* Non-nil means highlight trailing whitespace.
228299fa 21182The face used for trailing whitespace is `trailing-whitespace'. */);
8f897821
GM
21183 Vshow_trailing_whitespace = Qnil;
21184
f65536fa
KS
21185 DEFVAR_LISP ("show-text-cursor-in-void", &Vshow_text_cursor_in_void,
21186 doc: /* Non-nil means show the text cursor in void text areas.
21187The default is to show the non-text (typically arrow) cursor. */);
21188 Vshow_text_cursor_in_void = Qnil;
21189
7ee72033
MB
21190 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
21191 doc: /* Non-nil means don't actually do any redisplay.
228299fa 21192This is used for internal purposes. */);
735c094c
KH
21193 Vinhibit_redisplay = Qnil;
21194
7ee72033
MB
21195 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
21196 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
a2889657
JB
21197 Vglobal_mode_string = Qnil;
21198
7ee72033
MB
21199 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
21200 doc: /* Marker for where to display an arrow on top of the buffer text.
228299fa
GM
21201This must be the beginning of a line in order to work.
21202See also `overlay-arrow-string'. */);
a2889657
JB
21203 Voverlay_arrow_position = Qnil;
21204
7ee72033
MB
21205 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
21206 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
a2889657
JB
21207 Voverlay_arrow_string = Qnil;
21208
7ee72033
MB
21209 DEFVAR_INT ("scroll-step", &scroll_step,
21210 doc: /* *The number of lines to try scrolling a window by when point moves out.
228299fa
GM
21211If that fails to bring point back on frame, point is centered instead.
21212If this is zero, point is always centered after it moves off frame.
21213If you want scrolling to always be a line at a time, you should set
21214`scroll-conservatively' to a large value rather than set this to 1. */);
21215
7ee72033
MB
21216 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
21217 doc: /* *Scroll up to this many lines, to bring point back on screen.
228299fa
GM
21218A value of zero means to scroll the text to center point vertically
21219in the window. */);
0789adb2
RS
21220 scroll_conservatively = 0;
21221
7ee72033
MB
21222 DEFVAR_INT ("scroll-margin", &scroll_margin,
21223 doc: /* *Number of lines of margin at the top and bottom of a window.
228299fa
GM
21224Recenter the window whenever point gets within this many lines
21225of the top or bottom of the window. */);
9afd2168
RS
21226 scroll_margin = 0;
21227
f65536fa
KS
21228 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
21229 doc: /* Pixels per inch on current display.
21230Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
21231 Vdisplay_pixels_per_inch = make_float (72.0);
21232
5f5c8ee5 21233#if GLYPH_DEBUG
7ee72033 21234 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
5f5c8ee5 21235#endif
a2889657
JB
21236
21237 DEFVAR_BOOL ("truncate-partial-width-windows",
7ee72033
MB
21238 &truncate_partial_width_windows,
21239 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
a2889657
JB
21240 truncate_partial_width_windows = 1;
21241
7ee72033
MB
21242 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
21243 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
228299fa 21244Any other value means to use the appropriate face, `mode-line',
ccfe8f57 21245`header-line', or `menu' respectively. */);
1862a24e 21246 mode_line_inverse_video = 1;
aa6d10fa 21247
7ee72033
MB
21248 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
21249 doc: /* *Maximum buffer size for which line number should be displayed.
228299fa
GM
21250If the buffer is bigger than this, the line number does not appear
21251in the mode line. A value of nil means no limit. */);
090703f4 21252 Vline_number_display_limit = Qnil;
fba9ce76 21253
090703f4 21254 DEFVAR_INT ("line-number-display-limit-width",
7ee72033
MB
21255 &line_number_display_limit_width,
21256 doc: /* *Maximum line width (in characters) for line number display.
228299fa
GM
21257If the average length of the lines near point is bigger than this, then the
21258line number may be omitted from the mode line. */);
5d121aec
KH
21259 line_number_display_limit_width = 200;
21260
7ee72033
MB
21261 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
21262 doc: /* *Non-nil means highlight region even in nonselected windows. */);
293a54ce 21263 highlight_nonselected_windows = 0;
d39b6696 21264
7ee72033
MB
21265 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
21266 doc: /* Non-nil if more than one frame is visible on this display.
228299fa
GM
21267Minibuffer-only frames don't count, but iconified frames do.
21268This variable is not guaranteed to be accurate except while processing
21269`frame-title-format' and `icon-title-format'. */);
21270
7ee72033
MB
21271 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
21272 doc: /* Template for displaying the title bar of visible frames.
228299fa
GM
21273\(Assuming the window manager supports this feature.)
21274This variable has the same structure as `mode-line-format' (which see),
21275and is used only on frames for which no explicit name has been set
21276\(see `modify-frame-parameters'). */);
f65536fa 21277
7ee72033
MB
21278 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
21279 doc: /* Template for displaying the title bar of an iconified frame.
228299fa
GM
21280\(Assuming the window manager supports this feature.)
21281This variable has the same structure as `mode-line-format' (which see),
21282and is used only on frames for which no explicit name has been set
21283\(see `modify-frame-parameters'). */);
d39b6696
KH
21284 Vicon_title_format
21285 = Vframe_title_format
21286 = Fcons (intern ("multiple-frames"),
21287 Fcons (build_string ("%b"),
3ebf0ea9 21288 Fcons (Fcons (empty_string,
d39b6696
KH
21289 Fcons (intern ("invocation-name"),
21290 Fcons (build_string ("@"),
21291 Fcons (intern ("system-name"),
21292 Qnil)))),
21293 Qnil)));
5992c4f7 21294
7ee72033
MB
21295 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
21296 doc: /* Maximum number of lines to keep in the message log buffer.
228299fa
GM
21297If nil, disable message logging. If t, log messages but don't truncate
21298the buffer when it becomes large. */);
ac90c44f 21299 Vmessage_log_max = make_number (50);
08b610e4 21300
7ee72033
MB
21301 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
21302 doc: /* Functions called before redisplay, if window sizes have changed.
228299fa
GM
21303The value should be a list of functions that take one argument.
21304Just before redisplay, for each frame, if any of its windows have changed
21305size since the last redisplay, or have been split or deleted,
21306all the functions in the list are called, with the frame as argument. */);
08b610e4 21307 Vwindow_size_change_functions = Qnil;
75c43375 21308
7ee72033
MB
21309 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
21310 doc: /* List of Functions to call before redisplaying a window with scrolling.
228299fa
GM
21311Each function is called with two arguments, the window
21312and its new display-start position. Note that the value of `window-end'
21313is not valid when these functions are called. */);
75c43375 21314 Vwindow_scroll_functions = Qnil;
2311178e 21315
fa3c6b4d
KS
21316 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
21317 doc: /* *Non-nil means autoselect window with mouse pointer. */);
21318 mouse_autoselect_window = 0;
21319
7ee72033
MB
21320 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
21321 doc: /* *Non-nil means automatically resize tool-bars.
228299fa
GM
21322This increases a tool-bar's height if not all tool-bar items are visible.
21323It decreases a tool-bar's height when it would display blank lines
21324otherwise. */);
e037b9ec 21325 auto_resize_tool_bars_p = 1;
2311178e 21326
7ee72033
MB
21327 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
21328 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
e037b9ec 21329 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 21330
7ee72033
MB
21331 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
21332 doc: /* *Margin around tool-bar buttons in pixels.
228299fa 21333If an integer, use that for both horizontal and vertical margins.
f6c89f27 21334Otherwise, value should be a pair of integers `(HORZ . VERT)' with
228299fa
GM
21335HORZ specifying the horizontal margin, and VERT specifying the
21336vertical margin. */);
c3d76173 21337 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
5f5c8ee5 21338
7ee72033 21339 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
6da3c85b 21340 doc: /* *Relief thickness of tool-bar buttons. */);
c3d76173 21341 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
5f5c8ee5 21342
7ee72033
MB
21343 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
21344 doc: /* List of functions to call to fontify regions of text.
228299fa
GM
21345Each function is called with one argument POS. Functions must
21346fontify a region starting at POS in the current buffer, and give
21347fontified regions the property `fontified'. */);
5f5c8ee5 21348 Vfontification_functions = Qnil;
6b9f0906 21349 Fmake_variable_buffer_local (Qfontification_functions);
7bbe686f
AI
21350
21351 DEFVAR_BOOL ("unibyte-display-via-language-environment",
7ee72033
MB
21352 &unibyte_display_via_language_environment,
21353 doc: /* *Non-nil means display unibyte text according to language environment.
228299fa
GM
21354Specifically this means that unibyte non-ASCII characters
21355are displayed by converting them to the equivalent multibyte characters
21356according to the current language environment. As a result, they are
21357displayed according to the current fontset. */);
7bbe686f 21358 unibyte_display_via_language_environment = 0;
c6e89d6c 21359
7ee72033
MB
21360 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
21361 doc: /* *Maximum height for resizing mini-windows.
228299fa
GM
21362If a float, it specifies a fraction of the mini-window frame's height.
21363If an integer, it specifies a number of lines. */);
c6e89d6c 21364 Vmax_mini_window_height = make_float (0.25);
6422c1d7 21365
7ee72033
MB
21366 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
21367 doc: /* *How to resize mini-windows.
228299fa
GM
21368A value of nil means don't automatically resize mini-windows.
21369A value of t means resize them to fit the text displayed in them.
21370A value of `grow-only', the default, means let mini-windows grow
21371only, until their display becomes empty, at which point the windows
21372go back to their normal size. */);
6422c1d7
GM
21373 Vresize_mini_windows = Qgrow_only;
21374
cfe03a41
KS
21375 DEFVAR_LISP ("cursor-in-non-selected-windows",
21376 &Vcursor_in_non_selected_windows,
21377 doc: /* *Cursor type to display in non-selected windows.
21378t means to use hollow box cursor. See `cursor-type' for other values. */);
21379 Vcursor_in_non_selected_windows = Qt;
21380
cfe03a41
KS
21381 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
21382 doc: /* Alist specifying how to blink the cursor off.
21383Each element has the form (ON-STATE . OFF-STATE). Whenever the
21384`cursor-type' frame-parameter or variable equals ON-STATE,
21385comparing using `equal', Emacs uses OFF-STATE to specify
21386how to blink it off. */);
21387 Vblink_cursor_alist = Qnil;
2311178e 21388
e76d28d5 21389 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
7ee72033 21390 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
d475bcb8 21391 automatic_hscrolling_p = 1;
1df7e8f0 21392
e76d28d5 21393 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
1df7e8f0
EZ
21394 doc: /* *How many columns away from the window edge point is allowed to get
21395before automatic hscrolling will horizontally scroll the window. */);
e76d28d5 21396 hscroll_margin = 5;
1df7e8f0 21397
e76d28d5 21398 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
1df7e8f0
EZ
21399 doc: /* *How many columns to scroll the window when point gets too close to the edge.
21400When point is less than `automatic-hscroll-margin' columns from the window
21401edge, automatic hscrolling will scroll the window by the amount of columns
21402determined by this variable. If its value is a positive integer, scroll that
21403many columns. If it's a positive floating-point number, it specifies the
21404fraction of the window's width to scroll. If it's nil or zero, point will be
21405centered horizontally after the scroll. Any other value, including negative
21406numbers, are treated as if the value were zero.
21407
21408Automatic hscrolling always moves point outside the scroll margin, so if
21409point was more than scroll step columns inside the margin, the window will
21410scroll more than the value given by the scroll step.
21411
21412Note that the lower bound for automatic hscrolling specified by `scroll-left'
21413and `scroll-right' overrides this variable's effect. */);
e76d28d5 21414 Vhscroll_step = make_number (0);
2311178e 21415
7ee72033
MB
21416 DEFVAR_LISP ("image-types", &Vimage_types,
21417 doc: /* List of supported image types.
228299fa 21418Each element of the list is a symbol for a supported image type. */);
e00daaa0 21419 Vimage_types = Qnil;
2311178e 21420
7ee72033
MB
21421 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
21422 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
228299fa 21423Bind this around calls to `message' to let it take effect. */);
ad4f174e 21424 message_truncate_lines = 0;
0bca8940 21425
7ee72033
MB
21426 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
21427 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
228299fa 21428Can be used to update submenus whose contents should vary. */);
6422c1d7 21429 Vmenu_bar_update_hook = Qnil;
2311178e 21430
7ee72033
MB
21431 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
21432 doc: /* Non-nil means don't update menu bars. Internal use only. */);
e1477f43 21433 inhibit_menubar_update = 0;
30a3f61c 21434
7ee72033
MB
21435 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
21436 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30a3f61c 21437 inhibit_eval_during_redisplay = 0;
76cb5e06 21438
26683087
RS
21439 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
21440 doc: /* Non-nil means don't free realized faces. Internal use only. */);
21441 inhibit_free_realized_faces = 0;
21442
69d1f7c9 21443#if GLYPH_DEBUG
76cb5e06
GM
21444 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
21445 doc: /* Inhibit try_window_id display optimization. */);
21446 inhibit_try_window_id = 0;
21447
21448 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
21449 doc: /* Inhibit try_window_reusing display optimization. */);
21450 inhibit_try_window_reusing = 0;
21451
21452 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
21453 doc: /* Inhibit try_cursor_movement display optimization. */);
21454 inhibit_try_cursor_movement = 0;
21455#endif /* GLYPH_DEBUG */
a2889657
JB
21456}
21457
5f5c8ee5
GM
21458
21459/* Initialize this module when Emacs starts. */
21460
dfcf069d 21461void
a2889657
JB
21462init_xdisp ()
21463{
21464 Lisp_Object root_window;
5f5c8ee5 21465 struct window *mini_w;
a2889657 21466
04612a64
GM
21467 current_header_line_height = current_mode_line_height = -1;
21468
5f5c8ee5 21469 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
21470
21471 mini_w = XWINDOW (minibuf_window);
11e82b76 21472 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 21473
a2889657
JB
21474 if (!noninteractive)
21475 {
5f5c8ee5
GM
21476 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
21477 int i;
21478
da8b7f4f 21479 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
12c226c5 21480 set_window_height (root_window,
da8b7f4f 21481 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 21482 0);
da8b7f4f 21483 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
a2889657
JB
21484 set_window_height (minibuf_window, 1, 0);
21485
da8b7f4f
KS
21486 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
21487 mini_w->total_cols = make_number (FRAME_COLS (f));
5f5c8ee5
GM
21488
21489 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
21490 scratch_glyph_row.glyphs[TEXT_AREA + 1]
21491 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
21492
2311178e 21493 /* The default ellipsis glyphs `...'. */
5f5c8ee5 21494 for (i = 0; i < 3; ++i)
ac90c44f 21495 default_invis_vector[i] = make_number ('.');
a2889657 21496 }
5f5c8ee5 21497
5f5c8ee5 21498 {
93da8435
SM
21499 /* Allocate the buffer for frame titles.
21500 Also used for `format-mode-line'. */
5f5c8ee5
GM
21501 int size = 100;
21502 frame_title_buf = (char *) xmalloc (size);
21503 frame_title_buf_end = frame_title_buf + size;
21504 frame_title_ptr = NULL;
21505 }
2311178e 21506
21fdfb65 21507 help_echo_showing_p = 0;
a2889657 21508}
5f5c8ee5
GM
21509
21510
ab5796a9
MB
21511/* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
21512 (do not change this comment) */