lib-src/emacsclient.c: Implemented --here option (open a new Emacs tty). Needs...
[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
1987b083
RS
739/* Non-zero while redisplay_internal is in progress. */
740
741int redisplaying_p;
742
26683087
RS
743/* Non-zero means don't free realized faces. Bound while freeing
744 realized faces is dangerous because glyph matrices might still
745 reference them. */
1987b083 746
26683087
RS
747int inhibit_free_realized_faces;
748Lisp_Object Qinhibit_free_realized_faces;
ff6c30e5 749
fa3c6b4d
KS
750/* If a string, XTread_socket generates an event to display that string.
751 (The display is done in read_char.) */
752
753Lisp_Object help_echo_string;
754Lisp_Object help_echo_window;
755Lisp_Object help_echo_object;
756int help_echo_pos;
757
758/* Temporary variable for XTread_socket. */
759
760Lisp_Object previous_help_echo_string;
761
762
5f5c8ee5
GM
763\f
764/* Function prototypes. */
765
5a08cbaf 766static void setup_for_ellipsis P_ ((struct it *));
43c09969 767static void mark_window_display_accurate_1 P_ ((struct window *, int));
74bd6d65
GM
768static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
769static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
cafafe0b 770static int cursor_row_p P_ ((struct window *, struct glyph_row *));
715e84c9 771static int redisplay_mode_lines P_ ((Lisp_Object, int));
2e621225 772static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
c0a53abb
PJ
773
774#if 0
2e621225 775static int invisible_text_between_p P_ ((struct it *, int, int));
c0a53abb
PJ
776#endif
777
2e621225
GM
778static int next_element_from_ellipsis P_ ((struct it *));
779static void pint2str P_ ((char *, int, int));
525b8b09 780static void pint2hrstr P_ ((char *, int, int));
2e621225
GM
781static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
782 struct text_pos));
783static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
784static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
785static void store_frame_title_char P_ ((char));
50f80c2f 786static int store_frame_title P_ ((const unsigned char *, int, int));
2e621225
GM
787static void x_consider_frame_title P_ ((Lisp_Object));
788static void handle_stop P_ ((struct it *));
789static int tool_bar_lines_needed P_ ((struct frame *));
06568bbf 790static int single_display_prop_intangible_p P_ ((Lisp_Object));
5bcfeb49 791static void ensure_echo_area_buffers P_ ((void));
c6e89d6c
GM
792static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
793static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
23a96c77 794static int with_echo_area_buffer P_ ((struct window *, int,
23dd2d97
KR
795 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
796 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 797static void clear_garbaged_frames P_ ((void));
23dd2d97
KR
798static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
799static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
800static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 801static int display_echo_area P_ ((struct window *));
23dd2d97
KR
802static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
803static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
28514cd9 804static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
50f80c2f 805static int string_char_and_length P_ ((const unsigned char *, int, int *));
5f5c8ee5
GM
806static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
807 struct text_pos));
808static int compute_window_start_on_continuation_line P_ ((struct window *));
116d6f5c 809static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
5f5c8ee5
GM
810static void insert_left_trunc_glyphs P_ ((struct it *));
811static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
812static void extend_face_to_end_of_line P_ ((struct it *));
80c6cb1f 813static int append_space P_ ((struct it *, int));
cb617e7c 814static int make_cursor_line_fully_visible P_ ((struct window *));
03b0a4b4 815static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
47589c8c 816static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
5f5c8ee5
GM
817static int trailing_whitespace_p P_ ((int));
818static int message_log_check_duplicate P_ ((int, int, int, int));
5f5c8ee5
GM
819static void push_it P_ ((struct it *));
820static void pop_it P_ ((struct it *));
821static void sync_frame_with_window_matrix_rows P_ ((struct window *));
dd429b03 822static void select_frame_for_redisplay P_ ((Lisp_Object));
5f5c8ee5 823static void redisplay_internal P_ ((int));
c6e89d6c 824static int echo_area_display P_ ((int));
5f5c8ee5
GM
825static void redisplay_windows P_ ((Lisp_Object));
826static void redisplay_window P_ ((Lisp_Object, int));
82a7ab23
RS
827static Lisp_Object redisplay_window_error ();
828static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
829static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
5f5c8ee5
GM
830static void update_menu_bar P_ ((struct frame *, int));
831static int try_window_reusing_current_matrix P_ ((struct window *));
832static int try_window_id P_ ((struct window *));
833static int display_line P_ ((struct it *));
715e84c9 834static int display_mode_lines P_ ((struct window *));
04612a64 835static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
c53a1624 836static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
fec8f23e 837static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
72f62cb5 838static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
5f5c8ee5
GM
839static void display_menu_bar P_ ((struct window *));
840static int display_count_lines P_ ((int, int, int, int, int *));
841static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
842 int, int, struct it *, int, int, int, int));
843static void compute_line_metrics P_ ((struct it *));
844static void run_redisplay_end_trigger_hook P_ ((struct it *));
5a08cbaf 845static int get_overlay_strings P_ ((struct it *, int));
5f5c8ee5 846static void next_overlay_string P_ ((struct it *));
5f5c8ee5
GM
847static void reseat P_ ((struct it *, struct text_pos, int));
848static void reseat_1 P_ ((struct it *, struct text_pos, int));
849static void back_to_previous_visible_line_start P_ ((struct it *));
850static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 851static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
852static int next_element_from_display_vector P_ ((struct it *));
853static int next_element_from_string P_ ((struct it *));
854static int next_element_from_c_string P_ ((struct it *));
855static int next_element_from_buffer P_ ((struct it *));
260a86a0 856static int next_element_from_composition P_ ((struct it *));
5f5c8ee5
GM
857static int next_element_from_image P_ ((struct it *));
858static int next_element_from_stretch P_ ((struct it *));
5a08cbaf 859static void load_overlay_strings P_ ((struct it *, int));
47d57b22
GM
860static int init_from_display_pos P_ ((struct it *, struct window *,
861 struct display_pos *));
5f5c8ee5
GM
862static void reseat_to_string P_ ((struct it *, unsigned char *,
863 Lisp_Object, int, int, int, int));
5f5c8ee5
GM
864static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
865 int, int, int));
866void move_it_vertically_backward P_ ((struct it *, int));
867static void init_to_row_start P_ ((struct it *, struct window *,
868 struct glyph_row *));
47d57b22
GM
869static int init_to_row_end P_ ((struct it *, struct window *,
870 struct glyph_row *));
5f5c8ee5 871static void back_to_previous_line_start P_ ((struct it *));
cafafe0b 872static int forward_to_next_line_start P_ ((struct it *, int *));
5f5c8ee5
GM
873static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
874 Lisp_Object, int));
875static struct text_pos string_pos P_ ((int, Lisp_Object));
876static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
877static int number_of_chars P_ ((unsigned char *, int));
878static void compute_stop_pos P_ ((struct it *));
879static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
880 Lisp_Object));
881static int face_before_or_after_it_pos P_ ((struct it *, int));
882static int next_overlay_change P_ ((int));
883static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
a61b7058
GM
884 Lisp_Object, struct text_pos *,
885 int));
06a12811 886static int underlying_face_id P_ ((struct it *));
4bde0ebb
GM
887static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
888 struct window *));
5f5c8ee5
GM
889
890#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
891#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 892
5f5c8ee5 893#ifdef HAVE_WINDOW_SYSTEM
12adba34 894
e037b9ec
GM
895static void update_tool_bar P_ ((struct frame *, int));
896static void build_desired_tool_bar_string P_ ((struct frame *f));
897static int redisplay_tool_bar P_ ((struct frame *));
898static void display_tool_bar_line P_ ((struct it *));
fa3c6b4d
KS
899static void notice_overwritten_cursor P_ ((struct window *,
900 enum glyph_row_area,
901 int, int, int, int));
902
903
12adba34 904
5f5c8ee5 905#endif /* HAVE_WINDOW_SYSTEM */
12adba34 906
5f5c8ee5
GM
907\f
908/***********************************************************************
909 Window display dimensions
910 ***********************************************************************/
12adba34 911
40a301b3
RS
912/* Return the bottom boundary y-position for text lines in window W.
913 This is the first y position at which a line cannot start.
914 It is relative to the top of the window.
915
916 This is the height of W minus the height of a mode line, if any. */
5f5c8ee5
GM
917
918INLINE int
919window_text_bottom_y (w)
920 struct window *w;
921{
da8b7f4f 922 int height = WINDOW_TOTAL_HEIGHT (w);
1a578e9b 923
5f5c8ee5
GM
924 if (WINDOW_WANTS_MODELINE_P (w))
925 height -= CURRENT_MODE_LINE_HEIGHT (w);
926 return height;
f88eb0b6
KH
927}
928
5f5c8ee5 929/* Return the pixel width of display area AREA of window W. AREA < 0
b46952ae 930 means return the total width of W, not including fringes to
5f5c8ee5 931 the left and right of the window. */
ff6c30e5 932
5f5c8ee5
GM
933INLINE int
934window_box_width (w, area)
935 struct window *w;
936 int area;
937{
da8b7f4f
KS
938 int cols = XFASTINT (w->total_cols);
939 int pixels = 0;
2311178e 940
5f5c8ee5 941 if (!w->pseudo_window_p)
ff6c30e5 942 {
da8b7f4f 943 cols -= WINDOW_SCROLL_BAR_COLS (w);
2311178e 944
5f5c8ee5
GM
945 if (area == TEXT_AREA)
946 {
da8b7f4f
KS
947 if (INTEGERP (w->left_margin_cols))
948 cols -= XFASTINT (w->left_margin_cols);
949 if (INTEGERP (w->right_margin_cols))
950 cols -= XFASTINT (w->right_margin_cols);
951 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
5f5c8ee5
GM
952 }
953 else if (area == LEFT_MARGIN_AREA)
da8b7f4f
KS
954 {
955 cols = (INTEGERP (w->left_margin_cols)
956 ? XFASTINT (w->left_margin_cols) : 0);
957 pixels = 0;
958 }
5f5c8ee5 959 else if (area == RIGHT_MARGIN_AREA)
da8b7f4f
KS
960 {
961 cols = (INTEGERP (w->right_margin_cols)
962 ? XFASTINT (w->right_margin_cols) : 0);
963 pixels = 0;
964 }
ff6c30e5 965 }
5f5c8ee5 966
da8b7f4f 967 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
ff6c30e5 968}
1adc55de 969
1adc55de 970
5f5c8ee5 971/* Return the pixel height of the display area of window W, not
4b41cebb 972 including mode lines of W, if any. */
f88eb0b6 973
5f5c8ee5
GM
974INLINE int
975window_box_height (w)
976 struct window *w;
f88eb0b6 977{
5f5c8ee5 978 struct frame *f = XFRAME (w->frame);
da8b7f4f 979 int height = WINDOW_TOTAL_HEIGHT (w);
7ecd4937
GM
980
981 xassert (height >= 0);
2311178e 982
d9c9e99c
MB
983 /* Note: the code below that determines the mode-line/header-line
984 height is essentially the same as that contained in the macro
985 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
986 the appropriate glyph row has its `mode_line_p' flag set,
987 and if it doesn't, uses estimate_mode_line_height instead. */
988
5f5c8ee5 989 if (WINDOW_WANTS_MODELINE_P (w))
97dff879
MB
990 {
991 struct glyph_row *ml_row
992 = (w->current_matrix && w->current_matrix->rows
993 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
994 : 0);
995 if (ml_row && ml_row->mode_line_p)
996 height -= ml_row->height;
997 else
96d2320f 998 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
97dff879 999 }
5f5c8ee5 1000
045dee35 1001 if (WINDOW_WANTS_HEADER_LINE_P (w))
97dff879
MB
1002 {
1003 struct glyph_row *hl_row
1004 = (w->current_matrix && w->current_matrix->rows
1005 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1006 : 0);
1007 if (hl_row && hl_row->mode_line_p)
1008 height -= hl_row->height;
1009 else
1010 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1011 }
5f5c8ee5 1012
7c75be36
PJ
1013 /* With a very small font and a mode-line that's taller than
1014 default, we might end up with a negative height. */
1015 return max (0, height);
5992c4f7
KH
1016}
1017
da8b7f4f
KS
1018/* Return the window-relative coordinate of the left edge of display
1019 area AREA of window W. AREA < 0 means return the left edge of the
1020 whole window, to the right of the left fringe of W. */
1021
1022INLINE int
1023window_box_left_offset (w, area)
1024 struct window *w;
1025 int area;
1026{
1027 int x;
1028
1029 if (w->pseudo_window_p)
1030 return 0;
1031
1032 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1033
1034 if (area == TEXT_AREA)
1035 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1036 + window_box_width (w, LEFT_MARGIN_AREA));
1037 else if (area == RIGHT_MARGIN_AREA)
1038 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1039 + window_box_width (w, LEFT_MARGIN_AREA)
1040 + window_box_width (w, TEXT_AREA)
1041 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1042 ? 0
1043 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1044 else if (area == LEFT_MARGIN_AREA
1045 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1046 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1047
1048 return x;
1049}
1050
1051
1052/* Return the window-relative coordinate of the right edge of display
1053 area AREA of window W. AREA < 0 means return the left edge of the
1054 whole window, to the left of the right fringe of W. */
1055
1056INLINE int
1057window_box_right_offset (w, area)
1058 struct window *w;
1059 int area;
1060{
1061 return window_box_left_offset (w, area) + window_box_width (w, area);
1062}
5992c4f7 1063
5f5c8ee5
GM
1064/* Return the frame-relative coordinate of the left edge of display
1065 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 1066 whole window, to the right of the left fringe of W. */
5992c4f7 1067
5f5c8ee5
GM
1068INLINE int
1069window_box_left (w, area)
1070 struct window *w;
1071 int area;
90adcf20 1072{
5f5c8ee5 1073 struct frame *f = XFRAME (w->frame);
da8b7f4f 1074 int x;
a3788d53 1075
da8b7f4f
KS
1076 if (w->pseudo_window_p)
1077 return FRAME_INTERNAL_BORDER_WIDTH (f);
2311178e 1078
da8b7f4f
KS
1079 x = (WINDOW_LEFT_EDGE_X (w)
1080 + window_box_left_offset (w, area));
73af359d 1081
5f5c8ee5 1082 return x;
2311178e 1083}
90adcf20 1084
b6436d4e 1085
5f5c8ee5
GM
1086/* Return the frame-relative coordinate of the right edge of display
1087 area AREA of window W. AREA < 0 means return the left edge of the
b46952ae 1088 whole window, to the left of the right fringe of W. */
ded34426 1089
5f5c8ee5
GM
1090INLINE int
1091window_box_right (w, area)
1092 struct window *w;
1093 int area;
1094{
1095 return window_box_left (w, area) + window_box_width (w, area);
2311178e
TTN
1096}
1097
5f5c8ee5
GM
1098/* Get the bounding box of the display area AREA of window W, without
1099 mode lines, in frame-relative coordinates. AREA < 0 means the
b46952ae 1100 whole window, not including the left and right fringes of
5f5c8ee5
GM
1101 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1102 coordinates of the upper-left corner of the box. Return in
1103 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1104
1105INLINE void
1106window_box (w, area, box_x, box_y, box_width, box_height)
1107 struct window *w;
1108 int area;
1109 int *box_x, *box_y, *box_width, *box_height;
1110{
da8b7f4f
KS
1111 if (box_width)
1112 *box_width = window_box_width (w, area);
1113 if (box_height)
1114 *box_height = window_box_height (w);
1115 if (box_x)
1116 *box_x = window_box_left (w, area);
1117 if (box_y)
1118 {
1119 *box_y = WINDOW_TOP_EDGE_Y (w);
1120 if (WINDOW_WANTS_HEADER_LINE_P (w))
1121 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1122 }
ded34426 1123}
1adc55de 1124
1adc55de 1125
5f5c8ee5 1126/* Get the bounding box of the display area AREA of window W, without
b46952ae
KS
1127 mode lines. AREA < 0 means the whole window, not including the
1128 left and right fringe of the window. Return in *TOP_LEFT_X
5f5c8ee5
GM
1129 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1130 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1131 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1132 box. */
ded34426 1133
5f5c8ee5
GM
1134INLINE void
1135window_box_edges (w, area, top_left_x, top_left_y,
1136 bottom_right_x, bottom_right_y)
1137 struct window *w;
1138 int area;
1139 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 1140{
5f5c8ee5
GM
1141 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1142 bottom_right_y);
1143 *bottom_right_x += *top_left_x;
1144 *bottom_right_y += *top_left_y;
48ae5f0a
KH
1145}
1146
5f5c8ee5
GM
1147
1148\f
1149/***********************************************************************
1150 Utilities
1151 ***********************************************************************/
1152
8b6ea97f
GM
1153/* Return the bottom y-position of the line the iterator IT is in.
1154 This can modify IT's settings. */
1155
1156int
1157line_bottom_y (it)
1158 struct it *it;
1159{
1160 int line_height = it->max_ascent + it->max_descent;
1161 int line_top_y = it->current_y;
2311178e 1162
8b6ea97f
GM
1163 if (line_height == 0)
1164 {
1165 if (last_height)
1166 line_height = last_height;
1167 else if (IT_CHARPOS (*it) < ZV)
1168 {
1169 move_it_by_lines (it, 1, 1);
1170 line_height = (it->max_ascent || it->max_descent
1171 ? it->max_ascent + it->max_descent
1172 : last_height);
1173 }
1174 else
1175 {
1176 struct glyph_row *row = it->glyph_row;
2311178e 1177
8b6ea97f
GM
1178 /* Use the default character height. */
1179 it->glyph_row = NULL;
1180 it->what = IT_CHARACTER;
1181 it->c = ' ';
1182 it->len = 1;
1183 PRODUCE_GLYPHS (it);
1184 line_height = it->ascent + it->descent;
1185 it->glyph_row = row;
1186 }
1187 }
1188
1189 return line_top_y + line_height;
1190}
1191
1192
0db95684
GM
1193/* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1194 1 if POS is visible and the line containing POS is fully visible.
1195 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1196 and header-lines heights. */
3a641a69
GM
1197
1198int
04612a64 1199pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
3a641a69 1200 struct window *w;
04612a64 1201 int charpos, *fully, exact_mode_line_heights_p;
3a641a69
GM
1202{
1203 struct it it;
1204 struct text_pos top;
fcab1954
GM
1205 int visible_p;
1206 struct buffer *old_buffer = NULL;
1207
1208 if (XBUFFER (w->buffer) != current_buffer)
1209 {
1210 old_buffer = current_buffer;
1211 set_buffer_internal_1 (XBUFFER (w->buffer));
1212 }
3a641a69
GM
1213
1214 *fully = visible_p = 0;
1215 SET_TEXT_POS_FROM_MARKER (top, w->start);
2311178e 1216
04612a64
GM
1217 /* Compute exact mode line heights, if requested. */
1218 if (exact_mode_line_heights_p)
1219 {
1220 if (WINDOW_WANTS_MODELINE_P (w))
1221 current_mode_line_height
96d2320f 1222 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
04612a64 1223 current_buffer->mode_line_format);
2311178e 1224
04612a64
GM
1225 if (WINDOW_WANTS_HEADER_LINE_P (w))
1226 current_header_line_height
1227 = display_mode_line (w, HEADER_LINE_FACE_ID,
1228 current_buffer->header_line_format);
1229 }
3a641a69 1230
04612a64 1231 start_display (&it, w, top);
3a641a69
GM
1232 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1233 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
a13be207
GM
1234
1235 /* Note that we may overshoot because of invisible text. */
1236 if (IT_CHARPOS (it) >= charpos)
3a641a69 1237 {
8b6ea97f
GM
1238 int top_y = it.current_y;
1239 int bottom_y = line_bottom_y (&it);
da8b7f4f 1240 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
2311178e 1241
8b6ea97f
GM
1242 if (top_y < window_top_y)
1243 visible_p = bottom_y > window_top_y;
1244 else if (top_y < it.last_visible_y)
fcab1954
GM
1245 {
1246 visible_p = 1;
8b6ea97f 1247 *fully = bottom_y <= it.last_visible_y;
fcab1954 1248 }
3a641a69
GM
1249 }
1250 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1251 {
1252 move_it_by_lines (&it, 1, 0);
1253 if (charpos < IT_CHARPOS (it))
1254 {
1255 visible_p = 1;
1256 *fully = 0;
1257 }
1258 }
fcab1954
GM
1259
1260 if (old_buffer)
1261 set_buffer_internal_1 (old_buffer);
04612a64
GM
1262
1263 current_header_line_height = current_mode_line_height = -1;
3a641a69
GM
1264 return visible_p;
1265}
1266
1267
4fdb80f2
GM
1268/* Return the next character from STR which is MAXLEN bytes long.
1269 Return in *LEN the length of the character. This is like
1270 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
9ab8560d 1271 we find one, we return a `?', but with the length of the invalid
4fdb80f2
GM
1272 character. */
1273
1274static INLINE int
7a5b8a93 1275string_char_and_length (str, maxlen, len)
50f80c2f 1276 const unsigned char *str;
7a5b8a93 1277 int maxlen, *len;
4fdb80f2
GM
1278{
1279 int c;
1280
1281 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1282 if (!CHAR_VALID_P (c, 1))
1283 /* We may not change the length here because other places in Emacs
9ab8560d 1284 don't use this function, i.e. they silently accept invalid
4fdb80f2
GM
1285 characters. */
1286 c = '?';
1287
1288 return c;
1289}
1290
1291
1292
5f5c8ee5
GM
1293/* Given a position POS containing a valid character and byte position
1294 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1295
1296static struct text_pos
1297string_pos_nchars_ahead (pos, string, nchars)
1298 struct text_pos pos;
1299 Lisp_Object string;
1300 int nchars;
0b1005ef 1301{
5f5c8ee5
GM
1302 xassert (STRINGP (string) && nchars >= 0);
1303
1304 if (STRING_MULTIBYTE (string))
1305 {
2051c264 1306 int rest = SBYTES (string) - BYTEPOS (pos);
50f80c2f 1307 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
5f5c8ee5
GM
1308 int len;
1309
1310 while (nchars--)
1311 {
4fdb80f2 1312 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
1313 p += len, rest -= len;
1314 xassert (rest >= 0);
1315 CHARPOS (pos) += 1;
1316 BYTEPOS (pos) += len;
1317 }
1318 }
1319 else
1320 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1321
1322 return pos;
0a9dc68b
RS
1323}
1324
0a9dc68b 1325
5f5c8ee5
GM
1326/* Value is the text position, i.e. character and byte position,
1327 for character position CHARPOS in STRING. */
1328
1329static INLINE struct text_pos
1330string_pos (charpos, string)
1331 int charpos;
0a9dc68b 1332 Lisp_Object string;
0a9dc68b 1333{
5f5c8ee5
GM
1334 struct text_pos pos;
1335 xassert (STRINGP (string));
1336 xassert (charpos >= 0);
1337 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1338 return pos;
1339}
1340
1341
1342/* Value is a text position, i.e. character and byte position, for
1343 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1344 means recognize multibyte characters. */
1345
1346static struct text_pos
1347c_string_pos (charpos, s, multibyte_p)
1348 int charpos;
1349 unsigned char *s;
1350 int multibyte_p;
1351{
1352 struct text_pos pos;
1353
1354 xassert (s != NULL);
1355 xassert (charpos >= 0);
1356
1357 if (multibyte_p)
0a9dc68b 1358 {
5f5c8ee5
GM
1359 int rest = strlen (s), len;
1360
1361 SET_TEXT_POS (pos, 0, 0);
1362 while (charpos--)
0a9dc68b 1363 {
4fdb80f2 1364 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
1365 s += len, rest -= len;
1366 xassert (rest >= 0);
1367 CHARPOS (pos) += 1;
1368 BYTEPOS (pos) += len;
0a9dc68b
RS
1369 }
1370 }
5f5c8ee5
GM
1371 else
1372 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 1373
5f5c8ee5
GM
1374 return pos;
1375}
0a9dc68b 1376
0a9dc68b 1377
5f5c8ee5
GM
1378/* Value is the number of characters in C string S. MULTIBYTE_P
1379 non-zero means recognize multibyte characters. */
0a9dc68b 1380
5f5c8ee5
GM
1381static int
1382number_of_chars (s, multibyte_p)
1383 unsigned char *s;
1384 int multibyte_p;
1385{
1386 int nchars;
2311178e 1387
5f5c8ee5
GM
1388 if (multibyte_p)
1389 {
1390 int rest = strlen (s), len;
1391 unsigned char *p = (unsigned char *) s;
0a9dc68b 1392
5f5c8ee5
GM
1393 for (nchars = 0; rest > 0; ++nchars)
1394 {
4fdb80f2 1395 string_char_and_length (p, rest, &len);
5f5c8ee5 1396 rest -= len, p += len;
0a9dc68b
RS
1397 }
1398 }
5f5c8ee5
GM
1399 else
1400 nchars = strlen (s);
1401
1402 return nchars;
0b1005ef
KH
1403}
1404
2311178e 1405
5f5c8ee5
GM
1406/* Compute byte position NEWPOS->bytepos corresponding to
1407 NEWPOS->charpos. POS is a known position in string STRING.
1408 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1409
5f5c8ee5
GM
1410static void
1411compute_string_pos (newpos, pos, string)
1412 struct text_pos *newpos, pos;
1413 Lisp_Object string;
76412d64 1414{
5f5c8ee5
GM
1415 xassert (STRINGP (string));
1416 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
2311178e 1417
5f5c8ee5 1418 if (STRING_MULTIBYTE (string))
6fc556fd
KR
1419 *newpos = string_pos_nchars_ahead (pos, string,
1420 CHARPOS (*newpos) - CHARPOS (pos));
5f5c8ee5
GM
1421 else
1422 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1423}
1424
fa3c6b4d
KS
1425/* EXPORT:
1426 Return an estimation of the pixel height of mode or top lines on
1427 frame F. FACE_ID specifies what line's height to estimate. */
1428
1429int
1430estimate_mode_line_height (f, face_id)
1431 struct frame *f;
1432 enum face_id face_id;
1433{
1434#ifdef HAVE_WINDOW_SYSTEM
1435 if (FRAME_WINDOW_P (f))
1436 {
1437 int height = FONT_HEIGHT (FRAME_FONT (f));
1438
1439 /* This function is called so early when Emacs starts that the face
1440 cache and mode line face are not yet initialized. */
1441 if (FRAME_FACE_CACHE (f))
1442 {
1443 struct face *face = FACE_FROM_ID (f, face_id);
1444 if (face)
1445 {
1446 if (face->font)
1447 height = FONT_HEIGHT (face->font);
1448 if (face->box_line_width > 0)
1449 height += 2 * face->box_line_width;
1450 }
1451 }
1452
1453 return height;
1454 }
1455#endif
1456
1457 return 1;
1458}
1459
e080d3eb
KS
1460/* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1461 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1462 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1463 not force the value into range. */
1464
1465void
1466pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1467 FRAME_PTR f;
1468 register int pix_x, pix_y;
1469 int *x, *y;
1470 NativeRectangle *bounds;
1471 int noclip;
1472{
1473
1474#ifdef HAVE_WINDOW_SYSTEM
1475 if (FRAME_WINDOW_P (f))
1476 {
da8b7f4f 1477 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
e080d3eb
KS
1478 even for negative values. */
1479 if (pix_x < 0)
da8b7f4f 1480 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
e080d3eb 1481 if (pix_y < 0)
da8b7f4f 1482 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
e080d3eb 1483
da8b7f4f
KS
1484 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1485 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
e080d3eb
KS
1486
1487 if (bounds)
1488 STORE_NATIVE_RECT (*bounds,
da8b7f4f
KS
1489 FRAME_COL_TO_PIXEL_X (f, pix_x),
1490 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1491 FRAME_COLUMN_WIDTH (f) - 1,
1492 FRAME_LINE_HEIGHT (f) - 1);
e080d3eb
KS
1493
1494 if (!noclip)
1495 {
1496 if (pix_x < 0)
1497 pix_x = 0;
da8b7f4f
KS
1498 else if (pix_x > FRAME_TOTAL_COLS (f))
1499 pix_x = FRAME_TOTAL_COLS (f);
e080d3eb
KS
1500
1501 if (pix_y < 0)
1502 pix_y = 0;
da8b7f4f
KS
1503 else if (pix_y > FRAME_LINES (f))
1504 pix_y = FRAME_LINES (f);
e080d3eb
KS
1505 }
1506 }
1507#endif
1508
1509 *x = pix_x;
1510 *y = pix_y;
1511}
1512
1513
1514/* Given HPOS/VPOS in the current matrix of W, return corresponding
1515 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1516 can't tell the positions because W's display is not up to date,
1517 return 0. */
1518
1519int
1520glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1521 struct window *w;
1522 int hpos, vpos;
1523 int *frame_x, *frame_y;
1524{
1525#ifdef HAVE_WINDOW_SYSTEM
1526 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1527 {
1528 int success_p;
1529
1530 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1531 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1532
1533 if (display_completed)
1534 {
1535 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1536 struct glyph *glyph = row->glyphs[TEXT_AREA];
1537 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1538
1539 hpos = row->x;
1540 vpos = row->y;
1541 while (glyph < end)
1542 {
1543 hpos += glyph->pixel_width;
1544 ++glyph;
1545 }
1546
f65536fa
KS
1547 /* If first glyph is partially visible, its first visible position is still 0. */
1548 if (hpos < 0)
1549 hpos = 0;
1550
e080d3eb
KS
1551 success_p = 1;
1552 }
1553 else
1554 {
1555 hpos = vpos = 0;
1556 success_p = 0;
1557 }
1558
1559 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1560 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1561 return success_p;
1562 }
1563#endif
1564
1565 *frame_x = hpos;
1566 *frame_y = vpos;
1567 return 1;
1568}
1569
1570
fa3c6b4d
KS
1571#ifdef HAVE_WINDOW_SYSTEM
1572
1573/* Find the glyph under window-relative coordinates X/Y in window W.
1574 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1575 strings. Return in *HPOS and *VPOS the row and column number of
1576 the glyph found. Return in *AREA the glyph area containing X.
1577 Value is a pointer to the glyph found or null if X/Y is not on
1578 text, or we can't tell because W's current matrix is not up to
1579 date. */
1580
1581static struct glyph *
1582x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
1583 struct window *w;
1584 int x, y;
1585 int *hpos, *vpos, *area;
1586 int buffer_only_p;
1587{
1588 struct glyph *glyph, *end;
1589 struct glyph_row *row = NULL;
da8b7f4f 1590 int x0, i;
fa3c6b4d
KS
1591
1592 /* Find row containing Y. Give up if some row is not enabled. */
1593 for (i = 0; i < w->current_matrix->nrows; ++i)
1594 {
1595 row = MATRIX_ROW (w->current_matrix, i);
1596 if (!row->enabled_p)
1597 return NULL;
1598 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1599 break;
1600 }
1601
1602 *vpos = i;
1603 *hpos = 0;
1604
1605 /* Give up if Y is not in the window. */
1606 if (i == w->current_matrix->nrows)
1607 return NULL;
1608
1609 /* Get the glyph area containing X. */
1610 if (w->pseudo_window_p)
1611 {
1612 *area = TEXT_AREA;
1613 x0 = 0;
1614 }
1615 else
1616 {
da8b7f4f 1617 if (x < window_box_left_offset (w, TEXT_AREA))
fa3c6b4d
KS
1618 {
1619 *area = LEFT_MARGIN_AREA;
da8b7f4f 1620 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
fa3c6b4d 1621 }
da8b7f4f 1622 else if (x < window_box_right_offset (w, TEXT_AREA))
fa3c6b4d
KS
1623 {
1624 *area = TEXT_AREA;
f65536fa 1625 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
fa3c6b4d
KS
1626 }
1627 else
1628 {
1629 *area = RIGHT_MARGIN_AREA;
da8b7f4f 1630 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
fa3c6b4d
KS
1631 }
1632 }
1633
1634 /* Find glyph containing X. */
1635 glyph = row->glyphs[*area];
1636 end = glyph + row->used[*area];
1637 while (glyph < end)
1638 {
1639 if (x < x0 + glyph->pixel_width)
1640 {
1641 if (w->pseudo_window_p)
1642 break;
1643 else if (!buffer_only_p || BUFFERP (glyph->object))
1644 break;
1645 }
1646
1647 x0 += glyph->pixel_width;
1648 ++glyph;
1649 }
1650
1651 if (glyph == end)
1652 return NULL;
1653
1654 *hpos = glyph - row->glyphs[*area];
1655 return glyph;
1656}
1657
1658
1659/* EXPORT:
1660 Convert frame-relative x/y to coordinates relative to window W.
1661 Takes pseudo-windows into account. */
1662
1663void
1664frame_to_window_pixel_xy (w, x, y)
1665 struct window *w;
1666 int *x, *y;
1667{
1668 if (w->pseudo_window_p)
1669 {
1670 /* A pseudo-window is always full-width, and starts at the
1671 left edge of the frame, plus a frame border. */
1672 struct frame *f = XFRAME (w->frame);
da8b7f4f 1673 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
fa3c6b4d
KS
1674 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1675 }
1676 else
1677 {
da8b7f4f 1678 *x -= WINDOW_LEFT_EDGE_X (w);
fa3c6b4d
KS
1679 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1680 }
1681}
1682
1683/* EXPORT:
1684 Return in *R the clipping rectangle for glyph string S. */
1685
1686void
1687get_glyph_string_clip_rect (s, nr)
1688 struct glyph_string *s;
1689 NativeRectangle *nr;
1690{
1691 XRectangle r;
1692
1693 if (s->row->full_width_p)
1694 {
da8b7f4f
KS
1695 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1696 r.x = WINDOW_LEFT_EDGE_X (s->w);
1697 r.width = WINDOW_TOTAL_WIDTH (s->w);
fa3c6b4d
KS
1698
1699 /* Unless displaying a mode or menu bar line, which are always
1700 fully visible, clip to the visible part of the row. */
1701 if (s->w->pseudo_window_p)
1702 r.height = s->row->visible_height;
1703 else
1704 r.height = s->height;
1705 }
1706 else
1707 {
1708 /* This is a text line that may be partially visible. */
da8b7f4f 1709 r.x = window_box_left (s->w, s->area);
fa3c6b4d
KS
1710 r.width = window_box_width (s->w, s->area);
1711 r.height = s->row->visible_height;
1712 }
1713
1714 /* If S draws overlapping rows, it's sufficient to use the top and
1715 bottom of the window for clipping because this glyph string
1716 intentionally draws over other lines. */
1717 if (s->for_overlaps_p)
1718 {
da8b7f4f 1719 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
fa3c6b4d
KS
1720 r.height = window_text_bottom_y (s->w) - r.y;
1721 }
1722 else
1723 {
1724 /* Don't use S->y for clipping because it doesn't take partially
1725 visible lines into account. For example, it can be negative for
1726 partially visible lines at the top of a window. */
1727 if (!s->row->full_width_p
1728 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
da8b7f4f 1729 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
fa3c6b4d
KS
1730 else
1731 r.y = max (0, s->row->y);
1732
1733 /* If drawing a tool-bar window, draw it over the internal border
1734 at the top of the window. */
1735 if (s->w == XWINDOW (s->f->tool_bar_window))
b4ebbb12 1736 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
fa3c6b4d
KS
1737 }
1738
1739 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1740
1741#ifdef HAVE_NTGUI
1742 /* ++KFS: From W32 port, but it looks ok for all platforms to me. */
1743 /* If drawing the cursor, don't let glyph draw outside its
1744 advertised boundaries. Cleartype does this under some circumstances. */
1745 if (s->hl == DRAW_CURSOR)
1746 {
1747 if (s->x > r.x)
1748 {
1749 r.width -= s->x - r.x;
1750 r.x = s->x;
1751 }
1752 r.width = min (r.width, s->first_glyph->pixel_width);
1753 }
1754#endif
1755
1756#ifdef CONVERT_FROM_XRECT
1757 CONVERT_FROM_XRECT (r, *nr);
1758#else
1759 *nr = r;
fd4c9408 1760#endif
fa3c6b4d
KS
1761}
1762
1763#endif /* HAVE_WINDOW_SYSTEM */
9c74a0dd 1764
5f5c8ee5
GM
1765\f
1766/***********************************************************************
1767 Lisp form evaluation
1768 ***********************************************************************/
1769
116d6f5c 1770/* Error handler for safe_eval and safe_call. */
5f5c8ee5
GM
1771
1772static Lisp_Object
116d6f5c 1773safe_eval_handler (arg)
5f5c8ee5
GM
1774 Lisp_Object arg;
1775{
0dbf9fd2 1776 add_to_log ("Error during redisplay: %s", arg, Qnil);
5f5c8ee5
GM
1777 return Qnil;
1778}
1779
1780
1781/* Evaluate SEXPR and return the result, or nil if something went
2913a9c0 1782 wrong. Prevent redisplay during the evaluation. */
5f5c8ee5 1783
71e5b1b8 1784Lisp_Object
116d6f5c 1785safe_eval (sexpr)
5f5c8ee5
GM
1786 Lisp_Object sexpr;
1787{
5f5c8ee5 1788 Lisp_Object val;
2311178e 1789
30a3f61c
GM
1790 if (inhibit_eval_during_redisplay)
1791 val = Qnil;
1792 else
1793 {
331379bf 1794 int count = SPECPDL_INDEX ();
30a3f61c 1795 struct gcpro gcpro1;
0d8b31c0 1796
30a3f61c
GM
1797 GCPRO1 (sexpr);
1798 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
1799 /* Use Qt to ensure debugger does not run,
1800 so there is no possibility of wanting to redisplay. */
1801 val = internal_condition_case_1 (Feval, sexpr, Qt,
30a3f61c
GM
1802 safe_eval_handler);
1803 UNGCPRO;
1804 val = unbind_to (count, val);
1805 }
2311178e 1806
30a3f61c 1807 return val;
0d8b31c0
GM
1808}
1809
1810
1811/* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2913a9c0
GM
1812 Return the result, or nil if something went wrong. Prevent
1813 redisplay during the evaluation. */
0d8b31c0
GM
1814
1815Lisp_Object
116d6f5c 1816safe_call (nargs, args)
0d8b31c0
GM
1817 int nargs;
1818 Lisp_Object *args;
1819{
0d8b31c0 1820 Lisp_Object val;
2311178e 1821
30a3f61c
GM
1822 if (inhibit_eval_during_redisplay)
1823 val = Qnil;
1824 else
1825 {
331379bf 1826 int count = SPECPDL_INDEX ();
30a3f61c 1827 struct gcpro gcpro1;
0d8b31c0 1828
30a3f61c
GM
1829 GCPRO1 (args[0]);
1830 gcpro1.nvars = nargs;
1831 specbind (Qinhibit_redisplay, Qt);
7033d6df
RS
1832 /* Use Qt to ensure debugger does not run,
1833 so there is no possibility of wanting to redisplay. */
1834 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
30a3f61c
GM
1835 safe_eval_handler);
1836 UNGCPRO;
1837 val = unbind_to (count, val);
1838 }
1839
1840 return val;
5f5c8ee5
GM
1841}
1842
1843
116d6f5c
GM
1844/* Call function FN with one argument ARG.
1845 Return the result, or nil if something went wrong. */
1846
1847Lisp_Object
1848safe_call1 (fn, arg)
1849 Lisp_Object fn, arg;
1850{
1851 Lisp_Object args[2];
1852 args[0] = fn;
1853 args[1] = arg;
1854 return safe_call (2, args);
1855}
1856
1857
5f5c8ee5
GM
1858\f
1859/***********************************************************************
1860 Debugging
1861 ***********************************************************************/
1862
1863#if 0
1864
1865/* Define CHECK_IT to perform sanity checks on iterators.
1866 This is for debugging. It is too slow to do unconditionally. */
1867
1868static void
1869check_it (it)
1870 struct it *it;
1871{
1872 if (it->method == next_element_from_string)
a2889657 1873 {
5f5c8ee5
GM
1874 xassert (STRINGP (it->string));
1875 xassert (IT_STRING_CHARPOS (*it) >= 0);
1876 }
1877 else if (it->method == next_element_from_buffer)
1878 {
1879 /* Check that character and byte positions agree. */
1880 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1881 }
73af359d 1882
5f5c8ee5
GM
1883 if (it->dpvec)
1884 xassert (it->current.dpvec_index >= 0);
1885 else
1886 xassert (it->current.dpvec_index < 0);
1887}
1f40cad2 1888
5f5c8ee5
GM
1889#define CHECK_IT(IT) check_it ((IT))
1890
1891#else /* not 0 */
1892
1893#define CHECK_IT(IT) (void) 0
1894
1895#endif /* not 0 */
1896
1897
1898#if GLYPH_DEBUG
1899
1900/* Check that the window end of window W is what we expect it
1901 to be---the last row in the current matrix displaying text. */
1902
1903static void
1904check_window_end (w)
1905 struct window *w;
1906{
1907 if (!MINI_WINDOW_P (w)
1908 && !NILP (w->window_end_valid))
1909 {
1910 struct glyph_row *row;
1911 xassert ((row = MATRIX_ROW (w->current_matrix,
1912 XFASTINT (w->window_end_vpos)),
1913 !row->enabled_p
1914 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1915 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1916 }
1917}
1918
1919#define CHECK_WINDOW_END(W) check_window_end ((W))
1920
1921#else /* not GLYPH_DEBUG */
1922
1923#define CHECK_WINDOW_END(W) (void) 0
1924
1925#endif /* not GLYPH_DEBUG */
1926
1927
1928\f
1929/***********************************************************************
1930 Iterator initialization
1931 ***********************************************************************/
1932
1933/* Initialize IT for displaying current_buffer in window W, starting
1934 at character position CHARPOS. CHARPOS < 0 means that no buffer
1935 position is specified which is useful when the iterator is assigned
1936 a position later. BYTEPOS is the byte position corresponding to
3ebf0ea9 1937 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
5f5c8ee5
GM
1938
1939 If ROW is not null, calls to produce_glyphs with IT as parameter
1940 will produce glyphs in that row.
1941
1942 BASE_FACE_ID is the id of a base face to use. It must be one of
96d2320f
KS
1943 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1944 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1945 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2311178e 1946
96d2320f
KS
1947 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1948 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1949 will be initialized to use the corresponding mode line glyph row of
1950 the desired matrix of W. */
5f5c8ee5
GM
1951
1952void
1953init_iterator (it, w, charpos, bytepos, row, base_face_id)
1954 struct it *it;
1955 struct window *w;
1956 int charpos, bytepos;
1957 struct glyph_row *row;
1958 enum face_id base_face_id;
1959{
1960 int highlight_region_p;
5f5c8ee5
GM
1961
1962 /* Some precondition checks. */
1963 xassert (w != NULL && it != NULL);
3b6b6db7
SM
1964 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1965 && charpos <= ZV));
5f5c8ee5
GM
1966
1967 /* If face attributes have been changed since the last redisplay,
1968 free realized faces now because they depend on face definitions
7d0393cf 1969 that might have changed. Don't free faces while there might be
1987b083 1970 desired matrices pending which reference these faces. */
26683087 1971 if (face_change_count && !inhibit_free_realized_faces)
5f5c8ee5
GM
1972 {
1973 face_change_count = 0;
1974 free_all_realized_faces (Qnil);
1975 }
1976
1977 /* Use one of the mode line rows of W's desired matrix if
1978 appropriate. */
1979 if (row == NULL)
1980 {
96d2320f
KS
1981 if (base_face_id == MODE_LINE_FACE_ID
1982 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
5f5c8ee5 1983 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
1984 else if (base_face_id == HEADER_LINE_FACE_ID)
1985 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5 1986 }
2311178e 1987
5f5c8ee5
GM
1988 /* Clear IT. */
1989 bzero (it, sizeof *it);
1990 it->current.overlay_string_index = -1;
1991 it->current.dpvec_index = -1;
5f5c8ee5
GM
1992 it->base_face_id = base_face_id;
1993
1994 /* The window in which we iterate over current_buffer: */
1995 XSETWINDOW (it->window, w);
1996 it->w = w;
1997 it->f = XFRAME (w->frame);
1998
d475bcb8
GM
1999 /* Extra space between lines (on window systems only). */
2000 if (base_face_id == DEFAULT_FACE_ID
2001 && FRAME_WINDOW_P (it->f))
2002 {
2003 if (NATNUMP (current_buffer->extra_line_spacing))
2004 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2005 else if (it->f->extra_line_spacing > 0)
2006 it->extra_line_spacing = it->f->extra_line_spacing;
2007 }
2008
5f5c8ee5 2009 /* If realized faces have been removed, e.g. because of face
62be9979 2010 attribute changes of named faces, recompute them. When running
9628b887 2011 in batch mode, the face cache of the initial frame is null. If
62be9979 2012 we happen to get called, make a dummy face cache. */
a24d4cb2 2013 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
62be9979 2014 init_frame_faces (it->f);
5f5c8ee5
GM
2015 if (FRAME_FACE_CACHE (it->f)->used == 0)
2016 recompute_basic_faces (it->f);
2017
5f5c8ee5
GM
2018 /* Current value of the `space-width', and 'height' properties. */
2019 it->space_width = Qnil;
2020 it->font_height = Qnil;
2311178e 2021
5f5c8ee5
GM
2022 /* Are control characters displayed as `^C'? */
2023 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2024
2025 /* -1 means everything between a CR and the following line end
2026 is invisible. >0 means lines indented more than this value are
2027 invisible. */
2028 it->selective = (INTEGERP (current_buffer->selective_display)
2029 ? XFASTINT (current_buffer->selective_display)
2311178e 2030 : (!NILP (current_buffer->selective_display)
5f5c8ee5
GM
2031 ? -1 : 0));
2032 it->selective_display_ellipsis_p
2033 = !NILP (current_buffer->selective_display_ellipses);
2034
2035 /* Display table to use. */
2036 it->dp = window_display_table (w);
2037
2038 /* Are multibyte characters enabled in current_buffer? */
2039 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2040
2041 /* Non-zero if we should highlight the region. */
2042 highlight_region_p
3ebf0ea9 2043 = (!NILP (Vtransient_mark_mode)
5f5c8ee5
GM
2044 && !NILP (current_buffer->mark_active)
2045 && XMARKER (current_buffer->mark)->buffer != 0);
2046
2047 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2048 start and end of a visible region in window IT->w. Set both to
2049 -1 to indicate no region. */
2050 if (highlight_region_p
2051 /* Maybe highlight only in selected window. */
2311178e 2052 && (/* Either show region everywhere. */
5f5c8ee5
GM
2053 highlight_nonselected_windows
2054 /* Or show region in the selected window. */
2055 || w == XWINDOW (selected_window)
2056 /* Or show the region if we are in the mini-buffer and W is
2057 the window the mini-buffer refers to. */
2058 || (MINI_WINDOW_P (XWINDOW (selected_window))
5705966b
KS
2059 && WINDOWP (minibuf_selected_window)
2060 && w == XWINDOW (minibuf_selected_window))))
5f5c8ee5
GM
2061 {
2062 int charpos = marker_position (current_buffer->mark);
2063 it->region_beg_charpos = min (PT, charpos);
2064 it->region_end_charpos = max (PT, charpos);
2065 }
2066 else
2067 it->region_beg_charpos = it->region_end_charpos = -1;
2068
2069 /* Get the position at which the redisplay_end_trigger hook should
2070 be run, if it is to be run at all. */
2071 if (MARKERP (w->redisplay_end_trigger)
2072 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2073 it->redisplay_end_trigger_charpos
2074 = marker_position (w->redisplay_end_trigger);
2075 else if (INTEGERP (w->redisplay_end_trigger))
2076 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2077
2078 /* Correct bogus values of tab_width. */
2079 it->tab_width = XINT (current_buffer->tab_width);
2080 if (it->tab_width <= 0 || it->tab_width > 1000)
2081 it->tab_width = 8;
2082
2083 /* Are lines in the display truncated? */
2084 it->truncate_lines_p
2085 = (base_face_id != DEFAULT_FACE_ID
2086 || XINT (it->w->hscroll)
2087 || (truncate_partial_width_windows
2088 && !WINDOW_FULL_WIDTH_P (it->w))
2089 || !NILP (current_buffer->truncate_lines));
2090
2091 /* Get dimensions of truncation and continuation glyphs. These are
b46952ae 2092 displayed as fringe bitmaps under X, so we don't need them for such
5f5c8ee5
GM
2093 frames. */
2094 if (!FRAME_WINDOW_P (it->f))
2095 {
2096 if (it->truncate_lines_p)
2097 {
2098 /* We will need the truncation glyph. */
2099 xassert (it->glyph_row == NULL);
2100 produce_special_glyphs (it, IT_TRUNCATION);
2101 it->truncation_pixel_width = it->pixel_width;
2102 }
2103 else
2104 {
2105 /* We will need the continuation glyph. */
2106 xassert (it->glyph_row == NULL);
2107 produce_special_glyphs (it, IT_CONTINUATION);
2108 it->continuation_pixel_width = it->pixel_width;
2109 }
2110
153c2160 2111 /* Reset these values to zero because the produce_special_glyphs
5f5c8ee5
GM
2112 above has changed them. */
2113 it->pixel_width = it->ascent = it->descent = 0;
312246d1 2114 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
2115 }
2116
2117 /* Set this after getting the dimensions of truncation and
2118 continuation glyphs, so that we don't produce glyphs when calling
2119 produce_special_glyphs, above. */
2120 it->glyph_row = row;
2121 it->area = TEXT_AREA;
2122
2123 /* Get the dimensions of the display area. The display area
2124 consists of the visible window area plus a horizontally scrolled
2125 part to the left of the window. All x-values are relative to the
2126 start of this total display area. */
2127 if (base_face_id != DEFAULT_FACE_ID)
2128 {
2129 /* Mode lines, menu bar in terminal frames. */
2130 it->first_visible_x = 0;
da8b7f4f 2131 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
5f5c8ee5
GM
2132 }
2133 else
2134 {
2135 it->first_visible_x
da8b7f4f 2136 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
5f5c8ee5
GM
2137 it->last_visible_x = (it->first_visible_x
2138 + window_box_width (w, TEXT_AREA));
2139
2140 /* If we truncate lines, leave room for the truncator glyph(s) at
2141 the right margin. Otherwise, leave room for the continuation
2142 glyph(s). Truncation and continuation glyphs are not inserted
2143 for window-based redisplay. */
2144 if (!FRAME_WINDOW_P (it->f))
2145 {
2146 if (it->truncate_lines_p)
2147 it->last_visible_x -= it->truncation_pixel_width;
2148 else
2149 it->last_visible_x -= it->continuation_pixel_width;
2150 }
2151
045dee35 2152 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
da8b7f4f 2153 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
2154 }
2155
2156 /* Leave room for a border glyph. */
2157 if (!FRAME_WINDOW_P (it->f)
2158 && !WINDOW_RIGHTMOST_P (it->w))
2159 it->last_visible_x -= 1;
2160
2161 it->last_visible_y = window_text_bottom_y (w);
2162
2163 /* For mode lines and alike, arrange for the first glyph having a
2164 left box line if the face specifies a box. */
2165 if (base_face_id != DEFAULT_FACE_ID)
2166 {
2167 struct face *face;
2311178e 2168
5f5c8ee5
GM
2169 it->face_id = base_face_id;
2170
2171 /* If we have a boxed mode line, make the first character appear
2172 with a left box line. */
2173 face = FACE_FROM_ID (it->f, base_face_id);
2174 if (face->box != FACE_NO_BOX)
2175 it->start_of_box_run_p = 1;
2176 }
2177
2178 /* If a buffer position was specified, set the iterator there,
2179 getting overlays and face properties from that position. */
3ebf0ea9 2180 if (charpos >= BUF_BEG (current_buffer))
5f5c8ee5
GM
2181 {
2182 it->end_charpos = ZV;
2183 it->face_id = -1;
2184 IT_CHARPOS (*it) = charpos;
2311178e 2185
5f5c8ee5 2186 /* Compute byte position if not specified. */
3ebf0ea9 2187 if (bytepos < charpos)
5f5c8ee5
GM
2188 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2189 else
2190 IT_BYTEPOS (*it) = bytepos;
2191
29b3ea63
KS
2192 it->start = it->current;
2193
5f5c8ee5
GM
2194 /* Compute faces etc. */
2195 reseat (it, it->current.pos, 1);
2196 }
2311178e 2197
5f5c8ee5
GM
2198 CHECK_IT (it);
2199}
2200
2201
2202/* Initialize IT for the display of window W with window start POS. */
2203
2204void
2205start_display (it, w, pos)
2206 struct it *it;
2207 struct window *w;
2208 struct text_pos pos;
2209{
5f5c8ee5 2210 struct glyph_row *row;
045dee35 2211 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
2212
2213 row = w->desired_matrix->rows + first_vpos;
2214 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3ccb8bcf 2215 it->first_vpos = first_vpos;
17fdcfc8
GM
2216
2217 if (!it->truncate_lines_p)
5f5c8ee5 2218 {
17fdcfc8
GM
2219 int start_at_line_beg_p;
2220 int first_y = it->current_y;
2311178e 2221
17fdcfc8
GM
2222 /* If window start is not at a line start, skip forward to POS to
2223 get the correct continuation lines width. */
2224 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2225 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2226 if (!start_at_line_beg_p)
5f5c8ee5 2227 {
0e47bbf7
RS
2228 int new_x;
2229
17fdcfc8
GM
2230 reseat_at_previous_visible_line_start (it);
2231 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2232
0e47bbf7
RS
2233 new_x = it->current_x + it->pixel_width;
2234
17fdcfc8
GM
2235 /* If lines are continued, this line may end in the middle
2236 of a multi-glyph character (e.g. a control character
2237 displayed as \003, or in the middle of an overlay
2238 string). In this case move_it_to above will not have
2239 taken us to the start of the continuation line but to the
2240 end of the continued line. */
0e47bbf7
RS
2241 if (it->current_x > 0
2242 && !it->truncate_lines_p /* Lines are continued. */
2243 && (/* And glyph doesn't fit on the line. */
2244 new_x > it->last_visible_x
2245 /* Or it fits exactly and we're on a window
2246 system frame. */
2247 || (new_x == it->last_visible_x
2248 && FRAME_WINDOW_P (it->f))))
5f5c8ee5 2249 {
b28cb6ed
GM
2250 if (it->current.dpvec_index >= 0
2251 || it->current.overlay_string_index >= 0)
2252 {
cafafe0b 2253 set_iterator_to_next (it, 1);
b28cb6ed
GM
2254 move_it_in_display_line_to (it, -1, -1, 0);
2255 }
2311178e 2256
b28cb6ed 2257 it->continuation_lines_width += it->current_x;
5f5c8ee5 2258 }
b28cb6ed
GM
2259
2260 /* We're starting a new display line, not affected by the
2261 height of the continued line, so clear the appropriate
2262 fields in the iterator structure. */
2263 it->max_ascent = it->max_descent = 0;
2264 it->max_phys_ascent = it->max_phys_descent = 0;
2311178e 2265
17fdcfc8
GM
2266 it->current_y = first_y;
2267 it->vpos = 0;
2268 it->current_x = it->hpos = 0;
2269 }
5f5c8ee5
GM
2270 }
2271
2272#if 0 /* Don't assert the following because start_display is sometimes
2273 called intentionally with a window start that is not at a
2274 line start. Please leave this code in as a comment. */
2311178e 2275
5f5c8ee5
GM
2276 /* Window start should be on a line start, now. */
2277 xassert (it->continuation_lines_width
2278 || IT_CHARPOS (it) == BEGV
2279 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2280#endif /* 0 */
2281}
2282
2283
4bde0ebb
GM
2284/* Return 1 if POS is a position in ellipses displayed for invisible
2285 text. W is the window we display, for text property lookup. */
5f5c8ee5 2286
4bde0ebb
GM
2287static int
2288in_ellipses_for_invisible_text_p (pos, w)
5f5c8ee5 2289 struct display_pos *pos;
4bde0ebb 2290 struct window *w;
5f5c8ee5 2291{
ac90c44f 2292 Lisp_Object prop, window;
4bde0ebb
GM
2293 int ellipses_p = 0;
2294 int charpos = CHARPOS (pos->pos);
2311178e 2295
ac90c44f
GM
2296 /* If POS specifies a position in a display vector, this might
2297 be for an ellipsis displayed for invisible text. We won't
2298 get the iterator set up for delivering that ellipsis unless
2299 we make sure that it gets aware of the invisible text. */
2300 if (pos->dpvec_index >= 0
2301 && pos->overlay_string_index < 0
2302 && CHARPOS (pos->string_pos) < 0
2303 && charpos > BEGV
2304 && (XSETWINDOW (window, w),
2305 prop = Fget_char_property (make_number (charpos),
2306 Qinvisible, window),
20fbd925 2307 !TEXT_PROP_MEANS_INVISIBLE (prop)))
ac90c44f
GM
2308 {
2309 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2310 window);
8580a4e3 2311 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
4bde0ebb
GM
2312 }
2313
2314 return ellipses_p;
2315}
2316
2317
2318/* Initialize IT for stepping through current_buffer in window W,
2319 starting at position POS that includes overlay string and display
47d57b22
GM
2320 vector/ control character translation position information. Value
2321 is zero if there are overlay strings with newlines at POS. */
4bde0ebb 2322
47d57b22 2323static int
4bde0ebb
GM
2324init_from_display_pos (it, w, pos)
2325 struct it *it;
2326 struct window *w;
2327 struct display_pos *pos;
2328{
2329 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
47d57b22 2330 int i, overlay_strings_with_newlines = 0;
2311178e 2331
4bde0ebb
GM
2332 /* If POS specifies a position in a display vector, this might
2333 be for an ellipsis displayed for invisible text. We won't
2334 get the iterator set up for delivering that ellipsis unless
2335 we make sure that it gets aware of the invisible text. */
2336 if (in_ellipses_for_invisible_text_p (pos, w))
2337 {
2338 --charpos;
2339 bytepos = 0;
ac90c44f 2340 }
2311178e 2341
5f5c8ee5
GM
2342 /* Keep in mind: the call to reseat in init_iterator skips invisible
2343 text, so we might end up at a position different from POS. This
2344 is only a problem when POS is a row start after a newline and an
2345 overlay starts there with an after-string, and the overlay has an
2346 invisible property. Since we don't skip invisible text in
2347 display_line and elsewhere immediately after consuming the
2348 newline before the row start, such a POS will not be in a string,
2349 but the call to init_iterator below will move us to the
2350 after-string. */
ac90c44f 2351 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
5f5c8ee5 2352
47d57b22 2353 for (i = 0; i < it->n_overlay_strings; ++i)
1e1e5daf 2354 {
50f80c2f
KR
2355 const char *s = SDATA (it->overlay_strings[i]);
2356 const char *e = s + SBYTES (it->overlay_strings[i]);
2311178e 2357
1e1e5daf
GM
2358 while (s < e && *s != '\n')
2359 ++s;
2360
2361 if (s < e)
2362 {
2363 overlay_strings_with_newlines = 1;
2364 break;
2365 }
2366 }
47d57b22 2367
75c5350a
GM
2368 /* If position is within an overlay string, set up IT to the right
2369 overlay string. */
5f5c8ee5
GM
2370 if (pos->overlay_string_index >= 0)
2371 {
2372 int relative_index;
75c5350a
GM
2373
2374 /* If the first overlay string happens to have a `display'
2375 property for an image, the iterator will be set up for that
2376 image, and we have to undo that setup first before we can
2377 correct the overlay string index. */
2378 if (it->method == next_element_from_image)
2379 pop_it (it);
2311178e 2380
5f5c8ee5
GM
2381 /* We already have the first chunk of overlay strings in
2382 IT->overlay_strings. Load more until the one for
2383 pos->overlay_string_index is in IT->overlay_strings. */
2384 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2385 {
2386 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2387 it->current.overlay_string_index = 0;
2388 while (n--)
2389 {
5a08cbaf 2390 load_overlay_strings (it, 0);
5f5c8ee5
GM
2391 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2392 }
2393 }
2311178e 2394
5f5c8ee5
GM
2395 it->current.overlay_string_index = pos->overlay_string_index;
2396 relative_index = (it->current.overlay_string_index
2397 % OVERLAY_STRING_CHUNK_SIZE);
2398 it->string = it->overlay_strings[relative_index];
cafafe0b 2399 xassert (STRINGP (it->string));
5f5c8ee5
GM
2400 it->current.string_pos = pos->string_pos;
2401 it->method = next_element_from_string;
2402 }
2311178e 2403
1e1e5daf
GM
2404#if 0 /* This is bogus because POS not having an overlay string
2405 position does not mean it's after the string. Example: A
2406 line starting with a before-string and initialization of IT
2407 to the previous row's end position. */
2be8f184
GM
2408 else if (it->current.overlay_string_index >= 0)
2409 {
2410 /* If POS says we're already after an overlay string ending at
2411 POS, make sure to pop the iterator because it will be in
2412 front of that overlay string. When POS is ZV, we've thereby
2413 also ``processed'' overlay strings at ZV. */
aeb2b8fc
GM
2414 while (it->sp)
2415 pop_it (it);
2be8f184
GM
2416 it->current.overlay_string_index = -1;
2417 it->method = next_element_from_buffer;
2418 if (CHARPOS (pos->pos) == ZV)
2419 it->overlay_strings_at_end_processed_p = 1;
2420 }
1e1e5daf 2421#endif /* 0 */
2311178e 2422
2be8f184 2423 if (CHARPOS (pos->string_pos) >= 0)
5f5c8ee5
GM
2424 {
2425 /* Recorded position is not in an overlay string, but in another
2426 string. This can only be a string from a `display' property.
2427 IT should already be filled with that string. */
2428 it->current.string_pos = pos->string_pos;
2429 xassert (STRINGP (it->string));
2430 }
2431
ac90c44f
GM
2432 /* Restore position in display vector translations, control
2433 character translations or ellipses. */
5f5c8ee5
GM
2434 if (pos->dpvec_index >= 0)
2435 {
ac90c44f
GM
2436 if (it->dpvec == NULL)
2437 get_next_display_element (it);
5f5c8ee5
GM
2438 xassert (it->dpvec && it->current.dpvec_index == 0);
2439 it->current.dpvec_index = pos->dpvec_index;
2440 }
2311178e 2441
5f5c8ee5 2442 CHECK_IT (it);
47d57b22 2443 return !overlay_strings_with_newlines;
5f5c8ee5
GM
2444}
2445
2446
2447/* Initialize IT for stepping through current_buffer in window W
2448 starting at ROW->start. */
2449
2450static void
2451init_to_row_start (it, w, row)
2452 struct it *it;
2453 struct window *w;
2454 struct glyph_row *row;
2455{
2456 init_from_display_pos (it, w, &row->start);
29b3ea63 2457 it->start = row->start;
5f5c8ee5
GM
2458 it->continuation_lines_width = row->continuation_lines_width;
2459 CHECK_IT (it);
2460}
2461
2311178e 2462
5f5c8ee5 2463/* Initialize IT for stepping through current_buffer in window W
47d57b22
GM
2464 starting in the line following ROW, i.e. starting at ROW->end.
2465 Value is zero if there are overlay strings with newlines at ROW's
2466 end position. */
5f5c8ee5 2467
47d57b22 2468static int
5f5c8ee5
GM
2469init_to_row_end (it, w, row)
2470 struct it *it;
2471 struct window *w;
2472 struct glyph_row *row;
2473{
47d57b22 2474 int success = 0;
2311178e 2475
47d57b22
GM
2476 if (init_from_display_pos (it, w, &row->end))
2477 {
2478 if (row->continued_p)
2479 it->continuation_lines_width
2480 = row->continuation_lines_width + row->pixel_width;
2481 CHECK_IT (it);
2482 success = 1;
2483 }
2311178e 2484
47d57b22 2485 return success;
5f5c8ee5
GM
2486}
2487
2488
2489
2490\f
2491/***********************************************************************
2492 Text properties
2493 ***********************************************************************/
2494
2495/* Called when IT reaches IT->stop_charpos. Handle text property and
2496 overlay changes. Set IT->stop_charpos to the next position where
2497 to stop. */
2498
2499static void
2500handle_stop (it)
2501 struct it *it;
2502{
2503 enum prop_handled handled;
2504 int handle_overlay_change_p = 1;
2505 struct props *p;
2506
2507 it->dpvec = NULL;
2508 it->current.dpvec_index = -1;
2509
2510 do
2511 {
2512 handled = HANDLED_NORMALLY;
2311178e 2513
5f5c8ee5
GM
2514 /* Call text property handlers. */
2515 for (p = it_props; p->handler; ++p)
2516 {
2517 handled = p->handler (it);
2970b9be 2518
5f5c8ee5
GM
2519 if (handled == HANDLED_RECOMPUTE_PROPS)
2520 break;
2521 else if (handled == HANDLED_RETURN)
2522 return;
2523 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2524 handle_overlay_change_p = 0;
2525 }
2526
2527 if (handled != HANDLED_RECOMPUTE_PROPS)
2528 {
2529 /* Don't check for overlay strings below when set to deliver
2530 characters from a display vector. */
2531 if (it->method == next_element_from_display_vector)
2532 handle_overlay_change_p = 0;
2533
2534 /* Handle overlay changes. */
2535 if (handle_overlay_change_p)
2536 handled = handle_overlay_change (it);
2311178e 2537
5f5c8ee5
GM
2538 /* Determine where to stop next. */
2539 if (handled == HANDLED_NORMALLY)
2540 compute_stop_pos (it);
2541 }
2542 }
2543 while (handled == HANDLED_RECOMPUTE_PROPS);
2544}
2545
2546
2547/* Compute IT->stop_charpos from text property and overlay change
2548 information for IT's current position. */
2549
2550static void
2551compute_stop_pos (it)
2552 struct it *it;
2553{
2554 register INTERVAL iv, next_iv;
2555 Lisp_Object object, limit, position;
2556
2557 /* If nowhere else, stop at the end. */
2558 it->stop_charpos = it->end_charpos;
2311178e 2559
5f5c8ee5
GM
2560 if (STRINGP (it->string))
2561 {
2562 /* Strings are usually short, so don't limit the search for
2563 properties. */
2564 object = it->string;
2565 limit = Qnil;
ac90c44f 2566 position = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
2567 }
2568 else
2569 {
2570 int charpos;
2571
2572 /* If next overlay change is in front of the current stop pos
2573 (which is IT->end_charpos), stop there. Note: value of
2574 next_overlay_change is point-max if no overlay change
2575 follows. */
2576 charpos = next_overlay_change (IT_CHARPOS (*it));
2577 if (charpos < it->stop_charpos)
2578 it->stop_charpos = charpos;
2579
2580 /* If showing the region, we have to stop at the region
2581 start or end because the face might change there. */
2582 if (it->region_beg_charpos > 0)
2583 {
2584 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2585 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2586 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2587 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2588 }
2311178e 2589
5f5c8ee5
GM
2590 /* Set up variables for computing the stop position from text
2591 property changes. */
2592 XSETBUFFER (object, current_buffer);
ac90c44f
GM
2593 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2594 position = make_number (IT_CHARPOS (*it));
5f5c8ee5
GM
2595
2596 }
2597
2598 /* Get the interval containing IT's position. Value is a null
2599 interval if there isn't such an interval. */
2600 iv = validate_interval_range (object, &position, &position, 0);
2601 if (!NULL_INTERVAL_P (iv))
2602 {
2603 Lisp_Object values_here[LAST_PROP_IDX];
2604 struct props *p;
2605
2606 /* Get properties here. */
2607 for (p = it_props; p->handler; ++p)
2608 values_here[p->idx] = textget (iv->plist, *p->name);
2609
2610 /* Look for an interval following iv that has different
2611 properties. */
2612 for (next_iv = next_interval (iv);
2613 (!NULL_INTERVAL_P (next_iv)
2614 && (NILP (limit)
2615 || XFASTINT (limit) > next_iv->position));
2616 next_iv = next_interval (next_iv))
2617 {
2618 for (p = it_props; p->handler; ++p)
2619 {
2620 Lisp_Object new_value;
2621
2622 new_value = textget (next_iv->plist, *p->name);
2623 if (!EQ (values_here[p->idx], new_value))
2624 break;
2625 }
2311178e 2626
5f5c8ee5
GM
2627 if (p->handler)
2628 break;
2629 }
2630
2631 if (!NULL_INTERVAL_P (next_iv))
2632 {
2633 if (INTEGERP (limit)
2634 && next_iv->position >= XFASTINT (limit))
2635 /* No text property change up to limit. */
2636 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2637 else
2638 /* Text properties change in next_iv. */
2639 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2640 }
2641 }
2642
2643 xassert (STRINGP (it->string)
2644 || (it->stop_charpos >= BEGV
2645 && it->stop_charpos >= IT_CHARPOS (*it)));
2646}
2647
2648
2649/* Return the position of the next overlay change after POS in
2650 current_buffer. Value is point-max if no overlay change
2651 follows. This is like `next-overlay-change' but doesn't use
2652 xmalloc. */
2653
2654static int
2655next_overlay_change (pos)
2656 int pos;
2657{
2658 int noverlays;
2659 int endpos;
2660 Lisp_Object *overlays;
2661 int len;
2662 int i;
2663
2664 /* Get all overlays at the given position. */
2665 len = 10;
2666 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2667 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2668 if (noverlays > len)
2669 {
2670 len = noverlays;
2671 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2672 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2673 }
2674
2675 /* If any of these overlays ends before endpos,
2676 use its ending point instead. */
2677 for (i = 0; i < noverlays; ++i)
2678 {
2679 Lisp_Object oend;
2680 int oendpos;
2681
2682 oend = OVERLAY_END (overlays[i]);
2683 oendpos = OVERLAY_POSITION (oend);
2684 endpos = min (endpos, oendpos);
2685 }
2686
2687 return endpos;
2688}
2689
2690
2691\f
2692/***********************************************************************
2693 Fontification
2694 ***********************************************************************/
2695
2696/* Handle changes in the `fontified' property of the current buffer by
2697 calling hook functions from Qfontification_functions to fontify
2698 regions of text. */
2699
2700static enum prop_handled
2701handle_fontified_prop (it)
2702 struct it *it;
2703{
2704 Lisp_Object prop, pos;
2705 enum prop_handled handled = HANDLED_NORMALLY;
2706
2707 /* Get the value of the `fontified' property at IT's current buffer
2708 position. (The `fontified' property doesn't have a special
2709 meaning in strings.) If the value is nil, call functions from
2710 Qfontification_functions. */
2711 if (!STRINGP (it->string)
2712 && it->s == NULL
2713 && !NILP (Vfontification_functions)
085536c2 2714 && !NILP (Vrun_hooks)
5f5c8ee5
GM
2715 && (pos = make_number (IT_CHARPOS (*it)),
2716 prop = Fget_char_property (pos, Qfontified, Qnil),
2717 NILP (prop)))
2718 {
331379bf 2719 int count = SPECPDL_INDEX ();
085536c2
GM
2720 Lisp_Object val;
2721
2722 val = Vfontification_functions;
2723 specbind (Qfontification_functions, Qnil);
2311178e 2724
085536c2 2725 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
116d6f5c 2726 safe_call1 (val, pos);
085536c2
GM
2727 else
2728 {
2729 Lisp_Object globals, fn;
2730 struct gcpro gcpro1, gcpro2;
5f5c8ee5 2731
085536c2
GM
2732 globals = Qnil;
2733 GCPRO2 (val, globals);
2311178e 2734
085536c2
GM
2735 for (; CONSP (val); val = XCDR (val))
2736 {
2737 fn = XCAR (val);
2311178e 2738
085536c2
GM
2739 if (EQ (fn, Qt))
2740 {
2741 /* A value of t indicates this hook has a local
2742 binding; it means to run the global binding too.
2743 In a global value, t should not occur. If it
2744 does, we must ignore it to avoid an endless
2745 loop. */
2746 for (globals = Fdefault_value (Qfontification_functions);
2747 CONSP (globals);
2748 globals = XCDR (globals))
2749 {
2750 fn = XCAR (globals);
2751 if (!EQ (fn, Qt))
116d6f5c 2752 safe_call1 (fn, pos);
085536c2
GM
2753 }
2754 }
2755 else
116d6f5c 2756 safe_call1 (fn, pos);
085536c2
GM
2757 }
2758
2759 UNGCPRO;
2760 }
2761
2762 unbind_to (count, Qnil);
5f5c8ee5
GM
2763
2764 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2765 something. This avoids an endless loop if they failed to
2766 fontify the text for which reason ever. */
2767 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2768 handled = HANDLED_RECOMPUTE_PROPS;
2769 }
2770
2771 return handled;
2772}
2773
2774
2775\f
2776/***********************************************************************
2777 Faces
2778 ***********************************************************************/
2779
2780/* Set up iterator IT from face properties at its current position.
2781 Called from handle_stop. */
2782
2783static enum prop_handled
2784handle_face_prop (it)
2785 struct it *it;
2786{
2787 int new_face_id, next_stop;
2311178e 2788
5f5c8ee5
GM
2789 if (!STRINGP (it->string))
2790 {
2791 new_face_id
2792 = face_at_buffer_position (it->w,
2793 IT_CHARPOS (*it),
2794 it->region_beg_charpos,
2795 it->region_end_charpos,
2796 &next_stop,
2797 (IT_CHARPOS (*it)
2798 + TEXT_PROP_DISTANCE_LIMIT),
2799 0);
2311178e 2800
5f5c8ee5
GM
2801 /* Is this a start of a run of characters with box face?
2802 Caveat: this can be called for a freshly initialized
4b41cebb 2803 iterator; face_id is -1 in this case. We know that the new
5f5c8ee5
GM
2804 face will not change until limit, i.e. if the new face has a
2805 box, all characters up to limit will have one. But, as
2806 usual, we don't know whether limit is really the end. */
2807 if (new_face_id != it->face_id)
2808 {
2809 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2311178e 2810
5f5c8ee5
GM
2811 /* If new face has a box but old face has not, this is
2812 the start of a run of characters with box, i.e. it has
2813 a shadow on the left side. The value of face_id of the
2814 iterator will be -1 if this is the initial call that gets
2815 the face. In this case, we have to look in front of IT's
2816 position and see whether there is a face != new_face_id. */
2817 it->start_of_box_run_p
2818 = (new_face->box != FACE_NO_BOX
2819 && (it->face_id >= 0
2820 || IT_CHARPOS (*it) == BEG
2821 || new_face_id != face_before_it_pos (it)));
2822 it->face_box_p = new_face->box != FACE_NO_BOX;
2823 }
2824 }
2825 else
2826 {
06a12811
GM
2827 int base_face_id, bufpos;
2828
2829 if (it->current.overlay_string_index >= 0)
2830 bufpos = IT_CHARPOS (*it);
2831 else
2832 bufpos = 0;
2311178e 2833
06a12811
GM
2834 /* For strings from a buffer, i.e. overlay strings or strings
2835 from a `display' property, use the face at IT's current
2836 buffer position as the base face to merge with, so that
2837 overlay strings appear in the same face as surrounding
2838 text, unless they specify their own faces. */
2839 base_face_id = underlying_face_id (it);
2311178e 2840
06a12811
GM
2841 new_face_id = face_at_string_position (it->w,
2842 it->string,
2843 IT_STRING_CHARPOS (*it),
2844 bufpos,
2845 it->region_beg_charpos,
2846 it->region_end_charpos,
2847 &next_stop,
5de7c6f2 2848 base_face_id, 0);
2311178e 2849
5f5c8ee5
GM
2850#if 0 /* This shouldn't be neccessary. Let's check it. */
2851 /* If IT is used to display a mode line we would really like to
2852 use the mode line face instead of the frame's default face. */
2853 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2854 && new_face_id == DEFAULT_FACE_ID)
96d2320f 2855 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
5f5c8ee5 2856#endif
2311178e 2857
5f5c8ee5
GM
2858 /* Is this a start of a run of characters with box? Caveat:
2859 this can be called for a freshly allocated iterator; face_id
2860 is -1 is this case. We know that the new face will not
2861 change until the next check pos, i.e. if the new face has a
2862 box, all characters up to that position will have a
2863 box. But, as usual, we don't know whether that position
2864 is really the end. */
2865 if (new_face_id != it->face_id)
2866 {
2867 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2868 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2311178e 2869
5f5c8ee5
GM
2870 /* If new face has a box but old face hasn't, this is the
2871 start of a run of characters with box, i.e. it has a
2872 shadow on the left side. */
2873 it->start_of_box_run_p
2874 = new_face->box && (old_face == NULL || !old_face->box);
2875 it->face_box_p = new_face->box != FACE_NO_BOX;
2876 }
2877 }
2311178e 2878
5f5c8ee5 2879 it->face_id = new_face_id;
5f5c8ee5
GM
2880 return HANDLED_NORMALLY;
2881}
2882
2883
06a12811
GM
2884/* Return the ID of the face ``underlying'' IT's current position,
2885 which is in a string. If the iterator is associated with a
2886 buffer, return the face at IT's current buffer position.
2887 Otherwise, use the iterator's base_face_id. */
2888
2889static int
2890underlying_face_id (it)
2891 struct it *it;
2892{
2893 int face_id = it->base_face_id, i;
2894
2895 xassert (STRINGP (it->string));
2896
2897 for (i = it->sp - 1; i >= 0; --i)
2898 if (NILP (it->stack[i].string))
2899 face_id = it->stack[i].face_id;
2900
2901 return face_id;
2902}
2903
2904
5f5c8ee5
GM
2905/* Compute the face one character before or after the current position
2906 of IT. BEFORE_P non-zero means get the face in front of IT's
2907 position. Value is the id of the face. */
2908
2909static int
2910face_before_or_after_it_pos (it, before_p)
2911 struct it *it;
2912 int before_p;
2913{
2914 int face_id, limit;
2915 int next_check_charpos;
2916 struct text_pos pos;
2917
2918 xassert (it->s == NULL);
2311178e 2919
5f5c8ee5
GM
2920 if (STRINGP (it->string))
2921 {
06a12811 2922 int bufpos, base_face_id;
2311178e 2923
5f5c8ee5
GM
2924 /* No face change past the end of the string (for the case
2925 we are padding with spaces). No face change before the
2926 string start. */
2051c264 2927 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
5f5c8ee5
GM
2928 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2929 return it->face_id;
2930
2931 /* Set pos to the position before or after IT's current position. */
2932 if (before_p)
2933 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2934 else
260a86a0
KH
2935 /* For composition, we must check the character after the
2936 composition. */
2937 pos = (it->what == IT_COMPOSITION
2938 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2939 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
5f5c8ee5 2940
06a12811
GM
2941 if (it->current.overlay_string_index >= 0)
2942 bufpos = IT_CHARPOS (*it);
2943 else
2944 bufpos = 0;
2945
2946 base_face_id = underlying_face_id (it);
2947
5f5c8ee5 2948 /* Get the face for ASCII, or unibyte. */
06a12811
GM
2949 face_id = face_at_string_position (it->w,
2950 it->string,
2951 CHARPOS (pos),
2952 bufpos,
2953 it->region_beg_charpos,
2954 it->region_end_charpos,
2955 &next_check_charpos,
5de7c6f2 2956 base_face_id, 0);
5f5c8ee5
GM
2957
2958 /* Correct the face for charsets different from ASCII. Do it
2959 for the multibyte case only. The face returned above is
2960 suitable for unibyte text if IT->string is unibyte. */
2961 if (STRING_MULTIBYTE (it->string))
2962 {
50f80c2f 2963 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2051c264 2964 int rest = SBYTES (it->string) - BYTEPOS (pos);
980806b6
KH
2965 int c, len;
2966 struct face *face = FACE_FROM_ID (it->f, face_id);
2311178e 2967
4fdb80f2 2968 c = string_char_and_length (p, rest, &len);
980806b6 2969 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2970 }
2971 }
2972 else
2973 {
70851746
GM
2974 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2975 || (IT_CHARPOS (*it) <= BEGV && before_p))
2976 return it->face_id;
2311178e 2977
5f5c8ee5
GM
2978 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2979 pos = it->current.pos;
2311178e 2980
5f5c8ee5 2981 if (before_p)
c1005d06 2982 DEC_TEXT_POS (pos, it->multibyte_p);
5f5c8ee5 2983 else
260a86a0
KH
2984 {
2985 if (it->what == IT_COMPOSITION)
2986 /* For composition, we must check the position after the
2987 composition. */
2988 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2989 else
c1005d06 2990 INC_TEXT_POS (pos, it->multibyte_p);
260a86a0 2991 }
2311178e 2992
5f5c8ee5
GM
2993 /* Determine face for CHARSET_ASCII, or unibyte. */
2994 face_id = face_at_buffer_position (it->w,
2995 CHARPOS (pos),
2996 it->region_beg_charpos,
2997 it->region_end_charpos,
2998 &next_check_charpos,
2999 limit, 0);
3000
3001 /* Correct the face for charsets different from ASCII. Do it
3002 for the multibyte case only. The face returned above is
3003 suitable for unibyte text if current_buffer is unibyte. */
3004 if (it->multibyte_p)
3005 {
1d1b6e6a 3006 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
980806b6
KH
3007 struct face *face = FACE_FROM_ID (it->f, face_id);
3008 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
3009 }
3010 }
2311178e 3011
5f5c8ee5
GM
3012 return face_id;
3013}
3014
3015
3016\f
3017/***********************************************************************
3018 Invisible text
3019 ***********************************************************************/
3020
3021/* Set up iterator IT from invisible properties at its current
3022 position. Called from handle_stop. */
3023
3024static enum prop_handled
3025handle_invisible_prop (it)
3026 struct it *it;
3027{
3028 enum prop_handled handled = HANDLED_NORMALLY;
3029
3030 if (STRINGP (it->string))
3031 {
3032 extern Lisp_Object Qinvisible;
3033 Lisp_Object prop, end_charpos, limit, charpos;
3034
3035 /* Get the value of the invisible text property at the
3036 current position. Value will be nil if there is no such
3037 property. */
ac90c44f 3038 charpos = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
3039 prop = Fget_text_property (charpos, Qinvisible, it->string);
3040
eadc0bf8
GM
3041 if (!NILP (prop)
3042 && IT_STRING_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
3043 {
3044 handled = HANDLED_RECOMPUTE_PROPS;
2311178e 3045
5f5c8ee5
GM
3046 /* Get the position at which the next change of the
3047 invisible text property can be found in IT->string.
3048 Value will be nil if the property value is the same for
3049 all the rest of IT->string. */
2051c264 3050 XSETINT (limit, SCHARS (it->string));
5f5c8ee5 3051 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2051c264 3052 it->string, limit);
2311178e 3053
5f5c8ee5
GM
3054 /* Text at current position is invisible. The next
3055 change in the property is at position end_charpos.
3056 Move IT's current position to that position. */
3057 if (INTEGERP (end_charpos)
3058 && XFASTINT (end_charpos) < XFASTINT (limit))
3059 {
3060 struct text_pos old;
3061 old = it->current.string_pos;
3062 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3063 compute_string_pos (&it->current.string_pos, old, it->string);
3064 }
3065 else
3066 {
3067 /* The rest of the string is invisible. If this is an
3068 overlay string, proceed with the next overlay string
3069 or whatever comes and return a character from there. */
3070 if (it->current.overlay_string_index >= 0)
3071 {
3072 next_overlay_string (it);
3073 /* Don't check for overlay strings when we just
3074 finished processing them. */
3075 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3076 }
3077 else
3078 {
2051c264
GM
3079 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3080 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
5f5c8ee5
GM
3081 }
3082 }
3083 }
3084 }
3085 else
3086 {
8580a4e3 3087 int invis_p, newpos, next_stop, start_charpos;
5a08cbaf 3088 Lisp_Object pos, prop, overlay;
5f5c8ee5
GM
3089
3090 /* First of all, is there invisible text at this position? */
5a08cbaf 3091 start_charpos = IT_CHARPOS (*it);
ac90c44f 3092 pos = make_number (IT_CHARPOS (*it));
5a08cbaf
GM
3093 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3094 &overlay);
8580a4e3
SM
3095 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3096
5f5c8ee5 3097 /* If we are on invisible text, skip over it. */
8580a4e3 3098 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
3099 {
3100 /* Record whether we have to display an ellipsis for the
3101 invisible text. */
8580a4e3 3102 int display_ellipsis_p = invis_p == 2;
5f5c8ee5
GM
3103
3104 handled = HANDLED_RECOMPUTE_PROPS;
2311178e 3105
5f5c8ee5
GM
3106 /* Loop skipping over invisible text. The loop is left at
3107 ZV or with IT on the first char being visible again. */
3108 do
3109 {
3110 /* Try to skip some invisible text. Return value is the
3111 position reached which can be equal to IT's position
3112 if there is nothing invisible here. This skips both
3113 over invisible text properties and overlays with
3114 invisible property. */
3115 newpos = skip_invisible (IT_CHARPOS (*it),
3116 &next_stop, ZV, it->window);
3117
3118 /* If we skipped nothing at all we weren't at invisible
3119 text in the first place. If everything to the end of
3120 the buffer was skipped, end the loop. */
3121 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
8580a4e3 3122 invis_p = 0;
5f5c8ee5
GM
3123 else
3124 {
3125 /* We skipped some characters but not necessarily
3126 all there are. Check if we ended up on visible
3127 text. Fget_char_property returns the property of
3128 the char before the given position, i.e. if we
8580a4e3 3129 get invis_p = 0, this means that the char at
5f5c8ee5 3130 newpos is visible. */
ac90c44f 3131 pos = make_number (newpos);
5f5c8ee5 3132 prop = Fget_char_property (pos, Qinvisible, it->window);
8580a4e3 3133 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
5f5c8ee5 3134 }
2311178e 3135
5f5c8ee5
GM
3136 /* If we ended up on invisible text, proceed to
3137 skip starting with next_stop. */
8580a4e3 3138 if (invis_p)
5f5c8ee5
GM
3139 IT_CHARPOS (*it) = next_stop;
3140 }
8580a4e3 3141 while (invis_p);
5a08cbaf 3142
5f5c8ee5
GM
3143 /* The position newpos is now either ZV or on visible text. */
3144 IT_CHARPOS (*it) = newpos;
3145 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2311178e 3146
5a08cbaf
GM
3147 /* If there are before-strings at the start of invisible
3148 text, and the text is invisible because of a text
3149 property, arrange to show before-strings because 20.x did
3150 it that way. (If the text is invisible because of an
3151 overlay property instead of a text property, this is
3152 already handled in the overlay code.) */
3153 if (NILP (overlay)
3154 && get_overlay_strings (it, start_charpos))
5f5c8ee5 3155 {
5a08cbaf
GM
3156 handled = HANDLED_RECOMPUTE_PROPS;
3157 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
5f5c8ee5 3158 }
5a08cbaf
GM
3159 else if (display_ellipsis_p)
3160 setup_for_ellipsis (it);
5f5c8ee5
GM
3161 }
3162 }
3163
3164 return handled;
3165}
3166
3167
5a08cbaf
GM
3168/* Make iterator IT return `...' next. */
3169
3170static void
3171setup_for_ellipsis (it)
3172 struct it *it;
3173{
2311178e 3174 if (it->dp
5a08cbaf
GM
3175 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3176 {
3177 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3178 it->dpvec = v->contents;
3179 it->dpend = v->contents + v->size;
3180 }
2311178e 3181 else
5a08cbaf
GM
3182 {
3183 /* Default `...'. */
3184 it->dpvec = default_invis_vector;
3185 it->dpend = default_invis_vector + 3;
3186 }
2311178e 3187
5a08cbaf
GM
3188 /* The ellipsis display does not replace the display of the
3189 character at the new position. Indicate this by setting
3190 IT->dpvec_char_len to zero. */
3191 it->dpvec_char_len = 0;
2311178e 3192
5a08cbaf
GM
3193 it->current.dpvec_index = 0;
3194 it->method = next_element_from_display_vector;
3195}
3196
3197
5f5c8ee5
GM
3198\f
3199/***********************************************************************
3200 'display' property
3201 ***********************************************************************/
3202
3203/* Set up iterator IT from `display' property at its current position.
3204 Called from handle_stop. */
3205
3206static enum prop_handled
3207handle_display_prop (it)
3208 struct it *it;
3209{
3210 Lisp_Object prop, object;
3211 struct text_pos *position;
a61b7058 3212 int display_replaced_p = 0;
5f5c8ee5
GM
3213
3214 if (STRINGP (it->string))
3215 {
3216 object = it->string;
3217 position = &it->current.string_pos;
3218 }
3219 else
3220 {
221dd3e7 3221 object = it->w->buffer;
5f5c8ee5
GM
3222 position = &it->current.pos;
3223 }
3224
3225 /* Reset those iterator values set from display property values. */
3226 it->font_height = Qnil;
3227 it->space_width = Qnil;
3228 it->voffset = 0;
3229
3230 /* We don't support recursive `display' properties, i.e. string
3231 values that have a string `display' property, that have a string
3232 `display' property etc. */
3233 if (!it->string_from_display_prop_p)
3234 it->area = TEXT_AREA;
3235
3236 prop = Fget_char_property (make_number (position->charpos),
3237 Qdisplay, object);
3238 if (NILP (prop))
3239 return HANDLED_NORMALLY;
3240
f3751a65 3241 if (CONSP (prop)
12700f40
GM
3242 /* Simple properties. */
3243 && !EQ (XCAR (prop), Qimage)
3244 && !EQ (XCAR (prop), Qspace)
3245 && !EQ (XCAR (prop), Qwhen)
3246 && !EQ (XCAR (prop), Qspace_width)
3247 && !EQ (XCAR (prop), Qheight)
3248 && !EQ (XCAR (prop), Qraise)
3249 /* Marginal area specifications. */
3250 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3251 && !NILP (XCAR (prop)))
5f5c8ee5 3252 {
a61b7058 3253 for (; CONSP (prop); prop = XCDR (prop))
5f5c8ee5 3254 {
a61b7058
GM
3255 if (handle_single_display_prop (it, XCAR (prop), object,
3256 position, display_replaced_p))
3257 display_replaced_p = 1;
5f5c8ee5
GM
3258 }
3259 }
3260 else if (VECTORP (prop))
3261 {
3262 int i;
a61b7058
GM
3263 for (i = 0; i < ASIZE (prop); ++i)
3264 if (handle_single_display_prop (it, AREF (prop, i), object,
3265 position, display_replaced_p))
3266 display_replaced_p = 1;
5f5c8ee5
GM
3267 }
3268 else
3269 {
a61b7058
GM
3270 if (handle_single_display_prop (it, prop, object, position, 0))
3271 display_replaced_p = 1;
5f5c8ee5
GM
3272 }
3273
a61b7058 3274 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
5f5c8ee5
GM
3275}
3276
3277
6c577098 3278/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
3279 at START_POS in OBJECT. */
3280
3281static struct text_pos
3282display_prop_end (it, object, start_pos)
3283 struct it *it;
3284 Lisp_Object object;
3285 struct text_pos start_pos;
3286{
3287 Lisp_Object end;
3288 struct text_pos end_pos;
5f5c8ee5 3289
016b5642
MB
3290 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3291 Qdisplay, object, Qnil);
6c577098
GM
3292 CHARPOS (end_pos) = XFASTINT (end);
3293 if (STRINGP (object))
5f5c8ee5
GM
3294 compute_string_pos (&end_pos, start_pos, it->string);
3295 else
6c577098 3296 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
3297
3298 return end_pos;
3299}
3300
3301
3302/* Set up IT from a single `display' sub-property value PROP. OBJECT
3303 is the object in which the `display' property was found. *POSITION
a61b7058
GM
3304 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3305 means that we previously saw a display sub-property which already
3306 replaced text display with something else, for example an image;
3307 ignore such properties after the first one has been processed.
5f5c8ee5
GM
3308
3309 If PROP is a `space' or `image' sub-property, set *POSITION to the
3310 end position of the `display' property.
3311
4b41cebb 3312 Value is non-zero if something was found which replaces the display
a61b7058 3313 of buffer or string text. */
5f5c8ee5
GM
3314
3315static int
a61b7058
GM
3316handle_single_display_prop (it, prop, object, position,
3317 display_replaced_before_p)
5f5c8ee5
GM
3318 struct it *it;
3319 Lisp_Object prop;
3320 Lisp_Object object;
3321 struct text_pos *position;
a61b7058 3322 int display_replaced_before_p;
5f5c8ee5
GM
3323{
3324 Lisp_Object value;
a61b7058 3325 int replaces_text_display_p = 0;
5f5c8ee5
GM
3326 Lisp_Object form;
3327
d3acf96b 3328 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
4b41cebb 3329 evaluated. If the result is nil, VALUE is ignored. */
5f5c8ee5 3330 form = Qt;
d3acf96b 3331 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5f5c8ee5
GM
3332 {
3333 prop = XCDR (prop);
3334 if (!CONSP (prop))
3335 return 0;
3336 form = XCAR (prop);
3337 prop = XCDR (prop);
5f5c8ee5
GM
3338 }
3339
3340 if (!NILP (form) && !EQ (form, Qt))
3341 {
331379bf 3342 int count = SPECPDL_INDEX ();
5f5c8ee5 3343 struct gcpro gcpro1;
5f5c8ee5 3344
b384d6f8
GM
3345 /* Bind `object' to the object having the `display' property, a
3346 buffer or string. Bind `position' to the position in the
3347 object where the property was found, and `buffer-position'
3348 to the current position in the buffer. */
3349 specbind (Qobject, object);
31ac723b
SM
3350 specbind (Qposition, make_number (CHARPOS (*position)));
3351 specbind (Qbuffer_position,
3352 make_number (STRINGP (object)
3353 ? IT_CHARPOS (*it) : CHARPOS (*position)));
b384d6f8 3354 GCPRO1 (form);
116d6f5c 3355 form = safe_eval (form);
b384d6f8
GM
3356 UNGCPRO;
3357 unbind_to (count, Qnil);
5f5c8ee5 3358 }
2311178e 3359
5f5c8ee5
GM
3360 if (NILP (form))
3361 return 0;
3362
3363 if (CONSP (prop)
3364 && EQ (XCAR (prop), Qheight)
3365 && CONSP (XCDR (prop)))
3366 {
e4093f38 3367 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
5f5c8ee5 3368 return 0;
2311178e 3369
5f5c8ee5
GM
3370 /* `(height HEIGHT)'. */
3371 it->font_height = XCAR (XCDR (prop));
3372 if (!NILP (it->font_height))
3373 {
3374 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3375 int new_height = -1;
3376
3377 if (CONSP (it->font_height)
3378 && (EQ (XCAR (it->font_height), Qplus)
3379 || EQ (XCAR (it->font_height), Qminus))
3380 && CONSP (XCDR (it->font_height))
3381 && INTEGERP (XCAR (XCDR (it->font_height))))
3382 {
3383 /* `(+ N)' or `(- N)' where N is an integer. */
3384 int steps = XINT (XCAR (XCDR (it->font_height)));
3385 if (EQ (XCAR (it->font_height), Qplus))
3386 steps = - steps;
3387 it->face_id = smaller_face (it->f, it->face_id, steps);
3388 }
0d8b31c0 3389 else if (FUNCTIONP (it->font_height))
5f5c8ee5
GM
3390 {
3391 /* Call function with current height as argument.
3392 Value is the new height. */
116d6f5c
GM
3393 Lisp_Object height;
3394 height = safe_call1 (it->font_height,
3395 face->lface[LFACE_HEIGHT_INDEX]);
5f5c8ee5
GM
3396 if (NUMBERP (height))
3397 new_height = XFLOATINT (height);
5f5c8ee5
GM
3398 }
3399 else if (NUMBERP (it->font_height))
3400 {
3401 /* Value is a multiple of the canonical char height. */
3402 struct face *face;
2311178e 3403
5f5c8ee5
GM
3404 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3405 new_height = (XFLOATINT (it->font_height)
3406 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3407 }
3408 else
3409 {
3410 /* Evaluate IT->font_height with `height' bound to the
3411 current specified height to get the new height. */
3412 Lisp_Object value;
331379bf 3413 int count = SPECPDL_INDEX ();
2311178e 3414
5f5c8ee5 3415 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
116d6f5c 3416 value = safe_eval (it->font_height);
5f5c8ee5 3417 unbind_to (count, Qnil);
2311178e 3418
5f5c8ee5
GM
3419 if (NUMBERP (value))
3420 new_height = XFLOATINT (value);
3421 }
2311178e 3422
5f5c8ee5
GM
3423 if (new_height > 0)
3424 it->face_id = face_with_height (it->f, it->face_id, new_height);
3425 }
3426 }
3427 else if (CONSP (prop)
3428 && EQ (XCAR (prop), Qspace_width)
3429 && CONSP (XCDR (prop)))
3430 {
3431 /* `(space_width WIDTH)'. */
e4093f38 3432 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 3433 return 0;
2311178e 3434
5f5c8ee5
GM
3435 value = XCAR (XCDR (prop));
3436 if (NUMBERP (value) && XFLOATINT (value) > 0)
3437 it->space_width = value;
3438 }
3439 else if (CONSP (prop)
3440 && EQ (XCAR (prop), Qraise)
3441 && CONSP (XCDR (prop)))
3442 {
5f5c8ee5 3443 /* `(raise FACTOR)'. */
e4093f38 3444 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 3445 return 0;
2311178e 3446
fb3842a8 3447#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
3448 value = XCAR (XCDR (prop));
3449 if (NUMBERP (value))
3450 {
3451 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3452 it->voffset = - (XFLOATINT (value)
1ab3e082 3453 * (FONT_HEIGHT (face->font)));
5f5c8ee5
GM
3454 }
3455#endif /* HAVE_WINDOW_SYSTEM */
3456 }
3457 else if (!it->string_from_display_prop_p)
3458 {
f3751a65 3459 /* `((margin left-margin) VALUE)' or `((margin right-margin)
4b41cebb 3460 VALUE) or `((margin nil) VALUE)' or VALUE. */
5f5c8ee5
GM
3461 Lisp_Object location, value;
3462 struct text_pos start_pos;
3463 int valid_p;
3464
3465 /* Characters having this form of property are not displayed, so
3466 we have to find the end of the property. */
5f5c8ee5
GM
3467 start_pos = *position;
3468 *position = display_prop_end (it, object, start_pos);
15e26c76 3469 value = Qnil;
5f5c8ee5
GM
3470
3471 /* Let's stop at the new position and assume that all
3472 text properties change there. */
3473 it->stop_charpos = position->charpos;
3474
f3751a65
GM
3475 location = Qunbound;
3476 if (CONSP (prop) && CONSP (XCAR (prop)))
5f5c8ee5 3477 {
f3751a65 3478 Lisp_Object tem;
2311178e 3479
5f5c8ee5 3480 value = XCDR (prop);
f3751a65
GM
3481 if (CONSP (value))
3482 value = XCAR (value);
3483
3484 tem = XCAR (prop);
3485 if (EQ (XCAR (tem), Qmargin)
3486 && (tem = XCDR (tem),
3487 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3488 (NILP (tem)
3489 || EQ (tem, Qleft_margin)
3490 || EQ (tem, Qright_margin))))
3491 location = tem;
5f5c8ee5 3492 }
f3751a65
GM
3493
3494 if (EQ (location, Qunbound))
5f5c8ee5
GM
3495 {
3496 location = Qnil;
3497 value = prop;
3498 }
3499
3500#ifdef HAVE_WINDOW_SYSTEM
fb3842a8 3501 if (FRAME_TERMCAP_P (it->f))
5f5c8ee5
GM
3502 valid_p = STRINGP (value);
3503 else
3504 valid_p = (STRINGP (value)
3505 || (CONSP (value) && EQ (XCAR (value), Qspace))
3506 || valid_image_p (value));
3507#else /* not HAVE_WINDOW_SYSTEM */
3508 valid_p = STRINGP (value);
3509#endif /* not HAVE_WINDOW_SYSTEM */
2311178e 3510
5f5c8ee5
GM
3511 if ((EQ (location, Qleft_margin)
3512 || EQ (location, Qright_margin)
3513 || NILP (location))
a61b7058
GM
3514 && valid_p
3515 && !display_replaced_before_p)
5f5c8ee5 3516 {
a61b7058 3517 replaces_text_display_p = 1;
2311178e 3518
5f5c8ee5
GM
3519 /* Save current settings of IT so that we can restore them
3520 when we are finished with the glyph property value. */
3521 push_it (it);
2311178e 3522
5f5c8ee5
GM
3523 if (NILP (location))
3524 it->area = TEXT_AREA;
3525 else if (EQ (location, Qleft_margin))
3526 it->area = LEFT_MARGIN_AREA;
3527 else
3528 it->area = RIGHT_MARGIN_AREA;
2311178e 3529
5f5c8ee5
GM
3530 if (STRINGP (value))
3531 {
3532 it->string = value;
3533 it->multibyte_p = STRING_MULTIBYTE (it->string);
3534 it->current.overlay_string_index = -1;
3535 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2051c264 3536 it->end_charpos = it->string_nchars = SCHARS (it->string);
5f5c8ee5
GM
3537 it->method = next_element_from_string;
3538 it->stop_charpos = 0;
3539 it->string_from_display_prop_p = 1;
e187cf71
GM
3540 /* Say that we haven't consumed the characters with
3541 `display' property yet. The call to pop_it in
3542 set_iterator_to_next will clean this up. */
3543 *position = start_pos;
5f5c8ee5
GM
3544 }
3545 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3546 {
3547 it->method = next_element_from_stretch;
3548 it->object = value;
3549 it->current.pos = it->position = start_pos;
3550 }
3551#ifdef HAVE_WINDOW_SYSTEM
3552 else
3553 {
3554 it->what = IT_IMAGE;
3555 it->image_id = lookup_image (it->f, value);
3556 it->position = start_pos;
3557 it->object = NILP (object) ? it->w->buffer : object;
3558 it->method = next_element_from_image;
2311178e 3559
02513cdd 3560 /* Say that we haven't consumed the characters with
5f5c8ee5
GM
3561 `display' property yet. The call to pop_it in
3562 set_iterator_to_next will clean this up. */
3563 *position = start_pos;
3564 }
3565#endif /* HAVE_WINDOW_SYSTEM */
3566 }
a21a3943
GM
3567 else
3568 /* Invalid property or property not supported. Restore
3569 the position to what it was before. */
3570 *position = start_pos;
5f5c8ee5
GM
3571 }
3572
a61b7058 3573 return replaces_text_display_p;
5f5c8ee5
GM
3574}
3575
3576
06568bbf
GM
3577/* Check if PROP is a display sub-property value whose text should be
3578 treated as intangible. */
3579
3580static int
3581single_display_prop_intangible_p (prop)
3582 Lisp_Object prop;
3583{
3584 /* Skip over `when FORM'. */
3585 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3586 {
3587 prop = XCDR (prop);
3588 if (!CONSP (prop))
3589 return 0;
3590 prop = XCDR (prop);
3591 }
3592
959bc044
SM
3593 if (STRINGP (prop))
3594 return 1;
3595
06568bbf
GM
3596 if (!CONSP (prop))
3597 return 0;
3598
3599 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3600 we don't need to treat text as intangible. */
3601 if (EQ (XCAR (prop), Qmargin))
3602 {
3603 prop = XCDR (prop);
3604 if (!CONSP (prop))
3605 return 0;
3606
3607 prop = XCDR (prop);
3608 if (!CONSP (prop)
3609 || EQ (XCAR (prop), Qleft_margin)
3610 || EQ (XCAR (prop), Qright_margin))
3611 return 0;
3612 }
2311178e 3613
6cd83a9a
SM
3614 return (CONSP (prop)
3615 && (EQ (XCAR (prop), Qimage)
3616 || EQ (XCAR (prop), Qspace)));
06568bbf
GM
3617}
3618
3619
3620/* Check if PROP is a display property value whose text should be
3621 treated as intangible. */
3622
3623int
3624display_prop_intangible_p (prop)
3625 Lisp_Object prop;
3626{
3627 if (CONSP (prop)
3628 && CONSP (XCAR (prop))
3629 && !EQ (Qmargin, XCAR (XCAR (prop))))
3630 {
3631 /* A list of sub-properties. */
3632 while (CONSP (prop))
3633 {
3634 if (single_display_prop_intangible_p (XCAR (prop)))
3635 return 1;
3636 prop = XCDR (prop);
3637 }
3638 }
3639 else if (VECTORP (prop))
3640 {
3641 /* A vector of sub-properties. */
3642 int i;
a61b7058
GM
3643 for (i = 0; i < ASIZE (prop); ++i)
3644 if (single_display_prop_intangible_p (AREF (prop, i)))
06568bbf
GM
3645 return 1;
3646 }
3647 else
3648 return single_display_prop_intangible_p (prop);
3649
3650 return 0;
3651}
3652
74bd6d65
GM
3653
3654/* Return 1 if PROP is a display sub-property value containing STRING. */
3655
3656static int
3657single_display_prop_string_p (prop, string)
3658 Lisp_Object prop, string;
3659{
74bd6d65
GM
3660 if (EQ (string, prop))
3661 return 1;
2311178e 3662
74bd6d65
GM
3663 /* Skip over `when FORM'. */
3664 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3665 {
3666 prop = XCDR (prop);
3667 if (!CONSP (prop))
3668 return 0;
3669 prop = XCDR (prop);
3670 }
3671
3672 if (CONSP (prop))
3673 /* Skip over `margin LOCATION'. */
3674 if (EQ (XCAR (prop), Qmargin))
3675 {
3676 prop = XCDR (prop);
3677 if (!CONSP (prop))
3678 return 0;
3679
3680 prop = XCDR (prop);
3681 if (!CONSP (prop))
3682 return 0;
3683 }
2311178e 3684
74bd6d65
GM
3685 return CONSP (prop) && EQ (XCAR (prop), string);
3686}
3687
3688
3689/* Return 1 if STRING appears in the `display' property PROP. */
3690
3691static int
3692display_prop_string_p (prop, string)
3693 Lisp_Object prop, string;
3694{
74bd6d65
GM
3695 if (CONSP (prop)
3696 && CONSP (XCAR (prop))
3697 && !EQ (Qmargin, XCAR (XCAR (prop))))
3698 {
3699 /* A list of sub-properties. */
3700 while (CONSP (prop))
3701 {
3702 if (single_display_prop_string_p (XCAR (prop), string))
3703 return 1;
3704 prop = XCDR (prop);
3705 }
3706 }
3707 else if (VECTORP (prop))
3708 {
3709 /* A vector of sub-properties. */
3710 int i;
3711 for (i = 0; i < ASIZE (prop); ++i)
3712 if (single_display_prop_string_p (AREF (prop, i), string))
3713 return 1;
3714 }
3715 else
3716 return single_display_prop_string_p (prop, string);
3717
3718 return 0;
3719}
3720
3721
3722/* Determine from which buffer position in W's buffer STRING comes
3723 from. AROUND_CHARPOS is an approximate position where it could
3724 be from. Value is the buffer position or 0 if it couldn't be
3725 determined.
3726
3727 W's buffer must be current.
3728
3729 This function is necessary because we don't record buffer positions
3730 in glyphs generated from strings (to keep struct glyph small).
3731 This function may only use code that doesn't eval because it is
3732 called asynchronously from note_mouse_highlight. */
3733
3734int
3735string_buffer_position (w, string, around_charpos)
3736 struct window *w;
3737 Lisp_Object string;
3738 int around_charpos;
3739{
74bd6d65
GM
3740 Lisp_Object limit, prop, pos;
3741 const int MAX_DISTANCE = 1000;
3742 int found = 0;
3743
d8731202 3744 pos = make_number (around_charpos);
74bd6d65
GM
3745 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3746 while (!found && !EQ (pos, limit))
3747 {
3748 prop = Fget_char_property (pos, Qdisplay, Qnil);
3749 if (!NILP (prop) && display_prop_string_p (prop, string))
3750 found = 1;
3751 else
134d9283 3752 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
74bd6d65
GM
3753 }
3754
3755 if (!found)
3756 {
d8731202 3757 pos = make_number (around_charpos);
74bd6d65
GM
3758 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3759 while (!found && !EQ (pos, limit))
3760 {
3761 prop = Fget_char_property (pos, Qdisplay, Qnil);
3762 if (!NILP (prop) && display_prop_string_p (prop, string))
3763 found = 1;
3764 else
134d9283
GM
3765 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3766 limit);
74bd6d65
GM
3767 }
3768 }
3769
3770 return found ? XINT (pos) : 0;
3771}
3772
3773
5f5c8ee5 3774\f
260a86a0
KH
3775/***********************************************************************
3776 `composition' property
3777 ***********************************************************************/
3778
3779/* Set up iterator IT from `composition' property at its current
3780 position. Called from handle_stop. */
3781
3782static enum prop_handled
3783handle_composition_prop (it)
3784 struct it *it;
3785{
3786 Lisp_Object prop, string;
3787 int pos, pos_byte, end;
3788 enum prop_handled handled = HANDLED_NORMALLY;
3789
3790 if (STRINGP (it->string))
3791 {
3792 pos = IT_STRING_CHARPOS (*it);
3793 pos_byte = IT_STRING_BYTEPOS (*it);
3794 string = it->string;
3795 }
3796 else
3797 {
3798 pos = IT_CHARPOS (*it);
3799 pos_byte = IT_BYTEPOS (*it);
3800 string = Qnil;
3801 }
3802
3803 /* If there's a valid composition and point is not inside of the
3804 composition (in the case that the composition is from the current
3805 buffer), draw a glyph composed from the composition components. */
3806 if (find_composition (pos, -1, &pos, &end, &prop, string)
3807 && COMPOSITION_VALID_P (pos, end, prop)
3808 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3809 {
3810 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3811
3812 if (id >= 0)
3813 {
3814 it->method = next_element_from_composition;
3815 it->cmp_id = id;
3816 it->cmp_len = COMPOSITION_LENGTH (prop);
3817 /* For a terminal, draw only the first character of the
3818 components. */
3819 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3820 it->len = (STRINGP (it->string)
3821 ? string_char_to_byte (it->string, end)
3822 : CHAR_TO_BYTE (end)) - pos_byte;
3823 it->stop_charpos = end;
3824 handled = HANDLED_RETURN;
3825 }
3826 }
3827
3828 return handled;
3829}
3830
3831
3832\f
5f5c8ee5
GM
3833/***********************************************************************
3834 Overlay strings
3835 ***********************************************************************/
3836
3837/* The following structure is used to record overlay strings for
3838 later sorting in load_overlay_strings. */
3839
3840struct overlay_entry
3841{
2970b9be 3842 Lisp_Object overlay;
5f5c8ee5
GM
3843 Lisp_Object string;
3844 int priority;
3845 int after_string_p;
3846};
3847
3848
3849/* Set up iterator IT from overlay strings at its current position.
3850 Called from handle_stop. */
3851
3852static enum prop_handled
3853handle_overlay_change (it)
3854 struct it *it;
3855{
5a08cbaf 3856 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
2970b9be 3857 return HANDLED_RECOMPUTE_PROPS;
5f5c8ee5 3858 else
2970b9be 3859 return HANDLED_NORMALLY;
5f5c8ee5
GM
3860}
3861
3862
3863/* Set up the next overlay string for delivery by IT, if there is an
3864 overlay string to deliver. Called by set_iterator_to_next when the
3865 end of the current overlay string is reached. If there are more
3866 overlay strings to display, IT->string and
3867 IT->current.overlay_string_index are set appropriately here.
3868 Otherwise IT->string is set to nil. */
2311178e 3869
5f5c8ee5
GM
3870static void
3871next_overlay_string (it)
3872 struct it *it;
3873{
3874 ++it->current.overlay_string_index;
3875 if (it->current.overlay_string_index == it->n_overlay_strings)
3876 {
3877 /* No more overlay strings. Restore IT's settings to what
3878 they were before overlay strings were processed, and
3879 continue to deliver from current_buffer. */
5a08cbaf 3880 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
2311178e 3881
5f5c8ee5
GM
3882 pop_it (it);
3883 xassert (it->stop_charpos >= BEGV
3884 && it->stop_charpos <= it->end_charpos);
3885 it->string = Qnil;
3886 it->current.overlay_string_index = -1;
3887 SET_TEXT_POS (it->current.string_pos, -1, -1);
3888 it->n_overlay_strings = 0;
3889 it->method = next_element_from_buffer;
2970b9be
GM
3890
3891 /* If we're at the end of the buffer, record that we have
3892 processed the overlay strings there already, so that
3893 next_element_from_buffer doesn't try it again. */
3894 if (IT_CHARPOS (*it) >= it->end_charpos)
3895 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf
GM
3896
3897 /* If we have to display `...' for invisible text, set
3898 the iterator up for that. */
3899 if (display_ellipsis_p)
3900 setup_for_ellipsis (it);
5f5c8ee5
GM
3901 }
3902 else
3903 {
3904 /* There are more overlay strings to process. If
3905 IT->current.overlay_string_index has advanced to a position
3906 where we must load IT->overlay_strings with more strings, do
3907 it. */
3908 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2311178e 3909
5f5c8ee5 3910 if (it->current.overlay_string_index && i == 0)
5a08cbaf 3911 load_overlay_strings (it, 0);
5f5c8ee5
GM
3912
3913 /* Initialize IT to deliver display elements from the overlay
3914 string. */
3915 it->string = it->overlay_strings[i];
3916 it->multibyte_p = STRING_MULTIBYTE (it->string);
3917 SET_TEXT_POS (it->current.string_pos, 0, 0);
3918 it->method = next_element_from_string;
3919 it->stop_charpos = 0;
3920 }
2311178e 3921
5f5c8ee5
GM
3922 CHECK_IT (it);
3923}
3924
3925
3926/* Compare two overlay_entry structures E1 and E2. Used as a
3927 comparison function for qsort in load_overlay_strings. Overlay
3928 strings for the same position are sorted so that
3929
2970b9be
GM
3930 1. All after-strings come in front of before-strings, except
3931 when they come from the same overlay.
2311178e 3932
5f5c8ee5
GM
3933 2. Within after-strings, strings are sorted so that overlay strings
3934 from overlays with higher priorities come first.
3935
3936 2. Within before-strings, strings are sorted so that overlay
3937 strings from overlays with higher priorities come last.
3938
3939 Value is analogous to strcmp. */
3940
2311178e 3941
5f5c8ee5
GM
3942static int
3943compare_overlay_entries (e1, e2)
3944 void *e1, *e2;
3945{
3946 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3947 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3948 int result;
3949
3950 if (entry1->after_string_p != entry2->after_string_p)
2970b9be
GM
3951 {
3952 /* Let after-strings appear in front of before-strings if
3953 they come from different overlays. */
3954 if (EQ (entry1->overlay, entry2->overlay))
3955 result = entry1->after_string_p ? 1 : -1;
3956 else
3957 result = entry1->after_string_p ? -1 : 1;
3958 }
5f5c8ee5
GM
3959 else if (entry1->after_string_p)
3960 /* After-strings sorted in order of decreasing priority. */
3961 result = entry2->priority - entry1->priority;
3962 else
3963 /* Before-strings sorted in order of increasing priority. */
3964 result = entry1->priority - entry2->priority;
3965
3966 return result;
3967}
3968
3969
3970/* Load the vector IT->overlay_strings with overlay strings from IT's
5a08cbaf
GM
3971 current buffer position, or from CHARPOS if that is > 0. Set
3972 IT->n_overlays to the total number of overlay strings found.
5f5c8ee5
GM
3973
3974 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3975 a time. On entry into load_overlay_strings,
3976 IT->current.overlay_string_index gives the number of overlay
3977 strings that have already been loaded by previous calls to this
3978 function.
3979
2970b9be
GM
3980 IT->add_overlay_start contains an additional overlay start
3981 position to consider for taking overlay strings from, if non-zero.
3982 This position comes into play when the overlay has an `invisible'
3983 property, and both before and after-strings. When we've skipped to
3984 the end of the overlay, because of its `invisible' property, we
3985 nevertheless want its before-string to appear.
3986 IT->add_overlay_start will contain the overlay start position
3987 in this case.
3988
5f5c8ee5
GM
3989 Overlay strings are sorted so that after-string strings come in
3990 front of before-string strings. Within before and after-strings,
3991 strings are sorted by overlay priority. See also function
3992 compare_overlay_entries. */
2311178e 3993
5f5c8ee5 3994static void
5a08cbaf 3995load_overlay_strings (it, charpos)
5f5c8ee5 3996 struct it *it;
5a08cbaf 3997 int charpos;
5f5c8ee5
GM
3998{
3999 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
b7253a3e
SM
4000 Lisp_Object overlay, window, str, invisible;
4001 struct Lisp_Overlay *ov;
5f5c8ee5
GM
4002 int start, end;
4003 int size = 20;
cafafe0b 4004 int n = 0, i, j, invis_p;
5f5c8ee5
GM
4005 struct overlay_entry *entries
4006 = (struct overlay_entry *) alloca (size * sizeof *entries);
5a08cbaf
GM
4007
4008 if (charpos <= 0)
4009 charpos = IT_CHARPOS (*it);
5f5c8ee5
GM
4010
4011 /* Append the overlay string STRING of overlay OVERLAY to vector
4012 `entries' which has size `size' and currently contains `n'
4013 elements. AFTER_P non-zero means STRING is an after-string of
4014 OVERLAY. */
4015#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4016 do \
4017 { \
4018 Lisp_Object priority; \
4019 \
4020 if (n == size) \
4021 { \
4022 int new_size = 2 * size; \
4023 struct overlay_entry *old = entries; \
4024 entries = \
4025 (struct overlay_entry *) alloca (new_size \
4026 * sizeof *entries); \
4027 bcopy (old, entries, size * sizeof *entries); \
4028 size = new_size; \
4029 } \
4030 \
4031 entries[n].string = (STRING); \
2970b9be 4032 entries[n].overlay = (OVERLAY); \
5f5c8ee5 4033 priority = Foverlay_get ((OVERLAY), Qpriority); \
2970b9be 4034 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5f5c8ee5
GM
4035 entries[n].after_string_p = (AFTER_P); \
4036 ++n; \
4037 } \
4038 while (0)
4039
4040 /* Process overlay before the overlay center. */
b7253a3e 4041 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5f5c8ee5 4042 {
b7253a3e 4043 XSETMISC (overlay, ov);
5f5c8ee5
GM
4044 xassert (OVERLAYP (overlay));
4045 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4046 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2311178e 4047
cafafe0b 4048 if (end < charpos)
5f5c8ee5
GM
4049 break;
4050
4051 /* Skip this overlay if it doesn't start or end at IT's current
4052 position. */
cafafe0b 4053 if (end != charpos && start != charpos)
5f5c8ee5 4054 continue;
2311178e 4055
5f5c8ee5
GM
4056 /* Skip this overlay if it doesn't apply to IT->w. */
4057 window = Foverlay_get (overlay, Qwindow);
4058 if (WINDOWP (window) && XWINDOW (window) != it->w)
4059 continue;
4060
cafafe0b
GM
4061 /* If the text ``under'' the overlay is invisible, both before-
4062 and after-strings from this overlay are visible; start and
4063 end position are indistinguishable. */
4064 invisible = Foverlay_get (overlay, Qinvisible);
4065 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4066
5f5c8ee5 4067 /* If overlay has a non-empty before-string, record it. */
cafafe0b 4068 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 4069 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 4070 && SCHARS (str))
5f5c8ee5 4071 RECORD_OVERLAY_STRING (overlay, str, 0);
2311178e 4072
5f5c8ee5 4073 /* If overlay has a non-empty after-string, record it. */
cafafe0b 4074 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 4075 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 4076 && SCHARS (str))
5f5c8ee5
GM
4077 RECORD_OVERLAY_STRING (overlay, str, 1);
4078 }
2311178e 4079
5f5c8ee5 4080 /* Process overlays after the overlay center. */
b7253a3e 4081 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5f5c8ee5 4082 {
b7253a3e 4083 XSETMISC (overlay, ov);
5f5c8ee5
GM
4084 xassert (OVERLAYP (overlay));
4085 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4086 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4087
cafafe0b 4088 if (start > charpos)
5f5c8ee5 4089 break;
2311178e 4090
5f5c8ee5
GM
4091 /* Skip this overlay if it doesn't start or end at IT's current
4092 position. */
cafafe0b 4093 if (end != charpos && start != charpos)
5f5c8ee5
GM
4094 continue;
4095
4096 /* Skip this overlay if it doesn't apply to IT->w. */
4097 window = Foverlay_get (overlay, Qwindow);
4098 if (WINDOWP (window) && XWINDOW (window) != it->w)
4099 continue;
2311178e 4100
cafafe0b
GM
4101 /* If the text ``under'' the overlay is invisible, it has a zero
4102 dimension, and both before- and after-strings apply. */
4103 invisible = Foverlay_get (overlay, Qinvisible);
4104 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4105
5f5c8ee5 4106 /* If overlay has a non-empty before-string, record it. */
cafafe0b 4107 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5 4108 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2051c264 4109 && SCHARS (str))
5f5c8ee5 4110 RECORD_OVERLAY_STRING (overlay, str, 0);
2311178e 4111
5f5c8ee5 4112 /* If overlay has a non-empty after-string, record it. */
cafafe0b 4113 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5 4114 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2051c264 4115 && SCHARS (str))
5f5c8ee5
GM
4116 RECORD_OVERLAY_STRING (overlay, str, 1);
4117 }
4118
4119#undef RECORD_OVERLAY_STRING
2311178e 4120
5f5c8ee5 4121 /* Sort entries. */
cafafe0b 4122 if (n > 1)
2970b9be 4123 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5f5c8ee5
GM
4124
4125 /* Record the total number of strings to process. */
4126 it->n_overlay_strings = n;
4127
4128 /* IT->current.overlay_string_index is the number of overlay strings
4129 that have already been consumed by IT. Copy some of the
4130 remaining overlay strings to IT->overlay_strings. */
4131 i = 0;
4132 j = it->current.overlay_string_index;
4133 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4134 it->overlay_strings[i++] = entries[j++].string;
2970b9be 4135
5f5c8ee5
GM
4136 CHECK_IT (it);
4137}
4138
4139
4140/* Get the first chunk of overlay strings at IT's current buffer
5a08cbaf
GM
4141 position, or at CHARPOS if that is > 0. Value is non-zero if at
4142 least one overlay string was found. */
5f5c8ee5
GM
4143
4144static int
5a08cbaf 4145get_overlay_strings (it, charpos)
5f5c8ee5 4146 struct it *it;
5a08cbaf 4147 int charpos;
5f5c8ee5
GM
4148{
4149 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4150 process. This fills IT->overlay_strings with strings, and sets
4151 IT->n_overlay_strings to the total number of strings to process.
4152 IT->pos.overlay_string_index has to be set temporarily to zero
4153 because load_overlay_strings needs this; it must be set to -1
4154 when no overlay strings are found because a zero value would
4155 indicate a position in the first overlay string. */
4156 it->current.overlay_string_index = 0;
5a08cbaf 4157 load_overlay_strings (it, charpos);
5f5c8ee5
GM
4158
4159 /* If we found overlay strings, set up IT to deliver display
4160 elements from the first one. Otherwise set up IT to deliver
4161 from current_buffer. */
4162 if (it->n_overlay_strings)
4163 {
4164 /* Make sure we know settings in current_buffer, so that we can
4165 restore meaningful values when we're done with the overlay
4166 strings. */
4167 compute_stop_pos (it);
4168 xassert (it->face_id >= 0);
2311178e 4169
5f5c8ee5
GM
4170 /* Save IT's settings. They are restored after all overlay
4171 strings have been processed. */
4172 xassert (it->sp == 0);
4173 push_it (it);
4174
4175 /* Set up IT to deliver display elements from the first overlay
4176 string. */
4177 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5f5c8ee5 4178 it->string = it->overlay_strings[0];
b2046df8 4179 it->stop_charpos = 0;
5f5c8ee5 4180 xassert (STRINGP (it->string));
2051c264 4181 it->end_charpos = SCHARS (it->string);
d5db4077 4182 it->multibyte_p = STRING_MULTIBYTE (it->string);
5f5c8ee5
GM
4183 it->method = next_element_from_string;
4184 }
4185 else
4186 {
4187 it->string = Qnil;
4188 it->current.overlay_string_index = -1;
4189 it->method = next_element_from_buffer;
4190 }
4191
4192 CHECK_IT (it);
4193
4194 /* Value is non-zero if we found at least one overlay string. */
4195 return STRINGP (it->string);
4196}
4197
4198
4199\f
4200/***********************************************************************
4201 Saving and restoring state
4202 ***********************************************************************/
4203
4204/* Save current settings of IT on IT->stack. Called, for example,
4205 before setting up IT for an overlay string, to be able to restore
4206 IT's settings to what they were after the overlay string has been
4207 processed. */
4208
4209static void
4210push_it (it)
4211 struct it *it;
4212{
4213 struct iterator_stack_entry *p;
2311178e 4214
5f5c8ee5
GM
4215 xassert (it->sp < 2);
4216 p = it->stack + it->sp;
4217
4218 p->stop_charpos = it->stop_charpos;
4219 xassert (it->face_id >= 0);
4220 p->face_id = it->face_id;
4221 p->string = it->string;
4222 p->pos = it->current;
4223 p->end_charpos = it->end_charpos;
4224 p->string_nchars = it->string_nchars;
4225 p->area = it->area;
4226 p->multibyte_p = it->multibyte_p;
4227 p->space_width = it->space_width;
4228 p->font_height = it->font_height;
4229 p->voffset = it->voffset;
4230 p->string_from_display_prop_p = it->string_from_display_prop_p;
5a08cbaf 4231 p->display_ellipsis_p = 0;
5f5c8ee5
GM
4232 ++it->sp;
4233}
4234
4235
4236/* Restore IT's settings from IT->stack. Called, for example, when no
4237 more overlay strings must be processed, and we return to delivering
4238 display elements from a buffer, or when the end of a string from a
4239 `display' property is reached and we return to delivering display
4240 elements from an overlay string, or from a buffer. */
4241
4242static void
4243pop_it (it)
4244 struct it *it;
4245{
4246 struct iterator_stack_entry *p;
2311178e 4247
5f5c8ee5
GM
4248 xassert (it->sp > 0);
4249 --it->sp;
4250 p = it->stack + it->sp;
4251 it->stop_charpos = p->stop_charpos;
4252 it->face_id = p->face_id;
4253 it->string = p->string;
4254 it->current = p->pos;
4255 it->end_charpos = p->end_charpos;
4256 it->string_nchars = p->string_nchars;
4257 it->area = p->area;
4258 it->multibyte_p = p->multibyte_p;
4259 it->space_width = p->space_width;
4260 it->font_height = p->font_height;
4261 it->voffset = p->voffset;
4262 it->string_from_display_prop_p = p->string_from_display_prop_p;
4263}
4264
4265
4266\f
4267/***********************************************************************
4268 Moving over lines
4269 ***********************************************************************/
4270
4271/* Set IT's current position to the previous line start. */
4272
4273static void
4274back_to_previous_line_start (it)
4275 struct it *it;
4276{
4277 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4278 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4279}
4280
4281
cafafe0b 4282/* Move IT to the next line start.
2311178e 4283
cafafe0b
GM
4284 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4285 we skipped over part of the text (as opposed to moving the iterator
4286 continuously over the text). Otherwise, don't change the value
4287 of *SKIPPED_P.
2311178e 4288
cafafe0b
GM
4289 Newlines may come from buffer text, overlay strings, or strings
4290 displayed via the `display' property. That's the reason we can't
0fd37545
GM
4291 simply use find_next_newline_no_quit.
4292
4293 Note that this function may not skip over invisible text that is so
4294 because of text properties and immediately follows a newline. If
4295 it would, function reseat_at_next_visible_line_start, when called
4296 from set_iterator_to_next, would effectively make invisible
4297 characters following a newline part of the wrong glyph row, which
4298 leads to wrong cursor motion. */
5f5c8ee5 4299
cafafe0b
GM
4300static int
4301forward_to_next_line_start (it, skipped_p)
5f5c8ee5 4302 struct it *it;
cafafe0b 4303 int *skipped_p;
5f5c8ee5 4304{
54918e2b 4305 int old_selective, newline_found_p, n;
cafafe0b
GM
4306 const int MAX_NEWLINE_DISTANCE = 500;
4307
0fd37545
GM
4308 /* If already on a newline, just consume it to avoid unintended
4309 skipping over invisible text below. */
51695746
GM
4310 if (it->what == IT_CHARACTER
4311 && it->c == '\n'
4312 && CHARPOS (it->position) == IT_CHARPOS (*it))
0fd37545
GM
4313 {
4314 set_iterator_to_next (it, 0);
a77dc1ec 4315 it->c = 0;
0fd37545
GM
4316 return 1;
4317 }
4318
54918e2b 4319 /* Don't handle selective display in the following. It's (a)
0fd37545
GM
4320 unnecessary because it's done by the caller, and (b) leads to an
4321 infinite recursion because next_element_from_ellipsis indirectly
4322 calls this function. */
54918e2b
GM
4323 old_selective = it->selective;
4324 it->selective = 0;
4325
cafafe0b
GM
4326 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4327 from buffer text. */
3aec8722
GM
4328 for (n = newline_found_p = 0;
4329 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4330 n += STRINGP (it->string) ? 0 : 1)
cafafe0b 4331 {
8692ca92 4332 if (!get_next_display_element (it))
d43be70c 4333 return 0;
d02f1cb8 4334 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
cafafe0b 4335 set_iterator_to_next (it, 0);
cafafe0b
GM
4336 }
4337
4338 /* If we didn't find a newline near enough, see if we can use a
4339 short-cut. */
db0bb807 4340 if (!newline_found_p)
cafafe0b
GM
4341 {
4342 int start = IT_CHARPOS (*it);
4343 int limit = find_next_newline_no_quit (start, 1);
4344 Lisp_Object pos;
4345
4346 xassert (!STRINGP (it->string));
4347
4348 /* If there isn't any `display' property in sight, and no
4349 overlays, we can just use the position of the newline in
4350 buffer text. */
4351 if (it->stop_charpos >= limit
4352 || ((pos = Fnext_single_property_change (make_number (start),
4353 Qdisplay,
4354 Qnil, make_number (limit)),
4355 NILP (pos))
4356 && next_overlay_change (start) == ZV))
4357 {
4358 IT_CHARPOS (*it) = limit;
4359 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4360 *skipped_p = newline_found_p = 1;
4361 }
4362 else
4363 {
4364 while (get_next_display_element (it)
4365 && !newline_found_p)
4366 {
4367 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4368 set_iterator_to_next (it, 0);
4369 }
4370 }
4371 }
4372
54918e2b 4373 it->selective = old_selective;
cafafe0b 4374 return newline_found_p;
5f5c8ee5
GM
4375}
4376
4377
4378/* Set IT's current position to the previous visible line start. Skip
4379 invisible text that is so either due to text properties or due to
4380 selective display. Caution: this does not change IT->current_x and
4381 IT->hpos. */
4382
4383static void
4384back_to_previous_visible_line_start (it)
4385 struct it *it;
4386{
4387 int visible_p = 0;
4388
4389 /* Go back one newline if not on BEGV already. */
4390 if (IT_CHARPOS (*it) > BEGV)
4391 back_to_previous_line_start (it);
4392
4393 /* Move over lines that are invisible because of selective display
4394 or text properties. */
4395 while (IT_CHARPOS (*it) > BEGV
4396 && !visible_p)
4397 {
4398 visible_p = 1;
4399
4400 /* If selective > 0, then lines indented more than that values
4401 are invisible. */
4402 if (it->selective > 0
4403 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
a67e162b 4404 (double) it->selective)) /* iftc */
5f5c8ee5 4405 visible_p = 0;
2311178e 4406 else
5f5c8ee5
GM
4407 {
4408 Lisp_Object prop;
4409
6fc556fd
KR
4410 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4411 Qinvisible, it->window);
5f5c8ee5
GM
4412 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4413 visible_p = 0;
4414 }
5f5c8ee5
GM
4415
4416 /* Back one more newline if the current one is invisible. */
4417 if (!visible_p)
4418 back_to_previous_line_start (it);
4419 }
4420
4421 xassert (IT_CHARPOS (*it) >= BEGV);
4422 xassert (IT_CHARPOS (*it) == BEGV
4423 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4424 CHECK_IT (it);
4425}
4426
4427
4428/* Reseat iterator IT at the previous visible line start. Skip
4429 invisible text that is so either due to text properties or due to
4430 selective display. At the end, update IT's overlay information,
4431 face information etc. */
4432
4433static void
4434reseat_at_previous_visible_line_start (it)
4435 struct it *it;
4436{
4437 back_to_previous_visible_line_start (it);
4438 reseat (it, it->current.pos, 1);
4439 CHECK_IT (it);
4440}
4441
4442
4443/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
4444 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4445 preceding the line start. Skip over invisible text that is so
4446 because of selective display. Compute faces, overlays etc at the
4447 new position. Note that this function does not skip over text that
4448 is invisible because of text properties. */
5f5c8ee5
GM
4449
4450static void
312246d1 4451reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 4452 struct it *it;
312246d1 4453 int on_newline_p;
5f5c8ee5 4454{
cafafe0b
GM
4455 int newline_found_p, skipped_p = 0;
4456
4457 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4458
4459 /* Skip over lines that are invisible because they are indented
4460 more than the value of IT->selective. */
4461 if (it->selective > 0)
4462 while (IT_CHARPOS (*it) < ZV
a77dc1ec 4463 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
a67e162b 4464 (double) it->selective)) /* iftc */
a77dc1ec
GM
4465 {
4466 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4467 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4468 }
cafafe0b
GM
4469
4470 /* Position on the newline if that's what's requested. */
4471 if (on_newline_p && newline_found_p)
5f5c8ee5 4472 {
cafafe0b 4473 if (STRINGP (it->string))
5f5c8ee5 4474 {
cafafe0b
GM
4475 if (IT_STRING_CHARPOS (*it) > 0)
4476 {
4477 --IT_STRING_CHARPOS (*it);
4478 --IT_STRING_BYTEPOS (*it);
4479 }
5f5c8ee5 4480 }
cafafe0b 4481 else if (IT_CHARPOS (*it) > BEGV)
312246d1
GM
4482 {
4483 --IT_CHARPOS (*it);
cafafe0b
GM
4484 --IT_BYTEPOS (*it);
4485 reseat (it, it->current.pos, 0);
312246d1 4486 }
5f5c8ee5 4487 }
cafafe0b
GM
4488 else if (skipped_p)
4489 reseat (it, it->current.pos, 0);
2311178e 4490
5f5c8ee5
GM
4491 CHECK_IT (it);
4492}
4493
4494
4495\f
4496/***********************************************************************
4497 Changing an iterator's position
4498***********************************************************************/
4499
4500/* Change IT's current position to POS in current_buffer. If FORCE_P
4501 is non-zero, always check for text properties at the new position.
4502 Otherwise, text properties are only looked up if POS >=
4503 IT->check_charpos of a property. */
4504
4505static void
4506reseat (it, pos, force_p)
4507 struct it *it;
4508 struct text_pos pos;
4509 int force_p;
4510{
4511 int original_pos = IT_CHARPOS (*it);
4512
4513 reseat_1 (it, pos, 0);
4514
4515 /* Determine where to check text properties. Avoid doing it
4516 where possible because text property lookup is very expensive. */
4517 if (force_p
4518 || CHARPOS (pos) > it->stop_charpos
4519 || CHARPOS (pos) < original_pos)
4520 handle_stop (it);
4521
4522 CHECK_IT (it);
4523}
4524
4525
4526/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4527 IT->stop_pos to POS, also. */
4528
4529static void
4530reseat_1 (it, pos, set_stop_p)
4531 struct it *it;
4532 struct text_pos pos;
4533 int set_stop_p;
4534{
4535 /* Don't call this function when scanning a C string. */
4536 xassert (it->s == NULL);
4537
4538 /* POS must be a reasonable value. */
4539 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4540
4541 it->current.pos = it->position = pos;
4542 XSETBUFFER (it->object, current_buffer);
78c663d8 4543 it->end_charpos = ZV;
5f5c8ee5
GM
4544 it->dpvec = NULL;
4545 it->current.dpvec_index = -1;
4546 it->current.overlay_string_index = -1;
4547 IT_STRING_CHARPOS (*it) = -1;
4548 IT_STRING_BYTEPOS (*it) = -1;
4549 it->string = Qnil;
4550 it->method = next_element_from_buffer;
5905025c
RS
4551 /* RMS: I added this to fix a bug in move_it_vertically_backward
4552 where it->area continued to relate to the starting point
4553 for the backward motion. Bug report from
4554 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4555 However, I am not sure whether reseat still does the right thing
4556 in general after this change. */
4557 it->area = TEXT_AREA;
06fd3792 4558 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5f5c8ee5 4559 it->sp = 0;
4aad61f8 4560 it->face_before_selective_p = 0;
5f5c8ee5
GM
4561
4562 if (set_stop_p)
4563 it->stop_charpos = CHARPOS (pos);
4564}
4565
4566
4567/* Set up IT for displaying a string, starting at CHARPOS in window W.
4568 If S is non-null, it is a C string to iterate over. Otherwise,
4569 STRING gives a Lisp string to iterate over.
2311178e 4570
5f5c8ee5
GM
4571 If PRECISION > 0, don't return more then PRECISION number of
4572 characters from the string.
4573
4574 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4575 characters have been returned. FIELD_WIDTH < 0 means an infinite
4576 field width.
4577
4578 MULTIBYTE = 0 means disable processing of multibyte characters,
4579 MULTIBYTE > 0 means enable it,
4580 MULTIBYTE < 0 means use IT->multibyte_p.
4581
4582 IT must be initialized via a prior call to init_iterator before
4583 calling this function. */
4584
4585static void
4586reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4587 struct it *it;
4588 unsigned char *s;
4589 Lisp_Object string;
4590 int charpos;
4591 int precision, field_width, multibyte;
4592{
4593 /* No region in strings. */
4594 it->region_beg_charpos = it->region_end_charpos = -1;
4595
4596 /* No text property checks performed by default, but see below. */
4597 it->stop_charpos = -1;
4598
4599 /* Set iterator position and end position. */
4600 bzero (&it->current, sizeof it->current);
4601 it->current.overlay_string_index = -1;
4602 it->current.dpvec_index = -1;
5f5c8ee5 4603 xassert (charpos >= 0);
2311178e 4604
e719f5ae
GM
4605 /* If STRING is specified, use its multibyteness, otherwise use the
4606 setting of MULTIBYTE, if specified. */
cabf45da 4607 if (multibyte >= 0)
5f5c8ee5 4608 it->multibyte_p = multibyte > 0;
2311178e 4609
5f5c8ee5
GM
4610 if (s == NULL)
4611 {
4612 xassert (STRINGP (string));
4613 it->string = string;
4614 it->s = NULL;
2051c264 4615 it->end_charpos = it->string_nchars = SCHARS (string);
5f5c8ee5
GM
4616 it->method = next_element_from_string;
4617 it->current.string_pos = string_pos (charpos, string);
4618 }
4619 else
4620 {
4621 it->s = s;
4622 it->string = Qnil;
4623
4624 /* Note that we use IT->current.pos, not it->current.string_pos,
4625 for displaying C strings. */
4626 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4627 if (it->multibyte_p)
4628 {
4629 it->current.pos = c_string_pos (charpos, s, 1);
4630 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4631 }
4632 else
4633 {
4634 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4635 it->end_charpos = it->string_nchars = strlen (s);
4636 }
2311178e 4637
5f5c8ee5
GM
4638 it->method = next_element_from_c_string;
4639 }
4640
4641 /* PRECISION > 0 means don't return more than PRECISION characters
4642 from the string. */
4643 if (precision > 0 && it->end_charpos - charpos > precision)
4644 it->end_charpos = it->string_nchars = charpos + precision;
4645
4646 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4647 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4648 FIELD_WIDTH < 0 means infinite field width. This is useful for
4649 padding with `-' at the end of a mode line. */
4650 if (field_width < 0)
4651 field_width = INFINITY;
4652 if (field_width > it->end_charpos - charpos)
4653 it->end_charpos = charpos + field_width;
4654
4655 /* Use the standard display table for displaying strings. */
4656 if (DISP_TABLE_P (Vstandard_display_table))
4657 it->dp = XCHAR_TABLE (Vstandard_display_table);
4658
4659 it->stop_charpos = charpos;
4660 CHECK_IT (it);
4661}
4662
4663
4664\f
4665/***********************************************************************
4666 Iteration
4667 ***********************************************************************/
4668
4669/* Load IT's display element fields with information about the next
4670 display element from the current position of IT. Value is zero if
4671 end of buffer (or C string) is reached. */
4672
4673int
4674get_next_display_element (it)
4675 struct it *it;
4676{
7d0393cf 4677 /* Non-zero means that we found a display element. Zero means that
5f5c8ee5
GM
4678 we hit the end of what we iterate over. Performance note: the
4679 function pointer `method' used here turns out to be faster than
4680 using a sequence of if-statements. */
4681 int success_p = (*it->method) (it);
5f5c8ee5
GM
4682
4683 if (it->what == IT_CHARACTER)
4684 {
4685 /* Map via display table or translate control characters.
4686 IT->c, IT->len etc. have been set to the next character by
4687 the function call above. If we have a display table, and it
4688 contains an entry for IT->c, translate it. Don't do this if
4689 IT->c itself comes from a display table, otherwise we could
4690 end up in an infinite recursion. (An alternative could be to
4691 count the recursion depth of this function and signal an
4692 error when a certain maximum depth is reached.) Is it worth
4693 it? */
4694 if (success_p && it->dpvec == NULL)
4695 {
4696 Lisp_Object dv;
4697
4698 if (it->dp
4699 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4700 VECTORP (dv)))
4701 {
4702 struct Lisp_Vector *v = XVECTOR (dv);
4703
4704 /* Return the first character from the display table
4705 entry, if not empty. If empty, don't display the
4706 current character. */
4707 if (v->size)
4708 {
4709 it->dpvec_char_len = it->len;
4710 it->dpvec = v->contents;
4711 it->dpend = v->contents + v->size;
4712 it->current.dpvec_index = 0;
4713 it->method = next_element_from_display_vector;
7eee47cc
GM
4714 success_p = get_next_display_element (it);
4715 }
4716 else
4717 {
4718 set_iterator_to_next (it, 0);
4719 success_p = get_next_display_element (it);
5f5c8ee5 4720 }
5f5c8ee5
GM
4721 }
4722
4723 /* Translate control characters into `\003' or `^C' form.
4724 Control characters coming from a display table entry are
4725 currently not translated because we use IT->dpvec to hold
4726 the translation. This could easily be changed but I
197516c2
KH
4727 don't believe that it is worth doing.
4728
fa831cf8
KH
4729 If it->multibyte_p is nonzero, eight-bit characters and
4730 non-printable multibyte characters are also translated to
4731 octal form.
4732
4733 If it->multibyte_p is zero, eight-bit characters that
4734 don't have corresponding multibyte char code are also
4735 translated to octal form. */
ba197fe6 4736 else if ((it->c < ' '
5f5c8ee5 4737 && (it->area != TEXT_AREA
c6e89d6c 4738 || (it->c != '\n' && it->c != '\t')))
fa831cf8
KH
4739 || (it->multibyte_p
4740 ? ((it->c >= 127
4741 && it->len == 1)
4742 || !CHAR_PRINTABLE_P (it->c))
ba197fe6 4743 : (it->c >= 127
fa831cf8 4744 && it->c == unibyte_char_to_multibyte (it->c))))
5f5c8ee5
GM
4745 {
4746 /* IT->c is a control character which must be displayed
4747 either as '\003' or as `^C' where the '\\' and '^'
4748 can be defined in the display table. Fill
4749 IT->ctl_chars with glyphs for what we have to
4750 display. Then, set IT->dpvec to these glyphs. */
4751 GLYPH g;
4752
54c85a23 4753 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
4754 {
4755 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4756 if (it->dp
4757 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4758 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4759 g = XINT (DISP_CTRL_GLYPH (it->dp));
4760 else
4761 g = FAST_MAKE_GLYPH ('^', 0);
4762 XSETINT (it->ctl_chars[0], g);
4763
4764 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4765 XSETINT (it->ctl_chars[1], g);
4766
4767 /* Set up IT->dpvec and return first character from it. */
4768 it->dpvec_char_len = it->len;
4769 it->dpvec = it->ctl_chars;
4770 it->dpend = it->dpvec + 2;
4771 it->current.dpvec_index = 0;
4772 it->method = next_element_from_display_vector;
4773 get_next_display_element (it);
4774 }
4775 else
4776 {
260a86a0 4777 unsigned char str[MAX_MULTIBYTE_LENGTH];
c5924f47 4778 int len;
197516c2
KH
4779 int i;
4780 GLYPH escape_glyph;
4781
5f5c8ee5
GM
4782 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4783 if (it->dp
4784 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4785 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
197516c2 4786 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5f5c8ee5 4787 else
197516c2
KH
4788 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4789
c5924f47
KH
4790 if (SINGLE_BYTE_CHAR_P (it->c))
4791 str[0] = it->c, len = 1;
4792 else
ea9dd091
GM
4793 {
4794 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4795 if (len < 0)
4796 {
4797 /* It's an invalid character, which
4798 shouldn't happen actually, but due to
4799 bugs it may happen. Let's print the char
4800 as is, there's not much meaningful we can
4801 do with it. */
4802 str[0] = it->c;
4803 str[1] = it->c >> 8;
4804 str[2] = it->c >> 16;
4805 str[3] = it->c >> 24;
4806 len = 4;
4807 }
4808 }
c5924f47 4809
197516c2
KH
4810 for (i = 0; i < len; i++)
4811 {
4812 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4813 /* Insert three more glyphs into IT->ctl_chars for
4814 the octal display of the character. */
2311178e 4815 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
197516c2 4816 XSETINT (it->ctl_chars[i * 4 + 1], g);
2311178e 4817 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
197516c2 4818 XSETINT (it->ctl_chars[i * 4 + 2], g);
2311178e 4819 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
197516c2
KH
4820 XSETINT (it->ctl_chars[i * 4 + 3], g);
4821 }
5f5c8ee5
GM
4822
4823 /* Set up IT->dpvec and return the first character
4824 from it. */
4825 it->dpvec_char_len = it->len;
4826 it->dpvec = it->ctl_chars;
197516c2 4827 it->dpend = it->dpvec + len * 4;
5f5c8ee5
GM
4828 it->current.dpvec_index = 0;
4829 it->method = next_element_from_display_vector;
4830 get_next_display_element (it);
4831 }
4832 }
4833 }
4834
980806b6
KH
4835 /* Adjust face id for a multibyte character. There are no
4836 multibyte character in unibyte text. */
5f5c8ee5
GM
4837 if (it->multibyte_p
4838 && success_p
980806b6 4839 && FRAME_WINDOW_P (it->f))
5f5c8ee5 4840 {
980806b6
KH
4841 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4842 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5f5c8ee5
GM
4843 }
4844 }
4845
4846 /* Is this character the last one of a run of characters with
4847 box? If yes, set IT->end_of_box_run_p to 1. */
4848 if (it->face_box_p
4849 && it->s == NULL)
4850 {
4851 int face_id;
4852 struct face *face;
4853
4854 it->end_of_box_run_p
4855 = ((face_id = face_after_it_pos (it),
4856 face_id != it->face_id)
4857 && (face = FACE_FROM_ID (it->f, face_id),
4858 face->box == FACE_NO_BOX));
4859 }
4860
4861 /* Value is 0 if end of buffer or string reached. */
4862 return success_p;
4863}
4864
4865
4866/* Move IT to the next display element.
4867
cafafe0b
GM
4868 RESEAT_P non-zero means if called on a newline in buffer text,
4869 skip to the next visible line start.
4870
5f5c8ee5
GM
4871 Functions get_next_display_element and set_iterator_to_next are
4872 separate because I find this arrangement easier to handle than a
4873 get_next_display_element function that also increments IT's
4874 position. The way it is we can first look at an iterator's current
4875 display element, decide whether it fits on a line, and if it does,
4876 increment the iterator position. The other way around we probably
4877 would either need a flag indicating whether the iterator has to be
4878 incremented the next time, or we would have to implement a
4879 decrement position function which would not be easy to write. */
4880
4881void
cafafe0b 4882set_iterator_to_next (it, reseat_p)
5f5c8ee5 4883 struct it *it;
cafafe0b 4884 int reseat_p;
5f5c8ee5 4885{
483de32b
GM
4886 /* Reset flags indicating start and end of a sequence of characters
4887 with box. Reset them at the start of this function because
4888 moving the iterator to a new position might set them. */
4889 it->start_of_box_run_p = it->end_of_box_run_p = 0;
2311178e 4890
5f5c8ee5
GM
4891 if (it->method == next_element_from_buffer)
4892 {
4893 /* The current display element of IT is a character from
4894 current_buffer. Advance in the buffer, and maybe skip over
4895 invisible lines that are so because of selective display. */
cafafe0b 4896 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
312246d1 4897 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4898 else
4899 {
4900 xassert (it->len != 0);
4901 IT_BYTEPOS (*it) += it->len;
4902 IT_CHARPOS (*it) += 1;
4903 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4904 }
4905 }
260a86a0
KH
4906 else if (it->method == next_element_from_composition)
4907 {
4908 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4909 if (STRINGP (it->string))
4910 {
4911 IT_STRING_BYTEPOS (*it) += it->len;
4912 IT_STRING_CHARPOS (*it) += it->cmp_len;
4913 it->method = next_element_from_string;
4914 goto consider_string_end;
4915 }
4916 else
4917 {
4918 IT_BYTEPOS (*it) += it->len;
4919 IT_CHARPOS (*it) += it->cmp_len;
4920 it->method = next_element_from_buffer;
4921 }
4922 }
5f5c8ee5
GM
4923 else if (it->method == next_element_from_c_string)
4924 {
4925 /* Current display element of IT is from a C string. */
4926 IT_BYTEPOS (*it) += it->len;
4927 IT_CHARPOS (*it) += 1;
4928 }
4929 else if (it->method == next_element_from_display_vector)
4930 {
4931 /* Current display element of IT is from a display table entry.
4932 Advance in the display table definition. Reset it to null if
4933 end reached, and continue with characters from buffers/
4934 strings. */
4935 ++it->current.dpvec_index;
286bcbc9 4936
980806b6
KH
4937 /* Restore face of the iterator to what they were before the
4938 display vector entry (these entries may contain faces). */
5f5c8ee5 4939 it->face_id = it->saved_face_id;
2311178e 4940
5f5c8ee5
GM
4941 if (it->dpvec + it->current.dpvec_index == it->dpend)
4942 {
4943 if (it->s)
4944 it->method = next_element_from_c_string;
4945 else if (STRINGP (it->string))
4946 it->method = next_element_from_string;
4947 else
4948 it->method = next_element_from_buffer;
4949
4950 it->dpvec = NULL;
4951 it->current.dpvec_index = -1;
4952
312246d1
GM
4953 /* Skip over characters which were displayed via IT->dpvec. */
4954 if (it->dpvec_char_len < 0)
4955 reseat_at_next_visible_line_start (it, 1);
4956 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
4957 {
4958 it->len = it->dpvec_char_len;
cafafe0b 4959 set_iterator_to_next (it, reseat_p);
5f5c8ee5
GM
4960 }
4961 }
4962 }
4963 else if (it->method == next_element_from_string)
4964 {
4965 /* Current display element is a character from a Lisp string. */
4966 xassert (it->s == NULL && STRINGP (it->string));
4967 IT_STRING_BYTEPOS (*it) += it->len;
4968 IT_STRING_CHARPOS (*it) += 1;
2311178e 4969
5f5c8ee5
GM
4970 consider_string_end:
4971
4972 if (it->current.overlay_string_index >= 0)
4973 {
4974 /* IT->string is an overlay string. Advance to the
4975 next, if there is one. */
2051c264 4976 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
4977 next_overlay_string (it);
4978 }
4979 else
4980 {
4981 /* IT->string is not an overlay string. If we reached
4982 its end, and there is something on IT->stack, proceed
4983 with what is on the stack. This can be either another
4984 string, this time an overlay string, or a buffer. */
2051c264 4985 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5f5c8ee5
GM
4986 && it->sp > 0)
4987 {
4988 pop_it (it);
4989 if (!STRINGP (it->string))
4990 it->method = next_element_from_buffer;
b2046df8
GM
4991 else
4992 goto consider_string_end;
5f5c8ee5
GM
4993 }
4994 }
4995 }
4996 else if (it->method == next_element_from_image
4997 || it->method == next_element_from_stretch)
4998 {
4999 /* The position etc with which we have to proceed are on
5000 the stack. The position may be at the end of a string,
5001 if the `display' property takes up the whole string. */
5002 pop_it (it);
5003 it->image_id = 0;
5004 if (STRINGP (it->string))
5005 {
5006 it->method = next_element_from_string;
5007 goto consider_string_end;
5008 }
5009 else
5010 it->method = next_element_from_buffer;
5011 }
5012 else
5013 /* There are no other methods defined, so this should be a bug. */
5014 abort ();
5015
5f5c8ee5
GM
5016 xassert (it->method != next_element_from_string
5017 || (STRINGP (it->string)
5018 && IT_STRING_CHARPOS (*it) >= 0));
5019}
5020
5021
5022/* Load IT's display element fields with information about the next
5023 display element which comes from a display table entry or from the
5024 result of translating a control character to one of the forms `^C'
5025 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5026
5027static int
5028next_element_from_display_vector (it)
5029 struct it *it;
5030{
5031 /* Precondition. */
5032 xassert (it->dpvec && it->current.dpvec_index >= 0);
5033
5034 /* Remember the current face id in case glyphs specify faces.
5035 IT's face is restored in set_iterator_to_next. */
5036 it->saved_face_id = it->face_id;
2311178e 5037
5f5c8ee5
GM
5038 if (INTEGERP (*it->dpvec)
5039 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5040 {
5041 int lface_id;
5042 GLYPH g;
5043
5044 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5045 it->c = FAST_GLYPH_CHAR (g);
c5924f47 5046 it->len = CHAR_BYTES (it->c);
5f5c8ee5
GM
5047
5048 /* The entry may contain a face id to use. Such a face id is
5049 the id of a Lisp face, not a realized face. A face id of
969065c3 5050 zero means no face is specified. */
5f5c8ee5
GM
5051 lface_id = FAST_GLYPH_FACE (g);
5052 if (lface_id)
5053 {
969065c3 5054 /* The function returns -1 if lface_id is invalid. */
5f5c8ee5
GM
5055 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5056 if (face_id >= 0)
969065c3 5057 it->face_id = face_id;
5f5c8ee5
GM
5058 }
5059 }
5060 else
5061 /* Display table entry is invalid. Return a space. */
5062 it->c = ' ', it->len = 1;
5063
5064 /* Don't change position and object of the iterator here. They are
5065 still the values of the character that had this display table
5066 entry or was translated, and that's what we want. */
5067 it->what = IT_CHARACTER;
5068 return 1;
5069}
5070
5071
5072/* Load IT with the next display element from Lisp string IT->string.
5073 IT->current.string_pos is the current position within the string.
5074 If IT->current.overlay_string_index >= 0, the Lisp string is an
5075 overlay string. */
5076
5077static int
5078next_element_from_string (it)
5079 struct it *it;
5080{
5081 struct text_pos position;
5082
5083 xassert (STRINGP (it->string));
5084 xassert (IT_STRING_CHARPOS (*it) >= 0);
5085 position = it->current.string_pos;
5086
5087 /* Time to check for invisible text? */
5088 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5089 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5090 {
5091 handle_stop (it);
5092
5093 /* Since a handler may have changed IT->method, we must
5094 recurse here. */
5095 return get_next_display_element (it);
5096 }
5097
5098 if (it->current.overlay_string_index >= 0)
5099 {
5100 /* Get the next character from an overlay string. In overlay
5101 strings, There is no field width or padding with spaces to
5102 do. */
2051c264 5103 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5f5c8ee5
GM
5104 {
5105 it->what = IT_EOB;
5106 return 0;
5107 }
5108 else if (STRING_MULTIBYTE (it->string))
5109 {
2051c264 5110 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
50f80c2f
KR
5111 const unsigned char *s = (SDATA (it->string)
5112 + IT_STRING_BYTEPOS (*it));
4fdb80f2 5113 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
5114 }
5115 else
5116 {
2051c264 5117 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
5118 it->len = 1;
5119 }
5120 }
5121 else
5122 {
5123 /* Get the next character from a Lisp string that is not an
5124 overlay string. Such strings come from the mode line, for
5125 example. We may have to pad with spaces, or truncate the
5126 string. See also next_element_from_c_string. */
5127 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5128 {
5129 it->what = IT_EOB;
5130 return 0;
5131 }
5132 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5133 {
5134 /* Pad with spaces. */
5135 it->c = ' ', it->len = 1;
5136 CHARPOS (position) = BYTEPOS (position) = -1;
5137 }
5138 else if (STRING_MULTIBYTE (it->string))
5139 {
2051c264 5140 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
50f80c2f
KR
5141 const unsigned char *s = (SDATA (it->string)
5142 + IT_STRING_BYTEPOS (*it));
4fdb80f2 5143 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
5144 }
5145 else
5146 {
2051c264 5147 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5f5c8ee5
GM
5148 it->len = 1;
5149 }
5150 }
5151
5152 /* Record what we have and where it came from. Note that we store a
5153 buffer position in IT->position although it could arguably be a
5154 string position. */
5155 it->what = IT_CHARACTER;
5156 it->object = it->string;
5157 it->position = position;
5158 return 1;
5159}
5160
5161
5162/* Load IT with next display element from C string IT->s.
5163 IT->string_nchars is the maximum number of characters to return
5164 from the string. IT->end_charpos may be greater than
5165 IT->string_nchars when this function is called, in which case we
5166 may have to return padding spaces. Value is zero if end of string
5167 reached, including padding spaces. */
5168
5169static int
5170next_element_from_c_string (it)
5171 struct it *it;
5172{
5173 int success_p = 1;
2311178e 5174
5f5c8ee5
GM
5175 xassert (it->s);
5176 it->what = IT_CHARACTER;
5177 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5178 it->object = Qnil;
2311178e 5179
5f5c8ee5
GM
5180 /* IT's position can be greater IT->string_nchars in case a field
5181 width or precision has been specified when the iterator was
5182 initialized. */
5183 if (IT_CHARPOS (*it) >= it->end_charpos)
5184 {
5185 /* End of the game. */
5186 it->what = IT_EOB;
5187 success_p = 0;
5188 }
5189 else if (IT_CHARPOS (*it) >= it->string_nchars)
5190 {
5191 /* Pad with spaces. */
5192 it->c = ' ', it->len = 1;
5193 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5194 }
5195 else if (it->multibyte_p)
5196 {
5197 /* Implementation note: The calls to strlen apparently aren't a
5198 performance problem because there is no noticeable performance
5199 difference between Emacs running in unibyte or multibyte mode. */
5200 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
5201 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5202 maxlen, &it->len);
5f5c8ee5
GM
5203 }
5204 else
5205 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
2311178e 5206
5f5c8ee5
GM
5207 return success_p;
5208}
5209
5210
5211/* Set up IT to return characters from an ellipsis, if appropriate.
5212 The definition of the ellipsis glyphs may come from a display table
5213 entry. This function Fills IT with the first glyph from the
5214 ellipsis if an ellipsis is to be displayed. */
5215
13f19968 5216static int
5f5c8ee5
GM
5217next_element_from_ellipsis (it)
5218 struct it *it;
5219{
13f19968 5220 if (it->selective_display_ellipsis_p)
5f5c8ee5 5221 {
13f19968
GM
5222 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5223 {
5224 /* Use the display table definition for `...'. Invalid glyphs
5225 will be handled by the method returning elements from dpvec. */
5226 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5227 it->dpvec_char_len = it->len;
5228 it->dpvec = v->contents;
5229 it->dpend = v->contents + v->size;
5230 it->current.dpvec_index = 0;
5231 it->method = next_element_from_display_vector;
5232 }
5233 else
5234 {
5235 /* Use default `...' which is stored in default_invis_vector. */
5236 it->dpvec_char_len = it->len;
5237 it->dpvec = default_invis_vector;
5238 it->dpend = default_invis_vector + 3;
5239 it->current.dpvec_index = 0;
5240 it->method = next_element_from_display_vector;
5241 }
5f5c8ee5 5242 }
13f19968 5243 else
54918e2b 5244 {
4aad61f8
GM
5245 /* The face at the current position may be different from the
5246 face we find after the invisible text. Remember what it
5247 was in IT->saved_face_id, and signal that it's there by
5248 setting face_before_selective_p. */
5249 it->saved_face_id = it->face_id;
54918e2b
GM
5250 it->method = next_element_from_buffer;
5251 reseat_at_next_visible_line_start (it, 1);
4aad61f8 5252 it->face_before_selective_p = 1;
54918e2b 5253 }
2311178e 5254
13f19968 5255 return get_next_display_element (it);
5f5c8ee5
GM
5256}
5257
5258
5259/* Deliver an image display element. The iterator IT is already
5260 filled with image information (done in handle_display_prop). Value
5261 is always 1. */
2311178e 5262
5f5c8ee5
GM
5263
5264static int
5265next_element_from_image (it)
5266 struct it *it;
5267{
5268 it->what = IT_IMAGE;
5269 return 1;
5270}
5271
5272
5273/* Fill iterator IT with next display element from a stretch glyph
5274 property. IT->object is the value of the text property. Value is
5275 always 1. */
5276
5277static int
5278next_element_from_stretch (it)
5279 struct it *it;
5280{
5281 it->what = IT_STRETCH;
5282 return 1;
5283}
5284
5285
5286/* Load IT with the next display element from current_buffer. Value
5287 is zero if end of buffer reached. IT->stop_charpos is the next
5288 position at which to stop and check for text properties or buffer
5289 end. */
5290
5291static int
5292next_element_from_buffer (it)
5293 struct it *it;
5294{
5295 int success_p = 1;
5296
5297 /* Check this assumption, otherwise, we would never enter the
5298 if-statement, below. */
5299 xassert (IT_CHARPOS (*it) >= BEGV
5300 && IT_CHARPOS (*it) <= it->stop_charpos);
5301
5302 if (IT_CHARPOS (*it) >= it->stop_charpos)
5303 {
5304 if (IT_CHARPOS (*it) >= it->end_charpos)
5305 {
5306 int overlay_strings_follow_p;
2311178e 5307
5f5c8ee5
GM
5308 /* End of the game, except when overlay strings follow that
5309 haven't been returned yet. */
5310 if (it->overlay_strings_at_end_processed_p)
5311 overlay_strings_follow_p = 0;
5312 else
5313 {
5314 it->overlay_strings_at_end_processed_p = 1;
5a08cbaf 5315 overlay_strings_follow_p = get_overlay_strings (it, 0);
5f5c8ee5
GM
5316 }
5317
5318 if (overlay_strings_follow_p)
5319 success_p = get_next_display_element (it);
5320 else
5321 {
5322 it->what = IT_EOB;
5323 it->position = it->current.pos;
5324 success_p = 0;
5325 }
5326 }
5327 else
5328 {
5329 handle_stop (it);
5330 return get_next_display_element (it);
5331 }
5332 }
5333 else
5334 {
5335 /* No face changes, overlays etc. in sight, so just return a
5336 character from current_buffer. */
5337 unsigned char *p;
5338
5339 /* Maybe run the redisplay end trigger hook. Performance note:
5340 This doesn't seem to cost measurable time. */
5341 if (it->redisplay_end_trigger_charpos
5342 && it->glyph_row
5343 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5344 run_redisplay_end_trigger_hook (it);
5345
5346 /* Get the next character, maybe multibyte. */
5347 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
260a86a0 5348 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5f5c8ee5
GM
5349 {
5350 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5351 - IT_BYTEPOS (*it));
4fdb80f2 5352 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
5353 }
5354 else
5355 it->c = *p, it->len = 1;
5356
5357 /* Record what we have and where it came from. */
5358 it->what = IT_CHARACTER;;
5359 it->object = it->w->buffer;
5360 it->position = it->current.pos;
5361
5362 /* Normally we return the character found above, except when we
5363 really want to return an ellipsis for selective display. */
5364 if (it->selective)
5365 {
5366 if (it->c == '\n')
5367 {
5368 /* A value of selective > 0 means hide lines indented more
5369 than that number of columns. */
5370 if (it->selective > 0
5371 && IT_CHARPOS (*it) + 1 < ZV
5372 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5373 IT_BYTEPOS (*it) + 1,
a67e162b 5374 (double) it->selective)) /* iftc */
312246d1 5375 {
13f19968 5376 success_p = next_element_from_ellipsis (it);
312246d1
GM
5377 it->dpvec_char_len = -1;
5378 }
5f5c8ee5
GM
5379 }
5380 else if (it->c == '\r' && it->selective == -1)
5381 {
5382 /* A value of selective == -1 means that everything from the
5383 CR to the end of the line is invisible, with maybe an
5384 ellipsis displayed for it. */
13f19968 5385 success_p = next_element_from_ellipsis (it);
312246d1 5386 it->dpvec_char_len = -1;
5f5c8ee5
GM
5387 }
5388 }
5389 }
5390
5391 /* Value is zero if end of buffer reached. */
c880678e 5392 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5f5c8ee5
GM
5393 return success_p;
5394}
5395
2311178e 5396
5f5c8ee5
GM
5397/* Run the redisplay end trigger hook for IT. */
5398
5399static void
5400run_redisplay_end_trigger_hook (it)
5401 struct it *it;
5402{
5403 Lisp_Object args[3];
5404
5405 /* IT->glyph_row should be non-null, i.e. we should be actually
5406 displaying something, or otherwise we should not run the hook. */
5407 xassert (it->glyph_row);
5408
5409 /* Set up hook arguments. */
5410 args[0] = Qredisplay_end_trigger_functions;
5411 args[1] = it->window;
5412 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5413 it->redisplay_end_trigger_charpos = 0;
5414
5415 /* Since we are *trying* to run these functions, don't try to run
5416 them again, even if they get an error. */
5417 it->w->redisplay_end_trigger = Qnil;
5418 Frun_hook_with_args (3, args);
2311178e 5419
5f5c8ee5
GM
5420 /* Notice if it changed the face of the character we are on. */
5421 handle_face_prop (it);
5422}
5423
5424
260a86a0
KH
5425/* Deliver a composition display element. The iterator IT is already
5426 filled with composition information (done in
5427 handle_composition_prop). Value is always 1. */
5428
5429static int
5430next_element_from_composition (it)
5431 struct it *it;
5432{
5433 it->what = IT_COMPOSITION;
5434 it->position = (STRINGP (it->string)
5435 ? it->current.string_pos
5436 : it->current.pos);
5437 return 1;
5438}
5439
5440
5f5c8ee5
GM
5441\f
5442/***********************************************************************
5443 Moving an iterator without producing glyphs
5444 ***********************************************************************/
5445
5446/* Move iterator IT to a specified buffer or X position within one
5447 line on the display without producing glyphs.
5448
c53a1624
RS
5449 OP should be a bit mask including some or all of these bits:
5450 MOVE_TO_X: Stop on reaching x-position TO_X.
5451 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5452 Regardless of OP's value, stop in reaching the end of the display line.
5f5c8ee5 5453
c53a1624
RS
5454 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5455 This means, in particular, that TO_X includes window's horizontal
5456 scroll amount.
5f5c8ee5 5457
c53a1624
RS
5458 The return value has several possible values that
5459 say what condition caused the scan to stop:
5f5c8ee5
GM
5460
5461 MOVE_POS_MATCH_OR_ZV
5462 - when TO_POS or ZV was reached.
2311178e 5463
5f5c8ee5
GM
5464 MOVE_X_REACHED
5465 -when TO_X was reached before TO_POS or ZV were reached.
2311178e 5466
5f5c8ee5
GM
5467 MOVE_LINE_CONTINUED
5468 - when we reached the end of the display area and the line must
5469 be continued.
2311178e 5470
5f5c8ee5
GM
5471 MOVE_LINE_TRUNCATED
5472 - when we reached the end of the display area and the line is
5473 truncated.
5474
5475 MOVE_NEWLINE_OR_CR
5476 - when we stopped at a line end, i.e. a newline or a CR and selective
5477 display is on. */
5478
701552dd 5479static enum move_it_result
5f5c8ee5
GM
5480move_it_in_display_line_to (it, to_charpos, to_x, op)
5481 struct it *it;
5482 int to_charpos, to_x, op;
5483{
5484 enum move_it_result result = MOVE_UNDEFINED;
5485 struct glyph_row *saved_glyph_row;
5486
5487 /* Don't produce glyphs in produce_glyphs. */
5488 saved_glyph_row = it->glyph_row;
5489 it->glyph_row = NULL;
5490
5f5c8ee5
GM
5491 while (1)
5492 {
ae26e27d 5493 int x, i, ascent = 0, descent = 0;
2311178e 5494
5f5c8ee5
GM
5495 /* Stop when ZV or TO_CHARPOS reached. */
5496 if (!get_next_display_element (it)
5497 || ((op & MOVE_TO_POS) != 0
5498 && BUFFERP (it->object)
5499 && IT_CHARPOS (*it) >= to_charpos))
5500 {
5501 result = MOVE_POS_MATCH_OR_ZV;
5502 break;
5503 }
2311178e 5504
5f5c8ee5
GM
5505 /* The call to produce_glyphs will get the metrics of the
5506 display element IT is loaded with. We record in x the
5507 x-position before this display element in case it does not
5508 fit on the line. */
5509 x = it->current_x;
2311178e 5510
47589c8c
GM
5511 /* Remember the line height so far in case the next element doesn't
5512 fit on the line. */
5513 if (!it->truncate_lines_p)
5514 {
5515 ascent = it->max_ascent;
5516 descent = it->max_descent;
5517 }
2311178e 5518
5f5c8ee5
GM
5519 PRODUCE_GLYPHS (it);
5520
5521 if (it->area != TEXT_AREA)
5522 {
cafafe0b 5523 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5524 continue;
5525 }
5526
5527 /* The number of glyphs we get back in IT->nglyphs will normally
5528 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5529 character on a terminal frame, or (iii) a line end. For the
5530 second case, IT->nglyphs - 1 padding glyphs will be present
5531 (on X frames, there is only one glyph produced for a
5532 composite character.
5533
5534 The behavior implemented below means, for continuation lines,
5535 that as many spaces of a TAB as fit on the current line are
5536 displayed there. For terminal frames, as many glyphs of a
5537 multi-glyph character are displayed in the current line, too.
5538 This is what the old redisplay code did, and we keep it that
5539 way. Under X, the whole shape of a complex character must
5540 fit on the line or it will be completely displayed in the
5541 next line.
5542
5543 Note that both for tabs and padding glyphs, all glyphs have
5544 the same width. */
5545 if (it->nglyphs)
5546 {
5547 /* More than one glyph or glyph doesn't fit on line. All
5548 glyphs have the same width. */
5549 int single_glyph_width = it->pixel_width / it->nglyphs;
5550 int new_x;
2311178e 5551
5f5c8ee5
GM
5552 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5553 {
5554 new_x = x + single_glyph_width;
5555
5556 /* We want to leave anything reaching TO_X to the caller. */
5557 if ((op & MOVE_TO_X) && new_x > to_x)
5558 {
5559 it->current_x = x;
5560 result = MOVE_X_REACHED;
5561 break;
5562 }
5563 else if (/* Lines are continued. */
5564 !it->truncate_lines_p
5565 && (/* And glyph doesn't fit on the line. */
5566 new_x > it->last_visible_x
5567 /* Or it fits exactly and we're on a window
5568 system frame. */
5569 || (new_x == it->last_visible_x
5570 && FRAME_WINDOW_P (it->f))))
5571 {
5572 if (/* IT->hpos == 0 means the very first glyph
5573 doesn't fit on the line, e.g. a wide image. */
5574 it->hpos == 0
5575 || (new_x == it->last_visible_x
5576 && FRAME_WINDOW_P (it->f)))
5577 {
5578 ++it->hpos;
5579 it->current_x = new_x;
5580 if (i == it->nglyphs - 1)
cafafe0b 5581 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5582 }
5583 else
47589c8c
GM
5584 {
5585 it->current_x = x;
5586 it->max_ascent = ascent;
5587 it->max_descent = descent;
5588 }
2311178e 5589
47589c8c
GM
5590 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5591 IT_CHARPOS (*it)));
5f5c8ee5
GM
5592 result = MOVE_LINE_CONTINUED;
5593 break;
5594 }
5595 else if (new_x > it->first_visible_x)
5596 {
5597 /* Glyph is visible. Increment number of glyphs that
5598 would be displayed. */
5599 ++it->hpos;
5600 }
5601 else
5602 {
2311178e 5603 /* Glyph is completely off the left margin of the display
5f5c8ee5
GM
5604 area. Nothing to do. */
5605 }
5606 }
5607
5608 if (result != MOVE_UNDEFINED)
5609 break;
5610 }
5611 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5612 {
5613 /* Stop when TO_X specified and reached. This check is
5614 necessary here because of lines consisting of a line end,
5615 only. The line end will not produce any glyphs and we
5616 would never get MOVE_X_REACHED. */
5617 xassert (it->nglyphs == 0);
5618 result = MOVE_X_REACHED;
5619 break;
5620 }
2311178e 5621
5f5c8ee5
GM
5622 /* Is this a line end? If yes, we're done. */
5623 if (ITERATOR_AT_END_OF_LINE_P (it))
5624 {
5625 result = MOVE_NEWLINE_OR_CR;
5626 break;
5627 }
2311178e 5628
5f5c8ee5
GM
5629 /* The current display element has been consumed. Advance
5630 to the next. */
cafafe0b 5631 set_iterator_to_next (it, 1);
2311178e 5632
5f5c8ee5
GM
5633 /* Stop if lines are truncated and IT's current x-position is
5634 past the right edge of the window now. */
5635 if (it->truncate_lines_p
5636 && it->current_x >= it->last_visible_x)
5637 {
5638 result = MOVE_LINE_TRUNCATED;
5639 break;
5640 }
5641 }
5642
5643 /* Restore the iterator settings altered at the beginning of this
5644 function. */
5645 it->glyph_row = saved_glyph_row;
5646 return result;
5647}
5648
5649
9b2bba76
RS
5650/* Move IT forward until it satisfies one or more of the criteria in
5651 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5652
5653 OP is a bit-mask that specifies where to stop, and in particular,
5654 which of those four position arguments makes a difference. See the
5655 description of enum move_operation_enum.
2311178e 5656
5f5c8ee5
GM
5657 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5658 screen line, this function will set IT to the next position >
5659 TO_CHARPOS. */
5660
5661void
5662move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5663 struct it *it;
5664 int to_charpos, to_x, to_y, to_vpos;
5665 int op;
5666{
5667 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5668 int line_height;
47589c8c 5669 int reached = 0;
5f5c8ee5 5670
47589c8c 5671 for (;;)
5f5c8ee5
GM
5672 {
5673 if (op & MOVE_TO_VPOS)
5674 {
5675 /* If no TO_CHARPOS and no TO_X specified, stop at the
5676 start of the line TO_VPOS. */
5677 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5678 {
5679 if (it->vpos == to_vpos)
47589c8c
GM
5680 {
5681 reached = 1;
5682 break;
5683 }
5684 else
5685 skip = move_it_in_display_line_to (it, -1, -1, 0);
5f5c8ee5
GM
5686 }
5687 else
5688 {
5689 /* TO_VPOS >= 0 means stop at TO_X in the line at
5690 TO_VPOS, or at TO_POS, whichever comes first. */
47589c8c
GM
5691 if (it->vpos == to_vpos)
5692 {
5693 reached = 2;
5694 break;
5695 }
2311178e 5696
5f5c8ee5
GM
5697 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5698
5699 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
47589c8c
GM
5700 {
5701 reached = 3;
5702 break;
5703 }
5f5c8ee5
GM
5704 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5705 {
5706 /* We have reached TO_X but not in the line we want. */
5707 skip = move_it_in_display_line_to (it, to_charpos,
5708 -1, MOVE_TO_POS);
5709 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5710 {
5711 reached = 4;
5712 break;
5713 }
5f5c8ee5
GM
5714 }
5715 }
5716 }
5717 else if (op & MOVE_TO_Y)
5718 {
5719 struct it it_backup;
2311178e 5720
5f5c8ee5
GM
5721 /* TO_Y specified means stop at TO_X in the line containing
5722 TO_Y---or at TO_CHARPOS if this is reached first. The
5723 problem is that we can't really tell whether the line
5724 contains TO_Y before we have completely scanned it, and
5725 this may skip past TO_X. What we do is to first scan to
5726 TO_X.
5727
5728 If TO_X is not specified, use a TO_X of zero. The reason
5729 is to make the outcome of this function more predictable.
5730 If we didn't use TO_X == 0, we would stop at the end of
5731 the line which is probably not what a caller would expect
5732 to happen. */
5733 skip = move_it_in_display_line_to (it, to_charpos,
5734 ((op & MOVE_TO_X)
5735 ? to_x : 0),
5736 (MOVE_TO_X
5737 | (op & MOVE_TO_POS)));
5738
5739 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5740 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5741 {
5742 reached = 5;
5743 break;
5744 }
2311178e 5745
5f5c8ee5
GM
5746 /* If TO_X was reached, we would like to know whether TO_Y
5747 is in the line. This can only be said if we know the
5748 total line height which requires us to scan the rest of
5749 the line. */
5f5c8ee5
GM
5750 if (skip == MOVE_X_REACHED)
5751 {
5752 it_backup = *it;
47589c8c 5753 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5754 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5755 op & MOVE_TO_POS);
47589c8c 5756 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5757 }
5758
5759 /* Now, decide whether TO_Y is in this line. */
5760 line_height = it->max_ascent + it->max_descent;
47589c8c 5761 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
2311178e 5762
5f5c8ee5
GM
5763 if (to_y >= it->current_y
5764 && to_y < it->current_y + line_height)
5765 {
5766 if (skip == MOVE_X_REACHED)
5767 /* If TO_Y is in this line and TO_X was reached above,
5768 we scanned too far. We have to restore IT's settings
5769 to the ones before skipping. */
5770 *it = it_backup;
47589c8c 5771 reached = 6;
5f5c8ee5
GM
5772 }
5773 else if (skip == MOVE_X_REACHED)
5774 {
5775 skip = skip2;
5776 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c 5777 reached = 7;
5f5c8ee5
GM
5778 }
5779
47589c8c 5780 if (reached)
5f5c8ee5
GM
5781 break;
5782 }
5783 else
5784 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5785
5786 switch (skip)
5787 {
5788 case MOVE_POS_MATCH_OR_ZV:
47589c8c
GM
5789 reached = 8;
5790 goto out;
5f5c8ee5
GM
5791
5792 case MOVE_NEWLINE_OR_CR:
cafafe0b 5793 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5794 it->continuation_lines_width = 0;
5795 break;
5796
5797 case MOVE_LINE_TRUNCATED:
5798 it->continuation_lines_width = 0;
312246d1 5799 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
5800 if ((op & MOVE_TO_POS) != 0
5801 && IT_CHARPOS (*it) > to_charpos)
47589c8c
GM
5802 {
5803 reached = 9;
5804 goto out;
5805 }
5f5c8ee5
GM
5806 break;
5807
5808 case MOVE_LINE_CONTINUED:
5809 it->continuation_lines_width += it->current_x;
5810 break;
5811
5812 default:
5813 abort ();
5814 }
5815
5816 /* Reset/increment for the next run. */
5817 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5818 it->current_x = it->hpos = 0;
5819 it->current_y += it->max_ascent + it->max_descent;
5820 ++it->vpos;
5821 last_height = it->max_ascent + it->max_descent;
5822 last_max_ascent = it->max_ascent;
5823 it->max_ascent = it->max_descent = 0;
5824 }
2311178e 5825
47589c8c
GM
5826 out:
5827
5828 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5f5c8ee5
GM
5829}
5830
5831
5832/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5833
5834 If DY > 0, move IT backward at least that many pixels. DY = 0
5835 means move IT backward to the preceding line start or BEGV. This
5836 function may move over more than DY pixels if IT->current_y - DY
5837 ends up in the middle of a line; in this case IT->current_y will be
5838 set to the top of the line moved to. */
5839
5840void
5841move_it_vertically_backward (it, dy)
5842 struct it *it;
5843 int dy;
5844{
79ddf6f7
GM
5845 int nlines, h;
5846 struct it it2, it3;
5f5c8ee5 5847 int start_pos = IT_CHARPOS (*it);
2311178e 5848
5f5c8ee5
GM
5849 xassert (dy >= 0);
5850
5851 /* Estimate how many newlines we must move back. */
da8b7f4f 5852 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
5f5c8ee5
GM
5853
5854 /* Set the iterator's position that many lines back. */
5855 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5856 back_to_previous_visible_line_start (it);
5857
5858 /* Reseat the iterator here. When moving backward, we don't want
5859 reseat to skip forward over invisible text, set up the iterator
5860 to deliver from overlay strings at the new position etc. So,
5861 use reseat_1 here. */
5862 reseat_1 (it, it->current.pos, 1);
5863
5864 /* We are now surely at a line start. */
5865 it->current_x = it->hpos = 0;
0e47bbf7 5866 it->continuation_lines_width = 0;
5f5c8ee5
GM
5867
5868 /* Move forward and see what y-distance we moved. First move to the
5869 start of the next line so that we get its height. We need this
5870 height to be able to tell whether we reached the specified
5871 y-distance. */
5872 it2 = *it;
5873 it2.max_ascent = it2.max_descent = 0;
5874 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5875 MOVE_TO_POS | MOVE_TO_VPOS);
5876 xassert (IT_CHARPOS (*it) >= BEGV);
79ddf6f7 5877 it3 = it2;
2311178e 5878
5f5c8ee5
GM
5879 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5880 xassert (IT_CHARPOS (*it) >= BEGV);
ccbb9ed2
RS
5881 /* H is the actual vertical distance from the position in *IT
5882 and the starting position. */
5f5c8ee5 5883 h = it2.current_y - it->current_y;
ccbb9ed2 5884 /* NLINES is the distance in number of lines. */
5f5c8ee5
GM
5885 nlines = it2.vpos - it->vpos;
5886
ccbb9ed2
RS
5887 /* Correct IT's y and vpos position
5888 so that they are relative to the starting point. */
5f5c8ee5
GM
5889 it->vpos -= nlines;
5890 it->current_y -= h;
2311178e 5891
5f5c8ee5
GM
5892 if (dy == 0)
5893 {
5894 /* DY == 0 means move to the start of the screen line. The
5895 value of nlines is > 0 if continuation lines were involved. */
5896 if (nlines > 0)
5897 move_it_by_lines (it, nlines, 1);
5898 xassert (IT_CHARPOS (*it) <= start_pos);
5899 }
ccbb9ed2 5900 else
5f5c8ee5 5901 {
ccbb9ed2
RS
5902 /* The y-position we try to reach, relative to *IT.
5903 Note that H has been subtracted in front of the if-statement. */
5f5c8ee5 5904 int target_y = it->current_y + h - dy;
79ddf6f7
GM
5905 int y0 = it3.current_y;
5906 int y1 = line_bottom_y (&it3);
5907 int line_height = y1 - y0;
f7ccfc8c 5908
5f5c8ee5
GM
5909 /* If we did not reach target_y, try to move further backward if
5910 we can. If we moved too far backward, try to move forward. */
5911 if (target_y < it->current_y
79ddf6f7
GM
5912 /* This is heuristic. In a window that's 3 lines high, with
5913 a line height of 13 pixels each, recentering with point
5914 on the bottom line will try to move -39/2 = 19 pixels
5915 backward. Try to avoid moving into the first line. */
798dbe1f 5916 && it->current_y - target_y > line_height / 3 * 2
5f5c8ee5
GM
5917 && IT_CHARPOS (*it) > BEGV)
5918 {
f7ccfc8c
GM
5919 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5920 target_y - it->current_y));
5f5c8ee5
GM
5921 move_it_vertically (it, target_y - it->current_y);
5922 xassert (IT_CHARPOS (*it) >= BEGV);
5923 }
5924 else if (target_y >= it->current_y + line_height
5925 && IT_CHARPOS (*it) < ZV)
5926 {
55591976 5927 /* Should move forward by at least one line, maybe more.
2311178e 5928
55591976
GM
5929 Note: Calling move_it_by_lines can be expensive on
5930 terminal frames, where compute_motion is used (via
5931 vmotion) to do the job, when there are very long lines
5932 and truncate-lines is nil. That's the reason for
5933 treating terminal frames specially here. */
2311178e 5934
55591976
GM
5935 if (!FRAME_WINDOW_P (it->f))
5936 move_it_vertically (it, target_y - (it->current_y + line_height));
5937 else
f7ccfc8c 5938 {
55591976
GM
5939 do
5940 {
5941 move_it_by_lines (it, 1, 1);
5942 }
5943 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
f7ccfc8c 5944 }
f7ccfc8c 5945
5f5c8ee5
GM
5946 xassert (IT_CHARPOS (*it) >= BEGV);
5947 }
5948 }
5949}
5950
5951
5952/* Move IT by a specified amount of pixel lines DY. DY negative means
5953 move backwards. DY = 0 means move to start of screen line. At the
5954 end, IT will be on the start of a screen line. */
5955
2311178e 5956void
5f5c8ee5
GM
5957move_it_vertically (it, dy)
5958 struct it *it;
5959 int dy;
5960{
5961 if (dy <= 0)
5962 move_it_vertically_backward (it, -dy);
5963 else if (dy > 0)
5964 {
47589c8c 5965 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5f5c8ee5
GM
5966 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5967 MOVE_TO_POS | MOVE_TO_Y);
47589c8c 5968 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5969
5970 /* If buffer ends in ZV without a newline, move to the start of
5971 the line to satisfy the post-condition. */
5972 if (IT_CHARPOS (*it) == ZV
5973 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5974 move_it_by_lines (it, 0, 0);
5975 }
5976}
5977
5978
47fc2c10
GM
5979/* Move iterator IT past the end of the text line it is in. */
5980
5981void
5982move_it_past_eol (it)
5983 struct it *it;
5984{
5985 enum move_it_result rc;
2311178e 5986
47fc2c10
GM
5987 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5988 if (rc == MOVE_NEWLINE_OR_CR)
5989 set_iterator_to_next (it, 0);
5990}
5991
5992
2c79b732
GM
5993#if 0 /* Currently not used. */
5994
5f5c8ee5
GM
5995/* Return non-zero if some text between buffer positions START_CHARPOS
5996 and END_CHARPOS is invisible. IT->window is the window for text
5997 property lookup. */
5998
5999static int
6000invisible_text_between_p (it, start_charpos, end_charpos)
6001 struct it *it;
6002 int start_charpos, end_charpos;
6003{
5f5c8ee5
GM
6004 Lisp_Object prop, limit;
6005 int invisible_found_p;
2311178e 6006
5f5c8ee5
GM
6007 xassert (it != NULL && start_charpos <= end_charpos);
6008
6009 /* Is text at START invisible? */
6010 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6011 it->window);
6012 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6013 invisible_found_p = 1;
6014 else
6015 {
016b5642
MB
6016 limit = Fnext_single_char_property_change (make_number (start_charpos),
6017 Qinvisible, Qnil,
6018 make_number (end_charpos));
5f5c8ee5
GM
6019 invisible_found_p = XFASTINT (limit) < end_charpos;
6020 }
6021
6022 return invisible_found_p;
5f5c8ee5
GM
6023}
6024
2c79b732
GM
6025#endif /* 0 */
6026
5f5c8ee5
GM
6027
6028/* Move IT by a specified number DVPOS of screen lines down. DVPOS
6029 negative means move up. DVPOS == 0 means move to the start of the
6030 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6031 NEED_Y_P is zero, IT->current_y will be left unchanged.
6032
6033 Further optimization ideas: If we would know that IT->f doesn't use
6034 a face with proportional font, we could be faster for
6035 truncate-lines nil. */
6036
6037void
6038move_it_by_lines (it, dvpos, need_y_p)
6039 struct it *it;
6040 int dvpos, need_y_p;
6041{
6042 struct position pos;
2311178e 6043
5f5c8ee5
GM
6044 if (!FRAME_WINDOW_P (it->f))
6045 {
6046 struct text_pos textpos;
2311178e 6047
5f5c8ee5
GM
6048 /* We can use vmotion on frames without proportional fonts. */
6049 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6050 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6051 reseat (it, textpos, 1);
6052 it->vpos += pos.vpos;
6053 it->current_y += pos.vpos;
6054 }
6055 else if (dvpos == 0)
6056 {
6057 /* DVPOS == 0 means move to the start of the screen line. */
6058 move_it_vertically_backward (it, 0);
6059 xassert (it->current_x == 0 && it->hpos == 0);
6060 }
6061 else if (dvpos > 0)
2c79b732 6062 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5f5c8ee5
GM
6063 else
6064 {
6065 struct it it2;
6066 int start_charpos, i;
2311178e 6067
e8660d73
GM
6068 /* Start at the beginning of the screen line containing IT's
6069 position. */
6070 move_it_vertically_backward (it, 0);
2311178e 6071
5f5c8ee5
GM
6072 /* Go back -DVPOS visible lines and reseat the iterator there. */
6073 start_charpos = IT_CHARPOS (*it);
6074 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6075 back_to_previous_visible_line_start (it);
6076 reseat (it, it->current.pos, 1);
6077 it->current_x = it->hpos = 0;
6078
6079 /* Above call may have moved too far if continuation lines
6080 are involved. Scan forward and see if it did. */
6081 it2 = *it;
6082 it2.vpos = it2.current_y = 0;
6083 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6084 it->vpos -= it2.vpos;
6085 it->current_y -= it2.current_y;
6086 it->current_x = it->hpos = 0;
6087
6088 /* If we moved too far, move IT some lines forward. */
6089 if (it2.vpos > -dvpos)
6090 {
6091 int delta = it2.vpos + dvpos;
6092 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6093 }
6094 }
6095}
6096
3824b1a6
AS
6097/* Return 1 if IT points into the middle of a display vector. */
6098
6099int
6100in_display_vector_p (it)
6101 struct it *it;
6102{
6103 return (it->method == next_element_from_display_vector
6104 && it->current.dpvec_index > 0
6105 && it->dpvec + it->current.dpvec_index != it->dpend);
6106}
5f5c8ee5
GM
6107
6108\f
6109/***********************************************************************
6110 Messages
6111 ***********************************************************************/
6112
6113
937248bc
GM
6114/* Add a message with format string FORMAT and arguments ARG1 and ARG2
6115 to *Messages*. */
6116
6117void
6118add_to_log (format, arg1, arg2)
6119 char *format;
6120 Lisp_Object arg1, arg2;
6121{
6122 Lisp_Object args[3];
6123 Lisp_Object msg, fmt;
6124 char *buffer;
6125 int len;
6126 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6127
ae794295
GM
6128 /* Do nothing if called asynchronously. Inserting text into
6129 a buffer may call after-change-functions and alike and
6130 that would means running Lisp asynchronously. */
6131 if (handling_signal)
6132 return;
6133
937248bc
GM
6134 fmt = msg = Qnil;
6135 GCPRO4 (fmt, msg, arg1, arg2);
2311178e 6136
937248bc
GM
6137 args[0] = fmt = build_string (format);
6138 args[1] = arg1;
6139 args[2] = arg2;
6fc556fd 6140 msg = Fformat (3, args);
937248bc 6141
2051c264 6142 len = SBYTES (msg) + 1;
937248bc 6143 buffer = (char *) alloca (len);
2051c264 6144 bcopy (SDATA (msg), buffer, len);
2311178e 6145
796184bc 6146 message_dolog (buffer, len - 1, 1, 0);
937248bc
GM
6147 UNGCPRO;
6148}
6149
6150
5f5c8ee5
GM
6151/* Output a newline in the *Messages* buffer if "needs" one. */
6152
6153void
6154message_log_maybe_newline ()
6155{
6156 if (message_log_need_newline)
6157 message_dolog ("", 0, 1, 0);
6158}
6159
6160
1e313f28 6161/* Add a string M of length NBYTES to the message log, optionally
5f5c8ee5
GM
6162 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6163 nonzero, means interpret the contents of M as multibyte. This
6164 function calls low-level routines in order to bypass text property
6165 hooks, etc. which might not be safe to run. */
6166
6167void
1e313f28 6168message_dolog (m, nbytes, nlflag, multibyte)
50f80c2f 6169 const char *m;
1e313f28 6170 int nbytes, nlflag, multibyte;
5f5c8ee5 6171{
a67e162b
RS
6172 if (!NILP (Vmemory_full))
6173 return;
6174
5f5c8ee5
GM
6175 if (!NILP (Vmessage_log_max))
6176 {
6177 struct buffer *oldbuf;
6178 Lisp_Object oldpoint, oldbegv, oldzv;
6179 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6180 int point_at_end = 0;
6181 int zv_at_end = 0;
6182 Lisp_Object old_deactivate_mark, tem;
6052529b 6183 struct gcpro gcpro1;
5f5c8ee5
GM
6184
6185 old_deactivate_mark = Vdeactivate_mark;
6186 oldbuf = current_buffer;
6a94510a 6187 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5f5c8ee5
GM
6188 current_buffer->undo_list = Qt;
6189
b14bc55e
RS
6190 oldpoint = message_dolog_marker1;
6191 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6192 oldbegv = message_dolog_marker2;
6193 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6194 oldzv = message_dolog_marker3;
6195 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6196 GCPRO1 (old_deactivate_mark);
5f5c8ee5
GM
6197
6198 if (PT == Z)
6199 point_at_end = 1;
6200 if (ZV == Z)
6201 zv_at_end = 1;
6202
6203 BEGV = BEG;
6204 BEGV_BYTE = BEG_BYTE;
6205 ZV = Z;
6206 ZV_BYTE = Z_BYTE;
6207 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6208
6209 /* Insert the string--maybe converting multibyte to single byte
6210 or vice versa, so that all the text fits the buffer. */
6211 if (multibyte
6212 && NILP (current_buffer->enable_multibyte_characters))
6213 {
1e313f28 6214 int i, c, char_bytes;
5f5c8ee5 6215 unsigned char work[1];
2311178e 6216
5f5c8ee5
GM
6217 /* Convert a multibyte string to single-byte
6218 for the *Message* buffer. */
6e57ec5e 6219 for (i = 0; i < nbytes; i += char_bytes)
5f5c8ee5 6220 {
1e313f28 6221 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5f5c8ee5
GM
6222 work[0] = (SINGLE_BYTE_CHAR_P (c)
6223 ? c
6224 : multibyte_char_to_unibyte (c, Qnil));
6225 insert_1_both (work, 1, 1, 1, 0, 0);
6226 }
6227 }
6228 else if (! multibyte
6229 && ! NILP (current_buffer->enable_multibyte_characters))
6230 {
1e313f28 6231 int i, c, char_bytes;
5f5c8ee5 6232 unsigned char *msg = (unsigned char *) m;
260a86a0 6233 unsigned char str[MAX_MULTIBYTE_LENGTH];
5f5c8ee5
GM
6234 /* Convert a single-byte string to multibyte
6235 for the *Message* buffer. */
1e313f28 6236 for (i = 0; i < nbytes; i++)
5f5c8ee5
GM
6237 {
6238 c = unibyte_char_to_multibyte (msg[i]);
1e313f28
GM
6239 char_bytes = CHAR_STRING (c, str);
6240 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5f5c8ee5
GM
6241 }
6242 }
1e313f28
GM
6243 else if (nbytes)
6244 insert_1 (m, nbytes, 1, 0, 0);
5f5c8ee5
GM
6245
6246 if (nlflag)
6247 {
6248 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6249 insert_1 ("\n", 1, 1, 0, 0);
6250
6251 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6252 this_bol = PT;
6253 this_bol_byte = PT_BYTE;
6254
b14bc55e
RS
6255 /* See if this line duplicates the previous one.
6256 If so, combine duplicates. */
5f5c8ee5
GM
6257 if (this_bol > BEG)
6258 {
6259 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6260 prev_bol = PT;
6261 prev_bol_byte = PT_BYTE;
6262
6263 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6264 this_bol, this_bol_byte);
6265 if (dup)
6266 {
6267 del_range_both (prev_bol, prev_bol_byte,
6268 this_bol, this_bol_byte, 0);
6269 if (dup > 1)
6270 {
6271 char dupstr[40];
6272 int duplen;
6273
6274 /* If you change this format, don't forget to also
6275 change message_log_check_duplicate. */
6276 sprintf (dupstr, " [%d times]", dup);
6277 duplen = strlen (dupstr);
6278 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6279 insert_1 (dupstr, duplen, 1, 0, 1);
6280 }
6281 }
6282 }
6283
b14bc55e
RS
6284 /* If we have more than the desired maximum number of lines
6285 in the *Messages* buffer now, delete the oldest ones.
6286 This is safe because we don't have undo in this buffer. */
6287
5f5c8ee5
GM
6288 if (NATNUMP (Vmessage_log_max))
6289 {
6290 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6291 -XFASTINT (Vmessage_log_max) - 1, 0);
6292 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6293 }
6294 }
6295 BEGV = XMARKER (oldbegv)->charpos;
6296 BEGV_BYTE = marker_byte_position (oldbegv);
6297
6298 if (zv_at_end)
6299 {
6300 ZV = Z;
6301 ZV_BYTE = Z_BYTE;
6302 }
6303 else
6304 {
6305 ZV = XMARKER (oldzv)->charpos;
6306 ZV_BYTE = marker_byte_position (oldzv);
6307 }
6308
6309 if (point_at_end)
6310 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6311 else
6312 /* We can't do Fgoto_char (oldpoint) because it will run some
6313 Lisp code. */
6314 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6315 XMARKER (oldpoint)->bytepos);
6316
6317 UNGCPRO;
cfea0546
SM
6318 unchain_marker (XMARKER (oldpoint));
6319 unchain_marker (XMARKER (oldbegv));
6320 unchain_marker (XMARKER (oldzv));
5f5c8ee5
GM
6321
6322 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6323 set_buffer_internal (oldbuf);
6324 if (NILP (tem))
6325 windows_or_buffers_changed = old_windows_or_buffers_changed;
6326 message_log_need_newline = !nlflag;
6327 Vdeactivate_mark = old_deactivate_mark;
6328 }
6329}
6330
6331
6332/* We are at the end of the buffer after just having inserted a newline.
6333 (Note: We depend on the fact we won't be crossing the gap.)
6334 Check to see if the most recent message looks a lot like the previous one.
6335 Return 0 if different, 1 if the new one should just replace it, or a
6336 value N > 1 if we should also append " [N times]". */
6337
6338static int
6339message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6340 int prev_bol, this_bol;
6341 int prev_bol_byte, this_bol_byte;
6342{
6343 int i;
6344 int len = Z_BYTE - 1 - this_bol_byte;
6345 int seen_dots = 0;
6346 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6347 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6348
6349 for (i = 0; i < len; i++)
6350 {
509633e3 6351 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5f5c8ee5
GM
6352 seen_dots = 1;
6353 if (p1[i] != p2[i])
6354 return seen_dots;
6355 }
6356 p1 += len;
6357 if (*p1 == '\n')
6358 return 2;
6359 if (*p1++ == ' ' && *p1++ == '[')
6360 {
6361 int n = 0;
6362 while (*p1 >= '0' && *p1 <= '9')
6363 n = n * 10 + *p1++ - '0';
6364 if (strncmp (p1, " times]\n", 8) == 0)
6365 return n+1;
6366 }
6367 return 0;
6368}
6369
6370
1e313f28
GM
6371/* Display an echo area message M with a specified length of NBYTES
6372 bytes. The string may include null characters. If M is 0, clear
6373 out any existing message, and let the mini-buffer text show
6374 through.
5f5c8ee5
GM
6375
6376 The buffer M must continue to exist until after the echo area gets
6377 cleared or some other message gets displayed there. This means do
6378 not pass text that is stored in a Lisp string; do not pass text in
6379 a buffer that was alloca'd. */
6380
6381void
1e313f28 6382message2 (m, nbytes, multibyte)
50f80c2f 6383 const char *m;
1e313f28 6384 int nbytes;
5f5c8ee5
GM
6385 int multibyte;
6386{
6387 /* First flush out any partial line written with print. */
6388 message_log_maybe_newline ();
6389 if (m)
1e313f28
GM
6390 message_dolog (m, nbytes, 1, multibyte);
6391 message2_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
6392}
6393
6394
6395/* The non-logging counterpart of message2. */
6396
6397void
1e313f28 6398message2_nolog (m, nbytes, multibyte)
50f80c2f 6399 const char *m;
e3383b6f 6400 int nbytes, multibyte;
5f5c8ee5 6401{
886bd6f2 6402 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6403 message_enable_multibyte = multibyte;
6404
6405 if (noninteractive)
6406 {
6407 if (noninteractive_need_newline)
6408 putc ('\n', stderr);
6409 noninteractive_need_newline = 0;
6410 if (m)
1e313f28 6411 fwrite (m, nbytes, 1, stderr);
5f5c8ee5
GM
6412 if (cursor_in_echo_area == 0)
6413 fprintf (stderr, "\n");
6414 fflush (stderr);
6415 }
6416 /* A null message buffer means that the frame hasn't really been
6417 initialized yet. Error messages get reported properly by
6418 cmd_error, so this must be just an informative message; toss it. */
2311178e 6419 else if (INTERACTIVE
886bd6f2
GM
6420 && sf->glyphs_initialized_p
6421 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
6422 {
6423 Lisp_Object mini_window;
6424 struct frame *f;
6425
6426 /* Get the frame containing the mini-buffer
6427 that the selected frame is using. */
886bd6f2 6428 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6429 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6430
6431 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 6432 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
6433 && ! FRAME_VISIBLE_P (f))
6434 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6435
6436 if (m)
6437 {
1e313f28 6438 set_message (m, Qnil, nbytes, multibyte);
5f5c8ee5
GM
6439 if (minibuffer_auto_raise)
6440 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6441 }
6442 else
c6e89d6c 6443 clear_message (1, 1);
5f5c8ee5 6444
c6e89d6c 6445 do_pending_window_change (0);
5f5c8ee5 6446 echo_area_display (1);
c6e89d6c 6447 do_pending_window_change (0);
5f5c8ee5
GM
6448 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6449 (*frame_up_to_date_hook) (f);
6450 }
6451}
6452
6453
c6e89d6c
GM
6454/* Display an echo area message M with a specified length of NBYTES
6455 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
6456 string, clear out any existing message, and let the mini-buffer
6457 text show through. */
6458
6459void
c6e89d6c 6460message3 (m, nbytes, multibyte)
5f5c8ee5 6461 Lisp_Object m;
c6e89d6c 6462 int nbytes;
5f5c8ee5
GM
6463 int multibyte;
6464{
6465 struct gcpro gcpro1;
6466
6467 GCPRO1 (m);
2311178e 6468
5f5c8ee5
GM
6469 /* First flush out any partial line written with print. */
6470 message_log_maybe_newline ();
6471 if (STRINGP (m))
2051c264 6472 message_dolog (SDATA (m), nbytes, 1, multibyte);
c6e89d6c 6473 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
6474
6475 UNGCPRO;
6476}
6477
6478
6479/* The non-logging version of message3. */
6480
6481void
c6e89d6c 6482message3_nolog (m, nbytes, multibyte)
5f5c8ee5 6483 Lisp_Object m;
c6e89d6c 6484 int nbytes, multibyte;
5f5c8ee5 6485{
886bd6f2 6486 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6487 message_enable_multibyte = multibyte;
6488
6489 if (noninteractive)
6490 {
6491 if (noninteractive_need_newline)
6492 putc ('\n', stderr);
6493 noninteractive_need_newline = 0;
6494 if (STRINGP (m))
2051c264 6495 fwrite (SDATA (m), nbytes, 1, stderr);
5f5c8ee5
GM
6496 if (cursor_in_echo_area == 0)
6497 fprintf (stderr, "\n");
6498 fflush (stderr);
6499 }
6500 /* A null message buffer means that the frame hasn't really been
6501 initialized yet. Error messages get reported properly by
6502 cmd_error, so this must be just an informative message; toss it. */
2311178e 6503 else if (INTERACTIVE
886bd6f2
GM
6504 && sf->glyphs_initialized_p
6505 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
6506 {
6507 Lisp_Object mini_window;
c6e89d6c 6508 Lisp_Object frame;
5f5c8ee5
GM
6509 struct frame *f;
6510
6511 /* Get the frame containing the mini-buffer
6512 that the selected frame is using. */
886bd6f2 6513 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6514 frame = XWINDOW (mini_window)->frame;
6515 f = XFRAME (frame);
5f5c8ee5
GM
6516
6517 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 6518 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
6519 && !FRAME_VISIBLE_P (f))
6520 Fmake_frame_visible (frame);
5f5c8ee5 6521
2051c264 6522 if (STRINGP (m) && SCHARS (m) > 0)
5f5c8ee5 6523 {
c6e89d6c 6524 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
6525 if (minibuffer_auto_raise)
6526 Fraise_frame (frame);
5f5c8ee5
GM
6527 }
6528 else
c6e89d6c 6529 clear_message (1, 1);
5f5c8ee5 6530
c6e89d6c 6531 do_pending_window_change (0);
5f5c8ee5 6532 echo_area_display (1);
c6e89d6c 6533 do_pending_window_change (0);
5f5c8ee5
GM
6534 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6535 (*frame_up_to_date_hook) (f);
6536 }
6537}
6538
6539
6540/* Display a null-terminated echo area message M. If M is 0, clear
6541 out any existing message, and let the mini-buffer text show through.
6542
6543 The buffer M must continue to exist until after the echo area gets
6544 cleared or some other message gets displayed there. Do not pass
6545 text that is stored in a Lisp string. Do not pass text in a buffer
6546 that was alloca'd. */
6547
6548void
6549message1 (m)
6550 char *m;
6551{
6552 message2 (m, (m ? strlen (m) : 0), 0);
6553}
6554
6555
6556/* The non-logging counterpart of message1. */
6557
6558void
6559message1_nolog (m)
6560 char *m;
6561{
6562 message2_nolog (m, (m ? strlen (m) : 0), 0);
6563}
6564
6565/* Display a message M which contains a single %s
6566 which gets replaced with STRING. */
6567
6568void
6569message_with_string (m, string, log)
6570 char *m;
6571 Lisp_Object string;
6572 int log;
6573{
5b6d51b6
RS
6574 CHECK_STRING (string);
6575
5f5c8ee5
GM
6576 if (noninteractive)
6577 {
6578 if (m)
6579 {
6580 if (noninteractive_need_newline)
6581 putc ('\n', stderr);
6582 noninteractive_need_newline = 0;
2051c264 6583 fprintf (stderr, m, SDATA (string));
5f5c8ee5
GM
6584 if (cursor_in_echo_area == 0)
6585 fprintf (stderr, "\n");
6586 fflush (stderr);
6587 }
6588 }
6589 else if (INTERACTIVE)
6590 {
6591 /* The frame whose minibuffer we're going to display the message on.
6592 It may be larger than the selected frame, so we need
6593 to use its buffer, not the selected frame's buffer. */
6594 Lisp_Object mini_window;
886bd6f2 6595 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6596
6597 /* Get the frame containing the minibuffer
6598 that the selected frame is using. */
886bd6f2 6599 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6600 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6601
6602 /* A null message buffer means that the frame hasn't really been
6603 initialized yet. Error messages get reported properly by
6604 cmd_error, so this must be just an informative message; toss it. */
6605 if (FRAME_MESSAGE_BUF (f))
6606 {
eb484132
GM
6607 Lisp_Object args[2], message;
6608 struct gcpro gcpro1, gcpro2;
5f5c8ee5 6609
eb484132
GM
6610 args[0] = build_string (m);
6611 args[1] = message = string;
78e17433 6612 GCPRO2 (args[0], message);
eb484132 6613 gcpro1.nvars = 2;
2311178e 6614
eb484132 6615 message = Fformat (2, args);
5f5c8ee5
GM
6616
6617 if (log)
d5db4077 6618 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
5f5c8ee5 6619 else
d5db4077 6620 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
eb484132
GM
6621
6622 UNGCPRO;
5f5c8ee5
GM
6623
6624 /* Print should start at the beginning of the message
6625 buffer next time. */
6626 message_buf_print = 0;
6627 }
6628 }
6629}
6630
6631
5f5c8ee5
GM
6632/* Dump an informative message to the minibuf. If M is 0, clear out
6633 any existing message, and let the mini-buffer text show through. */
6634
6635/* VARARGS 1 */
6636void
6637message (m, a1, a2, a3)
6638 char *m;
6639 EMACS_INT a1, a2, a3;
6640{
6641 if (noninteractive)
6642 {
6643 if (m)
6644 {
6645 if (noninteractive_need_newline)
6646 putc ('\n', stderr);
6647 noninteractive_need_newline = 0;
6648 fprintf (stderr, m, a1, a2, a3);
6649 if (cursor_in_echo_area == 0)
6650 fprintf (stderr, "\n");
6651 fflush (stderr);
6652 }
6653 }
6654 else if (INTERACTIVE)
6655 {
6656 /* The frame whose mini-buffer we're going to display the message
6657 on. It may be larger than the selected frame, so we need to
6658 use its buffer, not the selected frame's buffer. */
6659 Lisp_Object mini_window;
886bd6f2 6660 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
6661
6662 /* Get the frame containing the mini-buffer
6663 that the selected frame is using. */
886bd6f2 6664 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
6665 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6666
6667 /* A null message buffer means that the frame hasn't really been
6668 initialized yet. Error messages get reported properly by
6669 cmd_error, so this must be just an informative message; toss
6670 it. */
6671 if (FRAME_MESSAGE_BUF (f))
6672 {
6673 if (m)
6674 {
6675 int len;
6676#ifdef NO_ARG_ARRAY
6677 char *a[3];
6678 a[0] = (char *) a1;
6679 a[1] = (char *) a2;
6680 a[2] = (char *) a3;
6681
6682 len = doprnt (FRAME_MESSAGE_BUF (f),
6683 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6684#else
6685 len = doprnt (FRAME_MESSAGE_BUF (f),
6686 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6687 (char **) &a1);
6688#endif /* NO_ARG_ARRAY */
6689
6690 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6691 }
6692 else
6693 message1 (0);
6694
6695 /* Print should start at the beginning of the message
6696 buffer next time. */
6697 message_buf_print = 0;
6698 }
6699 }
6700}
6701
6702
6703/* The non-logging version of message. */
6704
6705void
6706message_nolog (m, a1, a2, a3)
6707 char *m;
6708 EMACS_INT a1, a2, a3;
6709{
6710 Lisp_Object old_log_max;
6711 old_log_max = Vmessage_log_max;
6712 Vmessage_log_max = Qnil;
6713 message (m, a1, a2, a3);
6714 Vmessage_log_max = old_log_max;
6715}
6716
6717
c6e89d6c
GM
6718/* Display the current message in the current mini-buffer. This is
6719 only called from error handlers in process.c, and is not time
6720 critical. */
5f5c8ee5
GM
6721
6722void
6723update_echo_area ()
6724{
c6e89d6c
GM
6725 if (!NILP (echo_area_buffer[0]))
6726 {
6727 Lisp_Object string;
6728 string = Fcurrent_message ();
2311178e 6729 message3 (string, SBYTES (string),
c6e89d6c
GM
6730 !NILP (current_buffer->enable_multibyte_characters));
6731 }
6732}
6733
6734
a67e162b
RS
6735/* Make sure echo area buffers in `echo_buffers' are live.
6736 If they aren't, make new ones. */
5bcfeb49
GM
6737
6738static void
6739ensure_echo_area_buffers ()
6740{
6741 int i;
6742
6743 for (i = 0; i < 2; ++i)
6744 if (!BUFFERP (echo_buffer[i])
6745 || NILP (XBUFFER (echo_buffer[i])->name))
6746 {
6747 char name[30];
ff3d9573
GM
6748 Lisp_Object old_buffer;
6749 int j;
6750
6751 old_buffer = echo_buffer[i];
5bcfeb49
GM
6752 sprintf (name, " *Echo Area %d*", i);
6753 echo_buffer[i] = Fget_buffer_create (build_string (name));
ad4f174e 6754 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
ff3d9573
GM
6755
6756 for (j = 0; j < 2; ++j)
6757 if (EQ (old_buffer, echo_area_buffer[j]))
6758 echo_area_buffer[j] = echo_buffer[i];
5bcfeb49
GM
6759 }
6760}
6761
6762
23a96c77 6763/* Call FN with args A1..A4 with either the current or last displayed
c6e89d6c
GM
6764 echo_area_buffer as current buffer.
6765
6766 WHICH zero means use the current message buffer
6767 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6768 from echo_buffer[] and clear it.
6769
6770 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6771 suitable buffer from echo_buffer[] and clear it.
6772
6773 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6774 that the current message becomes the last displayed one, make
6775 choose a suitable buffer for echo_area_buffer[0], and clear it.
6776
4b41cebb 6777 Value is what FN returns. */
c6e89d6c
GM
6778
6779static int
23a96c77 6780with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
c6e89d6c
GM
6781 struct window *w;
6782 int which;
23dd2d97
KR
6783 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6784 EMACS_INT a1;
6785 Lisp_Object a2;
6786 EMACS_INT a3, a4;
c6e89d6c
GM
6787{
6788 Lisp_Object buffer;
15e26c76 6789 int this_one, the_other, clear_buffer_p, rc;
331379bf 6790 int count = SPECPDL_INDEX ();
c6e89d6c 6791
9583b2bb 6792 /* If buffers aren't live, make new ones. */
5bcfeb49 6793 ensure_echo_area_buffers ();
c6e89d6c
GM
6794
6795 clear_buffer_p = 0;
2311178e 6796
c6e89d6c
GM
6797 if (which == 0)
6798 this_one = 0, the_other = 1;
6799 else if (which > 0)
6800 this_one = 1, the_other = 0;
5f5c8ee5 6801 else
c6e89d6c
GM
6802 {
6803 this_one = 0, the_other = 1;
6804 clear_buffer_p = 1;
2311178e 6805
c6e89d6c
GM
6806 /* We need a fresh one in case the current echo buffer equals
6807 the one containing the last displayed echo area message. */
6808 if (!NILP (echo_area_buffer[this_one])
6809 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6810 echo_area_buffer[this_one] = Qnil;
c6e89d6c
GM
6811 }
6812
6813 /* Choose a suitable buffer from echo_buffer[] is we don't
6814 have one. */
6815 if (NILP (echo_area_buffer[this_one]))
6816 {
6817 echo_area_buffer[this_one]
6818 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6819 ? echo_buffer[the_other]
6820 : echo_buffer[this_one]);
6821 clear_buffer_p = 1;
6822 }
6823
6824 buffer = echo_area_buffer[this_one];
6825
1013f4e3
GM
6826 /* Don't get confused by reusing the buffer used for echoing
6827 for a different purpose. */
032906b1 6828 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
1013f4e3
GM
6829 cancel_echoing ();
6830
c6e89d6c
GM
6831 record_unwind_protect (unwind_with_echo_area_buffer,
6832 with_echo_area_buffer_unwind_data (w));
6833
6834 /* Make the echo area buffer current. Note that for display
6835 purposes, it is not necessary that the displayed window's buffer
6836 == current_buffer, except for text property lookup. So, let's
6837 only set that buffer temporarily here without doing a full
6838 Fset_window_buffer. We must also change w->pointm, though,
6839 because otherwise an assertions in unshow_buffer fails, and Emacs
6840 aborts. */
9142dd5b 6841 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
6842 if (w)
6843 {
6844 w->buffer = buffer;
6845 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6846 }
ad4f174e 6847
c6e89d6c
GM
6848 current_buffer->undo_list = Qt;
6849 current_buffer->read_only = Qnil;
bbbf6d06 6850 specbind (Qinhibit_read_only, Qt);
0b04fa5f 6851 specbind (Qinhibit_modification_hooks, Qt);
c6e89d6c
GM
6852
6853 if (clear_buffer_p && Z > BEG)
6854 del_range (BEG, Z);
6855
6856 xassert (BEGV >= BEG);
6857 xassert (ZV <= Z && ZV >= BEGV);
6858
23a96c77 6859 rc = fn (a1, a2, a3, a4);
c6e89d6c
GM
6860
6861 xassert (BEGV >= BEG);
6862 xassert (ZV <= Z && ZV >= BEGV);
6863
6864 unbind_to (count, Qnil);
6865 return rc;
5f5c8ee5
GM
6866}
6867
6868
c6e89d6c
GM
6869/* Save state that should be preserved around the call to the function
6870 FN called in with_echo_area_buffer. */
5f5c8ee5 6871
c6e89d6c
GM
6872static Lisp_Object
6873with_echo_area_buffer_unwind_data (w)
6874 struct window *w;
5f5c8ee5 6875{
c6e89d6c
GM
6876 int i = 0;
6877 Lisp_Object vector;
5f5c8ee5 6878
c6e89d6c
GM
6879 /* Reduce consing by keeping one vector in
6880 Vwith_echo_area_save_vector. */
6881 vector = Vwith_echo_area_save_vector;
6882 Vwith_echo_area_save_vector = Qnil;
2311178e 6883
c6e89d6c 6884 if (NILP (vector))
9142dd5b 6885 vector = Fmake_vector (make_number (7), Qnil);
2311178e 6886
a61b7058
GM
6887 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6888 AREF (vector, i) = Vdeactivate_mark, ++i;
6889 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
2311178e 6890
c6e89d6c
GM
6891 if (w)
6892 {
a61b7058
GM
6893 XSETWINDOW (AREF (vector, i), w); ++i;
6894 AREF (vector, i) = w->buffer; ++i;
6895 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6896 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
c6e89d6c
GM
6897 }
6898 else
6899 {
6900 int end = i + 4;
a61b7058
GM
6901 for (; i < end; ++i)
6902 AREF (vector, i) = Qnil;
c6e89d6c 6903 }
5f5c8ee5 6904
a61b7058 6905 xassert (i == ASIZE (vector));
c6e89d6c
GM
6906 return vector;
6907}
5f5c8ee5 6908
5f5c8ee5 6909
c6e89d6c
GM
6910/* Restore global state from VECTOR which was created by
6911 with_echo_area_buffer_unwind_data. */
6912
6913static Lisp_Object
6914unwind_with_echo_area_buffer (vector)
6915 Lisp_Object vector;
6916{
bbbf6d06
GM
6917 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6918 Vdeactivate_mark = AREF (vector, 1);
6919 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
c6e89d6c 6920
bbbf6d06 6921 if (WINDOWP (AREF (vector, 3)))
c6e89d6c
GM
6922 {
6923 struct window *w;
6924 Lisp_Object buffer, charpos, bytepos;
2311178e 6925
bbbf6d06
GM
6926 w = XWINDOW (AREF (vector, 3));
6927 buffer = AREF (vector, 4);
6928 charpos = AREF (vector, 5);
6929 bytepos = AREF (vector, 6);
2311178e 6930
c6e89d6c
GM
6931 w->buffer = buffer;
6932 set_marker_both (w->pointm, buffer,
6933 XFASTINT (charpos), XFASTINT (bytepos));
6934 }
6935
6936 Vwith_echo_area_save_vector = vector;
6937 return Qnil;
6938}
6939
6940
6941/* Set up the echo area for use by print functions. MULTIBYTE_P
6942 non-zero means we will print multibyte. */
6943
6944void
6945setup_echo_area_for_printing (multibyte_p)
6946 int multibyte_p;
6947{
03b0a4b4
RS
6948 /* If we can't find an echo area any more, exit. */
6949 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6950 Fkill_emacs (Qnil);
6951
5bcfeb49
GM
6952 ensure_echo_area_buffers ();
6953
c6e89d6c
GM
6954 if (!message_buf_print)
6955 {
6956 /* A message has been output since the last time we printed.
6957 Choose a fresh echo area buffer. */
6958 if (EQ (echo_area_buffer[1], echo_buffer[0]))
2311178e 6959 echo_area_buffer[0] = echo_buffer[1];
c6e89d6c
GM
6960 else
6961 echo_area_buffer[0] = echo_buffer[0];
6962
6963 /* Switch to that buffer and clear it. */
6964 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
ab2c5f0a 6965 current_buffer->truncate_lines = Qnil;
2311178e 6966
c6e89d6c 6967 if (Z > BEG)
bbbf6d06 6968 {
331379bf 6969 int count = SPECPDL_INDEX ();
bbbf6d06 6970 specbind (Qinhibit_read_only, Qt);
a67e162b 6971 /* Note that undo recording is always disabled. */
bbbf6d06
GM
6972 del_range (BEG, Z);
6973 unbind_to (count, Qnil);
6974 }
c6e89d6c
GM
6975 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6976
6977 /* Set up the buffer for the multibyteness we need. */
6978 if (multibyte_p
6979 != !NILP (current_buffer->enable_multibyte_characters))
6980 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6981
6982 /* Raise the frame containing the echo area. */
6983 if (minibuffer_auto_raise)
6984 {
886bd6f2 6985 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 6986 Lisp_Object mini_window;
886bd6f2 6987 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6988 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6989 }
6990
8a4e3c0c 6991 message_log_maybe_newline ();
c6e89d6c
GM
6992 message_buf_print = 1;
6993 }
fa77249f
GM
6994 else
6995 {
6996 if (NILP (echo_area_buffer[0]))
6997 {
6998 if (EQ (echo_area_buffer[1], echo_buffer[0]))
2311178e 6999 echo_area_buffer[0] = echo_buffer[1];
fa77249f
GM
7000 else
7001 echo_area_buffer[0] = echo_buffer[0];
7002 }
2311178e 7003
fa77249f 7004 if (current_buffer != XBUFFER (echo_area_buffer[0]))
ab2c5f0a
GM
7005 {
7006 /* Someone switched buffers between print requests. */
7007 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7008 current_buffer->truncate_lines = Qnil;
7009 }
fa77249f 7010 }
c6e89d6c
GM
7011}
7012
7013
dd2eb166
GM
7014/* Display an echo area message in window W. Value is non-zero if W's
7015 height is changed. If display_last_displayed_message_p is
7016 non-zero, display the message that was last displayed, otherwise
7017 display the current message. */
c6e89d6c
GM
7018
7019static int
7020display_echo_area (w)
7021 struct window *w;
7022{
25edb08f
GM
7023 int i, no_message_p, window_height_changed_p, count;
7024
7025 /* Temporarily disable garbage collections while displaying the echo
7026 area. This is done because a GC can print a message itself.
7027 That message would modify the echo area buffer's contents while a
7028 redisplay of the buffer is going on, and seriously confuse
7029 redisplay. */
7030 count = inhibit_garbage_collection ();
dd2eb166
GM
7031
7032 /* If there is no message, we must call display_echo_area_1
7033 nevertheless because it resizes the window. But we will have to
7034 reset the echo_area_buffer in question to nil at the end because
7035 with_echo_area_buffer will sets it to an empty buffer. */
7036 i = display_last_displayed_message_p ? 1 : 0;
7037 no_message_p = NILP (echo_area_buffer[i]);
2311178e 7038
dd2eb166
GM
7039 window_height_changed_p
7040 = with_echo_area_buffer (w, display_last_displayed_message_p,
23a96c77 7041 display_echo_area_1,
23dd2d97 7042 (EMACS_INT) w, Qnil, 0, 0);
dd2eb166
GM
7043
7044 if (no_message_p)
7045 echo_area_buffer[i] = Qnil;
25edb08f
GM
7046
7047 unbind_to (count, Qnil);
dd2eb166 7048 return window_height_changed_p;
c6e89d6c
GM
7049}
7050
7051
7052/* Helper for display_echo_area. Display the current buffer which
23a96c77
GM
7053 contains the current echo area message in window W, a mini-window,
7054 a pointer to which is passed in A1. A2..A4 are currently not used.
c6e89d6c
GM
7055 Change the height of W so that all of the message is displayed.
7056 Value is non-zero if height of W was changed. */
7057
7058static int
23a96c77 7059display_echo_area_1 (a1, a2, a3, a4)
23dd2d97
KR
7060 EMACS_INT a1;
7061 Lisp_Object a2;
7062 EMACS_INT a3, a4;
c6e89d6c 7063{
23a96c77 7064 struct window *w = (struct window *) a1;
c6e89d6c 7065 Lisp_Object window;
c6e89d6c
GM
7066 struct text_pos start;
7067 int window_height_changed_p = 0;
7068
7069 /* Do this before displaying, so that we have a large enough glyph
7070 matrix for the display. */
92a90e89 7071 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c
GM
7072
7073 /* Display. */
7074 clear_glyph_matrix (w->desired_matrix);
7075 XSETWINDOW (window, w);
7076 SET_TEXT_POS (start, BEG, BEG_BYTE);
7077 try_window (window, start);
7078
c6e89d6c
GM
7079 return window_height_changed_p;
7080}
7081
7082
92a90e89 7083/* Resize the echo area window to exactly the size needed for the
6d004fea
GM
7084 currently displayed message, if there is one. If a mini-buffer
7085 is active, don't shrink it. */
92a90e89
GM
7086
7087void
308a74d8 7088resize_echo_area_exactly ()
92a90e89
GM
7089{
7090 if (BUFFERP (echo_area_buffer[0])
7091 && WINDOWP (echo_area_window))
7092 {
7093 struct window *w = XWINDOW (echo_area_window);
7094 int resized_p;
6d004fea
GM
7095 Lisp_Object resize_exactly;
7096
7097 if (minibuf_level == 0)
7098 resize_exactly = Qt;
7099 else
7100 resize_exactly = Qnil;
2311178e 7101
23a96c77 7102 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6d004fea 7103 (EMACS_INT) w, resize_exactly, 0, 0);
92a90e89
GM
7104 if (resized_p)
7105 {
7106 ++windows_or_buffers_changed;
7107 ++update_mode_lines;
7108 redisplay_internal (0);
7109 }
7110 }
7111}
7112
7113
23a96c77 7114/* Callback function for with_echo_area_buffer, when used from
308a74d8 7115 resize_echo_area_exactly. A1 contains a pointer to the window to
6d004fea
GM
7116 resize, EXACTLY non-nil means resize the mini-window exactly to the
7117 size of the text displayed. A3 and A4 are not used. Value is what
7118 resize_mini_window returns. */
23a96c77
GM
7119
7120static int
6d004fea 7121resize_mini_window_1 (a1, exactly, a3, a4)
23dd2d97 7122 EMACS_INT a1;
6d004fea 7123 Lisp_Object exactly;
23dd2d97 7124 EMACS_INT a3, a4;
23a96c77 7125{
6d004fea 7126 return resize_mini_window ((struct window *) a1, !NILP (exactly));
23a96c77
GM
7127}
7128
7129
92a90e89
GM
7130/* Resize mini-window W to fit the size of its contents. EXACT:P
7131 means size the window exactly to the size needed. Otherwise, it's
7132 only enlarged until W's buffer is empty. Value is non-zero if
4b41cebb 7133 the window height has been changed. */
c6e89d6c 7134
9472f927 7135int
92a90e89 7136resize_mini_window (w, exact_p)
c6e89d6c 7137 struct window *w;
92a90e89 7138 int exact_p;
c6e89d6c
GM
7139{
7140 struct frame *f = XFRAME (w->frame);
7141 int window_height_changed_p = 0;
7142
7143 xassert (MINI_WINDOW_P (w));
97cafc0f 7144
2913a9c0
GM
7145 /* Don't resize windows while redisplaying a window; it would
7146 confuse redisplay functions when the size of the window they are
7147 displaying changes from under them. Such a resizing can happen,
7148 for instance, when which-func prints a long message while
7149 we are running fontification-functions. We're running these
7150 functions with safe_call which binds inhibit-redisplay to t. */
7151 if (!NILP (Vinhibit_redisplay))
64c5be50 7152 return 0;
2311178e 7153
97cafc0f 7154 /* Nil means don't try to resize. */
6422c1d7 7155 if (NILP (Vresize_mini_windows)
fa3c6b4d 7156 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
97cafc0f 7157 return 0;
2311178e 7158
c6e89d6c
GM
7159 if (!FRAME_MINIBUF_ONLY_P (f))
7160 {
7161 struct it it;
dd2eb166 7162 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
da8b7f4f 7163 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
dd2eb166 7164 int height, max_height;
da8b7f4f 7165 int unit = FRAME_LINE_HEIGHT (f);
dd2eb166 7166 struct text_pos start;
1bfdbe43
GM
7167 struct buffer *old_current_buffer = NULL;
7168
7169 if (current_buffer != XBUFFER (w->buffer))
7170 {
7171 old_current_buffer = current_buffer;
7172 set_buffer_internal (XBUFFER (w->buffer));
7173 }
9142dd5b 7174
c6e89d6c 7175 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 7176
dd2eb166
GM
7177 /* Compute the max. number of lines specified by the user. */
7178 if (FLOATP (Vmax_mini_window_height))
da8b7f4f 7179 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
dd2eb166
GM
7180 else if (INTEGERP (Vmax_mini_window_height))
7181 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
7182 else
7183 max_height = total_height / 4;
2311178e 7184
4b41cebb 7185 /* Correct that max. height if it's bogus. */
dd2eb166
GM
7186 max_height = max (1, max_height);
7187 max_height = min (total_height, max_height);
2311178e 7188
dd2eb166 7189 /* Find out the height of the text in the window. */
ad4f174e
GM
7190 if (it.truncate_lines_p)
7191 height = 1;
55b064bd 7192 else
ad4f174e
GM
7193 {
7194 last_height = 0;
7195 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7196 if (it.max_ascent == 0 && it.max_descent == 0)
7197 height = it.current_y + last_height;
7198 else
7199 height = it.current_y + it.max_ascent + it.max_descent;
3c4b7685 7200 height -= it.extra_line_spacing;
ad4f174e
GM
7201 height = (height + unit - 1) / unit;
7202 }
2311178e 7203
dd2eb166
GM
7204 /* Compute a suitable window start. */
7205 if (height > max_height)
7206 {
7207 height = max_height;
7208 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7209 move_it_vertically_backward (&it, (height - 1) * unit);
7210 start = it.current.pos;
7211 }
7212 else
7213 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7214 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 7215
6422c1d7 7216 if (EQ (Vresize_mini_windows, Qgrow_only))
dd2eb166 7217 {
6422c1d7
GM
7218 /* Let it grow only, until we display an empty message, in which
7219 case the window shrinks again. */
da8b7f4f 7220 if (height > WINDOW_TOTAL_LINES (w))
6422c1d7 7221 {
da8b7f4f 7222 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7 7223 freeze_window_starts (f, 1);
da8b7f4f
KS
7224 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7225 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7226 }
da8b7f4f 7227 else if (height < WINDOW_TOTAL_LINES (w)
6422c1d7
GM
7228 && (exact_p || BEGV == ZV))
7229 {
da8b7f4f 7230 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7
GM
7231 freeze_window_starts (f, 0);
7232 shrink_mini_window (w);
da8b7f4f 7233 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7234 }
da448723 7235 }
2311178e 7236 else
da448723 7237 {
6422c1d7 7238 /* Always resize to exact size needed. */
da8b7f4f 7239 if (height > WINDOW_TOTAL_LINES (w))
6422c1d7 7240 {
da8b7f4f 7241 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7 7242 freeze_window_starts (f, 1);
da8b7f4f
KS
7243 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7244 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7245 }
da8b7f4f 7246 else if (height < WINDOW_TOTAL_LINES (w))
6422c1d7 7247 {
da8b7f4f 7248 int old_height = WINDOW_TOTAL_LINES (w);
6422c1d7
GM
7249 freeze_window_starts (f, 0);
7250 shrink_mini_window (w);
7251
7252 if (height)
7253 {
7254 freeze_window_starts (f, 1);
da8b7f4f 7255 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
6422c1d7 7256 }
2311178e 7257
da8b7f4f 7258 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
6422c1d7 7259 }
9142dd5b 7260 }
1bfdbe43
GM
7261
7262 if (old_current_buffer)
7263 set_buffer_internal (old_current_buffer);
c6e89d6c
GM
7264 }
7265
7266 return window_height_changed_p;
7267}
7268
7269
7270/* Value is the current message, a string, or nil if there is no
7271 current message. */
7272
7273Lisp_Object
7274current_message ()
7275{
7276 Lisp_Object msg;
7277
7278 if (NILP (echo_area_buffer[0]))
7279 msg = Qnil;
7280 else
7281 {
23a96c77 7282 with_echo_area_buffer (0, 0, current_message_1,
23dd2d97 7283 (EMACS_INT) &msg, Qnil, 0, 0);
c6e89d6c
GM
7284 if (NILP (msg))
7285 echo_area_buffer[0] = Qnil;
7286 }
2311178e 7287
c6e89d6c
GM
7288 return msg;
7289}
7290
7291
7292static int
23a96c77 7293current_message_1 (a1, a2, a3, a4)
23dd2d97
KR
7294 EMACS_INT a1;
7295 Lisp_Object a2;
7296 EMACS_INT a3, a4;
c6e89d6c 7297{
23a96c77 7298 Lisp_Object *msg = (Lisp_Object *) a1;
2311178e 7299
c6e89d6c
GM
7300 if (Z > BEG)
7301 *msg = make_buffer_string (BEG, Z, 1);
7302 else
7303 *msg = Qnil;
7304 return 0;
7305}
7306
7307
7308/* Push the current message on Vmessage_stack for later restauration
7309 by restore_message. Value is non-zero if the current message isn't
7310 empty. This is a relatively infrequent operation, so it's not
7311 worth optimizing. */
7312
7313int
7314push_message ()
7315{
7316 Lisp_Object msg;
7317 msg = current_message ();
7318 Vmessage_stack = Fcons (msg, Vmessage_stack);
7319 return STRINGP (msg);
7320}
7321
7322
7323/* Restore message display from the top of Vmessage_stack. */
7324
7325void
7326restore_message ()
7327{
7328 Lisp_Object msg;
2311178e 7329
c6e89d6c
GM
7330 xassert (CONSP (Vmessage_stack));
7331 msg = XCAR (Vmessage_stack);
7332 if (STRINGP (msg))
d5db4077 7333 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
c6e89d6c
GM
7334 else
7335 message3_nolog (msg, 0, 0);
7336}
7337
7338
37d66095
RS
7339/* Handler for record_unwind_protect calling pop_message. */
7340
7341Lisp_Object
7342pop_message_unwind (dummy)
7343 Lisp_Object dummy;
7344{
7345 pop_message ();
7346 return Qnil;
7347}
7348
c6e89d6c
GM
7349/* Pop the top-most entry off Vmessage_stack. */
7350
7351void
7352pop_message ()
7353{
7354 xassert (CONSP (Vmessage_stack));
7355 Vmessage_stack = XCDR (Vmessage_stack);
7356}
7357
7358
7359/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7360 exits. If the stack is not empty, we have a missing pop_message
7361 somewhere. */
7362
7363void
7364check_message_stack ()
7365{
7366 if (!NILP (Vmessage_stack))
7367 abort ();
7368}
7369
7370
7371/* Truncate to NCHARS what will be displayed in the echo area the next
7372 time we display it---but don't redisplay it now. */
7373
7374void
7375truncate_echo_area (nchars)
7376 int nchars;
7377{
7378 if (nchars == 0)
7379 echo_area_buffer[0] = Qnil;
7380 /* A null message buffer means that the frame hasn't really been
7381 initialized yet. Error messages get reported properly by
7382 cmd_error, so this must be just an informative message; toss it. */
7383 else if (!noninteractive
7384 && INTERACTIVE
c6e89d6c 7385 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
7386 {
7387 struct frame *sf = SELECTED_FRAME ();
7388 if (FRAME_MESSAGE_BUF (sf))
23dd2d97 7389 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
886bd6f2 7390 }
c6e89d6c
GM
7391}
7392
7393
7394/* Helper function for truncate_echo_area. Truncate the current
7395 message to at most NCHARS characters. */
7396
7397static int
23a96c77 7398truncate_message_1 (nchars, a2, a3, a4)
23dd2d97
KR
7399 EMACS_INT nchars;
7400 Lisp_Object a2;
7401 EMACS_INT a3, a4;
c6e89d6c
GM
7402{
7403 if (BEG + nchars < Z)
7404 del_range (BEG + nchars, Z);
7405 if (Z == BEG)
7406 echo_area_buffer[0] = Qnil;
7407 return 0;
7408}
7409
7410
7411/* Set the current message to a substring of S or STRING.
7412
7413 If STRING is a Lisp string, set the message to the first NBYTES
7414 bytes from STRING. NBYTES zero means use the whole string. If
7415 STRING is multibyte, the message will be displayed multibyte.
7416
7417 If S is not null, set the message to the first LEN bytes of S. LEN
7418 zero means use the whole string. MULTIBYTE_P non-zero means S is
7419 multibyte. Display the message multibyte in that case. */
7420
7421void
7422set_message (s, string, nbytes, multibyte_p)
50f80c2f 7423 const char *s;
c6e89d6c 7424 Lisp_Object string;
e3383b6f 7425 int nbytes, multibyte_p;
c6e89d6c
GM
7426{
7427 message_enable_multibyte
7428 = ((s && multibyte_p)
7429 || (STRINGP (string) && STRING_MULTIBYTE (string)));
2311178e 7430
23a96c77
GM
7431 with_echo_area_buffer (0, -1, set_message_1,
7432 (EMACS_INT) s, string, nbytes, multibyte_p);
c6e89d6c 7433 message_buf_print = 0;
21fdfb65 7434 help_echo_showing_p = 0;
c6e89d6c
GM
7435}
7436
7437
7438/* Helper function for set_message. Arguments have the same meaning
23a96c77
GM
7439 as there, with A1 corresponding to S and A2 corresponding to STRING
7440 This function is called with the echo area buffer being
c6e89d6c
GM
7441 current. */
7442
7443static int
23a96c77 7444set_message_1 (a1, a2, nbytes, multibyte_p)
23dd2d97
KR
7445 EMACS_INT a1;
7446 Lisp_Object a2;
7447 EMACS_INT nbytes, multibyte_p;
c6e89d6c 7448{
50f80c2f 7449 const char *s = (const char *) a1;
23dd2d97 7450 Lisp_Object string = a2;
2311178e 7451
c6e89d6c 7452 xassert (BEG == Z);
2311178e 7453
c6e89d6c
GM
7454 /* Change multibyteness of the echo buffer appropriately. */
7455 if (message_enable_multibyte
7456 != !NILP (current_buffer->enable_multibyte_characters))
7457 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7458
ad4f174e 7459 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
2311178e 7460
c6e89d6c
GM
7461 /* Insert new message at BEG. */
7462 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7463
7464 if (STRINGP (string))
7465 {
7466 int nchars;
2311178e 7467
c6e89d6c 7468 if (nbytes == 0)
2051c264 7469 nbytes = SBYTES (string);
c6e89d6c 7470 nchars = string_byte_to_char (string, nbytes);
2311178e 7471
c6e89d6c
GM
7472 /* This function takes care of single/multibyte conversion. We
7473 just have to ensure that the echo area buffer has the right
7474 setting of enable_multibyte_characters. */
7475 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7476 }
7477 else if (s)
7478 {
7479 if (nbytes == 0)
7480 nbytes = strlen (s);
2311178e 7481
c6e89d6c
GM
7482 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7483 {
7484 /* Convert from multi-byte to single-byte. */
7485 int i, c, n;
7486 unsigned char work[1];
2311178e 7487
c6e89d6c
GM
7488 /* Convert a multibyte string to single-byte. */
7489 for (i = 0; i < nbytes; i += n)
7490 {
7491 c = string_char_and_length (s + i, nbytes - i, &n);
7492 work[0] = (SINGLE_BYTE_CHAR_P (c)
7493 ? c
7494 : multibyte_char_to_unibyte (c, Qnil));
7495 insert_1_both (work, 1, 1, 1, 0, 0);
7496 }
7497 }
7498 else if (!multibyte_p
7499 && !NILP (current_buffer->enable_multibyte_characters))
7500 {
7501 /* Convert from single-byte to multi-byte. */
7502 int i, c, n;
50f80c2f 7503 const unsigned char *msg = (const unsigned char *) s;
260a86a0 7504 unsigned char str[MAX_MULTIBYTE_LENGTH];
2311178e 7505
c6e89d6c
GM
7506 /* Convert a single-byte string to multibyte. */
7507 for (i = 0; i < nbytes; i++)
7508 {
7509 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
7510 n = CHAR_STRING (c, str);
7511 insert_1_both (str, 1, n, 1, 0, 0);
c6e89d6c
GM
7512 }
7513 }
7514 else
7515 insert_1 (s, nbytes, 1, 0, 0);
7516 }
7517
7518 return 0;
7519}
7520
7521
7522/* Clear messages. CURRENT_P non-zero means clear the current
7523 message. LAST_DISPLAYED_P non-zero means clear the message
7524 last displayed. */
7525
7526void
7527clear_message (current_p, last_displayed_p)
7528 int current_p, last_displayed_p;
7529{
7530 if (current_p)
6e019995
GM
7531 {
7532 echo_area_buffer[0] = Qnil;
7533 message_cleared_p = 1;
7534 }
2311178e 7535
c6e89d6c
GM
7536 if (last_displayed_p)
7537 echo_area_buffer[1] = Qnil;
2311178e 7538
c6e89d6c
GM
7539 message_buf_print = 0;
7540}
7541
7542/* Clear garbaged frames.
7543
7544 This function is used where the old redisplay called
7545 redraw_garbaged_frames which in turn called redraw_frame which in
7546 turn called clear_frame. The call to clear_frame was a source of
7547 flickering. I believe a clear_frame is not necessary. It should
7548 suffice in the new redisplay to invalidate all current matrices,
7549 and ensure a complete redisplay of all windows. */
7550
7551static void
7552clear_garbaged_frames ()
7553{
5f5c8ee5
GM
7554 if (frame_garbaged)
7555 {
5f5c8ee5 7556 Lisp_Object tail, frame;
5fb96e96 7557 int changed_count = 0;
9628b887 7558
5f5c8ee5
GM
7559 FOR_EACH_FRAME (tail, frame)
7560 {
7561 struct frame *f = XFRAME (frame);
9628b887 7562
5f5c8ee5
GM
7563 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7564 {
6bb95882 7565 if (f->resized_p)
573e57f1 7566 Fredraw_frame (frame);
5f5c8ee5 7567 clear_current_matrices (f);
5fb96e96 7568 changed_count++;
5f5c8ee5 7569 f->garbaged = 0;
6bb95882 7570 f->resized_p = 0;
5f5c8ee5
GM
7571 }
7572 }
9628b887 7573
5f5c8ee5 7574 frame_garbaged = 0;
5fb96e96
RS
7575 if (changed_count)
7576 ++windows_or_buffers_changed;
5f5c8ee5 7577 }
c6e89d6c 7578}
5f5c8ee5 7579
5f5c8ee5 7580
886bd6f2
GM
7581/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7582 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 7583 mini-windows height has been changed. */
5f5c8ee5 7584
c6e89d6c
GM
7585static int
7586echo_area_display (update_frame_p)
7587 int update_frame_p;
7588{
7589 Lisp_Object mini_window;
7590 struct window *w;
7591 struct frame *f;
7592 int window_height_changed_p = 0;
886bd6f2 7593 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 7594
886bd6f2 7595 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
7596 w = XWINDOW (mini_window);
7597 f = XFRAME (WINDOW_FRAME (w));
7598
7599 /* Don't display if frame is invisible or not yet initialized. */
7600 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7601 return 0;
5f5c8ee5 7602
1a578e9b 7603/* The terminal frame is used as the first Emacs frame on the Mac OS. */
e0f712ba 7604#ifndef MAC_OS8
1ab3e082 7605#ifdef HAVE_WINDOW_SYSTEM
c6e89d6c
GM
7606 /* When Emacs starts, selected_frame may be a visible terminal
7607 frame, even if we run under a window system. If we let this
7608 through, a message would be displayed on the terminal. */
9628b887 7609 if (!FRAME_WINDOW_P (XFRAME (selected_frame))
622e3754 7610 && !NILP (Vwindow_system))
c6e89d6c 7611 return 0;
1ab3e082 7612#endif /* HAVE_WINDOW_SYSTEM */
1a578e9b 7613#endif
c6e89d6c
GM
7614
7615 /* Redraw garbaged frames. */
7616 if (frame_garbaged)
7617 clear_garbaged_frames ();
7618
7619 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7620 {
7621 echo_area_window = mini_window;
7622 window_height_changed_p = display_echo_area (w);
5f5c8ee5 7623 w->must_be_updated_p = 1;
c59c668a 7624
d4358b37
GM
7625 /* Update the display, unless called from redisplay_internal.
7626 Also don't update the screen during redisplay itself. The
7627 update will happen at the end of redisplay, and an update
7628 here could cause confusion. */
7629 if (update_frame_p && !redisplaying_p)
5f5c8ee5 7630 {
715e84c9 7631 int n = 0;
edc68111 7632
715e84c9
GM
7633 /* If the display update has been interrupted by pending
7634 input, update mode lines in the frame. Due to the
7635 pending input, it might have been that redisplay hasn't
7636 been called, so that mode lines above the echo area are
7637 garbaged. This looks odd, so we prevent it here. */
7638 if (!display_completed)
7639 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
2311178e 7640
8af1308e
GM
7641 if (window_height_changed_p
7642 /* Don't do this if Emacs is shutting down. Redisplay
7643 needs to run hooks. */
7644 && !NILP (Vrun_hooks))
c59c668a 7645 {
c06017fb
GM
7646 /* Must update other windows. Likewise as in other
7647 cases, don't let this update be interrupted by
7648 pending input. */
331379bf 7649 int count = SPECPDL_INDEX ();
c06017fb 7650 specbind (Qredisplay_dont_pause, Qt);
edc68111 7651 windows_or_buffers_changed = 1;
c59c668a 7652 redisplay_internal (0);
c06017fb 7653 unbind_to (count, Qnil);
c59c668a 7654 }
715e84c9 7655 else if (FRAME_WINDOW_P (f) && n == 0)
5f5c8ee5 7656 {
edc68111 7657 /* Window configuration is the same as before.
715e84c9
GM
7658 Can do with a display update of the echo area,
7659 unless we displayed some mode lines. */
5f5c8ee5
GM
7660 update_single_window (w, 1);
7661 rif->flush_display (f);
7662 }
7663 else
7664 update_frame (f, 1, 1);
31b6671b
GM
7665
7666 /* If cursor is in the echo area, make sure that the next
7667 redisplay displays the minibuffer, so that the cursor will
7668 be replaced with what the minibuffer wants. */
7669 if (cursor_in_echo_area)
7670 ++windows_or_buffers_changed;
5f5c8ee5
GM
7671 }
7672 }
7673 else if (!EQ (mini_window, selected_window))
7674 windows_or_buffers_changed++;
c59c668a
GM
7675
7676 /* Last displayed message is now the current message. */
dd2eb166 7677 echo_area_buffer[1] = echo_area_buffer[0];
2311178e 7678
5f5c8ee5
GM
7679 /* Prevent redisplay optimization in redisplay_internal by resetting
7680 this_line_start_pos. This is done because the mini-buffer now
7681 displays the message instead of its buffer text. */
7682 if (EQ (mini_window, selected_window))
7683 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
7684
7685 return window_height_changed_p;
5f5c8ee5
GM
7686}
7687
7688
7689\f
7690/***********************************************************************
7691 Frame Titles
7692 ***********************************************************************/
7693
7694
8143e6ab
KS
7695/* The frame title buffering code is also used by Fformat_mode_line.
7696 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
5f5c8ee5
GM
7697
7698/* A buffer for constructing frame titles in it; allocated from the
7699 heap in init_xdisp and resized as needed in store_frame_title_char. */
7700
7701static char *frame_title_buf;
7702
7703/* The buffer's end, and a current output position in it. */
7704
7705static char *frame_title_buf_end;
7706static char *frame_title_ptr;
7707
7708
7709/* Store a single character C for the frame title in frame_title_buf.
7710 Re-allocate frame_title_buf if necessary. */
7711
7712static void
5d15cc8f
DL
7713#ifdef PROTOTYPES
7714store_frame_title_char (char c)
7715#else
5f5c8ee5
GM
7716store_frame_title_char (c)
7717 char c;
5d15cc8f 7718#endif
5f5c8ee5
GM
7719{
7720 /* If output position has reached the end of the allocated buffer,
7721 double the buffer's size. */
7722 if (frame_title_ptr == frame_title_buf_end)
7723 {
7724 int len = frame_title_ptr - frame_title_buf;
7725 int new_size = 2 * len * sizeof *frame_title_buf;
7726 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7727 frame_title_buf_end = frame_title_buf + new_size;
7728 frame_title_ptr = frame_title_buf + len;
7729 }
7730
7731 *frame_title_ptr++ = c;
7732}
7733
7734
7735/* Store part of a frame title in frame_title_buf, beginning at
d26b89b8
KH
7736 frame_title_ptr. STR is the string to store. Do not copy
7737 characters that yield more columns than PRECISION; PRECISION <= 0
7738 means copy the whole string. Pad with spaces until FIELD_WIDTH
7739 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7740 pad. Called from display_mode_element when it is used to build a
7741 frame title. */
5f5c8ee5
GM
7742
7743static int
7744store_frame_title (str, field_width, precision)
50f80c2f 7745 const unsigned char *str;
5f5c8ee5
GM
7746 int field_width, precision;
7747{
7748 int n = 0;
3b552d56 7749 int dummy, nbytes;
5f5c8ee5
GM
7750
7751 /* Copy at most PRECISION chars from STR. */
d26b89b8
KH
7752 nbytes = strlen (str);
7753 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7754 while (nbytes--)
7755 store_frame_title_char (*str++);
5f5c8ee5
GM
7756
7757 /* Fill up with spaces until FIELD_WIDTH reached. */
7758 while (field_width > 0
7759 && n < field_width)
7760 {
7761 store_frame_title_char (' ');
7762 ++n;
7763 }
7764
7765 return n;
7766}
7767
8143e6ab 7768#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
7769
7770/* Set the title of FRAME, if it has changed. The title format is
7771 Vicon_title_format if FRAME is iconified, otherwise it is
7772 frame_title_format. */
7773
7774static void
7775x_consider_frame_title (frame)
7776 Lisp_Object frame;
7777{
7778 struct frame *f = XFRAME (frame);
7779
7780 if (FRAME_WINDOW_P (f)
7781 || FRAME_MINIBUF_ONLY_P (f)
7782 || f->explicit_name)
7783 {
7784 /* Do we have more than one visible frame on this X display? */
7785 Lisp_Object tail;
7786 Lisp_Object fmt;
7787 struct buffer *obuf;
7788 int len;
7789 struct it it;
7790
9472f927 7791 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 7792 {
74779f52
JR
7793 Lisp_Object other_frame = XCAR (tail);
7794 struct frame *tf = XFRAME (other_frame);
5f5c8ee5 7795
2311178e 7796 if (tf != f
5f5c8ee5
GM
7797 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7798 && !FRAME_MINIBUF_ONLY_P (tf)
74779f52 7799 && !EQ (other_frame, tip_frame)
5f5c8ee5
GM
7800 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7801 break;
7802 }
7803
7804 /* Set global variable indicating that multiple frames exist. */
7805 multiple_frames = CONSP (tail);
7806
7807 /* Switch to the buffer of selected window of the frame. Set up
7808 frame_title_ptr so that display_mode_element will output into it;
7809 then display the title. */
7810 obuf = current_buffer;
bb336f8d 7811 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
5f5c8ee5
GM
7812 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7813 frame_title_ptr = frame_title_buf;
7814 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7815 NULL, DEFAULT_FACE_ID);
c53a1624 7816 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
d26b89b8 7817 len = frame_title_ptr - frame_title_buf;
5f5c8ee5 7818 frame_title_ptr = NULL;
bb336f8d 7819 set_buffer_internal_1 (obuf);
5f5c8ee5
GM
7820
7821 /* Set the title only if it's changed. This avoids consing in
7822 the common case where it hasn't. (If it turns out that we've
7823 already wasted too much time by walking through the list with
7824 display_mode_element, then we might need to optimize at a
7825 higher level than this.) */
2311178e 7826 if (! STRINGP (f->name)
2051c264
GM
7827 || SBYTES (f->name) != len
7828 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
5f5c8ee5
GM
7829 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7830 }
7831}
7832
5f5c8ee5
GM
7833#endif /* not HAVE_WINDOW_SYSTEM */
7834
7835
7836
7837\f
7838/***********************************************************************
7839 Menu Bars
7840 ***********************************************************************/
7841
7842
7843/* Prepare for redisplay by updating menu-bar item lists when
7844 appropriate. This can call eval. */
7845
7846void
7847prepare_menu_bars ()
7848{
7849 int all_windows;
7850 struct gcpro gcpro1, gcpro2;
7851 struct frame *f;
6b5c4794 7852 Lisp_Object tooltip_frame;
5f5c8ee5 7853
86ffe5cd 7854#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
7855 tooltip_frame = tip_frame;
7856#else
6b5c4794 7857 tooltip_frame = Qnil;
5f5c8ee5
GM
7858#endif
7859
7860 /* Update all frame titles based on their buffer names, etc. We do
7861 this before the menu bars so that the buffer-menu will show the
7862 up-to-date frame titles. */
7863#ifdef HAVE_WINDOW_SYSTEM
7864 if (windows_or_buffers_changed || update_mode_lines)
7865 {
7866 Lisp_Object tail, frame;
7867
7868 FOR_EACH_FRAME (tail, frame)
7869 {
7870 f = XFRAME (frame);
6b5c4794 7871 if (!EQ (frame, tooltip_frame)
5f5c8ee5
GM
7872 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7873 x_consider_frame_title (frame);
7874 }
7875 }
7876#endif /* HAVE_WINDOW_SYSTEM */
7877
7878 /* Update the menu bar item lists, if appropriate. This has to be
7879 done before any actual redisplay or generation of display lines. */
2311178e 7880 all_windows = (update_mode_lines
5f5c8ee5
GM
7881 || buffer_shared > 1
7882 || windows_or_buffers_changed);
7883 if (all_windows)
7884 {
7885 Lisp_Object tail, frame;
331379bf 7886 int count = SPECPDL_INDEX ();
5f5c8ee5
GM
7887
7888 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7889
7890 FOR_EACH_FRAME (tail, frame)
7891 {
7892 f = XFRAME (frame);
7893
7894 /* Ignore tooltip frame. */
6b5c4794 7895 if (EQ (frame, tooltip_frame))
5f5c8ee5 7896 continue;
2311178e 7897
5f5c8ee5
GM
7898 /* If a window on this frame changed size, report that to
7899 the user and clear the size-change flag. */
7900 if (FRAME_WINDOW_SIZES_CHANGED (f))
7901 {
7902 Lisp_Object functions;
2311178e 7903
5f5c8ee5
GM
7904 /* Clear flag first in case we get an error below. */
7905 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7906 functions = Vwindow_size_change_functions;
7907 GCPRO2 (tail, functions);
2311178e 7908
5f5c8ee5
GM
7909 while (CONSP (functions))
7910 {
7911 call1 (XCAR (functions), frame);
7912 functions = XCDR (functions);
7913 }
7914 UNGCPRO;
7915 }
2311178e 7916
5f5c8ee5
GM
7917 GCPRO1 (tail);
7918 update_menu_bar (f, 0);
7919#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 7920 update_tool_bar (f, 0);
5f5c8ee5
GM
7921#endif
7922 UNGCPRO;
7923 }
7924
7925 unbind_to (count, Qnil);
7926 }
7927 else
7928 {
886bd6f2
GM
7929 struct frame *sf = SELECTED_FRAME ();
7930 update_menu_bar (sf, 1);
5f5c8ee5 7931#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 7932 update_tool_bar (sf, 1);
5f5c8ee5
GM
7933#endif
7934 }
7935
7936 /* Motif needs this. See comment in xmenu.c. Turn it off when
7937 pending_menu_activation is not defined. */
7938#ifdef USE_X_TOOLKIT
7939 pending_menu_activation = 0;
7940#endif
7941}
7942
7943
7944/* Update the menu bar item list for frame F. This has to be done
7945 before we start to fill in any display lines, because it can call
7946 eval.
7947
7948 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7949
7950static void
7951update_menu_bar (f, save_match_data)
7952 struct frame *f;
7953 int save_match_data;
7954{
7955 Lisp_Object window;
7956 register struct window *w;
7957
e1477f43
GM
7958 /* If called recursively during a menu update, do nothing. This can
7959 happen when, for instance, an activate-menubar-hook causes a
7960 redisplay. */
7961 if (inhibit_menubar_update)
7962 return;
7963
5f5c8ee5
GM
7964 window = FRAME_SELECTED_WINDOW (f);
7965 w = XWINDOW (window);
2311178e 7966
84f2e615 7967#if 0 /* The if statement below this if statement used to include the
a5f08374
RS
7968 condition !NILP (w->update_mode_line), rather than using
7969 update_mode_lines directly, and this if statement may have
7970 been added to make that condition work. Now the if
2311178e 7971 statement below matches its comment, this isn't needed. */
5f5c8ee5
GM
7972 if (update_mode_lines)
7973 w->update_mode_line = Qt;
a5f08374 7974#endif
5f5c8ee5
GM
7975
7976 if (FRAME_WINDOW_P (f)
7977 ?
488dd4c4
JD
7978#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7979 || defined (USE_GTK)
2311178e 7980 FRAME_EXTERNAL_MENU_BAR (f)
5f5c8ee5
GM
7981#else
7982 FRAME_MENU_BAR_LINES (f) > 0
7983#endif
7984 : FRAME_MENU_BAR_LINES (f) > 0)
7985 {
7986 /* If the user has switched buffers or windows, we need to
7987 recompute to reflect the new bindings. But we'll
7988 recompute when update_mode_lines is set too; that means
7989 that people can use force-mode-line-update to request
7990 that the menu bar be recomputed. The adverse effect on
7991 the rest of the redisplay algorithm is about the same as
7992 windows_or_buffers_changed anyway. */
7993 if (windows_or_buffers_changed
a5f08374
RS
7994 /* This used to test w->update_mode_line, but we believe
7995 there is no need to recompute the menu in that case. */
7996 || update_mode_lines
5f5c8ee5
GM
7997 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7998 < BUF_MODIFF (XBUFFER (w->buffer)))
7999 != !NILP (w->last_had_star))
8000 || ((!NILP (Vtransient_mark_mode)
8001 && !NILP (XBUFFER (w->buffer)->mark_active))
8002 != !NILP (w->region_showing)))
8003 {
8004 struct buffer *prev = current_buffer;
331379bf 8005 int count = SPECPDL_INDEX ();
5f5c8ee5 8006
e1477f43
GM
8007 specbind (Qinhibit_menubar_update, Qt);
8008
5f5c8ee5
GM
8009 set_buffer_internal_1 (XBUFFER (w->buffer));
8010 if (save_match_data)
8011 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8012 if (NILP (Voverriding_local_map_menu_flag))
8013 {
8014 specbind (Qoverriding_terminal_local_map, Qnil);
8015 specbind (Qoverriding_local_map, Qnil);
8016 }
8017
8018 /* Run the Lucid hook. */
65048e97 8019 safe_run_hooks (Qactivate_menubar_hook);
2311178e 8020
5f5c8ee5
GM
8021 /* If it has changed current-menubar from previous value,
8022 really recompute the menu-bar from the value. */
8023 if (! NILP (Vlucid_menu_bar_dirty_flag))
8024 call0 (Qrecompute_lucid_menubar);
2311178e 8025
5f5c8ee5
GM
8026 safe_run_hooks (Qmenu_bar_update_hook);
8027 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
2311178e 8028
5f5c8ee5 8029 /* Redisplay the menu bar in case we changed it. */
488dd4c4
JD
8030#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8031 || defined (USE_GTK)
1a578e9b 8032 if (FRAME_WINDOW_P (f)
e0f712ba 8033#if defined (MAC_OS)
1a578e9b
AC
8034 /* All frames on Mac OS share the same menubar. So only the
8035 selected frame should be allowed to set it. */
8036 && f == SELECTED_FRAME ()
8037#endif
8038 )
5f5c8ee5
GM
8039 set_frame_menubar (f, 0, 0);
8040 else
8041 /* On a terminal screen, the menu bar is an ordinary screen
8042 line, and this makes it get updated. */
8043 w->update_mode_line = Qt;
488dd4c4 8044#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
5f5c8ee5
GM
8045 /* In the non-toolkit version, the menu bar is an ordinary screen
8046 line, and this makes it get updated. */
8047 w->update_mode_line = Qt;
488dd4c4 8048#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
5f5c8ee5
GM
8049
8050 unbind_to (count, Qnil);
8051 set_buffer_internal_1 (prev);
8052 }
8053 }
8054}
8055
8056
8057\f
fa3c6b4d
KS
8058/***********************************************************************
8059 Output Cursor
8060 ***********************************************************************/
8061
1dabd0cf
KS
8062#ifdef HAVE_WINDOW_SYSTEM
8063
fa3c6b4d
KS
8064/* EXPORT:
8065 Nominal cursor position -- where to draw output.
8066 HPOS and VPOS are window relative glyph matrix coordinates.
8067 X and Y are window relative pixel coordinates. */
8068
8069struct cursor_pos output_cursor;
8070
8071
8072/* EXPORT:
8073 Set the global variable output_cursor to CURSOR. All cursor
8074 positions are relative to updated_window. */
8075
8076void
8077set_output_cursor (cursor)
8078 struct cursor_pos *cursor;
8079{
8080 output_cursor.hpos = cursor->hpos;
8081 output_cursor.vpos = cursor->vpos;
8082 output_cursor.x = cursor->x;
8083 output_cursor.y = cursor->y;
8084}
8085
8086
8087/* EXPORT for RIF:
8088 Set a nominal cursor position.
8089
8090 HPOS and VPOS are column/row positions in a window glyph matrix. X
8091 and Y are window text area relative pixel positions.
8092
8093 If this is done during an update, updated_window will contain the
8094 window that is being updated and the position is the future output
8095 cursor position for that window. If updated_window is null, use
8096 selected_window and display the cursor at the given position. */
8097
8098void
8099x_cursor_to (vpos, hpos, y, x)
8100 int vpos, hpos, y, x;
8101{
8102 struct window *w;
8103
8104 /* If updated_window is not set, work on selected_window. */
8105 if (updated_window)
8106 w = updated_window;
8107 else
8108 w = XWINDOW (selected_window);
8109
8110 /* Set the output cursor. */
8111 output_cursor.hpos = hpos;
8112 output_cursor.vpos = vpos;
8113 output_cursor.x = x;
8114 output_cursor.y = y;
8115
8116 /* If not called as part of an update, really display the cursor.
8117 This will also set the cursor position of W. */
8118 if (updated_window == NULL)
8119 {
8120 BLOCK_INPUT;
8121 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8122 if (rif->flush_display_optional)
8123 rif->flush_display_optional (SELECTED_FRAME ());
8124 UNBLOCK_INPUT;
8125 }
8126}
8127
1dabd0cf 8128#endif /* HAVE_WINDOW_SYSTEM */
fa3c6b4d
KS
8129
8130\f
5f5c8ee5 8131/***********************************************************************
e037b9ec 8132 Tool-bars
5f5c8ee5
GM
8133 ***********************************************************************/
8134
8135#ifdef HAVE_WINDOW_SYSTEM
8136
fa3c6b4d
KS
8137/* Where the mouse was last time we reported a mouse event. */
8138
8139FRAME_PTR last_mouse_frame;
8140
8141/* Tool-bar item index of the item on which a mouse button was pressed
8142 or -1. */
8143
8144int last_tool_bar_item;
8145
8146
e037b9ec 8147/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
8148 before we start to fill in any display lines. Called from
8149 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8150 and restore it here. */
8151
8152static void
e037b9ec 8153update_tool_bar (f, save_match_data)
5f5c8ee5
GM
8154 struct frame *f;
8155 int save_match_data;
8156{
488dd4c4 8157#ifdef USE_GTK
810f2256 8158 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
488dd4c4
JD
8159#else
8160 int do_update = WINDOWP (f->tool_bar_window)
da8b7f4f 8161 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
488dd4c4
JD
8162#endif
8163
8164 if (do_update)
5f5c8ee5
GM
8165 {
8166 Lisp_Object window;
8167 struct window *w;
8168
8169 window = FRAME_SELECTED_WINDOW (f);
8170 w = XWINDOW (window);
2311178e 8171
5f5c8ee5
GM
8172 /* If the user has switched buffers or windows, we need to
8173 recompute to reflect the new bindings. But we'll
8174 recompute when update_mode_lines is set too; that means
8175 that people can use force-mode-line-update to request
8176 that the menu bar be recomputed. The adverse effect on
8177 the rest of the redisplay algorithm is about the same as
8178 windows_or_buffers_changed anyway. */
8179 if (windows_or_buffers_changed
8180 || !NILP (w->update_mode_line)
84f2e615 8181 || update_mode_lines
5f5c8ee5
GM
8182 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8183 < BUF_MODIFF (XBUFFER (w->buffer)))
8184 != !NILP (w->last_had_star))
8185 || ((!NILP (Vtransient_mark_mode)
8186 && !NILP (XBUFFER (w->buffer)->mark_active))
8187 != !NILP (w->region_showing)))
8188 {
8189 struct buffer *prev = current_buffer;
331379bf 8190 int count = SPECPDL_INDEX ();
84f2e615
RS
8191 Lisp_Object old_tool_bar;
8192 struct gcpro gcpro1;
a2889657 8193
5f5c8ee5
GM
8194 /* Set current_buffer to the buffer of the selected
8195 window of the frame, so that we get the right local
8196 keymaps. */
8197 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 8198
5f5c8ee5
GM
8199 /* Save match data, if we must. */
8200 if (save_match_data)
8201 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8202
8203 /* Make sure that we don't accidentally use bogus keymaps. */
8204 if (NILP (Voverriding_local_map_menu_flag))
8205 {
8206 specbind (Qoverriding_terminal_local_map, Qnil);
8207 specbind (Qoverriding_local_map, Qnil);
1f40cad2 8208 }
1f40cad2 8209
84f2e615
RS
8210 old_tool_bar = f->tool_bar_items;
8211 GCPRO1 (old_tool_bar);
8212
e037b9ec 8213 /* Build desired tool-bar items from keymaps. */
e3b2c21f 8214 BLOCK_INPUT;
7464726e
GM
8215 f->tool_bar_items
8216 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
e3b2c21f 8217 UNBLOCK_INPUT;
2311178e 8218
84f2e615
RS
8219 /* Redisplay the tool-bar if we changed it. */
8220 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8221 w->update_mode_line = Qt;
1e802fb5
SM
8222
8223 UNGCPRO;
5f5c8ee5
GM
8224
8225 unbind_to (count, Qnil);
8226 set_buffer_internal_1 (prev);
81d478f3 8227 }
a2889657
JB
8228 }
8229}
8230
6c4429a5 8231
e037b9ec 8232/* Set F->desired_tool_bar_string to a Lisp string representing frame
7464726e 8233 F's desired tool-bar contents. F->tool_bar_items must have
5f5c8ee5
GM
8234 been set up previously by calling prepare_menu_bars. */
8235
a2889657 8236static void
e037b9ec 8237build_desired_tool_bar_string (f)
5f5c8ee5 8238 struct frame *f;
a2889657 8239{
a23887b9 8240 int i, size, size_needed;
5f5c8ee5
GM
8241 struct gcpro gcpro1, gcpro2, gcpro3;
8242 Lisp_Object image, plist, props;
a2889657 8243
5f5c8ee5
GM
8244 image = plist = props = Qnil;
8245 GCPRO3 (image, plist, props);
a2889657 8246
e037b9ec 8247 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5 8248 Otherwise, make a new string. */
2311178e 8249
5f5c8ee5 8250 /* The size of the string we might be able to reuse. */
e037b9ec 8251 size = (STRINGP (f->desired_tool_bar_string)
2051c264 8252 ? SCHARS (f->desired_tool_bar_string)
5f5c8ee5
GM
8253 : 0);
8254
b94c0d9c 8255 /* We need one space in the string for each image. */
a23887b9 8256 size_needed = f->n_tool_bar_items;
2311178e 8257
b94c0d9c 8258 /* Reuse f->desired_tool_bar_string, if possible. */
cb2ddc53 8259 if (size < size_needed || NILP (f->desired_tool_bar_string))
6fc556fd
KR
8260 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8261 make_number (' '));
5f5c8ee5
GM
8262 else
8263 {
8264 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8265 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 8266 props, f->desired_tool_bar_string);
5f5c8ee5 8267 }
a2889657 8268
5f5c8ee5 8269 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
8270 put a `menu_item' property on tool-bar items with a value that
8271 is the index of the item in F's tool-bar item vector. */
a23887b9 8272 for (i = 0; i < f->n_tool_bar_items; ++i)
a2889657 8273 {
7464726e 8274#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
5f5c8ee5 8275
e037b9ec
GM
8276 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8277 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
35a41507 8278 int hmargin, vmargin, relief, idx, end;
ecd01a0e 8279 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
5f5c8ee5
GM
8280
8281 /* If image is a vector, choose the image according to the
8282 button state. */
e037b9ec 8283 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
8284 if (VECTORP (image))
8285 {
5f5c8ee5
GM
8286 if (enabled_p)
8287 idx = (selected_p
e037b9ec
GM
8288 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8289 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
8290 else
8291 idx = (selected_p
e037b9ec
GM
8292 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8293 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
2311178e 8294
37e4e482
GM
8295 xassert (ASIZE (image) >= idx);
8296 image = AREF (image, idx);
5f5c8ee5 8297 }
37e4e482
GM
8298 else
8299 idx = -1;
5f5c8ee5
GM
8300
8301 /* Ignore invalid image specifications. */
8302 if (!valid_image_p (image))
8303 continue;
8304
e037b9ec 8305 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
8306 plist = Fcopy_sequence (XCDR (image));
8307
8308 /* Compute margin and relief to draw. */
9f864bf9 8309 relief = (tool_bar_button_relief >= 0
c3d76173
GM
8310 ? tool_bar_button_relief
8311 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
35a41507
GM
8312 hmargin = vmargin = relief;
8313
8314 if (INTEGERP (Vtool_bar_button_margin)
8315 && XINT (Vtool_bar_button_margin) > 0)
8316 {
8317 hmargin += XFASTINT (Vtool_bar_button_margin);
8318 vmargin += XFASTINT (Vtool_bar_button_margin);
8319 }
8320 else if (CONSP (Vtool_bar_button_margin))
8321 {
8322 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8323 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8324 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
2311178e 8325
35a41507
GM
8326 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8327 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8328 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8329 }
2311178e 8330
e037b9ec 8331 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
8332 {
8333 /* Add a `:relief' property to the image spec if the item is
8334 selected. */
8335 if (selected_p)
8336 {
8337 plist = Fplist_put (plist, QCrelief, make_number (-relief));
35a41507
GM
8338 hmargin -= relief;
8339 vmargin -= relief;
5f5c8ee5
GM
8340 }
8341 }
8342 else
8343 {
8344 /* If image is selected, display it pressed, i.e. with a
8345 negative relief. If it's not selected, display it with a
8346 raised relief. */
8347 plist = Fplist_put (plist, QCrelief,
8348 (selected_p
8349 ? make_number (-relief)
8350 : make_number (relief)));
35a41507
GM
8351 hmargin -= relief;
8352 vmargin -= relief;
5f5c8ee5
GM
8353 }
8354
8355 /* Put a margin around the image. */
35a41507
GM
8356 if (hmargin || vmargin)
8357 {
8358 if (hmargin == vmargin)
8359 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8360 else
8361 plist = Fplist_put (plist, QCmargin,
8362 Fcons (make_number (hmargin),
8363 make_number (vmargin)));
8364 }
2311178e 8365
37e4e482
GM
8366 /* If button is not enabled, and we don't have special images
8367 for the disabled state, make the image appear disabled by
5f5c8ee5 8368 applying an appropriate algorithm to it. */
37e4e482 8369 if (!enabled_p && idx < 0)
ecd01a0e 8370 plist = Fplist_put (plist, QCconversion, Qdisabled);
2311178e 8371
5f5c8ee5
GM
8372 /* Put a `display' text property on the string for the image to
8373 display. Put a `menu-item' property on the string that gives
e037b9ec 8374 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
8375 vector. */
8376 image = Fcons (Qimage, plist);
8377 props = list4 (Qdisplay, image,
a23887b9
GM
8378 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8379
8380 /* Let the last image hide all remaining spaces in the tool bar
8381 string. The string can be longer than needed when we reuse a
8382 previous string. */
8383 if (i + 1 == f->n_tool_bar_items)
2051c264 8384 end = SCHARS (f->desired_tool_bar_string);
a23887b9
GM
8385 else
8386 end = i + 1;
8387 Fadd_text_properties (make_number (i), make_number (end),
e037b9ec 8388 props, f->desired_tool_bar_string);
5f5c8ee5 8389#undef PROP
a2889657
JB
8390 }
8391
5f5c8ee5
GM
8392 UNGCPRO;
8393}
8394
8395
e037b9ec 8396/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
8397
8398static void
e037b9ec 8399display_tool_bar_line (it)
5f5c8ee5
GM
8400 struct it *it;
8401{
8402 struct glyph_row *row = it->glyph_row;
8403 int max_x = it->last_visible_x;
8404 struct glyph *last;
2311178e 8405
5f5c8ee5
GM
8406 prepare_desired_row (row);
8407 row->y = it->current_y;
90aa2856
GM
8408
8409 /* Note that this isn't made use of if the face hasn't a box,
8410 so there's no need to check the face here. */
8411 it->start_of_box_run_p = 1;
2311178e 8412
5f5c8ee5 8413 while (it->current_x < max_x)
a2889657 8414 {
5f5c8ee5 8415 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 8416
5f5c8ee5
GM
8417 /* Get the next display element. */
8418 if (!get_next_display_element (it))
8419 break;
73af359d 8420
5f5c8ee5
GM
8421 /* Produce glyphs. */
8422 x_before = it->current_x;
8423 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8424 PRODUCE_GLYPHS (it);
daa37602 8425
5f5c8ee5
GM
8426 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8427 i = 0;
8428 x = x_before;
8429 while (i < nglyphs)
8430 {
8431 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
2311178e 8432
5f5c8ee5
GM
8433 if (x + glyph->pixel_width > max_x)
8434 {
8435 /* Glyph doesn't fit on line. */
8436 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8437 it->current_x = x;
8438 goto out;
8439 }
daa37602 8440
5f5c8ee5
GM
8441 ++it->hpos;
8442 x += glyph->pixel_width;
8443 ++i;
8444 }
8445
8446 /* Stop at line ends. */
8447 if (ITERATOR_AT_END_OF_LINE_P (it))
8448 break;
8449
cafafe0b 8450 set_iterator_to_next (it, 1);
a2889657 8451 }
a2889657 8452
5f5c8ee5 8453 out:;
a2889657 8454
5f5c8ee5
GM
8455 row->displays_text_p = row->used[TEXT_AREA] != 0;
8456 extend_face_to_end_of_line (it);
8457 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8458 last->right_box_line_p = 1;
90aa2856
GM
8459 if (last == row->glyphs[TEXT_AREA])
8460 last->left_box_line_p = 1;
5f5c8ee5 8461 compute_line_metrics (it);
2311178e 8462
e037b9ec 8463 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
8464 if (!row->displays_text_p)
8465 {
312246d1
GM
8466 row->height = row->phys_height = it->last_visible_y - row->y;
8467 row->ascent = row->phys_ascent = 0;
5f5c8ee5 8468 }
2311178e 8469
5f5c8ee5
GM
8470 row->full_width_p = 1;
8471 row->continued_p = 0;
8472 row->truncated_on_left_p = 0;
8473 row->truncated_on_right_p = 0;
8474
8475 it->current_x = it->hpos = 0;
8476 it->current_y += row->height;
8477 ++it->vpos;
8478 ++it->glyph_row;
a2889657 8479}
96a410bc 8480
5f5c8ee5 8481
e037b9ec 8482/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 8483 items of frame F visible. */
96a410bc 8484
d39b6696 8485static int
e037b9ec 8486tool_bar_lines_needed (f)
5f5c8ee5 8487 struct frame *f;
d39b6696 8488{
e037b9ec 8489 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5 8490 struct it it;
2311178e 8491
e037b9ec
GM
8492 /* Initialize an iterator for iteration over
8493 F->desired_tool_bar_string in the tool-bar window of frame F. */
8494 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5 8495 it.first_visible_x = 0;
da8b7f4f 8496 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
e037b9ec 8497 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
8498
8499 while (!ITERATOR_AT_END_P (&it))
8500 {
8501 it.glyph_row = w->desired_matrix->rows;
8502 clear_glyph_row (it.glyph_row);
e037b9ec 8503 display_tool_bar_line (&it);
5f5c8ee5
GM
8504 }
8505
da8b7f4f 8506 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
d39b6696 8507}
96a410bc 8508
5f5c8ee5 8509
57c28064
GM
8510DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8511 0, 1, 0,
7ee72033
MB
8512 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8513 (frame)
57c28064
GM
8514 Lisp_Object frame;
8515{
8516 struct frame *f;
8517 struct window *w;
8518 int nlines = 0;
8519
8520 if (NILP (frame))
8521 frame = selected_frame;
8522 else
b7826503 8523 CHECK_FRAME (frame);
57c28064 8524 f = XFRAME (frame);
2311178e 8525
57c28064
GM
8526 if (WINDOWP (f->tool_bar_window)
8527 || (w = XWINDOW (f->tool_bar_window),
da8b7f4f 8528 WINDOW_TOTAL_LINES (w) > 0))
57c28064
GM
8529 {
8530 update_tool_bar (f, 1);
8531 if (f->n_tool_bar_items)
8532 {
8533 build_desired_tool_bar_string (f);
8534 nlines = tool_bar_lines_needed (f);
8535 }
8536 }
8537
8538 return make_number (nlines);
8539}
8540
8541
e037b9ec 8542/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
8543 height should be changed. */
8544
8545static int
e037b9ec 8546redisplay_tool_bar (f)
5f5c8ee5 8547 struct frame *f;
96a410bc 8548{
5f5c8ee5
GM
8549 struct window *w;
8550 struct it it;
8551 struct glyph_row *row;
8552 int change_height_p = 0;
177c0ea7 8553
488dd4c4 8554#ifdef USE_GTK
810f2256 8555 if (FRAME_EXTERNAL_TOOL_BAR (f))
488dd4c4
JD
8556 update_frame_tool_bar (f);
8557 return 0;
8558#endif
2311178e 8559
e037b9ec
GM
8560 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8561 do anything. This means you must start with tool-bar-lines
5f5c8ee5 8562 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
8563 can turn off tool-bars by specifying tool-bar-lines zero. */
8564 if (!WINDOWP (f->tool_bar_window)
8565 || (w = XWINDOW (f->tool_bar_window),
da8b7f4f 8566 WINDOW_TOTAL_LINES (w) == 0))
5f5c8ee5 8567 return 0;
96a410bc 8568
e037b9ec
GM
8569 /* Set up an iterator for the tool-bar window. */
8570 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5 8571 it.first_visible_x = 0;
da8b7f4f 8572 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
5f5c8ee5 8573 row = it.glyph_row;
3450d04c 8574
e037b9ec
GM
8575 /* Build a string that represents the contents of the tool-bar. */
8576 build_desired_tool_bar_string (f);
8577 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 8578
e037b9ec 8579 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 8580 while (it.current_y < it.last_visible_y)
e037b9ec 8581 display_tool_bar_line (&it);
3450d04c 8582
e037b9ec 8583 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
8584 window, so don't do it. */
8585 w->desired_matrix->no_scrolling_p = 1;
8586 w->must_be_updated_p = 1;
3450d04c 8587
e037b9ec 8588 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
8589 {
8590 int nlines;
6e33e6f0
GM
8591
8592 /* If we couldn't display everything, change the tool-bar's
8593 height. */
8594 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8595 change_height_p = 1;
2311178e 8596
5f5c8ee5
GM
8597 /* If there are blank lines at the end, except for a partially
8598 visible blank line at the end that is smaller than
da8b7f4f 8599 FRAME_LINE_HEIGHT, change the tool-bar's height. */
5f5c8ee5
GM
8600 row = it.glyph_row - 1;
8601 if (!row->displays_text_p
da8b7f4f 8602 && row->height >= FRAME_LINE_HEIGHT (f))
5f5c8ee5
GM
8603 change_height_p = 1;
8604
e037b9ec
GM
8605 /* If row displays tool-bar items, but is partially visible,
8606 change the tool-bar's height. */
5f5c8ee5
GM
8607 if (row->displays_text_p
8608 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8609 change_height_p = 1;
8610
e037b9ec 8611 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
8612 frame parameter. */
8613 if (change_height_p
e037b9ec 8614 && (nlines = tool_bar_lines_needed (f),
da8b7f4f 8615 nlines != WINDOW_TOTAL_LINES (w)))
5f5c8ee5 8616 {
e037b9ec 8617 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5 8618 Lisp_Object frame;
da8b7f4f 8619 int old_height = WINDOW_TOTAL_LINES (w);
2311178e 8620
5f5c8ee5
GM
8621 XSETFRAME (frame, f);
8622 clear_glyph_matrix (w->desired_matrix);
8623 Fmodify_frame_parameters (frame,
e037b9ec 8624 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
8625 make_number (nlines)),
8626 Qnil));
da8b7f4f 8627 if (WINDOW_TOTAL_LINES (w) != old_height)
e85ee976 8628 fonts_changed_p = 1;
5f5c8ee5
GM
8629 }
8630 }
3450d04c 8631
5f5c8ee5 8632 return change_height_p;
96a410bc 8633}
90adcf20 8634
5f5c8ee5 8635
e037b9ec
GM
8636/* Get information about the tool-bar item which is displayed in GLYPH
8637 on frame F. Return in *PROP_IDX the index where tool-bar item
7464726e 8638 properties start in F->tool_bar_items. Value is zero if
e037b9ec 8639 GLYPH doesn't display a tool-bar item. */
5f5c8ee5 8640
fa3c6b4d 8641static int
e037b9ec 8642tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
8643 struct frame *f;
8644 struct glyph *glyph;
8645 int *prop_idx;
90adcf20 8646{
5f5c8ee5
GM
8647 Lisp_Object prop;
8648 int success_p;
be676094
GM
8649 int charpos;
8650
8651 /* This function can be called asynchronously, which means we must
8652 exclude any possibility that Fget_text_property signals an
8653 error. */
2051c264 8654 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
be676094 8655 charpos = max (0, charpos);
2311178e 8656
5f5c8ee5
GM
8657 /* Get the text property `menu-item' at pos. The value of that
8658 property is the start index of this item's properties in
7464726e 8659 F->tool_bar_items. */
be676094 8660 prop = Fget_text_property (make_number (charpos),
e037b9ec 8661 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
8662 if (INTEGERP (prop))
8663 {
8664 *prop_idx = XINT (prop);
8665 success_p = 1;
8666 }
8667 else
8668 success_p = 0;
90adcf20 8669
5f5c8ee5
GM
8670 return success_p;
8671}
2311178e 8672
fa3c6b4d
KS
8673\f
8674/* Get information about the tool-bar item at position X/Y on frame F.
8675 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8676 the current matrix of the tool-bar window of F, or NULL if not
8677 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8678 item in F->tool_bar_items. Value is
8679
8680 -1 if X/Y is not on a tool-bar item
8681 0 if X/Y is on the same item that was highlighted before.
8682 1 otherwise. */
8683
8684static int
8685get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8686 struct frame *f;
8687 int x, y;
8688 struct glyph **glyph;
8689 int *hpos, *vpos, *prop_idx;
8690{
8691 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8692 struct window *w = XWINDOW (f->tool_bar_window);
8693 int area;
8694
8695 /* Find the glyph under X/Y. */
8696 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
8697 if (*glyph == NULL)
8698 return -1;
8699
8700 /* Get the start of this tool-bar item's properties in
8701 f->tool_bar_items. */
8702 if (!tool_bar_item_info (f, *glyph, prop_idx))
8703 return -1;
8704
8705 /* Is mouse on the highlighted item? */
8706 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8707 && *vpos >= dpyinfo->mouse_face_beg_row
8708 && *vpos <= dpyinfo->mouse_face_end_row
8709 && (*vpos > dpyinfo->mouse_face_beg_row
8710 || *hpos >= dpyinfo->mouse_face_beg_col)
8711 && (*vpos < dpyinfo->mouse_face_end_row
8712 || *hpos < dpyinfo->mouse_face_end_col
8713 || dpyinfo->mouse_face_past_end))
8714 return 0;
8715
8716 return 1;
8717}
8718
8719
8720/* EXPORT:
8721 Handle mouse button event on the tool-bar of frame F, at
8722 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8723 0 for button release. MODIFIERS is event modifiers for button
8724 release. */
8725
8726void
8727handle_tool_bar_click (f, x, y, down_p, modifiers)
8728 struct frame *f;
8729 int x, y, down_p;
8730 unsigned int modifiers;
8731{
8732 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8733 struct window *w = XWINDOW (f->tool_bar_window);
8734 int hpos, vpos, prop_idx;
8735 struct glyph *glyph;
8736 Lisp_Object enabled_p;
8737
8738 /* If not on the highlighted tool-bar item, return. */
8739 frame_to_window_pixel_xy (w, &x, &y);
8740 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8741 return;
8742
8743 /* If item is disabled, do nothing. */
8744 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8745 if (NILP (enabled_p))
8746 return;
8747
8748 if (down_p)
8749 {
8750 /* Show item in pressed state. */
8751 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8752 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8753 last_tool_bar_item = prop_idx;
8754 }
8755 else
8756 {
8757 Lisp_Object key, frame;
8758 struct input_event event;
d86705ec 8759 EVENT_INIT (event);
fa3c6b4d
KS
8760
8761 /* Show item in released state. */
8762 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8763 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8764
8765 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8766
8767 XSETFRAME (frame, f);
8768 event.kind = TOOL_BAR_EVENT;
8769 event.frame_or_window = frame;
8770 event.arg = frame;
8771 kbd_buffer_store_event (&event);
8772
8773 event.kind = TOOL_BAR_EVENT;
8774 event.frame_or_window = frame;
8775 event.arg = key;
8776 event.modifiers = modifiers;
8777 kbd_buffer_store_event (&event);
8778 last_tool_bar_item = -1;
8779 }
8780}
8781
8782
8783/* Possibly highlight a tool-bar item on frame F when mouse moves to
8784 tool-bar window-relative coordinates X/Y. Called from
8785 note_mouse_highlight. */
8786
8787static void
8788note_tool_bar_highlight (f, x, y)
8789 struct frame *f;
8790 int x, y;
8791{
8792 Lisp_Object window = f->tool_bar_window;
8793 struct window *w = XWINDOW (window);
8794 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8795 int hpos, vpos;
8796 struct glyph *glyph;
8797 struct glyph_row *row;
8798 int i;
8799 Lisp_Object enabled_p;
8800 int prop_idx;
8801 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8802 int mouse_down_p, rc;
8803
8804 /* Function note_mouse_highlight is called with negative x(y
8805 values when mouse moves outside of the frame. */
8806 if (x <= 0 || y <= 0)
8807 {
8808 clear_mouse_face (dpyinfo);
8809 return;
8810 }
8811
8812 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
8813 if (rc < 0)
8814 {
8815 /* Not on tool-bar item. */
8816 clear_mouse_face (dpyinfo);
8817 return;
8818 }
8819 else if (rc == 0)
8820 /* On same tool-bar item as before. */
8821 goto set_help_echo;
8822
8823 clear_mouse_face (dpyinfo);
8824
8825 /* Mouse is down, but on different tool-bar item? */
8826 mouse_down_p = (dpyinfo->grabbed
8827 && f == last_mouse_frame
8828 && FRAME_LIVE_P (f));
8829 if (mouse_down_p
8830 && last_tool_bar_item != prop_idx)
8831 return;
8832
8833 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
8834 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
8835
8836 /* If tool-bar item is not enabled, don't highlight it. */
8837 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8838 if (!NILP (enabled_p))
8839 {
8840 /* Compute the x-position of the glyph. In front and past the
da8b7f4f 8841 image is a space. We include this in the highlighted area. */
fa3c6b4d
KS
8842 row = MATRIX_ROW (w->current_matrix, vpos);
8843 for (i = x = 0; i < hpos; ++i)
8844 x += row->glyphs[TEXT_AREA][i].pixel_width;
8845
8846 /* Record this as the current active region. */
8847 dpyinfo->mouse_face_beg_col = hpos;
8848 dpyinfo->mouse_face_beg_row = vpos;
8849 dpyinfo->mouse_face_beg_x = x;
8850 dpyinfo->mouse_face_beg_y = row->y;
8851 dpyinfo->mouse_face_past_end = 0;
8852
8853 dpyinfo->mouse_face_end_col = hpos + 1;
8854 dpyinfo->mouse_face_end_row = vpos;
8855 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
8856 dpyinfo->mouse_face_end_y = row->y;
8857 dpyinfo->mouse_face_window = window;
8858 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
8859
8860 /* Display it as active. */
8861 show_mouse_face (dpyinfo, draw);
8862 dpyinfo->mouse_face_image_state = draw;
8863 }
8864
8865 set_help_echo:
8866
8867 /* Set help_echo_string to a help string to display for this tool-bar item.
8868 XTread_socket does the rest. */
8869 help_echo_object = help_echo_window = Qnil;
8870 help_echo_pos = -1;
8871 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
8872 if (NILP (help_echo_string))
8873 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
8874}
8875
5f5c8ee5 8876#endif /* HAVE_WINDOW_SYSTEM */
ffbbc941
KS
8877
8878
8879\f
8880/***********************************************************************
8881 Fringes
8882 ***********************************************************************/
8883
8884#ifdef HAVE_WINDOW_SYSTEM
8885
8886/* An arrow like this: `<-'. */
8887static unsigned char left_bits[] = {
8888 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
8889
8890/* Right truncation arrow bitmap `->'. */
8891static unsigned char right_bits[] = {
8892 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
8893
8894/* Marker for continued lines. */
8895static unsigned char continued_bits[] = {
8896 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
8897
8898/* Marker for continuation lines. */
8899static unsigned char continuation_bits[] = {
8900 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
8901
8902/* Overlay arrow bitmap. A triangular arrow. */
8903static unsigned char ov_bits[] = {
8904 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
8905
8906/* Bitmap drawn to indicate lines not displaying text if
8907 `indicate-empty-lines' is non-nil. */
8908static unsigned char zv_bits[] = {
8909 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8910 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8911 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8912 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
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
8918struct fringe_bitmap fringe_bitmaps[MAX_FRINGE_BITMAPS] =
8919{
8920 { 0, 0, 0, NULL /* NO_FRINGE_BITMAP */ },
8921 { 8, sizeof (left_bits), 0, left_bits },
8922 { 8, sizeof (right_bits), 0, right_bits },
8923 { 8, sizeof (continued_bits), 0, continued_bits },
8924 { 8, sizeof (continuation_bits), 0, continuation_bits },
8925 { 8, sizeof (ov_bits), 0, ov_bits },
8926 { 8, sizeof (zv_bits), 3, zv_bits }
8927};
8928
8929
8930/* Draw the bitmap WHICH in one of the left or right fringes of
8931 window W. ROW is the glyph row for which to display the bitmap; it
8932 determines the vertical position at which the bitmap has to be
8933 drawn. */
8934
8935static void
8936draw_fringe_bitmap (w, row, which, left_p)
8937 struct window *w;
8938 struct glyph_row *row;
8939 enum fringe_bitmap_type which;
8940 int left_p;
8941{
8942 struct frame *f = XFRAME (WINDOW_FRAME (w));
8943 struct draw_fringe_bitmap_params p;
8944
8945 /* Convert row to frame coordinates. */
8946 p.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8947
8948 p.which = which;
8949 p.wd = fringe_bitmaps[which].width;
8950
8951 p.h = fringe_bitmaps[which].height;
8952 p.dh = (fringe_bitmaps[which].period
8953 ? (p.y % fringe_bitmaps[which].period)
8954 : 0);
8955 p.h -= p.dh;
8956 /* Clip bitmap if too high. */
8957 if (p.h > row->height)
8958 p.h = row->height;
8959
8960 p.face = FACE_FROM_ID (f, FRINGE_FACE_ID);
8961 PREPARE_FACE_FOR_DISPLAY (f, p.face);
8962
8963 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
8964 the fringe. */
8965 p.bx = -1;
8966 if (left_p)
8967 {
da8b7f4f
KS
8968 int wd = WINDOW_LEFT_FRINGE_WIDTH (w);
8969 int x = window_box_left (w, (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
8970 ? LEFT_MARGIN_AREA
8971 : TEXT_AREA));
8972 if (p.wd > wd)
8973 p.wd = wd;
8974 p.x = x - p.wd - (wd - p.wd) / 2;
8975
8976 if (p.wd < wd || row->height > p.h)
ffbbc941
KS
8977 {
8978 /* If W has a vertical border to its left, don't draw over it. */
da8b7f4f
KS
8979 wd -= ((!WINDOW_LEFTMOST_P (w)
8980 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
8981 ? 1 : 0);
8982 p.bx = x - wd;
8983 p.nx = wd;
ffbbc941
KS
8984 }
8985 }
8986 else
8987 {
da8b7f4f
KS
8988 int x = window_box_right (w,
8989 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
8990 ? RIGHT_MARGIN_AREA
8991 : TEXT_AREA));
8992 int wd = WINDOW_RIGHT_FRINGE_WIDTH (w);
8993 if (p.wd > wd)
8994 p.wd = wd;
8995 p.x = x + (wd - p.wd) / 2;
ffbbc941
KS
8996 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
8997 the fringe. */
da8b7f4f 8998 if (p.wd < wd || row->height > p.h)
ffbbc941 8999 {
da8b7f4f
KS
9000 p.bx = x;
9001 p.nx = wd;
ffbbc941
KS
9002 }
9003 }
9004
9005 if (p.bx >= 0)
9006 {
da8b7f4f 9007 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
ffbbc941
KS
9008
9009 p.by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, row->y));
9010 p.ny = row->visible_height;
9011 }
9012
9013 /* Adjust y to the offset in the row to start drawing the bitmap. */
9014 p.y += (row->height - p.h) / 2;
9015
9016 rif->draw_fringe_bitmap (w, row, &p);
9017}
9018
9019/* Draw fringe bitmaps for glyph row ROW on window W. Call this
9020 function with input blocked. */
9021
9022void
9023draw_row_fringe_bitmaps (w, row)
9024 struct window *w;
9025 struct glyph_row *row;
9026{
ffbbc941
KS
9027 enum fringe_bitmap_type bitmap;
9028
9029 xassert (interrupt_input_blocked);
9030
9031 /* If row is completely invisible, because of vscrolling, we
9032 don't have to draw anything. */
9033 if (row->visible_height <= 0)
9034 return;
9035
da8b7f4f 9036 if (WINDOW_LEFT_FRINGE_WIDTH (w) != 0)
ffbbc941
KS
9037 {
9038 /* Decide which bitmap to draw in the left fringe. */
9039 if (row->overlay_arrow_p)
9040 bitmap = OVERLAY_ARROW_BITMAP;
9041 else if (row->truncated_on_left_p)
9042 bitmap = LEFT_TRUNCATION_BITMAP;
9043 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
9044 bitmap = CONTINUATION_LINE_BITMAP;
9045 else if (row->indicate_empty_line_p)
9046 bitmap = ZV_LINE_BITMAP;
9047 else
9048 bitmap = NO_FRINGE_BITMAP;
9049
9050 draw_fringe_bitmap (w, row, bitmap, 1);
9051 }
9052
da8b7f4f 9053 if (WINDOW_RIGHT_FRINGE_WIDTH (w) != 0)
ffbbc941
KS
9054 {
9055 /* Decide which bitmap to draw in the right fringe. */
9056 if (row->truncated_on_right_p)
9057 bitmap = RIGHT_TRUNCATION_BITMAP;
9058 else if (row->continued_p)
9059 bitmap = CONTINUED_LINE_BITMAP;
da8b7f4f 9060 else if (row->indicate_empty_line_p && WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
ffbbc941
KS
9061 bitmap = ZV_LINE_BITMAP;
9062 else
9063 bitmap = NO_FRINGE_BITMAP;
9064
9065 draw_fringe_bitmap (w, row, bitmap, 0);
9066 }
9067}
9068
9069
564946b6
KS
9070/* Compute actual fringe widths for frame F.
9071
9072 If REDRAW is 1, redraw F if the fringe settings was actually
9073 modified and F is visible.
9074
9075 Since the combined left and right fringe must occupy an integral
9076 number of columns, we may need to add some pixels to each fringe.
9077 Typically, we add an equal amount (+/- 1 pixel) to each fringe,
9078 but a negative width value is taken literally (after negating it).
9079
9080 We never make the fringes narrower than specified. It is planned
9081 to make fringe bitmaps customizable and expandable, and at that
9082 time, the user will typically specify the minimum number of pixels
9083 needed for his bitmaps, so we shouldn't select anything less than
9084 what is specified.
9085*/
ffbbc941
KS
9086
9087void
9088compute_fringe_widths (f, redraw)
9089 struct frame *f;
9090 int redraw;
9091{
da8b7f4f
KS
9092 int o_left = FRAME_LEFT_FRINGE_WIDTH (f);
9093 int o_right = FRAME_RIGHT_FRINGE_WIDTH (f);
9094 int o_cols = FRAME_FRINGE_COLS (f);
ffbbc941
KS
9095
9096 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
9097 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
9098 int left_fringe_width, right_fringe_width;
9099
9100 if (!NILP (left_fringe))
9101 left_fringe = Fcdr (left_fringe);
9102 if (!NILP (right_fringe))
9103 right_fringe = Fcdr (right_fringe);
9104
9105 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
9106 XINT (left_fringe));
9107 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
9108 XINT (right_fringe));
9109
9110 if (left_fringe_width || right_fringe_width)
9111 {
9112 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
9113 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
9114 int conf_wid = left_wid + right_wid;
da8b7f4f 9115 int font_wid = FRAME_COLUMN_WIDTH (f);
ffbbc941
KS
9116 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
9117 int real_wid = cols * font_wid;
9118 if (left_wid && right_wid)
9119 {
9120 if (left_fringe_width < 0)
9121 {
9122 /* Left fringe width is fixed, adjust right fringe if necessary */
da8b7f4f
KS
9123 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid;
9124 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid - left_wid;
ffbbc941
KS
9125 }
9126 else if (right_fringe_width < 0)
9127 {
9128 /* Right fringe width is fixed, adjust left fringe if necessary */
da8b7f4f
KS
9129 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid - right_wid;
9130 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid;
ffbbc941
KS
9131 }
9132 else
9133 {
9134 /* Adjust both fringes with an equal amount.
9135 Note that we are doing integer arithmetic here, so don't
9136 lose a pixel if the total width is an odd number. */
9137 int fill = real_wid - conf_wid;
da8b7f4f
KS
9138 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid + fill/2;
9139 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid + fill - fill/2;
ffbbc941
KS
9140 }
9141 }
9142 else if (left_fringe_width)
9143 {
da8b7f4f
KS
9144 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid;
9145 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
ffbbc941
KS
9146 }
9147 else
9148 {
da8b7f4f
KS
9149 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9150 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid;
ffbbc941 9151 }
da8b7f4f 9152 FRAME_FRINGE_COLS (f) = cols;
ffbbc941
KS
9153 }
9154 else
9155 {
da8b7f4f
KS
9156 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9157 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9158 FRAME_FRINGE_COLS (f) = 0;
ffbbc941
KS
9159 }
9160
9161 if (redraw && FRAME_VISIBLE_P (f))
da8b7f4f
KS
9162 if (o_left != FRAME_LEFT_FRINGE_WIDTH (f) ||
9163 o_right != FRAME_RIGHT_FRINGE_WIDTH (f) ||
9164 o_cols != FRAME_FRINGE_COLS (f))
ffbbc941
KS
9165 redraw_frame (f);
9166}
9167
9168#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 9169
feb0c42f 9170
5f5c8ee5
GM
9171\f
9172/************************************************************************
9173 Horizontal scrolling
9174 ************************************************************************/
feb0c42f 9175
5f5c8ee5
GM
9176static int hscroll_window_tree P_ ((Lisp_Object));
9177static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 9178
5f5c8ee5
GM
9179/* For all leaf windows in the window tree rooted at WINDOW, set their
9180 hscroll value so that PT is (i) visible in the window, and (ii) so
9181 that it is not within a certain margin at the window's left and
9182 right border. Value is non-zero if any window's hscroll has been
9183 changed. */
9184
9185static int
9186hscroll_window_tree (window)
9187 Lisp_Object window;
9188{
9189 int hscrolled_p = 0;
e76d28d5 9190 int hscroll_relative_p = FLOATP (Vhscroll_step);
1df7e8f0
EZ
9191 int hscroll_step_abs = 0;
9192 double hscroll_step_rel = 0;
9193
9194 if (hscroll_relative_p)
9195 {
e76d28d5 9196 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
1df7e8f0
EZ
9197 if (hscroll_step_rel < 0)
9198 {
9199 hscroll_relative_p = 0;
9200 hscroll_step_abs = 0;
9201 }
9202 }
e76d28d5 9203 else if (INTEGERP (Vhscroll_step))
1df7e8f0 9204 {
e76d28d5 9205 hscroll_step_abs = XINT (Vhscroll_step);
1df7e8f0
EZ
9206 if (hscroll_step_abs < 0)
9207 hscroll_step_abs = 0;
9208 }
9209 else
9210 hscroll_step_abs = 0;
9211
5f5c8ee5 9212 while (WINDOWP (window))
90adcf20 9213 {
5f5c8ee5 9214 struct window *w = XWINDOW (window);
1df7e8f0 9215
5f5c8ee5
GM
9216 if (WINDOWP (w->hchild))
9217 hscrolled_p |= hscroll_window_tree (w->hchild);
9218 else if (WINDOWP (w->vchild))
9219 hscrolled_p |= hscroll_window_tree (w->vchild);
9220 else if (w->cursor.vpos >= 0)
9221 {
da8b7f4f
KS
9222 int h_margin;
9223 int text_area_width;
92a90e89
GM
9224 struct glyph_row *current_cursor_row
9225 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9226 struct glyph_row *desired_cursor_row
9227 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9228 struct glyph_row *cursor_row
9229 = (desired_cursor_row->enabled_p
9230 ? desired_cursor_row
9231 : current_cursor_row);
a2725ab2 9232
da8b7f4f 9233 text_area_width = window_box_width (w, TEXT_AREA);
90adcf20 9234
5f5c8ee5 9235 /* Scroll when cursor is inside this scroll margin. */
da8b7f4f 9236 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
1df7e8f0 9237
5f5c8ee5 9238 if ((XFASTINT (w->hscroll)
e76d28d5 9239 && w->cursor.x <= h_margin)
92a90e89
GM
9240 || (cursor_row->enabled_p
9241 && cursor_row->truncated_on_right_p
e76d28d5 9242 && (w->cursor.x >= text_area_width - h_margin)))
08b610e4 9243 {
5f5c8ee5
GM
9244 struct it it;
9245 int hscroll;
9246 struct buffer *saved_current_buffer;
9247 int pt;
1df7e8f0 9248 int wanted_x;
5f5c8ee5
GM
9249
9250 /* Find point in a display of infinite width. */
9251 saved_current_buffer = current_buffer;
9252 current_buffer = XBUFFER (w->buffer);
1df7e8f0 9253
5f5c8ee5
GM
9254 if (w == XWINDOW (selected_window))
9255 pt = BUF_PT (current_buffer);
9256 else
08b610e4 9257 {
5f5c8ee5
GM
9258 pt = marker_position (w->pointm);
9259 pt = max (BEGV, pt);
9260 pt = min (ZV, pt);
9261 }
9262
9263 /* Move iterator to pt starting at cursor_row->start in
9264 a line with infinite width. */
9265 init_to_row_start (&it, w, cursor_row);
9266 it.last_visible_x = INFINITY;
9267 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9268 current_buffer = saved_current_buffer;
9269
1df7e8f0
EZ
9270 /* Position cursor in window. */
9271 if (!hscroll_relative_p && hscroll_step_abs == 0)
9272 hscroll = max (0, it.current_x - text_area_width / 2)
da8b7f4f 9273 / FRAME_COLUMN_WIDTH (it.f);
e76d28d5 9274 else if (w->cursor.x >= text_area_width - h_margin)
1df7e8f0
EZ
9275 {
9276 if (hscroll_relative_p)
9277 wanted_x = text_area_width * (1 - hscroll_step_rel)
e76d28d5 9278 - h_margin;
1df7e8f0
EZ
9279 else
9280 wanted_x = text_area_width
da8b7f4f 9281 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
e76d28d5 9282 - h_margin;
1df7e8f0 9283 hscroll
da8b7f4f 9284 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
1df7e8f0
EZ
9285 }
9286 else
9287 {
9288 if (hscroll_relative_p)
9289 wanted_x = text_area_width * hscroll_step_rel
e76d28d5 9290 + h_margin;
1df7e8f0 9291 else
da8b7f4f 9292 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
e76d28d5 9293 + h_margin;
1df7e8f0 9294 hscroll
da8b7f4f 9295 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
1df7e8f0 9296 }
3172f70b 9297 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
5f5c8ee5
GM
9298
9299 /* Don't call Fset_window_hscroll if value hasn't
9300 changed because it will prevent redisplay
9301 optimizations. */
9302 if (XFASTINT (w->hscroll) != hscroll)
9303 {
3172f70b
GM
9304 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9305 w->hscroll = make_number (hscroll);
5f5c8ee5 9306 hscrolled_p = 1;
08b610e4 9307 }
08b610e4 9308 }
08b610e4 9309 }
a2725ab2 9310
5f5c8ee5 9311 window = w->next;
90adcf20 9312 }
cd6dfed6 9313
5f5c8ee5
GM
9314 /* Value is non-zero if hscroll of any leaf window has been changed. */
9315 return hscrolled_p;
9316}
9317
9318
9319/* Set hscroll so that cursor is visible and not inside horizontal
9320 scroll margins for all windows in the tree rooted at WINDOW. See
9321 also hscroll_window_tree above. Value is non-zero if any window's
9322 hscroll has been changed. If it has, desired matrices on the frame
9323 of WINDOW are cleared. */
9324
9325static int
9326hscroll_windows (window)
9327 Lisp_Object window;
9328{
d475bcb8 9329 int hscrolled_p;
2311178e 9330
d475bcb8
GM
9331 if (automatic_hscrolling_p)
9332 {
9333 hscrolled_p = hscroll_window_tree (window);
9334 if (hscrolled_p)
9335 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9336 }
9337 else
9338 hscrolled_p = 0;
5f5c8ee5 9339 return hscrolled_p;
90adcf20 9340}
5f5c8ee5
GM
9341
9342
90adcf20 9343\f
5f5c8ee5
GM
9344/************************************************************************
9345 Redisplay
9346 ************************************************************************/
9347
9348/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9349 to a non-zero value. This is sometimes handy to have in a debugger
9350 session. */
9351
9352#if GLYPH_DEBUG
a2889657 9353
5f5c8ee5
GM
9354/* First and last unchanged row for try_window_id. */
9355
9356int debug_first_unchanged_at_end_vpos;
9357int debug_last_unchanged_at_beg_vpos;
9358
9359/* Delta vpos and y. */
9360
9361int debug_dvpos, debug_dy;
9362
9363/* Delta in characters and bytes for try_window_id. */
9364
9365int debug_delta, debug_delta_bytes;
9366
9367/* Values of window_end_pos and window_end_vpos at the end of
9368 try_window_id. */
9369
31ade731 9370EMACS_INT debug_end_pos, debug_end_vpos;
5f5c8ee5
GM
9371
9372/* Append a string to W->desired_matrix->method. FMT is a printf
9373 format string. A1...A9 are a supplement for a variable-length
9374 argument list. If trace_redisplay_p is non-zero also printf the
9375 resulting string to stderr. */
9376
9377static void
9378debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9379 struct window *w;
9380 char *fmt;
9381 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9382{
9383 char buffer[512];
9384 char *method = w->desired_matrix->method;
9385 int len = strlen (method);
9386 int size = sizeof w->desired_matrix->method;
9387 int remaining = size - len - 1;
9388
9389 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9390 if (len && remaining)
9391 {
9392 method[len] = '|';
9393 --remaining, ++len;
9394 }
2311178e 9395
5f5c8ee5
GM
9396 strncpy (method + len, buffer, remaining);
9397
9398 if (trace_redisplay_p)
9399 fprintf (stderr, "%p (%s): %s\n",
9400 w,
9401 ((BUFFERP (w->buffer)
9402 && STRINGP (XBUFFER (w->buffer)->name))
2051c264 9403 ? (char *) SDATA (XBUFFER (w->buffer)->name)
5f5c8ee5
GM
9404 : "no buffer"),
9405 buffer);
9406}
a2889657 9407
5f5c8ee5 9408#endif /* GLYPH_DEBUG */
90adcf20 9409
a2889657 9410
5f5c8ee5
GM
9411/* Value is non-zero if all changes in window W, which displays
9412 current_buffer, are in the text between START and END. START is a
9413 buffer position, END is given as a distance from Z. Used in
9414 redisplay_internal for display optimization. */
9415
9416static INLINE int
9417text_outside_line_unchanged_p (w, start, end)
9418 struct window *w;
9419 int start, end;
9420{
9421 int unchanged_p = 1;
2311178e 9422
5f5c8ee5
GM
9423 /* If text or overlays have changed, see where. */
9424 if (XFASTINT (w->last_modified) < MODIFF
9425 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9426 {
9427 /* Gap in the line? */
9428 if (GPT < start || Z - GPT < end)
9429 unchanged_p = 0;
9430
9431 /* Changes start in front of the line, or end after it? */
9432 if (unchanged_p
9142dd5b
GM
9433 && (BEG_UNCHANGED < start - 1
9434 || END_UNCHANGED < end))
5f5c8ee5 9435 unchanged_p = 0;
2311178e 9436
5f5c8ee5
GM
9437 /* If selective display, can't optimize if changes start at the
9438 beginning of the line. */
9439 if (unchanged_p
9440 && INTEGERP (current_buffer->selective_display)
9441 && XINT (current_buffer->selective_display) > 0
9142dd5b 9442 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5 9443 unchanged_p = 0;
47d57b22
GM
9444
9445 /* If there are overlays at the start or end of the line, these
9446 may have overlay strings with newlines in them. A change at
9447 START, for instance, may actually concern the display of such
9448 overlay strings as well, and they are displayed on different
9449 lines. So, quickly rule out this case. (For the future, it
9450 might be desirable to implement something more telling than
9451 just BEG/END_UNCHANGED.) */
9452 if (unchanged_p)
9453 {
9454 if (BEG + BEG_UNCHANGED == start
9455 && overlay_touches_p (start))
9456 unchanged_p = 0;
9457 if (END_UNCHANGED == end
9458 && overlay_touches_p (Z - end))
9459 unchanged_p = 0;
9460 }
5f5c8ee5
GM
9461 }
9462
9463 return unchanged_p;
9464}
9465
9466
9467/* Do a frame update, taking possible shortcuts into account. This is
9468 the main external entry point for redisplay.
9469
9470 If the last redisplay displayed an echo area message and that message
9471 is no longer requested, we clear the echo area or bring back the
9472 mini-buffer if that is in use. */
20de20dc 9473
a2889657
JB
9474void
9475redisplay ()
e9874cee
RS
9476{
9477 redisplay_internal (0);
9478}
9479
47d57b22 9480
260a86a0
KH
9481/* Return 1 if point moved out of or into a composition. Otherwise
9482 return 0. PREV_BUF and PREV_PT are the last point buffer and
9483 position. BUF and PT are the current point buffer and position. */
9484
9485int
9486check_point_in_composition (prev_buf, prev_pt, buf, pt)
9487 struct buffer *prev_buf, *buf;
9488 int prev_pt, pt;
9489{
9490 int start, end;
9491 Lisp_Object prop;
9492 Lisp_Object buffer;
9493
9494 XSETBUFFER (buffer, buf);
9495 /* Check a composition at the last point if point moved within the
9496 same buffer. */
9497 if (prev_buf == buf)
9498 {
9499 if (prev_pt == pt)
9500 /* Point didn't move. */
9501 return 0;
2311178e 9502
260a86a0
KH
9503 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9504 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9505 && COMPOSITION_VALID_P (start, end, prop)
9506 && start < prev_pt && end > prev_pt)
9507 /* The last point was within the composition. Return 1 iff
9508 point moved out of the composition. */
9509 return (pt <= start || pt >= end);
9510 }
9511
9512 /* Check a composition at the current point. */
9513 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9514 && find_composition (pt, -1, &start, &end, &prop, buffer)
9515 && COMPOSITION_VALID_P (start, end, prop)
9516 && start < pt && end > pt);
9517}
5f5c8ee5 9518
47d57b22 9519
9142dd5b
GM
9520/* Reconsider the setting of B->clip_changed which is displayed
9521 in window W. */
9522
9523static INLINE void
9524reconsider_clip_changes (w, b)
9525 struct window *w;
9526 struct buffer *b;
9527{
c62c1bb5 9528 if (b->clip_changed
9142dd5b
GM
9529 && !NILP (w->window_end_valid)
9530 && w->current_matrix->buffer == b
9531 && w->current_matrix->zv == BUF_ZV (b)
9532 && w->current_matrix->begv == BUF_BEGV (b))
9533 b->clip_changed = 0;
260a86a0
KH
9534
9535 /* If display wasn't paused, and W is not a tool bar window, see if
9536 point has been moved into or out of a composition. In that case,
9537 we set b->clip_changed to 1 to force updating the screen. If
9538 b->clip_changed has already been set to 1, we can skip this
9539 check. */
9540 if (!b->clip_changed
9541 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9542 {
9543 int pt;
9544
9545 if (w == XWINDOW (selected_window))
9546 pt = BUF_PT (current_buffer);
9547 else
9548 pt = marker_position (w->pointm);
9549
9550 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
6fc556fd 9551 || pt != XINT (w->last_point))
260a86a0 9552 && check_point_in_composition (w->current_matrix->buffer,
6fc556fd 9553 XINT (w->last_point),
260a86a0
KH
9554 XBUFFER (w->buffer), pt))
9555 b->clip_changed = 1;
9556 }
9142dd5b 9557}
aac2d8b2 9558\f
dd429b03
KH
9559
9560/* Select FRAME to forward the values of frame-local variables into C
9561 variables so that the redisplay routines can access those values
9562 directly. */
9563
9564static void
9565select_frame_for_redisplay (frame)
9566 Lisp_Object frame;
9567{
9568 Lisp_Object tail, sym, val;
9569 Lisp_Object old = selected_frame;
9570
9571 selected_frame = frame;
9572
9573 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9574 if (CONSP (XCAR (tail))
9575 && (sym = XCAR (XCAR (tail)),
9576 SYMBOLP (sym))
9577 && (sym = indirect_variable (sym),
9578 val = SYMBOL_VALUE (sym),
9579 (BUFFER_LOCAL_VALUEP (val)
9580 || SOME_BUFFER_LOCAL_VALUEP (val)))
9581 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9582 Fsymbol_value (sym);
9583
9584 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9585 if (CONSP (XCAR (tail))
9586 && (sym = XCAR (XCAR (tail)),
9587 SYMBOLP (sym))
9588 && (sym = indirect_variable (sym),
9589 val = SYMBOL_VALUE (sym),
9590 (BUFFER_LOCAL_VALUEP (val)
9591 || SOME_BUFFER_LOCAL_VALUEP (val)))
9592 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9593 Fsymbol_value (sym);
9594}
9595
9596
aac2d8b2
RS
9597#define STOP_POLLING \
9598do { if (! polling_stopped_here) stop_polling (); \
9599 polling_stopped_here = 1; } while (0)
9600
9601#define RESUME_POLLING \
9602do { if (polling_stopped_here) start_polling (); \
9603 polling_stopped_here = 0; } while (0)
9142dd5b
GM
9604
9605
5f5c8ee5
GM
9606/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9607 response to any user action; therefore, we should preserve the echo
9608 area. (Actually, our caller does that job.) Perhaps in the future
9609 avoid recentering windows if it is not necessary; currently that
9610 causes some problems. */
e9874cee
RS
9611
9612static void
9613redisplay_internal (preserve_echo_area)
9614 int preserve_echo_area;
a2889657 9615{
5f5c8ee5
GM
9616 struct window *w = XWINDOW (selected_window);
9617 struct frame *f = XFRAME (w->frame);
9618 int pause;
a2889657 9619 int must_finish = 0;
5f5c8ee5 9620 struct text_pos tlbufpos, tlendpos;
89819bdd 9621 int number_of_visible_frames;
28514cd9 9622 int count;
886bd6f2 9623 struct frame *sf = SELECTED_FRAME ();
aac2d8b2 9624 int polling_stopped_here = 0;
a2889657 9625
5f5c8ee5
GM
9626 /* Non-zero means redisplay has to consider all windows on all
9627 frames. Zero means, only selected_window is considered. */
9628 int consider_all_windows_p;
2311178e 9629
5f5c8ee5
GM
9630 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9631
9632 /* No redisplay if running in batch mode or frame is not yet fully
9633 initialized, or redisplay is explicitly turned off by setting
9634 Vinhibit_redisplay. */
9635 if (noninteractive
9636 || !NILP (Vinhibit_redisplay)
9637 || !f->glyphs_initialized_p)
a2889657
JB
9638 return;
9639
5f5c8ee5
GM
9640 /* The flag redisplay_performed_directly_p is set by
9641 direct_output_for_insert when it already did the whole screen
9642 update necessary. */
9643 if (redisplay_performed_directly_p)
9644 {
9645 redisplay_performed_directly_p = 0;
9646 if (!hscroll_windows (selected_window))
9647 return;
9648 }
9649
488dd4c4 9650#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15f0cf78
RS
9651 if (popup_activated ())
9652 return;
9653#endif
9654
28514cd9 9655 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 9656 if (redisplaying_p)
735c094c
KH
9657 return;
9658
28514cd9
GM
9659 /* Record a function that resets redisplaying_p to its old value
9660 when we leave this function. */
331379bf 9661 count = SPECPDL_INDEX ();
dd429b03
KH
9662 record_unwind_protect (unwind_redisplay,
9663 Fcons (make_number (redisplaying_p), selected_frame));
28514cd9 9664 ++redisplaying_p;
26683087 9665 specbind (Qinhibit_free_realized_faces, Qnil);
2311178e 9666
8b32d885 9667 retry:
bd9d0f3f 9668 pause = 0;
9142dd5b
GM
9669 reconsider_clip_changes (w, current_buffer);
9670
5f5c8ee5
GM
9671 /* If new fonts have been loaded that make a glyph matrix adjustment
9672 necessary, do it. */
9673 if (fonts_changed_p)
9674 {
9675 adjust_glyphs (NULL);
9676 ++windows_or_buffers_changed;
9677 fonts_changed_p = 0;
9678 }
9679
6961e0c1
GM
9680 /* If face_change_count is non-zero, init_iterator will free all
9681 realized faces, which includes the faces referenced from current
9682 matrices. So, we can't reuse current matrices in this case. */
9683 if (face_change_count)
9684 ++windows_or_buffers_changed;
9685
9628b887
KL
9686 if (FRAME_TERMCAP_P (sf)
9687 && FRAME_TTY (sf)->previous_terminal_frame != sf)
20de20dc 9688 {
9628b887
KL
9689 /* Since frames on a single ASCII terminal share the same
9690 display area, displaying a different frame means redisplay
9691 the whole thing. */
20de20dc 9692 windows_or_buffers_changed++;
886bd6f2 9693 SET_FRAME_GARBAGED (sf);
9628b887 9694 FRAME_TTY (sf)->previous_terminal_frame = sf;
20de20dc 9695 }
20de20dc 9696
5f5c8ee5
GM
9697 /* Set the visible flags for all frames. Do this before checking
9698 for resized or garbaged frames; they want to know if their frames
9699 are visible. See the comment in frame.h for
9700 FRAME_SAMPLE_VISIBILITY. */
d724d989 9701 {
35f56f96 9702 Lisp_Object tail, frame;
d724d989 9703
89819bdd
RS
9704 number_of_visible_frames = 0;
9705
35f56f96 9706 FOR_EACH_FRAME (tail, frame)
f82aff7c 9707 {
5f5c8ee5 9708 struct frame *f = XFRAME (frame);
2311178e 9709
5f5c8ee5
GM
9710 FRAME_SAMPLE_VISIBILITY (f);
9711 if (FRAME_VISIBLE_P (f))
9712 ++number_of_visible_frames;
9713 clear_desired_matrices (f);
f82aff7c 9714 }
d724d989
JB
9715 }
9716
9628b887 9717
44fa5b1e 9718 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 9719 do_pending_window_change (1);
a2889657 9720
5f5c8ee5 9721 /* Clear frames marked as garbaged. */
44fa5b1e 9722 if (frame_garbaged)
c6e89d6c 9723 clear_garbaged_frames ();
a2889657 9724
e037b9ec 9725 /* Build menubar and tool-bar items. */
f82aff7c
RS
9726 prepare_menu_bars ();
9727
28995e67 9728 if (windows_or_buffers_changed)
a2889657
JB
9729 update_mode_lines++;
9730
538f13d4
RS
9731 /* Detect case that we need to write or remove a star in the mode line. */
9732 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
9733 {
9734 w->update_mode_line = Qt;
9735 if (buffer_shared > 1)
9736 update_mode_lines++;
9737 }
9738
5f5c8ee5 9739 /* If %c is in the mode line, update it if needed. */
28995e67
RS
9740 if (!NILP (w->column_number_displayed)
9741 /* This alternative quickly identifies a common case
9742 where no change is needed. */
9743 && !(PT == XFASTINT (w->last_point)
8850a573
RS
9744 && XFASTINT (w->last_modified) >= MODIFF
9745 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
2311178e
TTN
9746 && (XFASTINT (w->column_number_displayed)
9747 != (int) current_column ())) /* iftc */
9748 w->update_mode_line = Qt;
28995e67 9749
44fa5b1e 9750 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 9751
5f5c8ee5
GM
9752 /* The variable buffer_shared is set in redisplay_window and
9753 indicates that we redisplay a buffer in different windows. See
9754 there. */
5fb96e96
RS
9755 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9756 || cursor_type_changed);
a2889657
JB
9757
9758 /* If specs for an arrow have changed, do thorough redisplay
9759 to ensure we remove any arrow that should no longer exist. */
d45de95b 9760 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 9761 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 9762 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 9763
90adcf20
RS
9764 /* Normally the message* functions will have already displayed and
9765 updated the echo area, but the frame may have been trashed, or
9766 the update may have been preempted, so display the echo area
6e019995 9767 again here. Checking message_cleared_p captures the case that
c6e89d6c 9768 the echo area should be cleared. */
96d5e9a0
GM
9769 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9770 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
3fa69ee7
GM
9771 || (message_cleared_p
9772 && minibuf_level == 0
9773 /* If the mini-window is currently selected, this means the
9774 echo-area doesn't show through. */
9775 && !MINI_WINDOW_P (XWINDOW (selected_window))))
90adcf20 9776 {
c6e89d6c 9777 int window_height_changed_p = echo_area_display (0);
90adcf20 9778 must_finish = 1;
6e019995
GM
9779
9780 /* If we don't display the current message, don't clear the
9781 message_cleared_p flag, because, if we did, we wouldn't clear
9782 the echo area in the next redisplay which doesn't preserve
9783 the echo area. */
9784 if (!display_last_displayed_message_p)
9785 message_cleared_p = 0;
2311178e 9786
c6e89d6c
GM
9787 if (fonts_changed_p)
9788 goto retry;
9789 else if (window_height_changed_p)
9790 {
9791 consider_all_windows_p = 1;
9792 ++update_mode_lines;
9793 ++windows_or_buffers_changed;
2311178e 9794
9142dd5b
GM
9795 /* If window configuration was changed, frames may have been
9796 marked garbaged. Clear them or we will experience
9797 surprises wrt scrolling. */
9798 if (frame_garbaged)
9799 clear_garbaged_frames ();
c6e89d6c 9800 }
90adcf20 9801 }
97a36635 9802 else if (EQ (selected_window, minibuf_window)
dd2eb166
GM
9803 && (current_buffer->clip_changed
9804 || XFASTINT (w->last_modified) < MODIFF
9805 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 9806 && resize_mini_window (w, 0))
c6e89d6c
GM
9807 {
9808 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
9809 showing if its contents might have changed. */
9810 must_finish = 1;
9811 consider_all_windows_p = 1;
c6e89d6c 9812 ++windows_or_buffers_changed;
dd2eb166 9813 ++update_mode_lines;
2311178e 9814
9142dd5b
GM
9815 /* If window configuration was changed, frames may have been
9816 marked garbaged. Clear them or we will experience
9817 surprises wrt scrolling. */
9818 if (frame_garbaged)
9819 clear_garbaged_frames ();
c6e89d6c 9820 }
2311178e 9821
90adcf20 9822
5f5c8ee5
GM
9823 /* If showing the region, and mark has changed, we must redisplay
9824 the whole window. The assignment to this_line_start_pos prevents
9825 the optimization directly below this if-statement. */
bd66d1ba
RS
9826 if (((!NILP (Vtransient_mark_mode)
9827 && !NILP (XBUFFER (w->buffer)->mark_active))
9828 != !NILP (w->region_showing))
82d04750
JB
9829 || (!NILP (w->region_showing)
9830 && !EQ (w->region_showing,
9831 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
9832 CHARPOS (this_line_start_pos) = 0;
9833
9834 /* Optimize the case that only the line containing the cursor in the
9835 selected window has changed. Variables starting with this_ are
9836 set in display_line and record information about the line
9837 containing the cursor. */
9838 tlbufpos = this_line_start_pos;
9839 tlendpos = this_line_end_pos;
9840 if (!consider_all_windows_p
9841 && CHARPOS (tlbufpos) > 0
9842 && NILP (w->update_mode_line)
73af359d 9843 && !current_buffer->clip_changed
c62c1bb5 9844 && !current_buffer->prevent_redisplay_optimizations_p
44fa5b1e 9845 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 9846 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 9847 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
9848 && this_line_buffer == current_buffer
9849 && current_buffer == XBUFFER (w->buffer)
265a9e55 9850 && NILP (w->force_start)
acda20e1 9851 && NILP (w->optional_new_start)
5f5c8ee5
GM
9852 /* Point must be on the line that we have info recorded about. */
9853 && PT >= CHARPOS (tlbufpos)
9854 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
9855 /* All text outside that line, including its final newline,
9856 must be unchanged */
5f5c8ee5
GM
9857 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9858 CHARPOS (tlendpos)))
9859 {
9860 if (CHARPOS (tlbufpos) > BEGV
9861 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9862 && (CHARPOS (tlbufpos) == ZV
9863 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
9864 /* Former continuation line has disappeared by becoming empty */
9865 goto cancel;
9866 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 9867 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
9868 || MINI_WINDOW_P (w))
9869 {
1c9241f5
KH
9870 /* We have to handle the case of continuation around a
9871 wide-column character (See the comment in indent.c around
9872 line 885).
9873
9874 For instance, in the following case:
9875
9876 -------- Insert --------
9877 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9878 J_I_ ==> J_I_ `^^' are cursors.
9879 ^^ ^^
9880 -------- --------
9881
9882 As we have to redraw the line above, we should goto cancel. */
9883
5f5c8ee5
GM
9884 struct it it;
9885 int line_height_before = this_line_pixel_height;
9886
9887 /* Note that start_display will handle the case that the
9888 line starting at tlbufpos is a continuation lines. */
9889 start_display (&it, w, tlbufpos);
9890
9891 /* Implementation note: It this still necessary? */
9892 if (it.current_x != this_line_start_x)
1c9241f5
KH
9893 goto cancel;
9894
5f5c8ee5
GM
9895 TRACE ((stderr, "trying display optimization 1\n"));
9896 w->cursor.vpos = -1;
a2889657 9897 overlay_arrow_seen = 0;
5f5c8ee5
GM
9898 it.vpos = this_line_vpos;
9899 it.current_y = this_line_y;
9900 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9901 display_line (&it);
9902
a2889657 9903 /* If line contains point, is not continued,
5f5c8ee5 9904 and ends at same distance from eob as before, we win */
2311178e 9905 if (w->cursor.vpos >= 0
5f5c8ee5
GM
9906 /* Line is not continued, otherwise this_line_start_pos
9907 would have been set to 0 in display_line. */
9908 && CHARPOS (this_line_start_pos)
9909 /* Line ends as before. */
9910 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9911 /* Line has same height as before. Otherwise other lines
9912 would have to be shifted up or down. */
9913 && this_line_pixel_height == line_height_before)
a2889657 9914 {
5f5c8ee5
GM
9915 /* If this is not the window's last line, we must adjust
9916 the charstarts of the lines below. */
9917 if (it.current_y < it.last_visible_y)
9918 {
9919 struct glyph_row *row
9920 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9921 int delta, delta_bytes;
2311178e 9922
5f5c8ee5
GM
9923 if (Z - CHARPOS (tlendpos) == ZV)
9924 {
9925 /* This line ends at end of (accessible part of)
9926 buffer. There is no newline to count. */
9927 delta = (Z
9928 - CHARPOS (tlendpos)
9929 - MATRIX_ROW_START_CHARPOS (row));
9930 delta_bytes = (Z_BYTE
9931 - BYTEPOS (tlendpos)
9932 - MATRIX_ROW_START_BYTEPOS (row));
9933 }
9934 else
9935 {
9936 /* This line ends in a newline. Must take
9937 account of the newline and the rest of the
9938 text that follows. */
9939 delta = (Z
9940 - CHARPOS (tlendpos)
9941 - MATRIX_ROW_START_CHARPOS (row));
9942 delta_bytes = (Z_BYTE
9943 - BYTEPOS (tlendpos)
9944 - MATRIX_ROW_START_BYTEPOS (row));
9945 }
2311178e 9946
f2d86d7a
GM
9947 increment_matrix_positions (w->current_matrix,
9948 this_line_vpos + 1,
9949 w->current_matrix->nrows,
9950 delta, delta_bytes);
85bcef6c 9951 }
46db8486 9952
5f5c8ee5
GM
9953 /* If this row displays text now but previously didn't,
9954 or vice versa, w->window_end_vpos may have to be
9955 adjusted. */
9956 if ((it.glyph_row - 1)->displays_text_p)
9957 {
9958 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9959 XSETINT (w->window_end_vpos, this_line_vpos);
9960 }
9961 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9962 && this_line_vpos > 0)
9963 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9964 w->window_end_valid = Qnil;
2311178e 9965
5f5c8ee5
GM
9966 /* Update hint: No need to try to scroll in update_window. */
9967 w->desired_matrix->no_scrolling_p = 1;
9968
9969#if GLYPH_DEBUG
9970 *w->desired_matrix->method = 0;
9971 debug_method_add (w, "optimization 1");
9972#endif
a2889657
JB
9973 goto update;
9974 }
9975 else
9976 goto cancel;
9977 }
5f5c8ee5
GM
9978 else if (/* Cursor position hasn't changed. */
9979 PT == XFASTINT (w->last_point)
b6f0fe04
RS
9980 /* Make sure the cursor was last displayed
9981 in this window. Otherwise we have to reposition it. */
5f5c8ee5 9982 && 0 <= w->cursor.vpos
da8b7f4f 9983 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
a2889657
JB
9984 {
9985 if (!must_finish)
9986 {
c6e89d6c 9987 do_pending_window_change (1);
5f5c8ee5 9988
2311178e 9989 /* We used to always goto end_of_redisplay here, but this
5f5c8ee5
GM
9990 isn't enough if we have a blinking cursor. */
9991 if (w->cursor_off_p == w->last_cursor_off_p)
9992 goto end_of_redisplay;
a2889657
JB
9993 }
9994 goto update;
9995 }
8b51f1e3
KH
9996 /* If highlighting the region, or if the cursor is in the echo area,
9997 then we can't just move the cursor. */
bd66d1ba
RS
9998 else if (! (!NILP (Vtransient_mark_mode)
9999 && !NILP (current_buffer->mark_active))
97a36635 10000 && (EQ (selected_window, current_buffer->last_selected_window)
293a54ce 10001 || highlight_nonselected_windows)
8b51f1e3 10002 && NILP (w->region_showing)
8f897821 10003 && NILP (Vshow_trailing_whitespace)
8b51f1e3 10004 && !cursor_in_echo_area)
a2889657 10005 {
5f5c8ee5
GM
10006 struct it it;
10007 struct glyph_row *row;
10008
10009 /* Skip from tlbufpos to PT and see where it is. Note that
10010 PT may be in invisible text. If so, we will end at the
10011 next visible position. */
10012 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10013 NULL, DEFAULT_FACE_ID);
10014 it.current_x = this_line_start_x;
10015 it.current_y = this_line_y;
10016 it.vpos = this_line_vpos;
2311178e 10017
5f5c8ee5
GM
10018 /* The call to move_it_to stops in front of PT, but
10019 moves over before-strings. */
10020 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10021
10022 if (it.vpos == this_line_vpos
10023 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10024 row->enabled_p))
a2889657 10025 {
5f5c8ee5
GM
10026 xassert (this_line_vpos == it.vpos);
10027 xassert (this_line_y == it.current_y);
10028 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
ade0bee1
GM
10029#if GLYPH_DEBUG
10030 *w->desired_matrix->method = 0;
10031 debug_method_add (w, "optimization 3");
10032#endif
a2889657
JB
10033 goto update;
10034 }
10035 else
10036 goto cancel;
10037 }
5f5c8ee5 10038
a2889657 10039 cancel:
5f5c8ee5
GM
10040 /* Text changed drastically or point moved off of line. */
10041 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
10042 }
10043
5f5c8ee5
GM
10044 CHARPOS (this_line_start_pos) = 0;
10045 consider_all_windows_p |= buffer_shared > 1;
10046 ++clear_face_cache_count;
a2889657 10047
2311178e 10048
bd9d0f3f
GM
10049 /* Build desired matrices, and update the display. If
10050 consider_all_windows_p is non-zero, do it for all windows on all
10051 frames. Otherwise do it for selected_window, only. */
463f6b91 10052
5f5c8ee5 10053 if (consider_all_windows_p)
a2889657 10054 {
35f56f96 10055 Lisp_Object tail, frame;
0528abe1
GM
10056 int i, n = 0, size = 50;
10057 struct frame **updated
10058 = (struct frame **) alloca (size * sizeof *updated);
a2889657 10059
5f5c8ee5
GM
10060 /* Clear the face cache eventually. */
10061 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 10062 {
5f5c8ee5 10063 clear_face_cache (0);
463f6b91
RS
10064 clear_face_cache_count = 0;
10065 }
31b24551 10066
5f5c8ee5
GM
10067 /* Recompute # windows showing selected buffer. This will be
10068 incremented each time such a window is displayed. */
a2889657
JB
10069 buffer_shared = 0;
10070
35f56f96 10071 FOR_EACH_FRAME (tail, frame)
30c566e4 10072 {
5f5c8ee5 10073 struct frame *f = XFRAME (frame);
2311178e 10074
9628b887 10075 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
9769686d 10076 {
dd429b03
KH
10077 if (! EQ (frame, selected_frame))
10078 /* Select the frame, for the sake of frame-local
10079 variables. */
10080 select_frame_for_redisplay (frame);
10081
ae02e06a 10082#ifdef HAVE_WINDOW_SYSTEM
bb336f8d
RS
10083 if (clear_face_cache_count % 50 == 0
10084 && FRAME_WINDOW_P (f))
10085 clear_image_cache (f, 0);
ae02e06a 10086#endif /* HAVE_WINDOW_SYSTEM */
bb336f8d 10087
5f5c8ee5
GM
10088 /* Mark all the scroll bars to be removed; we'll redeem
10089 the ones we want when we redisplay their windows. */
9769686d 10090 if (condemn_scroll_bars_hook)
504454e8 10091 condemn_scroll_bars_hook (f);
30c566e4 10092
f21ef775 10093 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 10094 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 10095
5f5c8ee5
GM
10096 /* Any scroll bars which redisplay_windows should have
10097 nuked should now go away. */
9769686d 10098 if (judge_scroll_bars_hook)
504454e8 10099 judge_scroll_bars_hook (f);
bd9d0f3f
GM
10100
10101 /* If fonts changed, display again. */
b60c9653
RS
10102 /* ??? rms: I suspect it is a mistake to jump all the way
10103 back to retry here. It should just retry this frame. */
bd9d0f3f
GM
10104 if (fonts_changed_p)
10105 goto retry;
2311178e 10106
bd9d0f3f
GM
10107 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10108 {
10109 /* See if we have to hscroll. */
10110 if (hscroll_windows (f->root_window))
10111 goto retry;
10112
10113 /* Prevent various kinds of signals during display
10114 update. stdio is not robust about handling
10115 signals, which can cause an apparent I/O
10116 error. */
10117 if (interrupt_input)
10118 unrequest_sigio ();
aac2d8b2 10119 STOP_POLLING;
bd9d0f3f
GM
10120
10121 /* Update the display. */
10122 set_window_update_flags (XWINDOW (f->root_window), 1);
10123 pause |= update_frame (f, 0, 0);
ccbb9ed2 10124#if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
bd9d0f3f
GM
10125 if (pause)
10126 break;
ccbb9ed2 10127#endif
bd9d0f3f 10128
0528abe1
GM
10129 if (n == size)
10130 {
10131 int nbytes = size * sizeof *updated;
10132 struct frame **p = (struct frame **) alloca (2 * nbytes);
10133 bcopy (updated, p, nbytes);
10134 size *= 2;
10135 }
2311178e 10136
0528abe1 10137 updated[n++] = f;
bd9d0f3f 10138 }
9769686d 10139 }
30c566e4 10140 }
0528abe1 10141
6f68b035
GM
10142 if (!pause)
10143 {
10144 /* Do the mark_window_display_accurate after all windows have
10145 been redisplayed because this call resets flags in buffers
10146 which are needed for proper redisplay. */
10147 for (i = 0; i < n; ++i)
10148 {
10149 struct frame *f = updated[i];
10150 mark_window_display_accurate (f->root_window, 1);
10151 if (frame_up_to_date_hook)
10152 frame_up_to_date_hook (f);
10153 }
0528abe1 10154 }
a2889657 10155 }
bd9d0f3f
GM
10156 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10157 {
10158 Lisp_Object mini_window;
10159 struct frame *mini_frame;
5f5c8ee5 10160
82a7ab23 10161 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
7033d6df
RS
10162 /* Use list_of_error, not Qerror, so that
10163 we catch only errors and don't run the debugger. */
10164 internal_condition_case_1 (redisplay_window_1, selected_window,
10165 list_of_error,
82a7ab23 10166 redisplay_window_error);
2311178e 10167
bd9d0f3f 10168 /* Compare desired and current matrices, perform output. */
7d0393cf 10169
26683087 10170 update:
bd9d0f3f
GM
10171 /* If fonts changed, display again. */
10172 if (fonts_changed_p)
92a90e89 10173 goto retry;
a2889657 10174
bd9d0f3f
GM
10175 /* Prevent various kinds of signals during display update.
10176 stdio is not robust about handling signals,
10177 which can cause an apparent I/O error. */
10178 if (interrupt_input)
10179 unrequest_sigio ();
aac2d8b2 10180 STOP_POLLING;
1af9f229 10181
bd9d0f3f 10182 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
5f5c8ee5 10183 {
92a90e89
GM
10184 if (hscroll_windows (selected_window))
10185 goto retry;
2311178e 10186
5f5c8ee5 10187 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 10188 pause = update_frame (sf, 0, 0);
5f5c8ee5 10189 }
d724d989 10190
8de2d90b 10191 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
10192 function. If the echo area is on another frame, that may
10193 have put text on a frame other than the selected one, so the
10194 above call to update_frame would not have caught it. Catch
8de2d90b 10195 it here. */
bd9d0f3f
GM
10196 mini_window = FRAME_MINIBUF_WINDOW (sf);
10197 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
2311178e 10198
bd9d0f3f
GM
10199 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10200 {
10201 XWINDOW (mini_window)->must_be_updated_p = 1;
10202 pause |= update_frame (mini_frame, 0, 0);
10203 if (!pause && hscroll_windows (mini_window))
10204 goto retry;
10205 }
6e8290aa 10206 }
a2889657 10207
5f5c8ee5
GM
10208 /* If display was paused because of pending input, make sure we do a
10209 thorough update the next time. */
a2889657
JB
10210 if (pause)
10211 {
5f5c8ee5
GM
10212 /* Prevent the optimization at the beginning of
10213 redisplay_internal that tries a single-line update of the
10214 line containing the cursor in the selected window. */
10215 CHARPOS (this_line_start_pos) = 0;
10216
10217 /* Let the overlay arrow be updated the next time. */
265a9e55 10218 if (!NILP (last_arrow_position))
a2889657
JB
10219 {
10220 last_arrow_position = Qt;
10221 last_arrow_string = Qt;
10222 }
2311178e 10223
5f5c8ee5
GM
10224 /* If we pause after scrolling, some rows in the current
10225 matrices of some windows are not valid. */
10226 if (!WINDOW_FULL_WIDTH_P (w)
10227 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
10228 update_mode_lines = 1;
10229 }
43c09969 10230 else
a2889657 10231 {
43c09969 10232 if (!consider_all_windows_p)
a2889657 10233 {
43c09969
GM
10234 /* This has already been done above if
10235 consider_all_windows_p is set. */
10236 mark_window_display_accurate_1 (w, 1);
2311178e 10237
927c5b3b
GM
10238 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10239 last_arrow_string = Voverlay_arrow_string;
2311178e 10240
efc63ef0 10241 if (frame_up_to_date_hook != 0)
43c09969 10242 frame_up_to_date_hook (sf);
a2889657 10243 }
15e26c76 10244
a2889657
JB
10245 update_mode_lines = 0;
10246 windows_or_buffers_changed = 0;
5fb96e96 10247 cursor_type_changed = 0;
a2889657
JB
10248 }
10249
5f5c8ee5
GM
10250 /* Start SIGIO interrupts coming again. Having them off during the
10251 code above makes it less likely one will discard output, but not
10252 impossible, since there might be stuff in the system buffer here.
a2889657 10253 But it is much hairier to try to do anything about that. */
a2889657
JB
10254 if (interrupt_input)
10255 request_sigio ();
aac2d8b2 10256 RESUME_POLLING;
a2889657 10257
5f5c8ee5
GM
10258 /* If a frame has become visible which was not before, redisplay
10259 again, so that we display it. Expose events for such a frame
10260 (which it gets when becoming visible) don't call the parts of
10261 redisplay constructing glyphs, so simply exposing a frame won't
10262 display anything in this case. So, we have to display these
10263 frames here explicitly. */
11c52c4f
RS
10264 if (!pause)
10265 {
10266 Lisp_Object tail, frame;
10267 int new_count = 0;
10268
10269 FOR_EACH_FRAME (tail, frame)
10270 {
10271 int this_is_visible = 0;
8e83f802
RS
10272
10273 if (XFRAME (frame)->visible)
10274 this_is_visible = 1;
10275 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10276 if (XFRAME (frame)->visible)
10277 this_is_visible = 1;
11c52c4f
RS
10278
10279 if (this_is_visible)
10280 new_count++;
10281 }
10282
89819bdd 10283 if (new_count != number_of_visible_frames)
11c52c4f
RS
10284 windows_or_buffers_changed++;
10285 }
10286
44fa5b1e 10287 /* Change frame size now if a change is pending. */
c6e89d6c 10288 do_pending_window_change (1);
d8e242fd 10289
8b32d885
RS
10290 /* If we just did a pending size change, or have additional
10291 visible frames, redisplay again. */
3c8c72e0 10292 if (windows_or_buffers_changed && !pause)
8b32d885 10293 goto retry;
5f5c8ee5 10294
1987b083 10295 end_of_redisplay:
28514cd9 10296 unbind_to (count, Qnil);
aac2d8b2 10297 RESUME_POLLING;
a2889657
JB
10298}
10299
5f5c8ee5
GM
10300
10301/* Redisplay, but leave alone any recent echo area message unless
10302 another message has been requested in its place.
a2889657
JB
10303
10304 This is useful in situations where you need to redisplay but no
10305 user action has occurred, making it inappropriate for the message
10306 area to be cleared. See tracking_off and
9bf76936
GM
10307 wait_reading_process_input for examples of these situations.
10308
10309 FROM_WHERE is an integer saying from where this function was
10310 called. This is useful for debugging. */
a2889657 10311
8991bb31 10312void
9bf76936
GM
10313redisplay_preserve_echo_area (from_where)
10314 int from_where;
a2889657 10315{
9bf76936
GM
10316 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10317
c6e89d6c 10318 if (!NILP (echo_area_buffer[1]))
a2889657 10319 {
c6e89d6c
GM
10320 /* We have a previously displayed message, but no current
10321 message. Redisplay the previous message. */
10322 display_last_displayed_message_p = 1;
e9874cee 10323 redisplay_internal (1);
c6e89d6c 10324 display_last_displayed_message_p = 0;
a2889657
JB
10325 }
10326 else
e9874cee 10327 redisplay_internal (1);
a2889657
JB
10328}
10329
5f5c8ee5 10330
28514cd9 10331/* Function registered with record_unwind_protect in
acfca545
RS
10332 redisplay_internal. Reset redisplaying_p to the value it had
10333 before redisplay_internal was called, and clear
dd429b03
KH
10334 prevent_freeing_realized_faces_p. It also selects the previously
10335 selected frame. */
28514cd9
GM
10336
10337static Lisp_Object
dd429b03
KH
10338unwind_redisplay (val)
10339 Lisp_Object val;
28514cd9 10340{
dd429b03
KH
10341 Lisp_Object old_redisplaying_p, old_frame;
10342
10343 old_redisplaying_p = XCAR (val);
28514cd9 10344 redisplaying_p = XFASTINT (old_redisplaying_p);
dd429b03
KH
10345 old_frame = XCDR (val);
10346 if (! EQ (old_frame, selected_frame))
10347 select_frame_for_redisplay (old_frame);
c6e89d6c 10348 return Qnil;
28514cd9
GM
10349}
10350
10351
43c09969
GM
10352/* Mark the display of window W as accurate or inaccurate. If
10353 ACCURATE_P is non-zero mark display of W as accurate. If
10354 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10355 redisplay_internal is called. */
5f5c8ee5 10356
43c09969
GM
10357static void
10358mark_window_display_accurate_1 (w, accurate_p)
10359 struct window *w;
5f5c8ee5 10360 int accurate_p;
a2889657 10361{
43c09969 10362 if (BUFFERP (w->buffer))
a2889657 10363 {
43c09969 10364 struct buffer *b = XBUFFER (w->buffer);
2311178e 10365
43c09969
GM
10366 w->last_modified
10367 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10368 w->last_overlay_modified
10369 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10370 w->last_had_star
10371 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
a2889657 10372
43c09969 10373 if (accurate_p)
bd66d1ba 10374 {
43c09969
GM
10375 b->clip_changed = 0;
10376 b->prevent_redisplay_optimizations_p = 0;
10377
10378 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10379 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10380 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10381 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
2311178e 10382
43c09969
GM
10383 w->current_matrix->buffer = b;
10384 w->current_matrix->begv = BUF_BEGV (b);
10385 w->current_matrix->zv = BUF_ZV (b);
2311178e 10386
43c09969
GM
10387 w->last_cursor = w->cursor;
10388 w->last_cursor_off_p = w->cursor_off_p;
2311178e 10389
43c09969
GM
10390 if (w == XWINDOW (selected_window))
10391 w->last_point = make_number (BUF_PT (b));
10392 else
10393 w->last_point = make_number (XMARKER (w->pointm)->charpos);
bd66d1ba 10394 }
43c09969 10395 }
bd66d1ba 10396
43c09969
GM
10397 if (accurate_p)
10398 {
d2f84654 10399 w->window_end_valid = w->buffer;
99332eb6 10400#if 0 /* This is incorrect with variable-height lines. */
2913a9c0 10401 xassert (XINT (w->window_end_vpos)
da8b7f4f 10402 < (WINDOW_TOTAL_LINES (w)
2913a9c0 10403 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
99332eb6 10404#endif
a2889657 10405 w->update_mode_line = Qnil;
43c09969
GM
10406 }
10407}
10408
10409
10410/* Mark the display of windows in the window tree rooted at WINDOW as
10411 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10412 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10413 be redisplayed the next time redisplay_internal is called. */
10414
10415void
10416mark_window_display_accurate (window, accurate_p)
10417 Lisp_Object window;
10418 int accurate_p;
10419{
10420 struct window *w;
2311178e 10421
43c09969
GM
10422 for (; !NILP (window); window = w->next)
10423 {
10424 w = XWINDOW (window);
10425 mark_window_display_accurate_1 (w, accurate_p);
a2889657 10426
265a9e55 10427 if (!NILP (w->vchild))
5f5c8ee5 10428 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 10429 if (!NILP (w->hchild))
5f5c8ee5 10430 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
10431 }
10432
5f5c8ee5 10433 if (accurate_p)
a2889657 10434 {
d45de95b 10435 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
10436 last_arrow_string = Voverlay_arrow_string;
10437 }
10438 else
10439 {
5f5c8ee5
GM
10440 /* Force a thorough redisplay the next time by setting
10441 last_arrow_position and last_arrow_string to t, which is
4b41cebb 10442 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
10443 last_arrow_position = Qt;
10444 last_arrow_string = Qt;
10445 }
10446}
5f5c8ee5
GM
10447
10448
10449/* Return value in display table DP (Lisp_Char_Table *) for character
10450 C. Since a display table doesn't have any parent, we don't have to
10451 follow parent. Do not call this function directly but use the
10452 macro DISP_CHAR_VECTOR. */
10453
10454Lisp_Object
10455disp_char_vector (dp, c)
10456 struct Lisp_Char_Table *dp;
10457 int c;
10458{
10459 int code[4], i;
10460 Lisp_Object val;
10461
10462 if (SINGLE_BYTE_CHAR_P (c))
10463 return (dp->contents[c]);
2311178e 10464
c5924f47 10465 SPLIT_CHAR (c, code[0], code[1], code[2]);
260a86a0
KH
10466 if (code[1] < 32)
10467 code[1] = -1;
10468 else if (code[2] < 32)
10469 code[2] = -1;
2311178e 10470
5f5c8ee5
GM
10471 /* Here, the possible range of code[0] (== charset ID) is
10472 128..max_charset. Since the top level char table contains data
10473 for multibyte characters after 256th element, we must increment
10474 code[0] by 128 to get a correct index. */
10475 code[0] += 128;
10476 code[3] = -1; /* anchor */
10477
10478 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10479 {
10480 val = dp->contents[code[i]];
10481 if (!SUB_CHAR_TABLE_P (val))
10482 return (NILP (val) ? dp->defalt : val);
10483 }
2311178e 10484
5f5c8ee5
GM
10485 /* Here, val is a sub char table. We return the default value of
10486 it. */
10487 return (dp->defalt);
10488}
10489
10490
a2889657 10491\f
5f5c8ee5
GM
10492/***********************************************************************
10493 Window Redisplay
10494 ***********************************************************************/
a2725ab2 10495
5f5c8ee5 10496/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
10497
10498static void
5f5c8ee5
GM
10499redisplay_windows (window)
10500 Lisp_Object window;
90adcf20 10501{
5f5c8ee5
GM
10502 while (!NILP (window))
10503 {
10504 struct window *w = XWINDOW (window);
2311178e 10505
5f5c8ee5
GM
10506 if (!NILP (w->hchild))
10507 redisplay_windows (w->hchild);
10508 else if (!NILP (w->vchild))
10509 redisplay_windows (w->vchild);
10510 else
82a7ab23
RS
10511 {
10512 displayed_buffer = XBUFFER (w->buffer);
7033d6df
RS
10513 /* Use list_of_error, not Qerror, so that
10514 we catch only errors and don't run the debugger. */
2311178e 10515 internal_condition_case_1 (redisplay_window_0, window,
7033d6df 10516 list_of_error,
82a7ab23
RS
10517 redisplay_window_error);
10518 }
a2725ab2 10519
5f5c8ee5
GM
10520 window = w->next;
10521 }
10522}
10523
82a7ab23
RS
10524static Lisp_Object
10525redisplay_window_error ()
10526{
10527 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10528 return Qnil;
10529}
10530
10531static Lisp_Object
10532redisplay_window_0 (window)
10533 Lisp_Object window;
10534{
10535 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10536 redisplay_window (window, 0);
10537 return Qnil;
10538}
5f5c8ee5 10539
82a7ab23
RS
10540static Lisp_Object
10541redisplay_window_1 (window)
10542 Lisp_Object window;
10543{
10544 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10545 redisplay_window (window, 1);
10546 return Qnil;
10547}
10548\f
bc6371a6
KH
10549
10550/* Increment GLYPH until it reaches END or CONDITION fails while
10551 adding (GLYPH)->pixel_width to X. */
10552
10553#define SKIP_GLYPHS(glyph, end, x, condition) \
10554 do \
10555 { \
10556 (x) += (glyph)->pixel_width; \
10557 ++(glyph); \
10558 } \
10559 while ((glyph) < (end) && (condition))
10560
10561
5f5c8ee5
GM
10562/* Set cursor position of W. PT is assumed to be displayed in ROW.
10563 DELTA is the number of bytes by which positions recorded in ROW
10564 differ from current buffer positions. */
10565
10566void
10567set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10568 struct window *w;
10569 struct glyph_row *row;
10570 struct glyph_matrix *matrix;
10571 int delta, delta_bytes, dy, dvpos;
10572{
10573 struct glyph *glyph = row->glyphs[TEXT_AREA];
10574 struct glyph *end = glyph + row->used[TEXT_AREA];
ae73dc1c
KH
10575 /* The first glyph that starts a sequence of glyphs from string. */
10576 struct glyph *string_start;
10577 /* The X coordinate of string_start. */
10578 int string_start_x;
10579 /* The last known character position. */
10580 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10581 /* The last known character position before string_start. */
10582 int string_before_pos;
5f5c8ee5
GM
10583 int x = row->x;
10584 int pt_old = PT - delta;
10585
10586 /* Skip over glyphs not having an object at the start of the row.
10587 These are special glyphs like truncation marks on terminal
10588 frames. */
10589 if (row->displays_text_p)
10590 while (glyph < end
6fc556fd 10591 && INTEGERP (glyph->object)
5f5c8ee5
GM
10592 && glyph->charpos < 0)
10593 {
10594 x += glyph->pixel_width;
10595 ++glyph;
10596 }
10597
ae73dc1c 10598 string_start = NULL;
5f5c8ee5 10599 while (glyph < end
6fc556fd 10600 && !INTEGERP (glyph->object)
5f5c8ee5 10601 && (!BUFFERP (glyph->object)
ae73dc1c 10602 || (last_pos = glyph->charpos) < pt_old))
5f5c8ee5 10603 {
ae73dc1c
KH
10604 if (! STRINGP (glyph->object))
10605 {
10606 string_start = NULL;
10607 x += glyph->pixel_width;
10608 ++glyph;
10609 }
10610 else
10611 {
10612 string_before_pos = last_pos;
10613 string_start = glyph;
10614 string_start_x = x;
10615 /* Skip all glyphs from string. */
bc6371a6 10616 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
ae73dc1c
KH
10617 }
10618 }
10619
10620 if (string_start
10621 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10622 {
10623 /* We may have skipped over point because the previous glyphs
10624 are from string. As there's no easy way to know the
10625 character position of the current glyph, find the correct
10626 glyph on point by scanning from string_start again. */
bc6371a6
KH
10627 Lisp_Object limit;
10628 Lisp_Object string;
10629 int pos;
ae73dc1c 10630
bc6371a6
KH
10631 limit = make_number (pt_old + 1);
10632 end = glyph;
ae73dc1c
KH
10633 glyph = string_start;
10634 x = string_start_x;
bc6371a6
KH
10635 string = glyph->object;
10636 pos = string_buffer_position (w, string, string_before_pos);
10637 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10638 because we always put cursor after overlay strings. */
10639 while (pos == 0 && glyph < end)
ae73dc1c 10640 {
bc6371a6
KH
10641 string = glyph->object;
10642 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10643 if (glyph < end)
10644 pos = string_buffer_position (w, glyph->object, string_before_pos);
10645 }
10646
10647 while (glyph < end)
10648 {
10649 pos = XINT (Fnext_single_char_property_change
10650 (make_number (pos), Qdisplay, Qnil, limit));
10651 if (pos > pt_old)
10652 break;
ae73dc1c 10653 /* Skip glyphs from the same string. */
bc6371a6
KH
10654 string = glyph->object;
10655 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10656 /* Skip glyphs from an overlay. */
10657 while (glyph < end
10658 && ! string_buffer_position (w, glyph->object, pos))
ae73dc1c 10659 {
bc6371a6
KH
10660 string = glyph->object;
10661 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
ae73dc1c 10662 }
ae73dc1c 10663 }
5f5c8ee5
GM
10664 }
10665
10666 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10667 w->cursor.x = x;
10668 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10669 w->cursor.y = row->y + dy;
10670
10671 if (w == XWINDOW (selected_window))
10672 {
10673 if (!row->continued_p
10674 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10675 && row->x == 0)
10676 {
10677 this_line_buffer = XBUFFER (w->buffer);
2311178e 10678
5f5c8ee5
GM
10679 CHARPOS (this_line_start_pos)
10680 = MATRIX_ROW_START_CHARPOS (row) + delta;
10681 BYTEPOS (this_line_start_pos)
10682 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
2311178e 10683
5f5c8ee5
GM
10684 CHARPOS (this_line_end_pos)
10685 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10686 BYTEPOS (this_line_end_pos)
10687 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
2311178e 10688
5f5c8ee5
GM
10689 this_line_y = w->cursor.y;
10690 this_line_pixel_height = row->height;
10691 this_line_vpos = w->cursor.vpos;
10692 this_line_start_x = row->x;
10693 }
10694 else
10695 CHARPOS (this_line_start_pos) = 0;
10696 }
10697}
10698
10699
10700/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
10701 start STARTP. Sets the window start of WINDOW to that position.
10702
10703 We assume that the window's buffer is really current. */
5f5c8ee5
GM
10704
10705static INLINE struct text_pos
10706run_window_scroll_functions (window, startp)
10707 Lisp_Object window;
10708 struct text_pos startp;
10709{
10710 struct window *w = XWINDOW (window);
10711 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
10712
10713 if (current_buffer != XBUFFER (w->buffer))
10714 abort ();
10715
5f5c8ee5
GM
10716 if (!NILP (Vwindow_scroll_functions))
10717 {
2311178e 10718 run_hook_with_args_2 (Qwindow_scroll_functions, window,
5f5c8ee5
GM
10719 make_number (CHARPOS (startp)));
10720 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
10721 /* In case the hook functions switch buffers. */
10722 if (current_buffer != XBUFFER (w->buffer))
10723 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 10724 }
90adcf20 10725
5f5c8ee5
GM
10726 return startp;
10727}
10728
10729
ddf6b9a3
RS
10730/* Make sure the line containing the cursor is fully visible.
10731 A value of 1 means there is nothing to be done.
10732 (Either the line is fully visible, or it cannot be made so,
10733 or we cannot tell.)
10734 A value of 0 means the caller should do scrolling
10735 as if point had gone off the screen. */
5f5c8ee5 10736
cb617e7c 10737static int
5f5c8ee5
GM
10738make_cursor_line_fully_visible (w)
10739 struct window *w;
10740{
10741 struct glyph_matrix *matrix;
10742 struct glyph_row *row;
478d746b 10743 int window_height;
2311178e 10744
5f5c8ee5
GM
10745 /* It's not always possible to find the cursor, e.g, when a window
10746 is full of overlay strings. Don't do anything in that case. */
10747 if (w->cursor.vpos < 0)
cb617e7c 10748 return 1;
2311178e 10749
5f5c8ee5
GM
10750 matrix = w->desired_matrix;
10751 row = MATRIX_ROW (matrix, w->cursor.vpos);
10752
acda20e1 10753 /* If the cursor row is not partially visible, there's nothing to do. */
b28cb6ed 10754 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
cb617e7c 10755 return 1;
b28cb6ed
GM
10756
10757 /* If the row the cursor is in is taller than the window's height,
10758 it's not clear what to do, so do nothing. */
10759 window_height = window_box_height (w);
10760 if (row->height >= window_height)
cb617e7c 10761 return 1;
b28cb6ed 10762
ddf6b9a3
RS
10763 return 0;
10764
10765#if 0
10766 /* This code used to try to scroll the window just enough to make
10767 the line visible. It returned 0 to say that the caller should
10768 allocate larger glyph matrices. */
10769
b28cb6ed 10770 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
5f5c8ee5
GM
10771 {
10772 int dy = row->height - row->visible_height;
10773 w->vscroll = 0;
10774 w->cursor.y += dy;
10775 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10776 }
b28cb6ed 10777 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
5f5c8ee5
GM
10778 {
10779 int dy = - (row->height - row->visible_height);
10780 w->vscroll = dy;
10781 w->cursor.y += dy;
10782 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10783 }
2311178e 10784
5f5c8ee5
GM
10785 /* When we change the cursor y-position of the selected window,
10786 change this_line_y as well so that the display optimization for
10787 the cursor line of the selected window in redisplay_internal uses
10788 the correct y-position. */
10789 if (w == XWINDOW (selected_window))
10790 this_line_y = w->cursor.y;
cb617e7c
GM
10791
10792 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10793 redisplay with larger matrices. */
10794 if (matrix->nrows < required_matrix_height (w))
10795 {
10796 fonts_changed_p = 1;
10797 return 0;
10798 }
10799
10800 return 1;
ddf6b9a3 10801#endif /* 0 */
5f5c8ee5
GM
10802}
10803
10804
10805/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10806 non-zero means only WINDOW is redisplayed in redisplay_internal.
10807 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10808 in redisplay_window to bring a partially visible line into view in
10809 the case that only the cursor has moved.
10810
03b0a4b4
RS
10811 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10812 last screen line's vertical height extends past the end of the screen.
10813
5f5c8ee5
GM
10814 Value is
10815
10816 1 if scrolling succeeded
2311178e 10817
5f5c8ee5 10818 0 if scrolling didn't find point.
2311178e 10819
5f5c8ee5
GM
10820 -1 if new fonts have been loaded so that we must interrupt
10821 redisplay, adjust glyph matrices, and try again. */
10822
cb617e7c
GM
10823enum
10824{
10825 SCROLLING_SUCCESS,
10826 SCROLLING_FAILED,
10827 SCROLLING_NEED_LARGER_MATRICES
10828};
10829
5f5c8ee5
GM
10830static int
10831try_scrolling (window, just_this_one_p, scroll_conservatively,
03b0a4b4 10832 scroll_step, temp_scroll_step, last_line_misfit)
5f5c8ee5
GM
10833 Lisp_Object window;
10834 int just_this_one_p;
31ade731 10835 EMACS_INT scroll_conservatively, scroll_step;
5f5c8ee5 10836 int temp_scroll_step;
03b0a4b4 10837 int last_line_misfit;
5f5c8ee5
GM
10838{
10839 struct window *w = XWINDOW (window);
10840 struct frame *f = XFRAME (w->frame);
10841 struct text_pos scroll_margin_pos;
10842 struct text_pos pos;
10843 struct text_pos startp;
10844 struct it it;
10845 Lisp_Object window_end;
10846 int this_scroll_margin;
10847 int dy = 0;
10848 int scroll_max;
b8a63ccb 10849 int rc;
5f5c8ee5
GM
10850 int amount_to_scroll = 0;
10851 Lisp_Object aggressive;
10852 int height;
03b0a4b4 10853 int end_scroll_margin;
5f5c8ee5
GM
10854
10855#if GLYPH_DEBUG
10856 debug_method_add (w, "try_scrolling");
78614721 10857#endif
5f5c8ee5
GM
10858
10859 SET_TEXT_POS_FROM_MARKER (startp, w->start);
2311178e 10860
5f5c8ee5
GM
10861 /* Compute scroll margin height in pixels. We scroll when point is
10862 within this distance from the top or bottom of the window. */
10863 if (scroll_margin > 0)
90adcf20 10864 {
da8b7f4f
KS
10865 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10866 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
5f5c8ee5
GM
10867 }
10868 else
10869 this_scroll_margin = 0;
10870
10871 /* Compute how much we should try to scroll maximally to bring point
10872 into view. */
0894e696
SM
10873 if (scroll_step || scroll_conservatively || temp_scroll_step)
10874 scroll_max = max (scroll_step,
10875 max (scroll_conservatively, temp_scroll_step));
5f5c8ee5
GM
10876 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10877 || NUMBERP (current_buffer->scroll_up_aggressively))
10878 /* We're trying to scroll because of aggressive scrolling
10879 but no scroll_step is set. Choose an arbitrary one. Maybe
10880 there should be a variable for this. */
10881 scroll_max = 10;
10882 else
10883 scroll_max = 0;
da8b7f4f 10884 scroll_max *= FRAME_LINE_HEIGHT (f);
5f5c8ee5
GM
10885
10886 /* Decide whether we have to scroll down. Start at the window end
10887 and move this_scroll_margin up to find the position of the scroll
10888 margin. */
10889 window_end = Fwindow_end (window, Qt);
03b0a4b4
RS
10890
10891 too_near_end:
10892
5f5c8ee5
GM
10893 CHARPOS (scroll_margin_pos) = XINT (window_end);
10894 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
03b0a4b4
RS
10895
10896 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
10897 if (end_scroll_margin)
5f5c8ee5
GM
10898 {
10899 start_display (&it, w, scroll_margin_pos);
03b0a4b4 10900 move_it_vertically (&it, - end_scroll_margin);
5f5c8ee5
GM
10901 scroll_margin_pos = it.current.pos;
10902 }
10903
10904 if (PT >= CHARPOS (scroll_margin_pos))
10905 {
10906 int y0;
2311178e 10907
5f5c8ee5
GM
10908 /* Point is in the scroll margin at the bottom of the window, or
10909 below. Compute a new window start that makes point visible. */
47589c8c 10910
5f5c8ee5
GM
10911 /* Compute the distance from the scroll margin to PT.
10912 Give up if the distance is greater than scroll_max. */
10913 start_display (&it, w, scroll_margin_pos);
10914 y0 = it.current_y;
10915 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10916 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
539e92ad
GM
10917
10918 /* To make point visible, we have to move the window start
10919 down so that the line the cursor is in is visible, which
10920 means we have to add in the height of the cursor line. */
10921 dy = line_bottom_y (&it) - y0;
2311178e 10922
5f5c8ee5 10923 if (dy > scroll_max)
cb617e7c 10924 return SCROLLING_FAILED;
2311178e 10925
5f5c8ee5
GM
10926 /* Move the window start down. If scrolling conservatively,
10927 move it just enough down to make point visible. If
10928 scroll_step is set, move it down by scroll_step. */
10929 start_display (&it, w, startp);
10930
10931 if (scroll_conservatively)
03b0a4b4
RS
10932 /* Set AMOUNT_TO_SCROLL to at least one line,
10933 and at most scroll_conservatively lines. */
e89aaabd 10934 amount_to_scroll
da8b7f4f
KS
10935 = min (max (dy, FRAME_LINE_HEIGHT (f)),
10936 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
5f5c8ee5
GM
10937 else if (scroll_step || temp_scroll_step)
10938 amount_to_scroll = scroll_max;
10939 else
90adcf20 10940 {
46226c1d 10941 aggressive = current_buffer->scroll_up_aggressively;
da8b7f4f 10942 height = WINDOW_BOX_TEXT_HEIGHT (w);
5f5c8ee5
GM
10943 if (NUMBERP (aggressive))
10944 amount_to_scroll = XFLOATINT (aggressive) * height;
10945 }
a2725ab2 10946
5f5c8ee5 10947 if (amount_to_scroll <= 0)
cb617e7c 10948 return SCROLLING_FAILED;
a2725ab2 10949
03b0a4b4
RS
10950 /* If moving by amount_to_scroll leaves STARTP unchanged,
10951 move it down one screen line. */
10952
5f5c8ee5 10953 move_it_vertically (&it, amount_to_scroll);
03b0a4b4
RS
10954 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10955 move_it_by_lines (&it, 1, 1);
5f5c8ee5
GM
10956 startp = it.current.pos;
10957 }
10958 else
10959 {
10960 /* See if point is inside the scroll margin at the top of the
10961 window. */
10962 scroll_margin_pos = startp;
10963 if (this_scroll_margin)
10964 {
10965 start_display (&it, w, startp);
10966 move_it_vertically (&it, this_scroll_margin);
10967 scroll_margin_pos = it.current.pos;
10968 }
10969
10970 if (PT < CHARPOS (scroll_margin_pos))
10971 {
10972 /* Point is in the scroll margin at the top of the window or
10973 above what is displayed in the window. */
10974 int y0;
2311178e 10975
5f5c8ee5
GM
10976 /* Compute the vertical distance from PT to the scroll
10977 margin position. Give up if distance is greater than
10978 scroll_max. */
10979 SET_TEXT_POS (pos, PT, PT_BYTE);
10980 start_display (&it, w, pos);
10981 y0 = it.current_y;
10982 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
10983 it.last_visible_y, -1,
10984 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10985 dy = it.current_y - y0;
10986 if (dy > scroll_max)
cb617e7c 10987 return SCROLLING_FAILED;
2311178e 10988
5f5c8ee5
GM
10989 /* Compute new window start. */
10990 start_display (&it, w, startp);
2311178e 10991
5f5c8ee5 10992 if (scroll_conservatively)
0894e696 10993 amount_to_scroll =
da8b7f4f 10994 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
10995 else if (scroll_step || temp_scroll_step)
10996 amount_to_scroll = scroll_max;
538f13d4 10997 else
5f5c8ee5 10998 {
46226c1d 10999 aggressive = current_buffer->scroll_down_aggressively;
da8b7f4f 11000 height = WINDOW_BOX_TEXT_HEIGHT (w);
5f5c8ee5
GM
11001 if (NUMBERP (aggressive))
11002 amount_to_scroll = XFLOATINT (aggressive) * height;
11003 }
a2725ab2 11004
5f5c8ee5 11005 if (amount_to_scroll <= 0)
cb617e7c 11006 return SCROLLING_FAILED;
2311178e 11007
5f5c8ee5
GM
11008 move_it_vertically (&it, - amount_to_scroll);
11009 startp = it.current.pos;
90adcf20
RS
11010 }
11011 }
a2889657 11012
5f5c8ee5
GM
11013 /* Run window scroll functions. */
11014 startp = run_window_scroll_functions (window, startp);
90adcf20 11015
5f5c8ee5
GM
11016 /* Display the window. Give up if new fonts are loaded, or if point
11017 doesn't appear. */
11018 if (!try_window (window, startp))
cb617e7c 11019 rc = SCROLLING_NEED_LARGER_MATRICES;
5f5c8ee5
GM
11020 else if (w->cursor.vpos < 0)
11021 {
11022 clear_glyph_matrix (w->desired_matrix);
cb617e7c 11023 rc = SCROLLING_FAILED;
5f5c8ee5
GM
11024 }
11025 else
11026 {
11027 /* Maybe forget recorded base line for line number display. */
2311178e 11028 if (!just_this_one_p
5f5c8ee5 11029 || current_buffer->clip_changed
9142dd5b 11030 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5 11031 w->base_line_number = Qnil;
2311178e 11032
ddf6b9a3
RS
11033 /* If cursor ends up on a partially visible line,
11034 treat that as being off the bottom of the screen. */
11035 if (! make_cursor_line_fully_visible (w))
ef3c2c73
RS
11036 {
11037 clear_glyph_matrix (w->desired_matrix);
03b0a4b4 11038 last_line_misfit = 1;
ef3c2c73
RS
11039 goto too_near_end;
11040 }
ddf6b9a3 11041 rc = SCROLLING_SUCCESS;
5f5c8ee5
GM
11042 }
11043
11044 return rc;
a2889657
JB
11045}
11046
5f5c8ee5
GM
11047
11048/* Compute a suitable window start for window W if display of W starts
11049 on a continuation line. Value is non-zero if a new window start
11050 was computed.
11051
11052 The new window start will be computed, based on W's width, starting
11053 from the start of the continued line. It is the start of the
11054 screen line with the minimum distance from the old start W->start. */
11055
11056static int
11057compute_window_start_on_continuation_line (w)
11058 struct window *w;
1f1ff51d 11059{
5f5c8ee5
GM
11060 struct text_pos pos, start_pos;
11061 int window_start_changed_p = 0;
1f1ff51d 11062
5f5c8ee5 11063 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 11064
5f5c8ee5 11065 /* If window start is on a continuation line... Window start may be
2311178e 11066 < BEGV in case there's invisible text at the start of the
5f5c8ee5
GM
11067 buffer (M-x rmail, for example). */
11068 if (CHARPOS (start_pos) > BEGV
11069 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 11070 {
5f5c8ee5
GM
11071 struct it it;
11072 struct glyph_row *row;
f3751a65
GM
11073
11074 /* Handle the case that the window start is out of range. */
11075 if (CHARPOS (start_pos) < BEGV)
11076 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11077 else if (CHARPOS (start_pos) > ZV)
11078 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
2311178e 11079
5f5c8ee5
GM
11080 /* Find the start of the continued line. This should be fast
11081 because scan_buffer is fast (newline cache). */
045dee35 11082 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
11083 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11084 row, DEFAULT_FACE_ID);
11085 reseat_at_previous_visible_line_start (&it);
11086
11087 /* If the line start is "too far" away from the window start,
11088 say it takes too much time to compute a new window start. */
11089 if (CHARPOS (start_pos) - IT_CHARPOS (it)
da8b7f4f 11090 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
5f5c8ee5
GM
11091 {
11092 int min_distance, distance;
2311178e 11093
5f5c8ee5
GM
11094 /* Move forward by display lines to find the new window
11095 start. If window width was enlarged, the new start can
11096 be expected to be > the old start. If window width was
11097 decreased, the new window start will be < the old start.
11098 So, we're looking for the display line start with the
11099 minimum distance from the old window start. */
11100 pos = it.current.pos;
11101 min_distance = INFINITY;
11102 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11103 distance < min_distance)
11104 {
11105 min_distance = distance;
11106 pos = it.current.pos;
11107 move_it_by_lines (&it, 1, 0);
11108 }
2311178e 11109
5f5c8ee5
GM
11110 /* Set the window start there. */
11111 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11112 window_start_changed_p = 1;
11113 }
1f1ff51d 11114 }
2311178e 11115
5f5c8ee5 11116 return window_start_changed_p;
1f1ff51d
KH
11117}
11118
5f5c8ee5 11119
1dd5768b 11120/* Try cursor movement in case text has not changed in window WINDOW,
47589c8c
GM
11121 with window start STARTP. Value is
11122
cb617e7c 11123 CURSOR_MOVEMENT_SUCCESS if successful
2311178e 11124
cb617e7c
GM
11125 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11126
11127 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11128 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11129 we want to scroll as if scroll-step were set to 1. See the code.
11130
11131 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11132 which case we have to abort this redisplay, and adjust matrices
11133 first. */
11134
2311178e 11135enum
cb617e7c
GM
11136{
11137 CURSOR_MOVEMENT_SUCCESS,
11138 CURSOR_MOVEMENT_CANNOT_BE_USED,
11139 CURSOR_MOVEMENT_MUST_SCROLL,
11140 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11141};
47589c8c
GM
11142
11143static int
11144try_cursor_movement (window, startp, scroll_step)
11145 Lisp_Object window;
11146 struct text_pos startp;
11147 int *scroll_step;
11148{
11149 struct window *w = XWINDOW (window);
11150 struct frame *f = XFRAME (w->frame);
cb617e7c 11151 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
2311178e 11152
69d1f7c9 11153#if GLYPH_DEBUG
76cb5e06
GM
11154 if (inhibit_try_cursor_movement)
11155 return rc;
11156#endif
11157
47589c8c
GM
11158 /* Handle case where text has not changed, only point, and it has
11159 not moved off the frame. */
11160 if (/* Point may be in this window. */
11161 PT >= CHARPOS (startp)
47589c8c
GM
11162 /* Selective display hasn't changed. */
11163 && !current_buffer->clip_changed
4db87380
GM
11164 /* Function force-mode-line-update is used to force a thorough
11165 redisplay. It sets either windows_or_buffers_changed or
11166 update_mode_lines. So don't take a shortcut here for these
11167 cases. */
11168 && !update_mode_lines
11169 && !windows_or_buffers_changed
5fb96e96 11170 && !cursor_type_changed
2311178e 11171 /* Can't use this case if highlighting a region. When a
47589c8c
GM
11172 region exists, cursor movement has to do more than just
11173 set the cursor. */
11174 && !(!NILP (Vtransient_mark_mode)
11175 && !NILP (current_buffer->mark_active))
11176 && NILP (w->region_showing)
11177 && NILP (Vshow_trailing_whitespace)
11178 /* Right after splitting windows, last_point may be nil. */
11179 && INTEGERP (w->last_point)
11180 /* This code is not used for mini-buffer for the sake of the case
11181 of redisplaying to replace an echo area message; since in
11182 that case the mini-buffer contents per se are usually
11183 unchanged. This code is of no real use in the mini-buffer
11184 since the handling of this_line_start_pos, etc., in redisplay
11185 handles the same cases. */
11186 && !EQ (window, minibuf_window)
11187 /* When splitting windows or for new windows, it happens that
11188 redisplay is called with a nil window_end_vpos or one being
11189 larger than the window. This should really be fixed in
11190 window.c. I don't have this on my list, now, so we do
11191 approximately the same as the old redisplay code. --gerd. */
11192 && INTEGERP (w->window_end_vpos)
11193 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11194 && (FRAME_WINDOW_P (f)
11195 || !MARKERP (Voverlay_arrow_position)
11196 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
11197 {
11198 int this_scroll_margin;
8ee5b6a3 11199 struct glyph_row *row = NULL;
47589c8c
GM
11200
11201#if GLYPH_DEBUG
11202 debug_method_add (w, "cursor movement");
11203#endif
11204
11205 /* Scroll if point within this distance from the top or bottom
11206 of the window. This is a pixel value. */
11207 this_scroll_margin = max (0, scroll_margin);
da8b7f4f
KS
11208 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11209 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
47589c8c
GM
11210
11211 /* Start with the row the cursor was displayed during the last
11212 not paused redisplay. Give up if that row is not valid. */
bd9d0f3f
GM
11213 if (w->last_cursor.vpos < 0
11214 || w->last_cursor.vpos >= w->current_matrix->nrows)
cb617e7c 11215 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
11216 else
11217 {
11218 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11219 if (row->mode_line_p)
11220 ++row;
11221 if (!row->enabled_p)
cb617e7c 11222 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
11223 }
11224
cb617e7c 11225 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
47589c8c
GM
11226 {
11227 int scroll_p = 0;
68c5d1db 11228 int last_y = window_text_bottom_y (w) - this_scroll_margin;
2311178e 11229
47589c8c
GM
11230 if (PT > XFASTINT (w->last_point))
11231 {
11232 /* Point has moved forward. */
47589c8c
GM
11233 while (MATRIX_ROW_END_CHARPOS (row) < PT
11234 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11235 {
11236 xassert (row->enabled_p);
11237 ++row;
11238 }
11239
11240 /* The end position of a row equals the start position
11241 of the next row. If PT is there, we would rather
cafafe0b
GM
11242 display it in the next line. */
11243 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11244 && MATRIX_ROW_END_CHARPOS (row) == PT
11245 && !cursor_row_p (w, row))
11246 ++row;
47589c8c
GM
11247
11248 /* If within the scroll margin, scroll. Note that
11249 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11250 the next line would be drawn, and that
11251 this_scroll_margin can be zero. */
11252 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11253 || PT > MATRIX_ROW_END_CHARPOS (row)
11254 /* Line is completely visible last line in window
11255 and PT is to be set in the next line. */
11256 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11257 && PT == MATRIX_ROW_END_CHARPOS (row)
11258 && !row->ends_at_zv_p
11259 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11260 scroll_p = 1;
11261 }
11262 else if (PT < XFASTINT (w->last_point))
11263 {
11264 /* Cursor has to be moved backward. Note that PT >=
11265 CHARPOS (startp) because of the outer
11266 if-statement. */
11267 while (!row->mode_line_p
11268 && (MATRIX_ROW_START_CHARPOS (row) > PT
11269 || (MATRIX_ROW_START_CHARPOS (row) == PT
11270 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11271 && (row->y > this_scroll_margin
11272 || CHARPOS (startp) == BEGV))
11273 {
11274 xassert (row->enabled_p);
11275 --row;
11276 }
11277
11278 /* Consider the following case: Window starts at BEGV,
11279 there is invisible, intangible text at BEGV, so that
11280 display starts at some point START > BEGV. It can
11281 happen that we are called with PT somewhere between
11282 BEGV and START. Try to handle that case. */
11283 if (row < w->current_matrix->rows
11284 || row->mode_line_p)
11285 {
11286 row = w->current_matrix->rows;
11287 if (row->mode_line_p)
11288 ++row;
11289 }
11290
11291 /* Due to newlines in overlay strings, we may have to
11292 skip forward over overlay strings. */
68c5d1db
GM
11293 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11294 && MATRIX_ROW_END_CHARPOS (row) == PT
11295 && !cursor_row_p (w, row))
47589c8c 11296 ++row;
2311178e 11297
47589c8c
GM
11298 /* If within the scroll margin, scroll. */
11299 if (row->y < this_scroll_margin
11300 && CHARPOS (startp) != BEGV)
11301 scroll_p = 1;
11302 }
11303
11304 if (PT < MATRIX_ROW_START_CHARPOS (row)
11305 || PT > MATRIX_ROW_END_CHARPOS (row))
11306 {
11307 /* if PT is not in the glyph row, give up. */
cb617e7c 11308 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c 11309 }
440fc135 11310 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
47589c8c 11311 {
8de4aaf8
GM
11312 if (PT == MATRIX_ROW_END_CHARPOS (row)
11313 && !row->ends_at_zv_p
11314 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
cb617e7c 11315 rc = CURSOR_MOVEMENT_MUST_SCROLL;
3f7e3031 11316 else if (row->height > window_box_height (w))
440fc135 11317 {
8de4aaf8
GM
11318 /* If we end up in a partially visible line, let's
11319 make it fully visible, except when it's taller
11320 than the window, in which case we can't do much
11321 about it. */
440fc135 11322 *scroll_step = 1;
cb617e7c 11323 rc = CURSOR_MOVEMENT_MUST_SCROLL;
440fc135
GM
11324 }
11325 else
11326 {
11327 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
cb617e7c 11328 if (!make_cursor_line_fully_visible (w))
ddf6b9a3 11329 rc = CURSOR_MOVEMENT_MUST_SCROLL;
cb617e7c
GM
11330 else
11331 rc = CURSOR_MOVEMENT_SUCCESS;
440fc135 11332 }
47589c8c
GM
11333 }
11334 else if (scroll_p)
cb617e7c 11335 rc = CURSOR_MOVEMENT_MUST_SCROLL;
47589c8c
GM
11336 else
11337 {
11338 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
cb617e7c 11339 rc = CURSOR_MOVEMENT_SUCCESS;
47589c8c
GM
11340 }
11341 }
11342 }
11343
11344 return rc;
11345}
11346
9a08d928
SM
11347void
11348set_vertical_scroll_bar (w)
11349 struct window *w;
11350{
11351 int start, end, whole;
11352
11353 /* Calculate the start and end positions for the current window.
11354 At some point, it would be nice to choose between scrollbars
11355 which reflect the whole buffer size, with special markers
11356 indicating narrowing, and scrollbars which reflect only the
11357 visible region.
11358
11359 Note that mini-buffers sometimes aren't displaying any text. */
11360 if (!MINI_WINDOW_P (w)
11361 || (w == XWINDOW (minibuf_window)
11362 && NILP (echo_area_buffer[0])))
11363 {
11364 struct buffer *buf = XBUFFER (w->buffer);
11365 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11366 start = marker_position (w->start) - BUF_BEGV (buf);
11367 /* I don't think this is guaranteed to be right. For the
11368 moment, we'll pretend it is. */
11369 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11370
11371 if (end < start)
11372 end = start;
11373 if (whole < (end - start))
11374 whole = end - start;
11375 }
11376 else
11377 start = end = whole = 0;
11378
11379 /* Indicate what this scroll bar ought to be displaying now. */
11380 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11381}
47589c8c 11382
5f5c8ee5 11383/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
b60c9653
RS
11384 selected_window is redisplayed.
11385
11386 We can return without actually redisplaying the window if
11387 fonts_changed_p is nonzero. In that case, redisplay_internal will
11388 retry. */
90adcf20 11389
a2889657 11390static void
5f5c8ee5 11391redisplay_window (window, just_this_one_p)
a2889657 11392 Lisp_Object window;
5f5c8ee5 11393 int just_this_one_p;
a2889657 11394{
5f5c8ee5
GM
11395 struct window *w = XWINDOW (window);
11396 struct frame *f = XFRAME (w->frame);
11397 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 11398 struct buffer *old = current_buffer;
5f5c8ee5 11399 struct text_pos lpoint, opoint, startp;
e481f960 11400 int update_mode_line;
5f5c8ee5
GM
11401 int tem;
11402 struct it it;
11403 /* Record it now because it's overwritten. */
11404 int current_matrix_up_to_date_p = 0;
c62c1bb5
RS
11405 /* This is less strict than current_matrix_up_to_date_p.
11406 It indictes that the buffer contents and narrowing are unchanged. */
11407 int buffer_unchanged_p = 0;
5f5c8ee5 11408 int temp_scroll_step = 0;
331379bf 11409 int count = SPECPDL_INDEX ();
47589c8c 11410 int rc;
ddf6b9a3 11411 int centering_position;
03b0a4b4 11412 int last_line_misfit = 0;
a2889657 11413
5f5c8ee5
GM
11414 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11415 opoint = lpoint;
a2889657 11416
5f5c8ee5
GM
11417 /* W must be a leaf window here. */
11418 xassert (!NILP (w->buffer));
11419#if GLYPH_DEBUG
11420 *w->desired_matrix->method = 0;
11421#endif
2e54982e
RS
11422
11423 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
11424
11425 reconsider_clip_changes (w, buffer);
2311178e
TTN
11426
11427 /* Has the mode line to be updated? */
5f5c8ee5
GM
11428 update_mode_line = (!NILP (w->update_mode_line)
11429 || update_mode_lines
c62c1bb5
RS
11430 || buffer->clip_changed
11431 || buffer->prevent_redisplay_optimizations_p);
8de2d90b
JB
11432
11433 if (MINI_WINDOW_P (w))
11434 {
5f5c8ee5 11435 if (w == XWINDOW (echo_area_window)
c6e89d6c 11436 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
11437 {
11438 if (update_mode_line)
11439 /* We may have to update a tty frame's menu bar or a
e037b9ec 11440 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
11441 goto finish_menu_bars;
11442 else
11443 /* We've already displayed the echo area glyphs in this window. */
11444 goto finish_scroll_bars;
11445 }
b2b5455d
RS
11446 else if ((w != XWINDOW (minibuf_window)
11447 || minibuf_level == 0)
c0bcce6f
JPW
11448 /* When buffer is nonempty, redisplay window normally. */
11449 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
3354fdcf
RS
11450 /* Quail displays non-mini buffers in minibuffer window.
11451 In that case, redisplay the window normally. */
b2b5455d 11452 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
8de2d90b 11453 {
3354fdcf
RS
11454 /* W is a mini-buffer window, but it's not active, so clear
11455 it. */
5f5c8ee5
GM
11456 int yb = window_text_bottom_y (w);
11457 struct glyph_row *row;
11458 int y;
11459
11460 for (y = 0, row = w->desired_matrix->rows;
11461 y < yb;
11462 y += row->height, ++row)
11463 blank_row (w, row, y);
88f22aff 11464 goto finish_scroll_bars;
8de2d90b 11465 }
c095a1dd
GM
11466
11467 clear_glyph_matrix (w->desired_matrix);
8de2d90b 11468 }
a2889657 11469
5f5c8ee5
GM
11470 /* Otherwise set up data on this window; select its buffer and point
11471 value. */
6a93695f
GM
11472 /* Really select the buffer, for the sake of buffer-local
11473 variables. */
11474 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5
GM
11475 SET_TEXT_POS (opoint, PT, PT_BYTE);
11476
11477 current_matrix_up_to_date_p
11478 = (!NILP (w->window_end_valid)
11479 && !current_buffer->clip_changed
c62c1bb5 11480 && !current_buffer->prevent_redisplay_optimizations_p
5f5c8ee5
GM
11481 && XFASTINT (w->last_modified) >= MODIFF
11482 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 11483
c62c1bb5
RS
11484 buffer_unchanged_p
11485 = (!NILP (w->window_end_valid)
11486 && !current_buffer->clip_changed
3f1258d0 11487 && XFASTINT (w->last_modified) >= MODIFF
c62c1bb5
RS
11488 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11489
5f5c8ee5
GM
11490 /* When windows_or_buffers_changed is non-zero, we can't rely on
11491 the window end being valid, so set it to nil there. */
11492 if (windows_or_buffers_changed)
11493 {
11494 /* If window starts on a continuation line, maybe adjust the
11495 window start in case the window's width changed. */
11496 if (XMARKER (w->start)->buffer == current_buffer)
11497 compute_window_start_on_continuation_line (w);
2311178e 11498
5f5c8ee5
GM
11499 w->window_end_valid = Qnil;
11500 }
12adba34 11501
5f5c8ee5
GM
11502 /* Some sanity checks. */
11503 CHECK_WINDOW_END (w);
11504 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 11505 abort ();
5f5c8ee5 11506 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 11507 abort ();
a2889657 11508
28995e67
RS
11509 /* If %c is in mode line, update it if needed. */
11510 if (!NILP (w->column_number_displayed)
11511 /* This alternative quickly identifies a common case
11512 where no change is needed. */
11513 && !(PT == XFASTINT (w->last_point)
8850a573
RS
11514 && XFASTINT (w->last_modified) >= MODIFF
11515 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
2311178e
TTN
11516 && (XFASTINT (w->column_number_displayed)
11517 != (int) current_column ())) /* iftc */
11518 update_mode_line = 1;
28995e67 11519
5f5c8ee5
GM
11520 /* Count number of windows showing the selected buffer. An indirect
11521 buffer counts as its base buffer. */
11522 if (!just_this_one_p)
42640f83
RS
11523 {
11524 struct buffer *current_base, *window_base;
11525 current_base = current_buffer;
11526 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11527 if (current_base->base_buffer)
11528 current_base = current_base->base_buffer;
11529 if (window_base->base_buffer)
11530 window_base = window_base->base_buffer;
11531 if (current_base == window_base)
11532 buffer_shared++;
11533 }
a2889657 11534
5f5c8ee5
GM
11535 /* Point refers normally to the selected window. For any other
11536 window, set up appropriate value. */
a2889657
JB
11537 if (!EQ (window, selected_window))
11538 {
12adba34
RS
11539 int new_pt = XMARKER (w->pointm)->charpos;
11540 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 11541 if (new_pt < BEGV)
a2889657 11542 {
f67a0f51 11543 new_pt = BEGV;
12adba34
RS
11544 new_pt_byte = BEGV_BYTE;
11545 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 11546 }
f67a0f51 11547 else if (new_pt > (ZV - 1))
a2889657 11548 {
f67a0f51 11549 new_pt = ZV;
12adba34
RS
11550 new_pt_byte = ZV_BYTE;
11551 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 11552 }
2311178e 11553
f67a0f51 11554 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 11555 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
11556 }
11557
f4faa47c 11558 /* If any of the character widths specified in the display table
5f5c8ee5
GM
11559 have changed, invalidate the width run cache. It's true that
11560 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
11561 redisplay goes (non-fatally) haywire when the display table is
11562 changed, so why should we worry about doing any better? */
11563 if (current_buffer->width_run_cache)
11564 {
f908610f 11565 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
11566
11567 if (! disptab_matches_widthtab (disptab,
11568 XVECTOR (current_buffer->width_table)))
11569 {
11570 invalidate_region_cache (current_buffer,
11571 current_buffer->width_run_cache,
11572 BEG, Z);
11573 recompute_width_table (current_buffer, disptab);
11574 }
11575 }
11576
a2889657 11577 /* If window-start is screwed up, choose a new one. */
a2889657
JB
11578 if (XMARKER (w->start)->buffer != current_buffer)
11579 goto recenter;
11580
5f5c8ee5 11581 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 11582
cf0df6ab
RS
11583 /* If someone specified a new starting point but did not insist,
11584 check whether it can be used. */
cfad01b4
GM
11585 if (!NILP (w->optional_new_start)
11586 && CHARPOS (startp) >= BEGV
11587 && CHARPOS (startp) <= ZV)
cf0df6ab
RS
11588 {
11589 w->optional_new_start = Qnil;
5f5c8ee5
GM
11590 start_display (&it, w, startp);
11591 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11592 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11593 if (IT_CHARPOS (it) == PT)
cf0df6ab 11594 w->force_start = Qt;
29b3ea63
KS
11595 /* IT may overshoot PT if text at PT is invisible. */
11596 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11597 w->force_start = Qt;
11598
11599
cf0df6ab
RS
11600 }
11601
8de2d90b 11602 /* Handle case where place to start displaying has been specified,
aa6d10fa 11603 unless the specified location is outside the accessible range. */
9472f927
GM
11604 if (!NILP (w->force_start)
11605 || w->frozen_window_start_p)
a2889657 11606 {
b60c9653
RS
11607 /* We set this later on if we have to adjust point. */
11608 int new_vpos = -1;
11609
e63574d7 11610 w->force_start = Qnil;
5f5c8ee5 11611 w->vscroll = 0;
b5174a51 11612 w->window_end_valid = Qnil;
5f5c8ee5
GM
11613
11614 /* Forget any recorded base line for line number display. */
c62c1bb5 11615 if (!buffer_unchanged_p)
5f5c8ee5
GM
11616 w->base_line_number = Qnil;
11617
75c43375
RS
11618 /* Redisplay the mode line. Select the buffer properly for that.
11619 Also, run the hook window-scroll-functions
11620 because we have scrolled. */
e63574d7
RS
11621 /* Note, we do this after clearing force_start because
11622 if there's an error, it is better to forget about force_start
11623 than to get into an infinite loop calling the hook functions
11624 and having them get more errors. */
75c43375
RS
11625 if (!update_mode_line
11626 || ! NILP (Vwindow_scroll_functions))
e481f960 11627 {
e481f960
RS
11628 update_mode_line = 1;
11629 w->update_mode_line = Qt;
5f5c8ee5 11630 startp = run_window_scroll_functions (window, startp);
e481f960 11631 }
2311178e 11632
ac90c44f
GM
11633 w->last_modified = make_number (0);
11634 w->last_overlay_modified = make_number (0);
5f5c8ee5
GM
11635 if (CHARPOS (startp) < BEGV)
11636 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11637 else if (CHARPOS (startp) > ZV)
11638 SET_TEXT_POS (startp, ZV, ZV_BYTE);
2311178e
TTN
11639
11640 /* Redisplay, then check if cursor has been set during the
5f5c8ee5
GM
11641 redisplay. Give up if new fonts were loaded. */
11642 if (!try_window (window, startp))
11643 {
11644 w->force_start = Qt;
11645 clear_glyph_matrix (w->desired_matrix);
b60c9653 11646 goto need_larger_matrices;
5f5c8ee5
GM
11647 }
11648
9472f927 11649 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5 11650 {
b28cb6ed
GM
11651 /* If point does not appear, try to move point so it does
11652 appear. The desired matrix has been built above, so we
11653 can use it here. */
b60c9653
RS
11654 new_vpos = window_box_height (w) / 2;
11655 }
11656
11657 if (!make_cursor_line_fully_visible (w))
11658 {
11659 /* Point does appear, but on a line partly visible at end of window.
11660 Move it back to a fully-visible line. */
11661 new_vpos = window_box_height (w);
11662 }
11663
11664 /* If we need to move point for either of the above reasons,
11665 now actually do it. */
11666 if (new_vpos >= 0)
11667 {
b28cb6ed
GM
11668 struct glyph_row *row;
11669
b28cb6ed 11670 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
b60c9653 11671 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
5f5c8ee5
GM
11672 ++row;
11673
11674 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11675 MATRIX_ROW_START_BYTEPOS (row));
11676
90adcf20 11677 if (w != XWINDOW (selected_window))
12adba34 11678 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
11679 else if (current_buffer == old)
11680 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11681
11682 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
2311178e 11683
5f5c8ee5
GM
11684 /* If we are highlighting the region, then we just changed
11685 the region, so redisplay to show it. */
df0b5ea1
RS
11686 if (!NILP (Vtransient_mark_mode)
11687 && !NILP (current_buffer->mark_active))
6f27fa9b 11688 {
5f5c8ee5
GM
11689 clear_glyph_matrix (w->desired_matrix);
11690 if (!try_window (window, startp))
cb617e7c 11691 goto need_larger_matrices;
6f27fa9b 11692 }
a2889657 11693 }
5f5c8ee5 11694
5f5c8ee5
GM
11695#if GLYPH_DEBUG
11696 debug_method_add (w, "forced window start");
11697#endif
a2889657
JB
11698 goto done;
11699 }
11700
5f5c8ee5 11701 /* Handle case where text has not changed, only point, and it has
3f029ea0
RS
11702 not moved off the frame, and we are not retrying after hscroll.
11703 (current_matrix_up_to_date_p is nonzero when retrying.) */
11704 if (current_matrix_up_to_date_p
47589c8c 11705 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
cb617e7c 11706 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
a2889657 11707 {
cb617e7c
GM
11708 switch (rc)
11709 {
11710 case CURSOR_MOVEMENT_SUCCESS:
11711 goto done;
2311178e 11712
b60c9653 11713#if 0 /* try_cursor_movement never returns this value. */
cb617e7c
GM
11714 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11715 goto need_larger_matrices;
b60c9653 11716#endif
2311178e 11717
cb617e7c
GM
11718 case CURSOR_MOVEMENT_MUST_SCROLL:
11719 goto try_to_scroll;
2311178e 11720
cb617e7c
GM
11721 default:
11722 abort ();
11723 }
a2889657
JB
11724 }
11725 /* If current starting point was originally the beginning of a line
11726 but no longer is, find a new starting point. */
265a9e55 11727 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
11728 && !(CHARPOS (startp) <= BEGV
11729 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 11730 {
5f5c8ee5
GM
11731#if GLYPH_DEBUG
11732 debug_method_add (w, "recenter 1");
11733#endif
a2889657
JB
11734 goto recenter;
11735 }
2311178e 11736
27d16f05
GM
11737 /* Try scrolling with try_window_id. Value is > 0 if update has
11738 been done, it is -1 if we know that the same window start will
11739 not work. It is 0 if unsuccessful for some other reason. */
11740 else if ((tem = try_window_id (w)) != 0)
a2889657 11741 {
5f5c8ee5 11742#if GLYPH_DEBUG
ef121659 11743 debug_method_add (w, "try_window_id %d", tem);
5f5c8ee5
GM
11744#endif
11745
11746 if (fonts_changed_p)
cb617e7c 11747 goto need_larger_matrices;
a2889657
JB
11748 if (tem > 0)
11749 goto done;
ef121659 11750
5f5c8ee5
GM
11751 /* Otherwise try_window_id has returned -1 which means that we
11752 don't want the alternative below this comment to execute. */
a2889657 11753 }
5f5c8ee5
GM
11754 else if (CHARPOS (startp) >= BEGV
11755 && CHARPOS (startp) <= ZV
11756 && PT >= CHARPOS (startp)
11757 && (CHARPOS (startp) < ZV
e9874cee 11758 /* Avoid starting at end of buffer. */
5f5c8ee5 11759 || CHARPOS (startp) == BEGV
8850a573
RS
11760 || (XFASTINT (w->last_modified) >= MODIFF
11761 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 11762 {
5f5c8ee5
GM
11763#if GLYPH_DEBUG
11764 debug_method_add (w, "same window start");
11765#endif
2311178e 11766
5f5c8ee5
GM
11767 /* Try to redisplay starting at same place as before.
11768 If point has not moved off frame, accept the results. */
11769 if (!current_matrix_up_to_date_p
11770 /* Don't use try_window_reusing_current_matrix in this case
15e26c76
GM
11771 because a window scroll function can have changed the
11772 buffer. */
5f5c8ee5
GM
11773 || !NILP (Vwindow_scroll_functions)
11774 || MINI_WINDOW_P (w)
11775 || !try_window_reusing_current_matrix (w))
11776 {
11777 IF_DEBUG (debug_method_add (w, "1"));
11778 try_window (window, startp);
11779 }
11780
11781 if (fonts_changed_p)
cb617e7c 11782 goto need_larger_matrices;
2311178e 11783
5f5c8ee5 11784 if (w->cursor.vpos >= 0)
aa6d10fa 11785 {
2311178e 11786 if (!just_this_one_p
5f5c8ee5 11787 || current_buffer->clip_changed
9142dd5b 11788 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
11789 /* Forget any recorded base line for line number display. */
11790 w->base_line_number = Qnil;
2311178e 11791
cb617e7c 11792 if (!make_cursor_line_fully_visible (w))
03b0a4b4
RS
11793 {
11794 clear_glyph_matrix (w->desired_matrix);
11795 last_line_misfit = 1;
11796 }
ddf6b9a3 11797 /* Drop through and scroll. */
ef3c2c73
RS
11798 else
11799 goto done;
aa6d10fa 11800 }
a2889657 11801 else
5f5c8ee5 11802 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
11803 }
11804
5f5c8ee5
GM
11805 try_to_scroll:
11806
ac90c44f
GM
11807 w->last_modified = make_number (0);
11808 w->last_overlay_modified = make_number (0);
5f5c8ee5 11809
e481f960
RS
11810 /* Redisplay the mode line. Select the buffer properly for that. */
11811 if (!update_mode_line)
11812 {
e481f960
RS
11813 update_mode_line = 1;
11814 w->update_mode_line = Qt;
11815 }
a2889657 11816
5f5c8ee5
GM
11817 /* Try to scroll by specified few lines. */
11818 if ((scroll_conservatively
11819 || scroll_step
11820 || temp_scroll_step
11821 || NUMBERP (current_buffer->scroll_up_aggressively)
11822 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 11823 && !current_buffer->clip_changed
2311178e 11824 && CHARPOS (startp) >= BEGV
5f5c8ee5 11825 && CHARPOS (startp) <= ZV)
0789adb2 11826 {
5f5c8ee5
GM
11827 /* The function returns -1 if new fonts were loaded, 1 if
11828 successful, 0 if not successful. */
11829 int rc = try_scrolling (window, just_this_one_p,
11830 scroll_conservatively,
11831 scroll_step,
03b0a4b4 11832 temp_scroll_step, last_line_misfit);
cb617e7c
GM
11833 switch (rc)
11834 {
11835 case SCROLLING_SUCCESS:
11836 goto done;
2311178e 11837
cb617e7c
GM
11838 case SCROLLING_NEED_LARGER_MATRICES:
11839 goto need_larger_matrices;
2311178e 11840
cb617e7c
GM
11841 case SCROLLING_FAILED:
11842 break;
2311178e 11843
cb617e7c
GM
11844 default:
11845 abort ();
11846 }
5f5c8ee5 11847 }
f9c8af06 11848
5f5c8ee5 11849 /* Finally, just choose place to start which centers point */
5936754e 11850
5f5c8ee5 11851 recenter:
ddf6b9a3
RS
11852 centering_position = window_box_height (w) / 2;
11853
11854 point_at_top:
11855 /* Jump here with centering_position already set to 0. */
44173109 11856
5f5c8ee5
GM
11857#if GLYPH_DEBUG
11858 debug_method_add (w, "recenter");
11859#endif
0789adb2 11860
5f5c8ee5 11861 /* w->vscroll = 0; */
0789adb2 11862
5f5c8ee5 11863 /* Forget any previously recorded base line for line number display. */
c62c1bb5 11864 if (!buffer_unchanged_p)
5f5c8ee5
GM
11865 w->base_line_number = Qnil;
11866
11867 /* Move backward half the height of the window. */
11868 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11869 it.current_y = it.last_visible_y;
ddf6b9a3 11870 move_it_vertically_backward (&it, centering_position);
5f5c8ee5
GM
11871 xassert (IT_CHARPOS (it) >= BEGV);
11872
11873 /* The function move_it_vertically_backward may move over more
11874 than the specified y-distance. If it->w is small, e.g. a
11875 mini-buffer window, we may end up in front of the window's
11876 display area. Start displaying at the start of the line
11877 containing PT in this case. */
11878 if (it.current_y <= 0)
11879 {
11880 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11881 move_it_vertically (&it, 0);
11882 xassert (IT_CHARPOS (it) <= PT);
11883 it.current_y = 0;
0789adb2
RS
11884 }
11885
5f5c8ee5 11886 it.current_x = it.hpos = 0;
2311178e 11887
5f5c8ee5
GM
11888 /* Set startp here explicitly in case that helps avoid an infinite loop
11889 in case the window-scroll-functions functions get errors. */
11890 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11891
11892 /* Run scroll hooks. */
11893 startp = run_window_scroll_functions (window, it.current.pos);
11894
11895 /* Redisplay the window. */
11896 if (!current_matrix_up_to_date_p
11897 || windows_or_buffers_changed
5fb96e96 11898 || cursor_type_changed
5f5c8ee5
GM
11899 /* Don't use try_window_reusing_current_matrix in this case
11900 because it can have changed the buffer. */
11901 || !NILP (Vwindow_scroll_functions)
11902 || !just_this_one_p
11903 || MINI_WINDOW_P (w)
11904 || !try_window_reusing_current_matrix (w))
11905 try_window (window, startp);
11906
11907 /* If new fonts have been loaded (due to fontsets), give up. We
11908 have to start a new redisplay since we need to re-adjust glyph
11909 matrices. */
11910 if (fonts_changed_p)
cb617e7c 11911 goto need_larger_matrices;
5f5c8ee5
GM
11912
11913 /* If cursor did not appear assume that the middle of the window is
11914 in the first line of the window. Do it again with the next line.
11915 (Imagine a window of height 100, displaying two lines of height
11916 60. Moving back 50 from it->last_visible_y will end in the first
11917 line.) */
11918 if (w->cursor.vpos < 0)
a2889657 11919 {
5f5c8ee5
GM
11920 if (!NILP (w->window_end_valid)
11921 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 11922 {
5f5c8ee5
GM
11923 clear_glyph_matrix (w->desired_matrix);
11924 move_it_by_lines (&it, 1, 0);
11925 try_window (window, it.current.pos);
a2889657 11926 }
5f5c8ee5 11927 else if (PT < IT_CHARPOS (it))
a2889657 11928 {
5f5c8ee5
GM
11929 clear_glyph_matrix (w->desired_matrix);
11930 move_it_by_lines (&it, -1, 0);
11931 try_window (window, it.current.pos);
11932 }
11933 else
11934 {
11935 /* Not much we can do about it. */
a2889657 11936 }
a2889657 11937 }
010494d0 11938
5f5c8ee5
GM
11939 /* Consider the following case: Window starts at BEGV, there is
11940 invisible, intangible text at BEGV, so that display starts at
11941 some point START > BEGV. It can happen that we are called with
11942 PT somewhere between BEGV and START. Try to handle that case. */
11943 if (w->cursor.vpos < 0)
835766b6 11944 {
5f5c8ee5
GM
11945 struct glyph_row *row = w->current_matrix->rows;
11946 if (row->mode_line_p)
11947 ++row;
11948 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 11949 }
2311178e 11950
cb617e7c 11951 if (!make_cursor_line_fully_visible (w))
ddf6b9a3 11952 {
932357b4
KS
11953 /* If vscroll is enabled, disable it and try again. */
11954 if (w->vscroll)
11955 {
11956 w->vscroll = 0;
11957 clear_glyph_matrix (w->desired_matrix);
11958 goto recenter;
11959 }
2a6d0874 11960
ddf6b9a3
RS
11961 /* If centering point failed to make the whole line visible,
11962 put point at the top instead. That has to make the whole line
11963 visible, if it can be done. */
11964 centering_position = 0;
11965 goto point_at_top;
11966 }
b5174a51 11967
74d481ac
GM
11968 done:
11969
5f5c8ee5
GM
11970 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11971 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11972 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11973 ? Qt : Qnil);
a2889657 11974
5f5c8ee5 11975 /* Display the mode line, if we must. */
e481f960 11976 if ((update_mode_line
aa6d10fa 11977 /* If window not full width, must redo its mode line
2311178e 11978 if (a) the window to its side is being redone and
5f5c8ee5
GM
11979 (b) we do a frame-based redisplay. This is a consequence
11980 of how inverted lines are drawn in frame-based redisplay. */
2311178e 11981 || (!just_this_one_p
5f5c8ee5
GM
11982 && !FRAME_WINDOW_P (f)
11983 && !WINDOW_FULL_WIDTH_P (w))
11984 /* Line number to display. */
155ef550 11985 || INTEGERP (w->base_line_pos)
5f5c8ee5 11986 /* Column number is displayed and different from the one displayed. */
155ef550 11987 || (!NILP (w->column_number_displayed)
2311178e
TTN
11988 && (XFASTINT (w->column_number_displayed)
11989 != (int) current_column ()))) /* iftc */
5f5c8ee5
GM
11990 /* This means that the window has a mode line. */
11991 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 11992 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 11993 {
5f5c8ee5
GM
11994 display_mode_lines (w);
11995
11996 /* If mode line height has changed, arrange for a thorough
11997 immediate redisplay using the correct mode line height. */
11998 if (WINDOW_WANTS_MODELINE_P (w)
11999 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 12000 {
5f5c8ee5
GM
12001 fonts_changed_p = 1;
12002 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12003 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 12004 }
2311178e 12005
5f5c8ee5
GM
12006 /* If top line height has changed, arrange for a thorough
12007 immediate redisplay using the correct mode line height. */
045dee35
GM
12008 if (WINDOW_WANTS_HEADER_LINE_P (w)
12009 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
12010 {
12011 fonts_changed_p = 1;
045dee35
GM
12012 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12013 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
12014 }
12015
12016 if (fonts_changed_p)
cb617e7c 12017 goto need_larger_matrices;
5ba50c51 12018 }
5f5c8ee5
GM
12019
12020 if (!line_number_displayed
12021 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
12022 {
12023 w->base_line_pos = Qnil;
12024 w->base_line_number = Qnil;
12025 }
a2889657 12026
5f5c8ee5 12027 finish_menu_bars:
2311178e 12028
7ce2c095 12029 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 12030 if (update_mode_line
5f5c8ee5
GM
12031 && EQ (FRAME_SELECTED_WINDOW (f), window))
12032 {
12033 int redisplay_menu_p = 0;
488dd4c4 12034 int redisplay_tool_bar_p = 0;
5f5c8ee5
GM
12035
12036 if (FRAME_WINDOW_P (f))
12037 {
488dd4c4
JD
12038#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12039 || defined (USE_GTK)
5f5c8ee5 12040 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 12041#else
5f5c8ee5 12042 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 12043#endif
5f5c8ee5
GM
12044 }
12045 else
12046 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12047
12048 if (redisplay_menu_p)
12049 display_menu_bar (w);
12050
12051#ifdef HAVE_WINDOW_SYSTEM
488dd4c4
JD
12052#ifdef USE_GTK
12053 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12054#else
12055 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12056 && (FRAME_TOOL_BAR_LINES (f) > 0
12057 || auto_resize_tool_bars_p);
177c0ea7 12058
488dd4c4
JD
12059#endif
12060
12061 if (redisplay_tool_bar_p)
12062 redisplay_tool_bar (f);
5f5c8ee5
GM
12063#endif
12064 }
7ce2c095 12065
b60c9653
RS
12066 /* We go to this label, with fonts_changed_p nonzero,
12067 if it is necessary to try again using larger glyph matrices.
12068 We have to redeem the scroll bar even in this case,
12069 because the loop in redisplay_internal expects that. */
cb617e7c
GM
12070 need_larger_matrices:
12071 ;
88f22aff 12072 finish_scroll_bars:
5f5c8ee5 12073
da8b7f4f 12074 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
30c566e4 12075 {
9a08d928
SM
12076 /* Set the thumb's position and size. */
12077 set_vertical_scroll_bar (w);
30c566e4 12078
5f5c8ee5
GM
12079 /* Note that we actually used the scroll bar attached to this
12080 window, so it shouldn't be deleted at the end of redisplay. */
504454e8 12081 redeem_scroll_bar_hook (w);
30c566e4 12082 }
b1d1124b 12083
5f5c8ee5
GM
12084 /* Restore current_buffer and value of point in it. */
12085 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
6a93695f 12086 set_buffer_internal_1 (old);
5f5c8ee5 12087 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
12088
12089 unbind_to (count, Qnil);
a2889657 12090}
a2889657 12091
5f5c8ee5
GM
12092
12093/* Build the complete desired matrix of WINDOW with a window start
12094 buffer position POS. Value is non-zero if successful. It is zero
12095 if fonts were loaded during redisplay which makes re-adjusting
12096 glyph matrices necessary. */
12097
12098int
a2889657
JB
12099try_window (window, pos)
12100 Lisp_Object window;
5f5c8ee5
GM
12101 struct text_pos pos;
12102{
12103 struct window *w = XWINDOW (window);
12104 struct it it;
12105 struct glyph_row *last_text_row = NULL;
9cbab4ff 12106
5f5c8ee5
GM
12107 /* Make POS the new window start. */
12108 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 12109
5f5c8ee5
GM
12110 /* Mark cursor position as unknown. No overlay arrow seen. */
12111 w->cursor.vpos = -1;
a2889657 12112 overlay_arrow_seen = 0;
642eefc6 12113
5f5c8ee5
GM
12114 /* Initialize iterator and info to start at POS. */
12115 start_display (&it, w, pos);
a2889657 12116
5f5c8ee5
GM
12117 /* Display all lines of W. */
12118 while (it.current_y < it.last_visible_y)
12119 {
12120 if (display_line (&it))
12121 last_text_row = it.glyph_row - 1;
12122 if (fonts_changed_p)
12123 return 0;
12124 }
a2889657 12125
5f5c8ee5
GM
12126 /* If bottom moved off end of frame, change mode line percentage. */
12127 if (XFASTINT (w->window_end_pos) <= 0
12128 && Z != IT_CHARPOS (it))
a2889657
JB
12129 w->update_mode_line = Qt;
12130
5f5c8ee5
GM
12131 /* Set window_end_pos to the offset of the last character displayed
12132 on the window from the end of current_buffer. Set
12133 window_end_vpos to its row number. */
12134 if (last_text_row)
12135 {
12136 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12137 w->window_end_bytepos
12138 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12139 w->window_end_pos
12140 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12141 w->window_end_vpos
12142 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
12143 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12144 ->displays_text_p);
12145 }
12146 else
12147 {
86bf3faa
RS
12148 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12149 w->window_end_pos = make_number (Z - ZV);
12150 w->window_end_vpos = make_number (0);
5f5c8ee5 12151 }
2311178e 12152
a2889657
JB
12153 /* But that is not valid info until redisplay finishes. */
12154 w->window_end_valid = Qnil;
5f5c8ee5 12155 return 1;
a2889657 12156}
5f5c8ee5
GM
12157
12158
a2889657 12159\f
5f5c8ee5
GM
12160/************************************************************************
12161 Window redisplay reusing current matrix when buffer has not changed
12162 ************************************************************************/
12163
12164/* Try redisplay of window W showing an unchanged buffer with a
12165 different window start than the last time it was displayed by
12166 reusing its current matrix. Value is non-zero if successful.
12167 W->start is the new window start. */
a2889657
JB
12168
12169static int
5f5c8ee5
GM
12170try_window_reusing_current_matrix (w)
12171 struct window *w;
a2889657 12172{
5f5c8ee5
GM
12173 struct frame *f = XFRAME (w->frame);
12174 struct glyph_row *row, *bottom_row;
12175 struct it it;
12176 struct run run;
12177 struct text_pos start, new_start;
12178 int nrows_scrolled, i;
12179 struct glyph_row *last_text_row;
12180 struct glyph_row *last_reused_text_row;
12181 struct glyph_row *start_row;
12182 int start_vpos, min_y, max_y;
75c5350a 12183
69d1f7c9 12184#if GLYPH_DEBUG
76cb5e06
GM
12185 if (inhibit_try_window_reusing)
12186 return 0;
12187#endif
12188
d18354b6
GM
12189 if (/* This function doesn't handle terminal frames. */
12190 !FRAME_WINDOW_P (f)
12191 /* Don't try to reuse the display if windows have been split
12192 or such. */
5fb96e96
RS
12193 || windows_or_buffers_changed
12194 || cursor_type_changed)
5f5c8ee5 12195 return 0;
a2889657 12196
5f5c8ee5
GM
12197 /* Can't do this if region may have changed. */
12198 if ((!NILP (Vtransient_mark_mode)
12199 && !NILP (current_buffer->mark_active))
8f897821
GM
12200 || !NILP (w->region_showing)
12201 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 12202 return 0;
a2889657 12203
5f5c8ee5 12204 /* If top-line visibility has changed, give up. */
045dee35
GM
12205 if (WINDOW_WANTS_HEADER_LINE_P (w)
12206 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
12207 return 0;
12208
12209 /* Give up if old or new display is scrolled vertically. We could
12210 make this function handle this, but right now it doesn't. */
12211 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12212 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12213 return 0;
12214
12215 /* The variable new_start now holds the new window start. The old
12216 start `start' can be determined from the current matrix. */
12217 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12218 start = start_row->start.pos;
12219 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 12220
5f5c8ee5
GM
12221 /* Clear the desired matrix for the display below. */
12222 clear_glyph_matrix (w->desired_matrix);
2311178e 12223
5f5c8ee5
GM
12224 if (CHARPOS (new_start) <= CHARPOS (start))
12225 {
12226 int first_row_y;
2311178e 12227
607ec83c
GM
12228 /* Don't use this method if the display starts with an ellipsis
12229 displayed for invisible text. It's not easy to handle that case
12230 below, and it's certainly not worth the effort since this is
12231 not a frequent case. */
12232 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12233 return 0;
12234
5f5c8ee5 12235 IF_DEBUG (debug_method_add (w, "twu1"));
2311178e 12236
5f5c8ee5
GM
12237 /* Display up to a row that can be reused. The variable
12238 last_text_row is set to the last row displayed that displays
b48f74cb
GM
12239 text. Note that it.vpos == 0 if or if not there is a
12240 header-line; it's not the same as the MATRIX_ROW_VPOS! */
5f5c8ee5
GM
12241 start_display (&it, w, new_start);
12242 first_row_y = it.current_y;
12243 w->cursor.vpos = -1;
12244 last_text_row = last_reused_text_row = NULL;
2311178e 12245
5f5c8ee5
GM
12246 while (it.current_y < it.last_visible_y
12247 && IT_CHARPOS (it) < CHARPOS (start)
12248 && !fonts_changed_p)
12249 if (display_line (&it))
12250 last_text_row = it.glyph_row - 1;
12251
12252 /* A value of current_y < last_visible_y means that we stopped
12253 at the previous window start, which in turn means that we
12254 have at least one reusable row. */
12255 if (it.current_y < it.last_visible_y)
a2889657 12256 {
b48f74cb 12257 /* IT.vpos always starts from 0; it counts text lines. */
5f5c8ee5 12258 nrows_scrolled = it.vpos;
2311178e 12259
5f5c8ee5
GM
12260 /* Find PT if not already found in the lines displayed. */
12261 if (w->cursor.vpos < 0)
a2889657 12262 {
5f5c8ee5 12263 int dy = it.current_y - first_row_y;
2311178e 12264
5f5c8ee5 12265 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
0df56f4d
GM
12266 row = row_containing_pos (w, PT, row, NULL, dy);
12267 if (row)
12268 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12269 dy, nrows_scrolled);
12270 else
5f5c8ee5
GM
12271 {
12272 clear_glyph_matrix (w->desired_matrix);
12273 return 0;
12274 }
a2889657 12275 }
5f5c8ee5
GM
12276
12277 /* Scroll the display. Do it before the current matrix is
12278 changed. The problem here is that update has not yet
12279 run, i.e. part of the current matrix is not up to date.
12280 scroll_run_hook will clear the cursor, and use the
12281 current matrix to get the height of the row the cursor is
12282 in. */
12283 run.current_y = first_row_y;
12284 run.desired_y = it.current_y;
12285 run.height = it.last_visible_y - it.current_y;
b48f74cb
GM
12286
12287 if (run.height > 0 && run.current_y != run.desired_y)
a2889657 12288 {
5f5c8ee5
GM
12289 update_begin (f);
12290 rif->update_window_begin_hook (w);
fa3c6b4d 12291 rif->clear_window_mouse_face (w);
5f5c8ee5 12292 rif->scroll_run_hook (w, &run);
64d1e7d3 12293 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5 12294 update_end (f);
a2889657 12295 }
5f5c8ee5
GM
12296
12297 /* Shift current matrix down by nrows_scrolled lines. */
12298 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12299 rotate_matrix (w->current_matrix,
12300 start_vpos,
12301 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12302 nrows_scrolled);
2311178e 12303
9c8b8382 12304 /* Disable lines that must be updated. */
5f5c8ee5 12305 for (i = 0; i < it.vpos; ++i)
b48f74cb 12306 (start_row + i)->enabled_p = 0;
9c8b8382 12307
5f5c8ee5 12308 /* Re-compute Y positions. */
da8b7f4f 12309 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
5f5c8ee5 12310 max_y = it.last_visible_y;
b48f74cb
GM
12311 for (row = start_row + nrows_scrolled;
12312 row < bottom_row;
12313 ++row)
d2f84654 12314 {
5f5c8ee5 12315 row->y = it.current_y;
75c5350a 12316 row->visible_height = row->height;
5f5c8ee5
GM
12317
12318 if (row->y < min_y)
75c5350a
GM
12319 row->visible_height -= min_y - row->y;
12320 if (row->y + row->height > max_y)
12321 row->visible_height -= row->y + row->height - max_y;
2311178e 12322
5f5c8ee5 12323 it.current_y += row->height;
5f5c8ee5
GM
12324
12325 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12326 last_reused_text_row = row;
12327 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12328 break;
d2f84654 12329 }
2311178e 12330
9c8b8382
GM
12331 /* Disable lines in the current matrix which are now
12332 below the window. */
bafb434c 12333 for (++row; row < bottom_row; ++row)
9c8b8382 12334 row->enabled_p = 0;
a2889657 12335 }
5f5c8ee5
GM
12336
12337 /* Update window_end_pos etc.; last_reused_text_row is the last
12338 reused row from the current matrix containing text, if any.
12339 The value of last_text_row is the last displayed line
12340 containing text. */
12341 if (last_reused_text_row)
a2889657 12342 {
5f5c8ee5
GM
12343 w->window_end_bytepos
12344 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
ac90c44f
GM
12345 w->window_end_pos
12346 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12347 w->window_end_vpos
12348 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12349 w->current_matrix));
a2889657 12350 }
5f5c8ee5
GM
12351 else if (last_text_row)
12352 {
12353 w->window_end_bytepos
12354 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12355 w->window_end_pos
12356 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12357 w->window_end_vpos
12358 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
12359 }
12360 else
12361 {
12362 /* This window must be completely empty. */
86bf3faa
RS
12363 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12364 w->window_end_pos = make_number (Z - ZV);
12365 w->window_end_vpos = make_number (0);
5f5c8ee5
GM
12366 }
12367 w->window_end_valid = Qnil;
a2889657 12368
5f5c8ee5
GM
12369 /* Update hint: don't try scrolling again in update_window. */
12370 w->desired_matrix->no_scrolling_p = 1;
2311178e 12371
5f5c8ee5
GM
12372#if GLYPH_DEBUG
12373 debug_method_add (w, "try_window_reusing_current_matrix 1");
12374#endif
12375 return 1;
a2889657 12376 }
5f5c8ee5
GM
12377 else if (CHARPOS (new_start) > CHARPOS (start))
12378 {
12379 struct glyph_row *pt_row, *row;
12380 struct glyph_row *first_reusable_row;
12381 struct glyph_row *first_row_to_display;
12382 int dy;
12383 int yb = window_text_bottom_y (w);
12384
5f5c8ee5
GM
12385 /* Find the row starting at new_start, if there is one. Don't
12386 reuse a partially visible line at the end. */
b48f74cb 12387 first_reusable_row = start_row;
5f5c8ee5
GM
12388 while (first_reusable_row->enabled_p
12389 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12390 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12391 < CHARPOS (new_start)))
12392 ++first_reusable_row;
12393
12394 /* Give up if there is no row to reuse. */
12395 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
12396 || !first_reusable_row->enabled_p
12397 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12398 != CHARPOS (new_start)))
5f5c8ee5
GM
12399 return 0;
12400
5f5c8ee5
GM
12401 /* We can reuse fully visible rows beginning with
12402 first_reusable_row to the end of the window. Set
12403 first_row_to_display to the first row that cannot be reused.
12404 Set pt_row to the row containing point, if there is any. */
5f5c8ee5 12405 pt_row = NULL;
ac90c44f
GM
12406 for (first_row_to_display = first_reusable_row;
12407 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12408 ++first_row_to_display)
5f5c8ee5 12409 {
4bde0ebb
GM
12410 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12411 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
5f5c8ee5 12412 pt_row = first_row_to_display;
5f5c8ee5 12413 }
a2889657 12414
5f5c8ee5
GM
12415 /* Start displaying at the start of first_row_to_display. */
12416 xassert (first_row_to_display->y < yb);
12417 init_to_row_start (&it, w, first_row_to_display);
607ec83c 12418
b48f74cb
GM
12419 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12420 - start_vpos);
5f5c8ee5
GM
12421 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12422 - nrows_scrolled);
b48f74cb 12423 it.current_y = (first_row_to_display->y - first_reusable_row->y
da8b7f4f 12424 + WINDOW_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
12425
12426 /* Display lines beginning with first_row_to_display in the
12427 desired matrix. Set last_text_row to the last row displayed
12428 that displays text. */
12429 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12430 if (pt_row == NULL)
12431 w->cursor.vpos = -1;
12432 last_text_row = NULL;
12433 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12434 if (display_line (&it))
12435 last_text_row = it.glyph_row - 1;
12436
12437 /* Give up If point isn't in a row displayed or reused. */
12438 if (w->cursor.vpos < 0)
12439 {
12440 clear_glyph_matrix (w->desired_matrix);
12441 return 0;
12442 }
12adba34 12443
5f5c8ee5
GM
12444 /* If point is in a reused row, adjust y and vpos of the cursor
12445 position. */
12446 if (pt_row)
12447 {
12448 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12449 w->current_matrix);
12450 w->cursor.y -= first_reusable_row->y;
a2889657
JB
12451 }
12452
5f5c8ee5
GM
12453 /* Scroll the display. */
12454 run.current_y = first_reusable_row->y;
da8b7f4f 12455 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
5f5c8ee5 12456 run.height = it.last_visible_y - run.current_y;
4da61803 12457 dy = run.current_y - run.desired_y;
2311178e 12458
5f5c8ee5
GM
12459 if (run.height)
12460 {
12461 struct frame *f = XFRAME (WINDOW_FRAME (w));
12462 update_begin (f);
12463 rif->update_window_begin_hook (w);
fa3c6b4d 12464 rif->clear_window_mouse_face (w);
5f5c8ee5 12465 rif->scroll_run_hook (w, &run);
64d1e7d3 12466 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
12467 update_end (f);
12468 }
a2889657 12469
5f5c8ee5
GM
12470 /* Adjust Y positions of reused rows. */
12471 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
da8b7f4f 12472 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
5f5c8ee5 12473 max_y = it.last_visible_y;
b48f74cb 12474 for (row = first_reusable_row; row < first_row_to_display; ++row)
5f5c8ee5
GM
12475 {
12476 row->y -= dy;
75c5350a 12477 row->visible_height = row->height;
5f5c8ee5 12478 if (row->y < min_y)
75c5350a
GM
12479 row->visible_height -= min_y - row->y;
12480 if (row->y + row->height > max_y)
12481 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5 12482 }
a2889657 12483
5f5c8ee5
GM
12484 /* Scroll the current matrix. */
12485 xassert (nrows_scrolled > 0);
12486 rotate_matrix (w->current_matrix,
12487 start_vpos,
12488 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12489 -nrows_scrolled);
12490
ac90c44f
GM
12491 /* Disable rows not reused. */
12492 for (row -= nrows_scrolled; row < bottom_row; ++row)
12493 row->enabled_p = 0;
12494
5f5c8ee5
GM
12495 /* Adjust window end. A null value of last_text_row means that
12496 the window end is in reused rows which in turn means that
12497 only its vpos can have changed. */
12498 if (last_text_row)
12499 {
12500 w->window_end_bytepos
12501 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
12502 w->window_end_pos
12503 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12504 w->window_end_vpos
12505 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
12506 }
12507 else
a2889657 12508 {
ac90c44f
GM
12509 w->window_end_vpos
12510 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 12511 }
2311178e 12512
5f5c8ee5
GM
12513 w->window_end_valid = Qnil;
12514 w->desired_matrix->no_scrolling_p = 1;
12515
12516#if GLYPH_DEBUG
12517 debug_method_add (w, "try_window_reusing_current_matrix 2");
12518#endif
12519 return 1;
a2889657 12520 }
2311178e 12521
5f5c8ee5
GM
12522 return 0;
12523}
a2889657 12524
a2889657 12525
5f5c8ee5
GM
12526\f
12527/************************************************************************
12528 Window redisplay reusing current matrix when buffer has changed
12529 ************************************************************************/
12530
1ec185cb
GM
12531static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12532static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
5f5c8ee5
GM
12533 int *, int *));
12534static struct glyph_row *
12535find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12536 struct glyph_row *));
12537
12538
12539/* Return the last row in MATRIX displaying text. If row START is
12540 non-null, start searching with that row. IT gives the dimensions
12541 of the display. Value is null if matrix is empty; otherwise it is
12542 a pointer to the row found. */
12543
12544static struct glyph_row *
12545find_last_row_displaying_text (matrix, it, start)
12546 struct glyph_matrix *matrix;
12547 struct it *it;
12548 struct glyph_row *start;
12549{
12550 struct glyph_row *row, *row_found;
12551
12552 /* Set row_found to the last row in IT->w's current matrix
12553 displaying text. The loop looks funny but think of partially
12554 visible lines. */
12555 row_found = NULL;
12556 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12557 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12558 {
12559 xassert (row->enabled_p);
12560 row_found = row;
12561 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12562 break;
12563 ++row;
a2889657 12564 }
2311178e 12565
5f5c8ee5
GM
12566 return row_found;
12567}
12568
a2889657 12569
5f5c8ee5 12570/* Return the last row in the current matrix of W that is not affected
d43fbe9d
GM
12571 by changes at the start of current_buffer that occurred since W's
12572 current matrix was built. Value is null if no such row exists.
a2889657 12573
d43fbe9d
GM
12574 BEG_UNCHANGED us the number of characters unchanged at the start of
12575 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12576 first changed character in current_buffer. Characters at positions <
12577 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12578 when the current matrix was built. */
5f5c8ee5
GM
12579
12580static struct glyph_row *
1ec185cb 12581find_last_unchanged_at_beg_row (w)
5f5c8ee5
GM
12582 struct window *w;
12583{
9142dd5b 12584 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
12585 struct glyph_row *row;
12586 struct glyph_row *row_found = NULL;
12587 int yb = window_text_bottom_y (w);
12588
12589 /* Find the last row displaying unchanged text. */
12590 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12591 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12592 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 12593 {
5f5c8ee5
GM
12594 if (/* If row ends before first_changed_pos, it is unchanged,
12595 except in some case. */
12596 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12597 /* When row ends in ZV and we write at ZV it is not
12598 unchanged. */
12599 && !row->ends_at_zv_p
12600 /* When first_changed_pos is the end of a continued line,
12601 row is not unchanged because it may be no longer
12602 continued. */
12603 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12604 && row->continued_p))
12605 row_found = row;
12606
12607 /* Stop if last visible row. */
12608 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12609 break;
2311178e 12610
5f5c8ee5 12611 ++row;
a2889657
JB
12612 }
12613
5f5c8ee5 12614 return row_found;
a2889657 12615}
5f5c8ee5
GM
12616
12617
12618/* Find the first glyph row in the current matrix of W that is not
2311178e 12619 affected by changes at the end of current_buffer since the
d43fbe9d
GM
12620 time W's current matrix was built.
12621
12622 Return in *DELTA the number of chars by which buffer positions in
12623 unchanged text at the end of current_buffer must be adjusted.
2311178e 12624
d43fbe9d
GM
12625 Return in *DELTA_BYTES the corresponding number of bytes.
12626
12627 Value is null if no such row exists, i.e. all rows are affected by
12628 changes. */
2311178e 12629
5f5c8ee5 12630static struct glyph_row *
1ec185cb 12631find_first_unchanged_at_end_row (w, delta, delta_bytes)
5f5c8ee5
GM
12632 struct window *w;
12633 int *delta, *delta_bytes;
a2889657 12634{
5f5c8ee5
GM
12635 struct glyph_row *row;
12636 struct glyph_row *row_found = NULL;
c581d710 12637
5f5c8ee5 12638 *delta = *delta_bytes = 0;
b2a76982 12639
1ec185cb
GM
12640 /* Display must not have been paused, otherwise the current matrix
12641 is not up to date. */
12642 if (NILP (w->window_end_valid))
12643 abort ();
2311178e 12644
1ec185cb 12645 /* A value of window_end_pos >= END_UNCHANGED means that the window
5f5c8ee5
GM
12646 end is in the range of changed text. If so, there is no
12647 unchanged row at the end of W's current matrix. */
9142dd5b 12648 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
12649 return NULL;
12650
12651 /* Set row to the last row in W's current matrix displaying text. */
12652 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
2311178e
TTN
12653
12654 /* If matrix is entirely empty, no unchanged row exists. */
5f5c8ee5
GM
12655 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12656 {
12657 /* The value of row is the last glyph row in the matrix having a
12658 meaningful buffer position in it. The end position of row
12659 corresponds to window_end_pos. This allows us to translate
12660 buffer positions in the current matrix to current buffer
12661 positions for characters not in changed text. */
12662 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12663 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12664 int last_unchanged_pos, last_unchanged_pos_old;
12665 struct glyph_row *first_text_row
12666 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12667
12668 *delta = Z - Z_old;
12669 *delta_bytes = Z_BYTE - Z_BYTE_old;
12670
12671 /* Set last_unchanged_pos to the buffer position of the last
12672 character in the buffer that has not been changed. Z is the
d43fbe9d
GM
12673 index + 1 of the last character in current_buffer, i.e. by
12674 subtracting END_UNCHANGED we get the index of the last
5f5c8ee5
GM
12675 unchanged character, and we have to add BEG to get its buffer
12676 position. */
9142dd5b 12677 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5 12678 last_unchanged_pos_old = last_unchanged_pos - *delta;
2311178e 12679
5f5c8ee5
GM
12680 /* Search backward from ROW for a row displaying a line that
12681 starts at a minimum position >= last_unchanged_pos_old. */
1ec185cb 12682 for (; row > first_text_row; --row)
5f5c8ee5 12683 {
1ec185cb
GM
12684 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12685 abort ();
2311178e 12686
5f5c8ee5
GM
12687 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12688 row_found = row;
5f5c8ee5
GM
12689 }
12690 }
12691
1ec185cb
GM
12692 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12693 abort ();
2311178e 12694
5f5c8ee5 12695 return row_found;
c581d710
RS
12696}
12697
c581d710 12698
5f5c8ee5
GM
12699/* Make sure that glyph rows in the current matrix of window W
12700 reference the same glyph memory as corresponding rows in the
12701 frame's frame matrix. This function is called after scrolling W's
12702 current matrix on a terminal frame in try_window_id and
12703 try_window_reusing_current_matrix. */
12704
12705static void
12706sync_frame_with_window_matrix_rows (w)
12707 struct window *w;
c581d710 12708{
5f5c8ee5
GM
12709 struct frame *f = XFRAME (w->frame);
12710 struct glyph_row *window_row, *window_row_end, *frame_row;
12711
12712 /* Preconditions: W must be a leaf window and full-width. Its frame
12713 must have a frame matrix. */
12714 xassert (NILP (w->hchild) && NILP (w->vchild));
12715 xassert (WINDOW_FULL_WIDTH_P (w));
12716 xassert (!FRAME_WINDOW_P (f));
12717
12718 /* If W is a full-width window, glyph pointers in W's current matrix
12719 have, by definition, to be the same as glyph pointers in the
7d4cc828
GM
12720 corresponding frame matrix. Note that frame matrices have no
12721 marginal areas (see build_frame_matrix). */
5f5c8ee5
GM
12722 window_row = w->current_matrix->rows;
12723 window_row_end = window_row + w->current_matrix->nrows;
da8b7f4f 12724 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
5f5c8ee5 12725 while (window_row < window_row_end)
659a218f 12726 {
7d4cc828
GM
12727 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12728 struct glyph *end = window_row->glyphs[LAST_AREA];
12729
12730 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12731 frame_row->glyphs[TEXT_AREA] = start;
12732 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12733 frame_row->glyphs[LAST_AREA] = end;
f002db93
GM
12734
12735 /* Disable frame rows whose corresponding window rows have
12736 been disabled in try_window_id. */
12737 if (!window_row->enabled_p)
12738 frame_row->enabled_p = 0;
2311178e 12739
5f5c8ee5 12740 ++window_row, ++frame_row;
659a218f 12741 }
a2889657 12742}
5f5c8ee5
GM
12743
12744
e037b9ec
GM
12745/* Find the glyph row in window W containing CHARPOS. Consider all
12746 rows between START and END (not inclusive). END null means search
12747 all rows to the end of the display area of W. Value is the row
12748 containing CHARPOS or null. */
12749
0bef35bc 12750struct glyph_row *
0df56f4d 12751row_containing_pos (w, charpos, start, end, dy)
e037b9ec
GM
12752 struct window *w;
12753 int charpos;
12754 struct glyph_row *start, *end;
0df56f4d 12755 int dy;
e037b9ec
GM
12756{
12757 struct glyph_row *row = start;
12758 int last_y;
12759
12760 /* If we happen to start on a header-line, skip that. */
12761 if (row->mode_line_p)
12762 ++row;
2311178e 12763
e037b9ec
GM
12764 if ((end && row >= end) || !row->enabled_p)
12765 return NULL;
2311178e 12766
0df56f4d 12767 last_y = window_text_bottom_y (w) - dy;
2311178e 12768
5cce13dd
RS
12769 while (1)
12770 {
12771 /* Give up if we have gone too far. */
12772 if (end && row >= end)
12773 return NULL;
40a301b3
RS
12774 /* This formerly returned if they were equal.
12775 I think that both quantities are of a "last plus one" type;
12776 if so, when they are equal, the row is within the screen. -- rms. */
12777 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
5cce13dd
RS
12778 return NULL;
12779
12780 /* If it is in this row, return this row. */
12781 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
e037b9ec 12782 || (MATRIX_ROW_END_CHARPOS (row) == charpos
0df56f4d
GM
12783 /* The end position of a row equals the start
12784 position of the next row. If CHARPOS is there, we
12785 would rather display it in the next line, except
12786 when this line ends in ZV. */
12787 && !row->ends_at_zv_p
5cce13dd
RS
12788 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12789 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12790 return row;
12791 ++row;
12792 }
e037b9ec
GM
12793}
12794
12795
5f5c8ee5
GM
12796/* Try to redisplay window W by reusing its existing display. W's
12797 current matrix must be up to date when this function is called,
12798 i.e. window_end_valid must not be nil.
12799
12800 Value is
12801
12802 1 if display has been updated
12803 0 if otherwise unsuccessful
12804 -1 if redisplay with same window start is known not to succeed
12805
12806 The following steps are performed:
12807
12808 1. Find the last row in the current matrix of W that is not
12809 affected by changes at the start of current_buffer. If no such row
12810 is found, give up.
12811
12812 2. Find the first row in W's current matrix that is not affected by
12813 changes at the end of current_buffer. Maybe there is no such row.
12814
12815 3. Display lines beginning with the row + 1 found in step 1 to the
12816 row found in step 2 or, if step 2 didn't find a row, to the end of
12817 the window.
12818
12819 4. If cursor is not known to appear on the window, give up.
12820
12821 5. If display stopped at the row found in step 2, scroll the
12822 display and current matrix as needed.
12823
12824 6. Maybe display some lines at the end of W, if we must. This can
12825 happen under various circumstances, like a partially visible line
12826 becoming fully visible, or because newly displayed lines are displayed
12827 in smaller font sizes.
12828
12829 7. Update W's window end information. */
12830
12adba34 12831static int
5f5c8ee5 12832try_window_id (w)
12adba34 12833 struct window *w;
12adba34 12834{
5f5c8ee5
GM
12835 struct frame *f = XFRAME (w->frame);
12836 struct glyph_matrix *current_matrix = w->current_matrix;
12837 struct glyph_matrix *desired_matrix = w->desired_matrix;
12838 struct glyph_row *last_unchanged_at_beg_row;
12839 struct glyph_row *first_unchanged_at_end_row;
12840 struct glyph_row *row;
12841 struct glyph_row *bottom_row;
12842 int bottom_vpos;
12843 struct it it;
12844 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12845 struct text_pos start_pos;
12846 struct run run;
12847 int first_unchanged_at_end_vpos = 0;
12848 struct glyph_row *last_text_row, *last_text_row_at_end;
12849 struct text_pos start;
27d16f05 12850 int first_changed_charpos, last_changed_charpos;
5f5c8ee5 12851
69d1f7c9 12852#if GLYPH_DEBUG
76cb5e06
GM
12853 if (inhibit_try_window_id)
12854 return 0;
12855#endif
12856
27d16f05
GM
12857 /* This is handy for debugging. */
12858#if 0
12859#define GIVE_UP(X) \
12860 do { \
12861 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12862 return 0; \
12863 } while (0)
12864#else
62397849 12865#define GIVE_UP(X) return 0
27d16f05 12866#endif
2311178e 12867
5f5c8ee5
GM
12868 SET_TEXT_POS_FROM_MARKER (start, w->start);
12869
27d16f05
GM
12870 /* Don't use this for mini-windows because these can show
12871 messages and mini-buffers, and we don't handle that here. */
12872 if (MINI_WINDOW_P (w))
12873 GIVE_UP (1);
2311178e 12874
27d16f05 12875 /* This flag is used to prevent redisplay optimizations. */
5fb96e96 12876 if (windows_or_buffers_changed || cursor_type_changed)
27d16f05 12877 GIVE_UP (2);
2311178e 12878
c62c1bb5
RS
12879 /* Verify that narrowing has not changed.
12880 Also verify that we were not told to prevent redisplay optimizations.
12881 It would be nice to further
27d16f05 12882 reduce the number of cases where this prevents try_window_id. */
c62c1bb5
RS
12883 if (current_buffer->clip_changed
12884 || current_buffer->prevent_redisplay_optimizations_p)
27d16f05
GM
12885 GIVE_UP (3);
12886
12887 /* Window must either use window-based redisplay or be full width. */
12888 if (!FRAME_WINDOW_P (f)
28d440ab 12889 && (!TTY_LINE_INS_DEL_OK (CURTTY ())
27d16f05
GM
12890 || !WINDOW_FULL_WIDTH_P (w)))
12891 GIVE_UP (4);
5f5c8ee5 12892
f5376658 12893 /* Give up if point is not known NOT to appear in W. */
27d16f05
GM
12894 if (PT < CHARPOS (start))
12895 GIVE_UP (5);
12896
12897 /* Another way to prevent redisplay optimizations. */
12898 if (XFASTINT (w->last_modified) == 0)
12899 GIVE_UP (6);
2311178e 12900
f5376658 12901 /* Verify that window is not hscrolled. */
27d16f05
GM
12902 if (XFASTINT (w->hscroll) != 0)
12903 GIVE_UP (7);
2311178e 12904
f5376658 12905 /* Verify that display wasn't paused. */
27d16f05
GM
12906 if (NILP (w->window_end_valid))
12907 GIVE_UP (8);
2311178e 12908
27d16f05
GM
12909 /* Can't use this if highlighting a region because a cursor movement
12910 will do more than just set the cursor. */
12911 if (!NILP (Vtransient_mark_mode)
12912 && !NILP (current_buffer->mark_active))
12913 GIVE_UP (9);
12914
12915 /* Likewise if highlighting trailing whitespace. */
12916 if (!NILP (Vshow_trailing_whitespace))
12917 GIVE_UP (11);
2311178e 12918
27d16f05
GM
12919 /* Likewise if showing a region. */
12920 if (!NILP (w->region_showing))
12921 GIVE_UP (10);
2311178e 12922
27d16f05
GM
12923 /* Can use this if overlay arrow position and or string have changed. */
12924 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
12925 || !EQ (last_arrow_string, Voverlay_arrow_string))
12926 GIVE_UP (12);
12927
2311178e 12928
5f5c8ee5
GM
12929 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12930 only if buffer has really changed. The reason is that the gap is
12931 initially at Z for freshly visited files. The code below would
12932 set end_unchanged to 0 in that case. */
28ee91c0
GM
12933 if (MODIFF > SAVE_MODIFF
12934 /* This seems to happen sometimes after saving a buffer. */
12935 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
5f5c8ee5 12936 {
9142dd5b
GM
12937 if (GPT - BEG < BEG_UNCHANGED)
12938 BEG_UNCHANGED = GPT - BEG;
12939 if (Z - GPT < END_UNCHANGED)
12940 END_UNCHANGED = Z - GPT;
5f5c8ee5 12941 }
bf9249e3 12942
27d16f05
GM
12943 /* The position of the first and last character that has been changed. */
12944 first_changed_charpos = BEG + BEG_UNCHANGED;
12945 last_changed_charpos = Z - END_UNCHANGED;
12946
5f5c8ee5
GM
12947 /* If window starts after a line end, and the last change is in
12948 front of that newline, then changes don't affect the display.
f2d86d7a
GM
12949 This case happens with stealth-fontification. Note that although
12950 the display is unchanged, glyph positions in the matrix have to
12951 be adjusted, of course. */
5f5c8ee5 12952 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27d16f05 12953 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
e60f4527 12954 && ((last_changed_charpos < CHARPOS (start)
27d16f05 12955 && CHARPOS (start) == BEGV)
e60f4527 12956 || (last_changed_charpos < CHARPOS (start) - 1
27d16f05
GM
12957 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12958 {
12959 int Z_old, delta, Z_BYTE_old, delta_bytes;
12960 struct glyph_row *r0;
12961
12962 /* Compute how many chars/bytes have been added to or removed
12963 from the buffer. */
12964 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12965 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12966 delta = Z - Z_old;
12967 delta_bytes = Z_BYTE - Z_BYTE_old;
2311178e 12968
27d16f05
GM
12969 /* Give up if PT is not in the window. Note that it already has
12970 been checked at the start of try_window_id that PT is not in
12971 front of the window start. */
12972 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
12973 GIVE_UP (13);
12974
12975 /* If window start is unchanged, we can reuse the whole matrix
12976 as is, after adjusting glyph positions. No need to compute
12977 the window end again, since its offset from Z hasn't changed. */
12978 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12979 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
2d031b89
AS
12980 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
12981 /* PT must not be in a partially visible line. */
12982 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
12983 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
27d16f05
GM
12984 {
12985 /* Adjust positions in the glyph matrix. */
12986 if (delta || delta_bytes)
12987 {
12988 struct glyph_row *r1
12989 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12990 increment_matrix_positions (w->current_matrix,
12991 MATRIX_ROW_VPOS (r0, current_matrix),
12992 MATRIX_ROW_VPOS (r1, current_matrix),
12993 delta, delta_bytes);
12994 }
2311178e 12995
27d16f05 12996 /* Set the cursor. */
0df56f4d 12997 row = row_containing_pos (w, PT, r0, NULL, 0);
59adf8e8
KS
12998 if (row)
12999 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
40a301b3
RS
13000 else
13001 abort ();
27d16f05
GM
13002 return 1;
13003 }
5f5c8ee5
GM
13004 }
13005
27d16f05 13006 /* Handle the case that changes are all below what is displayed in
33ea6c4d 13007 the window, and that PT is in the window. This shortcut cannot
2b9c25e0
GM
13008 be taken if ZV is visible in the window, and text has been added
13009 there that is visible in the window. */
13010 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
33ea6c4d
GM
13011 /* ZV is not visible in the window, or there are no
13012 changes at ZV, actually. */
13013 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13014 || first_changed_charpos == last_changed_charpos))
5f5c8ee5 13015 {
27d16f05 13016 struct glyph_row *r0;
ef121659 13017
27d16f05
GM
13018 /* Give up if PT is not in the window. Note that it already has
13019 been checked at the start of try_window_id that PT is not in
13020 front of the window start. */
13021 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13022 GIVE_UP (14);
13023
13024 /* If window start is unchanged, we can reuse the whole matrix
13025 as is, without changing glyph positions since no text has
13026 been added/removed in front of the window end. */
13027 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
2d031b89
AS
13028 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13029 /* PT must not be in a partially visible line. */
13030 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13031 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
27d16f05
GM
13032 {
13033 /* We have to compute the window end anew since text
13034 can have been added/removed after it. */
13035 w->window_end_pos
13036 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13037 w->window_end_bytepos
13038 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13039
13040 /* Set the cursor. */
0df56f4d 13041 row = row_containing_pos (w, PT, r0, NULL, 0);
59adf8e8
KS
13042 if (row)
13043 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
40a301b3
RS
13044 else
13045 abort ();
27d16f05
GM
13046 return 2;
13047 }
5f5c8ee5
GM
13048 }
13049
2b9c25e0 13050 /* Give up if window start is in the changed area.
2311178e 13051
2b9c25e0
GM
13052 The condition used to read
13053
13054 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13055
13056 but why that was tested escapes me at the moment. */
13057 if (CHARPOS (start) >= first_changed_charpos
27d16f05
GM
13058 && CHARPOS (start) <= last_changed_charpos)
13059 GIVE_UP (15);
2311178e 13060
f5376658
RS
13061 /* Check that window start agrees with the start of the first glyph
13062 row in its current matrix. Check this after we know the window
13063 start is not in changed text, otherwise positions would not be
13064 comparable. */
27d16f05 13065 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
5f5c8ee5 13066 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
27d16f05 13067 GIVE_UP (16);
5f5c8ee5 13068
23e8bd86
GM
13069 /* Give up if the window ends in strings. Overlay strings
13070 at the end are difficult to handle, so don't try. */
13071 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13072 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13073 GIVE_UP (20);
13074
5f5c8ee5
GM
13075 /* Compute the position at which we have to start displaying new
13076 lines. Some of the lines at the top of the window might be
13077 reusable because they are not displaying changed text. Find the
13078 last row in W's current matrix not affected by changes at the
13079 start of current_buffer. Value is null if changes start in the
13080 first line of window. */
1ec185cb 13081 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
5f5c8ee5
GM
13082 if (last_unchanged_at_beg_row)
13083 {
67f1cf4c
GM
13084 /* Avoid starting to display in the moddle of a character, a TAB
13085 for instance. This is easier than to set up the iterator
13086 exactly, and it's not a frequent case, so the additional
13087 effort wouldn't really pay off. */
e74fb0f7
GM
13088 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13089 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
67f1cf4c
GM
13090 && last_unchanged_at_beg_row > w->current_matrix->rows)
13091 --last_unchanged_at_beg_row;
13092
13093 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
27d16f05 13094 GIVE_UP (17);
47d57b22
GM
13095
13096 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13097 GIVE_UP (18);
5f5c8ee5 13098 start_pos = it.current.pos;
2311178e 13099
5f5c8ee5
GM
13100 /* Start displaying new lines in the desired matrix at the same
13101 vpos we would use in the current matrix, i.e. below
13102 last_unchanged_at_beg_row. */
13103 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13104 current_matrix);
13105 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13106 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13107
13108 xassert (it.hpos == 0 && it.current_x == 0);
13109 }
13110 else
13111 {
13112 /* There are no reusable lines at the start of the window.
3ccb8bcf 13113 Start displaying in the first text line. */
5f5c8ee5 13114 start_display (&it, w, start);
3ccb8bcf 13115 it.vpos = it.first_vpos;
5f5c8ee5
GM
13116 start_pos = it.current.pos;
13117 }
13118
5f5c8ee5
GM
13119 /* Find the first row that is not affected by changes at the end of
13120 the buffer. Value will be null if there is no unchanged row, in
13121 which case we must redisplay to the end of the window. delta
13122 will be set to the value by which buffer positions beginning with
13123 first_unchanged_at_end_row have to be adjusted due to text
13124 changes. */
13125 first_unchanged_at_end_row
1ec185cb 13126 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
5f5c8ee5
GM
13127 IF_DEBUG (debug_delta = delta);
13128 IF_DEBUG (debug_delta_bytes = delta_bytes);
2311178e 13129
5f5c8ee5
GM
13130 /* Set stop_pos to the buffer position up to which we will have to
13131 display new lines. If first_unchanged_at_end_row != NULL, this
13132 is the buffer position of the start of the line displayed in that
13133 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13134 that we don't stop at a buffer position. */
13135 stop_pos = 0;
13136 if (first_unchanged_at_end_row)
13137 {
13138 xassert (last_unchanged_at_beg_row == NULL
13139 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
2311178e 13140
5f5c8ee5
GM
13141 /* If this is a continuation line, move forward to the next one
13142 that isn't. Changes in lines above affect this line.
13143 Caution: this may move first_unchanged_at_end_row to a row
13144 not displaying text. */
13145 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13146 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13147 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13148 < it.last_visible_y))
13149 ++first_unchanged_at_end_row;
13150
13151 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13152 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13153 >= it.last_visible_y))
13154 first_unchanged_at_end_row = NULL;
13155 else
13156 {
13157 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13158 + delta);
13159 first_unchanged_at_end_vpos
13160 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 13161 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
13162 }
13163 }
13164 else if (last_unchanged_at_beg_row == NULL)
23e8bd86 13165 GIVE_UP (19);
5f5c8ee5
GM
13166
13167
13168#if GLYPH_DEBUG
2311178e 13169
5f5c8ee5
GM
13170 /* Either there is no unchanged row at the end, or the one we have
13171 now displays text. This is a necessary condition for the window
13172 end pos calculation at the end of this function. */
13173 xassert (first_unchanged_at_end_row == NULL
13174 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
2311178e 13175
5f5c8ee5
GM
13176 debug_last_unchanged_at_beg_vpos
13177 = (last_unchanged_at_beg_row
13178 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13179 : -1);
13180 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
2311178e 13181
5f5c8ee5
GM
13182#endif /* GLYPH_DEBUG != 0 */
13183
2311178e 13184
5f5c8ee5
GM
13185 /* Display new lines. Set last_text_row to the last new line
13186 displayed which has text on it, i.e. might end up as being the
13187 line where the window_end_vpos is. */
13188 w->cursor.vpos = -1;
13189 last_text_row = NULL;
13190 overlay_arrow_seen = 0;
13191 while (it.current_y < it.last_visible_y
13192 && !fonts_changed_p
13193 && (first_unchanged_at_end_row == NULL
13194 || IT_CHARPOS (it) < stop_pos))
13195 {
13196 if (display_line (&it))
13197 last_text_row = it.glyph_row - 1;
13198 }
13199
13200 if (fonts_changed_p)
13201 return -1;
13202
13203
13204 /* Compute differences in buffer positions, y-positions etc. for
13205 lines reused at the bottom of the window. Compute what we can
13206 scroll. */
ca42b2e8
GM
13207 if (first_unchanged_at_end_row
13208 /* No lines reused because we displayed everything up to the
13209 bottom of the window. */
13210 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
13211 {
13212 dvpos = (it.vpos
13213 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13214 current_matrix));
13215 dy = it.current_y - first_unchanged_at_end_row->y;
13216 run.current_y = first_unchanged_at_end_row->y;
13217 run.desired_y = run.current_y + dy;
13218 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13219 }
13220 else
ca42b2e8
GM
13221 {
13222 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13223 first_unchanged_at_end_row = NULL;
13224 }
5f5c8ee5
GM
13225 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13226
8f8ba186 13227
5f5c8ee5
GM
13228 /* Find the cursor if not already found. We have to decide whether
13229 PT will appear on this window (it sometimes doesn't, but this is
13230 not a very frequent case.) This decision has to be made before
13231 the current matrix is altered. A value of cursor.vpos < 0 means
13232 that PT is either in one of the lines beginning at
13233 first_unchanged_at_end_row or below the window. Don't care for
13234 lines that might be displayed later at the window end; as
13235 mentioned, this is not a frequent case. */
13236 if (w->cursor.vpos < 0)
13237 {
5f5c8ee5
GM
13238 /* Cursor in unchanged rows at the top? */
13239 if (PT < CHARPOS (start_pos)
13240 && last_unchanged_at_beg_row)
13241 {
e037b9ec
GM
13242 row = row_containing_pos (w, PT,
13243 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
0df56f4d 13244 last_unchanged_at_beg_row + 1, 0);
bfe0ee88
GM
13245 if (row)
13246 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
13247 }
13248
13249 /* Start from first_unchanged_at_end_row looking for PT. */
13250 else if (first_unchanged_at_end_row)
13251 {
e037b9ec 13252 row = row_containing_pos (w, PT - delta,
0df56f4d 13253 first_unchanged_at_end_row, NULL, 0);
e037b9ec 13254 if (row)
468155d7
GM
13255 set_cursor_from_row (w, row, w->current_matrix, delta,
13256 delta_bytes, dy, dvpos);
5f5c8ee5
GM
13257 }
13258
13259 /* Give up if cursor was not found. */
13260 if (w->cursor.vpos < 0)
13261 {
13262 clear_glyph_matrix (w->desired_matrix);
13263 return -1;
13264 }
13265 }
2311178e 13266
5f5c8ee5
GM
13267 /* Don't let the cursor end in the scroll margins. */
13268 {
13269 int this_scroll_margin, cursor_height;
2311178e 13270
5f5c8ee5 13271 this_scroll_margin = max (0, scroll_margin);
da8b7f4f
KS
13272 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13273 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
5f5c8ee5 13274 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
2311178e 13275
5f5c8ee5
GM
13276 if ((w->cursor.y < this_scroll_margin
13277 && CHARPOS (start) > BEGV)
13278 /* Don't take scroll margin into account at the bottom because
13279 old redisplay didn't do it either. */
13280 || w->cursor.y + cursor_height > it.last_visible_y)
13281 {
13282 w->cursor.vpos = -1;
13283 clear_glyph_matrix (w->desired_matrix);
13284 return -1;
13285 }
13286 }
13287
13288 /* Scroll the display. Do it before changing the current matrix so
13289 that xterm.c doesn't get confused about where the cursor glyph is
13290 found. */
fa77249f 13291 if (dy && run.height)
5f5c8ee5
GM
13292 {
13293 update_begin (f);
2311178e 13294
5f5c8ee5
GM
13295 if (FRAME_WINDOW_P (f))
13296 {
13297 rif->update_window_begin_hook (w);
fa3c6b4d 13298 rif->clear_window_mouse_face (w);
5f5c8ee5 13299 rif->scroll_run_hook (w, &run);
64d1e7d3 13300 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
13301 }
13302 else
13303 {
13304 /* Terminal frame. In this case, dvpos gives the number of
13305 lines to scroll by; dvpos < 0 means scroll up. */
13306 int first_unchanged_at_end_vpos
13307 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
da8b7f4f
KS
13308 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13309 int end = (WINDOW_TOP_EDGE_LINE (w)
522ed7fb
GM
13310 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13311 + window_internal_height (w));
2311178e 13312
5f5c8ee5
GM
13313 /* Perform the operation on the screen. */
13314 if (dvpos > 0)
13315 {
13316 /* Scroll last_unchanged_at_beg_row to the end of the
13317 window down dvpos lines. */
13318 set_terminal_window (end);
13319
13320 /* On dumb terminals delete dvpos lines at the end
13321 before inserting dvpos empty lines. */
28d440ab 13322 if (!TTY_SCROLL_REGION_OK (CURTTY ()))
5f5c8ee5
GM
13323 ins_del_lines (end - dvpos, -dvpos);
13324
13325 /* Insert dvpos empty lines in front of
13326 last_unchanged_at_beg_row. */
13327 ins_del_lines (from, dvpos);
13328 }
13329 else if (dvpos < 0)
13330 {
13331 /* Scroll up last_unchanged_at_beg_vpos to the end of
13332 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13333 set_terminal_window (end);
13334
13335 /* Delete dvpos lines in front of
13336 last_unchanged_at_beg_vpos. ins_del_lines will set
13337 the cursor to the given vpos and emit |dvpos| delete
13338 line sequences. */
13339 ins_del_lines (from + dvpos, dvpos);
13340
13341 /* On a dumb terminal insert dvpos empty lines at the
13342 end. */
28d440ab 13343 if (!TTY_SCROLL_REGION_OK (CURTTY ()))
5f5c8ee5
GM
13344 ins_del_lines (end + dvpos, -dvpos);
13345 }
2311178e 13346
5f5c8ee5
GM
13347 set_terminal_window (0);
13348 }
2311178e 13349
5f5c8ee5
GM
13350 update_end (f);
13351 }
13352
ca42b2e8
GM
13353 /* Shift reused rows of the current matrix to the right position.
13354 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13355 text. */
13356 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13357 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
13358 if (dvpos < 0)
13359 {
13360 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13361 bottom_vpos, dvpos);
13362 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13363 bottom_vpos, 0);
13364 }
13365 else if (dvpos > 0)
13366 {
13367 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13368 bottom_vpos, dvpos);
13369 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13370 first_unchanged_at_end_vpos + dvpos, 0);
13371 }
13372
13373 /* For frame-based redisplay, make sure that current frame and window
13374 matrix are in sync with respect to glyph memory. */
13375 if (!FRAME_WINDOW_P (f))
13376 sync_frame_with_window_matrix_rows (w);
13377
13378 /* Adjust buffer positions in reused rows. */
13379 if (delta)
f2d86d7a
GM
13380 increment_matrix_positions (current_matrix,
13381 first_unchanged_at_end_vpos + dvpos,
13382 bottom_vpos, delta, delta_bytes);
5f5c8ee5
GM
13383
13384 /* Adjust Y positions. */
13385 if (dy)
13386 shift_glyph_matrix (w, current_matrix,
13387 first_unchanged_at_end_vpos + dvpos,
13388 bottom_vpos, dy);
13389
13390 if (first_unchanged_at_end_row)
13391 first_unchanged_at_end_row += dvpos;
13392
13393 /* If scrolling up, there may be some lines to display at the end of
13394 the window. */
13395 last_text_row_at_end = NULL;
13396 if (dy < 0)
13397 {
9c6ba6d1
GM
13398 /* Scrolling up can leave for example a partially visible line
13399 at the end of the window to be redisplayed. */
5f5c8ee5
GM
13400 /* Set last_row to the glyph row in the current matrix where the
13401 window end line is found. It has been moved up or down in
13402 the matrix by dvpos. */
13403 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13404 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13405
13406 /* If last_row is the window end line, it should display text. */
13407 xassert (last_row->displays_text_p);
13408
13409 /* If window end line was partially visible before, begin
13410 displaying at that line. Otherwise begin displaying with the
13411 line following it. */
13412 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13413 {
13414 init_to_row_start (&it, w, last_row);
13415 it.vpos = last_vpos;
13416 it.current_y = last_row->y;
13417 }
13418 else
13419 {
13420 init_to_row_end (&it, w, last_row);
13421 it.vpos = 1 + last_vpos;
13422 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13423 ++last_row;
13424 }
12adba34 13425
23e8bd86
GM
13426 /* We may start in a continuation line. If so, we have to
13427 get the right continuation_lines_width and current_x. */
13428 it.continuation_lines_width = last_row->continuation_lines_width;
13429 it.hpos = it.current_x = 0;
2311178e 13430
23e8bd86
GM
13431 /* Display the rest of the lines at the window end. */
13432 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13433 while (it.current_y < it.last_visible_y
13434 && !fonts_changed_p)
5f5c8ee5 13435 {
23e8bd86
GM
13436 /* Is it always sure that the display agrees with lines in
13437 the current matrix? I don't think so, so we mark rows
13438 displayed invalid in the current matrix by setting their
13439 enabled_p flag to zero. */
13440 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13441 if (display_line (&it))
13442 last_text_row_at_end = it.glyph_row - 1;
5f5c8ee5
GM
13443 }
13444 }
12adba34 13445
5f5c8ee5
GM
13446 /* Update window_end_pos and window_end_vpos. */
13447 if (first_unchanged_at_end_row
13448 && first_unchanged_at_end_row->y < it.last_visible_y
13449 && !last_text_row_at_end)
13450 {
13451 /* Window end line if one of the preserved rows from the current
13452 matrix. Set row to the last row displaying text in current
13453 matrix starting at first_unchanged_at_end_row, after
13454 scrolling. */
13455 xassert (first_unchanged_at_end_row->displays_text_p);
13456 row = find_last_row_displaying_text (w->current_matrix, &it,
13457 first_unchanged_at_end_row);
13458 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
2311178e 13459
ac90c44f 13460 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
5f5c8ee5 13461 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
ac90c44f
GM
13462 w->window_end_vpos
13463 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
23fca891 13464 xassert (w->window_end_bytepos >= 0);
0416532f 13465 IF_DEBUG (debug_method_add (w, "A"));
5f5c8ee5
GM
13466 }
13467 else if (last_text_row_at_end)
13468 {
ac90c44f
GM
13469 w->window_end_pos
13470 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
5f5c8ee5
GM
13471 w->window_end_bytepos
13472 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
ac90c44f
GM
13473 w->window_end_vpos
13474 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
23fca891 13475 xassert (w->window_end_bytepos >= 0);
0416532f 13476 IF_DEBUG (debug_method_add (w, "B"));
5f5c8ee5
GM
13477 }
13478 else if (last_text_row)
13479 {
13480 /* We have displayed either to the end of the window or at the
13481 end of the window, i.e. the last row with text is to be found
13482 in the desired matrix. */
ac90c44f
GM
13483 w->window_end_pos
13484 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
5f5c8ee5
GM
13485 w->window_end_bytepos
13486 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
13487 w->window_end_vpos
13488 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
23fca891 13489 xassert (w->window_end_bytepos >= 0);
5f5c8ee5
GM
13490 }
13491 else if (first_unchanged_at_end_row == NULL
13492 && last_text_row == NULL
13493 && last_text_row_at_end == NULL)
13494 {
13495 /* Displayed to end of window, but no line containing text was
13496 displayed. Lines were deleted at the end of the window. */
0416532f
GM
13497 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13498 int vpos = XFASTINT (w->window_end_vpos);
13499 struct glyph_row *current_row = current_matrix->rows + vpos;
13500 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13501
13502 for (row = NULL;
13503 row == NULL && vpos >= first_vpos;
13504 --vpos, --current_row, --desired_row)
13505 {
13506 if (desired_row->enabled_p)
13507 {
13508 if (desired_row->displays_text_p)
13509 row = desired_row;
13510 }
13511 else if (current_row->displays_text_p)
13512 row = current_row;
13513 }
12adba34 13514
0416532f 13515 xassert (row != NULL);
d88a79d4 13516 w->window_end_vpos = make_number (vpos + 1);
8c56a983
GM
13517 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13518 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
23fca891 13519 xassert (w->window_end_bytepos >= 0);
0416532f 13520 IF_DEBUG (debug_method_add (w, "C"));
5f5c8ee5
GM
13521 }
13522 else
13523 abort ();
ac90c44f 13524
8cdb267e
GM
13525#if 0 /* This leads to problems, for instance when the cursor is
13526 at ZV, and the cursor line displays no text. */
ac90c44f
GM
13527 /* Disable rows below what's displayed in the window. This makes
13528 debugging easier. */
13529 enable_glyph_matrix_rows (current_matrix,
13530 XFASTINT (w->window_end_vpos) + 1,
13531 bottom_vpos, 0);
8cdb267e 13532#endif
23e8bd86 13533
5f5c8ee5
GM
13534 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13535 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 13536
5f5c8ee5
GM
13537 /* Record that display has not been completed. */
13538 w->window_end_valid = Qnil;
13539 w->desired_matrix->no_scrolling_p = 1;
ef121659 13540 return 3;
27d16f05
GM
13541
13542#undef GIVE_UP
12adba34 13543}
0f9c0ff0 13544
a2889657 13545
5f5c8ee5
GM
13546\f
13547/***********************************************************************
13548 More debugging support
13549 ***********************************************************************/
a2889657 13550
5f5c8ee5 13551#if GLYPH_DEBUG
a2889657 13552
eaaa65b0 13553void dump_glyph_row P_ ((struct glyph_row *, int, int));
9ab436b9 13554void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
e187cf71 13555void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
1c9241f5 13556
31b24551 13557
9ab436b9 13558/* Dump the contents of glyph matrix MATRIX on stderr.
ca26e1c8 13559
9ab436b9
GM
13560 GLYPHS 0 means don't show glyph contents.
13561 GLYPHS 1 means show glyphs in short form
13562 GLYPHS > 1 means show glyphs in long form. */
13563
13564void
13565dump_glyph_matrix (matrix, glyphs)
5f5c8ee5 13566 struct glyph_matrix *matrix;
9ab436b9 13567 int glyphs;
5f5c8ee5 13568{
efc63ef0 13569 int i;
5f5c8ee5 13570 for (i = 0; i < matrix->nrows; ++i)
eaaa65b0 13571 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
5f5c8ee5 13572}
31b24551 13573
68a37fa8 13574
e187cf71
GM
13575/* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13576 the glyph row and area where the glyph comes from. */
13577
13578void
13579dump_glyph (row, glyph, area)
13580 struct glyph_row *row;
13581 struct glyph *glyph;
13582 int area;
13583{
13584 if (glyph->type == CHAR_GLYPH)
13585 {
13586 fprintf (stderr,
13587 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13588 glyph - row->glyphs[TEXT_AREA],
13589 'C',
13590 glyph->charpos,
13591 (BUFFERP (glyph->object)
13592 ? 'B'
13593 : (STRINGP (glyph->object)
13594 ? 'S'
13595 : '-')),
13596 glyph->pixel_width,
13597 glyph->u.ch,
13598 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13599 ? glyph->u.ch
13600 : '.'),
13601 glyph->face_id,
13602 glyph->left_box_line_p,
13603 glyph->right_box_line_p);
13604 }
13605 else if (glyph->type == STRETCH_GLYPH)
13606 {
13607 fprintf (stderr,
13608 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13609 glyph - row->glyphs[TEXT_AREA],
13610 'S',
13611 glyph->charpos,
13612 (BUFFERP (glyph->object)
13613 ? 'B'
13614 : (STRINGP (glyph->object)
13615 ? 'S'
13616 : '-')),
13617 glyph->pixel_width,
13618 0,
13619 '.',
13620 glyph->face_id,
13621 glyph->left_box_line_p,
13622 glyph->right_box_line_p);
13623 }
13624 else if (glyph->type == IMAGE_GLYPH)
13625 {
13626 fprintf (stderr,
13627 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13628 glyph - row->glyphs[TEXT_AREA],
13629 'I',
13630 glyph->charpos,
13631 (BUFFERP (glyph->object)
13632 ? 'B'
13633 : (STRINGP (glyph->object)
13634 ? 'S'
13635 : '-')),
13636 glyph->pixel_width,
13637 glyph->u.img_id,
13638 '.',
13639 glyph->face_id,
13640 glyph->left_box_line_p,
13641 glyph->right_box_line_p);
13642 }
13643}
13644
13645
5f5c8ee5 13646/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
9ab436b9
GM
13647 GLYPHS 0 means don't show glyph contents.
13648 GLYPHS 1 means show glyphs in short form
13649 GLYPHS > 1 means show glyphs in long form. */
a2889657 13650
5f5c8ee5 13651void
eaaa65b0
GM
13652dump_glyph_row (row, vpos, glyphs)
13653 struct glyph_row *row;
9ab436b9 13654 int vpos, glyphs;
5f5c8ee5 13655{
9ab436b9
GM
13656 if (glyphs != 1)
13657 {
b94c0d9c 13658 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
9ab436b9 13659 fprintf (stderr, "=======================================================================\n");
2311178e 13660
0ad38729 13661 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
33ea6c4d 13662%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 13663 vpos,
9ab436b9
GM
13664 MATRIX_ROW_START_CHARPOS (row),
13665 MATRIX_ROW_END_CHARPOS (row),
13666 row->used[TEXT_AREA],
13667 row->contains_overlapping_glyphs_p,
13668 row->enabled_p,
9ab436b9
GM
13669 row->truncated_on_left_p,
13670 row->truncated_on_right_p,
13671 row->overlay_arrow_p,
13672 row->continued_p,
13673 MATRIX_ROW_CONTINUATION_LINE_P (row),
13674 row->displays_text_p,
13675 row->ends_at_zv_p,
13676 row->fill_line_p,
13677 row->ends_in_middle_of_char_p,
13678 row->starts_in_middle_of_char_p,
b94c0d9c 13679 row->mouse_face_p,
9ab436b9
GM
13680 row->x,
13681 row->y,
13682 row->pixel_width,
13683 row->height,
13684 row->visible_height,
13685 row->ascent,
13686 row->phys_ascent);
33ea6c4d
GM
13687 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13688 row->end.overlay_string_index,
13689 row->continuation_lines_width);
9ab436b9
GM
13690 fprintf (stderr, "%9d %5d\n",
13691 CHARPOS (row->start.string_pos),
13692 CHARPOS (row->end.string_pos));
13693 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13694 row->end.dpvec_index);
13695 }
2311178e 13696
9ab436b9 13697 if (glyphs > 1)
bd66d1ba 13698 {
e187cf71
GM
13699 int area;
13700
13701 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13702 {
091f8878
GM
13703 struct glyph *glyph = row->glyphs[area];
13704 struct glyph *glyph_end = glyph + row->used[area];
2311178e 13705
e187cf71 13706 /* Glyph for a line end in text. */
091f8878 13707 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
e187cf71 13708 ++glyph_end;
2311178e 13709
e187cf71
GM
13710 if (glyph < glyph_end)
13711 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
2311178e 13712
e187cf71
GM
13713 for (; glyph < glyph_end; ++glyph)
13714 dump_glyph (row, glyph, area);
f7b4b63a 13715 }
f4faa47c 13716 }
9ab436b9
GM
13717 else if (glyphs == 1)
13718 {
e187cf71 13719 int area;
9ab436b9 13720
e187cf71 13721 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
9ab436b9 13722 {
e187cf71
GM
13723 char *s = (char *) alloca (row->used[area] + 1);
13724 int i;
13725
13726 for (i = 0; i < row->used[area]; ++i)
13727 {
13728 struct glyph *glyph = row->glyphs[area] + i;
13729 if (glyph->type == CHAR_GLYPH
13730 && glyph->u.ch < 0x80
13731 && glyph->u.ch >= ' ')
13732 s[i] = glyph->u.ch;
13733 else
13734 s[i] = '.';
13735 }
2311178e 13736
e187cf71
GM
13737 s[i] = '\0';
13738 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13739 }
9ab436b9 13740 }
5f5c8ee5 13741}
f4faa47c 13742
a2889657 13743
5f5c8ee5
GM
13744DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13745 Sdump_glyph_matrix, 0, 1, "p",
7ee72033 13746 doc: /* Dump the current matrix of the selected window to stderr.
228299fa
GM
13747Shows contents of glyph row structures. With non-nil
13748parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
7ee72033
MB
13749glyphs in short form, otherwise show glyphs in long form. */)
13750 (glyphs)
9ab436b9 13751 Lisp_Object glyphs;
5f5c8ee5
GM
13752{
13753 struct window *w = XWINDOW (selected_window);
13754 struct buffer *buffer = XBUFFER (w->buffer);
13755
13756 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13757 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13758 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13759 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13760 fprintf (stderr, "=============================================\n");
9ab436b9
GM
13761 dump_glyph_matrix (w->current_matrix,
13762 NILP (glyphs) ? 0 : XINT (glyphs));
5f5c8ee5
GM
13763 return Qnil;
13764}
1c2250c2 13765
1fca3fae 13766
7d4cc828
GM
13767DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13768 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13769 ()
13770{
13771 struct frame *f = XFRAME (selected_frame);
13772 dump_glyph_matrix (f->current_matrix, 1);
13773 return Qnil;
13774}
13775
13776
e9abc296 13777DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
7ee72033 13778 doc: /* Dump glyph row ROW to stderr.
228299fa
GM
13779GLYPH 0 means don't dump glyphs.
13780GLYPH 1 means dump glyphs in short form.
7ee72033
MB
13781GLYPH > 1 or omitted means dump glyphs in long form. */)
13782 (row, glyphs)
e9abc296 13783 Lisp_Object row, glyphs;
5f5c8ee5 13784{
eaaa65b0
GM
13785 struct glyph_matrix *matrix;
13786 int vpos;
2311178e 13787
b7826503 13788 CHECK_NUMBER (row);
eaaa65b0
GM
13789 matrix = XWINDOW (selected_window)->current_matrix;
13790 vpos = XINT (row);
13791 if (vpos >= 0 && vpos < matrix->nrows)
13792 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13793 vpos,
13794 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
13795 return Qnil;
13796}
1fca3fae 13797
67481ae5 13798
b94c0d9c 13799DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
c2552f79 13800 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
228299fa
GM
13801GLYPH 0 means don't dump glyphs.
13802GLYPH 1 means dump glyphs in short form.
7ee72033
MB
13803GLYPH > 1 or omitted means dump glyphs in long form. */)
13804 (row, glyphs)
b94c0d9c 13805 Lisp_Object row, glyphs;
5f5c8ee5 13806{
886bd6f2 13807 struct frame *sf = SELECTED_FRAME ();
eaaa65b0
GM
13808 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13809 int vpos;
2311178e 13810
b7826503 13811 CHECK_NUMBER (row);
eaaa65b0
GM
13812 vpos = XINT (row);
13813 if (vpos >= 0 && vpos < m->nrows)
13814 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13815 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
13816 return Qnil;
13817}
ca26e1c8 13818
0f9c0ff0 13819
62397849 13820DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
7ee72033
MB
13821 doc: /* Toggle tracing of redisplay.
13822With ARG, turn tracing on if and only if ARG is positive. */)
13823 (arg)
62397849 13824 Lisp_Object arg;
5f5c8ee5 13825{
62397849
GM
13826 if (NILP (arg))
13827 trace_redisplay_p = !trace_redisplay_p;
13828 else
13829 {
13830 arg = Fprefix_numeric_value (arg);
13831 trace_redisplay_p = XINT (arg) > 0;
13832 }
2311178e 13833
5f5c8ee5
GM
13834 return Qnil;
13835}
bf9249e3
GM
13836
13837
f4a374a1 13838DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
e9757d41
SM
13839 doc: /* Like `format', but print result to stderr.
13840usage: (trace-to-stderr STRING &rest OBJECTS) */)
7ee72033 13841 (nargs, args)
f4a374a1
GM
13842 int nargs;
13843 Lisp_Object *args;
bf9249e3 13844{
f4a374a1 13845 Lisp_Object s = Fformat (nargs, args);
2051c264 13846 fprintf (stderr, "%s", SDATA (s));
bf9249e3
GM
13847 return Qnil;
13848}
2311178e 13849
5f5c8ee5 13850#endif /* GLYPH_DEBUG */
ca26e1c8 13851
ca26e1c8 13852
5f5c8ee5
GM
13853\f
13854/***********************************************************************
13855 Building Desired Matrix Rows
13856 ***********************************************************************/
a2889657 13857
5f5c8ee5
GM
13858/* Return a temporary glyph row holding the glyphs of an overlay
13859 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 13860
5f5c8ee5
GM
13861static struct glyph_row *
13862get_overlay_arrow_glyph_row (w)
13863 struct window *w;
13864{
13865 struct frame *f = XFRAME (WINDOW_FRAME (w));
13866 struct buffer *buffer = XBUFFER (w->buffer);
13867 struct buffer *old = current_buffer;
50f80c2f 13868 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
2051c264 13869 int arrow_len = SCHARS (Voverlay_arrow_string);
50f80c2f
KR
13870 const unsigned char *arrow_end = arrow_string + arrow_len;
13871 const unsigned char *p;
5f5c8ee5
GM
13872 struct it it;
13873 int multibyte_p;
13874 int n_glyphs_before;
13875
13876 set_buffer_temp (buffer);
13877 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13878 it.glyph_row->used[TEXT_AREA] = 0;
13879 SET_TEXT_POS (it.position, 0, 0);
13880
13881 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13882 p = arrow_string;
13883 while (p < arrow_end)
13884 {
13885 Lisp_Object face, ilisp;
2311178e 13886
5f5c8ee5
GM
13887 /* Get the next character. */
13888 if (multibyte_p)
4fdb80f2 13889 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
13890 else
13891 it.c = *p, it.len = 1;
13892 p += it.len;
2311178e 13893
5f5c8ee5 13894 /* Get its face. */
ac90c44f 13895 ilisp = make_number (p - arrow_string);
5f5c8ee5
GM
13896 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
13897 it.face_id = compute_char_face (f, it.c, face);
13898
13899 /* Compute its width, get its glyphs. */
13900 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 13901 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
13902 PRODUCE_GLYPHS (&it);
13903
13904 /* If this character doesn't fit any more in the line, we have
13905 to remove some glyphs. */
13906 if (it.current_x > it.last_visible_x)
13907 {
13908 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13909 break;
13910 }
13911 }
2311178e 13912
5f5c8ee5
GM
13913 set_buffer_temp (old);
13914 return it.glyph_row;
13915}
ca26e1c8 13916
b0a0fbda 13917
5f5c8ee5
GM
13918/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13919 glyphs are only inserted for terminal frames since we can't really
13920 win with truncation glyphs when partially visible glyphs are
13921 involved. Which glyphs to insert is determined by
13922 produce_special_glyphs. */
67481ae5 13923
5f5c8ee5
GM
13924static void
13925insert_left_trunc_glyphs (it)
13926 struct it *it;
13927{
13928 struct it truncate_it;
13929 struct glyph *from, *end, *to, *toend;
13930
13931 xassert (!FRAME_WINDOW_P (it->f));
13932
13933 /* Get the truncation glyphs. */
13934 truncate_it = *it;
5f5c8ee5
GM
13935 truncate_it.current_x = 0;
13936 truncate_it.face_id = DEFAULT_FACE_ID;
13937 truncate_it.glyph_row = &scratch_glyph_row;
13938 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13939 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
6fc556fd 13940 truncate_it.object = make_number (0);
5f5c8ee5 13941 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
2311178e 13942
5f5c8ee5
GM
13943 /* Overwrite glyphs from IT with truncation glyphs. */
13944 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13945 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13946 to = it->glyph_row->glyphs[TEXT_AREA];
13947 toend = to + it->glyph_row->used[TEXT_AREA];
13948
13949 while (from < end)
13950 *to++ = *from++;
13951
37be86f2
KH
13952 /* There may be padding glyphs left over. Overwrite them too. */
13953 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13954 {
13955 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13956 while (from < end)
13957 *to++ = *from++;
13958 }
5f5c8ee5 13959
37be86f2
KH
13960 if (to > toend)
13961 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
5f5c8ee5 13962}
e0bfbde6 13963
e0bfbde6 13964
5f5c8ee5 13965/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 13966
5f5c8ee5
GM
13967 Most of the time, ascent and height of a display line will be equal
13968 to the max_ascent and max_height values of the display iterator
13969 structure. This is not the case if
67481ae5 13970
5f5c8ee5
GM
13971 1. We hit ZV without displaying anything. In this case, max_ascent
13972 and max_height will be zero.
1c9241f5 13973
5f5c8ee5
GM
13974 2. We have some glyphs that don't contribute to the line height.
13975 (The glyph row flag contributes_to_line_height_p is for future
13976 pixmap extensions).
f6fd109b 13977
5f5c8ee5
GM
13978 The first case is easily covered by using default values because in
13979 these cases, the line height does not really matter, except that it
13980 must not be zero. */
67481ae5 13981
5f5c8ee5
GM
13982static void
13983compute_line_metrics (it)
13984 struct it *it;
13985{
13986 struct glyph_row *row = it->glyph_row;
13987 int area, i;
1c2250c2 13988
5f5c8ee5
GM
13989 if (FRAME_WINDOW_P (it->f))
13990 {
75c5350a 13991 int i, min_y, max_y;
1c2250c2 13992
5f5c8ee5
GM
13993 /* The line may consist of one space only, that was added to
13994 place the cursor on it. If so, the row's height hasn't been
13995 computed yet. */
13996 if (row->height == 0)
13997 {
13998 if (it->max_ascent + it->max_descent == 0)
da8b7f4f 13999 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
5f5c8ee5
GM
14000 row->ascent = it->max_ascent;
14001 row->height = it->max_ascent + it->max_descent;
312246d1
GM
14002 row->phys_ascent = it->max_phys_ascent;
14003 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5 14004 }
2311178e 14005
5f5c8ee5
GM
14006 /* Compute the width of this line. */
14007 row->pixel_width = row->x;
14008 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14009 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14010
14011 xassert (row->pixel_width >= 0);
14012 xassert (row->ascent >= 0 && row->height > 0);
14013
312246d1
GM
14014 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14015 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14016
14017 /* If first line's physical ascent is larger than its logical
14018 ascent, use the physical ascent, and make the row taller.
14019 This makes accented characters fully visible. */
b28cb6ed 14020 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
312246d1
GM
14021 && row->phys_ascent > row->ascent)
14022 {
14023 row->height += row->phys_ascent - row->ascent;
14024 row->ascent = row->phys_ascent;
14025 }
14026
5f5c8ee5
GM
14027 /* Compute how much of the line is visible. */
14028 row->visible_height = row->height;
2311178e 14029
da8b7f4f
KS
14030 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14031 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
75c5350a
GM
14032
14033 if (row->y < min_y)
14034 row->visible_height -= min_y - row->y;
14035 if (row->y + row->height > max_y)
14036 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5
GM
14037 }
14038 else
14039 {
14040 row->pixel_width = row->used[TEXT_AREA];
33ea6c4d
GM
14041 if (row->continued_p)
14042 row->pixel_width -= it->continuation_pixel_width;
14043 else if (row->truncated_on_right_p)
14044 row->pixel_width -= it->truncation_pixel_width;
312246d1
GM
14045 row->ascent = row->phys_ascent = 0;
14046 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 14047 }
67481ae5 14048
5f5c8ee5
GM
14049 /* Compute a hash code for this row. */
14050 row->hash = 0;
14051 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14052 for (i = 0; i < row->used[area]; ++i)
14053 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14054 + row->glyphs[area][i].u.val
43d120d8
KH
14055 + row->glyphs[area][i].face_id
14056 + row->glyphs[area][i].padding_p
5f5c8ee5 14057 + (row->glyphs[area][i].type << 2));
a2889657 14058
5f5c8ee5 14059 it->max_ascent = it->max_descent = 0;
312246d1 14060 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 14061}
12adba34 14062
ca26e1c8 14063
5f5c8ee5
GM
14064/* Append one space to the glyph row of iterator IT if doing a
14065 window-based redisplay. DEFAULT_FACE_P non-zero means let the
14066 space have the default face, otherwise let it have the same face as
80c6cb1f 14067 IT->face_id. Value is non-zero if a space was added.
c6e89d6c
GM
14068
14069 This function is called to make sure that there is always one glyph
14070 at the end of a glyph row that the cursor can be set on under
14071 window-systems. (If there weren't such a glyph we would not know
14072 how wide and tall a box cursor should be displayed).
14073
14074 At the same time this space let's a nicely handle clearing to the
14075 end of the line if the row ends in italic text. */
ca26e1c8 14076
80c6cb1f 14077static int
5f5c8ee5
GM
14078append_space (it, default_face_p)
14079 struct it *it;
14080 int default_face_p;
14081{
14082 if (FRAME_WINDOW_P (it->f))
14083 {
14084 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 14085
5f5c8ee5
GM
14086 if (it->glyph_row->glyphs[TEXT_AREA] + n
14087 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 14088 {
cafafe0b
GM
14089 /* Save some values that must not be changed.
14090 Must save IT->c and IT->len because otherwise
14091 ITERATOR_AT_END_P wouldn't work anymore after
14092 append_space has been called. */
478d746b 14093 enum display_element_type saved_what = it->what;
cafafe0b
GM
14094 int saved_c = it->c, saved_len = it->len;
14095 int saved_x = it->current_x;
5f5c8ee5 14096 int saved_face_id = it->face_id;
cafafe0b 14097 struct text_pos saved_pos;
5f5c8ee5 14098 Lisp_Object saved_object;
980806b6 14099 struct face *face;
5f5c8ee5
GM
14100
14101 saved_object = it->object;
14102 saved_pos = it->position;
2311178e 14103
5f5c8ee5
GM
14104 it->what = IT_CHARACTER;
14105 bzero (&it->position, sizeof it->position);
6fc556fd 14106 it->object = make_number (0);
5f5c8ee5
GM
14107 it->c = ' ';
14108 it->len = 1;
5f5c8ee5
GM
14109
14110 if (default_face_p)
14111 it->face_id = DEFAULT_FACE_ID;
4aad61f8
GM
14112 else if (it->face_before_selective_p)
14113 it->face_id = it->saved_face_id;
980806b6
KH
14114 face = FACE_FROM_ID (it->f, it->face_id);
14115 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
1842fc1a 14116
5f5c8ee5 14117 PRODUCE_GLYPHS (it);
2311178e 14118
5f5c8ee5
GM
14119 it->current_x = saved_x;
14120 it->object = saved_object;
14121 it->position = saved_pos;
14122 it->what = saved_what;
14123 it->face_id = saved_face_id;
cafafe0b
GM
14124 it->len = saved_len;
14125 it->c = saved_c;
80c6cb1f 14126 return 1;
5f5c8ee5
GM
14127 }
14128 }
80c6cb1f
GM
14129
14130 return 0;
5f5c8ee5 14131}
12adba34 14132
1842fc1a 14133
5f5c8ee5
GM
14134/* Extend the face of the last glyph in the text area of IT->glyph_row
14135 to the end of the display line. Called from display_line.
14136 If the glyph row is empty, add a space glyph to it so that we
14137 know the face to draw. Set the glyph row flag fill_line_p. */
2311178e 14138
5f5c8ee5
GM
14139static void
14140extend_face_to_end_of_line (it)
14141 struct it *it;
14142{
14143 struct face *face;
14144 struct frame *f = it->f;
1842fc1a 14145
5f5c8ee5
GM
14146 /* If line is already filled, do nothing. */
14147 if (it->current_x >= it->last_visible_x)
14148 return;
2311178e 14149
5f5c8ee5
GM
14150 /* Face extension extends the background and box of IT->face_id
14151 to the end of the line. If the background equals the background
4aad61f8
GM
14152 of the frame, we don't have to do anything. */
14153 if (it->face_before_selective_p)
14154 face = FACE_FROM_ID (it->f, it->saved_face_id);
14155 else
14156 face = FACE_FROM_ID (f, it->face_id);
2311178e 14157
5f5c8ee5
GM
14158 if (FRAME_WINDOW_P (f)
14159 && face->box == FACE_NO_BOX
14160 && face->background == FRAME_BACKGROUND_PIXEL (f)
14161 && !face->stipple)
14162 return;
1842fc1a 14163
5f5c8ee5
GM
14164 /* Set the glyph row flag indicating that the face of the last glyph
14165 in the text area has to be drawn to the end of the text area. */
14166 it->glyph_row->fill_line_p = 1;
545e04f6 14167
980806b6
KH
14168 /* If current character of IT is not ASCII, make sure we have the
14169 ASCII face. This will be automatically undone the next time
14170 get_next_display_element returns a multibyte character. Note
14171 that the character will always be single byte in unibyte text. */
14172 if (!SINGLE_BYTE_CHAR_P (it->c))
5f5c8ee5 14173 {
980806b6 14174 it->face_id = FACE_FOR_CHAR (f, face, 0);
5f5c8ee5 14175 }
545e04f6 14176
5f5c8ee5
GM
14177 if (FRAME_WINDOW_P (f))
14178 {
14179 /* If the row is empty, add a space with the current face of IT,
14180 so that we know which face to draw. */
14181 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 14182 {
5f5c8ee5 14183 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
43d120d8 14184 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
5f5c8ee5 14185 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 14186 }
5f5c8ee5
GM
14187 }
14188 else
14189 {
14190 /* Save some values that must not be changed. */
14191 int saved_x = it->current_x;
14192 struct text_pos saved_pos;
14193 Lisp_Object saved_object;
478d746b 14194 enum display_element_type saved_what = it->what;
4aad61f8 14195 int saved_face_id = it->face_id;
5f5c8ee5
GM
14196
14197 saved_object = it->object;
14198 saved_pos = it->position;
2311178e 14199
5f5c8ee5
GM
14200 it->what = IT_CHARACTER;
14201 bzero (&it->position, sizeof it->position);
6fc556fd 14202 it->object = make_number (0);
5f5c8ee5
GM
14203 it->c = ' ';
14204 it->len = 1;
4aad61f8 14205 it->face_id = face->id;
2311178e 14206
5f5c8ee5 14207 PRODUCE_GLYPHS (it);
2311178e 14208
5f5c8ee5
GM
14209 while (it->current_x <= it->last_visible_x)
14210 PRODUCE_GLYPHS (it);
2311178e 14211
5f5c8ee5
GM
14212 /* Don't count these blanks really. It would let us insert a left
14213 truncation glyph below and make us set the cursor on them, maybe. */
14214 it->current_x = saved_x;
14215 it->object = saved_object;
14216 it->position = saved_pos;
14217 it->what = saved_what;
4aad61f8 14218 it->face_id = saved_face_id;
5f5c8ee5
GM
14219 }
14220}
12adba34 14221
545e04f6 14222
5f5c8ee5
GM
14223/* Value is non-zero if text starting at CHARPOS in current_buffer is
14224 trailing whitespace. */
1c9241f5 14225
5f5c8ee5
GM
14226static int
14227trailing_whitespace_p (charpos)
14228 int charpos;
14229{
14230 int bytepos = CHAR_TO_BYTE (charpos);
14231 int c = 0;
7bbe686f 14232
5f5c8ee5
GM
14233 while (bytepos < ZV_BYTE
14234 && (c = FETCH_CHAR (bytepos),
14235 c == ' ' || c == '\t'))
14236 ++bytepos;
0d09d1e6 14237
8f897821
GM
14238 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14239 {
14240 if (bytepos != PT_BYTE)
14241 return 1;
14242 }
14243 return 0;
5f5c8ee5 14244}
31b24551 14245
545e04f6 14246
5f5c8ee5 14247/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 14248
5f5c8ee5
GM
14249void
14250highlight_trailing_whitespace (f, row)
14251 struct frame *f;
14252 struct glyph_row *row;
14253{
14254 int used = row->used[TEXT_AREA];
2311178e 14255
5f5c8ee5
GM
14256 if (used)
14257 {
14258 struct glyph *start = row->glyphs[TEXT_AREA];
14259 struct glyph *glyph = start + used - 1;
14260
ade0bee1
GM
14261 /* Skip over glyphs inserted to display the cursor at the
14262 end of a line, for extending the face of the last glyph
14263 to the end of the line on terminals, and for truncation
14264 and continuation glyphs. */
65008712
GM
14265 while (glyph >= start
14266 && glyph->type == CHAR_GLYPH
65008712 14267 && INTEGERP (glyph->object))
5f5c8ee5
GM
14268 --glyph;
14269
14270 /* If last glyph is a space or stretch, and it's trailing
14271 whitespace, set the face of all trailing whitespace glyphs in
14272 IT->glyph_row to `trailing-whitespace'. */
14273 if (glyph >= start
14274 && BUFFERP (glyph->object)
14275 && (glyph->type == STRETCH_GLYPH
14276 || (glyph->type == CHAR_GLYPH
43d120d8 14277 && glyph->u.ch == ' '))
5f5c8ee5 14278 && trailing_whitespace_p (glyph->charpos))
545e04f6 14279 {
980806b6 14280 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
2311178e 14281
5f5c8ee5
GM
14282 while (glyph >= start
14283 && BUFFERP (glyph->object)
14284 && (glyph->type == STRETCH_GLYPH
14285 || (glyph->type == CHAR_GLYPH
43d120d8
KH
14286 && glyph->u.ch == ' ')))
14287 (glyph--)->face_id = face_id;
545e04f6 14288 }
a2889657 14289 }
5f5c8ee5 14290}
a2889657 14291
5fcbb24d 14292
cafafe0b 14293/* Value is non-zero if glyph row ROW in window W should be
0fd37545 14294 used to hold the cursor. */
cafafe0b
GM
14295
14296static int
14297cursor_row_p (w, row)
14298 struct window *w;
14299 struct glyph_row *row;
14300{
9a038881 14301 int cursor_row_p = 1;
2311178e 14302
cafafe0b
GM
14303 if (PT == MATRIX_ROW_END_CHARPOS (row))
14304 {
9a038881
GM
14305 /* If the row ends with a newline from a string, we don't want
14306 the cursor there (if the row is continued it doesn't end in a
14307 newline). */
14308 if (CHARPOS (row->end.string_pos) >= 0
14309 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14310 cursor_row_p = row->continued_p;
14311
14312 /* If the row ends at ZV, display the cursor at the end of that
14313 row instead of at the start of the row below. */
14314 else if (row->ends_at_zv_p)
14315 cursor_row_p = 1;
14316 else
14317 cursor_row_p = 0;
cafafe0b
GM
14318 }
14319
9a038881 14320 return cursor_row_p;
cafafe0b
GM
14321}
14322
14323
5f5c8ee5
GM
14324/* Construct the glyph row IT->glyph_row in the desired matrix of
14325 IT->w from text at the current position of IT. See dispextern.h
14326 for an overview of struct it. Value is non-zero if
14327 IT->glyph_row displays text, as opposed to a line displaying ZV
14328 only. */
2311178e 14329
5f5c8ee5
GM
14330static int
14331display_line (it)
14332 struct it *it;
14333{
14334 struct glyph_row *row = it->glyph_row;
14335
14336 /* We always start displaying at hpos zero even if hscrolled. */
14337 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 14338
5f5c8ee5
GM
14339 /* We must not display in a row that's not a text row. */
14340 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14341 < it->w->desired_matrix->nrows);
12adba34 14342
5f5c8ee5
GM
14343 /* Is IT->w showing the region? */
14344 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 14345
5f5c8ee5
GM
14346 /* Clear the result glyph row and enable it. */
14347 prepare_desired_row (row);
12adba34 14348
5f5c8ee5 14349 row->y = it->current_y;
29b3ea63 14350 row->start = it->start;
5f5c8ee5
GM
14351 row->continuation_lines_width = it->continuation_lines_width;
14352 row->displays_text_p = 1;
91004049
GM
14353 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14354 it->starts_in_middle_of_char_p = 0;
5f5c8ee5
GM
14355
14356 /* Arrange the overlays nicely for our purposes. Usually, we call
14357 display_line on only one line at a time, in which case this
14358 can't really hurt too much, or we call it on lines which appear
14359 one after another in the buffer, in which case all calls to
14360 recenter_overlay_lists but the first will be pretty cheap. */
14361 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14362
5f5c8ee5
GM
14363 /* Move over display elements that are not visible because we are
14364 hscrolled. This may stop at an x-position < IT->first_visible_x
14365 if the first glyph is partially visible or if we hit a line end. */
14366 if (it->current_x < it->first_visible_x)
14367 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14368 MOVE_TO_POS | MOVE_TO_X);
14369
14370 /* Get the initial row height. This is either the height of the
14371 text hscrolled, if there is any, or zero. */
14372 row->ascent = it->max_ascent;
14373 row->height = it->max_ascent + it->max_descent;
312246d1
GM
14374 row->phys_ascent = it->max_phys_ascent;
14375 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
14376
14377 /* Loop generating characters. The loop is left with IT on the next
14378 character to display. */
14379 while (1)
14380 {
14381 int n_glyphs_before, hpos_before, x_before;
14382 int x, i, nglyphs;
ae26e27d 14383 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
b28cb6ed 14384
5f5c8ee5
GM
14385 /* Retrieve the next thing to display. Value is zero if end of
14386 buffer reached. */
14387 if (!get_next_display_element (it))
14388 {
14389 /* Maybe add a space at the end of this line that is used to
1b335865
GM
14390 display the cursor there under X. Set the charpos of the
14391 first glyph of blank lines not corresponding to any text
14392 to -1. */
14393 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
14394 || row->used[TEXT_AREA] == 0)
a2889657 14395 {
5f5c8ee5
GM
14396 row->glyphs[TEXT_AREA]->charpos = -1;
14397 row->displays_text_p = 0;
14398
2ad551af 14399 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
e6b70fd8
GM
14400 && (!MINI_WINDOW_P (it->w)
14401 || (minibuf_level && EQ (it->window, minibuf_window))))
5f5c8ee5 14402 row->indicate_empty_line_p = 1;
a2889657 14403 }
2311178e 14404
5f5c8ee5
GM
14405 it->continuation_lines_width = 0;
14406 row->ends_at_zv_p = 1;
14407 break;
a2889657 14408 }
a2889657 14409
5f5c8ee5
GM
14410 /* Now, get the metrics of what we want to display. This also
14411 generates glyphs in `row' (which is IT->glyph_row). */
14412 n_glyphs_before = row->used[TEXT_AREA];
14413 x = it->current_x;
e6819faf
GM
14414
14415 /* Remember the line height so far in case the next element doesn't
14416 fit on the line. */
14417 if (!it->truncate_lines_p)
14418 {
14419 ascent = it->max_ascent;
14420 descent = it->max_descent;
14421 phys_ascent = it->max_phys_ascent;
14422 phys_descent = it->max_phys_descent;
14423 }
2311178e 14424
5f5c8ee5 14425 PRODUCE_GLYPHS (it);
a2889657 14426
5f5c8ee5
GM
14427 /* If this display element was in marginal areas, continue with
14428 the next one. */
14429 if (it->area != TEXT_AREA)
a2889657 14430 {
5f5c8ee5
GM
14431 row->ascent = max (row->ascent, it->max_ascent);
14432 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14433 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14434 row->phys_height = max (row->phys_height,
14435 it->max_phys_ascent + it->max_phys_descent);
cafafe0b 14436 set_iterator_to_next (it, 1);
5f5c8ee5
GM
14437 continue;
14438 }
5936754e 14439
5f5c8ee5
GM
14440 /* Does the display element fit on the line? If we truncate
14441 lines, we should draw past the right edge of the window. If
14442 we don't truncate, we want to stop so that we can display the
14443 continuation glyph before the right margin. If lines are
14444 continued, there are two possible strategies for characters
14445 resulting in more than 1 glyph (e.g. tabs): Display as many
14446 glyphs as possible in this line and leave the rest for the
14447 continuation line, or display the whole element in the next
14448 line. Original redisplay did the former, so we do it also. */
14449 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14450 hpos_before = it->hpos;
14451 x_before = x;
4dcd74e6 14452
4b41cebb 14453 if (/* Not a newline. */
4dcd74e6 14454 nglyphs > 0
202c1b5b 14455 /* Glyphs produced fit entirely in the line. */
4dcd74e6
GM
14456 && it->current_x < it->last_visible_x)
14457 {
37be86f2 14458 it->hpos += nglyphs;
5f5c8ee5
GM
14459 row->ascent = max (row->ascent, it->max_ascent);
14460 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14461 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14462 row->phys_height = max (row->phys_height,
14463 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
14464 if (it->current_x - it->pixel_width < it->first_visible_x)
14465 row->x = x - it->first_visible_x;
14466 }
14467 else
14468 {
14469 int new_x;
14470 struct glyph *glyph;
2311178e 14471
5f5c8ee5 14472 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 14473 {
5f5c8ee5
GM
14474 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14475 new_x = x + glyph->pixel_width;
14476
14477 if (/* Lines are continued. */
14478 !it->truncate_lines_p
14479 && (/* Glyph doesn't fit on the line. */
14480 new_x > it->last_visible_x
14481 /* Or it fits exactly on a window system frame. */
14482 || (new_x == it->last_visible_x
14483 && FRAME_WINDOW_P (it->f))))
a2889657 14484 {
5f5c8ee5 14485 /* End of a continued line. */
2311178e 14486
5f5c8ee5
GM
14487 if (it->hpos == 0
14488 || (new_x == it->last_visible_x
14489 && FRAME_WINDOW_P (it->f)))
14490 {
e6819faf
GM
14491 /* Current glyph is the only one on the line or
14492 fits exactly on the line. We must continue
14493 the line because we can't draw the cursor
14494 after the glyph. */
5f5c8ee5
GM
14495 row->continued_p = 1;
14496 it->current_x = new_x;
14497 it->continuation_lines_width += new_x;
14498 ++it->hpos;
14499 if (i == nglyphs - 1)
cafafe0b 14500 set_iterator_to_next (it, 1);
5f5c8ee5 14501 }
3b3c4bf0
GM
14502 else if (CHAR_GLYPH_PADDING_P (*glyph)
14503 && !FRAME_WINDOW_P (it->f))
14504 {
14505 /* A padding glyph that doesn't fit on this line.
14506 This means the whole character doesn't fit
14507 on the line. */
14508 row->used[TEXT_AREA] = n_glyphs_before;
2311178e 14509
3b3c4bf0
GM
14510 /* Fill the rest of the row with continuation
14511 glyphs like in 20.x. */
14512 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14513 < row->glyphs[1 + TEXT_AREA])
14514 produce_special_glyphs (it, IT_CONTINUATION);
2311178e 14515
3b3c4bf0
GM
14516 row->continued_p = 1;
14517 it->current_x = x_before;
14518 it->continuation_lines_width += x_before;
2311178e 14519
3b3c4bf0
GM
14520 /* Restore the height to what it was before the
14521 element not fitting on the line. */
14522 it->max_ascent = ascent;
14523 it->max_descent = descent;
14524 it->max_phys_ascent = phys_ascent;
14525 it->max_phys_descent = phys_descent;
14526 }
0df56f4d
GM
14527 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14528 {
14529 /* A TAB that extends past the right edge of the
14530 window. This produces a single glyph on
14531 window system frames. We leave the glyph in
14532 this row and let it fill the row, but don't
14533 consume the TAB. */
14534 it->continuation_lines_width += it->last_visible_x;
14535 row->ends_in_middle_of_char_p = 1;
14536 row->continued_p = 1;
14537 glyph->pixel_width = it->last_visible_x - x;
14538 it->starts_in_middle_of_char_p = 1;
14539 }
5f5c8ee5 14540 else
5936754e 14541 {
0df56f4d
GM
14542 /* Something other than a TAB that draws past
14543 the right edge of the window. Restore
14544 positions to values before the element. */
5f5c8ee5 14545 row->used[TEXT_AREA] = n_glyphs_before + i;
2311178e 14546
5f5c8ee5
GM
14547 /* Display continuation glyphs. */
14548 if (!FRAME_WINDOW_P (it->f))
14549 produce_special_glyphs (it, IT_CONTINUATION);
14550 row->continued_p = 1;
653c329b 14551
0df56f4d 14552 it->continuation_lines_width += x;
2311178e 14553
91004049
GM
14554 if (nglyphs > 1 && i > 0)
14555 {
14556 row->ends_in_middle_of_char_p = 1;
14557 it->starts_in_middle_of_char_p = 1;
14558 }
2311178e 14559
e6819faf
GM
14560 /* Restore the height to what it was before the
14561 element not fitting on the line. */
14562 it->max_ascent = ascent;
14563 it->max_descent = descent;
14564 it->max_phys_ascent = phys_ascent;
14565 it->max_phys_descent = phys_descent;
5936754e 14566 }
e6819faf 14567
5f5c8ee5
GM
14568 break;
14569 }
14570 else if (new_x > it->first_visible_x)
14571 {
14572 /* Increment number of glyphs actually displayed. */
14573 ++it->hpos;
2311178e 14574
5f5c8ee5
GM
14575 if (x < it->first_visible_x)
14576 /* Glyph is partially visible, i.e. row starts at
14577 negative X position. */
14578 row->x = x - it->first_visible_x;
14579 }
14580 else
14581 {
14582 /* Glyph is completely off the left margin of the
14583 window. This should not happen because of the
72b8c434
RS
14584 move_it_in_display_line at the start of this
14585 function, unless the text display area of the
14586 window is empty. */
14587 xassert (it->first_visible_x <= it->last_visible_x);
a2889657 14588 }
a2889657 14589 }
2311178e 14590
5f5c8ee5
GM
14591 row->ascent = max (row->ascent, it->max_ascent);
14592 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14593 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14594 row->phys_height = max (row->phys_height,
14595 it->max_phys_ascent + it->max_phys_descent);
2311178e 14596
5f5c8ee5
GM
14597 /* End of this display line if row is continued. */
14598 if (row->continued_p)
14599 break;
a2889657 14600 }
a2889657 14601
5f5c8ee5
GM
14602 /* Is this a line end? If yes, we're also done, after making
14603 sure that a non-default face is extended up to the right
14604 margin of the window. */
14605 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 14606 {
5f5c8ee5
GM
14607 int used_before = row->used[TEXT_AREA];
14608
e74fb0f7
GM
14609 row->ends_in_newline_from_string_p = STRINGP (it->object);
14610
5f5c8ee5
GM
14611 /* Add a space at the end of the line that is used to
14612 display the cursor there. */
14613 append_space (it, 0);
2311178e 14614
5f5c8ee5
GM
14615 /* Extend the face to the end of the line. */
14616 extend_face_to_end_of_line (it);
14617
14618 /* Make sure we have the position. */
14619 if (used_before == 0)
14620 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
2311178e 14621
5f5c8ee5 14622 /* Consume the line end. This skips over invisible lines. */
cafafe0b 14623 set_iterator_to_next (it, 1);
5f5c8ee5
GM
14624 it->continuation_lines_width = 0;
14625 break;
1c9241f5 14626 }
a2889657 14627
2311178e 14628 /* Proceed with next display element. Note that this skips
5f5c8ee5 14629 over lines invisible because of selective display. */
cafafe0b 14630 set_iterator_to_next (it, 1);
b1d1124b 14631
5f5c8ee5
GM
14632 /* If we truncate lines, we are done when the last displayed
14633 glyphs reach past the right margin of the window. */
14634 if (it->truncate_lines_p
14635 && (FRAME_WINDOW_P (it->f)
14636 ? (it->current_x >= it->last_visible_x)
14637 : (it->current_x > it->last_visible_x)))
75d13c64 14638 {
5f5c8ee5
GM
14639 /* Maybe add truncation glyphs. */
14640 if (!FRAME_WINDOW_P (it->f))
14641 {
a98b5ed9 14642 int i, n;
2311178e 14643
a98b5ed9
GM
14644 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14645 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14646 break;
14647
14648 for (n = row->used[TEXT_AREA]; i < n; ++i)
14649 {
14650 row->used[TEXT_AREA] = i;
14651 produce_special_glyphs (it, IT_TRUNCATION);
14652 }
5f5c8ee5 14653 }
2311178e 14654
5f5c8ee5
GM
14655 row->truncated_on_right_p = 1;
14656 it->continuation_lines_width = 0;
312246d1 14657 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
14658 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14659 it->hpos = hpos_before;
14660 it->current_x = x_before;
14661 break;
75d13c64 14662 }
a2889657 14663 }
a2889657 14664
5f5c8ee5
GM
14665 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14666 at the left window margin. */
14667 if (it->first_visible_x
14668 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14669 {
14670 if (!FRAME_WINDOW_P (it->f))
14671 insert_left_trunc_glyphs (it);
14672 row->truncated_on_left_p = 1;
14673 }
a2889657 14674
5f5c8ee5
GM
14675 /* If the start of this line is the overlay arrow-position, then
14676 mark this glyph row as the one containing the overlay arrow.
14677 This is clearly a mess with variable size fonts. It would be
14678 better to let it be displayed like cursors under X. */
e24c997d 14679 if (MARKERP (Voverlay_arrow_position)
a2889657 14680 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
14681 && (MATRIX_ROW_START_CHARPOS (row)
14682 == marker_position (Voverlay_arrow_position))
e24c997d 14683 && STRINGP (Voverlay_arrow_string)
a2889657
JB
14684 && ! overlay_arrow_seen)
14685 {
b46952ae 14686 /* Overlay arrow in window redisplay is a fringe bitmap. */
5f5c8ee5 14687 if (!FRAME_WINDOW_P (it->f))
c4628384 14688 {
5f5c8ee5
GM
14689 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
14690 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14691 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14692 struct glyph *p = row->glyphs[TEXT_AREA];
14693 struct glyph *p2, *end;
14694
14695 /* Copy the arrow glyphs. */
14696 while (glyph < arrow_end)
14697 *p++ = *glyph++;
14698
14699 /* Throw away padding glyphs. */
14700 p2 = p;
14701 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14702 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14703 ++p2;
14704 if (p2 > p)
212e4f87 14705 {
5f5c8ee5
GM
14706 while (p2 < end)
14707 *p++ = *p2++;
14708 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 14709 }
c4628384 14710 }
2311178e 14711
a2889657 14712 overlay_arrow_seen = 1;
5f5c8ee5 14713 row->overlay_arrow_p = 1;
a2889657
JB
14714 }
14715
5f5c8ee5
GM
14716 /* Compute pixel dimensions of this line. */
14717 compute_line_metrics (it);
14718
14719 /* Remember the position at which this line ends. */
14720 row->end = it->current;
14721
173cbca8 14722 /* Maybe set the cursor. */
5f5c8ee5
GM
14723 if (it->w->cursor.vpos < 0
14724 && PT >= MATRIX_ROW_START_CHARPOS (row)
cafafe0b
GM
14725 && PT <= MATRIX_ROW_END_CHARPOS (row)
14726 && cursor_row_p (it->w, row))
14727 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
14728
14729 /* Highlight trailing whitespace. */
8f897821 14730 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
14731 highlight_trailing_whitespace (it->f, it->glyph_row);
14732
14733 /* Prepare for the next line. This line starts horizontally at (X
14734 HPOS) = (0 0). Vertical positions are incremented. As a
14735 convenience for the caller, IT->glyph_row is set to the next
14736 row to be used. */
14737 it->current_x = it->hpos = 0;
14738 it->current_y += row->height;
14739 ++it->vpos;
14740 ++it->glyph_row;
29b3ea63 14741 it->start = it->current;
5f5c8ee5 14742 return row->displays_text_p;
a2889657 14743}
5f5c8ee5
GM
14744
14745
a2889657 14746\f
5f5c8ee5
GM
14747/***********************************************************************
14748 Menu Bar
14749 ***********************************************************************/
14750
14751/* Redisplay the menu bar in the frame for window W.
14752
14753 The menu bar of X frames that don't have X toolkit support is
14754 displayed in a special window W->frame->menu_bar_window.
2311178e 14755
5f5c8ee5
GM
14756 The menu bar of terminal frames is treated specially as far as
14757 glyph matrices are concerned. Menu bar lines are not part of
14758 windows, so the update is done directly on the frame matrix rows
14759 for the menu bar. */
7ce2c095
RS
14760
14761static void
14762display_menu_bar (w)
14763 struct window *w;
14764{
5f5c8ee5
GM
14765 struct frame *f = XFRAME (WINDOW_FRAME (w));
14766 struct it it;
14767 Lisp_Object items;
8351baf2 14768 int i;
7ce2c095 14769
5f5c8ee5 14770 /* Don't do all this for graphical frames. */
dc937613 14771#ifdef HAVE_NTGUI
d129c4c2
KH
14772 if (!NILP (Vwindow_system))
14773 return;
dc937613 14774#endif
488dd4c4 14775#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
d3413a53 14776 if (FRAME_X_P (f))
7ce2c095 14777 return;
5f5c8ee5 14778#endif
e0f712ba 14779#ifdef MAC_OS
1a578e9b
AC
14780 if (FRAME_MAC_P (f))
14781 return;
14782#endif
5f5c8ee5
GM
14783
14784#ifdef USE_X_TOOLKIT
14785 xassert (!FRAME_WINDOW_P (f));
52377a47 14786 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5 14787 it.first_visible_x = 0;
da8b7f4f 14788 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
5f5c8ee5
GM
14789#else /* not USE_X_TOOLKIT */
14790 if (FRAME_WINDOW_P (f))
14791 {
14792 /* Menu bar lines are displayed in the desired matrix of the
14793 dummy window menu_bar_window. */
14794 struct window *menu_w;
14795 xassert (WINDOWP (f->menu_bar_window));
14796 menu_w = XWINDOW (f->menu_bar_window);
14797 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 14798 MENU_FACE_ID);
5f5c8ee5 14799 it.first_visible_x = 0;
da8b7f4f 14800 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
5f5c8ee5
GM
14801 }
14802 else
14803 {
14804 /* This is a TTY frame, i.e. character hpos/vpos are used as
14805 pixel x/y. */
14806 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 14807 MENU_FACE_ID);
5f5c8ee5 14808 it.first_visible_x = 0;
da8b7f4f 14809 it.last_visible_x = FRAME_COLS (f);
5f5c8ee5
GM
14810 }
14811#endif /* not USE_X_TOOLKIT */
14812
1862a24e
MB
14813 if (! mode_line_inverse_video)
14814 /* Force the menu-bar to be displayed in the default face. */
14815 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14816
5f5c8ee5
GM
14817 /* Clear all rows of the menu bar. */
14818 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14819 {
14820 struct glyph_row *row = it.glyph_row + i;
14821 clear_glyph_row (row);
14822 row->enabled_p = 1;
14823 row->full_width_p = 1;
14824 }
7ce2c095 14825
5f5c8ee5
GM
14826 /* Display all items of the menu bar. */
14827 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 14828 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 14829 {
5f5c8ee5
GM
14830 Lisp_Object string;
14831
14832 /* Stop at nil string. */
a61b7058 14833 string = AREF (items, i + 1);
8351baf2
RS
14834 if (NILP (string))
14835 break;
2d66ad19 14836
5f5c8ee5 14837 /* Remember where item was displayed. */
a61b7058 14838 AREF (items, i + 3) = make_number (it.hpos);
7ce2c095 14839
5f5c8ee5
GM
14840 /* Display the item, pad with one space. */
14841 if (it.current_x < it.last_visible_x)
14842 display_string (NULL, string, Qnil, 0, 0, &it,
2051c264 14843 SCHARS (string) + 1, 0, 0, -1);
7ce2c095
RS
14844 }
14845
2d66ad19 14846 /* Fill out the line with spaces. */
5f5c8ee5
GM
14847 if (it.current_x < it.last_visible_x)
14848 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 14849
5f5c8ee5
GM
14850 /* Compute the total height of the lines. */
14851 compute_line_metrics (&it);
7ce2c095 14852}
5f5c8ee5
GM
14853
14854
7ce2c095 14855\f
5f5c8ee5
GM
14856/***********************************************************************
14857 Mode Line
14858 ***********************************************************************/
14859
715e84c9
GM
14860/* Redisplay mode lines in the window tree whose root is WINDOW. If
14861 FORCE is non-zero, redisplay mode lines unconditionally.
14862 Otherwise, redisplay only mode lines that are garbaged. Value is
14863 the number of windows whose mode lines were redisplayed. */
a2889657 14864
715e84c9
GM
14865static int
14866redisplay_mode_lines (window, force)
14867 Lisp_Object window;
14868 int force;
14869{
14870 int nwindows = 0;
2311178e 14871
715e84c9
GM
14872 while (!NILP (window))
14873 {
14874 struct window *w = XWINDOW (window);
2311178e 14875
715e84c9
GM
14876 if (WINDOWP (w->hchild))
14877 nwindows += redisplay_mode_lines (w->hchild, force);
14878 else if (WINDOWP (w->vchild))
14879 nwindows += redisplay_mode_lines (w->vchild, force);
14880 else if (force
14881 || FRAME_GARBAGED_P (XFRAME (w->frame))
14882 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14883 {
715e84c9
GM
14884 struct text_pos lpoint;
14885 struct buffer *old = current_buffer;
14886
14887 /* Set the window's buffer for the mode line display. */
14888 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14889 set_buffer_internal_1 (XBUFFER (w->buffer));
2311178e 14890
715e84c9
GM
14891 /* Point refers normally to the selected window. For any
14892 other window, set up appropriate value. */
14893 if (!EQ (window, selected_window))
14894 {
14895 struct text_pos pt;
2311178e 14896
715e84c9
GM
14897 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
14898 if (CHARPOS (pt) < BEGV)
14899 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14900 else if (CHARPOS (pt) > (ZV - 1))
14901 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
14902 else
14903 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
14904 }
14905
715e84c9
GM
14906 /* Display mode lines. */
14907 clear_glyph_matrix (w->desired_matrix);
14908 if (display_mode_lines (w))
14909 {
14910 ++nwindows;
14911 w->must_be_updated_p = 1;
14912 }
14913
14914 /* Restore old settings. */
715e84c9
GM
14915 set_buffer_internal_1 (old);
14916 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14917 }
14918
14919 window = w->next;
14920 }
14921
14922 return nwindows;
14923}
14924
14925
14926/* Display the mode and/or top line of window W. Value is the number
14927 of mode lines displayed. */
14928
14929static int
5f5c8ee5 14930display_mode_lines (w)
a2889657
JB
14931 struct window *w;
14932{
edd1e654 14933 Lisp_Object old_selected_window, old_selected_frame;
715e84c9 14934 int n = 0;
edd1e654
GM
14935
14936 old_selected_frame = selected_frame;
14937 selected_frame = w->frame;
14938 old_selected_window = selected_window;
14939 XSETWINDOW (selected_window, w);
2311178e 14940
5f5c8ee5 14941 /* These will be set while the mode line specs are processed. */
aa6d10fa 14942 line_number_displayed = 0;
155ef550 14943 w->column_number_displayed = Qnil;
aa6d10fa 14944
5f5c8ee5 14945 if (WINDOW_WANTS_MODELINE_P (w))
715e84c9 14946 {
c96e83bf 14947 struct window *sel_w = XWINDOW (old_selected_window);
f87c0a98 14948
96d2320f 14949 /* Select mode line face based on the real selected window. */
c96e83bf 14950 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
715e84c9
GM
14951 current_buffer->mode_line_format);
14952 ++n;
14953 }
2311178e 14954
045dee35 14955 if (WINDOW_WANTS_HEADER_LINE_P (w))
715e84c9
GM
14956 {
14957 display_mode_line (w, HEADER_LINE_FACE_ID,
14958 current_buffer->header_line_format);
14959 ++n;
14960 }
14961
edd1e654
GM
14962 selected_frame = old_selected_frame;
14963 selected_window = old_selected_window;
715e84c9 14964 return n;
5f5c8ee5 14965}
03b294dc 14966
03b294dc 14967
5f5c8ee5 14968/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 14969 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
04612a64
GM
14970 FORMAT is the mode line format to display. Value is the pixel
14971 height of the mode line displayed. */
03b294dc 14972
04612a64 14973static int
5f5c8ee5
GM
14974display_mode_line (w, face_id, format)
14975 struct window *w;
14976 enum face_id face_id;
14977 Lisp_Object format;
14978{
14979 struct it it;
14980 struct face *face;
03b294dc 14981
5f5c8ee5
GM
14982 init_iterator (&it, w, -1, -1, NULL, face_id);
14983 prepare_desired_row (it.glyph_row);
14984
1862a24e
MB
14985 if (! mode_line_inverse_video)
14986 /* Force the mode-line to be displayed in the default face. */
14987 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14988
5f5c8ee5
GM
14989 /* Temporarily make frame's keyboard the current kboard so that
14990 kboard-local variables in the mode_line_format will get the right
14991 values. */
14992 push_frame_kboard (it.f);
c53a1624 14993 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
5f5c8ee5 14994 pop_frame_kboard ();
a2889657 14995
5f5c8ee5
GM
14996 /* Fill up with spaces. */
14997 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
2311178e 14998
5f5c8ee5
GM
14999 compute_line_metrics (&it);
15000 it.glyph_row->full_width_p = 1;
15001 it.glyph_row->mode_line_p = 1;
5f5c8ee5
GM
15002 it.glyph_row->continued_p = 0;
15003 it.glyph_row->truncated_on_left_p = 0;
15004 it.glyph_row->truncated_on_right_p = 0;
15005
15006 /* Make a 3D mode-line have a shadow at its right end. */
15007 face = FACE_FROM_ID (it.f, face_id);
15008 extend_face_to_end_of_line (&it);
15009 if (face->box != FACE_NO_BOX)
d7eb09a0 15010 {
5f5c8ee5
GM
15011 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15012 + it.glyph_row->used[TEXT_AREA] - 1);
15013 last->right_box_line_p = 1;
d7eb09a0 15014 }
04612a64
GM
15015
15016 return it.glyph_row->height;
a2889657
JB
15017}
15018
0fcf414f
RS
15019/* Alist that caches the results of :propertize.
15020 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15021Lisp_Object mode_line_proptrans_alist;
a2889657 15022
fec8f23e
KS
15023/* List of strings making up the mode-line. */
15024Lisp_Object mode_line_string_list;
15025
15026/* Base face property when building propertized mode line string. */
15027static Lisp_Object mode_line_string_face;
15028static Lisp_Object mode_line_string_face_prop;
15029
15030
5f5c8ee5
GM
15031/* Contribute ELT to the mode line for window IT->w. How it
15032 translates into text depends on its data type.
a2889657 15033
5f5c8ee5 15034 IT describes the display environment in which we display, as usual.
a2889657
JB
15035
15036 DEPTH is the depth in recursion. It is used to prevent
15037 infinite recursion here.
15038
5f5c8ee5
GM
15039 FIELD_WIDTH is the number of characters the display of ELT should
15040 occupy in the mode line, and PRECISION is the maximum number of
15041 characters to display from ELT's representation. See
b8523839 15042 display_string for details.
a2889657 15043
c53a1624
RS
15044 Returns the hpos of the end of the text generated by ELT.
15045
15046 PROPS is a property list to add to any string we encounter.
15047
7d0393cf 15048 If RISKY is nonzero, remove (disregard) any properties in any string
93da8435
SM
15049 we encounter, and ignore :eval and :propertize.
15050
15051 If the global variable `frame_title_ptr' is non-NULL, then the output
15052 is passed to `store_frame_title' instead of `display_string'. */
a2889657
JB
15053
15054static int
c53a1624 15055display_mode_element (it, depth, field_width, precision, elt, props, risky)
5f5c8ee5 15056 struct it *it;
a2889657 15057 int depth;
5f5c8ee5 15058 int field_width, precision;
0fcf414f 15059 Lisp_Object elt, props;
c53a1624 15060 int risky;
a2889657 15061{
5f5c8ee5 15062 int n = 0, field, prec;
0fcf414f 15063 int literal = 0;
5f5c8ee5 15064
a2889657 15065 tail_recurse:
1a89be1e
SM
15066 if (depth > 100)
15067 elt = build_string ("*too-deep*");
a2889657
JB
15068
15069 depth++;
15070
0220c518 15071 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
15072 {
15073 case Lisp_String:
15074 {
15075 /* A string: output it and check for %-constructs within it. */
5f5c8ee5 15076 unsigned char c;
50f80c2f 15077 const unsigned char *this, *lisp_string;
5f5c8ee5 15078
c53a1624 15079 if (!NILP (props) || risky)
0fcf414f
RS
15080 {
15081 Lisp_Object oprops, aelt;
15082 oprops = Ftext_properties_at (make_number (0), elt);
c53a1624
RS
15083
15084 if (NILP (Fequal (props, oprops)) || risky)
0fcf414f 15085 {
ae02e06a
RS
15086 /* If the starting string has properties,
15087 merge the specified ones onto the existing ones. */
c53a1624 15088 if (! NILP (oprops) && !risky)
ae02e06a
RS
15089 {
15090 Lisp_Object tem;
15091
15092 oprops = Fcopy_sequence (oprops);
15093 tem = props;
15094 while (CONSP (tem))
15095 {
15096 oprops = Fplist_put (oprops, XCAR (tem),
15097 XCAR (XCDR (tem)));
15098 tem = XCDR (XCDR (tem));
15099 }
15100 props = oprops;
15101 }
15102
0fcf414f
RS
15103 aelt = Fassoc (elt, mode_line_proptrans_alist);
15104 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
adb63af1
RS
15105 {
15106 mode_line_proptrans_alist
15107 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15108 elt = XCAR (aelt);
15109 }
0fcf414f
RS
15110 else
15111 {
adb63af1
RS
15112 Lisp_Object tem;
15113
0fcf414f 15114 elt = Fcopy_sequence (elt);
dc3b2c8b
SM
15115 Fset_text_properties (make_number (0), Flength (elt),
15116 props, elt);
adb63af1 15117 /* Add this item to mode_line_proptrans_alist. */
0fcf414f
RS
15118 mode_line_proptrans_alist
15119 = Fcons (Fcons (elt, props),
15120 mode_line_proptrans_alist);
adb63af1
RS
15121 /* Truncate mode_line_proptrans_alist
15122 to at most 50 elements. */
15123 tem = Fnthcdr (make_number (50),
15124 mode_line_proptrans_alist);
15125 if (! NILP (tem))
15126 XSETCDR (tem, Qnil);
0fcf414f
RS
15127 }
15128 }
15129 }
15130
2051c264 15131 this = SDATA (elt);
ae02e06a
RS
15132 lisp_string = this;
15133
0fcf414f
RS
15134 if (literal)
15135 {
15136 prec = precision - n;
15137 if (frame_title_ptr)
2051c264 15138 n += store_frame_title (SDATA (elt), -1, prec);
fec8f23e
KS
15139 else if (!NILP (mode_line_string_list))
15140 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
0fcf414f
RS
15141 else
15142 n += display_string (NULL, elt, Qnil, 0, 0, it,
15143 0, prec, 0, STRING_MULTIBYTE (elt));
15144
15145 break;
15146 }
15147
5f5c8ee5
GM
15148 while ((precision <= 0 || n < precision)
15149 && *this
15150 && (frame_title_ptr
fec8f23e 15151 || !NILP (mode_line_string_list)
5f5c8ee5 15152 || it->current_x < it->last_visible_x))
a2889657 15153 {
50f80c2f 15154 const unsigned char *last = this;
5f5c8ee5
GM
15155
15156 /* Advance to end of string or next format specifier. */
a2889657
JB
15157 while ((c = *this++) != '\0' && c != '%')
15158 ;
2311178e 15159
a2889657
JB
15160 if (this - 1 != last)
15161 {
5f5c8ee5
GM
15162 /* Output to end of string or up to '%'. Field width
15163 is length of string. Don't output more than
15164 PRECISION allows us. */
d26b89b8 15165 --this;
eaaa65b0
GM
15166
15167 prec = chars_in_text (last, this - last);
5f5c8ee5
GM
15168 if (precision > 0 && prec > precision - n)
15169 prec = precision - n;
2311178e 15170
d39b6696 15171 if (frame_title_ptr)
d26b89b8 15172 n += store_frame_title (last, 0, prec);
fec8f23e
KS
15173 else if (!NILP (mode_line_string_list))
15174 {
15175 int bytepos = last - lisp_string;
15176 int charpos = string_byte_to_char (elt, bytepos);
15177 n += store_mode_line_string (NULL,
15178 Fsubstring (elt, make_number (charpos),
15179 make_number (charpos + prec)),
15180 0, 0, 0, Qnil);
15181 }
d39b6696 15182 else
eaaa65b0
GM
15183 {
15184 int bytepos = last - lisp_string;
15185 int charpos = string_byte_to_char (elt, bytepos);
15186 n += display_string (NULL, elt, Qnil, 0, charpos,
ca77144d
GM
15187 it, 0, prec, 0,
15188 STRING_MULTIBYTE (elt));
eaaa65b0 15189 }
a2889657
JB
15190 }
15191 else /* c == '%' */
15192 {
50f80c2f 15193 const unsigned char *percent_position = this;
2311178e 15194
5f5c8ee5
GM
15195 /* Get the specified minimum width. Zero means
15196 don't pad. */
15197 field = 0;
a2889657 15198 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 15199 field = field * 10 + c - '0';
a2889657 15200
5f5c8ee5
GM
15201 /* Don't pad beyond the total padding allowed. */
15202 if (field_width - n > 0 && field > field_width - n)
15203 field = field_width - n;
a2889657 15204
5f5c8ee5
GM
15205 /* Note that either PRECISION <= 0 or N < PRECISION. */
15206 prec = precision - n;
2311178e 15207
a2889657 15208 if (c == 'M')
5f5c8ee5 15209 n += display_mode_element (it, depth, field, prec,
c53a1624
RS
15210 Vglobal_mode_string, props,
15211 risky);
a2889657 15212 else if (c != 0)
d39b6696 15213 {
72f62cb5 15214 int multibyte;
ae02e06a
RS
15215 int bytepos, charpos;
15216 unsigned char *spec;
2311178e 15217
ae02e06a
RS
15218 bytepos = percent_position - lisp_string;
15219 charpos = (STRING_MULTIBYTE (elt)
15220 ? string_byte_to_char (elt, bytepos)
15221 : bytepos);
15222
15223 spec
72f62cb5
GM
15224 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15225
d39b6696 15226 if (frame_title_ptr)
5f5c8ee5 15227 n += store_frame_title (spec, field, prec);
fec8f23e
KS
15228 else if (!NILP (mode_line_string_list))
15229 {
15230 int len = strlen (spec);
15231 Lisp_Object tem = make_string (spec, len);
15232 props = Ftext_properties_at (make_number (charpos), elt);
15233 /* Should only keep face property in props */
15234 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15235 }
d39b6696 15236 else
5f5c8ee5 15237 {
ae02e06a 15238 int nglyphs_before, nwritten;
2311178e 15239
72f62cb5 15240 nglyphs_before = it->glyph_row->used[TEXT_AREA];
72f62cb5
GM
15241 nwritten = display_string (spec, Qnil, elt,
15242 charpos, 0, it,
15243 field, prec, 0,
15244 multibyte);
5f5c8ee5
GM
15245
15246 /* Assign to the glyphs written above the
15247 string where the `%x' came from, position
15248 of the `%'. */
15249 if (nwritten > 0)
15250 {
15251 struct glyph *glyph
15252 = (it->glyph_row->glyphs[TEXT_AREA]
15253 + nglyphs_before);
15254 int i;
15255
15256 for (i = 0; i < nwritten; ++i)
15257 {
15258 glyph[i].object = elt;
15259 glyph[i].charpos = charpos;
15260 }
2311178e 15261
5f5c8ee5
GM
15262 n += nwritten;
15263 }
15264 }
d39b6696 15265 }
b8523839
AS
15266 else /* c == 0 */
15267 break;
a2889657
JB
15268 }
15269 }
15270 }
15271 break;
15272
15273 case Lisp_Symbol:
15274 /* A symbol: process the value of the symbol recursively
15275 as if it appeared here directly. Avoid error if symbol void.
15276 Special case: if value of symbol is a string, output the string
15277 literally. */
15278 {
15279 register Lisp_Object tem;
c53a1624
RS
15280
15281 /* If the variable is not marked as risky to set
15282 then its contents are risky to use. */
15283 if (NILP (Fget (elt, Qrisky_local_variable)))
15284 risky = 1;
15285
a2889657 15286 tem = Fboundp (elt);
265a9e55 15287 if (!NILP (tem))
a2889657
JB
15288 {
15289 tem = Fsymbol_value (elt);
15290 /* If value is a string, output that string literally:
15291 don't check for % within it. */
e24c997d 15292 if (STRINGP (tem))
0fcf414f
RS
15293 literal = 1;
15294
15295 if (!EQ (tem, elt))
5f5c8ee5
GM
15296 {
15297 /* Give up right away for nil or t. */
15298 elt = tem;
15299 goto tail_recurse;
15300 }
a2889657
JB
15301 }
15302 }
15303 break;
15304
15305 case Lisp_Cons:
15306 {
15307 register Lisp_Object car, tem;
15308
0fcf414f
RS
15309 /* A cons cell: five distinct cases.
15310 If first element is :eval or :propertize, do something special.
a2889657
JB
15311 If first element is a string or a cons, process all the elements
15312 and effectively concatenate them.
15313 If first element is a negative number, truncate displaying cdr to
15314 at most that many characters. If positive, pad (with spaces)
15315 to at least that many characters.
15316 If first element is a symbol, process the cadr or caddr recursively
15317 according to whether the symbol's value is non-nil or nil. */
9472f927 15318 car = XCAR (elt);
0fcf414f 15319 if (EQ (car, QCeval))
5f5c8ee5
GM
15320 {
15321 /* An element of the form (:eval FORM) means evaluate FORM
15322 and use the result as mode line elements. */
0fcf414f 15323
c53a1624
RS
15324 if (risky)
15325 break;
15326
0fcf414f
RS
15327 if (CONSP (XCDR (elt)))
15328 {
15329 Lisp_Object spec;
15330 spec = safe_eval (XCAR (XCDR (elt)));
15331 n += display_mode_element (it, depth, field_width - n,
c53a1624
RS
15332 precision - n, spec, props,
15333 risky);
0fcf414f
RS
15334 }
15335 }
15336 else if (EQ (car, QCpropertize))
15337 {
c53a1624
RS
15338 /* An element of the form (:propertize ELT PROPS...)
15339 means display ELT but applying properties PROPS. */
15340
15341 if (risky)
15342 break;
15343
0fcf414f 15344 if (CONSP (XCDR (elt)))
c53a1624
RS
15345 n += display_mode_element (it, depth, field_width - n,
15346 precision - n, XCAR (XCDR (elt)),
15347 XCDR (XCDR (elt)), risky);
5f5c8ee5
GM
15348 }
15349 else if (SYMBOLP (car))
a2889657
JB
15350 {
15351 tem = Fboundp (car);
9472f927 15352 elt = XCDR (elt);
e24c997d 15353 if (!CONSP (elt))
a2889657
JB
15354 goto invalid;
15355 /* elt is now the cdr, and we know it is a cons cell.
15356 Use its car if CAR has a non-nil value. */
265a9e55 15357 if (!NILP (tem))
a2889657
JB
15358 {
15359 tem = Fsymbol_value (car);
265a9e55 15360 if (!NILP (tem))
9472f927
GM
15361 {
15362 elt = XCAR (elt);
15363 goto tail_recurse;
15364 }
a2889657
JB
15365 }
15366 /* Symbol's value is nil (or symbol is unbound)
15367 Get the cddr of the original list
15368 and if possible find the caddr and use that. */
9472f927 15369 elt = XCDR (elt);
265a9e55 15370 if (NILP (elt))
a2889657 15371 break;
e24c997d 15372 else if (!CONSP (elt))
a2889657 15373 goto invalid;
9472f927 15374 elt = XCAR (elt);
a2889657
JB
15375 goto tail_recurse;
15376 }
e24c997d 15377 else if (INTEGERP (car))
a2889657
JB
15378 {
15379 register int lim = XINT (car);
9472f927 15380 elt = XCDR (elt);
a2889657 15381 if (lim < 0)
5f5c8ee5
GM
15382 {
15383 /* Negative int means reduce maximum width. */
15384 if (precision <= 0)
15385 precision = -lim;
15386 else
15387 precision = min (precision, -lim);
15388 }
a2889657
JB
15389 else if (lim > 0)
15390 {
15391 /* Padding specified. Don't let it be more than
15392 current maximum. */
5f5c8ee5
GM
15393 if (precision > 0)
15394 lim = min (precision, lim);
15395
a2889657
JB
15396 /* If that's more padding than already wanted, queue it.
15397 But don't reduce padding already specified even if
15398 that is beyond the current truncation point. */
5f5c8ee5 15399 field_width = max (lim, field_width);
a2889657
JB
15400 }
15401 goto tail_recurse;
15402 }
e24c997d 15403 else if (STRINGP (car) || CONSP (car))
a2889657
JB
15404 {
15405 register int limit = 50;
5f5c8ee5
GM
15406 /* Limit is to protect against circular lists. */
15407 while (CONSP (elt)
15408 && --limit > 0
15409 && (precision <= 0 || n < precision))
a2889657 15410 {
5f5c8ee5 15411 n += display_mode_element (it, depth, field_width - n,
c53a1624
RS
15412 precision - n, XCAR (elt),
15413 props, risky);
9472f927 15414 elt = XCDR (elt);
a2889657
JB
15415 }
15416 }
15417 }
15418 break;
15419
15420 default:
15421 invalid:
1a89be1e
SM
15422 elt = build_string ("*invalid*");
15423 goto tail_recurse;
a2889657
JB
15424 }
15425
5f5c8ee5
GM
15426 /* Pad to FIELD_WIDTH. */
15427 if (field_width > 0 && n < field_width)
15428 {
15429 if (frame_title_ptr)
15430 n += store_frame_title ("", field_width - n, 0);
fec8f23e
KS
15431 else if (!NILP (mode_line_string_list))
15432 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
5f5c8ee5
GM
15433 else
15434 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15435 0, 0, 0);
15436 }
2311178e 15437
5f5c8ee5 15438 return n;
a2889657 15439}
5f5c8ee5 15440
fec8f23e
KS
15441/* Store a mode-line string element in mode_line_string_list.
15442
15443 If STRING is non-null, display that C string. Otherwise, the Lisp
15444 string LISP_STRING is displayed.
15445
15446 FIELD_WIDTH is the minimum number of output glyphs to produce.
15447 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15448 with spaces. FIELD_WIDTH <= 0 means don't pad.
15449
15450 PRECISION is the maximum number of characters to output from
15451 STRING. PRECISION <= 0 means don't truncate the string.
15452
15453 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15454 properties to the string.
15455
7d0393cf 15456 PROPS are the properties to add to the string.
fec8f23e
KS
15457 The mode_line_string_face face property is always added to the string.
15458 */
15459
15460static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15461 char *string;
15462 Lisp_Object lisp_string;
15463 int copy_string;
15464 int field_width;
15465 int precision;
15466 Lisp_Object props;
15467{
15468 int len;
15469 int n = 0;
15470
15471 if (string != NULL)
15472 {
15473 len = strlen (string);
15474 if (precision > 0 && len > precision)
15475 len = precision;
15476 lisp_string = make_string (string, len);
15477 if (NILP (props))
15478 props = mode_line_string_face_prop;
15479 else if (!NILP (mode_line_string_face))
15480 {
15481 Lisp_Object face = Fplist_get (props, Qface);
15482 props = Fcopy_sequence (props);
15483 if (NILP (face))
15484 face = mode_line_string_face;
15485 else
15486 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15487 props = Fplist_put (props, Qface, face);
15488 }
15489 Fadd_text_properties (make_number (0), make_number (len),
15490 props, lisp_string);
15491 }
7d0393cf 15492 else
fec8f23e 15493 {
c8224325 15494 len = XFASTINT (Flength (lisp_string));
fec8f23e
KS
15495 if (precision > 0 && len > precision)
15496 {
15497 len = precision;
15498 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15499 precision = -1;
15500 }
15501 if (!NILP (mode_line_string_face))
15502 {
15503 Lisp_Object face;
15504 if (NILP (props))
15505 props = Ftext_properties_at (make_number (0), lisp_string);
15506 face = Fplist_get (props, Qface);
15507 if (NILP (face))
15508 face = mode_line_string_face;
15509 else
15510 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15511 props = Fcons (Qface, Fcons (face, Qnil));
15512 if (copy_string)
15513 lisp_string = Fcopy_sequence (lisp_string);
15514 }
15515 if (!NILP (props))
15516 Fadd_text_properties (make_number (0), make_number (len),
15517 props, lisp_string);
15518 }
15519
15520 if (len > 0)
15521 {
15522 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
7d0393cf 15523 n += len;
fec8f23e
KS
15524 }
15525
15526 if (field_width > len)
15527 {
15528 field_width -= len;
15529 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15530 if (!NILP (props))
15531 Fadd_text_properties (make_number (0), make_number (field_width),
15532 props, lisp_string);
15533 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
7d0393cf 15534 n += field_width;
fec8f23e
KS
15535 }
15536
15537 return n;
15538}
15539
5f5c8ee5 15540
8143e6ab 15541DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
fec8f23e 15542 0, 3, 0,
8143e6ab
KS
15543 doc: /* Return the mode-line of selected window as a string.
15544First optional arg FORMAT specifies a different format string (see
8b22c65a 15545`mode-line-format' for details) to use. If FORMAT is t, return
8143e6ab 15546the buffer's header-line. Second optional arg WINDOW specifies a
fec8f23e
KS
15547different window to use as the context for the formatting.
15548If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15549 (format, window, no_props)
15550 Lisp_Object format, window, no_props;
8143e6ab
KS
15551{
15552 struct it it;
8143e6ab
KS
15553 int len;
15554 struct window *w;
15555 struct buffer *old_buffer = NULL;
fec8f23e 15556 enum face_id face_id = DEFAULT_FACE_ID;
8143e6ab
KS
15557
15558 if (NILP (window))
15559 window = selected_window;
15560 CHECK_WINDOW (window);
15561 w = XWINDOW (window);
15562 CHECK_BUFFER (w->buffer);
15563
15564 if (XBUFFER (w->buffer) != current_buffer)
15565 {
15566 old_buffer = current_buffer;
15567 set_buffer_internal_1 (XBUFFER (w->buffer));
15568 }
15569
15570 if (NILP (format) || EQ (format, Qt))
fec8f23e
KS
15571 {
15572 face_id = NILP (format)
15573 ? CURRENT_MODE_LINE_FACE_ID (w) :
15574 HEADER_LINE_FACE_ID;
15575 format = NILP (format)
15576 ? current_buffer->mode_line_format
15577 : current_buffer->header_line_format;
15578 }
15579
15580 init_iterator (&it, w, -1, -1, NULL, face_id);
8143e6ab 15581
fec8f23e
KS
15582 if (NILP (no_props))
15583 {
15584 mode_line_string_face =
15585 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15586 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15587 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
8143e6ab 15588
fec8f23e
KS
15589 mode_line_string_face_prop =
15590 NILP (mode_line_string_face) ? Qnil :
15591 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15592
15593 /* We need a dummy last element in mode_line_string_list to
15594 indicate we are building the propertized mode-line string.
15595 Using mode_line_string_face_prop here GC protects it. */
7d0393cf 15596 mode_line_string_list =
fec8f23e
KS
15597 Fcons (mode_line_string_face_prop, Qnil);
15598 frame_title_ptr = NULL;
15599 }
15600 else
15601 {
15602 mode_line_string_face_prop = Qnil;
15603 mode_line_string_list = Qnil;
15604 frame_title_ptr = frame_title_buf;
15605 }
8143e6ab
KS
15606
15607 push_frame_kboard (it.f);
15608 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15609 pop_frame_kboard ();
15610
15611 if (old_buffer)
15612 set_buffer_internal_1 (old_buffer);
15613
fec8f23e
KS
15614 if (NILP (no_props))
15615 {
15616 Lisp_Object str;
15617 mode_line_string_list = Fnreverse (mode_line_string_list);
15618 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15619 make_string ("", 0));
15620 mode_line_string_face_prop = Qnil;
15621 mode_line_string_list = Qnil;
15622 return str;
15623 }
15624
8143e6ab
KS
15625 len = frame_title_ptr - frame_title_buf;
15626 if (len > 0 && frame_title_ptr[-1] == '-')
15627 {
15628 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15629 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15630 ;
15631 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15632 if (len > frame_title_ptr - frame_title_buf)
15633 len = frame_title_ptr - frame_title_buf;
15634 }
15635
15636 frame_title_ptr = NULL;
15637 return make_string (frame_title_buf, len);
15638}
15639
766525bc
RS
15640/* Write a null-terminated, right justified decimal representation of
15641 the positive integer D to BUF using a minimal field width WIDTH. */
15642
15643static void
15644pint2str (buf, width, d)
15645 register char *buf;
15646 register int width;
15647 register int d;
15648{
15649 register char *p = buf;
2311178e 15650
766525bc 15651 if (d <= 0)
5f5c8ee5 15652 *p++ = '0';
766525bc 15653 else
5f5c8ee5 15654 {
766525bc 15655 while (d > 0)
5f5c8ee5 15656 {
766525bc
RS
15657 *p++ = d % 10 + '0';
15658 d /= 10;
5f5c8ee5
GM
15659 }
15660 }
2311178e 15661
5f5c8ee5
GM
15662 for (width -= (int) (p - buf); width > 0; --width)
15663 *p++ = ' ';
766525bc
RS
15664 *p-- = '\0';
15665 while (p > buf)
5f5c8ee5 15666 {
766525bc
RS
15667 d = *buf;
15668 *buf++ = *p;
15669 *p-- = d;
5f5c8ee5 15670 }
766525bc
RS
15671}
15672
525b8b09
LK
15673/* Write a null-terminated, right justified decimal and "human
15674 readable" representation of the nonnegative integer D to BUF using
15675 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15676
15677static const char power_letter[] =
15678 {
15679 0, /* not used */
15680 'k', /* kilo */
15681 'M', /* mega */
15682 'G', /* giga */
15683 'T', /* tera */
15684 'P', /* peta */
15685 'E', /* exa */
15686 'Z', /* zetta */
15687 'Y' /* yotta */
15688 };
15689
15690static void
15691pint2hrstr (buf, width, d)
15692 char *buf;
15693 int width;
15694 int d;
15695{
15696 /* We aim to represent the nonnegative integer D as
15697 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15698 int quotient = d;
15699 int remainder = 0;
15700 /* -1 means: do not use TENTHS. */
15701 int tenths = -1;
15702 int exponent = 0;
15703
15704 /* Length of QUOTIENT.TENTHS as a string. */
15705 int length;
15706
15707 char * psuffix;
15708 char * p;
15709
15710 if (1000 <= quotient)
15711 {
15712 /* Scale to the appropriate EXPONENT. */
15713 do
15714 {
15715 remainder = quotient % 1000;
15716 quotient /= 1000;
15717 exponent++;
15718 }
15719 while (1000 <= quotient);
15720
15721 /* Round to nearest and decide whether to use TENTHS or not. */
15722 if (quotient <= 9)
15723 {
15724 tenths = remainder / 100;
15725 if (50 <= remainder % 100)
15726 if (tenths < 9)
15727 tenths++;
15728 else
15729 {
15730 quotient++;
15731 if (quotient == 10)
15732 tenths = -1;
15733 else
15734 tenths = 0;
15735 }
15736 }
15737 else
15738 if (500 <= remainder)
15739 if (quotient < 999)
15740 quotient++;
15741 else
15742 {
15743 quotient = 1;
15744 exponent++;
15745 tenths = 0;
15746 }
15747 }
15748
15749 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
15750 if (tenths == -1 && quotient <= 99)
15751 if (quotient <= 9)
15752 length = 1;
15753 else
15754 length = 2;
15755 else
15756 length = 3;
15757 p = psuffix = buf + max (width, length);
15758
15759 /* Print EXPONENT. */
15760 if (exponent)
15761 *psuffix++ = power_letter[exponent];
15762 *psuffix = '\0';
15763
15764 /* Print TENTHS. */
15765 if (tenths >= 0)
15766 {
15767 *--p = '0' + tenths;
15768 *--p = '.';
15769 }
15770
15771 /* Print QUOTIENT. */
15772 do
15773 {
15774 int digit = quotient % 10;
15775 *--p = '0' + digit;
15776 }
15777 while ((quotient /= 10) != 0);
15778
15779 /* Print leading spaces. */
15780 while (buf < p)
15781 *--p = ' ';
15782}
15783
5f5c8ee5 15784/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
15785 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15786 type of CODING_SYSTEM. Return updated pointer into BUF. */
15787
6693a99a 15788static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 15789
1c9241f5
KH
15790static char *
15791decode_mode_spec_coding (coding_system, buf, eol_flag)
15792 Lisp_Object coding_system;
15793 register char *buf;
15794 int eol_flag;
15795{
1e1078d6 15796 Lisp_Object val;
916848d8 15797 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
50f80c2f 15798 const unsigned char *eol_str;
302f2b38
EZ
15799 int eol_str_len;
15800 /* The EOL conversion we are using. */
15801 Lisp_Object eoltype;
1e1078d6 15802
4a09dee0 15803 val = Fget (coding_system, Qcoding_system);
085536c2 15804 eoltype = Qnil;
1c9241f5 15805
4a09dee0 15806 if (!VECTORP (val)) /* Not yet decided. */
1c9241f5 15807 {
916848d8
RS
15808 if (multibyte)
15809 *buf++ = '-';
21e989e3 15810 if (eol_flag)
302f2b38 15811 eoltype = eol_mnemonic_undecided;
1e1078d6 15812 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
15813 }
15814 else
15815 {
1e1078d6
RS
15816 Lisp_Object eolvalue;
15817
15818 eolvalue = Fget (coding_system, Qeol_type);
15819
916848d8 15820 if (multibyte)
a61b7058 15821 *buf++ = XFASTINT (AREF (val, 1));
916848d8 15822
1c9241f5
KH
15823 if (eol_flag)
15824 {
1e1078d6
RS
15825 /* The EOL conversion that is normal on this system. */
15826
15827 if (NILP (eolvalue)) /* Not yet decided. */
15828 eoltype = eol_mnemonic_undecided;
15829 else if (VECTORP (eolvalue)) /* Not yet decided. */
15830 eoltype = eol_mnemonic_undecided;
15831 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15832 eoltype = (XFASTINT (eolvalue) == 0
15833 ? eol_mnemonic_unix
15834 : (XFASTINT (eolvalue) == 1
15835 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
15836 }
15837 }
2311178e 15838
302f2b38
EZ
15839 if (eol_flag)
15840 {
15841 /* Mention the EOL conversion if it is not the usual one. */
15842 if (STRINGP (eoltype))
15843 {
2051c264
GM
15844 eol_str = SDATA (eoltype);
15845 eol_str_len = SBYTES (eoltype);
302f2b38 15846 }
f30b3499
KH
15847 else if (INTEGERP (eoltype)
15848 && CHAR_VALID_P (XINT (eoltype), 0))
15849 {
50f80c2f
KR
15850 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
15851 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
15852 eol_str = tmp;
f30b3499 15853 }
302f2b38
EZ
15854 else
15855 {
15856 eol_str = invalid_eol_type;
15857 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 15858 }
f30b3499 15859 bcopy (eol_str, buf, eol_str_len);
302f2b38 15860 buf += eol_str_len;
1c9241f5 15861 }
302f2b38 15862
1c9241f5
KH
15863 return buf;
15864}
15865
a2889657 15866/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
15867 generated by character C. PRECISION >= 0 means don't return a
15868 string longer than that value. FIELD_WIDTH > 0 means pad the
72f62cb5
GM
15869 string returned with spaces to that value. Return 1 in *MULTIBYTE
15870 if the result is multibyte text. */
a2889657 15871
11e82b76
JB
15872static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
15873
a2889657 15874static char *
72f62cb5 15875decode_mode_spec (w, c, field_width, precision, multibyte)
a2889657 15876 struct window *w;
68c45bf0 15877 register int c;
5f5c8ee5 15878 int field_width, precision;
72f62cb5 15879 int *multibyte;
a2889657 15880{
0b67772d 15881 Lisp_Object obj;
5f5c8ee5
GM
15882 struct frame *f = XFRAME (WINDOW_FRAME (w));
15883 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 15884 struct buffer *b = XBUFFER (w->buffer);
a2889657 15885
0b67772d 15886 obj = Qnil;
72f62cb5 15887 *multibyte = 0;
a2889657
JB
15888
15889 switch (c)
15890 {
1af9f229
RS
15891 case '*':
15892 if (!NILP (b->read_only))
15893 return "%";
15894 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15895 return "*";
15896 return "-";
15897
15898 case '+':
15899 /* This differs from %* only for a modified read-only buffer. */
15900 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15901 return "*";
15902 if (!NILP (b->read_only))
15903 return "%";
15904 return "-";
15905
15906 case '&':
15907 /* This differs from %* in ignoring read-only-ness. */
15908 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15909 return "*";
15910 return "-";
15911
15912 case '%':
15913 return "%";
15914
2311178e 15915 case '[':
1af9f229
RS
15916 {
15917 int i;
15918 char *p;
15919
15920 if (command_loop_level > 5)
15921 return "[[[... ";
15922 p = decode_mode_spec_buf;
15923 for (i = 0; i < command_loop_level; i++)
15924 *p++ = '[';
15925 *p = 0;
15926 return decode_mode_spec_buf;
15927 }
15928
2311178e 15929 case ']':
1af9f229
RS
15930 {
15931 int i;
15932 char *p;
15933
15934 if (command_loop_level > 5)
15935 return " ...]]]";
15936 p = decode_mode_spec_buf;
15937 for (i = 0; i < command_loop_level; i++)
15938 *p++ = ']';
15939 *p = 0;
15940 return decode_mode_spec_buf;
15941 }
15942
15943 case '-':
15944 {
1af9f229 15945 register int i;
5f5c8ee5
GM
15946
15947 /* Let lots_of_dashes be a string of infinite length. */
fec8f23e
KS
15948 if (!NILP (mode_line_string_list))
15949 return "--";
5f5c8ee5
GM
15950 if (field_width <= 0
15951 || field_width > sizeof (lots_of_dashes))
1af9f229 15952 {
5f5c8ee5
GM
15953 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
15954 decode_mode_spec_buf[i] = '-';
15955 decode_mode_spec_buf[i] = '\0';
15956 return decode_mode_spec_buf;
1af9f229 15957 }
5f5c8ee5
GM
15958 else
15959 return lots_of_dashes;
1af9f229
RS
15960 }
15961
2311178e 15962 case 'b':
d39b6696 15963 obj = b->name;
a2889657
JB
15964 break;
15965
1af9f229
RS
15966 case 'c':
15967 {
2311178e 15968 int col = (int) current_column (); /* iftc */
ac90c44f 15969 w->column_number_displayed = make_number (col);
5f5c8ee5 15970 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
15971 return decode_mode_spec_buf;
15972 }
15973
15974 case 'F':
15975 /* %F displays the frame name. */
5f5c8ee5 15976 if (!NILP (f->title))
2051c264 15977 return (char *) SDATA (f->title);
fd8ff63d 15978 if (f->explicit_name || ! FRAME_WINDOW_P (f))
2051c264 15979 return (char *) SDATA (f->name);
9c6da96f 15980 return "Emacs";
1af9f229 15981
2311178e 15982 case 'f':
d39b6696 15983 obj = b->filename;
a2889657
JB
15984 break;
15985
525b8b09
LK
15986 case 'i':
15987 {
15988 int size = ZV - BEGV;
15989 pint2str (decode_mode_spec_buf, field_width, size);
15990 return decode_mode_spec_buf;
15991 }
15992
15993 case 'I':
15994 {
15995 int size = ZV - BEGV;
15996 pint2hrstr (decode_mode_spec_buf, field_width, size);
15997 return decode_mode_spec_buf;
15998 }
15999
aa6d10fa
RS
16000 case 'l':
16001 {
12adba34
RS
16002 int startpos = XMARKER (w->start)->charpos;
16003 int startpos_byte = marker_byte_position (w->start);
16004 int line, linepos, linepos_byte, topline;
aa6d10fa 16005 int nlines, junk;
da8b7f4f 16006 int height = WINDOW_TOTAL_LINES (w);
aa6d10fa 16007
2311178e 16008 /* If we decided that this buffer isn't suitable for line numbers,
aa6d10fa
RS
16009 don't forget that too fast. */
16010 if (EQ (w->base_line_pos, w->buffer))
766525bc 16011 goto no_value;
5300fd39
RS
16012 /* But do forget it, if the window shows a different buffer now. */
16013 else if (BUFFERP (w->base_line_pos))
16014 w->base_line_pos = Qnil;
aa6d10fa
RS
16015
16016 /* If the buffer is very big, don't waste time. */
37d034d3 16017 if (INTEGERP (Vline_number_display_limit)
090703f4 16018 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
aa6d10fa
RS
16019 {
16020 w->base_line_pos = Qnil;
16021 w->base_line_number = Qnil;
766525bc 16022 goto no_value;
aa6d10fa
RS
16023 }
16024
16025 if (!NILP (w->base_line_number)
16026 && !NILP (w->base_line_pos)
12adba34 16027 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
16028 {
16029 line = XFASTINT (w->base_line_number);
16030 linepos = XFASTINT (w->base_line_pos);
12adba34 16031 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
16032 }
16033 else
16034 {
16035 line = 1;
d39b6696 16036 linepos = BUF_BEGV (b);
12adba34 16037 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
16038 }
16039
16040 /* Count lines from base line to window start position. */
12adba34
RS
16041 nlines = display_count_lines (linepos, linepos_byte,
16042 startpos_byte,
16043 startpos, &junk);
aa6d10fa
RS
16044
16045 topline = nlines + line;
16046
16047 /* Determine a new base line, if the old one is too close
16048 or too far away, or if we did not have one.
16049 "Too close" means it's plausible a scroll-down would
16050 go back past it. */
d39b6696 16051 if (startpos == BUF_BEGV (b))
aa6d10fa 16052 {
ac90c44f
GM
16053 w->base_line_number = make_number (topline);
16054 w->base_line_pos = make_number (BUF_BEGV (b));
aa6d10fa
RS
16055 }
16056 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 16057 || linepos == BUF_BEGV (b))
aa6d10fa 16058 {
d39b6696 16059 int limit = BUF_BEGV (b);
12adba34 16060 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 16061 int position;
5d121aec 16062 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
16063
16064 if (startpos - distance > limit)
12adba34
RS
16065 {
16066 limit = startpos - distance;
16067 limit_byte = CHAR_TO_BYTE (limit);
16068 }
aa6d10fa 16069
12adba34
RS
16070 nlines = display_count_lines (startpos, startpos_byte,
16071 limit_byte,
16072 - (height * 2 + 30),
aa6d10fa 16073 &position);
2311178e 16074 /* If we couldn't find the lines we wanted within
5d121aec 16075 line_number_display_limit_width chars per line,
aa6d10fa 16076 give up on line numbers for this window. */
12adba34 16077 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
16078 {
16079 w->base_line_pos = w->buffer;
16080 w->base_line_number = Qnil;
766525bc 16081 goto no_value;
aa6d10fa
RS
16082 }
16083
ac90c44f
GM
16084 w->base_line_number = make_number (topline - nlines);
16085 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
aa6d10fa
RS
16086 }
16087
16088 /* Now count lines from the start pos to point. */
12adba34
RS
16089 nlines = display_count_lines (startpos, startpos_byte,
16090 PT_BYTE, PT, &junk);
aa6d10fa
RS
16091
16092 /* Record that we did display the line number. */
16093 line_number_displayed = 1;
16094
16095 /* Make the string to show. */
5f5c8ee5 16096 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 16097 return decode_mode_spec_buf;
766525bc
RS
16098 no_value:
16099 {
16100 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
16101 int pad = field_width - 2;
16102 while (pad-- > 0)
16103 *p++ = ' ';
16104 *p++ = '?';
b357b9d4
KR
16105 *p++ = '?';
16106 *p = '\0';
766525bc
RS
16107 return decode_mode_spec_buf;
16108 }
aa6d10fa
RS
16109 }
16110 break;
16111
2311178e 16112 case 'm':
d39b6696 16113 obj = b->mode_name;
a2889657
JB
16114 break;
16115
16116 case 'n':
d39b6696 16117 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
16118 return " Narrow";
16119 break;
16120
a2889657
JB
16121 case 'p':
16122 {
16123 int pos = marker_position (w->start);
d39b6696 16124 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 16125
d39b6696 16126 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 16127 {
d39b6696 16128 if (pos <= BUF_BEGV (b))
a2889657
JB
16129 return "All";
16130 else
16131 return "Bottom";
16132 }
d39b6696 16133 else if (pos <= BUF_BEGV (b))
a2889657
JB
16134 return "Top";
16135 else
16136 {
3c7d31b9
RS
16137 if (total > 1000000)
16138 /* Do it differently for a large value, to avoid overflow. */
16139 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16140 else
16141 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
16142 /* We can't normally display a 3-digit number,
16143 so get us a 2-digit number that is close. */
16144 if (total == 100)
16145 total = 99;
16146 sprintf (decode_mode_spec_buf, "%2d%%", total);
16147 return decode_mode_spec_buf;
16148 }
16149 }
16150
8ffcb79f
RS
16151 /* Display percentage of size above the bottom of the screen. */
16152 case 'P':
16153 {
16154 int toppos = marker_position (w->start);
d39b6696
KH
16155 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16156 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 16157
d39b6696 16158 if (botpos >= BUF_ZV (b))
8ffcb79f 16159 {
d39b6696 16160 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
16161 return "All";
16162 else
16163 return "Bottom";
16164 }
16165 else
16166 {
3c7d31b9
RS
16167 if (total > 1000000)
16168 /* Do it differently for a large value, to avoid overflow. */
16169 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16170 else
16171 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
16172 /* We can't normally display a 3-digit number,
16173 so get us a 2-digit number that is close. */
16174 if (total == 100)
16175 total = 99;
d39b6696 16176 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
16177 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16178 else
16179 sprintf (decode_mode_spec_buf, "%2d%%", total);
16180 return decode_mode_spec_buf;
16181 }
16182 }
16183
1af9f229
RS
16184 case 's':
16185 /* status of process */
16186 obj = Fget_buffer_process (w->buffer);
16187 if (NILP (obj))
16188 return "no process";
16189#ifdef subprocesses
16190 obj = Fsymbol_name (Fprocess_status (obj));
16191#endif
16192 break;
d39b6696 16193
1af9f229
RS
16194 case 't': /* indicate TEXT or BINARY */
16195#ifdef MODE_LINE_BINARY_TEXT
16196 return MODE_LINE_BINARY_TEXT (b);
16197#else
16198 return "T";
16199#endif
1c9241f5
KH
16200
16201 case 'z':
16202 /* coding-system (not including end-of-line format) */
16203 case 'Z':
16204 /* coding-system (including end-of-line type) */
16205 {
16206 int eol_flag = (c == 'Z');
539b4d41 16207 char *p = decode_mode_spec_buf;
1c9241f5 16208
d30e754b 16209 if (! FRAME_WINDOW_P (f))
1c9241f5 16210 {
11c52c4f
RS
16211 /* No need to mention EOL here--the terminal never needs
16212 to do EOL conversion. */
16213 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16214 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 16215 }
f13c925f 16216 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 16217 p, eol_flag);
f13c925f 16218
11c52c4f 16219#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
16220#ifdef subprocesses
16221 obj = Fget_buffer_process (Fcurrent_buffer ());
16222 if (PROCESSP (obj))
16223 {
16224 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16225 p, eol_flag);
16226 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16227 p, eol_flag);
16228 }
16229#endif /* subprocesses */
11c52c4f 16230#endif /* 0 */
1c9241f5
KH
16231 *p = 0;
16232 return decode_mode_spec_buf;
16233 }
a2889657 16234 }
d39b6696 16235
e24c997d 16236 if (STRINGP (obj))
72f62cb5
GM
16237 {
16238 *multibyte = STRING_MULTIBYTE (obj);
2051c264 16239 return (char *) SDATA (obj);
72f62cb5 16240 }
a2889657
JB
16241 else
16242 return "";
16243}
5f5c8ee5
GM
16244
16245
12adba34
RS
16246/* Count up to COUNT lines starting from START / START_BYTE.
16247 But don't go beyond LIMIT_BYTE.
16248 Return the number of lines thus found (always nonnegative).
59b49f63 16249
12adba34 16250 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
16251
16252static int
12adba34
RS
16253display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16254 int start, start_byte, limit_byte, count;
16255 int *byte_pos_ptr;
59b49f63 16256{
59b49f63
RS
16257 register unsigned char *cursor;
16258 unsigned char *base;
16259
16260 register int ceiling;
16261 register unsigned char *ceiling_addr;
12adba34 16262 int orig_count = count;
59b49f63
RS
16263
16264 /* If we are not in selective display mode,
16265 check only for newlines. */
12adba34
RS
16266 int selective_display = (!NILP (current_buffer->selective_display)
16267 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
16268
16269 if (count > 0)
12adba34
RS
16270 {
16271 while (start_byte < limit_byte)
16272 {
16273 ceiling = BUFFER_CEILING_OF (start_byte);
16274 ceiling = min (limit_byte - 1, ceiling);
16275 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16276 base = (cursor = BYTE_POS_ADDR (start_byte));
16277 while (1)
16278 {
16279 if (selective_display)
16280 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16281 ;
16282 else
16283 while (*cursor != '\n' && ++cursor != ceiling_addr)
16284 ;
16285
16286 if (cursor != ceiling_addr)
16287 {
16288 if (--count == 0)
16289 {
16290 start_byte += cursor - base + 1;
16291 *byte_pos_ptr = start_byte;
16292 return orig_count;
16293 }
16294 else
16295 if (++cursor == ceiling_addr)
16296 break;
16297 }
16298 else
16299 break;
16300 }
16301 start_byte += cursor - base;
16302 }
16303 }
59b49f63
RS
16304 else
16305 {
12adba34
RS
16306 while (start_byte > limit_byte)
16307 {
16308 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16309 ceiling = max (limit_byte, ceiling);
16310 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16311 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
16312 while (1)
16313 {
12adba34
RS
16314 if (selective_display)
16315 while (--cursor != ceiling_addr
16316 && *cursor != '\n' && *cursor != 015)
16317 ;
16318 else
16319 while (--cursor != ceiling_addr && *cursor != '\n')
16320 ;
16321
59b49f63
RS
16322 if (cursor != ceiling_addr)
16323 {
16324 if (++count == 0)
16325 {
12adba34
RS
16326 start_byte += cursor - base + 1;
16327 *byte_pos_ptr = start_byte;
16328 /* When scanning backwards, we should
16329 not count the newline posterior to which we stop. */
16330 return - orig_count - 1;
59b49f63
RS
16331 }
16332 }
16333 else
16334 break;
16335 }
12adba34
RS
16336 /* Here we add 1 to compensate for the last decrement
16337 of CURSOR, which took it past the valid range. */
16338 start_byte += cursor - base + 1;
59b49f63
RS
16339 }
16340 }
16341
12adba34 16342 *byte_pos_ptr = limit_byte;
aa6d10fa 16343
12adba34
RS
16344 if (count < 0)
16345 return - orig_count + count;
16346 return orig_count - count;
aa6d10fa 16347
12adba34 16348}
a2889657 16349
a2889657 16350
5f5c8ee5
GM
16351\f
16352/***********************************************************************
16353 Displaying strings
16354 ***********************************************************************/
278feba9 16355
5f5c8ee5 16356/* Display a NUL-terminated string, starting with index START.
a3788d53 16357
5f5c8ee5
GM
16358 If STRING is non-null, display that C string. Otherwise, the Lisp
16359 string LISP_STRING is displayed.
a2889657 16360
5f5c8ee5
GM
16361 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16362 FACE_STRING. Display STRING or LISP_STRING with the face at
16363 FACE_STRING_POS in FACE_STRING:
a2889657 16364
5f5c8ee5
GM
16365 Display the string in the environment given by IT, but use the
16366 standard display table, temporarily.
a3788d53 16367
5f5c8ee5
GM
16368 FIELD_WIDTH is the minimum number of output glyphs to produce.
16369 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16370 with spaces. If STRING has more characters, more than FIELD_WIDTH
16371 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
2311178e 16372
5f5c8ee5
GM
16373 PRECISION is the maximum number of characters to output from
16374 STRING. PRECISION < 0 means don't truncate the string.
a2889657 16375
5f5c8ee5 16376 This is roughly equivalent to printf format specifiers:
a2889657 16377
5f5c8ee5
GM
16378 FIELD_WIDTH PRECISION PRINTF
16379 ----------------------------------------
16380 -1 -1 %s
16381 -1 10 %.10s
16382 10 -1 %10s
16383 20 10 %20.10s
a2889657 16384
5f5c8ee5
GM
16385 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16386 display them, and < 0 means obey the current buffer's value of
16387 enable_multibyte_characters.
278feba9 16388
5f5c8ee5 16389 Value is the number of glyphs produced. */
b1d1124b 16390
5f5c8ee5
GM
16391static int
16392display_string (string, lisp_string, face_string, face_string_pos,
16393 start, it, field_width, precision, max_x, multibyte)
16394 unsigned char *string;
16395 Lisp_Object lisp_string;
68c45bf0
PE
16396 Lisp_Object face_string;
16397 int face_string_pos;
5f5c8ee5
GM
16398 int start;
16399 struct it *it;
16400 int field_width, precision, max_x;
16401 int multibyte;
16402{
16403 int hpos_at_start = it->hpos;
16404 int saved_face_id = it->face_id;
16405 struct glyph_row *row = it->glyph_row;
16406
16407 /* Initialize the iterator IT for iteration over STRING beginning
e719f5ae 16408 with index START. */
5f5c8ee5
GM
16409 reseat_to_string (it, string, lisp_string, start,
16410 precision, field_width, multibyte);
16411
16412 /* If displaying STRING, set up the face of the iterator
16413 from LISP_STRING, if that's given. */
16414 if (STRINGP (face_string))
16415 {
16416 int endptr;
16417 struct face *face;
2311178e 16418
5f5c8ee5
GM
16419 it->face_id
16420 = face_at_string_position (it->w, face_string, face_string_pos,
16421 0, it->region_beg_charpos,
16422 it->region_end_charpos,
5de7c6f2 16423 &endptr, it->base_face_id, 0);
5f5c8ee5
GM
16424 face = FACE_FROM_ID (it->f, it->face_id);
16425 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 16426 }
a2889657 16427
5f5c8ee5
GM
16428 /* Set max_x to the maximum allowed X position. Don't let it go
16429 beyond the right edge of the window. */
16430 if (max_x <= 0)
16431 max_x = it->last_visible_x;
16432 else
16433 max_x = min (max_x, it->last_visible_x);
efc63ef0 16434
5f5c8ee5
GM
16435 /* Skip over display elements that are not visible. because IT->w is
16436 hscrolled. */
16437 if (it->current_x < it->first_visible_x)
16438 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16439 MOVE_TO_POS | MOVE_TO_X);
a2889657 16440
5f5c8ee5
GM
16441 row->ascent = it->max_ascent;
16442 row->height = it->max_ascent + it->max_descent;
312246d1
GM
16443 row->phys_ascent = it->max_phys_ascent;
16444 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 16445
5f5c8ee5
GM
16446 /* This condition is for the case that we are called with current_x
16447 past last_visible_x. */
16448 while (it->current_x < max_x)
a2889657 16449 {
5f5c8ee5 16450 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 16451
5f5c8ee5
GM
16452 /* Get the next display element. */
16453 if (!get_next_display_element (it))
90adcf20 16454 break;
1c9241f5 16455
5f5c8ee5
GM
16456 /* Produce glyphs. */
16457 x_before = it->current_x;
16458 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16459 PRODUCE_GLYPHS (it);
90adcf20 16460
5f5c8ee5
GM
16461 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16462 i = 0;
16463 x = x_before;
16464 while (i < nglyphs)
a2889657 16465 {
5f5c8ee5 16466 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
2311178e 16467
5f5c8ee5
GM
16468 if (!it->truncate_lines_p
16469 && x + glyph->pixel_width > max_x)
16470 {
16471 /* End of continued line or max_x reached. */
37be86f2
KH
16472 if (CHAR_GLYPH_PADDING_P (*glyph))
16473 {
16474 /* A wide character is unbreakable. */
16475 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16476 it->current_x = x_before;
16477 }
16478 else
16479 {
16480 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16481 it->current_x = x;
16482 }
5f5c8ee5
GM
16483 break;
16484 }
16485 else if (x + glyph->pixel_width > it->first_visible_x)
16486 {
16487 /* Glyph is at least partially visible. */
16488 ++it->hpos;
16489 if (x < it->first_visible_x)
16490 it->glyph_row->x = x - it->first_visible_x;
16491 }
16492 else
a2889657 16493 {
5f5c8ee5
GM
16494 /* Glyph is off the left margin of the display area.
16495 Should not happen. */
16496 abort ();
a2889657 16497 }
5f5c8ee5
GM
16498
16499 row->ascent = max (row->ascent, it->max_ascent);
16500 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
16501 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16502 row->phys_height = max (row->phys_height,
16503 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
16504 x += glyph->pixel_width;
16505 ++i;
a2889657 16506 }
5f5c8ee5
GM
16507
16508 /* Stop if max_x reached. */
16509 if (i < nglyphs)
16510 break;
16511
16512 /* Stop at line ends. */
16513 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 16514 {
5f5c8ee5
GM
16515 it->continuation_lines_width = 0;
16516 break;
a2889657 16517 }
1c9241f5 16518
cafafe0b 16519 set_iterator_to_next (it, 1);
a688bb24 16520
5f5c8ee5
GM
16521 /* Stop if truncating at the right edge. */
16522 if (it->truncate_lines_p
16523 && it->current_x >= it->last_visible_x)
16524 {
16525 /* Add truncation mark, but don't do it if the line is
16526 truncated at a padding space. */
16527 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 16528 {
5f5c8ee5 16529 if (!FRAME_WINDOW_P (it->f))
37be86f2
KH
16530 {
16531 int i, n;
16532
9299cb15 16533 if (it->current_x > it->last_visible_x)
37be86f2 16534 {
9299cb15
KH
16535 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16536 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16537 break;
16538 for (n = row->used[TEXT_AREA]; i < n; ++i)
16539 {
16540 row->used[TEXT_AREA] = i;
16541 produce_special_glyphs (it, IT_TRUNCATION);
16542 }
37be86f2 16543 }
9299cb15 16544 produce_special_glyphs (it, IT_TRUNCATION);
37be86f2 16545 }
5f5c8ee5 16546 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 16547 }
5f5c8ee5 16548 break;
1c9241f5 16549 }
a2889657
JB
16550 }
16551
5f5c8ee5
GM
16552 /* Maybe insert a truncation at the left. */
16553 if (it->first_visible_x
16554 && IT_CHARPOS (*it) > 0)
a2889657 16555 {
5f5c8ee5
GM
16556 if (!FRAME_WINDOW_P (it->f))
16557 insert_left_trunc_glyphs (it);
16558 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
16559 }
16560
5f5c8ee5 16561 it->face_id = saved_face_id;
2311178e 16562
5f5c8ee5
GM
16563 /* Value is number of columns displayed. */
16564 return it->hpos - hpos_at_start;
16565}
a2889657 16566
a2889657 16567
a2889657 16568\f
3b6b6db7 16569/* This is like a combination of memq and assq. Return 1/2 if PROPVAL
5f5c8ee5
GM
16570 appears as an element of LIST or as the car of an element of LIST.
16571 If PROPVAL is a list, compare each element against LIST in that
3b6b6db7
SM
16572 way, and return 1/2 if any element of PROPVAL is found in LIST.
16573 Otherwise return 0. This function cannot quit.
16574 The return value is 2 if the text is invisible but with an ellipsis
16575 and 1 if it's invisible and without an ellipsis. */
642eefc6
RS
16576
16577int
16578invisible_p (propval, list)
16579 register Lisp_Object propval;
16580 Lisp_Object list;
16581{
3b6b6db7 16582 register Lisp_Object tail, proptail;
2311178e 16583
3b6b6db7
SM
16584 for (tail = list; CONSP (tail); tail = XCDR (tail))
16585 {
16586 register Lisp_Object tem;
16587 tem = XCAR (tail);
16588 if (EQ (propval, tem))
16589 return 1;
16590 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16591 return NILP (XCDR (tem)) ? 1 : 2;
16592 }
2311178e 16593
3b6b6db7
SM
16594 if (CONSP (propval))
16595 {
16596 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16597 {
16598 Lisp_Object propelt;
16599 propelt = XCAR (proptail);
16600 for (tail = list; CONSP (tail); tail = XCDR (tail))
16601 {
16602 register Lisp_Object tem;
16603 tem = XCAR (tail);
16604 if (EQ (propelt, tem))
16605 return 1;
16606 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16607 return NILP (XCDR (tem)) ? 1 : 2;
16608 }
16609 }
16610 }
2311178e 16611
3b6b6db7 16612 return 0;
642eefc6 16613}
5f5c8ee5 16614
642eefc6 16615\f
133c764e
KS
16616/***********************************************************************
16617 Glyph Display
16618 ***********************************************************************/
16619
79fa9e0f
KS
16620#ifdef HAVE_WINDOW_SYSTEM
16621
133c764e
KS
16622#if GLYPH_DEBUG
16623
16624void
16625dump_glyph_string (s)
16626 struct glyph_string *s;
16627{
16628 fprintf (stderr, "glyph string\n");
16629 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
16630 s->x, s->y, s->width, s->height);
16631 fprintf (stderr, " ybase = %d\n", s->ybase);
16632 fprintf (stderr, " hl = %d\n", s->hl);
16633 fprintf (stderr, " left overhang = %d, right = %d\n",
16634 s->left_overhang, s->right_overhang);
16635 fprintf (stderr, " nchars = %d\n", s->nchars);
16636 fprintf (stderr, " extends to end of line = %d\n",
16637 s->extends_to_end_of_line_p);
16638 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
16639 fprintf (stderr, " bg width = %d\n", s->background_width);
16640}
16641
16642#endif /* GLYPH_DEBUG */
16643
16644/* Initialize glyph string S. CHAR2B is a suitably allocated vector
16645 of XChar2b structures for S; it can't be allocated in
16646 init_glyph_string because it must be allocated via `alloca'. W
16647 is the window on which S is drawn. ROW and AREA are the glyph row
16648 and area within the row from which S is constructed. START is the
16649 index of the first glyph structure covered by S. HL is a
16650 face-override for drawing S. */
16651
16652#ifdef HAVE_NTGUI
16653#define OPTIONAL_HDC(hdc) hdc,
16654#define DECLARE_HDC(hdc) HDC hdc;
16655#define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
16656#define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
16657#endif
16658
16659#ifndef OPTIONAL_HDC
16660#define OPTIONAL_HDC(hdc)
16661#define DECLARE_HDC(hdc)
16662#define ALLOCATE_HDC(hdc, f)
16663#define RELEASE_HDC(hdc, f)
16664#endif
16665
16666static void
16667init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
16668 struct glyph_string *s;
16669 DECLARE_HDC (hdc)
16670 XChar2b *char2b;
16671 struct window *w;
16672 struct glyph_row *row;
16673 enum glyph_row_area area;
16674 int start;
16675 enum draw_glyphs_face hl;
16676{
16677 bzero (s, sizeof *s);
16678 s->w = w;
16679 s->f = XFRAME (w->frame);
16680#ifdef HAVE_NTGUI
16681 s->hdc = hdc;
16682#endif
16683 s->display = FRAME_X_DISPLAY (s->f);
16684 s->window = FRAME_X_WINDOW (s->f);
16685 s->char2b = char2b;
16686 s->hl = hl;
16687 s->row = row;
16688 s->area = area;
16689 s->first_glyph = row->glyphs[area] + start;
16690 s->height = row->height;
16691 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
16692
16693 /* Display the internal border below the tool-bar window. */
16694 if (s->w == XWINDOW (s->f->tool_bar_window))
16695 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
16696
16697 s->ybase = s->y + row->ascent;
16698}
16699
16700
16701/* Append the list of glyph strings with head H and tail T to the list
16702 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
16703
16704static INLINE void
16705append_glyph_string_lists (head, tail, h, t)
16706 struct glyph_string **head, **tail;
16707 struct glyph_string *h, *t;
16708{
16709 if (h)
16710 {
16711 if (*head)
16712 (*tail)->next = h;
16713 else
16714 *head = h;
16715 h->prev = *tail;
16716 *tail = t;
16717 }
16718}
16719
16720
16721/* Prepend the list of glyph strings with head H and tail T to the
16722 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
16723 result. */
16724
16725static INLINE void
16726prepend_glyph_string_lists (head, tail, h, t)
16727 struct glyph_string **head, **tail;
16728 struct glyph_string *h, *t;
16729{
16730 if (h)
16731 {
16732 if (*head)
16733 (*head)->prev = t;
16734 else
16735 *tail = t;
16736 t->next = *head;
16737 *head = h;
16738 }
16739}
16740
16741
16742/* Append glyph string S to the list with head *HEAD and tail *TAIL.
16743 Set *HEAD and *TAIL to the resulting list. */
16744
16745static INLINE void
16746append_glyph_string (head, tail, s)
16747 struct glyph_string **head, **tail;
16748 struct glyph_string *s;
16749{
16750 s->next = s->prev = NULL;
16751 append_glyph_string_lists (head, tail, s, s);
16752}
16753
16754
16755/* Get face and two-byte form of character glyph GLYPH on frame F.
16756 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
16757 a pointer to a realized face that is ready for display. */
16758
16759static INLINE struct face *
16760get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
16761 struct frame *f;
16762 struct glyph *glyph;
16763 XChar2b *char2b;
16764 int *two_byte_p;
16765{
16766 struct face *face;
16767
16768 xassert (glyph->type == CHAR_GLYPH);
16769 face = FACE_FROM_ID (f, glyph->face_id);
16770
16771 if (two_byte_p)
16772 *two_byte_p = 0;
16773
16774 if (!glyph->multibyte_p)
16775 {
16776 /* Unibyte case. We don't have to encode, but we have to make
16777 sure to use a face suitable for unibyte. */
16778 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16779 }
16780 else if (glyph->u.ch < 128
16781 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
16782 {
16783 /* Case of ASCII in a face known to fit ASCII. */
16784 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16785 }
16786 else
16787 {
16788 int c1, c2, charset;
16789
16790 /* Split characters into bytes. If c2 is -1 afterwards, C is
16791 really a one-byte character so that byte1 is zero. */
16792 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
16793 if (c2 > 0)
16794 STORE_XCHAR2B (char2b, c1, c2);
16795 else
16796 STORE_XCHAR2B (char2b, 0, c1);
16797
16798 /* Maybe encode the character in *CHAR2B. */
16799 if (charset != CHARSET_ASCII)
16800 {
16801 struct font_info *font_info
16802 = FONT_INFO_FROM_ID (f, face->font_info_id);
16803 if (font_info)
16804 glyph->font_type
16805 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
16806 }
16807 }
16808
16809 /* Make sure X resources of the face are allocated. */
16810 xassert (face != NULL);
16811 PREPARE_FACE_FOR_DISPLAY (f, face);
16812 return face;
16813}
16814
16815
16816/* Fill glyph string S with composition components specified by S->cmp.
16817
16818 FACES is an array of faces for all components of this composition.
16819 S->gidx is the index of the first component for S.
16820 OVERLAPS_P non-zero means S should draw the foreground only, and
16821 use its physical height for clipping.
16822
16823 Value is the index of a component not in S. */
16824
16825static int
16826fill_composite_glyph_string (s, faces, overlaps_p)
16827 struct glyph_string *s;
16828 struct face **faces;
16829 int overlaps_p;
16830{
16831 int i;
16832
16833 xassert (s);
16834
16835 s->for_overlaps_p = overlaps_p;
16836
16837 s->face = faces[s->gidx];
16838 s->font = s->face->font;
16839 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16840
16841 /* For all glyphs of this composition, starting at the offset
16842 S->gidx, until we reach the end of the definition or encounter a
16843 glyph that requires the different face, add it to S. */
16844 ++s->nchars;
16845 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
16846 ++s->nchars;
16847
16848 /* All glyph strings for the same composition has the same width,
16849 i.e. the width set for the first component of the composition. */
16850
16851 s->width = s->first_glyph->pixel_width;
16852
16853 /* If the specified font could not be loaded, use the frame's
16854 default font, but record the fact that we couldn't load it in
16855 the glyph string so that we can draw rectangles for the
16856 characters of the glyph string. */
16857 if (s->font == NULL)
16858 {
16859 s->font_not_found_p = 1;
16860 s->font = FRAME_FONT (s->f);
16861 }
16862
16863 /* Adjust base line for subscript/superscript text. */
16864 s->ybase += s->first_glyph->voffset;
16865
16866 xassert (s->face && s->face->gc);
16867
16868 /* This glyph string must always be drawn with 16-bit functions. */
16869 s->two_byte_p = 1;
16870
16871 return s->gidx + s->nchars;
16872}
16873
16874
16875/* Fill glyph string S from a sequence of character glyphs.
16876
16877 FACE_ID is the face id of the string. START is the index of the
16878 first glyph to consider, END is the index of the last + 1.
16879 OVERLAPS_P non-zero means S should draw the foreground only, and
16880 use its physical height for clipping.
16881
16882 Value is the index of the first glyph not in S. */
16883
16884static int
16885fill_glyph_string (s, face_id, start, end, overlaps_p)
16886 struct glyph_string *s;
16887 int face_id;
16888 int start, end, overlaps_p;
16889{
16890 struct glyph *glyph, *last;
16891 int voffset;
16892 int glyph_not_available_p;
16893
16894 xassert (s->f == XFRAME (s->w->frame));
16895 xassert (s->nchars == 0);
16896 xassert (start >= 0 && end > start);
16897
16898 s->for_overlaps_p = overlaps_p,
16899 glyph = s->row->glyphs[s->area] + start;
16900 last = s->row->glyphs[s->area] + end;
16901 voffset = glyph->voffset;
16902
16903 glyph_not_available_p = glyph->glyph_not_available_p;
16904
16905 while (glyph < last
16906 && glyph->type == CHAR_GLYPH
16907 && glyph->voffset == voffset
16908 /* Same face id implies same font, nowadays. */
16909 && glyph->face_id == face_id
16910 && glyph->glyph_not_available_p == glyph_not_available_p)
16911 {
16912 int two_byte_p;
16913
16914 s->face = get_glyph_face_and_encoding (s->f, glyph,
16915 s->char2b + s->nchars,
16916 &two_byte_p);
16917 s->two_byte_p = two_byte_p;
16918 ++s->nchars;
16919 xassert (s->nchars <= end - start);
16920 s->width += glyph->pixel_width;
16921 ++glyph;
16922 }
16923
16924 s->font = s->face->font;
16925 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16926
16927 /* If the specified font could not be loaded, use the frame's font,
16928 but record the fact that we couldn't load it in
16929 S->font_not_found_p so that we can draw rectangles for the
16930 characters of the glyph string. */
16931 if (s->font == NULL || glyph_not_available_p)
16932 {
16933 s->font_not_found_p = 1;
16934 s->font = FRAME_FONT (s->f);
16935 }
16936
16937 /* Adjust base line for subscript/superscript text. */
16938 s->ybase += voffset;
16939
16940 xassert (s->face && s->face->gc);
16941 return glyph - s->row->glyphs[s->area];
16942}
16943
16944
16945/* Fill glyph string S from image glyph S->first_glyph. */
16946
16947static void
16948fill_image_glyph_string (s)
16949 struct glyph_string *s;
16950{
16951 xassert (s->first_glyph->type == IMAGE_GLYPH);
16952 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
16953 xassert (s->img);
16954 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
16955 s->font = s->face->font;
16956 s->width = s->first_glyph->pixel_width;
16957
16958 /* Adjust base line for subscript/superscript text. */
16959 s->ybase += s->first_glyph->voffset;
16960}
16961
16962
16963/* Fill glyph string S from a sequence of stretch glyphs.
16964
16965 ROW is the glyph row in which the glyphs are found, AREA is the
16966 area within the row. START is the index of the first glyph to
16967 consider, END is the index of the last + 1.
16968
16969 Value is the index of the first glyph not in S. */
16970
16971static int
16972fill_stretch_glyph_string (s, row, area, start, end)
16973 struct glyph_string *s;
16974 struct glyph_row *row;
16975 enum glyph_row_area area;
16976 int start, end;
16977{
16978 struct glyph *glyph, *last;
16979 int voffset, face_id;
16980
16981 xassert (s->first_glyph->type == STRETCH_GLYPH);
16982
16983 glyph = s->row->glyphs[s->area] + start;
16984 last = s->row->glyphs[s->area] + end;
16985 face_id = glyph->face_id;
16986 s->face = FACE_FROM_ID (s->f, face_id);
16987 s->font = s->face->font;
16988 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16989 s->width = glyph->pixel_width;
16990 voffset = glyph->voffset;
16991
16992 for (++glyph;
16993 (glyph < last
16994 && glyph->type == STRETCH_GLYPH
16995 && glyph->voffset == voffset
16996 && glyph->face_id == face_id);
16997 ++glyph)
16998 s->width += glyph->pixel_width;
16999
17000 /* Adjust base line for subscript/superscript text. */
17001 s->ybase += voffset;
17002
17003 /* The case that face->gc == 0 is handled when drawing the glyph
17004 string by calling PREPARE_FACE_FOR_DISPLAY. */
17005 xassert (s->face);
17006 return glyph - s->row->glyphs[s->area];
17007}
17008
17009
17010/* EXPORT for RIF:
17011 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17012 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17013 assumed to be zero. */
17014
17015void
17016x_get_glyph_overhangs (glyph, f, left, right)
17017 struct glyph *glyph;
17018 struct frame *f;
17019 int *left, *right;
17020{
17021 *left = *right = 0;
17022
17023 if (glyph->type == CHAR_GLYPH)
17024 {
17025 XFontStruct *font;
17026 struct face *face;
17027 struct font_info *font_info;
17028 XChar2b char2b;
17029 XCharStruct *pcm;
17030
17031 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17032 font = face->font;
17033 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17034 if (font /* ++KFS: Should this be font_info ? */
17035 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17036 {
17037 if (pcm->rbearing > pcm->width)
17038 *right = pcm->rbearing - pcm->width;
17039 if (pcm->lbearing < 0)
17040 *left = -pcm->lbearing;
17041 }
17042 }
17043}
17044
17045
17046/* Return the index of the first glyph preceding glyph string S that
17047 is overwritten by S because of S's left overhang. Value is -1
17048 if no glyphs are overwritten. */
17049
17050static int
17051left_overwritten (s)
17052 struct glyph_string *s;
17053{
17054 int k;
17055
17056 if (s->left_overhang)
17057 {
17058 int x = 0, i;
17059 struct glyph *glyphs = s->row->glyphs[s->area];
17060 int first = s->first_glyph - glyphs;
17061
17062 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17063 x -= glyphs[i].pixel_width;
17064
17065 k = i + 1;
17066 }
17067 else
17068 k = -1;
17069
17070 return k;
17071}
17072
17073
17074/* Return the index of the first glyph preceding glyph string S that
17075 is overwriting S because of its right overhang. Value is -1 if no
17076 glyph in front of S overwrites S. */
17077
17078static int
17079left_overwriting (s)
17080 struct glyph_string *s;
17081{
17082 int i, k, x;
17083 struct glyph *glyphs = s->row->glyphs[s->area];
17084 int first = s->first_glyph - glyphs;
17085
17086 k = -1;
17087 x = 0;
17088 for (i = first - 1; i >= 0; --i)
17089 {
17090 int left, right;
17091 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17092 if (x + right > 0)
17093 k = i;
17094 x -= glyphs[i].pixel_width;
17095 }
17096
17097 return k;
17098}
17099
17100
17101/* Return the index of the last glyph following glyph string S that is
17102 not overwritten by S because of S's right overhang. Value is -1 if
17103 no such glyph is found. */
17104
17105static int
17106right_overwritten (s)
17107 struct glyph_string *s;
17108{
17109 int k = -1;
17110
17111 if (s->right_overhang)
17112 {
17113 int x = 0, i;
17114 struct glyph *glyphs = s->row->glyphs[s->area];
17115 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17116 int end = s->row->used[s->area];
17117
17118 for (i = first; i < end && s->right_overhang > x; ++i)
17119 x += glyphs[i].pixel_width;
17120
17121 k = i;
17122 }
17123
17124 return k;
17125}
17126
17127
17128/* Return the index of the last glyph following glyph string S that
17129 overwrites S because of its left overhang. Value is negative
17130 if no such glyph is found. */
17131
17132static int
17133right_overwriting (s)
17134 struct glyph_string *s;
17135{
17136 int i, k, x;
17137 int end = s->row->used[s->area];
17138 struct glyph *glyphs = s->row->glyphs[s->area];
17139 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17140
17141 k = -1;
17142 x = 0;
17143 for (i = first; i < end; ++i)
17144 {
17145 int left, right;
17146 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17147 if (x - left < 0)
17148 k = i;
17149 x += glyphs[i].pixel_width;
17150 }
17151
17152 return k;
17153}
17154
17155
17156/* Get face and two-byte form of character C in face FACE_ID on frame
17157 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17158 means we want to display multibyte text. DISPLAY_P non-zero means
17159 make sure that X resources for the face returned are allocated.
17160 Value is a pointer to a realized face that is ready for display if
17161 DISPLAY_P is non-zero. */
17162
17163static INLINE struct face *
17164get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17165 struct frame *f;
17166 int c, face_id;
17167 XChar2b *char2b;
17168 int multibyte_p, display_p;
17169{
17170 struct face *face = FACE_FROM_ID (f, face_id);
17171
17172 if (!multibyte_p)
17173 {
17174 /* Unibyte case. We don't have to encode, but we have to make
17175 sure to use a face suitable for unibyte. */
17176 STORE_XCHAR2B (char2b, 0, c);
17177 face_id = FACE_FOR_CHAR (f, face, c);
17178 face = FACE_FROM_ID (f, face_id);
17179 }
17180 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17181 {
17182 /* Case of ASCII in a face known to fit ASCII. */
17183 STORE_XCHAR2B (char2b, 0, c);
17184 }
17185 else
17186 {
17187 int c1, c2, charset;
17188
17189 /* Split characters into bytes. If c2 is -1 afterwards, C is
17190 really a one-byte character so that byte1 is zero. */
17191 SPLIT_CHAR (c, charset, c1, c2);
17192 if (c2 > 0)
17193 STORE_XCHAR2B (char2b, c1, c2);
17194 else
17195 STORE_XCHAR2B (char2b, 0, c1);
17196
17197 /* Maybe encode the character in *CHAR2B. */
17198 if (face->font != NULL)
17199 {
17200 struct font_info *font_info
17201 = FONT_INFO_FROM_ID (f, face->font_info_id);
17202 if (font_info)
17203 rif->encode_char (c, char2b, font_info, 0);
17204 }
17205 }
17206
17207 /* Make sure X resources of the face are allocated. */
17208#ifdef HAVE_X_WINDOWS
17209 if (display_p)
17210#endif
17211 {
17212 xassert (face != NULL);
17213 PREPARE_FACE_FOR_DISPLAY (f, face);
17214 }
17215
17216 return face;
17217}
17218
17219
17220/* Set background width of glyph string S. START is the index of the
17221 first glyph following S. LAST_X is the right-most x-position + 1
17222 in the drawing area. */
17223
17224static INLINE void
17225set_glyph_string_background_width (s, start, last_x)
17226 struct glyph_string *s;
17227 int start;
17228 int last_x;
17229{
17230 /* If the face of this glyph string has to be drawn to the end of
17231 the drawing area, set S->extends_to_end_of_line_p. */
17232 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17233
17234 if (start == s->row->used[s->area]
17235 && s->area == TEXT_AREA
17236 && ((s->hl == DRAW_NORMAL_TEXT
17237 && (s->row->fill_line_p
17238 || s->face->background != default_face->background
17239 || s->face->stipple != default_face->stipple
17240 || s->row->mouse_face_p))
17241 || s->hl == DRAW_MOUSE_FACE
17242 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17243 && s->row->fill_line_p)))
17244 s->extends_to_end_of_line_p = 1;
17245
17246 /* If S extends its face to the end of the line, set its
17247 background_width to the distance to the right edge of the drawing
17248 area. */
17249 if (s->extends_to_end_of_line_p)
17250 s->background_width = last_x - s->x + 1;
17251 else
17252 s->background_width = s->width;
17253}
17254
17255
17256/* Compute overhangs and x-positions for glyph string S and its
17257 predecessors, or successors. X is the starting x-position for S.
17258 BACKWARD_P non-zero means process predecessors. */
17259
17260static void
17261compute_overhangs_and_x (s, x, backward_p)
17262 struct glyph_string *s;
17263 int x;
17264 int backward_p;
17265{
17266 if (backward_p)
17267 {
17268 while (s)
17269 {
17270 if (rif->compute_glyph_string_overhangs)
17271 rif->compute_glyph_string_overhangs (s);
17272 x -= s->width;
17273 s->x = x;
17274 s = s->prev;
17275 }
17276 }
17277 else
17278 {
17279 while (s)
17280 {
17281 if (rif->compute_glyph_string_overhangs)
17282 rif->compute_glyph_string_overhangs (s);
17283 s->x = x;
17284 x += s->width;
17285 s = s->next;
17286 }
17287 }
17288}
17289
17290
17291
fa3c6b4d 17292/* The following macros are only called from draw_glyphs below.
133c764e
KS
17293 They reference the following parameters of that function directly:
17294 `w', `row', `area', and `overlap_p'
17295 as well as the following local variables:
17296 `s', `f', and `hdc' (in W32) */
17297
17298#ifdef HAVE_NTGUI
17299/* On W32, silently add local `hdc' variable to argument list of
17300 init_glyph_string. */
17301#define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17302 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17303#else
17304#define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17305 init_glyph_string (s, char2b, w, row, area, start, hl)
17306#endif
17307
17308/* Add a glyph string for a stretch glyph to the list of strings
17309 between HEAD and TAIL. START is the index of the stretch glyph in
17310 row area AREA of glyph row ROW. END is the index of the last glyph
17311 in that glyph row area. X is the current output position assigned
17312 to the new glyph string constructed. HL overrides that face of the
17313 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17314 is the right-most x-position of the drawing area. */
17315
17316/* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17317 and below -- keep them on one line. */
17318#define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17319 do \
17320 { \
17321 s = (struct glyph_string *) alloca (sizeof *s); \
17322 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17323 START = fill_stretch_glyph_string (s, row, area, START, END); \
17324 append_glyph_string (&HEAD, &TAIL, s); \
17325 s->x = (X); \
17326 } \
17327 while (0)
17328
17329
17330/* Add a glyph string for an image glyph to the list of strings
17331 between HEAD and TAIL. START is the index of the image glyph in
17332 row area AREA of glyph row ROW. END is the index of the last glyph
17333 in that glyph row area. X is the current output position assigned
17334 to the new glyph string constructed. HL overrides that face of the
17335 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17336 is the right-most x-position of the drawing area. */
17337
17338#define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17339 do \
17340 { \
17341 s = (struct glyph_string *) alloca (sizeof *s); \
17342 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17343 fill_image_glyph_string (s); \
17344 append_glyph_string (&HEAD, &TAIL, s); \
17345 ++START; \
17346 s->x = (X); \
17347 } \
17348 while (0)
17349
17350
17351/* Add a glyph string for a sequence of character glyphs to the list
17352 of strings between HEAD and TAIL. START is the index of the first
17353 glyph in row area AREA of glyph row ROW that is part of the new
17354 glyph string. END is the index of the last glyph in that glyph row
17355 area. X is the current output position assigned to the new glyph
17356 string constructed. HL overrides that face of the glyph; e.g. it
17357 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17358 right-most x-position of the drawing area. */
17359
17360#define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17361 do \
17362 { \
17363 int c, face_id; \
17364 XChar2b *char2b; \
17365 \
17366 c = (row)->glyphs[area][START].u.ch; \
17367 face_id = (row)->glyphs[area][START].face_id; \
17368 \
17369 s = (struct glyph_string *) alloca (sizeof *s); \
17370 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17371 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17372 append_glyph_string (&HEAD, &TAIL, s); \
17373 s->x = (X); \
17374 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17375 } \
17376 while (0)
17377
17378
17379/* Add a glyph string for a composite sequence to the list of strings
17380 between HEAD and TAIL. START is the index of the first glyph in
17381 row area AREA of glyph row ROW that is part of the new glyph
17382 string. END is the index of the last glyph in that glyph row area.
17383 X is the current output position assigned to the new glyph string
17384 constructed. HL overrides that face of the glyph; e.g. it is
17385 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17386 x-position of the drawing area. */
17387
17388#define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17389 do { \
17390 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17391 int face_id = (row)->glyphs[area][START].face_id; \
17392 struct face *base_face = FACE_FROM_ID (f, face_id); \
17393 struct composition *cmp = composition_table[cmp_id]; \
17394 int glyph_len = cmp->glyph_len; \
17395 XChar2b *char2b; \
17396 struct face **faces; \
17397 struct glyph_string *first_s = NULL; \
17398 int n; \
17399 \
17400 base_face = base_face->ascii_face; \
17401 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17402 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17403 /* At first, fill in `char2b' and `faces'. */ \
17404 for (n = 0; n < glyph_len; n++) \
17405 { \
17406 int c = COMPOSITION_GLYPH (cmp, n); \
17407 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17408 faces[n] = FACE_FROM_ID (f, this_face_id); \
17409 get_char_face_and_encoding (f, c, this_face_id, \
17410 char2b + n, 1, 1); \
17411 } \
17412 \
17413 /* Make glyph_strings for each glyph sequence that is drawable by \
17414 the same face, and append them to HEAD/TAIL. */ \
17415 for (n = 0; n < cmp->glyph_len;) \
17416 { \
17417 s = (struct glyph_string *) alloca (sizeof *s); \
17418 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17419 append_glyph_string (&(HEAD), &(TAIL), s); \
17420 s->cmp = cmp; \
17421 s->gidx = n; \
17422 s->x = (X); \
17423 \
17424 if (n == 0) \
17425 first_s = s; \
17426 \
17427 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17428 } \
17429 \
17430 ++START; \
17431 s = first_s; \
17432 } while (0)
17433
17434
17435/* Build a list of glyph strings between HEAD and TAIL for the glyphs
17436 of AREA of glyph row ROW on window W between indices START and END.
17437 HL overrides the face for drawing glyph strings, e.g. it is
17438 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17439 x-positions of the drawing area.
17440
17441 This is an ugly monster macro construct because we must use alloca
fa3c6b4d 17442 to allocate glyph strings (because draw_glyphs can be called
133c764e
KS
17443 asynchronously). */
17444
17445#define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17446 do \
17447 { \
17448 HEAD = TAIL = NULL; \
17449 while (START < END) \
17450 { \
17451 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17452 switch (first_glyph->type) \
17453 { \
17454 case CHAR_GLYPH: \
17455 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17456 HL, X, LAST_X); \
17457 break; \
17458 \
17459 case COMPOSITE_GLYPH: \
17460 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17461 HL, X, LAST_X); \
17462 break; \
17463 \
17464 case STRETCH_GLYPH: \
17465 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17466 HL, X, LAST_X); \
17467 break; \
17468 \
17469 case IMAGE_GLYPH: \
17470 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17471 HL, X, LAST_X); \
17472 break; \
17473 \
17474 default: \
17475 abort (); \
17476 } \
17477 \
17478 set_glyph_string_background_width (s, START, LAST_X); \
17479 (X) += s->width; \
17480 } \
17481 } \
17482 while (0)
17483
17484
17485/* Draw glyphs between START and END in AREA of ROW on window W,
17486 starting at x-position X. X is relative to AREA in W. HL is a
17487 face-override with the following meaning:
17488
17489 DRAW_NORMAL_TEXT draw normally
17490 DRAW_CURSOR draw in cursor face
17491 DRAW_MOUSE_FACE draw in mouse face.
17492 DRAW_INVERSE_VIDEO draw in mode line face
17493 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17494 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17495
17496 If OVERLAPS_P is non-zero, draw only the foreground of characters
17497 and clip to the physical height of ROW.
17498
17499 Value is the x-position reached, relative to AREA of W. */
17500
fa3c6b4d
KS
17501static int
17502draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
133c764e
KS
17503 struct window *w;
17504 int x;
17505 struct glyph_row *row;
17506 enum glyph_row_area area;
17507 int start, end;
17508 enum draw_glyphs_face hl;
17509 int overlaps_p;
17510{
17511 struct glyph_string *head, *tail;
17512 struct glyph_string *s;
17513 int last_x, area_width;
17514 int x_reached;
17515 int i, j;
17516 struct frame *f = XFRAME (WINDOW_FRAME (w));
17517 DECLARE_HDC (hdc);
17518
17519 ALLOCATE_HDC (hdc, f);
17520
17521 /* Let's rather be paranoid than getting a SEGV. */
17522 end = min (end, row->used[area]);
17523 start = max (0, start);
17524 start = min (end, start);
17525
17526 /* Translate X to frame coordinates. Set last_x to the right
17527 end of the drawing area. */
17528 if (row->full_width_p)
17529 {
17530 /* X is relative to the left edge of W, without scroll bars
17531 or fringes. */
da8b7f4f
KS
17532 x += WINDOW_LEFT_EDGE_X (w);
17533 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
133c764e
KS
17534 }
17535 else
17536 {
da8b7f4f
KS
17537 int area_left = window_box_left (w, area);
17538 x += area_left;
133c764e 17539 area_width = window_box_width (w, area);
da8b7f4f 17540 last_x = area_left + area_width;
133c764e
KS
17541 }
17542
17543 /* Build a doubly-linked list of glyph_string structures between
17544 head and tail from what we have to draw. Note that the macro
17545 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17546 the reason we use a separate variable `i'. */
17547 i = start;
17548 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17549 if (tail)
17550 x_reached = tail->x + tail->background_width;
17551 else
17552 x_reached = x;
17553
17554 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17555 the row, redraw some glyphs in front or following the glyph
17556 strings built above. */
17557 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17558 {
17559 int dummy_x = 0;
17560 struct glyph_string *h, *t;
17561
17562 /* Compute overhangs for all glyph strings. */
17563 if (rif->compute_glyph_string_overhangs)
17564 for (s = head; s; s = s->next)
17565 rif->compute_glyph_string_overhangs (s);
17566
17567 /* Prepend glyph strings for glyphs in front of the first glyph
17568 string that are overwritten because of the first glyph
17569 string's left overhang. The background of all strings
17570 prepended must be drawn because the first glyph string
17571 draws over it. */
17572 i = left_overwritten (head);
17573 if (i >= 0)
17574 {
17575 j = i;
17576 BUILD_GLYPH_STRINGS (j, start, h, t,
17577 DRAW_NORMAL_TEXT, dummy_x, last_x);
17578 start = i;
17579 compute_overhangs_and_x (t, head->x, 1);
17580 prepend_glyph_string_lists (&head, &tail, h, t);
17581 }
17582
17583 /* Prepend glyph strings for glyphs in front of the first glyph
17584 string that overwrite that glyph string because of their
17585 right overhang. For these strings, only the foreground must
17586 be drawn, because it draws over the glyph string at `head'.
17587 The background must not be drawn because this would overwrite
17588 right overhangs of preceding glyphs for which no glyph
17589 strings exist. */
17590 i = left_overwriting (head);
17591 if (i >= 0)
17592 {
17593 BUILD_GLYPH_STRINGS (i, start, h, t,
17594 DRAW_NORMAL_TEXT, dummy_x, last_x);
17595 for (s = h; s; s = s->next)
17596 s->background_filled_p = 1;
17597 compute_overhangs_and_x (t, head->x, 1);
17598 prepend_glyph_string_lists (&head, &tail, h, t);
17599 }
17600
17601 /* Append glyphs strings for glyphs following the last glyph
17602 string tail that are overwritten by tail. The background of
17603 these strings has to be drawn because tail's foreground draws
17604 over it. */
17605 i = right_overwritten (tail);
17606 if (i >= 0)
17607 {
17608 BUILD_GLYPH_STRINGS (end, i, h, t,
17609 DRAW_NORMAL_TEXT, x, last_x);
17610 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17611 append_glyph_string_lists (&head, &tail, h, t);
17612 }
17613
17614 /* Append glyph strings for glyphs following the last glyph
17615 string tail that overwrite tail. The foreground of such
17616 glyphs has to be drawn because it writes into the background
17617 of tail. The background must not be drawn because it could
17618 paint over the foreground of following glyphs. */
17619 i = right_overwriting (tail);
17620 if (i >= 0)
17621 {
17622 BUILD_GLYPH_STRINGS (end, i, h, t,
17623 DRAW_NORMAL_TEXT, x, last_x);
17624 for (s = h; s; s = s->next)
17625 s->background_filled_p = 1;
17626 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17627 append_glyph_string_lists (&head, &tail, h, t);
17628 }
17629 }
17630
17631 /* Draw all strings. */
17632 for (s = head; s; s = s->next)
17633 rif->draw_glyph_string (s);
17634
17635 if (area == TEXT_AREA
17636 && !row->full_width_p
17637 /* When drawing overlapping rows, only the glyph strings'
17638 foreground is drawn, which doesn't erase a cursor
17639 completely. */
17640 && !overlaps_p)
17641 {
17642 int x0 = head ? head->x : x;
17643 int x1 = tail ? tail->x + tail->background_width : x;
17644
da8b7f4f
KS
17645 int text_left = window_box_left (w, TEXT_AREA);
17646 x0 -= text_left;
17647 x1 -= text_left;
133c764e 17648
da8b7f4f 17649 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
133c764e
KS
17650 row->y, MATRIX_ROW_BOTTOM_Y (row));
17651 }
17652
17653 /* Value is the x-position up to which drawn, relative to AREA of W.
17654 This doesn't include parts drawn because of overhangs. */
da8b7f4f
KS
17655 if (row->full_width_p)
17656 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
17657 else
17658 x_reached -= window_box_left (w, area);
133c764e
KS
17659
17660 RELEASE_HDC (hdc, f);
17661
17662 return x_reached;
17663}
17664
17665
17666/* Store one glyph for IT->char_to_display in IT->glyph_row.
17667 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17668
17669static INLINE void
17670append_glyph (it)
17671 struct it *it;
17672{
17673 struct glyph *glyph;
17674 enum glyph_row_area area = it->area;
17675
17676 xassert (it->glyph_row);
17677 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
17678
17679 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17680 if (glyph < it->glyph_row->glyphs[area + 1])
17681 {
17682 glyph->charpos = CHARPOS (it->position);
17683 glyph->object = it->object;
17684 glyph->pixel_width = it->pixel_width;
17685 glyph->voffset = it->voffset;
17686 glyph->type = CHAR_GLYPH;
17687 glyph->multibyte_p = it->multibyte_p;
17688 glyph->left_box_line_p = it->start_of_box_run_p;
17689 glyph->right_box_line_p = it->end_of_box_run_p;
17690 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17691 || it->phys_descent > it->descent);
17692 glyph->padding_p = 0;
17693 glyph->glyph_not_available_p = it->glyph_not_available_p;
17694 glyph->face_id = it->face_id;
17695 glyph->u.ch = it->char_to_display;
17696 glyph->font_type = FONT_TYPE_UNKNOWN;
17697 ++it->glyph_row->used[area];
17698 }
17699}
17700
17701/* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
17702 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17703
17704static INLINE void
17705append_composite_glyph (it)
17706 struct it *it;
17707{
17708 struct glyph *glyph;
17709 enum glyph_row_area area = it->area;
17710
17711 xassert (it->glyph_row);
17712
17713 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17714 if (glyph < it->glyph_row->glyphs[area + 1])
17715 {
17716 glyph->charpos = CHARPOS (it->position);
17717 glyph->object = it->object;
17718 glyph->pixel_width = it->pixel_width;
17719 glyph->voffset = it->voffset;
17720 glyph->type = COMPOSITE_GLYPH;
17721 glyph->multibyte_p = it->multibyte_p;
17722 glyph->left_box_line_p = it->start_of_box_run_p;
17723 glyph->right_box_line_p = it->end_of_box_run_p;
17724 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17725 || it->phys_descent > it->descent);
17726 glyph->padding_p = 0;
17727 glyph->glyph_not_available_p = 0;
17728 glyph->face_id = it->face_id;
17729 glyph->u.cmp_id = it->cmp_id;
17730 glyph->font_type = FONT_TYPE_UNKNOWN;
17731 ++it->glyph_row->used[area];
17732 }
17733}
17734
17735
17736/* Change IT->ascent and IT->height according to the setting of
17737 IT->voffset. */
17738
17739static INLINE void
17740take_vertical_position_into_account (it)
17741 struct it *it;
17742{
17743 if (it->voffset)
17744 {
17745 if (it->voffset < 0)
17746 /* Increase the ascent so that we can display the text higher
17747 in the line. */
17748 it->ascent += abs (it->voffset);
17749 else
17750 /* Increase the descent so that we can display the text lower
17751 in the line. */
17752 it->descent += it->voffset;
17753 }
17754}
17755
17756
17757/* Produce glyphs/get display metrics for the image IT is loaded with.
17758 See the description of struct display_iterator in dispextern.h for
17759 an overview of struct display_iterator. */
17760
17761static void
17762produce_image_glyph (it)
17763 struct it *it;
17764{
17765 struct image *img;
17766 struct face *face;
17767
17768 xassert (it->what == IT_IMAGE);
17769
17770 face = FACE_FROM_ID (it->f, it->face_id);
17771 img = IMAGE_FROM_ID (it->f, it->image_id);
17772 xassert (img);
17773
17774 /* Make sure X resources of the face and image are loaded. */
17775 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17776 prepare_image_for_display (it->f, img);
17777
17778 it->ascent = it->phys_ascent = image_ascent (img, face);
17779 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
17780 it->pixel_width = img->width + 2 * img->hmargin;
17781
17782 it->nglyphs = 1;
17783
17784 if (face->box != FACE_NO_BOX)
17785 {
17786 if (face->box_line_width > 0)
17787 {
17788 it->ascent += face->box_line_width;
17789 it->descent += face->box_line_width;
17790 }
17791
17792 if (it->start_of_box_run_p)
17793 it->pixel_width += abs (face->box_line_width);
17794 if (it->end_of_box_run_p)
17795 it->pixel_width += abs (face->box_line_width);
17796 }
17797
17798 take_vertical_position_into_account (it);
17799
17800 if (it->glyph_row)
17801 {
17802 struct glyph *glyph;
17803 enum glyph_row_area area = it->area;
17804
17805 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17806 if (glyph < it->glyph_row->glyphs[area + 1])
17807 {
17808 glyph->charpos = CHARPOS (it->position);
17809 glyph->object = it->object;
17810 glyph->pixel_width = it->pixel_width;
17811 glyph->voffset = it->voffset;
17812 glyph->type = IMAGE_GLYPH;
17813 glyph->multibyte_p = it->multibyte_p;
17814 glyph->left_box_line_p = it->start_of_box_run_p;
17815 glyph->right_box_line_p = it->end_of_box_run_p;
17816 glyph->overlaps_vertically_p = 0;
17817 glyph->padding_p = 0;
17818 glyph->glyph_not_available_p = 0;
17819 glyph->face_id = it->face_id;
17820 glyph->u.img_id = img->id;
17821 glyph->font_type = FONT_TYPE_UNKNOWN;
17822 ++it->glyph_row->used[area];
17823 }
17824 }
17825}
17826
17827
17828/* Append a stretch glyph to IT->glyph_row. OBJECT is the source
17829 of the glyph, WIDTH and HEIGHT are the width and height of the
f65536fa 17830 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
133c764e
KS
17831
17832static void
17833append_stretch_glyph (it, object, width, height, ascent)
17834 struct it *it;
17835 Lisp_Object object;
17836 int width, height;
f65536fa 17837 int ascent;
133c764e
KS
17838{
17839 struct glyph *glyph;
17840 enum glyph_row_area area = it->area;
17841
f65536fa 17842 xassert (ascent >= 0 && ascent <= height);
133c764e
KS
17843
17844 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17845 if (glyph < it->glyph_row->glyphs[area + 1])
17846 {
17847 glyph->charpos = CHARPOS (it->position);
17848 glyph->object = object;
17849 glyph->pixel_width = width;
17850 glyph->voffset = it->voffset;
17851 glyph->type = STRETCH_GLYPH;
17852 glyph->multibyte_p = it->multibyte_p;
17853 glyph->left_box_line_p = it->start_of_box_run_p;
17854 glyph->right_box_line_p = it->end_of_box_run_p;
17855 glyph->overlaps_vertically_p = 0;
17856 glyph->padding_p = 0;
17857 glyph->glyph_not_available_p = 0;
17858 glyph->face_id = it->face_id;
f65536fa 17859 glyph->u.stretch.ascent = ascent;
133c764e
KS
17860 glyph->u.stretch.height = height;
17861 glyph->font_type = FONT_TYPE_UNKNOWN;
17862 ++it->glyph_row->used[area];
17863 }
17864}
17865
17866
f65536fa
KS
17867/* Calculate a width or height in pixels from a specification using
17868 the following elements:
17869
17870 SPEC ::=
17871 NUM - a (fractional) multiple of the default font width/height
17872 (NUM) - specifies exactly NUM pixels
17873 UNIT - a fixed number of pixels, see below.
17874 ELEMENT - size of a display element in pixels, see below.
17875 (NUM . SPEC) - equals NUM * SPEC
17876 (+ SPEC SPEC ...) - add pixel values
17877 (- SPEC SPEC ...) - subtract pixel values
17878 (- SPEC) - negate pixel value
17879
17880 NUM ::=
17881 INT or FLOAT - a number constant
17882 SYMBOL - use symbol's (buffer local) variable binding.
17883
17884 UNIT ::=
17885 in - pixels per inch *)
17886 mm - pixels per 1/1000 meter *)
17887 cm - pixels per 1/100 meter *)
17888 width - width of current font in pixels.
17889 height - height of current font in pixels.
17890
17891 *) using the ratio(s) defined in display-pixels-per-inch.
17892
17893 ELEMENT ::=
17894
17895 left-fringe - left fringe width in pixels
17896 (left-fringe . nil) - left fringe width if inside margins, else 0
17897 (left-fringe . t) - left fringe width if outside margins, else 0
17898
17899 right-fringe - right fringe width in pixels
17900 (right-fringe . nil) - right fringe width if inside margins, else 0
17901 (right-fringe . t) - right fringe width if outside margins, else 0
17902
17903 left-margin - left margin width in pixels
17904 right-margin - right margin width in pixels
17905
17906 scroll-bar - scroll-bar area width in pixels
17907 (scroll-bar . left) - scroll-bar width if on left, else 0
17908 (scroll-bar . right) - scroll-bar width if on right, else 0
17909
17910 Examples:
17911
17912 Pixels corresponding to 5 inches:
17913 (5 . in)
17914
17915 Total width of non-text areas on left side of window:
17916 (+ left-fringe left-margin (scroll-bar . left))
17917
17918 Total width of fringes if inside display margins:
17919 (+ (left-fringe) (right-fringe))
17920
17921 Width of left margin minus width of 1 character in the default font:
17922 (- left-margin 1)
17923
17924 Width of left margin minus width of 2 characters in the current font:
17925 (- left-margin (2 . width))
17926
17927 Width of left fringe plus left margin minus one pixel:
17928 (- (+ left-fringe left-margin) (1))
17929 (+ left-fringe left-margin (- (1)))
17930 (+ left-fringe left-margin (-1))
17931
17932*/
17933
17934#define NUMVAL(X) \
17935 ((INTEGERP (X) || FLOATP (X)) \
17936 ? XFLOATINT (X) \
17937 : - 1)
17938
17939static int
17940calc_pixel_width_or_height (res, it, prop, font, width_p)
17941 double *res;
17942 struct it *it;
17943 Lisp_Object prop;
17944 XFontStruct *font;
17945 int width_p;
17946{
17947 double pixels;
17948
17949#define OK_PIXELS(val) ((*res = (val)), 1)
17950
17951 if (SYMBOLP (prop))
17952 {
17953 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17954 {
17955 char *unit = SDATA (SYMBOL_NAME (prop));
17956
17957 if (unit[0] == 'i' && unit[1] == 'n')
17958 pixels = 1.0;
17959 else if (unit[0] == 'm' && unit[1] == 'm')
17960 pixels = 25.4;
17961 else if (unit[0] == 'c' && unit[1] == 'm')
17962 pixels = 2.54;
17963 else
17964 pixels = 0;
17965 if (pixels > 0)
17966 {
17967 double ppi;
17968 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17969 || (CONSP (Vdisplay_pixels_per_inch)
17970 && (ppi = (width_p
17971 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17972 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17973 ppi > 0)))
17974 return OK_PIXELS (ppi / pixels);
17975
17976 return 0;
17977 }
17978 }
17979
17980 if (EQ (prop, Qheight))
17981 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
17982 if (EQ (prop, Qwidth))
17983 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
17984 if (EQ (prop, Qleft_fringe))
17985 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17986 if (EQ (prop, Qright_fringe))
17987 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17988 if (EQ (prop, Qleft_margin))
17989 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17990 if (EQ (prop, Qright_margin))
17991 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17992 if (EQ (prop, Qscroll_bar))
17993 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17994
17995 prop = Fbuffer_local_value (prop, it->w->buffer);
17996 }
17997
17998 if (INTEGERP (prop) || FLOATP (prop))
17999 {
18000 int base_unit = (width_p
18001 ? FRAME_COLUMN_WIDTH (it->f)
18002 : FRAME_LINE_HEIGHT (it->f));
18003 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18004 }
18005
18006 if (CONSP (prop))
18007 {
18008 Lisp_Object car = XCAR (prop);
18009 Lisp_Object cdr = XCDR (prop);
18010
18011 if (SYMBOLP (car))
18012 {
18013 if (EQ (car, Qplus) || EQ (car, Qminus))
18014 {
18015 int first = 1;
18016 double px;
18017
18018 pixels = 0;
18019 while (CONSP (cdr))
18020 {
18021 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr), font, width_p))
18022 return 0;
18023 if (first)
18024 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18025 else
18026 pixels += px;
18027 cdr = XCDR (cdr);
18028 }
18029 if (EQ (car, Qminus))
18030 pixels = -pixels;
18031 return OK_PIXELS (pixels);
18032 }
18033
18034 if (EQ (car, Qleft_fringe))
18035 return OK_PIXELS ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18036 == !NILP (cdr))
18037 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
18038 : 0);
18039 if (EQ (car, Qright_fringe))
18040 return OK_PIXELS ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18041 == !NILP (cdr))
18042 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18043 : 0);
18044 if (EQ (car, Qscroll_bar))
18045 return OK_PIXELS ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18046 == EQ (cdr, Qleft))
18047 ? WINDOW_SCROLL_BAR_AREA_WIDTH (it->w)
18048 : 0);
18049
18050 car = Fbuffer_local_value (car, it->w->buffer);
18051 }
18052
18053 if (INTEGERP (car) || FLOATP (car))
18054 {
18055 double fact;
18056 pixels = XFLOATINT (car);
18057 if (NILP (cdr))
18058 return OK_PIXELS (pixels);
18059 if (calc_pixel_width_or_height (&fact, it, cdr, font, width_p))
18060 return OK_PIXELS (pixels * fact);
18061 return 0;
18062 }
18063
18064 return 0;
18065 }
18066
18067 return 0;
18068}
18069
133c764e
KS
18070/* Produce a stretch glyph for iterator IT. IT->object is the value
18071 of the glyph property displayed. The value must be a list
18072 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18073 being recognized:
18074
18075 1. `:width WIDTH' specifies that the space should be WIDTH *
18076 canonical char width wide. WIDTH may be an integer or floating
18077 point number.
18078
18079 2. `:relative-width FACTOR' specifies that the width of the stretch
18080 should be computed from the width of the first character having the
18081 `glyph' property, and should be FACTOR times that width.
18082
18083 3. `:align-to HPOS' specifies that the space should be wide enough
18084 to reach HPOS, a value in canonical character units.
18085
18086 Exactly one of the above pairs must be present.
18087
18088 4. `:height HEIGHT' specifies that the height of the stretch produced
18089 should be HEIGHT, measured in canonical character units.
18090
18091 5. `:relative-height FACTOR' specifies that the height of the
18092 stretch should be FACTOR times the height of the characters having
18093 the glyph property.
18094
18095 Either none or exactly one of 4 or 5 must be present.
18096
18097 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18098 of the stretch should be used for the ascent of the stretch.
18099 ASCENT must be in the range 0 <= ASCENT <= 100. */
18100
133c764e
KS
18101static void
18102produce_stretch_glyph (it)
18103 struct it *it;
18104{
f65536fa 18105 /* (space :width WIDTH :height HEIGHT ...) */
133c764e
KS
18106 Lisp_Object prop, plist;
18107 int width = 0, height = 0;
f65536fa
KS
18108 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18109 int ascent = 0;
18110 double tem;
133c764e
KS
18111 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18112 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18113
18114 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18115
18116 /* List should start with `space'. */
18117 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18118 plist = XCDR (it->object);
18119
18120 /* Compute the width of the stretch. */
f65536fa
KS
18121 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18122 && calc_pixel_width_or_height (&tem, it, prop, font, 1))
18123 {
18124 /* Absolute width `:width WIDTH' specified and valid. */
18125 zero_width_ok_p = 1;
18126 width = (int)tem;
18127 }
133c764e
KS
18128 else if (prop = Fplist_get (plist, QCrelative_width),
18129 NUMVAL (prop) > 0)
18130 {
18131 /* Relative width `:relative-width FACTOR' specified and valid.
18132 Compute the width of the characters having the `glyph'
18133 property. */
18134 struct it it2;
18135 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18136
18137 it2 = *it;
18138 if (it->multibyte_p)
18139 {
18140 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18141 - IT_BYTEPOS (*it));
18142 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18143 }
18144 else
18145 it2.c = *p, it2.len = 1;
18146
18147 it2.glyph_row = NULL;
18148 it2.what = IT_CHARACTER;
18149 x_produce_glyphs (&it2);
18150 width = NUMVAL (prop) * it2.pixel_width;
18151 }
f65536fa
KS
18152 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18153 && calc_pixel_width_or_height (&tem, it, prop, font, 1))
18154 {
18155 width = max (0, (int)tem - it->current_x);
18156 zero_width_ok_p = 1;
18157 }
133c764e
KS
18158 else
18159 /* Nothing specified -> width defaults to canonical char width. */
da8b7f4f 18160 width = FRAME_COLUMN_WIDTH (it->f);
133c764e 18161
f65536fa
KS
18162 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18163 width = 1;
18164
133c764e 18165 /* Compute height. */
f65536fa
KS
18166 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18167 && calc_pixel_width_or_height (&tem, it, prop, font, 0))
18168 {
18169 height = (int)tem;
18170 zero_height_ok_p = 1;
18171 }
133c764e
KS
18172 else if (prop = Fplist_get (plist, QCrelative_height),
18173 NUMVAL (prop) > 0)
18174 height = FONT_HEIGHT (font) * NUMVAL (prop);
18175 else
18176 height = FONT_HEIGHT (font);
18177
f65536fa
KS
18178 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18179 height = 1;
18180
133c764e
KS
18181 /* Compute percentage of height used for ascent. If
18182 `:ascent ASCENT' is present and valid, use that. Otherwise,
18183 derive the ascent from the font in use. */
18184 if (prop = Fplist_get (plist, QCascent),
18185 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
f65536fa
KS
18186 ascent = height * NUMVAL (prop) / 100.0;
18187 else if (!NILP (prop)
18188 && calc_pixel_width_or_height (&tem, it, prop, font, 0))
18189 ascent = min (max (0, (int)tem), height);
133c764e 18190 else
f65536fa 18191 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
133c764e 18192
f65536fa 18193 if (width > 0 && height > 0 && it->glyph_row)
133c764e
KS
18194 {
18195 Lisp_Object object = it->stack[it->sp - 1].string;
18196 if (!STRINGP (object))
18197 object = it->w->buffer;
18198 append_stretch_glyph (it, object, width, height, ascent);
18199 }
18200
18201 it->pixel_width = width;
f65536fa 18202 it->ascent = it->phys_ascent = ascent;
133c764e 18203 it->descent = it->phys_descent = height - it->ascent;
f65536fa 18204 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
133c764e 18205
f65536fa 18206 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
133c764e
KS
18207 {
18208 if (face->box_line_width > 0)
18209 {
18210 it->ascent += face->box_line_width;
18211 it->descent += face->box_line_width;
18212 }
18213
18214 if (it->start_of_box_run_p)
18215 it->pixel_width += abs (face->box_line_width);
18216 if (it->end_of_box_run_p)
18217 it->pixel_width += abs (face->box_line_width);
18218 }
18219
18220 take_vertical_position_into_account (it);
18221}
18222
18223/* RIF:
18224 Produce glyphs/get display metrics for the display element IT is
18225 loaded with. See the description of struct display_iterator in
18226 dispextern.h for an overview of struct display_iterator. */
18227
18228void
18229x_produce_glyphs (it)
18230 struct it *it;
18231{
18232 it->glyph_not_available_p = 0;
18233
18234 if (it->what == IT_CHARACTER)
18235 {
18236 XChar2b char2b;
18237 XFontStruct *font;
18238 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18239 XCharStruct *pcm;
18240 int font_not_found_p;
18241 struct font_info *font_info;
18242 int boff; /* baseline offset */
18243 /* We may change it->multibyte_p upon unibyte<->multibyte
18244 conversion. So, save the current value now and restore it
18245 later.
18246
18247 Note: It seems that we don't have to record multibyte_p in
18248 struct glyph because the character code itself tells if or
18249 not the character is multibyte. Thus, in the future, we must
18250 consider eliminating the field `multibyte_p' in the struct
18251 glyph. */
18252 int saved_multibyte_p = it->multibyte_p;
18253
18254 /* Maybe translate single-byte characters to multibyte, or the
18255 other way. */
18256 it->char_to_display = it->c;
18257 if (!ASCII_BYTE_P (it->c))
18258 {
18259 if (unibyte_display_via_language_environment
18260 && SINGLE_BYTE_CHAR_P (it->c)
18261 && (it->c >= 0240
18262 || !NILP (Vnonascii_translation_table)))
18263 {
18264 it->char_to_display = unibyte_char_to_multibyte (it->c);
18265 it->multibyte_p = 1;
18266 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18267 face = FACE_FROM_ID (it->f, it->face_id);
18268 }
18269 else if (!SINGLE_BYTE_CHAR_P (it->c)
18270 && !it->multibyte_p)
18271 {
18272 it->multibyte_p = 1;
18273 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18274 face = FACE_FROM_ID (it->f, it->face_id);
18275 }
18276 }
18277
18278 /* Get font to use. Encode IT->char_to_display. */
18279 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18280 &char2b, it->multibyte_p, 0);
18281 font = face->font;
18282
18283 /* When no suitable font found, use the default font. */
18284 font_not_found_p = font == NULL;
18285 if (font_not_found_p)
18286 {
18287 font = FRAME_FONT (it->f);
18288 boff = FRAME_BASELINE_OFFSET (it->f);
18289 font_info = NULL;
18290 }
18291 else
18292 {
18293 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18294 boff = font_info->baseline_offset;
18295 if (font_info->vertical_centering)
18296 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18297 }
18298
18299 if (it->char_to_display >= ' '
18300 && (!it->multibyte_p || it->char_to_display < 128))
18301 {
18302 /* Either unibyte or ASCII. */
18303 int stretched_p;
18304
18305 it->nglyphs = 1;
18306
18307 pcm = rif->per_char_metric (font, &char2b,
18308 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18309 it->ascent = FONT_BASE (font) + boff;
18310 it->descent = FONT_DESCENT (font) - boff;
18311
18312 if (pcm)
18313 {
18314 it->phys_ascent = pcm->ascent + boff;
18315 it->phys_descent = pcm->descent - boff;
18316 it->pixel_width = pcm->width;
18317 }
18318 else
18319 {
18320 it->glyph_not_available_p = 1;
18321 it->phys_ascent = FONT_BASE (font) + boff;
18322 it->phys_descent = FONT_DESCENT (font) - boff;
18323 it->pixel_width = FONT_WIDTH (font);
18324 }
18325
18326 /* If this is a space inside a region of text with
18327 `space-width' property, change its width. */
18328 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18329 if (stretched_p)
18330 it->pixel_width *= XFLOATINT (it->space_width);
18331
18332 /* If face has a box, add the box thickness to the character
18333 height. If character has a box line to the left and/or
18334 right, add the box line width to the character's width. */
18335 if (face->box != FACE_NO_BOX)
18336 {
18337 int thick = face->box_line_width;
18338
18339 if (thick > 0)
18340 {
18341 it->ascent += thick;
18342 it->descent += thick;
18343 }
18344 else
18345 thick = -thick;
18346
18347 if (it->start_of_box_run_p)
18348 it->pixel_width += thick;
18349 if (it->end_of_box_run_p)
18350 it->pixel_width += thick;
18351 }
18352
18353 /* If face has an overline, add the height of the overline
18354 (1 pixel) and a 1 pixel margin to the character height. */
18355 if (face->overline_p)
18356 it->ascent += 2;
18357
18358 take_vertical_position_into_account (it);
18359
18360 /* If we have to actually produce glyphs, do it. */
18361 if (it->glyph_row)
18362 {
18363 if (stretched_p)
18364 {
18365 /* Translate a space with a `space-width' property
18366 into a stretch glyph. */
f65536fa
KS
18367 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18368 / FONT_HEIGHT (font));
133c764e
KS
18369 append_stretch_glyph (it, it->object, it->pixel_width,
18370 it->ascent + it->descent, ascent);
18371 }
18372 else
18373 append_glyph (it);
18374
18375 /* If characters with lbearing or rbearing are displayed
18376 in this line, record that fact in a flag of the
18377 glyph row. This is used to optimize X output code. */
18378 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18379 it->glyph_row->contains_overlapping_glyphs_p = 1;
18380 }
18381 }
18382 else if (it->char_to_display == '\n')
18383 {
18384 /* A newline has no width but we need the height of the line. */
18385 it->pixel_width = 0;
18386 it->nglyphs = 0;
18387 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18388 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18389
18390 if (face->box != FACE_NO_BOX
18391 && face->box_line_width > 0)
18392 {
18393 it->ascent += face->box_line_width;
18394 it->descent += face->box_line_width;
18395 }
18396 }
18397 else if (it->char_to_display == '\t')
18398 {
da8b7f4f 18399 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
133c764e
KS
18400 int x = it->current_x + it->continuation_lines_width;
18401 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18402
18403 /* If the distance from the current position to the next tab
18404 stop is less than a canonical character width, use the
18405 tab stop after that. */
da8b7f4f 18406 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
133c764e
KS
18407 next_tab_x += tab_width;
18408
18409 it->pixel_width = next_tab_x - x;
18410 it->nglyphs = 1;
18411 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18412 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18413
18414 if (it->glyph_row)
18415 {
133c764e 18416 append_stretch_glyph (it, it->object, it->pixel_width,
f65536fa 18417 it->ascent + it->descent, it->ascent);
133c764e
KS
18418 }
18419 }
18420 else
18421 {
18422 /* A multi-byte character. Assume that the display width of the
18423 character is the width of the character multiplied by the
18424 width of the font. */
18425
18426 /* If we found a font, this font should give us the right
18427 metrics. If we didn't find a font, use the frame's
18428 default font and calculate the width of the character
18429 from the charset width; this is what old redisplay code
18430 did. */
18431
18432 pcm = rif->per_char_metric (font, &char2b,
18433 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18434
18435 if (font_not_found_p || !pcm)
18436 {
18437 int charset = CHAR_CHARSET (it->char_to_display);
18438
18439 it->glyph_not_available_p = 1;
da8b7f4f 18440 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
133c764e
KS
18441 * CHARSET_WIDTH (charset));
18442 it->phys_ascent = FONT_BASE (font) + boff;
18443 it->phys_descent = FONT_DESCENT (font) - boff;
18444 }
18445 else
18446 {
18447 it->pixel_width = pcm->width;
18448 it->phys_ascent = pcm->ascent + boff;
18449 it->phys_descent = pcm->descent - boff;
18450 if (it->glyph_row
18451 && (pcm->lbearing < 0
18452 || pcm->rbearing > pcm->width))
18453 it->glyph_row->contains_overlapping_glyphs_p = 1;
18454 }
18455 it->nglyphs = 1;
18456 it->ascent = FONT_BASE (font) + boff;
18457 it->descent = FONT_DESCENT (font) - boff;
18458 if (face->box != FACE_NO_BOX)
18459 {
18460 int thick = face->box_line_width;
18461
18462 if (thick > 0)
18463 {
18464 it->ascent += thick;
18465 it->descent += thick;
18466 }
18467 else
18468 thick = - thick;
18469
18470 if (it->start_of_box_run_p)
18471 it->pixel_width += thick;
18472 if (it->end_of_box_run_p)
18473 it->pixel_width += thick;
18474 }
18475
18476 /* If face has an overline, add the height of the overline
18477 (1 pixel) and a 1 pixel margin to the character height. */
18478 if (face->overline_p)
18479 it->ascent += 2;
18480
18481 take_vertical_position_into_account (it);
18482
18483 if (it->glyph_row)
18484 append_glyph (it);
18485 }
18486 it->multibyte_p = saved_multibyte_p;
18487 }
18488 else if (it->what == IT_COMPOSITION)
18489 {
18490 /* Note: A composition is represented as one glyph in the
18491 glyph matrix. There are no padding glyphs. */
18492 XChar2b char2b;
18493 XFontStruct *font;
18494 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18495 XCharStruct *pcm;
18496 int font_not_found_p;
18497 struct font_info *font_info;
18498 int boff; /* baseline offset */
18499 struct composition *cmp = composition_table[it->cmp_id];
18500
18501 /* Maybe translate single-byte characters to multibyte. */
18502 it->char_to_display = it->c;
18503 if (unibyte_display_via_language_environment
18504 && SINGLE_BYTE_CHAR_P (it->c)
18505 && (it->c >= 0240
18506 || (it->c >= 0200
18507 && !NILP (Vnonascii_translation_table))))
18508 {
18509 it->char_to_display = unibyte_char_to_multibyte (it->c);
18510 }
18511
18512 /* Get face and font to use. Encode IT->char_to_display. */
18513 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18514 face = FACE_FROM_ID (it->f, it->face_id);
18515 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18516 &char2b, it->multibyte_p, 0);
18517 font = face->font;
18518
18519 /* When no suitable font found, use the default font. */
18520 font_not_found_p = font == NULL;
18521 if (font_not_found_p)
18522 {
18523 font = FRAME_FONT (it->f);
18524 boff = FRAME_BASELINE_OFFSET (it->f);
18525 font_info = NULL;
18526 }
18527 else
18528 {
18529 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18530 boff = font_info->baseline_offset;
18531 if (font_info->vertical_centering)
18532 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18533 }
18534
18535 /* There are no padding glyphs, so there is only one glyph to
18536 produce for the composition. Important is that pixel_width,
18537 ascent and descent are the values of what is drawn by
18538 draw_glyphs (i.e. the values of the overall glyphs composed). */
18539 it->nglyphs = 1;
18540
18541 /* If we have not yet calculated pixel size data of glyphs of
18542 the composition for the current face font, calculate them
18543 now. Theoretically, we have to check all fonts for the
18544 glyphs, but that requires much time and memory space. So,
18545 here we check only the font of the first glyph. This leads
18546 to incorrect display very rarely, and C-l (recenter) can
18547 correct the display anyway. */
18548 if (cmp->font != (void *) font)
18549 {
18550 /* Ascent and descent of the font of the first character of
18551 this composition (adjusted by baseline offset). Ascent
18552 and descent of overall glyphs should not be less than
18553 them respectively. */
18554 int font_ascent = FONT_BASE (font) + boff;
18555 int font_descent = FONT_DESCENT (font) - boff;
18556 /* Bounding box of the overall glyphs. */
18557 int leftmost, rightmost, lowest, highest;
18558 int i, width, ascent, descent;
18559
18560 cmp->font = (void *) font;
18561
18562 /* Initialize the bounding box. */
18563 if (font_info
18564 && (pcm = rif->per_char_metric (font, &char2b,
18565 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
18566 {
18567 width = pcm->width;
18568 ascent = pcm->ascent;
18569 descent = pcm->descent;
18570 }
18571 else
18572 {
18573 width = FONT_WIDTH (font);
18574 ascent = FONT_BASE (font);
18575 descent = FONT_DESCENT (font);
18576 }
18577
18578 rightmost = width;
18579 lowest = - descent + boff;
18580 highest = ascent + boff;
18581 leftmost = 0;
18582
18583 if (font_info
18584 && font_info->default_ascent
18585 && CHAR_TABLE_P (Vuse_default_ascent)
18586 && !NILP (Faref (Vuse_default_ascent,
18587 make_number (it->char_to_display))))
18588 highest = font_info->default_ascent + boff;
18589
18590 /* Draw the first glyph at the normal position. It may be
18591 shifted to right later if some other glyphs are drawn at
18592 the left. */
18593 cmp->offsets[0] = 0;
18594 cmp->offsets[1] = boff;
18595
18596 /* Set cmp->offsets for the remaining glyphs. */
18597 for (i = 1; i < cmp->glyph_len; i++)
18598 {
18599 int left, right, btm, top;
18600 int ch = COMPOSITION_GLYPH (cmp, i);
18601 int face_id = FACE_FOR_CHAR (it->f, face, ch);
18602
18603 face = FACE_FROM_ID (it->f, face_id);
18604 get_char_face_and_encoding (it->f, ch, face->id,
18605 &char2b, it->multibyte_p, 0);
18606 font = face->font;
18607 if (font == NULL)
18608 {
18609 font = FRAME_FONT (it->f);
2a6d0874 18610 boff = FRAME_BASELINE_OFFSET (it->f);
133c764e
KS
18611 font_info = NULL;
18612 }
18613 else
18614 {
18615 font_info
18616 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18617 boff = font_info->baseline_offset;
18618 if (font_info->vertical_centering)
18619 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18620 }
18621
18622 if (font_info
2a6d0874 18623 && (pcm = rif->per_char_metric (font, &char2b,
133c764e
KS
18624 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
18625 {
18626 width = pcm->width;
18627 ascent = pcm->ascent;
18628 descent = pcm->descent;
18629 }
18630 else
18631 {
18632 width = FONT_WIDTH (font);
18633 ascent = 1;
18634 descent = 0;
18635 }
18636
18637 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
18638 {
18639 /* Relative composition with or without
18640 alternate chars. */
18641 left = (leftmost + rightmost - width) / 2;
18642 btm = - descent + boff;
18643 if (font_info && font_info->relative_compose
18644 && (! CHAR_TABLE_P (Vignore_relative_composition)
18645 || NILP (Faref (Vignore_relative_composition,
18646 make_number (ch)))))
18647 {
18648
18649 if (- descent >= font_info->relative_compose)
18650 /* One extra pixel between two glyphs. */
18651 btm = highest + 1;
18652 else if (ascent <= 0)
18653 /* One extra pixel between two glyphs. */
18654 btm = lowest - 1 - ascent - descent;
18655 }
18656 }
18657 else
18658 {
18659 /* A composition rule is specified by an integer
18660 value that encodes global and new reference
18661 points (GREF and NREF). GREF and NREF are
18662 specified by numbers as below:
18663
18664 0---1---2 -- ascent
18665 | |
18666 | |
18667 | |
18668 9--10--11 -- center
18669 | |
18670 ---3---4---5--- baseline
18671 | |
18672 6---7---8 -- descent
18673 */
18674 int rule = COMPOSITION_RULE (cmp, i);
18675 int gref, nref, grefx, grefy, nrefx, nrefy;
18676
18677 COMPOSITION_DECODE_RULE (rule, gref, nref);
18678 grefx = gref % 3, nrefx = nref % 3;
18679 grefy = gref / 3, nrefy = nref / 3;
18680
18681 left = (leftmost
18682 + grefx * (rightmost - leftmost) / 2
18683 - nrefx * width / 2);
18684 btm = ((grefy == 0 ? highest
18685 : grefy == 1 ? 0
18686 : grefy == 2 ? lowest
18687 : (highest + lowest) / 2)
18688 - (nrefy == 0 ? ascent + descent
18689 : nrefy == 1 ? descent - boff
18690 : nrefy == 2 ? 0
18691 : (ascent + descent) / 2));
18692 }
18693
18694 cmp->offsets[i * 2] = left;
18695 cmp->offsets[i * 2 + 1] = btm + descent;
18696
18697 /* Update the bounding box of the overall glyphs. */
18698 right = left + width;
18699 top = btm + descent + ascent;
18700 if (left < leftmost)
18701 leftmost = left;
18702 if (right > rightmost)
18703 rightmost = right;
18704 if (top > highest)
18705 highest = top;
18706 if (btm < lowest)
18707 lowest = btm;
18708 }
18709
18710 /* If there are glyphs whose x-offsets are negative,
18711 shift all glyphs to the right and make all x-offsets
18712 non-negative. */
18713 if (leftmost < 0)
18714 {
18715 for (i = 0; i < cmp->glyph_len; i++)
18716 cmp->offsets[i * 2] -= leftmost;
18717 rightmost -= leftmost;
18718 }
18719
18720 cmp->pixel_width = rightmost;
18721 cmp->ascent = highest;
18722 cmp->descent = - lowest;
18723 if (cmp->ascent < font_ascent)
18724 cmp->ascent = font_ascent;
18725 if (cmp->descent < font_descent)
18726 cmp->descent = font_descent;
18727 }
18728
18729 it->pixel_width = cmp->pixel_width;
18730 it->ascent = it->phys_ascent = cmp->ascent;
18731 it->descent = it->phys_descent = cmp->descent;
18732
18733 if (face->box != FACE_NO_BOX)
18734 {
18735 int thick = face->box_line_width;
18736
18737 if (thick > 0)
18738 {
18739 it->ascent += thick;
18740 it->descent += thick;
18741 }
18742 else
18743 thick = - thick;
18744
18745 if (it->start_of_box_run_p)
18746 it->pixel_width += thick;
18747 if (it->end_of_box_run_p)
18748 it->pixel_width += thick;
18749 }
18750
18751 /* If face has an overline, add the height of the overline
18752 (1 pixel) and a 1 pixel margin to the character height. */
18753 if (face->overline_p)
18754 it->ascent += 2;
18755
18756 take_vertical_position_into_account (it);
18757
18758 if (it->glyph_row)
18759 append_composite_glyph (it);
18760 }
18761 else if (it->what == IT_IMAGE)
18762 produce_image_glyph (it);
18763 else if (it->what == IT_STRETCH)
18764 produce_stretch_glyph (it);
18765
18766 /* Accumulate dimensions. Note: can't assume that it->descent > 0
18767 because this isn't true for images with `:ascent 100'. */
18768 xassert (it->ascent >= 0 && it->descent >= 0);
18769 if (it->area == TEXT_AREA)
18770 it->current_x += it->pixel_width;
18771
18772 it->descent += it->extra_line_spacing;
18773
18774 it->max_ascent = max (it->max_ascent, it->ascent);
18775 it->max_descent = max (it->max_descent, it->descent);
18776 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
18777 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
18778}
18779
fa3c6b4d
KS
18780/* EXPORT for RIF:
18781 Output LEN glyphs starting at START at the nominal cursor position.
18782 Advance the nominal cursor over the text. The global variable
18783 updated_window contains the window being updated, updated_row is
18784 the glyph row being updated, and updated_area is the area of that
18785 row being updated. */
133c764e 18786
fa3c6b4d
KS
18787void
18788x_write_glyphs (start, len)
18789 struct glyph *start;
18790 int len;
18791{
18792 int x, hpos;
133c764e 18793
fa3c6b4d
KS
18794 xassert (updated_window && updated_row);
18795 BLOCK_INPUT;
133c764e 18796
fa3c6b4d 18797 /* Write glyphs. */
cfe03a41 18798
fa3c6b4d
KS
18799 hpos = start - updated_row->glyphs[updated_area];
18800 x = draw_glyphs (updated_window, output_cursor.x,
18801 updated_row, updated_area,
18802 hpos, hpos + len,
18803 DRAW_NORMAL_TEXT, 0);
cfe03a41 18804
fa3c6b4d
KS
18805 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
18806 if (updated_area == TEXT_AREA
18807 && updated_window->phys_cursor_on_p
18808 && updated_window->phys_cursor.vpos == output_cursor.vpos
18809 && updated_window->phys_cursor.hpos >= hpos
18810 && updated_window->phys_cursor.hpos < hpos + len)
18811 updated_window->phys_cursor_on_p = 0;
cfe03a41 18812
fa3c6b4d 18813 UNBLOCK_INPUT;
cfe03a41 18814
fa3c6b4d
KS
18815 /* Advance the output cursor. */
18816 output_cursor.hpos += len;
18817 output_cursor.x = x;
18818}
cfe03a41 18819
cfe03a41 18820
fa3c6b4d
KS
18821/* EXPORT for RIF:
18822 Insert LEN glyphs from START at the nominal cursor position. */
18823
18824void
18825x_insert_glyphs (start, len)
18826 struct glyph *start;
18827 int len;
18828{
18829 struct frame *f;
18830 struct window *w;
18831 int line_height, shift_by_width, shifted_region_width;
18832 struct glyph_row *row;
18833 struct glyph *glyph;
18834 int frame_x, frame_y, hpos;
18835
18836 xassert (updated_window && updated_row);
18837 BLOCK_INPUT;
18838 w = updated_window;
18839 f = XFRAME (WINDOW_FRAME (w));
18840
18841 /* Get the height of the line we are in. */
18842 row = updated_row;
18843 line_height = row->height;
18844
18845 /* Get the width of the glyphs to insert. */
18846 shift_by_width = 0;
18847 for (glyph = start; glyph < start + len; ++glyph)
18848 shift_by_width += glyph->pixel_width;
18849
18850 /* Get the width of the region to shift right. */
18851 shifted_region_width = (window_box_width (w, updated_area)
18852 - output_cursor.x
18853 - shift_by_width);
18854
18855 /* Shift right. */
18856 frame_x = window_box_left (w, updated_area) + output_cursor.x;
18857 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
18858
7b7b454e
AS
18859 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
18860 line_height, shift_by_width);
fa3c6b4d
KS
18861
18862 /* Write the glyphs. */
18863 hpos = start - row->glyphs[updated_area];
18864 draw_glyphs (w, output_cursor.x, row, updated_area,
18865 hpos, hpos + len,
18866 DRAW_NORMAL_TEXT, 0);
18867
18868 /* Advance the output cursor. */
18869 output_cursor.hpos += len;
18870 output_cursor.x += shift_by_width;
18871 UNBLOCK_INPUT;
18872}
18873
18874
18875/* EXPORT for RIF:
18876 Erase the current text line from the nominal cursor position
18877 (inclusive) to pixel column TO_X (exclusive). The idea is that
18878 everything from TO_X onward is already erased.
18879
18880 TO_X is a pixel position relative to updated_area of
18881 updated_window. TO_X == -1 means clear to the end of this area. */
18882
18883void
18884x_clear_end_of_line (to_x)
18885 int to_x;
18886{
18887 struct frame *f;
18888 struct window *w = updated_window;
18889 int max_x, min_y, max_y;
18890 int from_x, from_y, to_y;
18891
18892 xassert (updated_window && updated_row);
18893 f = XFRAME (w->frame);
18894
18895 if (updated_row->full_width_p)
da8b7f4f 18896 max_x = WINDOW_TOTAL_WIDTH (w);
fa3c6b4d
KS
18897 else
18898 max_x = window_box_width (w, updated_area);
18899 max_y = window_text_bottom_y (w);
18900
18901 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
18902 of window. For TO_X > 0, truncate to end of drawing area. */
18903 if (to_x == 0)
18904 return;
18905 else if (to_x < 0)
18906 to_x = max_x;
18907 else
18908 to_x = min (to_x, max_x);
18909
18910 to_y = min (max_y, output_cursor.y + updated_row->height);
18911
18912 /* Notice if the cursor will be cleared by this operation. */
18913 if (!updated_row->full_width_p)
18914 notice_overwritten_cursor (w, updated_area,
18915 output_cursor.x, -1,
18916 updated_row->y,
18917 MATRIX_ROW_BOTTOM_Y (updated_row));
18918
18919 from_x = output_cursor.x;
18920
18921 /* Translate to frame coordinates. */
18922 if (updated_row->full_width_p)
18923 {
18924 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
18925 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
18926 }
18927 else
18928 {
da8b7f4f
KS
18929 int area_left = window_box_left (w, updated_area);
18930 from_x += area_left;
18931 to_x += area_left;
fa3c6b4d
KS
18932 }
18933
da8b7f4f 18934 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
fa3c6b4d
KS
18935 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
18936 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
18937
18938 /* Prevent inadvertently clearing to end of the X window. */
18939 if (to_x > from_x && to_y > from_y)
18940 {
18941 BLOCK_INPUT;
18942 rif->clear_frame_area (f, from_x, from_y,
18943 to_x - from_x, to_y - from_y);
18944 UNBLOCK_INPUT;
18945 }
18946}
18947
18948#endif /* HAVE_WINDOW_SYSTEM */
18949
18950
18951\f
18952/***********************************************************************
18953 Cursor types
18954 ***********************************************************************/
18955
18956/* Value is the internal representation of the specified cursor type
18957 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
18958 of the bar cursor. */
18959
f65536fa 18960static enum text_cursor_kinds
fa3c6b4d
KS
18961get_specified_cursor_type (arg, width)
18962 Lisp_Object arg;
18963 int *width;
18964{
18965 enum text_cursor_kinds type;
18966
18967 if (NILP (arg))
18968 return NO_CURSOR;
18969
18970 if (EQ (arg, Qbox))
18971 return FILLED_BOX_CURSOR;
18972
18973 if (EQ (arg, Qhollow))
18974 return HOLLOW_BOX_CURSOR;
18975
18976 if (EQ (arg, Qbar))
18977 {
18978 *width = 2;
18979 return BAR_CURSOR;
18980 }
18981
18982 if (CONSP (arg)
18983 && EQ (XCAR (arg), Qbar)
18984 && INTEGERP (XCDR (arg))
18985 && XINT (XCDR (arg)) >= 0)
18986 {
18987 *width = XINT (XCDR (arg));
18988 return BAR_CURSOR;
18989 }
18990
18991 if (EQ (arg, Qhbar))
18992 {
18993 *width = 2;
18994 return HBAR_CURSOR;
18995 }
18996
18997 if (CONSP (arg)
18998 && EQ (XCAR (arg), Qhbar)
18999 && INTEGERP (XCDR (arg))
19000 && XINT (XCDR (arg)) >= 0)
19001 {
19002 *width = XINT (XCDR (arg));
19003 return HBAR_CURSOR;
19004 }
19005
19006 /* Treat anything unknown as "hollow box cursor".
19007 It was bad to signal an error; people have trouble fixing
19008 .Xdefaults with Emacs, when it has something bad in it. */
19009 type = HOLLOW_BOX_CURSOR;
19010
19011 return type;
19012}
19013
19014/* Set the default cursor types for specified frame. */
19015void
19016set_frame_cursor_types (f, arg)
19017 struct frame *f;
19018 Lisp_Object arg;
19019{
19020 int width;
19021 Lisp_Object tem;
19022
19023 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19024 FRAME_CURSOR_WIDTH (f) = width;
19025
19026 /* By default, set up the blink-off state depending on the on-state. */
19027
19028 tem = Fassoc (arg, Vblink_cursor_alist);
19029 if (!NILP (tem))
19030 {
19031 FRAME_BLINK_OFF_CURSOR (f)
19032 = get_specified_cursor_type (XCDR (tem), &width);
19033 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19034 }
19035 else
19036 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19037}
19038
19039
19040/* Return the cursor we want to be displayed in window W. Return
19041 width of bar/hbar cursor through WIDTH arg. Return with
19042 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19043 (i.e. if the `system caret' should track this cursor).
19044
19045 In a mini-buffer window, we want the cursor only to appear if we
19046 are reading input from this window. For the selected window, we
19047 want the cursor type given by the frame parameter or buffer local
19048 setting of cursor-type. If explicitly marked off, draw no cursor.
19049 In all other cases, we want a hollow box cursor. */
19050
f65536fa
KS
19051static enum text_cursor_kinds
19052get_window_cursor_type (w, glyph, width, active_cursor)
fa3c6b4d 19053 struct window *w;
f65536fa 19054 struct glyph *glyph;
fa3c6b4d
KS
19055 int *width;
19056 int *active_cursor;
19057{
19058 struct frame *f = XFRAME (w->frame);
19059 struct buffer *b = XBUFFER (w->buffer);
19060 int cursor_type = DEFAULT_CURSOR;
19061 Lisp_Object alt_cursor;
19062 int non_selected = 0;
19063
19064 *active_cursor = 1;
19065
19066 /* Echo area */
19067 if (cursor_in_echo_area
19068 && FRAME_HAS_MINIBUF_P (f)
19069 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19070 {
19071 if (w == XWINDOW (echo_area_window))
19072 {
19073 *width = FRAME_CURSOR_WIDTH (f);
19074 return FRAME_DESIRED_CURSOR (f);
19075 }
19076
19077 *active_cursor = 0;
19078 non_selected = 1;
19079 }
19080
19081 /* Nonselected window or nonselected frame. */
19082 else if (w != XWINDOW (f->selected_window)
19083#ifdef HAVE_WINDOW_SYSTEM
19084 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19085#endif
19086 )
19087 {
19088 *active_cursor = 0;
19089
19090 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19091 return NO_CURSOR;
19092
19093 non_selected = 1;
19094 }
19095
19096 /* Never display a cursor in a window in which cursor-type is nil. */
19097 if (NILP (b->cursor_type))
19098 return NO_CURSOR;
19099
19100 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19101 if (non_selected)
19102 {
19103 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19104 return get_specified_cursor_type (alt_cursor, width);
19105 }
19106
19107 /* Get the normal cursor type for this window. */
19108 if (EQ (b->cursor_type, Qt))
19109 {
19110 cursor_type = FRAME_DESIRED_CURSOR (f);
19111 *width = FRAME_CURSOR_WIDTH (f);
19112 }
19113 else
19114 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19115
19116 /* Use normal cursor if not blinked off. */
19117 if (!w->cursor_off_p)
f65536fa
KS
19118 {
19119 if (glyph->type == IMAGE_GLYPH) {
19120 if (cursor_type == FILLED_BOX_CURSOR)
19121 cursor_type = HOLLOW_BOX_CURSOR;
19122 }
19123 return cursor_type;
19124 }
fa3c6b4d
KS
19125
19126 /* Cursor is blinked off, so determine how to "toggle" it. */
19127
19128 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19129 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19130 return get_specified_cursor_type (XCDR (alt_cursor), width);
19131
19132 /* Then see if frame has specified a specific blink off cursor type. */
19133 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19134 {
19135 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19136 return FRAME_BLINK_OFF_CURSOR (f);
19137 }
19138
a57dd8b1
KS
19139#if 0
19140 /* Some people liked having a permanently visible blinking cursor,
19141 while others had very strong opinions against it. So it was
19142 decided to remove it. KFS 2003-09-03 */
19143
fa3c6b4d
KS
19144 /* Finally perform built-in cursor blinking:
19145 filled box <-> hollow box
19146 wide [h]bar <-> narrow [h]bar
19147 narrow [h]bar <-> no cursor
19148 other type <-> no cursor */
19149
19150 if (cursor_type == FILLED_BOX_CURSOR)
19151 return HOLLOW_BOX_CURSOR;
19152
19153 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19154 {
19155 *width = 1;
19156 return cursor_type;
19157 }
a57dd8b1 19158#endif
fa3c6b4d
KS
19159
19160 return NO_CURSOR;
19161}
19162
19163
19164#ifdef HAVE_WINDOW_SYSTEM
19165
19166/* Notice when the text cursor of window W has been completely
19167 overwritten by a drawing operation that outputs glyphs in AREA
19168 starting at X0 and ending at X1 in the line starting at Y0 and
19169 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19170 the rest of the line after X0 has been written. Y coordinates
19171 are window-relative. */
19172
19173static void
19174notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19175 struct window *w;
19176 enum glyph_row_area area;
19177 int x0, y0, x1, y1;
19178{
fa3c6b4d
KS
19179 if (area == TEXT_AREA && w->phys_cursor_on_p)
19180 {
19181 int cx0 = w->phys_cursor.x;
19182 int cx1 = cx0 + w->phys_cursor_width;
19183 int cy0 = w->phys_cursor.y;
19184 int cy1 = cy0 + w->phys_cursor_height;
19185
19186 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
19187 {
19188 /* The cursor image will be completely removed from the
19189 screen if the output area intersects the cursor area in
19190 y-direction. When we draw in [y0 y1[, and some part of
19191 the cursor is at y < y0, that part must have been drawn
19192 before. When scrolling, the cursor is erased before
19193 actually scrolling, so we don't come here. When not
19194 scrolling, the rows above the old cursor row must have
19195 changed, and in this case these rows must have written
19196 over the cursor image.
19197
19198 Likewise if part of the cursor is below y1, with the
19199 exception of the cursor being in the first blank row at
19200 the buffer and window end because update_text_area
19201 doesn't draw that row. (Except when it does, but
19202 that's handled in update_text_area.) */
19203
19204 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
19205 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
19206 w->phys_cursor_on_p = 0;
19207 }
19208 }
fa3c6b4d
KS
19209}
19210
19211#endif /* HAVE_WINDOW_SYSTEM */
19212
19213\f
19214/************************************************************************
19215 Mouse Face
19216 ************************************************************************/
19217
19218#ifdef HAVE_WINDOW_SYSTEM
19219
19220/* EXPORT for RIF:
19221 Fix the display of area AREA of overlapping row ROW in window W. */
19222
19223void
19224x_fix_overlapping_area (w, row, area)
19225 struct window *w;
19226 struct glyph_row *row;
19227 enum glyph_row_area area;
19228{
19229 int i, x;
19230
19231 BLOCK_INPUT;
19232
57dd22f4 19233 x = 0;
fa3c6b4d
KS
19234 for (i = 0; i < row->used[area];)
19235 {
19236 if (row->glyphs[area][i].overlaps_vertically_p)
19237 {
19238 int start = i, start_x = x;
19239
19240 do
19241 {
19242 x += row->glyphs[area][i].pixel_width;
19243 ++i;
19244 }
19245 while (i < row->used[area]
19246 && row->glyphs[area][i].overlaps_vertically_p);
19247
19248 draw_glyphs (w, start_x, row, area,
19249 start, i,
19250 DRAW_NORMAL_TEXT, 1);
19251 }
19252 else
19253 {
19254 x += row->glyphs[area][i].pixel_width;
19255 ++i;
19256 }
19257 }
19258
19259 UNBLOCK_INPUT;
19260}
19261
19262
19263/* EXPORT:
19264 Draw the cursor glyph of window W in glyph row ROW. See the
19265 comment of draw_glyphs for the meaning of HL. */
19266
19267void
19268draw_phys_cursor_glyph (w, row, hl)
19269 struct window *w;
19270 struct glyph_row *row;
19271 enum draw_glyphs_face hl;
19272{
19273 /* If cursor hpos is out of bounds, don't draw garbage. This can
19274 happen in mini-buffer windows when switching between echo area
19275 glyphs and mini-buffer. */
19276 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19277 {
19278 int on_p = w->phys_cursor_on_p;
19279 int x1;
19280 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19281 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19282 hl, 0);
19283 w->phys_cursor_on_p = on_p;
19284
fa3c6b4d
KS
19285 if (hl == DRAW_CURSOR)
19286 w->phys_cursor_width = x1 - w->phys_cursor.x;
fa3c6b4d
KS
19287 /* When we erase the cursor, and ROW is overlapped by other
19288 rows, make sure that these overlapping parts of other rows
19289 are redrawn. */
365fa1b3 19290 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
fa3c6b4d
KS
19291 {
19292 if (row > w->current_matrix->rows
19293 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19294 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19295
19296 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19297 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19298 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19299 }
19300 }
19301}
19302
19303
19304/* EXPORT:
19305 Erase the image of a cursor of window W from the screen. */
19306
19307void
19308erase_phys_cursor (w)
19309 struct window *w;
19310{
19311 struct frame *f = XFRAME (w->frame);
19312 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19313 int hpos = w->phys_cursor.hpos;
19314 int vpos = w->phys_cursor.vpos;
19315 int mouse_face_here_p = 0;
19316 struct glyph_matrix *active_glyphs = w->current_matrix;
19317 struct glyph_row *cursor_row;
19318 struct glyph *cursor_glyph;
19319 enum draw_glyphs_face hl;
19320
19321 /* No cursor displayed or row invalidated => nothing to do on the
19322 screen. */
19323 if (w->phys_cursor_type == NO_CURSOR)
19324 goto mark_cursor_off;
19325
19326 /* VPOS >= active_glyphs->nrows means that window has been resized.
19327 Don't bother to erase the cursor. */
19328 if (vpos >= active_glyphs->nrows)
19329 goto mark_cursor_off;
19330
19331 /* If row containing cursor is marked invalid, there is nothing we
19332 can do. */
19333 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19334 if (!cursor_row->enabled_p)
19335 goto mark_cursor_off;
19336
19337 /* If row is completely invisible, don't attempt to delete a cursor which
19338 isn't there. This can happen if cursor is at top of a window, and
19339 we switch to a buffer with a header line in that window. */
19340 if (cursor_row->visible_height <= 0)
19341 goto mark_cursor_off;
19342
19343 /* This can happen when the new row is shorter than the old one.
19344 In this case, either draw_glyphs or clear_end_of_line
19345 should have cleared the cursor. Note that we wouldn't be
19346 able to erase the cursor in this case because we don't have a
19347 cursor glyph at hand. */
19348 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19349 goto mark_cursor_off;
19350
19351 /* If the cursor is in the mouse face area, redisplay that when
19352 we clear the cursor. */
19353 if (! NILP (dpyinfo->mouse_face_window)
19354 && w == XWINDOW (dpyinfo->mouse_face_window)
19355 && (vpos > dpyinfo->mouse_face_beg_row
19356 || (vpos == dpyinfo->mouse_face_beg_row
19357 && hpos >= dpyinfo->mouse_face_beg_col))
19358 && (vpos < dpyinfo->mouse_face_end_row
19359 || (vpos == dpyinfo->mouse_face_end_row
19360 && hpos < dpyinfo->mouse_face_end_col))
19361 /* Don't redraw the cursor's spot in mouse face if it is at the
19362 end of a line (on a newline). The cursor appears there, but
19363 mouse highlighting does not. */
19364 && cursor_row->used[TEXT_AREA] > hpos)
19365 mouse_face_here_p = 1;
19366
19367 /* Maybe clear the display under the cursor. */
19368 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19369 {
19370 int x, y;
da8b7f4f 19371 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
fa3c6b4d
KS
19372
19373 cursor_glyph = get_phys_cursor_glyph (w);
19374 if (cursor_glyph == NULL)
19375 goto mark_cursor_off;
19376
19377 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19378 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19379
19380 rif->clear_frame_area (f, x, y,
19381 cursor_glyph->pixel_width, cursor_row->visible_height);
19382 }
19383
19384 /* Erase the cursor by redrawing the character underneath it. */
19385 if (mouse_face_here_p)
19386 hl = DRAW_MOUSE_FACE;
19387 else
19388 hl = DRAW_NORMAL_TEXT;
19389 draw_phys_cursor_glyph (w, cursor_row, hl);
19390
19391 mark_cursor_off:
19392 w->phys_cursor_on_p = 0;
19393 w->phys_cursor_type = NO_CURSOR;
19394}
19395
19396
19397/* EXPORT:
19398 Display or clear cursor of window W. If ON is zero, clear the
19399 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19400 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19401
19402void
19403display_and_set_cursor (w, on, hpos, vpos, x, y)
19404 struct window *w;
19405 int on, hpos, vpos, x, y;
19406{
19407 struct frame *f = XFRAME (w->frame);
19408 int new_cursor_type;
19409 int new_cursor_width;
19410 int active_cursor;
19411 struct glyph_matrix *current_glyphs;
19412 struct glyph_row *glyph_row;
19413 struct glyph *glyph;
19414
19415 /* This is pointless on invisible frames, and dangerous on garbaged
19416 windows and frames; in the latter case, the frame or window may
19417 be in the midst of changing its size, and x and y may be off the
19418 window. */
19419 if (! FRAME_VISIBLE_P (f)
19420 || FRAME_GARBAGED_P (f)
19421 || vpos >= w->current_matrix->nrows
19422 || hpos >= w->current_matrix->matrix_w)
19423 return;
19424
19425 /* If cursor is off and we want it off, return quickly. */
19426 if (!on && !w->phys_cursor_on_p)
19427 return;
19428
19429 current_glyphs = w->current_matrix;
19430 glyph_row = MATRIX_ROW (current_glyphs, vpos);
19431 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
19432
19433 /* If cursor row is not enabled, we don't really know where to
19434 display the cursor. */
19435 if (!glyph_row->enabled_p)
19436 {
19437 w->phys_cursor_on_p = 0;
19438 return;
19439 }
19440
19441 xassert (interrupt_input_blocked);
19442
19443 /* Set new_cursor_type to the cursor we want to be displayed. */
f65536fa
KS
19444 new_cursor_type = get_window_cursor_type (w, glyph,
19445 &new_cursor_width, &active_cursor);
fa3c6b4d
KS
19446
19447 /* If cursor is currently being shown and we don't want it to be or
19448 it is in the wrong place, or the cursor type is not what we want,
19449 erase it. */
19450 if (w->phys_cursor_on_p
19451 && (!on
19452 || w->phys_cursor.x != x
19453 || w->phys_cursor.y != y
19454 || new_cursor_type != w->phys_cursor_type
19455 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19456 && new_cursor_width != w->phys_cursor_width)))
19457 erase_phys_cursor (w);
19458
19459 /* Don't check phys_cursor_on_p here because that flag is only set
19460 to zero in some cases where we know that the cursor has been
19461 completely erased, to avoid the extra work of erasing the cursor
19462 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19463 still not be visible, or it has only been partly erased. */
19464 if (on)
19465 {
19466 w->phys_cursor_ascent = glyph_row->ascent;
19467 w->phys_cursor_height = glyph_row->height;
19468
19469 /* Set phys_cursor_.* before x_draw_.* is called because some
19470 of them may need the information. */
19471 w->phys_cursor.x = x;
19472 w->phys_cursor.y = glyph_row->y;
19473 w->phys_cursor.hpos = hpos;
19474 w->phys_cursor.vpos = vpos;
19475 }
19476
b4ebbb12
KS
19477 rif->draw_window_cursor (w, glyph_row, x, y,
19478 new_cursor_type, new_cursor_width,
19479 on, active_cursor);
fa3c6b4d
KS
19480}
19481
19482
19483/* Switch the display of W's cursor on or off, according to the value
19484 of ON. */
19485
19486static void
19487update_window_cursor (w, on)
19488 struct window *w;
19489 int on;
19490{
19491 /* Don't update cursor in windows whose frame is in the process
19492 of being deleted. */
19493 if (w->current_matrix)
19494 {
19495 BLOCK_INPUT;
19496 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
19497 w->phys_cursor.x, w->phys_cursor.y);
19498 UNBLOCK_INPUT;
19499 }
19500}
19501
19502
19503/* Call update_window_cursor with parameter ON_P on all leaf windows
19504 in the window tree rooted at W. */
19505
19506static void
19507update_cursor_in_window_tree (w, on_p)
19508 struct window *w;
19509 int on_p;
19510{
19511 while (w)
19512 {
19513 if (!NILP (w->hchild))
19514 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
19515 else if (!NILP (w->vchild))
19516 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
19517 else
19518 update_window_cursor (w, on_p);
19519
19520 w = NILP (w->next) ? 0 : XWINDOW (w->next);
19521 }
19522}
19523
19524
19525/* EXPORT:
19526 Display the cursor on window W, or clear it, according to ON_P.
19527 Don't change the cursor's position. */
19528
19529void
19530x_update_cursor (f, on_p)
19531 struct frame *f;
19532 int on_p;
19533{
19534 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
19535}
19536
19537
19538/* EXPORT:
19539 Clear the cursor of window W to background color, and mark the
19540 cursor as not shown. This is used when the text where the cursor
19541 is is about to be rewritten. */
19542
19543void
19544x_clear_cursor (w)
19545 struct window *w;
19546{
19547 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
19548 update_window_cursor (w, 0);
19549}
19550
19551
19552/* EXPORT:
19553 Display the active region described by mouse_face_* according to DRAW. */
19554
19555void
19556show_mouse_face (dpyinfo, draw)
19557 Display_Info *dpyinfo;
19558 enum draw_glyphs_face draw;
19559{
19560 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
19561 struct frame *f = XFRAME (WINDOW_FRAME (w));
19562
19563 if (/* If window is in the process of being destroyed, don't bother
19564 to do anything. */
19565 w->current_matrix != NULL
19566 /* Don't update mouse highlight if hidden */
19567 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
19568 /* Recognize when we are called to operate on rows that don't exist
19569 anymore. This can happen when a window is split. */
19570 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
19571 {
19572 int phys_cursor_on_p = w->phys_cursor_on_p;
19573 struct glyph_row *row, *first, *last;
19574
19575 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19576 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19577
19578 for (row = first; row <= last && row->enabled_p; ++row)
19579 {
19580 int start_hpos, end_hpos, start_x;
19581
19582 /* For all but the first row, the highlight starts at column 0. */
19583 if (row == first)
19584 {
19585 start_hpos = dpyinfo->mouse_face_beg_col;
19586 start_x = dpyinfo->mouse_face_beg_x;
19587 }
19588 else
19589 {
19590 start_hpos = 0;
19591 start_x = 0;
19592 }
19593
19594 if (row == last)
19595 end_hpos = dpyinfo->mouse_face_end_col;
19596 else
19597 end_hpos = row->used[TEXT_AREA];
19598
19599 if (end_hpos > start_hpos)
19600 {
19601 draw_glyphs (w, start_x, row, TEXT_AREA,
19602 start_hpos, end_hpos,
19603 draw, 0);
19604
19605 row->mouse_face_p
19606 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
19607 }
19608 }
19609
19610 /* When we've written over the cursor, arrange for it to
19611 be displayed again. */
19612 if (phys_cursor_on_p && !w->phys_cursor_on_p)
19613 {
19614 BLOCK_INPUT;
19615 display_and_set_cursor (w, 1,
19616 w->phys_cursor.hpos, w->phys_cursor.vpos,
19617 w->phys_cursor.x, w->phys_cursor.y);
19618 UNBLOCK_INPUT;
19619 }
19620 }
19621
19622 /* Change the mouse cursor. */
19623 if (draw == DRAW_NORMAL_TEXT)
19624 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
19625 else if (draw == DRAW_MOUSE_FACE)
19626 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
19627 else
19628 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
19629}
19630
19631/* EXPORT:
19632 Clear out the mouse-highlighted active region.
19633 Redraw it un-highlighted first. Value is non-zero if mouse
19634 face was actually drawn unhighlighted. */
19635
19636int
19637clear_mouse_face (dpyinfo)
19638 Display_Info *dpyinfo;
19639{
19640 int cleared = 0;
19641
19642 if (!NILP (dpyinfo->mouse_face_window))
19643 {
19644 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
19645 cleared = 1;
19646 }
19647
19648 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
19649 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
19650 dpyinfo->mouse_face_window = Qnil;
19651 dpyinfo->mouse_face_overlay = Qnil;
19652 return cleared;
19653}
19654
19655
19656/* EXPORT:
19657 Non-zero if physical cursor of window W is within mouse face. */
19658
19659int
19660cursor_in_mouse_face_p (w)
19661 struct window *w;
19662{
19663 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
19664 int in_mouse_face = 0;
19665
19666 if (WINDOWP (dpyinfo->mouse_face_window)
19667 && XWINDOW (dpyinfo->mouse_face_window) == w)
19668 {
19669 int hpos = w->phys_cursor.hpos;
19670 int vpos = w->phys_cursor.vpos;
19671
19672 if (vpos >= dpyinfo->mouse_face_beg_row
19673 && vpos <= dpyinfo->mouse_face_end_row
19674 && (vpos > dpyinfo->mouse_face_beg_row
19675 || hpos >= dpyinfo->mouse_face_beg_col)
19676 && (vpos < dpyinfo->mouse_face_end_row
19677 || hpos < dpyinfo->mouse_face_end_col
19678 || dpyinfo->mouse_face_past_end))
19679 in_mouse_face = 1;
19680 }
19681
19682 return in_mouse_face;
19683}
19684
19685
19686
19687\f
19688/* Find the glyph matrix position of buffer position CHARPOS in window
19689 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
19690 current glyphs must be up to date. If CHARPOS is above window
19691 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
19692 of last line in W. In the row containing CHARPOS, stop before glyphs
19693 having STOP as object. */
19694
9c3521e0 19695#if 1 /* This is a version of fast_find_position that's more correct
fa3c6b4d
KS
19696 in the presence of hscrolling, for example. I didn't install
19697 it right away because the problem fixed is minor, it failed
19698 in 20.x as well, and I think it's too risky to install
19699 so near the release of 21.1. 2001-09-25 gerd. */
19700
19701static int
19702fast_find_position (w, charpos, hpos, vpos, x, y, stop)
19703 struct window *w;
19704 int charpos;
19705 int *hpos, *vpos, *x, *y;
19706 Lisp_Object stop;
19707{
19708 struct glyph_row *row, *first;
19709 struct glyph *glyph, *end;
b19a5b64 19710 int past_end = 0;
fa3c6b4d
KS
19711
19712 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19713 row = row_containing_pos (w, charpos, first, NULL, 0);
19714 if (row == NULL)
19715 {
19716 if (charpos < MATRIX_ROW_START_CHARPOS (first))
19717 {
19718 *x = *y = *hpos = *vpos = 0;
19719 return 0;
19720 }
19721 else
19722 {
19723 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
19724 past_end = 1;
19725 }
19726 }
19727
19728 *x = row->x;
19729 *y = row->y;
19730 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19731
19732 glyph = row->glyphs[TEXT_AREA];
19733 end = glyph + row->used[TEXT_AREA];
19734
19735 /* Skip over glyphs not having an object at the start of the row.
19736 These are special glyphs like truncation marks on terminal
19737 frames. */
19738 if (row->displays_text_p)
19739 while (glyph < end
19740 && INTEGERP (glyph->object)
19741 && !EQ (stop, glyph->object)
19742 && glyph->charpos < 0)
19743 {
19744 *x += glyph->pixel_width;
19745 ++glyph;
19746 }
19747
19748 while (glyph < end
19749 && !INTEGERP (glyph->object)
19750 && !EQ (stop, glyph->object)
19751 && (!BUFFERP (glyph->object)
19752 || glyph->charpos < charpos))
19753 {
19754 *x += glyph->pixel_width;
19755 ++glyph;
19756 }
19757
19758 *hpos = glyph - row->glyphs[TEXT_AREA];
19759 return past_end;
19760}
19761
9c3521e0 19762#else /* not 1 */
fa3c6b4d
KS
19763
19764static int
19765fast_find_position (w, pos, hpos, vpos, x, y, stop)
19766 struct window *w;
19767 int pos;
19768 int *hpos, *vpos, *x, *y;
19769 Lisp_Object stop;
19770{
19771 int i;
19772 int lastcol;
19773 int maybe_next_line_p = 0;
19774 int line_start_position;
19775 int yb = window_text_bottom_y (w);
19776 struct glyph_row *row, *best_row;
19777 int row_vpos, best_row_vpos;
19778 int current_x;
19779
19780 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19781 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19782
19783 while (row->y < yb)
19784 {
19785 if (row->used[TEXT_AREA])
19786 line_start_position = row->glyphs[TEXT_AREA]->charpos;
19787 else
19788 line_start_position = 0;
19789
19790 if (line_start_position > pos)
19791 break;
19792 /* If the position sought is the end of the buffer,
19793 don't include the blank lines at the bottom of the window. */
19794 else if (line_start_position == pos
19795 && pos == BUF_ZV (XBUFFER (w->buffer)))
19796 {
19797 maybe_next_line_p = 1;
19798 break;
19799 }
19800 else if (line_start_position > 0)
19801 {
19802 best_row = row;
19803 best_row_vpos = row_vpos;
19804 }
19805
19806 if (row->y + row->height >= yb)
19807 break;
19808
19809 ++row;
19810 ++row_vpos;
19811 }
19812
19813 /* Find the right column within BEST_ROW. */
19814 lastcol = 0;
19815 current_x = best_row->x;
19816 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
19817 {
19818 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
19819 int charpos = glyph->charpos;
19820
19821 if (BUFFERP (glyph->object))
19822 {
19823 if (charpos == pos)
19824 {
19825 *hpos = i;
19826 *vpos = best_row_vpos;
19827 *x = current_x;
19828 *y = best_row->y;
19829 return 1;
19830 }
19831 else if (charpos > pos)
19832 break;
19833 }
19834 else if (EQ (glyph->object, stop))
19835 break;
19836
19837 if (charpos > 0)
19838 lastcol = i;
19839 current_x += glyph->pixel_width;
19840 }
19841
19842 /* If we're looking for the end of the buffer,
19843 and we didn't find it in the line we scanned,
19844 use the start of the following line. */
19845 if (maybe_next_line_p)
19846 {
19847 ++best_row;
19848 ++best_row_vpos;
19849 lastcol = 0;
19850 current_x = best_row->x;
19851 }
19852
19853 *vpos = best_row_vpos;
19854 *hpos = lastcol + 1;
19855 *x = current_x;
19856 *y = best_row->y;
19857 return 0;
19858}
19859
9c3521e0 19860#endif /* not 1 */
fa3c6b4d
KS
19861
19862
19863/* Find the position of the glyph for position POS in OBJECT in
19864 window W's current matrix, and return in *X, *Y the pixel
19865 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
19866
19867 RIGHT_P non-zero means return the position of the right edge of the
19868 glyph, RIGHT_P zero means return the left edge position.
19869
19870 If no glyph for POS exists in the matrix, return the position of
19871 the glyph with the next smaller position that is in the matrix, if
19872 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
19873 exists in the matrix, return the position of the glyph with the
19874 next larger position in OBJECT.
19875
19876 Value is non-zero if a glyph was found. */
19877
19878static int
19879fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
19880 struct window *w;
19881 int pos;
19882 Lisp_Object object;
19883 int *hpos, *vpos, *x, *y;
19884 int right_p;
19885{
19886 int yb = window_text_bottom_y (w);
19887 struct glyph_row *r;
19888 struct glyph *best_glyph = NULL;
19889 struct glyph_row *best_row = NULL;
19890 int best_x = 0;
19891
19892 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19893 r->enabled_p && r->y < yb;
19894 ++r)
19895 {
19896 struct glyph *g = r->glyphs[TEXT_AREA];
19897 struct glyph *e = g + r->used[TEXT_AREA];
19898 int gx;
19899
19900 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
19901 if (EQ (g->object, object))
19902 {
19903 if (g->charpos == pos)
19904 {
19905 best_glyph = g;
19906 best_x = gx;
19907 best_row = r;
19908 goto found;
19909 }
19910 else if (best_glyph == NULL
19911 || ((abs (g->charpos - pos)
19912 < abs (best_glyph->charpos - pos))
19913 && (right_p
19914 ? g->charpos < pos
19915 : g->charpos > pos)))
19916 {
19917 best_glyph = g;
19918 best_x = gx;
19919 best_row = r;
19920 }
19921 }
19922 }
19923
19924 found:
19925
19926 if (best_glyph)
19927 {
19928 *x = best_x;
19929 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
19930
19931 if (right_p)
19932 {
19933 *x += best_glyph->pixel_width;
19934 ++*hpos;
19935 }
19936
19937 *y = best_row->y;
19938 *vpos = best_row - w->current_matrix->rows;
19939 }
19940
19941 return best_glyph != NULL;
19942}
19943
19944
fa3c6b4d
KS
19945/* Take proper action when mouse has moved to the mode or header line
19946 or marginal area AREA of window W, x-position X and y-position Y.
19947 X is relative to the start of the text display area of W, so the
19948 width of bitmap areas and scroll bars must be subtracted to get a
19949 position relative to the start of the mode line. */
19950
19951static void
19952note_mode_line_or_margin_highlight (w, x, y, area)
19953 struct window *w;
19954 int x, y;
19955 enum window_part area;
19956{
19957 struct frame *f = XFRAME (w->frame);
19958 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
f65536fa 19959 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
fa3c6b4d
KS
19960 int charpos;
19961 Lisp_Object string, help, map, pos;
19962
19963 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
f65536fa 19964 string = mode_line_string (w, &x, &y, 0, 0, area, &charpos);
fa3c6b4d 19965 else
f65536fa 19966 string = marginal_area_string (w, &x, &y, 0, 0, area, &charpos);
fa3c6b4d
KS
19967
19968 if (STRINGP (string))
19969 {
19970 pos = make_number (charpos);
19971
19972 /* If we're on a string with `help-echo' text property, arrange
19973 for the help to be displayed. This is done by setting the
19974 global variable help_echo_string to the help string. */
19975 help = Fget_text_property (pos, Qhelp_echo, string);
19976 if (!NILP (help))
19977 {
19978 help_echo_string = help;
19979 XSETWINDOW (help_echo_window, w);
19980 help_echo_object = string;
19981 help_echo_pos = charpos;
19982 }
19983
19984 /* Change the mouse pointer according to what is under X/Y. */
f65536fa
KS
19985 if (area == ON_MODE_LINE)
19986 {
19987 map = Fget_text_property (pos, Qlocal_map, string);
19988 if (!KEYMAPP (map))
19989 map = Fget_text_property (pos, Qkeymap, string);
19990 if (!KEYMAPP (map))
19991 cursor = dpyinfo->vertical_scroll_bar_cursor;
19992 }
fa3c6b4d
KS
19993 }
19994
19995 rif->define_frame_cursor (f, cursor);
19996}
19997
fa3c6b4d
KS
19998
19999/* EXPORT:
20000 Take proper action when the mouse has moved to position X, Y on
20001 frame F as regards highlighting characters that have mouse-face
20002 properties. Also de-highlighting chars where the mouse was before.
20003 X and Y can be negative or out of range. */
20004
20005void
20006note_mouse_highlight (f, x, y)
20007 struct frame *f;
20008 int x, y;
20009{
20010 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20011 enum window_part part;
20012 Lisp_Object window;
20013 struct window *w;
20014 Cursor cursor = No_Cursor;
20015 struct buffer *b;
20016
20017 /* When a menu is active, don't highlight because this looks odd. */
20018#if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20019 if (popup_activated ())
20020 return;
20021#endif
20022
20023 if (NILP (Vmouse_highlight)
20024 || !f->glyphs_initialized_p)
20025 return;
20026
20027 dpyinfo->mouse_face_mouse_x = x;
20028 dpyinfo->mouse_face_mouse_y = y;
20029 dpyinfo->mouse_face_mouse_frame = f;
20030
20031 if (dpyinfo->mouse_face_defer)
20032 return;
20033
20034 if (gc_in_progress)
20035 {
20036 dpyinfo->mouse_face_deferred_gc = 1;
20037 return;
20038 }
20039
20040 /* Which window is that in? */
da8b7f4f 20041 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
fa3c6b4d
KS
20042
20043 /* If we were displaying active text in another window, clear that. */
20044 if (! EQ (window, dpyinfo->mouse_face_window))
20045 clear_mouse_face (dpyinfo);
20046
20047 /* Not on a window -> return. */
20048 if (!WINDOWP (window))
20049 return;
20050
20051 /* Reset help_echo_string. It will get recomputed below. */
20052 /* ++KFS: X version didn't do this, but it looks harmless. */
20053 help_echo_string = Qnil;
20054
20055 /* Convert to window-relative pixel coordinates. */
20056 w = XWINDOW (window);
20057 frame_to_window_pixel_xy (w, &x, &y);
20058
20059 /* Handle tool-bar window differently since it doesn't display a
20060 buffer. */
20061 if (EQ (window, f->tool_bar_window))
20062 {
20063 note_tool_bar_highlight (f, x, y);
20064 return;
20065 }
20066
fa3c6b4d
KS
20067 /* Mouse is on the mode, header line or margin? */
20068 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20069 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20070 {
20071 note_mode_line_or_margin_highlight (w, x, y, part);
20072 return;
20073 }
fa3c6b4d
KS
20074
20075 if (part == ON_VERTICAL_BORDER)
20076 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
f65536fa
KS
20077 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
20078 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
fa3c6b4d
KS
20079 else
20080 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20081
20082 /* Are we in a window whose display is up to date?
20083 And verify the buffer's text has not changed. */
20084 b = XBUFFER (w->buffer);
20085 if (part == ON_TEXT
20086 && EQ (w->window_end_valid, w->buffer)
20087 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20088 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20089 {
20090 int hpos, vpos, pos, i, area;
20091 struct glyph *glyph;
20092 Lisp_Object object;
20093 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20094 Lisp_Object *overlay_vec = NULL;
20095 int len, noverlays;
20096 struct buffer *obuf;
20097 int obegv, ozv, same_region;
20098
20099 /* Find the glyph under X/Y. */
20100 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
20101
20102 /* Clear mouse face if X/Y not over text. */
20103 if (glyph == NULL
20104 || area != TEXT_AREA
20105 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
20106 {
fa3c6b4d
KS
20107 if (clear_mouse_face (dpyinfo))
20108 cursor = No_Cursor;
f65536fa
KS
20109 if (NILP (Vshow_text_cursor_in_void))
20110 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
fa3c6b4d
KS
20111 goto set_cursor;
20112 }
20113
20114 pos = glyph->charpos;
20115 object = glyph->object;
20116 if (!STRINGP (object) && !BUFFERP (object))
20117 goto set_cursor;
20118
20119 /* If we get an out-of-range value, return now; avoid an error. */
20120 if (BUFFERP (object) && pos > BUF_Z (b))
20121 goto set_cursor;
20122
f65536fa
KS
20123 if (glyph->type == IMAGE_GLYPH)
20124 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20125
fa3c6b4d
KS
20126 /* Make the window's buffer temporarily current for
20127 overlays_at and compute_char_face. */
20128 obuf = current_buffer;
20129 current_buffer = b;
20130 obegv = BEGV;
20131 ozv = ZV;
20132 BEGV = BEG;
20133 ZV = Z;
20134
20135 /* Is this char mouse-active or does it have help-echo? */
20136 position = make_number (pos);
20137
20138 if (BUFFERP (object))
20139 {
20140 /* Put all the overlays we want in a vector in overlay_vec.
20141 Store the length in len. If there are more than 10, make
20142 enough space for all, and try again. */
20143 len = 10;
20144 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20145 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
20146 if (noverlays > len)
20147 {
20148 len = noverlays;
20149 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20150 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
20151 }
20152
20153 /* Sort overlays into increasing priority order. */
20154 noverlays = sort_overlays (overlay_vec, noverlays, w);
20155 }
20156 else
20157 noverlays = 0;
20158
20159 same_region = (EQ (window, dpyinfo->mouse_face_window)
20160 && vpos >= dpyinfo->mouse_face_beg_row
20161 && vpos <= dpyinfo->mouse_face_end_row
20162 && (vpos > dpyinfo->mouse_face_beg_row
20163 || hpos >= dpyinfo->mouse_face_beg_col)
20164 && (vpos < dpyinfo->mouse_face_end_row
20165 || hpos < dpyinfo->mouse_face_end_col
20166 || dpyinfo->mouse_face_past_end));
20167
20168 if (same_region)
20169 cursor = No_Cursor;
20170
20171 /* Check mouse-face highlighting. */
20172 if (! same_region
20173 /* If there exists an overlay with mouse-face overlapping
20174 the one we are currently highlighting, we have to
20175 check if we enter the overlapping overlay, and then
20176 highlight only that. */
20177 || (OVERLAYP (dpyinfo->mouse_face_overlay)
20178 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
20179 {
20180 /* Find the highest priority overlay that has a mouse-face
20181 property. */
20182 overlay = Qnil;
20183 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
20184 {
20185 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
20186 if (!NILP (mouse_face))
20187 overlay = overlay_vec[i];
20188 }
20189
20190 /* If we're actually highlighting the same overlay as
20191 before, there's no need to do that again. */
20192 if (!NILP (overlay)
20193 && EQ (overlay, dpyinfo->mouse_face_overlay))
20194 goto check_help_echo;
20195
20196 dpyinfo->mouse_face_overlay = overlay;
20197
20198 /* Clear the display of the old active region, if any. */
20199 if (clear_mouse_face (dpyinfo))
20200 cursor = No_Cursor;
20201
20202 /* If no overlay applies, get a text property. */
20203 if (NILP (overlay))
20204 mouse_face = Fget_text_property (position, Qmouse_face, object);
20205
20206 /* Handle the overlay case. */
20207 if (!NILP (overlay))
20208 {
20209 /* Find the range of text around this char that
20210 should be active. */
20211 Lisp_Object before, after;
20212 int ignore;
20213
20214 before = Foverlay_start (overlay);
20215 after = Foverlay_end (overlay);
20216 /* Record this as the current active region. */
20217 fast_find_position (w, XFASTINT (before),
20218 &dpyinfo->mouse_face_beg_col,
20219 &dpyinfo->mouse_face_beg_row,
20220 &dpyinfo->mouse_face_beg_x,
20221 &dpyinfo->mouse_face_beg_y, Qnil);
20222
20223 dpyinfo->mouse_face_past_end
20224 = !fast_find_position (w, XFASTINT (after),
20225 &dpyinfo->mouse_face_end_col,
20226 &dpyinfo->mouse_face_end_row,
20227 &dpyinfo->mouse_face_end_x,
20228 &dpyinfo->mouse_face_end_y, Qnil);
20229 dpyinfo->mouse_face_window = window;
20230
20231 dpyinfo->mouse_face_face_id
20232 = face_at_buffer_position (w, pos, 0, 0,
20233 &ignore, pos + 1,
20234 !dpyinfo->mouse_face_hidden);
20235
20236 /* Display it as active. */
20237 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20238 cursor = No_Cursor;
20239 }
20240 /* Handle the text property case. */
20241 else if (!NILP (mouse_face) && BUFFERP (object))
20242 {
20243 /* Find the range of text around this char that
20244 should be active. */
20245 Lisp_Object before, after, beginning, end;
20246 int ignore;
20247
20248 beginning = Fmarker_position (w->start);
20249 end = make_number (BUF_Z (XBUFFER (object))
20250 - XFASTINT (w->window_end_pos));
20251 before
20252 = Fprevious_single_property_change (make_number (pos + 1),
20253 Qmouse_face,
20254 object, beginning);
20255 after
20256 = Fnext_single_property_change (position, Qmouse_face,
20257 object, end);
20258
20259 /* Record this as the current active region. */
20260 fast_find_position (w, XFASTINT (before),
20261 &dpyinfo->mouse_face_beg_col,
20262 &dpyinfo->mouse_face_beg_row,
20263 &dpyinfo->mouse_face_beg_x,
20264 &dpyinfo->mouse_face_beg_y, Qnil);
20265 dpyinfo->mouse_face_past_end
20266 = !fast_find_position (w, XFASTINT (after),
20267 &dpyinfo->mouse_face_end_col,
20268 &dpyinfo->mouse_face_end_row,
20269 &dpyinfo->mouse_face_end_x,
20270 &dpyinfo->mouse_face_end_y, Qnil);
20271 dpyinfo->mouse_face_window = window;
20272
20273 if (BUFFERP (object))
20274 dpyinfo->mouse_face_face_id
20275 = face_at_buffer_position (w, pos, 0, 0,
20276 &ignore, pos + 1,
20277 !dpyinfo->mouse_face_hidden);
20278
20279 /* Display it as active. */
20280 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20281 cursor = No_Cursor;
20282 }
20283 else if (!NILP (mouse_face) && STRINGP (object))
20284 {
20285 Lisp_Object b, e;
20286 int ignore;
20287
20288 b = Fprevious_single_property_change (make_number (pos + 1),
20289 Qmouse_face,
20290 object, Qnil);
20291 e = Fnext_single_property_change (position, Qmouse_face,
20292 object, Qnil);
20293 if (NILP (b))
20294 b = make_number (0);
20295 if (NILP (e))
20296 e = make_number (SCHARS (object) - 1);
20297 fast_find_string_pos (w, XINT (b), object,
20298 &dpyinfo->mouse_face_beg_col,
20299 &dpyinfo->mouse_face_beg_row,
20300 &dpyinfo->mouse_face_beg_x,
20301 &dpyinfo->mouse_face_beg_y, 0);
20302 fast_find_string_pos (w, XINT (e), object,
20303 &dpyinfo->mouse_face_end_col,
20304 &dpyinfo->mouse_face_end_row,
20305 &dpyinfo->mouse_face_end_x,
20306 &dpyinfo->mouse_face_end_y, 1);
20307 dpyinfo->mouse_face_past_end = 0;
20308 dpyinfo->mouse_face_window = window;
20309 dpyinfo->mouse_face_face_id
20310 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
20311 glyph->face_id, 1);
20312 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20313 cursor = No_Cursor;
20314 }
20315 else if (STRINGP (object) && NILP (mouse_face))
20316 {
20317 /* A string which doesn't have mouse-face, but
20318 the text ``under'' it might have. */
20319 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
20320 int start = MATRIX_ROW_START_CHARPOS (r);
20321
20322 pos = string_buffer_position (w, object, start);
20323 if (pos > 0)
20324 mouse_face = get_char_property_and_overlay (make_number (pos),
20325 Qmouse_face,
20326 w->buffer,
20327 &overlay);
20328 if (!NILP (mouse_face) && !NILP (overlay))
20329 {
20330 Lisp_Object before = Foverlay_start (overlay);
20331 Lisp_Object after = Foverlay_end (overlay);
20332 int ignore;
20333
20334 /* Note that we might not be able to find position
20335 BEFORE in the glyph matrix if the overlay is
20336 entirely covered by a `display' property. In
20337 this case, we overshoot. So let's stop in
20338 the glyph matrix before glyphs for OBJECT. */
20339 fast_find_position (w, XFASTINT (before),
20340 &dpyinfo->mouse_face_beg_col,
20341 &dpyinfo->mouse_face_beg_row,
20342 &dpyinfo->mouse_face_beg_x,
20343 &dpyinfo->mouse_face_beg_y,
20344 object);
20345
20346 dpyinfo->mouse_face_past_end
20347 = !fast_find_position (w, XFASTINT (after),
20348 &dpyinfo->mouse_face_end_col,
20349 &dpyinfo->mouse_face_end_row,
20350 &dpyinfo->mouse_face_end_x,
20351 &dpyinfo->mouse_face_end_y,
20352 Qnil);
20353 dpyinfo->mouse_face_window = window;
20354 dpyinfo->mouse_face_face_id
20355 = face_at_buffer_position (w, pos, 0, 0,
20356 &ignore, pos + 1,
20357 !dpyinfo->mouse_face_hidden);
20358
20359 /* Display it as active. */
20360 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20361 cursor = No_Cursor;
20362 }
20363 }
20364 }
20365
20366 check_help_echo:
20367
20368 /* Look for a `help-echo' property. */
20369 {
20370 Lisp_Object help, overlay;
20371
20372 /* Check overlays first. */
20373 help = overlay = Qnil;
20374 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
20375 {
20376 overlay = overlay_vec[i];
20377 help = Foverlay_get (overlay, Qhelp_echo);
20378 }
20379
20380 if (!NILP (help))
20381 {
20382 help_echo_string = help;
20383 help_echo_window = window;
20384 help_echo_object = overlay;
20385 help_echo_pos = pos;
20386 }
20387 else
20388 {
20389 Lisp_Object object = glyph->object;
20390 int charpos = glyph->charpos;
20391
20392 /* Try text properties. */
20393 if (STRINGP (object)
20394 && charpos >= 0
20395 && charpos < SCHARS (object))
20396 {
20397 help = Fget_text_property (make_number (charpos),
20398 Qhelp_echo, object);
20399 if (NILP (help))
20400 {
20401 /* If the string itself doesn't specify a help-echo,
20402 see if the buffer text ``under'' it does. */
20403 struct glyph_row *r
20404 = MATRIX_ROW (w->current_matrix, vpos);
20405 int start = MATRIX_ROW_START_CHARPOS (r);
20406 int pos = string_buffer_position (w, object, start);
20407 if (pos > 0)
20408 {
20409 help = Fget_char_property (make_number (pos),
20410 Qhelp_echo, w->buffer);
20411 if (!NILP (help))
20412 {
20413 charpos = pos;
20414 object = w->buffer;
20415 }
20416 }
20417 }
20418 }
20419 else if (BUFFERP (object)
20420 && charpos >= BEGV
20421 && charpos < ZV)
20422 help = Fget_text_property (make_number (charpos), Qhelp_echo,
20423 object);
20424
20425 if (!NILP (help))
20426 {
20427 help_echo_string = help;
20428 help_echo_window = window;
20429 help_echo_object = object;
20430 help_echo_pos = charpos;
20431 }
20432 }
20433 }
20434
20435 BEGV = obegv;
20436 ZV = ozv;
20437 current_buffer = obuf;
20438 }
20439
20440 set_cursor:
20441
365fa1b3 20442#ifndef HAVE_CARBON
fa3c6b4d 20443 if (cursor != No_Cursor)
365fa1b3
AC
20444#else
20445 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
20446#endif
fa3c6b4d
KS
20447 rif->define_frame_cursor (f, cursor);
20448}
20449
20450
20451/* EXPORT for RIF:
20452 Clear any mouse-face on window W. This function is part of the
20453 redisplay interface, and is called from try_window_id and similar
20454 functions to ensure the mouse-highlight is off. */
20455
20456void
20457x_clear_window_mouse_face (w)
20458 struct window *w;
20459{
20460 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20461 Lisp_Object window;
20462
20463 BLOCK_INPUT;
20464 XSETWINDOW (window, w);
20465 if (EQ (window, dpyinfo->mouse_face_window))
20466 clear_mouse_face (dpyinfo);
20467 UNBLOCK_INPUT;
20468}
20469
20470
20471/* EXPORT:
20472 Just discard the mouse face information for frame F, if any.
20473 This is used when the size of F is changed. */
20474
20475void
20476cancel_mouse_face (f)
20477 struct frame *f;
20478{
20479 Lisp_Object window;
20480 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20481
20482 window = dpyinfo->mouse_face_window;
20483 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
20484 {
20485 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20486 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20487 dpyinfo->mouse_face_window = Qnil;
20488 }
20489}
20490
20491
20492#endif /* HAVE_WINDOW_SYSTEM */
20493
20494\f
20495/***********************************************************************
20496 Exposure Events
20497 ***********************************************************************/
20498
20499#ifdef HAVE_WINDOW_SYSTEM
20500
20501/* Redraw the part of glyph row area AREA of glyph row ROW on window W
20502 which intersects rectangle R. R is in window-relative coordinates. */
20503
20504static void
20505expose_area (w, row, r, area)
20506 struct window *w;
20507 struct glyph_row *row;
20508 XRectangle *r;
20509 enum glyph_row_area area;
20510{
20511 struct glyph *first = row->glyphs[area];
20512 struct glyph *end = row->glyphs[area] + row->used[area];
20513 struct glyph *last;
20514 int first_x, start_x, x;
20515
20516 if (area == TEXT_AREA && row->fill_line_p)
20517 /* If row extends face to end of line write the whole line. */
20518 draw_glyphs (w, 0, row, area,
20519 0, row->used[area],
20520 DRAW_NORMAL_TEXT, 0);
20521 else
20522 {
20523 /* Set START_X to the window-relative start position for drawing glyphs of
20524 AREA. The first glyph of the text area can be partially visible.
20525 The first glyphs of other areas cannot. */
da8b7f4f 20526 start_x = window_box_left_offset (w, area);
fa3c6b4d 20527 x = start_x;
29b3ea63
KS
20528 if (area == TEXT_AREA)
20529 x += row->x;
fa3c6b4d
KS
20530
20531 /* Find the first glyph that must be redrawn. */
20532 while (first < end
20533 && x + first->pixel_width < r->x)
20534 {
20535 x += first->pixel_width;
20536 ++first;
20537 }
20538
20539 /* Find the last one. */
20540 last = first;
20541 first_x = x;
20542 while (last < end
20543 && x < r->x + r->width)
20544 {
20545 x += last->pixel_width;
20546 ++last;
20547 }
20548
20549 /* Repaint. */
20550 if (last > first)
20551 draw_glyphs (w, first_x - start_x, row, area,
20552 first - row->glyphs[area], last - row->glyphs[area],
20553 DRAW_NORMAL_TEXT, 0);
20554 }
20555}
20556
20557
20558/* Redraw the parts of the glyph row ROW on window W intersecting
20559 rectangle R. R is in window-relative coordinates. Value is
20560 non-zero if mouse-face was overwritten. */
20561
20562static int
20563expose_line (w, row, r)
20564 struct window *w;
20565 struct glyph_row *row;
20566 XRectangle *r;
20567{
20568 xassert (row->enabled_p);
20569
20570 if (row->mode_line_p || w->pseudo_window_p)
20571 draw_glyphs (w, 0, row, TEXT_AREA,
20572 0, row->used[TEXT_AREA],
20573 DRAW_NORMAL_TEXT, 0);
20574 else
20575 {
20576 if (row->used[LEFT_MARGIN_AREA])
20577 expose_area (w, row, r, LEFT_MARGIN_AREA);
20578 if (row->used[TEXT_AREA])
20579 expose_area (w, row, r, TEXT_AREA);
20580 if (row->used[RIGHT_MARGIN_AREA])
20581 expose_area (w, row, r, RIGHT_MARGIN_AREA);
20582 draw_row_fringe_bitmaps (w, row);
cfe03a41
KS
20583 }
20584
fa3c6b4d
KS
20585 return row->mouse_face_p;
20586}
20587
20588
20589/* Redraw those parts of glyphs rows during expose event handling that
20590 overlap other rows. Redrawing of an exposed line writes over parts
20591 of lines overlapping that exposed line; this function fixes that.
20592
20593 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
20594 row in W's current matrix that is exposed and overlaps other rows.
20595 LAST_OVERLAPPING_ROW is the last such row. */
20596
20597static void
20598expose_overlaps (w, first_overlapping_row, last_overlapping_row)
20599 struct window *w;
20600 struct glyph_row *first_overlapping_row;
20601 struct glyph_row *last_overlapping_row;
20602{
20603 struct glyph_row *row;
20604
20605 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
20606 if (row->overlapping_p)
20607 {
20608 xassert (row->enabled_p && !row->mode_line_p);
20609
20610 if (row->used[LEFT_MARGIN_AREA])
20611 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
cfe03a41 20612
fa3c6b4d
KS
20613 if (row->used[TEXT_AREA])
20614 x_fix_overlapping_area (w, row, TEXT_AREA);
20615
20616 if (row->used[RIGHT_MARGIN_AREA])
20617 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
20618 }
20619}
20620
20621
20622/* Return non-zero if W's cursor intersects rectangle R. */
20623
20624static int
20625phys_cursor_in_rect_p (w, r)
20626 struct window *w;
20627 XRectangle *r;
20628{
20629 XRectangle cr, result;
20630 struct glyph *cursor_glyph;
20631
20632 cursor_glyph = get_phys_cursor_glyph (w);
20633 if (cursor_glyph)
cfe03a41 20634 {
aecf50d2
KS
20635 /* r is relative to W's box, but w->phys_cursor.x is relative
20636 to left edge of W's TEXT area. Adjust it. */
20637 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
fa3c6b4d
KS
20638 cr.y = w->phys_cursor.y;
20639 cr.width = cursor_glyph->pixel_width;
20640 cr.height = w->phys_cursor_height;
20641 /* ++KFS: W32 version used W32-specific IntersectRect here, but
20642 I assume the effect is the same -- and this is portable. */
20643 return x_intersect_rectangles (&cr, r, &result);
cfe03a41 20644 }
fa3c6b4d
KS
20645 else
20646 return 0;
20647}
cfe03a41 20648
cfe03a41 20649
fa3c6b4d
KS
20650/* EXPORT:
20651 Draw a vertical window border to the right of window W if W doesn't
20652 have vertical scroll bars. */
cfe03a41 20653
cfe03a41 20654void
fa3c6b4d
KS
20655x_draw_vertical_border (w)
20656 struct window *w;
cfe03a41 20657{
da8b7f4f
KS
20658 /* We could do better, if we knew what type of scroll-bar the adjacent
20659 windows (on either side) have... But we don't :-(
20660 However, I think this works ok. ++KFS 2003-04-25 */
20661
fa3c6b4d
KS
20662 /* Redraw borders between horizontally adjacent windows. Don't
20663 do it for frames with vertical scroll bars because either the
20664 right scroll bar of a window, or the left scroll bar of its
20665 neighbor will suffice as a border. */
20666 if (!WINDOW_RIGHTMOST_P (w)
da8b7f4f 20667 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
fa3c6b4d
KS
20668 {
20669 int x0, x1, y0, y1;
cfe03a41 20670
fa3c6b4d 20671 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
fa3c6b4d 20672 y1 -= 1;
cfe03a41 20673
fa3c6b4d 20674 rif->draw_vertical_window_border (w, x1, y0, y1);
cfe03a41 20675 }
da8b7f4f
KS
20676 else if (!WINDOW_LEFTMOST_P (w)
20677 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
20678 {
20679 int x0, x1, y0, y1;
20680
20681 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
20682 y1 -= 1;
20683
20684 rif->draw_vertical_window_border (w, x0, y0, y1);
20685 }
cfe03a41
KS
20686}
20687
20688
fa3c6b4d
KS
20689/* Redraw the part of window W intersection rectangle FR. Pixel
20690 coordinates in FR are frame-relative. Call this function with
20691 input blocked. Value is non-zero if the exposure overwrites
20692 mouse-face. */
cfe03a41 20693
fa3c6b4d
KS
20694static int
20695expose_window (w, fr)
cfe03a41 20696 struct window *w;
fa3c6b4d 20697 XRectangle *fr;
cfe03a41
KS
20698{
20699 struct frame *f = XFRAME (w->frame);
fa3c6b4d
KS
20700 XRectangle wr, r;
20701 int mouse_face_overwritten_p = 0;
20702
20703 /* If window is not yet fully initialized, do nothing. This can
20704 happen when toolkit scroll bars are used and a window is split.
20705 Reconfiguring the scroll bar will generate an expose for a newly
20706 created window. */
20707 if (w->current_matrix == NULL)
20708 return 0;
cfe03a41 20709
fa3c6b4d
KS
20710 /* When we're currently updating the window, display and current
20711 matrix usually don't agree. Arrange for a thorough display
20712 later. */
20713 if (w == updated_window)
20714 {
20715 SET_FRAME_GARBAGED (f);
20716 return 0;
20717 }
e9c99027 20718
fa3c6b4d 20719 /* Frame-relative pixel rectangle of W. */
da8b7f4f
KS
20720 wr.x = WINDOW_LEFT_EDGE_X (w);
20721 wr.y = WINDOW_TOP_EDGE_Y (w);
20722 wr.width = WINDOW_TOTAL_WIDTH (w);
20723 wr.height = WINDOW_TOTAL_HEIGHT (w);
fa3c6b4d
KS
20724
20725 if (x_intersect_rectangles (fr, &wr, &r))
cfe03a41 20726 {
fa3c6b4d
KS
20727 int yb = window_text_bottom_y (w);
20728 struct glyph_row *row;
20729 int cursor_cleared_p;
20730 struct glyph_row *first_overlapping_row, *last_overlapping_row;
20731
20732 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
20733 r.x, r.y, r.width, r.height));
20734
20735 /* Convert to window coordinates. */
cc8549df
KS
20736 r.x -= WINDOW_LEFT_EDGE_X (w);
20737 r.y -= WINDOW_TOP_EDGE_Y (w);
fa3c6b4d
KS
20738
20739 /* Turn off the cursor. */
20740 if (!w->pseudo_window_p
20741 && phys_cursor_in_rect_p (w, &r))
cfe03a41 20742 {
fa3c6b4d
KS
20743 x_clear_cursor (w);
20744 cursor_cleared_p = 1;
cfe03a41 20745 }
fa3c6b4d
KS
20746 else
20747 cursor_cleared_p = 0;
cfe03a41 20748
fa3c6b4d
KS
20749 /* Update lines intersecting rectangle R. */
20750 first_overlapping_row = last_overlapping_row = NULL;
20751 for (row = w->current_matrix->rows;
20752 row->enabled_p;
20753 ++row)
20754 {
20755 int y0 = row->y;
20756 int y1 = MATRIX_ROW_BOTTOM_Y (row);
cfe03a41 20757
fa3c6b4d
KS
20758 if ((y0 >= r.y && y0 < r.y + r.height)
20759 || (y1 > r.y && y1 < r.y + r.height)
20760 || (r.y >= y0 && r.y < y1)
20761 || (r.y + r.height > y0 && r.y + r.height < y1))
20762 {
20763 if (row->overlapping_p)
20764 {
20765 if (first_overlapping_row == NULL)
20766 first_overlapping_row = row;
20767 last_overlapping_row = row;
20768 }
e9c99027 20769
fa3c6b4d
KS
20770 if (expose_line (w, row, &r))
20771 mouse_face_overwritten_p = 1;
20772 }
cfe03a41 20773
fa3c6b4d
KS
20774 if (y1 >= yb)
20775 break;
20776 }
cfe03a41 20777
fa3c6b4d
KS
20778 /* Display the mode line if there is one. */
20779 if (WINDOW_WANTS_MODELINE_P (w)
20780 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
20781 row->enabled_p)
20782 && row->y < r.y + r.height)
20783 {
20784 if (expose_line (w, row, &r))
20785 mouse_face_overwritten_p = 1;
20786 }
cfe03a41 20787
fa3c6b4d
KS
20788 if (!w->pseudo_window_p)
20789 {
20790 /* Fix the display of overlapping rows. */
20791 if (first_overlapping_row)
20792 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
20793
20794 /* Draw border between windows. */
20795 x_draw_vertical_border (w);
20796
20797 /* Turn the cursor on again. */
20798 if (cursor_cleared_p)
20799 update_window_cursor (w, 1);
20800 }
20801 }
20802
20803#ifdef HAVE_CARBON
20804 /* Display scroll bar for this window. */
20805 if (!NILP (w->vertical_scroll_bar))
cfe03a41 20806 {
fa3c6b4d
KS
20807 /* ++KFS:
20808 If this doesn't work here (maybe some header files are missing),
20809 make a function in macterm.c and call it to do the job! */
20810 ControlHandle ch
20811 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
20812
20813 Draw1Control (ch);
cfe03a41 20814 }
fa3c6b4d 20815#endif
cfe03a41 20816
fa3c6b4d
KS
20817 return mouse_face_overwritten_p;
20818}
20819
20820
20821
20822/* Redraw (parts) of all windows in the window tree rooted at W that
20823 intersect R. R contains frame pixel coordinates. Value is
20824 non-zero if the exposure overwrites mouse-face. */
20825
20826static int
20827expose_window_tree (w, r)
20828 struct window *w;
20829 XRectangle *r;
20830{
20831 struct frame *f = XFRAME (w->frame);
20832 int mouse_face_overwritten_p = 0;
20833
20834 while (w && !FRAME_GARBAGED_P (f))
cfe03a41 20835 {
fa3c6b4d
KS
20836 if (!NILP (w->hchild))
20837 mouse_face_overwritten_p
20838 |= expose_window_tree (XWINDOW (w->hchild), r);
20839 else if (!NILP (w->vchild))
20840 mouse_face_overwritten_p
20841 |= expose_window_tree (XWINDOW (w->vchild), r);
20842 else
20843 mouse_face_overwritten_p |= expose_window (w, r);
20844
20845 w = NILP (w->next) ? NULL : XWINDOW (w->next);
cfe03a41 20846 }
cfe03a41 20847
fa3c6b4d
KS
20848 return mouse_face_overwritten_p;
20849}
cfe03a41 20850
cfe03a41 20851
fa3c6b4d
KS
20852/* EXPORT:
20853 Redisplay an exposed area of frame F. X and Y are the upper-left
20854 corner of the exposed rectangle. W and H are width and height of
20855 the exposed area. All are pixel values. W or H zero means redraw
20856 the entire frame. */
cfe03a41 20857
fa3c6b4d
KS
20858void
20859expose_frame (f, x, y, w, h)
20860 struct frame *f;
20861 int x, y, w, h;
20862{
20863 XRectangle r;
20864 int mouse_face_overwritten_p = 0;
20865
20866 TRACE ((stderr, "expose_frame "));
20867
20868 /* No need to redraw if frame will be redrawn soon. */
20869 if (FRAME_GARBAGED_P (f))
cfe03a41 20870 {
fa3c6b4d
KS
20871 TRACE ((stderr, " garbaged\n"));
20872 return;
cfe03a41 20873 }
7d0393cf 20874
fa3c6b4d
KS
20875#ifdef HAVE_CARBON
20876 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
20877 or deactivated here, for unknown reasons, activated scroll bars
20878 are shown in deactivated frames in some instances. */
20879 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
20880 activate_scroll_bars (f);
20881 else
20882 deactivate_scroll_bars (f);
20883#endif
cfe03a41 20884
fa3c6b4d
KS
20885 /* If basic faces haven't been realized yet, there is no point in
20886 trying to redraw anything. This can happen when we get an expose
20887 event while Emacs is starting, e.g. by moving another window. */
20888 if (FRAME_FACE_CACHE (f) == NULL
20889 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
20890 {
20891 TRACE ((stderr, " no faces\n"));
20892 return;
20893 }
cfe03a41 20894
fa3c6b4d 20895 if (w == 0 || h == 0)
cfe03a41 20896 {
fa3c6b4d 20897 r.x = r.y = 0;
da8b7f4f
KS
20898 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
20899 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
fa3c6b4d
KS
20900 }
20901 else
20902 {
20903 r.x = x;
20904 r.y = y;
20905 r.width = w;
20906 r.height = h;
cfe03a41
KS
20907 }
20908
fa3c6b4d
KS
20909 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
20910 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
20911
20912 if (WINDOWP (f->tool_bar_window))
20913 mouse_face_overwritten_p
20914 |= expose_window (XWINDOW (f->tool_bar_window), &r);
20915
20916#ifdef HAVE_X_WINDOWS
20917#ifndef MSDOS
20918#ifndef USE_X_TOOLKIT
20919 if (WINDOWP (f->menu_bar_window))
20920 mouse_face_overwritten_p
20921 |= expose_window (XWINDOW (f->menu_bar_window), &r);
20922#endif /* not USE_X_TOOLKIT */
20923#endif
20924#endif
20925
20926 /* Some window managers support a focus-follows-mouse style with
20927 delayed raising of frames. Imagine a partially obscured frame,
20928 and moving the mouse into partially obscured mouse-face on that
20929 frame. The visible part of the mouse-face will be highlighted,
20930 then the WM raises the obscured frame. With at least one WM, KDE
20931 2.1, Emacs is not getting any event for the raising of the frame
20932 (even tried with SubstructureRedirectMask), only Expose events.
20933 These expose events will draw text normally, i.e. not
20934 highlighted. Which means we must redo the highlight here.
20935 Subsume it under ``we love X''. --gerd 2001-08-15 */
20936 /* Included in Windows version because Windows most likely does not
20937 do the right thing if any third party tool offers
20938 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
20939 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
20940 {
20941 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20942 if (f == dpyinfo->mouse_face_mouse_frame)
20943 {
20944 int x = dpyinfo->mouse_face_mouse_x;
20945 int y = dpyinfo->mouse_face_mouse_y;
20946 clear_mouse_face (dpyinfo);
20947 note_mouse_highlight (f, x, y);
20948 }
20949 }
cfe03a41
KS
20950}
20951
133c764e 20952
fa3c6b4d
KS
20953/* EXPORT:
20954 Determine the intersection of two rectangles R1 and R2. Return
20955 the intersection in *RESULT. Value is non-zero if RESULT is not
20956 empty. */
133c764e 20957
fa3c6b4d
KS
20958int
20959x_intersect_rectangles (r1, r2, result)
20960 XRectangle *r1, *r2, *result;
133c764e 20961{
fa3c6b4d
KS
20962 XRectangle *left, *right;
20963 XRectangle *upper, *lower;
20964 int intersection_p = 0;
20965
20966 /* Rearrange so that R1 is the left-most rectangle. */
20967 if (r1->x < r2->x)
20968 left = r1, right = r2;
20969 else
20970 left = r2, right = r1;
20971
20972 /* X0 of the intersection is right.x0, if this is inside R1,
20973 otherwise there is no intersection. */
20974 if (right->x <= left->x + left->width)
133c764e 20975 {
fa3c6b4d 20976 result->x = right->x;
133c764e 20977
fa3c6b4d
KS
20978 /* The right end of the intersection is the minimum of the
20979 the right ends of left and right. */
20980 result->width = (min (left->x + left->width, right->x + right->width)
20981 - result->x);
133c764e 20982
fa3c6b4d
KS
20983 /* Same game for Y. */
20984 if (r1->y < r2->y)
20985 upper = r1, lower = r2;
20986 else
20987 upper = r2, lower = r1;
133c764e 20988
fa3c6b4d
KS
20989 /* The upper end of the intersection is lower.y0, if this is inside
20990 of upper. Otherwise, there is no intersection. */
20991 if (lower->y <= upper->y + upper->height)
20992 {
20993 result->y = lower->y;
20994
20995 /* The lower end of the intersection is the minimum of the lower
20996 ends of upper and lower. */
20997 result->height = (min (lower->y + lower->height,
20998 upper->y + upper->height)
20999 - result->y);
21000 intersection_p = 1;
133c764e
KS
21001 }
21002 }
fa3c6b4d
KS
21003
21004 return intersection_p;
133c764e
KS
21005}
21006
fa3c6b4d
KS
21007#endif /* HAVE_WINDOW_SYSTEM */
21008
cfe03a41 21009\f
5f5c8ee5
GM
21010/***********************************************************************
21011 Initialization
21012 ***********************************************************************/
21013
a2889657
JB
21014void
21015syms_of_xdisp ()
21016{
c6e89d6c
GM
21017 Vwith_echo_area_save_vector = Qnil;
21018 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 21019
c6e89d6c
GM
21020 Vmessage_stack = Qnil;
21021 staticpro (&Vmessage_stack);
2311178e 21022
735c094c 21023 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 21024 staticpro (&Qinhibit_redisplay);
735c094c 21025
b14bc55e
RS
21026 message_dolog_marker1 = Fmake_marker ();
21027 staticpro (&message_dolog_marker1);
21028 message_dolog_marker2 = Fmake_marker ();
21029 staticpro (&message_dolog_marker2);
21030 message_dolog_marker3 = Fmake_marker ();
21031 staticpro (&message_dolog_marker3);
21032
5f5c8ee5 21033#if GLYPH_DEBUG
7d4cc828 21034 defsubr (&Sdump_frame_glyph_matrix);
5f5c8ee5
GM
21035 defsubr (&Sdump_glyph_matrix);
21036 defsubr (&Sdump_glyph_row);
e037b9ec 21037 defsubr (&Sdump_tool_bar_row);
62397849 21038 defsubr (&Strace_redisplay);
bf9249e3 21039 defsubr (&Strace_to_stderr);
5f5c8ee5 21040#endif
99a5de87 21041#ifdef HAVE_WINDOW_SYSTEM
57c28064 21042 defsubr (&Stool_bar_lines_needed);
99a5de87 21043#endif
8143e6ab 21044 defsubr (&Sformat_mode_line);
5f5c8ee5 21045
cf074754
RS
21046 staticpro (&Qmenu_bar_update_hook);
21047 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
21048
d46fb96a 21049 staticpro (&Qoverriding_terminal_local_map);
7079aefa 21050 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 21051
399164b4
KH
21052 staticpro (&Qoverriding_local_map);
21053 Qoverriding_local_map = intern ("overriding-local-map");
21054
75c43375
RS
21055 staticpro (&Qwindow_scroll_functions);
21056 Qwindow_scroll_functions = intern ("window-scroll-functions");
21057
e0bfbde6
RS
21058 staticpro (&Qredisplay_end_trigger_functions);
21059 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
2311178e 21060
2e54982e
RS
21061 staticpro (&Qinhibit_point_motion_hooks);
21062 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
21063
9499d71b
GM
21064 QCdata = intern (":data");
21065 staticpro (&QCdata);
5f5c8ee5 21066 Qdisplay = intern ("display");
f3751a65 21067 staticpro (&Qdisplay);
5f5c8ee5
GM
21068 Qspace_width = intern ("space-width");
21069 staticpro (&Qspace_width);
5f5c8ee5
GM
21070 Qraise = intern ("raise");
21071 staticpro (&Qraise);
21072 Qspace = intern ("space");
21073 staticpro (&Qspace);
f3751a65
GM
21074 Qmargin = intern ("margin");
21075 staticpro (&Qmargin);
5f5c8ee5 21076 Qleft_margin = intern ("left-margin");
f3751a65 21077 staticpro (&Qleft_margin);
5f5c8ee5 21078 Qright_margin = intern ("right-margin");
f3751a65 21079 staticpro (&Qright_margin);
5f5c8ee5
GM
21080 Qalign_to = intern ("align-to");
21081 staticpro (&Qalign_to);
21082 QCalign_to = intern (":align-to");
21083 staticpro (&QCalign_to);
5f5c8ee5
GM
21084 Qrelative_width = intern ("relative-width");
21085 staticpro (&Qrelative_width);
21086 QCrelative_width = intern (":relative-width");
21087 staticpro (&QCrelative_width);
21088 QCrelative_height = intern (":relative-height");
21089 staticpro (&QCrelative_height);
21090 QCeval = intern (":eval");
21091 staticpro (&QCeval);
0fcf414f
RS
21092 QCpropertize = intern (":propertize");
21093 staticpro (&QCpropertize);
886bd6f2
GM
21094 QCfile = intern (":file");
21095 staticpro (&QCfile);
5f5c8ee5
GM
21096 Qfontified = intern ("fontified");
21097 staticpro (&Qfontified);
21098 Qfontification_functions = intern ("fontification-functions");
21099 staticpro (&Qfontification_functions);
5f5c8ee5
GM
21100 Qtrailing_whitespace = intern ("trailing-whitespace");
21101 staticpro (&Qtrailing_whitespace);
21102 Qimage = intern ("image");
21103 staticpro (&Qimage);
ad4f174e
GM
21104 Qmessage_truncate_lines = intern ("message-truncate-lines");
21105 staticpro (&Qmessage_truncate_lines);
af79bccb
RS
21106 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
21107 staticpro (&Qcursor_in_non_selected_windows);
6422c1d7
GM
21108 Qgrow_only = intern ("grow-only");
21109 staticpro (&Qgrow_only);
e1477f43
GM
21110 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
21111 staticpro (&Qinhibit_menubar_update);
30a3f61c
GM
21112 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
21113 staticpro (&Qinhibit_eval_during_redisplay);
b384d6f8
GM
21114 Qposition = intern ("position");
21115 staticpro (&Qposition);
21116 Qbuffer_position = intern ("buffer-position");
21117 staticpro (&Qbuffer_position);
21118 Qobject = intern ("object");
21119 staticpro (&Qobject);
cfe03a41
KS
21120 Qbar = intern ("bar");
21121 staticpro (&Qbar);
21122 Qhbar = intern ("hbar");
21123 staticpro (&Qhbar);
21124 Qbox = intern ("box");
21125 staticpro (&Qbox);
21126 Qhollow = intern ("hollow");
21127 staticpro (&Qhollow);
c53a1624
RS
21128 Qrisky_local_variable = intern ("risky-local-variable");
21129 staticpro (&Qrisky_local_variable);
26683087
RS
21130 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
21131 staticpro (&Qinhibit_free_realized_faces);
5f5c8ee5 21132
7033d6df
RS
21133 list_of_error = Fcons (intern ("error"), Qnil);
21134 staticpro (&list_of_error);
21135
a2889657
JB
21136 last_arrow_position = Qnil;
21137 last_arrow_string = Qnil;
f3751a65
GM
21138 staticpro (&last_arrow_position);
21139 staticpro (&last_arrow_string);
2311178e 21140
c6e89d6c
GM
21141 echo_buffer[0] = echo_buffer[1] = Qnil;
21142 staticpro (&echo_buffer[0]);
21143 staticpro (&echo_buffer[1]);
21144
21145 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
21146 staticpro (&echo_area_buffer[0]);
21147 staticpro (&echo_area_buffer[1]);
a2889657 21148
6a94510a
GM
21149 Vmessages_buffer_name = build_string ("*Messages*");
21150 staticpro (&Vmessages_buffer_name);
0fcf414f
RS
21151
21152 mode_line_proptrans_alist = Qnil;
21153 staticpro (&mode_line_proptrans_alist);
2311178e 21154
fec8f23e
KS
21155 mode_line_string_list = Qnil;
21156 staticpro (&mode_line_string_list);
21157
fa3c6b4d
KS
21158 help_echo_string = Qnil;
21159 staticpro (&help_echo_string);
21160 help_echo_object = Qnil;
21161 staticpro (&help_echo_object);
21162 help_echo_window = Qnil;
21163 staticpro (&help_echo_window);
21164 previous_help_echo_string = Qnil;
21165 staticpro (&previous_help_echo_string);
21166 help_echo_pos = -1;
21167
21168#ifdef HAVE_WINDOW_SYSTEM
21169 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
21170 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
21171For example, if a block cursor is over a tab, it will be drawn as
21172wide as that tab on the display. */);
21173 x_stretch_cursor_p = 0;
21174#endif
21175
7ee72033
MB
21176 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
21177 doc: /* Non-nil means highlight trailing whitespace.
228299fa 21178The face used for trailing whitespace is `trailing-whitespace'. */);
8f897821
GM
21179 Vshow_trailing_whitespace = Qnil;
21180
f65536fa
KS
21181 DEFVAR_LISP ("show-text-cursor-in-void", &Vshow_text_cursor_in_void,
21182 doc: /* Non-nil means show the text cursor in void text areas.
21183The default is to show the non-text (typically arrow) cursor. */);
21184 Vshow_text_cursor_in_void = Qnil;
21185
7ee72033
MB
21186 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
21187 doc: /* Non-nil means don't actually do any redisplay.
228299fa 21188This is used for internal purposes. */);
735c094c
KH
21189 Vinhibit_redisplay = Qnil;
21190
7ee72033
MB
21191 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
21192 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
a2889657
JB
21193 Vglobal_mode_string = Qnil;
21194
7ee72033
MB
21195 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
21196 doc: /* Marker for where to display an arrow on top of the buffer text.
228299fa
GM
21197This must be the beginning of a line in order to work.
21198See also `overlay-arrow-string'. */);
a2889657
JB
21199 Voverlay_arrow_position = Qnil;
21200
7ee72033
MB
21201 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
21202 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
a2889657
JB
21203 Voverlay_arrow_string = Qnil;
21204
7ee72033
MB
21205 DEFVAR_INT ("scroll-step", &scroll_step,
21206 doc: /* *The number of lines to try scrolling a window by when point moves out.
228299fa
GM
21207If that fails to bring point back on frame, point is centered instead.
21208If this is zero, point is always centered after it moves off frame.
21209If you want scrolling to always be a line at a time, you should set
21210`scroll-conservatively' to a large value rather than set this to 1. */);
21211
7ee72033
MB
21212 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
21213 doc: /* *Scroll up to this many lines, to bring point back on screen.
228299fa
GM
21214A value of zero means to scroll the text to center point vertically
21215in the window. */);
0789adb2
RS
21216 scroll_conservatively = 0;
21217
7ee72033
MB
21218 DEFVAR_INT ("scroll-margin", &scroll_margin,
21219 doc: /* *Number of lines of margin at the top and bottom of a window.
228299fa
GM
21220Recenter the window whenever point gets within this many lines
21221of the top or bottom of the window. */);
9afd2168
RS
21222 scroll_margin = 0;
21223
f65536fa
KS
21224 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
21225 doc: /* Pixels per inch on current display.
21226Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
21227 Vdisplay_pixels_per_inch = make_float (72.0);
21228
5f5c8ee5 21229#if GLYPH_DEBUG
7ee72033 21230 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
5f5c8ee5 21231#endif
a2889657
JB
21232
21233 DEFVAR_BOOL ("truncate-partial-width-windows",
7ee72033
MB
21234 &truncate_partial_width_windows,
21235 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
a2889657
JB
21236 truncate_partial_width_windows = 1;
21237
7ee72033
MB
21238 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
21239 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
228299fa 21240Any other value means to use the appropriate face, `mode-line',
ccfe8f57 21241`header-line', or `menu' respectively. */);
1862a24e 21242 mode_line_inverse_video = 1;
aa6d10fa 21243
7ee72033
MB
21244 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
21245 doc: /* *Maximum buffer size for which line number should be displayed.
228299fa
GM
21246If the buffer is bigger than this, the line number does not appear
21247in the mode line. A value of nil means no limit. */);
090703f4 21248 Vline_number_display_limit = Qnil;
fba9ce76 21249
090703f4 21250 DEFVAR_INT ("line-number-display-limit-width",
7ee72033
MB
21251 &line_number_display_limit_width,
21252 doc: /* *Maximum line width (in characters) for line number display.
228299fa
GM
21253If the average length of the lines near point is bigger than this, then the
21254line number may be omitted from the mode line. */);
5d121aec
KH
21255 line_number_display_limit_width = 200;
21256
7ee72033
MB
21257 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
21258 doc: /* *Non-nil means highlight region even in nonselected windows. */);
293a54ce 21259 highlight_nonselected_windows = 0;
d39b6696 21260
7ee72033
MB
21261 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
21262 doc: /* Non-nil if more than one frame is visible on this display.
228299fa
GM
21263Minibuffer-only frames don't count, but iconified frames do.
21264This variable is not guaranteed to be accurate except while processing
21265`frame-title-format' and `icon-title-format'. */);
21266
7ee72033
MB
21267 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
21268 doc: /* Template for displaying the title bar of visible frames.
228299fa
GM
21269\(Assuming the window manager supports this feature.)
21270This variable has the same structure as `mode-line-format' (which see),
21271and is used only on frames for which no explicit name has been set
21272\(see `modify-frame-parameters'). */);
f65536fa 21273
7ee72033
MB
21274 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
21275 doc: /* Template for displaying the title bar of an iconified frame.
228299fa
GM
21276\(Assuming the window manager supports this feature.)
21277This variable has the same structure as `mode-line-format' (which see),
21278and is used only on frames for which no explicit name has been set
21279\(see `modify-frame-parameters'). */);
d39b6696
KH
21280 Vicon_title_format
21281 = Vframe_title_format
21282 = Fcons (intern ("multiple-frames"),
21283 Fcons (build_string ("%b"),
3ebf0ea9 21284 Fcons (Fcons (empty_string,
d39b6696
KH
21285 Fcons (intern ("invocation-name"),
21286 Fcons (build_string ("@"),
21287 Fcons (intern ("system-name"),
21288 Qnil)))),
21289 Qnil)));
5992c4f7 21290
7ee72033
MB
21291 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
21292 doc: /* Maximum number of lines to keep in the message log buffer.
228299fa
GM
21293If nil, disable message logging. If t, log messages but don't truncate
21294the buffer when it becomes large. */);
ac90c44f 21295 Vmessage_log_max = make_number (50);
08b610e4 21296
7ee72033
MB
21297 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
21298 doc: /* Functions called before redisplay, if window sizes have changed.
228299fa
GM
21299The value should be a list of functions that take one argument.
21300Just before redisplay, for each frame, if any of its windows have changed
21301size since the last redisplay, or have been split or deleted,
21302all the functions in the list are called, with the frame as argument. */);
08b610e4 21303 Vwindow_size_change_functions = Qnil;
75c43375 21304
7ee72033
MB
21305 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
21306 doc: /* List of Functions to call before redisplaying a window with scrolling.
228299fa
GM
21307Each function is called with two arguments, the window
21308and its new display-start position. Note that the value of `window-end'
21309is not valid when these functions are called. */);
75c43375 21310 Vwindow_scroll_functions = Qnil;
2311178e 21311
fa3c6b4d
KS
21312 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
21313 doc: /* *Non-nil means autoselect window with mouse pointer. */);
21314 mouse_autoselect_window = 0;
21315
7ee72033
MB
21316 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
21317 doc: /* *Non-nil means automatically resize tool-bars.
228299fa
GM
21318This increases a tool-bar's height if not all tool-bar items are visible.
21319It decreases a tool-bar's height when it would display blank lines
21320otherwise. */);
e037b9ec 21321 auto_resize_tool_bars_p = 1;
2311178e 21322
7ee72033
MB
21323 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
21324 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
e037b9ec 21325 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 21326
7ee72033
MB
21327 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
21328 doc: /* *Margin around tool-bar buttons in pixels.
228299fa 21329If an integer, use that for both horizontal and vertical margins.
f6c89f27 21330Otherwise, value should be a pair of integers `(HORZ . VERT)' with
228299fa
GM
21331HORZ specifying the horizontal margin, and VERT specifying the
21332vertical margin. */);
c3d76173 21333 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
5f5c8ee5 21334
7ee72033 21335 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
6da3c85b 21336 doc: /* *Relief thickness of tool-bar buttons. */);
c3d76173 21337 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
5f5c8ee5 21338
7ee72033
MB
21339 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
21340 doc: /* List of functions to call to fontify regions of text.
228299fa
GM
21341Each function is called with one argument POS. Functions must
21342fontify a region starting at POS in the current buffer, and give
21343fontified regions the property `fontified'. */);
5f5c8ee5 21344 Vfontification_functions = Qnil;
6b9f0906 21345 Fmake_variable_buffer_local (Qfontification_functions);
7bbe686f
AI
21346
21347 DEFVAR_BOOL ("unibyte-display-via-language-environment",
7ee72033
MB
21348 &unibyte_display_via_language_environment,
21349 doc: /* *Non-nil means display unibyte text according to language environment.
228299fa
GM
21350Specifically this means that unibyte non-ASCII characters
21351are displayed by converting them to the equivalent multibyte characters
21352according to the current language environment. As a result, they are
21353displayed according to the current fontset. */);
7bbe686f 21354 unibyte_display_via_language_environment = 0;
c6e89d6c 21355
7ee72033
MB
21356 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
21357 doc: /* *Maximum height for resizing mini-windows.
228299fa
GM
21358If a float, it specifies a fraction of the mini-window frame's height.
21359If an integer, it specifies a number of lines. */);
c6e89d6c 21360 Vmax_mini_window_height = make_float (0.25);
6422c1d7 21361
7ee72033
MB
21362 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
21363 doc: /* *How to resize mini-windows.
228299fa
GM
21364A value of nil means don't automatically resize mini-windows.
21365A value of t means resize them to fit the text displayed in them.
21366A value of `grow-only', the default, means let mini-windows grow
21367only, until their display becomes empty, at which point the windows
21368go back to their normal size. */);
6422c1d7
GM
21369 Vresize_mini_windows = Qgrow_only;
21370
cfe03a41
KS
21371 DEFVAR_LISP ("cursor-in-non-selected-windows",
21372 &Vcursor_in_non_selected_windows,
21373 doc: /* *Cursor type to display in non-selected windows.
21374t means to use hollow box cursor. See `cursor-type' for other values. */);
21375 Vcursor_in_non_selected_windows = Qt;
21376
cfe03a41
KS
21377 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
21378 doc: /* Alist specifying how to blink the cursor off.
21379Each element has the form (ON-STATE . OFF-STATE). Whenever the
21380`cursor-type' frame-parameter or variable equals ON-STATE,
21381comparing using `equal', Emacs uses OFF-STATE to specify
21382how to blink it off. */);
21383 Vblink_cursor_alist = Qnil;
2311178e 21384
e76d28d5 21385 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
7ee72033 21386 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
d475bcb8 21387 automatic_hscrolling_p = 1;
1df7e8f0 21388
e76d28d5 21389 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
1df7e8f0
EZ
21390 doc: /* *How many columns away from the window edge point is allowed to get
21391before automatic hscrolling will horizontally scroll the window. */);
e76d28d5 21392 hscroll_margin = 5;
1df7e8f0 21393
e76d28d5 21394 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
1df7e8f0
EZ
21395 doc: /* *How many columns to scroll the window when point gets too close to the edge.
21396When point is less than `automatic-hscroll-margin' columns from the window
21397edge, automatic hscrolling will scroll the window by the amount of columns
21398determined by this variable. If its value is a positive integer, scroll that
21399many columns. If it's a positive floating-point number, it specifies the
21400fraction of the window's width to scroll. If it's nil or zero, point will be
21401centered horizontally after the scroll. Any other value, including negative
21402numbers, are treated as if the value were zero.
21403
21404Automatic hscrolling always moves point outside the scroll margin, so if
21405point was more than scroll step columns inside the margin, the window will
21406scroll more than the value given by the scroll step.
21407
21408Note that the lower bound for automatic hscrolling specified by `scroll-left'
21409and `scroll-right' overrides this variable's effect. */);
e76d28d5 21410 Vhscroll_step = make_number (0);
2311178e 21411
7ee72033
MB
21412 DEFVAR_LISP ("image-types", &Vimage_types,
21413 doc: /* List of supported image types.
228299fa 21414Each element of the list is a symbol for a supported image type. */);
e00daaa0 21415 Vimage_types = Qnil;
2311178e 21416
7ee72033
MB
21417 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
21418 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
228299fa 21419Bind this around calls to `message' to let it take effect. */);
ad4f174e 21420 message_truncate_lines = 0;
0bca8940 21421
7ee72033
MB
21422 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
21423 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
228299fa 21424Can be used to update submenus whose contents should vary. */);
6422c1d7 21425 Vmenu_bar_update_hook = Qnil;
2311178e 21426
7ee72033
MB
21427 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
21428 doc: /* Non-nil means don't update menu bars. Internal use only. */);
e1477f43 21429 inhibit_menubar_update = 0;
30a3f61c 21430
7ee72033
MB
21431 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
21432 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30a3f61c 21433 inhibit_eval_during_redisplay = 0;
76cb5e06 21434
26683087
RS
21435 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
21436 doc: /* Non-nil means don't free realized faces. Internal use only. */);
21437 inhibit_free_realized_faces = 0;
21438
69d1f7c9 21439#if GLYPH_DEBUG
76cb5e06
GM
21440 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
21441 doc: /* Inhibit try_window_id display optimization. */);
21442 inhibit_try_window_id = 0;
21443
21444 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
21445 doc: /* Inhibit try_window_reusing display optimization. */);
21446 inhibit_try_window_reusing = 0;
21447
21448 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
21449 doc: /* Inhibit try_cursor_movement display optimization. */);
21450 inhibit_try_cursor_movement = 0;
21451#endif /* GLYPH_DEBUG */
a2889657
JB
21452}
21453
5f5c8ee5
GM
21454
21455/* Initialize this module when Emacs starts. */
21456
dfcf069d 21457void
a2889657
JB
21458init_xdisp ()
21459{
21460 Lisp_Object root_window;
5f5c8ee5 21461 struct window *mini_w;
a2889657 21462
04612a64
GM
21463 current_header_line_height = current_mode_line_height = -1;
21464
5f5c8ee5 21465 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
21466
21467 mini_w = XWINDOW (minibuf_window);
11e82b76 21468 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 21469
a2889657
JB
21470 if (!noninteractive)
21471 {
5f5c8ee5
GM
21472 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
21473 int i;
21474
da8b7f4f 21475 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
12c226c5 21476 set_window_height (root_window,
da8b7f4f 21477 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 21478 0);
da8b7f4f 21479 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
a2889657
JB
21480 set_window_height (minibuf_window, 1, 0);
21481
da8b7f4f
KS
21482 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
21483 mini_w->total_cols = make_number (FRAME_COLS (f));
5f5c8ee5
GM
21484
21485 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
21486 scratch_glyph_row.glyphs[TEXT_AREA + 1]
21487 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
21488
2311178e 21489 /* The default ellipsis glyphs `...'. */
5f5c8ee5 21490 for (i = 0; i < 3; ++i)
ac90c44f 21491 default_invis_vector[i] = make_number ('.');
a2889657 21492 }
5f5c8ee5 21493
5f5c8ee5 21494 {
93da8435
SM
21495 /* Allocate the buffer for frame titles.
21496 Also used for `format-mode-line'. */
5f5c8ee5
GM
21497 int size = 100;
21498 frame_title_buf = (char *) xmalloc (size);
21499 frame_title_buf_end = frame_title_buf + size;
21500 frame_title_ptr = NULL;
21501 }
2311178e 21502
21fdfb65 21503 help_echo_showing_p = 0;
a2889657 21504}
5f5c8ee5
GM
21505
21506
ab5796a9
MB
21507/* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
21508 (do not change this comment) */